]> gcc.gnu.org Git - gcc.git/commitdiff
tree-optimization/111445 - simple_iv simplification fault
authorRichard Biener <rguenther@suse.de>
Fri, 20 Oct 2023 13:08:49 +0000 (15:08 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 20 Oct 2023 13:45:37 +0000 (15:45 +0200)
The following fixes a missed check in the simple_iv attempt
to simplify (signed T)((unsigned T) base + step) where it
allows a truncating inner conversion leading to wrong code.

PR tree-optimization/111445
* tree-scalar-evolution.cc (simple_iv_with_niters):
Add missing check for a sign-conversion.

* gcc.dg/torture/pr111445.c: New testcase.

gcc/testsuite/gcc.dg/torture/pr111445.c [new file with mode: 0644]
gcc/tree-scalar-evolution.cc

diff --git a/gcc/testsuite/gcc.dg/torture/pr111445.c b/gcc/testsuite/gcc.dg/torture/pr111445.c
new file mode 100644 (file)
index 0000000..320e0b9
--- /dev/null
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+
+extern void abort (void);
+short a, b;
+unsigned char c = 255;
+unsigned cnt;
+void __attribute__((noipa))
+check (int x)
+{
+  if (x != 0)
+    abort ();
+  cnt++;
+}
+int main()
+{
+  int d;
+  unsigned char e;
+  d = 0;
+  for (; a >= 0; a--) {
+    int *f = &d;
+    *f = c;
+  }
+  e = 0;
+  for (; (unsigned char)(d - 255) + e <= 1; e++)
+    check (b);
+  if (cnt != 2)
+    abort ();
+  return 0;
+}
index 7cafe5ce576079921e380aaab5c5c4aa84cea372..95a15fe098810cd56a09044f7f4e9a7647ec9fdb 100644 (file)
@@ -3286,7 +3286,8 @@ simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop,
 
   type = TREE_TYPE (iv->base);
   e = TREE_OPERAND (iv->base, 0);
-  if (TREE_CODE (e) != PLUS_EXPR
+  if (!tree_nop_conversion_p (type, TREE_TYPE (e))
+      || TREE_CODE (e) != PLUS_EXPR
       || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
       || !tree_int_cst_equal (iv->step,
                              fold_convert (type, TREE_OPERAND (e, 1))))
This page took 0.081768 seconds and 5 git commands to generate.