]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/108164 - undefined overflow with IV vectorization
authorRichard Biener <rguenther@suse.de>
Mon, 19 Dec 2022 13:55:45 +0000 (14:55 +0100)
committerRichard Biener <rguenther@suse.de>
Tue, 24 Jan 2023 13:40:47 +0000 (14:40 +0100)
vect_update_ivs_after_vectorizer can end up emitting a signed
IV update when the loop body performed an unsigned computation.
The following makes sure to perform that update in the type
of the loop update type to avoid undefined behavior on overflow.

PR tree-optimization/108164
* tree-vect-loop-manip.cc (vect_update_ivs_after_vectorizer):
Perform vect_step_op_add update in the appropriate type.

* gcc.dg/pr108164.c: New testcase.

(cherry picked from commit ec459469f8a75d96a9b26694554efcc900d411de)

gcc/testsuite/gcc.dg/pr108164.c [new file with mode: 0644]
gcc/tree-vect-loop-manip.cc

diff --git a/gcc/testsuite/gcc.dg/pr108164.c b/gcc/testsuite/gcc.dg/pr108164.c
new file mode 100644 (file)
index 0000000..d76d557
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-options "-O3 -fno-tree-dce" } */
+
+int a, b, c;
+int main()
+{
+  int e = -1;
+  short f = -1;
+  for (; c < 1; c++)
+    while (f >= e)
+      f++;
+  for (; a < 2; a++) {
+    short g = ~(~b | ~f);
+    int h = -g;
+    int i = (3 / ~h) / ~b;
+    b = i;
+  }
+  return 0;
+}
index e4381eb707934d31fb67e325eb2611cd4ec5a4a7..cb5bb87ae337b1ba58040ea7f31a09e24b249104 100644 (file)
@@ -1596,14 +1596,16 @@ vect_update_ivs_after_vectorizer (loop_vec_info loop_vinfo,
 
       init_expr = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
 
-      off = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
-                        fold_convert (TREE_TYPE (step_expr), niters),
-                        step_expr);
+      tree stype = TREE_TYPE (step_expr);
+      off = fold_build2 (MULT_EXPR, stype,
+                        fold_convert (stype, niters), step_expr);
       if (POINTER_TYPE_P (type))
        ni = fold_build_pointer_plus (init_expr, off);
       else
-       ni = fold_build2 (PLUS_EXPR, type,
-                         init_expr, fold_convert (type, off));
+       ni = fold_convert (type,
+                          fold_build2 (PLUS_EXPR, stype,
+                                       fold_convert (stype, init_expr),
+                                       off));
 
       var = create_tmp_var (type, "tmp");