CGCall.cpp revision 207619
11573Srgrimes//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
21573Srgrimes//
31573Srgrimes//                     The LLVM Compiler Infrastructure
41573Srgrimes//
51573Srgrimes// This file is distributed under the University of Illinois Open Source
61573Srgrimes// License. See LICENSE.TXT for details.
71573Srgrimes//
81573Srgrimes//===----------------------------------------------------------------------===//
91573Srgrimes//
101573Srgrimes// These classes wrap the information about a call or function
111573Srgrimes// definition used to handle ABI compliancy.
121573Srgrimes//
131573Srgrimes//===----------------------------------------------------------------------===//
141573Srgrimes
151573Srgrimes#include "CGCall.h"
161573Srgrimes#include "CodeGenFunction.h"
171573Srgrimes#include "CodeGenModule.h"
181573Srgrimes#include "clang/Basic/TargetInfo.h"
191573Srgrimes#include "clang/AST/Decl.h"
201573Srgrimes#include "clang/AST/DeclCXX.h"
211573Srgrimes#include "clang/AST/DeclObjC.h"
221573Srgrimes#include "clang/CodeGen/CodeGenOptions.h"
231573Srgrimes#include "llvm/Attributes.h"
241573Srgrimes#include "llvm/Support/CallSite.h"
251573Srgrimes#include "llvm/Target/TargetData.h"
261573Srgrimes
271573Srgrimes#include "ABIInfo.h"
281573Srgrimes
291573Srgrimesusing namespace clang;
301573Srgrimesusing namespace CodeGen;
311573Srgrimes
321573Srgrimes/***/
331573Srgrimes
341573Srgrimes// FIXME: Use iterator and sidestep silly type array creation.
351573Srgrimes
361573Srgrimesstatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
371573Srgrimes  switch (CC) {
381573Srgrimes  default: return llvm::CallingConv::C;
391573Srgrimes  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
4092986Sobrien  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
4192986Sobrien  }
421573Srgrimes}
431573Srgrimes
441573Srgrimes/// Derives the 'this' type for codegen purposes, i.e. ignoring method
451573Srgrimes/// qualification.
461573Srgrimes/// FIXME: address space qualification?
471573Srgrimesstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
481573Srgrimes  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
4971579Sdeischen  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
501573Srgrimes}
511573Srgrimes
5287113Sfenner/// Returns the canonical formal type of the given C++ method.
531573Srgrimesstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
5487490Sphantom  return MD->getType()->getCanonicalTypeUnqualified()
5587113Sfenner           .getAs<FunctionProtoType>();
5687113Sfenner}
571573Srgrimes
581573Srgrimes/// Returns the "extra-canonicalized" return type, which discards
591573Srgrimes/// qualifiers on the return type.  Codegen doesn't care about them,
60103633Stjr/// and it makes ABI code a little easier to be able to assume that
611573Srgrimes/// all parameter and return types are top-level unqualified.
621573Srgrimesstatic CanQualType GetReturnType(QualType RetTy) {
6371579Sdeischen  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
641573Srgrimes}
6571579Sdeischen
661573Srgrimesconst CGFunctionInfo &
671573SrgrimesCodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
681573Srgrimes  return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
691573Srgrimes                         llvm::SmallVector<CanQualType, 16>(),
701573Srgrimes                         FTNP->getExtInfo());
711573Srgrimes}
7284922Sdfr
7384962Sbde/// \param Args - contains any initial parameters besides those
7484962Sbde///   in the formal type
7584962Sbdestatic const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
7684962Sbde                                  llvm::SmallVectorImpl<CanQualType> &ArgTys,
7787113Sfenner                                             CanQual<FunctionProtoType> FTP) {
7887113Sfenner  // FIXME: Kill copy.
7987113Sfenner  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
8087113Sfenner    ArgTys.push_back(FTP->getArgType(i));
8187113Sfenner  CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
8287113Sfenner  return CGT.getFunctionInfo(ResTy, ArgTys,
8384962Sbde                             FTP->getExtInfo());
8484962Sbde}
8587113Sfenner
8684962Sbdeconst CGFunctionInfo &
8784962SbdeCodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
8884962Sbde  llvm::SmallVector<CanQualType, 16> ArgTys;
8987113Sfenner  return ::getFunctionInfo(*this, ArgTys, FTP);
9087113Sfenner}
9187113Sfenner
9287113Sfennerstatic CallingConv getCallingConventionForDecl(const Decl *D) {
9384922Sdfr  // Set the appropriate calling convention for the Function.
9484962Sbde  if (D->hasAttr<StdCallAttr>())
9584962Sbde    return CC_X86StdCall;
9684922Sdfr
97103633Stjr  if (D->hasAttr<FastCallAttr>())
98103633Stjr    return CC_X86FastCall;
9984922Sdfr
10084922Sdfr  return CC_C;
10187113Sfenner}
10287113Sfenner
10387113Sfennerconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
10487113Sfenner                                                 const FunctionProtoType *FTP) {
10587113Sfenner  llvm::SmallVector<CanQualType, 16> ArgTys;
10687113Sfenner
10787113Sfenner  // Add the 'this' pointer.
10887113Sfenner  ArgTys.push_back(GetThisType(Context, RD));
109103633Stjr
11087113Sfenner  return ::getFunctionInfo(*this, ArgTys,
11187113Sfenner              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
11292905Sobrien}
11392941Sobrien
114113146Sdasconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
11592941Sobrien  llvm::SmallVector<CanQualType, 16> ArgTys;
116113146Sdas
11792941Sobrien  // Add the 'this' pointer unless this is a static method.
118103633Stjr  if (MD->isInstance())
11992905Sobrien    ArgTys.push_back(GetThisType(Context, MD->getParent()));
12092905Sobrien
12116586Sjraynard  return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
1221573Srgrimes}
1231573Srgrimes
1241573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
1251573Srgrimes                                                    CXXCtorType Type) {
1261573Srgrimes  llvm::SmallVector<CanQualType, 16> ArgTys;
12771579Sdeischen
1281573Srgrimes  // Add the 'this' pointer.
12971579Sdeischen  ArgTys.push_back(GetThisType(Context, D->getParent()));
1301573Srgrimes
1311573Srgrimes  // Check if we need to add a VTT parameter (which has type void **).
1321573Srgrimes  if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
1331573Srgrimes    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
1341573Srgrimes
1351573Srgrimes  return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
1361573Srgrimes}
1371573Srgrimes
1381573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
1391573Srgrimes                                                    CXXDtorType Type) {
1401573Srgrimes  llvm::SmallVector<CanQualType, 16> ArgTys;
1411573Srgrimes
1421573Srgrimes  // Add the 'this' pointer.
1431573Srgrimes  ArgTys.push_back(GetThisType(Context, D->getParent()));
1441573Srgrimes
1451573Srgrimes  // Check if we need to add a VTT parameter (which has type void **).
1461573Srgrimes  if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
14771579Sdeischen    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
1481573Srgrimes
1491573Srgrimes  return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
1501573Srgrimes}
1511573Srgrimes
1521573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
1531573Srgrimes  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
1541573Srgrimes    if (MD->isInstance())
1551573Srgrimes      return getFunctionInfo(MD);
1561573Srgrimes
1571573Srgrimes  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
158101776Stjr  assert(isa<FunctionType>(FTy));
1591573Srgrimes  if (isa<FunctionNoProtoType>(FTy))
1601573Srgrimes    return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
1611573Srgrimes  assert(isa<FunctionProtoType>(FTy));
1621573Srgrimes  return getFunctionInfo(FTy.getAs<FunctionProtoType>());
1631573Srgrimes}
1641573Srgrimes
1651573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
16671579Sdeischen  llvm::SmallVector<CanQualType, 16> ArgTys;
16771579Sdeischen  ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
1681573Srgrimes  ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
1691573Srgrimes  // FIXME: Kill copy?
1701573Srgrimes  for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
1711573Srgrimes         e = MD->param_end(); i != e; ++i) {
1721573Srgrimes    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
1731573Srgrimes  }
1741573Srgrimes  return getFunctionInfo(GetReturnType(MD->getResultType()),
1751573Srgrimes                         ArgTys,
1761573Srgrimes                         FunctionType::ExtInfo(
1771573Srgrimes                             /*NoReturn*/ false,
1781573Srgrimes                             /*RegParm*/ 0,
1791573Srgrimes                             getCallingConventionForDecl(MD)));
1801573Srgrimes}
1811573Srgrimes
1821573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
1831573Srgrimes  // FIXME: Do we need to handle ObjCMethodDecl?
1841573Srgrimes  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1851573Srgrimes
1861573Srgrimes  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
1871573Srgrimes    return getFunctionInfo(CD, GD.getCtorType());
188113146Sdas
18987815Sphantom  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
1901573Srgrimes    return getFunctionInfo(DD, GD.getDtorType());
19192889Sobrien
19292889Sobrien  return getFunctionInfo(FD);
19387113Sfenner}
1941573Srgrimes
1951573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
1961573Srgrimes                                                    const CallArgList &Args,
1971573Srgrimes                                            const FunctionType::ExtInfo &Info) {
1981573Srgrimes  // FIXME: Kill copy.
1991573Srgrimes  llvm::SmallVector<CanQualType, 16> ArgTys;
2001573Srgrimes  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
2011573Srgrimes       i != e; ++i)
2021573Srgrimes    ArgTys.push_back(Context.getCanonicalParamType(i->second));
2031573Srgrimes  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
2041573Srgrimes}
20587113Sfenner
2061573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
2071573Srgrimes                                                    const FunctionArgList &Args,
2081573Srgrimes                                            const FunctionType::ExtInfo &Info) {
2091573Srgrimes  // FIXME: Kill copy.
2101573Srgrimes  llvm::SmallVector<CanQualType, 16> ArgTys;
2111573Srgrimes  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
2121573Srgrimes       i != e; ++i)
2131573Srgrimes    ArgTys.push_back(Context.getCanonicalParamType(i->second));
21487113Sfenner  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
2151573Srgrimes}
2161573Srgrimes
2171573Srgrimesconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
2181573Srgrimes                           const llvm::SmallVectorImpl<CanQualType> &ArgTys,
2191573Srgrimes                                            const FunctionType::ExtInfo &Info) {
22087815Sphantom#ifndef NDEBUG
22187815Sphantom  for (llvm::SmallVectorImpl<CanQualType>::const_iterator
22287815Sphantom         I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
22387815Sphantom    assert(I->isCanonicalAsParam());
22487815Sphantom#endif
22587818Sphantom
22687818Sphantom  unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
22787815Sphantom
22887113Sfenner  // Lookup or create unique function info.
22987815Sphantom  llvm::FoldingSetNodeID ID;
23087815Sphantom  CGFunctionInfo::Profile(ID, Info, ResTy,
23187815Sphantom                          ArgTys.begin(), ArgTys.end());
23287815Sphantom
23387815Sphantom  void *InsertPos = 0;
23488057Sphantom  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
23588057Sphantom  if (FI)
23687113Sfenner    return *FI;
2371573Srgrimes
2381573Srgrimes  // Construct the function info.
2391573Srgrimes  FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys);
2401573Srgrimes  FunctionInfos.InsertNode(FI, InsertPos);
2411573Srgrimes
2421573Srgrimes  // Compute ABI information.
2431573Srgrimes  getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
2441573Srgrimes
2451573Srgrimes  return *FI;
2461573Srgrimes}
2471573Srgrimes
2481573SrgrimesCGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
2491573Srgrimes                               bool _NoReturn,
2501573Srgrimes                               unsigned _RegParm,
2511573Srgrimes                               CanQualType ResTy,
2521573Srgrimes                               const llvm::SmallVectorImpl<CanQualType> &ArgTys)
2531573Srgrimes  : CallingConvention(_CallingConvention),
2541573Srgrimes    EffectiveCallingConvention(_CallingConvention),
2551573Srgrimes    NoReturn(_NoReturn), RegParm(_RegParm)
2561573Srgrimes{
2571573Srgrimes  NumArgs = ArgTys.size();
2581573Srgrimes  Args = new ArgInfo[1 + NumArgs];
2591573Srgrimes  Args[0].type = ResTy;
2601573Srgrimes  for (unsigned i = 0; i < NumArgs; ++i)
2611573Srgrimes    Args[1 + i].type = ArgTys[i];
2621573Srgrimes}
26387113Sfenner
2641573Srgrimes/***/
265113146Sdas
26687815Sphantomvoid CodeGenTypes::GetExpandedTypes(QualType Ty,
2671573Srgrimes                                    std::vector<const llvm::Type*> &ArgTys) {
26871579Sdeischen  const RecordType *RT = Ty->getAsStructureType();
26987113Sfenner  assert(RT && "Can only expand structure types.");
27087113Sfenner  const RecordDecl *RD = RT->getDecl();
2711573Srgrimes  assert(!RD->hasFlexibleArrayMember() &&
2721573Srgrimes         "Cannot expand structure with flexible array.");
2731573Srgrimes
2741573Srgrimes  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
27587113Sfenner         i != e; ++i) {
27687815Sphantom    const FieldDecl *FD = *i;
2771573Srgrimes    assert(!FD->isBitField() &&
2781573Srgrimes           "Cannot expand structure with bit-field members.");
2791573Srgrimes
2801573Srgrimes    QualType FT = FD->getType();
2811573Srgrimes    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
2821573Srgrimes      GetExpandedTypes(FT, ArgTys);
28387113Sfenner    } else {
28487113Sfenner      ArgTys.push_back(ConvertType(FT));
2851573Srgrimes    }
28687113Sfenner  }
2871573Srgrimes}
2881573Srgrimes
2891573Srgrimesllvm::Function::arg_iterator
2901573SrgrimesCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
2911573Srgrimes                                    llvm::Function::arg_iterator AI) {
29287815Sphantom  const RecordType *RT = Ty->getAsStructureType();
29387815Sphantom  assert(RT && "Can only expand structure types.");
29487815Sphantom
29587815Sphantom  RecordDecl *RD = RT->getDecl();
29687815Sphantom  assert(LV.isSimple() &&
29787818Sphantom         "Unexpected non-simple lvalue during struct expansion.");
29887818Sphantom  llvm::Value *Addr = LV.getAddress();
29987815Sphantom  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
30087113Sfenner         i != e; ++i) {
30187815Sphantom    FieldDecl *FD = *i;
30287815Sphantom    QualType FT = FD->getType();
30387815Sphantom
30487815Sphantom    // FIXME: What are the right qualifiers here?
30587815Sphantom    LValue LV = EmitLValueForField(Addr, FD, 0);
30688057Sphantom    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
30788057Sphantom      AI = ExpandTypeFromArgs(FT, LV, AI);
30888057Sphantom    } else {
3091573Srgrimes      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
3101573Srgrimes      ++AI;
3111573Srgrimes    }
3121573Srgrimes  }
3131573Srgrimes
3141573Srgrimes  return AI;
3151573Srgrimes}
3161573Srgrimes
3171573Srgrimesvoid
3181573SrgrimesCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
3191573Srgrimes                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
3201573Srgrimes  const RecordType *RT = Ty->getAsStructureType();
3211573Srgrimes  assert(RT && "Can only expand structure types.");
3221573Srgrimes
3231573Srgrimes  RecordDecl *RD = RT->getDecl();
3241573Srgrimes  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
3251573Srgrimes  llvm::Value *Addr = RV.getAggregateAddr();
3261573Srgrimes  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3271573Srgrimes         i != e; ++i) {
3281573Srgrimes    FieldDecl *FD = *i;
3291573Srgrimes    QualType FT = FD->getType();
3301573Srgrimes
3311573Srgrimes    // FIXME: What are the right qualifiers here?
3321573Srgrimes    LValue LV = EmitLValueForField(Addr, FD, 0);
3331573Srgrimes    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
3341573Srgrimes      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
33571579Sdeischen    } else {
336103633Stjr      RValue RV = EmitLoadOfLValue(LV, FT);
337103633Stjr      assert(RV.isScalar() &&
338103633Stjr             "Unexpected non-scalar rvalue during struct expansion.");
339103633Stjr      Args.push_back(RV.getScalarVal());
340103633Stjr    }
341103633Stjr  }
342103633Stjr}
343103633Stjr
344103633Stjr/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
345103633Stjr/// a pointer to an object of type \arg Ty.
346103633Stjr///
347103633Stjr/// This safely handles the case when the src type is smaller than the
348103633Stjr/// destination type; in this situation the values of bits which not
349103633Stjr/// present in the src are undefined.
350103633Stjrstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
351103633Stjr                                      const llvm::Type *Ty,
352103633Stjr                                      CodeGenFunction &CGF) {
353103633Stjr  const llvm::Type *SrcTy =
354103633Stjr    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
355103633Stjr  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
356103633Stjr  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
357103633Stjr
358103633Stjr  // If load is legal, just bitcast the src pointer.
359103633Stjr  if (SrcSize >= DstSize) {
360103633Stjr    // Generally SrcSize is never greater than DstSize, since this means we are
361103633Stjr    // losing bits. However, this can happen in cases where the structure has
362103633Stjr    // additional padding, for example due to a user specified alignment.
363103633Stjr    //
364103633Stjr    // FIXME: Assert that we aren't truncating non-padding bits when have access
365103633Stjr    // to that information.
366103633Stjr    llvm::Value *Casted =
367103633Stjr      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
368103633Stjr    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
369103633Stjr    // FIXME: Use better alignment / avoid requiring aligned load.
370103633Stjr    Load->setAlignment(1);
371103633Stjr    return Load;
372103633Stjr  } else {
373103633Stjr    // Otherwise do coercion through memory. This is stupid, but
374103633Stjr    // simple.
375103633Stjr    llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
376103633Stjr    llvm::Value *Casted =
377103633Stjr      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
378103633Stjr    llvm::StoreInst *Store =
379103633Stjr      CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
380103633Stjr    // FIXME: Use better alignment / avoid requiring aligned store.
381103633Stjr    Store->setAlignment(1);
382103633Stjr    return CGF.Builder.CreateLoad(Tmp);
383103633Stjr  }
384103633Stjr}
385103633Stjr
386103633Stjr/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
387103633Stjr/// where the source and destination may have different types.
388103633Stjr///
389103633Stjr/// This safely handles the case when the src type is larger than the
390103633Stjr/// destination type; the upper bits of the src will be lost.
391103633Stjrstatic void CreateCoercedStore(llvm::Value *Src,
392103633Stjr                               llvm::Value *DstPtr,
393103633Stjr                               bool DstIsVolatile,
394103633Stjr                               CodeGenFunction &CGF) {
39571579Sdeischen  const llvm::Type *SrcTy = Src->getType();
39671579Sdeischen  const llvm::Type *DstTy =
39771579Sdeischen    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
398103012Stjr
399101914Srobert  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
40071579Sdeischen  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
40171579Sdeischen
40271579Sdeischen  // If store is legal, just bitcast the src pointer.
40371579Sdeischen  if (SrcSize <= DstSize) {
40471579Sdeischen    llvm::Value *Casted =
40571579Sdeischen      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
40671579Sdeischen    // FIXME: Use better alignment / avoid requiring aligned store.
40771579Sdeischen    CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
40871579Sdeischen  } else {
4091573Srgrimes    // Otherwise do coercion through memory. This is stupid, but
410113146Sdas    // simple.
411113146Sdas
412113146Sdas    // Generally SrcSize is never greater than DstSize, since this means we are
413113146Sdas    // losing bits. However, this can happen in cases where the structure has
414113146Sdas    // additional padding, for example due to a user specified alignment.
4151573Srgrimes    //
4161573Srgrimes    // FIXME: Assert that we aren't truncating non-padding bits when have access
417113146Sdas    // to that information.
4181573Srgrimes    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
4191573Srgrimes    CGF.Builder.CreateStore(Src, Tmp);
4201573Srgrimes    llvm::Value *Casted =
42192905Sobrien      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
4221573Srgrimes    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
423113142Sdas    // FIXME: Use better alignment / avoid requiring aligned load.
4241573Srgrimes    Load->setAlignment(1);
425113142Sdas    CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
426113142Sdas  }
427113142Sdas}
428113142Sdas
429113142Sdas/***/
430113142Sdas
431113142Sdasbool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
432113142Sdas  return FI.getReturnInfo().isIndirect();
4331573Srgrimes}
43421674Sjkh
4351573Srgrimesconst llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
4361573Srgrimes  const CGFunctionInfo &FI = getFunctionInfo(GD);
4371573Srgrimes
4381573Srgrimes  // For definition purposes, don't consider a K&R function variadic.
4391573Srgrimes  bool Variadic = false;
4401573Srgrimes  if (const FunctionProtoType *FPT =
44131871Sbde        cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
4421573Srgrimes    Variadic = FPT->isVariadic();
44387113Sfenner
4441573Srgrimes  return GetFunctionType(FI, Variadic);
4451573Srgrimes}
44688057Sphantom
44787815Sphantomconst llvm::FunctionType *
44887113SfennerCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
44987815Sphantom  std::vector<const llvm::Type*> ArgTys;
45087815Sphantom
45187815Sphantom  const llvm::Type *ResultType = 0;
45287815Sphantom
45387113Sfenner  QualType RetTy = FI.getReturnType();
45471579Sdeischen  const ABIArgInfo &RetAI = FI.getReturnInfo();
45571579Sdeischen  switch (RetAI.getKind()) {
45671579Sdeischen  case ABIArgInfo::Expand:
4571573Srgrimes    assert(0 && "Invalid ABI kind for return argument");
45871579Sdeischen
4591573Srgrimes  case ABIArgInfo::Extend:
46071579Sdeischen  case ABIArgInfo::Direct:
46171579Sdeischen    ResultType = ConvertType(RetTy);
46271579Sdeischen    break;
46371579Sdeischen
46471579Sdeischen  case ABIArgInfo::Indirect: {
46571579Sdeischen    assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
4661573Srgrimes    ResultType = llvm::Type::getVoidTy(getLLVMContext());
4671573Srgrimes    const llvm::Type *STy = ConvertType(RetTy);
468113146Sdas    ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
4691573Srgrimes    break;
47087815Sphantom  }
47187815Sphantom
4721573Srgrimes  case ABIArgInfo::Ignore:
473113146Sdas    ResultType = llvm::Type::getVoidTy(getLLVMContext());
474113146Sdas    break;
475113146Sdas
476113146Sdas  case ABIArgInfo::Coerce:
477113146Sdas    ResultType = RetAI.getCoerceToType();
478113146Sdas    break;
479113146Sdas  }
480113146Sdas
481113146Sdas  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
482113146Sdas         ie = FI.arg_end(); it != ie; ++it) {
483113146Sdas    const ABIArgInfo &AI = it->info;
484113146Sdas
485113146Sdas    switch (AI.getKind()) {
486113146Sdas    case ABIArgInfo::Ignore:
48787490Sphantom      break;
488113146Sdas
489113146Sdas    case ABIArgInfo::Coerce:
490113146Sdas      ArgTys.push_back(AI.getCoerceToType());
491113146Sdas      break;
492113146Sdas
4931573Srgrimes    case ABIArgInfo::Indirect: {
494113146Sdas      // indirect arguments are always on the stack, which is addr space #0.
495113146Sdas      const llvm::Type *LTy = ConvertTypeForMem(it->type);
4961573Srgrimes      ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
497113146Sdas      break;
498113146Sdas    }
499113146Sdas
50072523Stegge    case ABIArgInfo::Extend:
501113146Sdas    case ABIArgInfo::Direct:
502113146Sdas      ArgTys.push_back(ConvertType(it->type));
5031573Srgrimes      break;
5041573Srgrimes
50587113Sfenner    case ABIArgInfo::Expand:
5061573Srgrimes      GetExpandedTypes(it->type, ArgTys);
5071573Srgrimes      break;
50814727Sfenner    }
5091573Srgrimes  }
51031983Sache
511113146Sdas  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
5121573Srgrimes}
5131573Srgrimes
5141573Srgrimesstatic bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
515113142Sdas  if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
516113146Sdas    if (!TT->getDecl()->isDefinition())
517103399Stjr      return true;
518103399Stjr  }
519103399Stjr
520103399Stjr  for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
521103633Stjr    if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
5221573Srgrimes      if (!TT->getDecl()->isDefinition())
5231573Srgrimes        return true;
5241573Srgrimes    }
5251573Srgrimes  }
5261573Srgrimes
5271573Srgrimes  return false;
5281573Srgrimes}
5291573Srgrimes
5301573Srgrimesconst llvm::Type *
5311573SrgrimesCodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
5321573Srgrimes  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
5331573Srgrimes
534113146Sdas  if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
535113146Sdas    return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
536113146Sdas
5371573Srgrimes  return llvm::OpaqueType::get(getLLVMContext());
5381573Srgrimes}
5391573Srgrimes
5401573Srgrimesvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
5411573Srgrimes                                           const Decl *TargetDecl,
5421573Srgrimes                                           AttributeListType &PAL,
5431573Srgrimes                                           unsigned &CallingConv) {
5441573Srgrimes  unsigned FuncAttrs = 0;
5451573Srgrimes  unsigned RetAttrs = 0;
5461573Srgrimes
5471573Srgrimes  CallingConv = FI.getEffectiveCallingConvention();
5481573Srgrimes
5491573Srgrimes  if (FI.isNoReturn())
5501573Srgrimes    FuncAttrs |= llvm::Attribute::NoReturn;
5511573Srgrimes
5521573Srgrimes  // FIXME: handle sseregparm someday...
5531573Srgrimes  if (TargetDecl) {
5541573Srgrimes    if (TargetDecl->hasAttr<NoThrowAttr>())
5551573Srgrimes      FuncAttrs |= llvm::Attribute::NoUnwind;
5561573Srgrimes    if (TargetDecl->hasAttr<NoReturnAttr>())
5571573Srgrimes      FuncAttrs |= llvm::Attribute::NoReturn;
5581573Srgrimes    if (TargetDecl->hasAttr<ConstAttr>())
5591573Srgrimes      FuncAttrs |= llvm::Attribute::ReadNone;
5601573Srgrimes    else if (TargetDecl->hasAttr<PureAttr>())
5611573Srgrimes      FuncAttrs |= llvm::Attribute::ReadOnly;
5621573Srgrimes    if (TargetDecl->hasAttr<MallocAttr>())
5631573Srgrimes      RetAttrs |= llvm::Attribute::NoAlias;
5641573Srgrimes  }
5651573Srgrimes
5661573Srgrimes  if (CodeGenOpts.OptimizeSize)
567103399Stjr    FuncAttrs |= llvm::Attribute::OptimizeForSize;
568103399Stjr  if (CodeGenOpts.DisableRedZone)
569103399Stjr    FuncAttrs |= llvm::Attribute::NoRedZone;
570103399Stjr  if (CodeGenOpts.NoImplicitFloat)
571103399Stjr    FuncAttrs |= llvm::Attribute::NoImplicitFloat;
57221674Sjkh
573103399Stjr  QualType RetTy = FI.getReturnType();
574103399Stjr  unsigned Index = 1;
57521674Sjkh  const ABIArgInfo &RetAI = FI.getReturnInfo();
5761573Srgrimes  switch (RetAI.getKind()) {
5771573Srgrimes  case ABIArgInfo::Extend:
5781573Srgrimes   if (RetTy->isSignedIntegerType()) {
5791573Srgrimes     RetAttrs |= llvm::Attribute::SExt;
5801573Srgrimes   } else if (RetTy->isUnsignedIntegerType()) {
58121674Sjkh     RetAttrs |= llvm::Attribute::ZExt;
58221674Sjkh   }
58387113Sfenner   // FALLTHROUGH
58421674Sjkh  case ABIArgInfo::Direct:
5851573Srgrimes    break;
58621674Sjkh
58721674Sjkh  case ABIArgInfo::Indirect:
58887113Sfenner    PAL.push_back(llvm::AttributeWithIndex::get(Index,
58921674Sjkh                                                llvm::Attribute::StructRet));
59087113Sfenner    ++Index;
59187113Sfenner    // sret disables readnone and readonly
59287113Sfenner    FuncAttrs &= ~(llvm::Attribute::ReadOnly |
59387113Sfenner                   llvm::Attribute::ReadNone);
59487113Sfenner    break;
59587113Sfenner
59687113Sfenner  case ABIArgInfo::Ignore:
59787113Sfenner  case ABIArgInfo::Coerce:
59887113Sfenner    break;
59987113Sfenner
60087113Sfenner  case ABIArgInfo::Expand:
6011573Srgrimes    assert(0 && "Invalid ABI kind for return argument");
602103399Stjr  }
603103399Stjr
604103399Stjr  if (RetAttrs)
605103399Stjr    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
60621674Sjkh
607103399Stjr  // FIXME: we need to honour command line settings also...
608103399Stjr  // FIXME: RegParm should be reduced in case of nested functions and/or global
609103399Stjr  // register variable.
610103399Stjr  signed RegParm = FI.getRegParm();
611103399Stjr
612103399Stjr  unsigned PointerWidth = getContext().Target.getPointerWidth(0);
613103399Stjr  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
614103399Stjr         ie = FI.arg_end(); it != ie; ++it) {
615103399Stjr    QualType ParamType = it->type;
616103399Stjr    const ABIArgInfo &AI = it->info;
617103399Stjr    unsigned Attributes = 0;
618103399Stjr
619103399Stjr    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
62021674Sjkh    // have the corresponding parameter variable.  It doesn't make
621103399Stjr    // sense to do it here because parameters are so fucked up.
622103399Stjr
623103399Stjr    switch (AI.getKind()) {
624103399Stjr    case ABIArgInfo::Coerce:
625103399Stjr      break;
62687815Sphantom
62788057Sphantom    case ABIArgInfo::Indirect:
62887815Sphantom      if (AI.getIndirectByVal())
62987815Sphantom        Attributes |= llvm::Attribute::ByVal;
630103633Stjr
63172523Stegge      Attributes |=
63272523Stegge        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
63387113Sfenner      // byval disables readnone and readonly.
63472523Stegge      FuncAttrs &= ~(llvm::Attribute::ReadOnly |
6351573Srgrimes                     llvm::Attribute::ReadNone);
63671579Sdeischen      break;
6371573Srgrimes
6381573Srgrimes    case ABIArgInfo::Extend:
6391573Srgrimes     if (ParamType->isSignedIntegerType()) {
6401573Srgrimes       Attributes |= llvm::Attribute::SExt;
64171579Sdeischen     } else if (ParamType->isUnsignedIntegerType()) {
6421573Srgrimes       Attributes |= llvm::Attribute::ZExt;
6431573Srgrimes     }
6441573Srgrimes     // FALLS THROUGH
645103399Stjr    case ABIArgInfo::Direct:
646103399Stjr      if (RegParm > 0 &&
647103876Stjr          (ParamType->isIntegerType() || ParamType->isPointerType())) {
6481573Srgrimes        RegParm -=
6491573Srgrimes          (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
6501573Srgrimes        if (RegParm >= 0)
6511573Srgrimes          Attributes |= llvm::Attribute::InReg;
6521573Srgrimes      }
6531573Srgrimes      // FIXME: handle sseregparm someday...
6541573Srgrimes      break;
6551573Srgrimes
6561573Srgrimes    case ABIArgInfo::Ignore:
6571573Srgrimes      // Skip increment, no matching LLVM parameter.
6581573Srgrimes      continue;
6591573Srgrimes
66032253Sache    case ABIArgInfo::Expand: {
66131983Sache      std::vector<const llvm::Type*> Tys;
66231983Sache      // FIXME: This is rather inefficient. Do we ever actually need to do
66331983Sache      // anything here? The result should be just reconstructed on the other
6641573Srgrimes      // side, so extension should be a non-issue.
6651573Srgrimes      getTypes().GetExpandedTypes(ParamType, Tys);
6661573Srgrimes      Index += Tys.size();
6671573Srgrimes      continue;
6681573Srgrimes    }
6691573Srgrimes    }
6701573Srgrimes
6711573Srgrimes    if (Attributes)
6721573Srgrimes      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
6731573Srgrimes    ++Index;
6741573Srgrimes  }
6751573Srgrimes  if (FuncAttrs)
676113146Sdas    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
6771573Srgrimes}
6781573Srgrimes
6791573Srgrimesvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
6801573Srgrimes                                         llvm::Function *Fn,
68188057Sphantom                                         const FunctionArgList &Args) {
6821573Srgrimes  // If this is an implicit-return-zero function, go ahead and
6831573Srgrimes  // initialize the return value.  TODO: it might be nice to have
6841573Srgrimes  // a more general mechanism for this that didn't require synthesized
6851573Srgrimes  // return statements.
6861573Srgrimes  if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
6871573Srgrimes    if (FD->hasImplicitReturnZero()) {
6881573Srgrimes      QualType RetTy = FD->getResultType().getUnqualifiedType();
6891573Srgrimes      const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
6901573Srgrimes      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
6911573Srgrimes      Builder.CreateStore(Zero, ReturnValue);
6921573Srgrimes    }
69388057Sphantom  }
6941573Srgrimes
6951573Srgrimes  // FIXME: We no longer need the types from FunctionArgList; lift up and
6961573Srgrimes  // simplify.
6971573Srgrimes
6981573Srgrimes  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
69921674Sjkh  llvm::Function::arg_iterator AI = Fn->arg_begin();
70021674Sjkh
7011573Srgrimes  // Name the struct return argument.
7021573Srgrimes  if (CGM.ReturnTypeUsesSret(FI)) {
7031573Srgrimes    AI->setName("agg.result");
7041573Srgrimes    ++AI;
7051573Srgrimes  }
7061573Srgrimes
7071573Srgrimes  assert(FI.arg_size() == Args.size() &&
7081573Srgrimes         "Mismatch between function signature & arguments.");
7091573Srgrimes  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
71087113Sfenner  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
71187815Sphantom       i != e; ++i, ++info_it) {
71287815Sphantom    const VarDecl *Arg = i->first;
71387815Sphantom    QualType Ty = info_it->type;
71487113Sfenner    const ABIArgInfo &ArgI = info_it->info;
7151573Srgrimes
7161573Srgrimes    switch (ArgI.getKind()) {
71721674Sjkh    case ABIArgInfo::Indirect: {
7181573Srgrimes      llvm::Value* V = AI;
7191573Srgrimes      if (hasAggregateLLVMType(Ty)) {
7201573Srgrimes        // Do nothing, aggregates and complex variables are accessed by
7211573Srgrimes        // reference.
7221573Srgrimes      } else {
7231573Srgrimes        // Load scalar value from indirect argument.
7241573Srgrimes        V = EmitLoadOfScalar(V, false, Ty);
7251573Srgrimes        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
7261573Srgrimes          // This must be a promotion, for something like
7271573Srgrimes          // "void a(x) short x; {..."
7281573Srgrimes          V = EmitScalarConversion(V, Ty, Arg->getType());
72988057Sphantom        }
7301573Srgrimes      }
7311573Srgrimes      EmitParmDecl(*Arg, V);
7321573Srgrimes      break;
7331573Srgrimes    }
7341573Srgrimes
7351573Srgrimes    case ABIArgInfo::Extend:
7361573Srgrimes    case ABIArgInfo::Direct: {
7371573Srgrimes      assert(AI != Fn->arg_end() && "Argument mismatch!");
7381573Srgrimes      llvm::Value* V = AI;
7391573Srgrimes      if (hasAggregateLLVMType(Ty)) {
7401573Srgrimes        // Create a temporary alloca to hold the argument; the rest of
7411573Srgrimes        // codegen expects to access aggregates & complex values by
7421573Srgrimes        // reference.
74321674Sjkh        V = CreateMemTemp(Ty);
74421674Sjkh        Builder.CreateStore(AI, V);
745103399Stjr      } else {
746103399Stjr        if (Arg->getType().isRestrictQualified())
747103399Stjr          AI->addAttr(llvm::Attribute::NoAlias);
748103399Stjr
74921674Sjkh        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
75021674Sjkh          // This must be a promotion, for something like
751103399Stjr          // "void a(x) short x; {..."
7521573Srgrimes          V = EmitScalarConversion(V, Ty, Arg->getType());
7531573Srgrimes        }
7541573Srgrimes      }
7551573Srgrimes      EmitParmDecl(*Arg, V);
7561573Srgrimes      break;
7571573Srgrimes    }
7581573Srgrimes
7591573Srgrimes    case ABIArgInfo::Expand: {
76087113Sfenner      // If this structure was expanded into multiple arguments then
76187113Sfenner      // we need to create a temporary and reconstruct it from the
76287113Sfenner      // arguments.
76387113Sfenner      llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
76487113Sfenner      // FIXME: What are the right qualifiers here?
7651573Srgrimes      llvm::Function::arg_iterator End =
76687113Sfenner        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
76787113Sfenner      EmitParmDecl(*Arg, Temp);
76887113Sfenner
7691573Srgrimes      // Name the arguments used in expansion and increment AI.
77087113Sfenner      unsigned Index = 0;
77187113Sfenner      for (; AI != End; ++AI, ++Index)
77287113Sfenner        AI->setName(Arg->getName() + "." + llvm::Twine(Index));
77387113Sfenner      continue;
77444674Sdfr    }
7751573Srgrimes
7761573Srgrimes    case ABIArgInfo::Ignore:
77787113Sfenner      // Initialize the local variable appropriately.
7781573Srgrimes      if (hasAggregateLLVMType(Ty)) {
77987113Sfenner        EmitParmDecl(*Arg, CreateMemTemp(Ty));
78087113Sfenner      } else {
78187113Sfenner        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
78287113Sfenner      }
78387113Sfenner
78487113Sfenner      // Skip increment, no matching LLVM parameter.
785105204Stjr      continue;
786105204Stjr
787105204Stjr    case ABIArgInfo::Coerce: {
7881573Srgrimes      assert(AI != Fn->arg_end() && "Argument mismatch!");
789103633Stjr      // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
790103633Stjr      // result in a new alloca anyway, so we could just store into that
791103633Stjr      // directly if we broke the abstraction down more.
792103633Stjr      llvm::Value *V = CreateMemTemp(Ty, "coerce");
793103633Stjr      CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
794103633Stjr      // Match to what EmitParmDecl is expecting for this type.
795103633Stjr      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
796105234Stjr        V = EmitLoadOfScalar(V, false, Ty);
797105234Stjr        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
798103633Stjr          // This must be a promotion, for something like
799105234Stjr          // "void a(x) short x; {..."
800103633Stjr          V = EmitScalarConversion(V, Ty, Arg->getType());
801103633Stjr        }
802103633Stjr      }
803103633Stjr      EmitParmDecl(*Arg, V);
804103633Stjr      break;
8051573Srgrimes    }
8061573Srgrimes    }
8071573Srgrimes
8081573Srgrimes    ++AI;
8091573Srgrimes  }
8101573Srgrimes  assert(AI == Fn->arg_end() && "Argument mismatch!");
8111573Srgrimes}
81287113Sfenner
81387113Sfennervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
81487113Sfenner                                         llvm::Value *ReturnValue) {
81587113Sfenner  llvm::Value *RV = 0;
8161573Srgrimes
8171573Srgrimes  // Functions with no result always return void.
8181573Srgrimes  if (ReturnValue) {
8191573Srgrimes    QualType RetTy = FI.getReturnType();
8201573Srgrimes    const ABIArgInfo &RetAI = FI.getReturnInfo();
8211573Srgrimes
8221573Srgrimes    switch (RetAI.getKind()) {
8231573Srgrimes    case ABIArgInfo::Indirect:
8241573Srgrimes      if (RetTy->isAnyComplexType()) {
8251573Srgrimes        ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
8261573Srgrimes        StoreComplexToAddr(RT, CurFn->arg_begin(), false);
8271573Srgrimes      } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
82887113Sfenner        // Do nothing; aggregrates get evaluated directly into the destination.
82987113Sfenner      } else {
83087113Sfenner        EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
831113146Sdas                          false, RetTy);
832113146Sdas      }
833113146Sdas      break;
834113146Sdas
835113146Sdas    case ABIArgInfo::Extend:
836113146Sdas    case ABIArgInfo::Direct:
837113146Sdas      // The internal return value temp always will have
838113146Sdas      // pointer-to-return-type type.
839113146Sdas      RV = Builder.CreateLoad(ReturnValue);
840113146Sdas      break;
841113146Sdas
842113146Sdas    case ABIArgInfo::Ignore:
843113146Sdas      break;
844113146Sdas
845113146Sdas    case ABIArgInfo::Coerce:
846113146Sdas      RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
847113146Sdas      break;
848113146Sdas
849113146Sdas    case ABIArgInfo::Expand:
850113146Sdas      assert(0 && "Invalid ABI kind for return argument");
851113146Sdas    }
852113146Sdas  }
853113146Sdas
854113146Sdas  if (RV) {
855113146Sdas    Builder.CreateRet(RV);
85687113Sfenner  } else {
8577033Sbde    Builder.CreateRetVoid();
8581573Srgrimes  }
859113146Sdas}
860113146Sdas
861113146SdasRValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
862113146Sdas  if (ArgType->isReferenceType())
863113146Sdas    return EmitReferenceBindingToExpr(E);
864113146Sdas
8657033Sbde  return EmitAnyExprToTemp(E);
86687113Sfenner}
867113146Sdas
8687033SbdeRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
8691573Srgrimes                                 llvm::Value *Callee,
8701573Srgrimes                                 ReturnValueSlot ReturnValue,
871113146Sdas                                 const CallArgList &CallArgs,
8727033Sbde                                 const Decl *TargetDecl,
8737033Sbde                                 llvm::Instruction **callOrInvoke) {
874113146Sdas  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
875113146Sdas  llvm::SmallVector<llvm::Value*, 16> Args;
8761573Srgrimes
877113146Sdas  // Handle struct-return functions by passing a pointer to the
878113146Sdas  // location that we would like to return into.
879113146Sdas  QualType RetTy = CallInfo.getReturnType();
880113146Sdas  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
881113146Sdas
882113146Sdas
883113146Sdas  // If the call returns a temporary with struct return, create a temporary
884113146Sdas  // alloca to hold the result, unless one is given to us.
885113146Sdas  if (CGM.ReturnTypeUsesSret(CallInfo)) {
886113146Sdas    llvm::Value *Value = ReturnValue.getValue();
887113146Sdas    if (!Value)
888113146Sdas      Value = CreateMemTemp(RetTy);
889113146Sdas    Args.push_back(Value);
890113146Sdas  }
8911573Srgrimes
892113146Sdas  assert(CallInfo.arg_size() == CallArgs.size() &&
893113146Sdas         "Mismatch between function signature & arguments.");
894113146Sdas  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
895113146Sdas  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
896113146Sdas       I != E; ++I, ++info_it) {
897113146Sdas    const ABIArgInfo &ArgInfo = info_it->info;
898113146Sdas    RValue RV = I->first;
899113146Sdas
9001573Srgrimes    switch (ArgInfo.getKind()) {
9011573Srgrimes    case ABIArgInfo::Indirect:
9021573Srgrimes      if (RV.isScalar() || RV.isComplex()) {
9031573Srgrimes        // Make a temporary alloca to pass the argument.
904113146Sdas        Args.push_back(CreateMemTemp(I->second));
9051573Srgrimes        if (RV.isScalar())
906113146Sdas          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
907113146Sdas        else
908113146Sdas          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
909113146Sdas      } else {
910113146Sdas        Args.push_back(RV.getAggregateAddr());
911113146Sdas      }
912113146Sdas      break;
913113146Sdas
914113146Sdas    case ABIArgInfo::Extend:
915113146Sdas    case ABIArgInfo::Direct:
9168870Srgrimes      if (RV.isScalar()) {
917113146Sdas        Args.push_back(RV.getScalarVal());
918113146Sdas      } else if (RV.isComplex()) {
919113146Sdas        llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
920113146Sdas        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
9211573Srgrimes        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
922113146Sdas        Args.push_back(Tmp);
9231573Srgrimes      } else {
9241573Srgrimes        Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
9251573Srgrimes      }
9261573Srgrimes      break;
9271573Srgrimes
9281573Srgrimes    case ABIArgInfo::Ignore:
929113146Sdas      break;
930113146Sdas
931113146Sdas    case ABIArgInfo::Coerce: {
932113146Sdas      // FIXME: Avoid the conversion through memory if possible.
933113146Sdas      llvm::Value *SrcPtr;
934113146Sdas      if (RV.isScalar()) {
935113146Sdas        SrcPtr = CreateMemTemp(I->second, "coerce");
936113146Sdas        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
937113146Sdas      } else if (RV.isComplex()) {
938113146Sdas        SrcPtr = CreateMemTemp(I->second, "coerce");
939113146Sdas        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
940113146Sdas      } else
941113146Sdas        SrcPtr = RV.getAggregateAddr();
942113146Sdas      Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
943113146Sdas                                       *this));
944113146Sdas      break;
945113146Sdas    }
946113146Sdas
9471573Srgrimes    case ABIArgInfo::Expand:
9481573Srgrimes      ExpandTypeToArgs(I->second, RV, Args);
9491573Srgrimes      break;
95087113Sfenner    }
95187113Sfenner  }
95287113Sfenner
95387113Sfenner  // If the callee is a bitcast of a function to a varargs pointer to function
95487113Sfenner  // type, check to see if we can remove the bitcast.  This handles some cases
95587113Sfenner  // with unprototyped functions.
95687113Sfenner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
95787113Sfenner    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
95887113Sfenner      const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
95987113Sfenner      const llvm::FunctionType *CurFT =
96087113Sfenner        cast<llvm::FunctionType>(CurPT->getElementType());
96187113Sfenner      const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
96287113Sfenner
9631573Srgrimes      if (CE->getOpcode() == llvm::Instruction::BitCast &&
96431980Sache          ActualFT->getReturnType() == CurFT->getReturnType() &&
9651573Srgrimes          ActualFT->getNumParams() == CurFT->getNumParams() &&
96631980Sache          ActualFT->getNumParams() == Args.size()) {
96787113Sfenner        bool ArgsMatch = true;
96887113Sfenner        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
9691573Srgrimes          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
97031980Sache            ArgsMatch = false;
9711573Srgrimes            break;
9721573Srgrimes          }
9731573Srgrimes
9741573Srgrimes        // Strip the cast if we can get away with it.  This is a nice cleanup,
9751573Srgrimes        // but also allows us to inline the function at -O0 if it is marked
97687113Sfenner        // always_inline.
97787113Sfenner        if (ArgsMatch)
9781573Srgrimes          Callee = CalleeF;
9791573Srgrimes      }
9801573Srgrimes    }
9811573Srgrimes
9821573Srgrimes
98388057Sphantom  llvm::BasicBlock *InvokeDest = getInvokeDest();
9841573Srgrimes  unsigned CallingConv;
9851573Srgrimes  CodeGen::AttributeListType AttributeList;
9861573Srgrimes  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
9871573Srgrimes  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
9881573Srgrimes                                                   AttributeList.end());
9891573Srgrimes
99087113Sfenner  llvm::CallSite CS;
9911573Srgrimes  if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
992113146Sdas    CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
993113146Sdas  } else {
994113146Sdas    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
9951573Srgrimes    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
996105204Stjr                              Args.data(), Args.data()+Args.size());
997105204Stjr    EmitBlock(Cont);
998105204Stjr  }
9991573Srgrimes  if (callOrInvoke) {
1000103633Stjr    *callOrInvoke = CS.getInstruction();
1001103633Stjr  }
1002103633Stjr
1003103633Stjr  CS.setAttributes(Attrs);
1004103633Stjr  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1005103633Stjr
1006103633Stjr  // If the call doesn't return, finish the basic block and clear the
1007103633Stjr  // insertion point; this allows the rest of IRgen to discard
1008103633Stjr  // unreachable code.
1009105234Stjr  if (CS.doesNotReturn()) {
1010105234Stjr    Builder.CreateUnreachable();
1011103633Stjr    Builder.ClearInsertionPoint();
1012105234Stjr
1013103633Stjr    // FIXME: For now, emit a dummy basic block because expr emitters in
1014103633Stjr    // generally are not ready to handle emitting expressions at unreachable
1015103633Stjr    // points.
10161573Srgrimes    EnsureInsertPoint();
10171573Srgrimes
10181573Srgrimes    // Return a reasonable RValue.
10191573Srgrimes    return GetUndefRValue(RetTy);
10201573Srgrimes  }
10211573Srgrimes
10221573Srgrimes  llvm::Instruction *CI = CS.getInstruction();
102316586Sjraynard  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
10241573Srgrimes    CI->setName("call");
10251573Srgrimes
10261573Srgrimes  switch (RetAI.getKind()) {
10271573Srgrimes  case ABIArgInfo::Indirect:
10281573Srgrimes    if (RetTy->isAnyComplexType())
10291573Srgrimes      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
10301573Srgrimes    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
10311573Srgrimes      return RValue::getAggregate(Args[0]);
10321573Srgrimes    return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
10331573Srgrimes
10341573Srgrimes  case ABIArgInfo::Extend:
10351573Srgrimes  case ABIArgInfo::Direct:
10361573Srgrimes    if (RetTy->isAnyComplexType()) {
10371573Srgrimes      llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
10381573Srgrimes      llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
103987113Sfenner      return RValue::getComplex(std::make_pair(Real, Imag));
104087113Sfenner    }
10411573Srgrimes    if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
10421573Srgrimes      llvm::Value *DestPtr = ReturnValue.getValue();
10431573Srgrimes      bool DestIsVolatile = ReturnValue.isVolatile();
10441573Srgrimes
10451573Srgrimes      if (!DestPtr) {
1046113146Sdas        DestPtr = CreateMemTemp(RetTy, "agg.tmp");
10471573Srgrimes        DestIsVolatile = false;
10481573Srgrimes      }
1049113146Sdas      Builder.CreateStore(CI, DestPtr, DestIsVolatile);
105087113Sfenner      return RValue::getAggregate(DestPtr);
105187113Sfenner    }
105287113Sfenner    return RValue::get(CI);
10531573Srgrimes
10541573Srgrimes  case ABIArgInfo::Ignore:
10551573Srgrimes    // If we are ignoring an argument that had a result, make sure to
10561573Srgrimes    // construct the appropriate return value for our caller.
10571573Srgrimes    return GetUndefRValue(RetTy);
105887113Sfenner
1059113146Sdas  case ABIArgInfo::Coerce: {
10601573Srgrimes    llvm::Value *DestPtr = ReturnValue.getValue();
106187815Sphantom    bool DestIsVolatile = ReturnValue.isVolatile();
10621573Srgrimes
10631573Srgrimes    if (!DestPtr) {
106488057Sphantom      DestPtr = CreateMemTemp(RetTy, "coerce");
10651573Srgrimes      DestIsVolatile = false;
10661573Srgrimes    }
10671573Srgrimes
10681573Srgrimes    CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
10691573Srgrimes    if (RetTy->isAnyComplexType())
10701573Srgrimes      return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
10711573Srgrimes    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
107288057Sphantom      return RValue::getAggregate(DestPtr);
10731573Srgrimes    return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
10741573Srgrimes  }
10751573Srgrimes
10761573Srgrimes  case ABIArgInfo::Expand:
10771573Srgrimes    assert(0 && "Invalid ABI kind for return argument");
107887113Sfenner  }
107987113Sfenner
108087113Sfenner  assert(0 && "Unhandled ABIArgInfo::Kind");
108187815Sphantom  return RValue::get(0);
108287815Sphantom}
108387815Sphantom
10841573Srgrimes/* VarArg handling */
10851573Srgrimes
10861573Srgrimesllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
108787815Sphantom  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
108887815Sphantom}
108987815Sphantom