Skip to content

Commit 9cd11a6

Browse files
committed
Add optimization to remove unused global private variables
1 parent f5f214d commit 9cd11a6

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class SILGlobalOpt {
7373
/// A map from each visited global let variable to the store instructions
7474
/// which initialize it.
7575
llvm::MapVector<SILGlobalVariable *, StoreInst *> GlobalVarStore;
76+
77+
llvm::MapVector<SILGlobalVariable *, AllocGlobalInst *> AllocGlobalStore;
7678

7779
/// A set of visited global variables that for some reason we have decided is
7880
/// not able to be optimized safely or for which we do not know how to
@@ -117,6 +119,9 @@ class SILGlobalOpt {
117119

118120
/// This is the main entrypoint for collecting global accesses.
119121
void collectGlobalAccess(GlobalAddrInst *GAI);
122+
123+
void collectAllocGlobal(SILGlobalVariable *global,
124+
AllocGlobalInst *allocGlobal);
120125

121126
/// Returns true if we think that \p CurBB is inside a loop.
122127
bool isInLoop(SILBasicBlock *CurBB);
@@ -878,6 +883,10 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
878883
}
879884
}
880885

886+
void SILGlobalOpt::collectAllocGlobal(SILGlobalVariable *global, AllocGlobalInst *allocGlobal) {
887+
AllocGlobalStore[global] = allocGlobal;
888+
}
889+
881890
// Optimize access to the global variable, which is known to have a constant
882891
// value. Replace all loads from the global address by invocations of a getter
883892
// that returns the value of this variable.
@@ -955,12 +964,15 @@ bool SILGlobalOpt::run() {
955964
continue;
956965
}
957966

958-
auto *GAI = dyn_cast<GlobalAddrInst>(&I);
959-
if (!GAI) {
967+
if (auto *GAI = dyn_cast<GlobalAddrInst>(&I)) {
968+
collectGlobalAccess(GAI);
960969
continue;
961970
}
962971

963-
collectGlobalAccess(GAI);
972+
if (auto *allocGlobal = dyn_cast<AllocGlobalInst>(&I)) {
973+
collectAllocGlobal(allocGlobal->getReferencedGlobal(), allocGlobal);
974+
continue;
975+
}
964976
}
965977
}
966978
}
@@ -974,6 +986,24 @@ bool SILGlobalOpt::run() {
974986
for (auto &Init : GlobalVarStore) {
975987
// Optimize the access to globals if possible.
976988
optimizeGlobalAccess(Init.first, Init.second);
989+
990+
if (Init.first->getLinkage() != SILLinkage::Private) continue;
991+
bool canRemove = true;
992+
auto globalAddr = cast<GlobalAddrInst>(Init.second->getDest());
993+
for (auto *use : globalAddr->getUses()) {
994+
if (!isa<StoreInst>(use->getUser())) {
995+
canRemove = false;
996+
break;
997+
}
998+
}
999+
if (canRemove) {
1000+
Init.second->eraseFromParent();
1001+
assert(globalAddr->getUses().begin() == globalAddr->getUses().end());
1002+
globalAddr->eraseFromParent();
1003+
if (AllocGlobalStore.count(Init.first))
1004+
AllocGlobalStore[Init.first]->eraseFromParent();
1005+
Init.first->getModule().eraseGlobalVariable(Init.first);
1006+
}
9771007
}
9781008

9791009
return HasChanged;

0 commit comments

Comments
 (0)