blob: 9b96a59aec38d5af0c8ce31396720a680d973f30 [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"
Akira Hatanaka9d8ac612016-02-17 21:09:50 +000017#include "CGBlocks.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "CGCXXABI.h"
David Majnemer4e52d6f2015-12-12 05:39:21 +000019#include "CGCleanup.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000020#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000021#include "CodeGenModule.h"
John McCalla729c622012-02-17 03:33:10 +000022#include "TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000023#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000024#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000025#include "clang/AST/DeclObjC.h"
Eric Christopher15709992015-10-15 23:47:11 +000026#include "clang/Basic/TargetBuiltins.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "clang/Basic/TargetInfo.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000028#include "clang/CodeGen/CGFunctionInfo.h"
John McCall12f23522016-04-04 18:33:08 +000029#include "clang/CodeGen/SwiftCallingConv.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000030#include "clang/Frontend/CodeGenOptions.h"
Bill Wendling706469b2013-02-28 22:49:57 +000031#include "llvm/ADT/StringExtras.h"
Nick Lewyckyd9bce502016-09-20 15:49:58 +000032#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000033#include "llvm/IR/Attributes.h"
Nikolay Haustov8c6538b2016-06-30 09:06:33 +000034#include "llvm/IR/CallingConv.h"
Chandler Carruthc80ceea2014-03-04 11:02:08 +000035#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000036#include "llvm/IR/DataLayout.h"
37#include "llvm/IR/InlineAsm.h"
Saleem Abdulrasool94cfc602016-04-07 17:49:44 +000038#include "llvm/IR/Intrinsics.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000039#include "llvm/IR/IntrinsicInst.h"
Eli Friedmanf7456192011-06-15 22:09:18 +000040#include "llvm/Transforms/Utils/Local.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000041using namespace clang;
42using namespace CodeGen;
43
44/***/
45
Nikolay Haustov8c6538b2016-06-30 09:06:33 +000046unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
John McCallab26cfa2010-02-05 21:31:56 +000047 switch (CC) {
48 default: return llvm::CallingConv::C;
49 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
50 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Erich Keane757d3172016-11-02 18:29:35 +000051 case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000052 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Charles Davisb5a214e2013-08-30 04:39:01 +000053 case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64;
54 case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
Anton Korobeynikov231e8752011-04-14 20:06:49 +000055 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
56 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Guy Benyeif0a014b2012-12-25 08:53:55 +000057 case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
Reid Klecknerd7857f02014-10-24 17:42:17 +000058 // TODO: Add support for __pascal to LLVM.
59 case CC_X86Pascal: return llvm::CallingConv::C;
60 // TODO: Add support for __vectorcall to LLVM.
Reid Kleckner80944df2014-10-31 22:00:51 +000061 case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;
Alexander Kornienko21de0ae2015-01-20 11:20:41 +000062 case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;
Nikolay Haustov8c6538b2016-06-30 09:06:33 +000063 case CC_OpenCLKernel: return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
Roman Levenstein35aa5ce2016-03-16 18:00:46 +000064 case CC_PreserveMost: return llvm::CallingConv::PreserveMost;
65 case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
John McCall12f23522016-04-04 18:33:08 +000066 case CC_Swift: return llvm::CallingConv::Swift;
John McCallab26cfa2010-02-05 21:31:56 +000067 }
68}
69
John McCall8ee376f2010-02-24 07:14:12 +000070/// Derives the 'this' type for codegen purposes, i.e. ignoring method
71/// qualification.
72/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000073static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
74 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
75 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000076}
77
John McCall8ee376f2010-02-24 07:14:12 +000078/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000079static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
80 return MD->getType()->getCanonicalTypeUnqualified()
81 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000082}
83
84/// Returns the "extra-canonicalized" return type, which discards
85/// qualifiers on the return type. Codegen doesn't care about them,
86/// and it makes ABI code a little easier to be able to assume that
87/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000088static CanQualType GetReturnType(QualType RetTy) {
89 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000090}
91
John McCall8dda7b22012-07-07 06:41:13 +000092/// Arrange the argument and result information for a value of the given
93/// unprototyped freestanding function type.
John McCall8ee376f2010-02-24 07:14:12 +000094const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +000095CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
John McCalla729c622012-02-17 03:33:10 +000096 // When translating an unprototyped function type, always use a
97 // variadic type.
Alp Toker314cc812014-01-25 16:55:45 +000098 return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
Peter Collingbournef7706832014-12-12 23:41:25 +000099 /*instanceMethod=*/false,
100 /*chainCall=*/false, None,
John McCallc56a8b32016-03-11 04:30:31 +0000101 FTNP->getExtInfo(), {}, RequiredArgs(0));
John McCall8ee376f2010-02-24 07:14:12 +0000102}
103
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000104/// Adds the formal paramaters in FPT to the given prefix. If any parameter in
105/// FPT has pass_object_size attrs, then we'll add parameters for those, too.
106static void appendParameterTypes(const CodeGenTypes &CGT,
107 SmallVectorImpl<CanQualType> &prefix,
John McCallc56a8b32016-03-11 04:30:31 +0000108 SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
109 CanQual<FunctionProtoType> FPT,
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000110 const FunctionDecl *FD) {
John McCallc56a8b32016-03-11 04:30:31 +0000111 // Fill out paramInfos.
112 if (FPT->hasExtParameterInfos() || !paramInfos.empty()) {
113 assert(paramInfos.size() <= prefix.size());
114 auto protoParamInfos = FPT->getExtParameterInfos();
115 paramInfos.reserve(prefix.size() + protoParamInfos.size());
116 paramInfos.resize(prefix.size());
John McCall12f23522016-04-04 18:33:08 +0000117 paramInfos.append(protoParamInfos.begin(), protoParamInfos.end());
John McCallc56a8b32016-03-11 04:30:31 +0000118 }
119
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000120 // Fast path: unknown target.
121 if (FD == nullptr) {
122 prefix.append(FPT->param_type_begin(), FPT->param_type_end());
123 return;
124 }
125
126 // In the vast majority cases, we'll have precisely FPT->getNumParams()
127 // parameters; the only thing that can change this is the presence of
128 // pass_object_size. So, we preallocate for the common case.
129 prefix.reserve(prefix.size() + FPT->getNumParams());
130
131 assert(FD->getNumParams() == FPT->getNumParams());
132 for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
133 prefix.push_back(FPT->getParamType(I));
134 if (FD->getParamDecl(I)->hasAttr<PassObjectSizeAttr>())
135 prefix.push_back(CGT.getContext().getSizeType());
136 }
137}
138
John McCall8dda7b22012-07-07 06:41:13 +0000139/// Arrange the LLVM function layout for a value of the given function
Alexey Samsonove5ef3ca2014-08-13 23:55:54 +0000140/// type, on top of any implicit parameters already stored.
141static const CGFunctionInfo &
Peter Collingbournef7706832014-12-12 23:41:25 +0000142arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
Alexey Samsonove5ef3ca2014-08-13 23:55:54 +0000143 SmallVectorImpl<CanQualType> &prefix,
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000144 CanQual<FunctionProtoType> FTP,
145 const FunctionDecl *FD) {
John McCallc56a8b32016-03-11 04:30:31 +0000146 SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
George Burgess IV419996c2016-06-16 23:06:04 +0000147 RequiredArgs Required =
148 RequiredArgs::forPrototypePlus(FTP, prefix.size(), FD);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000149 // FIXME: Kill copy.
John McCallc56a8b32016-03-11 04:30:31 +0000150 appendParameterTypes(CGT, prefix, paramInfos, FTP, FD);
Alp Toker314cc812014-01-25 16:55:45 +0000151 CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
John McCallc56a8b32016-03-11 04:30:31 +0000152
Peter Collingbournef7706832014-12-12 23:41:25 +0000153 return CGT.arrangeLLVMFunctionInfo(resultType, instanceMethod,
154 /*chainCall=*/false, prefix,
John McCallc56a8b32016-03-11 04:30:31 +0000155 FTP->getExtInfo(), paramInfos,
George Burgess IV419996c2016-06-16 23:06:04 +0000156 Required);
John McCall8ee376f2010-02-24 07:14:12 +0000157}
158
John McCalla729c622012-02-17 03:33:10 +0000159/// Arrange the argument and result information for a value of the
John McCall8dda7b22012-07-07 06:41:13 +0000160/// given freestanding function type.
John McCall8ee376f2010-02-24 07:14:12 +0000161const CGFunctionInfo &
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000162CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP,
163 const FunctionDecl *FD) {
John McCalla729c622012-02-17 03:33:10 +0000164 SmallVector<CanQualType, 16> argTypes;
Peter Collingbournef7706832014-12-12 23:41:25 +0000165 return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000166 FTP, FD);
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000167}
168
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000169static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000170 // Set the appropriate calling convention for the Function.
171 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000172 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000173
174 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000175 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000176
Erich Keane757d3172016-11-02 18:29:35 +0000177 if (D->hasAttr<RegCallAttr>())
178 return CC_X86RegCall;
179
Douglas Gregora941dca2010-05-18 16:57:00 +0000180 if (D->hasAttr<ThisCallAttr>())
181 return CC_X86ThisCall;
182
Reid Klecknerd7857f02014-10-24 17:42:17 +0000183 if (D->hasAttr<VectorCallAttr>())
184 return CC_X86VectorCall;
185
Dawn Perchik335e16b2010-09-03 01:29:35 +0000186 if (D->hasAttr<PascalAttr>())
187 return CC_X86Pascal;
188
Anton Korobeynikov231e8752011-04-14 20:06:49 +0000189 if (PcsAttr *PCS = D->getAttr<PcsAttr>())
190 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
191
Guy Benyeif0a014b2012-12-25 08:53:55 +0000192 if (D->hasAttr<IntelOclBiccAttr>())
193 return CC_IntelOclBicc;
194
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000195 if (D->hasAttr<MSABIAttr>())
196 return IsWindows ? CC_C : CC_X86_64Win64;
197
198 if (D->hasAttr<SysVABIAttr>())
199 return IsWindows ? CC_X86_64SysV : CC_C;
200
Roman Levenstein35aa5ce2016-03-16 18:00:46 +0000201 if (D->hasAttr<PreserveMostAttr>())
202 return CC_PreserveMost;
203
204 if (D->hasAttr<PreserveAllAttr>())
205 return CC_PreserveAll;
206
John McCallab26cfa2010-02-05 21:31:56 +0000207 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000208}
209
John McCalla729c622012-02-17 03:33:10 +0000210/// Arrange the argument and result information for a call to an
211/// unknown C++ non-static member function of the given abstract type.
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000212/// (Zero value of RD means we don't have any meaningful "this" argument type,
213/// so fall back to a generic pointer type).
John McCalla729c622012-02-17 03:33:10 +0000214/// The member function must be an ordinary function, i.e. not a
215/// constructor or destructor.
216const CGFunctionInfo &
217CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000218 const FunctionProtoType *FTP,
219 const CXXMethodDecl *MD) {
John McCalla729c622012-02-17 03:33:10 +0000220 SmallVector<CanQualType, 16> argTypes;
John McCall8ee376f2010-02-24 07:14:12 +0000221
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000222 // Add the 'this' pointer.
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000223 if (RD)
224 argTypes.push_back(GetThisType(Context, RD));
225 else
226 argTypes.push_back(Context.VoidPtrTy);
John McCall8ee376f2010-02-24 07:14:12 +0000227
Alexey Samsonove5ef3ca2014-08-13 23:55:54 +0000228 return ::arrangeLLVMFunctionInfo(
229 *this, true, argTypes,
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000230 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>(), MD);
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000231}
232
John McCalla729c622012-02-17 03:33:10 +0000233/// Arrange the argument and result information for a declaration or
234/// definition of the given C++ non-static member function. The
235/// member function must be an ordinary function, i.e. not a
236/// constructor or destructor.
237const CGFunctionInfo &
238CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
Benjamin Kramer60509af2013-09-09 14:48:42 +0000239 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
John McCall0d635f52010-09-03 01:26:39 +0000240 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
241
John McCalla729c622012-02-17 03:33:10 +0000242 CanQual<FunctionProtoType> prototype = GetFormalType(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000243
John McCalla729c622012-02-17 03:33:10 +0000244 if (MD->isInstance()) {
245 // The abstract case is perfectly fine.
Mark Lacey5ea993b2013-10-02 20:35:23 +0000246 const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000247 return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
John McCalla729c622012-02-17 03:33:10 +0000248 }
249
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000250 return arrangeFreeFunctionType(prototype, MD);
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000251}
252
Richard Smith5179eb72016-06-28 19:03:57 +0000253bool CodeGenTypes::inheritingCtorHasParams(
254 const InheritedConstructor &Inherited, CXXCtorType Type) {
255 // Parameters are unnecessary if we're constructing a base class subobject
256 // and the inherited constructor lives in a virtual base.
257 return Type == Ctor_Complete ||
258 !Inherited.getShadowDecl()->constructsVirtualBase() ||
259 !Target.getCXXABI().hasConstructorVariants();
260 }
261
John McCalla729c622012-02-17 03:33:10 +0000262const CGFunctionInfo &
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000263CodeGenTypes::arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
264 StructorType Type) {
265
John McCalla729c622012-02-17 03:33:10 +0000266 SmallVector<CanQualType, 16> argTypes;
John McCallc56a8b32016-03-11 04:30:31 +0000267 SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000268 argTypes.push_back(GetThisType(Context, MD->getParent()));
Stephen Lin9dc6eef2013-06-30 20:40:16 +0000269
Richard Smith5179eb72016-06-28 19:03:57 +0000270 bool PassParams = true;
271
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000272 GlobalDecl GD;
273 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
274 GD = GlobalDecl(CD, toCXXCtorType(Type));
Richard Smith5179eb72016-06-28 19:03:57 +0000275
276 // A base class inheriting constructor doesn't get forwarded arguments
277 // needed to construct a virtual base (or base class thereof).
278 if (auto Inherited = CD->getInheritedConstructor())
279 PassParams = inheritingCtorHasParams(Inherited, toCXXCtorType(Type));
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000280 } else {
281 auto *DD = dyn_cast<CXXDestructorDecl>(MD);
282 GD = GlobalDecl(DD, toCXXDtorType(Type));
283 }
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000284
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000285 CanQual<FunctionProtoType> FTP = GetFormalType(MD);
John McCall5d865c322010-08-31 07:33:07 +0000286
287 // Add the formal parameters.
Richard Smith5179eb72016-06-28 19:03:57 +0000288 if (PassParams)
289 appendParameterTypes(*this, argTypes, paramInfos, FTP, MD);
John McCall5d865c322010-08-31 07:33:07 +0000290
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000291 TheCXXABI.buildStructorSignature(MD, Type, argTypes);
Reid Kleckner89077a12013-12-17 19:46:40 +0000292
293 RequiredArgs required =
Richard Smith5179eb72016-06-28 19:03:57 +0000294 (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
295 : RequiredArgs::All);
Reid Kleckner89077a12013-12-17 19:46:40 +0000296
John McCall8dda7b22012-07-07 06:41:13 +0000297 FunctionType::ExtInfo extInfo = FTP->getExtInfo();
David Majnemer0c0b6d92014-10-31 20:09:12 +0000298 CanQualType resultType = TheCXXABI.HasThisReturn(GD)
299 ? argTypes.front()
300 : TheCXXABI.hasMostDerivedReturn(GD)
301 ? CGM.getContext().VoidPtrTy
302 : Context.VoidTy;
Peter Collingbournef7706832014-12-12 23:41:25 +0000303 return arrangeLLVMFunctionInfo(resultType, /*instanceMethod=*/true,
304 /*chainCall=*/false, argTypes, extInfo,
John McCallc56a8b32016-03-11 04:30:31 +0000305 paramInfos, required);
306}
307
308static SmallVector<CanQualType, 16>
309getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {
310 SmallVector<CanQualType, 16> argTypes;
311 for (auto &arg : args)
312 argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
313 return argTypes;
314}
315
316static SmallVector<CanQualType, 16>
317getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) {
318 SmallVector<CanQualType, 16> argTypes;
319 for (auto &arg : args)
320 argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
321 return argTypes;
322}
323
324static void addExtParameterInfosForCall(
325 llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
326 const FunctionProtoType *proto,
327 unsigned prefixArgs,
328 unsigned totalArgs) {
329 assert(proto->hasExtParameterInfos());
330 assert(paramInfos.size() <= prefixArgs);
331 assert(proto->getNumParams() + prefixArgs <= totalArgs);
332
333 // Add default infos for any prefix args that don't already have infos.
334 paramInfos.resize(prefixArgs);
335
336 // Add infos for the prototype.
337 auto protoInfos = proto->getExtParameterInfos();
338 paramInfos.append(protoInfos.begin(), protoInfos.end());
339
340 // Add default infos for the variadic arguments.
341 paramInfos.resize(totalArgs);
342}
343
344static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16>
345getExtParameterInfosForCall(const FunctionProtoType *proto,
346 unsigned prefixArgs, unsigned totalArgs) {
347 llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result;
348 if (proto->hasExtParameterInfos()) {
349 addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
350 }
351 return result;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000352}
353
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000354/// Arrange a call to a C++ method, passing the given arguments.
355const CGFunctionInfo &
356CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
357 const CXXConstructorDecl *D,
358 CXXCtorType CtorKind,
359 unsigned ExtraArgs) {
360 // FIXME: Kill copy.
361 SmallVector<CanQualType, 16> ArgTypes;
Alexey Samsonov3551e312014-08-13 20:06:24 +0000362 for (const auto &Arg : args)
363 ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000364
365 CanQual<FunctionProtoType> FPT = GetFormalType(D);
George Burgess IV419996c2016-06-16 23:06:04 +0000366 RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs, D);
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000367 GlobalDecl GD(D, CtorKind);
David Majnemer0c0b6d92014-10-31 20:09:12 +0000368 CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
369 ? ArgTypes.front()
370 : TheCXXABI.hasMostDerivedReturn(GD)
371 ? CGM.getContext().VoidPtrTy
372 : Context.VoidTy;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000373
374 FunctionType::ExtInfo Info = FPT->getExtInfo();
John McCallc56a8b32016-03-11 04:30:31 +0000375 auto ParamInfos = getExtParameterInfosForCall(FPT.getTypePtr(), 1 + ExtraArgs,
376 ArgTypes.size());
Peter Collingbournef7706832014-12-12 23:41:25 +0000377 return arrangeLLVMFunctionInfo(ResultType, /*instanceMethod=*/true,
378 /*chainCall=*/false, ArgTypes, Info,
John McCallc56a8b32016-03-11 04:30:31 +0000379 ParamInfos, Required);
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000380}
381
John McCalla729c622012-02-17 03:33:10 +0000382/// Arrange the argument and result information for the declaration or
383/// definition of the given function.
384const CGFunctionInfo &
385CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000386 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000387 if (MD->isInstance())
John McCalla729c622012-02-17 03:33:10 +0000388 return arrangeCXXMethodDeclaration(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000389
John McCall2da83a32010-02-26 00:48:12 +0000390 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
John McCalla729c622012-02-17 03:33:10 +0000391
John McCall2da83a32010-02-26 00:48:12 +0000392 assert(isa<FunctionType>(FTy));
John McCalla729c622012-02-17 03:33:10 +0000393
394 // When declaring a function without a prototype, always use a
395 // non-variadic type.
396 if (isa<FunctionNoProtoType>(FTy)) {
397 CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
Peter Collingbournef7706832014-12-12 23:41:25 +0000398 return arrangeLLVMFunctionInfo(
399 noProto->getReturnType(), /*instanceMethod=*/false,
John McCallc56a8b32016-03-11 04:30:31 +0000400 /*chainCall=*/false, None, noProto->getExtInfo(), {},RequiredArgs::All);
John McCalla729c622012-02-17 03:33:10 +0000401 }
402
John McCall2da83a32010-02-26 00:48:12 +0000403 assert(isa<FunctionProtoType>(FTy));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +0000404 return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>(), FD);
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000405}
406
John McCalla729c622012-02-17 03:33:10 +0000407/// Arrange the argument and result information for the declaration or
408/// definition of an Objective-C method.
409const CGFunctionInfo &
410CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
411 // It happens that this is the same as a call with no optional
412 // arguments, except also using the formal 'self' type.
413 return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
414}
415
416/// Arrange the argument and result information for the function type
417/// through which to perform a send to the given Objective-C method,
418/// using the given receiver type. The receiver type is not always
419/// the 'self' type of the method or even an Objective-C pointer type.
420/// This is *not* the right method for actually performing such a
421/// message send, due to the possibility of optional arguments.
422const CGFunctionInfo &
423CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
424 QualType receiverType) {
425 SmallVector<CanQualType, 16> argTys;
426 argTys.push_back(Context.getCanonicalParamType(receiverType));
427 argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000428 // FIXME: Kill copy?
David Majnemer59f77922016-06-24 04:05:48 +0000429 for (const auto *I : MD->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +0000430 argTys.push_back(Context.getCanonicalParamType(I->getType()));
John McCall8ee376f2010-02-24 07:14:12 +0000431 }
John McCall31168b02011-06-15 23:02:42 +0000432
433 FunctionType::ExtInfo einfo;
Aaron Ballman0362a6d2013-12-18 16:23:37 +0000434 bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
435 einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
John McCall31168b02011-06-15 23:02:42 +0000436
David Blaikiebbafb8a2012-03-11 07:00:24 +0000437 if (getContext().getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +0000438 MD->hasAttr<NSReturnsRetainedAttr>())
439 einfo = einfo.withProducesResult(true);
440
John McCalla729c622012-02-17 03:33:10 +0000441 RequiredArgs required =
442 (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
443
Peter Collingbournef7706832014-12-12 23:41:25 +0000444 return arrangeLLVMFunctionInfo(
445 GetReturnType(MD->getReturnType()), /*instanceMethod=*/false,
John McCallc56a8b32016-03-11 04:30:31 +0000446 /*chainCall=*/false, argTys, einfo, {}, required);
447}
448
449const CGFunctionInfo &
450CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType,
451 const CallArgList &args) {
452 auto argTypes = getArgTypesForCall(Context, args);
453 FunctionType::ExtInfo einfo;
454
455 return arrangeLLVMFunctionInfo(
456 GetReturnType(returnType), /*instanceMethod=*/false,
457 /*chainCall=*/false, argTypes, einfo, {}, RequiredArgs::All);
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000458}
459
John McCalla729c622012-02-17 03:33:10 +0000460const CGFunctionInfo &
461CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
Anders Carlsson6710c532010-02-06 02:44:09 +0000462 // FIXME: Do we need to handle ObjCMethodDecl?
463 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000464
Anders Carlsson6710c532010-02-06 02:44:09 +0000465 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000466 return arrangeCXXStructorDeclaration(CD, getFromCtorType(GD.getCtorType()));
Anders Carlsson6710c532010-02-06 02:44:09 +0000467
468 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000469 return arrangeCXXStructorDeclaration(DD, getFromDtorType(GD.getDtorType()));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000470
John McCalla729c622012-02-17 03:33:10 +0000471 return arrangeFunctionDeclaration(FD);
Anders Carlsson6710c532010-02-06 02:44:09 +0000472}
473
Reid Klecknerc3473512014-08-29 21:43:29 +0000474/// Arrange a thunk that takes 'this' as the first parameter followed by
475/// varargs. Return a void pointer, regardless of the actual return type.
476/// The body of the thunk will end in a musttail call to a function of the
477/// correct type, and the caller will bitcast the function to the correct
478/// prototype.
479const CGFunctionInfo &
480CodeGenTypes::arrangeMSMemberPointerThunk(const CXXMethodDecl *MD) {
481 assert(MD->isVirtual() && "only virtual memptrs have thunks");
482 CanQual<FunctionProtoType> FTP = GetFormalType(MD);
483 CanQualType ArgTys[] = { GetThisType(Context, MD->getParent()) };
Peter Collingbournef7706832014-12-12 23:41:25 +0000484 return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/false,
485 /*chainCall=*/false, ArgTys,
John McCallc56a8b32016-03-11 04:30:31 +0000486 FTP->getExtInfo(), {}, RequiredArgs(1));
Reid Klecknerc3473512014-08-29 21:43:29 +0000487}
488
David Majnemerdfa6d202015-03-11 18:36:39 +0000489const CGFunctionInfo &
David Majnemer37fd66e2015-03-13 22:36:55 +0000490CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD,
491 CXXCtorType CT) {
492 assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
493
David Majnemerdfa6d202015-03-11 18:36:39 +0000494 CanQual<FunctionProtoType> FTP = GetFormalType(CD);
495 SmallVector<CanQualType, 2> ArgTys;
496 const CXXRecordDecl *RD = CD->getParent();
497 ArgTys.push_back(GetThisType(Context, RD));
David Majnemer37fd66e2015-03-13 22:36:55 +0000498 if (CT == Ctor_CopyingClosure)
499 ArgTys.push_back(*FTP->param_type_begin());
David Majnemerdfa6d202015-03-11 18:36:39 +0000500 if (RD->getNumVBases() > 0)
501 ArgTys.push_back(Context.IntTy);
502 CallingConv CC = Context.getDefaultCallingConvention(
503 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
504 return arrangeLLVMFunctionInfo(Context.VoidTy, /*instanceMethod=*/true,
505 /*chainCall=*/false, ArgTys,
John McCallc56a8b32016-03-11 04:30:31 +0000506 FunctionType::ExtInfo(CC), {},
507 RequiredArgs::All);
David Majnemerdfa6d202015-03-11 18:36:39 +0000508}
509
John McCallc818bbb2012-12-07 07:03:17 +0000510/// Arrange a call as unto a free function, except possibly with an
511/// additional number of formal parameters considered required.
512static const CGFunctionInfo &
513arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
Mark Lacey23455752013-10-10 20:57:00 +0000514 CodeGenModule &CGM,
John McCallc818bbb2012-12-07 07:03:17 +0000515 const CallArgList &args,
516 const FunctionType *fnType,
Peter Collingbournef7706832014-12-12 23:41:25 +0000517 unsigned numExtraRequiredArgs,
518 bool chainCall) {
John McCallc818bbb2012-12-07 07:03:17 +0000519 assert(args.size() >= numExtraRequiredArgs);
520
John McCallc56a8b32016-03-11 04:30:31 +0000521 llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
522
John McCallc818bbb2012-12-07 07:03:17 +0000523 // In most cases, there are no optional arguments.
524 RequiredArgs required = RequiredArgs::All;
525
526 // If we have a variadic prototype, the required arguments are the
527 // extra prefix plus the arguments in the prototype.
528 if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
529 if (proto->isVariadic())
Alp Toker9cacbab2014-01-20 20:26:09 +0000530 required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs);
John McCallc818bbb2012-12-07 07:03:17 +0000531
John McCallc56a8b32016-03-11 04:30:31 +0000532 if (proto->hasExtParameterInfos())
533 addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
534 args.size());
535
John McCallc818bbb2012-12-07 07:03:17 +0000536 // If we don't have a prototype at all, but we're supposed to
537 // explicitly use the variadic convention for unprototyped calls,
538 // treat all of the arguments as required but preserve the nominal
539 // possibility of variadics.
Mark Lacey23455752013-10-10 20:57:00 +0000540 } else if (CGM.getTargetCodeGenInfo()
541 .isNoProtoCallVariadic(args,
542 cast<FunctionNoProtoType>(fnType))) {
John McCallc818bbb2012-12-07 07:03:17 +0000543 required = RequiredArgs(args.size());
544 }
545
Peter Collingbournef7706832014-12-12 23:41:25 +0000546 // FIXME: Kill copy.
547 SmallVector<CanQualType, 16> argTypes;
548 for (const auto &arg : args)
549 argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
550 return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()),
551 /*instanceMethod=*/false, chainCall,
John McCallc56a8b32016-03-11 04:30:31 +0000552 argTypes, fnType->getExtInfo(), paramInfos,
553 required);
John McCallc818bbb2012-12-07 07:03:17 +0000554}
555
John McCalla729c622012-02-17 03:33:10 +0000556/// Figure out the rules for calling a function with the given formal
557/// type using the given arguments. The arguments are necessary
558/// because the function might be unprototyped, in which case it's
559/// target-dependent in crazy ways.
560const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000561CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
Peter Collingbournef7706832014-12-12 23:41:25 +0000562 const FunctionType *fnType,
563 bool chainCall) {
564 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
565 chainCall ? 1 : 0, chainCall);
John McCallc818bbb2012-12-07 07:03:17 +0000566}
John McCalla729c622012-02-17 03:33:10 +0000567
John McCallc56a8b32016-03-11 04:30:31 +0000568/// A block function is essentially a free function with an
John McCallc818bbb2012-12-07 07:03:17 +0000569/// extra implicit argument.
570const CGFunctionInfo &
571CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
572 const FunctionType *fnType) {
Peter Collingbournef7706832014-12-12 23:41:25 +0000573 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
574 /*chainCall=*/false);
John McCalla729c622012-02-17 03:33:10 +0000575}
576
577const CGFunctionInfo &
John McCallc56a8b32016-03-11 04:30:31 +0000578CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto,
579 const FunctionArgList &params) {
580 auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());
581 auto argTypes = getArgTypesForDeclaration(Context, params);
582
George Burgess IV419996c2016-06-16 23:06:04 +0000583 return arrangeLLVMFunctionInfo(
584 GetReturnType(proto->getReturnType()),
585 /*instanceMethod*/ false, /*chainCall*/ false, argTypes,
586 proto->getExtInfo(), paramInfos,
587 RequiredArgs::forPrototypePlus(proto, 1, nullptr));
John McCallc56a8b32016-03-11 04:30:31 +0000588}
589
590const CGFunctionInfo &
591CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType,
592 const CallArgList &args) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000593 // FIXME: Kill copy.
John McCalla729c622012-02-17 03:33:10 +0000594 SmallVector<CanQualType, 16> argTypes;
Alexey Samsonov3551e312014-08-13 20:06:24 +0000595 for (const auto &Arg : args)
596 argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
Peter Collingbournef7706832014-12-12 23:41:25 +0000597 return arrangeLLVMFunctionInfo(
598 GetReturnType(resultType), /*instanceMethod=*/false,
John McCallc56a8b32016-03-11 04:30:31 +0000599 /*chainCall=*/false, argTypes, FunctionType::ExtInfo(),
600 /*paramInfos=*/ {}, RequiredArgs::All);
John McCall8dda7b22012-07-07 06:41:13 +0000601}
602
John McCallc56a8b32016-03-11 04:30:31 +0000603const CGFunctionInfo &
604CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType,
605 const FunctionArgList &args) {
606 auto argTypes = getArgTypesForDeclaration(Context, args);
607
608 return arrangeLLVMFunctionInfo(
609 GetReturnType(resultType), /*instanceMethod=*/false, /*chainCall=*/false,
610 argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
611}
612
613const CGFunctionInfo &
614CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType,
615 ArrayRef<CanQualType> argTypes) {
616 return arrangeLLVMFunctionInfo(
617 resultType, /*instanceMethod=*/false, /*chainCall=*/false,
618 argTypes, FunctionType::ExtInfo(), {}, RequiredArgs::All);
619}
620
John McCall8dda7b22012-07-07 06:41:13 +0000621/// Arrange a call to a C++ method, passing the given arguments.
622const CGFunctionInfo &
623CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
John McCallc56a8b32016-03-11 04:30:31 +0000624 const FunctionProtoType *proto,
John McCall8dda7b22012-07-07 06:41:13 +0000625 RequiredArgs required) {
John McCallc56a8b32016-03-11 04:30:31 +0000626 unsigned numRequiredArgs =
627 (proto->isVariadic() ? required.getNumRequiredArgs() : args.size());
628 unsigned numPrefixArgs = numRequiredArgs - proto->getNumParams();
629 auto paramInfos =
630 getExtParameterInfosForCall(proto, numPrefixArgs, args.size());
631
John McCall8dda7b22012-07-07 06:41:13 +0000632 // FIXME: Kill copy.
John McCallc56a8b32016-03-11 04:30:31 +0000633 auto argTypes = getArgTypesForCall(Context, args);
John McCall8dda7b22012-07-07 06:41:13 +0000634
John McCallc56a8b32016-03-11 04:30:31 +0000635 FunctionType::ExtInfo info = proto->getExtInfo();
Peter Collingbournef7706832014-12-12 23:41:25 +0000636 return arrangeLLVMFunctionInfo(
John McCallc56a8b32016-03-11 04:30:31 +0000637 GetReturnType(proto->getReturnType()), /*instanceMethod=*/true,
638 /*chainCall=*/false, argTypes, info, paramInfos, required);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000639}
640
John McCalla729c622012-02-17 03:33:10 +0000641const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
Peter Collingbournef7706832014-12-12 23:41:25 +0000642 return arrangeLLVMFunctionInfo(
643 getContext().VoidTy, /*instanceMethod=*/false, /*chainCall=*/false,
John McCallc56a8b32016-03-11 04:30:31 +0000644 None, FunctionType::ExtInfo(), {}, RequiredArgs::All);
645}
646
647const CGFunctionInfo &
648CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,
649 const CallArgList &args) {
650 assert(signature.arg_size() <= args.size());
651 if (signature.arg_size() == args.size())
652 return signature;
653
654 SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
655 auto sigParamInfos = signature.getExtParameterInfos();
656 if (!sigParamInfos.empty()) {
657 paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
658 paramInfos.resize(args.size());
659 }
660
661 auto argTypes = getArgTypesForCall(Context, args);
662
663 assert(signature.getRequiredArgs().allowsOptionalArgs());
664 return arrangeLLVMFunctionInfo(signature.getReturnType(),
665 signature.isInstanceMethod(),
666 signature.isChainCall(),
667 argTypes,
668 signature.getExtInfo(),
669 paramInfos,
670 signature.getRequiredArgs());
John McCalla738c252011-03-09 04:27:21 +0000671}
672
John McCalla729c622012-02-17 03:33:10 +0000673/// Arrange the argument and result information for an abstract value
674/// of a given function type. This is the method which all of the
675/// above functions ultimately defer to.
676const CGFunctionInfo &
John McCall8dda7b22012-07-07 06:41:13 +0000677CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
Peter Collingbournef7706832014-12-12 23:41:25 +0000678 bool instanceMethod,
679 bool chainCall,
John McCall8dda7b22012-07-07 06:41:13 +0000680 ArrayRef<CanQualType> argTypes,
681 FunctionType::ExtInfo info,
John McCallc56a8b32016-03-11 04:30:31 +0000682 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
John McCall8dda7b22012-07-07 06:41:13 +0000683 RequiredArgs required) {
Saleem Abdulrasool32d1a962014-11-25 03:49:50 +0000684 assert(std::all_of(argTypes.begin(), argTypes.end(),
685 std::mem_fun_ref(&CanQualType::isCanonicalAsParam)));
John McCall2da83a32010-02-26 00:48:12 +0000686
Daniel Dunbare0be8292009-02-03 00:07:12 +0000687 // Lookup or create unique function info.
688 llvm::FoldingSetNodeID ID;
John McCallc56a8b32016-03-11 04:30:31 +0000689 CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos,
690 required, resultType, argTypes);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000691
Craig Topper8a13c412014-05-21 05:09:00 +0000692 void *insertPos = nullptr;
John McCalla729c622012-02-17 03:33:10 +0000693 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
Daniel Dunbare0be8292009-02-03 00:07:12 +0000694 if (FI)
695 return *FI;
696
John McCallc56a8b32016-03-11 04:30:31 +0000697 unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
698
John McCalla729c622012-02-17 03:33:10 +0000699 // Construct the function info. We co-allocate the ArgInfos.
Peter Collingbournef7706832014-12-12 23:41:25 +0000700 FI = CGFunctionInfo::create(CC, instanceMethod, chainCall, info,
John McCallc56a8b32016-03-11 04:30:31 +0000701 paramInfos, resultType, argTypes, required);
John McCalla729c622012-02-17 03:33:10 +0000702 FunctionInfos.InsertNode(FI, insertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000703
David Blaikie82e95a32014-11-19 07:49:47 +0000704 bool inserted = FunctionsBeingProcessed.insert(FI).second;
705 (void)inserted;
John McCalla729c622012-02-17 03:33:10 +0000706 assert(inserted && "Recursively being processed?");
Chris Lattner6fb0ccf2011-07-15 05:16:14 +0000707
Daniel Dunbar313321e2009-02-03 05:31:23 +0000708 // Compute ABI information.
John McCall12f23522016-04-04 18:33:08 +0000709 if (info.getCC() != CC_Swift) {
710 getABIInfo().computeInfo(*FI);
711 } else {
712 swiftcall::computeABIInfo(CGM, *FI);
713 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000714
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000715 // Loop over all of the computed argument and return value info. If any of
716 // them are direct or extend without a specified coerce type, specify the
717 // default now.
John McCalla729c622012-02-17 03:33:10 +0000718 ABIArgInfo &retInfo = FI->getReturnInfo();
Craig Topper8a13c412014-05-21 05:09:00 +0000719 if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
John McCalla729c622012-02-17 03:33:10 +0000720 retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000721
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000722 for (auto &I : FI->arguments())
Craig Topper8a13c412014-05-21 05:09:00 +0000723 if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
Aaron Ballmanec47bc22014-03-17 18:10:01 +0000724 I.info.setCoerceToType(ConvertType(I.type));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000725
John McCalla729c622012-02-17 03:33:10 +0000726 bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
727 assert(erased && "Not in set?");
Chris Lattner1a651332011-07-15 06:41:05 +0000728
Daniel Dunbare0be8292009-02-03 00:07:12 +0000729 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000730}
731
John McCalla729c622012-02-17 03:33:10 +0000732CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
Peter Collingbournef7706832014-12-12 23:41:25 +0000733 bool instanceMethod,
734 bool chainCall,
John McCalla729c622012-02-17 03:33:10 +0000735 const FunctionType::ExtInfo &info,
John McCallc56a8b32016-03-11 04:30:31 +0000736 ArrayRef<ExtParameterInfo> paramInfos,
John McCalla729c622012-02-17 03:33:10 +0000737 CanQualType resultType,
738 ArrayRef<CanQualType> argTypes,
739 RequiredArgs required) {
John McCallc56a8b32016-03-11 04:30:31 +0000740 assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
741
742 void *buffer =
743 operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(
744 argTypes.size() + 1, paramInfos.size()));
745
John McCalla729c622012-02-17 03:33:10 +0000746 CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
747 FI->CallingConvention = llvmCC;
748 FI->EffectiveCallingConvention = llvmCC;
749 FI->ASTCallingConvention = info.getCC();
Peter Collingbournef7706832014-12-12 23:41:25 +0000750 FI->InstanceMethod = instanceMethod;
751 FI->ChainCall = chainCall;
John McCalla729c622012-02-17 03:33:10 +0000752 FI->NoReturn = info.getNoReturn();
753 FI->ReturnsRetained = info.getProducesResult();
754 FI->Required = required;
755 FI->HasRegParm = info.getHasRegParm();
756 FI->RegParm = info.getRegParm();
Craig Topper8a13c412014-05-21 05:09:00 +0000757 FI->ArgStruct = nullptr;
John McCall7f416cc2015-09-08 08:05:57 +0000758 FI->ArgStructAlign = 0;
John McCalla729c622012-02-17 03:33:10 +0000759 FI->NumArgs = argTypes.size();
John McCallc56a8b32016-03-11 04:30:31 +0000760 FI->HasExtParameterInfos = !paramInfos.empty();
John McCalla729c622012-02-17 03:33:10 +0000761 FI->getArgsBuffer()[0].type = resultType;
762 for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
763 FI->getArgsBuffer()[i + 1].type = argTypes[i];
John McCallc56a8b32016-03-11 04:30:31 +0000764 for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)
765 FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
John McCalla729c622012-02-17 03:33:10 +0000766 return FI;
Daniel Dunbar313321e2009-02-03 05:31:23 +0000767}
768
769/***/
770
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000771namespace {
772// ABIArgInfo::Expand implementation.
773
774// Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
775struct TypeExpansion {
776 enum TypeExpansionKind {
777 // Elements of constant arrays are expanded recursively.
778 TEK_ConstantArray,
779 // Record fields are expanded recursively (but if record is a union, only
780 // the field with the largest size is expanded).
781 TEK_Record,
782 // For complex types, real and imaginary parts are expanded recursively.
783 TEK_Complex,
784 // All other types are not expandable.
785 TEK_None
786 };
787
788 const TypeExpansionKind Kind;
789
790 TypeExpansion(TypeExpansionKind K) : Kind(K) {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000791 virtual ~TypeExpansion() {}
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000792};
793
794struct ConstantArrayExpansion : TypeExpansion {
795 QualType EltTy;
796 uint64_t NumElts;
797
798 ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
799 : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
800 static bool classof(const TypeExpansion *TE) {
801 return TE->Kind == TEK_ConstantArray;
802 }
803};
804
805struct RecordExpansion : TypeExpansion {
Reid Klecknere9f6a712014-10-31 17:10:41 +0000806 SmallVector<const CXXBaseSpecifier *, 1> Bases;
807
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000808 SmallVector<const FieldDecl *, 1> Fields;
809
Reid Klecknere9f6a712014-10-31 17:10:41 +0000810 RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
811 SmallVector<const FieldDecl *, 1> &&Fields)
Benjamin Kramer0bb97742016-02-13 16:00:13 +0000812 : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
813 Fields(std::move(Fields)) {}
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000814 static bool classof(const TypeExpansion *TE) {
815 return TE->Kind == TEK_Record;
816 }
817};
818
819struct ComplexExpansion : TypeExpansion {
820 QualType EltTy;
821
822 ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
823 static bool classof(const TypeExpansion *TE) {
824 return TE->Kind == TEK_Complex;
825 }
826};
827
828struct NoExpansion : TypeExpansion {
829 NoExpansion() : TypeExpansion(TEK_None) {}
830 static bool classof(const TypeExpansion *TE) {
831 return TE->Kind == TEK_None;
832 }
833};
834} // namespace
835
836static std::unique_ptr<TypeExpansion>
837getTypeExpansion(QualType Ty, const ASTContext &Context) {
838 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
839 return llvm::make_unique<ConstantArrayExpansion>(
840 AT->getElementType(), AT->getSize().getZExtValue());
841 }
842 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Reid Klecknere9f6a712014-10-31 17:10:41 +0000843 SmallVector<const CXXBaseSpecifier *, 1> Bases;
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000844 SmallVector<const FieldDecl *, 1> Fields;
Bob Wilsone826a2a2011-08-03 05:58:22 +0000845 const RecordDecl *RD = RT->getDecl();
846 assert(!RD->hasFlexibleArrayMember() &&
847 "Cannot expand structure with flexible array.");
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000848 if (RD->isUnion()) {
849 // Unions can be here only in degenerative cases - all the fields are same
850 // after flattening. Thus we have to use the "largest" field.
Craig Topper8a13c412014-05-21 05:09:00 +0000851 const FieldDecl *LargestFD = nullptr;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000852 CharUnits UnionSize = CharUnits::Zero();
853
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000854 for (const auto *FD : RD->fields()) {
Reid Kleckner80944df2014-10-31 22:00:51 +0000855 // Skip zero length bitfields.
856 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
857 continue;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000858 assert(!FD->isBitField() &&
859 "Cannot expand structure with bit-field members.");
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000860 CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000861 if (UnionSize < FieldSize) {
862 UnionSize = FieldSize;
863 LargestFD = FD;
864 }
865 }
866 if (LargestFD)
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000867 Fields.push_back(LargestFD);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000868 } else {
Reid Klecknere9f6a712014-10-31 17:10:41 +0000869 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
870 assert(!CXXRD->isDynamicClass() &&
871 "cannot expand vtable pointers in dynamic classes");
872 for (const CXXBaseSpecifier &BS : CXXRD->bases())
873 Bases.push_back(&BS);
874 }
875
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000876 for (const auto *FD : RD->fields()) {
Reid Kleckner80944df2014-10-31 22:00:51 +0000877 // Skip zero length bitfields.
878 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
879 continue;
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000880 assert(!FD->isBitField() &&
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000881 "Cannot expand structure with bit-field members.");
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000882 Fields.push_back(FD);
Anton Korobeynikov4215ca72012-04-13 11:22:00 +0000883 }
Bob Wilsone826a2a2011-08-03 05:58:22 +0000884 }
Reid Klecknere9f6a712014-10-31 17:10:41 +0000885 return llvm::make_unique<RecordExpansion>(std::move(Bases),
886 std::move(Fields));
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000887 }
888 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
889 return llvm::make_unique<ComplexExpansion>(CT->getElementType());
890 }
891 return llvm::make_unique<NoExpansion>();
892}
893
Alexey Samsonov52c0f6a2014-09-29 20:30:22 +0000894static int getExpansionSize(QualType Ty, const ASTContext &Context) {
895 auto Exp = getTypeExpansion(Ty, Context);
896 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
897 return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
898 }
899 if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
900 int Res = 0;
Reid Klecknere9f6a712014-10-31 17:10:41 +0000901 for (auto BS : RExp->Bases)
902 Res += getExpansionSize(BS->getType(), Context);
Alexey Samsonov52c0f6a2014-09-29 20:30:22 +0000903 for (auto FD : RExp->Fields)
904 Res += getExpansionSize(FD->getType(), Context);
905 return Res;
906 }
907 if (isa<ComplexExpansion>(Exp.get()))
908 return 2;
909 assert(isa<NoExpansion>(Exp.get()));
910 return 1;
911}
912
Alexey Samsonov153004f2014-09-29 22:08:00 +0000913void
914CodeGenTypes::getExpandedTypes(QualType Ty,
915 SmallVectorImpl<llvm::Type *>::iterator &TI) {
916 auto Exp = getTypeExpansion(Ty, Context);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000917 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
918 for (int i = 0, n = CAExp->NumElts; i < n; i++) {
Alexey Samsonov153004f2014-09-29 22:08:00 +0000919 getExpandedTypes(CAExp->EltTy, TI);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000920 }
921 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
Reid Klecknere9f6a712014-10-31 17:10:41 +0000922 for (auto BS : RExp->Bases)
923 getExpandedTypes(BS->getType(), TI);
924 for (auto FD : RExp->Fields)
Alexey Samsonov153004f2014-09-29 22:08:00 +0000925 getExpandedTypes(FD->getType(), TI);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000926 } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
927 llvm::Type *EltTy = ConvertType(CExp->EltTy);
Alexey Samsonov153004f2014-09-29 22:08:00 +0000928 *TI++ = EltTy;
929 *TI++ = EltTy;
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000930 } else {
931 assert(isa<NoExpansion>(Exp.get()));
Alexey Samsonov153004f2014-09-29 22:08:00 +0000932 *TI++ = ConvertType(Ty);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000933 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000934}
935
John McCall7f416cc2015-09-08 08:05:57 +0000936static void forConstantArrayExpansion(CodeGenFunction &CGF,
937 ConstantArrayExpansion *CAE,
938 Address BaseAddr,
939 llvm::function_ref<void(Address)> Fn) {
940 CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy);
941 CharUnits EltAlign =
942 BaseAddr.getAlignment().alignmentOfArrayElement(EltSize);
943
944 for (int i = 0, n = CAE->NumElts; i < n; i++) {
945 llvm::Value *EltAddr =
946 CGF.Builder.CreateConstGEP2_32(nullptr, BaseAddr.getPointer(), 0, i);
947 Fn(Address(EltAddr, EltAlign));
948 }
949}
950
Alexey Samsonov91cf4552014-08-22 01:06:06 +0000951void CodeGenFunction::ExpandTypeFromArgs(
John McCall12f23522016-04-04 18:33:08 +0000952 QualType Ty, LValue LV, SmallVectorImpl<llvm::Value *>::iterator &AI) {
Mike Stump11289f42009-09-09 15:08:12 +0000953 assert(LV.isSimple() &&
954 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000955
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000956 auto Exp = getTypeExpansion(Ty, getContext());
957 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
John McCall7f416cc2015-09-08 08:05:57 +0000958 forConstantArrayExpansion(*this, CAExp, LV.getAddress(),
959 [&](Address EltAddr) {
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000960 LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
961 ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
John McCall7f416cc2015-09-08 08:05:57 +0000962 });
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000963 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
John McCall7f416cc2015-09-08 08:05:57 +0000964 Address This = LV.getAddress();
Reid Klecknere9f6a712014-10-31 17:10:41 +0000965 for (const CXXBaseSpecifier *BS : RExp->Bases) {
966 // Perform a single step derived-to-base conversion.
John McCall7f416cc2015-09-08 08:05:57 +0000967 Address Base =
Reid Klecknere9f6a712014-10-31 17:10:41 +0000968 GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
969 /*NullCheckValue=*/false, SourceLocation());
970 LValue SubLV = MakeAddrLValue(Base, BS->getType());
971
972 // Recurse onto bases.
973 ExpandTypeFromArgs(BS->getType(), SubLV, AI);
974 }
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000975 for (auto FD : RExp->Fields) {
976 // FIXME: What are the right qualifiers here?
Reid Kleckner9d031092016-05-02 22:42:34 +0000977 LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000978 ExpandTypeFromArgs(FD->getType(), SubLV, AI);
Bob Wilsone826a2a2011-08-03 05:58:22 +0000979 }
John McCall7f416cc2015-09-08 08:05:57 +0000980 } else if (isa<ComplexExpansion>(Exp.get())) {
981 auto realValue = *AI++;
982 auto imagValue = *AI++;
983 EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000984 } else {
985 assert(isa<NoExpansion>(Exp.get()));
986 EmitStoreThroughLValue(RValue::get(*AI++), LV);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000987 }
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000988}
989
990void CodeGenFunction::ExpandTypeToArgs(
991 QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
992 SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
993 auto Exp = getTypeExpansion(Ty, getContext());
994 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
John McCall7f416cc2015-09-08 08:05:57 +0000995 forConstantArrayExpansion(*this, CAExp, RV.getAggregateAddress(),
996 [&](Address EltAddr) {
Alexey Samsonov8a0bad02014-09-29 18:41:28 +0000997 RValue EltRV =
998 convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation());
999 ExpandTypeToArgs(CAExp->EltTy, EltRV, IRFuncTy, IRCallArgs, IRCallArgPos);
John McCall7f416cc2015-09-08 08:05:57 +00001000 });
Alexey Samsonov8a0bad02014-09-29 18:41:28 +00001001 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
John McCall7f416cc2015-09-08 08:05:57 +00001002 Address This = RV.getAggregateAddress();
Reid Klecknere9f6a712014-10-31 17:10:41 +00001003 for (const CXXBaseSpecifier *BS : RExp->Bases) {
1004 // Perform a single step derived-to-base conversion.
John McCall7f416cc2015-09-08 08:05:57 +00001005 Address Base =
Reid Klecknere9f6a712014-10-31 17:10:41 +00001006 GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
1007 /*NullCheckValue=*/false, SourceLocation());
1008 RValue BaseRV = RValue::getAggregate(Base);
1009
1010 // Recurse onto bases.
1011 ExpandTypeToArgs(BS->getType(), BaseRV, IRFuncTy, IRCallArgs,
1012 IRCallArgPos);
1013 }
1014
1015 LValue LV = MakeAddrLValue(This, Ty);
Alexey Samsonov8a0bad02014-09-29 18:41:28 +00001016 for (auto FD : RExp->Fields) {
1017 RValue FldRV = EmitRValueForField(LV, FD, SourceLocation());
1018 ExpandTypeToArgs(FD->getType(), FldRV, IRFuncTy, IRCallArgs,
1019 IRCallArgPos);
1020 }
1021 } else if (isa<ComplexExpansion>(Exp.get())) {
1022 ComplexPairTy CV = RV.getComplexVal();
1023 IRCallArgs[IRCallArgPos++] = CV.first;
1024 IRCallArgs[IRCallArgPos++] = CV.second;
1025 } else {
1026 assert(isa<NoExpansion>(Exp.get()));
1027 assert(RV.isScalar() &&
1028 "Unexpected non-scalar rvalue during struct expansion.");
1029
1030 // Insert a bitcast as needed.
1031 llvm::Value *V = RV.getScalarVal();
1032 if (IRCallArgPos < IRFuncTy->getNumParams() &&
1033 V->getType() != IRFuncTy->getParamType(IRCallArgPos))
1034 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
1035
1036 IRCallArgs[IRCallArgPos++] = V;
1037 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001038}
1039
John McCall7f416cc2015-09-08 08:05:57 +00001040/// Create a temporary allocation for the purposes of coercion.
1041static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty,
1042 CharUnits MinAlign) {
1043 // Don't use an alignment that's worse than what LLVM would prefer.
1044 auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(Ty);
1045 CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
1046
1047 return CGF.CreateTempAlloca(Ty, Align);
1048}
1049
Chris Lattner895c52b2010-06-27 06:04:18 +00001050/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +00001051/// accessing some number of bytes out of it, try to gep into the struct to get
1052/// at its inner goodness. Dive as deep as possible without entering an element
1053/// with an in-memory size smaller than DstSize.
John McCall7f416cc2015-09-08 08:05:57 +00001054static Address
1055EnterStructPointerForCoercedAccess(Address SrcPtr,
Chris Lattner2192fe52011-07-18 04:24:23 +00001056 llvm::StructType *SrcSTy,
Chris Lattner895c52b2010-06-27 06:04:18 +00001057 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +00001058 // We can't dive into a zero-element struct.
1059 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001060
Chris Lattner2192fe52011-07-18 04:24:23 +00001061 llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001062
Chris Lattner1cd66982010-06-27 05:56:15 +00001063 // If the first elt is at least as large as what we're looking for, or if the
James Molloy90d61012014-08-29 10:17:52 +00001064 // first element is the same size as the whole struct, we can enter it. The
1065 // comparison must be made on the store size and not the alloca size. Using
1066 // the alloca size may overstate the size of the load.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001067 uint64_t FirstEltSize =
James Molloy90d61012014-08-29 10:17:52 +00001068 CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001069 if (FirstEltSize < DstSize &&
James Molloy90d61012014-08-29 10:17:52 +00001070 FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
Chris Lattner1cd66982010-06-27 05:56:15 +00001071 return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001072
Chris Lattner1cd66982010-06-27 05:56:15 +00001073 // GEP into the first element.
John McCall7f416cc2015-09-08 08:05:57 +00001074 SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, CharUnits(), "coerce.dive");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001075
Chris Lattner1cd66982010-06-27 05:56:15 +00001076 // If the first element is a struct, recurse.
John McCall7f416cc2015-09-08 08:05:57 +00001077 llvm::Type *SrcTy = SrcPtr.getElementType();
Chris Lattner2192fe52011-07-18 04:24:23 +00001078 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +00001079 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +00001080
1081 return SrcPtr;
1082}
1083
Chris Lattner055097f2010-06-27 06:26:04 +00001084/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
1085/// are either integers or pointers. This does a truncation of the value if it
1086/// is too large or a zero extension if it is too small.
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +00001087///
1088/// This behaves as if the value were coerced through memory, so on big-endian
1089/// targets the high bits are preserved in a truncation, while little-endian
1090/// targets preserve the low bits.
Chris Lattner055097f2010-06-27 06:26:04 +00001091static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
Chris Lattner2192fe52011-07-18 04:24:23 +00001092 llvm::Type *Ty,
Chris Lattner055097f2010-06-27 06:26:04 +00001093 CodeGenFunction &CGF) {
1094 if (Val->getType() == Ty)
1095 return Val;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001096
Chris Lattner055097f2010-06-27 06:26:04 +00001097 if (isa<llvm::PointerType>(Val->getType())) {
1098 // If this is Pointer->Pointer avoid conversion to and from int.
1099 if (isa<llvm::PointerType>(Ty))
1100 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001101
Chris Lattner055097f2010-06-27 06:26:04 +00001102 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +00001103 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +00001104 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001105
Chris Lattner2192fe52011-07-18 04:24:23 +00001106 llvm::Type *DestIntTy = Ty;
Chris Lattner055097f2010-06-27 06:26:04 +00001107 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +00001108 DestIntTy = CGF.IntPtrTy;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001109
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +00001110 if (Val->getType() != DestIntTy) {
1111 const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
1112 if (DL.isBigEndian()) {
1113 // Preserve the high bits on big-endian targets.
1114 // That is what memory coercion does.
James Molloy491cefb2014-05-07 17:41:15 +00001115 uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
1116 uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
1117
Jakob Stoklund Olesen36af2522013-06-05 03:00:13 +00001118 if (SrcSize > DstSize) {
1119 Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
1120 Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
1121 } else {
1122 Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
1123 Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
1124 }
1125 } else {
1126 // Little-endian targets preserve the low bits. No shifts required.
1127 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
1128 }
1129 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001130
Chris Lattner055097f2010-06-27 06:26:04 +00001131 if (isa<llvm::PointerType>(Ty))
1132 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
1133 return Val;
1134}
1135
Chris Lattner1cd66982010-06-27 05:56:15 +00001136
1137
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001138/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
Ulrich Weigand6e2cea62015-07-10 11:31:43 +00001139/// a pointer to an object of type \arg Ty, known to be aligned to
1140/// \arg SrcAlign bytes.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001141///
1142/// This safely handles the case when the src type is smaller than the
1143/// destination type; in this situation the values of bits which not
1144/// present in the src are undefined.
John McCall7f416cc2015-09-08 08:05:57 +00001145static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001146 CodeGenFunction &CGF) {
John McCall7f416cc2015-09-08 08:05:57 +00001147 llvm::Type *SrcTy = Src.getElementType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001148
Chris Lattnerd200eda2010-06-28 22:51:39 +00001149 // If SrcTy and Ty are the same, just do a load.
1150 if (SrcTy == Ty)
John McCall7f416cc2015-09-08 08:05:57 +00001151 return CGF.Builder.CreateLoad(Src);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001152
Micah Villmowdd31ca12012-10-08 16:25:52 +00001153 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001154
Chris Lattner2192fe52011-07-18 04:24:23 +00001155 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
John McCall7f416cc2015-09-08 08:05:57 +00001156 Src = EnterStructPointerForCoercedAccess(Src, SrcSTy, DstSize, CGF);
1157 SrcTy = Src.getType()->getElementType();
Chris Lattner1cd66982010-06-27 05:56:15 +00001158 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001159
Micah Villmowdd31ca12012-10-08 16:25:52 +00001160 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001161
Chris Lattner055097f2010-06-27 06:26:04 +00001162 // If the source and destination are integer or pointer types, just do an
1163 // extension or truncation to the desired type.
1164 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
1165 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
John McCall7f416cc2015-09-08 08:05:57 +00001166 llvm::Value *Load = CGF.Builder.CreateLoad(Src);
Chris Lattner055097f2010-06-27 06:26:04 +00001167 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
1168 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001169
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001170 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +00001171 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +00001172 // Generally SrcSize is never greater than DstSize, since this means we are
1173 // losing bits. However, this can happen in cases where the structure has
1174 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +00001175 //
Mike Stump18bb9282009-05-16 07:57:57 +00001176 // FIXME: Assert that we aren't truncating non-padding bits when have access
1177 // to that information.
John McCall7f416cc2015-09-08 08:05:57 +00001178 Src = CGF.Builder.CreateBitCast(Src, llvm::PointerType::getUnqual(Ty));
1179 return CGF.Builder.CreateLoad(Src);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001180 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001181
John McCall7f416cc2015-09-08 08:05:57 +00001182 // Otherwise do coercion through memory. This is stupid, but simple.
1183 Address Tmp = CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment());
1184 Address Casted = CGF.Builder.CreateBitCast(Tmp, CGF.Int8PtrTy);
1185 Address SrcCasted = CGF.Builder.CreateBitCast(Src, CGF.Int8PtrTy);
Manman Ren84b921f2012-11-28 22:08:52 +00001186 CGF.Builder.CreateMemCpy(Casted, SrcCasted,
1187 llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
John McCall7f416cc2015-09-08 08:05:57 +00001188 false);
1189 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001190}
1191
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001192// Function to store a first-class aggregate into memory. We prefer to
1193// store the elements rather than the aggregate to be more friendly to
1194// fast-isel.
1195// FIXME: Do we need to recurse here?
1196static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
John McCall7f416cc2015-09-08 08:05:57 +00001197 Address Dest, bool DestIsVolatile) {
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001198 // Prefer scalar stores to first-class aggregate stores.
Chris Lattner2192fe52011-07-18 04:24:23 +00001199 if (llvm::StructType *STy =
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001200 dyn_cast<llvm::StructType>(Val->getType())) {
Ulrich Weigand6e2cea62015-07-10 11:31:43 +00001201 const llvm::StructLayout *Layout =
1202 CGF.CGM.getDataLayout().getStructLayout(STy);
1203
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001204 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00001205 auto EltOffset = CharUnits::fromQuantity(Layout->getElementOffset(i));
1206 Address EltPtr = CGF.Builder.CreateStructGEP(Dest, i, EltOffset);
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001207 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
John McCall7f416cc2015-09-08 08:05:57 +00001208 CGF.Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001209 }
1210 } else {
John McCall7f416cc2015-09-08 08:05:57 +00001211 CGF.Builder.CreateStore(Val, Dest, DestIsVolatile);
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001212 }
1213}
1214
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001215/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
Ulrich Weigand6e2cea62015-07-10 11:31:43 +00001216/// where the source and destination may have different types. The
1217/// destination is known to be aligned to \arg DstAlign bytes.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001218///
1219/// This safely handles the case when the src type is larger than the
1220/// destination type; the upper bits of the src will be lost.
1221static void CreateCoercedStore(llvm::Value *Src,
John McCall7f416cc2015-09-08 08:05:57 +00001222 Address Dst,
Anders Carlsson17490832009-12-24 20:40:36 +00001223 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001224 CodeGenFunction &CGF) {
Chris Lattner2192fe52011-07-18 04:24:23 +00001225 llvm::Type *SrcTy = Src->getType();
John McCall7f416cc2015-09-08 08:05:57 +00001226 llvm::Type *DstTy = Dst.getType()->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +00001227 if (SrcTy == DstTy) {
John McCall7f416cc2015-09-08 08:05:57 +00001228 CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
Chris Lattnerd200eda2010-06-28 22:51:39 +00001229 return;
1230 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001231
Micah Villmowdd31ca12012-10-08 16:25:52 +00001232 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001233
Chris Lattner2192fe52011-07-18 04:24:23 +00001234 if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
John McCall7f416cc2015-09-08 08:05:57 +00001235 Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy, SrcSize, CGF);
1236 DstTy = Dst.getType()->getElementType();
Chris Lattner895c52b2010-06-27 06:04:18 +00001237 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001238
Chris Lattner055097f2010-06-27 06:26:04 +00001239 // If the source and destination are integer or pointer types, just do an
1240 // extension or truncation to the desired type.
1241 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
1242 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
1243 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
John McCall7f416cc2015-09-08 08:05:57 +00001244 CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
Chris Lattner055097f2010-06-27 06:26:04 +00001245 return;
1246 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001247
Micah Villmowdd31ca12012-10-08 16:25:52 +00001248 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001249
Daniel Dunbar313321e2009-02-03 05:31:23 +00001250 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +00001251 if (SrcSize <= DstSize) {
John McCall7f416cc2015-09-08 08:05:57 +00001252 Dst = CGF.Builder.CreateBitCast(Dst, llvm::PointerType::getUnqual(SrcTy));
1253 BuildAggStore(CGF, Src, Dst, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001254 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001255 // Otherwise do coercion through memory. This is stupid, but
1256 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +00001257
1258 // Generally SrcSize is never greater than DstSize, since this means we are
1259 // losing bits. However, this can happen in cases where the structure has
1260 // additional padding, for example due to a user specified alignment.
1261 //
1262 // FIXME: Assert that we aren't truncating non-padding bits when have access
1263 // to that information.
John McCall7f416cc2015-09-08 08:05:57 +00001264 Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
1265 CGF.Builder.CreateStore(Src, Tmp);
1266 Address Casted = CGF.Builder.CreateBitCast(Tmp, CGF.Int8PtrTy);
1267 Address DstCasted = CGF.Builder.CreateBitCast(Dst, CGF.Int8PtrTy);
Manman Ren84b921f2012-11-28 22:08:52 +00001268 CGF.Builder.CreateMemCpy(DstCasted, Casted,
1269 llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
John McCall7f416cc2015-09-08 08:05:57 +00001270 false);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +00001271 }
1272}
1273
John McCall7f416cc2015-09-08 08:05:57 +00001274static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr,
1275 const ABIArgInfo &info) {
1276 if (unsigned offset = info.getDirectOffset()) {
1277 addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8Ty);
1278 addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,
1279 CharUnits::fromQuantity(offset));
1280 addr = CGF.Builder.CreateElementBitCast(addr, info.getCoerceToType());
1281 }
1282 return addr;
1283}
1284
Alexey Samsonov153004f2014-09-29 22:08:00 +00001285namespace {
1286
1287/// Encapsulates information about the way function arguments from
1288/// CGFunctionInfo should be passed to actual LLVM IR function.
1289class ClangToLLVMArgMapping {
1290 static const unsigned InvalidIndex = ~0U;
1291 unsigned InallocaArgNo;
1292 unsigned SRetArgNo;
1293 unsigned TotalIRArgs;
1294
1295 /// Arguments of LLVM IR function corresponding to single Clang argument.
1296 struct IRArgs {
1297 unsigned PaddingArgIndex;
1298 // Argument is expanded to IR arguments at positions
1299 // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
1300 unsigned FirstArgIndex;
1301 unsigned NumberOfArgs;
1302
1303 IRArgs()
1304 : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
1305 NumberOfArgs(0) {}
1306 };
1307
1308 SmallVector<IRArgs, 8> ArgInfo;
1309
1310public:
1311 ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
1312 bool OnlyRequiredArgs = false)
1313 : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
1314 ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
1315 construct(Context, FI, OnlyRequiredArgs);
1316 }
1317
1318 bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
1319 unsigned getInallocaArgNo() const {
1320 assert(hasInallocaArg());
1321 return InallocaArgNo;
1322 }
1323
1324 bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
1325 unsigned getSRetArgNo() const {
1326 assert(hasSRetArg());
1327 return SRetArgNo;
1328 }
1329
1330 unsigned totalIRArgs() const { return TotalIRArgs; }
1331
1332 bool hasPaddingArg(unsigned ArgNo) const {
1333 assert(ArgNo < ArgInfo.size());
1334 return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
1335 }
1336 unsigned getPaddingArgNo(unsigned ArgNo) const {
1337 assert(hasPaddingArg(ArgNo));
1338 return ArgInfo[ArgNo].PaddingArgIndex;
1339 }
1340
1341 /// Returns index of first IR argument corresponding to ArgNo, and their
1342 /// quantity.
1343 std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
1344 assert(ArgNo < ArgInfo.size());
1345 return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
1346 ArgInfo[ArgNo].NumberOfArgs);
1347 }
1348
1349private:
1350 void construct(const ASTContext &Context, const CGFunctionInfo &FI,
1351 bool OnlyRequiredArgs);
1352};
1353
1354void ClangToLLVMArgMapping::construct(const ASTContext &Context,
1355 const CGFunctionInfo &FI,
1356 bool OnlyRequiredArgs) {
1357 unsigned IRArgNo = 0;
1358 bool SwapThisWithSRet = false;
1359 const ABIArgInfo &RetAI = FI.getReturnInfo();
1360
1361 if (RetAI.getKind() == ABIArgInfo::Indirect) {
1362 SwapThisWithSRet = RetAI.isSRetAfterThis();
1363 SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
1364 }
1365
1366 unsigned ArgNo = 0;
1367 unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
1368 for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
1369 ++I, ++ArgNo) {
1370 assert(I != FI.arg_end());
1371 QualType ArgType = I->type;
1372 const ABIArgInfo &AI = I->info;
1373 // Collect data about IR arguments corresponding to Clang argument ArgNo.
1374 auto &IRArgs = ArgInfo[ArgNo];
1375
1376 if (AI.getPaddingType())
1377 IRArgs.PaddingArgIndex = IRArgNo++;
1378
1379 switch (AI.getKind()) {
1380 case ABIArgInfo::Extend:
1381 case ABIArgInfo::Direct: {
1382 // FIXME: handle sseregparm someday...
1383 llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
1384 if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
1385 IRArgs.NumberOfArgs = STy->getNumElements();
1386 } else {
1387 IRArgs.NumberOfArgs = 1;
1388 }
1389 break;
1390 }
1391 case ABIArgInfo::Indirect:
1392 IRArgs.NumberOfArgs = 1;
1393 break;
1394 case ABIArgInfo::Ignore:
1395 case ABIArgInfo::InAlloca:
1396 // ignore and inalloca doesn't have matching LLVM parameters.
1397 IRArgs.NumberOfArgs = 0;
1398 break;
John McCallf26e73d2016-03-11 04:30:43 +00001399 case ABIArgInfo::CoerceAndExpand:
1400 IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();
1401 break;
1402 case ABIArgInfo::Expand:
Alexey Samsonov153004f2014-09-29 22:08:00 +00001403 IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
1404 break;
1405 }
Alexey Samsonov153004f2014-09-29 22:08:00 +00001406
1407 if (IRArgs.NumberOfArgs > 0) {
1408 IRArgs.FirstArgIndex = IRArgNo;
1409 IRArgNo += IRArgs.NumberOfArgs;
1410 }
1411
1412 // Skip over the sret parameter when it comes second. We already handled it
1413 // above.
1414 if (IRArgNo == 1 && SwapThisWithSRet)
1415 IRArgNo++;
1416 }
1417 assert(ArgNo == ArgInfo.size());
1418
1419 if (FI.usesInAlloca())
1420 InallocaArgNo = IRArgNo++;
1421
1422 TotalIRArgs = IRArgNo;
1423}
1424} // namespace
1425
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001426/***/
1427
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001428bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001429 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001430}
1431
Tim Northovere77cc392014-03-29 13:28:05 +00001432bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
1433 return ReturnTypeUsesSRet(FI) &&
1434 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1435}
1436
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001437bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
1438 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
1439 switch (BT->getKind()) {
1440 default:
1441 return false;
1442 case BuiltinType::Float:
John McCallc8e01702013-04-16 22:48:15 +00001443 return getTarget().useObjCFPRetForRealType(TargetInfo::Float);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001444 case BuiltinType::Double:
John McCallc8e01702013-04-16 22:48:15 +00001445 return getTarget().useObjCFPRetForRealType(TargetInfo::Double);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001446 case BuiltinType::LongDouble:
John McCallc8e01702013-04-16 22:48:15 +00001447 return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001448 }
1449 }
1450
1451 return false;
1452}
1453
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001454bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
1455 if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
1456 if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
1457 if (BT->getKind() == BuiltinType::LongDouble)
John McCallc8e01702013-04-16 22:48:15 +00001458 return getTarget().useObjCFP2RetForComplexLongDouble();
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001459 }
1460 }
1461
1462 return false;
1463}
1464
Chris Lattnera5f58b02011-07-09 17:41:47 +00001465llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
John McCalla729c622012-02-17 03:33:10 +00001466 const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
1467 return GetFunctionType(FI);
John McCallf8ff7b92010-02-23 00:48:20 +00001468}
1469
Chris Lattnera5f58b02011-07-09 17:41:47 +00001470llvm::FunctionType *
John McCalla729c622012-02-17 03:33:10 +00001471CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
Alexey Samsonov153004f2014-09-29 22:08:00 +00001472
David Blaikie82e95a32014-11-19 07:49:47 +00001473 bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
1474 (void)Inserted;
Chris Lattner6fb0ccf2011-07-15 05:16:14 +00001475 assert(Inserted && "Recursively being processed?");
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001476
Alexey Samsonov153004f2014-09-29 22:08:00 +00001477 llvm::Type *resultType = nullptr;
John McCall85dd2c52011-05-15 02:19:42 +00001478 const ABIArgInfo &retAI = FI.getReturnInfo();
1479 switch (retAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +00001480 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +00001481 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbard3674e62008-09-11 01:48:57 +00001482
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001483 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001484 case ABIArgInfo::Direct:
John McCall85dd2c52011-05-15 02:19:42 +00001485 resultType = retAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +00001486 break;
1487
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001488 case ABIArgInfo::InAlloca:
Reid Klecknerfab1e892014-02-25 00:59:14 +00001489 if (retAI.getInAllocaSRet()) {
1490 // sret things on win32 aren't void, they return the sret pointer.
1491 QualType ret = FI.getReturnType();
1492 llvm::Type *ty = ConvertType(ret);
1493 unsigned addressSpace = Context.getTargetAddressSpace(ret);
1494 resultType = llvm::PointerType::get(ty, addressSpace);
1495 } else {
1496 resultType = llvm::Type::getVoidTy(getLLVMContext());
1497 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001498 break;
1499
John McCall7f416cc2015-09-08 08:05:57 +00001500 case ABIArgInfo::Indirect:
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001501 case ABIArgInfo::Ignore:
John McCall85dd2c52011-05-15 02:19:42 +00001502 resultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001503 break;
John McCallf26e73d2016-03-11 04:30:43 +00001504
1505 case ABIArgInfo::CoerceAndExpand:
1506 resultType = retAI.getUnpaddedCoerceAndExpandType();
1507 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001508 }
Mike Stump11289f42009-09-09 15:08:12 +00001509
Alexey Samsonov153004f2014-09-29 22:08:00 +00001510 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
1511 SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
1512
1513 // Add type for sret argument.
1514 if (IRFunctionArgs.hasSRetArg()) {
1515 QualType Ret = FI.getReturnType();
1516 llvm::Type *Ty = ConvertType(Ret);
1517 unsigned AddressSpace = Context.getTargetAddressSpace(Ret);
1518 ArgTypes[IRFunctionArgs.getSRetArgNo()] =
1519 llvm::PointerType::get(Ty, AddressSpace);
1520 }
1521
1522 // Add type for inalloca argument.
1523 if (IRFunctionArgs.hasInallocaArg()) {
1524 auto ArgStruct = FI.getArgStruct();
1525 assert(ArgStruct);
1526 ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
1527 }
1528
John McCallc818bbb2012-12-07 07:03:17 +00001529 // Add in all of the required arguments.
Alexey Samsonov153004f2014-09-29 22:08:00 +00001530 unsigned ArgNo = 0;
Alexey Samsonov34625dd2014-09-29 21:21:48 +00001531 CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1532 ie = it + FI.getNumRequiredArgs();
Alexey Samsonov153004f2014-09-29 22:08:00 +00001533 for (; it != ie; ++it, ++ArgNo) {
1534 const ABIArgInfo &ArgInfo = it->info;
Mike Stump11289f42009-09-09 15:08:12 +00001535
Rafael Espindolafad28de2012-10-24 01:59:00 +00001536 // Insert a padding type to ensure proper alignment.
Alexey Samsonov153004f2014-09-29 22:08:00 +00001537 if (IRFunctionArgs.hasPaddingArg(ArgNo))
1538 ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1539 ArgInfo.getPaddingType();
Rafael Espindolafad28de2012-10-24 01:59:00 +00001540
Alexey Samsonov153004f2014-09-29 22:08:00 +00001541 unsigned FirstIRArg, NumIRArgs;
1542 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1543
1544 switch (ArgInfo.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001545 case ABIArgInfo::Ignore:
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001546 case ABIArgInfo::InAlloca:
Alexey Samsonov153004f2014-09-29 22:08:00 +00001547 assert(NumIRArgs == 0);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001548 break;
1549
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001550 case ABIArgInfo::Indirect: {
Alexey Samsonov153004f2014-09-29 22:08:00 +00001551 assert(NumIRArgs == 1);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001552 // indirect arguments are always on the stack, which is addr space #0.
Chris Lattner2192fe52011-07-18 04:24:23 +00001553 llvm::Type *LTy = ConvertTypeForMem(it->type);
Alexey Samsonov153004f2014-09-29 22:08:00 +00001554 ArgTypes[FirstIRArg] = LTy->getPointerTo();
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001555 break;
1556 }
1557
1558 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +00001559 case ABIArgInfo::Direct: {
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00001560 // Fast-isel and the optimizer generally like scalar values better than
1561 // FCAs, so we flatten them if this is safe to do for this argument.
Alexey Samsonov153004f2014-09-29 22:08:00 +00001562 llvm::Type *argType = ArgInfo.getCoerceToType();
James Molloy6f244b62014-05-09 16:21:39 +00001563 llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
Alexey Samsonov153004f2014-09-29 22:08:00 +00001564 if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
1565 assert(NumIRArgs == st->getNumElements());
John McCall85dd2c52011-05-15 02:19:42 +00001566 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
Alexey Samsonov153004f2014-09-29 22:08:00 +00001567 ArgTypes[FirstIRArg + i] = st->getElementType(i);
Chris Lattner3dd716c2010-06-28 23:44:11 +00001568 } else {
Alexey Samsonov153004f2014-09-29 22:08:00 +00001569 assert(NumIRArgs == 1);
1570 ArgTypes[FirstIRArg] = argType;
Chris Lattner3dd716c2010-06-28 23:44:11 +00001571 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001572 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +00001573 }
Mike Stump11289f42009-09-09 15:08:12 +00001574
John McCallf26e73d2016-03-11 04:30:43 +00001575 case ABIArgInfo::CoerceAndExpand: {
1576 auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1577 for (auto EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {
1578 *ArgTypesIter++ = EltTy;
1579 }
1580 assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1581 break;
1582 }
1583
Daniel Dunbard3674e62008-09-11 01:48:57 +00001584 case ABIArgInfo::Expand:
Alexey Samsonov153004f2014-09-29 22:08:00 +00001585 auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1586 getExpandedTypes(it->type, ArgTypesIter);
1587 assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001588 break;
1589 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001590 }
1591
Chris Lattner6fb0ccf2011-07-15 05:16:14 +00001592 bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1593 assert(Erased && "Not in set?");
Alexey Samsonov153004f2014-09-29 22:08:00 +00001594
1595 return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
Daniel Dunbar81cf67f2008-09-09 23:48:28 +00001596}
1597
Chris Lattner2192fe52011-07-18 04:24:23 +00001598llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
John McCall5d865c322010-08-31 07:33:07 +00001599 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +00001600 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001601
Chris Lattner8806e322011-07-10 00:18:59 +00001602 if (!isFuncTypeConvertible(FPT))
1603 return llvm::StructType::get(getLLVMContext());
1604
1605 const CGFunctionInfo *Info;
1606 if (isa<CXXDestructorDecl>(MD))
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00001607 Info =
1608 &arrangeCXXStructorDeclaration(MD, getFromDtorType(GD.getDtorType()));
Chris Lattner8806e322011-07-10 00:18:59 +00001609 else
John McCalla729c622012-02-17 03:33:10 +00001610 Info = &arrangeCXXMethodDeclaration(MD);
1611 return GetFunctionType(*Info);
Anders Carlsson64457732009-11-24 05:08:52 +00001612}
1613
Samuel Antao798f11c2015-11-23 22:04:44 +00001614static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,
1615 llvm::AttrBuilder &FuncAttrs,
1616 const FunctionProtoType *FPT) {
1617 if (!FPT)
1618 return;
1619
1620 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
1621 FPT->isNothrow(Ctx))
1622 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1623}
1624
Chad Rosier7dbc9cf2016-01-06 14:35:46 +00001625void CodeGenModule::ConstructAttributeList(
1626 StringRef Name, const CGFunctionInfo &FI, CGCalleeInfo CalleeInfo,
1627 AttributeListType &PAL, unsigned &CallingConv, bool AttrOnCallSite) {
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001628 llvm::AttrBuilder FuncAttrs;
1629 llvm::AttrBuilder RetAttrs;
Paul Robinson08556952014-12-11 20:14:04 +00001630 bool HasOptnone = false;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001631
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001632 CallingConv = FI.getEffectiveCallingConvention();
1633
John McCallab26cfa2010-02-05 21:31:56 +00001634 if (FI.isNoReturn())
Bill Wendling207f0532012-12-20 19:27:06 +00001635 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
John McCallab26cfa2010-02-05 21:31:56 +00001636
Samuel Antao798f11c2015-11-23 22:04:44 +00001637 // If we have information about the function prototype, we can learn
1638 // attributes form there.
1639 AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,
1640 CalleeInfo.getCalleeFunctionProtoType());
1641
1642 const Decl *TargetDecl = CalleeInfo.getCalleeDecl();
1643
Amjad Aboudfaea5602016-03-07 14:22:46 +00001644 bool HasAnyX86InterruptAttr = false;
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001645 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001646 if (TargetDecl) {
Rafael Espindola2d21ab02011-10-12 19:51:18 +00001647 if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001648 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001649 if (TargetDecl->hasAttr<NoThrowAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001650 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Richard Smithdebc59d2013-01-30 05:45:05 +00001651 if (TargetDecl->hasAttr<NoReturnAttr>())
1652 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
Aaron Ballman7c19ab12014-02-22 16:59:24 +00001653 if (TargetDecl->hasAttr<NoDuplicateAttr>())
1654 FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
Yaxun Liu7d07ae72016-11-01 18:45:32 +00001655 if (TargetDecl->hasAttr<ConvergentAttr>())
1656 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
Richard Smithdebc59d2013-01-30 05:45:05 +00001657
1658 if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
Samuel Antao798f11c2015-11-23 22:04:44 +00001659 AddAttributesFromFunctionProtoType(
1660 getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
Richard Smith49af6292013-03-05 08:30:04 +00001661 // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
1662 // These attributes are not inherited by overloads.
1663 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
1664 if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
Richard Smithdebc59d2013-01-30 05:45:05 +00001665 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
John McCallbe349de2010-07-08 06:48:12 +00001666 }
1667
David Majnemer1bf0f8e2015-07-20 22:51:52 +00001668 // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
Eric Christopherbf005ec2011-08-15 22:38:22 +00001669 if (TargetDecl->hasAttr<ConstAttr>()) {
Bill Wendling207f0532012-12-20 19:27:06 +00001670 FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1671 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Eric Christopherbf005ec2011-08-15 22:38:22 +00001672 } else if (TargetDecl->hasAttr<PureAttr>()) {
Bill Wendling207f0532012-12-20 19:27:06 +00001673 FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1674 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
David Majnemer1bf0f8e2015-07-20 22:51:52 +00001675 } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
1676 FuncAttrs.addAttribute(llvm::Attribute::ArgMemOnly);
1677 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Eric Christopherbf005ec2011-08-15 22:38:22 +00001678 }
David Majnemer631a90b2015-02-04 07:23:21 +00001679 if (TargetDecl->hasAttr<RestrictAttr>())
Bill Wendling207f0532012-12-20 19:27:06 +00001680 RetAttrs.addAttribute(llvm::Attribute::NoAlias);
Hal Finkeld8442b12014-07-12 04:51:04 +00001681 if (TargetDecl->hasAttr<ReturnsNonNullAttr>())
1682 RetAttrs.addAttribute(llvm::Attribute::NonNull);
Paul Robinson08556952014-12-11 20:14:04 +00001683
Amjad Aboudfaea5602016-03-07 14:22:46 +00001684 HasAnyX86InterruptAttr = TargetDecl->hasAttr<AnyX86InterruptAttr>();
Paul Robinson08556952014-12-11 20:14:04 +00001685 HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
George Burgess IVe3763372016-12-22 02:50:20 +00001686 if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
1687 Optional<unsigned> NumElemsParam;
1688 // alloc_size args are base-1, 0 means not present.
1689 if (unsigned N = AllocSize->getNumElemsParam())
1690 NumElemsParam = N - 1;
1691 FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam() - 1,
1692 NumElemsParam);
1693 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001694 }
1695
Paul Robinson08556952014-12-11 20:14:04 +00001696 // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1697 if (!HasOptnone) {
1698 if (CodeGenOpts.OptimizeSize)
1699 FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1700 if (CodeGenOpts.OptimizeSize == 2)
1701 FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1702 }
1703
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001704 if (CodeGenOpts.DisableRedZone)
Bill Wendling207f0532012-12-20 19:27:06 +00001705 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001706 if (CodeGenOpts.NoImplicitFloat)
Bill Wendling207f0532012-12-20 19:27:06 +00001707 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
Peter Collingbourneb4728c12014-05-19 22:14:34 +00001708 if (CodeGenOpts.EnableSegmentedStacks &&
1709 !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
Reid Klecknerfb873af2014-04-10 22:59:13 +00001710 FuncAttrs.addAttribute("split-stack");
Devang Patel6e467b12009-06-04 23:32:02 +00001711
Bill Wendling2f81db62013-02-22 20:53:29 +00001712 if (AttrOnCallSite) {
1713 // Attributes that should go on the call site only.
Chad Rosier7dbc9cf2016-01-06 14:35:46 +00001714 if (!CodeGenOpts.SimplifyLibCalls ||
1715 CodeGenOpts.isNoBuiltinFunc(Name.data()))
Bill Wendling2f81db62013-02-22 20:53:29 +00001716 FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
Akira Hatanaka85365cd2015-07-02 22:15:41 +00001717 if (!CodeGenOpts.TrapFuncName.empty())
1718 FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);
Bill Wendling706469b2013-02-28 22:49:57 +00001719 } else {
1720 // Attributes that should go on the function, but not the call site.
Bill Wendling706469b2013-02-28 22:49:57 +00001721 if (!CodeGenOpts.DisableFPElim) {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001722 FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
Bill Wendling706469b2013-02-28 22:49:57 +00001723 } else if (CodeGenOpts.OmitLeafFramePointer) {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001724 FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
Bill Wendling17d1b6142013-08-22 21:16:51 +00001725 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
Bill Wendling706469b2013-02-28 22:49:57 +00001726 } else {
Bill Wendlingdabafea2013-03-13 22:24:33 +00001727 FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
Bill Wendling17d1b6142013-08-22 21:16:51 +00001728 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
Bill Wendling706469b2013-02-28 22:49:57 +00001729 }
1730
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001731 bool DisableTailCalls =
Amjad Aboudfaea5602016-03-07 14:22:46 +00001732 CodeGenOpts.DisableTailCalls || HasAnyX86InterruptAttr ||
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001733 (TargetDecl && TargetDecl->hasAttr<DisableTailCallsAttr>());
Amjad Aboudfaea5602016-03-07 14:22:46 +00001734 FuncAttrs.addAttribute(
1735 "disable-tail-calls",
1736 llvm::toStringRef(DisableTailCalls));
Akira Hatanaka7828b1e2015-11-13 00:42:21 +00001737
Bill Wendlingdabafea2013-03-13 22:24:33 +00001738 FuncAttrs.addAttribute("less-precise-fpmad",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001739 llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
Sjoerd Meijer0a8d4212016-08-30 08:09:45 +00001740
1741 if (!CodeGenOpts.FPDenormalMode.empty())
1742 FuncAttrs.addAttribute("denormal-fp-math",
1743 CodeGenOpts.FPDenormalMode);
1744
1745 FuncAttrs.addAttribute("no-trapping-math",
1746 llvm::toStringRef(CodeGenOpts.NoTrappingMath));
Sanjay Patel0bb72c12016-10-04 20:44:05 +00001747
1748 // TODO: Are these all needed?
1749 // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
Bill Wendlingdabafea2013-03-13 22:24:33 +00001750 FuncAttrs.addAttribute("no-infs-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001751 llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001752 FuncAttrs.addAttribute("no-nans-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001753 llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001754 FuncAttrs.addAttribute("unsafe-fp-math",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001755 llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
Bill Wendlingdabafea2013-03-13 22:24:33 +00001756 FuncAttrs.addAttribute("use-soft-float",
Bill Wendlingf69f5942013-07-26 21:51:11 +00001757 llvm::toStringRef(CodeGenOpts.SoftFloat));
Bill Wendlingb3219722013-07-22 20:15:41 +00001758 FuncAttrs.addAttribute("stack-protector-buffer-size",
Bill Wendling021c8de2013-07-12 22:26:07 +00001759 llvm::utostr(CodeGenOpts.SSPBufferSize));
Yaxun Liu79c99fb2016-07-08 20:28:29 +00001760 FuncAttrs.addAttribute("no-signed-zeros-fp-math",
1761 llvm::toStringRef(CodeGenOpts.NoSignedZeros));
Yaxun Liuffb60902016-08-09 20:10:18 +00001762 FuncAttrs.addAttribute(
1763 "correctly-rounded-divide-sqrt-fp-math",
1764 llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt));
Bill Wendlinga9cc8c02013-07-25 00:32:41 +00001765
Sanjay Patel0bb72c12016-10-04 20:44:05 +00001766 // TODO: Reciprocal estimate codegen options should apply to instructions?
1767 std::vector<std::string> &Recips = getTarget().getTargetOpts().Reciprocals;
1768 if (!Recips.empty())
1769 FuncAttrs.addAttribute("reciprocal-estimates",
1770 llvm::join(Recips.begin(), Recips.end(), ","));
1771
Akira Hatanakaaecca042015-09-11 18:55:09 +00001772 if (CodeGenOpts.StackRealignment)
1773 FuncAttrs.addAttribute("stackrealign");
Marcin Koscielnickib31ee6d2016-05-04 23:37:40 +00001774 if (CodeGenOpts.Backchain)
1775 FuncAttrs.addAttribute("backchain");
Eric Christopher70c16652015-03-25 23:14:47 +00001776
Eric Christopher11acf732015-06-12 01:35:52 +00001777 // Add target-cpu and target-features attributes to functions. If
1778 // we have a decl for the function and it has a target attribute then
1779 // parse that and add it to the feature set.
1780 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
Eric Christopher11acf732015-06-12 01:35:52 +00001781 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
Eric Christopherb57804a2015-09-01 22:03:56 +00001782 if (FD && FD->hasAttr<TargetAttr>()) {
Eric Christopher3a98b3c2015-08-27 19:59:34 +00001783 llvm::StringMap<bool> FeatureMap;
Eric Christopher2b90a642015-11-11 23:05:08 +00001784 getFunctionFeatureMap(FeatureMap, FD);
Eric Christopher11acf732015-06-12 01:35:52 +00001785
Eric Christopher3a98b3c2015-08-27 19:59:34 +00001786 // Produce the canonical string for this set of features.
1787 std::vector<std::string> Features;
1788 for (llvm::StringMap<bool>::const_iterator it = FeatureMap.begin(),
1789 ie = FeatureMap.end();
1790 it != ie; ++it)
1791 Features.push_back((it->second ? "+" : "-") + it->first().str());
Eric Christopher2249b812015-07-01 00:08:29 +00001792
Eric Christopher3a98b3c2015-08-27 19:59:34 +00001793 // Now add the target-cpu and target-features to the function.
Eric Christopher2b90a642015-11-11 23:05:08 +00001794 // While we populated the feature map above, we still need to
1795 // get and parse the target attribute so we can get the cpu for
1796 // the function.
1797 const auto *TD = FD->getAttr<TargetAttr>();
1798 TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
1799 if (ParsedAttr.second != "")
1800 TargetCPU = ParsedAttr.second;
Eric Christopher3a98b3c2015-08-27 19:59:34 +00001801 if (TargetCPU != "")
1802 FuncAttrs.addAttribute("target-cpu", TargetCPU);
1803 if (!Features.empty()) {
1804 std::sort(Features.begin(), Features.end());
1805 FuncAttrs.addAttribute(
1806 "target-features",
1807 llvm::join(Features.begin(), Features.end(), ","));
1808 }
1809 } else {
1810 // Otherwise just add the existing target cpu and target features to the
1811 // function.
1812 std::vector<std::string> &Features = getTarget().getTargetOpts().Features;
1813 if (TargetCPU != "")
1814 FuncAttrs.addAttribute("target-cpu", TargetCPU);
1815 if (!Features.empty()) {
1816 std::sort(Features.begin(), Features.end());
1817 FuncAttrs.addAttribute(
1818 "target-features",
1819 llvm::join(Features.begin(), Features.end(), ","));
1820 }
Eric Christopher70c16652015-03-25 23:14:47 +00001821 }
Bill Wendling985d1c52013-02-15 21:30:01 +00001822 }
1823
Justin Lebarddd97fa2016-02-24 21:55:11 +00001824 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
1825 // Conservatively, mark all functions and calls in CUDA as convergent
1826 // (meaning, they may call an intrinsically convergent op, such as
1827 // __syncthreads(), and so can't have certain optimizations applied around
1828 // them). LLVM will remove this attribute where it safely can.
1829 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
Justin Lebard3a44f62016-04-05 18:26:20 +00001830
Justin Lebar3e6449b2016-10-04 23:41:49 +00001831 // Exceptions aren't supported in CUDA device code.
1832 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1833
Justin Lebard3a44f62016-04-05 18:26:20 +00001834 // Respect -fcuda-flush-denormals-to-zero.
1835 if (getLangOpts().CUDADeviceFlushDenormalsToZero)
1836 FuncAttrs.addAttribute("nvptx-f32ftz", "true");
Justin Lebarddd97fa2016-02-24 21:55:11 +00001837 }
1838
Alexey Samsonov153004f2014-09-29 22:08:00 +00001839 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001840
Daniel Dunbar3668cb22009-02-02 23:43:58 +00001841 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001842 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00001843 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001844 case ABIArgInfo::Extend:
Jakob Stoklund Olesend7bf2932013-05-29 03:57:23 +00001845 if (RetTy->hasSignedIntegerRepresentation())
1846 RetAttrs.addAttribute(llvm::Attribute::SExt);
1847 else if (RetTy->hasUnsignedIntegerRepresentation())
1848 RetAttrs.addAttribute(llvm::Attribute::ZExt);
Jakob Stoklund Olesena3661142013-06-05 03:00:09 +00001849 // FALL THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +00001850 case ABIArgInfo::Direct:
Jakob Stoklund Olesena3661142013-06-05 03:00:09 +00001851 if (RetAI.getInReg())
1852 RetAttrs.addAttribute(llvm::Attribute::InReg);
1853 break;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001854 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001855 break;
1856
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001857 case ABIArgInfo::InAlloca:
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001858 case ABIArgInfo::Indirect: {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001859 // inalloca and sret disable readnone and readonly
Bill Wendling207f0532012-12-20 19:27:06 +00001860 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1861 .removeAttribute(llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001862 break;
Rafael Espindola06b2b4a2012-07-31 02:44:24 +00001863 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001864
John McCallf26e73d2016-03-11 04:30:43 +00001865 case ABIArgInfo::CoerceAndExpand:
1866 break;
1867
Daniel Dunbard3674e62008-09-11 01:48:57 +00001868 case ABIArgInfo::Expand:
David Blaikie83d382b2011-09-23 05:06:16 +00001869 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001870 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001871
Hal Finkela2347ba2014-07-18 15:52:10 +00001872 if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
1873 QualType PTy = RefTy->getPointeeType();
David Majnemer9df56372015-09-10 21:52:00 +00001874 if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
Hal Finkela2347ba2014-07-18 15:52:10 +00001875 RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1876 .getQuantity());
1877 else if (getContext().getTargetAddressSpace(PTy) == 0)
1878 RetAttrs.addAttribute(llvm::Attribute::NonNull);
1879 }
Nick Lewycky9b46eb82014-05-28 09:56:42 +00001880
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001881 // Attach return attributes.
1882 if (RetAttrs.hasAttributes()) {
1883 PAL.push_back(llvm::AttributeSet::get(
1884 getLLVMContext(), llvm::AttributeSet::ReturnIndex, RetAttrs));
1885 }
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001886
John McCall12f23522016-04-04 18:33:08 +00001887 bool hasUsedSRet = false;
1888
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001889 // Attach attributes to sret.
1890 if (IRFunctionArgs.hasSRetArg()) {
1891 llvm::AttrBuilder SRETAttrs;
1892 SRETAttrs.addAttribute(llvm::Attribute::StructRet);
John McCall12f23522016-04-04 18:33:08 +00001893 hasUsedSRet = true;
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001894 if (RetAI.getInReg())
1895 SRETAttrs.addAttribute(llvm::Attribute::InReg);
1896 PAL.push_back(llvm::AttributeSet::get(
1897 getLLVMContext(), IRFunctionArgs.getSRetArgNo() + 1, SRETAttrs));
1898 }
1899
1900 // Attach attributes to inalloca argument.
1901 if (IRFunctionArgs.hasInallocaArg()) {
1902 llvm::AttrBuilder Attrs;
1903 Attrs.addAttribute(llvm::Attribute::InAlloca);
1904 PAL.push_back(llvm::AttributeSet::get(
1905 getLLVMContext(), IRFunctionArgs.getInallocaArgNo() + 1, Attrs));
1906 }
1907
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001908 unsigned ArgNo = 0;
1909 for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
1910 E = FI.arg_end();
1911 I != E; ++I, ++ArgNo) {
1912 QualType ParamType = I->type;
1913 const ABIArgInfo &AI = I->info;
Bill Wendlinga514ebc2012-10-15 20:36:26 +00001914 llvm::AttrBuilder Attrs;
Anton Korobeynikovc8478242009-04-04 00:49:24 +00001915
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001916 // Add attribute for padding argument, if necessary.
1917 if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
Bill Wendling290d9522013-01-27 02:46:53 +00001918 if (AI.getPaddingInReg())
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001919 PAL.push_back(llvm::AttributeSet::get(
1920 getLLVMContext(), IRFunctionArgs.getPaddingArgNo(ArgNo) + 1,
1921 llvm::Attribute::InReg));
Rafael Espindolafad28de2012-10-24 01:59:00 +00001922 }
1923
John McCall39ec71f2010-03-27 00:47:27 +00001924 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
1925 // have the corresponding parameter variable. It doesn't make
Daniel Dunbarcb2b3d02011-02-10 18:10:07 +00001926 // sense to do it here because parameters are so messed up.
Daniel Dunbard3674e62008-09-11 01:48:57 +00001927 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001928 case ABIArgInfo::Extend:
Douglas Gregor6ab2fa82011-05-20 16:38:50 +00001929 if (ParamType->isSignedIntegerOrEnumerationType())
Bill Wendling207f0532012-12-20 19:27:06 +00001930 Attrs.addAttribute(llvm::Attribute::SExt);
Petar Jovanovic1a3f9652015-05-26 21:07:19 +00001931 else if (ParamType->isUnsignedIntegerOrEnumerationType()) {
1932 if (getTypes().getABIInfo().shouldSignExtUnsignedType(ParamType))
1933 Attrs.addAttribute(llvm::Attribute::SExt);
1934 else
1935 Attrs.addAttribute(llvm::Attribute::ZExt);
1936 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001937 // FALL THROUGH
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001938 case ABIArgInfo::Direct:
Peter Collingbournef7706832014-12-12 23:41:25 +00001939 if (ArgNo == 0 && FI.isChainCall())
1940 Attrs.addAttribute(llvm::Attribute::Nest);
1941 else if (AI.getInReg())
Bill Wendling207f0532012-12-20 19:27:06 +00001942 Attrs.addAttribute(llvm::Attribute::InReg);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001943 break;
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001944
James Y Knight71608572015-08-21 18:19:06 +00001945 case ABIArgInfo::Indirect: {
Rafael Espindola703c47f2012-10-19 05:04:37 +00001946 if (AI.getInReg())
Bill Wendling207f0532012-12-20 19:27:06 +00001947 Attrs.addAttribute(llvm::Attribute::InReg);
Rafael Espindola703c47f2012-10-19 05:04:37 +00001948
Anders Carlsson20759ad2009-09-16 15:53:40 +00001949 if (AI.getIndirectByVal())
Bill Wendling207f0532012-12-20 19:27:06 +00001950 Attrs.addAttribute(llvm::Attribute::ByVal);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001951
John McCall7f416cc2015-09-08 08:05:57 +00001952 CharUnits Align = AI.getIndirectAlign();
James Y Knight71608572015-08-21 18:19:06 +00001953
1954 // In a byval argument, it is important that the required
1955 // alignment of the type is honored, as LLVM might be creating a
1956 // *new* stack object, and needs to know what alignment to give
1957 // it. (Sometimes it can deduce a sensible alignment on its own,
1958 // but not if clang decides it must emit a packed struct, or the
1959 // user specifies increased alignment requirements.)
1960 //
1961 // This is different from indirect *not* byval, where the object
1962 // exists already, and the align attribute is purely
1963 // informative.
John McCall7f416cc2015-09-08 08:05:57 +00001964 assert(!Align.isZero());
James Y Knight71608572015-08-21 18:19:06 +00001965
John McCall7f416cc2015-09-08 08:05:57 +00001966 // For now, only add this when we have a byval argument.
1967 // TODO: be less lazy about updating test cases.
1968 if (AI.getIndirectByVal())
1969 Attrs.addAlignmentAttr(Align.getQuantity());
Bill Wendlinga7912f82012-10-10 07:36:56 +00001970
Daniel Dunbarc2304432009-03-18 19:51:01 +00001971 // byval disables readnone and readonly.
Bill Wendling207f0532012-12-20 19:27:06 +00001972 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1973 .removeAttribute(llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +00001974 break;
James Y Knight71608572015-08-21 18:19:06 +00001975 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001976 case ABIArgInfo::Ignore:
Alexey Samsonov91cf4552014-08-22 01:06:06 +00001977 case ABIArgInfo::Expand:
John McCallf26e73d2016-03-11 04:30:43 +00001978 case ABIArgInfo::CoerceAndExpand:
1979 break;
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001980
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001981 case ABIArgInfo::InAlloca:
1982 // inalloca disables readnone and readonly.
1983 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1984 .removeAttribute(llvm::Attribute::ReadNone);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00001985 continue;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00001986 }
Mike Stump11289f42009-09-09 15:08:12 +00001987
Hal Finkela2347ba2014-07-18 15:52:10 +00001988 if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
1989 QualType PTy = RefTy->getPointeeType();
David Majnemer9df56372015-09-10 21:52:00 +00001990 if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
Hal Finkela2347ba2014-07-18 15:52:10 +00001991 Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1992 .getQuantity());
1993 else if (getContext().getTargetAddressSpace(PTy) == 0)
1994 Attrs.addAttribute(llvm::Attribute::NonNull);
1995 }
Nick Lewycky9b46eb82014-05-28 09:56:42 +00001996
John McCall12f23522016-04-04 18:33:08 +00001997 switch (FI.getExtParameterInfo(ArgNo).getABI()) {
1998 case ParameterABI::Ordinary:
1999 break;
2000
2001 case ParameterABI::SwiftIndirectResult: {
2002 // Add 'sret' if we haven't already used it for something, but
2003 // only if the result is void.
2004 if (!hasUsedSRet && RetTy->isVoidType()) {
2005 Attrs.addAttribute(llvm::Attribute::StructRet);
2006 hasUsedSRet = true;
2007 }
2008
2009 // Add 'noalias' in either case.
2010 Attrs.addAttribute(llvm::Attribute::NoAlias);
2011
2012 // Add 'dereferenceable' and 'alignment'.
2013 auto PTy = ParamType->getPointeeType();
2014 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2015 auto info = getContext().getTypeInfoInChars(PTy);
2016 Attrs.addDereferenceableAttr(info.first.getQuantity());
2017 Attrs.addAttribute(llvm::Attribute::getWithAlignment(getLLVMContext(),
2018 info.second.getQuantity()));
2019 }
2020 break;
2021 }
2022
2023 case ParameterABI::SwiftErrorResult:
2024 Attrs.addAttribute(llvm::Attribute::SwiftError);
2025 break;
2026
2027 case ParameterABI::SwiftContext:
2028 Attrs.addAttribute(llvm::Attribute::SwiftSelf);
2029 break;
2030 }
2031
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002032 if (Attrs.hasAttributes()) {
2033 unsigned FirstIRArg, NumIRArgs;
2034 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2035 for (unsigned i = 0; i < NumIRArgs; i++)
2036 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(),
2037 FirstIRArg + i + 1, Attrs));
2038 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00002039 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002040 assert(ArgNo == FI.arg_size());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002041
Bill Wendlinga7912f82012-10-10 07:36:56 +00002042 if (FuncAttrs.hasAttributes())
Bill Wendling4f0c0802012-10-15 07:31:59 +00002043 PAL.push_back(llvm::
Bill Wendling290d9522013-01-27 02:46:53 +00002044 AttributeSet::get(getLLVMContext(),
2045 llvm::AttributeSet::FunctionIndex,
2046 FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +00002047}
2048
John McCalla738c252011-03-09 04:27:21 +00002049/// An argument came in as a promoted argument; demote it back to its
2050/// declared type.
2051static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
2052 const VarDecl *var,
2053 llvm::Value *value) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002054 llvm::Type *varType = CGF.ConvertType(var->getType());
John McCalla738c252011-03-09 04:27:21 +00002055
2056 // This can happen with promotions that actually don't change the
2057 // underlying type, like the enum promotions.
2058 if (value->getType() == varType) return value;
2059
2060 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
2061 && "unexpected promotion type");
2062
2063 if (isa<llvm::IntegerType>(varType))
2064 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
2065
2066 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
2067}
2068
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002069/// Returns the attribute (either parameter attribute, or function
2070/// attribute), which declares argument ArgNo to be non-null.
2071static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
2072 QualType ArgType, unsigned ArgNo) {
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002073 // FIXME: __attribute__((nonnull)) can also be applied to:
2074 // - references to pointers, where the pointee is known to be
2075 // nonnull (apparently a Clang extension)
2076 // - transparent unions containing pointers
2077 // In the former case, LLVM IR cannot represent the constraint. In
2078 // the latter case, we have no guarantee that the transparent union
2079 // is in fact passed as a pointer.
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002080 if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
2081 return nullptr;
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002082 // First, check attribute on parameter itself.
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002083 if (PVD) {
2084 if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
2085 return ParmNNAttr;
2086 }
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002087 // Check function attributes.
2088 if (!FD)
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002089 return nullptr;
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002090 for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002091 if (NNAttr->isNonNull(ArgNo))
2092 return NNAttr;
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002093 }
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002094 return nullptr;
Alexey Samsonov9fc9bf82014-08-28 00:53:20 +00002095}
2096
John McCall12f23522016-04-04 18:33:08 +00002097namespace {
2098 struct CopyBackSwiftError final : EHScopeStack::Cleanup {
2099 Address Temp;
2100 Address Arg;
2101 CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
2102 void Emit(CodeGenFunction &CGF, Flags flags) override {
2103 llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
2104 CGF.Builder.CreateStore(errorValue, Arg);
2105 }
2106 };
2107}
2108
Daniel Dunbard931a872009-02-02 22:03:45 +00002109void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
2110 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +00002111 const FunctionArgList &Args) {
Hans Wennborgd71907d2014-09-04 22:16:33 +00002112 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
2113 // Naked functions don't have prologues.
2114 return;
2115
John McCallcaa19452009-07-28 01:00:58 +00002116 // If this is an implicit-return-zero function, go ahead and
2117 // initialize the return value. TODO: it might be nice to have
2118 // a more general mechanism for this that didn't require synthesized
2119 // return statements.
John McCalldec348f72013-05-03 07:33:41 +00002120 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
John McCallcaa19452009-07-28 01:00:58 +00002121 if (FD->hasImplicitReturnZero()) {
Alp Toker314cc812014-01-25 16:55:45 +00002122 QualType RetTy = FD->getReturnType().getUnqualifiedType();
Chris Lattner2192fe52011-07-18 04:24:23 +00002123 llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +00002124 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +00002125 Builder.CreateStore(Zero, ReturnValue);
2126 }
2127 }
2128
Mike Stump18bb9282009-05-16 07:57:57 +00002129 // FIXME: We no longer need the types from FunctionArgList; lift up and
2130 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +00002131
Alexey Samsonov153004f2014-09-29 22:08:00 +00002132 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002133 // Flattened function arguments.
John McCall12f23522016-04-04 18:33:08 +00002134 SmallVector<llvm::Value *, 16> FnArgs;
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002135 FnArgs.reserve(IRFunctionArgs.totalIRArgs());
2136 for (auto &Arg : Fn->args()) {
2137 FnArgs.push_back(&Arg);
2138 }
2139 assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
Mike Stump11289f42009-09-09 15:08:12 +00002140
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002141 // If we're using inalloca, all the memory arguments are GEPs off of the last
2142 // parameter, which is a pointer to the complete memory area.
John McCall7f416cc2015-09-08 08:05:57 +00002143 Address ArgStruct = Address::invalid();
2144 const llvm::StructLayout *ArgStructLayout = nullptr;
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002145 if (IRFunctionArgs.hasInallocaArg()) {
John McCall7f416cc2015-09-08 08:05:57 +00002146 ArgStructLayout = CGM.getDataLayout().getStructLayout(FI.getArgStruct());
2147 ArgStruct = Address(FnArgs[IRFunctionArgs.getInallocaArgNo()],
2148 FI.getArgStructAlignment());
2149
2150 assert(ArgStruct.getType() == FI.getArgStruct()->getPointerTo());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002151 }
2152
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002153 // Name the struct return parameter.
2154 if (IRFunctionArgs.hasSRetArg()) {
John McCall12f23522016-04-04 18:33:08 +00002155 auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
Daniel Dunbar613855c2008-09-09 23:27:19 +00002156 AI->setName("agg.result");
Reid Kleckner37abaca2014-05-09 22:46:15 +00002157 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), AI->getArgNo() + 1,
Bill Wendlingce2f9c52013-01-23 06:15:10 +00002158 llvm::Attribute::NoAlias));
Daniel Dunbar613855c2008-09-09 23:27:19 +00002159 }
Mike Stump11289f42009-09-09 15:08:12 +00002160
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002161 // Track if we received the parameter as a pointer (indirect, byval, or
2162 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
2163 // into a local alloca for us.
John McCall7f416cc2015-09-08 08:05:57 +00002164 SmallVector<ParamValue, 16> ArgVals;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002165 ArgVals.reserve(Args.size());
2166
Reid Kleckner739756c2013-12-04 19:23:12 +00002167 // Create a pointer value for every parameter declaration. This usually
2168 // entails copying one or more LLVM IR arguments into an alloca. Don't push
2169 // any cleanups or do anything that might unwind. We do that separately, so
2170 // we can push the cleanups in the correct order for the ABI.
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00002171 assert(FI.arg_size() == Args.size() &&
2172 "Mismatch between function signature & arguments.");
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002173 unsigned ArgNo = 0;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00002174 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002175 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Devang Patel68a15252011-03-03 20:13:15 +00002176 i != e; ++i, ++info_it, ++ArgNo) {
John McCalla738c252011-03-09 04:27:21 +00002177 const VarDecl *Arg = *i;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00002178 QualType Ty = info_it->type;
2179 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +00002180
John McCalla738c252011-03-09 04:27:21 +00002181 bool isPromoted =
2182 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
2183
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002184 unsigned FirstIRArg, NumIRArgs;
2185 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
Rafael Espindolafad28de2012-10-24 01:59:00 +00002186
Daniel Dunbard3674e62008-09-11 01:48:57 +00002187 switch (ArgI.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002188 case ABIArgInfo::InAlloca: {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002189 assert(NumIRArgs == 0);
John McCall7f416cc2015-09-08 08:05:57 +00002190 auto FieldIndex = ArgI.getInAllocaFieldIndex();
2191 CharUnits FieldOffset =
2192 CharUnits::fromQuantity(ArgStructLayout->getElementOffset(FieldIndex));
2193 Address V = Builder.CreateStructGEP(ArgStruct, FieldIndex, FieldOffset,
2194 Arg->getName());
2195 ArgVals.push_back(ParamValue::forIndirect(V));
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002196 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002197 }
2198
Daniel Dunbar747865a2009-02-05 09:16:39 +00002199 case ABIArgInfo::Indirect: {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002200 assert(NumIRArgs == 1);
John McCall7f416cc2015-09-08 08:05:57 +00002201 Address ParamAddr = Address(FnArgs[FirstIRArg], ArgI.getIndirectAlign());
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00002202
John McCall47fb9502013-03-07 21:37:08 +00002203 if (!hasScalarEvaluationKind(Ty)) {
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00002204 // Aggregates and complex variables are accessed by reference. All we
John McCall7f416cc2015-09-08 08:05:57 +00002205 // need to do is realign the value, if requested.
2206 Address V = ParamAddr;
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00002207 if (ArgI.getIndirectRealign()) {
John McCall7f416cc2015-09-08 08:05:57 +00002208 Address AlignedTemp = CreateMemTemp(Ty, "coerce");
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00002209
2210 // Copy from the incoming argument pointer to the temporary with the
2211 // appropriate alignment.
2212 //
2213 // FIXME: We should have a common utility for generating an aggregate
2214 // copy.
Ken Dyck705ba072011-01-19 01:58:38 +00002215 CharUnits Size = getContext().getTypeSizeInChars(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00002216 auto SizeVal = llvm::ConstantInt::get(IntPtrTy, Size.getQuantity());
2217 Address Dst = Builder.CreateBitCast(AlignedTemp, Int8PtrTy);
2218 Address Src = Builder.CreateBitCast(ParamAddr, Int8PtrTy);
2219 Builder.CreateMemCpy(Dst, Src, SizeVal, false);
Daniel Dunbar7b7c2932010-09-16 20:42:02 +00002220 V = AlignedTemp;
2221 }
John McCall7f416cc2015-09-08 08:05:57 +00002222 ArgVals.push_back(ParamValue::forIndirect(V));
Daniel Dunbar747865a2009-02-05 09:16:39 +00002223 } else {
2224 // Load scalar value from indirect argument.
John McCall7f416cc2015-09-08 08:05:57 +00002225 llvm::Value *V =
2226 EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getLocStart());
John McCalla738c252011-03-09 04:27:21 +00002227
2228 if (isPromoted)
2229 V = emitArgumentDemotion(*this, Arg, V);
John McCall7f416cc2015-09-08 08:05:57 +00002230 ArgVals.push_back(ParamValue::forDirect(V));
Daniel Dunbar747865a2009-02-05 09:16:39 +00002231 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00002232 break;
2233 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002234
2235 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00002236 case ABIArgInfo::Direct: {
Akira Hatanaka18334dd2012-01-09 19:08:06 +00002237
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002238 // If we have the trivial case, handle it with no muss and fuss.
2239 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002240 ArgI.getCoerceToType() == ConvertType(Ty) &&
2241 ArgI.getDirectOffset() == 0) {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002242 assert(NumIRArgs == 1);
John McCall12f23522016-04-04 18:33:08 +00002243 llvm::Value *V = FnArgs[FirstIRArg];
2244 auto AI = cast<llvm::Argument>(V);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002245
Hal Finkel48d53e22014-07-19 01:41:07 +00002246 if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002247 if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
2248 PVD->getFunctionScopeIndex()))
Hal Finkel82504f02014-07-11 17:35:21 +00002249 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2250 AI->getArgNo() + 1,
2251 llvm::Attribute::NonNull));
2252
Hal Finkel48d53e22014-07-19 01:41:07 +00002253 QualType OTy = PVD->getOriginalType();
2254 if (const auto *ArrTy =
2255 getContext().getAsConstantArrayType(OTy)) {
2256 // A C99 array parameter declaration with the static keyword also
2257 // indicates dereferenceability, and if the size is constant we can
2258 // use the dereferenceable attribute (which requires the size in
2259 // bytes).
Hal Finkel16e394a2014-07-19 02:13:40 +00002260 if (ArrTy->getSizeModifier() == ArrayType::Static) {
Hal Finkel48d53e22014-07-19 01:41:07 +00002261 QualType ETy = ArrTy->getElementType();
2262 uint64_t ArrSize = ArrTy->getSize().getZExtValue();
2263 if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
2264 ArrSize) {
2265 llvm::AttrBuilder Attrs;
2266 Attrs.addDereferenceableAttr(
2267 getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize);
2268 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2269 AI->getArgNo() + 1, Attrs));
2270 } else if (getContext().getTargetAddressSpace(ETy) == 0) {
2271 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2272 AI->getArgNo() + 1,
2273 llvm::Attribute::NonNull));
2274 }
2275 }
2276 } else if (const auto *ArrTy =
2277 getContext().getAsVariableArrayType(OTy)) {
2278 // For C99 VLAs with the static keyword, we don't know the size so
2279 // we can't use the dereferenceable attribute, but in addrspace(0)
2280 // we know that it must be nonnull.
2281 if (ArrTy->getSizeModifier() == VariableArrayType::Static &&
2282 !getContext().getTargetAddressSpace(ArrTy->getElementType()))
2283 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2284 AI->getArgNo() + 1,
2285 llvm::Attribute::NonNull));
2286 }
Hal Finkel1b0d24e2014-10-02 21:21:25 +00002287
2288 const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
2289 if (!AVAttr)
2290 if (const auto *TOTy = dyn_cast<TypedefType>(OTy))
2291 AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
2292 if (AVAttr) {
2293 llvm::Value *AlignmentValue =
2294 EmitScalarExpr(AVAttr->getAlignment());
2295 llvm::ConstantInt *AlignmentCI =
2296 cast<llvm::ConstantInt>(AlignmentValue);
2297 unsigned Alignment =
2298 std::min((unsigned) AlignmentCI->getZExtValue(),
2299 +llvm::Value::MaximumAlignment);
2300
2301 llvm::AttrBuilder Attrs;
2302 Attrs.addAlignmentAttr(Alignment);
2303 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2304 AI->getArgNo() + 1, Attrs));
2305 }
Hal Finkel48d53e22014-07-19 01:41:07 +00002306 }
2307
Bill Wendling507c3512012-10-16 05:23:44 +00002308 if (Arg->getType().isRestrictQualified())
Bill Wendlingce2f9c52013-01-23 06:15:10 +00002309 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
2310 AI->getArgNo() + 1,
2311 llvm::Attribute::NoAlias));
John McCall39ec71f2010-03-27 00:47:27 +00002312
John McCall12f23522016-04-04 18:33:08 +00002313 // LLVM expects swifterror parameters to be used in very restricted
2314 // ways. Copy the value into a less-restricted temporary.
2315 if (FI.getExtParameterInfo(ArgNo).getABI()
2316 == ParameterABI::SwiftErrorResult) {
2317 QualType pointeeTy = Ty->getPointeeType();
2318 assert(pointeeTy->isPointerType());
2319 Address temp =
2320 CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
2321 Address arg = Address(V, getContext().getTypeAlignInChars(pointeeTy));
2322 llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
2323 Builder.CreateStore(incomingErrorValue, temp);
2324 V = temp.getPointer();
2325
2326 // Push a cleanup to copy the value back at the end of the function.
2327 // The convention does not guarantee that the value will be written
2328 // back if the function exits with an unwind exception.
2329 EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
2330 }
2331
Chris Lattner7369c142011-07-20 06:29:00 +00002332 // Ensure the argument is the correct type.
2333 if (V->getType() != ArgI.getCoerceToType())
2334 V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
2335
John McCalla738c252011-03-09 04:27:21 +00002336 if (isPromoted)
2337 V = emitArgumentDemotion(*this, Arg, V);
Rafael Espindola8778c282012-11-29 16:09:03 +00002338
2339 // Because of merging of function types from multiple decls it is
2340 // possible for the type of an argument to not match the corresponding
2341 // type in the function type. Since we are codegening the callee
2342 // in here, add a cast to the argument type.
2343 llvm::Type *LTy = ConvertType(Arg->getType());
2344 if (V->getType() != LTy)
2345 V = Builder.CreateBitCast(V, LTy);
2346
John McCall7f416cc2015-09-08 08:05:57 +00002347 ArgVals.push_back(ParamValue::forDirect(V));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002348 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +00002349 }
Mike Stump11289f42009-09-09 15:08:12 +00002350
John McCall7f416cc2015-09-08 08:05:57 +00002351 Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),
2352 Arg->getName());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002353
John McCall7f416cc2015-09-08 08:05:57 +00002354 // Pointer to store into.
2355 Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002356
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00002357 // Fast-isel and the optimizer generally like scalar values better than
2358 // FCAs, so we flatten them if this is safe to do for this argument.
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00002359 llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00002360 if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
2361 STy->getNumElements() > 1) {
John McCall7f416cc2015-09-08 08:05:57 +00002362 auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
Micah Villmowdd31ca12012-10-08 16:25:52 +00002363 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
John McCall7f416cc2015-09-08 08:05:57 +00002364 llvm::Type *DstTy = Ptr.getElementType();
Micah Villmowdd31ca12012-10-08 16:25:52 +00002365 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002366
John McCall7f416cc2015-09-08 08:05:57 +00002367 Address AddrToStoreInto = Address::invalid();
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00002368 if (SrcSize <= DstSize) {
John McCall7f416cc2015-09-08 08:05:57 +00002369 AddrToStoreInto =
2370 Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Evgeniy Stepanov3fae4ae2012-02-10 09:30:15 +00002371 } else {
John McCall7f416cc2015-09-08 08:05:57 +00002372 AddrToStoreInto =
2373 CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
Chris Lattner15ec3612010-06-29 00:06:42 +00002374 }
John McCall7f416cc2015-09-08 08:05:57 +00002375
2376 assert(STy->getNumElements() == NumIRArgs);
2377 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2378 auto AI = FnArgs[FirstIRArg + i];
2379 AI->setName(Arg->getName() + ".coerce" + Twine(i));
2380 auto Offset = CharUnits::fromQuantity(SrcLayout->getElementOffset(i));
2381 Address EltPtr =
2382 Builder.CreateStructGEP(AddrToStoreInto, i, Offset);
2383 Builder.CreateStore(AI, EltPtr);
2384 }
2385
2386 if (SrcSize > DstSize) {
2387 Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
2388 }
2389
Chris Lattner15ec3612010-06-29 00:06:42 +00002390 } else {
2391 // Simple case, just do a coerced store of the argument into the alloca.
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002392 assert(NumIRArgs == 1);
2393 auto AI = FnArgs[FirstIRArg];
Chris Lattner9e748e92010-06-29 00:14:52 +00002394 AI->setName(Arg->getName() + ".coerce");
John McCall7f416cc2015-09-08 08:05:57 +00002395 CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +00002396 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002397
Daniel Dunbar2f219b02009-02-03 19:12:28 +00002398 // Match to what EmitParmDecl is expecting for this type.
John McCall47fb9502013-03-07 21:37:08 +00002399 if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
John McCall7f416cc2015-09-08 08:05:57 +00002400 llvm::Value *V =
2401 EmitLoadOfScalar(Alloca, false, Ty, Arg->getLocStart());
John McCalla738c252011-03-09 04:27:21 +00002402 if (isPromoted)
2403 V = emitArgumentDemotion(*this, Arg, V);
John McCall7f416cc2015-09-08 08:05:57 +00002404 ArgVals.push_back(ParamValue::forDirect(V));
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002405 } else {
John McCall7f416cc2015-09-08 08:05:57 +00002406 ArgVals.push_back(ParamValue::forIndirect(Alloca));
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00002407 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002408 break;
Daniel Dunbar2f219b02009-02-03 19:12:28 +00002409 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002410
John McCallf26e73d2016-03-11 04:30:43 +00002411 case ABIArgInfo::CoerceAndExpand: {
2412 // Reconstruct into a temporary.
2413 Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
2414 ArgVals.push_back(ParamValue::forIndirect(alloca));
2415
2416 auto coercionType = ArgI.getCoerceAndExpandType();
2417 alloca = Builder.CreateElementBitCast(alloca, coercionType);
2418 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
2419
2420 unsigned argIndex = FirstIRArg;
2421 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2422 llvm::Type *eltType = coercionType->getElementType(i);
2423 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
2424 continue;
2425
2426 auto eltAddr = Builder.CreateStructGEP(alloca, i, layout);
2427 auto elt = FnArgs[argIndex++];
2428 Builder.CreateStore(elt, eltAddr);
2429 }
2430 assert(argIndex == FirstIRArg + NumIRArgs);
2431 break;
2432 }
2433
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002434 case ABIArgInfo::Expand: {
2435 // If this structure was expanded into multiple arguments then
2436 // we need to create a temporary and reconstruct it from the
2437 // arguments.
John McCall7f416cc2015-09-08 08:05:57 +00002438 Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
2439 LValue LV = MakeAddrLValue(Alloca, Ty);
2440 ArgVals.push_back(ParamValue::forIndirect(Alloca));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002441
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002442 auto FnArgIter = FnArgs.begin() + FirstIRArg;
2443 ExpandTypeFromArgs(Ty, LV, FnArgIter);
2444 assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
2445 for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
2446 auto AI = FnArgs[FirstIRArg + i];
2447 AI->setName(Arg->getName() + "." + Twine(i));
2448 }
2449 break;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002450 }
2451
2452 case ABIArgInfo::Ignore:
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002453 assert(NumIRArgs == 0);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002454 // Initialize the local variable appropriately.
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002455 if (!hasScalarEvaluationKind(Ty)) {
John McCall7f416cc2015-09-08 08:05:57 +00002456 ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002457 } else {
2458 llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
John McCall7f416cc2015-09-08 08:05:57 +00002459 ArgVals.push_back(ParamValue::forDirect(U));
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002460 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00002461 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +00002462 }
Daniel Dunbar613855c2008-09-09 23:27:19 +00002463 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002464
Reid Kleckner739756c2013-12-04 19:23:12 +00002465 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2466 for (int I = Args.size() - 1; I >= 0; --I)
John McCall7f416cc2015-09-08 08:05:57 +00002467 EmitParmDecl(*Args[I], ArgVals[I], I + 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00002468 } else {
2469 for (unsigned I = 0, E = Args.size(); I != E; ++I)
John McCall7f416cc2015-09-08 08:05:57 +00002470 EmitParmDecl(*Args[I], ArgVals[I], I + 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00002471 }
Daniel Dunbar613855c2008-09-09 23:27:19 +00002472}
2473
John McCallffa2c1a2012-01-29 07:46:59 +00002474static void eraseUnusedBitCasts(llvm::Instruction *insn) {
2475 while (insn->use_empty()) {
2476 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
2477 if (!bitcast) return;
2478
2479 // This is "safe" because we would have used a ConstantExpr otherwise.
2480 insn = cast<llvm::Instruction>(bitcast->getOperand(0));
2481 bitcast->eraseFromParent();
2482 }
2483}
2484
John McCall31168b02011-06-15 23:02:42 +00002485/// Try to emit a fused autorelease of a return result.
2486static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
2487 llvm::Value *result) {
2488 // We must be immediately followed the cast.
2489 llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
Craig Topper8a13c412014-05-21 05:09:00 +00002490 if (BB->empty()) return nullptr;
2491 if (&BB->back() != result) return nullptr;
John McCall31168b02011-06-15 23:02:42 +00002492
Chris Lattner2192fe52011-07-18 04:24:23 +00002493 llvm::Type *resultType = result->getType();
John McCall31168b02011-06-15 23:02:42 +00002494
2495 // result is in a BasicBlock and is therefore an Instruction.
2496 llvm::Instruction *generator = cast<llvm::Instruction>(result);
2497
Justin Bogner882f8612016-08-18 21:46:54 +00002498 SmallVector<llvm::Instruction *, 4> InstsToKill;
John McCall31168b02011-06-15 23:02:42 +00002499
2500 // Look for:
2501 // %generator = bitcast %type1* %generator2 to %type2*
2502 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
2503 // We would have emitted this as a constant if the operand weren't
2504 // an Instruction.
2505 generator = cast<llvm::Instruction>(bitcast->getOperand(0));
2506
2507 // Require the generator to be immediately followed by the cast.
2508 if (generator->getNextNode() != bitcast)
Craig Topper8a13c412014-05-21 05:09:00 +00002509 return nullptr;
John McCall31168b02011-06-15 23:02:42 +00002510
Justin Bogner882f8612016-08-18 21:46:54 +00002511 InstsToKill.push_back(bitcast);
John McCall31168b02011-06-15 23:02:42 +00002512 }
2513
2514 // Look for:
2515 // %generator = call i8* @objc_retain(i8* %originalResult)
2516 // or
2517 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
2518 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
Craig Topper8a13c412014-05-21 05:09:00 +00002519 if (!call) return nullptr;
John McCall31168b02011-06-15 23:02:42 +00002520
2521 bool doRetainAutorelease;
2522
John McCallb04ecb72015-10-21 18:06:43 +00002523 if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints().objc_retain) {
John McCall31168b02011-06-15 23:02:42 +00002524 doRetainAutorelease = true;
John McCallb04ecb72015-10-21 18:06:43 +00002525 } else if (call->getCalledValue() == CGF.CGM.getObjCEntrypoints()
John McCall31168b02011-06-15 23:02:42 +00002526 .objc_retainAutoreleasedReturnValue) {
2527 doRetainAutorelease = false;
2528
John McCallcfa4e9b2012-09-07 23:30:50 +00002529 // If we emitted an assembly marker for this call (and the
2530 // ARCEntrypoints field should have been set if so), go looking
2531 // for that call. If we can't find it, we can't do this
2532 // optimization. But it should always be the immediately previous
2533 // instruction, unless we needed bitcasts around the call.
John McCallb04ecb72015-10-21 18:06:43 +00002534 if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) {
John McCallcfa4e9b2012-09-07 23:30:50 +00002535 llvm::Instruction *prev = call->getPrevNode();
2536 assert(prev);
2537 if (isa<llvm::BitCastInst>(prev)) {
2538 prev = prev->getPrevNode();
2539 assert(prev);
2540 }
2541 assert(isa<llvm::CallInst>(prev));
2542 assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
John McCallb04ecb72015-10-21 18:06:43 +00002543 CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker);
Justin Bogner882f8612016-08-18 21:46:54 +00002544 InstsToKill.push_back(prev);
John McCallcfa4e9b2012-09-07 23:30:50 +00002545 }
John McCall31168b02011-06-15 23:02:42 +00002546 } else {
Craig Topper8a13c412014-05-21 05:09:00 +00002547 return nullptr;
John McCall31168b02011-06-15 23:02:42 +00002548 }
2549
2550 result = call->getArgOperand(0);
Justin Bogner882f8612016-08-18 21:46:54 +00002551 InstsToKill.push_back(call);
John McCall31168b02011-06-15 23:02:42 +00002552
2553 // Keep killing bitcasts, for sanity. Note that we no longer care
2554 // about precise ordering as long as there's exactly one use.
2555 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
2556 if (!bitcast->hasOneUse()) break;
Justin Bogner882f8612016-08-18 21:46:54 +00002557 InstsToKill.push_back(bitcast);
John McCall31168b02011-06-15 23:02:42 +00002558 result = bitcast->getOperand(0);
2559 }
2560
2561 // Delete all the unnecessary instructions, from latest to earliest.
Justin Bogner882f8612016-08-18 21:46:54 +00002562 for (auto *I : InstsToKill)
Saleem Abdulrasoolbe25c482016-08-18 21:40:06 +00002563 I->eraseFromParent();
John McCall31168b02011-06-15 23:02:42 +00002564
2565 // Do the fused retain/autorelease if we were asked to.
2566 if (doRetainAutorelease)
2567 result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
2568
2569 // Cast back to the result type.
2570 return CGF.Builder.CreateBitCast(result, resultType);
2571}
2572
John McCallffa2c1a2012-01-29 07:46:59 +00002573/// If this is a +1 of the value of an immutable 'self', remove it.
2574static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
2575 llvm::Value *result) {
2576 // This is only applicable to a method with an immutable 'self'.
John McCallff755cd2012-07-31 00:33:55 +00002577 const ObjCMethodDecl *method =
2578 dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
Craig Topper8a13c412014-05-21 05:09:00 +00002579 if (!method) return nullptr;
John McCallffa2c1a2012-01-29 07:46:59 +00002580 const VarDecl *self = method->getSelfDecl();
Craig Topper8a13c412014-05-21 05:09:00 +00002581 if (!self->getType().isConstQualified()) return nullptr;
John McCallffa2c1a2012-01-29 07:46:59 +00002582
2583 // Look for a retain call.
2584 llvm::CallInst *retainCall =
2585 dyn_cast<llvm::CallInst>(result->stripPointerCasts());
2586 if (!retainCall ||
John McCallb04ecb72015-10-21 18:06:43 +00002587 retainCall->getCalledValue() != CGF.CGM.getObjCEntrypoints().objc_retain)
Craig Topper8a13c412014-05-21 05:09:00 +00002588 return nullptr;
John McCallffa2c1a2012-01-29 07:46:59 +00002589
2590 // Look for an ordinary load of 'self'.
2591 llvm::Value *retainedValue = retainCall->getArgOperand(0);
2592 llvm::LoadInst *load =
2593 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
2594 if (!load || load->isAtomic() || load->isVolatile() ||
John McCall7f416cc2015-09-08 08:05:57 +00002595 load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer())
Craig Topper8a13c412014-05-21 05:09:00 +00002596 return nullptr;
John McCallffa2c1a2012-01-29 07:46:59 +00002597
2598 // Okay! Burn it all down. This relies for correctness on the
2599 // assumption that the retain is emitted as part of the return and
2600 // that thereafter everything is used "linearly".
2601 llvm::Type *resultType = result->getType();
2602 eraseUnusedBitCasts(cast<llvm::Instruction>(result));
2603 assert(retainCall->use_empty());
2604 retainCall->eraseFromParent();
2605 eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
2606
2607 return CGF.Builder.CreateBitCast(load, resultType);
2608}
2609
John McCall31168b02011-06-15 23:02:42 +00002610/// Emit an ARC autorelease of the result of a function.
John McCallffa2c1a2012-01-29 07:46:59 +00002611///
2612/// \return the value to actually return from the function
John McCall31168b02011-06-15 23:02:42 +00002613static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
2614 llvm::Value *result) {
John McCallffa2c1a2012-01-29 07:46:59 +00002615 // If we're returning 'self', kill the initial retain. This is a
2616 // heuristic attempt to "encourage correctness" in the really unfortunate
2617 // case where we have a return of self during a dealloc and we desperately
2618 // need to avoid the possible autorelease.
2619 if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
2620 return self;
2621
John McCall31168b02011-06-15 23:02:42 +00002622 // At -O0, try to emit a fused retain/autorelease.
2623 if (CGF.shouldUseFusedARCCalls())
2624 if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
2625 return fused;
2626
2627 return CGF.EmitARCAutoreleaseReturnValue(result);
2628}
2629
John McCall6e1c0122012-01-29 02:35:02 +00002630/// Heuristically search for a dominating store to the return-value slot.
2631static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
Jakub Kuderskif50ab0f2015-09-08 10:36:42 +00002632 // Check if a User is a store which pointerOperand is the ReturnValue.
2633 // We are looking for stores to the ReturnValue, not for stores of the
2634 // ReturnValue to some other location.
2635 auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
2636 auto *SI = dyn_cast<llvm::StoreInst>(U);
2637 if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer())
2638 return nullptr;
2639 // These aren't actually possible for non-coerced returns, and we
2640 // only care about non-coerced returns on this code path.
2641 assert(!SI->isAtomic() && !SI->isVolatile());
2642 return SI;
2643 };
John McCall6e1c0122012-01-29 02:35:02 +00002644 // If there are multiple uses of the return-value slot, just check
2645 // for something immediately preceding the IP. Sometimes this can
2646 // happen with how we generate implicit-returns; it can also happen
2647 // with noreturn cleanups.
John McCall7f416cc2015-09-08 08:05:57 +00002648 if (!CGF.ReturnValue.getPointer()->hasOneUse()) {
John McCall6e1c0122012-01-29 02:35:02 +00002649 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
Craig Topper8a13c412014-05-21 05:09:00 +00002650 if (IP->empty()) return nullptr;
David Majnemerdc012fa2015-04-22 21:38:15 +00002651 llvm::Instruction *I = &IP->back();
2652
2653 // Skip lifetime markers
2654 for (llvm::BasicBlock::reverse_iterator II = IP->rbegin(),
2655 IE = IP->rend();
2656 II != IE; ++II) {
2657 if (llvm::IntrinsicInst *Intrinsic =
2658 dyn_cast<llvm::IntrinsicInst>(&*II)) {
2659 if (Intrinsic->getIntrinsicID() == llvm::Intrinsic::lifetime_end) {
2660 const llvm::Value *CastAddr = Intrinsic->getArgOperand(1);
2661 ++II;
Alexey Samsonov10544202015-06-12 21:05:32 +00002662 if (II == IE)
2663 break;
2664 if (isa<llvm::BitCastInst>(&*II) && (CastAddr == &*II))
2665 continue;
David Majnemerdc012fa2015-04-22 21:38:15 +00002666 }
2667 }
2668 I = &*II;
2669 break;
2670 }
2671
Jakub Kuderskif50ab0f2015-09-08 10:36:42 +00002672 return GetStoreIfValid(I);
John McCall6e1c0122012-01-29 02:35:02 +00002673 }
2674
2675 llvm::StoreInst *store =
Jakub Kuderskif50ab0f2015-09-08 10:36:42 +00002676 GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back());
Craig Topper8a13c412014-05-21 05:09:00 +00002677 if (!store) return nullptr;
John McCall6e1c0122012-01-29 02:35:02 +00002678
John McCall6e1c0122012-01-29 02:35:02 +00002679 // Now do a first-and-dirty dominance check: just walk up the
2680 // single-predecessors chain from the current insertion point.
2681 llvm::BasicBlock *StoreBB = store->getParent();
2682 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
2683 while (IP != StoreBB) {
2684 if (!(IP = IP->getSinglePredecessor()))
Craig Topper8a13c412014-05-21 05:09:00 +00002685 return nullptr;
John McCall6e1c0122012-01-29 02:35:02 +00002686 }
2687
2688 // Okay, the store's basic block dominates the insertion point; we
2689 // can do our thing.
2690 return store;
2691}
2692
Adrian Prantl3be10542013-05-02 17:30:20 +00002693void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Nick Lewycky2d84e842013-10-02 02:29:49 +00002694 bool EmitRetDbgLoc,
2695 SourceLocation EndLoc) {
Hans Wennborgd71907d2014-09-04 22:16:33 +00002696 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
2697 // Naked functions don't have epilogues.
2698 Builder.CreateUnreachable();
2699 return;
2700 }
2701
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00002702 // Functions with no result always return void.
John McCall7f416cc2015-09-08 08:05:57 +00002703 if (!ReturnValue.isValid()) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00002704 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +00002705 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00002706 }
Daniel Dunbar6696e222010-06-30 21:27:58 +00002707
Dan Gohman481e40c2010-07-20 20:13:52 +00002708 llvm::DebugLoc RetDbgLoc;
Craig Topper8a13c412014-05-21 05:09:00 +00002709 llvm::Value *RV = nullptr;
Chris Lattner726b3d02010-06-26 23:13:19 +00002710 QualType RetTy = FI.getReturnType();
2711 const ABIArgInfo &RetAI = FI.getReturnInfo();
2712
2713 switch (RetAI.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002714 case ABIArgInfo::InAlloca:
Reid Klecknerfab1e892014-02-25 00:59:14 +00002715 // Aggregrates get evaluated directly into the destination. Sometimes we
2716 // need to return the sret value in a register, though.
2717 assert(hasAggregateEvaluationKind(RetTy));
2718 if (RetAI.getInAllocaSRet()) {
2719 llvm::Function::arg_iterator EI = CurFn->arg_end();
2720 --EI;
Duncan P. N. Exon Smith9f5260a2015-11-06 23:00:41 +00002721 llvm::Value *ArgStruct = &*EI;
David Blaikie2e804282015-04-05 22:47:07 +00002722 llvm::Value *SRet = Builder.CreateStructGEP(
2723 nullptr, ArgStruct, RetAI.getInAllocaFieldIndex());
John McCall7f416cc2015-09-08 08:05:57 +00002724 RV = Builder.CreateAlignedLoad(SRet, getPointerAlign(), "sret");
Reid Klecknerfab1e892014-02-25 00:59:14 +00002725 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002726 break;
2727
Daniel Dunbar03816342010-08-21 02:24:36 +00002728 case ABIArgInfo::Indirect: {
Reid Kleckner37abaca2014-05-09 22:46:15 +00002729 auto AI = CurFn->arg_begin();
2730 if (RetAI.isSRetAfterThis())
2731 ++AI;
John McCall47fb9502013-03-07 21:37:08 +00002732 switch (getEvaluationKind(RetTy)) {
2733 case TEK_Complex: {
2734 ComplexPairTy RT =
John McCall7f416cc2015-09-08 08:05:57 +00002735 EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
Duncan P. N. Exon Smith9f5260a2015-11-06 23:00:41 +00002736 EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
John McCall47fb9502013-03-07 21:37:08 +00002737 /*isInit*/ true);
2738 break;
2739 }
2740 case TEK_Aggregate:
Chris Lattner726b3d02010-06-26 23:13:19 +00002741 // Do nothing; aggregrates get evaluated directly into the destination.
John McCall47fb9502013-03-07 21:37:08 +00002742 break;
2743 case TEK_Scalar:
2744 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
Duncan P. N. Exon Smith9f5260a2015-11-06 23:00:41 +00002745 MakeNaturalAlignAddrLValue(&*AI, RetTy),
John McCall47fb9502013-03-07 21:37:08 +00002746 /*isInit*/ true);
2747 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00002748 }
2749 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00002750 }
Chris Lattner726b3d02010-06-26 23:13:19 +00002751
2752 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002753 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002754 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
2755 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002756 // The internal return value temp always will have pointer-to-return-type
2757 // type, just do a load.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002758
John McCall6e1c0122012-01-29 02:35:02 +00002759 // If there is a dominating store to ReturnValue, we can elide
2760 // the load, zap the store, and usually zap the alloca.
David Majnemerdc012fa2015-04-22 21:38:15 +00002761 if (llvm::StoreInst *SI =
2762 findDominatingStoreToReturnValue(*this)) {
Adrian Prantl4c9a38a2013-05-30 18:12:23 +00002763 // Reuse the debug location from the store unless there is
2764 // cleanup code to be emitted between the store and return
2765 // instruction.
2766 if (EmitRetDbgLoc && !AutoreleaseResult)
Adrian Prantl3be10542013-05-02 17:30:20 +00002767 RetDbgLoc = SI->getDebugLoc();
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002768 // Get the stored value and nuke the now-dead store.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002769 RV = SI->getValueOperand();
2770 SI->eraseFromParent();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002771
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002772 // If that was the only use of the return value, nuke it as well now.
John McCall7f416cc2015-09-08 08:05:57 +00002773 auto returnValueInst = ReturnValue.getPointer();
2774 if (returnValueInst->use_empty()) {
2775 if (auto alloca = dyn_cast<llvm::AllocaInst>(returnValueInst)) {
2776 alloca->eraseFromParent();
2777 ReturnValue = Address::invalid();
2778 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002779 }
John McCall6e1c0122012-01-29 02:35:02 +00002780
2781 // Otherwise, we have to do a simple load.
2782 } else {
2783 RV = Builder.CreateLoad(ReturnValue);
Chris Lattner3fcc7902010-06-27 01:06:27 +00002784 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002785 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002786 // If the value is offset in memory, apply the offset now.
John McCall7f416cc2015-09-08 08:05:57 +00002787 Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002788
John McCall7f416cc2015-09-08 08:05:57 +00002789 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00002790 }
John McCall31168b02011-06-15 23:02:42 +00002791
2792 // In ARC, end functions that return a retainable type with a call
2793 // to objc_autoreleaseReturnValue.
2794 if (AutoreleaseResult) {
Akira Hatanaka9d8ac612016-02-17 21:09:50 +00002795#ifndef NDEBUG
2796 // Type::isObjCRetainabletype has to be called on a QualType that hasn't
2797 // been stripped of the typedefs, so we cannot use RetTy here. Get the
2798 // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
2799 // CurCodeDecl or BlockInfo.
2800 QualType RT;
2801
2802 if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
2803 RT = FD->getReturnType();
2804 else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
2805 RT = MD->getReturnType();
2806 else if (isa<BlockDecl>(CurCodeDecl))
2807 RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
2808 else
2809 llvm_unreachable("Unexpected function/method type");
2810
David Blaikiebbafb8a2012-03-11 07:00:24 +00002811 assert(getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002812 !FI.isReturnsRetained() &&
Akira Hatanaka9d8ac612016-02-17 21:09:50 +00002813 RT->isObjCRetainableType());
2814#endif
John McCall31168b02011-06-15 23:02:42 +00002815 RV = emitAutoreleaseOfResult(*this, RV);
2816 }
2817
Chris Lattner726b3d02010-06-26 23:13:19 +00002818 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00002819
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002820 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00002821 break;
2822
John McCallf26e73d2016-03-11 04:30:43 +00002823 case ABIArgInfo::CoerceAndExpand: {
2824 auto coercionType = RetAI.getCoerceAndExpandType();
2825 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
2826
2827 // Load all of the coerced elements out into results.
2828 llvm::SmallVector<llvm::Value*, 4> results;
2829 Address addr = Builder.CreateElementBitCast(ReturnValue, coercionType);
2830 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2831 auto coercedEltType = coercionType->getElementType(i);
2832 if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
2833 continue;
2834
2835 auto eltAddr = Builder.CreateStructGEP(addr, i, layout);
2836 auto elt = Builder.CreateLoad(eltAddr);
2837 results.push_back(elt);
2838 }
2839
2840 // If we have one result, it's the single direct result type.
2841 if (results.size() == 1) {
2842 RV = results[0];
2843
2844 // Otherwise, we need to make a first-class aggregate.
2845 } else {
2846 // Construct a return type that lacks padding elements.
2847 llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
2848
2849 RV = llvm::UndefValue::get(returnType);
2850 for (unsigned i = 0, e = results.size(); i != e; ++i) {
2851 RV = Builder.CreateInsertValue(RV, results[i], i);
2852 }
2853 }
2854 break;
2855 }
2856
Chris Lattner726b3d02010-06-26 23:13:19 +00002857 case ABIArgInfo::Expand:
David Blaikie83d382b2011-09-23 05:06:16 +00002858 llvm_unreachable("Invalid ABI kind for return argument");
Chris Lattner726b3d02010-06-26 23:13:19 +00002859 }
2860
Alexey Samsonovde443c52014-08-13 00:26:40 +00002861 llvm::Instruction *Ret;
2862 if (RV) {
John McCall9a2c1c92015-09-10 00:57:46 +00002863 if (CurCodeDecl && SanOpts.has(SanitizerKind::ReturnsNonnullAttribute)) {
2864 if (auto RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>()) {
Alexey Samsonov90452df2014-09-08 20:17:19 +00002865 SanitizerScope SanScope(this);
2866 llvm::Value *Cond = Builder.CreateICmpNE(
2867 RV, llvm::Constant::getNullValue(RV->getType()));
2868 llvm::Constant *StaticData[] = {
2869 EmitCheckSourceLocation(EndLoc),
2870 EmitCheckSourceLocation(RetNNAttr->getLocation()),
2871 };
Alexey Samsonove396bfc2014-11-11 22:03:54 +00002872 EmitCheck(std::make_pair(Cond, SanitizerKind::ReturnsNonnullAttribute),
Filipe Cabecinhas322ecd92016-12-12 16:18:40 +00002873 SanitizerHandler::NonnullReturn, StaticData, None);
Alexey Samsonov90452df2014-09-08 20:17:19 +00002874 }
Alexey Samsonovde443c52014-08-13 00:26:40 +00002875 }
2876 Ret = Builder.CreateRet(RV);
2877 } else {
2878 Ret = Builder.CreateRetVoid();
2879 }
2880
Duncan P. N. Exon Smith2809cc72015-03-30 20:01:41 +00002881 if (RetDbgLoc)
Benjamin Kramer03278662015-02-07 13:15:54 +00002882 Ret->setDebugLoc(std::move(RetDbgLoc));
Daniel Dunbar613855c2008-09-09 23:27:19 +00002883}
2884
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002885static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
2886 const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
2887 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
2888}
2889
John McCall7f416cc2015-09-08 08:05:57 +00002890static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF,
2891 QualType Ty) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002892 // FIXME: Generate IR in one pass, rather than going back and fixing up these
2893 // placeholders.
2894 llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
Peter Collingbourneb367c562016-11-28 22:30:21 +00002895 llvm::Type *IRPtrTy = IRTy->getPointerTo();
2896 llvm::Value *Placeholder = llvm::UndefValue::get(IRPtrTy->getPointerTo());
John McCall7f416cc2015-09-08 08:05:57 +00002897
2898 // FIXME: When we generate this IR in one pass, we shouldn't need
2899 // this win32-specific alignment hack.
2900 CharUnits Align = CharUnits::fromQuantity(4);
Peter Collingbourneb367c562016-11-28 22:30:21 +00002901 Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
John McCall7f416cc2015-09-08 08:05:57 +00002902
2903 return AggValueSlot::forAddr(Address(Placeholder, Align),
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002904 Ty.getQualifiers(),
2905 AggValueSlot::IsNotDestructed,
2906 AggValueSlot::DoesNotNeedGCBarriers,
2907 AggValueSlot::IsNotAliased);
2908}
2909
John McCall32ea9692011-03-11 20:59:21 +00002910void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
Nick Lewycky2d84e842013-10-02 02:29:49 +00002911 const VarDecl *param,
2912 SourceLocation loc) {
John McCall23f66262010-05-26 22:34:26 +00002913 // StartFunction converted the ABI-lowered parameter(s) into a
2914 // local alloca. We need to turn that into an r-value suitable
2915 // for EmitCall.
John McCall7f416cc2015-09-08 08:05:57 +00002916 Address local = GetAddrOfLocalVar(param);
John McCall23f66262010-05-26 22:34:26 +00002917
John McCall32ea9692011-03-11 20:59:21 +00002918 QualType type = param->getType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002919
Reid Klecknerab2090d2014-07-26 01:34:32 +00002920 assert(!isInAllocaArgument(CGM.getCXXABI(), type) &&
2921 "cannot emit delegate call arguments for inalloca arguments!");
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002922
John McCall811b2912016-11-18 01:08:24 +00002923 // GetAddrOfLocalVar returns a pointer-to-pointer for references,
2924 // but the argument needs to be the original pointer.
2925 if (type->isReferenceType()) {
2926 args.add(RValue::get(Builder.CreateLoad(local)), type);
2927
2928 // In ARC, move out of consumed arguments so that the release cleanup
2929 // entered by StartFunction doesn't cause an over-release. This isn't
2930 // optimal -O0 code generation, but it should get cleaned up when
2931 // optimization is enabled. This also assumes that delegate calls are
2932 // performed exactly once for a set of arguments, but that should be safe.
2933 } else if (getLangOpts().ObjCAutoRefCount &&
2934 param->hasAttr<NSConsumedAttr>() &&
2935 type->isObjCRetainableType()) {
2936 llvm::Value *ptr = Builder.CreateLoad(local);
2937 auto null =
2938 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
2939 Builder.CreateStore(null, local);
2940 args.add(RValue::get(ptr), type);
2941
Richard Smithd62d4982016-06-14 01:13:21 +00002942 // For the most part, we just need to load the alloca, except that
2943 // aggregate r-values are actually pointers to temporaries.
John McCall811b2912016-11-18 01:08:24 +00002944 } else {
Richard Smithd62d4982016-06-14 01:13:21 +00002945 args.add(convertTempToRValue(local, type, loc), type);
John McCall811b2912016-11-18 01:08:24 +00002946 }
John McCall23f66262010-05-26 22:34:26 +00002947}
2948
John McCall31168b02011-06-15 23:02:42 +00002949static bool isProvablyNull(llvm::Value *addr) {
2950 return isa<llvm::ConstantPointerNull>(addr);
2951}
2952
John McCall31168b02011-06-15 23:02:42 +00002953/// Emit the actual writing-back of a writeback.
2954static void emitWriteback(CodeGenFunction &CGF,
2955 const CallArgList::Writeback &writeback) {
John McCalleff18842013-03-23 02:35:54 +00002956 const LValue &srcLV = writeback.Source;
John McCall7f416cc2015-09-08 08:05:57 +00002957 Address srcAddr = srcLV.getAddress();
2958 assert(!isProvablyNull(srcAddr.getPointer()) &&
John McCall31168b02011-06-15 23:02:42 +00002959 "shouldn't have writeback for provably null argument");
2960
Craig Topper8a13c412014-05-21 05:09:00 +00002961 llvm::BasicBlock *contBB = nullptr;
John McCall31168b02011-06-15 23:02:42 +00002962
2963 // If the argument wasn't provably non-null, we need to null check
2964 // before doing the store.
Nick Lewyckyd9bce502016-09-20 15:49:58 +00002965 bool provablyNonNull = llvm::isKnownNonNull(srcAddr.getPointer());
John McCall31168b02011-06-15 23:02:42 +00002966 if (!provablyNonNull) {
2967 llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
2968 contBB = CGF.createBasicBlock("icr.done");
2969
John McCall7f416cc2015-09-08 08:05:57 +00002970 llvm::Value *isNull =
2971 CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
John McCall31168b02011-06-15 23:02:42 +00002972 CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
2973 CGF.EmitBlock(writebackBB);
2974 }
2975
2976 // Load the value to writeback.
2977 llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
2978
2979 // Cast it back, in case we're writing an id to a Foo* or something.
John McCall7f416cc2015-09-08 08:05:57 +00002980 value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
2981 "icr.writeback-cast");
John McCall31168b02011-06-15 23:02:42 +00002982
2983 // Perform the writeback.
John McCalleff18842013-03-23 02:35:54 +00002984
2985 // If we have a "to use" value, it's something we need to emit a use
2986 // of. This has to be carefully threaded in: if it's done after the
2987 // release it's potentially undefined behavior (and the optimizer
2988 // will ignore it), and if it happens before the retain then the
2989 // optimizer could move the release there.
2990 if (writeback.ToUse) {
2991 assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
2992
2993 // Retain the new value. No need to block-copy here: the block's
2994 // being passed up the stack.
2995 value = CGF.EmitARCRetainNonBlock(value);
2996
2997 // Emit the intrinsic use here.
2998 CGF.EmitARCIntrinsicUse(writeback.ToUse);
2999
3000 // Load the old value (primitively).
Nick Lewycky2d84e842013-10-02 02:29:49 +00003001 llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
John McCalleff18842013-03-23 02:35:54 +00003002
3003 // Put the new value in place (primitively).
3004 CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
3005
3006 // Release the old value.
3007 CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
3008
3009 // Otherwise, we can just do a normal lvalue store.
3010 } else {
3011 CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
3012 }
John McCall31168b02011-06-15 23:02:42 +00003013
3014 // Jump to the continuation block.
3015 if (!provablyNonNull)
3016 CGF.EmitBlock(contBB);
3017}
3018
3019static void emitWritebacks(CodeGenFunction &CGF,
3020 const CallArgList &args) {
Aaron Ballman36a7fa82014-03-17 17:22:27 +00003021 for (const auto &I : args.writebacks())
3022 emitWriteback(CGF, I);
John McCall31168b02011-06-15 23:02:42 +00003023}
3024
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003025static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
3026 const CallArgList &CallArgs) {
Reid Kleckner739756c2013-12-04 19:23:12 +00003027 assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee());
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003028 ArrayRef<CallArgList::CallArgCleanup> Cleanups =
3029 CallArgs.getCleanupsToDeactivate();
3030 // Iterate in reverse to increase the likelihood of popping the cleanup.
Pete Cooper57d3f142015-07-30 17:22:52 +00003031 for (const auto &I : llvm::reverse(Cleanups)) {
3032 CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
3033 I.IsActiveIP->eraseFromParent();
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003034 }
3035}
3036
John McCalleff18842013-03-23 02:35:54 +00003037static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
3038 if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
3039 if (uop->getOpcode() == UO_AddrOf)
3040 return uop->getSubExpr();
Craig Topper8a13c412014-05-21 05:09:00 +00003041 return nullptr;
John McCalleff18842013-03-23 02:35:54 +00003042}
3043
John McCall31168b02011-06-15 23:02:42 +00003044/// Emit an argument that's being passed call-by-writeback. That is,
John McCall7f416cc2015-09-08 08:05:57 +00003045/// we are passing the address of an __autoreleased temporary; it
3046/// might be copy-initialized with the current value of the given
3047/// address, but it will definitely be copied out of after the call.
John McCall31168b02011-06-15 23:02:42 +00003048static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
3049 const ObjCIndirectCopyRestoreExpr *CRE) {
John McCalleff18842013-03-23 02:35:54 +00003050 LValue srcLV;
3051
3052 // Make an optimistic effort to emit the address as an l-value.
Eric Christopher2c4555a2015-06-19 01:52:53 +00003053 // This can fail if the argument expression is more complicated.
John McCalleff18842013-03-23 02:35:54 +00003054 if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
3055 srcLV = CGF.EmitLValue(lvExpr);
3056
3057 // Otherwise, just emit it as a scalar.
3058 } else {
John McCall7f416cc2015-09-08 08:05:57 +00003059 Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
John McCalleff18842013-03-23 02:35:54 +00003060
3061 QualType srcAddrType =
3062 CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
John McCall7f416cc2015-09-08 08:05:57 +00003063 srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
John McCalleff18842013-03-23 02:35:54 +00003064 }
John McCall7f416cc2015-09-08 08:05:57 +00003065 Address srcAddr = srcLV.getAddress();
John McCall31168b02011-06-15 23:02:42 +00003066
3067 // The dest and src types don't necessarily match in LLVM terms
3068 // because of the crazy ObjC compatibility rules.
3069
Chris Lattner2192fe52011-07-18 04:24:23 +00003070 llvm::PointerType *destType =
John McCall31168b02011-06-15 23:02:42 +00003071 cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
3072
3073 // If the address is a constant null, just pass the appropriate null.
John McCall7f416cc2015-09-08 08:05:57 +00003074 if (isProvablyNull(srcAddr.getPointer())) {
John McCall31168b02011-06-15 23:02:42 +00003075 args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
3076 CRE->getType());
3077 return;
3078 }
3079
John McCall31168b02011-06-15 23:02:42 +00003080 // Create the temporary.
John McCall7f416cc2015-09-08 08:05:57 +00003081 Address temp = CGF.CreateTempAlloca(destType->getElementType(),
3082 CGF.getPointerAlign(),
3083 "icr.temp");
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00003084 // Loading an l-value can introduce a cleanup if the l-value is __weak,
3085 // and that cleanup will be conditional if we can't prove that the l-value
3086 // isn't null, so we need to register a dominating point so that the cleanups
3087 // system will make valid IR.
3088 CodeGenFunction::ConditionalEvaluation condEval(CGF);
3089
John McCall31168b02011-06-15 23:02:42 +00003090 // Zero-initialize it if we're not doing a copy-initialization.
3091 bool shouldCopy = CRE->shouldCopy();
3092 if (!shouldCopy) {
3093 llvm::Value *null =
3094 llvm::ConstantPointerNull::get(
3095 cast<llvm::PointerType>(destType->getElementType()));
3096 CGF.Builder.CreateStore(null, temp);
3097 }
Craig Topper8a13c412014-05-21 05:09:00 +00003098
3099 llvm::BasicBlock *contBB = nullptr;
3100 llvm::BasicBlock *originBB = nullptr;
John McCall31168b02011-06-15 23:02:42 +00003101
3102 // If the address is *not* known to be non-null, we need to switch.
3103 llvm::Value *finalArgument;
3104
Nick Lewyckyd9bce502016-09-20 15:49:58 +00003105 bool provablyNonNull = llvm::isKnownNonNull(srcAddr.getPointer());
John McCall31168b02011-06-15 23:02:42 +00003106 if (provablyNonNull) {
John McCall7f416cc2015-09-08 08:05:57 +00003107 finalArgument = temp.getPointer();
John McCall31168b02011-06-15 23:02:42 +00003108 } else {
John McCall7f416cc2015-09-08 08:05:57 +00003109 llvm::Value *isNull =
3110 CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
John McCall31168b02011-06-15 23:02:42 +00003111
3112 finalArgument = CGF.Builder.CreateSelect(isNull,
3113 llvm::ConstantPointerNull::get(destType),
John McCall7f416cc2015-09-08 08:05:57 +00003114 temp.getPointer(), "icr.argument");
John McCall31168b02011-06-15 23:02:42 +00003115
3116 // If we need to copy, then the load has to be conditional, which
3117 // means we need control flow.
3118 if (shouldCopy) {
John McCalleff18842013-03-23 02:35:54 +00003119 originBB = CGF.Builder.GetInsertBlock();
John McCall31168b02011-06-15 23:02:42 +00003120 contBB = CGF.createBasicBlock("icr.cont");
3121 llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
3122 CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
3123 CGF.EmitBlock(copyBB);
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00003124 condEval.begin(CGF);
John McCall31168b02011-06-15 23:02:42 +00003125 }
3126 }
3127
Craig Topper8a13c412014-05-21 05:09:00 +00003128 llvm::Value *valueToUse = nullptr;
John McCalleff18842013-03-23 02:35:54 +00003129
John McCall31168b02011-06-15 23:02:42 +00003130 // Perform a copy if necessary.
3131 if (shouldCopy) {
Nick Lewycky2d84e842013-10-02 02:29:49 +00003132 RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
John McCall31168b02011-06-15 23:02:42 +00003133 assert(srcRV.isScalar());
3134
3135 llvm::Value *src = srcRV.getScalarVal();
3136 src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
3137 "icr.cast");
3138
3139 // Use an ordinary store, not a store-to-lvalue.
3140 CGF.Builder.CreateStore(src, temp);
John McCalleff18842013-03-23 02:35:54 +00003141
3142 // If optimization is enabled, and the value was held in a
3143 // __strong variable, we need to tell the optimizer that this
3144 // value has to stay alive until we're doing the store back.
3145 // This is because the temporary is effectively unretained,
3146 // and so otherwise we can violate the high-level semantics.
3147 if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
3148 srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
3149 valueToUse = src;
3150 }
John McCall31168b02011-06-15 23:02:42 +00003151 }
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00003152
John McCall31168b02011-06-15 23:02:42 +00003153 // Finish the control flow if we needed it.
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00003154 if (shouldCopy && !provablyNonNull) {
John McCalleff18842013-03-23 02:35:54 +00003155 llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
John McCall31168b02011-06-15 23:02:42 +00003156 CGF.EmitBlock(contBB);
John McCalleff18842013-03-23 02:35:54 +00003157
3158 // Make a phi for the value to intrinsically use.
3159 if (valueToUse) {
3160 llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
3161 "icr.to-use");
3162 phiToUse->addIncoming(valueToUse, copyBB);
3163 phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
3164 originBB);
3165 valueToUse = phiToUse;
3166 }
3167
Fariborz Jahanianfbd19742012-11-27 23:02:53 +00003168 condEval.end(CGF);
3169 }
John McCall31168b02011-06-15 23:02:42 +00003170
John McCalleff18842013-03-23 02:35:54 +00003171 args.addWriteback(srcLV, temp, valueToUse);
John McCall31168b02011-06-15 23:02:42 +00003172 args.add(RValue::get(finalArgument), CRE->getType());
3173}
3174
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003175void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
Richard Smith762672a2016-09-28 19:09:10 +00003176 assert(!StackBase);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003177
3178 // Save the stack.
3179 llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
David Blaikie43f9bb72015-05-18 22:14:03 +00003180 StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save");
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003181}
3182
Nico Weber8cdb3f92015-08-25 18:43:32 +00003183void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
3184 if (StackBase) {
Reid Kleckner7c2f9e82015-10-08 00:17:45 +00003185 // Restore the stack after the call.
Nico Weber8cdb3f92015-08-25 18:43:32 +00003186 llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
Nico Weber8cdb3f92015-08-25 18:43:32 +00003187 CGF.Builder.CreateCall(F, StackBase);
3188 }
3189}
3190
Nuno Lopes1ba2d782015-05-30 16:11:40 +00003191void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
3192 SourceLocation ArgLoc,
3193 const FunctionDecl *FD,
3194 unsigned ParmNum) {
3195 if (!SanOpts.has(SanitizerKind::NonnullAttribute) || !FD)
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00003196 return;
3197 auto PVD = ParmNum < FD->getNumParams() ? FD->getParamDecl(ParmNum) : nullptr;
3198 unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
3199 auto NNAttr = getNonNullAttr(FD, PVD, ArgType, ArgNo);
3200 if (!NNAttr)
3201 return;
Nuno Lopes1ba2d782015-05-30 16:11:40 +00003202 SanitizerScope SanScope(this);
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00003203 assert(RV.isScalar());
3204 llvm::Value *V = RV.getScalarVal();
3205 llvm::Value *Cond =
Nuno Lopes1ba2d782015-05-30 16:11:40 +00003206 Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00003207 llvm::Constant *StaticData[] = {
Nuno Lopes1ba2d782015-05-30 16:11:40 +00003208 EmitCheckSourceLocation(ArgLoc),
3209 EmitCheckSourceLocation(NNAttr->getLocation()),
3210 llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00003211 };
Nuno Lopes1ba2d782015-05-30 16:11:40 +00003212 EmitCheck(std::make_pair(Cond, SanitizerKind::NonnullAttribute),
Filipe Cabecinhas322ecd92016-12-12 16:18:40 +00003213 SanitizerHandler::NonnullArg, StaticData, None);
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00003214}
3215
David Blaikief05779e2015-07-21 18:37:18 +00003216void CodeGenFunction::EmitCallArgs(
3217 CallArgList &Args, ArrayRef<QualType> ArgTypes,
3218 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
Richard Smith762672a2016-09-28 19:09:10 +00003219 const FunctionDecl *CalleeDecl, unsigned ParamsToSkip,
Richard Smitha560ccf2016-09-29 21:30:12 +00003220 EvaluationOrder Order) {
David Blaikief05779e2015-07-21 18:37:18 +00003221 assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
George Burgess IV3e3bb95b2015-12-02 21:58:08 +00003222
3223 auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg) {
3224 if (CalleeDecl == nullptr || I >= CalleeDecl->getNumParams())
3225 return;
3226 auto *PS = CalleeDecl->getParamDecl(I)->getAttr<PassObjectSizeAttr>();
3227 if (PS == nullptr)
3228 return;
3229
3230 const auto &Context = getContext();
3231 auto SizeTy = Context.getSizeType();
3232 auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
3233 llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T);
3234 Args.add(RValue::get(V), SizeTy);
3235 };
3236
Reid Kleckner739756c2013-12-04 19:23:12 +00003237 // We *have* to evaluate arguments from right to left in the MS C++ ABI,
Richard Smitha560ccf2016-09-29 21:30:12 +00003238 // because arguments are destroyed left to right in the callee. As a special
3239 // case, there are certain language constructs that require left-to-right
3240 // evaluation, and in those cases we consider the evaluation order requirement
3241 // to trump the "destruction order is reverse construction order" guarantee.
3242 bool LeftToRight =
3243 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
3244 ? Order == EvaluationOrder::ForceLeftToRight
3245 : Order != EvaluationOrder::ForceRightToLeft;
3246
3247 // Insert a stack save if we're going to need any inalloca args.
3248 bool HasInAllocaArgs = false;
3249 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003250 for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end();
3251 I != E && !HasInAllocaArgs; ++I)
3252 HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I);
3253 if (HasInAllocaArgs) {
3254 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3255 Args.allocateArgumentMemory(*this);
3256 }
Richard Smitha560ccf2016-09-29 21:30:12 +00003257 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003258
Richard Smitha560ccf2016-09-29 21:30:12 +00003259 // Evaluate each argument in the appropriate order.
3260 size_t CallArgsStart = Args.size();
3261 for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
3262 unsigned Idx = LeftToRight ? I : E - I - 1;
3263 CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
3264 if (!LeftToRight) MaybeEmitImplicitObjectSize(Idx, *Arg);
3265 EmitCallArg(Args, *Arg, ArgTypes[Idx]);
3266 EmitNonNullArgCheck(Args.back().RV, ArgTypes[Idx], (*Arg)->getExprLoc(),
3267 CalleeDecl, ParamsToSkip + Idx);
3268 if (LeftToRight) MaybeEmitImplicitObjectSize(Idx, *Arg);
3269 }
Reid Kleckner739756c2013-12-04 19:23:12 +00003270
Richard Smitha560ccf2016-09-29 21:30:12 +00003271 if (!LeftToRight) {
Reid Kleckner739756c2013-12-04 19:23:12 +00003272 // Un-reverse the arguments we just evaluated so they match up with the LLVM
3273 // IR function.
3274 std::reverse(Args.begin() + CallArgsStart, Args.end());
Reid Kleckner739756c2013-12-04 19:23:12 +00003275 }
3276}
3277
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003278namespace {
3279
David Blaikie7e70d682015-08-18 22:40:54 +00003280struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
John McCall7f416cc2015-09-08 08:05:57 +00003281 DestroyUnpassedArg(Address Addr, QualType Ty)
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003282 : Addr(Addr), Ty(Ty) {}
3283
John McCall7f416cc2015-09-08 08:05:57 +00003284 Address Addr;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003285 QualType Ty;
3286
Craig Topper4f12f102014-03-12 06:41:41 +00003287 void Emit(CodeGenFunction &CGF, Flags flags) override {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003288 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
3289 assert(!Dtor->isTrivial());
3290 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
3291 /*Delegating=*/false, Addr);
3292 }
3293};
3294
David Blaikie38b25912015-02-09 19:13:51 +00003295struct DisableDebugLocationUpdates {
3296 CodeGenFunction &CGF;
3297 bool disabledDebugInfo;
3298 DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {
3299 if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))
3300 CGF.disableDebugInfo();
3301 }
3302 ~DisableDebugLocationUpdates() {
3303 if (disabledDebugInfo)
3304 CGF.enableDebugInfo();
3305 }
3306};
3307
Benjamin Kramer5b4296a2015-10-28 17:16:26 +00003308} // end anonymous namespace
3309
John McCall32ea9692011-03-11 20:59:21 +00003310void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
3311 QualType type) {
David Blaikie38b25912015-02-09 19:13:51 +00003312 DisableDebugLocationUpdates Dis(*this, E);
John McCall31168b02011-06-15 23:02:42 +00003313 if (const ObjCIndirectCopyRestoreExpr *CRE
3314 = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
Richard Smith9c6890a2012-11-01 22:30:59 +00003315 assert(getLangOpts().ObjCAutoRefCount);
Vedant Kumar30914f32016-10-03 15:29:22 +00003316 assert(getContext().hasSameUnqualifiedType(E->getType(), type));
John McCall31168b02011-06-15 23:02:42 +00003317 return emitWritebackArg(*this, args, CRE);
3318 }
3319
John McCall0a76c0c2011-08-26 18:42:59 +00003320 assert(type->isReferenceType() == E->isGLValue() &&
3321 "reference binding to unmaterialized r-value!");
3322
John McCall17054bd62011-08-26 21:08:13 +00003323 if (E->isGLValue()) {
3324 assert(E->getObjectKind() == OK_Ordinary);
Richard Smitha1c9d4d2013-06-12 23:38:09 +00003325 return args.add(EmitReferenceBindingToExpr(E), type);
John McCall17054bd62011-08-26 21:08:13 +00003326 }
Mike Stump11289f42009-09-09 15:08:12 +00003327
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003328 bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
3329
3330 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
3331 // However, we still have to push an EH-only cleanup in case we unwind before
3332 // we make it to the call.
Reid Klecknerac640602014-05-01 03:07:18 +00003333 if (HasAggregateEvalKind &&
3334 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3335 // If we're using inalloca, use the argument memory. Otherwise, use a
Reid Klecknere39ee212014-05-03 00:33:28 +00003336 // temporary.
Reid Klecknerac640602014-05-01 03:07:18 +00003337 AggValueSlot Slot;
3338 if (args.isUsingInAlloca())
3339 Slot = createPlaceholderSlot(*this, type);
3340 else
3341 Slot = CreateAggTemp(type, "agg.tmp");
Reid Klecknere39ee212014-05-03 00:33:28 +00003342
3343 const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
3344 bool DestroyedInCallee =
3345 RD && RD->hasNonTrivialDestructor() &&
3346 CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default;
3347 if (DestroyedInCallee)
3348 Slot.setExternallyDestructed();
3349
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003350 EmitAggExpr(E, Slot);
3351 RValue RV = Slot.asRValue();
3352 args.add(RV, type);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003353
Reid Klecknere39ee212014-05-03 00:33:28 +00003354 if (DestroyedInCallee) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003355 // Create a no-op GEP between the placeholder and the cleanup so we can
3356 // RAUW it successfully. It also serves as a marker of the first
3357 // instruction where the cleanup is active.
John McCall7f416cc2015-09-08 08:05:57 +00003358 pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(),
3359 type);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003360 // This unreachable is a temporary marker which will be removed later.
3361 llvm::Instruction *IsActive = Builder.CreateUnreachable();
3362 args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive);
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003363 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003364 return;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003365 }
3366
3367 if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
Eli Friedmandf968192011-05-26 00:10:27 +00003368 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
3369 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
3370 assert(L.isSimple());
Eli Friedman61f615a2013-06-11 01:08:22 +00003371 if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) {
3372 args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
3373 } else {
3374 // We can't represent a misaligned lvalue in the CallArgList, so copy
3375 // to an aligned temporary now.
John McCall7f416cc2015-09-08 08:05:57 +00003376 Address tmp = CreateMemTemp(type);
3377 EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile());
Eli Friedman61f615a2013-06-11 01:08:22 +00003378 args.add(RValue::getAggregate(tmp), type);
3379 }
Eli Friedmandf968192011-05-26 00:10:27 +00003380 return;
3381 }
3382
John McCall32ea9692011-03-11 20:59:21 +00003383 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00003384}
3385
Reid Kleckner79b0fd72014-10-10 00:05:45 +00003386QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
3387 // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
3388 // implicitly widens null pointer constants that are arguments to varargs
3389 // functions to pointer-sized ints.
3390 if (!getTarget().getTriple().isOSWindows())
3391 return Arg->getType();
3392
3393 if (Arg->getType()->isIntegerType() &&
3394 getContext().getTypeSize(Arg->getType()) <
3395 getContext().getTargetInfo().getPointerWidth(0) &&
3396 Arg->isNullPointerConstant(getContext(),
3397 Expr::NPC_ValueDependentIsNotNull)) {
3398 return getContext().getIntPtrType();
3399 }
3400
3401 return Arg->getType();
3402}
3403
Dan Gohman515a60d2012-02-16 00:57:37 +00003404// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
3405// optimizer it can aggressively ignore unwind edges.
3406void
3407CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
3408 if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
3409 !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
3410 Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
3411 CGM.getNoObjCARCExceptionsMetadata());
3412}
3413
John McCall882987f2013-02-28 19:01:20 +00003414/// Emits a call to the given no-arguments nounwind runtime function.
3415llvm::CallInst *
3416CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
3417 const llvm::Twine &name) {
Craig Topper5fc8fc22014-08-27 06:28:36 +00003418 return EmitNounwindRuntimeCall(callee, None, name);
John McCall882987f2013-02-28 19:01:20 +00003419}
3420
3421/// Emits a call to the given nounwind runtime function.
3422llvm::CallInst *
3423CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
3424 ArrayRef<llvm::Value*> args,
3425 const llvm::Twine &name) {
3426 llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
3427 call->setDoesNotThrow();
3428 return call;
3429}
3430
3431/// Emits a simple call (never an invoke) to the given no-arguments
3432/// runtime function.
3433llvm::CallInst *
3434CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
3435 const llvm::Twine &name) {
Craig Topper5fc8fc22014-08-27 06:28:36 +00003436 return EmitRuntimeCall(callee, None, name);
John McCall882987f2013-02-28 19:01:20 +00003437}
3438
David Majnemer0b17d442015-12-15 21:27:59 +00003439// Calls which may throw must have operand bundles indicating which funclet
3440// they are nested within.
3441static void
Sanjay Patel846b63b2016-01-18 22:15:33 +00003442getBundlesForFunclet(llvm::Value *Callee, llvm::Instruction *CurrentFuncletPad,
David Majnemer0b17d442015-12-15 21:27:59 +00003443 SmallVectorImpl<llvm::OperandBundleDef> &BundleList) {
Sanjay Patel846b63b2016-01-18 22:15:33 +00003444 // There is no need for a funclet operand bundle if we aren't inside a
3445 // funclet.
David Majnemer0b17d442015-12-15 21:27:59 +00003446 if (!CurrentFuncletPad)
3447 return;
3448
3449 // Skip intrinsics which cannot throw.
3450 auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts());
3451 if (CalleeFn && CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow())
3452 return;
3453
3454 BundleList.emplace_back("funclet", CurrentFuncletPad);
3455}
3456
David Majnemer971d31b2016-02-24 17:02:45 +00003457/// Emits a simple call (never an invoke) to the given runtime function.
3458llvm::CallInst *
3459CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
3460 ArrayRef<llvm::Value*> args,
3461 const llvm::Twine &name) {
3462 SmallVector<llvm::OperandBundleDef, 1> BundleList;
3463 getBundlesForFunclet(callee, CurrentFuncletPad, BundleList);
3464
3465 llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList, name);
3466 call->setCallingConv(getRuntimeCC());
3467 return call;
3468}
3469
John McCall882987f2013-02-28 19:01:20 +00003470/// Emits a call or invoke to the given noreturn runtime function.
3471void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
3472 ArrayRef<llvm::Value*> args) {
David Majnemer0b17d442015-12-15 21:27:59 +00003473 SmallVector<llvm::OperandBundleDef, 1> BundleList;
3474 getBundlesForFunclet(callee, CurrentFuncletPad, BundleList);
3475
John McCall882987f2013-02-28 19:01:20 +00003476 if (getInvokeDest()) {
3477 llvm::InvokeInst *invoke =
3478 Builder.CreateInvoke(callee,
3479 getUnreachableBlock(),
3480 getInvokeDest(),
David Majnemer0b17d442015-12-15 21:27:59 +00003481 args,
3482 BundleList);
John McCall882987f2013-02-28 19:01:20 +00003483 invoke->setDoesNotReturn();
3484 invoke->setCallingConv(getRuntimeCC());
3485 } else {
David Majnemer0b17d442015-12-15 21:27:59 +00003486 llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
John McCall882987f2013-02-28 19:01:20 +00003487 call->setDoesNotReturn();
3488 call->setCallingConv(getRuntimeCC());
3489 Builder.CreateUnreachable();
3490 }
3491}
3492
Sanjay Patel846b63b2016-01-18 22:15:33 +00003493/// Emits a call or invoke instruction to the given nullary runtime function.
John McCall882987f2013-02-28 19:01:20 +00003494llvm::CallSite
3495CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
3496 const Twine &name) {
Craig Topper5fc8fc22014-08-27 06:28:36 +00003497 return EmitRuntimeCallOrInvoke(callee, None, name);
John McCall882987f2013-02-28 19:01:20 +00003498}
3499
3500/// Emits a call or invoke instruction to the given runtime function.
3501llvm::CallSite
3502CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
3503 ArrayRef<llvm::Value*> args,
3504 const Twine &name) {
3505 llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
3506 callSite.setCallingConv(getRuntimeCC());
3507 return callSite;
3508}
3509
John McCallbd309292010-07-06 01:34:17 +00003510/// Emits a call or invoke instruction to the given function, depending
3511/// on the current state of the EH stack.
3512llvm::CallSite
3513CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner54b16772011-07-23 17:14:25 +00003514 ArrayRef<llvm::Value *> Args,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003515 const Twine &Name) {
John McCallbd309292010-07-06 01:34:17 +00003516 llvm::BasicBlock *InvokeDest = getInvokeDest();
David Majnemer3df77bc2016-01-26 23:14:47 +00003517 SmallVector<llvm::OperandBundleDef, 1> BundleList;
3518 getBundlesForFunclet(Callee, CurrentFuncletPad, BundleList);
John McCallbd309292010-07-06 01:34:17 +00003519
Dan Gohman515a60d2012-02-16 00:57:37 +00003520 llvm::Instruction *Inst;
3521 if (!InvokeDest)
David Majnemer3df77bc2016-01-26 23:14:47 +00003522 Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
Dan Gohman515a60d2012-02-16 00:57:37 +00003523 else {
3524 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
David Majnemer3df77bc2016-01-26 23:14:47 +00003525 Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
3526 Name);
Dan Gohman515a60d2012-02-16 00:57:37 +00003527 EmitBlock(ContBB);
3528 }
3529
3530 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
3531 // optimizer it can aggressively ignore unwind edges.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003532 if (CGM.getLangOpts().ObjCAutoRefCount)
Dan Gohman515a60d2012-02-16 00:57:37 +00003533 AddObjCARCExceptionMetadata(Inst);
3534
Benjamin Kramerc19cde12015-04-10 14:49:31 +00003535 return llvm::CallSite(Inst);
John McCallbd309292010-07-06 01:34:17 +00003536}
3537
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003538/// \brief Store a non-aggregate value to an address to initialize it. For
3539/// initialization, a non-atomic store will be used.
3540static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src,
3541 LValue Dst) {
3542 if (Src.isScalar())
3543 CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true);
3544 else
3545 CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true);
3546}
3547
3548void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
3549 llvm::Value *New) {
3550 DeferredReplacements.push_back(std::make_pair(Old, New));
3551}
Chris Lattnerd59d8672011-07-12 06:29:11 +00003552
Daniel Dunbard931a872009-02-02 22:03:45 +00003553RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
John McCallb92ab1a2016-10-26 23:46:34 +00003554 const CGCallee &Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00003555 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00003556 const CallArgList &CallArgs,
David Chisnallff5f88c2010-05-02 13:41:58 +00003557 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00003558 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00003559
John McCallb92ab1a2016-10-26 23:46:34 +00003560 assert(Callee.isOrdinary());
3561
Daniel Dunbar613855c2008-09-09 23:27:19 +00003562 // Handle struct-return functions by passing a pointer to the
3563 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00003564 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00003565 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00003566
John McCallb92ab1a2016-10-26 23:46:34 +00003567 llvm::FunctionType *IRFuncTy = Callee.getFunctionType();
3568
3569 // 1. Set up the arguments.
Mike Stump11289f42009-09-09 15:08:12 +00003570
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003571 // If we're using inalloca, insert the allocation after the stack save.
3572 // FIXME: Do this earlier rather than hacking it in here!
John McCall7f416cc2015-09-08 08:05:57 +00003573 Address ArgMemory = Address::invalid();
3574 const llvm::StructLayout *ArgMemoryLayout = nullptr;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003575 if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
John McCall7f416cc2015-09-08 08:05:57 +00003576 ArgMemoryLayout = CGM.getDataLayout().getStructLayout(ArgStruct);
Reid Kleckner9df1d972014-04-10 01:40:15 +00003577 llvm::Instruction *IP = CallArgs.getStackBase();
3578 llvm::AllocaInst *AI;
3579 if (IP) {
3580 IP = IP->getNextNode();
3581 AI = new llvm::AllocaInst(ArgStruct, "argmem", IP);
3582 } else {
Reid Kleckner966abe72014-05-15 23:01:46 +00003583 AI = CreateTempAlloca(ArgStruct, "argmem");
Reid Kleckner9df1d972014-04-10 01:40:15 +00003584 }
John McCall7f416cc2015-09-08 08:05:57 +00003585 auto Align = CallInfo.getArgStructAlignment();
3586 AI->setAlignment(Align.getQuantity());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003587 AI->setUsedWithInAlloca(true);
3588 assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
John McCall7f416cc2015-09-08 08:05:57 +00003589 ArgMemory = Address(AI, Align);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003590 }
3591
John McCall7f416cc2015-09-08 08:05:57 +00003592 // Helper function to drill into the inalloca allocation.
3593 auto createInAllocaStructGEP = [&](unsigned FieldIndex) -> Address {
3594 auto FieldOffset =
3595 CharUnits::fromQuantity(ArgMemoryLayout->getElementOffset(FieldIndex));
3596 return Builder.CreateStructGEP(ArgMemory, FieldIndex, FieldOffset);
3597 };
3598
Alexey Samsonov153004f2014-09-29 22:08:00 +00003599 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003600 SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
3601
Chris Lattner4ca97c32009-06-13 00:26:38 +00003602 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00003603 // alloca to hold the result, unless one is given to us.
John McCall7f416cc2015-09-08 08:05:57 +00003604 Address SRetPtr = Address::invalid();
Leny Kholodov6aab1112015-06-08 10:23:49 +00003605 size_t UnusedReturnSize = 0;
John McCallf26e73d2016-03-11 04:30:43 +00003606 if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
John McCall7f416cc2015-09-08 08:05:57 +00003607 if (!ReturnValue.isNull()) {
3608 SRetPtr = ReturnValue.getValue();
3609 } else {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003610 SRetPtr = CreateMemTemp(RetTy);
Leny Kholodov6aab1112015-06-08 10:23:49 +00003611 if (HaveInsertPoint() && ReturnValue.isUnused()) {
3612 uint64_t size =
3613 CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
John McCall7f416cc2015-09-08 08:05:57 +00003614 if (EmitLifetimeStart(size, SRetPtr.getPointer()))
Leny Kholodov6aab1112015-06-08 10:23:49 +00003615 UnusedReturnSize = size;
3616 }
3617 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003618 if (IRFunctionArgs.hasSRetArg()) {
John McCall7f416cc2015-09-08 08:05:57 +00003619 IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
John McCallf26e73d2016-03-11 04:30:43 +00003620 } else if (RetAI.isInAlloca()) {
John McCall7f416cc2015-09-08 08:05:57 +00003621 Address Addr = createInAllocaStructGEP(RetAI.getInAllocaFieldIndex());
3622 Builder.CreateStore(SRetPtr.getPointer(), Addr);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003623 }
Anders Carlsson17490832009-12-24 20:40:36 +00003624 }
Mike Stump11289f42009-09-09 15:08:12 +00003625
John McCall12f23522016-04-04 18:33:08 +00003626 Address swiftErrorTemp = Address::invalid();
3627 Address swiftErrorArg = Address::invalid();
3628
John McCallb92ab1a2016-10-26 23:46:34 +00003629 // Translate all of the arguments as necessary to match the IR lowering.
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00003630 assert(CallInfo.arg_size() == CallArgs.size() &&
3631 "Mismatch between function signature & arguments.");
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003632 unsigned ArgNo = 0;
Daniel Dunbarb52d0772009-02-03 05:59:18 +00003633 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00003634 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003635 I != E; ++I, ++info_it, ++ArgNo) {
Daniel Dunbarb52d0772009-02-03 05:59:18 +00003636 const ABIArgInfo &ArgInfo = info_it->info;
Eli Friedmanf4258eb2011-05-02 18:05:27 +00003637 RValue RV = I->RV;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00003638
Rafael Espindolafad28de2012-10-24 01:59:00 +00003639 // Insert a padding argument to ensure proper alignment.
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003640 if (IRFunctionArgs.hasPaddingArg(ArgNo))
3641 IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
3642 llvm::UndefValue::get(ArgInfo.getPaddingType());
3643
3644 unsigned FirstIRArg, NumIRArgs;
3645 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
Rafael Espindolafad28de2012-10-24 01:59:00 +00003646
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00003647 switch (ArgInfo.getKind()) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003648 case ABIArgInfo::InAlloca: {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003649 assert(NumIRArgs == 0);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003650 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3651 if (RV.isAggregate()) {
3652 // Replace the placeholder with the appropriate argument slot GEP.
3653 llvm::Instruction *Placeholder =
John McCall7f416cc2015-09-08 08:05:57 +00003654 cast<llvm::Instruction>(RV.getAggregatePointer());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003655 CGBuilderTy::InsertPoint IP = Builder.saveIP();
3656 Builder.SetInsertPoint(Placeholder);
John McCall7f416cc2015-09-08 08:05:57 +00003657 Address Addr = createInAllocaStructGEP(ArgInfo.getInAllocaFieldIndex());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003658 Builder.restoreIP(IP);
John McCall7f416cc2015-09-08 08:05:57 +00003659 deferPlaceholderReplacement(Placeholder, Addr.getPointer());
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003660 } else {
3661 // Store the RValue into the argument struct.
John McCall7f416cc2015-09-08 08:05:57 +00003662 Address Addr = createInAllocaStructGEP(ArgInfo.getInAllocaFieldIndex());
3663 unsigned AS = Addr.getType()->getPointerAddressSpace();
David Majnemer32b57b02014-03-31 16:12:47 +00003664 llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS);
3665 // There are some cases where a trivial bitcast is not avoidable. The
3666 // definition of a type later in a translation unit may change it's type
3667 // from {}* to (%struct.foo*)*.
John McCall7f416cc2015-09-08 08:05:57 +00003668 if (Addr.getType() != MemType)
David Majnemer32b57b02014-03-31 16:12:47 +00003669 Addr = Builder.CreateBitCast(Addr, MemType);
John McCall7f416cc2015-09-08 08:05:57 +00003670 LValue argLV = MakeAddrLValue(Addr, I->Ty);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003671 EmitInitStoreOfNonAggregate(*this, RV, argLV);
3672 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003673 break;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003674 }
3675
Daniel Dunbar03816342010-08-21 02:24:36 +00003676 case ABIArgInfo::Indirect: {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003677 assert(NumIRArgs == 1);
Daniel Dunbar747865a2009-02-05 09:16:39 +00003678 if (RV.isScalar() || RV.isComplex()) {
3679 // Make a temporary alloca to pass the argument.
John McCall7f416cc2015-09-08 08:05:57 +00003680 Address Addr = CreateMemTemp(I->Ty, ArgInfo.getIndirectAlign());
3681 IRCallArgs[FirstIRArg] = Addr.getPointer();
John McCall47fb9502013-03-07 21:37:08 +00003682
John McCall7f416cc2015-09-08 08:05:57 +00003683 LValue argLV = MakeAddrLValue(Addr, I->Ty);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003684 EmitInitStoreOfNonAggregate(*this, RV, argLV);
Daniel Dunbar747865a2009-02-05 09:16:39 +00003685 } else {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00003686 // We want to avoid creating an unnecessary temporary+copy here;
Guy Benyei3832bfd2013-03-10 12:59:00 +00003687 // however, we need one in three cases:
Eli Friedmaneb7fab62011-06-14 01:37:52 +00003688 // 1. If the argument is not byval, and we are required to copy the
3689 // source. (This case doesn't occur on any common architecture.)
3690 // 2. If the argument is byval, RV is not sufficiently aligned, and
3691 // we cannot force it to be sufficiently aligned.
Guy Benyei3832bfd2013-03-10 12:59:00 +00003692 // 3. If the argument is byval, but RV is located in an address space
3693 // different than that of the argument (0).
John McCall7f416cc2015-09-08 08:05:57 +00003694 Address Addr = RV.getAggregateAddress();
3695 CharUnits Align = ArgInfo.getIndirectAlign();
Micah Villmowdd31ca12012-10-08 16:25:52 +00003696 const llvm::DataLayout *TD = &CGM.getDataLayout();
John McCall7f416cc2015-09-08 08:05:57 +00003697 const unsigned RVAddrSpace = Addr.getType()->getAddressSpace();
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003698 const unsigned ArgAddrSpace =
3699 (FirstIRArg < IRFuncTy->getNumParams()
3700 ? IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace()
3701 : 0);
Eli Friedmanf7456192011-06-15 22:09:18 +00003702 if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
John McCall7f416cc2015-09-08 08:05:57 +00003703 (ArgInfo.getIndirectByVal() && Addr.getAlignment() < Align &&
3704 llvm::getOrEnforceKnownAlignment(Addr.getPointer(),
3705 Align.getQuantity(), *TD)
3706 < Align.getQuantity()) ||
Mehdi Aminib3d52092015-03-10 02:36:43 +00003707 (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00003708 // Create an aligned temporary, and copy to it.
John McCall7f416cc2015-09-08 08:05:57 +00003709 Address AI = CreateMemTemp(I->Ty, ArgInfo.getIndirectAlign());
3710 IRCallArgs[FirstIRArg] = AI.getPointer();
Chad Rosier615ed1a2012-03-29 17:37:10 +00003711 EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
Eli Friedmaneb7fab62011-06-14 01:37:52 +00003712 } else {
3713 // Skip the extra memcpy call.
John McCall7f416cc2015-09-08 08:05:57 +00003714 IRCallArgs[FirstIRArg] = Addr.getPointer();
Eli Friedmaneb7fab62011-06-14 01:37:52 +00003715 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00003716 }
3717 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00003718 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00003719
Daniel Dunbar94a6f252009-01-26 21:26:08 +00003720 case ABIArgInfo::Ignore:
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003721 assert(NumIRArgs == 0);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00003722 break;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003723
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003724 case ABIArgInfo::Extend:
3725 case ABIArgInfo::Direct: {
3726 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003727 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
3728 ArgInfo.getDirectOffset() == 0) {
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003729 assert(NumIRArgs == 1);
Chris Lattnerbb1952c2011-07-12 04:46:18 +00003730 llvm::Value *V;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003731 if (RV.isScalar())
Chris Lattnerbb1952c2011-07-12 04:46:18 +00003732 V = RV.getScalarVal();
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003733 else
John McCall7f416cc2015-09-08 08:05:57 +00003734 V = Builder.CreateLoad(RV.getAggregateAddress());
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003735
John McCall12f23522016-04-04 18:33:08 +00003736 // Implement swifterror by copying into a new swifterror argument.
3737 // We'll write back in the normal path out of the call.
3738 if (CallInfo.getExtParameterInfo(ArgNo).getABI()
3739 == ParameterABI::SwiftErrorResult) {
3740 assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
3741
3742 QualType pointeeTy = I->Ty->getPointeeType();
3743 swiftErrorArg =
3744 Address(V, getContext().getTypeAlignInChars(pointeeTy));
3745
3746 swiftErrorTemp =
3747 CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
3748 V = swiftErrorTemp.getPointer();
3749 cast<llvm::AllocaInst>(V)->setSwiftError(true);
3750
3751 llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
3752 Builder.CreateStore(errorValue, swiftErrorTemp);
3753 }
3754
Reid Kleckner79b0fd72014-10-10 00:05:45 +00003755 // We might have to widen integers, but we should never truncate.
3756 if (ArgInfo.getCoerceToType() != V->getType() &&
3757 V->getType()->isIntegerTy())
3758 V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
3759
Chris Lattner3ce86682011-07-12 04:53:39 +00003760 // If the argument doesn't match, perform a bitcast to coerce it. This
3761 // can happen due to trivial type mismatches.
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003762 if (FirstIRArg < IRFuncTy->getNumParams() &&
3763 V->getType() != IRFuncTy->getParamType(FirstIRArg))
3764 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
John McCall12f23522016-04-04 18:33:08 +00003765
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003766 IRCallArgs[FirstIRArg] = V;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00003767 break;
3768 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00003769
Daniel Dunbar2f219b02009-02-03 19:12:28 +00003770 // FIXME: Avoid the conversion through memory if possible.
John McCall7f416cc2015-09-08 08:05:57 +00003771 Address Src = Address::invalid();
John McCall47fb9502013-03-07 21:37:08 +00003772 if (RV.isScalar() || RV.isComplex()) {
John McCall7f416cc2015-09-08 08:05:57 +00003773 Src = CreateMemTemp(I->Ty, "coerce");
3774 LValue SrcLV = MakeAddrLValue(Src, I->Ty);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003775 EmitInitStoreOfNonAggregate(*this, RV, SrcLV);
Ulrich Weigand6e2cea62015-07-10 11:31:43 +00003776 } else {
John McCall7f416cc2015-09-08 08:05:57 +00003777 Src = RV.getAggregateAddress();
Ulrich Weigand6e2cea62015-07-10 11:31:43 +00003778 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003779
Chris Lattner8a2f3c72010-07-30 04:02:24 +00003780 // If the value is offset in memory, apply the offset now.
John McCall7f416cc2015-09-08 08:05:57 +00003781 Src = emitAddressAtOffset(*this, Src, ArgInfo);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003782
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00003783 // Fast-isel and the optimizer generally like scalar values better than
3784 // FCAs, so we flatten them if this is safe to do for this argument.
James Molloy6f244b62014-05-09 16:21:39 +00003785 llvm::StructType *STy =
3786 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
Oliver Stannard2bfdc5b2014-08-27 10:43:15 +00003787 if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
John McCall7f416cc2015-09-08 08:05:57 +00003788 llvm::Type *SrcTy = Src.getType()->getElementType();
Chandler Carrutha6399a52012-10-10 11:29:08 +00003789 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
3790 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
3791
3792 // If the source type is smaller than the destination type of the
3793 // coerce-to logic, copy the source value into a temp alloca the size
3794 // of the destination type to allow loading all of it. The bits past
3795 // the source value are left undef.
3796 if (SrcSize < DstSize) {
John McCall7f416cc2015-09-08 08:05:57 +00003797 Address TempAlloca
3798 = CreateTempAlloca(STy, Src.getAlignment(),
3799 Src.getName() + ".coerce");
3800 Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
3801 Src = TempAlloca;
Chandler Carrutha6399a52012-10-10 11:29:08 +00003802 } else {
John McCall7f416cc2015-09-08 08:05:57 +00003803 Src = Builder.CreateBitCast(Src, llvm::PointerType::getUnqual(STy));
Chandler Carrutha6399a52012-10-10 11:29:08 +00003804 }
3805
John McCall7f416cc2015-09-08 08:05:57 +00003806 auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003807 assert(NumIRArgs == STy->getNumElements());
Chris Lattnerceddafb2010-07-05 20:41:41 +00003808 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
John McCall7f416cc2015-09-08 08:05:57 +00003809 auto Offset = CharUnits::fromQuantity(SrcLayout->getElementOffset(i));
3810 Address EltPtr = Builder.CreateStructGEP(Src, i, Offset);
3811 llvm::Value *LI = Builder.CreateLoad(EltPtr);
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003812 IRCallArgs[FirstIRArg + i] = LI;
Chris Lattner15ec3612010-06-29 00:06:42 +00003813 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00003814 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00003815 // In the simple case, just pass the coerced loaded value.
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003816 assert(NumIRArgs == 1);
3817 IRCallArgs[FirstIRArg] =
John McCall7f416cc2015-09-08 08:05:57 +00003818 CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
Chris Lattner3dd716c2010-06-28 23:44:11 +00003819 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00003820
Daniel Dunbar2f219b02009-02-03 19:12:28 +00003821 break;
3822 }
3823
John McCallf26e73d2016-03-11 04:30:43 +00003824 case ABIArgInfo::CoerceAndExpand: {
John McCallf26e73d2016-03-11 04:30:43 +00003825 auto coercionType = ArgInfo.getCoerceAndExpandType();
3826 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
3827
John McCall12f23522016-04-04 18:33:08 +00003828 llvm::Value *tempSize = nullptr;
3829 Address addr = Address::invalid();
3830 if (RV.isAggregate()) {
3831 addr = RV.getAggregateAddress();
3832 } else {
3833 assert(RV.isScalar()); // complex should always just be direct
3834
3835 llvm::Type *scalarType = RV.getScalarVal()->getType();
3836 auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
3837 auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType);
3838
3839 tempSize = llvm::ConstantInt::get(CGM.Int64Ty, scalarSize);
3840
3841 // Materialize to a temporary.
3842 addr = CreateTempAlloca(RV.getScalarVal()->getType(),
3843 CharUnits::fromQuantity(std::max(layout->getAlignment(),
3844 scalarAlign)));
3845 EmitLifetimeStart(scalarSize, addr.getPointer());
3846
3847 Builder.CreateStore(RV.getScalarVal(), addr);
3848 }
3849
John McCallf26e73d2016-03-11 04:30:43 +00003850 addr = Builder.CreateElementBitCast(addr, coercionType);
3851
3852 unsigned IRArgPos = FirstIRArg;
3853 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
3854 llvm::Type *eltType = coercionType->getElementType(i);
3855 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
3856 Address eltAddr = Builder.CreateStructGEP(addr, i, layout);
3857 llvm::Value *elt = Builder.CreateLoad(eltAddr);
3858 IRCallArgs[IRArgPos++] = elt;
3859 }
3860 assert(IRArgPos == FirstIRArg + NumIRArgs);
3861
John McCall12f23522016-04-04 18:33:08 +00003862 if (tempSize) {
3863 EmitLifetimeEnd(tempSize, addr.getPointer());
3864 }
3865
John McCallf26e73d2016-03-11 04:30:43 +00003866 break;
3867 }
3868
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00003869 case ABIArgInfo::Expand:
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003870 unsigned IRArgPos = FirstIRArg;
3871 ExpandTypeToArgs(I->Ty, RV, IRFuncTy, IRCallArgs, IRArgPos);
3872 assert(IRArgPos == FirstIRArg + NumIRArgs);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00003873 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00003874 }
3875 }
Mike Stump11289f42009-09-09 15:08:12 +00003876
John McCallb92ab1a2016-10-26 23:46:34 +00003877 llvm::Value *CalleePtr = Callee.getFunctionPointer();
3878
3879 // If we're using inalloca, set up that argument.
John McCall7f416cc2015-09-08 08:05:57 +00003880 if (ArgMemory.isValid()) {
3881 llvm::Value *Arg = ArgMemory.getPointer();
Reid Klecknerafba553e2014-07-08 02:24:27 +00003882 if (CallInfo.isVariadic()) {
3883 // When passing non-POD arguments by value to variadic functions, we will
3884 // end up with a variadic prototype and an inalloca call site. In such
3885 // cases, we can't do any parameter mismatch checks. Give up and bitcast
3886 // the callee.
John McCallb92ab1a2016-10-26 23:46:34 +00003887 unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace();
3888 auto FnTy = getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS);
3889 CalleePtr = Builder.CreateBitCast(CalleePtr, FnTy);
Reid Klecknerafba553e2014-07-08 02:24:27 +00003890 } else {
3891 llvm::Type *LastParamTy =
3892 IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
3893 if (Arg->getType() != LastParamTy) {
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003894#ifndef NDEBUG
Reid Klecknerafba553e2014-07-08 02:24:27 +00003895 // Assert that these structs have equivalent element types.
3896 llvm::StructType *FullTy = CallInfo.getArgStruct();
3897 llvm::StructType *DeclaredTy = cast<llvm::StructType>(
3898 cast<llvm::PointerType>(LastParamTy)->getElementType());
3899 assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
3900 for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(),
3901 DE = DeclaredTy->element_end(),
3902 FI = FullTy->element_begin();
3903 DI != DE; ++DI, ++FI)
3904 assert(*DI == *FI);
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003905#endif
Reid Klecknerafba553e2014-07-08 02:24:27 +00003906 Arg = Builder.CreateBitCast(Arg, LastParamTy);
3907 }
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003908 }
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003909 assert(IRFunctionArgs.hasInallocaArg());
3910 IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
Reid Kleckner314ef7b2014-02-01 00:04:45 +00003911 }
3912
John McCallb92ab1a2016-10-26 23:46:34 +00003913 // 2. Prepare the function pointer.
3914
3915 // If the callee is a bitcast of a non-variadic function to have a
3916 // variadic function pointer type, check to see if we can remove the
3917 // bitcast. This comes up with unprototyped functions.
3918 //
3919 // This makes the IR nicer, but more importantly it ensures that we
3920 // can inline the function at -O0 if it is marked always_inline.
3921 auto simplifyVariadicCallee = [](llvm::Value *Ptr) -> llvm::Value* {
3922 llvm::FunctionType *CalleeFT =
3923 cast<llvm::FunctionType>(Ptr->getType()->getPointerElementType());
3924 if (!CalleeFT->isVarArg())
3925 return Ptr;
3926
3927 llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr);
3928 if (!CE || CE->getOpcode() != llvm::Instruction::BitCast)
3929 return Ptr;
3930
3931 llvm::Function *OrigFn = dyn_cast<llvm::Function>(CE->getOperand(0));
3932 if (!OrigFn)
3933 return Ptr;
3934
3935 llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
3936
3937 // If the original type is variadic, or if any of the component types
3938 // disagree, we cannot remove the cast.
3939 if (OrigFT->isVarArg() ||
3940 OrigFT->getNumParams() != CalleeFT->getNumParams() ||
3941 OrigFT->getReturnType() != CalleeFT->getReturnType())
3942 return Ptr;
3943
3944 for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
3945 if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
3946 return Ptr;
3947
3948 return OrigFn;
3949 };
3950 CalleePtr = simplifyVariadicCallee(CalleePtr);
3951
3952 // 3. Perform the actual call.
3953
3954 // Deactivate any cleanups that we're supposed to do immediately before
3955 // the call.
Reid Kleckner23f4c4b2013-06-21 12:45:15 +00003956 if (!CallArgs.getCleanupsToDeactivate().empty())
3957 deactivateArgCleanupsBeforeCall(*this, CallArgs);
3958
John McCallb92ab1a2016-10-26 23:46:34 +00003959 // Assert that the arguments we computed match up. The IR verifier
3960 // will catch this, but this is a common enough source of problems
3961 // during IRGen changes that it's way better for debugging to catch
3962 // it ourselves here.
3963#ifndef NDEBUG
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003964 assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
3965 for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
3966 // Inalloca argument can have different type.
3967 if (IRFunctionArgs.hasInallocaArg() &&
3968 i == IRFunctionArgs.getInallocaArgNo())
3969 continue;
3970 if (i < IRFuncTy->getNumParams())
3971 assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
3972 }
John McCallb92ab1a2016-10-26 23:46:34 +00003973#endif
Alexey Samsonov91cf4552014-08-22 01:06:06 +00003974
John McCallb92ab1a2016-10-26 23:46:34 +00003975 // Compute the calling convention and attributes.
Daniel Dunbar0ef34792009-09-12 00:59:20 +00003976 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00003977 CodeGen::AttributeListType AttributeList;
John McCallb92ab1a2016-10-26 23:46:34 +00003978 CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
3979 Callee.getAbstractInfo(),
Chad Rosier7dbc9cf2016-01-06 14:35:46 +00003980 AttributeList, CallingConv,
3981 /*AttrOnCallSite=*/true);
Bill Wendling3087d022012-12-07 23:17:26 +00003982 llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(),
Bill Wendlingf4d64cb2013-02-22 00:13:35 +00003983 AttributeList);
Mike Stump11289f42009-09-09 15:08:12 +00003984
John McCallb92ab1a2016-10-26 23:46:34 +00003985 // Apply some call-site-specific attributes.
3986 // TODO: work this into building the attribute set.
3987
3988 // Apply always_inline to all calls within flatten functions.
3989 // FIXME: should this really take priority over __try, below?
3990 if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
3991 !(Callee.getAbstractInfo().getCalleeDecl() &&
3992 Callee.getAbstractInfo().getCalleeDecl()->hasAttr<NoInlineAttr>())) {
3993 Attrs =
3994 Attrs.addAttribute(getLLVMContext(),
3995 llvm::AttributeSet::FunctionIndex,
3996 llvm::Attribute::AlwaysInline);
3997 }
3998
3999 // Disable inlining inside SEH __try blocks.
4000 if (isSEHTryScope()) {
4001 Attrs =
4002 Attrs.addAttribute(getLLVMContext(), llvm::AttributeSet::FunctionIndex,
4003 llvm::Attribute::NoInline);
4004 }
4005
4006 // Decide whether to use a call or an invoke.
David Majnemer4e52d6f2015-12-12 05:39:21 +00004007 bool CannotThrow;
4008 if (currentFunctionUsesSEHTry()) {
John McCallb92ab1a2016-10-26 23:46:34 +00004009 // SEH cares about asynchronous exceptions, so everything can "throw."
David Majnemer4e52d6f2015-12-12 05:39:21 +00004010 CannotThrow = false;
4011 } else if (isCleanupPadScope() &&
4012 EHPersonality::get(*this).isMSVCXXPersonality()) {
4013 // The MSVC++ personality will implicitly terminate the program if an
John McCallb92ab1a2016-10-26 23:46:34 +00004014 // exception is thrown during a cleanup outside of a try/catch.
4015 // We don't need to model anything in IR to get this behavior.
David Majnemer4e52d6f2015-12-12 05:39:21 +00004016 CannotThrow = true;
4017 } else {
John McCallb92ab1a2016-10-26 23:46:34 +00004018 // Otherwise, nounwind call sites will never throw.
David Majnemer4e52d6f2015-12-12 05:39:21 +00004019 CannotThrow = Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex,
4020 llvm::Attribute::NoUnwind);
4021 }
4022 llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
John McCallbd309292010-07-06 01:34:17 +00004023
David Majnemer0b17d442015-12-15 21:27:59 +00004024 SmallVector<llvm::OperandBundleDef, 1> BundleList;
John McCallb92ab1a2016-10-26 23:46:34 +00004025 getBundlesForFunclet(CalleePtr, CurrentFuncletPad, BundleList);
David Majnemer0b17d442015-12-15 21:27:59 +00004026
John McCallb92ab1a2016-10-26 23:46:34 +00004027 // Emit the actual call/invoke instruction.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004028 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00004029 if (!InvokeDest) {
John McCallb92ab1a2016-10-26 23:46:34 +00004030 CS = Builder.CreateCall(CalleePtr, IRCallArgs, BundleList);
Daniel Dunbar12347492009-02-23 17:26:39 +00004031 } else {
4032 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
John McCallb92ab1a2016-10-26 23:46:34 +00004033 CS = Builder.CreateInvoke(CalleePtr, Cont, InvokeDest, IRCallArgs,
David Majnemer0b17d442015-12-15 21:27:59 +00004034 BundleList);
Daniel Dunbar12347492009-02-23 17:26:39 +00004035 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00004036 }
John McCallb92ab1a2016-10-26 23:46:34 +00004037 llvm::Instruction *CI = CS.getInstruction();
Chris Lattnere70a0072010-06-29 16:40:28 +00004038 if (callOrInvoke)
John McCallb92ab1a2016-10-26 23:46:34 +00004039 *callOrInvoke = CI;
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00004040
John McCallb92ab1a2016-10-26 23:46:34 +00004041 // Apply the attributes and calling convention.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004042 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00004043 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004044
John McCallb92ab1a2016-10-26 23:46:34 +00004045 // Apply various metadata.
4046
4047 if (!CI->getType()->isVoidTy())
4048 CI->setName("call");
4049
Adam Nemet1e217bc2016-03-28 22:18:53 +00004050 // Insert instrumentation or attach profile metadata at indirect call sites.
4051 // For more details, see the comment before the definition of
4052 // IPVK_IndirectCallTarget in InstrProfData.inc.
Betul Buyukkurt518276a2016-01-23 22:50:44 +00004053 if (!CS.getCalledFunction())
4054 PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,
John McCallb92ab1a2016-10-26 23:46:34 +00004055 CI, CalleePtr);
Betul Buyukkurt518276a2016-01-23 22:50:44 +00004056
Dan Gohman515a60d2012-02-16 00:57:37 +00004057 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4058 // optimizer it can aggressively ignore unwind edges.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004059 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallb92ab1a2016-10-26 23:46:34 +00004060 AddObjCARCExceptionMetadata(CI);
4061
4062 // Suppress tail calls if requested.
4063 if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
4064 const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl();
4065 if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
4066 Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
4067 }
4068
4069 // 4. Finish the call.
Dan Gohman515a60d2012-02-16 00:57:37 +00004070
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004071 // If the call doesn't return, finish the basic block and clear the
John McCallb92ab1a2016-10-26 23:46:34 +00004072 // insertion point; this allows the rest of IRGen to discard
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004073 // unreachable code.
4074 if (CS.doesNotReturn()) {
Leny Kholodov6aab1112015-06-08 10:23:49 +00004075 if (UnusedReturnSize)
4076 EmitLifetimeEnd(llvm::ConstantInt::get(Int64Ty, UnusedReturnSize),
John McCall7f416cc2015-09-08 08:05:57 +00004077 SRetPtr.getPointer());
Leny Kholodov6aab1112015-06-08 10:23:49 +00004078
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004079 Builder.CreateUnreachable();
4080 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00004081
Mike Stump18bb9282009-05-16 07:57:57 +00004082 // FIXME: For now, emit a dummy basic block because expr emitters in
4083 // generally are not ready to handle emitting expressions at unreachable
4084 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004085 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00004086
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004087 // Return a reasonable RValue.
4088 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00004089 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00004090
John McCall12f23522016-04-04 18:33:08 +00004091 // Perform the swifterror writeback.
4092 if (swiftErrorTemp.isValid()) {
4093 llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
4094 Builder.CreateStore(errorResult, swiftErrorArg);
4095 }
4096
John McCallb92ab1a2016-10-26 23:46:34 +00004097 // Emit any call-associated writebacks immediately. Arguably this
4098 // should happen after any return-value munging.
John McCall31168b02011-06-15 23:02:42 +00004099 if (CallArgs.hasWritebacks())
4100 emitWritebacks(*this, CallArgs);
4101
Nico Weber8cdb3f92015-08-25 18:43:32 +00004102 // The stack cleanup for inalloca arguments has to run out of the normal
4103 // lexical order, so deactivate it and run it manually here.
4104 CallArgs.freeArgumentMemory(*this);
4105
John McCallb92ab1a2016-10-26 23:46:34 +00004106 // Extract the return value.
Hal Finkelee90a222014-09-26 05:04:30 +00004107 RValue Ret = [&] {
4108 switch (RetAI.getKind()) {
John McCallf26e73d2016-03-11 04:30:43 +00004109 case ABIArgInfo::CoerceAndExpand: {
4110 auto coercionType = RetAI.getCoerceAndExpandType();
4111 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
4112
4113 Address addr = SRetPtr;
4114 addr = Builder.CreateElementBitCast(addr, coercionType);
4115
John McCall12f23522016-04-04 18:33:08 +00004116 assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
4117 bool requiresExtract = isa<llvm::StructType>(CI->getType());
4118
John McCallf26e73d2016-03-11 04:30:43 +00004119 unsigned unpaddedIndex = 0;
4120 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4121 llvm::Type *eltType = coercionType->getElementType(i);
4122 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
4123 Address eltAddr = Builder.CreateStructGEP(addr, i, layout);
John McCall12f23522016-04-04 18:33:08 +00004124 llvm::Value *elt = CI;
4125 if (requiresExtract)
4126 elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
4127 else
4128 assert(unpaddedIndex == 0);
John McCallf26e73d2016-03-11 04:30:43 +00004129 Builder.CreateStore(elt, eltAddr);
4130 }
John McCall12f23522016-04-04 18:33:08 +00004131 // FALLTHROUGH
4132 }
4133
4134 case ABIArgInfo::InAlloca:
4135 case ABIArgInfo::Indirect: {
4136 RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
4137 if (UnusedReturnSize)
4138 EmitLifetimeEnd(llvm::ConstantInt::get(Int64Ty, UnusedReturnSize),
4139 SRetPtr.getPointer());
4140 return ret;
John McCallf26e73d2016-03-11 04:30:43 +00004141 }
4142
Hal Finkelee90a222014-09-26 05:04:30 +00004143 case ABIArgInfo::Ignore:
4144 // If we are ignoring an argument that had a result, make sure to
4145 // construct the appropriate return value for our caller.
4146 return GetUndefRValue(RetTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00004147
Hal Finkelee90a222014-09-26 05:04:30 +00004148 case ABIArgInfo::Extend:
4149 case ABIArgInfo::Direct: {
4150 llvm::Type *RetIRTy = ConvertType(RetTy);
4151 if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
4152 switch (getEvaluationKind(RetTy)) {
4153 case TEK_Complex: {
4154 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
4155 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
4156 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00004157 }
Hal Finkelee90a222014-09-26 05:04:30 +00004158 case TEK_Aggregate: {
John McCall7f416cc2015-09-08 08:05:57 +00004159 Address DestPtr = ReturnValue.getValue();
Hal Finkelee90a222014-09-26 05:04:30 +00004160 bool DestIsVolatile = ReturnValue.isVolatile();
4161
John McCall7f416cc2015-09-08 08:05:57 +00004162 if (!DestPtr.isValid()) {
Hal Finkelee90a222014-09-26 05:04:30 +00004163 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
4164 DestIsVolatile = false;
4165 }
John McCall7f416cc2015-09-08 08:05:57 +00004166 BuildAggStore(*this, CI, DestPtr, DestIsVolatile);
Hal Finkelee90a222014-09-26 05:04:30 +00004167 return RValue::getAggregate(DestPtr);
4168 }
4169 case TEK_Scalar: {
4170 // If the argument doesn't match, perform a bitcast to coerce it. This
4171 // can happen due to trivial type mismatches.
4172 llvm::Value *V = CI;
4173 if (V->getType() != RetIRTy)
4174 V = Builder.CreateBitCast(V, RetIRTy);
4175 return RValue::get(V);
4176 }
4177 }
4178 llvm_unreachable("bad evaluation kind");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00004179 }
Hal Finkelee90a222014-09-26 05:04:30 +00004180
John McCall7f416cc2015-09-08 08:05:57 +00004181 Address DestPtr = ReturnValue.getValue();
Hal Finkelee90a222014-09-26 05:04:30 +00004182 bool DestIsVolatile = ReturnValue.isVolatile();
4183
John McCall7f416cc2015-09-08 08:05:57 +00004184 if (!DestPtr.isValid()) {
Hal Finkelee90a222014-09-26 05:04:30 +00004185 DestPtr = CreateMemTemp(RetTy, "coerce");
4186 DestIsVolatile = false;
John McCall47fb9502013-03-07 21:37:08 +00004187 }
Hal Finkelee90a222014-09-26 05:04:30 +00004188
4189 // If the value is offset in memory, apply the offset now.
John McCall7f416cc2015-09-08 08:05:57 +00004190 Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
4191 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Hal Finkelee90a222014-09-26 05:04:30 +00004192
4193 return convertTempToRValue(DestPtr, RetTy, SourceLocation());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00004194 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00004195
Hal Finkelee90a222014-09-26 05:04:30 +00004196 case ABIArgInfo::Expand:
4197 llvm_unreachable("Invalid ABI kind for return argument");
Anders Carlsson17490832009-12-24 20:40:36 +00004198 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00004199
Hal Finkelee90a222014-09-26 05:04:30 +00004200 llvm_unreachable("Unhandled ABIArgInfo::Kind");
4201 } ();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00004202
John McCallb92ab1a2016-10-26 23:46:34 +00004203 // Emit the assume_aligned check on the return value.
4204 const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl();
Hal Finkelee90a222014-09-26 05:04:30 +00004205 if (Ret.isScalar() && TargetDecl) {
4206 if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
4207 llvm::Value *OffsetValue = nullptr;
4208 if (const auto *Offset = AA->getOffset())
4209 OffsetValue = EmitScalarExpr(Offset);
4210
4211 llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment());
4212 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment);
4213 EmitAlignmentAssumption(Ret.getScalarVal(), AlignmentCI->getZExtValue(),
4214 OffsetValue);
4215 }
Daniel Dunbar573884e2008-09-10 07:04:09 +00004216 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00004217
Hal Finkelee90a222014-09-26 05:04:30 +00004218 return Ret;
Daniel Dunbar613855c2008-09-09 23:27:19 +00004219}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00004220
4221/* VarArg handling */
4222
Charles Davisc7d5c942015-09-17 20:55:33 +00004223Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) {
4224 VAListAddr = VE->isMicrosoftABI()
4225 ? EmitMSVAListRef(VE->getSubExpr())
4226 : EmitVAListRef(VE->getSubExpr());
4227 QualType Ty = VE->getType();
4228 if (VE->isMicrosoftABI())
4229 return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
John McCall7f416cc2015-09-08 08:05:57 +00004230 return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00004231}