https://gcc.gnu.org/g:b170ab1d051dc97b2e5c03984dbe406939364adc
commit r13-8499-gb170ab1d051dc97b2e5c03984dbe406939364adc Author: Richard Biener <rguent...@suse.de> Date: Fri Aug 4 11:24:49 2023 +0200 tree-optimization/110838 - less aggressively fold out-of-bound shifts The following adjusts the shift simplification patterns to avoid touching out-of-bound shift value arithmetic right shifts of possibly negative values. While simplifying those to zero isn't wrong it's violating the principle of least surprise. PR tree-optimization/110838 * match.pd (([rl]shift @0 out-of-bounds) -> zero): Restrict the arithmetic right-shift case to non-negative operands. (cherry picked from commit 04aa0edcace22a7815cfc57575f1f7b1f166ac10) Diff: --- gcc/match.pd | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 6812853df67..47e48fa2ca5 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1002,6 +1002,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (shift @0 uniform_integer_cst_p@1) (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)) + /* Leave arithmetic right shifts of possibly negative values alone. */ + && (TYPE_UNSIGNED (type) + || shift == LSHIFT_EXPR + || tree_expr_nonnegative_p (@0)) /* Use a signed compare to leave negative shift counts alone. */ && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)), element_precision (type)))