[COMMITTEDv2] MATCH: Move `a ? one_zero : one_zero` matching after min/max matching
Checks
Commit Message
In PR 106677, I noticed that on the trunk we were producing:
```
_25 = SR.116_117 == 0;
_27 = (unsigned char) _25;
_32 = _27 | SR.116_117;
```
From `SR.115_117 != 0 ? SR.115_117 : 1`
Rather than:
```
_119 = MAX_EXPR <1, SR.115_117>;
```
Or (rather)
```
_119 = SR.115_117 | 1;
```
Due to the order of the patterns.
Committed as approved with the new comment and testcase.
Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
* match.pd (`a ? one_zero : one_zero`): Move
below detection of minmax.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/phi-opt-34.c: New test.
---
gcc/match.pd | 42 ++++++++++++----------
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c | 23 ++++++++++++
2 files changed, 47 insertions(+), 18 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-34.c
@@ -4961,24 +4961,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
)
)
-(simplify
- (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
- (switch
- /* bool0 ? bool1 : 0 -> bool0 & bool1 */
- (if (integer_zerop (@2))
- (bit_and (convert @0) @1))
- /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
- (if (integer_zerop (@1))
- (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
- /* bool0 ? 1 : bool2 -> bool0 | bool2 */
- (if (integer_onep (@1))
- (bit_ior (convert @0) @2))
- /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
- (if (integer_onep (@2))
- (bit_ior (bit_xor (convert @0) @2) @1))
- )
-)
-
/* Optimize
# x_5 in range [cst1, cst2] where cst2 = cst1 + 1
x_5 ? cstN ? cst4 : cst3
@@ -5309,6 +5291,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1)))
(max @2 @4))))))
+#if GIMPLE
+/* These patterns should be after min/max detection as simplifications
+ of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
+ and `(type)(zero_one^1)` are not done yet. See PR 110637.
+ Even without those, reaching min/max/and/ior faster is better. */
+(simplify
+ (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
+ (switch
+ /* bool0 ? bool1 : 0 -> bool0 & bool1 */
+ (if (integer_zerop (@2))
+ (bit_and (convert @0) @1))
+ /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
+ (if (integer_zerop (@1))
+ (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
+ /* bool0 ? 1 : bool2 -> bool0 | bool2 */
+ (if (integer_onep (@1))
+ (bit_ior (convert @0) @2))
+ /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
+ (if (integer_onep (@2))
+ (bit_ior (bit_xor (convert @0) @2) @1))
+ )
+)
+#endif
+
/* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
(simplify
(cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
new file mode 100644
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* Disable early phiopt1 as early ccp1 does not export non-zero bits
+ so at the point of phiopt1, we don't know that a is [0,1] range */
+/* { dg-options "-O1 -fdisable-tree-phiopt1 -fdump-tree-phiopt2-folding"} */
+
+unsigned f(unsigned a)
+{
+ a &= 1;
+ if (a > 0)
+ return a;
+ return 1;
+}
+/* PHIOPT2 should be able to change this into just return 1;
+ (which was `MAX<a, 1>` or `a | 1` but since a is known to be a
+ range of [0,1], it should be folded into 1)
+ And not fold it into `(a == 0) | a`. */
+/* { dg-final { scan-tree-dump-not " == " "phiopt2" } } */
+/* { dg-final { scan-tree-dump-not " if " "phiopt2" } } */
+/* { dg-final { scan-tree-dump-not "Folded into the sequence:" "phiopt2" } } */
+/* { dg-final { scan-tree-dump "return 1;" "phiopt2" } } */
+/* We want to make sure that phiopt2 is happening and not some other pass
+ before it does the transformation. */
+/* { dg-final { scan-tree-dump "Removing basic block" "phiopt2" } } */