https://gcc.gnu.org/g:e08ae151a4be653bf4c4d55d8d54b08c5d620a46

commit r17-6095-ge08ae151a4be653bf4c4d55d8d54b08c5d620a46
Author: Kyrylo Tkachov <[email protected]>
Date:   Wed Oct 14 15:51:09 2026 +0200

    gimple-fold: Test the outer comparison in same_bool_comparison_p
    
    same_bool_comparison_p (EXPR, CODE, OP1, OP2) checks whether EXPR is
    equivalent to OP1 CODE OP2.  If OP1 is a Boolean SSA name defined by a
    comparison, it handles OP1 != 0 and OP1 == 0 by recursing on that
    comparison or its inverse.  It tests the code of the defining comparison
    instead of CODE, though.  For M_1 = x_10(D) != y_11(D) it therefore
    treats M_1 <= 1, which is always true, as x_10(D) == y_11(D), and
    reassoc folds Z_17 in
    
      V_7 = I1_5 & I2_6;
      W_14 = V_7 == 0;
      T_15 = M_1 != 0;
      Z_17 = W_14 & T_15;
    
    to false, where I1_5 is x_10(D) == y_11(D) and I2_6 is M_1 <= 1.
    
    Test CODE.  The inverted branch can then see any comparison code, so
    honour NaNs when inverting it and give up when the inverse does not
    exist.  The front ends fold such comparisons of Boolean values, so only
    GIMPLE input reaches the wrong answer.
    
    Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu.
    
    gcc/ChangeLog:
    
            * gimple-fold.cc (same_bool_comparison_p): Test the code of the
            outer comparison, not that of the definition of OP1.  Honour NaNs
            when inverting the definition.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/reassoc-55.c: New test.
            * gcc.dg/tree-ssa/ssa-ifcombine-nan-3.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/gimple-fold.cc                                 | 22 ++++----
 gcc/testsuite/gcc.dg/tree-ssa/reassoc-55.c         | 37 +++++++++++++
 .../gcc.dg/tree-ssa/ssa-ifcombine-nan-3.c          | 62 ++++++++++++++++++++++
 3 files changed, 110 insertions(+), 11 deletions(-)

diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 1fdcc0c465fd..cd881f583fc3 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -7319,17 +7319,17 @@ same_bool_comparison_p (const_tree expr, enum tree_code 
code,
          && TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
        {
          enum tree_code c = gimple_assign_rhs_code (s);
-         if ((c == NE_EXPR && integer_zerop (op2))
-             || (c == EQ_EXPR && integer_nonzerop (op2)))
-           return same_bool_comparison_p (expr, c,
-                                          gimple_assign_rhs1 (s),
-                                          gimple_assign_rhs2 (s));
-         if ((c == EQ_EXPR && integer_zerop (op2))
-             || (c == NE_EXPR && integer_nonzerop (op2)))
-           return same_bool_comparison_p (expr,
-                                          invert_tree_comparison (c, false),
-                                          gimple_assign_rhs1 (s),
-                                          gimple_assign_rhs2 (s));
+         tree rhs1 = gimple_assign_rhs1 (s);
+         if ((code == EQ_EXPR || code == NE_EXPR)
+             && TREE_CODE (op2) == INTEGER_CST)
+           {
+             /* OP1 == 0 and OP1 != true test the inverse.  */
+             if ((code == EQ_EXPR) == integer_zerop (op2))
+               c = invert_tree_comparison (c, HONOR_NANS (rhs1));
+             return (c != ERROR_MARK
+                     && same_bool_comparison_p (expr, c, rhs1,
+                                                gimple_assign_rhs2 (s)));
+           }
        }
     }
   return false;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/reassoc-55.c 
b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-55.c
new file mode 100644
index 000000000000..4c924351a0d8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/reassoc-55.c
@@ -0,0 +1,37 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fgimple" } */
+
+/* M_1 <= 1 is always true, so it is not equivalent to x == y although
+   M_1 is x != y.  The fold happens in reassoc.  */
+
+int __GIMPLE (ssa,startwith("reassoc1")) __attribute__((noipa))
+f (int x, int y)
+{
+  _Bool M;
+  _Bool I1;
+  _Bool I2;
+  _Bool V;
+  _Bool W;
+  _Bool T;
+  _Bool Z;
+  int r;
+
+  __BB(2):
+  M_1 = x_10(D) != y_11(D);
+  I1_5 = x_10(D) == y_11(D);
+  I2_6 = M_1 <= _Literal (_Bool) 1;
+  V_7 = I1_5 & I2_6;
+  W_14 = V_7 == _Literal (_Bool) 0;
+  T_15 = M_1 != _Literal (_Bool) 0;
+  Z_17 = W_14 & T_15;
+  r_8 = (int) Z_17;
+  return r_8;
+}
+
+int
+main (void)
+{
+  if (f (1, 2) != 1 || f (2, 2) != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-3.c
new file mode 100644
index 000000000000..c9cc156cb381
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-nan-3.c
@@ -0,0 +1,62 @@
+/* M_1 == 0 is the inverse of a < b, which is a u>= b and not a >= b.  */
+/* { dg-do run } */
+/* { dg-options "-O1 -fgimple -fno-trapping-math" } */
+/* { dg-add-options ieee } */
+
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f (double a, double b, int c)
+{
+  _Bool M;
+  _Bool U;
+  _Bool K;
+  _Bool A;
+  _Bool P;
+  int r;
+
+  __BB(2):
+  M_1 = a_2(D) < b_3(D);
+  U_5 = a_2(D) __UNORDERED b_3(D);
+  K_6 = a_2(D) __UNGE b_3(D);
+  A_7 = U_5 & K_6;
+  if (c_4(D) != 0)
+    goto __BB4;
+  else
+    goto __BB3;
+
+  __BB(3):
+  goto __BB4;
+
+  __BB(4):
+  P_8 = __PHI (__BB2: A_7, __BB3: _Literal (_Bool) 0);
+  if (P_8 == _Literal (_Bool) 0)
+    goto __BB5;
+  else
+    goto __BB7;
+
+  __BB(5):
+  if (M_1 == _Literal (_Bool) 0)
+    goto __BB6;
+  else
+    goto __BB7;
+
+  __BB(6):
+  goto __BB8;
+
+  __BB(7):
+  goto __BB8;
+
+  __BB(8):
+  r_9 = __PHI (__BB6: 1, __BB7: 0);
+  return r_9;
+}
+
+int
+main (void)
+{
+  double n = __builtin_nan ("");
+  if (f (n, 0.0, 0) != 1 || f (n, 0.0, 1) != 0
+      || f (1.0, 0.0, 0) != 1 || f (1.0, 0.0, 1) != 1
+      || f (0.0, 1.0, 0) != 0 || f (0.0, 1.0, 1) != 0)
+    __builtin_abort ();
+  return 0;
+}

Reply via email to