PR tree-optimization/90662 - strlen of a string in a vla plus offset not folded
authorMartin Sebor <msebor@redhat.com>
Fri, 14 Jun 2019 02:07:02 +0000 (02:07 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Fri, 14 Jun 2019 02:07:02 +0000 (20:07 -0600)
gcc/ChangeLog:

PR tree-optimization/90662
* tree-ssa-strlen.c (get_stridx): Convert fold_build2 operands
to the same type.

gcc/testsuite/ChangeLog:

PR tree-optimization/90662
* gcc.dg/pr90866-2.c: New test.
* gcc.dg/pr90866.c: Ditto.

From-SVN: r272281

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr90866-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr90866.c [new file with mode: 0644]
gcc/tree-ssa-strlen.c

index 09e05e697432196f591e58111f8ae9c7abe284cc..cbf0f02da0c69a36b23207dfe453e7c5129a074f 100644 (file)
@@ -1,3 +1,9 @@
+2019-06-13  Martin Sebor  <msebor@redhat.com>
+
+       PR tree-optimization/90662
+       * tree-ssa-strlen.c (get_stridx): Convert fold_build2 operands
+       to the same type.
+
 2019-06-13  Jan Hubicka  <hubicka@ucw.cz>
 
        PR bootstrap/90873
index 7032352934c87e751282b3345a323e933ff601d9..bff5bbcddf7952e07e99f1689213e10d6075b001 100644 (file)
@@ -1,3 +1,9 @@
+2019-06-13  Martin Sebor  <msebor@redhat.com>
+
+       PR tree-optimization/90662
+       * gcc.dg/pr90866-2.c: New test.
+       * gcc.dg/pr90866.c: Ditto.
+
 2019-06-13  Jiufu Guo  <guojiufu@linux.ibm.com>
            Lijia He  <helijia@linux.ibm.com>
 
diff --git a/gcc/testsuite/gcc.dg/pr90866-2.c b/gcc/testsuite/gcc.dg/pr90866-2.c
new file mode 100644 (file)
index 0000000..8c11049
--- /dev/null
@@ -0,0 +1,24 @@
+/* PR tree-optimization/90866 - ICE in fold_binary_loc, at fold-const.c:9827
+   { dg-do compile  }
+   { dg-options "-O2 -fsanitize=thread" } */
+
+typedef enum { a } b;
+typedef struct {
+  int c[0];
+} d;
+typedef struct {
+  int *data;
+} e;
+typedef struct {
+  e buffer;
+} f;
+int g, h;
+int i();
+int i(f *j, d *k, b l, int m) {
+  if (l)
+    if (m) {
+      h = j->buffer.data[0];
+      k->c[g] = k->c[g] * 8;
+    }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/pr90866.c b/gcc/testsuite/gcc.dg/pr90866.c
new file mode 100644 (file)
index 0000000..66c92ee
--- /dev/null
@@ -0,0 +1,18 @@
+/* PR tree-optimization/90866 - ICE in fold_binary_loc, at fold-const.c:9827
+   { dg-do compile  }
+   { dg-options "-O3 -Wall -fno-tree-loop-optimize" } */
+
+int a[1024], b[1024];
+
+void f (void)
+{
+  int i = 0;
+  for ( ; ; i++)
+    {
+    b[16] = a[i * 16 + 10];
+    b[i * 16 + 11] = a[i * 16 + 11] * 3;
+    b[i * 16 + 12] = a[i * 16 + 12] * 4;
+    b[i * 16 + 13] = a[i * 16 + 13] * 4;
+    b[i * 16 + 14] = a[i * 16 + 14] * 3;
+  }
+}
index 944650cecd5302c04107fc9ae4d2007c00b035fb..7369a73ecc531932f054f0625c7f21879577145c 100644 (file)
@@ -322,11 +322,17 @@ get_stridx (tree exp)
                  if (TREE_CODE (ptr) == ARRAY_REF)
                    {
                      off = TREE_OPERAND (ptr, 1);
-                     /* Scale the array index by the size of the element
-                        type (normally 1 for char).  */
-                     off = fold_build2 (MULT_EXPR, TREE_TYPE (off), off,
-                                        eltsize);
                      ptr = TREE_OPERAND (ptr, 0);
+                     if (!integer_onep (eltsize))
+                       {
+                         /* Scale the array index by the size of the element
+                            type in the rare case that it's greater than
+                            the typical 1 for char, making sure both operands
+                            have the same type.  */
+                         eltsize = fold_convert (ssizetype, eltsize);
+                         off = fold_convert (ssizetype, off);
+                         off = fold_build2 (MULT_EXPR, ssizetype, off, eltsize);
+                       }
                    }
                  else
                    off = integer_zero_node;