[llvm] 3259cae - [IR][NFC] Adds BasicBlock::erase().

Vasileios Porpodas via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 17:48:45 PST 2022


Author: Vasileios Porpodas
Date: 2022-12-01T17:47:55-08:00
New Revision: 3259caefb3b16213b41f8d7b9e85a0b04f0c1f13

URL: https://github.com/llvm/llvm-project/commit/3259caefb3b16213b41f8d7b9e85a0b04f0c1f13
DIFF: https://github.com/llvm/llvm-project/commit/3259caefb3b16213b41f8d7b9e85a0b04f0c1f13.diff

LOG: [IR][NFC] Adds BasicBlock::erase().

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/D139142

Added: 
    

Modified: 
    llvm/include/llvm/IR/BasicBlock.h
    llvm/lib/IR/BasicBlock.cpp
    llvm/unittests/IR/BasicBlockTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/BasicBlock.h b/llvm/include/llvm/IR/BasicBlock.h
index 379dd4f8922c..57a2ace099ad 100644
--- a/llvm/include/llvm/IR/BasicBlock.h
+++ b/llvm/include/llvm/IR/BasicBlock.h
@@ -475,6 +475,10 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
               BasicBlock::iterator FromBeginIt,
               BasicBlock::iterator FromEndIt);
 
+  /// Erases a range of instructions from \p FromIt to (not including) \p ToIt.
+  /// \Returns \p ToIt.
+  BasicBlock::iterator erase(BasicBlock::iterator FromIt, BasicBlock::iterator ToIt);
+
   /// Returns true if there are any uses of this basic block other than
   /// direct branches, switches, etc. to it.
   bool hasAddressTaken() const {

diff  --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index fcd6ff08c319..16addda83e42 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -480,6 +480,11 @@ void BasicBlock::splice(BasicBlock::iterator ToIt, BasicBlock *FromBB,
   getInstList().splice(ToIt, FromBB->getInstList(), FromBeginIt, FromEndIt);
 }
 
+BasicBlock::iterator BasicBlock::erase(BasicBlock::iterator FromIt,
+                                       BasicBlock::iterator ToIt) {
+  return getInstList().erase(FromIt, ToIt);
+}
+
 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {
   // N.B. This might not be a complete BasicBlock, so don't assume
   // that it ends with a non-phi instruction.

diff  --git a/llvm/unittests/IR/BasicBlockTest.cpp b/llvm/unittests/IR/BasicBlockTest.cpp
index c0cf5db4e699..5e93b788852c 100644
--- a/llvm/unittests/IR/BasicBlockTest.cpp
+++ b/llvm/unittests/IR/BasicBlockTest.cpp
@@ -507,5 +507,39 @@ TEST(BasicBlockTest, SpliceEndBeforeBegin) {
 }
 #endif //EXPENSIVE_CHECKS
 
+TEST(BasicBlockTest, EraseRange) {
+  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++;
+
+  EXPECT_EQ(BB0->size(), 3u);
+
+  // Erase no instruction
+  BB0->erase(Instr1->getIterator(), Instr1->getIterator());
+  EXPECT_EQ(BB0->size(), 3u);
+
+  // Erase %instr1
+  BB0->erase(Instr1->getIterator(), Instr2->getIterator());
+  EXPECT_EQ(BB0->size(), 2u);
+  EXPECT_EQ(&*BB0->begin(), Instr2);
+
+  // Erase all instructions
+  BB0->erase(BB0->begin(), BB0->end());
+  EXPECT_TRUE(BB0->empty());
+}
 } // End anonymous namespace.
 } // End llvm namespace.


        


More information about the llvm-commits mailing list