Skip to content

Commit 1eacf8b

Browse files
committed
[SILOptimizer] SILCombine uses shared worklist.
In the previous commit, SILInstructionWorklist was added as a verbatim extraction (modulo some minor style tweaks) of SILCombineWorklist. Here, SILCombine is moved over to using that renamed type.
1 parent 4968dcc commit 1eacf8b

File tree

2 files changed

+6
-105
lines changed

2 files changed

+6
-105
lines changed

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void SILCombiner::addReachableCodeToWorklist(SILBasicBlock *BB) {
102102
}
103103

104104
static void eraseSingleInstFromFunction(SILInstruction &I,
105-
SILCombineWorklist &Worklist,
105+
SILInstructionWorklist &Worklist,
106106
bool AddOperandsToWorklist) {
107107
LLVM_DEBUG(llvm::dbgs() << "SC: ERASE " << I << '\n');
108108

@@ -127,21 +127,13 @@ static void eraseSingleInstFromFunction(SILInstruction &I,
127127
// Implementation
128128
//===----------------------------------------------------------------------===//
129129

130-
void SILCombineWorklist::add(SILInstruction *I) {
131-
if (!WorklistMap.insert(std::make_pair(I, Worklist.size())).second)
132-
return;
133-
134-
LLVM_DEBUG(llvm::dbgs() << "SC: ADD: " << *I << '\n');
135-
Worklist.push_back(I);
136-
}
137-
138130
// Define a CanonicalizeInstruction subclass for use in SILCombine.
139131
class SILCombineCanonicalize final : CanonicalizeInstruction {
140-
SILCombineWorklist &Worklist;
132+
SILInstructionWorklist &Worklist;
141133
bool changed = false;
142134

143135
public:
144-
SILCombineCanonicalize(SILCombineWorklist &Worklist)
136+
SILCombineCanonicalize(SILInstructionWorklist &Worklist)
145137
: CanonicalizeInstruction(DEBUG_TYPE), Worklist(Worklist) {}
146138

147139
void notifyNewInstruction(SILInstruction *inst) override {
@@ -270,20 +262,6 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
270262
return MadeChange;
271263
}
272264

273-
void SILCombineWorklist::addInitialGroup(ArrayRef<SILInstruction *> List) {
274-
assert(Worklist.empty() && "Worklist must be empty to add initial group");
275-
Worklist.reserve(List.size()+16);
276-
WorklistMap.reserve(List.size());
277-
LLVM_DEBUG(llvm::dbgs() << "SC: ADDING: " << List.size()
278-
<< " instrs to worklist\n");
279-
while (!List.empty()) {
280-
SILInstruction *I = List.back();
281-
List = List.slice(0, List.size()-1);
282-
WorklistMap.insert(std::make_pair(I, Worklist.size()));
283-
Worklist.push_back(I);
284-
}
285-
}
286-
287265
bool SILCombiner::runOnFunction(SILFunction &F) {
288266
clear();
289267

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "swift/SIL/SILBuilder.h"
2525
#include "swift/SIL/SILInstruction.h"
26+
#include "swift/SIL/SILInstructionWorklist.h"
2627
#include "swift/SIL/SILValue.h"
2728
#include "swift/SIL/SILVisitor.h"
2829
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
@@ -37,84 +38,6 @@ namespace swift {
3738

3839
class AliasAnalysis;
3940

40-
/// This is the worklist management logic for SILCombine.
41-
class SILCombineWorklist {
42-
llvm::SmallVector<SILInstruction *, 256> Worklist;
43-
llvm::DenseMap<SILInstruction *, unsigned> WorklistMap;
44-
45-
void operator=(const SILCombineWorklist &RHS) = delete;
46-
SILCombineWorklist(const SILCombineWorklist &Worklist) = delete;
47-
public:
48-
SILCombineWorklist() {}
49-
50-
/// Returns true if the worklist is empty.
51-
bool isEmpty() const { return Worklist.empty(); }
52-
53-
/// Add the specified instruction to the worklist if it isn't already in it.
54-
void add(SILInstruction *I);
55-
56-
/// If the given ValueBase is a SILInstruction add it to the worklist.
57-
void addValue(ValueBase *V) {
58-
if (auto *I = V->getDefiningInstruction())
59-
add(I);
60-
}
61-
62-
/// Add the given list of instructions in reverse order to the worklist. This
63-
/// routine assumes that the worklist is empty and the given list has no
64-
/// duplicates.
65-
void addInitialGroup(ArrayRef<SILInstruction *> List);
66-
67-
// If I is in the worklist, remove it.
68-
void remove(SILInstruction *I) {
69-
auto It = WorklistMap.find(I);
70-
if (It == WorklistMap.end())
71-
return; // Not in worklist.
72-
73-
// Don't bother moving everything down, just null out the slot. We will
74-
// check before we process any instruction if it is null.
75-
Worklist[It->second] = nullptr;
76-
WorklistMap.erase(It);
77-
}
78-
79-
/// Remove the top element from the worklist.
80-
SILInstruction *removeOne() {
81-
SILInstruction *I = Worklist.pop_back_val();
82-
WorklistMap.erase(I);
83-
return I;
84-
}
85-
86-
/// When an instruction has been simplified, add all of its users to the
87-
/// worklist, since additional simplifications of its users may have been
88-
/// exposed.
89-
void addUsersToWorklist(ValueBase *I) {
90-
for (auto UI : I->getUses())
91-
add(UI->getUser());
92-
}
93-
94-
void addUsersToWorklist(SILValue value) {
95-
for (auto *use : value->getUses())
96-
add(use->getUser());
97-
}
98-
99-
/// When an instruction has been simplified, add all of its users to the
100-
/// worklist, since additional simplifications of its users may have been
101-
/// exposed.
102-
void addUsersOfAllResultsToWorklist(SILInstruction *I) {
103-
for (auto result : I->getResults()) {
104-
addUsersToWorklist(result);
105-
}
106-
}
107-
108-
/// Check that the worklist is empty and nuke the backing store for the map if
109-
/// it is large.
110-
void zap() {
111-
assert(WorklistMap.empty() && "Worklist empty, but the map is not?");
112-
113-
// Do an explicit clear, this shrinks the map if needed.
114-
WorklistMap.clear();
115-
}
116-
};
117-
11841
/// This is a class which maintains the state of the combiner and simplifies
11942
/// many operations such as removing/adding instructions and syncing them with
12043
/// the worklist.
@@ -134,7 +57,7 @@ class SILCombiner :
13457
ClassHierarchyAnalysis *CHA;
13558

13659
/// Worklist containing all of the instructions primed for simplification.
137-
SILCombineWorklist Worklist;
60+
SILInstructionWorklist Worklist;
13861

13962
/// Variable to track if the SILCombiner made any changes.
14063
bool MadeChange;
@@ -156,7 +79,7 @@ class SILCombiner :
15679
AliasAnalysis *AA, DominanceAnalysis *DA,
15780
ProtocolConformanceAnalysis *PCA, ClassHierarchyAnalysis *CHA,
15881
bool removeCondFails)
159-
: AA(AA), DA(DA), PCA(PCA), CHA(CHA), Worklist(), MadeChange(false),
82+
: AA(AA), DA(DA), PCA(PCA), CHA(CHA), Worklist("SC"), MadeChange(false),
16083
RemoveCondFails(removeCondFails), Iteration(0), Builder(B),
16184
CastOpt(FuncBuilder, nullptr /*SILBuilderContext*/,
16285
/* ReplaceValueUsesAction */

0 commit comments

Comments
 (0)