Skip to content

Commit eddc4db

Browse files
committed
[SROA] Use SmallPtrSet for PromotableAllocas
Optimize SROA pass for large number of allocas by speeding-up PromotableAllocas erase operation. The optimization involves using SmallPtrSet which proves to be efficient since PromotableAllocas is used only for manipulating unique pointers. Signed-off-by: Bartłomiej Chmiel <[email protected]>
1 parent 67a9093 commit eddc4db

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class SROA {
198198
SmallSetVector<AllocaInst *, 16> PostPromotionWorklist;
199199

200200
/// A collection of alloca instructions we can directly promote.
201-
std::vector<AllocaInst *> PromotableAllocas;
201+
SmallPtrSet<AllocaInst *, 16> PromotableAllocas;
202202

203203
/// A worklist of PHIs to speculate prior to promoting allocas.
204204
///
@@ -4769,9 +4769,8 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
47694769

47704770
// Finally, don't try to promote any allocas that new require re-splitting.
47714771
// They have already been added to the worklist above.
4772-
llvm::erase_if(PromotableAllocas, [&](AllocaInst *AI) {
4773-
return ResplitPromotableAllocas.count(AI);
4774-
});
4772+
for (auto *RPA : ResplitPromotableAllocas)
4773+
PromotableAllocas.erase(RPA);
47754774

47764775
return true;
47774776
}
@@ -4933,7 +4932,7 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
49334932
}
49344933
if (PHIUsers.empty() && SelectUsers.empty()) {
49354934
// Promote the alloca.
4936-
PromotableAllocas.push_back(NewAI);
4935+
PromotableAllocas.insert(NewAI);
49374936
} else {
49384937
// If we have either PHIs or Selects to speculate, add them to those
49394938
// worklists and re-queue the new alloca so that we promote in on the
@@ -5568,7 +5567,9 @@ bool SROA::promoteAllocas(Function &F) {
55685567
LLVM_DEBUG(dbgs() << "Not promoting allocas with mem2reg!\n");
55695568
} else {
55705569
LLVM_DEBUG(dbgs() << "Promoting allocas with mem2reg...\n");
5571-
PromoteMemToReg(PromotableAllocas, DTU->getDomTree(), AC);
5570+
PromoteMemToReg(
5571+
std::vector(PromotableAllocas.begin(), PromotableAllocas.end()),
5572+
DTU->getDomTree(), AC);
55725573
}
55735574

55745575
PromotableAllocas.clear();
@@ -5585,7 +5586,7 @@ std::pair<bool /*Changed*/, bool /*CFGChanged*/> SROA::runSROA(Function &F) {
55855586
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
55865587
if (DL.getTypeAllocSize(AI->getAllocatedType()).isScalable() &&
55875588
isAllocaPromotable(AI))
5588-
PromotableAllocas.push_back(AI);
5589+
PromotableAllocas.insert(AI);
55895590
else
55905591
Worklist.insert(AI);
55915592
}
@@ -5609,10 +5610,10 @@ std::pair<bool /*Changed*/, bool /*CFGChanged*/> SROA::runSROA(Function &F) {
56095610
// Remove the deleted allocas from various lists so that we don't try to
56105611
// continue processing them.
56115612
if (!DeletedAllocas.empty()) {
5612-
auto IsInSet = [&](AllocaInst *AI) { return DeletedAllocas.count(AI); };
5613-
Worklist.remove_if(IsInSet);
5614-
PostPromotionWorklist.remove_if(IsInSet);
5615-
llvm::erase_if(PromotableAllocas, IsInSet);
5613+
Worklist.set_subtract(DeletedAllocas);
5614+
PostPromotionWorklist.set_subtract(DeletedAllocas);
5615+
for (auto *DA : DeletedAllocas)
5616+
PromotableAllocas.erase(DA);
56165617
DeletedAllocas.clear();
56175618
}
56185619
}

0 commit comments

Comments
 (0)