]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: Avoid introducing UB in the ((X /[ex] C1) +- C2) * (C1 * C3) simplification...
authorJakub Jelinek <jakub@redhat.com>
Wed, 27 Nov 2024 13:32:07 +0000 (14:32 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 27 Nov 2024 13:32:07 +0000 (14:32 +0100)
As the pr117692.c testcase shows, the generalized pattern can introduce
UB when there wasn't any.
The old pattern was I believe correct, it is as if in the new
pattern C3 was always 1 and I don't see how that could have introduced
UB.
But if type is signed and C3 (aka factor) isn't 1 and for + X and C2
could have different sign or for - X and C2 could have the same sign,
when doing the addition/subtraction first the absolute value could
decrease, while if first multiplying by C3 we could invoke UB already
during that multiplication.

The following patch fixes it by going through the casts to utype if
ranger (get_range_pos_neg) detects the sign compared to sign of C2
(INTEGER_CST) could be the same or could be different depending on op
because then the absolute value will not increase.

Other possibility (perhaps as another check if this check doesn't succeed)
would be to test whether X * C3 could actually overflow.
vr-values.cc has check_for_binary_op_overflow (currently not exported)
which I think does what we'd need to check, if it returns true and sets
ovf to false.

2024-11-27  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/117692
* tree.cc (get_range_pos_neg): Adjust function comment, use
non-negative instead of positive.
* match.pd
(((X /[ex] C1) +- C2) * (C1 * C3) -> (X * C3) +- (C1 * C2 * C3)):
Use casts to utype if type is signed, factor isn't 1 and
C1 and C2 could have different sign for + or could have the
same sign for -.

* gcc.dg/tree-ssa/mulexactdiv-5.c: Expect 8 nop_exprs.
* gcc.dg/tree-ssa/pr117692.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/mulexactdiv-5.c
gcc/testsuite/gcc.dg/tree-ssa/pr117692.c [new file with mode: 0644]
gcc/tree.cc

index ad354274b2a1992e2094489d28c28dfe339f13b8..8606b85941175d0c1a725ebf2058237814d32d89 100644 (file)
@@ -5577,7 +5577,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
          && TREE_CODE (@3) == INTEGER_CST
          && (mul = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
                             TYPE_SIGN (type), &overflow),
-             !overflow))
+             !overflow)
+         && (TYPE_UNSIGNED (type)
+             /* Not using unsigned arithmetics is unsafe if factor
+                isn't 1 and if for op plus @0 and @2 could have different
+                sign or for op minus @0 and @2 could have the same sign.  */
+             || known_eq (factor, 1)
+             || (get_range_pos_neg (@0)
+                 | (((op == PLUS_EXPR) ^ (tree_int_cst_sgn (@2) < 0))
+                    ? 1 : 2)) != 3))
       (op (mult @0 { wide_int_to_tree (type, factor); })
          { wide_int_to_tree (type, mul); })
       (with { tree utype = unsigned_type_for (type); }
index 37cd676fff69a74773d008881fc78fd12e06f261..af92f654073d1164aa77a68f8dcf4bbb77323e26 100644 (file)
@@ -18,7 +18,7 @@ TEST_CMP (f4, 8, 4, 200)
 
 /* { dg-final { scan-tree-dump-not {<[a-z]*_div_expr,} "optimized" } } */
 /* { dg-final { scan-tree-dump-not {<rshift_expr,} "optimized" } } */
-/* { dg-final { scan-tree-dump-not {<nop_expr,} "optimized" } } */
+/* { dg-final { scan-tree-dump-times {<nop_expr,} 8 "optimized" } } */
 /* { dg-final { scan-tree-dump {<mult_expr, [^,]*, [^,]*, 3,} "optimized" } } */
 /* { dg-final { scan-tree-dump {<mult_expr, [^,]*, [^,]*, 5,} "optimized" } } */
 /* { dg-final { scan-tree-dump {<mult_expr, [^,]*, [^,]*, 20,} "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr117692.c b/gcc/testsuite/gcc.dg/tree-ssa/pr117692.c
new file mode 100644 (file)
index 0000000..41a5986
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/117692 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-vrp1" } */
+/* { dg-final { scan-tree-dump " \\\* 25;" "vrp1" } } */
+/* { dg-final { scan-tree-dump " \\\+ 800;" "vrp1" } } */
+/* { dg-final { scan-tree-dump " = \\\(unsigned int\\\) " "vrp1" } } */
+/* { dg-final { scan-tree-dump " = \\\(int\\\) " "vrp1" } } */
+
+int
+foo (int x)
+{
+  if (x & 7)
+    __builtin_unreachable ();
+  x /= 8;
+  x += 4;
+  return x * 200;
+}
index 833c3a9cc89deda81ac9af4b53c523e72d1a012e..3ef1b6b483b3188e0131230917720167dea251c8 100644 (file)
@@ -14511,8 +14511,8 @@ verify_type (const_tree t)
 
 
 /* Return 1 if ARG interpreted as signed in its precision is known to be
-   always positive or 2 if ARG is known to be always negative, or 3 if
-   ARG may be positive or negative.  */
+   always non-negative or 2 if ARG is known to be always negative, or 3 if
+   ARG may be non-negative or negative.  */
 
 int
 get_range_pos_neg (tree arg)