]> granicus.if.org Git - clang/commitdiff
Do not codegen dummy block.
authorDevang Patel <dpatel@apple.com>
Fri, 28 Sep 2007 21:49:18 +0000 (21:49 +0000)
committerDevang Patel <dpatel@apple.com>
Fri, 28 Sep 2007 21:49:18 +0000 (21:49 +0000)
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

CodeGen/CGStmt.cpp
CodeGen/CodeGenFunction.cpp
CodeGen/CodeGenFunction.h

index b7f1f29f878365c3dca764327cd67f137bd4919a..f1f915e7c19cd73341e858e4b0883a9d9920de34 100644 (file)
@@ -131,13 +131,25 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
   // Emit the 'then' code.
   EmitBlock(ThenBlock);
   EmitStmt(S.getThen());
-  Builder.CreateBr(ContBlock);
+  llvm::BasicBlock *BB = Builder.GetInsertBlock();
+  if (isDummyBlock(BB)) {
+    BB->eraseFromParent();
+    Builder.SetInsertPoint(ThenBlock);
+  }
+  else
+    Builder.CreateBr(ContBlock);
   
   // Emit the 'else' code if present.
   if (const Stmt *Else = S.getElse()) {
     EmitBlock(ElseBlock);
     EmitStmt(Else);
-    Builder.CreateBr(ContBlock);
+    llvm::BasicBlock *BB = Builder.GetInsertBlock();
+    if (isDummyBlock(BB)) {
+      BB->eraseFromParent();
+      Builder.SetInsertPoint(ElseBlock);
+    }
+    else
+      Builder.CreateBr(ContBlock);
   }
   
   // Emit the continuation block for code after the if.
index d540ea7aa8005fcb56131af26c5d49872fe2d5b9..363d299c2604cb832ea889d39cec91f18ae1b531 100644 (file)
@@ -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 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
   // 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 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
   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;
+}
+
index 318368d5ebed7ca813a1a03f8b9b7e8c11a6a430..2f7e0736e021e970234eaef26f1bbb7db19c0bbc 100644 (file)
@@ -276,7 +276,11 @@ public:
   /// the result should be returned.
   RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0, 
                      bool isAggLocVolatile = false);
-  
+
+  /// isDummyBlock - Return true if BB is an empty basic block
+  /// with no predecessors.
+  static bool isDummyBlock(const llvm::BasicBlock *BB);
+
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission
   //===--------------------------------------------------------------------===//