From c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Sun, 15 Oct 2023 10:36:56 -0700 Subject: [PATCH] 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. --- gcc/match.pd | 49 ++++++++++++++++------------ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c | 51 ++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c | 24 ++++++++++++++ 3 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-36.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-37.c diff --git a/gcc/match.pd b/gcc/match.pd index e76ec1ec034..dbd554e2e7c 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 00000000000..4baf9f82a22 --- /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 00000000000..f1ff472aaff --- /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" } } */ -- 2.11.4.GIT