Skip to content

Commit 0083b1b

Browse files
committed
SILCombine: some refactoring
NFC
1 parent 4e3d4d2 commit 0083b1b

File tree

2 files changed

+61
-64
lines changed

2 files changed

+61
-64
lines changed

lib/SILOptimizer/SILCombiner/SILCombine.cpp

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,57 @@ class SILCombineCanonicalize final : CanonicalizeInstruction {
161161
}
162162
};
163163

164+
SILCombiner::SILCombiner(SILFunctionTransform *trans,
165+
bool removeCondFails, bool enableCopyPropagation) :
166+
parentTransform(trans),
167+
AA(trans->getPassManager()->getAnalysis<AliasAnalysis>(trans->getFunction())),
168+
DA(trans->getPassManager()->getAnalysis<DominanceAnalysis>()),
169+
PCA(trans->getPassManager()->getAnalysis<ProtocolConformanceAnalysis>()),
170+
CHA(trans->getPassManager()->getAnalysis<ClassHierarchyAnalysis>()),
171+
NLABA(trans->getPassManager()->getAnalysis<NonLocalAccessBlockAnalysis>()),
172+
Worklist("SC"),
173+
deleter(InstModCallbacks()
174+
.onDelete([&](SILInstruction *instToDelete) {
175+
// We allow for users in SILCombine to perform 2 stage
176+
// deletion, so we need to split the erasing of
177+
// instructions from adding operands to the worklist.
178+
eraseInstFromFunction(*instToDelete,
179+
false /* don't add operands */);
180+
})
181+
.onNotifyWillBeDeleted(
182+
[&](SILInstruction *instThatWillBeDeleted) {
183+
Worklist.addOperandsToWorklist(
184+
*instThatWillBeDeleted);
185+
})
186+
.onCreateNewInst([&](SILInstruction *newlyCreatedInst) {
187+
Worklist.add(newlyCreatedInst);
188+
})
189+
.onSetUseValue([&](Operand *use, SILValue newValue) {
190+
use->set(newValue);
191+
Worklist.add(use->getUser());
192+
})),
193+
deadEndBlocks(trans->getFunction()), MadeChange(false),
194+
RemoveCondFails(removeCondFails),
195+
enableCopyPropagation(enableCopyPropagation), Iteration(0),
196+
Builder(*trans->getFunction(), &TrackingList),
197+
FuncBuilder(*trans),
198+
CastOpt(
199+
FuncBuilder, nullptr /*SILBuilderContext*/,
200+
/* ReplaceValueUsesAction */
201+
[&](SILValue Original, SILValue Replacement) {
202+
replaceValueUsesWith(Original, Replacement);
203+
},
204+
/* ReplaceInstUsesAction */
205+
[&](SingleValueInstruction *I, ValueBase *V) {
206+
replaceInstUsesWith(*I, V);
207+
},
208+
/* EraseAction */
209+
[&](SILInstruction *I) { eraseInstFromFunction(*I); }),
210+
deBlocks(trans->getFunction()),
211+
ownershipFixupContext(getInstModCallbacks(), deBlocks),
212+
swiftPassInvocation(trans->getPassManager(),
213+
trans->getFunction(), this) {}
214+
164215
bool SILCombiner::trySinkOwnedForwardingInst(SingleValueInstruction *svi) {
165216
if (auto *consumingUse = svi->getSingleConsumingUse()) {
166217
auto *consumingUser = consumingUse->getUser();
@@ -455,7 +506,7 @@ bool SILCombiner::runOnFunction(SILFunction &F) {
455506
StackNesting::fixNesting(&F);
456507
}
457508

458-
// Cleanup the builder and return whether or not we made any changes.
509+
assert(TrackingList.empty() && "TrackingList should be fully processed");
459510
return Changed;
460511
}
461512

@@ -532,33 +583,18 @@ namespace {
532583

533584
class SILCombine : public SILFunctionTransform {
534585

535-
llvm::SmallVector<SILInstruction *, 64> TrackingList;
536-
537586
/// The entry point to the transformation.
538587
void run() override {
539-
auto *AA = PM->getAnalysis<AliasAnalysis>(getFunction());
540-
auto *DA = PM->getAnalysis<DominanceAnalysis>();
541-
auto *PCA = PM->getAnalysis<ProtocolConformanceAnalysis>();
542-
auto *CHA = PM->getAnalysis<ClassHierarchyAnalysis>();
543-
auto *NLABA = PM->getAnalysis<NonLocalAccessBlockAnalysis>();
544-
545588
bool enableCopyPropagation =
546589
getOptions().CopyPropagation == CopyPropagationOption::On;
547590
if (getOptions().EnableOSSAModules) {
548591
enableCopyPropagation =
549592
getOptions().CopyPropagation != CopyPropagationOption::Off;
550593
}
551594

552-
SILOptFunctionBuilder FuncBuilder(*this);
553-
// Create a SILBuilder with a tracking list for newly added
554-
// instructions, which we will periodically move to our worklist.
555-
SILBuilder B(*getFunction(), &TrackingList);
556-
SILCombiner Combiner(this, FuncBuilder, B, AA, DA, PCA, CHA, NLABA,
557-
getOptions().RemoveRuntimeAsserts,
595+
SILCombiner Combiner(this, getOptions().RemoveRuntimeAsserts,
558596
enableCopyPropagation);
559597
bool Changed = Combiner.runOnFunction(*getFunction());
560-
assert(TrackingList.empty() &&
561-
"TrackingList should be fully processed by SILCombiner");
562598

563599
if (Changed) {
564600
// Invalidate everything.

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,14 @@ class SILCombiner :
9696
/// The current iteration of the SILCombine.
9797
unsigned Iteration;
9898

99+
// The tracking list is used by `Builder` for newly added
100+
// instructions, which we will periodically move to our worklist.
101+
llvm::SmallVector<SILInstruction *, 64> TrackingList;
102+
99103
/// Builder used to insert instructions.
100-
SILBuilder &Builder;
104+
SILBuilder Builder;
105+
106+
SILOptFunctionBuilder FuncBuilder;
101107

102108
/// Cast optimizer
103109
CastOptimizer CastOpt;
@@ -114,52 +120,7 @@ class SILCombiner :
114120

115121
public:
116122
SILCombiner(SILFunctionTransform *parentTransform,
117-
SILOptFunctionBuilder &FuncBuilder, SILBuilder &B,
118-
AliasAnalysis *AA, DominanceAnalysis *DA,
119-
ProtocolConformanceAnalysis *PCA, ClassHierarchyAnalysis *CHA,
120-
NonLocalAccessBlockAnalysis *NLABA, bool removeCondFails,
121-
bool enableCopyPropagation)
122-
: parentTransform(parentTransform), AA(AA), DA(DA), PCA(PCA), CHA(CHA),
123-
NLABA(NLABA), Worklist("SC"),
124-
deleter(InstModCallbacks()
125-
.onDelete([&](SILInstruction *instToDelete) {
126-
// We allow for users in SILCombine to perform 2 stage
127-
// deletion, so we need to split the erasing of
128-
// instructions from adding operands to the worklist.
129-
eraseInstFromFunction(*instToDelete,
130-
false /* don't add operands */);
131-
})
132-
.onNotifyWillBeDeleted(
133-
[&](SILInstruction *instThatWillBeDeleted) {
134-
Worklist.addOperandsToWorklist(
135-
*instThatWillBeDeleted);
136-
})
137-
.onCreateNewInst([&](SILInstruction *newlyCreatedInst) {
138-
Worklist.add(newlyCreatedInst);
139-
})
140-
.onSetUseValue([&](Operand *use, SILValue newValue) {
141-
use->set(newValue);
142-
Worklist.add(use->getUser());
143-
})),
144-
deadEndBlocks(&B.getFunction()), MadeChange(false),
145-
RemoveCondFails(removeCondFails),
146-
enableCopyPropagation(enableCopyPropagation), Iteration(0), Builder(B),
147-
CastOpt(
148-
FuncBuilder, nullptr /*SILBuilderContext*/,
149-
/* ReplaceValueUsesAction */
150-
[&](SILValue Original, SILValue Replacement) {
151-
replaceValueUsesWith(Original, Replacement);
152-
},
153-
/* ReplaceInstUsesAction */
154-
[&](SingleValueInstruction *I, ValueBase *V) {
155-
replaceInstUsesWith(*I, V);
156-
},
157-
/* EraseAction */
158-
[&](SILInstruction *I) { eraseInstFromFunction(*I); }),
159-
deBlocks(&B.getFunction()),
160-
ownershipFixupContext(getInstModCallbacks(), deBlocks),
161-
swiftPassInvocation(parentTransform->getPassManager(),
162-
parentTransform->getFunction(), this) {}
123+
bool removeCondFails, bool enableCopyPropagation);
163124

164125
bool runOnFunction(SILFunction &F);
165126

0 commit comments

Comments
 (0)