1263509Sdim//===--- CGCall.cpp - Encapsulate calling convention details --------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// These classes wrap the information about a call or function 11193326Sed// definition used to handle ABI compliancy. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15193326Sed#include "CGCall.h" 16252723Sdim#include "ABIInfo.h" 17212904Sdim#include "CGCXXABI.h" 18193326Sed#include "CodeGenFunction.h" 19193326Sed#include "CodeGenModule.h" 20235633Sdim#include "TargetInfo.h" 21193326Sed#include "clang/AST/Decl.h" 22193326Sed#include "clang/AST/DeclCXX.h" 23193326Sed#include "clang/AST/DeclObjC.h" 24252723Sdim#include "clang/Basic/TargetInfo.h" 25263509Sdim#include "clang/CodeGen/CGFunctionInfo.h" 26210299Sed#include "clang/Frontend/CodeGenOptions.h" 27252723Sdim#include "llvm/ADT/StringExtras.h" 28252723Sdim#include "llvm/IR/Attributes.h" 29252723Sdim#include "llvm/IR/DataLayout.h" 30252723Sdim#include "llvm/IR/InlineAsm.h" 31252723Sdim#include "llvm/MC/SubtargetFeature.h" 32193326Sed#include "llvm/Support/CallSite.h" 33224145Sdim#include "llvm/Transforms/Utils/Local.h" 34193326Sedusing namespace clang; 35193326Sedusing namespace CodeGen; 36193326Sed 37193326Sed/***/ 38193326Sed 39203955Srdivackystatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { 40203955Srdivacky switch (CC) { 41203955Srdivacky default: return llvm::CallingConv::C; 42203955Srdivacky case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; 43203955Srdivacky case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; 44208600Srdivacky case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; 45256382Sdim case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64; 46256382Sdim case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV; 47221345Sdim case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; 48221345Sdim case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 49252723Sdim case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI; 50212904Sdim // TODO: add support for CC_X86Pascal to llvm 51203955Srdivacky } 52203955Srdivacky} 53203955Srdivacky 54204643Srdivacky/// Derives the 'this' type for codegen purposes, i.e. ignoring method 55204643Srdivacky/// qualification. 56204643Srdivacky/// FIXME: address space qualification? 57204643Srdivackystatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { 58204643Srdivacky QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); 59204643Srdivacky return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); 60193326Sed} 61193326Sed 62204643Srdivacky/// Returns the canonical formal type of the given C++ method. 63204643Srdivackystatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { 64204643Srdivacky return MD->getType()->getCanonicalTypeUnqualified() 65204643Srdivacky .getAs<FunctionProtoType>(); 66204643Srdivacky} 67204643Srdivacky 68204643Srdivacky/// Returns the "extra-canonicalized" return type, which discards 69204643Srdivacky/// qualifiers on the return type. Codegen doesn't care about them, 70204643Srdivacky/// and it makes ABI code a little easier to be able to assume that 71204643Srdivacky/// all parameter and return types are top-level unqualified. 72204643Srdivackystatic CanQualType GetReturnType(QualType RetTy) { 73204643Srdivacky return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); 74204643Srdivacky} 75204643Srdivacky 76245431Sdim/// Arrange the argument and result information for a value of the given 77245431Sdim/// unprototyped freestanding function type. 78204643Srdivackyconst CGFunctionInfo & 79245431SdimCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { 80235633Sdim // When translating an unprototyped function type, always use a 81235633Sdim // variadic type. 82245431Sdim return arrangeLLVMFunctionInfo(FTNP->getResultType().getUnqualifiedType(), 83252723Sdim None, FTNP->getExtInfo(), RequiredArgs(0)); 84204643Srdivacky} 85204643Srdivacky 86245431Sdim/// Arrange the LLVM function layout for a value of the given function 87245431Sdim/// type, on top of any implicit parameters already stored. Use the 88245431Sdim/// given ExtInfo instead of the ExtInfo from the function type. 89245431Sdimstatic const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT, 90245431Sdim SmallVectorImpl<CanQualType> &prefix, 91245431Sdim CanQual<FunctionProtoType> FTP, 92245431Sdim FunctionType::ExtInfo extInfo) { 93245431Sdim RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); 94193326Sed // FIXME: Kill copy. 95193326Sed for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 96245431Sdim prefix.push_back(FTP->getArgType(i)); 97235633Sdim CanQualType resultType = FTP->getResultType().getUnqualifiedType(); 98245431Sdim return CGT.arrangeLLVMFunctionInfo(resultType, prefix, extInfo, required); 99193326Sed} 100193326Sed 101245431Sdim/// Arrange the argument and result information for a free function (i.e. 102245431Sdim/// not a C++ or ObjC instance method) of the given type. 103245431Sdimstatic const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT, 104245431Sdim SmallVectorImpl<CanQualType> &prefix, 105245431Sdim CanQual<FunctionProtoType> FTP) { 106245431Sdim return arrangeLLVMFunctionInfo(CGT, prefix, FTP, FTP->getExtInfo()); 107245431Sdim} 108245431Sdim 109245431Sdim/// Arrange the argument and result information for a free function (i.e. 110245431Sdim/// not a C++ or ObjC instance method) of the given type. 111245431Sdimstatic const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT, 112245431Sdim SmallVectorImpl<CanQualType> &prefix, 113245431Sdim CanQual<FunctionProtoType> FTP) { 114245431Sdim FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 115245431Sdim return arrangeLLVMFunctionInfo(CGT, prefix, FTP, extInfo); 116245431Sdim} 117245431Sdim 118235633Sdim/// Arrange the argument and result information for a value of the 119245431Sdim/// given freestanding function type. 120204643Srdivackyconst CGFunctionInfo & 121245431SdimCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { 122235633Sdim SmallVector<CanQualType, 16> argTypes; 123245431Sdim return ::arrangeFreeFunctionType(*this, argTypes, FTP); 124204643Srdivacky} 125204643Srdivacky 126203955Srdivackystatic CallingConv getCallingConventionForDecl(const Decl *D) { 127198092Srdivacky // Set the appropriate calling convention for the Function. 128198092Srdivacky if (D->hasAttr<StdCallAttr>()) 129203955Srdivacky return CC_X86StdCall; 130198092Srdivacky 131198092Srdivacky if (D->hasAttr<FastCallAttr>()) 132203955Srdivacky return CC_X86FastCall; 133198092Srdivacky 134208600Srdivacky if (D->hasAttr<ThisCallAttr>()) 135208600Srdivacky return CC_X86ThisCall; 136208600Srdivacky 137212904Sdim if (D->hasAttr<PascalAttr>()) 138212904Sdim return CC_X86Pascal; 139212904Sdim 140221345Sdim if (PcsAttr *PCS = D->getAttr<PcsAttr>()) 141221345Sdim return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); 142221345Sdim 143245431Sdim if (D->hasAttr<PnaclCallAttr>()) 144245431Sdim return CC_PnaclCall; 145245431Sdim 146252723Sdim if (D->hasAttr<IntelOclBiccAttr>()) 147252723Sdim return CC_IntelOclBicc; 148252723Sdim 149203955Srdivacky return CC_C; 150198092Srdivacky} 151198092Srdivacky 152235633Sdim/// Arrange the argument and result information for a call to an 153235633Sdim/// unknown C++ non-static member function of the given abstract type. 154263509Sdim/// (Zero value of RD means we don't have any meaningful "this" argument type, 155263509Sdim/// so fall back to a generic pointer type). 156235633Sdim/// The member function must be an ordinary function, i.e. not a 157235633Sdim/// constructor or destructor. 158235633Sdimconst CGFunctionInfo & 159235633SdimCodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, 160235633Sdim const FunctionProtoType *FTP) { 161235633Sdim SmallVector<CanQualType, 16> argTypes; 162204643Srdivacky 163198092Srdivacky // Add the 'this' pointer. 164263509Sdim if (RD) 165263509Sdim argTypes.push_back(GetThisType(Context, RD)); 166263509Sdim else 167263509Sdim argTypes.push_back(Context.VoidPtrTy); 168204643Srdivacky 169245431Sdim return ::arrangeCXXMethodType(*this, argTypes, 170204643Srdivacky FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); 171198092Srdivacky} 172198092Srdivacky 173235633Sdim/// Arrange the argument and result information for a declaration or 174235633Sdim/// definition of the given C++ non-static member function. The 175235633Sdim/// member function must be an ordinary function, i.e. not a 176235633Sdim/// constructor or destructor. 177235633Sdimconst CGFunctionInfo & 178235633SdimCodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { 179263509Sdim assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!"); 180212904Sdim assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); 181212904Sdim 182235633Sdim CanQual<FunctionProtoType> prototype = GetFormalType(MD); 183198092Srdivacky 184235633Sdim if (MD->isInstance()) { 185235633Sdim // The abstract case is perfectly fine. 186263509Sdim const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD); 187263509Sdim return arrangeCXXMethodType(ThisType, prototype.getTypePtr()); 188235633Sdim } 189235633Sdim 190245431Sdim return arrangeFreeFunctionType(prototype); 191193326Sed} 192193326Sed 193235633Sdim/// Arrange the argument and result information for a declaration 194235633Sdim/// or definition to the given constructor variant. 195235633Sdimconst CGFunctionInfo & 196235633SdimCodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D, 197235633Sdim CXXCtorType ctorKind) { 198235633Sdim SmallVector<CanQualType, 16> argTypes; 199235633Sdim argTypes.push_back(GetThisType(Context, D->getParent())); 200199990Srdivacky 201263509Sdim GlobalDecl GD(D, ctorKind); 202263509Sdim CanQualType resultType = 203263509Sdim TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy; 204263509Sdim 205235633Sdim TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes); 206204643Srdivacky 207212904Sdim CanQual<FunctionProtoType> FTP = GetFormalType(D); 208212904Sdim 209235633Sdim RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size()); 210235633Sdim 211212904Sdim // Add the formal parameters. 212212904Sdim for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 213235633Sdim argTypes.push_back(FTP->getArgType(i)); 214212904Sdim 215245431Sdim FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 216245431Sdim return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, required); 217199990Srdivacky} 218199990Srdivacky 219235633Sdim/// Arrange the argument and result information for a declaration, 220235633Sdim/// definition, or call to the given destructor variant. It so 221235633Sdim/// happens that all three cases produce the same information. 222235633Sdimconst CGFunctionInfo & 223235633SdimCodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D, 224235633Sdim CXXDtorType dtorKind) { 225235633Sdim SmallVector<CanQualType, 2> argTypes; 226235633Sdim argTypes.push_back(GetThisType(Context, D->getParent())); 227204643Srdivacky 228263509Sdim GlobalDecl GD(D, dtorKind); 229263509Sdim CanQualType resultType = 230263509Sdim TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy; 231263509Sdim 232235633Sdim TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes); 233212904Sdim 234212904Sdim CanQual<FunctionProtoType> FTP = GetFormalType(D); 235212904Sdim assert(FTP->getNumArgs() == 0 && "dtor with formal parameters"); 236245431Sdim assert(FTP->isVariadic() == 0 && "dtor with formal parameters"); 237212904Sdim 238245431Sdim FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 239245431Sdim return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, 240245431Sdim RequiredArgs::All); 241199990Srdivacky} 242199990Srdivacky 243235633Sdim/// Arrange the argument and result information for the declaration or 244235633Sdim/// definition of the given function. 245235633Sdimconst CGFunctionInfo & 246235633SdimCodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { 247193326Sed if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 248193326Sed if (MD->isInstance()) 249235633Sdim return arrangeCXXMethodDeclaration(MD); 250198092Srdivacky 251204643Srdivacky CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); 252235633Sdim 253204643Srdivacky assert(isa<FunctionType>(FTy)); 254235633Sdim 255235633Sdim // When declaring a function without a prototype, always use a 256235633Sdim // non-variadic type. 257235633Sdim if (isa<FunctionNoProtoType>(FTy)) { 258235633Sdim CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>(); 259252723Sdim return arrangeLLVMFunctionInfo(noProto->getResultType(), None, 260252723Sdim noProto->getExtInfo(), RequiredArgs::All); 261235633Sdim } 262235633Sdim 263204643Srdivacky assert(isa<FunctionProtoType>(FTy)); 264245431Sdim return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>()); 265193326Sed} 266193326Sed 267235633Sdim/// Arrange the argument and result information for the declaration or 268235633Sdim/// definition of an Objective-C method. 269235633Sdimconst CGFunctionInfo & 270235633SdimCodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { 271235633Sdim // It happens that this is the same as a call with no optional 272235633Sdim // arguments, except also using the formal 'self' type. 273235633Sdim return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); 274235633Sdim} 275235633Sdim 276235633Sdim/// Arrange the argument and result information for the function type 277235633Sdim/// through which to perform a send to the given Objective-C method, 278235633Sdim/// using the given receiver type. The receiver type is not always 279235633Sdim/// the 'self' type of the method or even an Objective-C pointer type. 280235633Sdim/// This is *not* the right method for actually performing such a 281235633Sdim/// message send, due to the possibility of optional arguments. 282235633Sdimconst CGFunctionInfo & 283235633SdimCodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 284235633Sdim QualType receiverType) { 285235633Sdim SmallVector<CanQualType, 16> argTys; 286235633Sdim argTys.push_back(Context.getCanonicalParamType(receiverType)); 287235633Sdim argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); 288193326Sed // FIXME: Kill copy? 289226890Sdim for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), 290204643Srdivacky e = MD->param_end(); i != e; ++i) { 291235633Sdim argTys.push_back(Context.getCanonicalParamType((*i)->getType())); 292204643Srdivacky } 293224145Sdim 294224145Sdim FunctionType::ExtInfo einfo; 295224145Sdim einfo = einfo.withCallingConv(getCallingConventionForDecl(MD)); 296224145Sdim 297235633Sdim if (getContext().getLangOpts().ObjCAutoRefCount && 298224145Sdim MD->hasAttr<NSReturnsRetainedAttr>()) 299224145Sdim einfo = einfo.withProducesResult(true); 300224145Sdim 301235633Sdim RequiredArgs required = 302235633Sdim (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); 303235633Sdim 304245431Sdim return arrangeLLVMFunctionInfo(GetReturnType(MD->getResultType()), argTys, 305245431Sdim einfo, required); 306193326Sed} 307193326Sed 308235633Sdimconst CGFunctionInfo & 309235633SdimCodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { 310203955Srdivacky // FIXME: Do we need to handle ObjCMethodDecl? 311203955Srdivacky const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 312218893Sdim 313203955Srdivacky if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 314235633Sdim return arrangeCXXConstructorDeclaration(CD, GD.getCtorType()); 315203955Srdivacky 316203955Srdivacky if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) 317235633Sdim return arrangeCXXDestructor(DD, GD.getDtorType()); 318218893Sdim 319235633Sdim return arrangeFunctionDeclaration(FD); 320203955Srdivacky} 321203955Srdivacky 322252723Sdim/// Arrange a call as unto a free function, except possibly with an 323252723Sdim/// additional number of formal parameters considered required. 324252723Sdimstatic const CGFunctionInfo & 325252723SdimarrangeFreeFunctionLikeCall(CodeGenTypes &CGT, 326263509Sdim CodeGenModule &CGM, 327252723Sdim const CallArgList &args, 328252723Sdim const FunctionType *fnType, 329252723Sdim unsigned numExtraRequiredArgs) { 330252723Sdim assert(args.size() >= numExtraRequiredArgs); 331252723Sdim 332252723Sdim // In most cases, there are no optional arguments. 333252723Sdim RequiredArgs required = RequiredArgs::All; 334252723Sdim 335252723Sdim // If we have a variadic prototype, the required arguments are the 336252723Sdim // extra prefix plus the arguments in the prototype. 337252723Sdim if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { 338252723Sdim if (proto->isVariadic()) 339252723Sdim required = RequiredArgs(proto->getNumArgs() + numExtraRequiredArgs); 340252723Sdim 341252723Sdim // If we don't have a prototype at all, but we're supposed to 342252723Sdim // explicitly use the variadic convention for unprototyped calls, 343252723Sdim // treat all of the arguments as required but preserve the nominal 344252723Sdim // possibility of variadics. 345263509Sdim } else if (CGM.getTargetCodeGenInfo() 346263509Sdim .isNoProtoCallVariadic(args, 347263509Sdim cast<FunctionNoProtoType>(fnType))) { 348252723Sdim required = RequiredArgs(args.size()); 349252723Sdim } 350252723Sdim 351252723Sdim return CGT.arrangeFreeFunctionCall(fnType->getResultType(), args, 352252723Sdim fnType->getExtInfo(), required); 353252723Sdim} 354252723Sdim 355235633Sdim/// Figure out the rules for calling a function with the given formal 356235633Sdim/// type using the given arguments. The arguments are necessary 357235633Sdim/// because the function might be unprototyped, in which case it's 358235633Sdim/// target-dependent in crazy ways. 359235633Sdimconst CGFunctionInfo & 360245431SdimCodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, 361245431Sdim const FunctionType *fnType) { 362263509Sdim return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0); 363252723Sdim} 364235633Sdim 365252723Sdim/// A block function call is essentially a free-function call with an 366252723Sdim/// extra implicit argument. 367252723Sdimconst CGFunctionInfo & 368252723SdimCodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args, 369252723Sdim const FunctionType *fnType) { 370263509Sdim return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1); 371235633Sdim} 372235633Sdim 373235633Sdimconst CGFunctionInfo & 374245431SdimCodeGenTypes::arrangeFreeFunctionCall(QualType resultType, 375245431Sdim const CallArgList &args, 376245431Sdim FunctionType::ExtInfo info, 377245431Sdim RequiredArgs required) { 378193326Sed // FIXME: Kill copy. 379235633Sdim SmallVector<CanQualType, 16> argTypes; 380235633Sdim for (CallArgList::const_iterator i = args.begin(), e = args.end(); 381193326Sed i != e; ++i) 382235633Sdim argTypes.push_back(Context.getCanonicalParamType(i->Ty)); 383245431Sdim return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info, 384245431Sdim required); 385193326Sed} 386193326Sed 387245431Sdim/// Arrange a call to a C++ method, passing the given arguments. 388235633Sdimconst CGFunctionInfo & 389245431SdimCodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, 390245431Sdim const FunctionProtoType *FPT, 391245431Sdim RequiredArgs required) { 392245431Sdim // FIXME: Kill copy. 393245431Sdim SmallVector<CanQualType, 16> argTypes; 394245431Sdim for (CallArgList::const_iterator i = args.begin(), e = args.end(); 395245431Sdim i != e; ++i) 396245431Sdim argTypes.push_back(Context.getCanonicalParamType(i->Ty)); 397245431Sdim 398245431Sdim FunctionType::ExtInfo info = FPT->getExtInfo(); 399245431Sdim return arrangeLLVMFunctionInfo(GetReturnType(FPT->getResultType()), 400245431Sdim argTypes, info, required); 401245431Sdim} 402245431Sdim 403245431Sdimconst CGFunctionInfo & 404235633SdimCodeGenTypes::arrangeFunctionDeclaration(QualType resultType, 405235633Sdim const FunctionArgList &args, 406235633Sdim const FunctionType::ExtInfo &info, 407235633Sdim bool isVariadic) { 408193326Sed // FIXME: Kill copy. 409235633Sdim SmallVector<CanQualType, 16> argTypes; 410235633Sdim for (FunctionArgList::const_iterator i = args.begin(), e = args.end(); 411193326Sed i != e; ++i) 412235633Sdim argTypes.push_back(Context.getCanonicalParamType((*i)->getType())); 413235633Sdim 414235633Sdim RequiredArgs required = 415235633Sdim (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All); 416245431Sdim return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info, 417245431Sdim required); 418193326Sed} 419193326Sed 420235633Sdimconst CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { 421252723Sdim return arrangeLLVMFunctionInfo(getContext().VoidTy, None, 422245431Sdim FunctionType::ExtInfo(), RequiredArgs::All); 423221345Sdim} 424221345Sdim 425235633Sdim/// Arrange the argument and result information for an abstract value 426235633Sdim/// of a given function type. This is the method which all of the 427235633Sdim/// above functions ultimately defer to. 428235633Sdimconst CGFunctionInfo & 429245431SdimCodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, 430245431Sdim ArrayRef<CanQualType> argTypes, 431245431Sdim FunctionType::ExtInfo info, 432245431Sdim RequiredArgs required) { 433204643Srdivacky#ifndef NDEBUG 434235633Sdim for (ArrayRef<CanQualType>::const_iterator 435235633Sdim I = argTypes.begin(), E = argTypes.end(); I != E; ++I) 436204643Srdivacky assert(I->isCanonicalAsParam()); 437204643Srdivacky#endif 438204643Srdivacky 439235633Sdim unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); 440203955Srdivacky 441193326Sed // Lookup or create unique function info. 442193326Sed llvm::FoldingSetNodeID ID; 443235633Sdim CGFunctionInfo::Profile(ID, info, required, resultType, argTypes); 444193326Sed 445235633Sdim void *insertPos = 0; 446235633Sdim CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); 447193326Sed if (FI) 448193326Sed return *FI; 449193326Sed 450235633Sdim // Construct the function info. We co-allocate the ArgInfos. 451235633Sdim FI = CGFunctionInfo::create(CC, info, resultType, argTypes, required); 452235633Sdim FunctionInfos.InsertNode(FI, insertPos); 453193326Sed 454235633Sdim bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted; 455235633Sdim assert(inserted && "Recursively being processed?"); 456224145Sdim 457212904Sdim // Compute ABI information. 458212904Sdim getABIInfo().computeInfo(*FI); 459218893Sdim 460212904Sdim // Loop over all of the computed argument and return value info. If any of 461212904Sdim // them are direct or extend without a specified coerce type, specify the 462212904Sdim // default now. 463235633Sdim ABIArgInfo &retInfo = FI->getReturnInfo(); 464235633Sdim if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0) 465235633Sdim retInfo.setCoerceToType(ConvertType(FI->getReturnType())); 466218893Sdim 467212904Sdim for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end(); 468212904Sdim I != E; ++I) 469212904Sdim if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0) 470224145Sdim I->info.setCoerceToType(ConvertType(I->type)); 471193326Sed 472235633Sdim bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; 473235633Sdim assert(erased && "Not in set?"); 474224145Sdim 475193326Sed return *FI; 476193326Sed} 477193326Sed 478235633SdimCGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, 479235633Sdim const FunctionType::ExtInfo &info, 480235633Sdim CanQualType resultType, 481235633Sdim ArrayRef<CanQualType> argTypes, 482235633Sdim RequiredArgs required) { 483235633Sdim void *buffer = operator new(sizeof(CGFunctionInfo) + 484235633Sdim sizeof(ArgInfo) * (argTypes.size() + 1)); 485235633Sdim CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); 486235633Sdim FI->CallingConvention = llvmCC; 487235633Sdim FI->EffectiveCallingConvention = llvmCC; 488235633Sdim FI->ASTCallingConvention = info.getCC(); 489235633Sdim FI->NoReturn = info.getNoReturn(); 490235633Sdim FI->ReturnsRetained = info.getProducesResult(); 491235633Sdim FI->Required = required; 492235633Sdim FI->HasRegParm = info.getHasRegParm(); 493235633Sdim FI->RegParm = info.getRegParm(); 494235633Sdim FI->NumArgs = argTypes.size(); 495235633Sdim FI->getArgsBuffer()[0].type = resultType; 496235633Sdim for (unsigned i = 0, e = argTypes.size(); i != e; ++i) 497235633Sdim FI->getArgsBuffer()[i + 1].type = argTypes[i]; 498235633Sdim return FI; 499193326Sed} 500193326Sed 501193326Sed/***/ 502193326Sed 503223017Sdimvoid CodeGenTypes::GetExpandedTypes(QualType type, 504226890Sdim SmallVectorImpl<llvm::Type*> &expandedTypes) { 505226890Sdim if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) { 506226890Sdim uint64_t NumElts = AT->getSize().getZExtValue(); 507226890Sdim for (uint64_t Elt = 0; Elt < NumElts; ++Elt) 508226890Sdim GetExpandedTypes(AT->getElementType(), expandedTypes); 509235633Sdim } else if (const RecordType *RT = type->getAs<RecordType>()) { 510226890Sdim const RecordDecl *RD = RT->getDecl(); 511226890Sdim assert(!RD->hasFlexibleArrayMember() && 512226890Sdim "Cannot expand structure with flexible array."); 513235633Sdim if (RD->isUnion()) { 514235633Sdim // Unions can be here only in degenerative cases - all the fields are same 515235633Sdim // after flattening. Thus we have to use the "largest" field. 516235633Sdim const FieldDecl *LargestFD = 0; 517235633Sdim CharUnits UnionSize = CharUnits::Zero(); 518235633Sdim 519235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 520235633Sdim i != e; ++i) { 521235633Sdim const FieldDecl *FD = *i; 522235633Sdim assert(!FD->isBitField() && 523235633Sdim "Cannot expand structure with bit-field members."); 524235633Sdim CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 525235633Sdim if (UnionSize < FieldSize) { 526235633Sdim UnionSize = FieldSize; 527235633Sdim LargestFD = FD; 528235633Sdim } 529235633Sdim } 530235633Sdim if (LargestFD) 531235633Sdim GetExpandedTypes(LargestFD->getType(), expandedTypes); 532235633Sdim } else { 533235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 534235633Sdim i != e; ++i) { 535245431Sdim assert(!i->isBitField() && 536235633Sdim "Cannot expand structure with bit-field members."); 537245431Sdim GetExpandedTypes(i->getType(), expandedTypes); 538235633Sdim } 539226890Sdim } 540226890Sdim } else if (const ComplexType *CT = type->getAs<ComplexType>()) { 541226890Sdim llvm::Type *EltTy = ConvertType(CT->getElementType()); 542226890Sdim expandedTypes.push_back(EltTy); 543226890Sdim expandedTypes.push_back(EltTy); 544226890Sdim } else 545226890Sdim expandedTypes.push_back(ConvertType(type)); 546193326Sed} 547193326Sed 548198092Srdivackyllvm::Function::arg_iterator 549193326SedCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 550193326Sed llvm::Function::arg_iterator AI) { 551198092Srdivacky assert(LV.isSimple() && 552198092Srdivacky "Unexpected non-simple lvalue during struct expansion."); 553226890Sdim 554226890Sdim if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 555226890Sdim unsigned NumElts = AT->getSize().getZExtValue(); 556226890Sdim QualType EltTy = AT->getElementType(); 557226890Sdim for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 558235633Sdim llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt); 559226890Sdim LValue LV = MakeAddrLValue(EltAddr, EltTy); 560226890Sdim AI = ExpandTypeFromArgs(EltTy, LV, AI); 561226890Sdim } 562235633Sdim } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 563226890Sdim RecordDecl *RD = RT->getDecl(); 564235633Sdim if (RD->isUnion()) { 565235633Sdim // Unions can be here only in degenerative cases - all the fields are same 566235633Sdim // after flattening. Thus we have to use the "largest" field. 567235633Sdim const FieldDecl *LargestFD = 0; 568235633Sdim CharUnits UnionSize = CharUnits::Zero(); 569193326Sed 570235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 571235633Sdim i != e; ++i) { 572235633Sdim const FieldDecl *FD = *i; 573235633Sdim assert(!FD->isBitField() && 574235633Sdim "Cannot expand structure with bit-field members."); 575235633Sdim CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 576235633Sdim if (UnionSize < FieldSize) { 577235633Sdim UnionSize = FieldSize; 578235633Sdim LargestFD = FD; 579235633Sdim } 580235633Sdim } 581235633Sdim if (LargestFD) { 582235633Sdim // FIXME: What are the right qualifiers here? 583235633Sdim LValue SubLV = EmitLValueForField(LV, LargestFD); 584235633Sdim AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI); 585235633Sdim } 586235633Sdim } else { 587235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 588235633Sdim i != e; ++i) { 589235633Sdim FieldDecl *FD = *i; 590235633Sdim QualType FT = FD->getType(); 591235633Sdim 592235633Sdim // FIXME: What are the right qualifiers here? 593235633Sdim LValue SubLV = EmitLValueForField(LV, FD); 594235633Sdim AI = ExpandTypeFromArgs(FT, SubLV, AI); 595235633Sdim } 596193326Sed } 597226890Sdim } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 598226890Sdim QualType EltTy = CT->getElementType(); 599235633Sdim llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real"); 600226890Sdim EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy)); 601235633Sdim llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag"); 602226890Sdim EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy)); 603226890Sdim } else { 604226890Sdim EmitStoreThroughLValue(RValue::get(AI), LV); 605226890Sdim ++AI; 606193326Sed } 607193326Sed 608193326Sed return AI; 609193326Sed} 610193326Sed 611210299Sed/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are 612210299Sed/// accessing some number of bytes out of it, try to gep into the struct to get 613210299Sed/// at its inner goodness. Dive as deep as possible without entering an element 614210299Sed/// with an in-memory size smaller than DstSize. 615210299Sedstatic llvm::Value * 616210299SedEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, 617226890Sdim llvm::StructType *SrcSTy, 618210299Sed uint64_t DstSize, CodeGenFunction &CGF) { 619210299Sed // We can't dive into a zero-element struct. 620210299Sed if (SrcSTy->getNumElements() == 0) return SrcPtr; 621218893Sdim 622226890Sdim llvm::Type *FirstElt = SrcSTy->getElementType(0); 623218893Sdim 624210299Sed // If the first elt is at least as large as what we're looking for, or if the 625210299Sed // first element is the same size as the whole struct, we can enter it. 626218893Sdim uint64_t FirstEltSize = 627245431Sdim CGF.CGM.getDataLayout().getTypeAllocSize(FirstElt); 628218893Sdim if (FirstEltSize < DstSize && 629245431Sdim FirstEltSize < CGF.CGM.getDataLayout().getTypeAllocSize(SrcSTy)) 630210299Sed return SrcPtr; 631218893Sdim 632210299Sed // GEP into the first element. 633210299Sed SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); 634218893Sdim 635210299Sed // If the first element is a struct, recurse. 636226890Sdim llvm::Type *SrcTy = 637210299Sed cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 638226890Sdim if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) 639210299Sed return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 640210299Sed 641210299Sed return SrcPtr; 642210299Sed} 643210299Sed 644210299Sed/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both 645210299Sed/// are either integers or pointers. This does a truncation of the value if it 646210299Sed/// is too large or a zero extension if it is too small. 647263509Sdim/// 648263509Sdim/// This behaves as if the value were coerced through memory, so on big-endian 649263509Sdim/// targets the high bits are preserved in a truncation, while little-endian 650263509Sdim/// targets preserve the low bits. 651210299Sedstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, 652226890Sdim llvm::Type *Ty, 653210299Sed CodeGenFunction &CGF) { 654210299Sed if (Val->getType() == Ty) 655210299Sed return Val; 656218893Sdim 657210299Sed if (isa<llvm::PointerType>(Val->getType())) { 658210299Sed // If this is Pointer->Pointer avoid conversion to and from int. 659210299Sed if (isa<llvm::PointerType>(Ty)) 660210299Sed return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); 661218893Sdim 662210299Sed // Convert the pointer to an integer so we can play with its width. 663210299Sed Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); 664210299Sed } 665218893Sdim 666226890Sdim llvm::Type *DestIntTy = Ty; 667210299Sed if (isa<llvm::PointerType>(DestIntTy)) 668210299Sed DestIntTy = CGF.IntPtrTy; 669218893Sdim 670263509Sdim if (Val->getType() != DestIntTy) { 671263509Sdim const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); 672263509Sdim if (DL.isBigEndian()) { 673263509Sdim // Preserve the high bits on big-endian targets. 674263509Sdim // That is what memory coercion does. 675263509Sdim uint64_t SrcSize = DL.getTypeAllocSizeInBits(Val->getType()); 676263509Sdim uint64_t DstSize = DL.getTypeAllocSizeInBits(DestIntTy); 677263509Sdim if (SrcSize > DstSize) { 678263509Sdim Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits"); 679263509Sdim Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii"); 680263509Sdim } else { 681263509Sdim Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii"); 682263509Sdim Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits"); 683263509Sdim } 684263509Sdim } else { 685263509Sdim // Little-endian targets preserve the low bits. No shifts required. 686263509Sdim Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); 687263509Sdim } 688263509Sdim } 689218893Sdim 690210299Sed if (isa<llvm::PointerType>(Ty)) 691210299Sed Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); 692210299Sed return Val; 693210299Sed} 694210299Sed 695210299Sed 696210299Sed 697193326Sed/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 698193326Sed/// a pointer to an object of type \arg Ty. 699193326Sed/// 700193326Sed/// This safely handles the case when the src type is smaller than the 701193326Sed/// destination type; in this situation the values of bits which not 702193326Sed/// present in the src are undefined. 703193326Sedstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 704226890Sdim llvm::Type *Ty, 705193326Sed CodeGenFunction &CGF) { 706226890Sdim llvm::Type *SrcTy = 707193326Sed cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 708218893Sdim 709210299Sed // If SrcTy and Ty are the same, just do a load. 710210299Sed if (SrcTy == Ty) 711210299Sed return CGF.Builder.CreateLoad(SrcPtr); 712218893Sdim 713245431Sdim uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty); 714218893Sdim 715226890Sdim if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { 716210299Sed SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 717210299Sed SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 718210299Sed } 719218893Sdim 720245431Sdim uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); 721193326Sed 722210299Sed // If the source and destination are integer or pointer types, just do an 723210299Sed // extension or truncation to the desired type. 724210299Sed if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && 725210299Sed (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { 726210299Sed llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); 727210299Sed return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); 728210299Sed } 729218893Sdim 730193326Sed // If load is legal, just bitcast the src pointer. 731193326Sed if (SrcSize >= DstSize) { 732193326Sed // Generally SrcSize is never greater than DstSize, since this means we are 733193326Sed // losing bits. However, this can happen in cases where the structure has 734193326Sed // additional padding, for example due to a user specified alignment. 735193326Sed // 736193326Sed // FIXME: Assert that we aren't truncating non-padding bits when have access 737193326Sed // to that information. 738193326Sed llvm::Value *Casted = 739193326Sed CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 740193326Sed llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 741193326Sed // FIXME: Use better alignment / avoid requiring aligned load. 742193326Sed Load->setAlignment(1); 743193326Sed return Load; 744193326Sed } 745218893Sdim 746210299Sed // Otherwise do coercion through memory. This is stupid, but 747210299Sed // simple. 748210299Sed llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 749252723Sdim llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); 750252723Sdim llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); 751252723Sdim llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy); 752252723Sdim // FIXME: Use better alignment. 753252723Sdim CGF.Builder.CreateMemCpy(Casted, SrcCasted, 754252723Sdim llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize), 755252723Sdim 1, false); 756210299Sed return CGF.Builder.CreateLoad(Tmp); 757193326Sed} 758193326Sed 759223017Sdim// Function to store a first-class aggregate into memory. We prefer to 760223017Sdim// store the elements rather than the aggregate to be more friendly to 761223017Sdim// fast-isel. 762223017Sdim// FIXME: Do we need to recurse here? 763223017Sdimstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, 764223017Sdim llvm::Value *DestPtr, bool DestIsVolatile, 765223017Sdim bool LowAlignment) { 766223017Sdim // Prefer scalar stores to first-class aggregate stores. 767226890Sdim if (llvm::StructType *STy = 768223017Sdim dyn_cast<llvm::StructType>(Val->getType())) { 769223017Sdim for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 770223017Sdim llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); 771223017Sdim llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); 772223017Sdim llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, 773223017Sdim DestIsVolatile); 774223017Sdim if (LowAlignment) 775223017Sdim SI->setAlignment(1); 776223017Sdim } 777223017Sdim } else { 778235633Sdim llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); 779235633Sdim if (LowAlignment) 780235633Sdim SI->setAlignment(1); 781223017Sdim } 782223017Sdim} 783223017Sdim 784193326Sed/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 785193326Sed/// where the source and destination may have different types. 786193326Sed/// 787193326Sed/// This safely handles the case when the src type is larger than the 788193326Sed/// destination type; the upper bits of the src will be lost. 789193326Sedstatic void CreateCoercedStore(llvm::Value *Src, 790193326Sed llvm::Value *DstPtr, 791201361Srdivacky bool DstIsVolatile, 792193326Sed CodeGenFunction &CGF) { 793226890Sdim llvm::Type *SrcTy = Src->getType(); 794226890Sdim llvm::Type *DstTy = 795193326Sed cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 796210299Sed if (SrcTy == DstTy) { 797210299Sed CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 798210299Sed return; 799210299Sed } 800218893Sdim 801245431Sdim uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); 802218893Sdim 803226890Sdim if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { 804210299Sed DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); 805210299Sed DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 806210299Sed } 807218893Sdim 808210299Sed // If the source and destination are integer or pointer types, just do an 809210299Sed // extension or truncation to the desired type. 810210299Sed if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && 811210299Sed (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { 812210299Sed Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); 813210299Sed CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 814210299Sed return; 815210299Sed } 816218893Sdim 817245431Sdim uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy); 818193326Sed 819193326Sed // If store is legal, just bitcast the src pointer. 820193576Sed if (SrcSize <= DstSize) { 821193326Sed llvm::Value *Casted = 822193326Sed CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 823193326Sed // FIXME: Use better alignment / avoid requiring aligned store. 824223017Sdim BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); 825193326Sed } else { 826193326Sed // Otherwise do coercion through memory. This is stupid, but 827193326Sed // simple. 828193576Sed 829193576Sed // Generally SrcSize is never greater than DstSize, since this means we are 830193576Sed // losing bits. However, this can happen in cases where the structure has 831193576Sed // additional padding, for example due to a user specified alignment. 832193576Sed // 833193576Sed // FIXME: Assert that we aren't truncating non-padding bits when have access 834193576Sed // to that information. 835193326Sed llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 836193326Sed CGF.Builder.CreateStore(Src, Tmp); 837252723Sdim llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); 838252723Sdim llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); 839252723Sdim llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy); 840252723Sdim // FIXME: Use better alignment. 841252723Sdim CGF.Builder.CreateMemCpy(DstCasted, Casted, 842252723Sdim llvm::ConstantInt::get(CGF.IntPtrTy, DstSize), 843252723Sdim 1, false); 844193326Sed } 845193326Sed} 846193326Sed 847193326Sed/***/ 848193326Sed 849210299Sedbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { 850193326Sed return FI.getReturnInfo().isIndirect(); 851193326Sed} 852193326Sed 853210299Sedbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { 854210299Sed if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { 855210299Sed switch (BT->getKind()) { 856210299Sed default: 857210299Sed return false; 858210299Sed case BuiltinType::Float: 859252723Sdim return getTarget().useObjCFPRetForRealType(TargetInfo::Float); 860210299Sed case BuiltinType::Double: 861252723Sdim return getTarget().useObjCFPRetForRealType(TargetInfo::Double); 862210299Sed case BuiltinType::LongDouble: 863252723Sdim return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble); 864210299Sed } 865210299Sed } 866210299Sed 867210299Sed return false; 868210299Sed} 869210299Sed 870235633Sdimbool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { 871235633Sdim if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { 872235633Sdim if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { 873235633Sdim if (BT->getKind() == BuiltinType::LongDouble) 874252723Sdim return getTarget().useObjCFP2RetForComplexLongDouble(); 875235633Sdim } 876235633Sdim } 877218893Sdim 878235633Sdim return false; 879235633Sdim} 880204643Srdivacky 881235633Sdimllvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { 882235633Sdim const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); 883235633Sdim return GetFunctionType(FI); 884204643Srdivacky} 885204643Srdivacky 886224145Sdimllvm::FunctionType * 887235633SdimCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { 888224145Sdim 889224145Sdim bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted; 890224145Sdim assert(Inserted && "Recursively being processed?"); 891224145Sdim 892226890Sdim SmallVector<llvm::Type*, 8> argTypes; 893226890Sdim llvm::Type *resultType = 0; 894193326Sed 895223017Sdim const ABIArgInfo &retAI = FI.getReturnInfo(); 896223017Sdim switch (retAI.getKind()) { 897193326Sed case ABIArgInfo::Expand: 898223017Sdim llvm_unreachable("Invalid ABI kind for return argument"); 899193326Sed 900193631Sed case ABIArgInfo::Extend: 901193326Sed case ABIArgInfo::Direct: 902223017Sdim resultType = retAI.getCoerceToType(); 903193326Sed break; 904193326Sed 905193326Sed case ABIArgInfo::Indirect: { 906223017Sdim assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); 907223017Sdim resultType = llvm::Type::getVoidTy(getLLVMContext()); 908223017Sdim 909223017Sdim QualType ret = FI.getReturnType(); 910226890Sdim llvm::Type *ty = ConvertType(ret); 911223017Sdim unsigned addressSpace = Context.getTargetAddressSpace(ret); 912223017Sdim argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); 913193326Sed break; 914193326Sed } 915193326Sed 916193326Sed case ABIArgInfo::Ignore: 917223017Sdim resultType = llvm::Type::getVoidTy(getLLVMContext()); 918193326Sed break; 919193326Sed } 920198092Srdivacky 921252723Sdim // Add in all of the required arguments. 922252723Sdim CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), ie; 923252723Sdim if (FI.isVariadic()) { 924252723Sdim ie = it + FI.getRequiredArgs().getNumRequiredArgs(); 925252723Sdim } else { 926252723Sdim ie = FI.arg_end(); 927252723Sdim } 928252723Sdim for (; it != ie; ++it) { 929223017Sdim const ABIArgInfo &argAI = it->info; 930198092Srdivacky 931245431Sdim // Insert a padding type to ensure proper alignment. 932245431Sdim if (llvm::Type *PaddingType = argAI.getPaddingType()) 933245431Sdim argTypes.push_back(PaddingType); 934245431Sdim 935223017Sdim switch (argAI.getKind()) { 936193326Sed case ABIArgInfo::Ignore: 937193326Sed break; 938193326Sed 939212904Sdim case ABIArgInfo::Indirect: { 940212904Sdim // indirect arguments are always on the stack, which is addr space #0. 941226890Sdim llvm::Type *LTy = ConvertTypeForMem(it->type); 942223017Sdim argTypes.push_back(LTy->getPointerTo()); 943212904Sdim break; 944212904Sdim } 945212904Sdim 946212904Sdim case ABIArgInfo::Extend: 947212904Sdim case ABIArgInfo::Direct: { 948210299Sed // If the coerce-to type is a first class aggregate, flatten it. Either 949210299Sed // way is semantically identical, but fast-isel and the optimizer 950210299Sed // generally likes scalar values better than FCAs. 951224145Sdim llvm::Type *argType = argAI.getCoerceToType(); 952226890Sdim if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { 953223017Sdim for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) 954223017Sdim argTypes.push_back(st->getElementType(i)); 955210299Sed } else { 956223017Sdim argTypes.push_back(argType); 957210299Sed } 958193326Sed break; 959210299Sed } 960193326Sed 961193326Sed case ABIArgInfo::Expand: 962224145Sdim GetExpandedTypes(it->type, argTypes); 963193326Sed break; 964193326Sed } 965193326Sed } 966193326Sed 967224145Sdim bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; 968224145Sdim assert(Erased && "Not in set?"); 969224145Sdim 970235633Sdim return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic()); 971193326Sed} 972193326Sed 973226890Sdimllvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { 974212904Sdim const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 975199990Srdivacky const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 976218893Sdim 977224145Sdim if (!isFuncTypeConvertible(FPT)) 978224145Sdim return llvm::StructType::get(getLLVMContext()); 979224145Sdim 980224145Sdim const CGFunctionInfo *Info; 981224145Sdim if (isa<CXXDestructorDecl>(MD)) 982235633Sdim Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType()); 983224145Sdim else 984235633Sdim Info = &arrangeCXXMethodDeclaration(MD); 985235633Sdim return GetFunctionType(*Info); 986199990Srdivacky} 987199990Srdivacky 988193326Sedvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 989193326Sed const Decl *TargetDecl, 990218893Sdim AttributeListType &PAL, 991252723Sdim unsigned &CallingConv, 992252723Sdim bool AttrOnCallSite) { 993245431Sdim llvm::AttrBuilder FuncAttrs; 994245431Sdim llvm::AttrBuilder RetAttrs; 995193326Sed 996198092Srdivacky CallingConv = FI.getEffectiveCallingConvention(); 997198092Srdivacky 998203955Srdivacky if (FI.isNoReturn()) 999252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1000203955Srdivacky 1001193326Sed // FIXME: handle sseregparm someday... 1002193326Sed if (TargetDecl) { 1003226890Sdim if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 1004252723Sdim FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice); 1005195341Sed if (TargetDecl->hasAttr<NoThrowAttr>()) 1006252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1007252723Sdim if (TargetDecl->hasAttr<NoReturnAttr>()) 1008252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1009252723Sdim 1010252723Sdim if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { 1011210299Sed const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); 1012221345Sdim if (FPT && FPT->isNothrow(getContext())) 1013252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1014252723Sdim // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. 1015252723Sdim // These attributes are not inherited by overloads. 1016252723Sdim const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); 1017252723Sdim if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) 1018252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1019210299Sed } 1020210299Sed 1021226890Sdim // 'const' and 'pure' attribute functions are also nounwind. 1022226890Sdim if (TargetDecl->hasAttr<ConstAttr>()) { 1023252723Sdim FuncAttrs.addAttribute(llvm::Attribute::ReadNone); 1024252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1025226890Sdim } else if (TargetDecl->hasAttr<PureAttr>()) { 1026252723Sdim FuncAttrs.addAttribute(llvm::Attribute::ReadOnly); 1027252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1028226890Sdim } 1029198092Srdivacky if (TargetDecl->hasAttr<MallocAttr>()) 1030252723Sdim RetAttrs.addAttribute(llvm::Attribute::NoAlias); 1031193326Sed } 1032193326Sed 1033199482Srdivacky if (CodeGenOpts.OptimizeSize) 1034252723Sdim FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize); 1035245431Sdim if (CodeGenOpts.OptimizeSize == 2) 1036252723Sdim FuncAttrs.addAttribute(llvm::Attribute::MinSize); 1037199482Srdivacky if (CodeGenOpts.DisableRedZone) 1038252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoRedZone); 1039199482Srdivacky if (CodeGenOpts.NoImplicitFloat) 1040252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat); 1041193576Sed 1042252723Sdim if (AttrOnCallSite) { 1043252723Sdim // Attributes that should go on the call site only. 1044252723Sdim if (!CodeGenOpts.SimplifyLibCalls) 1045252723Sdim FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin); 1046252723Sdim } else { 1047252723Sdim // Attributes that should go on the function, but not the call site. 1048252723Sdim if (!CodeGenOpts.DisableFPElim) { 1049252723Sdim FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); 1050252723Sdim } else if (CodeGenOpts.OmitLeafFramePointer) { 1051252723Sdim FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); 1052263509Sdim FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); 1053252723Sdim } else { 1054252723Sdim FuncAttrs.addAttribute("no-frame-pointer-elim", "true"); 1055263509Sdim FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); 1056252723Sdim } 1057252723Sdim 1058252723Sdim FuncAttrs.addAttribute("less-precise-fpmad", 1059263509Sdim llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD)); 1060252723Sdim FuncAttrs.addAttribute("no-infs-fp-math", 1061263509Sdim llvm::toStringRef(CodeGenOpts.NoInfsFPMath)); 1062252723Sdim FuncAttrs.addAttribute("no-nans-fp-math", 1063263509Sdim llvm::toStringRef(CodeGenOpts.NoNaNsFPMath)); 1064252723Sdim FuncAttrs.addAttribute("unsafe-fp-math", 1065263509Sdim llvm::toStringRef(CodeGenOpts.UnsafeFPMath)); 1066252723Sdim FuncAttrs.addAttribute("use-soft-float", 1067263509Sdim llvm::toStringRef(CodeGenOpts.SoftFloat)); 1068263509Sdim FuncAttrs.addAttribute("stack-protector-buffer-size", 1069263509Sdim llvm::utostr(CodeGenOpts.SSPBufferSize)); 1070263509Sdim 1071263509Sdim if (!CodeGenOpts.StackRealignment) 1072263509Sdim FuncAttrs.addAttribute("no-realign-stack"); 1073252723Sdim } 1074252723Sdim 1075193326Sed QualType RetTy = FI.getReturnType(); 1076193326Sed unsigned Index = 1; 1077193326Sed const ABIArgInfo &RetAI = FI.getReturnInfo(); 1078193326Sed switch (RetAI.getKind()) { 1079193631Sed case ABIArgInfo::Extend: 1080263509Sdim if (RetTy->hasSignedIntegerRepresentation()) 1081263509Sdim RetAttrs.addAttribute(llvm::Attribute::SExt); 1082263509Sdim else if (RetTy->hasUnsignedIntegerRepresentation()) 1083263509Sdim RetAttrs.addAttribute(llvm::Attribute::ZExt); 1084263509Sdim // FALL THROUGH 1085263509Sdim case ABIArgInfo::Direct: 1086263509Sdim if (RetAI.getInReg()) 1087263509Sdim RetAttrs.addAttribute(llvm::Attribute::InReg); 1088212904Sdim break; 1089212904Sdim case ABIArgInfo::Ignore: 1090193326Sed break; 1091193326Sed 1092245431Sdim case ABIArgInfo::Indirect: { 1093245431Sdim llvm::AttrBuilder SRETAttrs; 1094252723Sdim SRETAttrs.addAttribute(llvm::Attribute::StructRet); 1095245431Sdim if (RetAI.getInReg()) 1096252723Sdim SRETAttrs.addAttribute(llvm::Attribute::InReg); 1097245431Sdim PAL.push_back(llvm:: 1098252723Sdim AttributeSet::get(getLLVMContext(), Index, SRETAttrs)); 1099245431Sdim 1100193326Sed ++Index; 1101193326Sed // sret disables readnone and readonly 1102252723Sdim FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) 1103252723Sdim .removeAttribute(llvm::Attribute::ReadNone); 1104193326Sed break; 1105245431Sdim } 1106193326Sed 1107193326Sed case ABIArgInfo::Expand: 1108226890Sdim llvm_unreachable("Invalid ABI kind for return argument"); 1109193326Sed } 1110193326Sed 1111245431Sdim if (RetAttrs.hasAttributes()) 1112245431Sdim PAL.push_back(llvm:: 1113252723Sdim AttributeSet::get(getLLVMContext(), 1114252723Sdim llvm::AttributeSet::ReturnIndex, 1115252723Sdim RetAttrs)); 1116193326Sed 1117198092Srdivacky for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 1118193326Sed ie = FI.arg_end(); it != ie; ++it) { 1119193326Sed QualType ParamType = it->type; 1120193326Sed const ABIArgInfo &AI = it->info; 1121245431Sdim llvm::AttrBuilder Attrs; 1122193326Sed 1123245431Sdim if (AI.getPaddingType()) { 1124252723Sdim if (AI.getPaddingInReg()) 1125252723Sdim PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, 1126252723Sdim llvm::Attribute::InReg)); 1127245431Sdim // Increment Index if there is padding. 1128245431Sdim ++Index; 1129245431Sdim } 1130245431Sdim 1131206084Srdivacky // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we 1132206084Srdivacky // have the corresponding parameter variable. It doesn't make 1133218893Sdim // sense to do it here because parameters are so messed up. 1134193326Sed switch (AI.getKind()) { 1135212904Sdim case ABIArgInfo::Extend: 1136223017Sdim if (ParamType->isSignedIntegerOrEnumerationType()) 1137252723Sdim Attrs.addAttribute(llvm::Attribute::SExt); 1138223017Sdim else if (ParamType->isUnsignedIntegerOrEnumerationType()) 1139252723Sdim Attrs.addAttribute(llvm::Attribute::ZExt); 1140212904Sdim // FALL THROUGH 1141212904Sdim case ABIArgInfo::Direct: 1142245431Sdim if (AI.getInReg()) 1143252723Sdim Attrs.addAttribute(llvm::Attribute::InReg); 1144245431Sdim 1145212904Sdim // FIXME: handle sseregparm someday... 1146218893Sdim 1147226890Sdim if (llvm::StructType *STy = 1148245431Sdim dyn_cast<llvm::StructType>(AI.getCoerceToType())) { 1149245431Sdim unsigned Extra = STy->getNumElements()-1; // 1 will be added below. 1150245431Sdim if (Attrs.hasAttributes()) 1151245431Sdim for (unsigned I = 0; I < Extra; ++I) 1152252723Sdim PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index + I, 1153252723Sdim Attrs)); 1154245431Sdim Index += Extra; 1155245431Sdim } 1156212904Sdim break; 1157193326Sed 1158193326Sed case ABIArgInfo::Indirect: 1159245431Sdim if (AI.getInReg()) 1160252723Sdim Attrs.addAttribute(llvm::Attribute::InReg); 1161245431Sdim 1162198092Srdivacky if (AI.getIndirectByVal()) 1163252723Sdim Attrs.addAttribute(llvm::Attribute::ByVal); 1164198092Srdivacky 1165245431Sdim Attrs.addAlignmentAttr(AI.getIndirectAlign()); 1166245431Sdim 1167193326Sed // byval disables readnone and readonly. 1168252723Sdim FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) 1169252723Sdim .removeAttribute(llvm::Attribute::ReadNone); 1170193326Sed break; 1171193631Sed 1172193326Sed case ABIArgInfo::Ignore: 1173193326Sed // Skip increment, no matching LLVM parameter. 1174198092Srdivacky continue; 1175193326Sed 1176193326Sed case ABIArgInfo::Expand: { 1177226890Sdim SmallVector<llvm::Type*, 8> types; 1178193326Sed // FIXME: This is rather inefficient. Do we ever actually need to do 1179193326Sed // anything here? The result should be just reconstructed on the other 1180193326Sed // side, so extension should be a non-issue. 1181224145Sdim getTypes().GetExpandedTypes(ParamType, types); 1182223017Sdim Index += types.size(); 1183193326Sed continue; 1184193326Sed } 1185193326Sed } 1186198092Srdivacky 1187245431Sdim if (Attrs.hasAttributes()) 1188252723Sdim PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs)); 1189193326Sed ++Index; 1190193326Sed } 1191245431Sdim if (FuncAttrs.hasAttributes()) 1192245431Sdim PAL.push_back(llvm:: 1193252723Sdim AttributeSet::get(getLLVMContext(), 1194252723Sdim llvm::AttributeSet::FunctionIndex, 1195252723Sdim FuncAttrs)); 1196193326Sed} 1197193326Sed 1198221345Sdim/// An argument came in as a promoted argument; demote it back to its 1199221345Sdim/// declared type. 1200221345Sdimstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, 1201221345Sdim const VarDecl *var, 1202221345Sdim llvm::Value *value) { 1203226890Sdim llvm::Type *varType = CGF.ConvertType(var->getType()); 1204221345Sdim 1205221345Sdim // This can happen with promotions that actually don't change the 1206221345Sdim // underlying type, like the enum promotions. 1207221345Sdim if (value->getType() == varType) return value; 1208221345Sdim 1209221345Sdim assert((varType->isIntegerTy() || varType->isFloatingPointTy()) 1210221345Sdim && "unexpected promotion type"); 1211221345Sdim 1212221345Sdim if (isa<llvm::IntegerType>(varType)) 1213221345Sdim return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); 1214221345Sdim 1215221345Sdim return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); 1216221345Sdim} 1217221345Sdim 1218193326Sedvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 1219193326Sed llvm::Function *Fn, 1220193326Sed const FunctionArgList &Args) { 1221198092Srdivacky // If this is an implicit-return-zero function, go ahead and 1222198092Srdivacky // initialize the return value. TODO: it might be nice to have 1223198092Srdivacky // a more general mechanism for this that didn't require synthesized 1224198092Srdivacky // return statements. 1225252723Sdim if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) { 1226198092Srdivacky if (FD->hasImplicitReturnZero()) { 1227198092Srdivacky QualType RetTy = FD->getResultType().getUnqualifiedType(); 1228226890Sdim llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); 1229198092Srdivacky llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); 1230198092Srdivacky Builder.CreateStore(Zero, ReturnValue); 1231198092Srdivacky } 1232198092Srdivacky } 1233198092Srdivacky 1234193326Sed // FIXME: We no longer need the types from FunctionArgList; lift up and 1235193326Sed // simplify. 1236193326Sed 1237193326Sed // Emit allocs for param decls. Give the LLVM Argument nodes names. 1238193326Sed llvm::Function::arg_iterator AI = Fn->arg_begin(); 1239198092Srdivacky 1240193326Sed // Name the struct return argument. 1241210299Sed if (CGM.ReturnTypeUsesSRet(FI)) { 1242193326Sed AI->setName("agg.result"); 1243252723Sdim AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1244252723Sdim AI->getArgNo() + 1, 1245252723Sdim llvm::Attribute::NoAlias)); 1246193326Sed ++AI; 1247193326Sed } 1248198092Srdivacky 1249193326Sed assert(FI.arg_size() == Args.size() && 1250193326Sed "Mismatch between function signature & arguments."); 1251221345Sdim unsigned ArgNo = 1; 1252193326Sed CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 1253221345Sdim for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 1254221345Sdim i != e; ++i, ++info_it, ++ArgNo) { 1255221345Sdim const VarDecl *Arg = *i; 1256193326Sed QualType Ty = info_it->type; 1257193326Sed const ABIArgInfo &ArgI = info_it->info; 1258193326Sed 1259221345Sdim bool isPromoted = 1260221345Sdim isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); 1261221345Sdim 1262245431Sdim // Skip the dummy padding argument. 1263245431Sdim if (ArgI.getPaddingType()) 1264245431Sdim ++AI; 1265245431Sdim 1266193326Sed switch (ArgI.getKind()) { 1267193326Sed case ABIArgInfo::Indirect: { 1268210299Sed llvm::Value *V = AI; 1269218893Sdim 1270252723Sdim if (!hasScalarEvaluationKind(Ty)) { 1271218893Sdim // Aggregates and complex variables are accessed by reference. All we 1272218893Sdim // need to do is realign the value, if requested 1273218893Sdim if (ArgI.getIndirectRealign()) { 1274218893Sdim llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); 1275218893Sdim 1276218893Sdim // Copy from the incoming argument pointer to the temporary with the 1277218893Sdim // appropriate alignment. 1278218893Sdim // 1279218893Sdim // FIXME: We should have a common utility for generating an aggregate 1280218893Sdim // copy. 1281226890Sdim llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 1282218893Sdim CharUnits Size = getContext().getTypeSizeInChars(Ty); 1283221345Sdim llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 1284221345Sdim llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); 1285221345Sdim Builder.CreateMemCpy(Dst, 1286221345Sdim Src, 1287218893Sdim llvm::ConstantInt::get(IntPtrTy, 1288218893Sdim Size.getQuantity()), 1289218893Sdim ArgI.getIndirectAlign(), 1290218893Sdim false); 1291218893Sdim V = AlignedTemp; 1292218893Sdim } 1293193326Sed } else { 1294193326Sed // Load scalar value from indirect argument. 1295218893Sdim CharUnits Alignment = getContext().getTypeAlignInChars(Ty); 1296263509Sdim V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty, 1297263509Sdim Arg->getLocStart()); 1298221345Sdim 1299221345Sdim if (isPromoted) 1300221345Sdim V = emitArgumentDemotion(*this, Arg, V); 1301193326Sed } 1302221345Sdim EmitParmDecl(*Arg, V, ArgNo); 1303193326Sed break; 1304193326Sed } 1305193631Sed 1306193631Sed case ABIArgInfo::Extend: 1307193326Sed case ABIArgInfo::Direct: { 1308235633Sdim 1309212904Sdim // If we have the trivial case, handle it with no muss and fuss. 1310212904Sdim if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && 1311212904Sdim ArgI.getCoerceToType() == ConvertType(Ty) && 1312212904Sdim ArgI.getDirectOffset() == 0) { 1313212904Sdim assert(AI != Fn->arg_end() && "Argument mismatch!"); 1314212904Sdim llvm::Value *V = AI; 1315218893Sdim 1316206084Srdivacky if (Arg->getType().isRestrictQualified()) 1317252723Sdim AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1318252723Sdim AI->getArgNo() + 1, 1319252723Sdim llvm::Attribute::NoAlias)); 1320206084Srdivacky 1321226890Sdim // Ensure the argument is the correct type. 1322226890Sdim if (V->getType() != ArgI.getCoerceToType()) 1323226890Sdim V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); 1324226890Sdim 1325221345Sdim if (isPromoted) 1326221345Sdim V = emitArgumentDemotion(*this, Arg, V); 1327252723Sdim 1328263509Sdim if (const CXXMethodDecl *MD = 1329263509Sdim dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) { 1330263509Sdim if (MD->isVirtual() && Arg == CXXABIThisDecl) 1331263509Sdim V = CGM.getCXXABI(). 1332263509Sdim adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V); 1333263509Sdim } 1334263509Sdim 1335252723Sdim // Because of merging of function types from multiple decls it is 1336252723Sdim // possible for the type of an argument to not match the corresponding 1337252723Sdim // type in the function type. Since we are codegening the callee 1338252723Sdim // in here, add a cast to the argument type. 1339252723Sdim llvm::Type *LTy = ConvertType(Arg->getType()); 1340252723Sdim if (V->getType() != LTy) 1341252723Sdim V = Builder.CreateBitCast(V, LTy); 1342252723Sdim 1343221345Sdim EmitParmDecl(*Arg, V, ArgNo); 1344212904Sdim break; 1345193326Sed } 1346198092Srdivacky 1347235633Sdim llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); 1348218893Sdim 1349212904Sdim // The alignment we need to use is the max of the requested alignment for 1350212904Sdim // the argument plus the alignment required by our access code below. 1351218893Sdim unsigned AlignmentToUse = 1352245431Sdim CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType()); 1353212904Sdim AlignmentToUse = std::max(AlignmentToUse, 1354212904Sdim (unsigned)getContext().getDeclAlign(Arg).getQuantity()); 1355218893Sdim 1356212904Sdim Alloca->setAlignment(AlignmentToUse); 1357210299Sed llvm::Value *V = Alloca; 1358212904Sdim llvm::Value *Ptr = V; // Pointer to store into. 1359218893Sdim 1360212904Sdim // If the value is offset in memory, apply the offset now. 1361212904Sdim if (unsigned Offs = ArgI.getDirectOffset()) { 1362212904Sdim Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); 1363212904Sdim Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); 1364218893Sdim Ptr = Builder.CreateBitCast(Ptr, 1365212904Sdim llvm::PointerType::getUnqual(ArgI.getCoerceToType())); 1366212904Sdim } 1367218893Sdim 1368210299Sed // If the coerce-to type is a first class aggregate, we flatten it and 1369210299Sed // pass the elements. Either way is semantically identical, but fast-isel 1370210299Sed // and the optimizer generally likes scalar values better than FCAs. 1371235633Sdim llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); 1372235633Sdim if (STy && STy->getNumElements() > 1) { 1373245431Sdim uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy); 1374235633Sdim llvm::Type *DstTy = 1375235633Sdim cast<llvm::PointerType>(Ptr->getType())->getElementType(); 1376245431Sdim uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy); 1377218893Sdim 1378235633Sdim if (SrcSize <= DstSize) { 1379235633Sdim Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); 1380235633Sdim 1381235633Sdim for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1382235633Sdim assert(AI != Fn->arg_end() && "Argument mismatch!"); 1383235633Sdim AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1384235633Sdim llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); 1385235633Sdim Builder.CreateStore(AI++, EltPtr); 1386235633Sdim } 1387235633Sdim } else { 1388235633Sdim llvm::AllocaInst *TempAlloca = 1389235633Sdim CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); 1390235633Sdim TempAlloca->setAlignment(AlignmentToUse); 1391235633Sdim llvm::Value *TempV = TempAlloca; 1392235633Sdim 1393235633Sdim for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1394235633Sdim assert(AI != Fn->arg_end() && "Argument mismatch!"); 1395235633Sdim AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1396235633Sdim llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); 1397235633Sdim Builder.CreateStore(AI++, EltPtr); 1398235633Sdim } 1399235633Sdim 1400235633Sdim Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); 1401210299Sed } 1402210299Sed } else { 1403210299Sed // Simple case, just do a coerced store of the argument into the alloca. 1404210299Sed assert(AI != Fn->arg_end() && "Argument mismatch!"); 1405210299Sed AI->setName(Arg->getName() + ".coerce"); 1406212904Sdim CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); 1407210299Sed } 1408218893Sdim 1409218893Sdim 1410193326Sed // Match to what EmitParmDecl is expecting for this type. 1411252723Sdim if (CodeGenFunction::hasScalarEvaluationKind(Ty)) { 1412263509Sdim V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart()); 1413221345Sdim if (isPromoted) 1414221345Sdim V = emitArgumentDemotion(*this, Arg, V); 1415193326Sed } 1416221345Sdim EmitParmDecl(*Arg, V, ArgNo); 1417210299Sed continue; // Skip ++AI increment, already done. 1418193326Sed } 1419212904Sdim 1420212904Sdim case ABIArgInfo::Expand: { 1421212904Sdim // If this structure was expanded into multiple arguments then 1422212904Sdim // we need to create a temporary and reconstruct it from the 1423212904Sdim // arguments. 1424235633Sdim llvm::AllocaInst *Alloca = CreateMemTemp(Ty); 1425235633Sdim CharUnits Align = getContext().getDeclAlign(Arg); 1426235633Sdim Alloca->setAlignment(Align.getQuantity()); 1427235633Sdim LValue LV = MakeAddrLValue(Alloca, Ty, Align); 1428235633Sdim llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI); 1429235633Sdim EmitParmDecl(*Arg, Alloca, ArgNo); 1430212904Sdim 1431212904Sdim // Name the arguments used in expansion and increment AI. 1432212904Sdim unsigned Index = 0; 1433212904Sdim for (; AI != End; ++AI, ++Index) 1434226890Sdim AI->setName(Arg->getName() + "." + Twine(Index)); 1435212904Sdim continue; 1436193326Sed } 1437193326Sed 1438212904Sdim case ABIArgInfo::Ignore: 1439212904Sdim // Initialize the local variable appropriately. 1440252723Sdim if (!hasScalarEvaluationKind(Ty)) 1441221345Sdim EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo); 1442212904Sdim else 1443221345Sdim EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())), 1444221345Sdim ArgNo); 1445212904Sdim 1446212904Sdim // Skip increment, no matching LLVM parameter. 1447212904Sdim continue; 1448212904Sdim } 1449212904Sdim 1450193326Sed ++AI; 1451193326Sed } 1452193326Sed assert(AI == Fn->arg_end() && "Argument mismatch!"); 1453193326Sed} 1454193326Sed 1455235633Sdimstatic void eraseUnusedBitCasts(llvm::Instruction *insn) { 1456235633Sdim while (insn->use_empty()) { 1457235633Sdim llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); 1458235633Sdim if (!bitcast) return; 1459235633Sdim 1460235633Sdim // This is "safe" because we would have used a ConstantExpr otherwise. 1461235633Sdim insn = cast<llvm::Instruction>(bitcast->getOperand(0)); 1462235633Sdim bitcast->eraseFromParent(); 1463235633Sdim } 1464235633Sdim} 1465235633Sdim 1466224145Sdim/// Try to emit a fused autorelease of a return result. 1467224145Sdimstatic llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, 1468224145Sdim llvm::Value *result) { 1469224145Sdim // We must be immediately followed the cast. 1470224145Sdim llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); 1471224145Sdim if (BB->empty()) return 0; 1472224145Sdim if (&BB->back() != result) return 0; 1473224145Sdim 1474226890Sdim llvm::Type *resultType = result->getType(); 1475224145Sdim 1476224145Sdim // result is in a BasicBlock and is therefore an Instruction. 1477224145Sdim llvm::Instruction *generator = cast<llvm::Instruction>(result); 1478224145Sdim 1479226890Sdim SmallVector<llvm::Instruction*,4> insnsToKill; 1480224145Sdim 1481224145Sdim // Look for: 1482224145Sdim // %generator = bitcast %type1* %generator2 to %type2* 1483224145Sdim while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { 1484224145Sdim // We would have emitted this as a constant if the operand weren't 1485224145Sdim // an Instruction. 1486224145Sdim generator = cast<llvm::Instruction>(bitcast->getOperand(0)); 1487224145Sdim 1488224145Sdim // Require the generator to be immediately followed by the cast. 1489224145Sdim if (generator->getNextNode() != bitcast) 1490224145Sdim return 0; 1491224145Sdim 1492224145Sdim insnsToKill.push_back(bitcast); 1493224145Sdim } 1494224145Sdim 1495224145Sdim // Look for: 1496224145Sdim // %generator = call i8* @objc_retain(i8* %originalResult) 1497224145Sdim // or 1498224145Sdim // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) 1499224145Sdim llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); 1500224145Sdim if (!call) return 0; 1501224145Sdim 1502224145Sdim bool doRetainAutorelease; 1503224145Sdim 1504224145Sdim if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { 1505224145Sdim doRetainAutorelease = true; 1506224145Sdim } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() 1507224145Sdim .objc_retainAutoreleasedReturnValue) { 1508224145Sdim doRetainAutorelease = false; 1509224145Sdim 1510245431Sdim // If we emitted an assembly marker for this call (and the 1511245431Sdim // ARCEntrypoints field should have been set if so), go looking 1512245431Sdim // for that call. If we can't find it, we can't do this 1513245431Sdim // optimization. But it should always be the immediately previous 1514245431Sdim // instruction, unless we needed bitcasts around the call. 1515245431Sdim if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) { 1516245431Sdim llvm::Instruction *prev = call->getPrevNode(); 1517245431Sdim assert(prev); 1518245431Sdim if (isa<llvm::BitCastInst>(prev)) { 1519245431Sdim prev = prev->getPrevNode(); 1520245431Sdim assert(prev); 1521245431Sdim } 1522245431Sdim assert(isa<llvm::CallInst>(prev)); 1523245431Sdim assert(cast<llvm::CallInst>(prev)->getCalledValue() == 1524245431Sdim CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker); 1525245431Sdim insnsToKill.push_back(prev); 1526245431Sdim } 1527224145Sdim } else { 1528224145Sdim return 0; 1529224145Sdim } 1530224145Sdim 1531224145Sdim result = call->getArgOperand(0); 1532224145Sdim insnsToKill.push_back(call); 1533224145Sdim 1534224145Sdim // Keep killing bitcasts, for sanity. Note that we no longer care 1535224145Sdim // about precise ordering as long as there's exactly one use. 1536224145Sdim while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { 1537224145Sdim if (!bitcast->hasOneUse()) break; 1538224145Sdim insnsToKill.push_back(bitcast); 1539224145Sdim result = bitcast->getOperand(0); 1540224145Sdim } 1541224145Sdim 1542224145Sdim // Delete all the unnecessary instructions, from latest to earliest. 1543226890Sdim for (SmallVectorImpl<llvm::Instruction*>::iterator 1544224145Sdim i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) 1545224145Sdim (*i)->eraseFromParent(); 1546224145Sdim 1547224145Sdim // Do the fused retain/autorelease if we were asked to. 1548224145Sdim if (doRetainAutorelease) 1549224145Sdim result = CGF.EmitARCRetainAutoreleaseReturnValue(result); 1550224145Sdim 1551224145Sdim // Cast back to the result type. 1552224145Sdim return CGF.Builder.CreateBitCast(result, resultType); 1553224145Sdim} 1554224145Sdim 1555235633Sdim/// If this is a +1 of the value of an immutable 'self', remove it. 1556235633Sdimstatic llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, 1557235633Sdim llvm::Value *result) { 1558235633Sdim // This is only applicable to a method with an immutable 'self'. 1559245431Sdim const ObjCMethodDecl *method = 1560245431Sdim dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); 1561235633Sdim if (!method) return 0; 1562235633Sdim const VarDecl *self = method->getSelfDecl(); 1563235633Sdim if (!self->getType().isConstQualified()) return 0; 1564235633Sdim 1565235633Sdim // Look for a retain call. 1566235633Sdim llvm::CallInst *retainCall = 1567235633Sdim dyn_cast<llvm::CallInst>(result->stripPointerCasts()); 1568235633Sdim if (!retainCall || 1569235633Sdim retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) 1570235633Sdim return 0; 1571235633Sdim 1572235633Sdim // Look for an ordinary load of 'self'. 1573235633Sdim llvm::Value *retainedValue = retainCall->getArgOperand(0); 1574235633Sdim llvm::LoadInst *load = 1575235633Sdim dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); 1576235633Sdim if (!load || load->isAtomic() || load->isVolatile() || 1577235633Sdim load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) 1578235633Sdim return 0; 1579235633Sdim 1580235633Sdim // Okay! Burn it all down. This relies for correctness on the 1581235633Sdim // assumption that the retain is emitted as part of the return and 1582235633Sdim // that thereafter everything is used "linearly". 1583235633Sdim llvm::Type *resultType = result->getType(); 1584235633Sdim eraseUnusedBitCasts(cast<llvm::Instruction>(result)); 1585235633Sdim assert(retainCall->use_empty()); 1586235633Sdim retainCall->eraseFromParent(); 1587235633Sdim eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); 1588235633Sdim 1589235633Sdim return CGF.Builder.CreateBitCast(load, resultType); 1590235633Sdim} 1591235633Sdim 1592224145Sdim/// Emit an ARC autorelease of the result of a function. 1593235633Sdim/// 1594235633Sdim/// \return the value to actually return from the function 1595224145Sdimstatic llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, 1596224145Sdim llvm::Value *result) { 1597235633Sdim // If we're returning 'self', kill the initial retain. This is a 1598235633Sdim // heuristic attempt to "encourage correctness" in the really unfortunate 1599235633Sdim // case where we have a return of self during a dealloc and we desperately 1600235633Sdim // need to avoid the possible autorelease. 1601235633Sdim if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) 1602235633Sdim return self; 1603235633Sdim 1604224145Sdim // At -O0, try to emit a fused retain/autorelease. 1605224145Sdim if (CGF.shouldUseFusedARCCalls()) 1606224145Sdim if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) 1607224145Sdim return fused; 1608224145Sdim 1609224145Sdim return CGF.EmitARCAutoreleaseReturnValue(result); 1610224145Sdim} 1611224145Sdim 1612235633Sdim/// Heuristically search for a dominating store to the return-value slot. 1613235633Sdimstatic llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { 1614235633Sdim // If there are multiple uses of the return-value slot, just check 1615235633Sdim // for something immediately preceding the IP. Sometimes this can 1616235633Sdim // happen with how we generate implicit-returns; it can also happen 1617235633Sdim // with noreturn cleanups. 1618235633Sdim if (!CGF.ReturnValue->hasOneUse()) { 1619235633Sdim llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1620235633Sdim if (IP->empty()) return 0; 1621235633Sdim llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); 1622235633Sdim if (!store) return 0; 1623235633Sdim if (store->getPointerOperand() != CGF.ReturnValue) return 0; 1624235633Sdim assert(!store->isAtomic() && !store->isVolatile()); // see below 1625235633Sdim return store; 1626235633Sdim } 1627235633Sdim 1628235633Sdim llvm::StoreInst *store = 1629235633Sdim dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back()); 1630235633Sdim if (!store) return 0; 1631235633Sdim 1632235633Sdim // These aren't actually possible for non-coerced returns, and we 1633235633Sdim // only care about non-coerced returns on this code path. 1634235633Sdim assert(!store->isAtomic() && !store->isVolatile()); 1635235633Sdim 1636235633Sdim // Now do a first-and-dirty dominance check: just walk up the 1637235633Sdim // single-predecessors chain from the current insertion point. 1638235633Sdim llvm::BasicBlock *StoreBB = store->getParent(); 1639235633Sdim llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1640235633Sdim while (IP != StoreBB) { 1641235633Sdim if (!(IP = IP->getSinglePredecessor())) 1642235633Sdim return 0; 1643235633Sdim } 1644235633Sdim 1645235633Sdim // Okay, the store's basic block dominates the insertion point; we 1646235633Sdim // can do our thing. 1647235633Sdim return store; 1648235633Sdim} 1649235633Sdim 1650252723Sdimvoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, 1651263509Sdim bool EmitRetDbgLoc, 1652263509Sdim SourceLocation EndLoc) { 1653210299Sed // Functions with no result always return void. 1654210299Sed if (ReturnValue == 0) { 1655210299Sed Builder.CreateRetVoid(); 1656210299Sed return; 1657210299Sed } 1658210299Sed 1659212904Sdim llvm::DebugLoc RetDbgLoc; 1660193326Sed llvm::Value *RV = 0; 1661210299Sed QualType RetTy = FI.getReturnType(); 1662210299Sed const ABIArgInfo &RetAI = FI.getReturnInfo(); 1663193326Sed 1664210299Sed switch (RetAI.getKind()) { 1665212904Sdim case ABIArgInfo::Indirect: { 1666252723Sdim switch (getEvaluationKind(RetTy)) { 1667252723Sdim case TEK_Complex: { 1668252723Sdim ComplexPairTy RT = 1669263509Sdim EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy), 1670263509Sdim EndLoc); 1671252723Sdim EmitStoreOfComplex(RT, 1672252723Sdim MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy), 1673252723Sdim /*isInit*/ true); 1674252723Sdim break; 1675252723Sdim } 1676252723Sdim case TEK_Aggregate: 1677210299Sed // Do nothing; aggregrates get evaluated directly into the destination. 1678252723Sdim break; 1679252723Sdim case TEK_Scalar: 1680252723Sdim EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), 1681252723Sdim MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy), 1682252723Sdim /*isInit*/ true); 1683252723Sdim break; 1684210299Sed } 1685210299Sed break; 1686212904Sdim } 1687193631Sed 1688210299Sed case ABIArgInfo::Extend: 1689212904Sdim case ABIArgInfo::Direct: 1690212904Sdim if (RetAI.getCoerceToType() == ConvertType(RetTy) && 1691212904Sdim RetAI.getDirectOffset() == 0) { 1692212904Sdim // The internal return value temp always will have pointer-to-return-type 1693212904Sdim // type, just do a load. 1694218893Sdim 1695235633Sdim // If there is a dominating store to ReturnValue, we can elide 1696235633Sdim // the load, zap the store, and usually zap the alloca. 1697235633Sdim if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { 1698263509Sdim // Reuse the debug location from the store unless there is 1699263509Sdim // cleanup code to be emitted between the store and return 1700263509Sdim // instruction. 1701263509Sdim if (EmitRetDbgLoc && !AutoreleaseResult) 1702252723Sdim RetDbgLoc = SI->getDebugLoc(); 1703212904Sdim // Get the stored value and nuke the now-dead store. 1704212904Sdim RV = SI->getValueOperand(); 1705212904Sdim SI->eraseFromParent(); 1706218893Sdim 1707212904Sdim // If that was the only use of the return value, nuke it as well now. 1708212904Sdim if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { 1709212904Sdim cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); 1710212904Sdim ReturnValue = 0; 1711212904Sdim } 1712235633Sdim 1713235633Sdim // Otherwise, we have to do a simple load. 1714235633Sdim } else { 1715235633Sdim RV = Builder.CreateLoad(ReturnValue); 1716212904Sdim } 1717210299Sed } else { 1718212904Sdim llvm::Value *V = ReturnValue; 1719212904Sdim // If the value is offset in memory, apply the offset now. 1720212904Sdim if (unsigned Offs = RetAI.getDirectOffset()) { 1721212904Sdim V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); 1722212904Sdim V = Builder.CreateConstGEP1_32(V, Offs); 1723218893Sdim V = Builder.CreateBitCast(V, 1724212904Sdim llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 1725212904Sdim } 1726218893Sdim 1727212904Sdim RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); 1728210299Sed } 1729224145Sdim 1730224145Sdim // In ARC, end functions that return a retainable type with a call 1731224145Sdim // to objc_autoreleaseReturnValue. 1732224145Sdim if (AutoreleaseResult) { 1733235633Sdim assert(getLangOpts().ObjCAutoRefCount && 1734224145Sdim !FI.isReturnsRetained() && 1735224145Sdim RetTy->isObjCRetainableType()); 1736224145Sdim RV = emitAutoreleaseOfResult(*this, RV); 1737224145Sdim } 1738224145Sdim 1739210299Sed break; 1740212904Sdim 1741210299Sed case ABIArgInfo::Ignore: 1742210299Sed break; 1743193326Sed 1744210299Sed case ABIArgInfo::Expand: 1745226890Sdim llvm_unreachable("Invalid ABI kind for return argument"); 1746193326Sed } 1747198092Srdivacky 1748210299Sed llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); 1749212904Sdim if (!RetDbgLoc.isUnknown()) 1750212904Sdim Ret->setDebugLoc(RetDbgLoc); 1751193326Sed} 1752193326Sed 1753221345Sdimvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args, 1754263509Sdim const VarDecl *param, 1755263509Sdim SourceLocation loc) { 1756208600Srdivacky // StartFunction converted the ABI-lowered parameter(s) into a 1757208600Srdivacky // local alloca. We need to turn that into an r-value suitable 1758208600Srdivacky // for EmitCall. 1759221345Sdim llvm::Value *local = GetAddrOfLocalVar(param); 1760208600Srdivacky 1761221345Sdim QualType type = param->getType(); 1762218893Sdim 1763208600Srdivacky // For the most part, we just need to load the alloca, except: 1764208600Srdivacky // 1) aggregate r-values are actually pointers to temporaries, and 1765252723Sdim // 2) references to non-scalars are pointers directly to the aggregate. 1766252723Sdim // I don't know why references to scalars are different here. 1767221345Sdim if (const ReferenceType *ref = type->getAs<ReferenceType>()) { 1768252723Sdim if (!hasScalarEvaluationKind(ref->getPointeeType())) 1769221345Sdim return args.add(RValue::getAggregate(local), type); 1770208600Srdivacky 1771208600Srdivacky // Locals which are references to scalars are represented 1772208600Srdivacky // with allocas holding the pointer. 1773221345Sdim return args.add(RValue::get(Builder.CreateLoad(local)), type); 1774208600Srdivacky } 1775208600Srdivacky 1776263509Sdim args.add(convertTempToRValue(local, type, loc), type); 1777208600Srdivacky} 1778208600Srdivacky 1779224145Sdimstatic bool isProvablyNull(llvm::Value *addr) { 1780224145Sdim return isa<llvm::ConstantPointerNull>(addr); 1781224145Sdim} 1782224145Sdim 1783224145Sdimstatic bool isProvablyNonNull(llvm::Value *addr) { 1784224145Sdim return isa<llvm::AllocaInst>(addr); 1785224145Sdim} 1786224145Sdim 1787224145Sdim/// Emit the actual writing-back of a writeback. 1788224145Sdimstatic void emitWriteback(CodeGenFunction &CGF, 1789224145Sdim const CallArgList::Writeback &writeback) { 1790252723Sdim const LValue &srcLV = writeback.Source; 1791252723Sdim llvm::Value *srcAddr = srcLV.getAddress(); 1792224145Sdim assert(!isProvablyNull(srcAddr) && 1793224145Sdim "shouldn't have writeback for provably null argument"); 1794224145Sdim 1795224145Sdim llvm::BasicBlock *contBB = 0; 1796224145Sdim 1797224145Sdim // If the argument wasn't provably non-null, we need to null check 1798224145Sdim // before doing the store. 1799224145Sdim bool provablyNonNull = isProvablyNonNull(srcAddr); 1800224145Sdim if (!provablyNonNull) { 1801224145Sdim llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); 1802224145Sdim contBB = CGF.createBasicBlock("icr.done"); 1803224145Sdim 1804224145Sdim llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1805224145Sdim CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); 1806224145Sdim CGF.EmitBlock(writebackBB); 1807224145Sdim } 1808224145Sdim 1809224145Sdim // Load the value to writeback. 1810224145Sdim llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); 1811224145Sdim 1812224145Sdim // Cast it back, in case we're writing an id to a Foo* or something. 1813224145Sdim value = CGF.Builder.CreateBitCast(value, 1814224145Sdim cast<llvm::PointerType>(srcAddr->getType())->getElementType(), 1815224145Sdim "icr.writeback-cast"); 1816224145Sdim 1817224145Sdim // Perform the writeback. 1818224145Sdim 1819252723Sdim // If we have a "to use" value, it's something we need to emit a use 1820252723Sdim // of. This has to be carefully threaded in: if it's done after the 1821252723Sdim // release it's potentially undefined behavior (and the optimizer 1822252723Sdim // will ignore it), and if it happens before the retain then the 1823252723Sdim // optimizer could move the release there. 1824252723Sdim if (writeback.ToUse) { 1825252723Sdim assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong); 1826252723Sdim 1827252723Sdim // Retain the new value. No need to block-copy here: the block's 1828252723Sdim // being passed up the stack. 1829252723Sdim value = CGF.EmitARCRetainNonBlock(value); 1830252723Sdim 1831252723Sdim // Emit the intrinsic use here. 1832252723Sdim CGF.EmitARCIntrinsicUse(writeback.ToUse); 1833252723Sdim 1834252723Sdim // Load the old value (primitively). 1835263509Sdim llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation()); 1836252723Sdim 1837252723Sdim // Put the new value in place (primitively). 1838252723Sdim CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false); 1839252723Sdim 1840252723Sdim // Release the old value. 1841252723Sdim CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime()); 1842252723Sdim 1843252723Sdim // Otherwise, we can just do a normal lvalue store. 1844252723Sdim } else { 1845252723Sdim CGF.EmitStoreThroughLValue(RValue::get(value), srcLV); 1846252723Sdim } 1847252723Sdim 1848224145Sdim // Jump to the continuation block. 1849224145Sdim if (!provablyNonNull) 1850224145Sdim CGF.EmitBlock(contBB); 1851224145Sdim} 1852224145Sdim 1853224145Sdimstatic void emitWritebacks(CodeGenFunction &CGF, 1854224145Sdim const CallArgList &args) { 1855224145Sdim for (CallArgList::writeback_iterator 1856224145Sdim i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i) 1857224145Sdim emitWriteback(CGF, *i); 1858224145Sdim} 1859224145Sdim 1860263509Sdimstatic void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, 1861263509Sdim const CallArgList &CallArgs) { 1862263509Sdim assert(CGF.getTarget().getCXXABI().isArgumentDestroyedByCallee()); 1863263509Sdim ArrayRef<CallArgList::CallArgCleanup> Cleanups = 1864263509Sdim CallArgs.getCleanupsToDeactivate(); 1865263509Sdim // Iterate in reverse to increase the likelihood of popping the cleanup. 1866263509Sdim for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator 1867263509Sdim I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) { 1868263509Sdim CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP); 1869263509Sdim I->IsActiveIP->eraseFromParent(); 1870263509Sdim } 1871263509Sdim} 1872263509Sdim 1873252723Sdimstatic const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) { 1874252723Sdim if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens())) 1875252723Sdim if (uop->getOpcode() == UO_AddrOf) 1876252723Sdim return uop->getSubExpr(); 1877252723Sdim return 0; 1878252723Sdim} 1879252723Sdim 1880224145Sdim/// Emit an argument that's being passed call-by-writeback. That is, 1881224145Sdim/// we are passing the address of 1882224145Sdimstatic void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, 1883224145Sdim const ObjCIndirectCopyRestoreExpr *CRE) { 1884252723Sdim LValue srcLV; 1885224145Sdim 1886252723Sdim // Make an optimistic effort to emit the address as an l-value. 1887252723Sdim // This can fail if the the argument expression is more complicated. 1888252723Sdim if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) { 1889252723Sdim srcLV = CGF.EmitLValue(lvExpr); 1890252723Sdim 1891252723Sdim // Otherwise, just emit it as a scalar. 1892252723Sdim } else { 1893252723Sdim llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); 1894252723Sdim 1895252723Sdim QualType srcAddrType = 1896252723Sdim CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 1897252723Sdim srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType); 1898252723Sdim } 1899252723Sdim llvm::Value *srcAddr = srcLV.getAddress(); 1900252723Sdim 1901224145Sdim // The dest and src types don't necessarily match in LLVM terms 1902224145Sdim // because of the crazy ObjC compatibility rules. 1903224145Sdim 1904226890Sdim llvm::PointerType *destType = 1905224145Sdim cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); 1906224145Sdim 1907224145Sdim // If the address is a constant null, just pass the appropriate null. 1908224145Sdim if (isProvablyNull(srcAddr)) { 1909224145Sdim args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), 1910224145Sdim CRE->getType()); 1911224145Sdim return; 1912224145Sdim } 1913224145Sdim 1914224145Sdim // Create the temporary. 1915224145Sdim llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), 1916224145Sdim "icr.temp"); 1917252723Sdim // Loading an l-value can introduce a cleanup if the l-value is __weak, 1918252723Sdim // and that cleanup will be conditional if we can't prove that the l-value 1919252723Sdim // isn't null, so we need to register a dominating point so that the cleanups 1920252723Sdim // system will make valid IR. 1921252723Sdim CodeGenFunction::ConditionalEvaluation condEval(CGF); 1922252723Sdim 1923224145Sdim // Zero-initialize it if we're not doing a copy-initialization. 1924224145Sdim bool shouldCopy = CRE->shouldCopy(); 1925224145Sdim if (!shouldCopy) { 1926224145Sdim llvm::Value *null = 1927224145Sdim llvm::ConstantPointerNull::get( 1928224145Sdim cast<llvm::PointerType>(destType->getElementType())); 1929224145Sdim CGF.Builder.CreateStore(null, temp); 1930224145Sdim } 1931252723Sdim 1932224145Sdim llvm::BasicBlock *contBB = 0; 1933252723Sdim llvm::BasicBlock *originBB = 0; 1934224145Sdim 1935224145Sdim // If the address is *not* known to be non-null, we need to switch. 1936224145Sdim llvm::Value *finalArgument; 1937224145Sdim 1938224145Sdim bool provablyNonNull = isProvablyNonNull(srcAddr); 1939224145Sdim if (provablyNonNull) { 1940224145Sdim finalArgument = temp; 1941224145Sdim } else { 1942224145Sdim llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1943224145Sdim 1944224145Sdim finalArgument = CGF.Builder.CreateSelect(isNull, 1945224145Sdim llvm::ConstantPointerNull::get(destType), 1946224145Sdim temp, "icr.argument"); 1947224145Sdim 1948224145Sdim // If we need to copy, then the load has to be conditional, which 1949224145Sdim // means we need control flow. 1950224145Sdim if (shouldCopy) { 1951252723Sdim originBB = CGF.Builder.GetInsertBlock(); 1952224145Sdim contBB = CGF.createBasicBlock("icr.cont"); 1953224145Sdim llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); 1954224145Sdim CGF.Builder.CreateCondBr(isNull, contBB, copyBB); 1955224145Sdim CGF.EmitBlock(copyBB); 1956252723Sdim condEval.begin(CGF); 1957224145Sdim } 1958224145Sdim } 1959224145Sdim 1960252723Sdim llvm::Value *valueToUse = 0; 1961252723Sdim 1962224145Sdim // Perform a copy if necessary. 1963224145Sdim if (shouldCopy) { 1964263509Sdim RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation()); 1965224145Sdim assert(srcRV.isScalar()); 1966224145Sdim 1967224145Sdim llvm::Value *src = srcRV.getScalarVal(); 1968224145Sdim src = CGF.Builder.CreateBitCast(src, destType->getElementType(), 1969224145Sdim "icr.cast"); 1970224145Sdim 1971224145Sdim // Use an ordinary store, not a store-to-lvalue. 1972224145Sdim CGF.Builder.CreateStore(src, temp); 1973252723Sdim 1974252723Sdim // If optimization is enabled, and the value was held in a 1975252723Sdim // __strong variable, we need to tell the optimizer that this 1976252723Sdim // value has to stay alive until we're doing the store back. 1977252723Sdim // This is because the temporary is effectively unretained, 1978252723Sdim // and so otherwise we can violate the high-level semantics. 1979252723Sdim if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 && 1980252723Sdim srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) { 1981252723Sdim valueToUse = src; 1982252723Sdim } 1983224145Sdim } 1984252723Sdim 1985224145Sdim // Finish the control flow if we needed it. 1986252723Sdim if (shouldCopy && !provablyNonNull) { 1987252723Sdim llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock(); 1988224145Sdim CGF.EmitBlock(contBB); 1989224145Sdim 1990252723Sdim // Make a phi for the value to intrinsically use. 1991252723Sdim if (valueToUse) { 1992252723Sdim llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2, 1993252723Sdim "icr.to-use"); 1994252723Sdim phiToUse->addIncoming(valueToUse, copyBB); 1995252723Sdim phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()), 1996252723Sdim originBB); 1997252723Sdim valueToUse = phiToUse; 1998252723Sdim } 1999252723Sdim 2000252723Sdim condEval.end(CGF); 2001252723Sdim } 2002252723Sdim 2003252723Sdim args.addWriteback(srcLV, temp, valueToUse); 2004224145Sdim args.add(RValue::get(finalArgument), CRE->getType()); 2005224145Sdim} 2006224145Sdim 2007221345Sdimvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, 2008221345Sdim QualType type) { 2009224145Sdim if (const ObjCIndirectCopyRestoreExpr *CRE 2010224145Sdim = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { 2011245431Sdim assert(getLangOpts().ObjCAutoRefCount); 2012224145Sdim assert(getContext().hasSameType(E->getType(), type)); 2013224145Sdim return emitWritebackArg(*this, args, CRE); 2014224145Sdim } 2015224145Sdim 2016226890Sdim assert(type->isReferenceType() == E->isGLValue() && 2017226890Sdim "reference binding to unmaterialized r-value!"); 2018226890Sdim 2019226890Sdim if (E->isGLValue()) { 2020226890Sdim assert(E->getObjectKind() == OK_Ordinary); 2021263509Sdim return args.add(EmitReferenceBindingToExpr(E), type); 2022226890Sdim } 2023198092Srdivacky 2024263509Sdim bool HasAggregateEvalKind = hasAggregateEvaluationKind(type); 2025263509Sdim 2026263509Sdim // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee. 2027263509Sdim // However, we still have to push an EH-only cleanup in case we unwind before 2028263509Sdim // we make it to the call. 2029263509Sdim if (HasAggregateEvalKind && 2030263509Sdim CGM.getTarget().getCXXABI().isArgumentDestroyedByCallee()) { 2031263509Sdim const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); 2032263509Sdim if (RD && RD->hasNonTrivialDestructor()) { 2033263509Sdim AggValueSlot Slot = CreateAggTemp(type, "agg.arg.tmp"); 2034263509Sdim Slot.setExternallyDestructed(); 2035263509Sdim EmitAggExpr(E, Slot); 2036263509Sdim RValue RV = Slot.asRValue(); 2037263509Sdim args.add(RV, type); 2038263509Sdim 2039263509Sdim pushDestroy(EHCleanup, RV.getAggregateAddr(), type, destroyCXXObject, 2040263509Sdim /*useEHCleanupForArray*/ true); 2041263509Sdim // This unreachable is a temporary marker which will be removed later. 2042263509Sdim llvm::Instruction *IsActive = Builder.CreateUnreachable(); 2043263509Sdim args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive); 2044263509Sdim return; 2045263509Sdim } 2046263509Sdim } 2047263509Sdim 2048263509Sdim if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) && 2049223017Sdim cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { 2050223017Sdim LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); 2051223017Sdim assert(L.isSimple()); 2052263509Sdim if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) { 2053263509Sdim args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); 2054263509Sdim } else { 2055263509Sdim // We can't represent a misaligned lvalue in the CallArgList, so copy 2056263509Sdim // to an aligned temporary now. 2057263509Sdim llvm::Value *tmp = CreateMemTemp(type); 2058263509Sdim EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(), 2059263509Sdim L.getAlignment()); 2060263509Sdim args.add(RValue::getAggregate(tmp), type); 2061263509Sdim } 2062223017Sdim return; 2063223017Sdim } 2064223017Sdim 2065221345Sdim args.add(EmitAnyExprToTemp(E), type); 2066193326Sed} 2067193326Sed 2068235633Sdim// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2069235633Sdim// optimizer it can aggressively ignore unwind edges. 2070235633Sdimvoid 2071235633SdimCodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { 2072235633Sdim if (CGM.getCodeGenOpts().OptimizationLevel != 0 && 2073235633Sdim !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) 2074235633Sdim Inst->setMetadata("clang.arc.no_objc_arc_exceptions", 2075235633Sdim CGM.getNoObjCARCExceptionsMetadata()); 2076235633Sdim} 2077235633Sdim 2078252723Sdim/// Emits a call to the given no-arguments nounwind runtime function. 2079252723Sdimllvm::CallInst * 2080252723SdimCodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, 2081252723Sdim const llvm::Twine &name) { 2082252723Sdim return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value*>(), name); 2083252723Sdim} 2084252723Sdim 2085252723Sdim/// Emits a call to the given nounwind runtime function. 2086252723Sdimllvm::CallInst * 2087252723SdimCodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, 2088252723Sdim ArrayRef<llvm::Value*> args, 2089252723Sdim const llvm::Twine &name) { 2090252723Sdim llvm::CallInst *call = EmitRuntimeCall(callee, args, name); 2091252723Sdim call->setDoesNotThrow(); 2092252723Sdim return call; 2093252723Sdim} 2094252723Sdim 2095252723Sdim/// Emits a simple call (never an invoke) to the given no-arguments 2096252723Sdim/// runtime function. 2097252723Sdimllvm::CallInst * 2098252723SdimCodeGenFunction::EmitRuntimeCall(llvm::Value *callee, 2099252723Sdim const llvm::Twine &name) { 2100252723Sdim return EmitRuntimeCall(callee, ArrayRef<llvm::Value*>(), name); 2101252723Sdim} 2102252723Sdim 2103252723Sdim/// Emits a simple call (never an invoke) to the given runtime 2104252723Sdim/// function. 2105252723Sdimllvm::CallInst * 2106252723SdimCodeGenFunction::EmitRuntimeCall(llvm::Value *callee, 2107252723Sdim ArrayRef<llvm::Value*> args, 2108252723Sdim const llvm::Twine &name) { 2109252723Sdim llvm::CallInst *call = Builder.CreateCall(callee, args, name); 2110252723Sdim call->setCallingConv(getRuntimeCC()); 2111252723Sdim return call; 2112252723Sdim} 2113252723Sdim 2114252723Sdim/// Emits a call or invoke to the given noreturn runtime function. 2115252723Sdimvoid CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, 2116252723Sdim ArrayRef<llvm::Value*> args) { 2117252723Sdim if (getInvokeDest()) { 2118252723Sdim llvm::InvokeInst *invoke = 2119252723Sdim Builder.CreateInvoke(callee, 2120252723Sdim getUnreachableBlock(), 2121252723Sdim getInvokeDest(), 2122252723Sdim args); 2123252723Sdim invoke->setDoesNotReturn(); 2124252723Sdim invoke->setCallingConv(getRuntimeCC()); 2125252723Sdim } else { 2126252723Sdim llvm::CallInst *call = Builder.CreateCall(callee, args); 2127252723Sdim call->setDoesNotReturn(); 2128252723Sdim call->setCallingConv(getRuntimeCC()); 2129252723Sdim Builder.CreateUnreachable(); 2130252723Sdim } 2131252723Sdim} 2132252723Sdim 2133252723Sdim/// Emits a call or invoke instruction to the given nullary runtime 2134252723Sdim/// function. 2135252723Sdimllvm::CallSite 2136252723SdimCodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, 2137252723Sdim const Twine &name) { 2138252723Sdim return EmitRuntimeCallOrInvoke(callee, ArrayRef<llvm::Value*>(), name); 2139252723Sdim} 2140252723Sdim 2141252723Sdim/// Emits a call or invoke instruction to the given runtime function. 2142252723Sdimllvm::CallSite 2143252723SdimCodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, 2144252723Sdim ArrayRef<llvm::Value*> args, 2145252723Sdim const Twine &name) { 2146252723Sdim llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name); 2147252723Sdim callSite.setCallingConv(getRuntimeCC()); 2148252723Sdim return callSite; 2149252723Sdim} 2150252723Sdim 2151252723Sdimllvm::CallSite 2152252723SdimCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 2153252723Sdim const Twine &Name) { 2154252723Sdim return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name); 2155252723Sdim} 2156252723Sdim 2157210299Sed/// Emits a call or invoke instruction to the given function, depending 2158210299Sed/// on the current state of the EH stack. 2159210299Sedllvm::CallSite 2160210299SedCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 2161226890Sdim ArrayRef<llvm::Value *> Args, 2162226890Sdim const Twine &Name) { 2163210299Sed llvm::BasicBlock *InvokeDest = getInvokeDest(); 2164235633Sdim 2165235633Sdim llvm::Instruction *Inst; 2166210299Sed if (!InvokeDest) 2167235633Sdim Inst = Builder.CreateCall(Callee, Args, Name); 2168235633Sdim else { 2169235633Sdim llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); 2170235633Sdim Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name); 2171235633Sdim EmitBlock(ContBB); 2172235633Sdim } 2173210299Sed 2174235633Sdim // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2175235633Sdim // optimizer it can aggressively ignore unwind edges. 2176235633Sdim if (CGM.getLangOpts().ObjCAutoRefCount) 2177235633Sdim AddObjCARCExceptionMetadata(Inst); 2178235633Sdim 2179235633Sdim return Inst; 2180210299Sed} 2181210299Sed 2182224145Sdimstatic void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo, 2183224145Sdim llvm::FunctionType *FTy) { 2184224145Sdim if (ArgNo < FTy->getNumParams()) 2185224145Sdim assert(Elt->getType() == FTy->getParamType(ArgNo)); 2186224145Sdim else 2187224145Sdim assert(FTy->isVarArg()); 2188224145Sdim ++ArgNo; 2189224145Sdim} 2190224145Sdim 2191224145Sdimvoid CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 2192263509Sdim SmallVectorImpl<llvm::Value *> &Args, 2193224145Sdim llvm::FunctionType *IRFuncTy) { 2194226890Sdim if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 2195226890Sdim unsigned NumElts = AT->getSize().getZExtValue(); 2196226890Sdim QualType EltTy = AT->getElementType(); 2197226890Sdim llvm::Value *Addr = RV.getAggregateAddr(); 2198226890Sdim for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 2199226890Sdim llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt); 2200263509Sdim RValue EltRV = convertTempToRValue(EltAddr, EltTy, SourceLocation()); 2201226890Sdim ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy); 2202226890Sdim } 2203235633Sdim } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 2204226890Sdim RecordDecl *RD = RT->getDecl(); 2205226890Sdim assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 2206235633Sdim LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty); 2207235633Sdim 2208235633Sdim if (RD->isUnion()) { 2209235633Sdim const FieldDecl *LargestFD = 0; 2210235633Sdim CharUnits UnionSize = CharUnits::Zero(); 2211235633Sdim 2212235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2213235633Sdim i != e; ++i) { 2214235633Sdim const FieldDecl *FD = *i; 2215235633Sdim assert(!FD->isBitField() && 2216235633Sdim "Cannot expand structure with bit-field members."); 2217235633Sdim CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 2218235633Sdim if (UnionSize < FieldSize) { 2219235633Sdim UnionSize = FieldSize; 2220235633Sdim LargestFD = FD; 2221235633Sdim } 2222235633Sdim } 2223235633Sdim if (LargestFD) { 2224263509Sdim RValue FldRV = EmitRValueForField(LV, LargestFD, SourceLocation()); 2225235633Sdim ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy); 2226235633Sdim } 2227235633Sdim } else { 2228235633Sdim for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2229235633Sdim i != e; ++i) { 2230235633Sdim FieldDecl *FD = *i; 2231235633Sdim 2232263509Sdim RValue FldRV = EmitRValueForField(LV, FD, SourceLocation()); 2233235633Sdim ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy); 2234235633Sdim } 2235224145Sdim } 2236235633Sdim } else if (Ty->isAnyComplexType()) { 2237226890Sdim ComplexPairTy CV = RV.getComplexVal(); 2238226890Sdim Args.push_back(CV.first); 2239226890Sdim Args.push_back(CV.second); 2240226890Sdim } else { 2241224145Sdim assert(RV.isScalar() && 2242224145Sdim "Unexpected non-scalar rvalue during struct expansion."); 2243224145Sdim 2244224145Sdim // Insert a bitcast as needed. 2245224145Sdim llvm::Value *V = RV.getScalarVal(); 2246224145Sdim if (Args.size() < IRFuncTy->getNumParams() && 2247224145Sdim V->getType() != IRFuncTy->getParamType(Args.size())) 2248224145Sdim V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size())); 2249224145Sdim 2250224145Sdim Args.push_back(V); 2251224145Sdim } 2252224145Sdim} 2253224145Sdim 2254224145Sdim 2255193326SedRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 2256198092Srdivacky llvm::Value *Callee, 2257201361Srdivacky ReturnValueSlot ReturnValue, 2258193326Sed const CallArgList &CallArgs, 2259207619Srdivacky const Decl *TargetDecl, 2260207619Srdivacky llvm::Instruction **callOrInvoke) { 2261193326Sed // FIXME: We no longer need the types from CallArgs; lift up and simplify. 2262226890Sdim SmallVector<llvm::Value*, 16> Args; 2263193326Sed 2264193326Sed // Handle struct-return functions by passing a pointer to the 2265193326Sed // location that we would like to return into. 2266193326Sed QualType RetTy = CallInfo.getReturnType(); 2267193326Sed const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 2268198092Srdivacky 2269224145Sdim // IRArgNo - Keep track of the argument number in the callee we're looking at. 2270224145Sdim unsigned IRArgNo = 0; 2271224145Sdim llvm::FunctionType *IRFuncTy = 2272224145Sdim cast<llvm::FunctionType>( 2273224145Sdim cast<llvm::PointerType>(Callee->getType())->getElementType()); 2274198092Srdivacky 2275194179Sed // If the call returns a temporary with struct return, create a temporary 2276201361Srdivacky // alloca to hold the result, unless one is given to us. 2277210299Sed if (CGM.ReturnTypeUsesSRet(CallInfo)) { 2278201361Srdivacky llvm::Value *Value = ReturnValue.getValue(); 2279201361Srdivacky if (!Value) 2280203955Srdivacky Value = CreateMemTemp(RetTy); 2281201361Srdivacky Args.push_back(Value); 2282224145Sdim checkArgMatches(Value, IRArgNo, IRFuncTy); 2283201361Srdivacky } 2284198092Srdivacky 2285193326Sed assert(CallInfo.arg_size() == CallArgs.size() && 2286193326Sed "Mismatch between function signature & arguments."); 2287193326Sed CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 2288198092Srdivacky for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 2289193326Sed I != E; ++I, ++info_it) { 2290193326Sed const ABIArgInfo &ArgInfo = info_it->info; 2291221345Sdim RValue RV = I->RV; 2292193326Sed 2293252723Sdim CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty); 2294245431Sdim 2295245431Sdim // Insert a padding argument to ensure proper alignment. 2296245431Sdim if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) { 2297245431Sdim Args.push_back(llvm::UndefValue::get(PaddingType)); 2298245431Sdim ++IRArgNo; 2299245431Sdim } 2300245431Sdim 2301193326Sed switch (ArgInfo.getKind()) { 2302212904Sdim case ABIArgInfo::Indirect: { 2303193326Sed if (RV.isScalar() || RV.isComplex()) { 2304193326Sed // Make a temporary alloca to pass the argument. 2305224145Sdim llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 2306224145Sdim if (ArgInfo.getIndirectAlign() > AI->getAlignment()) 2307224145Sdim AI->setAlignment(ArgInfo.getIndirectAlign()); 2308224145Sdim Args.push_back(AI); 2309252723Sdim 2310252723Sdim LValue argLV = 2311252723Sdim MakeAddrLValue(Args.back(), I->Ty, TypeAlign); 2312224145Sdim 2313193326Sed if (RV.isScalar()) 2314252723Sdim EmitStoreOfScalar(RV.getScalarVal(), argLV, /*init*/ true); 2315193326Sed else 2316252723Sdim EmitStoreOfComplex(RV.getComplexVal(), argLV, /*init*/ true); 2317224145Sdim 2318224145Sdim // Validate argument match. 2319224145Sdim checkArgMatches(AI, IRArgNo, IRFuncTy); 2320193326Sed } else { 2321224145Sdim // We want to avoid creating an unnecessary temporary+copy here; 2322252723Sdim // however, we need one in three cases: 2323224145Sdim // 1. If the argument is not byval, and we are required to copy the 2324224145Sdim // source. (This case doesn't occur on any common architecture.) 2325224145Sdim // 2. If the argument is byval, RV is not sufficiently aligned, and 2326224145Sdim // we cannot force it to be sufficiently aligned. 2327252723Sdim // 3. If the argument is byval, but RV is located in an address space 2328252723Sdim // different than that of the argument (0). 2329224145Sdim llvm::Value *Addr = RV.getAggregateAddr(); 2330224145Sdim unsigned Align = ArgInfo.getIndirectAlign(); 2331245431Sdim const llvm::DataLayout *TD = &CGM.getDataLayout(); 2332252723Sdim const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace(); 2333252723Sdim const unsigned ArgAddrSpace = (IRArgNo < IRFuncTy->getNumParams() ? 2334252723Sdim IRFuncTy->getParamType(IRArgNo)->getPointerAddressSpace() : 0); 2335224145Sdim if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || 2336252723Sdim (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align && 2337252723Sdim llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) || 2338252723Sdim (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) { 2339224145Sdim // Create an aligned temporary, and copy to it. 2340224145Sdim llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 2341224145Sdim if (Align > AI->getAlignment()) 2342224145Sdim AI->setAlignment(Align); 2343224145Sdim Args.push_back(AI); 2344224145Sdim EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); 2345224145Sdim 2346224145Sdim // Validate argument match. 2347224145Sdim checkArgMatches(AI, IRArgNo, IRFuncTy); 2348224145Sdim } else { 2349224145Sdim // Skip the extra memcpy call. 2350224145Sdim Args.push_back(Addr); 2351224145Sdim 2352224145Sdim // Validate argument match. 2353224145Sdim checkArgMatches(Addr, IRArgNo, IRFuncTy); 2354224145Sdim } 2355193326Sed } 2356193326Sed break; 2357212904Sdim } 2358193326Sed 2359212904Sdim case ABIArgInfo::Ignore: 2360212904Sdim break; 2361218893Sdim 2362193631Sed case ABIArgInfo::Extend: 2363212904Sdim case ABIArgInfo::Direct: { 2364212904Sdim if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && 2365212904Sdim ArgInfo.getCoerceToType() == ConvertType(info_it->type) && 2366212904Sdim ArgInfo.getDirectOffset() == 0) { 2367224145Sdim llvm::Value *V; 2368212904Sdim if (RV.isScalar()) 2369224145Sdim V = RV.getScalarVal(); 2370212904Sdim else 2371224145Sdim V = Builder.CreateLoad(RV.getAggregateAddr()); 2372224145Sdim 2373224145Sdim // If the argument doesn't match, perform a bitcast to coerce it. This 2374224145Sdim // can happen due to trivial type mismatches. 2375224145Sdim if (IRArgNo < IRFuncTy->getNumParams() && 2376224145Sdim V->getType() != IRFuncTy->getParamType(IRArgNo)) 2377224145Sdim V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo)); 2378224145Sdim Args.push_back(V); 2379224145Sdim 2380224145Sdim checkArgMatches(V, IRArgNo, IRFuncTy); 2381212904Sdim break; 2382193326Sed } 2383198092Srdivacky 2384193326Sed // FIXME: Avoid the conversion through memory if possible. 2385193326Sed llvm::Value *SrcPtr; 2386252723Sdim if (RV.isScalar() || RV.isComplex()) { 2387221345Sdim SrcPtr = CreateMemTemp(I->Ty, "coerce"); 2388252723Sdim LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign); 2389252723Sdim if (RV.isScalar()) { 2390252723Sdim EmitStoreOfScalar(RV.getScalarVal(), SrcLV, /*init*/ true); 2391252723Sdim } else { 2392252723Sdim EmitStoreOfComplex(RV.getComplexVal(), SrcLV, /*init*/ true); 2393252723Sdim } 2394198092Srdivacky } else 2395193326Sed SrcPtr = RV.getAggregateAddr(); 2396218893Sdim 2397212904Sdim // If the value is offset in memory, apply the offset now. 2398212904Sdim if (unsigned Offs = ArgInfo.getDirectOffset()) { 2399212904Sdim SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); 2400212904Sdim SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); 2401218893Sdim SrcPtr = Builder.CreateBitCast(SrcPtr, 2402212904Sdim llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); 2403212904Sdim 2404212904Sdim } 2405218893Sdim 2406210299Sed // If the coerce-to type is a first class aggregate, we flatten it and 2407210299Sed // pass the elements. Either way is semantically identical, but fast-isel 2408210299Sed // and the optimizer generally likes scalar values better than FCAs. 2409226890Sdim if (llvm::StructType *STy = 2410210299Sed dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { 2411245431Sdim llvm::Type *SrcTy = 2412245431Sdim cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 2413245431Sdim uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); 2414245431Sdim uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); 2415245431Sdim 2416245431Sdim // If the source type is smaller than the destination type of the 2417245431Sdim // coerce-to logic, copy the source value into a temp alloca the size 2418245431Sdim // of the destination type to allow loading all of it. The bits past 2419245431Sdim // the source value are left undef. 2420245431Sdim if (SrcSize < DstSize) { 2421245431Sdim llvm::AllocaInst *TempAlloca 2422245431Sdim = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce"); 2423245431Sdim Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0); 2424245431Sdim SrcPtr = TempAlloca; 2425245431Sdim } else { 2426245431Sdim SrcPtr = Builder.CreateBitCast(SrcPtr, 2427245431Sdim llvm::PointerType::getUnqual(STy)); 2428245431Sdim } 2429245431Sdim 2430210299Sed for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 2431210299Sed llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); 2432212904Sdim llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); 2433212904Sdim // We don't know what we're loading from. 2434212904Sdim LI->setAlignment(1); 2435212904Sdim Args.push_back(LI); 2436224145Sdim 2437224145Sdim // Validate argument match. 2438224145Sdim checkArgMatches(LI, IRArgNo, IRFuncTy); 2439210299Sed } 2440210299Sed } else { 2441210299Sed // In the simple case, just pass the coerced loaded value. 2442210299Sed Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), 2443210299Sed *this)); 2444224145Sdim 2445224145Sdim // Validate argument match. 2446224145Sdim checkArgMatches(Args.back(), IRArgNo, IRFuncTy); 2447210299Sed } 2448218893Sdim 2449193326Sed break; 2450193326Sed } 2451193326Sed 2452193326Sed case ABIArgInfo::Expand: 2453224145Sdim ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy); 2454224145Sdim IRArgNo = Args.size(); 2455193326Sed break; 2456193326Sed } 2457193326Sed } 2458198092Srdivacky 2459263509Sdim if (!CallArgs.getCleanupsToDeactivate().empty()) 2460263509Sdim deactivateArgCleanupsBeforeCall(*this, CallArgs); 2461263509Sdim 2462194179Sed // If the callee is a bitcast of a function to a varargs pointer to function 2463194179Sed // type, check to see if we can remove the bitcast. This handles some cases 2464194179Sed // with unprototyped functions. 2465194179Sed if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) 2466194179Sed if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { 2467226890Sdim llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); 2468226890Sdim llvm::FunctionType *CurFT = 2469194179Sed cast<llvm::FunctionType>(CurPT->getElementType()); 2470226890Sdim llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); 2471198092Srdivacky 2472194179Sed if (CE->getOpcode() == llvm::Instruction::BitCast && 2473194179Sed ActualFT->getReturnType() == CurFT->getReturnType() && 2474194711Sed ActualFT->getNumParams() == CurFT->getNumParams() && 2475221345Sdim ActualFT->getNumParams() == Args.size() && 2476221345Sdim (CurFT->isVarArg() || !ActualFT->isVarArg())) { 2477194179Sed bool ArgsMatch = true; 2478194179Sed for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) 2479194179Sed if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { 2480194179Sed ArgsMatch = false; 2481194179Sed break; 2482194179Sed } 2483198092Srdivacky 2484194179Sed // Strip the cast if we can get away with it. This is a nice cleanup, 2485194179Sed // but also allows us to inline the function at -O0 if it is marked 2486194179Sed // always_inline. 2487194179Sed if (ArgsMatch) 2488194179Sed Callee = CalleeF; 2489194179Sed } 2490194179Sed } 2491193326Sed 2492198092Srdivacky unsigned CallingConv; 2493193326Sed CodeGen::AttributeListType AttributeList; 2494252723Sdim CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, 2495252723Sdim CallingConv, true); 2496252723Sdim llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(), 2497252723Sdim AttributeList); 2498198092Srdivacky 2499210299Sed llvm::BasicBlock *InvokeDest = 0; 2500252723Sdim if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex, 2501252723Sdim llvm::Attribute::NoUnwind)) 2502210299Sed InvokeDest = getInvokeDest(); 2503210299Sed 2504193326Sed llvm::CallSite CS; 2505210299Sed if (!InvokeDest) { 2506224145Sdim CS = Builder.CreateCall(Callee, Args); 2507193326Sed } else { 2508193326Sed llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 2509224145Sdim CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args); 2510193326Sed EmitBlock(Cont); 2511193326Sed } 2512210299Sed if (callOrInvoke) 2513207619Srdivacky *callOrInvoke = CS.getInstruction(); 2514193326Sed 2515193326Sed CS.setAttributes(Attrs); 2516198092Srdivacky CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 2517193326Sed 2518235633Sdim // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2519235633Sdim // optimizer it can aggressively ignore unwind edges. 2520235633Sdim if (CGM.getLangOpts().ObjCAutoRefCount) 2521235633Sdim AddObjCARCExceptionMetadata(CS.getInstruction()); 2522235633Sdim 2523193326Sed // If the call doesn't return, finish the basic block and clear the 2524193326Sed // insertion point; this allows the rest of IRgen to discard 2525193326Sed // unreachable code. 2526193326Sed if (CS.doesNotReturn()) { 2527193326Sed Builder.CreateUnreachable(); 2528193326Sed Builder.ClearInsertionPoint(); 2529198092Srdivacky 2530193326Sed // FIXME: For now, emit a dummy basic block because expr emitters in 2531193326Sed // generally are not ready to handle emitting expressions at unreachable 2532193326Sed // points. 2533193326Sed EnsureInsertPoint(); 2534198092Srdivacky 2535193326Sed // Return a reasonable RValue. 2536193326Sed return GetUndefRValue(RetTy); 2537198092Srdivacky } 2538193326Sed 2539193326Sed llvm::Instruction *CI = CS.getInstruction(); 2540198092Srdivacky if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) 2541193326Sed CI->setName("call"); 2542193326Sed 2543224145Sdim // Emit any writebacks immediately. Arguably this should happen 2544224145Sdim // after any return-value munging. 2545224145Sdim if (CallArgs.hasWritebacks()) 2546224145Sdim emitWritebacks(*this, CallArgs); 2547224145Sdim 2548193326Sed switch (RetAI.getKind()) { 2549252723Sdim case ABIArgInfo::Indirect: 2550263509Sdim return convertTempToRValue(Args[0], RetTy, SourceLocation()); 2551193326Sed 2552193326Sed case ABIArgInfo::Ignore: 2553193326Sed // If we are ignoring an argument that had a result, make sure to 2554193326Sed // construct the appropriate return value for our caller. 2555193326Sed return GetUndefRValue(RetTy); 2556218893Sdim 2557212904Sdim case ABIArgInfo::Extend: 2558212904Sdim case ABIArgInfo::Direct: { 2559224145Sdim llvm::Type *RetIRTy = ConvertType(RetTy); 2560224145Sdim if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { 2561252723Sdim switch (getEvaluationKind(RetTy)) { 2562252723Sdim case TEK_Complex: { 2563212904Sdim llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 2564212904Sdim llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 2565212904Sdim return RValue::getComplex(std::make_pair(Real, Imag)); 2566212904Sdim } 2567252723Sdim case TEK_Aggregate: { 2568212904Sdim llvm::Value *DestPtr = ReturnValue.getValue(); 2569212904Sdim bool DestIsVolatile = ReturnValue.isVolatile(); 2570193326Sed 2571212904Sdim if (!DestPtr) { 2572212904Sdim DestPtr = CreateMemTemp(RetTy, "agg.tmp"); 2573212904Sdim DestIsVolatile = false; 2574212904Sdim } 2575223017Sdim BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); 2576212904Sdim return RValue::getAggregate(DestPtr); 2577212904Sdim } 2578252723Sdim case TEK_Scalar: { 2579252723Sdim // If the argument doesn't match, perform a bitcast to coerce it. This 2580252723Sdim // can happen due to trivial type mismatches. 2581252723Sdim llvm::Value *V = CI; 2582252723Sdim if (V->getType() != RetIRTy) 2583252723Sdim V = Builder.CreateBitCast(V, RetIRTy); 2584252723Sdim return RValue::get(V); 2585252723Sdim } 2586252723Sdim } 2587252723Sdim llvm_unreachable("bad evaluation kind"); 2588212904Sdim } 2589218893Sdim 2590201361Srdivacky llvm::Value *DestPtr = ReturnValue.getValue(); 2591201361Srdivacky bool DestIsVolatile = ReturnValue.isVolatile(); 2592218893Sdim 2593201361Srdivacky if (!DestPtr) { 2594203955Srdivacky DestPtr = CreateMemTemp(RetTy, "coerce"); 2595201361Srdivacky DestIsVolatile = false; 2596201361Srdivacky } 2597218893Sdim 2598212904Sdim // If the value is offset in memory, apply the offset now. 2599212904Sdim llvm::Value *StorePtr = DestPtr; 2600212904Sdim if (unsigned Offs = RetAI.getDirectOffset()) { 2601212904Sdim StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); 2602212904Sdim StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); 2603218893Sdim StorePtr = Builder.CreateBitCast(StorePtr, 2604212904Sdim llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 2605212904Sdim } 2606212904Sdim CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); 2607218893Sdim 2608263509Sdim return convertTempToRValue(DestPtr, RetTy, SourceLocation()); 2609193326Sed } 2610193326Sed 2611193326Sed case ABIArgInfo::Expand: 2612226890Sdim llvm_unreachable("Invalid ABI kind for return argument"); 2613193326Sed } 2614193326Sed 2615226890Sdim llvm_unreachable("Unhandled ABIArgInfo::Kind"); 2616193326Sed} 2617193326Sed 2618193326Sed/* VarArg handling */ 2619193326Sed 2620193326Sedllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 2621193326Sed return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 2622193326Sed} 2623