@@ -110,7 +110,7 @@ class SILGlobalOpt {
110
110
111
111
void
112
112
optimizeObjectAllocation (AllocRefInst *ARI,
113
- llvm::SmallVector<SILInstruction *, 4 > &toRemove );
113
+ llvm::SmallVector<SILInstruction *, 4 > &ToRemove );
114
114
void replaceFindStringCall (ApplyInst *FindStringCall);
115
115
116
116
SILGlobalVariable *getVariableOfGlobalInit (SILFunction *AddrF);
@@ -1221,7 +1221,7 @@ class GlobalVariableMangler : public Mangle::ASTMangler {
1221
1221
// / return [1, 2, 3]
1222
1222
// / }
1223
1223
void SILGlobalOpt::optimizeObjectAllocation (
1224
- AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4 > &toRemove ) {
1224
+ AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4 > &ToRemove ) {
1225
1225
1226
1226
if (ARI->isObjC ())
1227
1227
return ;
@@ -1299,14 +1299,14 @@ void SILGlobalOpt::optimizeObjectAllocation(
1299
1299
assert (MemberStore);
1300
1300
ObjectArgs.push_back (Cloner.clone (
1301
1301
cast<SingleValueInstruction>(MemberStore->getSrc ())));
1302
- toRemove .push_back (MemberStore);
1302
+ ToRemove .push_back (MemberStore);
1303
1303
}
1304
1304
// Create the initializers for the tail elements.
1305
1305
unsigned NumBaseElements = ObjectArgs.size ();
1306
1306
for (StoreInst *TailStore : TailStores) {
1307
1307
ObjectArgs.push_back (Cloner.clone (
1308
1308
cast<SingleValueInstruction>(TailStore->getSrc ())));
1309
- toRemove .push_back (TailStore);
1309
+ ToRemove .push_back (TailStore);
1310
1310
}
1311
1311
// Create the initializer for the object itself.
1312
1312
SILBuilder StaticInitBuilder (Glob);
@@ -1323,7 +1323,7 @@ void SILGlobalOpt::optimizeObjectAllocation(
1323
1323
SILInstruction *User = Use->getUser ();
1324
1324
switch (User->getKind ()) {
1325
1325
case SILInstructionKind::DeallocRefInst:
1326
- toRemove .push_back (User);
1326
+ ToRemove .push_back (User);
1327
1327
break ;
1328
1328
default :
1329
1329
Use->set (GVI);
@@ -1336,7 +1336,7 @@ void SILGlobalOpt::optimizeObjectAllocation(
1336
1336
replaceFindStringCall (FindStringCall);
1337
1337
}
1338
1338
1339
- toRemove .push_back (ARI);
1339
+ ToRemove .push_back (ARI);
1340
1340
HasChanged = true ;
1341
1341
}
1342
1342
@@ -1459,7 +1459,15 @@ bool SILGlobalOpt::run() {
1459
1459
for (auto &BB : F) {
1460
1460
bool IsCold = ColdBlocks.isCold (&BB);
1461
1461
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
+
1463
1471
while (Iter != BB.end ()) {
1464
1472
SILInstruction *I = &*Iter;
1465
1473
Iter++;
@@ -1477,11 +1485,11 @@ bool SILGlobalOpt::run() {
1477
1485
// for serializable functions.
1478
1486
// TODO: We may do the optimization _after_ serialization in the
1479
1487
// pass pipeline.
1480
- optimizeObjectAllocation (ARI, toRemove );
1488
+ optimizeObjectAllocation (ARI, ToRemove );
1481
1489
}
1482
1490
}
1483
1491
}
1484
- for (auto *I : toRemove )
1492
+ for (auto *I : ToRemove )
1485
1493
I->eraseFromParent ();
1486
1494
}
1487
1495
}
0 commit comments