From: Samarth Tandale <[email protected]>
When both operands of an addition are known to be zero or one, fold
the addition when its result is converted to boolean or compared
against 0, 1, or 2.
Rewrite these forms to equivalent bitwise OR, AND, XOR, and equality
operations. This avoids computing the promoted integer sum solely to
produce a boolean result.
Preserve wrapping, trapping, and sanitizer semantics for narrow
integer types.
gcc/ChangeLog:
PR tree-optimization/104292
* match.pd: Optimize comparisons of zero-one-valued additions
only when the addition type permits the transformation.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr104292.c: Test base cases for zero-one
addition comparisons.
* gcc.dg/tree-ssa/pr104292-bitint-unsigned.c: Test unsigned
one-bit wrapping.
* gcc.dg/tree-ssa/pr104292-bitint-trapv.c: Test signed overflow
trapping.
* gcc.dg/tree-ssa/pr104292-bitint-sanitize.c: Test signed overflow
sanitization.
* gcc.dg/tree-ssa/pr104292-bitint-wrapv.c: Test signed overflow
wrapping.
* gcc.dg/tree-ssa/pr104292-negative.c: Test operands not known to be
zero or one.
Signed-off-by: Samarth Tandale <[email protected]>
---
When both operands of an addition are known to be zero or one, fold
the addition when its result is converted to boolean or compared
against 0, 1, or 2.
Rewrite these forms to equivalent bitwise OR, AND, XOR, and equality
operations. This avoids computing the promoted integer sum solely to
produce a boolean result.
Preserve wrapping, trapping, and sanitizer semantics for narrow
integer types.
PR tree-optimization/104292
gcc/ChangeLog:
* match.pd: Optimize comparisons of zero-one-valued additions
only when the addition type permits the transformation.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr104292.c: Test base cases for zero-one
addition comparisons.
* gcc.dg/tree-ssa/pr104292-bitint-unsigned.c: Test unsigned
one-bit wrapping.
* gcc.dg/tree-ssa/pr104292-bitint-trapv.c: Test signed overflow
trapping.
* gcc.dg/tree-ssa/pr104292-bitint-sanitize.c: Test signed overflow
sanitization.
* gcc.dg/tree-ssa/pr104292-bitint-wrapv.c: Test signed overflow
wrapping.
* gcc.dg/tree-ssa/pr104292-negative.c: Test operands not known to be
zero or one.
Signed-off-by: Samarth Tandale <[email protected]>
This is a forge pull request published on the gcc-patches mailing list mailing
list
as requested by Samarth Tandale via Sourceware Forge
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/257
Get it locally using:
```
git fetch forge-upstream "+refs/versioned_pull/257/*:refs/versioned_pull/257/*"
git switch -c "pr-257-v1" "refs/versioned_pull/257/1/head"
```
Or, download the patch at: https://forge.sourceware.org/gcc/gcc/pulls/257.diff
Created on: 2026-10-26 09:17:09+00:00
Latest update: 2026-10-26 09:34:15+00:00
Changes: 7 changed files, 182 additions, 0 deletions
Head revision: sam-100/gcc ref pr104292 commit
b3114caafcbb1427bb3ae8fe9c69f3c5ed1d69c2
Base revision: gcc/gcc ref trunk commit
cf43f83b4195dccbd61abeb4aeabbdd22fcbe828 r17-5566-gcf43f83b4195dc
Merge base: cf43f83b4195dccbd61abeb4aeabbdd22fcbe828
Requested Reviewers:
Changed files:
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-sanitize.c
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-trapv.c
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-unsigned.c
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-wrapv.c
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292-negative.c
- A: gcc/testsuite/gcc.dg/tree-ssa/pr104292.c
- M: gcc/match.pd
gcc/match.pd | 45 +++++++++++++
.../tree-ssa/pr104292-bitint-sanitize.c | 15 +++++
.../gcc.dg/tree-ssa/pr104292-bitint-trapv.c | 15 +++++
.../tree-ssa/pr104292-bitint-unsigned.c | 14 +++++
.../gcc.dg/tree-ssa/pr104292-bitint-wrapv.c | 15 +++++
.../gcc.dg/tree-ssa/pr104292-negative.c | 15 +++++
gcc/testsuite/gcc.dg/tree-ssa/pr104292.c | 63 +++++++++++++++++++
7 files changed, 182 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-sanitize.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-trapv.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-unsigned.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-wrapv.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292-negative.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr104292.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 0bda1efac591c..13b2ee5d76d49 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2840,6 +2840,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
|| TYPE_PRECISION (type) > 1)
&& wi::leu_p (tree_nonzero_bits (@1), 1))))
+/* Simplify comparisons of { 0 or 1 } + { 0 or 1 } against 0, 1, and 2. */
+(for neeq (ne eq)
+ (simplify
+ (neeq (plus@3 zero_one_valued_p@0 zero_one_valued_p@1)
+ INTEGER_CST@2)
+ (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
+ && (TYPE_UNSIGNED (TREE_TYPE (@3))
+ ? TYPE_PRECISION (TREE_TYPE (@3)) > 1
+ : (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
+ && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@3)))))
+ (with {
+ tree_code newcode = ERROR_MARK;
+ bool invert_result = false;
+ tree cst1 = build_one_cst (TREE_TYPE (@0));
+ wide_int cst = wi::to_wide (@2);
+ if (cst == 2)
+ newcode = BIT_AND_EXPR, invert_result = neeq == NE_EXPR;
+ else if (cst == 1)
+ newcode = BIT_XOR_EXPR, invert_result = neeq == NE_EXPR;
+ else if (cst == 0)
+ newcode = BIT_IOR_EXPR, invert_result = neeq == EQ_EXPR;
+ }
+ (if (newcode != ERROR_MARK)
+ (if (invert_result)
+ (switch
+ (if (newcode == BIT_IOR_EXPR)
+ (convert:type (bit_xor (bit_ior @0 @1)
+ { cst1; })))
+ (if (newcode == BIT_AND_EXPR)
+ (convert:type (bit_xor (bit_and @0 @1)
+ { cst1; })))
+ (if (newcode == BIT_XOR_EXPR)
+ (convert:type (eq @0 @1))))
+ (switch
+ (if (newcode == BIT_IOR_EXPR)
+ (convert:type (bit_ior @0 @1)))
+ (if (newcode == BIT_AND_EXPR)
+ (convert:type (bit_and @0 @1)))
+ (if (newcode == BIT_XOR_EXPR)
+ (convert:type (bit_xor @0 @1))))
+ )
+ )
+ )
+ )))
+
/* VCE<bool>(zero_one) ==/!= false -> zero_one ==/!= 0.
This is done as SRA likes to create VCE for boolean accesses
and it helps code gneration and uninitialized variable warnings. */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-sanitize.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-sanitize.c
new file mode 100644
index 0000000000000..391ce1c26daa0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-sanitize.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/104292 */
+/* Preserve signed overflow sanitization for narrow _BitInt additions. */
+/* { dg-require-effective-target bitint } */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fsanitize=signed-integer-overflow -fdump-tree-optimized"
} */
+
+_BitInt(2)
+signed_bitint2_eq_zero_sanitize (_BitInt(2) a, _BitInt(2) b)
+{
+ a &= 1;
+ b &= 1;
+ return a + b == 0;
+}
+
+/* { dg-final { scan-tree-dump "UBSAN_CHECK_ADD" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-trapv.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-trapv.c
new file mode 100644
index 0000000000000..016a1c995d87c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-trapv.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/104292 */
+/* Preserve signed overflow trapping for narrow _BitInt additions. */
+/* { dg-require-effective-target bitint } */
+/* { dg-do compile } */
+/* { dg-options "-O1 -ftrapv -fdump-tree-optimized" } */
+
+_BitInt(2)
+signed_bitint2_eq_zero (_BitInt(2) a, _BitInt(2) b)
+{
+ a &= 1;
+ b &= 1;
+ return a + b == 0;
+}
+
+/* { dg-final { scan-tree-dump "\\+" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-unsigned.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-unsigned.c
new file mode 100644
index 0000000000000..3a552da154f02
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-unsigned.c
@@ -0,0 +1,14 @@
+/* PR tree-optimization/104292 */
+/* The addition of unsigned _BitInt(1) operands wraps modulo two. */
+/* { dg-require-effective-target bitint } */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int
+eq_zero (unsigned _BitInt(1) a, unsigned _BitInt(1) b)
+{
+ return a + b == 0;
+}
+
+/* The addition must remain because 1 + 1 wraps to zero for this type. */
+/* { dg-final { scan-tree-dump "\\+" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-wrapv.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-wrapv.c
new file mode 100644
index 0000000000000..3b663ba8cf155
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-bitint-wrapv.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/104292 */
+/* Preserve signed wrapping for narrow _BitInt additions. */
+/* { dg-require-effective-target bitint } */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fwrapv -fdump-tree-optimized" } */
+
+_BitInt(2)
+signed_bitint2_eq_zero_wrapv (_BitInt(2) a, _BitInt(2) b)
+{
+ a &= 1;
+ b &= 1;
+ return a + b == 0;
+}
+
+/* { dg-final { scan-tree-dump "\\+" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292-negative.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-negative.c
new file mode 100644
index 0000000000000..913aa1f6f52f9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292-negative.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/104292 */
+/* Do not fold an addition when its operands are not known to be zero or one.
+ This is a negative test for the zero-one-valued operand predicate. */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int
+unknown_range_eq_zero (int a, int b)
+{
+ return a + b == 0;
+}
+
+/* The addition must remain because neither operand has a proven zero/one
+ range. */
+/* { dg-final { scan-tree-dump "\\+" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr104292.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr104292.c
new file mode 100644
index 0000000000000..d4c6f4839476c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr104292.c
@@ -0,0 +1,63 @@
+/* PR tree-optimization/104292 */
+/* Test comparisons of additions whose operands are known to be zero or one.
*/
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+_Bool
+bool_plus (_Bool a, _Bool b)
+{
+ return a + b;
+}
+
+_Bool
+eq_zero (_Bool a, _Bool b)
+{
+ return a + b == 0;
+}
+
+_Bool
+ne_zero (_Bool a, _Bool b)
+{
+ return a + b != 0;
+}
+
+_Bool
+eq_one (_Bool a, _Bool b)
+{
+ return a + b == 1;
+}
+
+_Bool
+ne_one (_Bool a, _Bool b)
+{
+ return a + b != 1;
+}
+
+_Bool
+eq_two (_Bool a, _Bool b)
+{
+ return a + b == 2;
+}
+
+_Bool
+ne_two (_Bool a, _Bool b)
+{
+ return a + b != 2;
+}
+
+/* Test non-Boolean operands known to be zero or one. */
+
+int
+int_range_eq_one (int a, int b)
+{
+ a &= 1;
+ b &= 1;
+ return a + b == 1;
+}
+
+/* { dg-final { scan-tree-dump-not "\\+" "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\\|" 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "~" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "&" 3 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "\\^" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "==" 1 "optimized" } } */
--
2.54.0