[gcc r17-2803] match: Fix up expr_no_side_effects_p call for `(a != 0) ? (a / b) : 0` pattern [PR126470]

Andrea Pinski via Gcc-cvs <[email protected]> Thu, 30 Jul 2026 03:53:27 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:366b7772dd5424bc2b09c0796395d63d22710939

commit r17-2803-g366b7772dd5424bc2b09c0796395d63d22710939
Author: Andrea Pinski <[email protected]>
Date:   Wed Jul 29 16:12:42 2026 -0700

    match: Fix up expr_no_side_effects_p call for `(a != 0) ? (a / b) : 0` pattern [PR126470]
    
    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]>

Diff:
---
 gcc/match.pd                              |  2 +-
 gcc/testsuite/gcc.dg/torture/pr126470-1.c | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index eb2730c24f73..668fbe70d24d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4908,7 +4908,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 000000000000..f180f959486b
--- /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;
+}