https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115154
Andrew Pinski <pinskia at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Known to work|14.1.0 | Summary|[13 Regression] wrong code |[13/14/15 Regression] wrong |at optimization levels -O2, |code at optimization levels |-O3, -Os since |-O2, -O3, -Os since |r13-7434-g682bbd364708fe |r13-7434-g682bbd364708fe --- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> --- This works and might be the best for GCC 13/14 but maybe I think we can do better. ``` diff --git a/gcc/match.pd b/gcc/match.pd index 0f9c34fa897..6bc512cabdf 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2400,7 +2400,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2)) (if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (@0)) - && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))) + && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)) + && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1)) (mult (convert @1) (convert @2)))) /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p. ``` For bit 1 precision, look at the lower bit of the INTEGER_CST and decide if it should be @1 or `0` and that would avoid the overflow too. Note you can reproduce the failure on the trunk and GCC 14 by using -fsanitize=undefined which I missed in the original bug report and that is because r14-3432-gddd64a6ec3b38e is disable for undefined sanitizers case.