[PATCH] D60612: Cowardly refuse to insert instructions after a terminator.

Arnt Gulbrandsen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 12 05:56:51 PDT 2019


arnt created this revision.
arnt added reviewers: craig.topper, vsk.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Allowing instructions to be added after a terminator just invites trouble later. Better to assert early, so the bug is easy to spot.


Repository:
  rL LLVM

https://reviews.llvm.org/D60612

Files:
  llvm/lib/IR/Instruction.cpp
  llvm/unittests/Analysis/PhiValuesTest.cpp


Index: llvm/unittests/Analysis/PhiValuesTest.cpp
===================================================================
--- llvm/unittests/Analysis/PhiValuesTest.cpp
+++ llvm/unittests/Analysis/PhiValuesTest.cpp
@@ -33,15 +33,15 @@
   BasicBlock *If = BasicBlock::Create(C, "if", F);
   BasicBlock *Else = BasicBlock::Create(C, "else", F);
   BasicBlock *Then = BasicBlock::Create(C, "then", F);
-  BranchInst::Create(If, Else, UndefValue::get(I1Ty), Entry);
-  BranchInst::Create(Then, If);
-  BranchInst::Create(Then, Else);
-
   Value *Val1 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val1", Entry);
   Value *Val2 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val2", Entry);
   Value *Val3 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val3", Entry);
   Value *Val4 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val4", Entry);
 
+  BranchInst::Create(If, Else, UndefValue::get(I1Ty), Entry);
+  BranchInst::Create(Then, If);
+  BranchInst::Create(Then, Else);
+
   PHINode *Phi1 = PHINode::Create(I32Ty, 2, "phi1", Then);
   Phi1->addIncoming(Val1, If);
   Phi1->addIncoming(Val2, Else);
@@ -103,24 +103,28 @@
   BasicBlock *If2 = BasicBlock::Create(C, "if2", F);
   BasicBlock *Else2 = BasicBlock::Create(C, "else2", F);
   BasicBlock *End = BasicBlock::Create(C, "then", F);
-  BranchInst::Create(If1, Else1, UndefValue::get(I1Ty), Entry);
-  BranchInst::Create(Then, If1);
-  BranchInst::Create(Then, Else1);
-  BranchInst::Create(If2, Else2, UndefValue::get(I1Ty), Then);
-  BranchInst::Create(End, If2);
-  BranchInst::Create(End, Else2);
 
   Value *Val1 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val1", Entry);
   Value *Val2 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val2", Entry);
   Value *Val3 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val3", Entry);
   Value *Val4 = new LoadInst(I32Ty, UndefValue::get(I32PtrTy), "val4", Entry);
 
+  BranchInst::Create(If1, Else1, UndefValue::get(I1Ty), Entry);
+  BranchInst::Create(Then, If1);
+  BranchInst::Create(Then, Else1);
+
   PHINode *Phi1 = PHINode::Create(I32Ty, 2, "phi1", Then);
   Phi1->addIncoming(Val1, If1);
   Phi1->addIncoming(Val2, Else1);
+
   PHINode *Phi2 = PHINode::Create(I32Ty, 2, "phi2", Then);
   Phi2->addIncoming(Val2, If1);
   Phi2->addIncoming(Val3, Else1);
+
+  BranchInst::Create(If2, Else2, UndefValue::get(I1Ty), Then);
+  BranchInst::Create(End, If2);
+  BranchInst::Create(End, Else2);
+
   PHINode *Phi3 = PHINode::Create(I32Ty, 2, "phi3", End);
   Phi3->addIncoming(Phi1, If2);
   Phi3->addIncoming(Val3, Else2);
Index: llvm/lib/IR/Instruction.cpp
===================================================================
--- llvm/lib/IR/Instruction.cpp
+++ llvm/lib/IR/Instruction.cpp
@@ -38,6 +38,7 @@
 
   // append this instruction into the basic block
   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
+  assert(!InsertAtEnd->getTerminator());
   InsertAtEnd->getInstList().push_back(this);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60612.194856.patch
Type: text/x-patch
Size: 2948 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190412/6c1e092a/attachment-0001.bin>


More information about the llvm-commits mailing list