Skip to content

Commit 8fa00f0

Browse files
[SSAUpdater] Avoid scanning basic blocks to find instruction order.
This fixes a compile-time regression caused by llvm#116645, where an entry basic block with a very large number of allocas and other instructions caused SROA to take ~100× its expected runtime, as every alloca (with ~2 uses) now calls this method to find the order of those few instructions, rescanning the very large basic block every single time. Since this code was originally written, Instructions now have ordering numbers available to determine relative order without unnecessarily scanning the basic block.
1 parent 4564ac9 commit 8fa00f0

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

llvm/lib/Transforms/Utils/SSAUpdater.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -432,26 +432,30 @@ void LoadAndStorePromoter::run(const SmallVectorImpl<Instruction *> &Insts) {
432432
}
433433
}
434434

435-
// If so, we can queue them all as live in loads. We don't have an
436-
// efficient way to tell which on is first in the block and don't want to
437-
// scan large blocks, so just add all loads as live ins.
435+
// If so, we can queue them all as live in loads.
438436
if (!HasStore) {
439437
for (Instruction *I : BlockUses)
440438
LiveInLoads.push_back(cast<LoadInst>(I));
441439
BlockUses.clear();
442440
continue;
443441
}
444442

443+
// Sort all of the interesting instructions in the block so that we don't
444+
// have to scan a large block just to find a few instructions.
445+
std::sort(BlockUses.begin(), BlockUses.end(),
446+
[](Instruction *A, Instruction *B) { return A->comesBefore(B); });
447+
445448
// Otherwise, we have mixed loads and stores (or just a bunch of stores).
446449
// Since SSAUpdater is purely for cross-block values, we need to determine
447450
// the order of these instructions in the block. If the first use in the
448451
// block is a load, then it uses the live in value. The last store defines
449-
// the live out value. We handle this by doing a linear scan of the block.
452+
// the live out value.
450453
Value *StoredValue = nullptr;
451-
for (Instruction &I : *BB) {
452-
if (LoadInst *L = dyn_cast<LoadInst>(&I)) {
454+
for (Instruction *I : BlockUses) {
455+
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
453456
// If this is a load from an unrelated pointer, ignore it.
454-
if (!isInstInList(L, Insts)) continue;
457+
if (!isInstInList(L, Insts))
458+
continue;
455459

456460
// If we haven't seen a store yet, this is a live in use, otherwise
457461
// use the stored value.
@@ -465,14 +469,15 @@ void LoadAndStorePromoter::run(const SmallVectorImpl<Instruction *> &Insts) {
465469
continue;
466470
}
467471

468-
if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
472+
if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
469473
// If this is a store to an unrelated pointer, ignore it.
470-
if (!isInstInList(SI, Insts)) continue;
474+
if (!isInstInList(SI, Insts))
475+
continue;
471476
updateDebugInfo(SI);
472477

473478
// Remember that this is the active value in the block.
474479
StoredValue = SI->getOperand(0);
475-
} else if (auto *AI = dyn_cast<AllocaInst>(&I)) {
480+
} else if (auto *AI = dyn_cast<AllocaInst>(I)) {
476481
// Check if this an alloca, in which case we treat it as a store of
477482
// getValueToUseForAlloca.
478483
if (!isInstInList(AI, Insts))

0 commit comments

Comments
 (0)