summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2008-07-10 05:51:40 +0000
committerNick Lewycky <nicholas@mxc.ca>2008-07-10 05:51:40 +0000
commit5dcc41f5b3daf6ef7097f169767291ff9cd0dd0b (patch)
tree5feccea7217ec8cf6427f2d294fd31e406c87817 /lib/Transforms/Scalar
parent743a1e6a9ced9152e24a70b7dd87153eb55c772f (diff)
downloadllvm-5dcc41f5b3daf6ef7097f169767291ff9cd0dd0b.tar.gz
llvm-5dcc41f5b3daf6ef7097f169767291ff9cd0dd0b.tar.bz2
llvm-5dcc41f5b3daf6ef7097f169767291ff9cd0dd0b.tar.xz
Fix overzealous optimization. Thanks to Duncan Sands for pointing out my error!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53393 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index c573f7f531..e0e3f49be1 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -3462,16 +3462,21 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
return BinaryOperator::CreateAnd(V, AndRHS);
- // (A - N) & AndRHS -> -N & AndRHS where A & AndRHS == 0
- if (Op0I->hasOneUse() && MaskedValueIsZero(Op0LHS, AndRHSMask)) {
+ // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
+ // has 1's for all bits that the subtraction with A might affect.
+ if (Op0I->hasOneUse()) {
+ uint32_t BitWidth = AndRHSMask.getBitWidth();
+ uint32_t Zeros = AndRHSMask.countLeadingZeros();
+ APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros);
+
ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
- if (!A || !A->isZero()) {
+ if (!(A && A->isZero()) && // avoid infinite recursion.
+ MaskedValueIsZero(Op0LHS, Mask)) {
Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
InsertNewInstBefore(NewNeg, I);
return BinaryOperator::CreateAnd(NewNeg, AndRHS);
}
}
-
break;
case Instruction::Shl: