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;
|
|
|
|
|
2009-09-09 23:08:12 +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-10-13 14:55:33 +08:00
|
|
|
DebugInfo(0), IndirectGotoSwitch(0),
|
|
|
|
SwitchInsn(0), CaseRangeBlock(0), InvokeDest(0),
|
2009-04-05 04:47:02 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-30 08:13:02 +08:00
|
|
|
// 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-09-29 10:09:01 +08:00
|
|
|
return T->isRecordType() || T->isArrayType() || T->isAnyComplexType() ||
|
|
|
|
T->isMemberFunctionPointerType();
|
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()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::BranchInst *BI =
|
2009-01-27 07:27:52 +08:00
|
|
|
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-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-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Emit function epilog (to return).
|
2009-01-27 07:27:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-09-11 08:07:24 +08:00
|
|
|
void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
|
2008-09-10 07:14:03 +08:00
|
|
|
llvm::Function *Fn,
|
2008-10-19 02:22:23 +08:00
|
|
|
const FunctionArgList &Args,
|
|
|
|
SourceLocation StartLoc) {
|
2009-09-11 08:07:24 +08:00
|
|
|
const Decl *D = GD.getDecl();
|
|
|
|
|
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-08-14 05:57:51 +08:00
|
|
|
llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext));
|
2009-09-26 02:11:00 +08:00
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef,
|
|
|
|
llvm::Type::getInt32Ty(VMContext), "",
|
2007-12-18 04:50:59 +08:00
|
|
|
EntryBB);
|
2009-03-22 08:24:14 +08:00
|
|
|
if (Builder.isNamePreserving())
|
|
|
|
AllocaInsertPt->setName("allocapt");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 04:50:59 +08:00
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2009-09-09 23:08:12 +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);
|
2009-09-11 08:11:35 +08:00
|
|
|
if (isa<FunctionDecl>(D)) {
|
|
|
|
DI->EmitFunctionStart(CGM.getMangledName(GD), RetTy, CurFn, Builder);
|
2008-10-19 02:22:23 +08:00
|
|
|
} else {
|
|
|
|
// Just use LLVM function name.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 13:30:36 +08:00
|
|
|
// FIXME: Remove unnecessary conversion to std::string when API settles.
|
2009-09-09 23:08:12 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
2009-09-11 08:07:24 +08:00
|
|
|
void CodeGenFunction::GenerateCode(GlobalDecl GD,
|
2008-09-10 07:14:03 +08:00
|
|
|
llvm::Function *Fn) {
|
2009-09-11 08:07:24 +08:00
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
|
|
|
|
|
2009-02-13 16:11:52 +08:00
|
|
|
// Check if we should generate debug info for this function.
|
2009-08-27 06:31:08 +08:00
|
|
|
if (CGM.getDebugInfo() && !FD->hasAttr<NoDebugAttr>())
|
2009-02-13 16:11:52 +08:00
|
|
|
DebugInfo = CGM.getDebugInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
FunctionArgList Args;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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(),
|
2009-09-09 23:08:12 +08:00
|
|
|
&getContext().Idents.get("this"),
|
2009-04-05 04:47:02 +08:00
|
|
|
MD->getThisType(getContext()));
|
|
|
|
Args.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-26 05:31:01 +08:00
|
|
|
if (FD->getNumParams()) {
|
2009-09-22 07:43:11 +08:00
|
|
|
const FunctionProtoType* FProto = FD->getType()->getAs<FunctionProtoType>();
|
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)
|
2009-09-09 23:08:12 +08:00
|
|
|
Args.push_back(std::make_pair(FD->getParamDecl(i),
|
2008-09-10 07:14:03 +08:00
|
|
|
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-09-11 08:07:24 +08:00
|
|
|
StartFunction(GD, FD->getResultType(), Fn, Args, S->getLBracLoc());
|
2009-10-07 02:09:57 +08:00
|
|
|
const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD);
|
|
|
|
llvm::BasicBlock *DtorEpilogue = 0;
|
|
|
|
if (DD) {
|
|
|
|
DtorEpilogue = createBasicBlock("dtor.epilogue");
|
|
|
|
|
|
|
|
PushCleanupBlock(DtorEpilogue);
|
|
|
|
}
|
|
|
|
|
2009-07-21 06:35:22 +08:00
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
|
2009-09-14 13:32:02 +08:00
|
|
|
EmitCtorPrologue(CD, GD.getCtorType());
|
2009-04-27 04:35:05 +08:00
|
|
|
EmitStmt(S);
|
2009-10-07 02:09:57 +08:00
|
|
|
|
|
|
|
if (DD) {
|
|
|
|
CleanupBlockInfo Info = PopCleanupBlock();
|
|
|
|
|
|
|
|
assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
|
|
|
|
EmitBlock(DtorEpilogue);
|
2009-09-14 13:32:02 +08:00
|
|
|
EmitDtorEpilogue(DD, GD.getDtorType());
|
2009-10-07 02:09:57 +08:00
|
|
|
|
|
|
|
if (Info.SwitchBlock)
|
|
|
|
EmitBlock(Info.SwitchBlock);
|
|
|
|
if (Info.EndBlock)
|
|
|
|
EmitBlock(Info.EndBlock);
|
|
|
|
}
|
2009-04-27 04:35:05 +08:00
|
|
|
FinishFunction(S->getRBracLoc());
|
2009-10-02 04:44:19 +08:00
|
|
|
} else if (FD->isImplicit()) {
|
|
|
|
const CXXRecordDecl *ClassDecl =
|
|
|
|
cast<CXXRecordDecl>(FD->getDeclContext());
|
|
|
|
(void) ClassDecl;
|
2009-07-31 07:22:00 +08:00
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
|
2009-10-02 04:44:19 +08:00
|
|
|
// FIXME: For C++0x, we want to look for implicit *definitions* of
|
|
|
|
// these special member functions, rather than implicit *declarations*.
|
2009-08-07 07:38:16 +08:00
|
|
|
if (CD->isCopyConstructor(getContext())) {
|
|
|
|
assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
|
2009-10-02 04:44:19 +08:00
|
|
|
"Cannot synthesize a non-implicit copy constructor");
|
2009-09-14 13:32:02 +08:00
|
|
|
SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args);
|
2009-10-02 04:44:19 +08:00
|
|
|
} else if (CD->isDefaultConstructor()) {
|
2009-08-07 07:38:16 +08:00
|
|
|
assert(!ClassDecl->hasUserDeclaredConstructor() &&
|
2009-10-02 04:44:19 +08:00
|
|
|
"Cannot synthesize a non-implicit default constructor.");
|
2009-09-14 13:32:02 +08:00
|
|
|
SynthesizeDefaultConstructor(CD, GD.getCtorType(), Fn, Args);
|
2009-10-02 04:44:19 +08:00
|
|
|
} else {
|
|
|
|
assert(false && "Implicit constructor cannot be synthesized");
|
2009-08-07 07:38:16 +08:00
|
|
|
}
|
2009-10-02 04:44:19 +08:00
|
|
|
} else if (const CXXDestructorDecl *CD = dyn_cast<CXXDestructorDecl>(FD)) {
|
|
|
|
assert(!ClassDecl->hasUserDeclaredDestructor() &&
|
|
|
|
"Cannot synthesize a non-implicit destructor");
|
|
|
|
SynthesizeDefaultDestructor(CD, GD.getDtorType(), Fn, Args);
|
|
|
|
} else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
assert(MD->isCopyAssignment() &&
|
|
|
|
!ClassDecl->hasUserDeclaredCopyAssignment() &&
|
|
|
|
"Cannot synthesize a method that is not an implicit-defined "
|
|
|
|
"copy constructor");
|
2009-09-14 13:32:02 +08:00
|
|
|
SynthesizeCXXCopyAssignment(MD, Fn, Args);
|
2009-10-02 04:44:19 +08:00
|
|
|
} else {
|
|
|
|
assert(false && "Cannot synthesize unknown implicit function");
|
|
|
|
}
|
2009-09-11 08:07:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// 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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// 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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// If this is a switch statement, we want to ignore cases below it.
|
|
|
|
if (isa<SwitchStmt>(S))
|
|
|
|
IgnoreCaseStmts = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// 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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() ||
|
2008-12-01 10:46:24 +08:00
|
|
|
Result.HasSideEffects)
|
2008-11-23 06:32:07 +08:00
|
|
|
return 0; // Not foldable, not integer or not fully evaluatable.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
if (CodeGenFunction::ContainsLabel(Cond))
|
|
|
|
return 0; // Contains a label.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// 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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// 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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-12 16:13:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-04-22 01:59:23 +08:00
|
|
|
void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) {
|
2009-10-13 14:02:42 +08:00
|
|
|
const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-31 03:51:14 +08:00
|
|
|
// FIXME: Handle variable sized types.
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(VMContext,
|
2009-08-14 05:57:51 +08:00
|
|
|
LLVMPointerWidth);
|
2008-08-31 03:51:14 +08:00
|
|
|
|
|
|
|
Builder.CreateCall4(CGM.getMemSetFn(), DestPtr,
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)),
|
2008-08-31 03:51:14 +08:00
|
|
|
// TypeInfo.first describes size in bits.
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2008-08-31 03:51:14 +08:00
|
|
|
TypeInfo.second/8));
|
|
|
|
}
|
|
|
|
|
2009-10-13 14:55:33 +08:00
|
|
|
unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
|
|
|
|
// Use LabelIDs.size()+1 as the new ID if one hasn't been assigned.
|
|
|
|
unsigned &Entry = LabelIDs[L];
|
|
|
|
if (Entry) return Entry;
|
|
|
|
|
|
|
|
Entry = LabelIDs.size();
|
|
|
|
|
|
|
|
// If this is the first "address taken" of a label and the indirect goto has
|
|
|
|
// already been seen, add this to it.
|
|
|
|
if (IndirectGotoSwitch) {
|
|
|
|
// If this is the first address-taken label, set it as the default dest.
|
|
|
|
if (Entry == 1)
|
|
|
|
IndirectGotoSwitch->setSuccessor(0, getBasicBlockForLabel(L));
|
|
|
|
else {
|
|
|
|
// Otherwise add it to the switch as a new dest.
|
|
|
|
const llvm::IntegerType *Int32Ty = llvm::Type::getInt32Ty(VMContext);
|
|
|
|
IndirectGotoSwitch->addCase(llvm::ConstantInt::get(Int32Ty, Entry),
|
|
|
|
getBasicBlockForLabel(L));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Entry;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-13 14:55:33 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
|
|
|
|
// If we already made the switch stmt for indirect goto, return its block.
|
|
|
|
if (IndirectGotoSwitch) return IndirectGotoSwitch->getParent();
|
|
|
|
|
|
|
|
EmitBlock(createBasicBlock("indirectgoto"));
|
|
|
|
|
|
|
|
// Create the PHI node that indirect gotos will add entries to.
|
|
|
|
llvm::Value *DestVal =
|
|
|
|
Builder.CreatePHI(llvm::Type::getInt32Ty(VMContext), "indirect.goto.dest");
|
|
|
|
|
|
|
|
// Create the switch instruction. For now, set the insert block to this block
|
|
|
|
// which will be fixed as labels are added.
|
|
|
|
IndirectGotoSwitch = Builder.CreateSwitch(DestVal, Builder.GetInsertBlock());
|
|
|
|
|
|
|
|
// Clear the insertion point to indicate we are in unreachable code.
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
|
|
|
|
// If we already have labels created, add them.
|
2008-08-05 00:51:22 +08:00
|
|
|
if (!LabelIDs.empty()) {
|
2009-10-13 14:55:33 +08:00
|
|
|
// Invert LabelID's so that the order is determinstic.
|
|
|
|
std::vector<const LabelStmt*> AddrTakenLabelsByID;
|
|
|
|
AddrTakenLabelsByID.resize(LabelIDs.size());
|
|
|
|
|
|
|
|
for (std::map<const LabelStmt*,unsigned>::iterator
|
|
|
|
LI = LabelIDs.begin(), LE = LabelIDs.end(); LI != LE; ++LI) {
|
|
|
|
assert(LI->second-1 < AddrTakenLabelsByID.size() &&
|
|
|
|
"Numbering inconsistent");
|
|
|
|
AddrTakenLabelsByID[LI->second-1] = LI->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default entry as the first block.
|
|
|
|
IndirectGotoSwitch->setSuccessor(0,
|
|
|
|
getBasicBlockForLabel(AddrTakenLabelsByID[0]));
|
|
|
|
|
|
|
|
const llvm::IntegerType *Int32Ty = llvm::Type::getInt32Ty(VMContext);
|
|
|
|
|
|
|
|
// FIXME: The iteration order of this is nondeterminstic!
|
|
|
|
for (unsigned i = 1, e = AddrTakenLabelsByID.size(); i != e; ++i)
|
|
|
|
IndirectGotoSwitch->addCase(llvm::ConstantInt::get(Int32Ty, i+1),
|
|
|
|
getBasicBlockForLabel(AddrTakenLabelsByID[i]));
|
2008-08-05 00:51:22 +08:00
|
|
|
} else {
|
2009-10-13 14:55:33 +08:00
|
|
|
// Otherwise, create a dead block and set it as the default dest. This will
|
|
|
|
// be removed by the optimizers after the indirect goto is set up.
|
|
|
|
llvm::BasicBlock *Dummy = createBasicBlock("indgoto.dummy");
|
|
|
|
EmitBlock(Dummy);
|
|
|
|
IndirectGotoSwitch->setSuccessor(0, Dummy);
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
Builder.ClearInsertionPoint();
|
2008-08-05 00:51:22 +08:00
|
|
|
}
|
|
|
|
|
2009-10-13 14:55:33 +08:00
|
|
|
return IndirectGotoSwitch->getParent();
|
2008-08-05 00:51:22 +08:00
|
|
|
}
|
2008-11-04 13:30:00 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) {
|
2009-08-15 10:50:32 +08:00
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) {
|
2009-08-15 10:50:32 +08:00
|
|
|
llvm::Value *&SizeEntry = VLASizeMap[VAT->getSizeExpr()];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (!SizeEntry) {
|
2009-02-06 03:43:10 +08:00
|
|
|
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-15 08:03:43 +08:00
|
|
|
// Get the element size;
|
|
|
|
QualType ElemTy = VAT->getElementType();
|
|
|
|
llvm::Value *ElemSize;
|
2008-12-21 05:51:53 +08:00
|
|
|
if (ElemTy->isVariableArrayType())
|
|
|
|
ElemSize = EmitVLASize(ElemTy);
|
2009-08-15 08:03:43 +08:00
|
|
|
else
|
2009-07-25 07:12:58 +08:00
|
|
|
ElemSize = llvm::ConstantInt::get(SizeTy,
|
2008-12-21 05:51:53 +08:00
|
|
|
getContext().getTypeSize(ElemTy) / 8);
|
2009-09-09 23:08:12 +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");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
SizeEntry = Builder.CreateMul(ElemSize, NumElements);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
return SizeEntry;
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-15 08:03:43 +08:00
|
|
|
if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
EmitVLASize(AT->getElementType());
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-08-15 08:03:43 +08:00
|
|
|
const PointerType *PT = Ty->getAs<PointerType>();
|
|
|
|
assert(PT && "unknown VM type!");
|
|
|
|
EmitVLASize(PT->getPointeeType());
|
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-09-09 23:08:12 +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
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenFunction::EmitCleanupBlocks(size_t OldCleanupStackSize) {
|
|
|
|
assert(CleanupEntries.size() >= OldCleanupStackSize &&
|
2009-02-08 07:50:39 +08:00
|
|
|
"Cleanup stack mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 07:50:39 +08:00
|
|
|
while (CleanupEntries.size() > OldCleanupStackSize)
|
|
|
|
EmitCleanupBlock();
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
CodeGenFunction::CleanupBlockInfo CodeGenFunction::PopCleanupBlock() {
|
2009-02-08 15:46:24 +08:00
|
|
|
CleanupEntry &CE = CleanupEntries.back();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
llvm::BasicBlock *CleanupBlock = CE.CleanupBlock;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
std::vector<llvm::BasicBlock *> Blocks;
|
|
|
|
std::swap(Blocks, CE.Blocks);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
std::vector<llvm::BranchInst *> BranchFixups;
|
|
|
|
std::swap(BranchFixups, CE.BranchFixups);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
if (I == BlockScopes.end())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-09 06:45:15 +08:00
|
|
|
assert(I->second <= CleanupEntries.size() && "Invalid branch fixup!");
|
2009-09-09 23:08:12 +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-09-09 23:08:12 +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-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
Builder.SetInsertPoint(SwitchBlock);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Value *DestCodePtr = CreateTempAlloca(llvm::Type::getInt32Ty(VMContext),
|
2009-02-08 09:23:05 +08:00
|
|
|
"cleanup.dst");
|
|
|
|
llvm::Value *DestCode = Builder.CreateLoad(DestCodePtr, "tmp");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
// Create a switch instruction to determine where to jump next.
|
2009-09-09 23:08:12 +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-09-09 23:08:12 +08:00
|
|
|
|
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-08-14 05:57:51 +08:00
|
|
|
Builder.CreateStore(llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)),
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
// Fixup the branch instruction to point to the cleanup block.
|
|
|
|
BI->setSuccessor(0, CleanupBlock);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
if (CleanupEntries.empty()) {
|
2009-02-09 06:46:50 +08:00
|
|
|
llvm::ConstantInt *ID;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-09 06:46:50 +08:00
|
|
|
// Check if we already have a destination for this block.
|
|
|
|
if (Dest == SI->getDefaultDest())
|
2009-08-14 05:57:51 +08:00
|
|
|
ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 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-09-09 23:08:12 +08:00
|
|
|
ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2009-02-09 06:46:50 +08:00
|
|
|
SI->getNumSuccessors());
|
|
|
|
SI->addCase(ID, Dest);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-09 06:46:50 +08:00
|
|
|
// 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.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
// 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-09-09 23:08:12 +08:00
|
|
|
llvm::ConstantInt *ID = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
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-09-09 23:08:12 +08:00
|
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 09:23:05 +08:00
|
|
|
// And add it as a branch fixup.
|
|
|
|
CleanupEntries.back().BranchFixups.push_back(BI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:16:35 +08:00
|
|
|
BlockScopes.erase(Blocks[i]);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 15:46:24 +08:00
|
|
|
return CleanupBlockInfo(CleanupBlock, SwitchBlock, EndBlock);
|
2009-02-08 11:55:35 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenFunction::EmitCleanupBlock() {
|
2009-02-08 15:46:24 +08:00
|
|
|
CleanupBlockInfo Info = PopCleanupBlock();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-31 08:33:20 +08:00
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
2009-09-09 23:08:12 +08:00
|
|
|
if (CurBB && !CurBB->getTerminator() &&
|
2009-05-31 08:33:20 +08:00
|
|
|
Info.CleanupBlock->getNumUses() == 0) {
|
|
|
|
CurBB->getInstList().splice(CurBB->end(), Info.CleanupBlock->getInstList());
|
|
|
|
delete Info.CleanupBlock;
|
2009-09-09 23:08:12 +08:00
|
|
|
} else
|
2009-05-31 08:33:20 +08:00
|
|
|
EmitBlock(Info.CleanupBlock);
|
2009-09-09 23:08:12 +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-09-09 23:08:12 +08:00
|
|
|
void CodeGenFunction::AddBranchFixup(llvm::BranchInst *BI) {
|
|
|
|
assert(!CleanupEntries.empty() &&
|
2009-02-08 08:50:42 +08:00
|
|
|
"Trying to add branch fixup without cleanup block!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenFunction::EmitBranchThroughCleanup(llvm::BasicBlock *Dest) {
|
2009-02-09 06:13:37 +08:00
|
|
|
if (!HaveInsertPoint())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
llvm::BranchInst* BI = Builder.CreateBr(Dest);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-09 06:13:37 +08:00
|
|
|
Builder.ClearInsertionPoint();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
// The stack is empty, no need to do any cleanup.
|
|
|
|
if (CleanupEntries.empty())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
if (!Dest->getParent()) {
|
|
|
|
// We are trying to branch to a block that hasn't been inserted yet.
|
|
|
|
AddBranchFixup(BI);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
assert(I->second < CleanupEntries.size() &&
|
|
|
|
"Trying to branch into cleanup region");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
if (I->second == CleanupEntries.size() - 1) {
|
|
|
|
// We have a branch to a block in the same scope.
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 08:50:42 +08:00
|
|
|
AddBranchFixup(BI);
|
|
|
|
}
|