Skip to content
This repository was archived by the owner on Sep 2, 2018. It is now read-only.

Commit 9426133

Browse files
committed
Move logic from JumpThreading into LazyValue info to simplify caller.
This change is hopefully NFC. The only tricky part is that I changed the context instruction being used to the branch rather than the comparison. I believe both to be correct, but the branch is strictly more powerful. With the moved code, using the branch instruction is required for the basic block comparison test to return the same result. The previous code was able to directly access both the branch and the comparison where the revised code is not. Differential Revision: http://reviews.llvm.org/D9652 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239797 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5449852 commit 9426133

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

lib/Analysis/LazyValueInfo.cpp

+34-2
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,40 @@ LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C,
12621262
Instruction *CxtI) {
12631263
const DataLayout &DL = CxtI->getModule()->getDataLayout();
12641264
LVILatticeVal Result = getCache(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
1265-
1266-
return getPredicateResult(Pred, C, Result, DL, TLI);
1265+
Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
1266+
if (Ret != Unknown)
1267+
return Ret;
1268+
1269+
// TODO: Move this logic inside getValueAt so that it can be cached rather
1270+
// than re-queried on each call. This would also allow us to merge the
1271+
// underlying lattice values to get more information
1272+
if (CxtI) {
1273+
// For a comparison where the V is outside this block, it's possible
1274+
// that we've branched on it before. Look to see if the value is known
1275+
// on all incoming edges.
1276+
BasicBlock *BB = CxtI->getParent();
1277+
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1278+
if (PI != PE &&
1279+
(!isa<Instruction>(V) ||
1280+
cast<Instruction>(V)->getParent() != BB)) {
1281+
// For predecessor edge, determine if the comparison is true or false
1282+
// on that edge. If they're all true or all false, we can conclude
1283+
// the value of the comparison in this block.
1284+
Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1285+
if (Baseline != Unknown) {
1286+
// Check that all remaining incoming values match the first one.
1287+
while (++PI != PE) {
1288+
Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1289+
if (Ret != Baseline) break;
1290+
}
1291+
// If we terminated early, then one of the values didn't match.
1292+
if (PI == PE) {
1293+
return Baseline;
1294+
}
1295+
}
1296+
}
1297+
}
1298+
return Unknown;
12671299
}
12681300

12691301
void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,

lib/Transforms/Scalar/JumpThreading.cpp

+17-51
Original file line numberDiff line numberDiff line change
@@ -758,67 +758,33 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) {
758758

759759

760760
if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
761-
// For a comparison where the LHS is outside this block, it's possible
762-
// that we've branched on it before. Used LVI to see if we can simplify
763-
// the branch based on that.
761+
// If we're branching on a conditional, LVI might be able to determine
762+
// it's value at the the branch instruction. We only handle comparisons
763+
// against a constant at this time.
764+
// TODO: This should be extended to handle switches as well.
764765
BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
765766
Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1));
766-
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
767-
if (CondBr && CondConst && CondBr->isConditional() && PI != PE &&
768-
(!isa<Instruction>(CondCmp->getOperand(0)) ||
769-
cast<Instruction>(CondCmp->getOperand(0))->getParent() != BB)) {
770-
// For predecessor edge, determine if the comparison is true or false
771-
// on that edge. If they're all true or all false, we can simplify the
772-
// branch.
773-
// FIXME: We could handle mixed true/false by duplicating code.
774-
LazyValueInfo::Tristate Baseline =
775-
LVI->getPredicateOnEdge(CondCmp->getPredicate(), CondCmp->getOperand(0),
776-
CondConst, *PI, BB, CondCmp);
777-
if (Baseline != LazyValueInfo::Unknown) {
778-
// Check that all remaining incoming values match the first one.
779-
while (++PI != PE) {
780-
LazyValueInfo::Tristate Ret =
781-
LVI->getPredicateOnEdge(CondCmp->getPredicate(),
782-
CondCmp->getOperand(0), CondConst, *PI, BB,
783-
CondCmp);
784-
if (Ret != Baseline) break;
785-
}
786-
787-
// If we terminated early, then one of the values didn't match.
788-
if (PI == PE) {
789-
unsigned ToRemove = Baseline == LazyValueInfo::True ? 1 : 0;
790-
unsigned ToKeep = Baseline == LazyValueInfo::True ? 0 : 1;
791-
CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
792-
BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
793-
CondBr->eraseFromParent();
794-
if (CondCmp->use_empty())
795-
CondCmp->eraseFromParent();
796-
else if (CondCmp->getParent() == BB) {
797-
// If the fact we just learned is true for all uses of the
798-
// condition, replace it with a constant value
799-
auto *CI = Baseline == LazyValueInfo::True ?
800-
ConstantInt::getTrue(CondCmp->getType()) :
801-
ConstantInt::getFalse(CondCmp->getType());
802-
CondCmp->replaceAllUsesWith(CI);
803-
CondCmp->eraseFromParent();
804-
}
805-
return true;
806-
}
807-
}
808-
809-
} else if (CondBr && CondConst && CondBr->isConditional()) {
810-
// There might be an invariant in the same block with the conditional
811-
// that can determine the predicate.
812-
767+
if (CondBr && CondConst && CondBr->isConditional()) {
813768
LazyValueInfo::Tristate Ret =
814769
LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
815-
CondConst, CondCmp);
770+
CondConst, CondBr);
816771
if (Ret != LazyValueInfo::Unknown) {
817772
unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0;
818773
unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
819774
CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
820775
BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
821776
CondBr->eraseFromParent();
777+
if (CondCmp->use_empty())
778+
CondCmp->eraseFromParent();
779+
else if (CondCmp->getParent() == BB) {
780+
// If the fact we just learned is true for all uses of the
781+
// condition, replace it with a constant value
782+
auto *CI = Ret == LazyValueInfo::True ?
783+
ConstantInt::getTrue(CondCmp->getType()) :
784+
ConstantInt::getFalse(CondCmp->getType());
785+
CondCmp->replaceAllUsesWith(CI);
786+
CondCmp->eraseFromParent();
787+
}
822788
return true;
823789
}
824790
}

0 commit comments

Comments
 (0)