]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH v2] match.pd: Add pattern to fold (X - (X<0)) ^ -(X<0) into ABS_EXPR<X> [PR123514]
authorEikansh Gupta <eikansh.gupta@oss.qualcomm.com>
Thu, 7 May 2026 22:45:20 +0000 (16:45 -0600)
committerJeff Law <jeffrey.law@oss.qualcomm.com>
Thu, 7 May 2026 22:46:15 +0000 (16:46 -0600)
Adds a pattern to fold (x - (x<0)) ^ -(x<0) into ABS_EXPR<x>.

PR tree-optimization/123514

gcc/ChangeLog:

* match.pd ((x - (x<0)) ^ -(x<0) into ABS_EXPR<x>): New pattern.

gcc/testsuite/ChangeLog:

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

Signed-off-by: Eikansh Gupta <eikansh.gupta@oss.qualcomm.com>
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr123514.c [new file with mode: 0644]

index 04faffb760d1ed3c239cf239a52b367796085aa8..8682a9797b70b2d6efc480b673e54ed9613984a4 100644 (file)
@@ -230,6 +230,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (abs @0)))
 #endif
 
+/* (X - (X < 0)) ^ -(X < 0) -> abs (X) */
+(simplify
+ (bit_xor:c (minus @0 (convert@1 (lt @0 integer_zerop)))
+           (negate @1))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && !TYPE_UNSIGNED (TREE_TYPE (@0)))
+  (abs @0)))
+
 /* Simplifications of operations with one constant operand and
    simplifications to constants or single values.  */
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr123514.c b/gcc/testsuite/gcc.dg/tree-ssa/pr123514.c
new file mode 100644 (file)
index 0000000..4a09d85
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int
+bit_trick (int x)
+{
+  int mask = -(x < 0);
+  return ((x + mask) ^ mask);
+}
+
+/* { dg-final { scan-tree-dump "ABS_EXPR" "optimized" } } */