https://gcc.gnu.org/g:57d1344c75e764d905c9e2c6501bc38d5a071552

commit r17-6633-g57d1344c75e764d905c9e2c6501bc38d5a071552
Author: Wesley Lewis <[email protected]>
Date:   Tue Nov 24 07:52:06 2026 -0700

    [PATCH v3] match: Fold ~X op -Y as `(Y-1) op X` [PR112628]
    
    Since ~(-Y) == Y - 1, the existing fold of ~X cmp ~Y into Y cmp X
    extends to ~X cmp -Y. Combined with existing patterns this turns
    unsigned ~a < -a into a != 0, signed ~a < -a into true and ~a == -a
    into false.
    
    For signed types the new Y + (-1) overflows only when -Y already does
    (Y == INT_MIN), so no new undefined behaviour is introduced.
    With -fwrapv both sides wrap identically.
    
    Bootstrapped tested on x86_64-pc-linux-gnu.
    
    ---
    Changes in v3:
    - Use :s on bit_not and negate instead of explicit single_use checks.
    
    Changes in v2:
    - Gate the fold on !TYPE_OVERFLOW_SANITIZED and !TYPE_OVERFLOW_TRAPS.
    - Add -ftrapv and ubsan testcases.
    
            PR tree-optimization/112628
    
    gcc/ChangeLog:
    
            * match.pd (~X cmp -Y): New pattern.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/pr112628.c: New test.
            * gcc.dg/tree-ssa/pr112628-2.c: New test.
            * gcc.dg/tree-ssa/pr112628.c: New test.
            * gcc.dg/tree-ssa/pr112628-3.c: New test.
            * c-c++-common/ubsan/pr112628.c: New test.
    
    Signed-off-by: Wesley Lewis <[email protected]>

Diff:
---
 gcc/match.pd                                | 10 +++++
 gcc/testsuite/c-c++-common/ubsan/pr112628.c | 19 ++++++++++
 gcc/testsuite/gcc.dg/torture/pr112628.c     | 58 +++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr112628-2.c  | 39 +++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr112628-3.c  | 12 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr112628.c    | 34 +++++++++++++++++
 6 files changed, 172 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 07fb23ef84a5..1930b847b83a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8598,6 +8598,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (with { tree otype = TREE_TYPE (@4); }
     (cmp (convert:otype @1) (convert:otype @0))))))
 
+/* Fold ~X op -Y as (Y - 1) op X */
+(for cmp (simple_comparison)
+  (simplify
+    (cmp:c (bit_not:s @0) (negate:s @1))
+    (if (types_match (@0, @1)
+        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))
+        && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@1)))
+      (cmp (plus @1 { build_minus_one_cst (TREE_TYPE (@0)); }) @0))))
+
+
 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
 (for cmp (simple_comparison)
      scmp (swapped_simple_comparison)
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr112628.c 
b/gcc/testsuite/c-c++-common/ubsan/pr112628.c
new file mode 100644
index 000000000000..dbb3e0c1a260
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/pr112628.c
@@ -0,0 +1,19 @@
+// PR tree-optimization/112628
+// { dg-do run }
+// { dg-require-effective-target int32 }
+// { dg-options "-fsanitize=signed-integer-overflow" }
+
+__attribute__((noipa)) int
+f (int a)
+{
+       return (~a) < -a;
+}
+
+int main()
+{
+  volatile int x = -__INT_MAX__ - 1;
+  f (x);
+  return 0;
+}
+
+// { dg-output "negation of -2147483648 cannot be represented in type 'int'" }
diff --git a/gcc/testsuite/gcc.dg/torture/pr112628.c 
b/gcc/testsuite/gcc.dg/torture/pr112628.c
new file mode 100644
index 000000000000..937271f7348b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr112628.c
@@ -0,0 +1,58 @@
+/* PR tree-optimization/112628 */
+/* { dg-do run } */
+/* { dg-additional-options "-fwrapv" } */
+
+/* Check the fold is correct when signed overflow wraps:
+   then ~A < -A is false for A == INT_MIN.  */
+
+__attribute__((noipa)) int
+slt (int a)
+{
+  return (~a) < -a;
+}
+
+__attribute__((noipa)) int
+seq (int a)
+{
+  return (~a) == -a;
+}
+
+__attribute__((noipa)) int
+ult (unsigned a)
+{
+  return (~a) < -a;
+}
+
+__attribute__((noipa)) int
+uge (unsigned a)
+{
+  return (~a) >= -a;
+}
+
+int
+main ()
+{
+  int sv[] = { 0, 1, -1, 42, -42, __INT_MAX__, -__INT_MAX__ - 1 };
+  unsigned uv[] = { 0, 1, 2, 42, 0x80000000u, ~0u };
+  unsigned i;
+
+  for (i = 0; i < sizeof (sv) / sizeof (sv[0]); i++)
+    {
+      int a = sv[i];
+      if (slt (a) != (a != -__INT_MAX__ - 1))
+       __builtin_abort ();
+      if (seq (a) != 0)
+       __builtin_abort ();
+    }
+
+  for (i = 0; i < sizeof (uv) / sizeof (uv[0]); i++)
+    {
+      unsigned a = uv[i];
+      if (ult (a) != (a != 0))
+       __builtin_abort ();
+      if (uge (a) != (a == 0))
+       __builtin_abort ();
+    }
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr112628-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr112628-2.c
new file mode 100644
index 000000000000..4716df1ab72e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr112628-2.c
@@ -0,0 +1,39 @@
+/* PR tree-optimization/112628 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* For signed A (overflow undefined), ~A < -A is always true.
+   For any integer type, ~A == -A is always false.  */
+
+int
+f (int a)
+{
+  return (~a) < -a;
+}
+
+int
+f0 (int a)
+{
+  return a > ~(-a);
+}
+
+int
+fne (int a)
+{
+  return (~a) != -a;
+}
+
+int
+feq (int a)
+{
+  return (~a) == -a;
+}
+
+int
+ufeq (unsigned a)
+{
+  return (~a) == -a;
+}
+
+/* { dg-final { scan-tree-dump-times "return 1;" 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr112628-3.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr112628-3.c
new file mode 100644
index 000000000000..49a0de8bd205
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr112628-3.c
@@ -0,0 +1,12 @@
+// PR tree-optimization/112628
+// { dg-do compile }
+// { dg-options "-O2 -ftrapv -fdump-tree-optimized" }
+
+int
+f (int a)
+{
+  return (~a) < -a;
+}
+
+// The negation must survive so that it can trap
+// { dg-final { scan-tree-dump "= -a_" "optimized" } }
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr112628.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr112628.c
new file mode 100644
index 000000000000..7a3561158b92
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr112628.c
@@ -0,0 +1,34 @@
+/* PR tree-optimization/112628 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+/* For unsigned A, ~A < -A is A != 0 and ~A >= -A is A == 0.  */
+
+int
+f1 (unsigned a)
+{
+  return (~a) < -a;
+}
+
+int
+f2 (unsigned a)
+{
+  return -a > ~a;
+}
+
+int
+f3 (unsigned a)
+{
+  return (~a) >= -a;
+}
+
+int
+f4 (unsigned a)
+{
+  return -a <= ~a;
+}
+
+/* { dg-final { scan-tree-dump-not "~" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "= -" "optimized" } } */
+/* { dg-final { scan-tree-dump-times " != 0" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " == 0" 2 "optimized" } } */

Reply via email to