[PATCH] D140237: [IR][NFC] Add BasicBlock::erase(Instruction *I) for erasing a single Instruction.
Vasileios Porpodas via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 16 12:21:53 PST 2022
vporpo updated this revision to Diff 483637.
vporpo added a comment.
Herald added a subscriber: hiraditya.
I->eraseFromParent() now calls BasicBlock::erase(BB). Also BasicBlock::erase(BB) now calls InstList.erase() directly instead of calling BasicBlock::erase(FromIt, ToIt).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D140237/new/
https://reviews.llvm.org/D140237
Files:
llvm/include/llvm/IR/BasicBlock.h
llvm/lib/IR/Instruction.cpp
llvm/unittests/IR/BasicBlockTest.cpp
Index: llvm/unittests/IR/BasicBlockTest.cpp
===================================================================
--- llvm/unittests/IR/BasicBlockTest.cpp
+++ llvm/unittests/IR/BasicBlockTest.cpp
@@ -541,5 +541,43 @@
BB0->erase(BB0->begin(), BB0->end());
EXPECT_TRUE(BB0->empty());
}
+
+TEST(BasicBlockTest, EraseSingleInstr) {
+ LLVMContext Ctx;
+ std::unique_ptr<Module> M = parseIR(Ctx, R"(
+ define void @f(i32 %a) {
+ bb0:
+ %instr1 = add i32 %a, %a
+ %instr2 = sub i32 %a, %a
+ ret void
+ }
+)");
+ Function *F = &*M->begin();
+
+ auto BB0It = F->begin();
+ BasicBlock *BB0 = &*BB0It;
+
+ auto It = BB0->begin();
+ Instruction *Instr1 = &*It++;
+ Instruction *Instr2 = &*It++;
+ Instruction *Ret = &*It++;
+
+ EXPECT_EQ(BB0->size(), 3u);
+
+ // Erase %instr1
+ BB0->erase(Instr1);
+ EXPECT_EQ(BB0->size(), 2u);
+ EXPECT_EQ(&*BB0->begin(), Instr2);
+
+ // Erase ret
+ BB0->erase(Ret);
+ EXPECT_EQ(BB0->size(), 1u);
+ EXPECT_EQ(&*BB0->begin(), Instr2);
+
+ // Erase %instr2
+ BB0->erase(Instr2);
+ EXPECT_TRUE(BB0->empty());
+}
+
} // End anonymous namespace.
} // End llvm namespace.
Index: llvm/lib/IR/Instruction.cpp
===================================================================
--- llvm/lib/IR/Instruction.cpp
+++ llvm/lib/IR/Instruction.cpp
@@ -79,7 +79,7 @@
}
iplist<Instruction>::iterator Instruction::eraseFromParent() {
- return getParent()->getInstList().erase(getIterator());
+ return getParent()->erase(this);
}
/// Insert an unlinked instruction into a basic block immediately before the
Index: llvm/include/llvm/IR/BasicBlock.h
===================================================================
--- llvm/include/llvm/IR/BasicBlock.h
+++ llvm/include/llvm/IR/BasicBlock.h
@@ -490,6 +490,12 @@
/// \Returns \p ToIt.
BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt);
+ /// Erases \p I from the instruction list. \Returns the iterator of the
+ /// instruction following \p I.
+ BasicBlock::iterator erase(Instruction *I) {
+ return InstList.erase(I->getIterator());
+ }
+
/// Returns true if there are any uses of this basic block other than
/// direct branches, switches, etc. to it.
bool hasAddressTaken() const {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140237.483637.patch
Type: text/x-patch
Size: 2261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221216/ac522cdb/attachment.bin>
More information about the llvm-commits
mailing list