[gcc r17-55] match.pd: x != -CST ? x + CST : 0 -> x + CST [PR122996]

Andrew Pinski pinskia@gcc.gnu.org
Fri Apr 24 07:50:07 GMT 2026


https://gcc.gnu.org/g:2b0b9d3c696e500cbbdd5864566c726f3d7e903a

commit r17-55-g2b0b9d3c696e500cbbdd5864566c726f3d7e903a
Author: Netanel Komm <netanelkomm@gmail.com>
Date:   Sat Apr 11 14:15:44 2026 +0300

    match.pd: x != -CST ? x + CST : 0 -> x + CST [PR122996]
    
    This patch simplifies expressions of the form x != CST1 ? x + CST2 : 0
    into x + CST2 when CST1 == -CST2. This comes up, for example, when
    dealing with 'rtrim'-style operations.
    
    Bootstrapped and regression tested on x86_64-pc-linux-gnu.
    
            PR tree-optimization/122996
    
    gcc/ChangeLog:
    
            * match.pd (x != CST1 ? x + CST2 : 0 -> x + CST2): New pattern.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr122996.c: New test.
    
    Signed-off-by: Netanel Komm <netanelkomm@gmail.com>

Diff:
---
 gcc/match.pd                             |  6 ++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr122996.c | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 7b652afb43de..d0f913e640e5 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5251,6 +5251,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	&& expr_no_side_effects_p (@1))
    @2)))
 
+/* x != CST1 ? x + CST2 : 0 -> x + CST2 when CST1 == -CST2.  */
+(simplify
+ (cond (ne @0 INTEGER_CST@2) (plus@1 @0 INTEGER_CST@3) integer_zerop)
+ (if (wi::to_wide (@2) == -wi::to_wide (@3))
+  @1))
+
 /* Simplifications of shift and rotates.  */
 
 (for rotate (lrotate rrotate)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr122996.c b/gcc/testsuite/gcc.dg/tree-ssa/pr122996.c
new file mode 100644
index 000000000000..6cf81aec8981
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr122996.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
+typedef unsigned long u64;
+
+u64 f1 (u64 x) { return x == -1UL ? 0 : x + 1; }
+
+u64 f2 (u64 x) { return x != -100UL ? x + 100 : 0; }
+
+u64 f3 (u64 x) { return x == 1 ? 0 : x - 1; }
+
+u32 f4 (u32 x) { return x == -5 ? 0 : x + 5; }
+
+u16 f5 (u16 x) { return x == -30 ? 0 : x + 30; }
+
+u8  f6 (u8 x)  { return x == 255 ? 0: x + 1; }
+
+u64 g1 (u64 x) { return x == 10 ? 0 : x + 1; }
+
+/* { dg-final { scan-tree-dump-times "if " 1 "optimized" } } */


More information about the Gcc-cvs mailing list