From cf0e3e88dfb351bdd5d33ee0d88f549a2385adf0 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 11 Mar 2011 09:00:19 +0000 Subject: [PATCH] Teach ComputeMaskedBits about nsw on add. I don't think there's anything we can do with nuw here, but sub and mul should be given similar treatment. Fixes PR9343 #15! llvm-svn: 127463 --- lib/Analysis/ValueTracking.cpp | 14 ++++++++++++++ test/Transforms/InstSimplify/compare.ll | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index f05ad66b87d..33fb1e8cdc8 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -429,6 +429,20 @@ void llvm::ComputeMaskedBits(Value *V, const APInt &Mask, KnownZero |= LHSKnownZero & Mask; KnownOne |= LHSKnownOne & Mask; } + + // Are we still trying to solve for the sign bit? + if (Mask.isNegative() && !KnownZero.isNegative() && !KnownOne.isNegative()){ + OverflowingBinaryOperator *OBO = cast(I); + if (OBO->hasNoSignedWrap()) { + // Adding two positive numbers can't wrap into negative ... + if (LHSKnownZero.isNegative() && KnownZero2.isNegative()) + KnownZero |= APInt::getSignBit(BitWidth); + // and adding two negative numbers can't wrap into positive. + else if (LHSKnownOne.isNegative() && KnownOne2.isNegative()) + KnownOne |= APInt::getSignBit(BitWidth); + } + } + return; } case Instruction::SRem: diff --git a/test/Transforms/InstSimplify/compare.ll b/test/Transforms/InstSimplify/compare.ll index b5146ee7407..75a36b499e3 100644 --- a/test/Transforms/InstSimplify/compare.ll +++ b/test/Transforms/InstSimplify/compare.ll @@ -261,6 +261,16 @@ define i1 @srem1(i32 %X) { ; CHECK: ret i1 false } +; PR9343 #15 +; CHECK: @srem2 +; CHECK: ret i1 false +define i1 @srem2(i16 %X, i32 %Y) { + %A = zext i16 %X to i32 + %B = add nsw i32 %A, 1 + %C = srem i32 %B, %Y + %D = icmp slt i32 %C, 0 + ret i1 %D +} define i1 @udiv1(i32 %X) { ; CHECK: @udiv1 %A = udiv i32 %X, 1000000