Skip to content

Commit b81e47b

Browse files
committed
Remove double-collect
1 parent 6a3d32b commit b81e47b

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ bool SILGlobalOpt::tryRemoveGlobalAddr(SILGlobalVariable *global) {
887887
if (!isa<StoreInst>(use->getUser()))
888888
return false;
889889
}
890-
InstToRemove.addUsersOfAllResultsToWorklist(addr);
890+
891891
InstToRemove.add(addr);
892892
}
893893

@@ -898,10 +898,43 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
898898
if (!isSafeToRemove(global))
899899
return false;
900900

901+
if (GlobalVarSkipProcessing.count(global))
902+
return false;
903+
901904
if (GlobalVarSkipProcessing.count(global) || GlobalAddrMap[global].size() ||
902905
GlobalAccessMap[global].size() || GlobalLoadMap[global].size() ||
903-
AllocGlobalStore.count(global) || GlobalVarStore.count(global))
904-
return false;
906+
AllocGlobalStore.count(global) || GlobalVarStore.count(global)) {
907+
SmallVector<SILInstruction *, 4> deadInsts;
908+
while (!InstToRemove.isEmpty()) {
909+
auto *inst = InstToRemove.pop_back_val();
910+
deadInsts.push_back(inst);
911+
}
912+
InstToRemove.addInitialGroup(deadInsts);
913+
914+
for (auto *inst : deadInsts) {
915+
if (GlobalAddrMap[global].size() &&
916+
!std::any_of(GlobalAddrMap[global].begin(),
917+
GlobalAddrMap[global].end(),
918+
[&inst](SILInstruction *addr) { return inst == addr; }))
919+
return false;
920+
if (GlobalAccessMap[global].size() &&
921+
!std::any_of(
922+
GlobalAccessMap[global].begin(), GlobalAccessMap[global].end(),
923+
[&inst](SILInstruction *access) { return inst == access; }))
924+
return false;
925+
if (GlobalLoadMap[global].size() &&
926+
!std::any_of(GlobalLoadMap[global].begin(),
927+
GlobalLoadMap[global].end(),
928+
[&inst](SILInstruction *load) { return inst == load; }))
929+
return false;
930+
931+
if (AllocGlobalStore.count(global) && AllocGlobalStore[global] != inst)
932+
return false;
933+
934+
if (GlobalVarStore.count(global) && GlobalVarStore[global] != inst)
935+
return false;
936+
}
937+
}
905938

906939
GlobalsToRemove.push_back(global);
907940
return true;
@@ -1161,13 +1194,9 @@ bool SILGlobalOpt::run() {
11611194

11621195
// Erase the instructions that we have marked for deletion.
11631196
while (!InstToRemove.isEmpty()) {
1164-
InstToRemove.pop_back_val()->eraseFromParent();
1197+
eraseUsesOfInstruction(InstToRemove.pop_back_val());
11651198
}
11661199

1167-
// After we erase some instructions, re-collect.
1168-
reset();
1169-
collect();
1170-
11711200
for (auto &global : Module->getSILGlobals()) {
11721201
HasChanged |= tryRemoveUnusedGlobal(&global);
11731202
}

0 commit comments

Comments
 (0)