diff options
author | Andrew Pinski <apinski@marvell.com> | 2023-08-09 13:49:24 -0700 |
---|---|---|
committer | Andrew Pinski <apinski@marvell.com> | 2023-08-10 23:51:07 -0700 |
commit | f956c232649e4bb7482786cd54e5d5b4085cd00a (patch) | |
tree | ca36a466e6fcf6bba9662c833b2b1e46b5d90ac2 /gcc/match.pd | |
parent | 68783211f660a501dee203aec11dae99bf9808ba (diff) | |
download | gcc-f956c232649e4bb7482786cd54e5d5b4085cd00a.zip gcc-f956c232649e4bb7482786cd54e5d5b4085cd00a.tar.gz gcc-f956c232649e4bb7482786cd54e5d5b4085cd00a.tar.bz2 |
Fix PR 110954: wrong code with cmp | !cmp
This was an oversight on my part forgetting that
cmp will might have a different true value than all ones
but will have a value of 1 in most cases.
This means if we have `(f < 0) | !(f < 0)` we would
optimize this to -1 rather than just 1.
This is version 2 of the patch.
Decided to go down a different route than just checking if
the precission was 1 inside bitwise_inverted_equal_p.
So instead bitwise_inverted_equal_p gets passed an argument
that will be set if there was a comparison that was being compared
and the user of bitwise_inverted_equal_p decides what needs to be done.
In most uses of bitwise_inverted_equal_p, the check will be
`!wascmp || element_precision (type) == 1` .
But in the case of `a & ~a` and `a ^| ~a` we can handle the case
of wascmp by using constant_boolean_node isntead.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
PR tree-optimization/110954
gcc/ChangeLog:
* generic-match-head.cc (bitwise_inverted_equal_p): Add
wascmp argument and set it accordingly.
* gimple-match-head.cc (bitwise_inverted_equal_p): Add
wascmp argument to the macro.
(gimple_bitwise_inverted_equal_p): Add
wascmp argument and set it accordingly.
* match.pd (`a & ~a`, `a ^| ~a`): Update call
to bitwise_inverted_equal_p and handle wascmp case.
(`(~x | y) & x`, `(~x | y) & x`, `a?~t:t`): Update
call to bitwise_inverted_equal_p and check to see
if was !wascmp or if precision was 1.
gcc/testsuite/ChangeLog:
* gcc.c-torture/execute/pr110954-1.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r-- | gcc/match.pd | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index fc630b6..6791060 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1179,9 +1179,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplify ~X & X as zero. */ (simplify (bit_and (convert? @0) (convert? @1)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) - && bitwise_inverted_equal_p (@0, @1)) - { build_zero_cst (type); })) + (with { bool wascmp; } + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1, wascmp)) + { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); }))) /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ (simplify @@ -1419,9 +1420,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (for op (bit_ior bit_xor) (simplify (op (convert? @0) (convert? @1)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) - && bitwise_inverted_equal_p (@0, @1)) - (convert { build_all_ones_cst (TREE_TYPE (@0)); })))) + (with { bool wascmp; } + (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)) + && bitwise_inverted_equal_p (@0, @1, wascmp)) + (convert + { wascmp + ? constant_boolean_node (true, type) + : build_all_ones_cst (TREE_TYPE (@0)); }))))) /* x ^ x -> 0 */ (simplify @@ -1969,8 +1974,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (~x & y) | x -> x | y */ (simplify (bitop:c (rbitop:c @2 @1) @0) - (if (bitwise_inverted_equal_p (@0, @2)) - (bitop @0 @1)))) + (with { bool wascmp; } + (if (bitwise_inverted_equal_p (@0, @2, wascmp) + && (!wascmp || element_precision (type) == 1)) + (bitop @0 @1))))) /* ((x | y) & z) | x -> (z & y) | x */ (simplify @@ -6464,14 +6471,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* a?~t:t -> (-(a))^t */ (simplify (cond @0 @1 @2) - (if (INTEGRAL_TYPE_P (type) - && bitwise_inverted_equal_p (@1, @2)) - (with { - auto prec = TYPE_PRECISION (type); - auto unsign = TYPE_UNSIGNED (type); - tree inttype = build_nonstandard_integer_type (prec, unsign); - } - (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))) + (with { bool wascmp; } + (if (INTEGRAL_TYPE_P (type) + && bitwise_inverted_equal_p (@1, @2, wascmp) + && (!wascmp || element_precision (type) == 1)) + (with { + auto prec = TYPE_PRECISION (type); + auto unsign = TYPE_UNSIGNED (type); + tree inttype = build_nonstandard_integer_type (prec, unsign); + } + (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))))))) #endif /* Simplify pointer equality compares using PTA. */ |