Skip to content

Commit 2940e7d

Browse files
committed
[apply-site] Refactor out a helper function called insertAfterApply -> ApplySite.insertAfter().
Often times when one is working with apply sites, one wants to insert instructions after both terminator apply sites and normal apply sites. This can get ackward and result in unnecessary if-else code that is all really doing the same thing, once for terminator instructions and once for normal instructions. insertAfterApply is a helper method that MandatoryInlining uses for this purpose and it is so useful that I want to use it somewhere else in closure lifetime fixup as well. I am moving it onto apply site since that is the true abstraction that insertAfterApply works with.
1 parent 40016b5 commit 2940e7d

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

include/swift/SIL/ApplySite.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef SWIFT_SIL_APPLYSITE_H
2222
#define SWIFT_SIL_APPLYSITE_H
2323

24+
#include "swift/SIL/SILBasicBlock.h"
2425
#include "swift/SIL/SILInstruction.h"
2526

2627
namespace swift {
@@ -397,6 +398,24 @@ class ApplySite {
397398
}
398399
}
399400

401+
/// If this is a terminator apply site, then pass the first instruction of
402+
/// each successor to fun. Otherwise, pass std::next(Inst).
403+
///
404+
/// The intention is that this abstraction will enable the compiler writer to
405+
/// ignore whether or not an apply site is a terminator when inserting
406+
/// instructions after an apply site. This results in eliminating unnecessary
407+
/// if-else code otherwise required to handle such situations.
408+
void insertAfter(llvm::function_ref<void(SILBasicBlock::iterator)> func) {
409+
auto *ti = dyn_cast<TermInst>(Inst);
410+
if (!ti) {
411+
return func(std::next(Inst->getIterator()));
412+
}
413+
414+
for (auto *succBlock : ti->getSuccessorBlocks()) {
415+
func(succBlock->begin());
416+
}
417+
}
418+
400419
static ApplySite getFromOpaqueValue(void *p) { return ApplySite(p); }
401420

402421
friend bool operator==(ApplySite lhs, ApplySite rhs) {

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,6 @@ static SILValue stripCopiesAndBorrows(SILValue v) {
5353
return v;
5454
}
5555

56-
/// If \p applySite is a terminator then pass the first instruction of each
57-
/// successor to fun. Otherwise, pass std::next(applySite).
58-
static void
59-
insertAfterApply(SILInstruction *applySite,
60-
llvm::function_ref<void(SILBasicBlock::iterator)> &&fun) {
61-
auto *ti = dyn_cast<TermInst>(applySite);
62-
if (!ti) {
63-
return fun(std::next(applySite->getIterator()));
64-
}
65-
66-
for (auto *succBlocks : ti->getSuccessorBlocks()) {
67-
fun(succBlocks->begin());
68-
}
69-
}
70-
7156
/// Fixup reference counts after inlining a function call (which is a no-op
7257
/// unless the function is a thick function).
7358
///
@@ -174,13 +159,12 @@ static void fixupReferenceCounts(
174159
// insert a destroy after the apply since the leak will just cover the
175160
// other path.
176161
if (!error.getFoundOverConsume()) {
177-
insertAfterApply(
178-
applySite.getInstruction(), [&](SILBasicBlock::iterator iter) {
179-
if (hasOwnership) {
180-
SILBuilderWithScope(iter).createEndBorrow(loc, argument);
181-
}
182-
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, copy);
183-
});
162+
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
163+
if (hasOwnership) {
164+
SILBuilderWithScope(iter).createEndBorrow(loc, argument);
165+
}
166+
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, copy);
167+
});
184168
}
185169
v = argument;
186170
break;
@@ -215,10 +199,9 @@ static void fixupReferenceCounts(
215199
}
216200
}
217201

218-
insertAfterApply(
219-
applySite.getInstruction(), [&](SILBasicBlock::iterator iter) {
220-
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, v);
221-
});
202+
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
203+
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, v);
204+
});
222205
break;
223206
}
224207

@@ -263,10 +246,9 @@ static void fixupReferenceCounts(
263246
// Destroy the callee as the apply would have done if our function is not
264247
// callee guaranteed.
265248
if (!isCalleeGuaranteed) {
266-
insertAfterApply(
267-
applySite.getInstruction(), [&](SILBasicBlock::iterator iter) {
268-
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, calleeValue);
269-
});
249+
applySite.insertAfter([&](SILBasicBlock::iterator iter) {
250+
SILBuilderWithScope(iter).emitDestroyValueOperation(loc, calleeValue);
251+
});
270252
}
271253
}
272254

0 commit comments

Comments
 (0)