1 //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
2 //
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"
16 #include "ABIInfo.h"
17 #include "CGCXXABI.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "TargetInfo.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/Basic/TargetInfo.h"
25 #include "clang/CodeGen/CGFunctionInfo.h"
26 #include "clang/Frontend/CodeGenOptions.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/CallSite.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/InlineAsm.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/Transforms/Utils/Local.h"
34 using namespace clang;
35 using namespace CodeGen;
36 
37 /***/
38 
39 static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
40   switch (CC) {
41   default: return llvm::CallingConv::C;
42   case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
43   case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
44   case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
45   case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64;
46   case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
47   case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
48   case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
49   case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
50   // TODO: add support for CC_X86Pascal to llvm
51   }
52 }
53 
54 /// Derives the 'this' type for codegen purposes, i.e. ignoring method
55 /// qualification.
56 /// FIXME: address space qualification?
57 static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
58   QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
59   return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
60 }
61 
62 /// Returns the canonical formal type of the given C++ method.
63 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
64   return MD->getType()->getCanonicalTypeUnqualified()
65            .getAs<FunctionProtoType>();
66 }
67 
68 /// Returns the "extra-canonicalized" return type, which discards
69 /// qualifiers on the return type.  Codegen doesn't care about them,
70 /// and it makes ABI code a little easier to be able to assume that
71 /// all parameter and return types are top-level unqualified.
72 static CanQualType GetReturnType(QualType RetTy) {
73   return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
74 }
75 
76 /// Arrange the argument and result information for a value of the given
77 /// unprototyped freestanding function type.
78 const CGFunctionInfo &
79 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
80   // When translating an unprototyped function type, always use a
81   // variadic type.
82   return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
83                                  false, None, FTNP->getExtInfo(),
84                                  RequiredArgs(0));
85 }
86 
87 /// Arrange the LLVM function layout for a value of the given function
88 /// type, on top of any implicit parameters already stored.
89 static const CGFunctionInfo &
90 arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool IsInstanceMethod,
91                         SmallVectorImpl<CanQualType> &prefix,
92                         CanQual<FunctionProtoType> FTP) {
93   RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
94   // FIXME: Kill copy.
95   for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i)
96     prefix.push_back(FTP->getParamType(i));
97   CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
98   return CGT.arrangeLLVMFunctionInfo(resultType, IsInstanceMethod, prefix,
99                                      FTP->getExtInfo(), required);
100 }
101 
102 /// Arrange the argument and result information for a value of the
103 /// given freestanding function type.
104 const CGFunctionInfo &
105 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
106   SmallVector<CanQualType, 16> argTypes;
107   return ::arrangeLLVMFunctionInfo(*this, false, argTypes, FTP);
108 }
109 
110 static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) {
111   // Set the appropriate calling convention for the Function.
112   if (D->hasAttr<StdCallAttr>())
113     return CC_X86StdCall;
114 
115   if (D->hasAttr<FastCallAttr>())
116     return CC_X86FastCall;
117 
118   if (D->hasAttr<ThisCallAttr>())
119     return CC_X86ThisCall;
120 
121   if (D->hasAttr<PascalAttr>())
122     return CC_X86Pascal;
123 
124   if (PcsAttr *PCS = D->getAttr<PcsAttr>())
125     return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
126 
127   if (D->hasAttr<PnaclCallAttr>())
128     return CC_PnaclCall;
129 
130   if (D->hasAttr<IntelOclBiccAttr>())
131     return CC_IntelOclBicc;
132 
133   if (D->hasAttr<MSABIAttr>())
134     return IsWindows ? CC_C : CC_X86_64Win64;
135 
136   if (D->hasAttr<SysVABIAttr>())
137     return IsWindows ? CC_X86_64SysV : CC_C;
138 
139   return CC_C;
140 }
141 
142 /// Arrange the argument and result information for a call to an
143 /// unknown C++ non-static member function of the given abstract type.
144 /// (Zero value of RD means we don't have any meaningful "this" argument type,
145 ///  so fall back to a generic pointer type).
146 /// The member function must be an ordinary function, i.e. not a
147 /// constructor or destructor.
148 const CGFunctionInfo &
149 CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
150                                    const FunctionProtoType *FTP) {
151   SmallVector<CanQualType, 16> argTypes;
152 
153   // Add the 'this' pointer.
154   if (RD)
155     argTypes.push_back(GetThisType(Context, RD));
156   else
157     argTypes.push_back(Context.VoidPtrTy);
158 
159   return ::arrangeLLVMFunctionInfo(
160       *this, true, argTypes,
161       FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
162 }
163 
164 /// Arrange the argument and result information for a declaration or
165 /// definition of the given C++ non-static member function.  The
166 /// member function must be an ordinary function, i.e. not a
167 /// constructor or destructor.
168 const CGFunctionInfo &
169 CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
170   assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
171   assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
172 
173   CanQual<FunctionProtoType> prototype = GetFormalType(MD);
174 
175   if (MD->isInstance()) {
176     // The abstract case is perfectly fine.
177     const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
178     return arrangeCXXMethodType(ThisType, prototype.getTypePtr());
179   }
180 
181   return arrangeFreeFunctionType(prototype);
182 }
183 
184 const CGFunctionInfo &
185 CodeGenTypes::arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
186                                             StructorType Type) {
187 
188   SmallVector<CanQualType, 16> argTypes;
189   argTypes.push_back(GetThisType(Context, MD->getParent()));
190 
191   GlobalDecl GD;
192   if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
193     GD = GlobalDecl(CD, toCXXCtorType(Type));
194   } else {
195     auto *DD = dyn_cast<CXXDestructorDecl>(MD);
196     GD = GlobalDecl(DD, toCXXDtorType(Type));
197   }
198 
199   CanQual<FunctionProtoType> FTP = GetFormalType(MD);
200 
201   // Add the formal parameters.
202   for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i)
203     argTypes.push_back(FTP->getParamType(i));
204 
205   TheCXXABI.buildStructorSignature(MD, Type, argTypes);
206 
207   RequiredArgs required =
208       (MD->isVariadic() ? RequiredArgs(argTypes.size()) : RequiredArgs::All);
209 
210   FunctionType::ExtInfo extInfo = FTP->getExtInfo();
211   CanQualType resultType =
212       TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy;
213   return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, required);
214 }
215 
216 /// Arrange a call to a C++ method, passing the given arguments.
217 const CGFunctionInfo &
218 CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
219                                         const CXXConstructorDecl *D,
220                                         CXXCtorType CtorKind,
221                                         unsigned ExtraArgs) {
222   // FIXME: Kill copy.
223   SmallVector<CanQualType, 16> ArgTypes;
224   for (const auto &Arg : args)
225     ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
226 
227   CanQual<FunctionProtoType> FPT = GetFormalType(D);
228   RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs);
229   GlobalDecl GD(D, CtorKind);
230   CanQualType ResultType =
231       TheCXXABI.HasThisReturn(GD) ? ArgTypes.front() : Context.VoidTy;
232 
233   FunctionType::ExtInfo Info = FPT->getExtInfo();
234   return arrangeLLVMFunctionInfo(ResultType, true, ArgTypes, Info, Required);
235 }
236 
237 /// Arrange the argument and result information for the declaration or
238 /// definition of the given function.
239 const CGFunctionInfo &
240 CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
241   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
242     if (MD->isInstance())
243       return arrangeCXXMethodDeclaration(MD);
244 
245   CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
246 
247   assert(isa<FunctionType>(FTy));
248 
249   // When declaring a function without a prototype, always use a
250   // non-variadic type.
251   if (isa<FunctionNoProtoType>(FTy)) {
252     CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
253     return arrangeLLVMFunctionInfo(noProto->getReturnType(), false, None,
254                                    noProto->getExtInfo(), RequiredArgs::All);
255   }
256 
257   assert(isa<FunctionProtoType>(FTy));
258   return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>());
259 }
260 
261 /// Arrange the argument and result information for the declaration or
262 /// definition of an Objective-C method.
263 const CGFunctionInfo &
264 CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
265   // It happens that this is the same as a call with no optional
266   // arguments, except also using the formal 'self' type.
267   return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
268 }
269 
270 /// Arrange the argument and result information for the function type
271 /// through which to perform a send to the given Objective-C method,
272 /// using the given receiver type.  The receiver type is not always
273 /// the 'self' type of the method or even an Objective-C pointer type.
274 /// This is *not* the right method for actually performing such a
275 /// message send, due to the possibility of optional arguments.
276 const CGFunctionInfo &
277 CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
278                                               QualType receiverType) {
279   SmallVector<CanQualType, 16> argTys;
280   argTys.push_back(Context.getCanonicalParamType(receiverType));
281   argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
282   // FIXME: Kill copy?
283   for (const auto *I : MD->params()) {
284     argTys.push_back(Context.getCanonicalParamType(I->getType()));
285   }
286 
287   FunctionType::ExtInfo einfo;
288   bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
289   einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
290 
291   if (getContext().getLangOpts().ObjCAutoRefCount &&
292       MD->hasAttr<NSReturnsRetainedAttr>())
293     einfo = einfo.withProducesResult(true);
294 
295   RequiredArgs required =
296     (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
297 
298   return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()), false,
299                                  argTys, einfo, required);
300 }
301 
302 const CGFunctionInfo &
303 CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
304   // FIXME: Do we need to handle ObjCMethodDecl?
305   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
306 
307   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
308     return arrangeCXXStructorDeclaration(CD, getFromCtorType(GD.getCtorType()));
309 
310   if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
311     return arrangeCXXStructorDeclaration(DD, getFromDtorType(GD.getDtorType()));
312 
313   return arrangeFunctionDeclaration(FD);
314 }
315 
316 /// Arrange a thunk that takes 'this' as the first parameter followed by
317 /// varargs.  Return a void pointer, regardless of the actual return type.
318 /// The body of the thunk will end in a musttail call to a function of the
319 /// correct type, and the caller will bitcast the function to the correct
320 /// prototype.
321 const CGFunctionInfo &
322 CodeGenTypes::arrangeMSMemberPointerThunk(const CXXMethodDecl *MD) {
323   assert(MD->isVirtual() && "only virtual memptrs have thunks");
324   CanQual<FunctionProtoType> FTP = GetFormalType(MD);
325   CanQualType ArgTys[] = { GetThisType(Context, MD->getParent()) };
326   return arrangeLLVMFunctionInfo(Context.VoidTy, false, ArgTys,
327                                  FTP->getExtInfo(), RequiredArgs(1));
328 }
329 
330 /// Arrange a call as unto a free function, except possibly with an
331 /// additional number of formal parameters considered required.
332 static const CGFunctionInfo &
333 arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
334                             CodeGenModule &CGM,
335                             const CallArgList &args,
336                             const FunctionType *fnType,
337                             unsigned numExtraRequiredArgs) {
338   assert(args.size() >= numExtraRequiredArgs);
339 
340   // In most cases, there are no optional arguments.
341   RequiredArgs required = RequiredArgs::All;
342 
343   // If we have a variadic prototype, the required arguments are the
344   // extra prefix plus the arguments in the prototype.
345   if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
346     if (proto->isVariadic())
347       required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs);
348 
349   // If we don't have a prototype at all, but we're supposed to
350   // explicitly use the variadic convention for unprototyped calls,
351   // treat all of the arguments as required but preserve the nominal
352   // possibility of variadics.
353   } else if (CGM.getTargetCodeGenInfo()
354                 .isNoProtoCallVariadic(args,
355                                        cast<FunctionNoProtoType>(fnType))) {
356     required = RequiredArgs(args.size());
357   }
358 
359   return CGT.arrangeFreeFunctionCall(fnType->getReturnType(), args,
360                                      fnType->getExtInfo(), required);
361 }
362 
363 /// Figure out the rules for calling a function with the given formal
364 /// type using the given arguments.  The arguments are necessary
365 /// because the function might be unprototyped, in which case it's
366 /// target-dependent in crazy ways.
367 const CGFunctionInfo &
368 CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
369                                       const FunctionType *fnType) {
370   return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0);
371 }
372 
373 /// A block function call is essentially a free-function call with an
374 /// extra implicit argument.
375 const CGFunctionInfo &
376 CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
377                                        const FunctionType *fnType) {
378   return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1);
379 }
380 
381 const CGFunctionInfo &
382 CodeGenTypes::arrangeFreeFunctionCall(QualType resultType,
383                                       const CallArgList &args,
384                                       FunctionType::ExtInfo info,
385                                       RequiredArgs required) {
386   // FIXME: Kill copy.
387   SmallVector<CanQualType, 16> argTypes;
388   for (const auto &Arg : args)
389     argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
390   return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes,
391                                  info, required);
392 }
393 
394 /// Arrange a call to a C++ method, passing the given arguments.
395 const CGFunctionInfo &
396 CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
397                                    const FunctionProtoType *FPT,
398                                    RequiredArgs required) {
399   // FIXME: Kill copy.
400   SmallVector<CanQualType, 16> argTypes;
401   for (const auto &Arg : args)
402     argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
403 
404   FunctionType::ExtInfo info = FPT->getExtInfo();
405   return arrangeLLVMFunctionInfo(GetReturnType(FPT->getReturnType()), true,
406                                  argTypes, info, required);
407 }
408 
409 const CGFunctionInfo &CodeGenTypes::arrangeFreeFunctionDeclaration(
410     QualType resultType, const FunctionArgList &args,
411     const FunctionType::ExtInfo &info, bool isVariadic) {
412   // FIXME: Kill copy.
413   SmallVector<CanQualType, 16> argTypes;
414   for (auto Arg : args)
415     argTypes.push_back(Context.getCanonicalParamType(Arg->getType()));
416 
417   RequiredArgs required =
418     (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All);
419   return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, info,
420                                  required);
421 }
422 
423 const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
424   return arrangeLLVMFunctionInfo(getContext().VoidTy, false, None,
425                                  FunctionType::ExtInfo(), RequiredArgs::All);
426 }
427 
428 /// Arrange the argument and result information for an abstract value
429 /// of a given function type.  This is the method which all of the
430 /// above functions ultimately defer to.
431 const CGFunctionInfo &
432 CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
433                                       bool IsInstanceMethod,
434                                       ArrayRef<CanQualType> argTypes,
435                                       FunctionType::ExtInfo info,
436                                       RequiredArgs required) {
437 #ifndef NDEBUG
438   for (ArrayRef<CanQualType>::const_iterator
439          I = argTypes.begin(), E = argTypes.end(); I != E; ++I)
440     assert(I->isCanonicalAsParam());
441 #endif
442 
443   unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
444 
445   // Lookup or create unique function info.
446   llvm::FoldingSetNodeID ID;
447   CGFunctionInfo::Profile(ID, IsInstanceMethod, info, required, resultType,
448                           argTypes);
449 
450   void *insertPos = nullptr;
451   CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
452   if (FI)
453     return *FI;
454 
455   // Construct the function info.  We co-allocate the ArgInfos.
456   FI = CGFunctionInfo::create(CC, IsInstanceMethod, info, resultType, argTypes,
457                               required);
458   FunctionInfos.InsertNode(FI, insertPos);
459 
460   bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted;
461   assert(inserted && "Recursively being processed?");
462 
463   // Compute ABI information.
464   getABIInfo().computeInfo(*FI);
465 
466   // Loop over all of the computed argument and return value info.  If any of
467   // them are direct or extend without a specified coerce type, specify the
468   // default now.
469   ABIArgInfo &retInfo = FI->getReturnInfo();
470   if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
471     retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
472 
473   for (auto &I : FI->arguments())
474     if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
475       I.info.setCoerceToType(ConvertType(I.type));
476 
477   bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
478   assert(erased && "Not in set?");
479 
480   return *FI;
481 }
482 
483 CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
484                                        bool IsInstanceMethod,
485                                        const FunctionType::ExtInfo &info,
486                                        CanQualType resultType,
487                                        ArrayRef<CanQualType> argTypes,
488                                        RequiredArgs required) {
489   void *buffer = operator new(sizeof(CGFunctionInfo) +
490                               sizeof(ArgInfo) * (argTypes.size() + 1));
491   CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
492   FI->CallingConvention = llvmCC;
493   FI->EffectiveCallingConvention = llvmCC;
494   FI->ASTCallingConvention = info.getCC();
495   FI->InstanceMethod = IsInstanceMethod;
496   FI->NoReturn = info.getNoReturn();
497   FI->ReturnsRetained = info.getProducesResult();
498   FI->Required = required;
499   FI->HasRegParm = info.getHasRegParm();
500   FI->RegParm = info.getRegParm();
501   FI->ArgStruct = nullptr;
502   FI->NumArgs = argTypes.size();
503   FI->getArgsBuffer()[0].type = resultType;
504   for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
505     FI->getArgsBuffer()[i + 1].type = argTypes[i];
506   return FI;
507 }
508 
509 /***/
510 
511 namespace {
512 // ABIArgInfo::Expand implementation.
513 
514 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
515 struct TypeExpansion {
516   enum TypeExpansionKind {
517     // Elements of constant arrays are expanded recursively.
518     TEK_ConstantArray,
519     // Record fields are expanded recursively (but if record is a union, only
520     // the field with the largest size is expanded).
521     TEK_Record,
522     // For complex types, real and imaginary parts are expanded recursively.
523     TEK_Complex,
524     // All other types are not expandable.
525     TEK_None
526   };
527 
528   const TypeExpansionKind Kind;
529 
530   TypeExpansion(TypeExpansionKind K) : Kind(K) {}
531   virtual ~TypeExpansion() {}
532 };
533 
534 struct ConstantArrayExpansion : TypeExpansion {
535   QualType EltTy;
536   uint64_t NumElts;
537 
538   ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
539       : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
540   static bool classof(const TypeExpansion *TE) {
541     return TE->Kind == TEK_ConstantArray;
542   }
543 };
544 
545 struct RecordExpansion : TypeExpansion {
546   SmallVector<const FieldDecl *, 1> Fields;
547 
548   RecordExpansion(SmallVector<const FieldDecl *, 1> &&Fields)
549       : TypeExpansion(TEK_Record), Fields(Fields) {}
550   static bool classof(const TypeExpansion *TE) {
551     return TE->Kind == TEK_Record;
552   }
553 };
554 
555 struct ComplexExpansion : TypeExpansion {
556   QualType EltTy;
557 
558   ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
559   static bool classof(const TypeExpansion *TE) {
560     return TE->Kind == TEK_Complex;
561   }
562 };
563 
564 struct NoExpansion : TypeExpansion {
565   NoExpansion() : TypeExpansion(TEK_None) {}
566   static bool classof(const TypeExpansion *TE) {
567     return TE->Kind == TEK_None;
568   }
569 };
570 }  // namespace
571 
572 static std::unique_ptr<TypeExpansion>
573 getTypeExpansion(QualType Ty, const ASTContext &Context) {
574   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
575     return llvm::make_unique<ConstantArrayExpansion>(
576         AT->getElementType(), AT->getSize().getZExtValue());
577   }
578   if (const RecordType *RT = Ty->getAs<RecordType>()) {
579     SmallVector<const FieldDecl *, 1> Fields;
580     const RecordDecl *RD = RT->getDecl();
581     assert(!RD->hasFlexibleArrayMember() &&
582            "Cannot expand structure with flexible array.");
583     if (RD->isUnion()) {
584       // Unions can be here only in degenerative cases - all the fields are same
585       // after flattening. Thus we have to use the "largest" field.
586       const FieldDecl *LargestFD = nullptr;
587       CharUnits UnionSize = CharUnits::Zero();
588 
589       for (const auto *FD : RD->fields()) {
590         assert(!FD->isBitField() &&
591                "Cannot expand structure with bit-field members.");
592         CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
593         if (UnionSize < FieldSize) {
594           UnionSize = FieldSize;
595           LargestFD = FD;
596         }
597       }
598       if (LargestFD)
599         Fields.push_back(LargestFD);
600     } else {
601       for (const auto *FD : RD->fields()) {
602         assert(!FD->isBitField() &&
603                "Cannot expand structure with bit-field members.");
604         Fields.push_back(FD);
605       }
606     }
607     return llvm::make_unique<RecordExpansion>(std::move(Fields));
608   }
609   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
610     return llvm::make_unique<ComplexExpansion>(CT->getElementType());
611   }
612   return llvm::make_unique<NoExpansion>();
613 }
614 
615 static int getExpansionSize(QualType Ty, const ASTContext &Context) {
616   auto Exp = getTypeExpansion(Ty, Context);
617   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
618     return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
619   }
620   if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
621     int Res = 0;
622     for (auto FD : RExp->Fields)
623       Res += getExpansionSize(FD->getType(), Context);
624     return Res;
625   }
626   if (isa<ComplexExpansion>(Exp.get()))
627     return 2;
628   assert(isa<NoExpansion>(Exp.get()));
629   return 1;
630 }
631 
632 void
633 CodeGenTypes::getExpandedTypes(QualType Ty,
634                                SmallVectorImpl<llvm::Type *>::iterator &TI) {
635   auto Exp = getTypeExpansion(Ty, Context);
636   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
637     for (int i = 0, n = CAExp->NumElts; i < n; i++) {
638       getExpandedTypes(CAExp->EltTy, TI);
639     }
640   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
641     for (auto FD : RExp->Fields) {
642       getExpandedTypes(FD->getType(), TI);
643     }
644   } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
645     llvm::Type *EltTy = ConvertType(CExp->EltTy);
646     *TI++ = EltTy;
647     *TI++ = EltTy;
648   } else {
649     assert(isa<NoExpansion>(Exp.get()));
650     *TI++ = ConvertType(Ty);
651   }
652 }
653 
654 void CodeGenFunction::ExpandTypeFromArgs(
655     QualType Ty, LValue LV, SmallVectorImpl<llvm::Argument *>::iterator &AI) {
656   assert(LV.isSimple() &&
657          "Unexpected non-simple lvalue during struct expansion.");
658 
659   auto Exp = getTypeExpansion(Ty, getContext());
660   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
661     for (int i = 0, n = CAExp->NumElts; i < n; i++) {
662       llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, i);
663       LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
664       ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
665     }
666   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
667     for (auto FD : RExp->Fields) {
668       // FIXME: What are the right qualifiers here?
669       LValue SubLV = EmitLValueForField(LV, FD);
670       ExpandTypeFromArgs(FD->getType(), SubLV, AI);
671     }
672   } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
673     llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real");
674     EmitStoreThroughLValue(RValue::get(*AI++),
675                            MakeAddrLValue(RealAddr, CExp->EltTy));
676     llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag");
677     EmitStoreThroughLValue(RValue::get(*AI++),
678                            MakeAddrLValue(ImagAddr, CExp->EltTy));
679   } else {
680     assert(isa<NoExpansion>(Exp.get()));
681     EmitStoreThroughLValue(RValue::get(*AI++), LV);
682   }
683 }
684 
685 void CodeGenFunction::ExpandTypeToArgs(
686     QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
687     SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
688   auto Exp = getTypeExpansion(Ty, getContext());
689   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
690     llvm::Value *Addr = RV.getAggregateAddr();
691     for (int i = 0, n = CAExp->NumElts; i < n; i++) {
692       llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, i);
693       RValue EltRV =
694           convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation());
695       ExpandTypeToArgs(CAExp->EltTy, EltRV, IRFuncTy, IRCallArgs, IRCallArgPos);
696     }
697   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
698     LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty);
699     for (auto FD : RExp->Fields) {
700       RValue FldRV = EmitRValueForField(LV, FD, SourceLocation());
701       ExpandTypeToArgs(FD->getType(), FldRV, IRFuncTy, IRCallArgs,
702                        IRCallArgPos);
703     }
704   } else if (isa<ComplexExpansion>(Exp.get())) {
705     ComplexPairTy CV = RV.getComplexVal();
706     IRCallArgs[IRCallArgPos++] = CV.first;
707     IRCallArgs[IRCallArgPos++] = CV.second;
708   } else {
709     assert(isa<NoExpansion>(Exp.get()));
710     assert(RV.isScalar() &&
711            "Unexpected non-scalar rvalue during struct expansion.");
712 
713     // Insert a bitcast as needed.
714     llvm::Value *V = RV.getScalarVal();
715     if (IRCallArgPos < IRFuncTy->getNumParams() &&
716         V->getType() != IRFuncTy->getParamType(IRCallArgPos))
717       V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
718 
719     IRCallArgs[IRCallArgPos++] = V;
720   }
721 }
722 
723 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
724 /// accessing some number of bytes out of it, try to gep into the struct to get
725 /// at its inner goodness.  Dive as deep as possible without entering an element
726 /// with an in-memory size smaller than DstSize.
727 static llvm::Value *
728 EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
729                                    llvm::StructType *SrcSTy,
730                                    uint64_t DstSize, CodeGenFunction &CGF) {
731   // We can't dive into a zero-element struct.
732   if (SrcSTy->getNumElements() == 0) return SrcPtr;
733 
734   llvm::Type *FirstElt = SrcSTy->getElementType(0);
735 
736   // If the first elt is at least as large as what we're looking for, or if the
737   // first element is the same size as the whole struct, we can enter it. The
738   // comparison must be made on the store size and not the alloca size. Using
739   // the alloca size may overstate the size of the load.
740   uint64_t FirstEltSize =
741     CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
742   if (FirstEltSize < DstSize &&
743       FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
744     return SrcPtr;
745 
746   // GEP into the first element.
747   SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
748 
749   // If the first element is a struct, recurse.
750   llvm::Type *SrcTy =
751     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
752   if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
753     return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
754 
755   return SrcPtr;
756 }
757 
758 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
759 /// are either integers or pointers.  This does a truncation of the value if it
760 /// is too large or a zero extension if it is too small.
761 ///
762 /// This behaves as if the value were coerced through memory, so on big-endian
763 /// targets the high bits are preserved in a truncation, while little-endian
764 /// targets preserve the low bits.
765 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
766                                              llvm::Type *Ty,
767                                              CodeGenFunction &CGF) {
768   if (Val->getType() == Ty)
769     return Val;
770 
771   if (isa<llvm::PointerType>(Val->getType())) {
772     // If this is Pointer->Pointer avoid conversion to and from int.
773     if (isa<llvm::PointerType>(Ty))
774       return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
775 
776     // Convert the pointer to an integer so we can play with its width.
777     Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
778   }
779 
780   llvm::Type *DestIntTy = Ty;
781   if (isa<llvm::PointerType>(DestIntTy))
782     DestIntTy = CGF.IntPtrTy;
783 
784   if (Val->getType() != DestIntTy) {
785     const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
786     if (DL.isBigEndian()) {
787       // Preserve the high bits on big-endian targets.
788       // That is what memory coercion does.
789       uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
790       uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
791 
792       if (SrcSize > DstSize) {
793         Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
794         Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
795       } else {
796         Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
797         Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
798       }
799     } else {
800       // Little-endian targets preserve the low bits. No shifts required.
801       Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
802     }
803   }
804 
805   if (isa<llvm::PointerType>(Ty))
806     Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
807   return Val;
808 }
809 
810 
811 
812 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
813 /// a pointer to an object of type \arg Ty.
814 ///
815 /// This safely handles the case when the src type is smaller than the
816 /// destination type; in this situation the values of bits which not
817 /// present in the src are undefined.
818 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
819                                       llvm::Type *Ty,
820                                       CodeGenFunction &CGF) {
821   llvm::Type *SrcTy =
822     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
823 
824   // If SrcTy and Ty are the same, just do a load.
825   if (SrcTy == Ty)
826     return CGF.Builder.CreateLoad(SrcPtr);
827 
828   uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
829 
830   if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
831     SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
832     SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
833   }
834 
835   uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
836 
837   // If the source and destination are integer or pointer types, just do an
838   // extension or truncation to the desired type.
839   if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
840       (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
841     llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
842     return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
843   }
844 
845   // If load is legal, just bitcast the src pointer.
846   if (SrcSize >= DstSize) {
847     // Generally SrcSize is never greater than DstSize, since this means we are
848     // losing bits. However, this can happen in cases where the structure has
849     // additional padding, for example due to a user specified alignment.
850     //
851     // FIXME: Assert that we aren't truncating non-padding bits when have access
852     // to that information.
853     llvm::Value *Casted =
854       CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
855     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
856     // FIXME: Use better alignment / avoid requiring aligned load.
857     Load->setAlignment(1);
858     return Load;
859   }
860 
861   // Otherwise do coercion through memory. This is stupid, but
862   // simple.
863   llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
864   llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
865   llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
866   llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy);
867   // FIXME: Use better alignment.
868   CGF.Builder.CreateMemCpy(Casted, SrcCasted,
869       llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
870       1, false);
871   return CGF.Builder.CreateLoad(Tmp);
872 }
873 
874 // Function to store a first-class aggregate into memory.  We prefer to
875 // store the elements rather than the aggregate to be more friendly to
876 // fast-isel.
877 // FIXME: Do we need to recurse here?
878 static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
879                           llvm::Value *DestPtr, bool DestIsVolatile,
880                           bool LowAlignment) {
881   // Prefer scalar stores to first-class aggregate stores.
882   if (llvm::StructType *STy =
883         dyn_cast<llvm::StructType>(Val->getType())) {
884     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
885       llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
886       llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
887       llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
888                                                     DestIsVolatile);
889       if (LowAlignment)
890         SI->setAlignment(1);
891     }
892   } else {
893     llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
894     if (LowAlignment)
895       SI->setAlignment(1);
896   }
897 }
898 
899 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
900 /// where the source and destination may have different types.
901 ///
902 /// This safely handles the case when the src type is larger than the
903 /// destination type; the upper bits of the src will be lost.
904 static void CreateCoercedStore(llvm::Value *Src,
905                                llvm::Value *DstPtr,
906                                bool DstIsVolatile,
907                                CodeGenFunction &CGF) {
908   llvm::Type *SrcTy = Src->getType();
909   llvm::Type *DstTy =
910     cast<llvm::PointerType>(DstPtr->getType())->getElementType();
911   if (SrcTy == DstTy) {
912     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
913     return;
914   }
915 
916   uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
917 
918   if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
919     DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
920     DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
921   }
922 
923   // If the source and destination are integer or pointer types, just do an
924   // extension or truncation to the desired type.
925   if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
926       (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
927     Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
928     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
929     return;
930   }
931 
932   uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
933 
934   // If store is legal, just bitcast the src pointer.
935   if (SrcSize <= DstSize) {
936     llvm::Value *Casted =
937       CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
938     // FIXME: Use better alignment / avoid requiring aligned store.
939     BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
940   } else {
941     // Otherwise do coercion through memory. This is stupid, but
942     // simple.
943 
944     // Generally SrcSize is never greater than DstSize, since this means we are
945     // losing bits. However, this can happen in cases where the structure has
946     // additional padding, for example due to a user specified alignment.
947     //
948     // FIXME: Assert that we aren't truncating non-padding bits when have access
949     // to that information.
950     llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
951     CGF.Builder.CreateStore(Src, Tmp);
952     llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
953     llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
954     llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy);
955     // FIXME: Use better alignment.
956     CGF.Builder.CreateMemCpy(DstCasted, Casted,
957         llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
958         1, false);
959   }
960 }
961 
962 namespace {
963 
964 /// Encapsulates information about the way function arguments from
965 /// CGFunctionInfo should be passed to actual LLVM IR function.
966 class ClangToLLVMArgMapping {
967   static const unsigned InvalidIndex = ~0U;
968   unsigned InallocaArgNo;
969   unsigned SRetArgNo;
970   unsigned TotalIRArgs;
971 
972   /// Arguments of LLVM IR function corresponding to single Clang argument.
973   struct IRArgs {
974     unsigned PaddingArgIndex;
975     // Argument is expanded to IR arguments at positions
976     // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
977     unsigned FirstArgIndex;
978     unsigned NumberOfArgs;
979 
980     IRArgs()
981         : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
982           NumberOfArgs(0) {}
983   };
984 
985   SmallVector<IRArgs, 8> ArgInfo;
986 
987 public:
988   ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
989                         bool OnlyRequiredArgs = false)
990       : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
991         ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
992     construct(Context, FI, OnlyRequiredArgs);
993   }
994 
995   bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
996   unsigned getInallocaArgNo() const {
997     assert(hasInallocaArg());
998     return InallocaArgNo;
999   }
1000 
1001   bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
1002   unsigned getSRetArgNo() const {
1003     assert(hasSRetArg());
1004     return SRetArgNo;
1005   }
1006 
1007   unsigned totalIRArgs() const { return TotalIRArgs; }
1008 
1009   bool hasPaddingArg(unsigned ArgNo) const {
1010     assert(ArgNo < ArgInfo.size());
1011     return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
1012   }
1013   unsigned getPaddingArgNo(unsigned ArgNo) const {
1014     assert(hasPaddingArg(ArgNo));
1015     return ArgInfo[ArgNo].PaddingArgIndex;
1016   }
1017 
1018   /// Returns index of first IR argument corresponding to ArgNo, and their
1019   /// quantity.
1020   std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
1021     assert(ArgNo < ArgInfo.size());
1022     return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
1023                           ArgInfo[ArgNo].NumberOfArgs);
1024   }
1025 
1026 private:
1027   void construct(const ASTContext &Context, const CGFunctionInfo &FI,
1028                  bool OnlyRequiredArgs);
1029 };
1030 
1031 void ClangToLLVMArgMapping::construct(const ASTContext &Context,
1032                                       const CGFunctionInfo &FI,
1033                                       bool OnlyRequiredArgs) {
1034   unsigned IRArgNo = 0;
1035   bool SwapThisWithSRet = false;
1036   const ABIArgInfo &RetAI = FI.getReturnInfo();
1037 
1038   if (RetAI.getKind() == ABIArgInfo::Indirect) {
1039     SwapThisWithSRet = RetAI.isSRetAfterThis();
1040     SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
1041   }
1042 
1043   unsigned ArgNo = 0;
1044   unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
1045   for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
1046        ++I, ++ArgNo) {
1047     assert(I != FI.arg_end());
1048     QualType ArgType = I->type;
1049     const ABIArgInfo &AI = I->info;
1050     // Collect data about IR arguments corresponding to Clang argument ArgNo.
1051     auto &IRArgs = ArgInfo[ArgNo];
1052 
1053     if (AI.getPaddingType())
1054       IRArgs.PaddingArgIndex = IRArgNo++;
1055 
1056     switch (AI.getKind()) {
1057     case ABIArgInfo::Extend:
1058     case ABIArgInfo::Direct: {
1059       // FIXME: handle sseregparm someday...
1060       llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
1061       if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
1062         IRArgs.NumberOfArgs = STy->getNumElements();
1063       } else {
1064         IRArgs.NumberOfArgs = 1;
1065       }
1066       break;
1067     }
1068     case ABIArgInfo::Indirect:
1069       IRArgs.NumberOfArgs = 1;
1070       break;
1071     case ABIArgInfo::Ignore:
1072     case ABIArgInfo::InAlloca:
1073       // ignore and inalloca doesn't have matching LLVM parameters.
1074       IRArgs.NumberOfArgs = 0;
1075       break;
1076     case ABIArgInfo::Expand: {
1077       IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
1078       break;
1079     }
1080     }
1081 
1082     if (IRArgs.NumberOfArgs > 0) {
1083       IRArgs.FirstArgIndex = IRArgNo;
1084       IRArgNo += IRArgs.NumberOfArgs;
1085     }
1086 
1087     // Skip over the sret parameter when it comes second.  We already handled it
1088     // above.
1089     if (IRArgNo == 1 && SwapThisWithSRet)
1090       IRArgNo++;
1091   }
1092   assert(ArgNo == ArgInfo.size());
1093 
1094   if (FI.usesInAlloca())
1095     InallocaArgNo = IRArgNo++;
1096 
1097   TotalIRArgs = IRArgNo;
1098 }
1099 }  // namespace
1100 
1101 /***/
1102 
1103 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
1104   return FI.getReturnInfo().isIndirect();
1105 }
1106 
1107 bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
1108   return ReturnTypeUsesSRet(FI) &&
1109          getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1110 }
1111 
1112 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
1113   if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
1114     switch (BT->getKind()) {
1115     default:
1116       return false;
1117     case BuiltinType::Float:
1118       return getTarget().useObjCFPRetForRealType(TargetInfo::Float);
1119     case BuiltinType::Double:
1120       return getTarget().useObjCFPRetForRealType(TargetInfo::Double);
1121     case BuiltinType::LongDouble:
1122       return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble);
1123     }
1124   }
1125 
1126   return false;
1127 }
1128 
1129 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
1130   if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
1131     if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
1132       if (BT->getKind() == BuiltinType::LongDouble)
1133         return getTarget().useObjCFP2RetForComplexLongDouble();
1134     }
1135   }
1136 
1137   return false;
1138 }
1139 
1140 llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
1141   const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
1142   return GetFunctionType(FI);
1143 }
1144 
1145 llvm::FunctionType *
1146 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
1147 
1148   bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted;
1149   assert(Inserted && "Recursively being processed?");
1150 
1151   llvm::Type *resultType = nullptr;
1152   const ABIArgInfo &retAI = FI.getReturnInfo();
1153   switch (retAI.getKind()) {
1154   case ABIArgInfo::Expand:
1155     llvm_unreachable("Invalid ABI kind for return argument");
1156 
1157   case ABIArgInfo::Extend:
1158   case ABIArgInfo::Direct:
1159     resultType = retAI.getCoerceToType();
1160     break;
1161 
1162   case ABIArgInfo::InAlloca:
1163     if (retAI.getInAllocaSRet()) {
1164       // sret things on win32 aren't void, they return the sret pointer.
1165       QualType ret = FI.getReturnType();
1166       llvm::Type *ty = ConvertType(ret);
1167       unsigned addressSpace = Context.getTargetAddressSpace(ret);
1168       resultType = llvm::PointerType::get(ty, addressSpace);
1169     } else {
1170       resultType = llvm::Type::getVoidTy(getLLVMContext());
1171     }
1172     break;
1173 
1174   case ABIArgInfo::Indirect: {
1175     assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
1176     resultType = llvm::Type::getVoidTy(getLLVMContext());
1177     break;
1178   }
1179 
1180   case ABIArgInfo::Ignore:
1181     resultType = llvm::Type::getVoidTy(getLLVMContext());
1182     break;
1183   }
1184 
1185   ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
1186   SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
1187 
1188   // Add type for sret argument.
1189   if (IRFunctionArgs.hasSRetArg()) {
1190     QualType Ret = FI.getReturnType();
1191     llvm::Type *Ty = ConvertType(Ret);
1192     unsigned AddressSpace = Context.getTargetAddressSpace(Ret);
1193     ArgTypes[IRFunctionArgs.getSRetArgNo()] =
1194         llvm::PointerType::get(Ty, AddressSpace);
1195   }
1196 
1197   // Add type for inalloca argument.
1198   if (IRFunctionArgs.hasInallocaArg()) {
1199     auto ArgStruct = FI.getArgStruct();
1200     assert(ArgStruct);
1201     ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
1202   }
1203 
1204   // Add in all of the required arguments.
1205   unsigned ArgNo = 0;
1206   CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1207                                      ie = it + FI.getNumRequiredArgs();
1208   for (; it != ie; ++it, ++ArgNo) {
1209     const ABIArgInfo &ArgInfo = it->info;
1210 
1211     // Insert a padding type to ensure proper alignment.
1212     if (IRFunctionArgs.hasPaddingArg(ArgNo))
1213       ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1214           ArgInfo.getPaddingType();
1215 
1216     unsigned FirstIRArg, NumIRArgs;
1217     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1218 
1219     switch (ArgInfo.getKind()) {
1220     case ABIArgInfo::Ignore:
1221     case ABIArgInfo::InAlloca:
1222       assert(NumIRArgs == 0);
1223       break;
1224 
1225     case ABIArgInfo::Indirect: {
1226       assert(NumIRArgs == 1);
1227       // indirect arguments are always on the stack, which is addr space #0.
1228       llvm::Type *LTy = ConvertTypeForMem(it->type);
1229       ArgTypes[FirstIRArg] = LTy->getPointerTo();
1230       break;
1231     }
1232 
1233     case ABIArgInfo::Extend:
1234     case ABIArgInfo::Direct: {
1235       // Fast-isel and the optimizer generally like scalar values better than
1236       // FCAs, so we flatten them if this is safe to do for this argument.
1237       llvm::Type *argType = ArgInfo.getCoerceToType();
1238       llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
1239       if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
1240         assert(NumIRArgs == st->getNumElements());
1241         for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
1242           ArgTypes[FirstIRArg + i] = st->getElementType(i);
1243       } else {
1244         assert(NumIRArgs == 1);
1245         ArgTypes[FirstIRArg] = argType;
1246       }
1247       break;
1248     }
1249 
1250     case ABIArgInfo::Expand:
1251       auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1252       getExpandedTypes(it->type, ArgTypesIter);
1253       assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1254       break;
1255     }
1256   }
1257 
1258   bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1259   assert(Erased && "Not in set?");
1260 
1261   return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
1262 }
1263 
1264 llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
1265   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1266   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
1267 
1268   if (!isFuncTypeConvertible(FPT))
1269     return llvm::StructType::get(getLLVMContext());
1270 
1271   const CGFunctionInfo *Info;
1272   if (isa<CXXDestructorDecl>(MD))
1273     Info =
1274         &arrangeCXXStructorDeclaration(MD, getFromDtorType(GD.getDtorType()));
1275   else
1276     Info = &arrangeCXXMethodDeclaration(MD);
1277   return GetFunctionType(*Info);
1278 }
1279 
1280 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
1281                                            const Decl *TargetDecl,
1282                                            AttributeListType &PAL,
1283                                            unsigned &CallingConv,
1284                                            bool AttrOnCallSite) {
1285   llvm::AttrBuilder FuncAttrs;
1286   llvm::AttrBuilder RetAttrs;
1287 
1288   CallingConv = FI.getEffectiveCallingConvention();
1289 
1290   if (FI.isNoReturn())
1291     FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1292 
1293   // FIXME: handle sseregparm someday...
1294   if (TargetDecl) {
1295     if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
1296       FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
1297     if (TargetDecl->hasAttr<NoThrowAttr>())
1298       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1299     if (TargetDecl->hasAttr<NoReturnAttr>())
1300       FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1301     if (TargetDecl->hasAttr<NoDuplicateAttr>())
1302       FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
1303 
1304     if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
1305       const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
1306       if (FPT && FPT->isNothrow(getContext()))
1307         FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1308       // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
1309       // These attributes are not inherited by overloads.
1310       const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
1311       if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
1312         FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1313     }
1314 
1315     // 'const' and 'pure' attribute functions are also nounwind.
1316     if (TargetDecl->hasAttr<ConstAttr>()) {
1317       FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1318       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1319     } else if (TargetDecl->hasAttr<PureAttr>()) {
1320       FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1321       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1322     }
1323     if (TargetDecl->hasAttr<MallocAttr>())
1324       RetAttrs.addAttribute(llvm::Attribute::NoAlias);
1325     if (TargetDecl->hasAttr<ReturnsNonNullAttr>())
1326       RetAttrs.addAttribute(llvm::Attribute::NonNull);
1327   }
1328 
1329   if (CodeGenOpts.OptimizeSize)
1330     FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1331   if (CodeGenOpts.OptimizeSize == 2)
1332     FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1333   if (CodeGenOpts.DisableRedZone)
1334     FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
1335   if (CodeGenOpts.NoImplicitFloat)
1336     FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
1337   if (CodeGenOpts.EnableSegmentedStacks &&
1338       !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
1339     FuncAttrs.addAttribute("split-stack");
1340 
1341   if (AttrOnCallSite) {
1342     // Attributes that should go on the call site only.
1343     if (!CodeGenOpts.SimplifyLibCalls)
1344       FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1345   } else {
1346     // Attributes that should go on the function, but not the call site.
1347     if (!CodeGenOpts.DisableFPElim) {
1348       FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1349     } else if (CodeGenOpts.OmitLeafFramePointer) {
1350       FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1351       FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
1352     } else {
1353       FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
1354       FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf");
1355     }
1356 
1357     FuncAttrs.addAttribute("less-precise-fpmad",
1358                            llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
1359     FuncAttrs.addAttribute("no-infs-fp-math",
1360                            llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
1361     FuncAttrs.addAttribute("no-nans-fp-math",
1362                            llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
1363     FuncAttrs.addAttribute("unsafe-fp-math",
1364                            llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
1365     FuncAttrs.addAttribute("use-soft-float",
1366                            llvm::toStringRef(CodeGenOpts.SoftFloat));
1367     FuncAttrs.addAttribute("stack-protector-buffer-size",
1368                            llvm::utostr(CodeGenOpts.SSPBufferSize));
1369 
1370     if (!CodeGenOpts.StackRealignment)
1371       FuncAttrs.addAttribute("no-realign-stack");
1372   }
1373 
1374   ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
1375 
1376   QualType RetTy = FI.getReturnType();
1377   const ABIArgInfo &RetAI = FI.getReturnInfo();
1378   switch (RetAI.getKind()) {
1379   case ABIArgInfo::Extend:
1380     if (RetTy->hasSignedIntegerRepresentation())
1381       RetAttrs.addAttribute(llvm::Attribute::SExt);
1382     else if (RetTy->hasUnsignedIntegerRepresentation())
1383       RetAttrs.addAttribute(llvm::Attribute::ZExt);
1384     // FALL THROUGH
1385   case ABIArgInfo::Direct:
1386     if (RetAI.getInReg())
1387       RetAttrs.addAttribute(llvm::Attribute::InReg);
1388     break;
1389   case ABIArgInfo::Ignore:
1390     break;
1391 
1392   case ABIArgInfo::InAlloca:
1393   case ABIArgInfo::Indirect: {
1394     // inalloca and sret disable readnone and readonly
1395     FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1396       .removeAttribute(llvm::Attribute::ReadNone);
1397     break;
1398   }
1399 
1400   case ABIArgInfo::Expand:
1401     llvm_unreachable("Invalid ABI kind for return argument");
1402   }
1403 
1404   if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
1405     QualType PTy = RefTy->getPointeeType();
1406     if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
1407       RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1408                                         .getQuantity());
1409     else if (getContext().getTargetAddressSpace(PTy) == 0)
1410       RetAttrs.addAttribute(llvm::Attribute::NonNull);
1411   }
1412 
1413   // Attach return attributes.
1414   if (RetAttrs.hasAttributes()) {
1415     PAL.push_back(llvm::AttributeSet::get(
1416         getLLVMContext(), llvm::AttributeSet::ReturnIndex, RetAttrs));
1417   }
1418 
1419   // Attach attributes to sret.
1420   if (IRFunctionArgs.hasSRetArg()) {
1421     llvm::AttrBuilder SRETAttrs;
1422     SRETAttrs.addAttribute(llvm::Attribute::StructRet);
1423     if (RetAI.getInReg())
1424       SRETAttrs.addAttribute(llvm::Attribute::InReg);
1425     PAL.push_back(llvm::AttributeSet::get(
1426         getLLVMContext(), IRFunctionArgs.getSRetArgNo() + 1, SRETAttrs));
1427   }
1428 
1429   // Attach attributes to inalloca argument.
1430   if (IRFunctionArgs.hasInallocaArg()) {
1431     llvm::AttrBuilder Attrs;
1432     Attrs.addAttribute(llvm::Attribute::InAlloca);
1433     PAL.push_back(llvm::AttributeSet::get(
1434         getLLVMContext(), IRFunctionArgs.getInallocaArgNo() + 1, Attrs));
1435   }
1436 
1437 
1438   unsigned ArgNo = 0;
1439   for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
1440                                           E = FI.arg_end();
1441        I != E; ++I, ++ArgNo) {
1442     QualType ParamType = I->type;
1443     const ABIArgInfo &AI = I->info;
1444     llvm::AttrBuilder Attrs;
1445 
1446     // Add attribute for padding argument, if necessary.
1447     if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
1448       if (AI.getPaddingInReg())
1449         PAL.push_back(llvm::AttributeSet::get(
1450             getLLVMContext(), IRFunctionArgs.getPaddingArgNo(ArgNo) + 1,
1451             llvm::Attribute::InReg));
1452     }
1453 
1454     // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
1455     // have the corresponding parameter variable.  It doesn't make
1456     // sense to do it here because parameters are so messed up.
1457     switch (AI.getKind()) {
1458     case ABIArgInfo::Extend:
1459       if (ParamType->isSignedIntegerOrEnumerationType())
1460         Attrs.addAttribute(llvm::Attribute::SExt);
1461       else if (ParamType->isUnsignedIntegerOrEnumerationType())
1462         Attrs.addAttribute(llvm::Attribute::ZExt);
1463       // FALL THROUGH
1464     case ABIArgInfo::Direct:
1465       if (AI.getInReg())
1466         Attrs.addAttribute(llvm::Attribute::InReg);
1467       break;
1468 
1469     case ABIArgInfo::Indirect:
1470       if (AI.getInReg())
1471         Attrs.addAttribute(llvm::Attribute::InReg);
1472 
1473       if (AI.getIndirectByVal())
1474         Attrs.addAttribute(llvm::Attribute::ByVal);
1475 
1476       Attrs.addAlignmentAttr(AI.getIndirectAlign());
1477 
1478       // byval disables readnone and readonly.
1479       FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1480         .removeAttribute(llvm::Attribute::ReadNone);
1481       break;
1482 
1483     case ABIArgInfo::Ignore:
1484     case ABIArgInfo::Expand:
1485       continue;
1486 
1487     case ABIArgInfo::InAlloca:
1488       // inalloca disables readnone and readonly.
1489       FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1490           .removeAttribute(llvm::Attribute::ReadNone);
1491       continue;
1492     }
1493 
1494     if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
1495       QualType PTy = RefTy->getPointeeType();
1496       if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
1497         Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1498                                        .getQuantity());
1499       else if (getContext().getTargetAddressSpace(PTy) == 0)
1500         Attrs.addAttribute(llvm::Attribute::NonNull);
1501     }
1502 
1503     if (Attrs.hasAttributes()) {
1504       unsigned FirstIRArg, NumIRArgs;
1505       std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1506       for (unsigned i = 0; i < NumIRArgs; i++)
1507         PAL.push_back(llvm::AttributeSet::get(getLLVMContext(),
1508                                               FirstIRArg + i + 1, Attrs));
1509     }
1510   }
1511   assert(ArgNo == FI.arg_size());
1512 
1513   if (FuncAttrs.hasAttributes())
1514     PAL.push_back(llvm::
1515                   AttributeSet::get(getLLVMContext(),
1516                                     llvm::AttributeSet::FunctionIndex,
1517                                     FuncAttrs));
1518 }
1519 
1520 /// An argument came in as a promoted argument; demote it back to its
1521 /// declared type.
1522 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
1523                                          const VarDecl *var,
1524                                          llvm::Value *value) {
1525   llvm::Type *varType = CGF.ConvertType(var->getType());
1526 
1527   // This can happen with promotions that actually don't change the
1528   // underlying type, like the enum promotions.
1529   if (value->getType() == varType) return value;
1530 
1531   assert((varType->isIntegerTy() || varType->isFloatingPointTy())
1532          && "unexpected promotion type");
1533 
1534   if (isa<llvm::IntegerType>(varType))
1535     return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
1536 
1537   return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
1538 }
1539 
1540 /// Returns the attribute (either parameter attribute, or function
1541 /// attribute), which declares argument ArgNo to be non-null.
1542 static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
1543                                          QualType ArgType, unsigned ArgNo) {
1544   // FIXME: __attribute__((nonnull)) can also be applied to:
1545   //   - references to pointers, where the pointee is known to be
1546   //     nonnull (apparently a Clang extension)
1547   //   - transparent unions containing pointers
1548   // In the former case, LLVM IR cannot represent the constraint. In
1549   // the latter case, we have no guarantee that the transparent union
1550   // is in fact passed as a pointer.
1551   if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
1552     return nullptr;
1553   // First, check attribute on parameter itself.
1554   if (PVD) {
1555     if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
1556       return ParmNNAttr;
1557   }
1558   // Check function attributes.
1559   if (!FD)
1560     return nullptr;
1561   for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
1562     if (NNAttr->isNonNull(ArgNo))
1563       return NNAttr;
1564   }
1565   return nullptr;
1566 }
1567 
1568 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1569                                          llvm::Function *Fn,
1570                                          const FunctionArgList &Args) {
1571   if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
1572     // Naked functions don't have prologues.
1573     return;
1574 
1575   // If this is an implicit-return-zero function, go ahead and
1576   // initialize the return value.  TODO: it might be nice to have
1577   // a more general mechanism for this that didn't require synthesized
1578   // return statements.
1579   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
1580     if (FD->hasImplicitReturnZero()) {
1581       QualType RetTy = FD->getReturnType().getUnqualifiedType();
1582       llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
1583       llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
1584       Builder.CreateStore(Zero, ReturnValue);
1585     }
1586   }
1587 
1588   // FIXME: We no longer need the types from FunctionArgList; lift up and
1589   // simplify.
1590 
1591   ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
1592   // Flattened function arguments.
1593   SmallVector<llvm::Argument *, 16> FnArgs;
1594   FnArgs.reserve(IRFunctionArgs.totalIRArgs());
1595   for (auto &Arg : Fn->args()) {
1596     FnArgs.push_back(&Arg);
1597   }
1598   assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
1599 
1600   // If we're using inalloca, all the memory arguments are GEPs off of the last
1601   // parameter, which is a pointer to the complete memory area.
1602   llvm::Value *ArgStruct = nullptr;
1603   if (IRFunctionArgs.hasInallocaArg()) {
1604     ArgStruct = FnArgs[IRFunctionArgs.getInallocaArgNo()];
1605     assert(ArgStruct->getType() == FI.getArgStruct()->getPointerTo());
1606   }
1607 
1608   // Name the struct return parameter.
1609   if (IRFunctionArgs.hasSRetArg()) {
1610     auto AI = FnArgs[IRFunctionArgs.getSRetArgNo()];
1611     AI->setName("agg.result");
1612     AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), AI->getArgNo() + 1,
1613                                         llvm::Attribute::NoAlias));
1614   }
1615 
1616   // Track if we received the parameter as a pointer (indirect, byval, or
1617   // inalloca).  If already have a pointer, EmitParmDecl doesn't need to copy it
1618   // into a local alloca for us.
1619   enum ValOrPointer { HaveValue = 0, HavePointer = 1 };
1620   typedef llvm::PointerIntPair<llvm::Value *, 1> ValueAndIsPtr;
1621   SmallVector<ValueAndIsPtr, 16> ArgVals;
1622   ArgVals.reserve(Args.size());
1623 
1624   // Create a pointer value for every parameter declaration.  This usually
1625   // entails copying one or more LLVM IR arguments into an alloca.  Don't push
1626   // any cleanups or do anything that might unwind.  We do that separately, so
1627   // we can push the cleanups in the correct order for the ABI.
1628   assert(FI.arg_size() == Args.size() &&
1629          "Mismatch between function signature & arguments.");
1630   unsigned ArgNo = 0;
1631   CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1632   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1633        i != e; ++i, ++info_it, ++ArgNo) {
1634     const VarDecl *Arg = *i;
1635     QualType Ty = info_it->type;
1636     const ABIArgInfo &ArgI = info_it->info;
1637 
1638     bool isPromoted =
1639       isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
1640 
1641     unsigned FirstIRArg, NumIRArgs;
1642     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1643 
1644     switch (ArgI.getKind()) {
1645     case ABIArgInfo::InAlloca: {
1646       assert(NumIRArgs == 0);
1647       llvm::Value *V = Builder.CreateStructGEP(
1648           ArgStruct, ArgI.getInAllocaFieldIndex(), Arg->getName());
1649       ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
1650       break;
1651     }
1652 
1653     case ABIArgInfo::Indirect: {
1654       assert(NumIRArgs == 1);
1655       llvm::Value *V = FnArgs[FirstIRArg];
1656 
1657       if (!hasScalarEvaluationKind(Ty)) {
1658         // Aggregates and complex variables are accessed by reference.  All we
1659         // need to do is realign the value, if requested
1660         if (ArgI.getIndirectRealign()) {
1661           llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
1662 
1663           // Copy from the incoming argument pointer to the temporary with the
1664           // appropriate alignment.
1665           //
1666           // FIXME: We should have a common utility for generating an aggregate
1667           // copy.
1668           llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
1669           CharUnits Size = getContext().getTypeSizeInChars(Ty);
1670           llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
1671           llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
1672           Builder.CreateMemCpy(Dst,
1673                                Src,
1674                                llvm::ConstantInt::get(IntPtrTy,
1675                                                       Size.getQuantity()),
1676                                ArgI.getIndirectAlign(),
1677                                false);
1678           V = AlignedTemp;
1679         }
1680         ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
1681       } else {
1682         // Load scalar value from indirect argument.
1683         CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1684         V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty,
1685                              Arg->getLocStart());
1686 
1687         if (isPromoted)
1688           V = emitArgumentDemotion(*this, Arg, V);
1689         ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
1690       }
1691       break;
1692     }
1693 
1694     case ABIArgInfo::Extend:
1695     case ABIArgInfo::Direct: {
1696 
1697       // If we have the trivial case, handle it with no muss and fuss.
1698       if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
1699           ArgI.getCoerceToType() == ConvertType(Ty) &&
1700           ArgI.getDirectOffset() == 0) {
1701         assert(NumIRArgs == 1);
1702         auto AI = FnArgs[FirstIRArg];
1703         llvm::Value *V = AI;
1704 
1705         if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
1706           if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
1707                              PVD->getFunctionScopeIndex()))
1708             AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1709                                                 AI->getArgNo() + 1,
1710                                                 llvm::Attribute::NonNull));
1711 
1712           QualType OTy = PVD->getOriginalType();
1713           if (const auto *ArrTy =
1714               getContext().getAsConstantArrayType(OTy)) {
1715             // A C99 array parameter declaration with the static keyword also
1716             // indicates dereferenceability, and if the size is constant we can
1717             // use the dereferenceable attribute (which requires the size in
1718             // bytes).
1719             if (ArrTy->getSizeModifier() == ArrayType::Static) {
1720               QualType ETy = ArrTy->getElementType();
1721               uint64_t ArrSize = ArrTy->getSize().getZExtValue();
1722               if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
1723                   ArrSize) {
1724                 llvm::AttrBuilder Attrs;
1725                 Attrs.addDereferenceableAttr(
1726                   getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize);
1727                 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1728                                                     AI->getArgNo() + 1, Attrs));
1729               } else if (getContext().getTargetAddressSpace(ETy) == 0) {
1730                 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1731                                                     AI->getArgNo() + 1,
1732                                                     llvm::Attribute::NonNull));
1733               }
1734             }
1735           } else if (const auto *ArrTy =
1736                      getContext().getAsVariableArrayType(OTy)) {
1737             // For C99 VLAs with the static keyword, we don't know the size so
1738             // we can't use the dereferenceable attribute, but in addrspace(0)
1739             // we know that it must be nonnull.
1740             if (ArrTy->getSizeModifier() == VariableArrayType::Static &&
1741                 !getContext().getTargetAddressSpace(ArrTy->getElementType()))
1742               AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1743                                                   AI->getArgNo() + 1,
1744                                                   llvm::Attribute::NonNull));
1745           }
1746 
1747           const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
1748           if (!AVAttr)
1749             if (const auto *TOTy = dyn_cast<TypedefType>(OTy))
1750               AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
1751           if (AVAttr) {
1752             llvm::Value *AlignmentValue =
1753               EmitScalarExpr(AVAttr->getAlignment());
1754             llvm::ConstantInt *AlignmentCI =
1755               cast<llvm::ConstantInt>(AlignmentValue);
1756             unsigned Alignment =
1757               std::min((unsigned) AlignmentCI->getZExtValue(),
1758                        +llvm::Value::MaximumAlignment);
1759 
1760             llvm::AttrBuilder Attrs;
1761             Attrs.addAlignmentAttr(Alignment);
1762             AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1763                                                 AI->getArgNo() + 1, Attrs));
1764           }
1765         }
1766 
1767         if (Arg->getType().isRestrictQualified())
1768           AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1769                                               AI->getArgNo() + 1,
1770                                               llvm::Attribute::NoAlias));
1771 
1772         // Ensure the argument is the correct type.
1773         if (V->getType() != ArgI.getCoerceToType())
1774           V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
1775 
1776         if (isPromoted)
1777           V = emitArgumentDemotion(*this, Arg, V);
1778 
1779         if (const CXXMethodDecl *MD =
1780             dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) {
1781           if (MD->isVirtual() && Arg == CXXABIThisDecl)
1782             V = CGM.getCXXABI().
1783                 adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V);
1784         }
1785 
1786         // Because of merging of function types from multiple decls it is
1787         // possible for the type of an argument to not match the corresponding
1788         // type in the function type. Since we are codegening the callee
1789         // in here, add a cast to the argument type.
1790         llvm::Type *LTy = ConvertType(Arg->getType());
1791         if (V->getType() != LTy)
1792           V = Builder.CreateBitCast(V, LTy);
1793 
1794         ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
1795         break;
1796       }
1797 
1798       llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName());
1799 
1800       // The alignment we need to use is the max of the requested alignment for
1801       // the argument plus the alignment required by our access code below.
1802       unsigned AlignmentToUse =
1803         CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType());
1804       AlignmentToUse = std::max(AlignmentToUse,
1805                         (unsigned)getContext().getDeclAlign(Arg).getQuantity());
1806 
1807       Alloca->setAlignment(AlignmentToUse);
1808       llvm::Value *V = Alloca;
1809       llvm::Value *Ptr = V;    // Pointer to store into.
1810 
1811       // If the value is offset in memory, apply the offset now.
1812       if (unsigned Offs = ArgI.getDirectOffset()) {
1813         Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1814         Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
1815         Ptr = Builder.CreateBitCast(Ptr,
1816                           llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1817       }
1818 
1819       // Fast-isel and the optimizer generally like scalar values better than
1820       // FCAs, so we flatten them if this is safe to do for this argument.
1821       llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
1822       if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
1823           STy->getNumElements() > 1) {
1824         uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
1825         llvm::Type *DstTy =
1826           cast<llvm::PointerType>(Ptr->getType())->getElementType();
1827         uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
1828 
1829         if (SrcSize <= DstSize) {
1830           Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1831 
1832           assert(STy->getNumElements() == NumIRArgs);
1833           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1834             auto AI = FnArgs[FirstIRArg + i];
1835             AI->setName(Arg->getName() + ".coerce" + Twine(i));
1836             llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1837             Builder.CreateStore(AI, EltPtr);
1838           }
1839         } else {
1840           llvm::AllocaInst *TempAlloca =
1841             CreateTempAlloca(ArgI.getCoerceToType(), "coerce");
1842           TempAlloca->setAlignment(AlignmentToUse);
1843           llvm::Value *TempV = TempAlloca;
1844 
1845           assert(STy->getNumElements() == NumIRArgs);
1846           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1847             auto AI = FnArgs[FirstIRArg + i];
1848             AI->setName(Arg->getName() + ".coerce" + Twine(i));
1849             llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i);
1850             Builder.CreateStore(AI, EltPtr);
1851           }
1852 
1853           Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse);
1854         }
1855       } else {
1856         // Simple case, just do a coerced store of the argument into the alloca.
1857         assert(NumIRArgs == 1);
1858         auto AI = FnArgs[FirstIRArg];
1859         AI->setName(Arg->getName() + ".coerce");
1860         CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this);
1861       }
1862 
1863 
1864       // Match to what EmitParmDecl is expecting for this type.
1865       if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
1866         V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart());
1867         if (isPromoted)
1868           V = emitArgumentDemotion(*this, Arg, V);
1869         ArgVals.push_back(ValueAndIsPtr(V, HaveValue));
1870       } else {
1871         ArgVals.push_back(ValueAndIsPtr(V, HavePointer));
1872       }
1873       break;
1874     }
1875 
1876     case ABIArgInfo::Expand: {
1877       // If this structure was expanded into multiple arguments then
1878       // we need to create a temporary and reconstruct it from the
1879       // arguments.
1880       llvm::AllocaInst *Alloca = CreateMemTemp(Ty);
1881       CharUnits Align = getContext().getDeclAlign(Arg);
1882       Alloca->setAlignment(Align.getQuantity());
1883       LValue LV = MakeAddrLValue(Alloca, Ty, Align);
1884       ArgVals.push_back(ValueAndIsPtr(Alloca, HavePointer));
1885 
1886       auto FnArgIter = FnArgs.begin() + FirstIRArg;
1887       ExpandTypeFromArgs(Ty, LV, FnArgIter);
1888       assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
1889       for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
1890         auto AI = FnArgs[FirstIRArg + i];
1891         AI->setName(Arg->getName() + "." + Twine(i));
1892       }
1893       break;
1894     }
1895 
1896     case ABIArgInfo::Ignore:
1897       assert(NumIRArgs == 0);
1898       // Initialize the local variable appropriately.
1899       if (!hasScalarEvaluationKind(Ty)) {
1900         ArgVals.push_back(ValueAndIsPtr(CreateMemTemp(Ty), HavePointer));
1901       } else {
1902         llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
1903         ArgVals.push_back(ValueAndIsPtr(U, HaveValue));
1904       }
1905       break;
1906     }
1907   }
1908 
1909   if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
1910     for (int I = Args.size() - 1; I >= 0; --I)
1911       EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(),
1912                    I + 1);
1913   } else {
1914     for (unsigned I = 0, E = Args.size(); I != E; ++I)
1915       EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(),
1916                    I + 1);
1917   }
1918 }
1919 
1920 static void eraseUnusedBitCasts(llvm::Instruction *insn) {
1921   while (insn->use_empty()) {
1922     llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
1923     if (!bitcast) return;
1924 
1925     // This is "safe" because we would have used a ConstantExpr otherwise.
1926     insn = cast<llvm::Instruction>(bitcast->getOperand(0));
1927     bitcast->eraseFromParent();
1928   }
1929 }
1930 
1931 /// Try to emit a fused autorelease of a return result.
1932 static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1933                                                     llvm::Value *result) {
1934   // We must be immediately followed the cast.
1935   llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1936   if (BB->empty()) return nullptr;
1937   if (&BB->back() != result) return nullptr;
1938 
1939   llvm::Type *resultType = result->getType();
1940 
1941   // result is in a BasicBlock and is therefore an Instruction.
1942   llvm::Instruction *generator = cast<llvm::Instruction>(result);
1943 
1944   SmallVector<llvm::Instruction*,4> insnsToKill;
1945 
1946   // Look for:
1947   //  %generator = bitcast %type1* %generator2 to %type2*
1948   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1949     // We would have emitted this as a constant if the operand weren't
1950     // an Instruction.
1951     generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1952 
1953     // Require the generator to be immediately followed by the cast.
1954     if (generator->getNextNode() != bitcast)
1955       return nullptr;
1956 
1957     insnsToKill.push_back(bitcast);
1958   }
1959 
1960   // Look for:
1961   //   %generator = call i8* @objc_retain(i8* %originalResult)
1962   // or
1963   //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1964   llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1965   if (!call) return nullptr;
1966 
1967   bool doRetainAutorelease;
1968 
1969   if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1970     doRetainAutorelease = true;
1971   } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1972                                           .objc_retainAutoreleasedReturnValue) {
1973     doRetainAutorelease = false;
1974 
1975     // If we emitted an assembly marker for this call (and the
1976     // ARCEntrypoints field should have been set if so), go looking
1977     // for that call.  If we can't find it, we can't do this
1978     // optimization.  But it should always be the immediately previous
1979     // instruction, unless we needed bitcasts around the call.
1980     if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) {
1981       llvm::Instruction *prev = call->getPrevNode();
1982       assert(prev);
1983       if (isa<llvm::BitCastInst>(prev)) {
1984         prev = prev->getPrevNode();
1985         assert(prev);
1986       }
1987       assert(isa<llvm::CallInst>(prev));
1988       assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
1989                CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker);
1990       insnsToKill.push_back(prev);
1991     }
1992   } else {
1993     return nullptr;
1994   }
1995 
1996   result = call->getArgOperand(0);
1997   insnsToKill.push_back(call);
1998 
1999   // Keep killing bitcasts, for sanity.  Note that we no longer care
2000   // about precise ordering as long as there's exactly one use.
2001   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
2002     if (!bitcast->hasOneUse()) break;
2003     insnsToKill.push_back(bitcast);
2004     result = bitcast->getOperand(0);
2005   }
2006 
2007   // Delete all the unnecessary instructions, from latest to earliest.
2008   for (SmallVectorImpl<llvm::Instruction*>::iterator
2009          i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
2010     (*i)->eraseFromParent();
2011 
2012   // Do the fused retain/autorelease if we were asked to.
2013   if (doRetainAutorelease)
2014     result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
2015 
2016   // Cast back to the result type.
2017   return CGF.Builder.CreateBitCast(result, resultType);
2018 }
2019 
2020 /// If this is a +1 of the value of an immutable 'self', remove it.
2021 static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
2022                                           llvm::Value *result) {
2023   // This is only applicable to a method with an immutable 'self'.
2024   const ObjCMethodDecl *method =
2025     dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
2026   if (!method) return nullptr;
2027   const VarDecl *self = method->getSelfDecl();
2028   if (!self->getType().isConstQualified()) return nullptr;
2029 
2030   // Look for a retain call.
2031   llvm::CallInst *retainCall =
2032     dyn_cast<llvm::CallInst>(result->stripPointerCasts());
2033   if (!retainCall ||
2034       retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain)
2035     return nullptr;
2036 
2037   // Look for an ordinary load of 'self'.
2038   llvm::Value *retainedValue = retainCall->getArgOperand(0);
2039   llvm::LoadInst *load =
2040     dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
2041   if (!load || load->isAtomic() || load->isVolatile() ||
2042       load->getPointerOperand() != CGF.GetAddrOfLocalVar(self))
2043     return nullptr;
2044 
2045   // Okay!  Burn it all down.  This relies for correctness on the
2046   // assumption that the retain is emitted as part of the return and
2047   // that thereafter everything is used "linearly".
2048   llvm::Type *resultType = result->getType();
2049   eraseUnusedBitCasts(cast<llvm::Instruction>(result));
2050   assert(retainCall->use_empty());
2051   retainCall->eraseFromParent();
2052   eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
2053 
2054   return CGF.Builder.CreateBitCast(load, resultType);
2055 }
2056 
2057 /// Emit an ARC autorelease of the result of a function.
2058 ///
2059 /// \return the value to actually return from the function
2060 static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
2061                                             llvm::Value *result) {
2062   // If we're returning 'self', kill the initial retain.  This is a
2063   // heuristic attempt to "encourage correctness" in the really unfortunate
2064   // case where we have a return of self during a dealloc and we desperately
2065   // need to avoid the possible autorelease.
2066   if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
2067     return self;
2068 
2069   // At -O0, try to emit a fused retain/autorelease.
2070   if (CGF.shouldUseFusedARCCalls())
2071     if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
2072       return fused;
2073 
2074   return CGF.EmitARCAutoreleaseReturnValue(result);
2075 }
2076 
2077 /// Heuristically search for a dominating store to the return-value slot.
2078 static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
2079   // If there are multiple uses of the return-value slot, just check
2080   // for something immediately preceding the IP.  Sometimes this can
2081   // happen with how we generate implicit-returns; it can also happen
2082   // with noreturn cleanups.
2083   if (!CGF.ReturnValue->hasOneUse()) {
2084     llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
2085     if (IP->empty()) return nullptr;
2086     llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back());
2087     if (!store) return nullptr;
2088     if (store->getPointerOperand() != CGF.ReturnValue) return nullptr;
2089     assert(!store->isAtomic() && !store->isVolatile()); // see below
2090     return store;
2091   }
2092 
2093   llvm::StoreInst *store =
2094     dyn_cast<llvm::StoreInst>(CGF.ReturnValue->user_back());
2095   if (!store) return nullptr;
2096 
2097   // These aren't actually possible for non-coerced returns, and we
2098   // only care about non-coerced returns on this code path.
2099   assert(!store->isAtomic() && !store->isVolatile());
2100 
2101   // Now do a first-and-dirty dominance check: just walk up the
2102   // single-predecessors chain from the current insertion point.
2103   llvm::BasicBlock *StoreBB = store->getParent();
2104   llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
2105   while (IP != StoreBB) {
2106     if (!(IP = IP->getSinglePredecessor()))
2107       return nullptr;
2108   }
2109 
2110   // Okay, the store's basic block dominates the insertion point; we
2111   // can do our thing.
2112   return store;
2113 }
2114 
2115 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
2116                                          bool EmitRetDbgLoc,
2117                                          SourceLocation EndLoc) {
2118   if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
2119     // Naked functions don't have epilogues.
2120     Builder.CreateUnreachable();
2121     return;
2122   }
2123 
2124   // Functions with no result always return void.
2125   if (!ReturnValue) {
2126     Builder.CreateRetVoid();
2127     return;
2128   }
2129 
2130   llvm::DebugLoc RetDbgLoc;
2131   llvm::Value *RV = nullptr;
2132   QualType RetTy = FI.getReturnType();
2133   const ABIArgInfo &RetAI = FI.getReturnInfo();
2134 
2135   switch (RetAI.getKind()) {
2136   case ABIArgInfo::InAlloca:
2137     // Aggregrates get evaluated directly into the destination.  Sometimes we
2138     // need to return the sret value in a register, though.
2139     assert(hasAggregateEvaluationKind(RetTy));
2140     if (RetAI.getInAllocaSRet()) {
2141       llvm::Function::arg_iterator EI = CurFn->arg_end();
2142       --EI;
2143       llvm::Value *ArgStruct = EI;
2144       llvm::Value *SRet =
2145           Builder.CreateStructGEP(ArgStruct, RetAI.getInAllocaFieldIndex());
2146       RV = Builder.CreateLoad(SRet, "sret");
2147     }
2148     break;
2149 
2150   case ABIArgInfo::Indirect: {
2151     auto AI = CurFn->arg_begin();
2152     if (RetAI.isSRetAfterThis())
2153       ++AI;
2154     switch (getEvaluationKind(RetTy)) {
2155     case TEK_Complex: {
2156       ComplexPairTy RT =
2157         EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy),
2158                           EndLoc);
2159       EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(AI, RetTy),
2160                          /*isInit*/ true);
2161       break;
2162     }
2163     case TEK_Aggregate:
2164       // Do nothing; aggregrates get evaluated directly into the destination.
2165       break;
2166     case TEK_Scalar:
2167       EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
2168                         MakeNaturalAlignAddrLValue(AI, RetTy),
2169                         /*isInit*/ true);
2170       break;
2171     }
2172     break;
2173   }
2174 
2175   case ABIArgInfo::Extend:
2176   case ABIArgInfo::Direct:
2177     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
2178         RetAI.getDirectOffset() == 0) {
2179       // The internal return value temp always will have pointer-to-return-type
2180       // type, just do a load.
2181 
2182       // If there is a dominating store to ReturnValue, we can elide
2183       // the load, zap the store, and usually zap the alloca.
2184       if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
2185         // Reuse the debug location from the store unless there is
2186         // cleanup code to be emitted between the store and return
2187         // instruction.
2188         if (EmitRetDbgLoc && !AutoreleaseResult)
2189           RetDbgLoc = SI->getDebugLoc();
2190         // Get the stored value and nuke the now-dead store.
2191         RV = SI->getValueOperand();
2192         SI->eraseFromParent();
2193 
2194         // If that was the only use of the return value, nuke it as well now.
2195         if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
2196           cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
2197           ReturnValue = nullptr;
2198         }
2199 
2200       // Otherwise, we have to do a simple load.
2201       } else {
2202         RV = Builder.CreateLoad(ReturnValue);
2203       }
2204     } else {
2205       llvm::Value *V = ReturnValue;
2206       // If the value is offset in memory, apply the offset now.
2207       if (unsigned Offs = RetAI.getDirectOffset()) {
2208         V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
2209         V = Builder.CreateConstGEP1_32(V, Offs);
2210         V = Builder.CreateBitCast(V,
2211                          llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
2212       }
2213 
2214       RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
2215     }
2216 
2217     // In ARC, end functions that return a retainable type with a call
2218     // to objc_autoreleaseReturnValue.
2219     if (AutoreleaseResult) {
2220       assert(getLangOpts().ObjCAutoRefCount &&
2221              !FI.isReturnsRetained() &&
2222              RetTy->isObjCRetainableType());
2223       RV = emitAutoreleaseOfResult(*this, RV);
2224     }
2225 
2226     break;
2227 
2228   case ABIArgInfo::Ignore:
2229     break;
2230 
2231   case ABIArgInfo::Expand:
2232     llvm_unreachable("Invalid ABI kind for return argument");
2233   }
2234 
2235   llvm::Instruction *Ret;
2236   if (RV) {
2237     if (SanOpts->ReturnsNonnullAttribute) {
2238       if (auto RetNNAttr = CurGD.getDecl()->getAttr<ReturnsNonNullAttr>()) {
2239         SanitizerScope SanScope(this);
2240         llvm::Value *Cond = Builder.CreateICmpNE(
2241             RV, llvm::Constant::getNullValue(RV->getType()));
2242         llvm::Constant *StaticData[] = {
2243             EmitCheckSourceLocation(EndLoc),
2244             EmitCheckSourceLocation(RetNNAttr->getLocation()),
2245         };
2246         EmitCheck(Cond, "nonnull_return", StaticData, None, CRK_Recoverable);
2247       }
2248     }
2249     Ret = Builder.CreateRet(RV);
2250   } else {
2251     Ret = Builder.CreateRetVoid();
2252   }
2253 
2254   if (!RetDbgLoc.isUnknown())
2255     Ret->setDebugLoc(RetDbgLoc);
2256 }
2257 
2258 static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
2259   const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
2260   return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
2261 }
2262 
2263 static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty) {
2264   // FIXME: Generate IR in one pass, rather than going back and fixing up these
2265   // placeholders.
2266   llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
2267   llvm::Value *Placeholder =
2268       llvm::UndefValue::get(IRTy->getPointerTo()->getPointerTo());
2269   Placeholder = CGF.Builder.CreateLoad(Placeholder);
2270   return AggValueSlot::forAddr(Placeholder, CharUnits::Zero(),
2271                                Ty.getQualifiers(),
2272                                AggValueSlot::IsNotDestructed,
2273                                AggValueSlot::DoesNotNeedGCBarriers,
2274                                AggValueSlot::IsNotAliased);
2275 }
2276 
2277 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
2278                                           const VarDecl *param,
2279                                           SourceLocation loc) {
2280   // StartFunction converted the ABI-lowered parameter(s) into a
2281   // local alloca.  We need to turn that into an r-value suitable
2282   // for EmitCall.
2283   llvm::Value *local = GetAddrOfLocalVar(param);
2284 
2285   QualType type = param->getType();
2286 
2287   // For the most part, we just need to load the alloca, except:
2288   // 1) aggregate r-values are actually pointers to temporaries, and
2289   // 2) references to non-scalars are pointers directly to the aggregate.
2290   // I don't know why references to scalars are different here.
2291   if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
2292     if (!hasScalarEvaluationKind(ref->getPointeeType()))
2293       return args.add(RValue::getAggregate(local), type);
2294 
2295     // Locals which are references to scalars are represented
2296     // with allocas holding the pointer.
2297     return args.add(RValue::get(Builder.CreateLoad(local)), type);
2298   }
2299 
2300   assert(!isInAllocaArgument(CGM.getCXXABI(), type) &&
2301          "cannot emit delegate call arguments for inalloca arguments!");
2302 
2303   args.add(convertTempToRValue(local, type, loc), type);
2304 }
2305 
2306 static bool isProvablyNull(llvm::Value *addr) {
2307   return isa<llvm::ConstantPointerNull>(addr);
2308 }
2309 
2310 static bool isProvablyNonNull(llvm::Value *addr) {
2311   return isa<llvm::AllocaInst>(addr);
2312 }
2313 
2314 /// Emit the actual writing-back of a writeback.
2315 static void emitWriteback(CodeGenFunction &CGF,
2316                           const CallArgList::Writeback &writeback) {
2317   const LValue &srcLV = writeback.Source;
2318   llvm::Value *srcAddr = srcLV.getAddress();
2319   assert(!isProvablyNull(srcAddr) &&
2320          "shouldn't have writeback for provably null argument");
2321 
2322   llvm::BasicBlock *contBB = nullptr;
2323 
2324   // If the argument wasn't provably non-null, we need to null check
2325   // before doing the store.
2326   bool provablyNonNull = isProvablyNonNull(srcAddr);
2327   if (!provablyNonNull) {
2328     llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
2329     contBB = CGF.createBasicBlock("icr.done");
2330 
2331     llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
2332     CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
2333     CGF.EmitBlock(writebackBB);
2334   }
2335 
2336   // Load the value to writeback.
2337   llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
2338 
2339   // Cast it back, in case we're writing an id to a Foo* or something.
2340   value = CGF.Builder.CreateBitCast(value,
2341                cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
2342                             "icr.writeback-cast");
2343 
2344   // Perform the writeback.
2345 
2346   // If we have a "to use" value, it's something we need to emit a use
2347   // of.  This has to be carefully threaded in: if it's done after the
2348   // release it's potentially undefined behavior (and the optimizer
2349   // will ignore it), and if it happens before the retain then the
2350   // optimizer could move the release there.
2351   if (writeback.ToUse) {
2352     assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
2353 
2354     // Retain the new value.  No need to block-copy here:  the block's
2355     // being passed up the stack.
2356     value = CGF.EmitARCRetainNonBlock(value);
2357 
2358     // Emit the intrinsic use here.
2359     CGF.EmitARCIntrinsicUse(writeback.ToUse);
2360 
2361     // Load the old value (primitively).
2362     llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
2363 
2364     // Put the new value in place (primitively).
2365     CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
2366 
2367     // Release the old value.
2368     CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
2369 
2370   // Otherwise, we can just do a normal lvalue store.
2371   } else {
2372     CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
2373   }
2374 
2375   // Jump to the continuation block.
2376   if (!provablyNonNull)
2377     CGF.EmitBlock(contBB);
2378 }
2379 
2380 static void emitWritebacks(CodeGenFunction &CGF,
2381                            const CallArgList &args) {
2382   for (const auto &I : args.writebacks())
2383     emitWriteback(CGF, I);
2384 }
2385 
2386 static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
2387                                             const CallArgList &CallArgs) {
2388   assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee());
2389   ArrayRef<CallArgList::CallArgCleanup> Cleanups =
2390     CallArgs.getCleanupsToDeactivate();
2391   // Iterate in reverse to increase the likelihood of popping the cleanup.
2392   for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator
2393          I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) {
2394     CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP);
2395     I->IsActiveIP->eraseFromParent();
2396   }
2397 }
2398 
2399 static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
2400   if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
2401     if (uop->getOpcode() == UO_AddrOf)
2402       return uop->getSubExpr();
2403   return nullptr;
2404 }
2405 
2406 /// Emit an argument that's being passed call-by-writeback.  That is,
2407 /// we are passing the address of
2408 static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
2409                              const ObjCIndirectCopyRestoreExpr *CRE) {
2410   LValue srcLV;
2411 
2412   // Make an optimistic effort to emit the address as an l-value.
2413   // This can fail if the the argument expression is more complicated.
2414   if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
2415     srcLV = CGF.EmitLValue(lvExpr);
2416 
2417   // Otherwise, just emit it as a scalar.
2418   } else {
2419     llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
2420 
2421     QualType srcAddrType =
2422       CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
2423     srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType);
2424   }
2425   llvm::Value *srcAddr = srcLV.getAddress();
2426 
2427   // The dest and src types don't necessarily match in LLVM terms
2428   // because of the crazy ObjC compatibility rules.
2429 
2430   llvm::PointerType *destType =
2431     cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
2432 
2433   // If the address is a constant null, just pass the appropriate null.
2434   if (isProvablyNull(srcAddr)) {
2435     args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
2436              CRE->getType());
2437     return;
2438   }
2439 
2440   // Create the temporary.
2441   llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
2442                                            "icr.temp");
2443   // Loading an l-value can introduce a cleanup if the l-value is __weak,
2444   // and that cleanup will be conditional if we can't prove that the l-value
2445   // isn't null, so we need to register a dominating point so that the cleanups
2446   // system will make valid IR.
2447   CodeGenFunction::ConditionalEvaluation condEval(CGF);
2448 
2449   // Zero-initialize it if we're not doing a copy-initialization.
2450   bool shouldCopy = CRE->shouldCopy();
2451   if (!shouldCopy) {
2452     llvm::Value *null =
2453       llvm::ConstantPointerNull::get(
2454         cast<llvm::PointerType>(destType->getElementType()));
2455     CGF.Builder.CreateStore(null, temp);
2456   }
2457 
2458   llvm::BasicBlock *contBB = nullptr;
2459   llvm::BasicBlock *originBB = nullptr;
2460 
2461   // If the address is *not* known to be non-null, we need to switch.
2462   llvm::Value *finalArgument;
2463 
2464   bool provablyNonNull = isProvablyNonNull(srcAddr);
2465   if (provablyNonNull) {
2466     finalArgument = temp;
2467   } else {
2468     llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
2469 
2470     finalArgument = CGF.Builder.CreateSelect(isNull,
2471                                    llvm::ConstantPointerNull::get(destType),
2472                                              temp, "icr.argument");
2473 
2474     // If we need to copy, then the load has to be conditional, which
2475     // means we need control flow.
2476     if (shouldCopy) {
2477       originBB = CGF.Builder.GetInsertBlock();
2478       contBB = CGF.createBasicBlock("icr.cont");
2479       llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
2480       CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
2481       CGF.EmitBlock(copyBB);
2482       condEval.begin(CGF);
2483     }
2484   }
2485 
2486   llvm::Value *valueToUse = nullptr;
2487 
2488   // Perform a copy if necessary.
2489   if (shouldCopy) {
2490     RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
2491     assert(srcRV.isScalar());
2492 
2493     llvm::Value *src = srcRV.getScalarVal();
2494     src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
2495                                     "icr.cast");
2496 
2497     // Use an ordinary store, not a store-to-lvalue.
2498     CGF.Builder.CreateStore(src, temp);
2499 
2500     // If optimization is enabled, and the value was held in a
2501     // __strong variable, we need to tell the optimizer that this
2502     // value has to stay alive until we're doing the store back.
2503     // This is because the temporary is effectively unretained,
2504     // and so otherwise we can violate the high-level semantics.
2505     if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
2506         srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
2507       valueToUse = src;
2508     }
2509   }
2510 
2511   // Finish the control flow if we needed it.
2512   if (shouldCopy && !provablyNonNull) {
2513     llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
2514     CGF.EmitBlock(contBB);
2515 
2516     // Make a phi for the value to intrinsically use.
2517     if (valueToUse) {
2518       llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
2519                                                       "icr.to-use");
2520       phiToUse->addIncoming(valueToUse, copyBB);
2521       phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
2522                             originBB);
2523       valueToUse = phiToUse;
2524     }
2525 
2526     condEval.end(CGF);
2527   }
2528 
2529   args.addWriteback(srcLV, temp, valueToUse);
2530   args.add(RValue::get(finalArgument), CRE->getType());
2531 }
2532 
2533 void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
2534   assert(!StackBase && !StackCleanup.isValid());
2535 
2536   // Save the stack.
2537   llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
2538   StackBase = CGF.Builder.CreateCall(F, "inalloca.save");
2539 
2540   // Control gets really tied up in landing pads, so we have to spill the
2541   // stacksave to an alloca to avoid violating SSA form.
2542   // TODO: This is dead if we never emit the cleanup.  We should create the
2543   // alloca and store lazily on the first cleanup emission.
2544   StackBaseMem = CGF.CreateTempAlloca(CGF.Int8PtrTy, "inalloca.spmem");
2545   CGF.Builder.CreateStore(StackBase, StackBaseMem);
2546   CGF.pushStackRestore(EHCleanup, StackBaseMem);
2547   StackCleanup = CGF.EHStack.getInnermostEHScope();
2548   assert(StackCleanup.isValid());
2549 }
2550 
2551 void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
2552   if (StackBase) {
2553     CGF.DeactivateCleanupBlock(StackCleanup, StackBase);
2554     llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
2555     // We could load StackBase from StackBaseMem, but in the non-exceptional
2556     // case we can skip it.
2557     CGF.Builder.CreateCall(F, StackBase);
2558   }
2559 }
2560 
2561 static void emitNonNullArgCheck(CodeGenFunction &CGF, RValue RV,
2562                                 QualType ArgType, SourceLocation ArgLoc,
2563                                 const FunctionDecl *FD, unsigned ParmNum) {
2564   if (!CGF.SanOpts->NonnullAttribute || !FD)
2565     return;
2566   auto PVD = ParmNum < FD->getNumParams() ? FD->getParamDecl(ParmNum) : nullptr;
2567   unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
2568   auto NNAttr = getNonNullAttr(FD, PVD, ArgType, ArgNo);
2569   if (!NNAttr)
2570     return;
2571   CodeGenFunction::SanitizerScope SanScope(&CGF);
2572   assert(RV.isScalar());
2573   llvm::Value *V = RV.getScalarVal();
2574   llvm::Value *Cond =
2575       CGF.Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
2576   llvm::Constant *StaticData[] = {
2577       CGF.EmitCheckSourceLocation(ArgLoc),
2578       CGF.EmitCheckSourceLocation(NNAttr->getLocation()),
2579       llvm::ConstantInt::get(CGF.Int32Ty, ArgNo + 1),
2580   };
2581   CGF.EmitCheck(Cond, "nonnull_arg", StaticData, None,
2582                 CodeGenFunction::CRK_Recoverable);
2583 }
2584 
2585 void CodeGenFunction::EmitCallArgs(CallArgList &Args,
2586                                    ArrayRef<QualType> ArgTypes,
2587                                    CallExpr::const_arg_iterator ArgBeg,
2588                                    CallExpr::const_arg_iterator ArgEnd,
2589                                    const FunctionDecl *CalleeDecl,
2590                                    unsigned ParamsToSkip,
2591                                    bool ForceColumnInfo) {
2592   CGDebugInfo *DI = getDebugInfo();
2593   SourceLocation CallLoc;
2594   if (DI) CallLoc = DI->getLocation();
2595 
2596   // We *have* to evaluate arguments from right to left in the MS C++ ABI,
2597   // because arguments are destroyed left to right in the callee.
2598   if (CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2599     // Insert a stack save if we're going to need any inalloca args.
2600     bool HasInAllocaArgs = false;
2601     for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end();
2602          I != E && !HasInAllocaArgs; ++I)
2603       HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I);
2604     if (HasInAllocaArgs) {
2605       assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
2606       Args.allocateArgumentMemory(*this);
2607     }
2608 
2609     // Evaluate each argument.
2610     size_t CallArgsStart = Args.size();
2611     for (int I = ArgTypes.size() - 1; I >= 0; --I) {
2612       CallExpr::const_arg_iterator Arg = ArgBeg + I;
2613       EmitCallArg(Args, *Arg, ArgTypes[I]);
2614       emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(),
2615                           CalleeDecl, ParamsToSkip + I);
2616       // Restore the debug location.
2617       if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo);
2618     }
2619 
2620     // Un-reverse the arguments we just evaluated so they match up with the LLVM
2621     // IR function.
2622     std::reverse(Args.begin() + CallArgsStart, Args.end());
2623     return;
2624   }
2625 
2626   for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
2627     CallExpr::const_arg_iterator Arg = ArgBeg + I;
2628     assert(Arg != ArgEnd);
2629     EmitCallArg(Args, *Arg, ArgTypes[I]);
2630     emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(),
2631                         CalleeDecl, ParamsToSkip + I);
2632     // Restore the debug location.
2633     if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo);
2634   }
2635 }
2636 
2637 namespace {
2638 
2639 struct DestroyUnpassedArg : EHScopeStack::Cleanup {
2640   DestroyUnpassedArg(llvm::Value *Addr, QualType Ty)
2641       : Addr(Addr), Ty(Ty) {}
2642 
2643   llvm::Value *Addr;
2644   QualType Ty;
2645 
2646   void Emit(CodeGenFunction &CGF, Flags flags) override {
2647     const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
2648     assert(!Dtor->isTrivial());
2649     CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
2650                               /*Delegating=*/false, Addr);
2651   }
2652 };
2653 
2654 }
2655 
2656 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
2657                                   QualType type) {
2658   if (const ObjCIndirectCopyRestoreExpr *CRE
2659         = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
2660     assert(getLangOpts().ObjCAutoRefCount);
2661     assert(getContext().hasSameType(E->getType(), type));
2662     return emitWritebackArg(*this, args, CRE);
2663   }
2664 
2665   assert(type->isReferenceType() == E->isGLValue() &&
2666          "reference binding to unmaterialized r-value!");
2667 
2668   if (E->isGLValue()) {
2669     assert(E->getObjectKind() == OK_Ordinary);
2670     return args.add(EmitReferenceBindingToExpr(E), type);
2671   }
2672 
2673   bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
2674 
2675   // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
2676   // However, we still have to push an EH-only cleanup in case we unwind before
2677   // we make it to the call.
2678   if (HasAggregateEvalKind &&
2679       CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2680     // If we're using inalloca, use the argument memory.  Otherwise, use a
2681     // temporary.
2682     AggValueSlot Slot;
2683     if (args.isUsingInAlloca())
2684       Slot = createPlaceholderSlot(*this, type);
2685     else
2686       Slot = CreateAggTemp(type, "agg.tmp");
2687 
2688     const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
2689     bool DestroyedInCallee =
2690         RD && RD->hasNonTrivialDestructor() &&
2691         CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default;
2692     if (DestroyedInCallee)
2693       Slot.setExternallyDestructed();
2694 
2695     EmitAggExpr(E, Slot);
2696     RValue RV = Slot.asRValue();
2697     args.add(RV, type);
2698 
2699     if (DestroyedInCallee) {
2700       // Create a no-op GEP between the placeholder and the cleanup so we can
2701       // RAUW it successfully.  It also serves as a marker of the first
2702       // instruction where the cleanup is active.
2703       pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddr(), type);
2704       // This unreachable is a temporary marker which will be removed later.
2705       llvm::Instruction *IsActive = Builder.CreateUnreachable();
2706       args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive);
2707     }
2708     return;
2709   }
2710 
2711   if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
2712       cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
2713     LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
2714     assert(L.isSimple());
2715     if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) {
2716       args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
2717     } else {
2718       // We can't represent a misaligned lvalue in the CallArgList, so copy
2719       // to an aligned temporary now.
2720       llvm::Value *tmp = CreateMemTemp(type);
2721       EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(),
2722                         L.getAlignment());
2723       args.add(RValue::getAggregate(tmp), type);
2724     }
2725     return;
2726   }
2727 
2728   args.add(EmitAnyExprToTemp(E), type);
2729 }
2730 
2731 QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
2732   // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
2733   // implicitly widens null pointer constants that are arguments to varargs
2734   // functions to pointer-sized ints.
2735   if (!getTarget().getTriple().isOSWindows())
2736     return Arg->getType();
2737 
2738   if (Arg->getType()->isIntegerType() &&
2739       getContext().getTypeSize(Arg->getType()) <
2740           getContext().getTargetInfo().getPointerWidth(0) &&
2741       Arg->isNullPointerConstant(getContext(),
2742                                  Expr::NPC_ValueDependentIsNotNull)) {
2743     return getContext().getIntPtrType();
2744   }
2745 
2746   return Arg->getType();
2747 }
2748 
2749 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2750 // optimizer it can aggressively ignore unwind edges.
2751 void
2752 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
2753   if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
2754       !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
2755     Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
2756                       CGM.getNoObjCARCExceptionsMetadata());
2757 }
2758 
2759 /// Emits a call to the given no-arguments nounwind runtime function.
2760 llvm::CallInst *
2761 CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2762                                          const llvm::Twine &name) {
2763   return EmitNounwindRuntimeCall(callee, None, name);
2764 }
2765 
2766 /// Emits a call to the given nounwind runtime function.
2767 llvm::CallInst *
2768 CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2769                                          ArrayRef<llvm::Value*> args,
2770                                          const llvm::Twine &name) {
2771   llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
2772   call->setDoesNotThrow();
2773   return call;
2774 }
2775 
2776 /// Emits a simple call (never an invoke) to the given no-arguments
2777 /// runtime function.
2778 llvm::CallInst *
2779 CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2780                                  const llvm::Twine &name) {
2781   return EmitRuntimeCall(callee, None, name);
2782 }
2783 
2784 /// Emits a simple call (never an invoke) to the given runtime
2785 /// function.
2786 llvm::CallInst *
2787 CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2788                                  ArrayRef<llvm::Value*> args,
2789                                  const llvm::Twine &name) {
2790   llvm::CallInst *call = Builder.CreateCall(callee, args, name);
2791   call->setCallingConv(getRuntimeCC());
2792   return call;
2793 }
2794 
2795 /// Emits a call or invoke to the given noreturn runtime function.
2796 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2797                                                ArrayRef<llvm::Value*> args) {
2798   if (getInvokeDest()) {
2799     llvm::InvokeInst *invoke =
2800       Builder.CreateInvoke(callee,
2801                            getUnreachableBlock(),
2802                            getInvokeDest(),
2803                            args);
2804     invoke->setDoesNotReturn();
2805     invoke->setCallingConv(getRuntimeCC());
2806   } else {
2807     llvm::CallInst *call = Builder.CreateCall(callee, args);
2808     call->setDoesNotReturn();
2809     call->setCallingConv(getRuntimeCC());
2810     Builder.CreateUnreachable();
2811   }
2812   PGO.setCurrentRegionUnreachable();
2813 }
2814 
2815 /// Emits a call or invoke instruction to the given nullary runtime
2816 /// function.
2817 llvm::CallSite
2818 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2819                                          const Twine &name) {
2820   return EmitRuntimeCallOrInvoke(callee, None, name);
2821 }
2822 
2823 /// Emits a call or invoke instruction to the given runtime function.
2824 llvm::CallSite
2825 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2826                                          ArrayRef<llvm::Value*> args,
2827                                          const Twine &name) {
2828   llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
2829   callSite.setCallingConv(getRuntimeCC());
2830   return callSite;
2831 }
2832 
2833 llvm::CallSite
2834 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
2835                                   const Twine &Name) {
2836   return EmitCallOrInvoke(Callee, None, Name);
2837 }
2838 
2839 /// Emits a call or invoke instruction to the given function, depending
2840 /// on the current state of the EH stack.
2841 llvm::CallSite
2842 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
2843                                   ArrayRef<llvm::Value *> Args,
2844                                   const Twine &Name) {
2845   llvm::BasicBlock *InvokeDest = getInvokeDest();
2846 
2847   llvm::Instruction *Inst;
2848   if (!InvokeDest)
2849     Inst = Builder.CreateCall(Callee, Args, Name);
2850   else {
2851     llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
2852     Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name);
2853     EmitBlock(ContBB);
2854   }
2855 
2856   // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2857   // optimizer it can aggressively ignore unwind edges.
2858   if (CGM.getLangOpts().ObjCAutoRefCount)
2859     AddObjCARCExceptionMetadata(Inst);
2860 
2861   return Inst;
2862 }
2863 
2864 /// \brief Store a non-aggregate value to an address to initialize it.  For
2865 /// initialization, a non-atomic store will be used.
2866 static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src,
2867                                         LValue Dst) {
2868   if (Src.isScalar())
2869     CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true);
2870   else
2871     CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true);
2872 }
2873 
2874 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
2875                                                   llvm::Value *New) {
2876   DeferredReplacements.push_back(std::make_pair(Old, New));
2877 }
2878 
2879 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2880                                  llvm::Value *Callee,
2881                                  ReturnValueSlot ReturnValue,
2882                                  const CallArgList &CallArgs,
2883                                  const Decl *TargetDecl,
2884                                  llvm::Instruction **callOrInvoke) {
2885   // FIXME: We no longer need the types from CallArgs; lift up and simplify.
2886 
2887   // Handle struct-return functions by passing a pointer to the
2888   // location that we would like to return into.
2889   QualType RetTy = CallInfo.getReturnType();
2890   const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
2891 
2892   llvm::FunctionType *IRFuncTy =
2893     cast<llvm::FunctionType>(
2894                   cast<llvm::PointerType>(Callee->getType())->getElementType());
2895 
2896   // If we're using inalloca, insert the allocation after the stack save.
2897   // FIXME: Do this earlier rather than hacking it in here!
2898   llvm::Value *ArgMemory = nullptr;
2899   if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
2900     llvm::Instruction *IP = CallArgs.getStackBase();
2901     llvm::AllocaInst *AI;
2902     if (IP) {
2903       IP = IP->getNextNode();
2904       AI = new llvm::AllocaInst(ArgStruct, "argmem", IP);
2905     } else {
2906       AI = CreateTempAlloca(ArgStruct, "argmem");
2907     }
2908     AI->setUsedWithInAlloca(true);
2909     assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
2910     ArgMemory = AI;
2911   }
2912 
2913   ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
2914   SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
2915 
2916   // If the call returns a temporary with struct return, create a temporary
2917   // alloca to hold the result, unless one is given to us.
2918   llvm::Value *SRetPtr = nullptr;
2919   if (RetAI.isIndirect() || RetAI.isInAlloca()) {
2920     SRetPtr = ReturnValue.getValue();
2921     if (!SRetPtr)
2922       SRetPtr = CreateMemTemp(RetTy);
2923     if (IRFunctionArgs.hasSRetArg()) {
2924       IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr;
2925     } else {
2926       llvm::Value *Addr =
2927           Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
2928       Builder.CreateStore(SRetPtr, Addr);
2929     }
2930   }
2931 
2932   assert(CallInfo.arg_size() == CallArgs.size() &&
2933          "Mismatch between function signature & arguments.");
2934   unsigned ArgNo = 0;
2935   CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
2936   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
2937        I != E; ++I, ++info_it, ++ArgNo) {
2938     const ABIArgInfo &ArgInfo = info_it->info;
2939     RValue RV = I->RV;
2940 
2941     CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty);
2942 
2943     // Insert a padding argument to ensure proper alignment.
2944     if (IRFunctionArgs.hasPaddingArg(ArgNo))
2945       IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
2946           llvm::UndefValue::get(ArgInfo.getPaddingType());
2947 
2948     unsigned FirstIRArg, NumIRArgs;
2949     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2950 
2951     switch (ArgInfo.getKind()) {
2952     case ABIArgInfo::InAlloca: {
2953       assert(NumIRArgs == 0);
2954       assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
2955       if (RV.isAggregate()) {
2956         // Replace the placeholder with the appropriate argument slot GEP.
2957         llvm::Instruction *Placeholder =
2958             cast<llvm::Instruction>(RV.getAggregateAddr());
2959         CGBuilderTy::InsertPoint IP = Builder.saveIP();
2960         Builder.SetInsertPoint(Placeholder);
2961         llvm::Value *Addr = Builder.CreateStructGEP(
2962             ArgMemory, ArgInfo.getInAllocaFieldIndex());
2963         Builder.restoreIP(IP);
2964         deferPlaceholderReplacement(Placeholder, Addr);
2965       } else {
2966         // Store the RValue into the argument struct.
2967         llvm::Value *Addr =
2968             Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
2969         unsigned AS = Addr->getType()->getPointerAddressSpace();
2970         llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS);
2971         // There are some cases where a trivial bitcast is not avoidable.  The
2972         // definition of a type later in a translation unit may change it's type
2973         // from {}* to (%struct.foo*)*.
2974         if (Addr->getType() != MemType)
2975           Addr = Builder.CreateBitCast(Addr, MemType);
2976         LValue argLV = MakeAddrLValue(Addr, I->Ty, TypeAlign);
2977         EmitInitStoreOfNonAggregate(*this, RV, argLV);
2978       }
2979       break;
2980     }
2981 
2982     case ABIArgInfo::Indirect: {
2983       assert(NumIRArgs == 1);
2984       if (RV.isScalar() || RV.isComplex()) {
2985         // Make a temporary alloca to pass the argument.
2986         llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
2987         if (ArgInfo.getIndirectAlign() > AI->getAlignment())
2988           AI->setAlignment(ArgInfo.getIndirectAlign());
2989         IRCallArgs[FirstIRArg] = AI;
2990 
2991         LValue argLV = MakeAddrLValue(AI, I->Ty, TypeAlign);
2992         EmitInitStoreOfNonAggregate(*this, RV, argLV);
2993       } else {
2994         // We want to avoid creating an unnecessary temporary+copy here;
2995         // however, we need one in three cases:
2996         // 1. If the argument is not byval, and we are required to copy the
2997         //    source.  (This case doesn't occur on any common architecture.)
2998         // 2. If the argument is byval, RV is not sufficiently aligned, and
2999         //    we cannot force it to be sufficiently aligned.
3000         // 3. If the argument is byval, but RV is located in an address space
3001         //    different than that of the argument (0).
3002         llvm::Value *Addr = RV.getAggregateAddr();
3003         unsigned Align = ArgInfo.getIndirectAlign();
3004         const llvm::DataLayout *TD = &CGM.getDataLayout();
3005         const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace();
3006         const unsigned ArgAddrSpace =
3007             (FirstIRArg < IRFuncTy->getNumParams()
3008                  ? IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace()
3009                  : 0);
3010         if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
3011             (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align &&
3012              llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) ||
3013              (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) {
3014           // Create an aligned temporary, and copy to it.
3015           llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
3016           if (Align > AI->getAlignment())
3017             AI->setAlignment(Align);
3018           IRCallArgs[FirstIRArg] = AI;
3019           EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
3020         } else {
3021           // Skip the extra memcpy call.
3022           IRCallArgs[FirstIRArg] = Addr;
3023         }
3024       }
3025       break;
3026     }
3027 
3028     case ABIArgInfo::Ignore:
3029       assert(NumIRArgs == 0);
3030       break;
3031 
3032     case ABIArgInfo::Extend:
3033     case ABIArgInfo::Direct: {
3034       if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
3035           ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
3036           ArgInfo.getDirectOffset() == 0) {
3037         assert(NumIRArgs == 1);
3038         llvm::Value *V;
3039         if (RV.isScalar())
3040           V = RV.getScalarVal();
3041         else
3042           V = Builder.CreateLoad(RV.getAggregateAddr());
3043 
3044         // We might have to widen integers, but we should never truncate.
3045         if (ArgInfo.getCoerceToType() != V->getType() &&
3046             V->getType()->isIntegerTy())
3047           V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
3048 
3049         // If the argument doesn't match, perform a bitcast to coerce it.  This
3050         // can happen due to trivial type mismatches.
3051         if (FirstIRArg < IRFuncTy->getNumParams() &&
3052             V->getType() != IRFuncTy->getParamType(FirstIRArg))
3053           V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
3054         IRCallArgs[FirstIRArg] = V;
3055         break;
3056       }
3057 
3058       // FIXME: Avoid the conversion through memory if possible.
3059       llvm::Value *SrcPtr;
3060       if (RV.isScalar() || RV.isComplex()) {
3061         SrcPtr = CreateMemTemp(I->Ty, "coerce");
3062         LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign);
3063         EmitInitStoreOfNonAggregate(*this, RV, SrcLV);
3064       } else
3065         SrcPtr = RV.getAggregateAddr();
3066 
3067       // If the value is offset in memory, apply the offset now.
3068       if (unsigned Offs = ArgInfo.getDirectOffset()) {
3069         SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
3070         SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
3071         SrcPtr = Builder.CreateBitCast(SrcPtr,
3072                        llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
3073 
3074       }
3075 
3076       // Fast-isel and the optimizer generally like scalar values better than
3077       // FCAs, so we flatten them if this is safe to do for this argument.
3078       llvm::StructType *STy =
3079             dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
3080       if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
3081         llvm::Type *SrcTy =
3082           cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
3083         uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
3084         uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
3085 
3086         // If the source type is smaller than the destination type of the
3087         // coerce-to logic, copy the source value into a temp alloca the size
3088         // of the destination type to allow loading all of it. The bits past
3089         // the source value are left undef.
3090         if (SrcSize < DstSize) {
3091           llvm::AllocaInst *TempAlloca
3092             = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce");
3093           Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0);
3094           SrcPtr = TempAlloca;
3095         } else {
3096           SrcPtr = Builder.CreateBitCast(SrcPtr,
3097                                          llvm::PointerType::getUnqual(STy));
3098         }
3099 
3100         assert(NumIRArgs == STy->getNumElements());
3101         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3102           llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
3103           llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
3104           // We don't know what we're loading from.
3105           LI->setAlignment(1);
3106           IRCallArgs[FirstIRArg + i] = LI;
3107         }
3108       } else {
3109         // In the simple case, just pass the coerced loaded value.
3110         assert(NumIRArgs == 1);
3111         IRCallArgs[FirstIRArg] =
3112             CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), *this);
3113       }
3114 
3115       break;
3116     }
3117 
3118     case ABIArgInfo::Expand:
3119       unsigned IRArgPos = FirstIRArg;
3120       ExpandTypeToArgs(I->Ty, RV, IRFuncTy, IRCallArgs, IRArgPos);
3121       assert(IRArgPos == FirstIRArg + NumIRArgs);
3122       break;
3123     }
3124   }
3125 
3126   if (ArgMemory) {
3127     llvm::Value *Arg = ArgMemory;
3128     if (CallInfo.isVariadic()) {
3129       // When passing non-POD arguments by value to variadic functions, we will
3130       // end up with a variadic prototype and an inalloca call site.  In such
3131       // cases, we can't do any parameter mismatch checks.  Give up and bitcast
3132       // the callee.
3133       unsigned CalleeAS =
3134           cast<llvm::PointerType>(Callee->getType())->getAddressSpace();
3135       Callee = Builder.CreateBitCast(
3136           Callee, getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS));
3137     } else {
3138       llvm::Type *LastParamTy =
3139           IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
3140       if (Arg->getType() != LastParamTy) {
3141 #ifndef NDEBUG
3142         // Assert that these structs have equivalent element types.
3143         llvm::StructType *FullTy = CallInfo.getArgStruct();
3144         llvm::StructType *DeclaredTy = cast<llvm::StructType>(
3145             cast<llvm::PointerType>(LastParamTy)->getElementType());
3146         assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
3147         for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(),
3148                                                 DE = DeclaredTy->element_end(),
3149                                                 FI = FullTy->element_begin();
3150              DI != DE; ++DI, ++FI)
3151           assert(*DI == *FI);
3152 #endif
3153         Arg = Builder.CreateBitCast(Arg, LastParamTy);
3154       }
3155     }
3156     assert(IRFunctionArgs.hasInallocaArg());
3157     IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
3158   }
3159 
3160   if (!CallArgs.getCleanupsToDeactivate().empty())
3161     deactivateArgCleanupsBeforeCall(*this, CallArgs);
3162 
3163   // If the callee is a bitcast of a function to a varargs pointer to function
3164   // type, check to see if we can remove the bitcast.  This handles some cases
3165   // with unprototyped functions.
3166   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
3167     if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
3168       llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
3169       llvm::FunctionType *CurFT =
3170         cast<llvm::FunctionType>(CurPT->getElementType());
3171       llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
3172 
3173       if (CE->getOpcode() == llvm::Instruction::BitCast &&
3174           ActualFT->getReturnType() == CurFT->getReturnType() &&
3175           ActualFT->getNumParams() == CurFT->getNumParams() &&
3176           ActualFT->getNumParams() == IRCallArgs.size() &&
3177           (CurFT->isVarArg() || !ActualFT->isVarArg())) {
3178         bool ArgsMatch = true;
3179         for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
3180           if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
3181             ArgsMatch = false;
3182             break;
3183           }
3184 
3185         // Strip the cast if we can get away with it.  This is a nice cleanup,
3186         // but also allows us to inline the function at -O0 if it is marked
3187         // always_inline.
3188         if (ArgsMatch)
3189           Callee = CalleeF;
3190       }
3191     }
3192 
3193   assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
3194   for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
3195     // Inalloca argument can have different type.
3196     if (IRFunctionArgs.hasInallocaArg() &&
3197         i == IRFunctionArgs.getInallocaArgNo())
3198       continue;
3199     if (i < IRFuncTy->getNumParams())
3200       assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
3201   }
3202 
3203   unsigned CallingConv;
3204   CodeGen::AttributeListType AttributeList;
3205   CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList,
3206                              CallingConv, true);
3207   llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(),
3208                                                      AttributeList);
3209 
3210   llvm::BasicBlock *InvokeDest = nullptr;
3211   if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex,
3212                           llvm::Attribute::NoUnwind))
3213     InvokeDest = getInvokeDest();
3214 
3215   llvm::CallSite CS;
3216   if (!InvokeDest) {
3217     CS = Builder.CreateCall(Callee, IRCallArgs);
3218   } else {
3219     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
3220     CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, IRCallArgs);
3221     EmitBlock(Cont);
3222   }
3223   if (callOrInvoke)
3224     *callOrInvoke = CS.getInstruction();
3225 
3226   if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
3227       !CS.hasFnAttr(llvm::Attribute::NoInline))
3228     Attrs =
3229         Attrs.addAttribute(getLLVMContext(), llvm::AttributeSet::FunctionIndex,
3230                            llvm::Attribute::AlwaysInline);
3231 
3232   CS.setAttributes(Attrs);
3233   CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
3234 
3235   // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
3236   // optimizer it can aggressively ignore unwind edges.
3237   if (CGM.getLangOpts().ObjCAutoRefCount)
3238     AddObjCARCExceptionMetadata(CS.getInstruction());
3239 
3240   // If the call doesn't return, finish the basic block and clear the
3241   // insertion point; this allows the rest of IRgen to discard
3242   // unreachable code.
3243   if (CS.doesNotReturn()) {
3244     Builder.CreateUnreachable();
3245     Builder.ClearInsertionPoint();
3246 
3247     // FIXME: For now, emit a dummy basic block because expr emitters in
3248     // generally are not ready to handle emitting expressions at unreachable
3249     // points.
3250     EnsureInsertPoint();
3251 
3252     // Return a reasonable RValue.
3253     return GetUndefRValue(RetTy);
3254   }
3255 
3256   llvm::Instruction *CI = CS.getInstruction();
3257   if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
3258     CI->setName("call");
3259 
3260   // Emit any writebacks immediately.  Arguably this should happen
3261   // after any return-value munging.
3262   if (CallArgs.hasWritebacks())
3263     emitWritebacks(*this, CallArgs);
3264 
3265   // The stack cleanup for inalloca arguments has to run out of the normal
3266   // lexical order, so deactivate it and run it manually here.
3267   CallArgs.freeArgumentMemory(*this);
3268 
3269   RValue Ret = [&] {
3270     switch (RetAI.getKind()) {
3271     case ABIArgInfo::InAlloca:
3272     case ABIArgInfo::Indirect:
3273       return convertTempToRValue(SRetPtr, RetTy, SourceLocation());
3274 
3275     case ABIArgInfo::Ignore:
3276       // If we are ignoring an argument that had a result, make sure to
3277       // construct the appropriate return value for our caller.
3278       return GetUndefRValue(RetTy);
3279 
3280     case ABIArgInfo::Extend:
3281     case ABIArgInfo::Direct: {
3282       llvm::Type *RetIRTy = ConvertType(RetTy);
3283       if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
3284         switch (getEvaluationKind(RetTy)) {
3285         case TEK_Complex: {
3286           llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
3287           llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
3288           return RValue::getComplex(std::make_pair(Real, Imag));
3289         }
3290         case TEK_Aggregate: {
3291           llvm::Value *DestPtr = ReturnValue.getValue();
3292           bool DestIsVolatile = ReturnValue.isVolatile();
3293 
3294           if (!DestPtr) {
3295             DestPtr = CreateMemTemp(RetTy, "agg.tmp");
3296             DestIsVolatile = false;
3297           }
3298           BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
3299           return RValue::getAggregate(DestPtr);
3300         }
3301         case TEK_Scalar: {
3302           // If the argument doesn't match, perform a bitcast to coerce it.  This
3303           // can happen due to trivial type mismatches.
3304           llvm::Value *V = CI;
3305           if (V->getType() != RetIRTy)
3306             V = Builder.CreateBitCast(V, RetIRTy);
3307           return RValue::get(V);
3308         }
3309         }
3310         llvm_unreachable("bad evaluation kind");
3311       }
3312 
3313       llvm::Value *DestPtr = ReturnValue.getValue();
3314       bool DestIsVolatile = ReturnValue.isVolatile();
3315 
3316       if (!DestPtr) {
3317         DestPtr = CreateMemTemp(RetTy, "coerce");
3318         DestIsVolatile = false;
3319       }
3320 
3321       // If the value is offset in memory, apply the offset now.
3322       llvm::Value *StorePtr = DestPtr;
3323       if (unsigned Offs = RetAI.getDirectOffset()) {
3324         StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
3325         StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
3326         StorePtr = Builder.CreateBitCast(StorePtr,
3327                            llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
3328       }
3329       CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
3330 
3331       return convertTempToRValue(DestPtr, RetTy, SourceLocation());
3332     }
3333 
3334     case ABIArgInfo::Expand:
3335       llvm_unreachable("Invalid ABI kind for return argument");
3336     }
3337 
3338     llvm_unreachable("Unhandled ABIArgInfo::Kind");
3339   } ();
3340 
3341   if (Ret.isScalar() && TargetDecl) {
3342     if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) {
3343       llvm::Value *OffsetValue = nullptr;
3344       if (const auto *Offset = AA->getOffset())
3345         OffsetValue = EmitScalarExpr(Offset);
3346 
3347       llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment());
3348       llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment);
3349       EmitAlignmentAssumption(Ret.getScalarVal(), AlignmentCI->getZExtValue(),
3350                               OffsetValue);
3351     }
3352   }
3353 
3354   return Ret;
3355 }
3356 
3357 /* VarArg handling */
3358 
3359 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
3360   return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
3361 }
3362