From 49775e0173cb852b4f4e9e9105cddbbff4872dd3 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Fri, 22 Aug 2014 17:11:04 +0000 Subject: InstCombine: Don't unconditionally preserve 'nuw' when shrinking constants Consider: %add = add nuw i32 %a, -16777216 %and = and i32 %add, 255 Regardless of whether or not we demand the sign bit of %add, we cannot replace -16777216 with 2130706432 without also removing 'nuw' from the instruction. llvm-svn: 216273 --- .../InstCombine/InstCombineSimplifyDemanded.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index c26766a95ac..f0c96bdf768 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -44,12 +44,18 @@ static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, Demanded &= OpC->getValue(); I->setOperand(OpNo, ConstantInt::get(OpC->getType(), Demanded)); - // If 'nsw' is set and the constant is negative, removing *any* bits from the - // constant could make overflow occur. Remove 'nsw' from the instruction in - // this case. - if (auto *OBO = dyn_cast(I)) - if (OBO->hasNoSignedWrap() && OpC->getValue().isNegative()) - cast(OBO)->setHasNoSignedWrap(false); + // If either 'nsw' or 'nuw' is set and the constant is negative, + // removing *any* bits from the constant could make overflow occur. + // Remove 'nsw' and 'nuw' from the instruction in this case. + if (auto *OBO = dyn_cast(I)) { + assert(OBO->getOpcode() == Instruction::Add); + if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap()) { + if (OpC->getValue().isNegative()) { + cast(OBO)->setHasNoSignedWrap(false); + cast(OBO)->setHasNoUnsignedWrap(false); + } + } + } return true; } -- cgit v1.2.3