@@ -865,7 +865,15 @@ bool SILGlobalOpt::tryRemoveGlobalAlloc(SILGlobalVariable *global,
865
865
if (!isSafeToRemove (global))
866
866
return false ;
867
867
868
- if (GlobalAddrMap[global].size ())
868
+ // Make sure the global's address is never taken and we shouldn't skip this
869
+ // global.
870
+ if (GlobalVarSkipProcessing.count (global) ||
871
+ (GlobalAddrMap[global].size () &&
872
+ std::any_of (GlobalAddrMap[global].begin (), GlobalAddrMap[global].end (),
873
+ [=](GlobalAddrInst *addr) {
874
+ return std::find (InstToRemove.begin (), InstToRemove.end (),
875
+ addr) != InstToRemove.end ();
876
+ })))
869
877
return false ;
870
878
871
879
InstToRemove.push_back (alloc);
@@ -901,6 +909,9 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
901
909
if (GlobalVarSkipProcessing.count (global))
902
910
return false ;
903
911
912
+ // If this global is used, check if the user is going to be removed.
913
+ // Make sure none of the removed instructions are the same as this global's
914
+ // alloc instruction
904
915
if (AllocGlobalStore.count (global) &&
905
916
std::none_of (InstToRemove.begin (), InstToRemove.end (),
906
917
[=](SILInstruction *inst) {
@@ -914,27 +925,23 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
914
925
[=](SILInstruction *inst) { return GlobalVarStore[global] == inst; }))
915
926
return false ;
916
927
917
- // If this global is used, check if the user is going to be removed.
918
- if (GlobalVarSkipProcessing.count (global) || GlobalAddrMap[global].size () ||
919
- GlobalAccessMap[global].size () || GlobalLoadMap[global].size ()) {
920
- for (auto *inst : InstToRemove) {
921
- if (GlobalAddrMap[global].size () &&
922
- std::none_of (GlobalAddrMap[global].begin (),
923
- GlobalAddrMap[global].end (),
924
- [&inst](SILInstruction *addr) { return inst == addr; }))
925
- return false ;
926
- if (GlobalAccessMap[global].size () &&
927
- std::none_of (
928
- GlobalAccessMap[global].begin (), GlobalAccessMap[global].end (),
929
- [&inst](SILInstruction *access) { return inst == access; }))
930
- return false ;
931
- if (GlobalLoadMap[global].size () &&
932
- std::none_of (GlobalLoadMap[global].begin (),
933
- GlobalLoadMap[global].end (),
934
- [&inst](SILInstruction *load) { return inst == load; }))
935
- return false ;
936
- }
937
- }
928
+ // Check if any of the global_addr instructions associated with this global
929
+ // aren't going to be removed. In that case, we need to keep the global.
930
+ if (GlobalAddrMap[global].size () &&
931
+ std::any_of (GlobalAddrMap[global].begin (), GlobalAddrMap[global].end (),
932
+ [=](GlobalAddrInst *addr) {
933
+ return std::find (InstToRemove.begin (), InstToRemove.end (),
934
+ addr) != InstToRemove.end ();
935
+ }))
936
+ return false ;
937
+
938
+ if (GlobalAccessMap[global].size () &&
939
+ std::any_of (GlobalAccessMap[global].begin (),
940
+ GlobalAccessMap[global].end (), [=](BeginAccessInst *access) {
941
+ return std::find (InstToRemove.begin (), InstToRemove.end (),
942
+ access) != InstToRemove.end ();
943
+ }))
944
+ return false ;
938
945
939
946
GlobalsToRemove.push_back (global);
940
947
return true ;
@@ -968,6 +975,11 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
968
975
if (!SILG)
969
976
return ;
970
977
978
+ if (!SILG->getDecl ())
979
+ return ;
980
+
981
+ GlobalAddrMap[SILG].push_back (GAI);
982
+
971
983
if (!SILG->isLet ()) {
972
984
// We cannot determine the value for global variables which could be
973
985
// changed externally at run-time.
@@ -987,13 +999,6 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
987
999
return ;
988
1000
}
989
1001
990
- if (!SILG->getDecl ())
991
- return ;
992
-
993
- // We want to make sure that we still collect global addr instructions
994
- // that are inside the addressors.
995
- GlobalAddrMap[SILG].push_back (GAI);
996
-
997
1002
// Ignore any accesses inside addressors for SILG
998
1003
auto GlobalVar = getVariableOfGlobalInit (F);
999
1004
if (GlobalVar == SILG)
0 commit comments