]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Implement some of fold_binary_op_with_conditional_arg in match.pd
authorRichard Biener <rguenther@suse.de>
Wed, 11 May 2022 09:53:53 +0000 (11:53 +0200)
committerRichard Biener <rguenther@suse.de>
Wed, 11 May 2022 12:34:30 +0000 (14:34 +0200)
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.

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
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr61839_1.c
gcc/testsuite/gcc.dg/tree-ssa/vrp24.c

index e11a736b343725e2e89f83126c47836a3d1a72da..cb0fbd32fa6fbea7b3c96462bf54abe891396fd6 100644 (file)
@@ -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
index 632243ea92e20cc3503d033f4cf60e142ab67c6f..f5efa77560c5b606317c0841b657fe044243d305 100644 (file)
@@ -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). */
index f5af7a1d6b6758f6a1566d17138880e429bb0fda..d41256736a2d056780774ebdfc16e3f19d76a73a 100644 (file)
@@ -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.  */
index 91015da86aebe385c6d47256d2e8e1e0782bc640..c28ca473fc6c2083b8427f3315b7b5530eceadba 100644 (file)
@@ -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" } } */