Skip to content

Commit e2183cc

Browse files
authored
Merge pull request #12643 from dcci/styleglobt
2 parents 152c1ea + 5ece6d4 commit e2183cc

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class SILGlobalOpt {
110110

111111
void
112112
optimizeObjectAllocation(AllocRefInst *ARI,
113-
llvm::SmallVector<SILInstruction *, 4> &toRemove);
113+
llvm::SmallVector<SILInstruction *, 4> &ToRemove);
114114
void replaceFindStringCall(ApplyInst *FindStringCall);
115115

116116
SILGlobalVariable *getVariableOfGlobalInit(SILFunction *AddrF);
@@ -1221,7 +1221,7 @@ class GlobalVariableMangler : public Mangle::ASTMangler {
12211221
/// return [1, 2, 3]
12221222
/// }
12231223
void SILGlobalOpt::optimizeObjectAllocation(
1224-
AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4> &toRemove) {
1224+
AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4> &ToRemove) {
12251225

12261226
if (ARI->isObjC())
12271227
return;
@@ -1299,14 +1299,14 @@ void SILGlobalOpt::optimizeObjectAllocation(
12991299
assert(MemberStore);
13001300
ObjectArgs.push_back(Cloner.clone(
13011301
cast<SingleValueInstruction>(MemberStore->getSrc())));
1302-
toRemove.push_back(MemberStore);
1302+
ToRemove.push_back(MemberStore);
13031303
}
13041304
// Create the initializers for the tail elements.
13051305
unsigned NumBaseElements = ObjectArgs.size();
13061306
for (StoreInst *TailStore : TailStores) {
13071307
ObjectArgs.push_back(Cloner.clone(
13081308
cast<SingleValueInstruction>(TailStore->getSrc())));
1309-
toRemove.push_back(TailStore);
1309+
ToRemove.push_back(TailStore);
13101310
}
13111311
// Create the initializer for the object itself.
13121312
SILBuilder StaticInitBuilder(Glob);
@@ -1323,7 +1323,7 @@ void SILGlobalOpt::optimizeObjectAllocation(
13231323
SILInstruction *User = Use->getUser();
13241324
switch (User->getKind()) {
13251325
case SILInstructionKind::DeallocRefInst:
1326-
toRemove.push_back(User);
1326+
ToRemove.push_back(User);
13271327
break;
13281328
default:
13291329
Use->set(GVI);
@@ -1336,7 +1336,7 @@ void SILGlobalOpt::optimizeObjectAllocation(
13361336
replaceFindStringCall(FindStringCall);
13371337
}
13381338

1339-
toRemove.push_back(ARI);
1339+
ToRemove.push_back(ARI);
13401340
HasChanged = true;
13411341
}
13421342

@@ -1459,7 +1459,15 @@ bool SILGlobalOpt::run() {
14591459
for (auto &BB : F) {
14601460
bool IsCold = ColdBlocks.isCold(&BB);
14611461
auto Iter = BB.begin();
1462-
llvm::SmallVector<SILInstruction *, 4> toRemove;
1462+
1463+
// We can't remove instructions willy-nilly as we iterate because
1464+
// that might cause a pointer to the next instruction to become
1465+
// garbage, causing iterator invalidations (and crashes).
1466+
// Instead, we collect in a list the instructions we want to remove
1467+
// and erase the BB they belong to at the end of the loop, once we're
1468+
// sure it's safe to do so.
1469+
llvm::SmallVector<SILInstruction *, 4> ToRemove;
1470+
14631471
while (Iter != BB.end()) {
14641472
SILInstruction *I = &*Iter;
14651473
Iter++;
@@ -1477,11 +1485,11 @@ bool SILGlobalOpt::run() {
14771485
// for serializable functions.
14781486
// TODO: We may do the optimization _after_ serialization in the
14791487
// pass pipeline.
1480-
optimizeObjectAllocation(ARI, toRemove);
1488+
optimizeObjectAllocation(ARI, ToRemove);
14811489
}
14821490
}
14831491
}
1484-
for (auto *I : toRemove)
1492+
for (auto *I : ToRemove)
14851493
I->eraseFromParent();
14861494
}
14871495
}

0 commit comments

Comments
 (0)