[gcc(refs/users/mikael/heads/refactor_descriptor_v08)] Déplacement shift descriptor vers gfc_conv_array_parameter

Mikael Morin mikael@gcc.gnu.org
Sat Sep 20 13:13:52 GMT 2025


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

commit b57d6f3603282a60c166627341ea83e65d573a14
Author: Mikael Morin <mikael@gcc.gnu.org>
Date:   Tue Dec 17 17:27:24 2024 +0100

    Déplacement shift descriptor vers gfc_conv_array_parameter
    
    Suppression variables inutilisées

Diff:
---
 gcc/fortran/trans-array.cc      | 61 ++++++++++++++++-------------------------
 gcc/fortran/trans-array.h       |  2 +-
 gcc/fortran/trans-descriptor.cc | 47 +++++++++++++++++++++++++++++++
 gcc/fortran/trans-descriptor.h  |  3 ++
 gcc/fortran/trans-expr.cc       | 20 +-------------
 5 files changed, 76 insertions(+), 57 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 26b50f9b354f..92b5abe9d92a 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -107,40 +107,31 @@ gfc_array_dataptr_type (tree desc)
   return (GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (desc)));
 }
 
-/* Modify a descriptor such that the lbound of a given dimension is the value
-   specified.  This also updates ubound and offset accordingly.  */
 
-void
-gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
-				  int dim, tree new_lbound)
+static bool
+keep_descriptor_lower_bound (gfc_expr *e)
 {
-  tree offs, ubound, lbound, stride;
-  tree diff, offs_diff;
-
-  new_lbound = fold_convert (gfc_array_index_type, new_lbound);
-
-  offs = gfc_conv_descriptor_offset_get (desc);
-  lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
-  ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
-  stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
+  gfc_ref *ref;
 
-  /* Get difference (new - old) by which to shift stuff.  */
-  diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-			  new_lbound, lbound);
+  /* Detect any array references with vector subscripts.  */
+  for (ref = e->ref; ref; ref = ref->next)
+    if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
+	&& ref->u.ar.type != AR_FULL)
+      {
+	int dim;
+	for (dim = 0; dim < ref->u.ar.dimen; dim++)
+	  if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
+	    break;
+	if (dim < ref->u.ar.dimen)
+	  break;
+      }
 
-  /* Shift ubound and offset accordingly.  This has to be done before
-     updating the lbound, as they depend on the lbound expression!  */
-  ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
-			    ubound, diff);
-  gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
-  offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
-			       diff, stride);
-  offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
-			  offs, offs_diff);
-  gfc_conv_descriptor_offset_set (block, desc, offs);
+  /* Array references with vector subscripts and non-variable
+     expressions need be converted to a one-based descriptor.  */
+  if (ref || e->expr_type != EXPR_VARIABLE)
+    return false;
 
-  /* Finally set lbound to value we want.  */
-  gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
+  return true;
 }
 
 
@@ -8597,7 +8588,7 @@ is_pointer (gfc_expr *e)
 void
 gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
 			  const gfc_symbol *fsym, const char *proc_name,
-			  tree *size, tree *lbshift, tree *packed)
+			  tree *size, bool maybe_shift, tree *packed)
 {
   tree ptr;
   tree desc;
@@ -8834,13 +8825,9 @@ gfc_conv_array_parameter (gfc_se *se, gfc_expr *expr, bool g77,
 	  stmtblock_t block;
 
 	  gfc_init_block (&block);
-	  if (lbshift && *lbshift)
-	    {
-	      /* Apply a shift of the lbound when supplied.  */
-	      for (int dim = 0; dim < expr->rank; ++dim)
-		gfc_conv_shift_descriptor_lbound (&block, se->expr, dim,
-						  *lbshift);
-	    }
+	  if (maybe_shift && !keep_descriptor_lower_bound (expr))
+	    gfc_conv_shift_descriptor (&block, se->expr, expr->rank);
+
 	  tmp = gfc_class_data_get (ctree);
 	  if (expr->rank > 1 && CLASS_DATA (fsym)->as->rank != expr->rank
 	      && CLASS_DATA (fsym)->as->type == AS_EXPLICIT && !no_pack)
diff --git a/gcc/fortran/trans-array.h b/gcc/fortran/trans-array.h
index c1886eb1faaf..f0ecc8c57877 100644
--- a/gcc/fortran/trans-array.h
+++ b/gcc/fortran/trans-array.h
@@ -153,7 +153,7 @@ tree gfc_get_array_span (tree, gfc_expr *);
 void gfc_conv_expr_descriptor (gfc_se *, gfc_expr *);
 /* Convert an array for passing as an actual function parameter.  */
 void gfc_conv_array_parameter (gfc_se *, gfc_expr *, bool, const gfc_symbol *,
-			       const char *, tree *, tree * = nullptr,
+			       const char *, tree *, bool = false,
 			       tree * = nullptr);
 
 /* These work with both descriptors and descriptorless arrays.  */
diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 9d30f42063ea..b8ab46190fe4 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -980,3 +980,50 @@ gfc_set_descriptor_from_scalar (stmtblock_t *block, tree descr,
 				  gfc_expr_attr (scalar_expr), cond_presence,
 				  caf_token);
 }
+
+
+/* Modify a descriptor such that the lbound of a given dimension is the value
+   specified.  This also updates ubound and offset accordingly.  */
+
+void
+gfc_conv_shift_descriptor_lbound (stmtblock_t* block, tree desc,
+				  int dim, tree new_lbound)
+{
+  tree offs, ubound, lbound, stride;
+  tree diff, offs_diff;
+
+  new_lbound = fold_convert (gfc_array_index_type, new_lbound);
+
+  offs = gfc_conv_descriptor_offset_get (desc);
+  lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]);
+  ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[dim]);
+  stride = gfc_conv_descriptor_stride_get (desc, gfc_rank_cst[dim]);
+
+  /* Get difference (new - old) by which to shift stuff.  */
+  diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
+			  new_lbound, lbound);
+
+  /* Shift ubound and offset accordingly.  This has to be done before
+     updating the lbound, as they depend on the lbound expression!  */
+  ubound = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
+			    ubound, diff);
+  gfc_conv_descriptor_ubound_set (block, desc, gfc_rank_cst[dim], ubound);
+  offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
+			       diff, stride);
+  offs = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
+			  offs, offs_diff);
+  gfc_conv_descriptor_offset_set (block, desc, offs);
+
+  /* Finally set lbound to value we want.  */
+  gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound);
+}
+
+
+void
+gfc_conv_shift_descriptor (stmtblock_t* block, tree desc, int rank)
+{
+  /* Apply a shift of the lbound when supplied.  */
+  for (int dim = 0; dim < rank; ++dim)
+    gfc_conv_shift_descriptor_lbound (block, desc, dim,
+				      gfc_index_one_node);
+}
diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h
index d7d4fe43d4b2..21c52e37f0e0 100644
--- a/gcc/fortran/trans-descriptor.h
+++ b/gcc/fortran/trans-descriptor.h
@@ -100,6 +100,9 @@ tree gfc_create_null_actual_descriptor (stmtblock_t *, gfc_typespec *,
 
 void gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, tree descr);
 
+void gfc_conv_shift_descriptor_lbound (stmtblock_t *, tree, int, tree);
+void gfc_conv_shift_descriptor (stmtblock_t *, tree, int);
+
 void gfc_set_descriptor_from_scalar_class (stmtblock_t *, tree, tree, gfc_expr *);
 void gfc_set_descriptor_from_scalar (stmtblock_t *, tree, tree, symbol_attribute,
 				     tree = NULL_TREE, tree = NULL_TREE);
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 1d0b058b4061..8916087d53e5 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -917,8 +917,6 @@ gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
 	  stmtblock_t block;
 	  gfc_init_block (&block);
 	  gfc_ref *ref;
-	  int dim;
-	  tree lbshift = NULL_TREE;
 
 	  /* Array refs with sections indicate, that a for a formal argument
 	     expecting contiguous repacking needs to be done.  */
@@ -931,25 +929,9 @@ gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
 	      && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
 	    fsym->attr.contiguous = 1;
 
-	  /* Detect any array references with vector subscripts.  */
-	  for (ref = e->ref; ref; ref = ref->next)
-	    if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
-		&& ref->u.ar.type != AR_FULL)
-	      {
-		for (dim = 0; dim < ref->u.ar.dimen; dim++)
-		  if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
-		    break;
-		if (dim < ref->u.ar.dimen)
-		  break;
-	      }
-	  /* Array references with vector subscripts and non-variable
-	     expressions need be converted to a one-based descriptor.  */
-	  if (ref || e->expr_type != EXPR_VARIABLE)
-	    lbshift = gfc_index_one_node;
-
 	  parmse->expr = var;
 	  gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
-				    &lbshift, &packed);
+				    true, &packed);
 
 	  if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
 	    {


More information about the Gcc-cvs mailing list