forked from OSchip/llvm-project
Do not codegen dummy block.
Dummy block is an empty block with no predecessors. llvm-svn: 42451
This commit is contained in:
parent
14851c3de3
commit
8ec4f837fa
|
@ -131,13 +131,25 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
|
||||||
// Emit the 'then' code.
|
// Emit the 'then' code.
|
||||||
EmitBlock(ThenBlock);
|
EmitBlock(ThenBlock);
|
||||||
EmitStmt(S.getThen());
|
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.
|
// Emit the 'else' code if present.
|
||||||
if (const Stmt *Else = S.getElse()) {
|
if (const Stmt *Else = S.getElse()) {
|
||||||
EmitBlock(ElseBlock);
|
EmitBlock(ElseBlock);
|
||||||
EmitStmt(Else);
|
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.
|
// Emit the continuation block for code after the if.
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
#include "llvm/Function.h"
|
#include "llvm/Function.h"
|
||||||
#include "llvm/Analysis/Verifier.h"
|
#include "llvm/Analysis/Verifier.h"
|
||||||
|
#include "llvm/Support/CFG.h"
|
||||||
using namespace clang;
|
using namespace clang;
|
||||||
using namespace CodeGen;
|
using namespace CodeGen;
|
||||||
|
|
||||||
|
@ -87,13 +88,18 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
|
||||||
// Emit the function body.
|
// Emit the function body.
|
||||||
EmitStmt(FD->getBody());
|
EmitStmt(FD->getBody());
|
||||||
|
|
||||||
// Emit a return for code that falls off the end.
|
// Emit a return for code that falls off the end. If insert point
|
||||||
// FIXME: if this is C++ main, this should return 0.
|
// is a dummy block with no predecessors then remove the block itself.
|
||||||
if (CurFn->getReturnType() == llvm::Type::VoidTy)
|
llvm::BasicBlock *BB = Builder.GetInsertBlock();
|
||||||
Builder.CreateRetVoid();
|
if (isDummyBlock(BB))
|
||||||
else
|
BB->eraseFromParent();
|
||||||
Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
|
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() &&
|
assert(BreakContinueStack.empty() &&
|
||||||
"mismatched push/pop in break/continue stack!");
|
"mismatched push/pop in break/continue stack!");
|
||||||
|
|
||||||
|
@ -101,3 +107,11 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
|
||||||
assert(!verifyFunction(*CurFn));
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -276,7 +276,11 @@ public:
|
||||||
/// the result should be returned.
|
/// the result should be returned.
|
||||||
RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
|
RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
|
||||||
bool isAggLocVolatile = false);
|
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
|
// Declaration Emission
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
|
Loading…
Reference in New Issue