blob: 56004f364f1f223ed8039c78365dc0a0ff6eda98 [file] [log] [blame]
Nick Lewycky5fa40c32013-10-01 21:51:38 +00001//===--- CGCall.cpp - Encapsulate calling convention details --------------===//
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
Chris Lattnere70a0072010-06-29 16:40:28 +000016#include "ABIInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGCXXABI.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000018#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000019#include "CodeGenModule.h"
John McCalla729c622012-02-17 03:33:10 +000020#include "TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000021#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000023#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Basic/TargetInfo.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000025#include "clang/CodeGen/CGFunctionInfo.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Bill Wendling706469b2013-02-28 22:49:57 +000027#include "llvm/ADT/StringExtras.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000028#include "llvm/IR/Attributes.h"
Chandler Carruthc80ceea2014-03-04 11:02:08 +000029#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000030#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/InlineAsm.h"
Reid Kleckner314ef7b2014-02-01 00:04:45 +000032#include "llvm/IR/Intrinsics.h"
Eli Friedmanf7456192011-06-15 22:09:18 +000033#include "llvm/Transforms/Utils/Local.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000034using namespace clang;
35using namespace CodeGen;
36
37/***/
38
John McCallab26cfa2010-02-05 21:31:56 +000039static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
40 switch (CC) {
41 default: return llvm::CallingConv::C;
42 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
43 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000044 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Charles Davisb5a214e2013-08-30 04:39:01 +000045 case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64;
46 case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
Anton Korobeynikov231e8752011-04-14 20:06:49 +000047 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
48 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Guy Benyeif0a014b2012-12-25 08:53:55 +000049 case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
Dawn Perchik335e16b2010-09-03 01:29:35 +000050 // TODO: add support for CC_X86Pascal to llvm
John McCallab26cfa2010-02-05 21:31:56 +000051 }
52}
53
John McCall8ee376f2010-02-24 07:14:12 +000054/// Derives the 'this' type for codegen purposes, i.e. ignoring method
55/// qualification.
56/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000057static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
58 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
59 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000060}
61
John McCall8ee376f2010-02-24 07:14:12 +000062/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000063static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
64 return MD->getType()->getCanonicalTypeUnqualified()
65 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000066}
67
68/// Returns the "extra-canonicalized" return type, which discards
69/// qualifiers on the return type. Codegen doesn't care about them,
70/// and it makes ABI code a little easier to be able to assume that
71/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000072static CanQualType GetReturnType(QualType RetTy) {
73 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000074}
75
John McCall8dda7b22012-07-07 06:41:13 +000076/// Arrange the argument and result information for a value of the given
77/// unprototyped freestanding function type.
John McCall8ee376f2010-02-24 07:14:12 +000078const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +000079CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
John McCalla729c622012-02-17 03:33:10 +000080 // When translating an unprototyped function type, always use a
81 // variadic type.
Alp Toker314cc812014-01-25 16:55:45 +000082 return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
Reid Kleckner4982b822014-01-31 22:54:50 +000083 false, None, FTNP->getExtInfo(),
84 RequiredArgs(0));
John McCall8ee376f2010-02-24 07:14:12 +000085}
86
John McCall8dda7b22012-07-07 06:41:13 +000087/// Arrange the LLVM function layout for a value of the given function
88/// type, on top of any implicit parameters already stored. Use the
89/// given ExtInfo instead of the ExtInfo from the function type.
90static const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT,
Reid Kleckner4982b822014-01-31 22:54:50 +000091 bool IsInstanceMethod,
John McCall8dda7b22012-07-07 06:41:13 +000092 SmallVectorImpl<CanQualType> &prefix,
93 CanQual<FunctionProtoType> FTP,
94 FunctionType::ExtInfo extInfo) {
95 RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000096 // FIXME: Kill copy.
Alp Toker9cacbab2014-01-20 20:26:09 +000097 for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i)
98 prefix.push_back(FTP->getParamType(i));
Alp Toker314cc812014-01-25 16:55:45 +000099 CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
Reid Kleckner4982b822014-01-31 22:54:50 +0000100 return CGT.arrangeLLVMFunctionInfo(resultType, IsInstanceMethod, prefix,
101 extInfo, required);
John McCall8dda7b22012-07-07 06:41:13 +0000102}
103
104/// Arrange the argument and result information for a free function (i.e.
105/// not a C++ or ObjC instance method) of the given type.
106static const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT,
107 SmallVectorImpl<CanQualType> &prefix,
108 CanQual<FunctionProtoType> FTP) {
Reid Kleckner4982b822014-01-31 22:54:50 +0000109 return arrangeLLVMFunctionInfo(CGT, false, prefix, FTP, FTP->getExtInfo());
John McCall8dda7b22012-07-07 06:41:13 +0000110}
111
John McCall8dda7b22012-07-07 06:41:13 +0000112/// Arrange the argument and result information for a free function (i.e.
113/// not a C++ or ObjC instance method) of the given type.
114static const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT,
115 SmallVectorImpl<CanQualType> &prefix,
116 CanQual<FunctionProtoType> FTP) {
117 FunctionType::ExtInfo extInfo = FTP->getExtInfo();
Reid Kleckner4982b822014-01-31 22:54:50 +0000118 return arrangeLLVMFunctionInfo(CGT, true, prefix, FTP, extInfo);
John McCall8ee376f2010-02-24 07:14:12 +0000119}
120
John McCalla729c622012-02-17 03:33:10 +0000121/// Arrange the argument and result information for a value of the
John McCall8dda7b22012-07-07 06:41:13 +0000122/// given freestanding function type.
John McCall8ee376f2010-02-24 07:14:12 +0000123const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000124CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
John McCalla729c622012-02-17 03:33:10 +0000125 SmallVector<CanQualType, 16> argTypes;
John McCall8dda7b22012-07-07 06:41:13 +0000126 return ::arrangeFreeFunctionType(*this, argTypes, FTP);
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000127}
128
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000129static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000130 // Set the appropriate calling convention for the Function.
131 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000132 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000133
134 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000135 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000136
Douglas Gregora941dca2010-05-18 16:57:00 +0000137 if (D->hasAttr<ThisCallAttr>())
138 return CC_X86ThisCall;
139
Dawn Perchik335e16b2010-09-03 01:29:35 +0000140 if (D->hasAttr<PascalAttr>())
141 return CC_X86Pascal;
142
Anton Korobeynikov231e8752011-04-14 20:06:49 +0000143 if (PcsAttr *PCS = D->getAttr<PcsAttr>())
144 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
145
Derek Schuffa2020962012-10-16 22:30:41 +0000146 if (D->hasAttr<PnaclCallAttr>())
147 return CC_PnaclCall;
148
Guy Benyeif0a014b2012-12-25 08:53:55 +0000149 if (D->hasAttr<IntelOclBiccAttr>())
150 return CC_IntelOclBicc;
151
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000152 if (D->hasAttr<MSABIAttr>())
153 return IsWindows ? CC_C : CC_X86_64Win64;
154
155 if (D->hasAttr<SysVABIAttr>())
156 return IsWindows ? CC_X86_64SysV : CC_C;
157
John McCallab26cfa2010-02-05 21:31:56 +0000158 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000159}
160
James Molloy6f244b62014-05-09 16:21:39 +0000161static bool isAAPCSVFP(const CGFunctionInfo &FI, const TargetInfo &Target) {
162 switch (FI.getEffectiveCallingConvention()) {
163 case llvm::CallingConv::C:
164 switch (Target.getTriple().getEnvironment()) {
165 case llvm::Triple::EABIHF:
166 case llvm::Triple::GNUEABIHF:
167 return true;
168 default:
169 return false;
170 }
171 case llvm::CallingConv::ARM_AAPCS_VFP:
172 return true;
173 default:
174 return false;
175 }
176}
177
John McCalla729c622012-02-17 03:33:10 +0000178/// Arrange the argument and result information for a call to an
179/// unknown C++ non-static member function of the given abstract type.
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000180/// (Zero value of RD means we don't have any meaningful "this" argument type,
181/// so fall back to a generic pointer type).
John McCalla729c622012-02-17 03:33:10 +0000182/// The member function must be an ordinary function, i.e. not a
183/// constructor or destructor.
184const CGFunctionInfo &
185CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
186 const FunctionProtoType *FTP) {
187 SmallVector<CanQualType, 16> argTypes;
John McCall8ee376f2010-02-24 07:14:12 +0000188
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000189 // Add the 'this' pointer.
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000190 if (RD)
191 argTypes.push_back(GetThisType(Context, RD));
192 else
193 argTypes.push_back(Context.VoidPtrTy);
John McCall8ee376f2010-02-24 07:14:12 +0000194
John McCall8dda7b22012-07-07 06:41:13 +0000195 return ::arrangeCXXMethodType(*this, argTypes,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000196 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000197}
198
John McCalla729c622012-02-17 03:33:10 +0000199/// Arrange the argument and result information for a declaration or
200/// definition of the given C++ non-static member function. The
201/// member function must be an ordinary function, i.e. not a
202/// constructor or destructor.
203const CGFunctionInfo &
204CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
Benjamin Kramer60509af2013-09-09 14:48:42 +0000205 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
John McCall0d635f52010-09-03 01:26:39 +0000206 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
207
John McCalla729c622012-02-17 03:33:10 +0000208 CanQual<FunctionProtoType> prototype = GetFormalType(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000209
John McCalla729c622012-02-17 03:33:10 +0000210 if (MD->isInstance()) {
211 // The abstract case is perfectly fine.
Mark Lacey5ea993b2013-10-02 20:35:23 +0000212 const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000213 return arrangeCXXMethodType(ThisType, prototype.getTypePtr());
John McCalla729c622012-02-17 03:33:10 +0000214 }
215
John McCall8dda7b22012-07-07 06:41:13 +0000216 return arrangeFreeFunctionType(prototype);
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000217}
218
John McCalla729c622012-02-17 03:33:10 +0000219/// Arrange the argument and result information for a declaration
220/// or definition to the given constructor variant.
221const CGFunctionInfo &
222CodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D,
223 CXXCtorType ctorKind) {
224 SmallVector<CanQualType, 16> argTypes;
225 argTypes.push_back(GetThisType(Context, D->getParent()));
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000226
227 GlobalDecl GD(D, ctorKind);
228 CanQualType resultType =
229 TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000230
John McCall5d865c322010-08-31 07:33:07 +0000231 CanQual<FunctionProtoType> FTP = GetFormalType(D);
232
233 // Add the formal parameters.
Alp Toker9cacbab2014-01-20 20:26:09 +0000234 for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i)
235 argTypes.push_back(FTP->getParamType(i));
John McCall5d865c322010-08-31 07:33:07 +0000236
Reid Kleckner89077a12013-12-17 19:46:40 +0000237 TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes);
238
239 RequiredArgs required =
240 (D->isVariadic() ? RequiredArgs(argTypes.size()) : RequiredArgs::All);
241
John McCall8dda7b22012-07-07 06:41:13 +0000242 FunctionType::ExtInfo extInfo = FTP->getExtInfo();
Reid Kleckner4982b822014-01-31 22:54:50 +0000243 return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, required);
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000244}
245
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000246/// Arrange a call to a C++ method, passing the given arguments.
247const CGFunctionInfo &
248CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
249 const CXXConstructorDecl *D,
250 CXXCtorType CtorKind,
251 unsigned ExtraArgs) {
252 // FIXME: Kill copy.
253 SmallVector<CanQualType, 16> ArgTypes;
254 for (CallArgList::const_iterator i = args.begin(), e = args.end(); i != e;
255 ++i)
256 ArgTypes.push_back(Context.getCanonicalParamType(i->Ty));
257
258 CanQual<FunctionProtoType> FPT = GetFormalType(D);
259 RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs);
260 GlobalDecl GD(D, CtorKind);
261 CanQualType ResultType =
262 TheCXXABI.HasThisReturn(GD) ? ArgTypes.front() : Context.VoidTy;
263
264 FunctionType::ExtInfo Info = FPT->getExtInfo();
265 return arrangeLLVMFunctionInfo(ResultType, true, ArgTypes, Info, Required);
266}
267
John McCalla729c622012-02-17 03:33:10 +0000268/// Arrange the argument and result information for a declaration,
269/// definition, or call to the given destructor variant. It so
270/// happens that all three cases produce the same information.
271const CGFunctionInfo &
272CodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D,
273 CXXDtorType dtorKind) {
274 SmallVector<CanQualType, 2> argTypes;
275 argTypes.push_back(GetThisType(Context, D->getParent()));
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000276
277 GlobalDecl GD(D, dtorKind);
278 CanQualType resultType =
279 TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy;
John McCall8ee376f2010-02-24 07:14:12 +0000280
John McCalla729c622012-02-17 03:33:10 +0000281 TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes);
John McCall5d865c322010-08-31 07:33:07 +0000282
283 CanQual<FunctionProtoType> FTP = GetFormalType(D);
Alp Toker9cacbab2014-01-20 20:26:09 +0000284 assert(FTP->getNumParams() == 0 && "dtor with formal parameters");
Timur Iskhodzhanovc5098ad2012-07-12 09:50:54 +0000285 assert(FTP->isVariadic() == 0 && "dtor with formal parameters");
John McCall5d865c322010-08-31 07:33:07 +0000286
John McCall8dda7b22012-07-07 06:41:13 +0000287 FunctionType::ExtInfo extInfo = FTP->getExtInfo();
Reid Kleckner4982b822014-01-31 22:54:50 +0000288 return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo,
John McCall8dda7b22012-07-07 06:41:13 +0000289 RequiredArgs::All);
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000290}
291
John McCalla729c622012-02-17 03:33:10 +0000292/// Arrange the argument and result information for the declaration or
293/// definition of the given function.
294const CGFunctionInfo &
295CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000296 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000297 if (MD->isInstance())
John McCalla729c622012-02-17 03:33:10 +0000298 return arrangeCXXMethodDeclaration(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000299
John McCall2da83a32010-02-26 00:48:12 +0000300 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
John McCalla729c622012-02-17 03:33:10 +0000301
John McCall2da83a32010-02-26 00:48:12 +0000302 assert(isa<FunctionType>(FTy));
John McCalla729c622012-02-17 03:33:10 +0000303
304 // When declaring a function without a prototype, always use a
305 // non-variadic type.
306 if (isa<FunctionNoProtoType>(FTy)) {
307 CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
Reid Kleckner4982b822014-01-31 22:54:50 +0000308 return arrangeLLVMFunctionInfo(noProto->getReturnType(), false, None,
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000309 noProto->getExtInfo(), RequiredArgs::All);
John McCalla729c622012-02-17 03:33:10 +0000310 }
311
John McCall2da83a32010-02-26 00:48:12 +0000312 assert(isa<FunctionProtoType>(FTy));
John McCall8dda7b22012-07-07 06:41:13 +0000313 return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000314}
315
John McCalla729c622012-02-17 03:33:10 +0000316/// Arrange the argument and result information for the declaration or
317/// definition of an Objective-C method.
318const CGFunctionInfo &
319CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
320 // It happens that this is the same as a call with no optional
321 // arguments, except also using the formal 'self' type.
322 return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
323}
324
325/// Arrange the argument and result information for the function type
326/// through which to perform a send to the given Objective-C method,
327/// using the given receiver type. The receiver type is not always
328/// the 'self' type of the method or even an Objective-C pointer type.
329/// This is *not* the right method for actually performing such a
330/// message send, due to the possibility of optional arguments.
331const CGFunctionInfo &
332CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
333 QualType receiverType) {
334 SmallVector<CanQualType, 16> argTys;
335 argTys.push_back(Context.getCanonicalParamType(receiverType));
336 argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000337 // FIXME: Kill copy?
Aaron Ballman43b68be2014-03-07 17:50:17 +0000338 for (const auto *I : MD->params()) {
339 argTys.push_back(Context.getCanonicalParamType(I->getType()));
John McCall8ee376f2010-02-24 07:14:12 +0000340 }
John McCall31168b02011-06-15 23:02:42 +0000341
342 FunctionType::ExtInfo einfo;
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000343 bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
344 einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
John McCall31168b02011-06-15 23:02:42 +0000345
David Blaikiebbafb8a2012-03-11 07:00:24 +0000346 if (getContext().getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +0000347 MD->hasAttr<NSReturnsRetainedAttr>())
348 einfo = einfo.withProducesResult(true);
349
John McCalla729c622012-02-17 03:33:10 +0000350 RequiredArgs required =
351 (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
352
Reid Kleckner4982b822014-01-31 22:54:50 +0000353 return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()), false,
354 argTys, einfo, required);
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000355}
356
John McCalla729c622012-02-17 03:33:10 +0000357const CGFunctionInfo &
358CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
Anders Carlsson6710c532010-02-06 02:44:09 +0000359 // FIXME: Do we need to handle ObjCMethodDecl?
360 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000361
Anders Carlsson6710c532010-02-06 02:44:09 +0000362 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
John McCalla729c622012-02-17 03:33:10 +0000363 return arrangeCXXConstructorDeclaration(CD, GD.getCtorType());
Anders Carlsson6710c532010-02-06 02:44:09 +0000364
365 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
John McCalla729c622012-02-17 03:33:10 +0000366 return arrangeCXXDestructor(DD, GD.getDtorType());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000367
John McCalla729c622012-02-17 03:33:10 +0000368 return arrangeFunctionDeclaration(FD);
Anders Carlsson6710c532010-02-06 02:44:09 +0000369}
370
John McCallc818bbb2012-12-07 07:03:17 +0000371/// Arrange a call as unto a free function, except possibly with an
372/// additional number of formal parameters considered required.
373static const CGFunctionInfo &
374arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
Mark Lacey23455752013-10-10 20:57:00 +0000375 CodeGenModule &CGM,
John McCallc818bbb2012-12-07 07:03:17 +0000376 const CallArgList &args,
377 const FunctionType *fnType,
378 unsigned numExtraRequiredArgs) {
379 assert(args.size() >= numExtraRequiredArgs);
380
381 // In most cases, there are no optional arguments.
382 RequiredArgs required = RequiredArgs::All;
383
384 // If we have a variadic prototype, the required arguments are the
385 // extra prefix plus the arguments in the prototype.
386 if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
387 if (proto->isVariadic())
Alp Toker9cacbab2014-01-20 20:26:09 +0000388 required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs);
John McCallc818bbb2012-12-07 07:03:17 +0000389
390 // If we don't have a prototype at all, but we're supposed to
391 // explicitly use the variadic convention for unprototyped calls,
392 // treat all of the arguments as required but preserve the nominal
393 // possibility of variadics.
Mark Lacey23455752013-10-10 20:57:00 +0000394 } else if (CGM.getTargetCodeGenInfo()
395 .isNoProtoCallVariadic(args,
396 cast<FunctionNoProtoType>(fnType))) {
John McCallc818bbb2012-12-07 07:03:17 +0000397 required = RequiredArgs(args.size());
398 }
399
Alp Toker314cc812014-01-25 16:55:45 +0000400 return CGT.arrangeFreeFunctionCall(fnType->getReturnType(), args,
John McCallc818bbb2012-12-07 07:03:17 +0000401 fnType->getExtInfo(), required);
402}
403
John McCalla729c622012-02-17 03:33:10 +0000404/// Figure out the rules for calling a function with the given formal
405/// type using the given arguments. The arguments are necessary
406/// because the function might be unprototyped, in which case it's
407/// target-dependent in crazy ways.
408const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000409CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
410 const FunctionType *fnType) {
Mark Lacey23455752013-10-10 20:57:00 +0000411 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0);
John McCallc818bbb2012-12-07 07:03:17 +0000412}
John McCalla729c622012-02-17 03:33:10 +0000413
John McCallc818bbb2012-12-07 07:03:17 +0000414/// A block function call is essentially a free-function call with an
415/// extra implicit argument.
416const CGFunctionInfo &
417CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
418 const FunctionType *fnType) {
Mark Lacey23455752013-10-10 20:57:00 +0000419 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1);
John McCalla729c622012-02-17 03:33:10 +0000420}
421
422const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000423CodeGenTypes::arrangeFreeFunctionCall(QualType resultType,
424 const CallArgList &args,
425 FunctionType::ExtInfo info,
426 RequiredArgs required) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000427 // FIXME: Kill copy.
John McCalla729c622012-02-17 03:33:10 +0000428 SmallVector<CanQualType, 16> argTypes;
429 for (CallArgList::const_iterator i = args.begin(), e = args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000430 i != e; ++i)
John McCalla729c622012-02-17 03:33:10 +0000431 argTypes.push_back(Context.getCanonicalParamType(i->Ty));
Reid Kleckner4982b822014-01-31 22:54:50 +0000432 return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes,
433 info, required);
John McCall8dda7b22012-07-07 06:41:13 +0000434}
435
436/// Arrange a call to a C++ method, passing the given arguments.
437const CGFunctionInfo &
438CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
439 const FunctionProtoType *FPT,
440 RequiredArgs required) {
441 // FIXME: Kill copy.
442 SmallVector<CanQualType, 16> argTypes;
443 for (CallArgList::const_iterator i = args.begin(), e = args.end();
444 i != e; ++i)
445 argTypes.push_back(Context.getCanonicalParamType(i->Ty));
446
447 FunctionType::ExtInfo info = FPT->getExtInfo();
Reid Kleckner4982b822014-01-31 22:54:50 +0000448 return arrangeLLVMFunctionInfo(GetReturnType(FPT->getReturnType()), true,
449 argTypes, info, required);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000450}
451
Reid Kleckner4982b822014-01-31 22:54:50 +0000452const CGFunctionInfo &CodeGenTypes::arrangeFreeFunctionDeclaration(
453 QualType resultType, const FunctionArgList &args,
454 const FunctionType::ExtInfo &info, bool isVariadic) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000455 // FIXME: Kill copy.
John McCalla729c622012-02-17 03:33:10 +0000456 SmallVector<CanQualType, 16> argTypes;
457 for (FunctionArgList::const_iterator i = args.begin(), e = args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000458 i != e; ++i)
John McCalla729c622012-02-17 03:33:10 +0000459 argTypes.push_back(Context.getCanonicalParamType((*i)->getType()));
460
461 RequiredArgs required =
462 (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All);
Reid Kleckner4982b822014-01-31 22:54:50 +0000463 return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, info,
John McCall8dda7b22012-07-07 06:41:13 +0000464 required);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000465}
466
John McCalla729c622012-02-17 03:33:10 +0000467const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
Reid Kleckner4982b822014-01-31 22:54:50 +0000468 return arrangeLLVMFunctionInfo(getContext().VoidTy, false, None,
John McCall8dda7b22012-07-07 06:41:13 +0000469 FunctionType::ExtInfo(), RequiredArgs::All);
John McCalla738c252011-03-09 04:27:21 +0000470}
471
John McCalla729c622012-02-17 03:33:10 +0000472/// Arrange the argument and result information for an abstract value
473/// of a given function type. This is the method which all of the
474/// above functions ultimately defer to.
475const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000476CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
Reid Kleckner4982b822014-01-31 22:54:50 +0000477 bool IsInstanceMethod,
John McCall8dda7b22012-07-07 06:41:13 +0000478 ArrayRef<CanQualType> argTypes,
479 FunctionType::ExtInfo info,
480 RequiredArgs required) {
John McCall2da83a32010-02-26 00:48:12 +0000481#ifndef NDEBUG
John McCalla729c622012-02-17 03:33:10 +0000482 for (ArrayRef<CanQualType>::const_iterator
483 I = argTypes.begin(), E = argTypes.end(); I != E; ++I)
John McCall2da83a32010-02-26 00:48:12 +0000484 assert(I->isCanonicalAsParam());
485#endif
486
John McCalla729c622012-02-17 03:33:10 +0000487 unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000488
Daniel Dunbare0be8292009-02-03 00:07:12 +0000489 // Lookup or create unique function info.
490 llvm::FoldingSetNodeID ID;
Reid Kleckner4982b822014-01-31 22:54:50 +0000491 CGFunctionInfo::Profile(ID, IsInstanceMethod, info, required, resultType,
492 argTypes);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000493
John McCalla729c622012-02-17 03:33:10 +0000494 void *insertPos = 0;
495 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000496 if (FI)
497 return *FI;
498
John McCalla729c622012-02-17 03:33:10 +0000499 // Construct the function info. We co-allocate the ArgInfos.
Reid Kleckner4982b822014-01-31 22:54:50 +0000500 FI = CGFunctionInfo::create(CC, IsInstanceMethod, info, resultType, argTypes,
501 required);
John McCalla729c622012-02-17 03:33:10 +0000502 FunctionInfos.InsertNode(FI, insertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000503
John McCalla729c622012-02-17 03:33:10 +0000504 bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted;
505 assert(inserted && "Recursively being processed?");
Chris Lattner6fb0ccf2011-07-15 05:16:14 +0000506
Daniel Dunbar313321e2009-02-03 05:31:23 +0000507 // Compute ABI information.
Chris Lattner22326a12010-07-29 02:31:05 +0000508 getABIInfo().computeInfo(*FI);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000509
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000510 // Loop over all of the computed argument and return value info. If any of
511 // them are direct or extend without a specified coerce type, specify the
512 // default now.
John McCalla729c622012-02-17 03:33:10 +0000513 ABIArgInfo &retInfo = FI->getReturnInfo();
514 if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0)
515 retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000516
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000517 for (auto &I : FI->arguments())
518 if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == 0)
519 I.info.setCoerceToType(ConvertType(I.type));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000520
John McCalla729c622012-02-17 03:33:10 +0000521 bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
522 assert(erased && "Not in set?");
Chris Lattner1a651332011-07-15 06:41:05 +0000523
Daniel Dunbare0be8292009-02-03 00:07:12 +0000524 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000525}
526
John McCalla729c622012-02-17 03:33:10 +0000527CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
Reid Kleckner4982b822014-01-31 22:54:50 +0000528 bool IsInstanceMethod,
John McCalla729c622012-02-17 03:33:10 +0000529 const FunctionType::ExtInfo &info,
530 CanQualType resultType,
531 ArrayRef<CanQualType> argTypes,
532 RequiredArgs required) {
533 void *buffer = operator new(sizeof(CGFunctionInfo) +
534 sizeof(ArgInfo) * (argTypes.size() + 1));
535 CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
536 FI->CallingConvention = llvmCC;
537 FI->EffectiveCallingConvention = llvmCC;
538 FI->ASTCallingConvention = info.getCC();
Reid Kleckner4982b822014-01-31 22:54:50 +0000539 FI->InstanceMethod = IsInstanceMethod;
John McCalla729c622012-02-17 03:33:10 +0000540 FI->NoReturn = info.getNoReturn();
541 FI->ReturnsRetained = info.getProducesResult();
542 FI->Required = required;
543 FI->HasRegParm = info.getHasRegParm();
544 FI->RegParm = info.getRegParm();
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000545 FI->ArgStruct = 0;
John McCalla729c622012-02-17 03:33:10 +0000546 FI->NumArgs = argTypes.size();
547 FI->getArgsBuffer()[0].type = resultType;
548 for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
549 FI->getArgsBuffer()[i + 1].type = argTypes[i];
550 return FI;
Daniel Dunbar313321e2009-02-03 05:31:23 +0000551}
552
553/***/
554
John McCall85dd2c52011-05-15 02:19:42 +0000555void CodeGenTypes::GetExpandedTypes(QualType type,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000556 SmallVectorImpl<llvm::Type*> &expandedTypes) {
Bob Wilsone826a2a2011-08-03 05:58:22 +0000557 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) {
558 uint64_t NumElts = AT->getSize().getZExtValue();
559 for (uint64_t Elt = 0; Elt < NumElts; ++Elt)
560 GetExpandedTypes(AT->getElementType(), expandedTypes);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000561 } else if (const RecordType *RT = type->getAs<RecordType>()) {
Bob Wilsone826a2a2011-08-03 05:58:22 +0000562 const RecordDecl *RD = RT->getDecl();
563 assert(!RD->hasFlexibleArrayMember() &&
564 "Cannot expand structure with flexible array.");
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000565 if (RD->isUnion()) {
566 // Unions can be here only in degenerative cases - all the fields are same
567 // after flattening. Thus we have to use the "largest" field.
568 const FieldDecl *LargestFD = 0;
569 CharUnits UnionSize = CharUnits::Zero();
570
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000571 for (const auto *FD : RD->fields()) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000572 assert(!FD->isBitField() &&
573 "Cannot expand structure with bit-field members.");
574 CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
575 if (UnionSize < FieldSize) {
576 UnionSize = FieldSize;
577 LargestFD = FD;
578 }
579 }
580 if (LargestFD)
581 GetExpandedTypes(LargestFD->getType(), expandedTypes);
582 } else {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000583 for (const auto *I : RD->fields()) {
584 assert(!I->isBitField() &&
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000585 "Cannot expand structure with bit-field members.");
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000586 GetExpandedTypes(I->getType(), expandedTypes);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000587 }
Bob Wilsone826a2a2011-08-03 05:58:22 +0000588 }
589 } else if (const ComplexType *CT = type->getAs<ComplexType>()) {
590 llvm::Type *EltTy = ConvertType(CT->getElementType());
591 expandedTypes.push_back(EltTy);
592 expandedTypes.push_back(EltTy);
593 } else
594 expandedTypes.push_back(ConvertType(type));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000595}
596
Mike Stump11289f42009-09-09 15:08:12 +0000597llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000598CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
599 llvm::Function::arg_iterator AI) {
Mike Stump11289f42009-09-09 15:08:12 +0000600 assert(LV.isSimple() &&
601 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000602
Bob Wilsone826a2a2011-08-03 05:58:22 +0000603 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
604 unsigned NumElts = AT->getSize().getZExtValue();
605 QualType EltTy = AT->getElementType();
606 for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
Eli Friedman7f1ff602012-04-16 03:54:45 +0000607 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt);
Bob Wilsone826a2a2011-08-03 05:58:22 +0000608 LValue LV = MakeAddrLValue(EltAddr, EltTy);
609 AI = ExpandTypeFromArgs(EltTy, LV, AI);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000610 }
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000611 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Bob Wilsone826a2a2011-08-03 05:58:22 +0000612 RecordDecl *RD = RT->getDecl();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000613 if (RD->isUnion()) {
614 // Unions can be here only in degenerative cases - all the fields are same
615 // after flattening. Thus we have to use the "largest" field.
616 const FieldDecl *LargestFD = 0;
617 CharUnits UnionSize = CharUnits::Zero();
Bob Wilsone826a2a2011-08-03 05:58:22 +0000618
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000619 for (const auto *FD : RD->fields()) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000620 assert(!FD->isBitField() &&
621 "Cannot expand structure with bit-field members.");
622 CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
623 if (UnionSize < FieldSize) {
624 UnionSize = FieldSize;
625 LargestFD = FD;
626 }
627 }
628 if (LargestFD) {
629 // FIXME: What are the right qualifiers here?
Eli Friedman7f1ff602012-04-16 03:54:45 +0000630 LValue SubLV = EmitLValueForField(LV, LargestFD);
631 AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000632 }
633 } else {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000634 for (const auto *FD : RD->fields()) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000635 QualType FT = FD->getType();
636
637 // FIXME: What are the right qualifiers here?
Eli Friedman7f1ff602012-04-16 03:54:45 +0000638 LValue SubLV = EmitLValueForField(LV, FD);
639 AI = ExpandTypeFromArgs(FT, SubLV, AI);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000640 }
Bob Wilsone826a2a2011-08-03 05:58:22 +0000641 }
642 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
643 QualType EltTy = CT->getElementType();
Eli Friedman7f1ff602012-04-16 03:54:45 +0000644 llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real");
Bob Wilsone826a2a2011-08-03 05:58:22 +0000645 EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy));
Eli Friedman7f1ff602012-04-16 03:54:45 +0000646 llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag");
Bob Wilsone826a2a2011-08-03 05:58:22 +0000647 EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy));
648 } else {
649 EmitStoreThroughLValue(RValue::get(AI), LV);
650 ++AI;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000651 }
652
653 return AI;
654}
655
Chris Lattner895c52b2010-06-27 06:04:18 +0000656/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000657/// accessing some number of bytes out of it, try to gep into the struct to get
658/// at its inner goodness. Dive as deep as possible without entering an element
659/// with an in-memory size smaller than DstSize.
660static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000661EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
Chris Lattner2192fe52011-07-18 04:24:23 +0000662 llvm::StructType *SrcSTy,
Chris Lattner895c52b2010-06-27 06:04:18 +0000663 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000664 // We can't dive into a zero-element struct.
665 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000666
Chris Lattner2192fe52011-07-18 04:24:23 +0000667 llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000668
Chris Lattner1cd66982010-06-27 05:56:15 +0000669 // If the first elt is at least as large as what we're looking for, or if the
670 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000671 uint64_t FirstEltSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000672 CGF.CGM.getDataLayout().getTypeAllocSize(FirstElt);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000673 if (FirstEltSize < DstSize &&
Micah Villmowdd31ca12012-10-08 16:25:52 +0000674 FirstEltSize < CGF.CGM.getDataLayout().getTypeAllocSize(SrcSTy))
Chris Lattner1cd66982010-06-27 05:56:15 +0000675 return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000676
Chris Lattner1cd66982010-06-27 05:56:15 +0000677 // GEP into the first element.
678 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000679
Chris Lattner1cd66982010-06-27 05:56:15 +0000680 // If the first element is a struct, recurse.
Chris Lattner2192fe52011-07-18 04:24:23 +0000681 llvm::Type *SrcTy =
Chris Lattner1cd66982010-06-27 05:56:15 +0000682 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattner2192fe52011-07-18 04:24:23 +0000683 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000684 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000685
686 return SrcPtr;
687}
688
Chris Lattner055097f2010-06-27 06:26:04 +0000689/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
690/// are either integers or pointers. This does a truncation of the value if it
691/// is too large or a zero extension if it is too small.
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +0000692///
693/// This behaves as if the value were coerced through memory, so on big-endian
694/// targets the high bits are preserved in a truncation, while little-endian
695/// targets preserve the low bits.
Chris Lattner055097f2010-06-27 06:26:04 +0000696static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
Chris Lattner2192fe52011-07-18 04:24:23 +0000697 llvm::Type *Ty,
Chris Lattner055097f2010-06-27 06:26:04 +0000698 CodeGenFunction &CGF) {
699 if (Val->getType() == Ty)
700 return Val;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000701
Chris Lattner055097f2010-06-27 06:26:04 +0000702 if (isa<llvm::PointerType>(Val->getType())) {
703 // If this is Pointer->Pointer avoid conversion to and from int.
704 if (isa<llvm::PointerType>(Ty))
705 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000706
Chris Lattner055097f2010-06-27 06:26:04 +0000707 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000708 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000709 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000710
Chris Lattner2192fe52011-07-18 04:24:23 +0000711 llvm::Type *DestIntTy = Ty;
Chris Lattner055097f2010-06-27 06:26:04 +0000712 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000713 DestIntTy = CGF.IntPtrTy;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000714
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +0000715 if (Val->getType() != DestIntTy) {
716 const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
717 if (DL.isBigEndian()) {
718 // Preserve the high bits on big-endian targets.
719 // That is what memory coercion does.
James Molloy491cefb2014-05-07 17:41:15 +0000720 uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
721 uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
722
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +0000723 if (SrcSize > DstSize) {
724 Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
725 Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
726 } else {
727 Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
728 Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
729 }
730 } else {
731 // Little-endian targets preserve the low bits. No shifts required.
732 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
733 }
734 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000735
Chris Lattner055097f2010-06-27 06:26:04 +0000736 if (isa<llvm::PointerType>(Ty))
737 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
738 return Val;
739}
740
Chris Lattner1cd66982010-06-27 05:56:15 +0000741
742
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000743/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
744/// a pointer to an object of type \arg Ty.
745///
746/// This safely handles the case when the src type is smaller than the
747/// destination type; in this situation the values of bits which not
748/// present in the src are undefined.
749static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
Chris Lattner2192fe52011-07-18 04:24:23 +0000750 llvm::Type *Ty,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000751 CodeGenFunction &CGF) {
Chris Lattner2192fe52011-07-18 04:24:23 +0000752 llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000753 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000754
Chris Lattnerd200eda2010-06-28 22:51:39 +0000755 // If SrcTy and Ty are the same, just do a load.
756 if (SrcTy == Ty)
757 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000758
Micah Villmowdd31ca12012-10-08 16:25:52 +0000759 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000760
Chris Lattner2192fe52011-07-18 04:24:23 +0000761 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000762 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000763 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
764 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000765
Micah Villmowdd31ca12012-10-08 16:25:52 +0000766 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000767
Chris Lattner055097f2010-06-27 06:26:04 +0000768 // If the source and destination are integer or pointer types, just do an
769 // extension or truncation to the desired type.
770 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
771 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
772 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
773 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
774 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000775
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000776 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000777 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000778 // Generally SrcSize is never greater than DstSize, since this means we are
779 // losing bits. However, this can happen in cases where the structure has
780 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000781 //
Mike Stump18bb9282009-05-16 07:57:57 +0000782 // FIXME: Assert that we aren't truncating non-padding bits when have access
783 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000784 llvm::Value *Casted =
785 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000786 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
787 // FIXME: Use better alignment / avoid requiring aligned load.
788 Load->setAlignment(1);
789 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000790 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000791
Chris Lattner3fcc7902010-06-27 01:06:27 +0000792 // Otherwise do coercion through memory. This is stupid, but
793 // simple.
794 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Manman Ren84b921f2012-11-28 22:08:52 +0000795 llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
796 llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
797 llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy);
Manman Ren836a93b2012-11-28 22:29:41 +0000798 // FIXME: Use better alignment.
Manman Ren84b921f2012-11-28 22:08:52 +0000799 CGF.Builder.CreateMemCpy(Casted, SrcCasted,
800 llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
801 1, false);
Chris Lattner3fcc7902010-06-27 01:06:27 +0000802 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000803}
804
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000805// Function to store a first-class aggregate into memory. We prefer to
806// store the elements rather than the aggregate to be more friendly to
807// fast-isel.
808// FIXME: Do we need to recurse here?
809static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
810 llvm::Value *DestPtr, bool DestIsVolatile,
811 bool LowAlignment) {
812 // Prefer scalar stores to first-class aggregate stores.
Chris Lattner2192fe52011-07-18 04:24:23 +0000813 if (llvm::StructType *STy =
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000814 dyn_cast<llvm::StructType>(Val->getType())) {
815 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
816 llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
817 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
818 llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
819 DestIsVolatile);
820 if (LowAlignment)
821 SI->setAlignment(1);
822 }
823 } else {
Bill Wendlingf6af30f2012-03-16 21:45:12 +0000824 llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
825 if (LowAlignment)
826 SI->setAlignment(1);
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000827 }
828}
829
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000830/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
831/// where the source and destination may have different types.
832///
833/// This safely handles the case when the src type is larger than the
834/// destination type; the upper bits of the src will be lost.
835static void CreateCoercedStore(llvm::Value *Src,
836 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000837 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000838 CodeGenFunction &CGF) {
Chris Lattner2192fe52011-07-18 04:24:23 +0000839 llvm::Type *SrcTy = Src->getType();
840 llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000841 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000842 if (SrcTy == DstTy) {
843 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
844 return;
845 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000846
Micah Villmowdd31ca12012-10-08 16:25:52 +0000847 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000848
Chris Lattner2192fe52011-07-18 04:24:23 +0000849 if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000850 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
851 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
852 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000853
Chris Lattner055097f2010-06-27 06:26:04 +0000854 // If the source and destination are integer or pointer types, just do an
855 // extension or truncation to the desired type.
856 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
857 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
858 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
859 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
860 return;
861 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000862
Micah Villmowdd31ca12012-10-08 16:25:52 +0000863 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000864
Daniel Dunbar313321e2009-02-03 05:31:23 +0000865 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000866 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000867 llvm::Value *Casted =
868 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000869 // FIXME: Use better alignment / avoid requiring aligned store.
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000870 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000871 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000872 // Otherwise do coercion through memory. This is stupid, but
873 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000874
875 // Generally SrcSize is never greater than DstSize, since this means we are
876 // losing bits. However, this can happen in cases where the structure has
877 // additional padding, for example due to a user specified alignment.
878 //
879 // FIXME: Assert that we aren't truncating non-padding bits when have access
880 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000881 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
882 CGF.Builder.CreateStore(Src, Tmp);
Manman Ren84b921f2012-11-28 22:08:52 +0000883 llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
884 llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
885 llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy);
Manman Ren836a93b2012-11-28 22:29:41 +0000886 // FIXME: Use better alignment.
Manman Ren84b921f2012-11-28 22:08:52 +0000887 CGF.Builder.CreateMemCpy(DstCasted, Casted,
888 llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
889 1, false);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000890 }
891}
892
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000893/***/
894
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000895bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000896 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000897}
898
Tim Northovere77cc392014-03-29 13:28:05 +0000899bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
900 return ReturnTypeUsesSRet(FI) &&
901 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
902}
903
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000904bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
905 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
906 switch (BT->getKind()) {
907 default:
908 return false;
909 case BuiltinType::Float:
John McCallc8e01702013-04-16 22:48:15 +0000910 return getTarget().useObjCFPRetForRealType(TargetInfo::Float);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000911 case BuiltinType::Double:
John McCallc8e01702013-04-16 22:48:15 +0000912 return getTarget().useObjCFPRetForRealType(TargetInfo::Double);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000913 case BuiltinType::LongDouble:
John McCallc8e01702013-04-16 22:48:15 +0000914 return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000915 }
916 }
917
918 return false;
919}
920
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000921bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
922 if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
923 if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
924 if (BT->getKind() == BuiltinType::LongDouble)
John McCallc8e01702013-04-16 22:48:15 +0000925 return getTarget().useObjCFP2RetForComplexLongDouble();
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000926 }
927 }
928
929 return false;
930}
931
Chris Lattnera5f58b02011-07-09 17:41:47 +0000932llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
John McCalla729c622012-02-17 03:33:10 +0000933 const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
934 return GetFunctionType(FI);
John McCallf8ff7b92010-02-23 00:48:20 +0000935}
936
Chris Lattnera5f58b02011-07-09 17:41:47 +0000937llvm::FunctionType *
John McCalla729c622012-02-17 03:33:10 +0000938CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Chris Lattner6fb0ccf2011-07-15 05:16:14 +0000939
940 bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted;
941 assert(Inserted && "Recursively being processed?");
942
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000943 SmallVector<llvm::Type*, 8> argTypes;
Chris Lattner2192fe52011-07-18 04:24:23 +0000944 llvm::Type *resultType = 0;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000945
John McCall85dd2c52011-05-15 02:19:42 +0000946 const ABIArgInfo &retAI = FI.getReturnInfo();
947 switch (retAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000948 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +0000949 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbard3674e62008-09-11 01:48:57 +0000950
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000951 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000952 case ABIArgInfo::Direct:
John McCall85dd2c52011-05-15 02:19:42 +0000953 resultType = retAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +0000954 break;
955
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000956 case ABIArgInfo::InAlloca:
Reid Klecknerfab1e892014-02-25 00:59:14 +0000957 if (retAI.getInAllocaSRet()) {
958 // sret things on win32 aren't void, they return the sret pointer.
959 QualType ret = FI.getReturnType();
960 llvm::Type *ty = ConvertType(ret);
961 unsigned addressSpace = Context.getTargetAddressSpace(ret);
962 resultType = llvm::PointerType::get(ty, addressSpace);
963 } else {
964 resultType = llvm::Type::getVoidTy(getLLVMContext());
965 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000966 break;
967
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000968 case ABIArgInfo::Indirect: {
John McCall85dd2c52011-05-15 02:19:42 +0000969 assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
970 resultType = llvm::Type::getVoidTy(getLLVMContext());
971
972 QualType ret = FI.getReturnType();
Chris Lattner2192fe52011-07-18 04:24:23 +0000973 llvm::Type *ty = ConvertType(ret);
John McCall85dd2c52011-05-15 02:19:42 +0000974 unsigned addressSpace = Context.getTargetAddressSpace(ret);
975 argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000976 break;
977 }
978
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000979 case ABIArgInfo::Ignore:
John McCall85dd2c52011-05-15 02:19:42 +0000980 resultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000981 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983
John McCallc818bbb2012-12-07 07:03:17 +0000984 // Add in all of the required arguments.
985 CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), ie;
986 if (FI.isVariadic()) {
987 ie = it + FI.getRequiredArgs().getNumRequiredArgs();
988 } else {
989 ie = FI.arg_end();
990 }
991 for (; it != ie; ++it) {
John McCall85dd2c52011-05-15 02:19:42 +0000992 const ABIArgInfo &argAI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000993
Rafael Espindolafad28de2012-10-24 01:59:00 +0000994 // Insert a padding type to ensure proper alignment.
995 if (llvm::Type *PaddingType = argAI.getPaddingType())
996 argTypes.push_back(PaddingType);
997
John McCall85dd2c52011-05-15 02:19:42 +0000998 switch (argAI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000999 case ABIArgInfo::Ignore:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001000 case ABIArgInfo::InAlloca:
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001001 break;
1002
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001003 case ABIArgInfo::Indirect: {
1004 // indirect arguments are always on the stack, which is addr space #0.
Chris Lattner2192fe52011-07-18 04:24:23 +00001005 llvm::Type *LTy = ConvertTypeForMem(it->type);
John McCall85dd2c52011-05-15 02:19:42 +00001006 argTypes.push_back(LTy->getPointerTo());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001007 break;
1008 }
1009
1010 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +00001011 case ABIArgInfo::Direct: {
Chris Lattner3dd716c2010-06-28 23:44:11 +00001012 // If the coerce-to type is a first class aggregate, flatten it. Either
1013 // way is semantically identical, but fast-isel and the optimizer
1014 // generally likes scalar values better than FCAs.
James Molloy6f244b62014-05-09 16:21:39 +00001015 // We cannot do this for functions using the AAPCS calling convention,
1016 // as structures are treated differently by that calling convention.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001017 llvm::Type *argType = argAI.getCoerceToType();
James Molloy6f244b62014-05-09 16:21:39 +00001018 llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
1019 if (st && !isAAPCSVFP(FI, getTarget())) {
John McCall85dd2c52011-05-15 02:19:42 +00001020 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
1021 argTypes.push_back(st->getElementType(i));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001022 } else {
John McCall85dd2c52011-05-15 02:19:42 +00001023 argTypes.push_back(argType);
Chris Lattner3dd716c2010-06-28 23:44:11 +00001024 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001025 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +00001026 }
Mike Stump11289f42009-09-09 15:08:12 +00001027
Daniel Dunbard3674e62008-09-11 01:48:57 +00001028 case ABIArgInfo::Expand:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001029 GetExpandedTypes(it->type, argTypes);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001030 break;
1031 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001032 }
1033
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001034 // Add the inalloca struct as the last parameter type.
1035 if (llvm::StructType *ArgStruct = FI.getArgStruct())
1036 argTypes.push_back(ArgStruct->getPointerTo());
1037
Chris Lattner6fb0ccf2011-07-15 05:16:14 +00001038 bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1039 assert(Erased && "Not in set?");
1040
John McCalla729c622012-02-17 03:33:10 +00001041 return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic());
Daniel Dunbar81cf67f2008-09-09 23:48:28 +00001042}
1043
Chris Lattner2192fe52011-07-18 04:24:23 +00001044llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
John McCall5d865c322010-08-31 07:33:07 +00001045 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +00001046 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001047
Chris Lattner8806e322011-07-10 00:18:59 +00001048 if (!isFuncTypeConvertible(FPT))
1049 return llvm::StructType::get(getLLVMContext());
1050
1051 const CGFunctionInfo *Info;
1052 if (isa<CXXDestructorDecl>(MD))
John McCalla729c622012-02-17 03:33:10 +00001053 Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType());
Chris Lattner8806e322011-07-10 00:18:59 +00001054 else
John McCalla729c622012-02-17 03:33:10 +00001055 Info = &arrangeCXXMethodDeclaration(MD);
1056 return GetFunctionType(*Info);
Anders Carlsson64457732009-11-24 05:08:52 +00001057}
1058
Daniel Dunbar3668cb22009-02-02 23:43:58 +00001059void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +00001060 const Decl *TargetDecl,
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001061 AttributeListType &PAL,
Bill Wendlingf4d64cb2013-02-22 00:13:35 +00001062 unsigned &CallingConv,
1063 bool AttrOnCallSite) {
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001064 llvm::AttrBuilder FuncAttrs;
1065 llvm::AttrBuilder RetAttrs;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001066
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001067 CallingConv = FI.getEffectiveCallingConvention();
1068
John McCallab26cfa2010-02-05 21:31:56 +00001069 if (FI.isNoReturn())
Bill Wendling207f0532012-12-20 19:27:06 +00001070 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
John McCallab26cfa2010-02-05 21:31:56 +00001071
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001072 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001073 if (TargetDecl) {
Rafael Espindola2d21ab02011-10-12 19:51:18 +00001074 if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001075 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001076 if (TargetDecl->hasAttr<NoThrowAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001077 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Richard Smithdebc59d2013-01-30 05:45:05 +00001078 if (TargetDecl->hasAttr<NoReturnAttr>())
1079 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
Aaron Ballman7c19ab12014-02-22 16:59:24 +00001080 if (TargetDecl->hasAttr<NoDuplicateAttr>())
1081 FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
Richard Smithdebc59d2013-01-30 05:45:05 +00001082
1083 if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
John McCallbe349de2010-07-08 06:48:12 +00001084 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl31ad7542011-03-13 17:09:40 +00001085 if (FPT && FPT->isNothrow(getContext()))
Bill Wendling207f0532012-12-20 19:27:06 +00001086 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Richard Smith49af6292013-03-05 08:30:04 +00001087 // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
1088 // These attributes are not inherited by overloads.
1089 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
1090 if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
Richard Smithdebc59d2013-01-30 05:45:05 +00001091 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
John McCallbe349de2010-07-08 06:48:12 +00001092 }
1093
Eric Christopherbf005ec2011-08-15 22:38:22 +00001094 // 'const' and 'pure' attribute functions are also nounwind.
1095 if (TargetDecl->hasAttr<ConstAttr>()) {
Bill Wendling207f0532012-12-20 19:27:06 +00001096 FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1097 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Eric Christopherbf005ec2011-08-15 22:38:22 +00001098 } else if (TargetDecl->hasAttr<PureAttr>()) {
Bill Wendling207f0532012-12-20 19:27:06 +00001099 FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1100 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Eric Christopherbf005ec2011-08-15 22:38:22 +00001101 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001102 if (TargetDecl->hasAttr<MallocAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001103 RetAttrs.addAttribute(llvm::Attribute::NoAlias);
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001104 }
1105
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001106 if (CodeGenOpts.OptimizeSize)
Bill Wendling207f0532012-12-20 19:27:06 +00001107 FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
Quentin Colombet5ee5ca12012-10-26 00:29:48 +00001108 if (CodeGenOpts.OptimizeSize == 2)
Bill Wendling207f0532012-12-20 19:27:06 +00001109 FuncAttrs.addAttribute(llvm::Attribute::MinSize);
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001110 if (CodeGenOpts.DisableRedZone)
Bill Wendling207f0532012-12-20 19:27:06 +00001111 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001112 if (CodeGenOpts.NoImplicitFloat)
Bill Wendling207f0532012-12-20 19:27:06 +00001113 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
Reid Klecknerfb873af2014-04-10 22:59:13 +00001114 if (CodeGenOpts.EnableSegmentedStacks)
1115 FuncAttrs.addAttribute("split-stack");
Devang Patel6e467b12009-06-04 23:32:02 +00001116
Bill Wendling2f81db62013-02-22 20:53:29 +00001117 if (AttrOnCallSite) {
1118 // Attributes that should go on the call site only.
1119 if (!CodeGenOpts.SimplifyLibCalls)
1120 FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
Bill Wendling706469b2013-02-28 22:49:57 +00001121 } else {
1122 // Attributes that should go on the function, but not the call site.
Bill Wendling706469b2013-02-28 22:49:57 +00001123 if (!CodeGenOpts.DisableFPElim) {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001124 FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
Bill Wendling706469b2013-02-28 22:49:57 +00001125 } else if (CodeGenOpts.OmitLeafFramePointer) {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001126 FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
Bill Wendling17d1b6142013-08-22 21:16:51 +00001127 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
Bill Wendling706469b2013-02-28 22:49:57 +00001128 } else {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001129 FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
Bill Wendling17d1b6142013-08-22 21:16:51 +00001130 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
Bill Wendling706469b2013-02-28 22:49:57 +00001131 }
1132
Bill Wendlingdabafea2013-03-13 22:24:33 +00001133 FuncAttrs.addAttribute("less-precise-fpmad",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001134 llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001135 FuncAttrs.addAttribute("no-infs-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001136 llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001137 FuncAttrs.addAttribute("no-nans-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001138 llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001139 FuncAttrs.addAttribute("unsafe-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001140 llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001141 FuncAttrs.addAttribute("use-soft-float",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001142 llvm::toStringRef(CodeGenOpts.SoftFloat));
Bill Wendlingb3219722013-07-22 20:15:41 +00001143 FuncAttrs.addAttribute("stack-protector-buffer-size",
Bill Wendling021c8de2013-07-12 22:26:07 +00001144 llvm::utostr(CodeGenOpts.SSPBufferSize));
Bill Wendlinga9cc8c02013-07-25 00:32:41 +00001145
Bill Wendlingd8f49502013-08-01 21:41:02 +00001146 if (!CodeGenOpts.StackRealignment)
1147 FuncAttrs.addAttribute("no-realign-stack");
Bill Wendling985d1c52013-02-15 21:30:01 +00001148 }
1149
Daniel Dunbar3668cb22009-02-02 23:43:58 +00001150 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001151 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001152 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001153 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001154 case ABIArgInfo::Extend:
Jakob Stoklund Olesend7bf2932013-05-29 03:57:23 +00001155 if (RetTy->hasSignedIntegerRepresentation())
1156 RetAttrs.addAttribute(llvm::Attribute::SExt);
1157 else if (RetTy->hasUnsignedIntegerRepresentation())
1158 RetAttrs.addAttribute(llvm::Attribute::ZExt);
Jakob Stoklund Olesena3661142013-06-05 03:00:09 +00001159 // FALL THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +00001160 case ABIArgInfo::Direct:
Jakob Stoklund Olesena3661142013-06-05 03:00:09 +00001161 if (RetAI.getInReg())
1162 RetAttrs.addAttribute(llvm::Attribute::InReg);
1163 break;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001164 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001165 break;
1166
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001167 case ABIArgInfo::InAlloca: {
1168 // inalloca disables readnone and readonly
1169 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1170 .removeAttribute(llvm::Attribute::ReadNone);
1171 break;
1172 }
1173
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001174 case ABIArgInfo::Indirect: {
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001175 llvm::AttrBuilder SRETAttrs;
Bill Wendling207f0532012-12-20 19:27:06 +00001176 SRETAttrs.addAttribute(llvm::Attribute::StructRet);
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001177 if (RetAI.getInReg())
Bill Wendling207f0532012-12-20 19:27:06 +00001178 SRETAttrs.addAttribute(llvm::Attribute::InReg);
Bill Wendlinga7912f82012-10-10 07:36:56 +00001179 PAL.push_back(llvm::
Bill Wendling290d9522013-01-27 02:46:53 +00001180 AttributeSet::get(getLLVMContext(), Index, SRETAttrs));
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001181
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001182 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +00001183 // sret disables readnone and readonly
Bill Wendling207f0532012-12-20 19:27:06 +00001184 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1185 .removeAttribute(llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001186 break;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001187 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001188
Daniel Dunbard3674e62008-09-11 01:48:57 +00001189 case ABIArgInfo::Expand:
David Blaikie83d382b2011-09-23 05:06:16 +00001190 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001191 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001192
Bill Wendlinga7912f82012-10-10 07:36:56 +00001193 if (RetAttrs.hasAttributes())
1194 PAL.push_back(llvm::
Bill Wendling290d9522013-01-27 02:46:53 +00001195 AttributeSet::get(getLLVMContext(),
1196 llvm::AttributeSet::ReturnIndex,
1197 RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001198
Aaron Ballmanec47bc22014-03-17 18:10:01 +00001199 for (const auto &I : FI.arguments()) {
1200 QualType ParamType = I.type;
1201 const ABIArgInfo &AI = I.info;
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001202 llvm::AttrBuilder Attrs;
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001203
Rafael Espindolafad28de2012-10-24 01:59:00 +00001204 if (AI.getPaddingType()) {
Bill Wendling290d9522013-01-27 02:46:53 +00001205 if (AI.getPaddingInReg())
1206 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index,
1207 llvm::Attribute::InReg));
Rafael Espindolafad28de2012-10-24 01:59:00 +00001208 // Increment Index if there is padding.
1209 ++Index;
1210 }
1211
John McCall39ec71f2010-03-27 00:47:27 +00001212 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
1213 // have the corresponding parameter variable. It doesn't make
Daniel Dunbarcb2b3d02011-02-10 18:10:07 +00001214 // sense to do it here because parameters are so messed up.
Daniel Dunbard3674e62008-09-11 01:48:57 +00001215 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001216 case ABIArgInfo::Extend:
Douglas Gregor6ab2fa82011-05-20 16:38:50 +00001217 if (ParamType->isSignedIntegerOrEnumerationType())
Bill Wendling207f0532012-12-20 19:27:06 +00001218 Attrs.addAttribute(llvm::Attribute::SExt);
Douglas Gregor6ab2fa82011-05-20 16:38:50 +00001219 else if (ParamType->isUnsignedIntegerOrEnumerationType())
Bill Wendling207f0532012-12-20 19:27:06 +00001220 Attrs.addAttribute(llvm::Attribute::ZExt);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001221 // FALL THROUGH
James Molloy6f244b62014-05-09 16:21:39 +00001222 case ABIArgInfo::Direct: {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001223 if (AI.getInReg())
Bill Wendling207f0532012-12-20 19:27:06 +00001224 Attrs.addAttribute(llvm::Attribute::InReg);
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001225
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001226 // FIXME: handle sseregparm someday...
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001227
James Molloy6f244b62014-05-09 16:21:39 +00001228 llvm::StructType *STy =
1229 dyn_cast<llvm::StructType>(AI.getCoerceToType());
1230 if (!isAAPCSVFP(FI, getTarget()) && STy) {
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001231 unsigned Extra = STy->getNumElements()-1; // 1 will be added below.
Bill Wendlinga7912f82012-10-10 07:36:56 +00001232 if (Attrs.hasAttributes())
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001233 for (unsigned I = 0; I < Extra; ++I)
Bill Wendling290d9522013-01-27 02:46:53 +00001234 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index + I,
1235 Attrs));
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001236 Index += Extra;
1237 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001238 break;
James Molloy6f244b62014-05-09 16:21:39 +00001239 }
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001240 case ABIArgInfo::Indirect:
Rafael Espindola703c47f2012-10-19 05:04:37 +00001241 if (AI.getInReg())
Bill Wendling207f0532012-12-20 19:27:06 +00001242 Attrs.addAttribute(llvm::Attribute::InReg);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001243
Anders Carlsson20759ad2009-09-16 15:53:40 +00001244 if (AI.getIndirectByVal())
Bill Wendling207f0532012-12-20 19:27:06 +00001245 Attrs.addAttribute(llvm::Attribute::ByVal);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001246
Bill Wendlinga7912f82012-10-10 07:36:56 +00001247 Attrs.addAlignmentAttr(AI.getIndirectAlign());
1248
Daniel Dunbarc2304432009-03-18 19:51:01 +00001249 // byval disables readnone and readonly.
Bill Wendling207f0532012-12-20 19:27:06 +00001250 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1251 .removeAttribute(llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001252 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001253
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001254 case ABIArgInfo::Ignore:
1255 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +00001256 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001257
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001258 case ABIArgInfo::InAlloca:
1259 // inalloca disables readnone and readonly.
1260 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1261 .removeAttribute(llvm::Attribute::ReadNone);
1262 // Skip increment, no matching LLVM parameter.
1263 continue;
1264
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001265 case ABIArgInfo::Expand: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001266 SmallVector<llvm::Type*, 8> types;
Mike Stump18bb9282009-05-16 07:57:57 +00001267 // FIXME: This is rather inefficient. Do we ever actually need to do
1268 // anything here? The result should be just reconstructed on the other
1269 // side, so extension should be a non-issue.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001270 getTypes().GetExpandedTypes(ParamType, types);
John McCall85dd2c52011-05-15 02:19:42 +00001271 Index += types.size();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001272 continue;
1273 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001274 }
Mike Stump11289f42009-09-09 15:08:12 +00001275
Bill Wendlinga7912f82012-10-10 07:36:56 +00001276 if (Attrs.hasAttributes())
Bill Wendling290d9522013-01-27 02:46:53 +00001277 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001278 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001279 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001280
1281 // Add the inalloca attribute to the trailing inalloca parameter if present.
1282 if (FI.usesInAlloca()) {
1283 llvm::AttrBuilder Attrs;
1284 Attrs.addAttribute(llvm::Attribute::InAlloca);
1285 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
1286 }
1287
Bill Wendlinga7912f82012-10-10 07:36:56 +00001288 if (FuncAttrs.hasAttributes())
Bill Wendling4f0c0802012-10-15 07:31:59 +00001289 PAL.push_back(llvm::
Bill Wendling290d9522013-01-27 02:46:53 +00001290 AttributeSet::get(getLLVMContext(),
1291 llvm::AttributeSet::FunctionIndex,
1292 FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001293}
1294
John McCalla738c252011-03-09 04:27:21 +00001295/// An argument came in as a promoted argument; demote it back to its
1296/// declared type.
1297static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
1298 const VarDecl *var,
1299 llvm::Value *value) {
Chris Lattner2192fe52011-07-18 04:24:23 +00001300 llvm::Type *varType = CGF.ConvertType(var->getType());
John McCalla738c252011-03-09 04:27:21 +00001301
1302 // This can happen with promotions that actually don't change the
1303 // underlying type, like the enum promotions.
1304 if (value->getType() == varType) return value;
1305
1306 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
1307 && "unexpected promotion type");
1308
1309 if (isa<llvm::IntegerType>(varType))
1310 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
1311
1312 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
1313}
1314
Daniel Dunbard931a872009-02-02 22:03:45 +00001315void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1316 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +00001317 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +00001318 // If this is an implicit-return-zero function, go ahead and
1319 // initialize the return value. TODO: it might be nice to have
1320 // a more general mechanism for this that didn't require synthesized
1321 // return statements.
John McCalldec348f72013-05-03 07:33:41 +00001322 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
John McCallcaa19452009-07-28 01:00:58 +00001323 if (FD->hasImplicitReturnZero()) {
Alp Toker314cc812014-01-25 16:55:45 +00001324 QualType RetTy = FD->getReturnType().getUnqualifiedType();
Chris Lattner2192fe52011-07-18 04:24:23 +00001325 llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +00001326 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +00001327 Builder.CreateStore(Zero, ReturnValue);
1328 }
1329 }
1330
Mike Stump18bb9282009-05-16 07:57:57 +00001331 // FIXME: We no longer need the types from FunctionArgList; lift up and
1332 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +00001333
Daniel Dunbar613855c2008-09-09 23:27:19 +00001334 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1335 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001336
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001337 // If we're using inalloca, all the memory arguments are GEPs off of the last
1338 // parameter, which is a pointer to the complete memory area.
1339 llvm::Value *ArgStruct = 0;
1340 if (FI.usesInAlloca()) {
1341 llvm::Function::arg_iterator EI = Fn->arg_end();
1342 --EI;
1343 ArgStruct = EI;
1344 assert(ArgStruct->getType() == FI.getArgStruct()->getPointerTo());
1345 }
1346
Daniel Dunbar613855c2008-09-09 23:27:19 +00001347 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001348 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +00001349 AI->setName("agg.result");
Bill Wendlingce2f9c52013-01-23 06:15:10 +00001350 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1351 AI->getArgNo() + 1,
1352 llvm::Attribute::NoAlias));
Daniel Dunbar613855c2008-09-09 23:27:19 +00001353 ++AI;
1354 }
Mike Stump11289f42009-09-09 15:08:12 +00001355
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001356 // Track if we received the parameter as a pointer (indirect, byval, or
1357 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
1358 // into a local alloca for us.
1359 enum ValOrPointer { HaveValue = 0, HavePointer = 1 };
Reid Kleckner8ae16272014-02-01 00:23:22 +00001360 typedef llvm::PointerIntPair<llvm::Value *, 1> ValueAndIsPtr;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001361 SmallVector<ValueAndIsPtr, 16> ArgVals;
1362 ArgVals.reserve(Args.size());
1363
Reid Kleckner739756c2013-12-04 19:23:12 +00001364 // Create a pointer value for every parameter declaration. This usually
1365 // entails copying one or more LLVM IR arguments into an alloca. Don't push
1366 // any cleanups or do anything that might unwind. We do that separately, so
1367 // we can push the cleanups in the correct order for the ABI.
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001368 assert(FI.arg_size() == Args.size() &&
1369 "Mismatch between function signature & arguments.");
Devang Patel68a15252011-03-03 20:13:15 +00001370 unsigned ArgNo = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001371 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel68a15252011-03-03 20:13:15 +00001372 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1373 i != e; ++i, ++info_it, ++ArgNo) {
John McCalla738c252011-03-09 04:27:21 +00001374 const VarDecl *Arg = *i;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001375 QualType Ty = info_it->type;
1376 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001377
John McCalla738c252011-03-09 04:27:21 +00001378 bool isPromoted =
1379 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
1380
Rafael Espindolafad28de2012-10-24 01:59:00 +00001381 // Skip the dummy padding argument.
1382 if (ArgI.getPaddingType())
1383 ++AI;
1384
Daniel Dunbard3674e62008-09-11 01:48:57 +00001385 switch (ArgI.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001386 case ABIArgInfo::InAlloca: {
1387 llvm::Value *V = Builder.CreateStructGEP(
1388 ArgStruct, ArgI.getInAllocaFieldIndex(), Arg->getName());
1389 ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
1390 continue; // Don't increment AI!
1391 }
1392
Daniel Dunbar747865a2009-02-05 09:16:39 +00001393 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +00001394 llvm::Value *V = AI;
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00001395
John McCall47fb9502013-03-07 21:37:08 +00001396 if (!hasScalarEvaluationKind(Ty)) {
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00001397 // Aggregates and complex variables are accessed by reference. All we
1398 // need to do is realign the value, if requested
1399 if (ArgI.getIndirectRealign()) {
1400 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
1401
1402 // Copy from the incoming argument pointer to the temporary with the
1403 // appropriate alignment.
1404 //
1405 // FIXME: We should have a common utility for generating an aggregate
1406 // copy.
Chris Lattner2192fe52011-07-18 04:24:23 +00001407 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyck705ba072011-01-19 01:58:38 +00001408 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumidd634362011-03-10 14:02:21 +00001409 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
1410 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
1411 Builder.CreateMemCpy(Dst,
1412 Src,
Ken Dyck705ba072011-01-19 01:58:38 +00001413 llvm::ConstantInt::get(IntPtrTy,
1414 Size.getQuantity()),
Benjamin Krameracc6b4e2010-12-30 00:13:21 +00001415 ArgI.getIndirectAlign(),
1416 false);
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00001417 V = AlignedTemp;
1418 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001419 ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001420 } else {
1421 // Load scalar value from indirect argument.
Ken Dyck705ba072011-01-19 01:58:38 +00001422 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
Nick Lewycky2d84e842013-10-02 02:29:49 +00001423 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty,
1424 Arg->getLocStart());
John McCalla738c252011-03-09 04:27:21 +00001425
1426 if (isPromoted)
1427 V = emitArgumentDemotion(*this, Arg, V);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001428 ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001429 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001430 break;
1431 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001432
1433 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001434 case ABIArgInfo::Direct: {
Akira Hatanaka18334dd2012-01-09 19:08:06 +00001435
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001436 // If we have the trivial case, handle it with no muss and fuss.
1437 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001438 ArgI.getCoerceToType() == ConvertType(Ty) &&
1439 ArgI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001440 assert(AI != Fn->arg_end() && "Argument mismatch!");
1441 llvm::Value *V = AI;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001442
Bill Wendling507c3512012-10-16 05:23:44 +00001443 if (Arg->getType().isRestrictQualified())
Bill Wendlingce2f9c52013-01-23 06:15:10 +00001444 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1445 AI->getArgNo() + 1,
1446 llvm::Attribute::NoAlias));
John McCall39ec71f2010-03-27 00:47:27 +00001447
Chris Lattner7369c142011-07-20 06:29:00 +00001448 // Ensure the argument is the correct type.
1449 if (V->getType() != ArgI.getCoerceToType())
1450 V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
1451
John McCalla738c252011-03-09 04:27:21 +00001452 if (isPromoted)
1453 V = emitArgumentDemotion(*this, Arg, V);
Rafael Espindola8778c282012-11-29 16:09:03 +00001454
Nick Lewycky5fa40c32013-10-01 21:51:38 +00001455 if (const CXXMethodDecl *MD =
1456 dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001457 if (MD->isVirtual() && Arg == CXXABIThisDecl)
Nick Lewycky5fa40c32013-10-01 21:51:38 +00001458 V = CGM.getCXXABI().
1459 adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V);
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +00001460 }
1461
Rafael Espindola8778c282012-11-29 16:09:03 +00001462 // Because of merging of function types from multiple decls it is
1463 // possible for the type of an argument to not match the corresponding
1464 // type in the function type. Since we are codegening the callee
1465 // in here, add a cast to the argument type.
1466 llvm::Type *LTy = ConvertType(Arg->getType());
1467 if (V->getType() != LTy)
1468 V = Builder.CreateBitCast(V, LTy);
1469
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001470 ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001471 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +00001472 }
Mike Stump11289f42009-09-09 15:08:12 +00001473
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00001474 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001475
Chris Lattnerff941a62010-07-28 18:24:28 +00001476 // The alignment we need to use is the max of the requested alignment for
1477 // the argument plus the alignment required by our access code below.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001478 unsigned AlignmentToUse =
Micah Villmowdd31ca12012-10-08 16:25:52 +00001479 CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerff941a62010-07-28 18:24:28 +00001480 AlignmentToUse = std::max(AlignmentToUse,
1481 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001482
Chris Lattnerff941a62010-07-28 18:24:28 +00001483 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +00001484 llvm::Value *V = Alloca;
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001485 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001486
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001487 // If the value is offset in memory, apply the offset now.
1488 if (unsigned Offs = ArgI.getDirectOffset()) {
1489 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1490 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001491 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001492 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1493 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001494
Chris Lattner15ec3612010-06-29 00:06:42 +00001495 // If the coerce-to type is a first class aggregate, we flatten it and
1496 // pass the elements. Either way is semantically identical, but fast-isel
1497 // and the optimizer generally likes scalar values better than FCAs.
James Molloy6f244b62014-05-09 16:21:39 +00001498 // We cannot do this for functions using the AAPCS calling convention,
1499 // as structures are treated differently by that calling convention.
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00001500 llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
James Molloy6f244b62014-05-09 16:21:39 +00001501 if (!isAAPCSVFP(FI, getTarget()) && STy && STy->getNumElements() > 1) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00001502 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00001503 llvm::Type *DstTy =
1504 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00001505 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001506
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00001507 if (SrcSize <= DstSize) {
1508 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1509
1510 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1511 assert(AI != Fn->arg_end() && "Argument mismatch!");
1512 AI->setName(Arg->getName() + ".coerce" + Twine(i));
1513 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1514 Builder.CreateStore(AI++, EltPtr);
1515 }
1516 } else {
1517 llvm::AllocaInst *TempAlloca =
1518 CreateTempAlloca(ArgI.getCoerceToType(), "coerce");
1519 TempAlloca->setAlignment(AlignmentToUse);
1520 llvm::Value *TempV = TempAlloca;
1521
1522 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1523 assert(AI != Fn->arg_end() && "Argument mismatch!");
1524 AI->setName(Arg->getName() + ".coerce" + Twine(i));
1525 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i);
1526 Builder.CreateStore(AI++, EltPtr);
1527 }
1528
1529 Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse);
Chris Lattner15ec3612010-06-29 00:06:42 +00001530 }
1531 } else {
1532 // Simple case, just do a coerced store of the argument into the alloca.
1533 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +00001534 AI->setName(Arg->getName() + ".coerce");
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001535 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +00001536 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001537
1538
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001539 // Match to what EmitParmDecl is expecting for this type.
John McCall47fb9502013-03-07 21:37:08 +00001540 if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
Nick Lewycky2d84e842013-10-02 02:29:49 +00001541 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart());
John McCalla738c252011-03-09 04:27:21 +00001542 if (isPromoted)
1543 V = emitArgumentDemotion(*this, Arg, V);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001544 ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
1545 } else {
1546 ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00001547 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001548 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001549 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001550
1551 case ABIArgInfo::Expand: {
1552 // If this structure was expanded into multiple arguments then
1553 // we need to create a temporary and reconstruct it from the
1554 // arguments.
Eli Friedman3d9f47f2011-11-03 21:39:02 +00001555 llvm::AllocaInst *Alloca = CreateMemTemp(Ty);
Eli Friedmana0544d62011-12-03 04:14:32 +00001556 CharUnits Align = getContext().getDeclAlign(Arg);
1557 Alloca->setAlignment(Align.getQuantity());
1558 LValue LV = MakeAddrLValue(Alloca, Ty, Align);
Eli Friedman3d9f47f2011-11-03 21:39:02 +00001559 llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001560 ArgVals.push_back(ValueAndIsPtr(Alloca, HavePointer));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001561
1562 // Name the arguments used in expansion and increment AI.
1563 unsigned Index = 0;
1564 for (; AI != End; ++AI, ++Index)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001565 AI->setName(Arg->getName() + "." + Twine(Index));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001566 continue;
1567 }
1568
1569 case ABIArgInfo::Ignore:
1570 // Initialize the local variable appropriately.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001571 if (!hasScalarEvaluationKind(Ty)) {
1572 ArgVals.push_back(ValueAndIsPtr(CreateMemTemp(Ty), HavePointer));
1573 } else {
1574 llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
1575 ArgVals.push_back(ValueAndIsPtr(U, HaveValue));
1576 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001577
1578 // Skip increment, no matching LLVM parameter.
1579 continue;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001580 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001581
1582 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001583 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001584
1585 if (FI.usesInAlloca())
1586 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001587 assert(AI == Fn->arg_end() && "Argument mismatch!");
Reid Kleckner739756c2013-12-04 19:23:12 +00001588
1589 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
1590 for (int I = Args.size() - 1; I >= 0; --I)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001591 EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(),
1592 I + 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00001593 } else {
1594 for (unsigned I = 0, E = Args.size(); I != E; ++I)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001595 EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(),
1596 I + 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00001597 }
Daniel Dunbar613855c2008-09-09 23:27:19 +00001598}
1599
John McCallffa2c1a2012-01-29 07:46:59 +00001600static void eraseUnusedBitCasts(llvm::Instruction *insn) {
1601 while (insn->use_empty()) {
1602 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
1603 if (!bitcast) return;
1604
1605 // This is "safe" because we would have used a ConstantExpr otherwise.
1606 insn = cast<llvm::Instruction>(bitcast->getOperand(0));
1607 bitcast->eraseFromParent();
1608 }
1609}
1610
John McCall31168b02011-06-15 23:02:42 +00001611/// Try to emit a fused autorelease of a return result.
1612static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1613 llvm::Value *result) {
1614 // We must be immediately followed the cast.
1615 llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1616 if (BB->empty()) return 0;
1617 if (&BB->back() != result) return 0;
1618
Chris Lattner2192fe52011-07-18 04:24:23 +00001619 llvm::Type *resultType = result->getType();
John McCall31168b02011-06-15 23:02:42 +00001620
1621 // result is in a BasicBlock and is therefore an Instruction.
1622 llvm::Instruction *generator = cast<llvm::Instruction>(result);
1623
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001624 SmallVector<llvm::Instruction*,4> insnsToKill;
John McCall31168b02011-06-15 23:02:42 +00001625
1626 // Look for:
1627 // %generator = bitcast %type1* %generator2 to %type2*
1628 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1629 // We would have emitted this as a constant if the operand weren't
1630 // an Instruction.
1631 generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1632
1633 // Require the generator to be immediately followed by the cast.
1634 if (generator->getNextNode() != bitcast)
1635 return 0;
1636
1637 insnsToKill.push_back(bitcast);
1638 }
1639
1640 // Look for:
1641 // %generator = call i8* @objc_retain(i8* %originalResult)
1642 // or
1643 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1644 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1645 if (!call) return 0;
1646
1647 bool doRetainAutorelease;
1648
1649 if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1650 doRetainAutorelease = true;
1651 } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1652 .objc_retainAutoreleasedReturnValue) {
1653 doRetainAutorelease = false;
1654
John McCallcfa4e9b2012-09-07 23:30:50 +00001655 // If we emitted an assembly marker for this call (and the
1656 // ARCEntrypoints field should have been set if so), go looking
1657 // for that call. If we can't find it, we can't do this
1658 // optimization. But it should always be the immediately previous
1659 // instruction, unless we needed bitcasts around the call.
1660 if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) {
1661 llvm::Instruction *prev = call->getPrevNode();
1662 assert(prev);
1663 if (isa<llvm::BitCastInst>(prev)) {
1664 prev = prev->getPrevNode();
1665 assert(prev);
1666 }
1667 assert(isa<llvm::CallInst>(prev));
1668 assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
1669 CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker);
1670 insnsToKill.push_back(prev);
1671 }
John McCall31168b02011-06-15 23:02:42 +00001672 } else {
1673 return 0;
1674 }
1675
1676 result = call->getArgOperand(0);
1677 insnsToKill.push_back(call);
1678
1679 // Keep killing bitcasts, for sanity. Note that we no longer care
1680 // about precise ordering as long as there's exactly one use.
1681 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1682 if (!bitcast->hasOneUse()) break;
1683 insnsToKill.push_back(bitcast);
1684 result = bitcast->getOperand(0);
1685 }
1686
1687 // Delete all the unnecessary instructions, from latest to earliest.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001688 for (SmallVectorImpl<llvm::Instruction*>::iterator
John McCall31168b02011-06-15 23:02:42 +00001689 i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1690 (*i)->eraseFromParent();
1691
1692 // Do the fused retain/autorelease if we were asked to.
1693 if (doRetainAutorelease)
1694 result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1695
1696 // Cast back to the result type.
1697 return CGF.Builder.CreateBitCast(result, resultType);
1698}
1699
John McCallffa2c1a2012-01-29 07:46:59 +00001700/// If this is a +1 of the value of an immutable 'self', remove it.
1701static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
1702 llvm::Value *result) {
1703 // This is only applicable to a method with an immutable 'self'.
John McCallff755cd2012-07-31 00:33:55 +00001704 const ObjCMethodDecl *method =
1705 dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
John McCallffa2c1a2012-01-29 07:46:59 +00001706 if (!method) return 0;
1707 const VarDecl *self = method->getSelfDecl();
1708 if (!self->getType().isConstQualified()) return 0;
1709
1710 // Look for a retain call.
1711 llvm::CallInst *retainCall =
1712 dyn_cast<llvm::CallInst>(result->stripPointerCasts());
1713 if (!retainCall ||
1714 retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain)
1715 return 0;
1716
1717 // Look for an ordinary load of 'self'.
1718 llvm::Value *retainedValue = retainCall->getArgOperand(0);
1719 llvm::LoadInst *load =
1720 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
1721 if (!load || load->isAtomic() || load->isVolatile() ||
1722 load->getPointerOperand() != CGF.GetAddrOfLocalVar(self))
1723 return 0;
1724
1725 // Okay! Burn it all down. This relies for correctness on the
1726 // assumption that the retain is emitted as part of the return and
1727 // that thereafter everything is used "linearly".
1728 llvm::Type *resultType = result->getType();
1729 eraseUnusedBitCasts(cast<llvm::Instruction>(result));
1730 assert(retainCall->use_empty());
1731 retainCall->eraseFromParent();
1732 eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
1733
1734 return CGF.Builder.CreateBitCast(load, resultType);
1735}
1736
John McCall31168b02011-06-15 23:02:42 +00001737/// Emit an ARC autorelease of the result of a function.
John McCallffa2c1a2012-01-29 07:46:59 +00001738///
1739/// \return the value to actually return from the function
John McCall31168b02011-06-15 23:02:42 +00001740static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1741 llvm::Value *result) {
John McCallffa2c1a2012-01-29 07:46:59 +00001742 // If we're returning 'self', kill the initial retain. This is a
1743 // heuristic attempt to "encourage correctness" in the really unfortunate
1744 // case where we have a return of self during a dealloc and we desperately
1745 // need to avoid the possible autorelease.
1746 if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
1747 return self;
1748
John McCall31168b02011-06-15 23:02:42 +00001749 // At -O0, try to emit a fused retain/autorelease.
1750 if (CGF.shouldUseFusedARCCalls())
1751 if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1752 return fused;
1753
1754 return CGF.EmitARCAutoreleaseReturnValue(result);
1755}
1756
John McCall6e1c0122012-01-29 02:35:02 +00001757/// Heuristically search for a dominating store to the return-value slot.
1758static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
1759 // If there are multiple uses of the return-value slot, just check
1760 // for something immediately preceding the IP. Sometimes this can
1761 // happen with how we generate implicit-returns; it can also happen
1762 // with noreturn cleanups.
1763 if (!CGF.ReturnValue->hasOneUse()) {
1764 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1765 if (IP->empty()) return 0;
1766 llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back());
1767 if (!store) return 0;
1768 if (store->getPointerOperand() != CGF.ReturnValue) return 0;
1769 assert(!store->isAtomic() && !store->isVolatile()); // see below
1770 return store;
1771 }
1772
1773 llvm::StoreInst *store =
Chandler Carruth4d01fff2014-03-09 03:16:50 +00001774 dyn_cast<llvm::StoreInst>(CGF.ReturnValue->user_back());
John McCall6e1c0122012-01-29 02:35:02 +00001775 if (!store) return 0;
1776
1777 // These aren't actually possible for non-coerced returns, and we
1778 // only care about non-coerced returns on this code path.
1779 assert(!store->isAtomic() && !store->isVolatile());
1780
1781 // Now do a first-and-dirty dominance check: just walk up the
1782 // single-predecessors chain from the current insertion point.
1783 llvm::BasicBlock *StoreBB = store->getParent();
1784 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1785 while (IP != StoreBB) {
1786 if (!(IP = IP->getSinglePredecessor()))
1787 return 0;
1788 }
1789
1790 // Okay, the store's basic block dominates the insertion point; we
1791 // can do our thing.
1792 return store;
1793}
1794
Adrian Prantl3be10542013-05-02 17:30:20 +00001795void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001796 bool EmitRetDbgLoc,
1797 SourceLocation EndLoc) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001798 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +00001799 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001800 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +00001801 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001802 }
Daniel Dunbar6696e222010-06-30 21:27:58 +00001803
Dan Gohman481e40c2010-07-20 20:13:52 +00001804 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +00001805 llvm::Value *RV = 0;
1806 QualType RetTy = FI.getReturnType();
1807 const ABIArgInfo &RetAI = FI.getReturnInfo();
1808
1809 switch (RetAI.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001810 case ABIArgInfo::InAlloca:
Reid Klecknerfab1e892014-02-25 00:59:14 +00001811 // Aggregrates get evaluated directly into the destination. Sometimes we
1812 // need to return the sret value in a register, though.
1813 assert(hasAggregateEvaluationKind(RetTy));
1814 if (RetAI.getInAllocaSRet()) {
1815 llvm::Function::arg_iterator EI = CurFn->arg_end();
1816 --EI;
1817 llvm::Value *ArgStruct = EI;
1818 llvm::Value *SRet =
1819 Builder.CreateStructGEP(ArgStruct, RetAI.getInAllocaFieldIndex());
1820 RV = Builder.CreateLoad(SRet, "sret");
1821 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001822 break;
1823
Daniel Dunbar03816342010-08-21 02:24:36 +00001824 case ABIArgInfo::Indirect: {
John McCall47fb9502013-03-07 21:37:08 +00001825 switch (getEvaluationKind(RetTy)) {
1826 case TEK_Complex: {
1827 ComplexPairTy RT =
Nick Lewycky2d84e842013-10-02 02:29:49 +00001828 EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy),
1829 EndLoc);
John McCall47fb9502013-03-07 21:37:08 +00001830 EmitStoreOfComplex(RT,
1831 MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
1832 /*isInit*/ true);
1833 break;
1834 }
1835 case TEK_Aggregate:
Chris Lattner726b3d02010-06-26 23:13:19 +00001836 // Do nothing; aggregrates get evaluated directly into the destination.
John McCall47fb9502013-03-07 21:37:08 +00001837 break;
1838 case TEK_Scalar:
1839 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
1840 MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
1841 /*isInit*/ true);
1842 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001843 }
1844 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001845 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001846
1847 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001848 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001849 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1850 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001851 // The internal return value temp always will have pointer-to-return-type
1852 // type, just do a load.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001853
John McCall6e1c0122012-01-29 02:35:02 +00001854 // If there is a dominating store to ReturnValue, we can elide
1855 // the load, zap the store, and usually zap the alloca.
1856 if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
Adrian Prantl4c9a38a2013-05-30 18:12:23 +00001857 // Reuse the debug location from the store unless there is
1858 // cleanup code to be emitted between the store and return
1859 // instruction.
1860 if (EmitRetDbgLoc && !AutoreleaseResult)
Adrian Prantl3be10542013-05-02 17:30:20 +00001861 RetDbgLoc = SI->getDebugLoc();
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001862 // Get the stored value and nuke the now-dead store.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001863 RV = SI->getValueOperand();
1864 SI->eraseFromParent();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001865
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001866 // If that was the only use of the return value, nuke it as well now.
1867 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1868 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1869 ReturnValue = 0;
1870 }
John McCall6e1c0122012-01-29 02:35:02 +00001871
1872 // Otherwise, we have to do a simple load.
1873 } else {
1874 RV = Builder.CreateLoad(ReturnValue);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001875 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001876 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001877 llvm::Value *V = ReturnValue;
1878 // If the value is offset in memory, apply the offset now.
1879 if (unsigned Offs = RetAI.getDirectOffset()) {
1880 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1881 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001882 V = Builder.CreateBitCast(V,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001883 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1884 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001885
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001886 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001887 }
John McCall31168b02011-06-15 23:02:42 +00001888
1889 // In ARC, end functions that return a retainable type with a call
1890 // to objc_autoreleaseReturnValue.
1891 if (AutoreleaseResult) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001892 assert(getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001893 !FI.isReturnsRetained() &&
1894 RetTy->isObjCRetainableType());
1895 RV = emitAutoreleaseOfResult(*this, RV);
1896 }
1897
Chris Lattner726b3d02010-06-26 23:13:19 +00001898 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001899
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001900 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00001901 break;
1902
1903 case ABIArgInfo::Expand:
David Blaikie83d382b2011-09-23 05:06:16 +00001904 llvm_unreachable("Invalid ABI kind for return argument");
Chris Lattner726b3d02010-06-26 23:13:19 +00001905 }
1906
Daniel Dunbar6696e222010-06-30 21:27:58 +00001907 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001908 if (!RetDbgLoc.isUnknown())
1909 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001910}
1911
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001912static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
1913 const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
1914 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
1915}
1916
1917static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty) {
1918 // FIXME: Generate IR in one pass, rather than going back and fixing up these
1919 // placeholders.
1920 llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
1921 llvm::Value *Placeholder =
1922 llvm::UndefValue::get(IRTy->getPointerTo()->getPointerTo());
1923 Placeholder = CGF.Builder.CreateLoad(Placeholder);
1924 return AggValueSlot::forAddr(Placeholder, CharUnits::Zero(),
1925 Ty.getQualifiers(),
1926 AggValueSlot::IsNotDestructed,
1927 AggValueSlot::DoesNotNeedGCBarriers,
1928 AggValueSlot::IsNotAliased);
1929}
1930
John McCall32ea9692011-03-11 20:59:21 +00001931void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
Nick Lewycky2d84e842013-10-02 02:29:49 +00001932 const VarDecl *param,
1933 SourceLocation loc) {
John McCall23f66262010-05-26 22:34:26 +00001934 // StartFunction converted the ABI-lowered parameter(s) into a
1935 // local alloca. We need to turn that into an r-value suitable
1936 // for EmitCall.
John McCall32ea9692011-03-11 20:59:21 +00001937 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall23f66262010-05-26 22:34:26 +00001938
John McCall32ea9692011-03-11 20:59:21 +00001939 QualType type = param->getType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001940
John McCall23f66262010-05-26 22:34:26 +00001941 // For the most part, we just need to load the alloca, except:
1942 // 1) aggregate r-values are actually pointers to temporaries, and
John McCall47fb9502013-03-07 21:37:08 +00001943 // 2) references to non-scalars are pointers directly to the aggregate.
1944 // I don't know why references to scalars are different here.
John McCall32ea9692011-03-11 20:59:21 +00001945 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
John McCall47fb9502013-03-07 21:37:08 +00001946 if (!hasScalarEvaluationKind(ref->getPointeeType()))
John McCall32ea9692011-03-11 20:59:21 +00001947 return args.add(RValue::getAggregate(local), type);
John McCall23f66262010-05-26 22:34:26 +00001948
1949 // Locals which are references to scalars are represented
1950 // with allocas holding the pointer.
John McCall32ea9692011-03-11 20:59:21 +00001951 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall23f66262010-05-26 22:34:26 +00001952 }
1953
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001954 if (isInAllocaArgument(CGM.getCXXABI(), type)) {
1955 AggValueSlot Slot = createPlaceholderSlot(*this, type);
1956 Slot.setExternallyDestructed();
1957
1958 // FIXME: Either emit a copy constructor call, or figure out how to do
1959 // guaranteed tail calls with perfect forwarding in LLVM.
1960 CGM.ErrorUnsupported(param, "non-trivial argument copy for thunk");
1961 EmitNullInitialization(Slot.getAddr(), type);
1962
1963 RValue RV = Slot.asRValue();
1964 args.add(RV, type);
1965 return;
1966 }
1967
Nick Lewycky2d84e842013-10-02 02:29:49 +00001968 args.add(convertTempToRValue(local, type, loc), type);
John McCall23f66262010-05-26 22:34:26 +00001969}
1970
John McCall31168b02011-06-15 23:02:42 +00001971static bool isProvablyNull(llvm::Value *addr) {
1972 return isa<llvm::ConstantPointerNull>(addr);
1973}
1974
1975static bool isProvablyNonNull(llvm::Value *addr) {
1976 return isa<llvm::AllocaInst>(addr);
1977}
1978
1979/// Emit the actual writing-back of a writeback.
1980static void emitWriteback(CodeGenFunction &CGF,
1981 const CallArgList::Writeback &writeback) {
John McCalleff18842013-03-23 02:35:54 +00001982 const LValue &srcLV = writeback.Source;
1983 llvm::Value *srcAddr = srcLV.getAddress();
John McCall31168b02011-06-15 23:02:42 +00001984 assert(!isProvablyNull(srcAddr) &&
1985 "shouldn't have writeback for provably null argument");
1986
1987 llvm::BasicBlock *contBB = 0;
1988
1989 // If the argument wasn't provably non-null, we need to null check
1990 // before doing the store.
1991 bool provablyNonNull = isProvablyNonNull(srcAddr);
1992 if (!provablyNonNull) {
1993 llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1994 contBB = CGF.createBasicBlock("icr.done");
1995
1996 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1997 CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1998 CGF.EmitBlock(writebackBB);
1999 }
2000
2001 // Load the value to writeback.
2002 llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
2003
2004 // Cast it back, in case we're writing an id to a Foo* or something.
2005 value = CGF.Builder.CreateBitCast(value,
2006 cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
2007 "icr.writeback-cast");
2008
2009 // Perform the writeback.
John McCalleff18842013-03-23 02:35:54 +00002010
2011 // If we have a "to use" value, it's something we need to emit a use
2012 // of. This has to be carefully threaded in: if it's done after the
2013 // release it's potentially undefined behavior (and the optimizer
2014 // will ignore it), and if it happens before the retain then the
2015 // optimizer could move the release there.
2016 if (writeback.ToUse) {
2017 assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
2018
2019 // Retain the new value. No need to block-copy here: the block's
2020 // being passed up the stack.
2021 value = CGF.EmitARCRetainNonBlock(value);
2022
2023 // Emit the intrinsic use here.
2024 CGF.EmitARCIntrinsicUse(writeback.ToUse);
2025
2026 // Load the old value (primitively).
Nick Lewycky2d84e842013-10-02 02:29:49 +00002027 llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
John McCalleff18842013-03-23 02:35:54 +00002028
2029 // Put the new value in place (primitively).
2030 CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
2031
2032 // Release the old value.
2033 CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
2034
2035 // Otherwise, we can just do a normal lvalue store.
2036 } else {
2037 CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
2038 }
John McCall31168b02011-06-15 23:02:42 +00002039
2040 // Jump to the continuation block.
2041 if (!provablyNonNull)
2042 CGF.EmitBlock(contBB);
2043}
2044
2045static void emitWritebacks(CodeGenFunction &CGF,
2046 const CallArgList &args) {
Aaron Ballman36a7fa82014-03-17 17:22:27 +00002047 for (const auto &I : args.writebacks())
2048 emitWriteback(CGF, I);
John McCall31168b02011-06-15 23:02:42 +00002049}
2050
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002051static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
2052 const CallArgList &CallArgs) {
Reid Kleckner739756c2013-12-04 19:23:12 +00002053 assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee());
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002054 ArrayRef<CallArgList::CallArgCleanup> Cleanups =
2055 CallArgs.getCleanupsToDeactivate();
2056 // Iterate in reverse to increase the likelihood of popping the cleanup.
2057 for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator
2058 I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) {
2059 CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP);
2060 I->IsActiveIP->eraseFromParent();
2061 }
2062}
2063
John McCalleff18842013-03-23 02:35:54 +00002064static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
2065 if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
2066 if (uop->getOpcode() == UO_AddrOf)
2067 return uop->getSubExpr();
2068 return 0;
2069}
2070
John McCall31168b02011-06-15 23:02:42 +00002071/// Emit an argument that's being passed call-by-writeback. That is,
2072/// we are passing the address of
2073static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
2074 const ObjCIndirectCopyRestoreExpr *CRE) {
John McCalleff18842013-03-23 02:35:54 +00002075 LValue srcLV;
2076
2077 // Make an optimistic effort to emit the address as an l-value.
2078 // This can fail if the the argument expression is more complicated.
2079 if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
2080 srcLV = CGF.EmitLValue(lvExpr);
2081
2082 // Otherwise, just emit it as a scalar.
2083 } else {
2084 llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
2085
2086 QualType srcAddrType =
2087 CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
2088 srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType);
2089 }
2090 llvm::Value *srcAddr = srcLV.getAddress();
John McCall31168b02011-06-15 23:02:42 +00002091
2092 // The dest and src types don't necessarily match in LLVM terms
2093 // because of the crazy ObjC compatibility rules.
2094
Chris Lattner2192fe52011-07-18 04:24:23 +00002095 llvm::PointerType *destType =
John McCall31168b02011-06-15 23:02:42 +00002096 cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
2097
2098 // If the address is a constant null, just pass the appropriate null.
2099 if (isProvablyNull(srcAddr)) {
2100 args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
2101 CRE->getType());
2102 return;
2103 }
2104
John McCall31168b02011-06-15 23:02:42 +00002105 // Create the temporary.
2106 llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
2107 "icr.temp");
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002108 // Loading an l-value can introduce a cleanup if the l-value is __weak,
2109 // and that cleanup will be conditional if we can't prove that the l-value
2110 // isn't null, so we need to register a dominating point so that the cleanups
2111 // system will make valid IR.
2112 CodeGenFunction::ConditionalEvaluation condEval(CGF);
2113
John McCall31168b02011-06-15 23:02:42 +00002114 // Zero-initialize it if we're not doing a copy-initialization.
2115 bool shouldCopy = CRE->shouldCopy();
2116 if (!shouldCopy) {
2117 llvm::Value *null =
2118 llvm::ConstantPointerNull::get(
2119 cast<llvm::PointerType>(destType->getElementType()));
2120 CGF.Builder.CreateStore(null, temp);
2121 }
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002122
John McCall31168b02011-06-15 23:02:42 +00002123 llvm::BasicBlock *contBB = 0;
John McCalleff18842013-03-23 02:35:54 +00002124 llvm::BasicBlock *originBB = 0;
John McCall31168b02011-06-15 23:02:42 +00002125
2126 // If the address is *not* known to be non-null, we need to switch.
2127 llvm::Value *finalArgument;
2128
2129 bool provablyNonNull = isProvablyNonNull(srcAddr);
2130 if (provablyNonNull) {
2131 finalArgument = temp;
2132 } else {
2133 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
2134
2135 finalArgument = CGF.Builder.CreateSelect(isNull,
2136 llvm::ConstantPointerNull::get(destType),
2137 temp, "icr.argument");
2138
2139 // If we need to copy, then the load has to be conditional, which
2140 // means we need control flow.
2141 if (shouldCopy) {
John McCalleff18842013-03-23 02:35:54 +00002142 originBB = CGF.Builder.GetInsertBlock();
John McCall31168b02011-06-15 23:02:42 +00002143 contBB = CGF.createBasicBlock("icr.cont");
2144 llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
2145 CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
2146 CGF.EmitBlock(copyBB);
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002147 condEval.begin(CGF);
John McCall31168b02011-06-15 23:02:42 +00002148 }
2149 }
2150
John McCalleff18842013-03-23 02:35:54 +00002151 llvm::Value *valueToUse = 0;
2152
John McCall31168b02011-06-15 23:02:42 +00002153 // Perform a copy if necessary.
2154 if (shouldCopy) {
Nick Lewycky2d84e842013-10-02 02:29:49 +00002155 RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
John McCall31168b02011-06-15 23:02:42 +00002156 assert(srcRV.isScalar());
2157
2158 llvm::Value *src = srcRV.getScalarVal();
2159 src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
2160 "icr.cast");
2161
2162 // Use an ordinary store, not a store-to-lvalue.
2163 CGF.Builder.CreateStore(src, temp);
John McCalleff18842013-03-23 02:35:54 +00002164
2165 // If optimization is enabled, and the value was held in a
2166 // __strong variable, we need to tell the optimizer that this
2167 // value has to stay alive until we're doing the store back.
2168 // This is because the temporary is effectively unretained,
2169 // and so otherwise we can violate the high-level semantics.
2170 if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
2171 srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
2172 valueToUse = src;
2173 }
John McCall31168b02011-06-15 23:02:42 +00002174 }
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002175
John McCall31168b02011-06-15 23:02:42 +00002176 // Finish the control flow if we needed it.
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002177 if (shouldCopy && !provablyNonNull) {
John McCalleff18842013-03-23 02:35:54 +00002178 llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
John McCall31168b02011-06-15 23:02:42 +00002179 CGF.EmitBlock(contBB);
John McCalleff18842013-03-23 02:35:54 +00002180
2181 // Make a phi for the value to intrinsically use.
2182 if (valueToUse) {
2183 llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
2184 "icr.to-use");
2185 phiToUse->addIncoming(valueToUse, copyBB);
2186 phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
2187 originBB);
2188 valueToUse = phiToUse;
2189 }
2190
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00002191 condEval.end(CGF);
2192 }
John McCall31168b02011-06-15 23:02:42 +00002193
John McCalleff18842013-03-23 02:35:54 +00002194 args.addWriteback(srcLV, temp, valueToUse);
John McCall31168b02011-06-15 23:02:42 +00002195 args.add(RValue::get(finalArgument), CRE->getType());
2196}
2197
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002198void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
2199 assert(!StackBase && !StackCleanup.isValid());
2200
2201 // Save the stack.
2202 llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
2203 StackBase = CGF.Builder.CreateCall(F, "inalloca.save");
2204
2205 // Control gets really tied up in landing pads, so we have to spill the
2206 // stacksave to an alloca to avoid violating SSA form.
2207 // TODO: This is dead if we never emit the cleanup. We should create the
2208 // alloca and store lazily on the first cleanup emission.
2209 StackBaseMem = CGF.CreateTempAlloca(CGF.Int8PtrTy, "inalloca.spmem");
2210 CGF.Builder.CreateStore(StackBase, StackBaseMem);
2211 CGF.pushStackRestore(EHCleanup, StackBaseMem);
2212 StackCleanup = CGF.EHStack.getInnermostEHScope();
2213 assert(StackCleanup.isValid());
2214}
2215
2216void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
2217 if (StackBase) {
2218 CGF.DeactivateCleanupBlock(StackCleanup, StackBase);
2219 llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
2220 // We could load StackBase from StackBaseMem, but in the non-exceptional
2221 // case we can skip it.
2222 CGF.Builder.CreateCall(F, StackBase);
2223 }
2224}
2225
Reid Kleckner739756c2013-12-04 19:23:12 +00002226void CodeGenFunction::EmitCallArgs(CallArgList &Args,
2227 ArrayRef<QualType> ArgTypes,
2228 CallExpr::const_arg_iterator ArgBeg,
2229 CallExpr::const_arg_iterator ArgEnd,
2230 bool ForceColumnInfo) {
2231 CGDebugInfo *DI = getDebugInfo();
2232 SourceLocation CallLoc;
2233 if (DI) CallLoc = DI->getLocation();
2234
2235 // We *have* to evaluate arguments from right to left in the MS C++ ABI,
2236 // because arguments are destroyed left to right in the callee.
2237 if (CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002238 // Insert a stack save if we're going to need any inalloca args.
2239 bool HasInAllocaArgs = false;
2240 for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end();
2241 I != E && !HasInAllocaArgs; ++I)
2242 HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I);
2243 if (HasInAllocaArgs) {
2244 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
2245 Args.allocateArgumentMemory(*this);
2246 }
2247
2248 // Evaluate each argument.
Reid Kleckner739756c2013-12-04 19:23:12 +00002249 size_t CallArgsStart = Args.size();
2250 for (int I = ArgTypes.size() - 1; I >= 0; --I) {
2251 CallExpr::const_arg_iterator Arg = ArgBeg + I;
2252 EmitCallArg(Args, *Arg, ArgTypes[I]);
2253 // Restore the debug location.
2254 if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo);
2255 }
2256
2257 // Un-reverse the arguments we just evaluated so they match up with the LLVM
2258 // IR function.
2259 std::reverse(Args.begin() + CallArgsStart, Args.end());
2260 return;
2261 }
2262
2263 for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
2264 CallExpr::const_arg_iterator Arg = ArgBeg + I;
2265 assert(Arg != ArgEnd);
2266 EmitCallArg(Args, *Arg, ArgTypes[I]);
2267 // Restore the debug location.
2268 if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo);
2269 }
2270}
2271
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002272namespace {
2273
2274struct DestroyUnpassedArg : EHScopeStack::Cleanup {
2275 DestroyUnpassedArg(llvm::Value *Addr, QualType Ty)
2276 : Addr(Addr), Ty(Ty) {}
2277
2278 llvm::Value *Addr;
2279 QualType Ty;
2280
Craig Topper4f12f102014-03-12 06:41:41 +00002281 void Emit(CodeGenFunction &CGF, Flags flags) override {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002282 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
2283 assert(!Dtor->isTrivial());
2284 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
2285 /*Delegating=*/false, Addr);
2286 }
2287};
2288
2289}
2290
John McCall32ea9692011-03-11 20:59:21 +00002291void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
2292 QualType type) {
John McCall31168b02011-06-15 23:02:42 +00002293 if (const ObjCIndirectCopyRestoreExpr *CRE
2294 = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
Richard Smith9c6890a2012-11-01 22:30:59 +00002295 assert(getLangOpts().ObjCAutoRefCount);
John McCall31168b02011-06-15 23:02:42 +00002296 assert(getContext().hasSameType(E->getType(), type));
2297 return emitWritebackArg(*this, args, CRE);
2298 }
2299
John McCall0a76c0c2011-08-26 18:42:59 +00002300 assert(type->isReferenceType() == E->isGLValue() &&
2301 "reference binding to unmaterialized r-value!");
2302
John McCall17054bd62011-08-26 21:08:13 +00002303 if (E->isGLValue()) {
2304 assert(E->getObjectKind() == OK_Ordinary);
Richard Smitha1c9d4d2013-06-12 23:38:09 +00002305 return args.add(EmitReferenceBindingToExpr(E), type);
John McCall17054bd62011-08-26 21:08:13 +00002306 }
Mike Stump11289f42009-09-09 15:08:12 +00002307
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002308 bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
2309
2310 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
2311 // However, we still have to push an EH-only cleanup in case we unwind before
2312 // we make it to the call.
Reid Klecknerac640602014-05-01 03:07:18 +00002313 if (HasAggregateEvalKind &&
2314 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2315 // If we're using inalloca, use the argument memory. Otherwise, use a
Reid Klecknere39ee212014-05-03 00:33:28 +00002316 // temporary.
Reid Klecknerac640602014-05-01 03:07:18 +00002317 AggValueSlot Slot;
2318 if (args.isUsingInAlloca())
2319 Slot = createPlaceholderSlot(*this, type);
2320 else
2321 Slot = CreateAggTemp(type, "agg.tmp");
Reid Klecknere39ee212014-05-03 00:33:28 +00002322
2323 const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
2324 bool DestroyedInCallee =
2325 RD && RD->hasNonTrivialDestructor() &&
2326 CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default;
2327 if (DestroyedInCallee)
2328 Slot.setExternallyDestructed();
2329
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002330 EmitAggExpr(E, Slot);
2331 RValue RV = Slot.asRValue();
2332 args.add(RV, type);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002333
Reid Klecknere39ee212014-05-03 00:33:28 +00002334 if (DestroyedInCallee) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002335 // Create a no-op GEP between the placeholder and the cleanup so we can
2336 // RAUW it successfully. It also serves as a marker of the first
2337 // instruction where the cleanup is active.
2338 pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddr(), type);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002339 // This unreachable is a temporary marker which will be removed later.
2340 llvm::Instruction *IsActive = Builder.CreateUnreachable();
2341 args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002342 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002343 return;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002344 }
2345
2346 if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
Eli Friedmandf968192011-05-26 00:10:27 +00002347 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
2348 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
2349 assert(L.isSimple());
Eli Friedman61f615a2013-06-11 01:08:22 +00002350 if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) {
2351 args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
2352 } else {
2353 // We can't represent a misaligned lvalue in the CallArgList, so copy
2354 // to an aligned temporary now.
2355 llvm::Value *tmp = CreateMemTemp(type);
2356 EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(),
2357 L.getAlignment());
2358 args.add(RValue::getAggregate(tmp), type);
2359 }
Eli Friedmandf968192011-05-26 00:10:27 +00002360 return;
2361 }
2362
John McCall32ea9692011-03-11 20:59:21 +00002363 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00002364}
2365
Dan Gohman515a60d2012-02-16 00:57:37 +00002366// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2367// optimizer it can aggressively ignore unwind edges.
2368void
2369CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
2370 if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
2371 !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
2372 Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
2373 CGM.getNoObjCARCExceptionsMetadata());
2374}
2375
John McCall882987f2013-02-28 19:01:20 +00002376/// Emits a call to the given no-arguments nounwind runtime function.
2377llvm::CallInst *
2378CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2379 const llvm::Twine &name) {
2380 return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
2381}
2382
2383/// Emits a call to the given nounwind runtime function.
2384llvm::CallInst *
2385CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2386 ArrayRef<llvm::Value*> args,
2387 const llvm::Twine &name) {
2388 llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
2389 call->setDoesNotThrow();
2390 return call;
2391}
2392
2393/// Emits a simple call (never an invoke) to the given no-arguments
2394/// runtime function.
2395llvm::CallInst *
2396CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2397 const llvm::Twine &name) {
2398 return EmitRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
2399}
2400
2401/// Emits a simple call (never an invoke) to the given runtime
2402/// function.
2403llvm::CallInst *
2404CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2405 ArrayRef<llvm::Value*> args,
2406 const llvm::Twine &name) {
2407 llvm::CallInst *call = Builder.CreateCall(callee, args, name);
2408 call->setCallingConv(getRuntimeCC());
2409 return call;
2410}
2411
2412/// Emits a call or invoke to the given noreturn runtime function.
2413void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2414 ArrayRef<llvm::Value*> args) {
2415 if (getInvokeDest()) {
2416 llvm::InvokeInst *invoke =
2417 Builder.CreateInvoke(callee,
2418 getUnreachableBlock(),
2419 getInvokeDest(),
2420 args);
2421 invoke->setDoesNotReturn();
2422 invoke->setCallingConv(getRuntimeCC());
2423 } else {
2424 llvm::CallInst *call = Builder.CreateCall(callee, args);
2425 call->setDoesNotReturn();
2426 call->setCallingConv(getRuntimeCC());
2427 Builder.CreateUnreachable();
2428 }
Justin Bogner06bd6d02014-01-13 21:24:18 +00002429 PGO.setCurrentRegionUnreachable();
John McCall882987f2013-02-28 19:01:20 +00002430}
2431
2432/// Emits a call or invoke instruction to the given nullary runtime
2433/// function.
2434llvm::CallSite
2435CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2436 const Twine &name) {
2437 return EmitRuntimeCallOrInvoke(callee, ArrayRef<llvm::Value*>(), name);
2438}
2439
2440/// Emits a call or invoke instruction to the given runtime function.
2441llvm::CallSite
2442CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2443 ArrayRef<llvm::Value*> args,
2444 const Twine &name) {
2445 llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
2446 callSite.setCallingConv(getRuntimeCC());
2447 return callSite;
2448}
2449
2450llvm::CallSite
2451CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
2452 const Twine &Name) {
2453 return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name);
2454}
2455
John McCallbd309292010-07-06 01:34:17 +00002456/// Emits a call or invoke instruction to the given function, depending
2457/// on the current state of the EH stack.
2458llvm::CallSite
2459CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner54b16772011-07-23 17:14:25 +00002460 ArrayRef<llvm::Value *> Args,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002461 const Twine &Name) {
John McCallbd309292010-07-06 01:34:17 +00002462 llvm::BasicBlock *InvokeDest = getInvokeDest();
John McCallbd309292010-07-06 01:34:17 +00002463
Dan Gohman515a60d2012-02-16 00:57:37 +00002464 llvm::Instruction *Inst;
2465 if (!InvokeDest)
2466 Inst = Builder.CreateCall(Callee, Args, Name);
2467 else {
2468 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
2469 Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name);
2470 EmitBlock(ContBB);
2471 }
2472
2473 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2474 // optimizer it can aggressively ignore unwind edges.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002475 if (CGM.getLangOpts().ObjCAutoRefCount)
Dan Gohman515a60d2012-02-16 00:57:37 +00002476 AddObjCARCExceptionMetadata(Inst);
2477
2478 return Inst;
John McCallbd309292010-07-06 01:34:17 +00002479}
2480
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002481static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
2482 llvm::FunctionType *FTy) {
2483 if (ArgNo < FTy->getNumParams())
2484 assert(Elt->getType() == FTy->getParamType(ArgNo));
2485 else
2486 assert(FTy->isVarArg());
2487 ++ArgNo;
2488}
2489
Chris Lattnerd59d8672011-07-12 06:29:11 +00002490void CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Craig Topper5603df42013-07-05 19:34:19 +00002491 SmallVectorImpl<llvm::Value *> &Args,
Chris Lattnerd59d8672011-07-12 06:29:11 +00002492 llvm::FunctionType *IRFuncTy) {
Bob Wilsone826a2a2011-08-03 05:58:22 +00002493 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2494 unsigned NumElts = AT->getSize().getZExtValue();
2495 QualType EltTy = AT->getElementType();
2496 llvm::Value *Addr = RV.getAggregateAddr();
2497 for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
2498 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt);
Nick Lewycky2d84e842013-10-02 02:29:49 +00002499 RValue EltRV = convertTempToRValue(EltAddr, EltTy, SourceLocation());
Bob Wilsone826a2a2011-08-03 05:58:22 +00002500 ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy);
Chris Lattnerd59d8672011-07-12 06:29:11 +00002501 }
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002502 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Bob Wilsone826a2a2011-08-03 05:58:22 +00002503 RecordDecl *RD = RT->getDecl();
2504 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
Eli Friedman7f1ff602012-04-16 03:54:45 +00002505 LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002506
2507 if (RD->isUnion()) {
2508 const FieldDecl *LargestFD = 0;
2509 CharUnits UnionSize = CharUnits::Zero();
2510
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002511 for (const auto *FD : RD->fields()) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002512 assert(!FD->isBitField() &&
2513 "Cannot expand structure with bit-field members.");
2514 CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
2515 if (UnionSize < FieldSize) {
2516 UnionSize = FieldSize;
2517 LargestFD = FD;
2518 }
2519 }
2520 if (LargestFD) {
Nick Lewycky2d84e842013-10-02 02:29:49 +00002521 RValue FldRV = EmitRValueForField(LV, LargestFD, SourceLocation());
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002522 ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy);
2523 }
2524 } else {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00002525 for (const auto *FD : RD->fields()) {
Nick Lewycky2d84e842013-10-02 02:29:49 +00002526 RValue FldRV = EmitRValueForField(LV, FD, SourceLocation());
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002527 ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy);
2528 }
Bob Wilsone826a2a2011-08-03 05:58:22 +00002529 }
Eli Friedman95ff7002011-11-15 02:46:03 +00002530 } else if (Ty->isAnyComplexType()) {
Bob Wilsone826a2a2011-08-03 05:58:22 +00002531 ComplexPairTy CV = RV.getComplexVal();
2532 Args.push_back(CV.first);
2533 Args.push_back(CV.second);
2534 } else {
Chris Lattnerd59d8672011-07-12 06:29:11 +00002535 assert(RV.isScalar() &&
2536 "Unexpected non-scalar rvalue during struct expansion.");
2537
2538 // Insert a bitcast as needed.
2539 llvm::Value *V = RV.getScalarVal();
2540 if (Args.size() < IRFuncTy->getNumParams() &&
2541 V->getType() != IRFuncTy->getParamType(Args.size()))
2542 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size()));
2543
2544 Args.push_back(V);
2545 }
2546}
2547
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002548/// \brief Store a non-aggregate value to an address to initialize it. For
2549/// initialization, a non-atomic store will be used.
2550static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src,
2551 LValue Dst) {
2552 if (Src.isScalar())
2553 CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true);
2554 else
2555 CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true);
2556}
2557
2558void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
2559 llvm::Value *New) {
2560 DeferredReplacements.push_back(std::make_pair(Old, New));
2561}
Chris Lattnerd59d8672011-07-12 06:29:11 +00002562
Daniel Dunbard931a872009-02-02 22:03:45 +00002563RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00002564 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00002565 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00002566 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00002567 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00002568 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00002569 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002570 SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00002571
2572 // Handle struct-return functions by passing a pointer to the
2573 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00002574 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00002575 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00002576
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002577 // IRArgNo - Keep track of the argument number in the callee we're looking at.
2578 unsigned IRArgNo = 0;
2579 llvm::FunctionType *IRFuncTy =
2580 cast<llvm::FunctionType>(
2581 cast<llvm::PointerType>(Callee->getType())->getElementType());
Mike Stump11289f42009-09-09 15:08:12 +00002582
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002583 // If we're using inalloca, insert the allocation after the stack save.
2584 // FIXME: Do this earlier rather than hacking it in here!
2585 llvm::Value *ArgMemory = 0;
2586 if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
Reid Kleckner9df1d972014-04-10 01:40:15 +00002587 llvm::Instruction *IP = CallArgs.getStackBase();
2588 llvm::AllocaInst *AI;
2589 if (IP) {
2590 IP = IP->getNextNode();
2591 AI = new llvm::AllocaInst(ArgStruct, "argmem", IP);
2592 } else {
2593 AI = Builder.CreateAlloca(ArgStruct, nullptr, "argmem");
2594 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002595 AI->setUsedWithInAlloca(true);
2596 assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
2597 ArgMemory = AI;
2598 }
2599
Chris Lattner4ca97c32009-06-13 00:26:38 +00002600 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00002601 // alloca to hold the result, unless one is given to us.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002602 llvm::Value *SRetPtr = 0;
2603 if (CGM.ReturnTypeUsesSRet(CallInfo) || RetAI.isInAlloca()) {
2604 SRetPtr = ReturnValue.getValue();
2605 if (!SRetPtr)
2606 SRetPtr = CreateMemTemp(RetTy);
2607 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
2608 Args.push_back(SRetPtr);
2609 checkArgMatches(SRetPtr, IRArgNo, IRFuncTy);
2610 } else {
2611 llvm::Value *Addr =
2612 Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
2613 Builder.CreateStore(SRetPtr, Addr);
2614 }
Anders Carlsson17490832009-12-24 20:40:36 +00002615 }
Mike Stump11289f42009-09-09 15:08:12 +00002616
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00002617 assert(CallInfo.arg_size() == CallArgs.size() &&
2618 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00002619 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00002620 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00002621 I != E; ++I, ++info_it) {
2622 const ABIArgInfo &ArgInfo = info_it->info;
Eli Friedmanf4258eb2011-05-02 18:05:27 +00002623 RValue RV = I->RV;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00002624
John McCall47fb9502013-03-07 21:37:08 +00002625 CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty);
Rafael Espindolafad28de2012-10-24 01:59:00 +00002626
2627 // Insert a padding argument to ensure proper alignment.
2628 if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) {
2629 Args.push_back(llvm::UndefValue::get(PaddingType));
2630 ++IRArgNo;
2631 }
2632
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00002633 switch (ArgInfo.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002634 case ABIArgInfo::InAlloca: {
2635 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
2636 if (RV.isAggregate()) {
2637 // Replace the placeholder with the appropriate argument slot GEP.
2638 llvm::Instruction *Placeholder =
2639 cast<llvm::Instruction>(RV.getAggregateAddr());
2640 CGBuilderTy::InsertPoint IP = Builder.saveIP();
2641 Builder.SetInsertPoint(Placeholder);
2642 llvm::Value *Addr = Builder.CreateStructGEP(
2643 ArgMemory, ArgInfo.getInAllocaFieldIndex());
2644 Builder.restoreIP(IP);
2645 deferPlaceholderReplacement(Placeholder, Addr);
2646 } else {
2647 // Store the RValue into the argument struct.
2648 llvm::Value *Addr =
2649 Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
David Majnemer32b57b02014-03-31 16:12:47 +00002650 unsigned AS = Addr->getType()->getPointerAddressSpace();
2651 llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS);
2652 // There are some cases where a trivial bitcast is not avoidable. The
2653 // definition of a type later in a translation unit may change it's type
2654 // from {}* to (%struct.foo*)*.
2655 if (Addr->getType() != MemType)
2656 Addr = Builder.CreateBitCast(Addr, MemType);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002657 LValue argLV = MakeAddrLValue(Addr, I->Ty, TypeAlign);
2658 EmitInitStoreOfNonAggregate(*this, RV, argLV);
2659 }
2660 break; // Don't increment IRArgNo!
2661 }
2662
Daniel Dunbar03816342010-08-21 02:24:36 +00002663 case ABIArgInfo::Indirect: {
Daniel Dunbar747865a2009-02-05 09:16:39 +00002664 if (RV.isScalar() || RV.isComplex()) {
2665 // Make a temporary alloca to pass the argument.
Eli Friedman7e68c882011-06-15 18:26:32 +00002666 llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
2667 if (ArgInfo.getIndirectAlign() > AI->getAlignment())
2668 AI->setAlignment(ArgInfo.getIndirectAlign());
2669 Args.push_back(AI);
John McCall47fb9502013-03-07 21:37:08 +00002670
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002671 LValue argLV = MakeAddrLValue(Args.back(), I->Ty, TypeAlign);
2672 EmitInitStoreOfNonAggregate(*this, RV, argLV);
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002673
2674 // Validate argument match.
2675 checkArgMatches(AI, IRArgNo, IRFuncTy);
Daniel Dunbar747865a2009-02-05 09:16:39 +00002676 } else {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002677 // We want to avoid creating an unnecessary temporary+copy here;
Guy Benyei3832bfd2013-03-10 12:59:00 +00002678 // however, we need one in three cases:
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002679 // 1. If the argument is not byval, and we are required to copy the
2680 // source. (This case doesn't occur on any common architecture.)
2681 // 2. If the argument is byval, RV is not sufficiently aligned, and
2682 // we cannot force it to be sufficiently aligned.
Guy Benyei3832bfd2013-03-10 12:59:00 +00002683 // 3. If the argument is byval, but RV is located in an address space
2684 // different than that of the argument (0).
Eli Friedmanf7456192011-06-15 22:09:18 +00002685 llvm::Value *Addr = RV.getAggregateAddr();
2686 unsigned Align = ArgInfo.getIndirectAlign();
Micah Villmowdd31ca12012-10-08 16:25:52 +00002687 const llvm::DataLayout *TD = &CGM.getDataLayout();
Guy Benyei3832bfd2013-03-10 12:59:00 +00002688 const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace();
2689 const unsigned ArgAddrSpace = (IRArgNo < IRFuncTy->getNumParams() ?
2690 IRFuncTy->getParamType(IRArgNo)->getPointerAddressSpace() : 0);
Eli Friedmanf7456192011-06-15 22:09:18 +00002691 if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
John McCall47fb9502013-03-07 21:37:08 +00002692 (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align &&
Guy Benyei3832bfd2013-03-10 12:59:00 +00002693 llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) ||
2694 (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002695 // Create an aligned temporary, and copy to it.
Eli Friedmanf7456192011-06-15 22:09:18 +00002696 llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
2697 if (Align > AI->getAlignment())
2698 AI->setAlignment(Align);
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002699 Args.push_back(AI);
Chad Rosier615ed1a2012-03-29 17:37:10 +00002700 EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002701
2702 // Validate argument match.
2703 checkArgMatches(AI, IRArgNo, IRFuncTy);
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002704 } else {
2705 // Skip the extra memcpy call.
Eli Friedmanf7456192011-06-15 22:09:18 +00002706 Args.push_back(Addr);
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002707
2708 // Validate argument match.
2709 checkArgMatches(Addr, IRArgNo, IRFuncTy);
Eli Friedmaneb7fab62011-06-14 01:37:52 +00002710 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00002711 }
2712 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00002713 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00002714
Daniel Dunbar94a6f252009-01-26 21:26:08 +00002715 case ABIArgInfo::Ignore:
2716 break;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002717
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002718 case ABIArgInfo::Extend:
2719 case ABIArgInfo::Direct: {
2720 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002721 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
2722 ArgInfo.getDirectOffset() == 0) {
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002723 llvm::Value *V;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002724 if (RV.isScalar())
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002725 V = RV.getScalarVal();
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002726 else
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002727 V = Builder.CreateLoad(RV.getAggregateAddr());
2728
Chris Lattner3ce86682011-07-12 04:53:39 +00002729 // If the argument doesn't match, perform a bitcast to coerce it. This
2730 // can happen due to trivial type mismatches.
2731 if (IRArgNo < IRFuncTy->getNumParams() &&
2732 V->getType() != IRFuncTy->getParamType(IRArgNo))
2733 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002734 Args.push_back(V);
2735
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002736 checkArgMatches(V, IRArgNo, IRFuncTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002737 break;
2738 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00002739
Daniel Dunbar2f219b02009-02-03 19:12:28 +00002740 // FIXME: Avoid the conversion through memory if possible.
2741 llvm::Value *SrcPtr;
John McCall47fb9502013-03-07 21:37:08 +00002742 if (RV.isScalar() || RV.isComplex()) {
Eli Friedmanf4258eb2011-05-02 18:05:27 +00002743 SrcPtr = CreateMemTemp(I->Ty, "coerce");
John McCall47fb9502013-03-07 21:37:08 +00002744 LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002745 EmitInitStoreOfNonAggregate(*this, RV, SrcLV);
Mike Stump11289f42009-09-09 15:08:12 +00002746 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00002747 SrcPtr = RV.getAggregateAddr();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002748
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002749 // If the value is offset in memory, apply the offset now.
2750 if (unsigned Offs = ArgInfo.getDirectOffset()) {
2751 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
2752 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002753 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002754 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
2755
2756 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002757
Chris Lattner3dd716c2010-06-28 23:44:11 +00002758 // If the coerce-to type is a first class aggregate, we flatten it and
2759 // pass the elements. Either way is semantically identical, but fast-isel
2760 // and the optimizer generally likes scalar values better than FCAs.
James Molloy6f244b62014-05-09 16:21:39 +00002761 // We cannot do this for functions using the AAPCS calling convention,
2762 // as structures are treated differently by that calling convention.
2763 llvm::StructType *STy =
2764 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
2765 if (STy && !isAAPCSVFP(CallInfo, getTarget())) {
Chandler Carrutha6399a52012-10-10 11:29:08 +00002766 llvm::Type *SrcTy =
2767 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
2768 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
2769 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
2770
2771 // If the source type is smaller than the destination type of the
2772 // coerce-to logic, copy the source value into a temp alloca the size
2773 // of the destination type to allow loading all of it. The bits past
2774 // the source value are left undef.
2775 if (SrcSize < DstSize) {
2776 llvm::AllocaInst *TempAlloca
2777 = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce");
2778 Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0);
2779 SrcPtr = TempAlloca;
2780 } else {
2781 SrcPtr = Builder.CreateBitCast(SrcPtr,
2782 llvm::PointerType::getUnqual(STy));
2783 }
2784
Chris Lattnerceddafb2010-07-05 20:41:41 +00002785 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2786 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00002787 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
2788 // We don't know what we're loading from.
2789 LI->setAlignment(1);
2790 Args.push_back(LI);
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002791
2792 // Validate argument match.
2793 checkArgMatches(LI, IRArgNo, IRFuncTy);
Chris Lattner15ec3612010-06-29 00:06:42 +00002794 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00002795 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00002796 // In the simple case, just pass the coerced loaded value.
2797 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2798 *this));
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002799
2800 // Validate argument match.
2801 checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
Chris Lattner3dd716c2010-06-28 23:44:11 +00002802 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002803
Daniel Dunbar2f219b02009-02-03 19:12:28 +00002804 break;
2805 }
2806
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00002807 case ABIArgInfo::Expand:
Chris Lattnerd59d8672011-07-12 06:29:11 +00002808 ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy);
Chris Lattnerbb1952c2011-07-12 04:46:18 +00002809 IRArgNo = Args.size();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00002810 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00002811 }
2812 }
Mike Stump11289f42009-09-09 15:08:12 +00002813
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002814 if (ArgMemory) {
2815 llvm::Value *Arg = ArgMemory;
2816 llvm::Type *LastParamTy =
2817 IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
2818 if (Arg->getType() != LastParamTy) {
2819#ifndef NDEBUG
2820 // Assert that these structs have equivalent element types.
2821 llvm::StructType *FullTy = CallInfo.getArgStruct();
2822 llvm::StructType *Prefix = cast<llvm::StructType>(
2823 cast<llvm::PointerType>(LastParamTy)->getElementType());
2824
2825 // For variadic functions, the caller might supply a larger struct than
2826 // the callee expects, and that's OK.
2827 assert(Prefix->getNumElements() == FullTy->getNumElements() ||
2828 (CallInfo.isVariadic() &&
2829 Prefix->getNumElements() <= FullTy->getNumElements()));
2830
2831 for (llvm::StructType::element_iterator PI = Prefix->element_begin(),
2832 PE = Prefix->element_end(),
2833 FI = FullTy->element_begin();
2834 PI != PE; ++PI, ++FI)
2835 assert(*PI == *FI);
2836#endif
2837 Arg = Builder.CreateBitCast(Arg, LastParamTy);
2838 }
2839 Args.push_back(Arg);
2840 }
2841
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00002842 if (!CallArgs.getCleanupsToDeactivate().empty())
2843 deactivateArgCleanupsBeforeCall(*this, CallArgs);
2844
Chris Lattner4ca97c32009-06-13 00:26:38 +00002845 // If the callee is a bitcast of a function to a varargs pointer to function
2846 // type, check to see if we can remove the bitcast. This handles some cases
2847 // with unprototyped functions.
2848 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
2849 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002850 llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
2851 llvm::FunctionType *CurFT =
Chris Lattner4ca97c32009-06-13 00:26:38 +00002852 cast<llvm::FunctionType>(CurPT->getElementType());
Chris Lattner2192fe52011-07-18 04:24:23 +00002853 llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002854
Chris Lattner4ca97c32009-06-13 00:26:38 +00002855 if (CE->getOpcode() == llvm::Instruction::BitCast &&
2856 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00002857 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00002858 ActualFT->getNumParams() == Args.size() &&
2859 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00002860 bool ArgsMatch = true;
2861 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
2862 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
2863 ArgsMatch = false;
2864 break;
2865 }
Mike Stump11289f42009-09-09 15:08:12 +00002866
Chris Lattner4ca97c32009-06-13 00:26:38 +00002867 // Strip the cast if we can get away with it. This is a nice cleanup,
2868 // but also allows us to inline the function at -O0 if it is marked
2869 // always_inline.
2870 if (ArgsMatch)
2871 Callee = CalleeF;
2872 }
2873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874
Daniel Dunbar0ef34792009-09-12 00:59:20 +00002875 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00002876 CodeGen::AttributeListType AttributeList;
Bill Wendlingf4d64cb2013-02-22 00:13:35 +00002877 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList,
2878 CallingConv, true);
Bill Wendling3087d022012-12-07 23:17:26 +00002879 llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(),
Bill Wendlingf4d64cb2013-02-22 00:13:35 +00002880 AttributeList);
Mike Stump11289f42009-09-09 15:08:12 +00002881
John McCallbd309292010-07-06 01:34:17 +00002882 llvm::BasicBlock *InvokeDest = 0;
Bill Wendling5e85be42012-12-30 10:32:17 +00002883 if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex,
2884 llvm::Attribute::NoUnwind))
John McCallbd309292010-07-06 01:34:17 +00002885 InvokeDest = getInvokeDest();
2886
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002887 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00002888 if (!InvokeDest) {
Jay Foad5bd375a2011-07-15 08:37:34 +00002889 CS = Builder.CreateCall(Callee, Args);
Daniel Dunbar12347492009-02-23 17:26:39 +00002890 } else {
2891 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Jay Foad5bd375a2011-07-15 08:37:34 +00002892 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args);
Daniel Dunbar12347492009-02-23 17:26:39 +00002893 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00002894 }
Chris Lattnere70a0072010-06-29 16:40:28 +00002895 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00002896 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00002897
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002898 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00002899 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002900
Dan Gohman515a60d2012-02-16 00:57:37 +00002901 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2902 // optimizer it can aggressively ignore unwind edges.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002903 if (CGM.getLangOpts().ObjCAutoRefCount)
Dan Gohman515a60d2012-02-16 00:57:37 +00002904 AddObjCARCExceptionMetadata(CS.getInstruction());
2905
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002906 // If the call doesn't return, finish the basic block and clear the
2907 // insertion point; this allows the rest of IRgen to discard
2908 // unreachable code.
2909 if (CS.doesNotReturn()) {
2910 Builder.CreateUnreachable();
2911 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00002912
Mike Stump18bb9282009-05-16 07:57:57 +00002913 // FIXME: For now, emit a dummy basic block because expr emitters in
2914 // generally are not ready to handle emitting expressions at unreachable
2915 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002916 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00002917
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002918 // Return a reasonable RValue.
2919 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00002920 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00002921
2922 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00002923 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00002924 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00002925
John McCall31168b02011-06-15 23:02:42 +00002926 // Emit any writebacks immediately. Arguably this should happen
2927 // after any return-value munging.
2928 if (CallArgs.hasWritebacks())
2929 emitWritebacks(*this, CallArgs);
2930
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002931 // The stack cleanup for inalloca arguments has to run out of the normal
2932 // lexical order, so deactivate it and run it manually here.
2933 CallArgs.freeArgumentMemory(*this);
2934
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00002935 switch (RetAI.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002936 case ABIArgInfo::InAlloca:
John McCall47fb9502013-03-07 21:37:08 +00002937 case ABIArgInfo::Indirect:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002938 return convertTempToRValue(SRetPtr, RetTy, SourceLocation());
Daniel Dunbard3674e62008-09-11 01:48:57 +00002939
Daniel Dunbar94a6f252009-01-26 21:26:08 +00002940 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00002941 // If we are ignoring an argument that had a result, make sure to
2942 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00002943 return GetUndefRValue(RetTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002944
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002945 case ABIArgInfo::Extend:
2946 case ABIArgInfo::Direct: {
Chris Lattner3517f142011-07-13 03:59:32 +00002947 llvm::Type *RetIRTy = ConvertType(RetTy);
2948 if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
John McCall47fb9502013-03-07 21:37:08 +00002949 switch (getEvaluationKind(RetTy)) {
2950 case TEK_Complex: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002951 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2952 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2953 return RValue::getComplex(std::make_pair(Real, Imag));
2954 }
John McCall47fb9502013-03-07 21:37:08 +00002955 case TEK_Aggregate: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002956 llvm::Value *DestPtr = ReturnValue.getValue();
2957 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar94a6f252009-01-26 21:26:08 +00002958
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002959 if (!DestPtr) {
2960 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
2961 DestIsVolatile = false;
2962 }
Eli Friedmanaf9b3252011-05-17 21:08:01 +00002963 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002964 return RValue::getAggregate(DestPtr);
2965 }
John McCall47fb9502013-03-07 21:37:08 +00002966 case TEK_Scalar: {
2967 // If the argument doesn't match, perform a bitcast to coerce it. This
2968 // can happen due to trivial type mismatches.
2969 llvm::Value *V = CI;
2970 if (V->getType() != RetIRTy)
2971 V = Builder.CreateBitCast(V, RetIRTy);
2972 return RValue::get(V);
2973 }
2974 }
2975 llvm_unreachable("bad evaluation kind");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002976 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002977
Anders Carlsson17490832009-12-24 20:40:36 +00002978 llvm::Value *DestPtr = ReturnValue.getValue();
2979 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002980
Anders Carlsson17490832009-12-24 20:40:36 +00002981 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00002982 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00002983 DestIsVolatile = false;
2984 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002985
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002986 // If the value is offset in memory, apply the offset now.
2987 llvm::Value *StorePtr = DestPtr;
2988 if (unsigned Offs = RetAI.getDirectOffset()) {
2989 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
2990 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002991 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002992 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
2993 }
2994 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002995
Nick Lewycky2d84e842013-10-02 02:29:49 +00002996 return convertTempToRValue(DestPtr, RetTy, SourceLocation());
Daniel Dunbar573884e2008-09-10 07:04:09 +00002997 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00002998
Daniel Dunbard3674e62008-09-11 01:48:57 +00002999 case ABIArgInfo::Expand:
David Blaikie83d382b2011-09-23 05:06:16 +00003000 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00003001 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00003002
David Blaikie83d382b2011-09-23 05:06:16 +00003003 llvm_unreachable("Unhandled ABIArgInfo::Kind");
Daniel Dunbar613855c2008-09-09 23:27:19 +00003004}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00003005
3006/* VarArg handling */
3007
3008llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
3009 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
3010}