"lld/git@repo.hca.bsc.es:lalbano/llvm-bpevl.git" did not exist on "e5d642cf5b51db3a82f35c818fd9afbde16f05ee"
Newer
Older
Anders Carlsson
committed
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This contains code dealing with C++ code generation.
//
//===----------------------------------------------------------------------===//
// We might split this into multiple files if it gets too unwieldy
Anders Carlsson
committed
#include "CodeGenFunction.h"
#include "CodeGenModule.h"
#include "Mangle.h"
Anders Carlsson
committed
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecordLayout.h"
Anders Carlsson
committed
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/StmtCXX.h"
Anders Carlsson
committed
#include "llvm/ADT/StringExtras.h"
using namespace clang;
using namespace CodeGen;
CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlsson
committed
llvm::Constant *DeclPtr) {
const llvm::Type *Int8PtrTy =
llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Anders Carlsson
committed
std::vector<const llvm::Type *> Params;
Params.push_back(Int8PtrTy);
Anders Carlsson
committed
// Get the destructor function type
llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson
committed
DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Anders Carlsson
committed
Params.clear();
Params.push_back(DtorFnTy);
Params.push_back(Int8PtrTy);
Params.push_back(Int8PtrTy);
Anders Carlsson
committed
// Get the __cxa_atexit function type
// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
const llvm::FunctionType *AtExitFnTy =
Anders Carlsson
committed
llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Anders Carlsson
committed
llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
"__cxa_atexit");
Anders Carlsson
committed
llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
"__dso_handle");
llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
}
void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson
committed
llvm::Constant *DeclPtr) {
assert(D.hasGlobalStorage() &&
"VarDecl must have global storage!");
Anders Carlsson
committed
const Expr *Init = D.getInit();
QualType T = D.getType();
bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Anders Carlsson
committed
if (T->isReferenceType()) {
ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson
committed
} else if (!hasAggregateLLVMType(T)) {
llvm::Value *V = EmitScalarExpr(Init);
EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson
committed
} else if (T->isAnyComplexType()) {
EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson
committed
} else {
EmitAggExpr(Init, DeclPtr, isVolatile);
// Avoid generating destructor(s) for initialized objects.
if (!isa<CXXConstructExpr>(Init))
return;
const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
if (Array)
T = getContext().getBaseElementType(Array);
Anders Carlsson
committed
if (const RecordType *RT = T->getAs<RecordType>()) {
CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
if (!RD->hasTrivialDestructor()) {
llvm::Constant *DtorFn;
if (Array) {
DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
RD->getDestructor(getContext()),
Array, DeclPtr);
DeclPtr =
llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
}
else
DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
Dtor_Complete);
EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
}
Anders Carlsson
committed
}
}
}
void
CodeGenModule::EmitCXXGlobalInitFunc() {
if (CXXGlobalInits.empty())
return;
const llvm::FunctionType *FTy
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
false);
// Create our global initialization function.
// FIXME: Should this be tweakable by targets?
llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
"__cxx_global_initialization", &TheModule);
CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
CXXGlobalInits.size());
AddGlobalCtor(Fn);
}
void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
const VarDecl **Decls,
unsigned NumDecls) {
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
for (unsigned i = 0; i != NumDecls; ++i) {
const VarDecl *D = Decls[i];
llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
EmitCXXGlobalVarDeclInit(*D, DeclPtr);
}
FinishFunction();
}
void
CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson
committed
llvm::GlobalVariable *GV) {
// FIXME: This should use __cxa_guard_{acquire,release}?
Anders Carlsson
committed
assert(!getContext().getLangOptions().ThreadsafeStatics &&
"thread safe statics are currently not supported!");
llvm::SmallString<256> GuardVName;
llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlsson
committed
mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Anders Carlsson
committed
// Create the guard variable.
new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
false, GV->getLinkage(),
llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Anders Carlsson
committed
// Load the first byte of the guard variable.
const llvm::Type *PtrTy
= llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlsson
committed
"tmp");
Anders Carlsson
committed
// Compare it against 0.
llvm::Value *nullValue
= llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlsson
committed
llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
llvm::BasicBlock *InitBlock = createBasicBlock("init");
llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlsson
committed
// If the guard variable is 0, jump to the initializer code.
Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Anders Carlsson
committed
EmitBlock(InitBlock);
Anders Carlsson
committed
EmitCXXGlobalVarDeclInit(D, GV);
Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
1),
Anders Carlsson
committed
Builder.CreateBitCast(GuardV, PtrTy));
Anders Carlsson
committed
EmitBlock(EndBlock);
}
Anders Carlsson
committed
RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
llvm::Value *Callee,
llvm::Value *This,
CallExpr::const_arg_iterator ArgBeg,
CallExpr::const_arg_iterator ArgEnd) {
"Trying to emit a member call expr on a static method!");
Anders Carlsson
committed
// A call to a trivial destructor requires no code generation.
if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
if (Destructor->isTrivial())
return RValue::get(0);
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Anders Carlsson
committed
CallArgList Args;
Anders Carlsson
committed
// Push the this ptr.
Args.push_back(std::make_pair(RValue::get(This),
MD->getThisType(getContext())));
Anders Carlsson
committed
// And the rest of the call args
EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlsson
committed
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Callee, Args, MD);
}
Anders Carlsson
committed
/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
/// expr can be devirtualized.
static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// This is a record decl. We know the type and can devirtualize it.
return VD->getType()->isRecordType();
}
return false;
Anders Carlsson
committed
}
// We can always devirtualize calls on temporary object expressions.
if (isa<CXXTemporaryObjectExpr>(Base))
return true;
// And calls on bound temporaries.
if (isa<CXXBindTemporaryExpr>(Base))
return true;
// Check if this is a call expr that returns a record type.
if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
return CE->getCallReturnType()->isRecordType();
Anders Carlsson
committed
// We can't devirtualize the call.
return false;
}
Anders Carlsson
committed
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
if (isa<BinaryOperator>(CE->getCallee()))
return EmitCXXMemberPointerCallExpr(CE);
Anders Carlsson
committed
const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
if (MD->isStatic()) {
// The method is static, emit it as we would a regular call.
llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
return EmitCall(Callee, getContext().getPointerType(MD->getType()),
CE->arg_begin(), CE->arg_end(), 0);
}
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::Type *Ty =
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlsson
committed
FPT->isVariadic());
Anders Carlsson
committed
llvm::Value *This;
Anders Carlsson
committed
This = EmitScalarExpr(ME->getBase());
else {
LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlsson
committed
This = BaseLV.getAddress();
// C++ [class.virtual]p12:
// Explicit qualification with the scope operator (5.1) suppresses the
// virtual call mechanism.
Anders Carlsson
committed
//
// We also don't emit a virtual call if the base expression has a record type
// because then we know what the type is.
llvm::Value *Callee;
Anders Carlsson
committed
if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson
committed
!canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson
committed
Callee = BuildVirtualCall(MD, This, Ty);
else if (const CXXDestructorDecl *Destructor
= dyn_cast<CXXDestructorDecl>(MD))
Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
else
Callee = CGM.GetAddrOfFunction(MD, Ty);
return EmitCXXMemberCall(MD, Callee, This,
Anders Carlsson
committed
CE->arg_begin(), CE->arg_end());
RValue
CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson
committed
const Expr *BaseExpr = BO->getLHS();
const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson
committed
const MemberPointerType *MPT =
MemFnExpr->getType()->getAs<MemberPointerType>();
const FunctionProtoType *FPT =
MPT->getPointeeType()->getAs<FunctionProtoType>();
const CXXRecordDecl *RD =
cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
const llvm::FunctionType *FTy =
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
FPT->isVariadic());
const llvm::Type *Int8PtrTy =
llvm::Type::getInt8Ty(VMContext)->getPointerTo();
// Get the member function pointer.
llvm::Value *MemFnPtr =
Anders Carlsson
committed
CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Emit the 'this' pointer.
llvm::Value *This;
if (BO->getOpcode() == BinaryOperator::PtrMemI)
This = EmitScalarExpr(BaseExpr);
else
This = EmitLValue(BaseExpr).getAddress();
// Adjust it.
llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
This = Builder.CreateBitCast(Ptr, This->getType(), "this");
llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
// If the LSB in the function pointer is 1, the function pointer points to
// a virtual function.
llvm::Value *IsVirtual
= Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
"and");
IsVirtual = Builder.CreateTrunc(IsVirtual,
llvm::Type::getInt1Ty(VMContext));
llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
EmitBlock(FnVirtual);
const llvm::Type *VTableTy =
FTy->getPointerTo()->getPointerTo()->getPointerTo();
llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
VTable = Builder.CreateLoad(VTable);
VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
// Since the function pointer is 1 plus the virtual table offset, we
// subtract 1 by using a GEP.
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
EmitBranch(FnEnd);
EmitBlock(FnNonVirtual);
// If the function is not virtual, just load the pointer.
llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
EmitBlock(FnEnd);
llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
Callee->reserveOperandSpace(2);
Callee->addIncoming(VirtualFn, FnVirtual);
Callee->addIncoming(NonVirtualFn, FnNonVirtual);
CallArgList Args;
QualType ThisType =
getContext().getPointerType(getContext().getTagDeclType(RD));
// Push the this ptr.
Args.push_back(std::make_pair(RValue::get(This), ThisType));
// And the rest of the call args
EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
Callee, Args, 0);
}
CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
const CXXMethodDecl *MD) {
"Trying to emit a member call expr on a static method!");
if (MD->isCopyAssignment()) {
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
if (ClassDecl->hasTrivialCopyAssignment()) {
assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
"EmitCXXOperatorMemberCallExpr - user declared copy assignment");
llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
QualType Ty = E->getType();
EmitAggregateCopy(This, Src, Ty);
return RValue::get(This);
}
}
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::Type *Ty =
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
FPT->isVariadic());
llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
return EmitCXXMemberCall(MD, Callee, This,
E->arg_begin() + 1, E->arg_end());
}
llvm::Value *CodeGenFunction::LoadCXXThis() {
assert(isa<CXXMethodDecl>(CurFuncDecl) &&
"Must be in a C++ member function decl to load 'this'");
assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
"Must be in a C++ member function decl to load 'this'");
// FIXME: What if we're inside a block?
// ans: See how CodeGenFunction::LoadObjCSelf() uses
// CodeGenFunction::BlockForwardSelf() for how to do this.
return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
}
/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
/// for-loop to call the default constructor on individual members of the
Anders Carlsson
committed
/// array.
/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
/// array type and 'ArrayPtr' points to the beginning fo the array.
/// It is assumed that all relevant checks have been made by the caller.
void
CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson
committed
const ConstantArrayType *ArrayTy,
llvm::Value *ArrayPtr) {
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
llvm::Value * NumElements =
llvm::ConstantInt::get(SizeTy,
getContext().getConstantArrayElementCount(ArrayTy));
EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
}
void
CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
llvm::Value *NumElements,
llvm::Value *ArrayPtr) {
const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
// Create a temporary for the loop index and initialize it with 0.
Anders Carlsson
committed
llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
Builder.CreateStore(Zero, IndexPtr, false);
// Start the loop with a block that tests the condition.
llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
EmitBlock(CondBlock);
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
// Generate: if (loop-index < number-of-elements fall to the loop body,
// otherwise, go to the block after the for-loop.
llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson
committed
llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
// If the condition is true, execute the body.
Builder.CreateCondBr(IsLess, ForBody, AfterFor);
EmitBlock(ForBody);
llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
// Inside the loop body, emit the constructor call on the array element.
Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson
committed
llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
"arrayidx");
EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
EmitBlock(ContinueBlock);
// Emit the increment of the loop counter.
Anders Carlsson
committed
llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Counter = Builder.CreateLoad(IndexPtr);
NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
Builder.CreateStore(NextVal, IndexPtr, false);
// Finally, branch back up to the condition for the next iteration.
EmitBranch(CondBlock);
// Emit the fall-through block.
EmitBlock(AfterFor, true);
}
/// EmitCXXAggrDestructorCall - calls the default destructor on array
/// elements in reverse order of construction.
void
CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
const ArrayType *Array,
llvm::Value *This) {
const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
assert(CA && "Do we support VLA for destruction ?");
llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1);
uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
// Create a temporary for the loop index and initialize it with count of
// array elements.
llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
"loop.index");
// Index = ElementCount;
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
Builder.CreateStore(UpperCount, IndexPtr, false);
// Start the loop with a block that tests the condition.
llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
EmitBlock(CondBlock);
llvm::BasicBlock *ForBody = createBasicBlock("for.body");
// Generate: if (loop-index != 0 fall to the loop body,
// otherwise, go to the block after the for-loop.
llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
"isne");
// If the condition is true, execute the body.
Builder.CreateCondBr(IsNE, ForBody, AfterFor);
EmitBlock(ForBody);
llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
// Inside the loop body, emit the constructor call on the array element.
Counter = Builder.CreateLoad(IndexPtr);
Counter = Builder.CreateSub(Counter, One);
llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
EmitCXXDestructorCall(D, Dtor_Complete, Address);
EmitBlock(ContinueBlock);
// Emit the decrement of the loop counter.
Counter = Builder.CreateLoad(IndexPtr);
Counter = Builder.CreateSub(Counter, One, "dec");
Builder.CreateStore(Counter, IndexPtr, false);
// Finally, branch back up to the condition for the next iteration.
EmitBranch(CondBlock);
// Emit the fall-through block.
EmitBlock(AfterFor, true);
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/// EmitCXXAggrDestructorCall - Generates a helper function which when invoked,
/// calls the default destructor on array elements in reverse order of
/// construction.
llvm::Constant *
CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
const ArrayType *Array,
llvm::Value *This) {
static int UniqueCount;
FunctionArgList Args;
ImplicitParamDecl *Dst =
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Dst, Dst->getType()));
llvm::SmallString<16> Name;
llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
QualType R = getContext().VoidTy;
const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
llvm::Function *Fn =
llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
Name.c_str(),
&CGM.getModule());
IdentifierInfo *II
= &CGM.getContext().Idents.get(Name.c_str());
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
SourceLocation(), II, R, 0,
FunctionDecl::Static,
false, true);
StartFunction(FD, R, Fn, Args, SourceLocation());
QualType BaseElementTy = getContext().getBaseElementType(Array);
const llvm::Type *BasePtr = ConvertType(BaseElementTy);
BasePtr = llvm::PointerType::getUnqual(BasePtr);
llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
FinishFunction();
llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
0);
llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
return m;
}
void
CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
CXXCtorType Type,
llvm::Value *This,
CallExpr::const_arg_iterator ArgBeg,
CallExpr::const_arg_iterator ArgEnd) {
if (D->isCopyConstructor(getContext())) {
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
if (ClassDecl->hasTrivialCopyConstructor()) {
assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
"EmitCXXConstructorCall - user declared copy constructor");
const Expr *E = (*ArgBeg);
QualType Ty = E->getType();
llvm::Value *Src = EmitLValue(E).getAddress();
EmitAggregateCopy(This, Src, Ty);
return;
}
}
Anders Carlsson
committed
llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
}
void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
CXXDtorType Type,
llvm::Value *This) {
llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
EmitCXXMemberCall(D, Callee, This, 0, 0);
}
void
CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
const CXXConstructExpr *E) {
assert(Dest && "Must have a destination!");
const CXXConstructorDecl *CD = E->getConstructor();
const ConstantArrayType *Array =
getContext().getAsConstantArrayType(E->getType());
// For a copy constructor, even if it is trivial, must fall thru so
// its argument is code-gen'ed.
if (!CD->isCopyConstructor(getContext())) {
QualType InitType = E->getType();
InitType = getContext().getBaseElementType(Array);
const CXXRecordDecl *RD =
cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
if (RD->hasTrivialConstructor())
return;
// Code gen optimization to eliminate copy constructor and return
// its first argument instead.
if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
const Expr *Arg = E->getArg(0);
if (const CXXBindTemporaryExpr *BindExpr =
dyn_cast<CXXBindTemporaryExpr>(Arg))
Arg = BindExpr->getSubExpr();
EmitAggExpr(Arg, Dest, false);
QualType BaseElementTy = getContext().getBaseElementType(Array);
const llvm::Type *BasePtr = ConvertType(BaseElementTy);
BasePtr = llvm::PointerType::getUnqual(BasePtr);
llvm::Value *BaseAddrPtr =
Builder.CreateBitCast(Dest, BasePtr);
EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
}
else
// Call the constructor.
EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
E->arg_begin(), E->arg_end());
}
void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson
committed
EmitGlobal(GlobalDecl(D, Ctor_Complete));
EmitGlobal(GlobalDecl(D, Ctor_Base));
void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson
committed
CXXCtorType Type) {
Anders Carlsson
committed
llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Anders Carlsson
committed
SetFunctionDefinitionAttributes(D, Fn);
SetLLVMFunctionAttributesForDefinition(D, Fn);
}
CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
const llvm::FunctionType *FTy =
getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
FPT->isVariadic());
const char *Name = getMangledCXXCtorName(D, Type);
return cast<llvm::Function>(
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson
committed
const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson
committed
CXXCtorType Type) {
llvm::SmallString<256> Name;
llvm::raw_svector_ostream Out(Name);
Anders Carlsson
committed
mangleCXXCtor(getMangleContext(), D, Type, Out);
Anders Carlsson
committed
Name += '\0';
return UniqueMangledName(Name.begin(), Name.end());
}
void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
EmitCXXDestructor(D, Dtor_Complete);
EmitCXXDestructor(D, Dtor_Base);
}
void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson
committed
CXXDtorType Type) {
llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Anders Carlsson
committed
SetFunctionDefinitionAttributes(D, Fn);
SetLLVMFunctionAttributesForDefinition(D, Fn);
}
llvm::Function *
CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson
committed
CXXDtorType Type) {
const llvm::FunctionType *FTy =
getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Anders Carlsson
committed
const char *Name = getMangledCXXDtorName(D, Type);
return cast<llvm::Function>(
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson
committed
}
const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson
committed
CXXDtorType Type) {
llvm::SmallString<256> Name;
llvm::raw_svector_ostream Out(Name);
Anders Carlsson
committed
mangleCXXDtor(getMangleContext(), D, Type, Out);
Anders Carlsson
committed
Name += '\0';
return UniqueMangledName(Name.begin(), Name.end());
}
llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
const CXXMethodDecl *MD,
bool Extern, int64_t nv,
int64_t v) {
return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
int64_t v) {
llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
0);
const llvm::Type *OrigTy = V->getType();
if (nv) {
// Do the non-virtual adjustment
V = Builder.CreateBitCast(V, Ptr8Ty);
V = Builder.CreateConstInBoundsGEP1_64(V, nv);
V = Builder.CreateBitCast(V, OrigTy);
}
if (v) {
// Do the virtual this adjustment
const llvm::Type *PtrDiffTy =
ConvertType(getContext().getPointerDiffType());
llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
V = Builder.CreateLoad(V, "vtable");
llvm::Value *VTablePtr = V;
assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
v /= LLVMPointerWidth/8;
V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
V = Builder.CreateLoad(V);
V = Builder.CreateGEP(ThisVal, V);
V = Builder.CreateBitCast(V, OrigTy);
}
return V;
}
llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
const CXXMethodDecl *MD,
bool Extern,
int64_t nv_t,
int64_t v_t,
int64_t nv_r,
int64_t v_r) {
QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
FunctionArgList Args;
ImplicitParamDecl *ThisDecl =
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
MD->getThisType(getContext()));
Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
for (FunctionDecl::param_const_iterator i = MD->param_begin(),
e = MD->param_end();
i != e; ++i) {
ParmVarDecl *D = *i;
Args.push_back(std::make_pair(D, D->getType()));
}
IdentifierInfo *II
= &CGM.getContext().Idents.get("__thunk_named_foo_");
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
Extern
? FunctionDecl::Extern
: FunctionDecl::Static,
false, true);
StartFunction(FD, ResultType, Fn, Args, SourceLocation());
// generate body
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::Type *Ty =
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
FPT->isVariadic());
llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
QualType ArgType = MD->getThisType(getContext());
llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
if (nv_t || v_t) {
const llvm::Type *OrigTy = Callee->getType();
Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
if (nv_r || v_r) {
Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
Callee = Builder.CreateBitCast(Callee, OrigTy);
nv_r = v_r = 0;
}
}
CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
for (FunctionDecl::param_const_iterator i = MD->param_begin(),
e = MD->param_end();
i != e; ++i) {
ParmVarDecl *D = *i;
QualType ArgType = D->getType();
// llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
}
RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
Callee, CallArgs, MD);
bool CanBeZero = !(ResultType->isReferenceType()
// FIXME: attr nonnull can't be zero either
/* || ResultType->hasAttr<NonNullAttr>() */ );
// Do the return result adjustment.
if (CanBeZero) {
llvm::BasicBlock *NonZeroBlock = createBasicBlock();
llvm::BasicBlock *ZeroBlock = createBasicBlock();
llvm::BasicBlock *ContBlock = createBasicBlock();
const llvm::Type *Ty = RV.getScalarVal()->getType();
llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
NonZeroBlock, ZeroBlock);
EmitBlock(NonZeroBlock);
llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
EmitBranch(ContBlock);
EmitBlock(ZeroBlock);
llvm::Value *Z = RV.getScalarVal();
EmitBlock(ContBlock);
llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
RVOrZero->reserveOperandSpace(2);
RVOrZero->addIncoming(NZ, NonZeroBlock);
RVOrZero->addIncoming(Z, ZeroBlock);
RV = RValue::get(RVOrZero);
} else
RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
if (!ResultType->isVoidType())
EmitReturnOfRValue(RV, ResultType);
FinishFunction();
return Fn;
}
llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
int64_t nv, int64_t v) {
llvm::SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
Anders Carlsson
committed
mangleThunk(getMangleContext(), MD, nv, v, Out);
llvm::GlobalVariable::LinkageTypes linktype;
linktype = llvm::GlobalValue::WeakAnyLinkage;
if (!Extern)
linktype = llvm::GlobalValue::InternalLinkage;
llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::FunctionType *FTy =
getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
FPT->isVariadic());
llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
&getModule());
CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
return m;
}
llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
bool Extern, int64_t nv_t,
int64_t v_t, int64_t nv_r,
int64_t v_r) {
llvm::SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
Anders Carlsson
committed
mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
llvm::GlobalVariable::LinkageTypes linktype;
linktype = llvm::GlobalValue::WeakAnyLinkage;
if (!Extern)
linktype = llvm::GlobalValue::InternalLinkage;
llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
const llvm::FunctionType *FTy =
getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
FPT->isVariadic());
llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
&getModule());
CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
v_r);
llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
return m;
}
Anders Carlsson
committed
llvm::Value *
CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
const CXXRecordDecl *ClassDecl,
const CXXRecordDecl *BaseClassDecl) {
const llvm::Type *Int8PtrTy =
llvm::Type::getInt8Ty(VMContext)->getPointerTo();
llvm::Value *VTablePtr = Builder.CreateBitCast(This,
Int8PtrTy->getPointerTo());
VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
int64_t VBaseOffsetIndex =
CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
Anders Carlsson
committed
llvm::Value *VBaseOffsetPtr =
Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson
committed
const llvm::Type *PtrDiffTy =
ConvertType(getContext().getPointerDiffType());
VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
PtrDiffTy->getPointerTo());
llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
return VBaseOffset;
}
Anders Carlsson
committed
static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
llvm::Value *This, const llvm::Type *Ty) {
Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
Vtable = CGF.Builder.CreateLoad(Vtable);
llvm::Value *VFuncPtr =
CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
return CGF.Builder.CreateLoad(VFuncPtr);
}
llvm::Value *