aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-02-15 09:16:06 +0100
committerJakub Jelinek <jakub@redhat.com>2021-02-15 09:16:06 +0100
commit70099a6acf5169eca55ef74549fb64de14e668f0 (patch)
tree33b0aba7ef68ca1124bb5a11af2c1c0b899e10c5 /gcc/match.pd
parentc5ae38e8dc3db44a137db30f14f1235160771eb2 (diff)
downloadgcc-70099a6acf5169eca55ef74549fb64de14e668f0.zip
gcc-70099a6acf5169eca55ef74549fb64de14e668f0.tar.gz
gcc-70099a6acf5169eca55ef74549fb64de14e668f0.tar.bz2
match.pd: Fix up A % (cast) (pow2cst << B) simplification [PR99079]
The (mod @0 (convert?@3 (power_of_two_cand@1 @2))) simplification uses tree_nop_conversion_p (type, TREE_TYPE (@3)) condition, but I believe it doesn't check what it was meant to check. On convert?@3 TREE_TYPE (@3) is not the type of what it has been converted from, but what it has been converted to, which needs to be (because it is operand of normal binary operation) equal or compatible to type of the modulo result and first operand - type. I could fix that by using && tree_nop_conversion_p (type, TREE_TYPE (@1)) and be done with it, but actually most of the non-nop conversions are IMHO ok and so we would regress those optimizations. In particular, if we have say narrowing conversions (foo5 and foo6 in the new testcase), I think we are fine, either the shift of the power of two constant after narrowing conversion is still that power of two (or negation of that) and then it will still work, or the result of narrowing conversion is 0 and then we would have UB which we can ignore. Similarly, widening conversions where the shift result is unsigned are fine, or even widening conversions where the shift result is signed, but we sign extend to a signed wider divisor, the problematic case of INT_MIN will become x % (long long) INT_MIN and we can still optimize that to x & (long long) INT_MAX. What doesn't work is the case in the pr99079.c testcase, widening conversion of a signed shift result to wider unsigned divisor, where if the shift is negative, we end up with x % (unsigned long long) INT_MIN which is x % 0xffffffff80000000ULL where the divisor is not a power of two and we can't optimize that to x & 0x7fffffffULL. So, the patch rejects only the single problematic case. Furthermore, when the shift result is signed, we were introducing UB into a program which previously didn't have one (well, left shift into the sign bit is UB in some language/version pairs, but it is definitely valid in C++20 - wonder if I shouldn't move the gcc.c-torture/execute/pr99079.c testcase to g++.dg/torture/pr99079.C and use -std=c++20), by adding that subtraction of 1, x % (1 << 31) in C++20 is well defined, but x & ((1 << 31) - 1) triggers UB on the subtraction. So, the patch performs the subtraction in the unsigned type if it isn't wrapping. 2021-02-15 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/99079 * match.pd (A % (pow2pcst << N) -> A & ((pow2pcst << N) - 1)): Remove useless tree_nop_conversion_p (type, TREE_TYPE (@3)) check. Instead require both type and TREE_TYPE (@1) to be integral types and either type having smaller or equal precision, or TREE_TYPE (@1) being unsigned type, or type being signed type. If TREE_TYPE (@1) doesn't have wrapping overflow, perform the subtraction of one in unsigned type. * gcc.dg/fold-modpow2-2.c: New test. * gcc.c-torture/execute/pr99079.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd23
1 files changed, 17 insertions, 6 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 17c35ee4..e14f697 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -619,12 +619,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
1); }))))))
(simplify
- (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
- (if ((TYPE_UNSIGNED (type)
- || tree_expr_nonnegative_p (@0))
- && tree_nop_conversion_p (type, TREE_TYPE (@3))
- && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
- (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
+ (mod @0 (convert? (power_of_two_cand@1 @2)))
+ (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
+ /* Allow any integral conversions of the divisor, except
+ conversion from narrower signed to wider unsigned type
+ where if @1 would be negative power of two, the divisor
+ would not be a power of two. */
+ && INTEGRAL_TYPE_P (type)
+ && INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
+ || TYPE_UNSIGNED (TREE_TYPE (@1))
+ || !TYPE_UNSIGNED (type))
+ && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
+ (with { tree utype = TREE_TYPE (@1);
+ if (!TYPE_OVERFLOW_WRAPS (utype))
+ utype = unsigned_type_for (utype); }
+ (bit_and @0 (convert (minus (convert:utype @1)
+ { build_one_cst (utype); })))))))
/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
(simplify