Fortran: Fix non_negative_strides_array_p
The non_negative_strides_array_p function might return wrongly 'true', e.g.
for assumed-shape arrays, if the argument is '*a.0 ...' instead of 'a.0 ...'
as then the saved array descriptor for the PARAM_DECL 'a' is not found.
This potentially leads to wrong code - but I could not find a testcase
leading to wrong code on mainline. Asserts show that this happens with
CLASS; however, for those no ARRAY_REF seems to get used.
The issue show up when applying the delinearization patch as posted
at https://gcc.gnu.org/pipermail/gcc-patches/2020-December/562230.html
that has been applied to the OG12 alias devel/omp/gcc-12 vendor branch, as
commit 39a8c371fda6136cf77c74895a00b136409e0ba3. This patch calls
gfc_build_array_ref inside gfc_conv_array_ref. The issue mentioned
above show up with this patch in gfortran.dg/array_reference_3.f90,
a testcase added together with non_negative_strides_array_p in commit
r12-8230-g7964ab6c364 for PR 102043. Here, non_negative_strides_array_p
returns true for assumed_shape_x but assumed shape arrays may have
negative strides.
gcc/fortran/ChangeLog:
* trans-array.cc (non_negative_strides_array_p): Fix handling
of GFC_DECL_SAVED_DESCRIPTOR.
gcc/fortran/trans-array.cc | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
@@ -3695,11 +3695,16 @@ non_negative_strides_array_p (tree expr)
/* If the array was originally a dummy with a descriptor, strides can be
negative. */
- if (DECL_P (expr)
- && DECL_LANG_SPECIFIC (expr)
- && GFC_DECL_SAVED_DESCRIPTOR (expr)
- && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
- return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
+ tree decl = expr;
+ STRIP_NOPS (decl);
+ if (TREE_CODE (decl) == INDIRECT_REF)
+ decl = TREE_OPERAND (decl, 0);
+
+ if (DECL_P (decl)
+ && DECL_LANG_SPECIFIC (decl)
+ && GFC_DECL_SAVED_DESCRIPTOR (decl)
+ && GFC_DECL_SAVED_DESCRIPTOR (decl) != expr)
+ return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (decl));
return true;
}