diff options
author | Andrew Pinski <apinski@marvell.com> | 2023-05-15 21:44:27 +0000 |
---|---|---|
committer | Andrew Pinski <apinski@marvell.com> | 2023-05-16 03:46:44 +0000 |
commit | b06cfb62229f17eca59fa4aabf853d7e17e2327b (patch) | |
tree | 09ebdb59d2b90aaadda1fb5f959ad080282c9024 | |
parent | 8cd140d3658c29df9571e5f926464f742d87e0c4 (diff) | |
download | gcc-b06cfb62229f17eca59fa4aabf853d7e17e2327b.zip gcc-b06cfb62229f17eca59fa4aabf853d7e17e2327b.tar.gz gcc-b06cfb62229f17eca59fa4aabf853d7e17e2327b.tar.bz2 |
MATCH: [PR109424] Simplify min/max of boolean arguments
This is version 2 of https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577394.html
which does not depend on adding gimple_truth_valued_p at this point.
Instead will use zero_one_valued_p which is already used for mult simplifications
to make sure that we only have [0,1] rather having the mistake of maybe having [-1,0]
as the range for signed bools.
This shows up in a few places in GCC itself but only at -O1, we miss the min/max conversion
because of PR 107888 (which I will be testing seperately).
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
Thanks,
Andrew Pinski
PR tree-optimization/109424
gcc/ChangeLog:
* match.pd: Add patterns for min/max of zero_one_valued
values to `&`/`|`.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/bool-12.c: New test.
* gcc.dg/tree-ssa/bool-13.c: New test.
* gcc.dg/tree-ssa/minmax-20.c: New test.
* gcc.dg/tree-ssa/minmax-21.c: New test.
-rw-r--r-- | gcc/match.pd | 8 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/bool-12.c | 44 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/bool-13.c | 38 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c | 27 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c | 28 |
5 files changed, 145 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index b025fb8..30ffdfc 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7439,6 +7439,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TREE_CODE (@0) != INTEGER_CST) (op @0 (ext @1 @2))))) +/* Max<bool0, bool1> -> bool0 | bool1 + Min<bool0, bool1> -> bool0 & bool1 */ +(for op (max min) + logic (bit_ior bit_and) + (simplify + (op zero_one_valued_p@0 zero_one_valued_p@1) + (logic @0 @1))) + /* signbit(x) != 0 ? -x : x -> abs(x) signbit(x) == 0 ? -x : x -> -abs(x) */ (for sign (SIGNBIT) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c new file mode 100644 index 0000000..e62594e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */ +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + if (a > b) + c = a; + else + c = b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + if (a < b) + c = a; + else + c = b; + return c; +} +/* In Original, we should still have the if form as that is what is written. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "original" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "original" } } */ +/* { dg-final { scan-tree-dump-times "if " 2 "original" } } */ + +/* PHI-OPT1 should have converted it into min/max */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */ + +/* Forwprop2 (after ccp) will convert it into &\| */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */ + +/* By optimize there should be no min/max nor if */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c new file mode 100644 index 0000000..438f15a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c @@ -0,0 +1,38 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */ +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = a > b ? a : b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = a < b ? a : b; + return c; +} +/* In Original, we should still have the min/max form as that is what is written. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "original" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "original" } } */ + +/* PHI-OPT1 should have kept it as min/max. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */ + +/* Forwprop2 (after ccp) will convert it into &\| */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */ + +/* By optimize there should be no min/max nor if */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c new file mode 100644 index 0000000..ab64033 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c @@ -0,0 +1,27 @@ +/* PR tree-optimization/109424 */ +/* { dg-do compile } */ +/* Need -O2 for early non-zero */ +/* { dg-options "-O2 -fdump-tree-forwprop1-raw" } */ + +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = (a > b)?a : b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = (a < b)?a : b; + return c; +} + +/* { dg-final { scan-tree-dump-not "max_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-not "min_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 1 "forwprop1"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "forwprop1"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c new file mode 100644 index 0000000..5f2b8af --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c @@ -0,0 +1,28 @@ +/* PR tree-optimization/109424 */ +/* { dg-do compile } */ +/* Need -O2 for early non-zero bits */ +/* { dg-options "-O2 -fdump-tree-forwprop1-raw -fdump-tree-optimized-raw" } */ + + +int maxbool(int ab, int bb) +{ + int a = ab & 1; + int b = bb & 1; + int c; + c = (a > b)?a : b; + return c; +} +int minbool(int ab, int bb) +{ + int a = ab & 1; + int b = bb & 1; + int c; + c = (a < b)?a : b; + return c; +} + +/* { dg-final { scan-tree-dump-not "max_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-not "min_expr, " "forwprop1"} } */ +/* There should only be 3 total bit_and_expr, one &1 for each function and one & for minbool. */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 3 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "optimized"} } */ |