From d0a5f61cb3eb3be9966036fbaff89e3700d4ad80 Mon Sep 17 00:00:00 2001 From: jakub Date: Thu, 15 May 2014 10:01:11 +0000 Subject: PR tree-optimization/61158 * fold-const.c (fold_binary_loc): If X is zero-extended and shiftc >= prec, make sure zerobits is all ones instead of invoking undefined behavior. * gcc.dg/pr61158.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@210467 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/fold-const.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'gcc/fold-const.c') diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 5e064dfa98c..8f659db76a2 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -11972,11 +11972,17 @@ fold_binary_loc (location_t loc, /* See if we can shorten the right shift. */ if (shiftc < prec) shift_type = inner_type; + /* Otherwise X >> C1 is all zeros, so we'll optimize + it into (X, 0) later on by making sure zerobits + is all ones. */ } } zerobits = ~(unsigned HOST_WIDE_INT) 0; - zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; - zerobits <<= prec - shiftc; + if (shiftc < prec) + { + zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; + zerobits <<= prec - shiftc; + } /* For arithmetic shift if sign bit could be set, zerobits can contain actually sign bits, so no transformation is possible, unless MASK masks them all away. In that @@ -11994,7 +12000,7 @@ fold_binary_loc (location_t loc, /* ((X << 16) & 0xff00) is (X, 0). */ if ((mask & zerobits) == mask) return omit_one_operand_loc (loc, type, - build_int_cst (type, 0), arg0); + build_int_cst (type, 0), arg0); newmask = mask | zerobits; if (newmask != mask && (newmask & (newmask + 1)) == 0) -- cgit v1.2.1