https://gcc.gnu.org/g:5f6c199003762a2572741ff43cf8c64f6940ec13
commit 5f6c199003762a2572741ff43cf8c64f6940ec13 Author: Mikael Morin <mik...@gcc.gnu.org> Date: Thu Feb 6 17:16:13 2025 +0100 Factorisation gfc_conv_shift_descriptor Correction compil' Correction régression allocated_4.f90 Factorisation gfc_conv_shift_descriptor. Correction régression allocated_4.f90 Modifications mineures Correction régression bound_10.f90 Diff: --- gcc/fortran/trans-array.cc | 165 ++++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 84 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index c09b9bdab155..bf965dc68268 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -1476,35 +1476,43 @@ gfc_build_null_descriptor (tree type) specified. This also updates ubound and offset accordingly. */ static void -conv_shift_descriptor_lbound (stmtblock_t* block, tree desc, int dim, - tree new_lbound, tree offset) +conv_shift_descriptor_lbound (stmtblock_t* block, tree from_desc, tree to_desc, int dim, + tree new_lbound, tree offset, bool zero_based) { - tree ubound, lbound, stride; - tree diff, offs_diff; - + /* Set lbound to the value we want. */ new_lbound = fold_convert (gfc_array_index_type, new_lbound); + new_lbound = gfc_evaluate_now (new_lbound, block); + gfc_conv_descriptor_lbound_set (block, to_desc, gfc_rank_cst[dim], new_lbound); - 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]); + tree lbound = gfc_conv_descriptor_lbound_get (from_desc, gfc_rank_cst[dim]); + tree ubound = gfc_conv_descriptor_ubound_get (from_desc, gfc_rank_cst[dim]); + tree stride = gfc_conv_descriptor_stride_get (from_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); + tree diff; + if (zero_based) + diff = new_lbound; + else + { + /* Get difference (new - old) by which to shift stuff. */ + diff = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type, + new_lbound, lbound); + diff = gfc_evaluate_now (diff, block); + } /* 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); - tree tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type, - offset, offs_diff); - gfc_add_modify (block, offset, tmp); + tree tmp1 = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, + ubound, diff); + gfc_conv_descriptor_ubound_set (block, to_desc, gfc_rank_cst[dim], tmp1); - /* Finally set lbound to value we want. */ - gfc_conv_descriptor_lbound_set (block, desc, gfc_rank_cst[dim], new_lbound); + tree offs_diff = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, + diff, stride); + tree tmp2 = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type, + offset, offs_diff); + gfc_add_modify (block, offset, tmp2); + + if (from_desc != to_desc) + gfc_conv_descriptor_stride_set (block, to_desc, gfc_rank_cst[dim], stride); } @@ -1512,6 +1520,7 @@ class lb_info_base { public: virtual tree lower_bound (stmtblock_t *block, int dim) const = 0; + virtual bool zero_based_src () const { return false; } }; @@ -1572,21 +1581,64 @@ public: static void -conv_shift_descriptor (stmtblock_t *block, tree desc, int rank, +conv_shift_descriptor (stmtblock_t *block, tree src, tree dest, int rank, const lb_info_base &info) { - tree tmp = gfc_conv_descriptor_offset_get (desc); - tree offset_var = gfc_create_var (TREE_TYPE (tmp), "offset"); - gfc_add_modify (block, offset_var, tmp); + if (src != dest) + { + tree tmp = gfc_conv_descriptor_data_get (src); + gfc_conv_descriptor_data_set (block, dest, tmp); + } + + tree offset_var = gfc_create_var (gfc_array_index_type, "offset"); + tree init_offset; + if (info.zero_based_src ()) + init_offset = gfc_index_zero_node; + else + init_offset = gfc_conv_descriptor_offset_get (src); + gfc_add_modify (block, offset_var, init_offset); /* Apply a shift of the lbound when supplied. */ for (int dim = 0; dim < rank; ++dim) { tree lower_bound = info.lower_bound (block, dim); - conv_shift_descriptor_lbound (block, desc, dim, lower_bound, offset_var); + conv_shift_descriptor_lbound (block, src, dest, dim, lower_bound, offset_var, + info.zero_based_src ()); } - gfc_conv_descriptor_offset_set (block, desc, offset_var); + gfc_conv_descriptor_offset_set (block, dest, offset_var); +} + + +static void +conv_shift_descriptor (stmtblock_t *block, tree desc, int rank, + const lb_info_base &info) +{ + conv_shift_descriptor (block, desc, desc, rank, info); +} + + +class cond_descr_lb : public lb_info_base +{ + tree desc; + tree cond; +public: + cond_descr_lb (tree arg_desc, tree arg_cond) + : desc (arg_desc), cond (arg_cond) { } + + virtual tree lower_bound (stmtblock_t *block, int dim) const; + virtual bool zero_based_src () const { return true; } +}; + + +tree +cond_descr_lb::lower_bound (stmtblock_t *block ATTRIBUTE_UNUSED, int dim) const +{ + tree lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[dim]); + lbound = fold_build3_loc (input_location, COND_EXPR, + gfc_array_index_type, cond, + gfc_index_one_node, lbound); + return lbound; } @@ -1859,67 +1911,12 @@ gfc_conv_remap_descriptor (stmtblock_t *block, tree dest, tree src, } -class conditional_lb -{ - tree cond; -public: - conditional_lb (tree arg_cond) - : cond (arg_cond) { } - - tree lower_bound (tree src, int n) const { - tree lbound = gfc_conv_descriptor_lbound_get (src, gfc_rank_cst[n]); - lbound = fold_build3_loc (input_location, COND_EXPR, - gfc_array_index_type, cond, - gfc_index_one_node, lbound); - return lbound; - } -}; - - -static void -gfc_conv_shift_descriptor (stmtblock_t *block, tree dest, tree src, - int rank, const conditional_lb &lb) -{ - tree tmp = gfc_conv_descriptor_data_get (src); - gfc_conv_descriptor_data_set (block, dest, tmp); - - tree offset = gfc_index_zero_node; - for (int n = 0 ; n < rank; n++) - { - tree lbound; - - lbound = lb.lower_bound (dest, n); - lbound = gfc_evaluate_now (lbound, block); - - tmp = gfc_conv_descriptor_ubound_get (src, gfc_rank_cst[n]); - tmp = fold_build2_loc (input_location, PLUS_EXPR, - gfc_array_index_type, tmp, lbound); - gfc_conv_descriptor_lbound_set (block, dest, - gfc_rank_cst[n], lbound); - gfc_conv_descriptor_ubound_set (block, dest, - gfc_rank_cst[n], tmp); - - /* Set stride and accumulate the offset. */ - tmp = gfc_conv_descriptor_stride_get (src, gfc_rank_cst[n]); - gfc_conv_descriptor_stride_set (block, dest, - gfc_rank_cst[n], tmp); - tmp = fold_build2_loc (input_location, MULT_EXPR, - gfc_array_index_type, lbound, tmp); - offset = fold_build2_loc (input_location, MINUS_EXPR, - gfc_array_index_type, offset, tmp); - offset = gfc_evaluate_now (offset, block); - } - - gfc_conv_descriptor_offset_set (block, dest, offset); -} - - void gfc_conv_shift_descriptor (stmtblock_t *block, tree dest, tree src, int rank, tree zero_cond) { - gfc_conv_shift_descriptor (block, dest, src, rank, - conditional_lb (zero_cond)); + conv_shift_descriptor (block, src, dest, rank, + cond_descr_lb (src, zero_cond)); }