The following fixes the lack of simplification of a vector shift by an out-of-bounds shift value. For scalars this is done both by CCP and VRP but vectors are not handled there. This results in PR91838 differences in outcome dependent on whether a vector shift ISA is available and thus vector lowering does or does not expose scalar shifts here.
The following adds a match.pd pattern to catch uniform out-of-bound shifts, simplifying them to zero when not sanitizing shift amounts. Bootstrapped and tested on x86_64-unknown-linux-gnu. OK? Thanks, Richard. PR tree-optimization/91838 * match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern. --- gcc/match.pd | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index a443dc48634..eace7d635e7 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1059,6 +1059,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && tree_nop_conversion_p (type, TREE_TYPE (@1))) (lshift @0 @2))) +/* Shifts by precision or greater result in zero. */ +(for shift (lshift rshift) + (simplify + (shift @0 uniform_integer_cst_p@1) + (if (!(flag_sanitize & SANITIZE_SHIFT_EXPONENT) + /* Use a signed compare to leave negative shift counts alone. */ + && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)), + element_precision (type))) + { build_zero_cst (type); }))) + /* Shifts by constants distribute over several binary operations, hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */ (for op (plus minus) -- 2.35.3