aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <pinskia@gmail.com>2023-10-24 23:13:18 +0000
committerAndrew Pinski <pinskia@gmail.com>2023-10-26 18:58:40 +0000
commit662655e22dddf5392d9aa67fce45beee980e5454 (patch)
tree4e7c52a3141a8644d9ee3bd0525a83e7bbdc5894
parentabd78dc61076120649ccc1ca0b1d74d0b0a22f81 (diff)
downloadgcc-662655e22dddf5392d9aa67fce45beee980e5454.zip
gcc-662655e22dddf5392d9aa67fce45beee980e5454.tar.gz
gcc-662655e22dddf5392d9aa67fce45beee980e5454.tar.bz2
match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead of abs. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111957 gcc/ChangeLog: * match.pd (`a != C1 ? abs(a) : C2`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-40.c: New test.
-rw-r--r--gcc/match.pd10
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c25
2 files changed, 35 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index f725a68..5f6aeb0 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5611,6 +5611,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
@3))
+/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
+(for op (abs absu)
+ (simplify
+ (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
+ (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
+ (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
+ (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
+ (convert (absu:utype @0)))
+ @3))))
+
/* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
(simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
new file mode 100644
index 0000000..a9011ce
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt" } */
+/* PR tree-optimization/111957 */
+
+int f(int a)
+{
+ if (a)
+ return a > 0 ? a : -a;
+ return 0;
+}
+
+int f1(int x)
+{
+ int intmin = (-1u >> 1);
+ intmin = -intmin - 1;
+ if (x != intmin)
+ return x > 0 ? x : -x;
+ return intmin;
+}
+
+/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */