diff options
author | Devang Patel <dpatel@apple.com> | 2007-09-28 21:49:18 +0000 |
---|---|---|
committer | Devang Patel <dpatel@apple.com> | 2007-09-28 21:49:18 +0000 |
commit | d9363c3a80168283b3da518b4e17f545a6246857 (patch) | |
tree | 913f8155e3a3d191da08e41590f1f7f9bf8d63f9 | |
parent | bcb07d5d3f4e47b72f7a3a5f62b75b7dabe8c68d (diff) |
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
-rw-r--r-- | CodeGen/CGStmt.cpp | 16 | ||||
-rw-r--r-- | CodeGen/CodeGenFunction.cpp | 28 | ||||
-rw-r--r-- | CodeGen/CodeGenFunction.h | 6 |
3 files changed, 40 insertions, 10 deletions
diff --git a/CodeGen/CGStmt.cpp b/CodeGen/CGStmt.cpp index b7f1f29f87..f1f915e7c1 100644 --- a/CodeGen/CGStmt.cpp +++ b/CodeGen/CGStmt.cpp @@ -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. diff --git a/CodeGen/CodeGenFunction.cpp b/CodeGen/CodeGenFunction.cpp index d540ea7aa8..363d299c26 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 @@ 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; +} + diff --git a/CodeGen/CodeGenFunction.h b/CodeGen/CodeGenFunction.h index 318368d5eb..2f7e0736e0 100644 --- a/CodeGen/CodeGenFunction.h +++ b/CodeGen/CodeGenFunction.h @@ -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 //===--------------------------------------------------------------------===// |