Implement some of fold_binary_op_with_conditional_arg in match.pd
Commit Message
The following allows (c != 0 ? 0 : 100) != 0 to be simplified as
c != 0 as fold_binary_op_with_conditional_arg would have done
via forwprop and GENERIC folding. Likewise it allows to combine
(a != 0) != 0 directly via match.pd instead of only via
forwprop and again fold_binary_op_with_conditional_arg.
The patterns do not fully implement all cases of
fold_binary_op_with_conditional_arg, some aspects like
"any of the operands simplify" cannot currently be expressed.
Bootstrapped and tested on x86_64-unknown-linux-gnu.
2022-05-11 Richard Biener <rguenther@suse.de>
* generic-match-head.cc: Include tree-eh.h.
* match.pd ((cond ...) cmp X): New simplification inspired
by fold_binary_op_with_conditional_arg.
(eq/ne (cmp ...) true/false): Likewise.
* gcc.dg/tree-ssa/pr61839_1.c: Adjust.
* gcc.dg/tree-ssa/vrp24.c: Likewise.
---
gcc/generic-match-head.cc | 1 +
gcc/match.pd | 41 ++++++++++++++++++++++-
gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c | 2 +-
gcc/testsuite/gcc.dg/tree-ssa/vrp24.c | 4 +--
4 files changed, 44 insertions(+), 4 deletions(-)
@@ -38,6 +38,7 @@ along with GCC; see the file COPYING3. If not see
#include "optabs-tree.h"
#include "dbgcnt.h"
#include "tm.h"
+#include "tree-eh.h"
/* Routine to determine if the types T1 and T2 are effectively
the same for GENERIC. If T1 or T2 is not a type, the test
@@ -4656,7 +4656,34 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (ic == icmp)
(icmp @0 @1)
(if (ic == ncmp)
- (ncmp @0 @1))))))
+ (ncmp @0 @1)))))
+ /* The following bits are handled by fold_binary_op_with_conditional_arg. */
+ (simplify
+ (ne (cmp@2 @0 @1) integer_zerop)
+ (if (types_match (type, TREE_TYPE (@2)))
+ (cmp @0 @1)))
+ (simplify
+ (eq (cmp@2 @0 @1) integer_truep)
+ (if (types_match (type, TREE_TYPE (@2)))
+ (cmp @0 @1)))
+ (simplify
+ (ne (cmp@2 @0 @1) integer_truep)
+ (if (types_match (type, TREE_TYPE (@2)))
+ (with { enum tree_code ic = invert_tree_comparison
+ (cmp, HONOR_NANS (@0)); }
+ (if (ic == icmp)
+ (icmp @0 @1)
+ (if (ic == ncmp)
+ (ncmp @0 @1))))))
+ (simplify
+ (eq (cmp@2 @0 @1) integer_zerop)
+ (if (types_match (type, TREE_TYPE (@2)))
+ (with { enum tree_code ic = invert_tree_comparison
+ (cmp, HONOR_NANS (@0)); }
+ (if (ic == icmp)
+ (icmp @0 @1)
+ (if (ic == ncmp)
+ (ncmp @0 @1)))))))
/* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
??? The transformation is valid for the other operators if overflow
@@ -5486,6 +5513,18 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cmp (bit_and@2 @0 integer_pow2p@1) @1)
(icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
+/* From fold_binary_op_with_conditional_arg handle the case of
+ rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
+ compares simplify. */
+(for cmp (simple_comparison)
+ (simplify
+ (cmp:c (cond @0 @1 @2) @3)
+ /* Do not move possibly trapping operations into the conditional as this
+ pessimizes code and causes gimplification issues when applied late. */
+ (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
+ || operation_could_trap_p (cmp, true, false, @3))
+ (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
+
(for cmp (ge lt)
/* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
/* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
@@ -38,7 +38,7 @@ int main ()
}
/* Scan for c = 972195717) >> [0, 1] in function foo. */
-/* { dg-final { scan-tree-dump-times "486097858 : 972195717" 1 "vrp1" } } */
+/* { dg-final { scan-tree-dump-times "972195717 : 486097858" 1 "vrp1" } } */
/* Previously we were checking for two ?: with constant PHI arguments,
but now we collapse them into one. */
@@ -1,5 +1,5 @@
/* { dg-do compile } */
-/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-evrp-details -fdump-tree-optimized -fno-tree-ccp" } */
+/* { dg-options "-O2 -fno-tree-forwprop -fdump-tree-evrp-details -fdump-tree-optimized -fno-tree-ccp --param logical-op-non-short-circuit=1" } */
struct rtx_def;
@@ -89,5 +89,5 @@ L7:
boolean operation. */
/* { dg-final { scan-tree-dump-times "Simplified relational" 2 "evrp" } } */
-/* { dg-final { scan-tree-dump-times "if " 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "if " 3 "optimized" } } */