Skip to content

Commit 7304d5a

Browse files
committed
Fix logic around checking dead instructions
1 parent af84de8 commit 7304d5a

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,15 @@ bool SILGlobalOpt::tryRemoveGlobalAlloc(SILGlobalVariable *global,
865865
if (!isSafeToRemove(global))
866866
return false;
867867

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+
})))
869877
return false;
870878

871879
InstToRemove.push_back(alloc);
@@ -901,6 +909,9 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
901909
if (GlobalVarSkipProcessing.count(global))
902910
return false;
903911

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
904915
if (AllocGlobalStore.count(global) &&
905916
std::none_of(InstToRemove.begin(), InstToRemove.end(),
906917
[=](SILInstruction *inst) {
@@ -914,27 +925,23 @@ bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
914925
[=](SILInstruction *inst) { return GlobalVarStore[global] == inst; }))
915926
return false;
916927

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;
938945

939946
GlobalsToRemove.push_back(global);
940947
return true;
@@ -968,6 +975,11 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
968975
if (!SILG)
969976
return;
970977

978+
if (!SILG->getDecl())
979+
return;
980+
981+
GlobalAddrMap[SILG].push_back(GAI);
982+
971983
if (!SILG->isLet()) {
972984
// We cannot determine the value for global variables which could be
973985
// changed externally at run-time.
@@ -987,13 +999,6 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
987999
return;
9881000
}
9891001

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-
9971002
// Ignore any accesses inside addressors for SILG
9981003
auto GlobalVar = getVariableOfGlobalInit(F);
9991004
if (GlobalVar == SILG)

0 commit comments

Comments
 (0)