[llvm] 606f790 - [IR][NFC] Adds Instruction::insertAt() for inserting at a specific point in the instr list.
Vasileios Porpodas via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 29 20:15:50 PST 2022
Author: Vasileios Porpodas
Date: 2022-11-29T20:15:10-08:00
New Revision: 606f790330bb23dffc90fd60bbb37b047f4fdad5
URL: https://github.com/llvm/llvm-project/commit/606f790330bb23dffc90fd60bbb37b047f4fdad5
DIFF: https://github.com/llvm/llvm-project/commit/606f790330bb23dffc90fd60bbb37b047f4fdad5.diff
LOG: [IR][NFC] Adds Instruction::insertAt() for inserting at a specific point in the instr list.
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.
Differential Revision: https://reviews.llvm.org/D138875
Added:
Modified:
llvm/include/llvm/IR/Instruction.h
llvm/lib/IR/Instruction.cpp
llvm/unittests/IR/InstructionsTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index 9fa8a0abbd8cd..e9f07fcecbb57 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -129,6 +129,11 @@ class Instruction : public User,
/// 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);
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 74fc3416b564f..1dea11c6ea7d2 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -95,6 +95,13 @@ void Instruction::insertAfter(Instruction *InsertPos) {
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) {
diff --git a/llvm/unittests/IR/InstructionsTest.cpp b/llvm/unittests/IR/InstructionsTest.cpp
index 65cb48a7ab038..26a513902bdef 100644
--- a/llvm/unittests/IR/InstructionsTest.cpp
+++ b/llvm/unittests/IR/InstructionsTest.cpp
@@ -1681,5 +1681,43 @@ TEST(InstructionsTest, AllocaInst) {
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
More information about the llvm-commits
mailing list