r15-3870-g6c5543d3d9c4bb introduced a fix for this pattern
to use expr_no_side_effects_p but I was testing the wrong
operand here which allowed b to become unconditional even
if that expression traps.

Puhsed as obvious after bootstrap/test on x86_64-linux-gnu.

        PR tree-optimization/126470

gcc/ChangeLog:

        * match.pd (`(a != 0) ? (a / b) : 0`): Fix argument
        to expr_no_side_effects_p.

gcc/testsuite/ChangeLog:

        * gcc.dg/torture/pr126470-1.c: New test.

Signed-off-by: Andrea Pinski <[email protected]>
---
 gcc/match.pd                              |  2 +-
 gcc/testsuite/gcc.dg/torture/pr126470-1.c | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr126470-1.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 9b14a042d05..b67a2c9fdde 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4937,7 +4937,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
         && tree_expr_nonzero_p (@1)
        /* Cannot make a expression with side effects
           unconditional. */
-       && expr_no_side_effects_p (@3))
+       && expr_no_side_effects_p (@1))
     @2)))
 
 /* Note we prefer the != case here
diff --git a/gcc/testsuite/gcc.dg/torture/pr126470-1.c 
b/gcc/testsuite/gcc.dg/torture/pr126470-1.c
new file mode 100644
index 00000000000..f180f959486
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr126470-1.c
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* PR tree-optimization/126470 */
+
+/* *p should not become unconditional.  */
+
+__attribute__((noipa)) int
+f (int a, int *p)
+{
+  return a != 0 ? a / (*p | 1) : 0;
+}
+
+int
+main (void)
+{
+  if (f (0, 0) != 0)
+    __builtin_abort ();
+  return 0;
+}
-- 
2.43.0

Reply via email to