The following fixes recording uses in ARRAY_REFs with non-constant
element size or low bound.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

Richard.

2022-04-05  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/105148
        * tree-ssa-loop-ivopts.cc (idx_record_use): Walk raw operands
        2 and 3 of ARRAY_REFs.

        * gcc.dg/torture/pr105148.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr105148.c | 20 ++++++++++++++++++++
 gcc/tree-ssa-loop-ivopts.cc             |  6 ++++--
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr105148.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr105148.c 
b/gcc/testsuite/gcc.dg/torture/pr105148.c
new file mode 100644
index 00000000000..3338b0f3281
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr105148.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+
+extern void foo (void);
+
+static inline int
+bar (int n)
+{
+  for (int i = 0; i < n; i++)
+    {
+      foo ();
+      int y[1][i];
+      y[n][i] = 0;
+    }
+}
+
+int
+baz (void)
+{
+  return bar (5);
+}
diff --git a/gcc/tree-ssa-loop-ivopts.cc b/gcc/tree-ssa-loop-ivopts.cc
index 935d2d4d8f3..581f92b059b 100644
--- a/gcc/tree-ssa-loop-ivopts.cc
+++ b/gcc/tree-ssa-loop-ivopts.cc
@@ -2123,8 +2123,10 @@ idx_record_use (tree base, tree *idx,
   find_interesting_uses_op (data, *idx);
   if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
     {
-      find_interesting_uses_op (data, array_ref_element_size (base));
-      find_interesting_uses_op (data, array_ref_low_bound (base));
+      if (TREE_OPERAND (base, 2))
+       find_interesting_uses_op (data, TREE_OPERAND (base, 2));
+      if (TREE_OPERAND (base, 3))
+       find_interesting_uses_op (data, TREE_OPERAND (base, 3));
     }
   return true;
 }
-- 
2.34.1

Reply via email to