diff options
author | Andrew Pinski <pinskia@gmail.com> | 2023-10-15 10:36:56 -0700 |
---|---|---|
committer | Andrew Pinski <pinskia@gmail.com> | 2023-10-16 10:11:13 -0700 |
commit | c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8 (patch) | |
tree | e6518a09c07f7b6f4507f0235c84beee3b3034f9 /gcc | |
parent | 29a4453c7b8a86d242dab89b9e4d222749fd911e (diff) | |
download | gcc-c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8.zip gcc-c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8.tar.gz gcc-c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8.tar.bz2 |
MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
This improves the `A CMP 0 ? A : -A` set of match patterns to use
bitwise_equal_p which allows an nop cast between signed and unsigned.
This allows catching a few extra cases which were not being caught before.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/101541
* match.pd (A CMP 0 ? A : -A): Improve
using bitwise_equal_p.
gcc/testsuite/ChangeLog:
PR tree-optimization/101541
* gcc.dg/tree-ssa/phi-opt-36.c: New test.
* gcc.dg/tree-ssa/phi-opt-37.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/match.pd | 49 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 |
3 files changed, 104 insertions, 20 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index e76ec1e..dbd554e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5660,42 +5660,51 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* A == 0 ? A : -A same as -A */ (for cmp (eq uneq) (simplify - (cnd (cmp @0 zerop) @0 (negate@1 @0)) - (if (!HONOR_SIGNED_ZEROS (type)) + (cnd (cmp @0 zerop) @2 (negate@1 @2)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @2)) @1)) (simplify - (cnd (cmp @0 zerop) zerop (negate@1 @0)) - (if (!HONOR_SIGNED_ZEROS (type)) + (cnd (cmp @0 zerop) zerop (negate@1 @2)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @2)) @1)) ) /* A != 0 ? A : -A same as A */ (for cmp (ne ltgt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type)) - @0)) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @1)) + @1)) (simplify - (cnd (cmp @0 zerop) @0 integer_zerop) - (if (!HONOR_SIGNED_ZEROS (type)) - @0)) + (cnd (cmp @0 zerop) @1 integer_zerop) + (if (!HONOR_SIGNED_ZEROS (type) + && bitwise_equal_p (@0, @1)) + @1)) ) /* A >=/> 0 ? A : -A same as abs (A) */ (for cmp (ge gt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type) - && !TYPE_UNSIGNED (type)) - (abs @0)))) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0)) + && !TYPE_UNSIGNED (TREE_TYPE(@0)) + && bitwise_equal_p (@0, @1)) + (if (TYPE_UNSIGNED (type)) + (absu:type @0) + (abs @0))))) /* A <=/< 0 ? A : -A same as -abs (A) */ (for cmp (le lt) (simplify - (cnd (cmp @0 zerop) @0 (negate @0)) - (if (!HONOR_SIGNED_ZEROS (type) - && !TYPE_UNSIGNED (type)) - (if (ANY_INTEGRAL_TYPE_P (type) - && !TYPE_OVERFLOW_WRAPS (type)) + (cnd (cmp @0 zerop) @1 (negate @1)) + (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0)) + && !TYPE_UNSIGNED (TREE_TYPE(@0)) + && bitwise_equal_p (@0, @1)) + (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) + || TYPE_UNSIGNED (type)) (with { - tree utype = unsigned_type_for (type); + tree utype = unsigned_type_for (TREE_TYPE(@0)); } (convert (negate (absu:utype @0)))) (negate (abs @0))))) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c new file mode 100644 index 0000000..4baf9f8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c @@ -0,0 +1,51 @@ +/* { dg-options "-O2 -fdump-tree-phiopt" } */ + +unsigned f0(int A) +{ + unsigned t = A; +// A == 0? A : -A same as -A + if (A == 0) return t; + return -t; +} + +unsigned f1(int A) +{ + unsigned t = A; +// A != 0? A : -A same as A + if (A != 0) return t; + return -t; +} +unsigned f2(int A) +{ + unsigned t = A; +// A >= 0? A : -A same as abs (A) + if (A >= 0) return t; + return -t; +} +unsigned f3(int A) +{ + unsigned t = A; +// A > 0? A : -A same as abs (A) + if (A > 0) return t; + return -t; +} +unsigned f4(int A) +{ + unsigned t = A; +// A <= 0? A : -A same as -abs (A) + if (A <= 0) return t; + return -t; +} +unsigned f5(int A) +{ + unsigned t = A; +// A < 0? A : -A same as -abs (A) + if (A < 0) return t; + return -t; +} + +/* f4 and f5 are not allowed to be optimized in early phi-opt. */ +/* { dg-final { scan-tree-dump-times "if " 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */ + + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c new file mode 100644 index 0000000..f1ff472 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt1" } */ + +unsigned abs_with_convert0 (int x) +{ + unsigned int y = x; + + if (x < 0) + y = -y; + + return y; +} +unsigned abs_with_convert1 (unsigned x) +{ + int y = x; + + if (y < 0) + x = -x; + + return x; +} + +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ |