Skip to content

Commit fd3f343

Browse files
committed
SIL: add a utility function to check if a terminator exits a function. NFC
1 parent 66808b9 commit fd3f343

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,6 +3787,12 @@ class TermInst : public SILInstruction {
37873787

37883788
bool isBranch() const { return !getSuccessors().empty(); }
37893789

3790+
/// Returns true if this terminator exits the function.
3791+
bool isFunctionExiting() const {
3792+
return getKind() == ValueKind::ThrowInst ||
3793+
getKind() == ValueKind::ReturnInst;
3794+
}
3795+
37903796
TermKind getTermKind() const { return ValueKindAsTermKind(getKind()); }
37913797
};
37923798

lib/SIL/SILVerifier.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,11 +2975,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
29752975
"stack dealloc does not match most recent stack alloc");
29762976
stack.pop_back();
29772977
}
2978-
if (isa<ReturnInst>(&i) || isa<ThrowInst>(&i)) {
2979-
require(stack.empty(),
2980-
"return with stack allocs that haven't been deallocated");
2981-
}
29822978
if (auto term = dyn_cast<TermInst>(&i)) {
2979+
if (term->isFunctionExiting()) {
2980+
require(stack.empty(),
2981+
"return with stack allocs that haven't been deallocated");
2982+
}
29832983
for (auto &successor : term->getSuccessors()) {
29842984
SILBasicBlock *SuccBB = successor.getBB();
29852985
auto found = visitedBBs.find(SuccBB);

lib/SILOptimizer/IPO/EagerSpecializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ llvm::cl::opt<bool> EagerSpecializeFlag(
4444
/// for new return or error values.
4545
static bool isTrivialReturnBlock(SILBasicBlock *RetBB) {
4646
auto *RetInst = RetBB->getTerminator();
47-
assert(isa<ReturnInst>(RetInst) || isa<ThrowInst>(RetInst) &&
47+
assert(RetInst->isFunctionExiting() &&
4848
"expected a properly terminated return or throw block");
4949

5050
auto RetOperand = RetInst->getOperand(0);
@@ -87,7 +87,7 @@ static void addReturnValueImpl(SILBasicBlock *RetBB, SILBasicBlock *NewRetBB,
8787
SILLocation Loc = F->getLocation();
8888

8989
auto *RetInst = RetBB->getTerminator();
90-
assert(isa<ReturnInst>(RetInst) || isa<ThrowInst>(RetInst) &&
90+
assert(RetInst->isFunctionExiting() &&
9191
"expected a properly terminated return or throw block");
9292
assert(RetInst->getOperand(0)->getType() == NewRetVal->getType() &&
9393
"Mismatched return type");

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ SILValue LifetimeChecker::handleConditionalInitAssign() {
18741874
// before the return.
18751875
for (auto &BB : TheMemory.getFunction()) {
18761876
auto *Term = BB.getTerminator();
1877-
if (isa<ReturnInst>(Term) || isa<ThrowInst>(Term)) {
1877+
if (Term->isFunctionExiting()) {
18781878
B.setInsertionPoint(Term);
18791879
B.createDeallocStack(Loc, ControlVariableBox);
18801880
}

lib/SILOptimizer/Mandatory/InOutDeshadowing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class StackSlotState {
9494
// We need to see a store back to the inout on every exit path.
9595
for (auto &bb : *F) {
9696
auto term = bb.getTerminator();
97-
if (isa<ReturnInst>(term) || isa<ThrowInst>(term)) {
97+
if (term->isFunctionExiting()) {
9898
DEBUG(llvm::dbgs() << " need load from stack slot on exit " << &bb
9999
<< '\n');
100100
ExitBBs.insert(&bb);
@@ -224,7 +224,7 @@ static void analyzeUseOfInOut(Operand *UI, StackSlotState &state) {
224224
if (isa<UnreachableInst>(term))
225225
return;
226226

227-
if (!isa<ReturnInst>(term) && !isa<ThrowInst>(term))
227+
if (!term->isFunctionExiting())
228228
// Any copy from the inout outside of an exit block fails the analysis.
229229
// We don't need full flow-sensitive analysis for SILGen-ed code.
230230
return state.setFailed("inout is stored outside of an exit block");

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ bool PartialApplyCombiner::allocateTemporaries() {
219219

220220
/// Emit dealloc_stack for all temporaries.
221221
void PartialApplyCombiner::deallocateTemporaries() {
222-
// Insert dealloc_stack instructions.
222+
// Insert dealloc_stack instructions at all function exit points.
223223
for (SILBasicBlock &BB : *PAI->getFunction()) {
224224
TermInst *Term = BB.getTerminator();
225-
if (!isa<ReturnInst>(Term) && !isa<ThrowInst>(Term))
225+
if (!Term->isFunctionExiting())
226226
continue;
227227

228228
for (auto Op : Tmps) {

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ class AllocBoxToStack : public SILFunctionTransform {
837837

838838
for (auto &BB : *getFunction()) {
839839
auto *Term = BB.getTerminator();
840-
if (isa<ReturnInst>(Term) || isa<ThrowInst>(Term))
840+
if (Term->isFunctionExiting())
841841
Returns.push_back(Term);
842842

843843
for (auto &I : BB)

0 commit comments

Comments
 (0)