From 0f8c7f7ef7ac375193374bd2de7f58581b32a8bd Mon Sep 17 00:00:00 2001
From: Odysseas Georgoudis <odygrd@gmail.com>
Date: Wed, 1 Jul 2026 02:43:49 +0100
Subject: [PATCH v2] match.pd: Recognize branchless conditional negate
 [PR113894]

This patch teaches match.pd to recognize the branchless conditional negate
idiom (x ^ -cmp) + cmp when cmp is known to be zero or one.  The
expression is folded to a conditional negate form.

For the sign-test spelling based on x < 0, the patch exposes ABS_EXPR.

PR tree-optimization/113894

gcc/ChangeLog:

	* match.pd: Add simplifications for branchless conditional negate
	and sign-test absolute value idioms.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/pr113894.c: New test.

Signed-off-by: Odysseas Georgoudis <odygrd@gmail.com>
---
Changes in v2:
- Exclude saturating types from the sign-test ABS transformation.
- Require a side-effect-free operand when matching in GENERIC.
- Test the transformation in the earlier forwprop1 dump.
- Use one converted value in the _Bool testcase.

 gcc/match.pd                             | 17 +++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr113894.c | 63 ++++++++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr113894.c

diff --git a/gcc/match.pd b/gcc/match.pd
index ddf3b61638c..e6ee1b7d84b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -236,6 +236,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
   (abs @0)))
 
+/* (X ^ -(X < 0)) + (X < 0) -> abs (X) */
+(simplify
+ (plus:c (bit_xor:c @0 (negate (convert@1 (lt @0 integer_zerop)))) @1)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && !TYPE_UNSIGNED (TREE_TYPE (@0))
+      && !TYPE_SATURATING (TREE_TYPE (@0))
+      && (GIMPLE || !TREE_SIDE_EFFECTS (@0)))
+  (abs @0)))
+
 /* Following match patterns are used by the match_spaceship function to detect
    all possible spaceship combinations.  */
 #if GIMPLE
@@ -4793,6 +4802,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
   (cond (convert:boolean_type_node @2) @1 @0)))
 
+/* Transform (A ^ -cmp) + cmp into cmp ? -A : A.  */
+(simplify
+ (plus:c (bit_xor:c @0 (negate zero_one_valued_p@1)) @1)
+ (if (INTEGRAL_TYPE_P (type)
+      && !TYPE_SATURATING (type)
+      && (GIMPLE || !TREE_SIDE_EFFECTS (@0)))
+  (cond (convert:boolean_type_node @1) (negate @0) @0)))
+
 /* Transform A & (B*cmp) into (A&B)*cmp.  */
 (simplify
  (bit_and:c (mult:cs zero_one_valued_p@0 @1) @2)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr113894.c b/gcc/testsuite/gcc.dg/tree-ssa/pr113894.c
new file mode 100644
index 00000000000..dc7a450d3e8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr113894.c
@@ -0,0 +1,63 @@
+/* PR tree-optimization/113894 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1" } */
+
+int f_cmp_lt(int x, int y)
+{
+  int cmp = x < y;
+  return (x ^ -cmp) + cmp;
+}
+
+int f_cmp_gt_commuted(int x, int y)
+{
+  int cmp = x > y;
+  return cmp + (-cmp ^ x);
+}
+
+unsigned f_unsigned_cmp(unsigned x, unsigned y)
+{
+  unsigned cmp = x < y;
+  return (x ^ -cmp) + cmp;
+}
+
+int f_mask(int x, unsigned y)
+{
+  int cmp = y & 1;
+  return (x ^ -cmp) + cmp;
+}
+
+int f_bool(int x, _Bool cmp)
+{
+  int icmp = cmp;
+  return (x ^ -icmp) + icmp;
+}
+
+int f_abs_int(int x)
+{
+  int cmp = x < 0;
+  return (x ^ -cmp) + cmp;
+}
+
+long f_abs_long(long x)
+{
+  long cmp = x < 0;
+  return (x ^ -cmp) + cmp;
+}
+
+int f_signed_not_zero_one(int x, int cmp)
+{
+  return (x ^ -cmp) + cmp;
+}
+
+unsigned f_unsigned_not_zero_one(unsigned x, unsigned cmp)
+{
+  return (x ^ -cmp) + cmp;
+}
+
+/* The branchless conditional negate spelling should only survive when cmp is
+   not known to be 0 or 1.  */
+/* { dg-final { scan-tree-dump-times " \\^ " 2 "forwprop1" } } */
+/* Sign tests should expose absolute value.  */
+/* { dg-final { scan-tree-dump-times " = ABS_EXPR" 2 "forwprop1" } } */
+/* Other zero-one predicates should expose conditional negation.  */
+/* { dg-final { scan-tree-dump-times " \\? " 5 "forwprop1" } } */
-- 
2.43.5
