[cfe-commits] r42451 - in /cfe/trunk/CodeGen: CGStmt.cpp CodeGenFunction.cpp CodeGenFunction.h
Devang Patel
dpatel at apple.com
Fri Sep 28 14:49:18 PDT 2007
Author: dpatel
Date: Fri Sep 28 16:49:18 2007
New Revision: 42451
URL: http://llvm.org/viewvc/llvm-project?rev=42451&view=rev
Log:
Do not codegen dummy block.
Dummy block is an empty block with no predecessors.
Modified:
cfe/trunk/CodeGen/CGStmt.cpp
cfe/trunk/CodeGen/CodeGenFunction.cpp
cfe/trunk/CodeGen/CodeGenFunction.h
Modified: cfe/trunk/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGStmt.cpp?rev=42451&r1=42450&r2=42451&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/CodeGen/CGStmt.cpp Fri Sep 28 16:49:18 2007
@@ -131,13 +131,25 @@
// 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.
Modified: cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=42451&r1=42450&r2=42451&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.cpp Fri Sep 28 16:49:18 2007
@@ -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;
+}
+
Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=42451&r1=42450&r2=42451&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Fri Sep 28 16:49:18 2007
@@ -276,7 +276,11 @@
/// 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
//===--------------------------------------------------------------------===//
More information about the cfe-commits
mailing list