aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-01-15 21:10:44 +0100
committerJakub Jelinek <jakub@redhat.com>2021-01-15 21:10:44 +0100
commit5c046034e3ea61dd68965154a398f8f813daf8f2 (patch)
tree3bd073451c69e3dbb6f9eff9dac0a17f6f6d0e49 /gcc/match.pd
parente63c2161d09903ca5c07b1eb0d8c0a1a17967499 (diff)
downloadgcc-5c046034e3ea61dd68965154a398f8f813daf8f2.zip
gcc-5c046034e3ea61dd68965154a398f8f813daf8f2.tar.gz
gcc-5c046034e3ea61dd68965154a398f8f813daf8f2.tar.bz2
match.pd: Optimize (x < 0) ^ (y < 0) to (x ^ y) < 0 etc. [PR96681]
This patch simplifies comparisons that test the sign bit xored together. If the comparisons are both < 0 or both >= 0, then we should xor the operands together and compare the result to < 0, if the comparisons are different, we should compare to >= 0. 2021-01-15 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/96681 * match.pd ((x < 0) ^ (y < 0) to (x ^ y) < 0): New simplification. ((x >= 0) ^ (y >= 0) to (x ^ y) < 0): Likewise. ((x < 0) ^ (y >= 0) to (x ^ y) >= 0): Likewise. ((x >= 0) ^ (y < 0) to (x ^ y) >= 0): Likewise. * gcc.dg/tree-ssa/pr96681.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd18
1 files changed, 18 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index f08ab67..e731bdb 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3993,6 +3993,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (single_use (@2))
(cmp @0 @1)))))
+/* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
+ (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
+(for cmp (lt ge)
+ (simplify
+ (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+ (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
+/* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
+ (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
+(simplify
+ (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+ (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
+
/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
signed arithmetic case. That form is created by the compiler
often enough for folding it to be of value. One example is in