Skip to content

Commit 865919d

Browse files
committed
IR: Add BasicBlock::insertInto()
Although unlinked `BasicBlock`s can be created, there's currently no way to insert them into `Function`s after the fact. In particular, `moveAfter()` and `moveBefore()` require that the basic block is already linked. Extract the logic for initially linking a `BasicBlock` out of the constructor and into a member function that can be used for lazy insertion. - Asserts that the basic block is currently unlinked. - Matches the logic of the constructor. - Changed the constructor to use it since the logic matches. This is needed in a follow-up commit for PR5680. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214563 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f149954 commit 865919d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

include/llvm/IR/BasicBlock.h

+7
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ class BasicBlock : public Value, // Basic blocks are data objects also
173173
/// right after \p MovePos in the function \p MovePos lives in.
174174
void moveAfter(BasicBlock *MovePos);
175175

176+
/// \brief Insert unlinked basic block into a function.
177+
///
178+
/// Inserts an unlinked basic block into \c Parent. If \c InsertBefore is
179+
/// provided, inserts before that basic block, otherwise inserts at the end.
180+
///
181+
/// \pre \a getParent() is \c nullptr.
182+
void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr);
176183

177184
/// \brief Return the predecessor of this block if it has a single predecessor
178185
/// block. Otherwise return a null pointer.

lib/IR/BasicBlock.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,24 @@ BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent,
5050
// Make sure that we get added to a function
5151
LeakDetector::addGarbageObject(this);
5252

53-
if (InsertBefore) {
54-
assert(NewParent &&
53+
if (NewParent)
54+
insertInto(NewParent, InsertBefore);
55+
else
56+
assert(!InsertBefore &&
5557
"Cannot insert block before another block with no function!");
56-
NewParent->getBasicBlockList().insert(InsertBefore, this);
57-
} else if (NewParent) {
58-
NewParent->getBasicBlockList().push_back(this);
59-
}
6058

6159
setName(Name);
6260
}
6361

62+
void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) {
63+
assert(NewParent && "Expected a parent");
64+
assert(!Parent && "Already has a parent");
65+
66+
if (InsertBefore)
67+
NewParent->getBasicBlockList().insert(InsertBefore, this);
68+
else
69+
NewParent->getBasicBlockList().push_back(this);
70+
}
6471

6572
BasicBlock::~BasicBlock() {
6673
// If the address of the block is taken and it is being deleted (e.g. because

0 commit comments

Comments
 (0)