diff options
author | David Majnemer <david.majnemer@gmail.com> | 2015-04-22 22:42:05 +0000 |
---|---|---|
committer | David Majnemer <david.majnemer@gmail.com> | 2015-04-22 22:42:05 +0000 |
commit | f9c92b069a1a6f73ec41b7741b2ac6b7740c36a6 (patch) | |
tree | c834e55e270e23c4885db63255850ddd8d462cb3 | |
parent | d9b72fea112333a8998f1185d454eae1496523bd (diff) |
[InstCombine] Use a more targeted fix instead of r235544
Only clear out the NSW/NUW flags if we are optimizing 'add'/'sub' while
taking advantage that the sign bit is not set. We do this optimization
to further shrink the mask but shrinking the mask isn't NSW/NUW
preserving in this case.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235558 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp | 17 | ||||
-rw-r--r-- | test/Transforms/InstCombine/cast.ll | 12 |
2 files changed, 20 insertions, 9 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index 0695ec17e3..3dbb1b190b 100644 --- a/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -88,13 +88,6 @@ bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask, KnownOne, Depth, UserI); if (!NewVal) return false; U = NewVal; - - // Shrinking a constant might cause a nsw/nuw violation to occur in - // instructions which are themselves demanded. - if (auto *UserOBO = dyn_cast<OverflowingBinaryOperator>(UserI)) { - cast<BinaryOperator>(UserOBO)->setHasNoSignedWrap(false); - cast<BinaryOperator>(UserOBO)->setHasNoUnsignedWrap(false); - } return true; } @@ -607,8 +600,11 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, LHSKnownZero, LHSKnownOne, Depth + 1) || SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, - LHSKnownZero, LHSKnownOne, Depth + 1)) + LHSKnownZero, LHSKnownOne, Depth + 1)) { + cast<BinaryOperator>(I)->setHasNoSignedWrap(false); + cast<BinaryOperator>(I)->setHasNoUnsignedWrap(false); return I; + } } } break; @@ -624,8 +620,11 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, LHSKnownZero, LHSKnownOne, Depth + 1) || SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, - LHSKnownZero, LHSKnownOne, Depth + 1)) + LHSKnownZero, LHSKnownOne, Depth + 1)) { + cast<BinaryOperator>(I)->setHasNoSignedWrap(false); + cast<BinaryOperator>(I)->setHasNoUnsignedWrap(false); return I; + } } // Otherwise just hand the sub off to computeKnownBits to fill in diff --git a/test/Transforms/InstCombine/cast.ll b/test/Transforms/InstCombine/cast.ll index 7bf4a6047f..8a4a60e875 100644 --- a/test/Transforms/InstCombine/cast.ll +++ b/test/Transforms/InstCombine/cast.ll @@ -1125,3 +1125,15 @@ define i1 @PR23309(i32 %A, i32 %B) { %trunc = trunc i32 %sub to i1 ret i1 %trunc } + +define i1 @PR23309v2(i32 %A, i32 %B) { +; CHECK-LABEL: @PR23309v2( +; CHECK-NEXT: %[[sub:.*]] = add i32 %A, %B +; CHECK-NEXT: %[[and:.*]] = and i32 %[[sub]], 1 +; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32 %[[and]], 0 +; CHECK-NEXT: ret i1 %[[cmp]] + %add = add i32 %A, -4 + %sub = add nuw i32 %add, %B + %trunc = trunc i32 %sub to i1 + ret i1 %trunc +} |