@@ -887,7 +887,7 @@ bool SILGlobalOpt::tryRemoveGlobalAddr(SILGlobalVariable *global) {
887
887
if (!isa<StoreInst>(use->getUser ()))
888
888
return false ;
889
889
}
890
- InstToRemove. addUsersOfAllResultsToWorklist (addr);
890
+
891
891
InstToRemove.add (addr);
892
892
}
893
893
@@ -898,10 +898,43 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
898
898
if (!isSafeToRemove (global))
899
899
return false ;
900
900
901
+ if (GlobalVarSkipProcessing.count (global))
902
+ return false ;
903
+
901
904
if (GlobalVarSkipProcessing.count (global) || GlobalAddrMap[global].size () ||
902
905
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
+ }
905
938
906
939
GlobalsToRemove.push_back (global);
907
940
return true ;
@@ -1161,13 +1194,9 @@ bool SILGlobalOpt::run() {
1161
1194
1162
1195
// Erase the instructions that we have marked for deletion.
1163
1196
while (!InstToRemove.isEmpty ()) {
1164
- InstToRemove.pop_back_val ()-> eraseFromParent ( );
1197
+ eraseUsesOfInstruction ( InstToRemove.pop_back_val ());
1165
1198
}
1166
1199
1167
- // After we erase some instructions, re-collect.
1168
- reset ();
1169
- collect ();
1170
-
1171
1200
for (auto &global : Module->getSILGlobals ()) {
1172
1201
HasChanged |= tryRemoveUnusedGlobal (&global);
1173
1202
}
0 commit comments