Skip to content

Commit 2385a97

Browse files
committed
SILSSAUpdater: use the InstructionDeleter utility for deleting branch instructions.
the SSA updater replaces branch instructions when adding phi arguments. For deleting the old instruction use the InstructionDeleter utility.
1 parent f74a75f commit 2385a97

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

include/swift/SILOptimizer/Utils/CFGOptUtils.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,7 @@ struct InstModCallbacks;
4747
/// \return The created branch. The old branch is deleted.
4848
/// The argument is appended at the end of the argument tuple.
4949
TermInst *addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
50-
SILValue val, InstModCallbacks &callbacks);
51-
52-
/// Adds a new argument to an edge between a branch and a destination
53-
/// block.
54-
///
55-
/// \param branch The terminator to add the argument to.
56-
/// \param dest The destination block of the edge.
57-
/// \param val The value to the arguments of the branch.
58-
/// \return The created branch. The old branch is deleted.
59-
/// The argument is appended at the end of the argument tuple.
60-
inline TermInst *addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
61-
SILValue val) {
62-
InstModCallbacks callbacks;
63-
return addNewEdgeValueToBranch(branch, dest, val, callbacks);
64-
}
50+
SILValue val, InstructionDeleter &deleter);
6551

6652
/// Changes the edge value between a branch and destination basic block
6753
/// at the specified index. Changes all edges from \p Branch to \p Dest to carry

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_SIL_SILSSAUPDATER_H
1515

1616
#include "llvm/Support/Allocator.h"
17+
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
1718
#include "swift/SIL/SILInstruction.h"
1819
#include "swift/SIL/SILValue.h"
1920

@@ -58,6 +59,10 @@ class SILSSAUpdater {
5859
// If not null updated with inserted 'phi' nodes (SILArgument).
5960
SmallVectorImpl<SILPhiArgument *> *insertedPhis;
6061

62+
// Used to delete branch instructions when they are replaced for adding
63+
// phi arguments.
64+
InstructionDeleter deleter;
65+
6166
// Not copyable.
6267
void operator=(const SILSSAUpdater &) = delete;
6368
SILSSAUpdater(const SILSSAUpdater &) = delete;
@@ -67,6 +72,8 @@ class SILSSAUpdater {
6772
SmallVectorImpl<SILPhiArgument *> *insertedPhis = nullptr);
6873
~SILSSAUpdater();
6974

75+
InstructionDeleter &getDeleter() { return deleter; }
76+
7077
void setInsertedPhis(SmallVectorImpl<SILPhiArgument *> *inputInsertedPhis) {
7178
insertedPhis = inputInsertedPhis;
7279
}

lib/SILOptimizer/Utils/CFGOptUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using namespace swift;
2626

2727
TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
2828
SILValue val,
29-
InstModCallbacks &callbacks) {
29+
InstructionDeleter &deleter) {
3030
SILBuilderWithScope builder(branch);
3131
TermInst *newBr = nullptr;
3232

@@ -53,7 +53,7 @@ TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
5353
cbi->getLoc(), cbi->getCondition(), cbi->getTrueBB(), trueArgs,
5454
cbi->getFalseBB(), falseArgs, cbi->getTrueBBCount(),
5555
cbi->getFalseBBCount());
56-
callbacks.createdNewInst(newBr);
56+
deleter.getCallbacks().createdNewInst(newBr);
5757
} else if (auto *bi = dyn_cast<BranchInst>(branch)) {
5858
SmallVector<SILValue, 8> args;
5959

@@ -63,13 +63,13 @@ TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
6363
args.push_back(val);
6464
assert(args.size() == dest->getNumArguments());
6565
newBr = builder.createBranch(bi->getLoc(), bi->getDestBB(), args);
66-
callbacks.createdNewInst(newBr);
66+
deleter.getCallbacks().createdNewInst(newBr);
6767
} else {
6868
// At the moment we can only add arguments to br and cond_br.
6969
llvm_unreachable("Can't add argument to terminator");
7070
}
7171

72-
callbacks.deleteInst(branch);
72+
deleter.forceDelete(branch);
7373

7474
return newBr;
7575
}

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ insertOwnedBaseValueAlongBranchEdge(BranchInst *bi, SILValue innerCopy,
7878
// argument.
7979
auto *phiArg =
8080
destBB->createPhiArgument(innerCopy->getType(), OwnershipKind::Owned);
81-
addNewEdgeValueToBranch(bi, destBB, innerCopy, callbacks);
81+
InstructionDeleter deleter(callbacks);
82+
addNewEdgeValueToBranch(bi, destBB, innerCopy, deleter);
8283

8384
// Grab our predecessor blocks, ignoring us, add to the branch edge an
8485
// undef corresponding to our value.
@@ -93,7 +94,7 @@ insertOwnedBaseValueAlongBranchEdge(BranchInst *bi, SILValue innerCopy,
9394
continue;
9495
addNewEdgeValueToBranch(
9596
predBlock->getTerminator(), destBB,
96-
SILUndef::get(innerCopy->getType(), *destBB->getParent()), callbacks);
97+
SILUndef::get(innerCopy->getType(), *destBB->getParent()), deleter);
9798
}
9899

99100
return phiArg;

lib/SILOptimizer/Utils/SILSSAUpdater.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ SILValue SILSSAUpdater::getValueInMiddleOfBlock(SILBasicBlock *block) {
230230

231231
// Create a new phi node.
232232
SILPhiArgument *phiArg = block->createPhiArgument(type, OwnershipKind::Owned);
233-
for (auto &pair : predVals)
234-
addNewEdgeValueToBranch(pair.first->getTerminator(), block, pair.second);
235-
233+
for (auto &pair : predVals) {
234+
addNewEdgeValueToBranch(pair.first->getTerminator(), block, pair.second,
235+
deleter);
236+
}
236237
if (insertedPhis)
237238
insertedPhis->push_back(phiArg);
238239

@@ -326,7 +327,8 @@ class SSAUpdaterTraits<SILSSAUpdater> {
326327

327328
for (auto *predBlock : predBlockList) {
328329
TermInst *ti = predBlock->getTerminator();
329-
addNewEdgeValueToBranch(ti, block, ssaUpdater->phiSentinel.get());
330+
addNewEdgeValueToBranch(ti, block, ssaUpdater->phiSentinel.get(),
331+
ssaUpdater->deleter);
330332
}
331333

332334
return phi;

0 commit comments

Comments
 (0)