Skip to content

Commit 808c9fa

Browse files
committed
* use worklist instead of vector
* replace dead global collection with global vector * fix test
1 parent 578b15e commit 808c9fa

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/SIL/SILCloner.h"
2121
#include "swift/SIL/SILGlobalVariable.h"
2222
#include "swift/SIL/SILInstruction.h"
23+
#include "swift/SIL/SILInstructionWorklist.h"
2324
#include "swift/SILOptimizer/Analysis/ColdBlockInfo.h"
2425
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
2526
#include "swift/SILOptimizer/PassManager/Passes.h"
@@ -110,7 +111,8 @@ class SILGlobalOpt {
110111
/// function.
111112
llvm::DenseMap<SILFunction *, unsigned> InitializerCount;
112113

113-
llvm::SmallVector<SILInstruction *, 4> InstToRemove;
114+
SmallSILInstructionWorklist<4> InstToRemove;
115+
llvm::SmallVector<SILGlobalVariable *, 4> GlobalsToRemove;
114116

115117
public:
116118
SILGlobalOpt(SILOptFunctionBuilder &FunctionBuilder, SILModule *M, DominanceAnalysis *DA)
@@ -860,20 +862,21 @@ static bool isSafeToRemove(SILGlobalVariable *global) {
860862

861863
bool SILGlobalOpt::tryRemoveGlobalAlloc(SILGlobalVariable *global,
862864
AllocGlobalInst *alloc) {
863-
if (!isSafeToRemove(global)) return false;
864-
865-
if (GlobalAddrMap[global].size())
865+
if (!isSafeToRemove(global))
866866
return false;
867867

868-
InstToRemove.push_back(alloc);
868+
if (GlobalAddrMap[global].size())
869+
return false;
869870

871+
InstToRemove.add(alloc);
870872
return true;
871873
}
872874

873875
/// If there are no loads or accesses of a given global, then remove its
874876
/// associated global addr and all asssociated instructions.
875877
bool SILGlobalOpt::tryRemoveGlobalAddr(SILGlobalVariable *global) {
876-
if (!isSafeToRemove(global)) return false;
878+
if (!isSafeToRemove(global))
879+
return false;
877880

878881
if (GlobalVarSkipProcessing.count(global) || GlobalLoadMap[global].size() ||
879882
GlobalAccessMap[global].size())
@@ -884,22 +887,23 @@ bool SILGlobalOpt::tryRemoveGlobalAddr(SILGlobalVariable *global) {
884887
if (!isa<StoreInst>(use->getUser()))
885888
return false;
886889
}
887-
collectUsesOfInstructionForDeletion(addr);
888-
InstToRemove.push_back(addr);
890+
InstToRemove.addUsersOfAllResultsToWorklist(addr);
891+
InstToRemove.add(addr);
889892
}
890893

891894
return true;
892895
}
893896

894897
bool SILGlobalOpt::tryRemoveUnusedGlobal(SILGlobalVariable *global) {
895-
if (!isSafeToRemove(global)) return false;
898+
if (!isSafeToRemove(global))
899+
return false;
896900

897901
if (GlobalVarSkipProcessing.count(global) || GlobalAddrMap[global].size() ||
898902
GlobalAccessMap[global].size() || GlobalLoadMap[global].size() ||
899903
AllocGlobalStore.count(global) || GlobalVarStore.count(global))
900904
return false;
901905

902-
Module->eraseGlobalVariable(global);
906+
GlobalsToRemove.push_back(global);
903907
return true;
904908
}
905909

@@ -927,7 +931,6 @@ static LoadInst *getValidLoad(SILInstruction *I, SILValue V) {
927931

928932
/// If this is a read from a global let variable, map it.
929933
void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
930-
GAI->dump();
931934
auto *SILG = GAI->getReferencedGlobal();
932935
if (!SILG)
933936
return;
@@ -957,7 +960,7 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
957960
// We want to make sure that we still collect global addr instructions
958961
// that are inside the addressors.
959962
GlobalAddrMap[SILG].push_back(GAI);
960-
963+
961964
// Ignore any accesses inside addressors for SILG
962965
auto GlobalVar = getVariableOfGlobalInit(F);
963966
if (GlobalVar == SILG)
@@ -1055,22 +1058,6 @@ void SILGlobalOpt::reset() {
10551058
GlobalInitCallMap.clear();
10561059
}
10571060

1058-
void SILGlobalOpt::collectUsesOfInstructionForDeletion(SILInstruction *inst) {
1059-
for (auto result : inst->getResults()) {
1060-
for (auto *use : result->getUses()) {
1061-
auto *user = use->getUser();
1062-
assert(user && "User should never be NULL!");
1063-
assert(use->get()->use_begin() == use->get()->use_end()
1064-
&& "All collected uses must not be used themselves.");
1065-
1066-
// If the instruction itself has any uses, recursively zap them so that
1067-
// nothing uses this instruction.
1068-
collectUsesOfInstructionForDeletion(user);
1069-
InstToRemove.push_back(user);
1070-
}
1071-
}
1072-
}
1073-
10741061
void SILGlobalOpt::collect() {
10751062
for (auto &F : *Module) {
10761063
// TODO: Add support for ownership.
@@ -1173,22 +1160,20 @@ bool SILGlobalOpt::run() {
11731160
}
11741161

11751162
// Erase the instructions that we have marked for deletion.
1176-
for (auto *inst : InstToRemove) {
1163+
while (auto *inst = InstToRemove.pop_back_val()) {
11771164
inst->eraseFromParent();
11781165
}
11791166

11801167
// After we erase some instructions, re-collect.
11811168
reset();
11821169
collect();
11831170

1184-
// Copy the globals so we don't get issues with modifying while iterating.
1185-
SmallVector<SILGlobalVariable *, 12> globals;
11861171
for (auto &global : Module->getSILGlobals()) {
1187-
globals.push_back(&global);
1172+
HasChanged |= tryRemoveUnusedGlobal(&global);
11881173
}
11891174

1190-
for (auto *global : globals) {
1191-
HasChanged |= tryRemoveUnusedGlobal(global);
1175+
for (auto *global : GlobalsToRemove) {
1176+
Module->eraseGlobalVariable(global);
11921177
}
11931178

11941179
// Reset incase we re-run this function (when HasChanged is true).

test/SIL/Serialization/perf_inline_without_inline_all.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ import Swift
1616
// CHECK-NEXT: integer_literal
1717
// CHECK-NEXT: return
1818

19-
var a = doSomething()
19+
public var a = doSomething()
2020
a.isBConfused()

0 commit comments

Comments
 (0)