etcwilde created this revision.
etcwilde added reviewers: compnerd, vporpo.
etcwilde added a project: LLVM.
Herald added a project: All.
etcwilde requested review of this revision.
Herald added a project: clang.
Herald added subscribers: llvm-commits, cfe-commits.

Appending a basic block to a function is a fairly common operation. The basic 
block list is private now, so pushing back is currently done by 
`myFunction->insert(myFunction->getEnd(), bb);`, which feels a bit redundant. 
This patch adds a `push_back` convenience function to `llvm::Function` to make 
appending basic blocks a bit easier again.

  +  /// append \p BB to the end of the function
  +  void push_back(BasicBlock *BB) {
  +    return BasicBlocks.push_back(BB);
  +  }

I've also gone through and migrated the usage in kaleidoscope, the getting 
started docs, and in clang where I saw it.
I'm open to leaving that part of the change off, but it makes it a little bit 
clearer what the intended goal is.

I was going back and forth a little regarding the naming. `appendBasicBlock` is 
clearest, `push_back` is most consistent though.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155413

Files:
  clang/lib/CodeGen/CGStmt.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
  llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
  llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
  llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
  llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
  llvm/examples/Kaleidoscope/Chapter5/toy.cpp
  llvm/examples/Kaleidoscope/Chapter6/toy.cpp
  llvm/examples/Kaleidoscope/Chapter7/toy.cpp
  llvm/examples/Kaleidoscope/Chapter8/toy.cpp
  llvm/examples/Kaleidoscope/Chapter9/toy.cpp
  llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
  llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
  llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
  llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
  llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
  llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
  llvm/include/llvm/IR/Function.h

Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -693,6 +693,11 @@
     return BasicBlocks.insert(Position, BB);
   }
 
+  /// append \p BB to the end of the function
+  void push_back(BasicBlock *BB) {
+    return BasicBlocks.push_back(BB);
+  }
+
   /// Transfer all blocks from \p FromF to this function at \p ToIt.
   void splice(Function::iterator ToIt, Function *FromF) {
     splice(ToIt, FromF, FromF->begin(), FromF->end());
Index: llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/lazy/toy.cpp
@@ -1025,7 +1025,7 @@
   ThenBB = Builder.GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->Codegen();
@@ -1036,7 +1036,7 @@
   ElseBB = Builder.GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp
@@ -745,7 +745,7 @@
   ThenBB = Builder.GetInsertBlock();
   
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
   
   Value *ElseV = Else->Codegen();
@@ -756,7 +756,7 @@
   ElseBB = Builder.GetInsertBlock();
   
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/initial/toy.cpp
@@ -985,7 +985,7 @@
   ThenBB = Builder.GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->Codegen();
@@ -996,7 +996,7 @@
   ElseBB = Builder.GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/complete/toy.cpp
@@ -1205,7 +1205,7 @@
   ThenBB = Builder.GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->Codegen();
@@ -1216,7 +1216,7 @@
   ElseBB = Builder.GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/cached/toy.cpp
@@ -1127,7 +1127,7 @@
   ThenBB = Builder.GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->Codegen();
@@ -1138,7 +1138,7 @@
   ElseBB = Builder.GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
===================================================================
--- llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
+++ llvm/examples/Kaleidoscope/MCJIT/cached/toy-jit.cpp
@@ -763,7 +763,7 @@
   ThenBB = Builder.GetInsertBlock();
   
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder.SetInsertPoint(ElseBB);
   
   Value *ElseV = Else->Codegen();
@@ -774,7 +774,7 @@
   ElseBB = Builder.GetInsertBlock();
   
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder.SetInsertPoint(MergeBB);
   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/Chapter9/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter9/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter9/toy.cpp
@@ -1037,7 +1037,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -1049,7 +1049,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/Chapter8/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter8/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter8/toy.cpp
@@ -867,7 +867,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -879,7 +879,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/Chapter7/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -869,7 +869,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -881,7 +881,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/Chapter6/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -762,7 +762,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -774,7 +774,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/Chapter5/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -650,7 +650,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -662,7 +662,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
+++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
@@ -846,7 +846,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -858,7 +858,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
+++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
@@ -863,7 +863,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -875,7 +875,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
+++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
@@ -863,7 +863,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -875,7 +875,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
+++ llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
@@ -863,7 +863,7 @@
   ThenBB = Builder->GetInsertBlock();
 
   // Emit else block.
-  TheFunction->insert(TheFunction->end(), ElseBB);
+  TheFunction->push_back(ElseBB);
   Builder->SetInsertPoint(ElseBB);
 
   Value *ElseV = Else->codegen();
@@ -875,7 +875,7 @@
   ElseBB = Builder->GetInsertBlock();
 
   // Emit merge block.
-  TheFunction->insert(TheFunction->end(), MergeBB);
+  TheFunction->push_back(MergeBB);
   Builder->SetInsertPoint(MergeBB);
   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
 
Index: llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
===================================================================
--- llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
+++ llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl05.rst
@@ -377,7 +377,7 @@
 .. code-block:: c++
 
       // Emit else block.
-      TheFunction->insert(TheFunction->end(), ElseBB);
+      TheFunction->push_back(ElseBB);
       Builder->SetInsertPoint(ElseBB);
 
       Value *ElseV = Else->codegen();
@@ -398,7 +398,7 @@
 .. code-block:: c++
 
       // Emit merge block.
-      TheFunction->insert(TheFunction->end(), MergeBB);
+      TheFunction->push_back(MergeBB);
       Builder->SetInsertPoint(MergeBB);
       PHINode *PN =
         Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -322,7 +322,7 @@
 static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
   if (!BB) return;
   if (!BB->use_empty()) {
-    CGF.CurFn->insert(CGF.CurFn->end(), BB);
+    CGF.CurFn->push_back(BB);
     return;
   }
   delete BB;
Index: clang/lib/CodeGen/CGStmt.cpp
===================================================================
--- clang/lib/CodeGen/CGStmt.cpp
+++ clang/lib/CodeGen/CGStmt.cpp
@@ -582,7 +582,7 @@
   if (CurBB && CurBB->getParent())
     CurFn->insert(std::next(CurBB->getIterator()), BB);
   else
-    CurFn->insert(CurFn->end(), BB);
+    CurFn->push_back(BB);
   Builder.SetInsertPoint(BB);
 }
 
@@ -614,7 +614,7 @@
   }
 
   if (!inserted)
-    CurFn->insert(CurFn->end(), block);
+    CurFn->push_back(block);
 
   Builder.SetInsertPoint(block);
 }
@@ -1474,7 +1474,7 @@
   llvm::BasicBlock *FalseDest = CaseRangeBlock;
   CaseRangeBlock = createBasicBlock("sw.caserange");
 
-  CurFn->insert(CurFn->end(), CaseRangeBlock);
+  CurFn->push_back(CaseRangeBlock);
   Builder.SetInsertPoint(CaseRangeBlock);
 
   // Emit range check.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to