]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/110838 - less aggressively fold out-of-bound shifts
authorRichard Biener <rguenther@suse.de>
Fri, 4 Aug 2023 09:24:49 +0000 (11:24 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 4 Aug 2023 10:16:00 +0000 (12:16 +0200)
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.

gcc/match.pd

index aa49574b22f819879878b4f6cad754ec67145d3e..2278029d608bf8082490724005de75336359ae83 100644 (file)
@@ -1081,6 +1081,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)))