Skip to content

Commit 113c22a

Browse files
committed
[sil] Move partial apply combiner code from SILCombiner into InstOptUtils.h/SILCombinerApplyVisitor.cpp.
This is in preparation for moving this into the mandatory combiner.
1 parent 7309868 commit 113c22a

File tree

6 files changed

+411
-370
lines changed

6 files changed

+411
-370
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,21 @@ bool tryCheckedCastBrJumpThreading(
161161
/// A structure containing callbacks that are called when an instruction is
162162
/// removed or added.
163163
struct InstModCallbacks {
164-
using CallbackTy = std::function<void(SILInstruction *)>;
165-
CallbackTy deleteInst = [](SILInstruction *inst) { inst->eraseFromParent(); };
166-
CallbackTy createdNewInst = [](SILInstruction *) {};
167-
168-
InstModCallbacks(CallbackTy deleteInst, CallbackTy createdNewInst)
169-
: deleteInst(deleteInst), createdNewInst(createdNewInst) {}
164+
std::function<void(SILInstruction *)> deleteInst = [](SILInstruction *inst) {
165+
inst->eraseFromParent();
166+
};
167+
std::function<void(SILInstruction *)> createdNewInst = [](SILInstruction *) {
168+
};
169+
std::function<void(SILValue, SILValue)> replaceValueUsesWith =
170+
[](SILValue oldValue, SILValue newValue) {
171+
oldValue->replaceAllUsesWith(newValue);
172+
};
173+
174+
InstModCallbacks(decltype(deleteInst) deleteInst,
175+
decltype(createdNewInst) createdNewInst,
176+
decltype(replaceValueUsesWith) replaceValueUsesWith)
177+
: deleteInst(deleteInst), createdNewInst(createdNewInst),
178+
replaceValueUsesWith(replaceValueUsesWith) {}
170179
InstModCallbacks() = default;
171180
~InstModCallbacks() = default;
172181
InstModCallbacks(const InstModCallbacks &) = default;
@@ -391,6 +400,10 @@ findLocalApplySites(FunctionRefBaseInst *fri);
391400
/// Gets the base implementation of a method.
392401
AbstractFunctionDecl *getBaseMethod(AbstractFunctionDecl *FD);
393402

403+
SILInstruction *
404+
tryOptimizeApplyOfPartialApply(PartialApplyInst *pai, SILBuilder &builder,
405+
InstModCallbacks callbacks = InstModCallbacks());
406+
394407
} // end namespace swift
395408

396409
#endif

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ class MandatoryCombiner final
7777
SmallVector<SILInstruction *, 16> instructionsPendingDeletion;
7878

7979
public:
80-
MandatoryCombiner(
81-
SmallVectorImpl<SILInstruction *> &createdInstructions)
80+
MandatoryCombiner(SmallVectorImpl<SILInstruction *> &createdInstructions)
8281
: worklist("MC"), madeChange(false), iteration(0),
8382
instModCallbacks(
8483
[&](SILInstruction *instruction) {
8584
worklist.erase(instruction);
8685
instructionsPendingDeletion.push_back(instruction);
8786
},
88-
[&](SILInstruction *instruction) { worklist.add(instruction); }),
87+
[&](SILInstruction *instruction) { worklist.add(instruction); },
88+
[this](SILValue oldValue, SILValue newValue) {
89+
worklist.replaceValueUsesWith(oldValue, newValue);
90+
}),
8991
createdInstructions(createdInstructions){};
9092

9193
void addReachableCodeToWorklist(SILFunction &function);
@@ -216,6 +218,7 @@ bool MandatoryCombiner::doOneIteration(SILFunction &function,
216218
//===----------------------------------------------------------------------===//
217219

218220
SILInstruction *MandatoryCombiner::visitApplyInst(ApplyInst *instruction) {
221+
219222
// Apply this pass only to partial applies all of whose arguments are
220223
// trivial.
221224
auto calledValue = instruction->getCallee();

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ class SILCombiner :
236236
// the result bit.
237237
SILInstruction *optimizeBuiltinCompareEq(BuiltinInst *AI, bool NegateResult);
238238

239-
SILInstruction *tryOptimizeApplyOfPartialApply(PartialApplyInst *PAI);
240-
241239
SILInstruction *optimizeApplyOfConvertFunctionInst(FullApplySite AI,
242240
ConvertFunctionInst *CFI);
243241

@@ -253,6 +251,15 @@ class SILCombiner :
253251
StringRef FInverseName, StringRef FName);
254252

255253
private:
254+
InstModCallbacks getInstModCallbacks() {
255+
return InstModCallbacks(
256+
[this](SILInstruction *DeadInst) { eraseInstFromFunction(*DeadInst); },
257+
[this](SILInstruction *NewInst) { Worklist.add(NewInst); },
258+
[this](SILValue oldValue, SILValue newValue) {
259+
replaceValueUsesWith(oldValue, newValue);
260+
});
261+
}
262+
256263
FullApplySite rewriteApplyCallee(FullApplySite apply, SILValue callee);
257264

258265
// Build concrete existential information using findInitExistential.

0 commit comments

Comments
 (0)