Do not codegen dummy block.
Dummy block is an empty block with no predecessors.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42451 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CodeGen/CodeGenFunction.cpp b/CodeGen/CodeGenFunction.cpp
index d540ea7..363d299 100644
--- a/CodeGen/CodeGenFunction.cpp
+++ b/CodeGen/CodeGenFunction.cpp
@@ -19,6 +19,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
 #include "llvm/Analysis/Verifier.h"
+#include "llvm/Support/CFG.h"
 using namespace clang;
 using namespace CodeGen;
 
@@ -87,13 +88,18 @@
   // Emit the function body.
   EmitStmt(FD->getBody());
   
-  // Emit a return for code that falls off the end.
-  // FIXME: if this is C++ main, this should return 0.
-  if (CurFn->getReturnType() == llvm::Type::VoidTy)
-    Builder.CreateRetVoid();
-  else
-    Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
-  
+  // Emit a return for code that falls off the end. If insert point
+  // is a dummy block with no predecessors then remove the block itself.
+  llvm::BasicBlock *BB = Builder.GetInsertBlock();
+  if (isDummyBlock(BB))
+    BB->eraseFromParent();
+  else {
+    // FIXME: if this is C++ main, this should return 0.
+    if (CurFn->getReturnType() == llvm::Type::VoidTy)
+      Builder.CreateRetVoid();
+    else
+      Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
+  }
   assert(BreakContinueStack.empty() &&
          "mismatched push/pop in break/continue stack!");
   
@@ -101,3 +107,11 @@
   assert(!verifyFunction(*CurFn));
 }
 
+/// isDummyBlock - Return true if BB is an empty basic block
+/// with no predecessors.
+bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
+  if (BB->empty() && pred_begin(BB) == pred_end(BB)) 
+    return true;
+  return false;
+}
+