https://gcc.gnu.org/g:86a6d4ff7844e54cb8e685b6ea0782ceca618888

commit r17-1572-g86a6d4ff7844e54cb8e685b6ea0782ceca618888
Author: Kael Andrew Franco <[email protected]>
Date:   Mon Jun 15 11:13:18 2026 -0600

    [PATCH] match: For nonnegative X, relax condition on X < (typeof X)(X != 0) 
to false [PR125756]
    
    From 0d094722ecdb86aa8373134a36392c60e6bb2b69 Mon Sep 17 00:00:00 2001
    From: Kael Andrew Alonzo Franco <[email protected]>
    Date: Sat, 13 Jun 2026 08:56:34 -0400
    Subject: [PATCH] match: For nonnegative X, relax condition on X < (typeof 
X)(X != 0) to false [PR125756]
    
    tree_expr_nonnegative_p covers both TYPE_UNSIGNED (type) or when X is known 
to be nonnegative.
    
    Bootstrapped and tested on x86_64-pc-linux-gnu
    
            PR tree-optimization/125756
    
    gcc/ChangeLog:
    
            * match.pd: Use tree_expr_nonnegative_p for X < (typeof X)(X != 0) 
to false.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/pr125756.c: New test.

Diff:
---
 gcc/match.pd                    |  7 +++----
 gcc/testsuite/gcc.dg/pr125756.c | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index d0d37116ddf1..f2503b35dca0 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8272,11 +8272,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
         (if (cmp == GT_EXPR)
          (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
 
-/* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
+/* For nonnegative x, x < (typeof x)(x != 0) is always false.  */
 (simplify
- (lt:c @0 (convert (ne @0 integer_zerop)))
- (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
-  { constant_boolean_node (false, type); }))
+ (lt:c @0 (convert (ne tree_expr_nonnegative_p@0 integer_zerop)))
+ { constant_boolean_node (false, type); })
 
 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 
0) */
 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 
1) */
diff --git a/gcc/testsuite/gcc.dg/pr125756.c b/gcc/testsuite/gcc.dg/pr125756.c
new file mode 100644
index 000000000000..3be98d901aaf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr125756.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+bool
+f1 (unsigned short aa)
+{
+  int a = aa;
+  bool t = a != 0;
+  int tu = t;
+  return a < tu;
+}
+
+bool
+f2 (unsigned short a)
+{
+  bool t = a != 0;
+  unsigned short tu = t;
+  return a < tu;
+}
+
+/* { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */

Reply via email to