@@ -108,7 +108,9 @@ class SILGlobalOpt {
108
108
bool handleTailAddr (int TailIdx, SILInstruction *I,
109
109
llvm::SmallVectorImpl<StoreInst *> &TailStores);
110
110
111
- void optimizeObjectAllocation (AllocRefInst *ARI);
111
+ void
112
+ optimizeObjectAllocation (AllocRefInst *ARI,
113
+ llvm::SmallVector<SILInstruction *, 4 > &toRemove);
112
114
void replaceFindStringCall (ApplyInst *FindStringCall);
113
115
114
116
SILGlobalVariable *getVariableOfGlobalInit (SILFunction *AddrF);
@@ -1218,7 +1220,8 @@ class GlobalVariableMangler : public Mangle::ASTMangler {
1218
1220
// / func getarray() -> [Int] {
1219
1221
// / return [1, 2, 3]
1220
1222
// / }
1221
- void SILGlobalOpt::optimizeObjectAllocation (AllocRefInst *ARI) {
1223
+ void SILGlobalOpt::optimizeObjectAllocation (
1224
+ AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4 > &toRemove) {
1222
1225
1223
1226
if (ARI->isObjC ())
1224
1227
return ;
@@ -1296,14 +1299,14 @@ void SILGlobalOpt::optimizeObjectAllocation(AllocRefInst *ARI) {
1296
1299
assert (MemberStore);
1297
1300
ObjectArgs.push_back (Cloner.clone (
1298
1301
cast<SingleValueInstruction>(MemberStore->getSrc ())));
1299
- MemberStore-> eraseFromParent ( );
1302
+ toRemove. push_back (MemberStore );
1300
1303
}
1301
1304
// Create the initializers for the tail elements.
1302
1305
unsigned NumBaseElements = ObjectArgs.size ();
1303
1306
for (StoreInst *TailStore : TailStores) {
1304
1307
ObjectArgs.push_back (Cloner.clone (
1305
1308
cast<SingleValueInstruction>(TailStore->getSrc ())));
1306
- TailStore-> eraseFromParent ( );
1309
+ toRemove. push_back (TailStore );
1307
1310
}
1308
1311
// Create the initializer for the object itself.
1309
1312
SILBuilder StaticInitBuilder (Glob);
@@ -1314,12 +1317,13 @@ void SILGlobalOpt::optimizeObjectAllocation(AllocRefInst *ARI) {
1314
1317
SILBuilder B (ARI);
1315
1318
GlobalValueInst *GVI = B.createGlobalValue (ARI->getLoc (), Glob);
1316
1319
B.createStrongRetain (ARI->getLoc (), GVI, B.getDefaultAtomicity ());
1317
- while (!ARI->use_empty ()) {
1318
- Operand *Use = *ARI->use_begin ();
1320
+ llvm::SmallVector<Operand *, 8 > Worklist (ARI->use_begin (), ARI->use_end ());
1321
+ while (!Worklist.empty ()) {
1322
+ auto *Use = Worklist.pop_back_val ();
1319
1323
SILInstruction *User = Use->getUser ();
1320
1324
switch (User->getKind ()) {
1321
1325
case SILInstructionKind::DeallocRefInst:
1322
- User-> eraseFromParent ( );
1326
+ toRemove. push_back (User );
1323
1327
break ;
1324
1328
default :
1325
1329
Use->set (GVI);
@@ -1332,8 +1336,7 @@ void SILGlobalOpt::optimizeObjectAllocation(AllocRefInst *ARI) {
1332
1336
replaceFindStringCall (FindStringCall);
1333
1337
}
1334
1338
1335
- ARI->eraseFromParent ();
1336
-
1339
+ toRemove.push_back (ARI);
1337
1340
HasChanged = true ;
1338
1341
}
1339
1342
@@ -1456,6 +1459,7 @@ bool SILGlobalOpt::run() {
1456
1459
for (auto &BB : F) {
1457
1460
bool IsCold = ColdBlocks.isCold (&BB);
1458
1461
auto Iter = BB.begin ();
1462
+ llvm::SmallVector<SILInstruction *, 4 > toRemove;
1459
1463
while (Iter != BB.end ()) {
1460
1464
SILInstruction *I = &*Iter;
1461
1465
Iter++;
@@ -1473,10 +1477,12 @@ bool SILGlobalOpt::run() {
1473
1477
// for serializable functions.
1474
1478
// TODO: We may do the optimization _after_ serialization in the
1475
1479
// pass pipeline.
1476
- optimizeObjectAllocation (ARI);
1480
+ optimizeObjectAllocation (ARI, toRemove );
1477
1481
}
1478
1482
}
1479
1483
}
1484
+ for (auto *I : toRemove)
1485
+ I->eraseFromParent ();
1480
1486
}
1481
1487
}
1482
1488
0 commit comments