Skip to content

Commit 496955f

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 (cherry-picked from 2ead347)
1 parent 62a4708 commit 496955f

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
@@ -6548,19 +6548,21 @@ static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I, bool PtrValu
65486548

65496549
if (C->isNullValue() || isa<UndefValue>(C)) {
65506550
// Only look at the first use, avoid hurting compile time with long uselists
6551-
User *Use = *I->user_begin();
6551+
auto *Use = cast<Instruction>(*I->user_begin());
6552+
// Bail out if Use is not in the same BB as I or Use == I or Use comes
6553+
// before I in the block. The latter two can be the case if Use is a PHI
6554+
// node.
6555+
if (Use->getParent() != I->getParent() || Use == I || Use->comesBefore(I))
6556+
return false;
65526557

65536558
// Now make sure that there are no instructions in between that can alter
65546559
// control flow (eg. calls)
6555-
for (BasicBlock::iterator
6556-
i = ++BasicBlock::iterator(I),
6557-
UI = BasicBlock::iterator(dyn_cast<Instruction>(Use));
6558-
i != UI; ++i) {
6559-
if (i == I->getParent()->end())
6560-
return false;
6561-
if (!isGuaranteedToTransferExecutionToSuccessor(&*i))
6562-
return false;
6563-
}
6560+
auto InstrRange =
6561+
make_range(std::next(I->getIterator()), Use->getIterator());
6562+
if (any_of(InstrRange, [](Instruction &I) {
6563+
return !isGuaranteedToTransferExecutionToSuccessor(&I);
6564+
}))
6565+
return false;
65646566

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

0 commit comments

Comments
 (0)