https://gcc.gnu.org/g:e584f5b0d320a198c193ed6e1768960a49014f99

commit e584f5b0d320a198c193ed6e1768960a49014f99
Author: Mikael Morin <mik...@gcc.gnu.org>
Date:   Sat Sep 6 16:34:11 2025 +0200

    Introduction gfc_conv_descriptor_extent_get

Diff:
---
 gcc/fortran/trans-array.cc      | 20 +++-----------------
 gcc/fortran/trans-decl.cc       |  6 +-----
 gcc/fortran/trans-descriptor.cc | 34 +++++++++++++++++-----------------
 gcc/fortran/trans-descriptor.h  |  1 +
 gcc/fortran/trans-expr.cc       |  9 ++-------
 gcc/fortran/trans-intrinsic.cc  | 30 ++++++++----------------------
 gcc/fortran/trans-openmp.cc     | 34 +++++-----------------------------
 gcc/fortran/trans-stmt.cc       | 10 ++++------
 8 files changed, 41 insertions(+), 103 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 885f7440f3a2..92680c4b0fb0 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -7972,13 +7972,8 @@ gfc_tree_array_size (stmtblock_t *block, tree desc, 
gfc_expr *expr, tree dim)
     {
       if (!dim)
        dim = gfc_index_zero_node;
-      tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
-      tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
 
-      size = fold_build2_loc (input_location, MINUS_EXPR,
-                             gfc_array_index_type, ubound, lbound);
-      size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                             size, gfc_index_one_node);
+      size = gfc_conv_descriptor_extent_get (desc, dim);
       /* if (!allocatable && !pointer && assumed rank)
           size = (idx == rank && ubound[rank-1] == -1 ? -1 : size;
         else
@@ -8039,11 +8034,7 @@ gfc_tree_array_size (stmtblock_t *block, tree desc, 
gfc_expr *expr, tree dim)
       cond = fold_build2_loc (input_location, TRUTH_AND_EXPR, 
boolean_type_node,
                              cond, tmp);
     }
-  tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                        gfc_conv_descriptor_ubound_get (desc, idx),
-                        gfc_conv_descriptor_lbound_get (desc, idx));
-  tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                        tmp, gfc_index_one_node);
+  tmp = gfc_conv_descriptor_extent_get (desc, idx);
   gfc_add_modify (&cond_block, extent, tmp);
   tmp = fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
                         extent, gfc_index_zero_node);
@@ -8610,12 +8601,7 @@ gfc_full_array_size (stmtblock_t *block, tree decl, int 
rank)
     idx = gfc_conv_descriptor_rank_get (decl);
   else
     idx = gfc_rank_cst[rank - 1];
-  nelems = gfc_conv_descriptor_ubound_get (decl, idx);
-  tmp = gfc_conv_descriptor_lbound_get (decl, idx);
-  tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                        nelems, tmp);
-  tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                        tmp, gfc_index_one_node);
+  tmp = gfc_conv_descriptor_extent_get (decl, idx);
   tmp = gfc_evaluate_now (tmp, block);
 
   nelems = gfc_conv_descriptor_stride_get (decl, idx);
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 9f5200f34d33..64ca5d76e6c8 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -7621,11 +7621,7 @@ done:
   gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx),
                  gfc_conv_descriptor_lbound_get (gfc_desc, idx));
   /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
-  tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                            gfc_conv_descriptor_ubound_get (gfc_desc, idx),
-                            gfc_conv_descriptor_lbound_get (gfc_desc, idx));
-  tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, tmp,
-                        gfc_index_one_node);
+  tmp = gfc_conv_descriptor_extent_get (gfc_desc, idx);
   gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
   /* d->dim[n].sm = gfc->dim[i].stride  * gfc->span); */
   tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 9b296f17ed14..5a5f57835ea5 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -594,6 +594,20 @@ gfc_conv_descriptor_sm_get (tree desc, tree dim)
                          gfc_conv_descriptor_span_get (desc));
 }
 
+
+tree
+gfc_conv_descriptor_extent_get (tree desc, tree dim)
+{
+  tree lbound = gfc_conv_descriptor_lbound_get (desc, dim);
+  tree ubound = gfc_conv_descriptor_ubound_get (desc, dim);
+
+  tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
+                             gfc_index_one_node, lbound);
+  return fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
+                         ubound, tmp);
+}
+
+
 
/*******************************************************************************
  * Array descriptor higher level routines.                                     
*
  
******************************************************************************/
@@ -1044,14 +1058,7 @@ gfc_conv_descriptor_size_1 (tree desc, int from_dim, int 
to_dim)
 
   for (dim = from_dim; dim < to_dim; ++dim)
     {
-      tree lbound;
-      tree ubound;
-      tree extent;
-
-      lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
-      ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
-
-      extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+      tree extent = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[dim]);
       res = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
                             res, extent);
     }
@@ -2830,15 +2837,8 @@ gfc_descriptor_init_count (tree descriptor, int rank, 
int corank,
                 start at zero, but when allocating it, the standard expects
                 the array to start at one.  Therefore fix the upper bound to be
                 (desc.ubound - desc.lbound) + 1.  */
-             tmp = fold_build2_loc (input_location, MINUS_EXPR,
-                                    gfc_array_index_type,
-                                    gfc_conv_descriptor_ubound_get (
-                                      expr3_desc, gfc_rank_cst[n]),
-                                    gfc_conv_descriptor_lbound_get (
-                                      expr3_desc, gfc_rank_cst[n]));
-             tmp = fold_build2_loc (input_location, PLUS_EXPR,
-                                    gfc_array_index_type, tmp,
-                                    gfc_index_one_node);
+             tmp = gfc_conv_descriptor_extent_get (expr3_desc,
+                                                   gfc_rank_cst[n]);
              se.expr = gfc_evaluate_now (tmp, pblock);
            }
          else
diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h
index 52ac158993fb..52762c3c94a4 100644
--- a/gcc/fortran/trans-descriptor.h
+++ b/gcc/fortran/trans-descriptor.h
@@ -58,6 +58,7 @@ tree gfc_conv_descriptor_stride_get (tree desc, tree dim);
 tree gfc_conv_descriptor_lbound_get (tree desc, tree dim);
 tree gfc_conv_descriptor_ubound_get (tree desc, tree dim);
 tree gfc_conv_descriptor_sm_get (tree desc, tree dim);
+tree gfc_conv_descriptor_extent_get (tree desc, tree dim);
 
 void gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value);
 void gfc_conv_descriptor_data_set (stmtblock_t *block, tree desc, tree value);
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 50d260cdd0ef..96b8f62ad692 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -2540,8 +2540,7 @@ gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, 
tree desc)
                                   TREE_TYPE (tmp), img_idx, tmp);
        if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
          {
-           ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
-           tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+           tmp = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[i]);
            extent = fold_build2_loc (input_location, MULT_EXPR,
                                      TREE_TYPE (tmp), extent, tmp);
          }
@@ -6164,11 +6163,7 @@ gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr 
*e, gfc_symbol *fsym)
        tmp = gfc_index_zero_node;
       gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
       /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
-      tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                            gfc_conv_descriptor_ubound_get (gfc, idx),
-                            gfc_conv_descriptor_lbound_get (gfc, idx));
-      tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                            tmp, gfc_index_one_node);
+      tmp = gfc_conv_descriptor_extent_get (gfc, idx);
       gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
       /* d->dim[n].sm = gfc->dim[i].stride * gfc->span); */
       tmp = gfc_conv_descriptor_sm_get (gfc, idx);
diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc
index e02f2c307daf..07b6cd7ccb36 100644
--- a/gcc/fortran/trans-intrinsic.cc
+++ b/gcc/fortran/trans-intrinsic.cc
@@ -1825,7 +1825,7 @@ trans_this_image (gfc_se * se, gfc_expr *expr)
 {
   stmtblock_t loop;
   tree type, desc, dim_arg, cond, tmp, m, loop_var, exit_label, min_var, 
lbound,
-    ubound, extent, ml, team;
+      extent, ml, team;
   gfc_se argse;
   int rank, corank;
 
@@ -1991,9 +1991,7 @@ trans_this_image (gfc_se * se, gfc_expr *expr)
   gfc_add_modify (&loop, ml, m);
 
   /* extent = ...  */
-  lbound = gfc_conv_descriptor_lbound_get (desc, loop_var);
-  ubound = gfc_conv_descriptor_ubound_get (desc, loop_var);
-  extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+  extent = gfc_conv_descriptor_extent_get (desc, loop_var);
   extent = fold_convert (type, extent);
 
   /* m = m/extent.  */
@@ -2200,12 +2198,10 @@ trans_image_index (gfc_se * se, gfc_expr *expr)
 
   for (codim = corank + rank - 2; codim >= rank; codim--)
     {
-      tree extent, ubound;
+      tree extent;
 
       /* coindex = coindex*extent(codim) + sub(codim) - lcobound(codim).  */
-      lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[codim]);
-      ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[codim]);
-      extent = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+      extent = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[codim]);
 
       /* coindex *= extent.  */
       coindex = fold_build2_loc (input_location, MULT_EXPR,
@@ -2357,13 +2353,7 @@ gfc_conv_is_contiguous_expr (gfc_se *se, gfc_expr *arg)
 
       for (i = 0; i < arg->rank - 1; i++)
        {
-         tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
-         extent = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
-         extent = fold_build2_loc (input_location, MINUS_EXPR,
-                                   gfc_array_index_type, extent, tmp);
-         extent = fold_build2_loc (input_location, PLUS_EXPR,
-                                   gfc_array_index_type, extent,
-                                   gfc_index_one_node);
+         extent = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[i]);
          tmp = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[i]);
          tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
                                 tmp, extent);
@@ -2483,10 +2473,7 @@ gfc_conv_intrinsic_bound (gfc_se * se, gfc_expr * expr, 
enum gfc_isym_id op)
 
   ubound = gfc_conv_descriptor_ubound_get (desc, bound);
   lbound = gfc_conv_descriptor_lbound_get (desc, bound);
-  size = fold_build2_loc (input_location, MINUS_EXPR,
-                         gfc_array_index_type, ubound, lbound);
-  size = fold_build2_loc (input_location, PLUS_EXPR,
-                         gfc_array_index_type, size, gfc_index_one_node);
+  size = gfc_conv_descriptor_extent_get (desc, bound);
 
   /* 13.14.53: Result value for LBOUND
 
@@ -12711,7 +12698,7 @@ conv_intrinsic_event_query (gfc_code *code)
       /* For arrays, obtain the array index.  */
       if (gfc_expr_attr (event_expr).dimension)
        {
-         tree desc, tmp, extent, lbound, ubound;
+         tree desc, tmp, extent, lbound;
           gfc_array_ref *ar, ar2;
           int i;
 
@@ -12744,8 +12731,7 @@ conv_intrinsic_event_query (gfc_code *code)
                                       TREE_TYPE (tmp), index, tmp);
              if (i < ar->dimen - 1)
                {
-                 ubound = gfc_conv_descriptor_ubound_get (desc, 
gfc_rank_cst[i]);
-                 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+                 tmp = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[i]);
                  extent = fold_build2_loc (input_location, MULT_EXPR,
                                            TREE_TYPE (tmp), extent, tmp);
                }
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 0b5ea14629a9..07e3649914fb 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -829,12 +829,7 @@ gfc_omp_clause_default_ctor (tree clause, tree decl, tree 
outer)
     {
       gfc_add_modify (&cond_block, decl, outer);
       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
-      size = gfc_conv_descriptor_ubound_get (decl, rank);
-      size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                             size,
-                             gfc_conv_descriptor_lbound_get (decl, rank));
-      size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                             size, gfc_index_one_node);
+      size = gfc_conv_descriptor_extent_get (decl, rank);
       if (GFC_TYPE_ARRAY_RANK (type) > 1)
        size = fold_build2_loc (input_location, MULT_EXPR,
                                gfc_array_index_type, size,
@@ -1020,12 +1015,7 @@ gfc_omp_clause_copy_ctor (tree clause, tree dest, tree 
src)
   if (GFC_DESCRIPTOR_TYPE_P (type))
     {
       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
-      size = gfc_conv_descriptor_ubound_get (dest, rank);
-      size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                             size,
-                             gfc_conv_descriptor_lbound_get (dest, rank));
-      size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                             size, gfc_index_one_node);
+      size = gfc_conv_descriptor_extent_get (dest, rank);
       if (GFC_TYPE_ARRAY_RANK (type) > 1)
        size = fold_build2_loc (input_location, MULT_EXPR,
                                gfc_array_index_type, size,
@@ -1143,12 +1133,7 @@ gfc_omp_clause_assign_op (tree clause, tree dest, tree 
src)
   if (GFC_DESCRIPTOR_TYPE_P (type))
     {
       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
-      size = gfc_conv_descriptor_ubound_get (src, rank);
-      size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                             size,
-                             gfc_conv_descriptor_lbound_get (src, rank));
-      size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                             size, gfc_index_one_node);
+      size = gfc_conv_descriptor_extent_get (src, rank);
       if (GFC_TYPE_ARRAY_RANK (type) > 1)
        size = fold_build2_loc (input_location, MULT_EXPR,
                                gfc_array_index_type, size,
@@ -1374,12 +1359,7 @@ gfc_omp_clause_linear_ctor (tree clause, tree dest, tree 
src, tree add)
   if (GFC_DESCRIPTOR_TYPE_P (type))
     {
       tree rank = gfc_rank_cst[GFC_TYPE_ARRAY_RANK (type) - 1];
-      size = gfc_conv_descriptor_ubound_get (dest, rank);
-      size = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-                             size,
-                             gfc_conv_descriptor_lbound_get (dest, rank));
-      size = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-                             size, gfc_index_one_node);
+      size = gfc_conv_descriptor_extent_get (dest, rank);
       if (GFC_TYPE_ARRAY_RANK (type) > 1)
        size = fold_build2_loc (input_location, MULT_EXPR,
                                gfc_array_index_type, size,
@@ -2058,11 +2038,7 @@ gfc_omp_get_array_size (location_t loc, tree desc, 
gimple_seq *seq)
   gfc_omp_gen_simple_loop (idx, begin, end, LT_EXPR, step, loc, &seq1, &seq2);
   gimple_seq_add_seq (seq, seq1);
 
-  tmp = fold_build2_loc (loc, MINUS_EXPR, gfc_array_index_type,
-                        gfc_conv_descriptor_ubound_get (desc, idx),
-                        gfc_conv_descriptor_lbound_get (desc, idx));
-  tmp = fold_build2_loc (loc, PLUS_EXPR, gfc_array_index_type,
-                        tmp, gfc_index_one_node);
+  tmp = gfc_conv_descriptor_extent_get (desc, idx);
   gimplify_assign (extent, tmp, seq);
   tmp = fold_build2_loc (loc, LT_EXPR, boolean_type_node,
                         extent, gfc_index_zero_node);
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index de625b293eae..a8fccb60058b 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -1049,7 +1049,7 @@ gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
       /* For arrays, obtain the array index.  */
       if (gfc_expr_attr (code->expr1).dimension)
        {
-         tree desc, tmp, extent, lbound, ubound;
+         tree desc, tmp, extent, lbound;
          gfc_array_ref *ar, ar2;
          int i, rank;
 
@@ -1085,8 +1085,7 @@ gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
                                       TREE_TYPE (tmp), index, tmp);
              if (i < ar->dimen - 1)
                {
-                 ubound = gfc_conv_descriptor_ubound_get (desc, 
gfc_rank_cst[i]);
-                 tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+                 tmp = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[i]);
                  extent = fold_build2_loc (input_location, MULT_EXPR,
                                            TREE_TYPE (tmp), extent, tmp);
                }
@@ -1250,7 +1249,7 @@ gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
   /* For arrays, obtain the array index.  */
   if (gfc_expr_attr (code->expr1).dimension)
     {
-      tree desc, tmp, extent, lbound, ubound;
+      tree desc, tmp, extent, lbound;
       gfc_array_ref *ar, ar2;
       int i;
 
@@ -1283,8 +1282,7 @@ gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
                                   TREE_TYPE (tmp), index, tmp);
          if (i < ar->dimen - 1)
            {
-             ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
-             tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
+             tmp = gfc_conv_descriptor_extent_get (desc, gfc_rank_cst[i]);
              extent = fold_build2_loc (input_location, MULT_EXPR,
                                        TREE_TYPE (tmp), extent, tmp);
            }

Reply via email to