[PATCH] D138875: [IR][NFC] Adds Instruction::insertAt() for inserting at a specific point in the instr list.
Vasileios Porpodas via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 28 15:59:27 PST 2022
vporpo created this revision.
vporpo added a reviewer: asbirlea.
Herald added a subscriber: hiraditya.
Herald added a project: All.
vporpo requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Currently the only way to do this is to work with the instruction list directly.
This is part of a series of cleanup patches towards making BasicBlock::getInstList() private.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D138875
Files:
llvm/include/llvm/IR/Instruction.h
llvm/lib/IR/Instruction.cpp
llvm/unittests/IR/InstructionsTest.cpp
Index: llvm/unittests/IR/InstructionsTest.cpp
===================================================================
--- llvm/unittests/IR/InstructionsTest.cpp
+++ llvm/unittests/IR/InstructionsTest.cpp
@@ -1681,5 +1681,43 @@
EXPECT_EQ(H.getAllocationSizeInBits(DL), TypeSize::getFixed(160));
}
+TEST(InstructionsTest, InsertAtBegin) {
+ LLVMContext Ctx;
+ std::unique_ptr<Module> M = parseIR(Ctx, R"(
+ define void @f(i32 %a, i32 %b) {
+ entry:
+ ret void
+ }
+)");
+ Function *F = &*M->begin();
+ Argument *ArgA = F->getArg(0);
+ Argument *ArgB = F->getArg(1);
+ BasicBlock *BB = &*F->begin();
+ Instruction *Ret = &*BB->begin();
+ Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
+ auto It = I->insertAt(BB, BB->begin());
+ EXPECT_EQ(&*It, I);
+ EXPECT_EQ(I->getNextNode(), Ret);
+}
+
+TEST(InstructionsTest, InsertAtEnd) {
+ LLVMContext Ctx;
+ std::unique_ptr<Module> M = parseIR(Ctx, R"(
+ define void @f(i32 %a, i32 %b) {
+ entry:
+ ret void
+ }
+)");
+ Function *F = &*M->begin();
+ Argument *ArgA = F->getArg(0);
+ Argument *ArgB = F->getArg(1);
+ BasicBlock *BB = &*F->begin();
+ Instruction *Ret = &*BB->begin();
+ Instruction *I = BinaryOperator::CreateAdd(ArgA, ArgB);
+ auto It = I->insertAt(BB, BB->end());
+ EXPECT_EQ(&*It, I);
+ EXPECT_EQ(Ret->getNextNode(), I);
+}
+
} // end anonymous namespace
} // end namespace llvm
Index: llvm/lib/IR/Instruction.cpp
===================================================================
--- llvm/lib/IR/Instruction.cpp
+++ llvm/lib/IR/Instruction.cpp
@@ -95,6 +95,13 @@
this);
}
+BasicBlock::iterator Instruction::insertAt(BasicBlock *BB,
+ BasicBlock::iterator It) {
+ assert(getParent() == nullptr && "Expected detached instruction");
+ assert((It == BB->end() || It->getParent() == BB) && "It not in BB");
+ return BB->getInstList().insert(It, this);
+}
+
/// Unlink this instruction from its current basic block and insert it into the
/// basic block that MovePos lives in, right before MovePos.
void Instruction::moveBefore(Instruction *MovePos) {
Index: llvm/include/llvm/IR/Instruction.h
===================================================================
--- llvm/include/llvm/IR/Instruction.h
+++ llvm/include/llvm/IR/Instruction.h
@@ -129,6 +129,11 @@
/// specified instruction.
void insertAfter(Instruction *InsertPos);
+ /// Inserts an unlinked instruction into \p BB at position \p It and returns
+ /// the iterator of the inserted instruction.
+ SymbolTableList<Instruction>::iterator
+ insertAt(BasicBlock *BB, SymbolTableList<Instruction>::iterator It);
+
/// Unlink this instruction from its current basic block and insert it into
/// the basic block that MovePos lives in, right before MovePos.
void moveBefore(Instruction *MovePos);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138875.478408.patch
Type: text/x-patch
Size: 2896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221128/40188939/attachment.bin>
More information about the llvm-commits
mailing list