https://gcc.gnu.org/g:1b629eabc8fee175dc3fc13666a82fd999a2638a

commit 1b629eabc8fee175dc3fc13666a82fd999a2638a
Author: Mikael Morin <[email protected]>
Date:   Tue Sep 16 21:52:51 2025 +0200

    Factorisation calcul index dimension

Diff:
---
 gcc/fortran/trans-array.cc | 77 +++++++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 35 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 4072b3609845..47033b3cc7ec 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -3452,6 +3452,33 @@ array_bound_check_elemental (gfc_se * se, gfc_ss * ss, 
gfc_expr * expr)
 }
 
 
+/* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
+
+static void
+add_to_offset (tree *cst_offset, tree *offset, tree t)
+{
+  if (TREE_CODE (t) == INTEGER_CST)
+    *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
+  else
+    {
+      if (!integer_zerop (*offset))
+       *offset = fold_build2_loc (input_location, PLUS_EXPR,
+                                  gfc_array_index_type, *offset, t);
+      else
+       *offset = t;
+    }
+}
+
+
+/* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
+
+static void
+add_to_offset (gfc_array_ref_info *ref_info, tree val)
+{
+  add_to_offset (&ref_info->cst_index, &ref_info->index, val);
+}
+
+
 static void
 conv_array_index_dim (gfc_array_ref_info *ref_info, tree index, tree stride)
 {
@@ -3462,9 +3489,7 @@ conv_array_index_dim (gfc_array_ref_info *ref_info, tree 
index, tree stride)
 
   /* Add the offset for this dimension to the stored offset for all other
      dimensions.  */
-  ref_info->index = fold_build2_loc (input_location, PLUS_EXPR,
-                                    gfc_array_index_type,
-                                    ref_info->index, index);
+  add_to_offset (ref_info, index);
 }
 
 
@@ -3771,23 +3796,6 @@ gfc_conv_tmp_array_ref (gfc_se * se)
   gfc_advance_se_ss_chain (se);
 }
 
-/* Add T to the offset pair *OFFSET, *CST_OFFSET.  */
-
-static void
-add_to_offset (tree *cst_offset, tree *offset, tree t)
-{
-  if (TREE_CODE (t) == INTEGER_CST)
-    *cst_offset = int_const_binop (PLUS_EXPR, *cst_offset, t);
-  else
-    {
-      if (!integer_zerop (*offset))
-       *offset = fold_build2_loc (input_location, PLUS_EXPR,
-                                  gfc_array_index_type, *offset, t);
-      else
-       *offset = t;
-    }
-}
-
 
 static tree
 build_array_ref (tree desc, tree offset, tree decl, tree vptr)
@@ -3836,9 +3844,7 @@ gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, 
gfc_expr *expr,
                    locus * where)
 {
   int n;
-  tree offset, cst_offset;
   tree tmp;
-  tree stride;
   tree decl = NULL_TREE;
   gfc_se indexse;
   gfc_se tmpse;
@@ -3894,8 +3900,13 @@ gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, 
gfc_expr *expr,
       && ar->as->type != AS_DEFERRED)
     decl = sym->backend_decl;
 
-  cst_offset = offset = gfc_index_zero_node;
-  add_to_offset (&cst_offset, &offset, gfc_conv_array_offset (decl));
+  gfc_array_ref_info ref_info;
+  memset (&ref_info, 0, sizeof (ref_info));
+
+  ref_info.base = decl;
+  ref_info.cst_index = gfc_index_zero_node;
+  ref_info.index = gfc_index_zero_node;
+  add_to_offset (&ref_info, gfc_conv_array_offset (decl));
 
   /* Calculate the offsets from all the dimensions.  Make sure to associate
      the final offset so that we form a chain of loop invariant summands.  */
@@ -3962,18 +3973,14 @@ gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, 
gfc_expr *expr,
            }
        }
 
-      /* Multiply the index by the stride.  */
-      stride = gfc_conv_array_stride (decl, n);
-      tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
-                            indexse.expr, stride);
-
-      /* And add it to the total.  */
-      add_to_offset (&cst_offset, &offset, tmp);
+      conv_array_index_dim (&ref_info, indexse.expr,
+                           gfc_conv_array_stride (decl, n));
     }
 
-  if (!integer_zerop (cst_offset))
-    offset = fold_build2_loc (input_location, PLUS_EXPR,
-                             gfc_array_index_type, offset, cst_offset);
+  if (!integer_zerop (ref_info.cst_index))
+    ref_info.index = fold_build2_loc (input_location, PLUS_EXPR,
+                                     gfc_array_index_type, ref_info.index,
+                                     ref_info.cst_index);
 
   /* A pointer array component can be detected from its field decl. Fix
      the descriptor, mark the resulting variable decl and pass it to
@@ -4021,7 +4028,7 @@ gfc_conv_array_ref (gfc_se * se, gfc_array_ref * ar, 
gfc_expr *expr,
     }
 
   free (var_name);
-  se->expr = build_array_ref (se->expr, offset, decl, se->class_vptr);
+  se->expr = build_array_ref (se->expr, ref_info.index, decl, se->class_vptr);
 }

Reply via email to