Skip to content

Commit 16b63b1

Browse files
committed
[cfgoptutils] Add a new overload of addNewEdgeValueToBranch that takes an InstModCallback.
I reimplemented the original addNewEdgeValueToBranch to just call the new overload with a default InstModCallbacks, so nothing changed and now we can plug in callbacks to this utility!
1 parent 3d91d7f commit 16b63b1

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

include/swift/SILOptimizer/Utils/CFGOptUtils.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "swift/SIL/SILBuilder.h"
2727
#include "swift/SIL/SILInstruction.h"
28+
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
2829

2930
namespace llvm {
3031
template <typename T> class TinyPtrVector;
@@ -35,17 +36,32 @@ namespace swift {
3536
class DominanceInfo;
3637
class SILLoop;
3738
class SILLoopInfo;
39+
struct InstModCallbacks;
3840

3941
/// Adds a new argument to an edge between a branch and a destination
40-
/// block.
42+
/// block. Allows for user injected callbacks via \p callbacks.
4143
///
4244
/// \param branch The terminator to add the argument to.
4345
/// \param dest The destination block of the edge.
4446
/// \param val The value to the arguments of the branch.
4547
/// \return The created branch. The old branch is deleted.
4648
/// The argument is appended at the end of the argument tuple.
4749
TermInst *addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
48-
SILValue val);
50+
SILValue val,
51+
const InstModCallbacks &callbacks);
52+
53+
/// Adds a new argument to an edge between a branch and a destination
54+
/// block.
55+
///
56+
/// \param branch The terminator to add the argument to.
57+
/// \param dest The destination block of the edge.
58+
/// \param val The value to the arguments of the branch.
59+
/// \return The created branch. The old branch is deleted.
60+
/// The argument is appended at the end of the argument tuple.
61+
inline TermInst *addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
62+
SILValue val) {
63+
return addNewEdgeValueToBranch(branch, dest, val, InstModCallbacks());
64+
}
4965

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

lib/SILOptimizer/Utils/CFGOptUtils.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,9 @@
2323

2424
using namespace swift;
2525

26-
/// Adds a new argument to an edge between a branch and a destination
27-
/// block.
28-
///
29-
/// \param branch The terminator to add the argument to.
30-
/// \param dest The destination block of the edge.
31-
/// \param val The value to the arguments of the branch.
32-
/// \return The created branch. The old branch is deleted.
33-
/// The argument is appended at the end of the argument tuple.
3426
TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
35-
SILValue val) {
27+
SILValue val,
28+
const InstModCallbacks &callbacks) {
3629
SILBuilderWithScope builder(branch);
3730
TermInst *newBr = nullptr;
3831

@@ -59,6 +52,7 @@ TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
5952
cbi->getLoc(), cbi->getCondition(), cbi->getTrueBB(), trueArgs,
6053
cbi->getFalseBB(), falseArgs, cbi->getTrueBBCount(),
6154
cbi->getFalseBBCount());
55+
callbacks.createdNewInst(newBr);
6256
} else if (auto *bi = dyn_cast<BranchInst>(branch)) {
6357
SmallVector<SILValue, 8> args;
6458

@@ -68,13 +62,13 @@ TermInst *swift::addNewEdgeValueToBranch(TermInst *branch, SILBasicBlock *dest,
6862
args.push_back(val);
6963
assert(args.size() == dest->getNumArguments());
7064
newBr = builder.createBranch(bi->getLoc(), bi->getDestBB(), args);
65+
callbacks.createdNewInst(newBr);
7166
} else {
7267
// At the moment we can only add arguments to br and cond_br.
7368
llvm_unreachable("Can't add argument to terminator");
7469
}
7570

76-
branch->dropAllReferences();
77-
branch->eraseFromParent();
71+
callbacks.deleteInst(branch);
7872

7973
return newBr;
8074
}

0 commit comments

Comments
 (0)