2007-05-28 09:07:47 +08:00
|
|
|
//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-28 09:07:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-function state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-05-22 09:40:10 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-05-30 07:17:50 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-11-12 16:04:58 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-05 04:47:02 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-02-22 04:00:35 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-05-30 07:17:50 +08:00
|
|
|
CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
|
2009-03-06 09:33:24 +08:00
|
|
|
: BlockFunction(cgm, *this, Builder), CGM(cgm),
|
|
|
|
Target(CGM.getContext().Target),
|
2009-07-09 04:52:20 +08:00
|
|
|
Builder(cgm.getModule().getContext()),
|
2009-04-05 04:47:02 +08:00
|
|
|
DebugInfo(0), SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
|
|
|
|
CXXThisDecl(0) {
|
2009-02-22 04:00:35 +08:00
|
|
|
LLVMIntTy = ConvertType(getContext().IntTy);
|
|
|
|
LLVMPointerWidth = Target.getPointerWidth(0);
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2007-06-03 06:49:07 +08:00
|
|
|
ASTContext &CodeGenFunction::getContext() const {
|
|
|
|
return CGM.getContext();
|
|
|
|
}
|
|
|
|
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2007-05-30 08:13:02 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::BasicBlock *&BB = LabelMap[S];
|
2007-05-30 08:13:02 +08:00
|
|
|
if (BB) return BB;
|
|
|
|
|
|
|
|
// Create, but don't insert, the new block.
|
2008-11-11 10:29:29 +08:00
|
|
|
return BB = createBasicBlock(S->getName());
|
2007-05-30 08:13:02 +08:00
|
|
|
}
|
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) {
|
|
|
|
llvm::Value *Res = LocalDeclMap[VD];
|
|
|
|
assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
|
|
|
|
return Res;
|
2008-02-27 05:41:45 +08:00
|
|
|
}
|
2007-05-30 08:13:02 +08:00
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
|
|
|
|
return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
|
2008-09-11 17:15:33 +08:00
|
|
|
}
|
|
|
|
|
2009-02-04 07:03:55 +08:00
|
|
|
const llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertTypeForMem(T);
|
|
|
|
}
|
|
|
|
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
2007-06-09 10:28:57 +08:00
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: Use positive checks instead of negative ones to be more robust in
|
|
|
|
// the face of extension.
|
2009-02-27 04:52:22 +08:00
|
|
|
return !T->hasPointerRepresentation() &&!T->isRealType() &&
|
|
|
|
!T->isVoidType() && !T->isVectorType() && !T->isFunctionType() &&
|
2009-01-09 10:44:18 +08:00
|
|
|
!T->isBlockPointerType();
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2008-03-31 07:03:07 +08:00
|
|
|
|
2009-01-27 07:27:52 +08:00
|
|
|
void CodeGenFunction::EmitReturnBlock() {
|
|
|
|
// For cleanliness, we try to avoid emitting the return block for
|
|
|
|
// simple cases.
|
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
if (CurBB) {
|
|
|
|
assert(!CurBB->getTerminator() && "Unexpected terminated block.");
|
|
|
|
|
2009-07-19 16:24:34 +08:00
|
|
|
// We have a valid insert point, reuse it if it is empty or there are no
|
|
|
|
// explicit jumps to the return block.
|
|
|
|
if (CurBB->empty() || ReturnBlock->use_empty()) {
|
|
|
|
ReturnBlock->replaceAllUsesWith(CurBB);
|
2009-01-27 07:27:52 +08:00
|
|
|
delete ReturnBlock;
|
2009-07-19 16:24:34 +08:00
|
|
|
} else
|
2009-01-27 07:27:52 +08:00
|
|
|
EmitBlock(ReturnBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if the return block is the target of a single direct
|
|
|
|
// branch then we can just put the code in that block instead. This
|
|
|
|
// cleans up functions which started with a unified return block.
|
|
|
|
if (ReturnBlock->hasOneUse()) {
|
|
|
|
llvm::BranchInst *BI =
|
|
|
|
dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin());
|
|
|
|
if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) {
|
|
|
|
// Reset insertion point and delete the branch.
|
|
|
|
Builder.SetInsertPoint(BI->getParent());
|
|
|
|
BI->eraseFromParent();
|
|
|
|
delete ReturnBlock;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: We are at an unreachable point, there is no reason to emit the block
|
|
|
|
// unless it has uses. However, we still need a place to put the debug
|
|
|
|
// region.end for now.
|
2009-01-27 07:27:52 +08:00
|
|
|
|
|
|
|
EmitBlock(ReturnBlock);
|
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
|
2008-08-05 00:51:22 +08:00
|
|
|
// Finish emission of indirect switches.
|
|
|
|
EmitIndirectSwitches();
|
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
assert(BreakContinueStack.empty() &&
|
|
|
|
"mismatched push/pop in break/continue stack!");
|
2009-02-08 08:16:35 +08:00
|
|
|
assert(BlockScopes.empty() &&
|
|
|
|
"did not remove all blocks from block scope map!");
|
|
|
|
assert(CleanupEntries.empty() &&
|
|
|
|
"mismatched push/pop in cleanup stack!");
|
|
|
|
|
2009-01-27 07:27:52 +08:00
|
|
|
// Emit function epilog (to return).
|
|
|
|
EmitReturnBlock();
|
2008-11-12 04:59:54 +08:00
|
|
|
|
|
|
|
// Emit debug descriptor for function end.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-11-12 04:59:54 +08:00
|
|
|
DI->setLocation(EndLoc);
|
|
|
|
DI->EmitRegionEnd(CurFn, Builder);
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
EmitFunctionEpilog(*CurFnInfo, ReturnValue);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
// Remove the AllocaInsertPt instruction, which is just a convenience for us.
|
2009-04-01 06:17:44 +08:00
|
|
|
llvm::Instruction *Ptr = AllocaInsertPt;
|
2008-03-31 07:03:07 +08:00
|
|
|
AllocaInsertPt = 0;
|
2009-04-01 06:17:44 +08:00
|
|
|
Ptr->eraseFromParent();
|
2008-04-04 12:07:35 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy,
|
|
|
|
llvm::Function *Fn,
|
2008-10-19 02:22:23 +08:00
|
|
|
const FunctionArgList &Args,
|
|
|
|
SourceLocation StartLoc) {
|
2009-02-10 04:20:56 +08:00
|
|
|
DidCallStackSave = false;
|
2009-04-23 13:30:27 +08:00
|
|
|
CurCodeDecl = CurFuncDecl = D;
|
2008-09-10 07:14:03 +08:00
|
|
|
FnRetTy = RetTy;
|
2008-07-30 07:18:29 +08:00
|
|
|
CurFn = Fn;
|
2007-06-20 12:44:43 +08:00
|
|
|
assert(CurFn->isDeclaration() && "Function already has body?");
|
2008-03-03 11:28:21 +08:00
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
// Create a marker to make it easy to insert allocas into the entryblock
|
2007-12-18 04:50:59 +08:00
|
|
|
// later. Don't create this with the builder, because we don't want it
|
|
|
|
// folded.
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Value *Undef = VMContext.getUndef(llvm::Type::Int32Ty);
|
2009-03-22 08:24:14 +08:00
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "",
|
2007-12-18 04:50:59 +08:00
|
|
|
EntryBB);
|
2009-03-22 08:24:14 +08:00
|
|
|
if (Builder.isNamePreserving())
|
|
|
|
AllocaInsertPt->setName("allocapt");
|
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
ReturnBlock = createBasicBlock("return");
|
2008-09-10 05:00:17 +08:00
|
|
|
ReturnValue = 0;
|
2008-09-10 07:14:03 +08:00
|
|
|
if (!RetTy->isVoidType())
|
|
|
|
ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval");
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2007-12-18 04:50:59 +08:00
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2008-07-04 19:04:26 +08:00
|
|
|
// Emit subprogram debug descriptor.
|
2008-09-10 07:14:03 +08:00
|
|
|
// FIXME: The cast here is a huge hack.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-10-19 02:22:23 +08:00
|
|
|
DI->setLocation(StartLoc);
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2009-02-19 07:53:56 +08:00
|
|
|
DI->EmitFunctionStart(CGM.getMangledName(FD), RetTy, CurFn, Builder);
|
2008-10-19 02:22:23 +08:00
|
|
|
} else {
|
|
|
|
// Just use LLVM function name.
|
2009-07-23 13:30:36 +08:00
|
|
|
|
|
|
|
// FIXME: Remove unnecessary conversion to std::string when API settles.
|
|
|
|
DI->EmitFunctionStart(std::string(Fn->getName()).c_str(),
|
2008-10-19 02:22:23 +08:00
|
|
|
RetTy, CurFn, Builder);
|
2008-07-04 19:04:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:03:45 +08:00
|
|
|
// FIXME: Leaked.
|
2009-02-03 07:23:47 +08:00
|
|
|
CurFnInfo = &CGM.getTypes().getFunctionInfo(FnRetTy, Args);
|
2009-02-03 06:03:45 +08:00
|
|
|
EmitFunctionProlog(*CurFnInfo, CurFn, Args);
|
2008-12-21 05:28:43 +08:00
|
|
|
|
|
|
|
// If any of the arguments have a variably modified type, make sure to
|
|
|
|
// emit the type size.
|
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
QualType Ty = i->second;
|
|
|
|
|
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
|
|
|
}
|
2008-09-10 07:14:03 +08:00
|
|
|
}
|
2008-08-26 05:31:01 +08:00
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
|
|
|
|
llvm::Function *Fn) {
|
2009-02-13 16:11:52 +08:00
|
|
|
// Check if we should generate debug info for this function.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (CGM.getDebugInfo() && !FD->hasAttr<NodebugAttr>())
|
2009-02-13 16:11:52 +08:00
|
|
|
DebugInfo = CGM.getDebugInfo();
|
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
FunctionArgList Args;
|
2009-04-05 04:47:02 +08:00
|
|
|
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
if (MD->isInstance()) {
|
|
|
|
// Create the implicit 'this' decl.
|
|
|
|
// FIXME: I'm not entirely sure I like using a fake decl just for code
|
|
|
|
// generation. Maybe we can come up with a better way?
|
|
|
|
CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0, SourceLocation(),
|
|
|
|
&getContext().Idents.get("this"),
|
|
|
|
MD->getThisType(getContext()));
|
|
|
|
Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-26 05:31:01 +08:00
|
|
|
if (FD->getNumParams()) {
|
2009-02-27 07:50:07 +08:00
|
|
|
const FunctionProtoType* FProto = FD->getType()->getAsFunctionProtoType();
|
2008-08-26 05:31:01 +08:00
|
|
|
assert(FProto && "Function def must have prototype!");
|
2008-09-10 07:14:03 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
|
|
|
|
Args.push_back(std::make_pair(FD->getParamDecl(i),
|
|
|
|
FProto->getArgType(i)));
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2008-08-26 16:29:31 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
// FIXME: Support CXXTryStmt here, too.
|
2009-06-30 10:35:26 +08:00
|
|
|
if (const CompoundStmt *S = FD->getCompoundBody()) {
|
2009-04-27 04:35:05 +08:00
|
|
|
StartFunction(FD, FD->getResultType(), Fn, Args, S->getLBracLoc());
|
2009-07-21 06:35:22 +08:00
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
|
|
|
|
EmitCtorPrologue(CD);
|
2009-04-27 04:35:05 +08:00
|
|
|
EmitStmt(S);
|
|
|
|
FinishFunction(S->getRBracLoc());
|
|
|
|
}
|
2008-09-10 07:14:03 +08:00
|
|
|
|
2009-04-05 04:47:02 +08:00
|
|
|
// Destroy the 'this' declaration.
|
|
|
|
if (CXXThisDecl)
|
|
|
|
CXXThisDecl->Destroy(getContext());
|
2007-05-30 07:50:05 +08:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
/// ContainsLabel - Return true if the statement contains a label in it. If
|
|
|
|
/// this statement is not executed normally, it not containing a label means
|
|
|
|
/// that we can just remove the code.
|
|
|
|
bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
|
|
|
|
// Null statement, not a label!
|
|
|
|
if (S == 0) return false;
|
|
|
|
|
|
|
|
// If this is a label, we have to emit the code, consider something like:
|
|
|
|
// if (0) { ... foo: bar(); } goto foo;
|
|
|
|
if (isa<LabelStmt>(S))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If this is a case/default statement, and we haven't seen a switch, we have
|
|
|
|
// to emit the code.
|
|
|
|
if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If this is a switch statement, we want to ignore cases below it.
|
|
|
|
if (isa<SwitchStmt>(S))
|
|
|
|
IgnoreCaseStmts = true;
|
|
|
|
|
|
|
|
// Scan subexpressions for verboten labels.
|
|
|
|
for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (ContainsLabel(*I, IgnoreCaseStmts))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
|
|
|
|
/// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to
|
|
|
|
/// a constant, or if it does but contains a label, return 0. If it constant
|
|
|
|
/// folds to 'true' and does not contain a label, return 1, if it constant folds
|
|
|
|
/// to 'false' and does not contain a label, return -1.
|
|
|
|
int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) {
|
2008-11-13 06:37:10 +08:00
|
|
|
// FIXME: Rename and handle conversion of other evaluatable things
|
|
|
|
// to bool.
|
2008-12-01 10:46:24 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
|
|
|
|
Result.HasSideEffects)
|
2008-11-23 06:32:07 +08:00
|
|
|
return 0; // Not foldable, not integer or not fully evaluatable.
|
2008-11-12 16:04:58 +08:00
|
|
|
|
|
|
|
if (CodeGenFunction::ContainsLabel(Cond))
|
|
|
|
return 0; // Contains a label.
|
|
|
|
|
2008-12-01 10:46:24 +08:00
|
|
|
return Result.Val.getInt().getBoolValue() ? 1 : -1;
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
|
|
|
|
/// statement) to the specified blocks. Based on the condition, this might try
|
|
|
|
/// to simplify the codegen of the conditional based on the branch.
|
|
|
|
///
|
|
|
|
void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
|
|
|
|
llvm::BasicBlock *TrueBlock,
|
|
|
|
llvm::BasicBlock *FalseBlock) {
|
|
|
|
if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond))
|
|
|
|
return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock);
|
|
|
|
|
|
|
|
if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
|
|
|
|
// Handle X && Y in a condition.
|
|
|
|
if (CondBOp->getOpcode() == BinaryOperator::LAnd) {
|
|
|
|
// If we have "1 && X", simplify the code. "0 && X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) {
|
|
|
|
// br(1 && X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have "X && 1", simplify the code to use an uncond branch.
|
|
|
|
// "X && 0" would have been constant folded to 0.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) {
|
|
|
|
// br(X && 1) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is false, we
|
|
|
|
// want to jump to the FalseBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
|
|
|
|
EmitBlock(LHSTrue);
|
|
|
|
|
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
} else if (CondBOp->getOpcode() == BinaryOperator::LOr) {
|
|
|
|
// If we have "0 || X", simplify the code. "1 || X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) {
|
|
|
|
// br(0 || X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have "X || 0", simplify the code to use an uncond branch.
|
|
|
|
// "X || 1" would have been constant folded to 1.
|
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) {
|
|
|
|
// br(X || 0) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is true, we
|
|
|
|
// want to jump to the TrueBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
|
|
|
|
EmitBlock(LHSFalse);
|
|
|
|
|
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-12 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
|
|
|
|
// br(!x, t, f) -> br(x, f, t)
|
|
|
|
if (CondUOp->getOpcode() == UnaryOperator::LNot)
|
|
|
|
return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 18:30:32 +08:00
|
|
|
if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
|
|
|
|
// Handle ?: operator.
|
|
|
|
|
|
|
|
// Just ignore GNU ?: extension.
|
|
|
|
if (CondOp->getLHS()) {
|
|
|
|
// br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
|
|
|
|
llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
|
|
|
|
EmitBlock(LHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
EmitBlock(RHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the code with the fully general case.
|
|
|
|
llvm::Value *CondV = EvaluateExprAsBool(Cond);
|
|
|
|
Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
2007-11-02 03:11:01 +08:00
|
|
|
/// getCGRecordLayout - Return record layout info.
|
|
|
|
const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
|
2008-02-05 14:55:31 +08:00
|
|
|
QualType Ty) {
|
2009-07-18 01:50:17 +08:00
|
|
|
const RecordType *RTy = Ty->getAsRecordType();
|
2008-02-05 14:55:31 +08:00
|
|
|
assert (RTy && "Unexpected type. RecordType expected here.");
|
2007-10-23 10:10:49 +08:00
|
|
|
|
2008-02-05 14:55:31 +08:00
|
|
|
return CGT.getCGRecordLayout(RTy->getDecl());
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
2007-12-02 09:43:38 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 09:43:38 +08:00
|
|
|
/// specified stmt yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
CGM.ErrorUnsupported(S, Type, OmitOnError);
|
2007-12-02 09:43:38 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
|
|
|
|
// Use LabelIDs.size() as the new ID if one hasn't been assigned.
|
|
|
|
return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
|
|
|
|
}
|
|
|
|
|
2009-04-22 01:59:23 +08:00
|
|
|
void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
|
2009-07-15 07:10:40 +08:00
|
|
|
const llvm::Type *BP = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
|
2008-08-31 03:51:14 +08:00
|
|
|
if (DestPtr->getType() != BP)
|
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
|
|
|
|
|
|
|
|
// Get size and alignment info for this aggregate.
|
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
|
|
|
|
|
2009-04-22 01:59:23 +08:00
|
|
|
// Don't bother emitting a zero-byte memset.
|
|
|
|
if (TypeInfo.first == 0)
|
|
|
|
return;
|
|
|
|
|
2008-08-31 03:51:14 +08:00
|
|
|
// FIXME: Handle variable sized types.
|
2009-07-15 07:10:40 +08:00
|
|
|
const llvm::Type *IntPtr = VMContext.getIntegerType(LLVMPointerWidth);
|
2008-08-31 03:51:14 +08:00
|
|
|
|
|
|
|
Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
|
2009-07-13 12:10:07 +08:00
|
|
|
getLLVMContext().getNullValue(llvm::Type::Int8Ty),
|
2008-08-31 03:51:14 +08:00
|
|
|
// TypeInfo.first describes size in bits.
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getConstantInt(IntPtr, TypeInfo.first/8),
|
|
|
|
VMContext.getConstantInt(llvm::Type::Int32Ty,
|
2008-08-31 03:51:14 +08:00
|
|
|
TypeInfo.second/8));
|
|
|
|
}
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
void CodeGenFunction::EmitIndirectSwitches() {
|
|
|
|
llvm::BasicBlock *Default;
|
|
|
|
|
2008-08-05 01:24:44 +08:00
|
|
|
if (IndirectSwitches.empty())
|
|
|
|
return;
|
|
|
|
|
2008-08-05 00:51:22 +08:00
|
|
|
if (!LabelIDs.empty()) {
|
|
|
|
Default = getBasicBlockForLabel(LabelIDs.begin()->first);
|
|
|
|
} else {
|
|
|
|
// No possible targets for indirect goto, just emit an infinite
|
|
|
|
// loop.
|
2008-11-11 10:29:29 +08:00
|
|
|
Default = createBasicBlock("indirectgoto.loop", CurFn);
|
2008-08-05 00:51:22 +08:00
|
|
|
llvm::BranchInst::Create(Default, Default);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
|
|
|
|
e = IndirectSwitches.end(); i != e; ++i) {
|
|
|
|
llvm::SwitchInst *I = *i;
|
|
|
|
|
|
|
|
I->setSuccessor(0, Default);
|
|
|
|
for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
|
|
|
|
LE = LabelIDs.end(); LI != LE; ++LI) {
|
2009-07-15 07:10:40 +08:00
|
|
|
I->addCase(VMContext.getConstantInt(llvm::Type::Int32Ty,
|
2008-08-05 00:51:22 +08:00
|
|
|
LI->second),
|
|
|
|
getBasicBlockForLabel(LI->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 13:30:00 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
|
2008-12-12 15:19:02 +08:00
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT];
|
|
|
|
|
2008-12-21 04:27:15 +08:00
|
|
|
assert(SizeEntry && "Did not emit size for type");
|
|
|
|
return SizeEntry;
|
|
|
|
}
|
2008-12-12 15:19:02 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) {
|
2008-12-21 04:46:34 +08:00
|
|
|
assert(Ty->isVariablyModifiedType() &&
|
|
|
|
"Must pass variably modified type to EmitVLASizes!");
|
2008-12-21 04:27:15 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
|
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT];
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (!SizeEntry) {
|
|
|
|
// Get the element size;
|
|
|
|
llvm::Value *ElemSize;
|
2008-12-21 04:46:34 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
QualType ElemTy = VAT->getElementType();
|
2009-02-06 03:43:10 +08:00
|
|
|
|
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (ElemTy->isVariableArrayType())
|
|
|
|
ElemSize = EmitVLASize(ElemTy);
|
|
|
|
else {
|
2009-07-15 07:10:40 +08:00
|
|
|
ElemSize = VMContext.getConstantInt(SizeTy,
|
2008-12-21 05:51:53 +08:00
|
|
|
getContext().getTypeSize(ElemTy) / 8);
|
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr());
|
2009-02-06 03:43:10 +08:00
|
|
|
NumElements = Builder.CreateIntCast(NumElements, SizeTy, false, "tmp");
|
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
SizeEntry = Builder.CreateMul(ElemSize, NumElements);
|
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
|
|
|
return SizeEntry;
|
2009-05-30 03:23:46 +08:00
|
|
|
} else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
EmitVLASize(AT->getElementType());
|
2009-07-18 01:50:17 +08:00
|
|
|
} else if (const PointerType *PT = Ty->getAsPointerType())
|
2008-12-21 04:46:34 +08:00
|
|
|
EmitVLASize(PT->getPointeeType());
|
2008-12-21 04:27:15 +08:00
|
|
|
else {
|
2008-12-21 04:46:34 +08:00
|
|
|
assert(0 && "unknown VM type!");
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2008-12-21 04:46:34 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2009-01-21 01:46:04 +08:00
|
|
|
|
|
|
|
llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
|
|
|
|
if (CGM.getContext().getBuiltinVaListType()->isArrayType()) {
|
|
|
|
return EmitScalarExpr(E);
|
|
|
|
}
|
|
|
|
return EmitLValue(E).getAddress();
|
|
|
|
}
|
2009-02-08 06:53:43 +08:00
|
|
|
|
2009-02-08 11:22:36 +08:00
|
|
|
void CodeGenFunction::PushCleanupBlock(llvm::BasicBlock *CleanupBlock)
|
2009-02-08 06:53:43 +08:00
|
|
|
{
|
|
|
|
CleanupEntries.push_back(CleanupEntry(CleanupBlock));
|
|
|
|
}
|
2009-02-08 07:50:39 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize)
|
|
|
|
{
|
|
|
|
assert(CleanupEntries.size() >= OldCleanupStackSize &&
|
|
|
|
"Cleanup stack mismatch!");
|
|
|
|
|
|
|
|
while (CleanupEntries.size() > OldCleanupStackSize)
|
|
|
|
EmitCleanupBlock();
|
|
|
|
}
|
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock()
|
2009-02-08 07:50:39 +08:00
|
|
|
{
|
2009-02-08 15:46:24 +08:00
|
|
|
CleanupEntry &CE = CleanupEntries.back();
|
|
|
|
|
|
|
|
llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
|
|
|
|
|
|
|
|
std::vector<llvm::BasicBlock *> Blocks;
|
|
|
|
std::swap(Blocks, CE.Blocks);
|
|
|
|
|
|
|
|
std::vector<llvm::BranchInst *> BranchFixups;
|
|
|
|
std::swap(BranchFixups, CE.BranchFixups);
|
|
|
|
|
|
|
|
CleanupEntries.pop_back();
|
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
// Check if any branch fixups pointed to the scope we just popped. If so,
|
|
|
|
// we can remove them.
|
|
|
|
for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
|
|
|
|
llvm::BasicBlock *Dest = BranchFixups[i]->getSuccessor(0);
|
|
|
|
BlockScopeMap::iterator I = BlockScopes.find(Dest);
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
if (I == BlockScopes.end())
|
|
|
|
continue;
|
2009-02-08 09:23:05 +08:00
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
if (I->second == CleanupEntries.size()) {
|
|
|
|
// We don't need to do this branch fixup.
|
|
|
|
BranchFixups[i] = BranchFixups.back();
|
|
|
|
BranchFixups.pop_back();
|
|
|
|
i--;
|
|
|
|
e--;
|
|
|
|
continue;
|
2009-02-08 09:23:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
llvm::BasicBlock *SwitchBlock = 0;
|
|
|
|
llvm::BasicBlock *EndBlock = 0;
|
2009-02-08 09:23:05 +08:00
|
|
|
if (!BranchFixups.empty()) {
|
2009-02-08 15:46:24 +08:00
|
|
|
SwitchBlock = createBasicBlock("cleanup.switch");
|
|
|
|
EndBlock = createBasicBlock("cleanup.end");
|
2009-02-08 09:23:05 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
2009-03-17 13:53:35 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
Builder.SetInsertPoint(SwitchBlock);
|
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::Int32Ty,
|
|
|
|
"cleanup.dst");
|
|
|
|
llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
|
|
|
|
|
|
|
|
// Create a switch instruction to determine where to jump next.
|
2009-02-08 15:46:24 +08:00
|
|
|
llvm::SwitchInst *SI = Builder.CreateSwitch(DestCode, EndBlock,
|
2009-02-08 09:23:05 +08:00
|
|
|
BranchFixups.size());
|
2009-02-08 15:46:24 +08:00
|
|
|
|
2009-02-09 06:13:37 +08:00
|
|
|
// Restore the current basic block (if any)
|
2009-03-17 13:53:35 +08:00
|
|
|
if (CurBB) {
|
2009-02-09 06:13:37 +08:00
|
|
|
Builder.SetInsertPoint(CurBB);
|
2009-03-17 13:53:35 +08:00
|
|
|
|
|
|
|
// If we had a current basic block, we also need to emit an instruction
|
|
|
|
// to initialize the cleanup destination.
|
2009-07-13 12:10:07 +08:00
|
|
|
Builder.CreateStore(getLLVMContext().getNullValue(llvm::Type::Int32Ty),
|
2009-03-17 13:53:35 +08:00
|
|
|
DestCodePtr);
|
|
|
|
} else
|
2009-02-09 06:13:37 +08:00
|
|
|
Builder.ClearInsertionPoint();
|
2009-02-08 15:46:24 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
for (size_t i = 0, e = BranchFixups.size(); i != e; ++i) {
|
|
|
|
llvm::BranchInst *BI = BranchFixups[i];
|
|
|
|
llvm::BasicBlock *Dest = BI->getSuccessor(0);
|
|
|
|
|
|
|
|
// Fixup the branch instruction to point to the cleanup block.
|
|
|
|
BI->setSuccessor(0, CleanupBlock);
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
if (CleanupEntries.empty()) {
|
2009-02-09 06:46:50 +08:00
|
|
|
llvm::ConstantInt *ID;
|
|
|
|
|
|
|
|
// Check if we already have a destination for this block.
|
|
|
|
if (Dest == SI->getDefaultDest())
|
2009-07-15 07:10:40 +08:00
|
|
|
ID = VMContext.getConstantInt(llvm::Type::Int32Ty, 0);
|
2009-02-09 06:46:50 +08:00
|
|
|
else {
|
|
|
|
ID = SI->findCaseDest(Dest);
|
|
|
|
if (!ID) {
|
|
|
|
// No code found, get a new unique one by using the number of
|
|
|
|
// switch successors.
|
2009-07-15 07:10:40 +08:00
|
|
|
ID = VMContext.getConstantInt(llvm::Type::Int32Ty,
|
2009-02-09 06:46:50 +08:00
|
|
|
SI->getNumSuccessors());
|
|
|
|
SI->addCase(ID, Dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the jump destination before the branch instruction.
|
|
|
|
new llvm::StoreInst(ID, DestCodePtr, BI);
|
2009-02-08 09:23:05 +08:00
|
|
|
} else {
|
|
|
|
// We need to jump through another cleanup block. Create a pad block
|
|
|
|
// with a branch instruction that jumps to the final destination and
|
|
|
|
// add it as a branch fixup to the current cleanup scope.
|
|
|
|
|
|
|
|
// Create the pad block.
|
|
|
|
llvm::BasicBlock *CleanupPad = createBasicBlock("cleanup.pad", CurFn);
|
2009-02-09 06:46:50 +08:00
|
|
|
|
|
|
|
// Create a unique case ID.
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::ConstantInt *ID = VMContext.getConstantInt(llvm::Type::Int32Ty,
|
2009-02-09 06:46:50 +08:00
|
|
|
SI->getNumSuccessors());
|
|
|
|
|
|
|
|
// Store the jump destination before the branch instruction.
|
|
|
|
new llvm::StoreInst(ID, DestCodePtr, BI);
|
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
// Add it as the destination.
|
2009-02-09 06:46:50 +08:00
|
|
|
SI->addCase(ID, CleanupPad);
|
2009-02-08 09:23:05 +08:00
|
|
|
|
|
|
|
// Create the branch to the final destination.
|
|
|
|
llvm::BranchInst *BI = llvm::BranchInst::Create(Dest);
|
|
|
|
CleanupPad->getInstList().push_back(BI);
|
|
|
|
|
|
|
|
// And add it as a branch fixup.
|
|
|
|
CleanupEntries.back().BranchFixups.push_back(BI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-08 08:16:35 +08:00
|
|
|
// Remove all blocks from the block scope map.
|
|
|
|
for (size_t i = 0, e = Blocks.size(); i != e; ++i) {
|
|
|
|
assert(BlockScopes.count(Blocks[i]) &&
|
|
|
|
"Did not find block in scope map!");
|
|
|
|
|
|
|
|
BlockScopes.erase(Blocks[i]);
|
|
|
|
}
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
|
2009-02-08 11:55:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitCleanupBlock()
|
|
|
|
{
|
2009-02-08 15:46:24 +08:00
|
|
|
CleanupBlockInfo Info = PopCleanupBlock();
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-05-31 08:33:20 +08:00
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
if (CurBB && !CurBB->getTerminator() &&
|
|
|
|
Info.CleanupBlock->getNumUses() == 0) {
|
|
|
|
CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
|
|
|
|
delete Info.CleanupBlock;
|
|
|
|
} else
|
|
|
|
EmitBlock(Info.CleanupBlock);
|
2009-02-08 11:55:35 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
if (Info.SwitchBlock)
|
|
|
|
EmitBlock(Info.SwitchBlock);
|
|
|
|
if (Info.EndBlock)
|
|
|
|
EmitBlock(Info.EndBlock);
|
2009-02-08 11:55:35 +08:00
|
|
|
}
|
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI)
|
|
|
|
{
|
|
|
|
assert(!CleanupEntries.empty() &&
|
|
|
|
"Trying to add branch fixup without cleanup block!");
|
|
|
|
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: We could be more clever here and check if there's already a branch
|
|
|
|
// fixup for this destination and recycle it.
|
2009-02-08 08:50:42 +08:00
|
|
|
CleanupEntries.back().BranchFixups.push_back(BI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest)
|
|
|
|
{
|
2009-02-09 06:13:37 +08:00
|
|
|
if (!HaveInsertPoint())
|
|
|
|
return;
|
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
llvm::BranchInst* BI = Builder.CreateBr(Dest);
|
|
|
|
|
2009-02-09 06:13:37 +08:00
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
// The stack is empty, no need to do any cleanup.
|
|
|
|
if (CleanupEntries.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Dest->getParent()) {
|
|
|
|
// We are trying to branch to a block that hasn't been inserted yet.
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockScopeMap::iterator I = BlockScopes.find(Dest);
|
|
|
|
if (I == BlockScopes.end()) {
|
|
|
|
// We are trying to jump to a block that is outside of any cleanup scope.
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(I->second < CleanupEntries.size() &&
|
|
|
|
"Trying to branch into cleanup region");
|
|
|
|
|
|
|
|
if (I->second == CleanupEntries.size() - 1) {
|
|
|
|
// We have a branch to a block in the same scope.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
}
|