public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-4955] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
@ 2023-10-26 18:58 Andrew Pinski
  0 siblings, 0 replies; only message in thread
From: Andrew Pinski @ 2023-10-26 18:58 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:662655e22dddf5392d9aa67fce45beee980e5454

commit r14-4955-g662655e22dddf5392d9aa67fce45beee980e5454
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Tue Oct 24 23:13:18 2023 +0000

    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.

Diff:
---
 gcc/match.pd                               | 10 ++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 +++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index f725a685863d..5f6aeb07ac01 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 000000000000..a9011ce97fb1
--- /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" } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-26 18:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-26 18:58 [gcc r14-4955] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957] Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).