Rework IRgen invariant w.r.t. current insert point.
 - EmitStmt is no longer required to finish with a current insertion
   point defined (i.e. it does not need to make dummy
   blocks). Instead, it can clear the insertion point in the builder
   which indicates that the current insertion point is unreachable.
 - CodeGenFunction provides HaveInsertPoint and EnsureInsertPoint
   which respectively test if there is an insert point and ensure an
   insertion point exists (by making a dummy block).
 - Clearly mark functions in CodeGenFunction which can be called with
   no insertion point defined. Currently this is a limited set, and
   EmitStmt simply EnsureInsertPoint()s before emitting subsequent IR.

Remove EmitDummyBlock, which is no longer needed. Clients who haven't
already cleared the insertion point (typically via EmitBranch) can do
so by hand.

Remove isDummyBlock, which has effectively been renamed to
HaveInsertPoint.

The main thrust of this change is that we no longer have create dummy
blocks just to destroy them a short time later in EmitBlock in the
common case that there is no unreachable code following something like
a goto. 

Additionally, this means that we are not using the hokey condition in
isDummyBlock that a block without a name is a dummy block. Guess how
well that works when we never emit block names!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59089 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index 97a7a05..daad3f5 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -27,6 +27,13 @@
 
 void CodeGenFunction::EmitStmt(const Stmt *S) {
   assert(S && "Null statement?");
+
+  // If we happen to be at an unreachable point just create a dummy
+  // basic block to hold the code. We could change parts of irgen to
+  // simply not generate this code, but this situation is rare and
+  // probably not worth the effort.
+  // FIXME: Verify previous performance/effort claim.
+  EnsureInsertPoint();
   
   // Generate stoppoints if we are emitting debug info.
   // Beginning of a Compound Statement (e.g. an opening '{') does not produce 
@@ -128,6 +135,7 @@
     EmitStmt(*I);
 
   if (DI) {
+    EnsureInsertPoint();
     DI->setLocation(S.getRBracLoc());
     DI->EmitRegionEnd(CurFn, Builder);
   }
@@ -145,6 +153,8 @@
     LastStmt = LS->getSubStmt();
   }
   
+  EnsureInsertPoint();
+
   return EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
 }
 
@@ -164,10 +174,6 @@
   if (!CurBB || CurBB->getTerminator()) {
     // If there is no insert point or the previous block is already
     // terminated, don't touch it.
-  } else if (isDummyBlock(CurBB)) {
-    // If the last block was an empty placeholder, remove it now.
-    // TODO: cache and reuse these.
-    CurBB->eraseFromParent();
   } else {
     // Otherwise, create a fall-through branch.
     Builder.CreateBr(Target);
@@ -176,10 +182,6 @@
   Builder.ClearInsertionPoint();
 }
 
-void CodeGenFunction::EmitDummyBlock() {
-  EmitBlock(createBasicBlock());
-}
-
 void CodeGenFunction::EmitLabel(const LabelStmt &S) {
   llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
   EmitBlock(NextBB);
@@ -199,10 +201,6 @@
   }
 
   EmitBranch(getBasicBlockForLabel(S.getLabel()));
-  
-  // Emit a block after the branch so that dead code after a goto has some place
-  // to go.
-  EmitDummyBlock();
 }
 
 void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
@@ -221,9 +219,8 @@
   llvm::SwitchInst *I = Builder.CreateSwitch(V, Builder.GetInsertBlock());
   IndirectSwitches.push_back(I);
 
-  // Emit a block after the branch so that dead code after a goto has some place
-  // to go.
-  EmitDummyBlock();
+  // Clear the insertion point to indicate we are in unreachable code.
+  Builder.ClearInsertionPoint();
 }
 
 void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
@@ -445,10 +442,6 @@
     StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
   }
   EmitBranch(ReturnBlock);
-
-  // Emit a block after the branch so that dead code after a return has some
-  // place to go.
-  EmitDummyBlock();
 }
 
 /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
@@ -485,10 +478,6 @@
   } 
 
   EmitBranch(ReturnBlock);
-  
-  // Emit a block after the branch so that dead code after a return has some
-  // place to go.
-  EmitDummyBlock();
 }
 
 void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
@@ -502,7 +491,6 @@
 
   llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
   EmitBranch(Block);
-  EmitDummyBlock();
 }
 
 void CodeGenFunction::EmitContinueStmt() {
@@ -510,7 +498,6 @@
 
   llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
   EmitBranch(Block);
-  EmitDummyBlock();
 }
 
 /// EmitCaseStmtRange - If case statement range is not too big then
@@ -566,7 +553,10 @@
   Builder.CreateCondBr(Cond, CaseDest, FalseDest);
 
   // Restore the appropriate insertion point.
-  Builder.SetInsertPoint(RestoreBB);
+  if (RestoreBB)
+    Builder.SetInsertPoint(RestoreBB);
+  else
+    Builder.ClearInsertionPoint();
 }
 
 void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {