Skip to content

Commit 2ead347

Browse files
committed
[SimplifyCFG] Add early bailout if Use is not in same BB.
Without this patch, passingValueIsAlwaysUndefined will iterate over all instructions from I to the end of the basic block, even if the use is outside the block. This patch adds an early bail out, if the use instruction is outside I's BB. This can greatly reduce compile-time in cases where very large basic blocks are involved, with a large number of PHI nodes and incoming values. Note that the refactoring makes the handling of the case where I is a phi and Use is in PHI more explicit as well: for phi nodes, we can also directly bail out. In the existing code, we would iterate until we reach the end and return false. Based on an earlier patch by Matt Wala. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D113293
1 parent b0de656 commit 2ead347

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6527,19 +6527,21 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
65276527

65286528
if (C->isNullValue() || isa<UndefValue>(C)) {
65296529
// Only look at the first use, avoid hurting compile time with long uselists
6530-
User *Use = *I->user_begin();
6530+
auto *Use = cast<Instruction>(*I->user_begin());
6531+
// Bail out if Use is not in the same BB as I or Use == I or Use comes
6532+
// before I in the block. The latter two can be the case if Use is a PHI
6533+
// node.
6534+
if (Use->getParent() != I->getParent() || Use == I || Use->comesBefore(I))
6535+
return false;
65316536

65326537
// Now make sure that there are no instructions in between that can alter
65336538
// control flow (eg. calls)
6534-
for (BasicBlock::iterator
6535-
i = ++BasicBlock::iterator(I),
6536-
UI = BasicBlock::iterator(dyn_cast<Instruction>(Use));
6537-
i != UI; ++i) {
6538-
if (i == I->getParent()->end())
6539-
return false;
6540-
if (!isGuaranteedToTransferExecutionToSuccessor(&*i))
6541-
return false;
6542-
}
6539+
auto InstrRange =
6540+
make_range(std::next(I->getIterator()), Use->getIterator());
6541+
if (any_of(InstrRange, [](Instruction &I) {
6542+
return !isGuaranteedToTransferExecutionToSuccessor(&I);
6543+
}))
6544+
return false;
65436545

65446546
// Look through GEPs. A load from a GEP derived from NULL is still undefined
65456547
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))

0 commit comments

Comments
 (0)