xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGCall.cpp (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1 //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGCall.h"
15 #include "ABIInfo.h"
16 #include "ABIInfoImpl.h"
17 #include "CGBlocks.h"
18 #include "CGCXXABI.h"
19 #include "CGCleanup.h"
20 #include "CGRecordLayout.h"
21 #include "CodeGenFunction.h"
22 #include "CodeGenModule.h"
23 #include "TargetInfo.h"
24 #include "clang/AST/Attr.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/AST/DeclCXX.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/Basic/CodeGenOptions.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/CodeGen/CGFunctionInfo.h"
31 #include "clang/CodeGen/SwiftCallingConv.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/Assumptions.h"
35 #include "llvm/IR/AttributeMask.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/CallingConv.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/InlineAsm.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Intrinsics.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include <optional>
45 using namespace clang;
46 using namespace CodeGen;
47 
48 /***/
49 
50 unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
51   switch (CC) {
52   default: return llvm::CallingConv::C;
53   case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
54   case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
55   case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;
56   case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
57   case CC_Win64: return llvm::CallingConv::Win64;
58   case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;
59   case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
60   case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
61   case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
62   // TODO: Add support for __pascal to LLVM.
63   case CC_X86Pascal: return llvm::CallingConv::C;
64   // TODO: Add support for __vectorcall to LLVM.
65   case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;
66   case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_VectorCall;
67   case CC_AArch64SVEPCS: return llvm::CallingConv::AArch64_SVE_VectorCall;
68   case CC_AMDGPUKernelCall: return llvm::CallingConv::AMDGPU_KERNEL;
69   case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;
70   case CC_OpenCLKernel: return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
71   case CC_PreserveMost: return llvm::CallingConv::PreserveMost;
72   case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
73   case CC_Swift: return llvm::CallingConv::Swift;
74   case CC_SwiftAsync: return llvm::CallingConv::SwiftTail;
75   }
76 }
77 
78 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
79 /// qualification. Either or both of RD and MD may be null. A null RD indicates
80 /// that there is no meaningful 'this' type, and a null MD can occur when
81 /// calling a method pointer.
82 CanQualType CodeGenTypes::DeriveThisType(const CXXRecordDecl *RD,
83                                          const CXXMethodDecl *MD) {
84   QualType RecTy;
85   if (RD)
86     RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
87   else
88     RecTy = Context.VoidTy;
89 
90   if (MD)
91     RecTy = Context.getAddrSpaceQualType(RecTy, MD->getMethodQualifiers().getAddressSpace());
92   return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
93 }
94 
95 /// Returns the canonical formal type of the given C++ method.
96 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
97   return MD->getType()->getCanonicalTypeUnqualified()
98            .getAs<FunctionProtoType>();
99 }
100 
101 /// Returns the "extra-canonicalized" return type, which discards
102 /// qualifiers on the return type.  Codegen doesn't care about them,
103 /// and it makes ABI code a little easier to be able to assume that
104 /// all parameter and return types are top-level unqualified.
105 static CanQualType GetReturnType(QualType RetTy) {
106   return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
107 }
108 
109 /// Arrange the argument and result information for a value of the given
110 /// unprototyped freestanding function type.
111 const CGFunctionInfo &
112 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
113   // When translating an unprototyped function type, always use a
114   // variadic type.
115   return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
116                                  FnInfoOpts::None, std::nullopt,
117                                  FTNP->getExtInfo(), {}, RequiredArgs(0));
118 }
119 
120 static void addExtParameterInfosForCall(
121          llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
122                                         const FunctionProtoType *proto,
123                                         unsigned prefixArgs,
124                                         unsigned totalArgs) {
125   assert(proto->hasExtParameterInfos());
126   assert(paramInfos.size() <= prefixArgs);
127   assert(proto->getNumParams() + prefixArgs <= totalArgs);
128 
129   paramInfos.reserve(totalArgs);
130 
131   // Add default infos for any prefix args that don't already have infos.
132   paramInfos.resize(prefixArgs);
133 
134   // Add infos for the prototype.
135   for (const auto &ParamInfo : proto->getExtParameterInfos()) {
136     paramInfos.push_back(ParamInfo);
137     // pass_object_size params have no parameter info.
138     if (ParamInfo.hasPassObjectSize())
139       paramInfos.emplace_back();
140   }
141 
142   assert(paramInfos.size() <= totalArgs &&
143          "Did we forget to insert pass_object_size args?");
144   // Add default infos for the variadic and/or suffix arguments.
145   paramInfos.resize(totalArgs);
146 }
147 
148 /// Adds the formal parameters in FPT to the given prefix. If any parameter in
149 /// FPT has pass_object_size attrs, then we'll add parameters for those, too.
150 static void appendParameterTypes(const CodeGenTypes &CGT,
151                                  SmallVectorImpl<CanQualType> &prefix,
152               SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &paramInfos,
153                                  CanQual<FunctionProtoType> FPT) {
154   // Fast path: don't touch param info if we don't need to.
155   if (!FPT->hasExtParameterInfos()) {
156     assert(paramInfos.empty() &&
157            "We have paramInfos, but the prototype doesn't?");
158     prefix.append(FPT->param_type_begin(), FPT->param_type_end());
159     return;
160   }
161 
162   unsigned PrefixSize = prefix.size();
163   // In the vast majority of cases, we'll have precisely FPT->getNumParams()
164   // parameters; the only thing that can change this is the presence of
165   // pass_object_size. So, we preallocate for the common case.
166   prefix.reserve(prefix.size() + FPT->getNumParams());
167 
168   auto ExtInfos = FPT->getExtParameterInfos();
169   assert(ExtInfos.size() == FPT->getNumParams());
170   for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
171     prefix.push_back(FPT->getParamType(I));
172     if (ExtInfos[I].hasPassObjectSize())
173       prefix.push_back(CGT.getContext().getSizeType());
174   }
175 
176   addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,
177                               prefix.size());
178 }
179 
180 /// Arrange the LLVM function layout for a value of the given function
181 /// type, on top of any implicit parameters already stored.
182 static const CGFunctionInfo &
183 arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
184                         SmallVectorImpl<CanQualType> &prefix,
185                         CanQual<FunctionProtoType> FTP) {
186   SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
187   RequiredArgs Required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
188   // FIXME: Kill copy.
189   appendParameterTypes(CGT, prefix, paramInfos, FTP);
190   CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
191 
192   FnInfoOpts opts =
193       instanceMethod ? FnInfoOpts::IsInstanceMethod : FnInfoOpts::None;
194   return CGT.arrangeLLVMFunctionInfo(resultType, opts, prefix,
195                                      FTP->getExtInfo(), paramInfos, Required);
196 }
197 
198 /// Arrange the argument and result information for a value of the
199 /// given freestanding function type.
200 const CGFunctionInfo &
201 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
202   SmallVector<CanQualType, 16> argTypes;
203   return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
204                                    FTP);
205 }
206 
207 static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
208                                                bool IsWindows) {
209   // Set the appropriate calling convention for the Function.
210   if (D->hasAttr<StdCallAttr>())
211     return CC_X86StdCall;
212 
213   if (D->hasAttr<FastCallAttr>())
214     return CC_X86FastCall;
215 
216   if (D->hasAttr<RegCallAttr>())
217     return CC_X86RegCall;
218 
219   if (D->hasAttr<ThisCallAttr>())
220     return CC_X86ThisCall;
221 
222   if (D->hasAttr<VectorCallAttr>())
223     return CC_X86VectorCall;
224 
225   if (D->hasAttr<PascalAttr>())
226     return CC_X86Pascal;
227 
228   if (PcsAttr *PCS = D->getAttr<PcsAttr>())
229     return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
230 
231   if (D->hasAttr<AArch64VectorPcsAttr>())
232     return CC_AArch64VectorCall;
233 
234   if (D->hasAttr<AArch64SVEPcsAttr>())
235     return CC_AArch64SVEPCS;
236 
237   if (D->hasAttr<AMDGPUKernelCallAttr>())
238     return CC_AMDGPUKernelCall;
239 
240   if (D->hasAttr<IntelOclBiccAttr>())
241     return CC_IntelOclBicc;
242 
243   if (D->hasAttr<MSABIAttr>())
244     return IsWindows ? CC_C : CC_Win64;
245 
246   if (D->hasAttr<SysVABIAttr>())
247     return IsWindows ? CC_X86_64SysV : CC_C;
248 
249   if (D->hasAttr<PreserveMostAttr>())
250     return CC_PreserveMost;
251 
252   if (D->hasAttr<PreserveAllAttr>())
253     return CC_PreserveAll;
254 
255   return CC_C;
256 }
257 
258 /// Arrange the argument and result information for a call to an
259 /// unknown C++ non-static member function of the given abstract type.
260 /// (A null RD means we don't have any meaningful "this" argument type,
261 ///  so fall back to a generic pointer type).
262 /// The member function must be an ordinary function, i.e. not a
263 /// constructor or destructor.
264 const CGFunctionInfo &
265 CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
266                                    const FunctionProtoType *FTP,
267                                    const CXXMethodDecl *MD) {
268   SmallVector<CanQualType, 16> argTypes;
269 
270   // Add the 'this' pointer.
271   argTypes.push_back(DeriveThisType(RD, MD));
272 
273   return ::arrangeLLVMFunctionInfo(
274       *this, /*instanceMethod=*/true, argTypes,
275       FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
276 }
277 
278 /// Set calling convention for CUDA/HIP kernel.
279 static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM,
280                                            const FunctionDecl *FD) {
281   if (FD->hasAttr<CUDAGlobalAttr>()) {
282     const FunctionType *FT = FTy->getAs<FunctionType>();
283     CGM.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT);
284     FTy = FT->getCanonicalTypeUnqualified();
285   }
286 }
287 
288 /// Arrange the argument and result information for a declaration or
289 /// definition of the given C++ non-static member function.  The
290 /// member function must be an ordinary function, i.e. not a
291 /// constructor or destructor.
292 const CGFunctionInfo &
293 CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
294   assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
295   assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
296 
297   CanQualType FT = GetFormalType(MD).getAs<Type>();
298   setCUDAKernelCallingConvention(FT, CGM, MD);
299   auto prototype = FT.getAs<FunctionProtoType>();
300 
301   if (MD->isInstance()) {
302     // The abstract case is perfectly fine.
303     const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
304     return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
305   }
306 
307   return arrangeFreeFunctionType(prototype);
308 }
309 
310 bool CodeGenTypes::inheritingCtorHasParams(
311     const InheritedConstructor &Inherited, CXXCtorType Type) {
312   // Parameters are unnecessary if we're constructing a base class subobject
313   // and the inherited constructor lives in a virtual base.
314   return Type == Ctor_Complete ||
315          !Inherited.getShadowDecl()->constructsVirtualBase() ||
316          !Target.getCXXABI().hasConstructorVariants();
317 }
318 
319 const CGFunctionInfo &
320 CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
321   auto *MD = cast<CXXMethodDecl>(GD.getDecl());
322 
323   SmallVector<CanQualType, 16> argTypes;
324   SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
325 
326   const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(GD);
327   argTypes.push_back(DeriveThisType(ThisType, MD));
328 
329   bool PassParams = true;
330 
331   if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
332     // A base class inheriting constructor doesn't get forwarded arguments
333     // needed to construct a virtual base (or base class thereof).
334     if (auto Inherited = CD->getInheritedConstructor())
335       PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType());
336   }
337 
338   CanQual<FunctionProtoType> FTP = GetFormalType(MD);
339 
340   // Add the formal parameters.
341   if (PassParams)
342     appendParameterTypes(*this, argTypes, paramInfos, FTP);
343 
344   CGCXXABI::AddedStructorArgCounts AddedArgs =
345       TheCXXABI.buildStructorSignature(GD, argTypes);
346   if (!paramInfos.empty()) {
347     // Note: prefix implies after the first param.
348     if (AddedArgs.Prefix)
349       paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,
350                         FunctionProtoType::ExtParameterInfo{});
351     if (AddedArgs.Suffix)
352       paramInfos.append(AddedArgs.Suffix,
353                         FunctionProtoType::ExtParameterInfo{});
354   }
355 
356   RequiredArgs required =
357       (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
358                                       : RequiredArgs::All);
359 
360   FunctionType::ExtInfo extInfo = FTP->getExtInfo();
361   CanQualType resultType = TheCXXABI.HasThisReturn(GD)
362                                ? argTypes.front()
363                                : TheCXXABI.hasMostDerivedReturn(GD)
364                                      ? CGM.getContext().VoidPtrTy
365                                      : Context.VoidTy;
366   return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,
367                                  argTypes, extInfo, paramInfos, required);
368 }
369 
370 static SmallVector<CanQualType, 16>
371 getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {
372   SmallVector<CanQualType, 16> argTypes;
373   for (auto &arg : args)
374     argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
375   return argTypes;
376 }
377 
378 static SmallVector<CanQualType, 16>
379 getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) {
380   SmallVector<CanQualType, 16> argTypes;
381   for (auto &arg : args)
382     argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
383   return argTypes;
384 }
385 
386 static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16>
387 getExtParameterInfosForCall(const FunctionProtoType *proto,
388                             unsigned prefixArgs, unsigned totalArgs) {
389   llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result;
390   if (proto->hasExtParameterInfos()) {
391     addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
392   }
393   return result;
394 }
395 
396 /// Arrange a call to a C++ method, passing the given arguments.
397 ///
398 /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
399 /// parameter.
400 /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
401 /// args.
402 /// PassProtoArgs indicates whether `args` has args for the parameters in the
403 /// given CXXConstructorDecl.
404 const CGFunctionInfo &
405 CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
406                                         const CXXConstructorDecl *D,
407                                         CXXCtorType CtorKind,
408                                         unsigned ExtraPrefixArgs,
409                                         unsigned ExtraSuffixArgs,
410                                         bool PassProtoArgs) {
411   // FIXME: Kill copy.
412   SmallVector<CanQualType, 16> ArgTypes;
413   for (const auto &Arg : args)
414     ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
415 
416   // +1 for implicit this, which should always be args[0].
417   unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
418 
419   CanQual<FunctionProtoType> FPT = GetFormalType(D);
420   RequiredArgs Required = PassProtoArgs
421                               ? RequiredArgs::forPrototypePlus(
422                                     FPT, TotalPrefixArgs + ExtraSuffixArgs)
423                               : RequiredArgs::All;
424 
425   GlobalDecl GD(D, CtorKind);
426   CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
427                                ? ArgTypes.front()
428                                : TheCXXABI.hasMostDerivedReturn(GD)
429                                      ? CGM.getContext().VoidPtrTy
430                                      : Context.VoidTy;
431 
432   FunctionType::ExtInfo Info = FPT->getExtInfo();
433   llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
434   // If the prototype args are elided, we should only have ABI-specific args,
435   // which never have param info.
436   if (PassProtoArgs && FPT->hasExtParameterInfos()) {
437     // ABI-specific suffix arguments are treated the same as variadic arguments.
438     addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,
439                                 ArgTypes.size());
440   }
441 
442   return arrangeLLVMFunctionInfo(ResultType, FnInfoOpts::IsInstanceMethod,
443                                  ArgTypes, Info, ParamInfos, Required);
444 }
445 
446 /// Arrange the argument and result information for the declaration or
447 /// definition of the given function.
448 const CGFunctionInfo &
449 CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
450   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
451     if (MD->isInstance())
452       return arrangeCXXMethodDeclaration(MD);
453 
454   CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
455 
456   assert(isa<FunctionType>(FTy));
457   setCUDAKernelCallingConvention(FTy, CGM, FD);
458 
459   // When declaring a function without a prototype, always use a
460   // non-variadic type.
461   if (CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>()) {
462     return arrangeLLVMFunctionInfo(noProto->getReturnType(), FnInfoOpts::None,
463                                    std::nullopt, noProto->getExtInfo(), {},
464                                    RequiredArgs::All);
465   }
466 
467   return arrangeFreeFunctionType(FTy.castAs<FunctionProtoType>());
468 }
469 
470 /// Arrange the argument and result information for the declaration or
471 /// definition of an Objective-C method.
472 const CGFunctionInfo &
473 CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
474   // It happens that this is the same as a call with no optional
475   // arguments, except also using the formal 'self' type.
476   return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
477 }
478 
479 /// Arrange the argument and result information for the function type
480 /// through which to perform a send to the given Objective-C method,
481 /// using the given receiver type.  The receiver type is not always
482 /// the 'self' type of the method or even an Objective-C pointer type.
483 /// This is *not* the right method for actually performing such a
484 /// message send, due to the possibility of optional arguments.
485 const CGFunctionInfo &
486 CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
487                                               QualType receiverType) {
488   SmallVector<CanQualType, 16> argTys;
489   SmallVector<FunctionProtoType::ExtParameterInfo, 4> extParamInfos(
490       MD->isDirectMethod() ? 1 : 2);
491   argTys.push_back(Context.getCanonicalParamType(receiverType));
492   if (!MD->isDirectMethod())
493     argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
494   // FIXME: Kill copy?
495   for (const auto *I : MD->parameters()) {
496     argTys.push_back(Context.getCanonicalParamType(I->getType()));
497     auto extParamInfo = FunctionProtoType::ExtParameterInfo().withIsNoEscape(
498         I->hasAttr<NoEscapeAttr>());
499     extParamInfos.push_back(extParamInfo);
500   }
501 
502   FunctionType::ExtInfo einfo;
503   bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
504   einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));
505 
506   if (getContext().getLangOpts().ObjCAutoRefCount &&
507       MD->hasAttr<NSReturnsRetainedAttr>())
508     einfo = einfo.withProducesResult(true);
509 
510   RequiredArgs required =
511     (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
512 
513   return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()),
514                                  FnInfoOpts::None, argTys, einfo, extParamInfos,
515                                  required);
516 }
517 
518 const CGFunctionInfo &
519 CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType,
520                                                  const CallArgList &args) {
521   auto argTypes = getArgTypesForCall(Context, args);
522   FunctionType::ExtInfo einfo;
523 
524   return arrangeLLVMFunctionInfo(GetReturnType(returnType), FnInfoOpts::None,
525                                  argTypes, einfo, {}, RequiredArgs::All);
526 }
527 
528 const CGFunctionInfo &
529 CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
530   // FIXME: Do we need to handle ObjCMethodDecl?
531   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
532 
533   if (isa<CXXConstructorDecl>(GD.getDecl()) ||
534       isa<CXXDestructorDecl>(GD.getDecl()))
535     return arrangeCXXStructorDeclaration(GD);
536 
537   return arrangeFunctionDeclaration(FD);
538 }
539 
540 /// Arrange a thunk that takes 'this' as the first parameter followed by
541 /// varargs.  Return a void pointer, regardless of the actual return type.
542 /// The body of the thunk will end in a musttail call to a function of the
543 /// correct type, and the caller will bitcast the function to the correct
544 /// prototype.
545 const CGFunctionInfo &
546 CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD) {
547   assert(MD->isVirtual() && "only methods have thunks");
548   CanQual<FunctionProtoType> FTP = GetFormalType(MD);
549   CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)};
550   return arrangeLLVMFunctionInfo(Context.VoidTy, FnInfoOpts::None, ArgTys,
551                                  FTP->getExtInfo(), {}, RequiredArgs(1));
552 }
553 
554 const CGFunctionInfo &
555 CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD,
556                                    CXXCtorType CT) {
557   assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
558 
559   CanQual<FunctionProtoType> FTP = GetFormalType(CD);
560   SmallVector<CanQualType, 2> ArgTys;
561   const CXXRecordDecl *RD = CD->getParent();
562   ArgTys.push_back(DeriveThisType(RD, CD));
563   if (CT == Ctor_CopyingClosure)
564     ArgTys.push_back(*FTP->param_type_begin());
565   if (RD->getNumVBases() > 0)
566     ArgTys.push_back(Context.IntTy);
567   CallingConv CC = Context.getDefaultCallingConvention(
568       /*IsVariadic=*/false, /*IsCXXMethod=*/true);
569   return arrangeLLVMFunctionInfo(Context.VoidTy, FnInfoOpts::IsInstanceMethod,
570                                  ArgTys, FunctionType::ExtInfo(CC), {},
571                                  RequiredArgs::All);
572 }
573 
574 /// Arrange a call as unto a free function, except possibly with an
575 /// additional number of formal parameters considered required.
576 static const CGFunctionInfo &
577 arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
578                             CodeGenModule &CGM,
579                             const CallArgList &args,
580                             const FunctionType *fnType,
581                             unsigned numExtraRequiredArgs,
582                             bool chainCall) {
583   assert(args.size() >= numExtraRequiredArgs);
584 
585   llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
586 
587   // In most cases, there are no optional arguments.
588   RequiredArgs required = RequiredArgs::All;
589 
590   // If we have a variadic prototype, the required arguments are the
591   // extra prefix plus the arguments in the prototype.
592   if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
593     if (proto->isVariadic())
594       required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs);
595 
596     if (proto->hasExtParameterInfos())
597       addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
598                                   args.size());
599 
600   // If we don't have a prototype at all, but we're supposed to
601   // explicitly use the variadic convention for unprototyped calls,
602   // treat all of the arguments as required but preserve the nominal
603   // possibility of variadics.
604   } else if (CGM.getTargetCodeGenInfo()
605                 .isNoProtoCallVariadic(args,
606                                        cast<FunctionNoProtoType>(fnType))) {
607     required = RequiredArgs(args.size());
608   }
609 
610   // FIXME: Kill copy.
611   SmallVector<CanQualType, 16> argTypes;
612   for (const auto &arg : args)
613     argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
614   FnInfoOpts opts = chainCall ? FnInfoOpts::IsChainCall : FnInfoOpts::None;
615   return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()),
616                                      opts, argTypes, fnType->getExtInfo(),
617                                      paramInfos, required);
618 }
619 
620 /// Figure out the rules for calling a function with the given formal
621 /// type using the given arguments.  The arguments are necessary
622 /// because the function might be unprototyped, in which case it's
623 /// target-dependent in crazy ways.
624 const CGFunctionInfo &
625 CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
626                                       const FunctionType *fnType,
627                                       bool chainCall) {
628   return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
629                                      chainCall ? 1 : 0, chainCall);
630 }
631 
632 /// A block function is essentially a free function with an
633 /// extra implicit argument.
634 const CGFunctionInfo &
635 CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
636                                        const FunctionType *fnType) {
637   return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
638                                      /*chainCall=*/false);
639 }
640 
641 const CGFunctionInfo &
642 CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto,
643                                               const FunctionArgList &params) {
644   auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());
645   auto argTypes = getArgTypesForDeclaration(Context, params);
646 
647   return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()),
648                                  FnInfoOpts::None, argTypes,
649                                  proto->getExtInfo(), paramInfos,
650                                  RequiredArgs::forPrototypePlus(proto, 1));
651 }
652 
653 const CGFunctionInfo &
654 CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType,
655                                          const CallArgList &args) {
656   // FIXME: Kill copy.
657   SmallVector<CanQualType, 16> argTypes;
658   for (const auto &Arg : args)
659     argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
660   return arrangeLLVMFunctionInfo(GetReturnType(resultType), FnInfoOpts::None,
661                                  argTypes, FunctionType::ExtInfo(),
662                                  /*paramInfos=*/{}, RequiredArgs::All);
663 }
664 
665 const CGFunctionInfo &
666 CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType,
667                                                 const FunctionArgList &args) {
668   auto argTypes = getArgTypesForDeclaration(Context, args);
669 
670   return arrangeLLVMFunctionInfo(GetReturnType(resultType), FnInfoOpts::None,
671                                  argTypes, FunctionType::ExtInfo(), {},
672                                  RequiredArgs::All);
673 }
674 
675 const CGFunctionInfo &
676 CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType,
677                                               ArrayRef<CanQualType> argTypes) {
678   return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::None, argTypes,
679                                  FunctionType::ExtInfo(), {},
680                                  RequiredArgs::All);
681 }
682 
683 /// Arrange a call to a C++ method, passing the given arguments.
684 ///
685 /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
686 /// does not count `this`.
687 const CGFunctionInfo &
688 CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
689                                    const FunctionProtoType *proto,
690                                    RequiredArgs required,
691                                    unsigned numPrefixArgs) {
692   assert(numPrefixArgs + 1 <= args.size() &&
693          "Emitting a call with less args than the required prefix?");
694   // Add one to account for `this`. It's a bit awkward here, but we don't count
695   // `this` in similar places elsewhere.
696   auto paramInfos =
697     getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());
698 
699   // FIXME: Kill copy.
700   auto argTypes = getArgTypesForCall(Context, args);
701 
702   FunctionType::ExtInfo info = proto->getExtInfo();
703   return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()),
704                                  FnInfoOpts::IsInstanceMethod, argTypes, info,
705                                  paramInfos, required);
706 }
707 
708 const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
709   return arrangeLLVMFunctionInfo(getContext().VoidTy, FnInfoOpts::None,
710                                  std::nullopt, FunctionType::ExtInfo(), {},
711                                  RequiredArgs::All);
712 }
713 
714 const CGFunctionInfo &
715 CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,
716                           const CallArgList &args) {
717   assert(signature.arg_size() <= args.size());
718   if (signature.arg_size() == args.size())
719     return signature;
720 
721   SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
722   auto sigParamInfos = signature.getExtParameterInfos();
723   if (!sigParamInfos.empty()) {
724     paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
725     paramInfos.resize(args.size());
726   }
727 
728   auto argTypes = getArgTypesForCall(Context, args);
729 
730   assert(signature.getRequiredArgs().allowsOptionalArgs());
731   FnInfoOpts opts = FnInfoOpts::None;
732   if (signature.isInstanceMethod())
733     opts |= FnInfoOpts::IsInstanceMethod;
734   if (signature.isChainCall())
735     opts |= FnInfoOpts::IsChainCall;
736   if (signature.isDelegateCall())
737     opts |= FnInfoOpts::IsDelegateCall;
738   return arrangeLLVMFunctionInfo(signature.getReturnType(), opts, argTypes,
739                                  signature.getExtInfo(), paramInfos,
740                                  signature.getRequiredArgs());
741 }
742 
743 namespace clang {
744 namespace CodeGen {
745 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);
746 }
747 }
748 
749 /// Arrange the argument and result information for an abstract value
750 /// of a given function type.  This is the method which all of the
751 /// above functions ultimately defer to.
752 const CGFunctionInfo &CodeGenTypes::arrangeLLVMFunctionInfo(
753     CanQualType resultType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,
754     FunctionType::ExtInfo info,
755     ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
756     RequiredArgs required) {
757   assert(llvm::all_of(argTypes,
758                       [](CanQualType T) { return T.isCanonicalAsParam(); }));
759 
760   // Lookup or create unique function info.
761   llvm::FoldingSetNodeID ID;
762   bool isInstanceMethod =
763       (opts & FnInfoOpts::IsInstanceMethod) == FnInfoOpts::IsInstanceMethod;
764   bool isChainCall =
765       (opts & FnInfoOpts::IsChainCall) == FnInfoOpts::IsChainCall;
766   bool isDelegateCall =
767       (opts & FnInfoOpts::IsDelegateCall) == FnInfoOpts::IsDelegateCall;
768   CGFunctionInfo::Profile(ID, isInstanceMethod, isChainCall, isDelegateCall,
769                           info, paramInfos, required, resultType, argTypes);
770 
771   void *insertPos = nullptr;
772   CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
773   if (FI)
774     return *FI;
775 
776   unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
777 
778   // Construct the function info.  We co-allocate the ArgInfos.
779   FI = CGFunctionInfo::create(CC, isInstanceMethod, isChainCall, isDelegateCall,
780                               info, paramInfos, resultType, argTypes, required);
781   FunctionInfos.InsertNode(FI, insertPos);
782 
783   bool inserted = FunctionsBeingProcessed.insert(FI).second;
784   (void)inserted;
785   assert(inserted && "Recursively being processed?");
786 
787   // Compute ABI information.
788   if (CC == llvm::CallingConv::SPIR_KERNEL) {
789     // Force target independent argument handling for the host visible
790     // kernel functions.
791     computeSPIRKernelABIInfo(CGM, *FI);
792   } else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {
793     swiftcall::computeABIInfo(CGM, *FI);
794   } else {
795     getABIInfo().computeInfo(*FI);
796   }
797 
798   // Loop over all of the computed argument and return value info.  If any of
799   // them are direct or extend without a specified coerce type, specify the
800   // default now.
801   ABIArgInfo &retInfo = FI->getReturnInfo();
802   if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)
803     retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
804 
805   for (auto &I : FI->arguments())
806     if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)
807       I.info.setCoerceToType(ConvertType(I.type));
808 
809   bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
810   assert(erased && "Not in set?");
811 
812   return *FI;
813 }
814 
815 CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, bool instanceMethod,
816                                        bool chainCall, bool delegateCall,
817                                        const FunctionType::ExtInfo &info,
818                                        ArrayRef<ExtParameterInfo> paramInfos,
819                                        CanQualType resultType,
820                                        ArrayRef<CanQualType> argTypes,
821                                        RequiredArgs required) {
822   assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
823   assert(!required.allowsOptionalArgs() ||
824          required.getNumRequiredArgs() <= argTypes.size());
825 
826   void *buffer =
827     operator new(totalSizeToAlloc<ArgInfo,             ExtParameterInfo>(
828                                   argTypes.size() + 1, paramInfos.size()));
829 
830   CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
831   FI->CallingConvention = llvmCC;
832   FI->EffectiveCallingConvention = llvmCC;
833   FI->ASTCallingConvention = info.getCC();
834   FI->InstanceMethod = instanceMethod;
835   FI->ChainCall = chainCall;
836   FI->DelegateCall = delegateCall;
837   FI->CmseNSCall = info.getCmseNSCall();
838   FI->NoReturn = info.getNoReturn();
839   FI->ReturnsRetained = info.getProducesResult();
840   FI->NoCallerSavedRegs = info.getNoCallerSavedRegs();
841   FI->NoCfCheck = info.getNoCfCheck();
842   FI->Required = required;
843   FI->HasRegParm = info.getHasRegParm();
844   FI->RegParm = info.getRegParm();
845   FI->ArgStruct = nullptr;
846   FI->ArgStructAlign = 0;
847   FI->NumArgs = argTypes.size();
848   FI->HasExtParameterInfos = !paramInfos.empty();
849   FI->getArgsBuffer()[0].type = resultType;
850   FI->MaxVectorWidth = 0;
851   for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
852     FI->getArgsBuffer()[i + 1].type = argTypes[i];
853   for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)
854     FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
855   return FI;
856 }
857 
858 /***/
859 
860 namespace {
861 // ABIArgInfo::Expand implementation.
862 
863 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
864 struct TypeExpansion {
865   enum TypeExpansionKind {
866     // Elements of constant arrays are expanded recursively.
867     TEK_ConstantArray,
868     // Record fields are expanded recursively (but if record is a union, only
869     // the field with the largest size is expanded).
870     TEK_Record,
871     // For complex types, real and imaginary parts are expanded recursively.
872     TEK_Complex,
873     // All other types are not expandable.
874     TEK_None
875   };
876 
877   const TypeExpansionKind Kind;
878 
879   TypeExpansion(TypeExpansionKind K) : Kind(K) {}
880   virtual ~TypeExpansion() {}
881 };
882 
883 struct ConstantArrayExpansion : TypeExpansion {
884   QualType EltTy;
885   uint64_t NumElts;
886 
887   ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)
888       : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
889   static bool classof(const TypeExpansion *TE) {
890     return TE->Kind == TEK_ConstantArray;
891   }
892 };
893 
894 struct RecordExpansion : TypeExpansion {
895   SmallVector<const CXXBaseSpecifier *, 1> Bases;
896 
897   SmallVector<const FieldDecl *, 1> Fields;
898 
899   RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
900                   SmallVector<const FieldDecl *, 1> &&Fields)
901       : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
902         Fields(std::move(Fields)) {}
903   static bool classof(const TypeExpansion *TE) {
904     return TE->Kind == TEK_Record;
905   }
906 };
907 
908 struct ComplexExpansion : TypeExpansion {
909   QualType EltTy;
910 
911   ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}
912   static bool classof(const TypeExpansion *TE) {
913     return TE->Kind == TEK_Complex;
914   }
915 };
916 
917 struct NoExpansion : TypeExpansion {
918   NoExpansion() : TypeExpansion(TEK_None) {}
919   static bool classof(const TypeExpansion *TE) {
920     return TE->Kind == TEK_None;
921   }
922 };
923 }  // namespace
924 
925 static std::unique_ptr<TypeExpansion>
926 getTypeExpansion(QualType Ty, const ASTContext &Context) {
927   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
928     return std::make_unique<ConstantArrayExpansion>(
929         AT->getElementType(), AT->getSize().getZExtValue());
930   }
931   if (const RecordType *RT = Ty->getAs<RecordType>()) {
932     SmallVector<const CXXBaseSpecifier *, 1> Bases;
933     SmallVector<const FieldDecl *, 1> Fields;
934     const RecordDecl *RD = RT->getDecl();
935     assert(!RD->hasFlexibleArrayMember() &&
936            "Cannot expand structure with flexible array.");
937     if (RD->isUnion()) {
938       // Unions can be here only in degenerative cases - all the fields are same
939       // after flattening. Thus we have to use the "largest" field.
940       const FieldDecl *LargestFD = nullptr;
941       CharUnits UnionSize = CharUnits::Zero();
942 
943       for (const auto *FD : RD->fields()) {
944         if (FD->isZeroLengthBitField(Context))
945           continue;
946         assert(!FD->isBitField() &&
947                "Cannot expand structure with bit-field members.");
948         CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());
949         if (UnionSize < FieldSize) {
950           UnionSize = FieldSize;
951           LargestFD = FD;
952         }
953       }
954       if (LargestFD)
955         Fields.push_back(LargestFD);
956     } else {
957       if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
958         assert(!CXXRD->isDynamicClass() &&
959                "cannot expand vtable pointers in dynamic classes");
960         llvm::append_range(Bases, llvm::make_pointer_range(CXXRD->bases()));
961       }
962 
963       for (const auto *FD : RD->fields()) {
964         if (FD->isZeroLengthBitField(Context))
965           continue;
966         assert(!FD->isBitField() &&
967                "Cannot expand structure with bit-field members.");
968         Fields.push_back(FD);
969       }
970     }
971     return std::make_unique<RecordExpansion>(std::move(Bases),
972                                               std::move(Fields));
973   }
974   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
975     return std::make_unique<ComplexExpansion>(CT->getElementType());
976   }
977   return std::make_unique<NoExpansion>();
978 }
979 
980 static int getExpansionSize(QualType Ty, const ASTContext &Context) {
981   auto Exp = getTypeExpansion(Ty, Context);
982   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
983     return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
984   }
985   if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
986     int Res = 0;
987     for (auto BS : RExp->Bases)
988       Res += getExpansionSize(BS->getType(), Context);
989     for (auto FD : RExp->Fields)
990       Res += getExpansionSize(FD->getType(), Context);
991     return Res;
992   }
993   if (isa<ComplexExpansion>(Exp.get()))
994     return 2;
995   assert(isa<NoExpansion>(Exp.get()));
996   return 1;
997 }
998 
999 void
1000 CodeGenTypes::getExpandedTypes(QualType Ty,
1001                                SmallVectorImpl<llvm::Type *>::iterator &TI) {
1002   auto Exp = getTypeExpansion(Ty, Context);
1003   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1004     for (int i = 0, n = CAExp->NumElts; i < n; i++) {
1005       getExpandedTypes(CAExp->EltTy, TI);
1006     }
1007   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1008     for (auto BS : RExp->Bases)
1009       getExpandedTypes(BS->getType(), TI);
1010     for (auto FD : RExp->Fields)
1011       getExpandedTypes(FD->getType(), TI);
1012   } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
1013     llvm::Type *EltTy = ConvertType(CExp->EltTy);
1014     *TI++ = EltTy;
1015     *TI++ = EltTy;
1016   } else {
1017     assert(isa<NoExpansion>(Exp.get()));
1018     *TI++ = ConvertType(Ty);
1019   }
1020 }
1021 
1022 static void forConstantArrayExpansion(CodeGenFunction &CGF,
1023                                       ConstantArrayExpansion *CAE,
1024                                       Address BaseAddr,
1025                                       llvm::function_ref<void(Address)> Fn) {
1026   CharUnits EltSize = CGF.getContext().getTypeSizeInChars(CAE->EltTy);
1027   CharUnits EltAlign =
1028     BaseAddr.getAlignment().alignmentOfArrayElement(EltSize);
1029   llvm::Type *EltTy = CGF.ConvertTypeForMem(CAE->EltTy);
1030 
1031   for (int i = 0, n = CAE->NumElts; i < n; i++) {
1032     llvm::Value *EltAddr = CGF.Builder.CreateConstGEP2_32(
1033         BaseAddr.getElementType(), BaseAddr.getPointer(), 0, i);
1034     Fn(Address(EltAddr, EltTy, EltAlign));
1035   }
1036 }
1037 
1038 void CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1039                                          llvm::Function::arg_iterator &AI) {
1040   assert(LV.isSimple() &&
1041          "Unexpected non-simple lvalue during struct expansion.");
1042 
1043   auto Exp = getTypeExpansion(Ty, getContext());
1044   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1045     forConstantArrayExpansion(
1046         *this, CAExp, LV.getAddress(*this), [&](Address EltAddr) {
1047           LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
1048           ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
1049         });
1050   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1051     Address This = LV.getAddress(*this);
1052     for (const CXXBaseSpecifier *BS : RExp->Bases) {
1053       // Perform a single step derived-to-base conversion.
1054       Address Base =
1055           GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
1056                                 /*NullCheckValue=*/false, SourceLocation());
1057       LValue SubLV = MakeAddrLValue(Base, BS->getType());
1058 
1059       // Recurse onto bases.
1060       ExpandTypeFromArgs(BS->getType(), SubLV, AI);
1061     }
1062     for (auto FD : RExp->Fields) {
1063       // FIXME: What are the right qualifiers here?
1064       LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
1065       ExpandTypeFromArgs(FD->getType(), SubLV, AI);
1066     }
1067   } else if (isa<ComplexExpansion>(Exp.get())) {
1068     auto realValue = &*AI++;
1069     auto imagValue = &*AI++;
1070     EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);
1071   } else {
1072     // Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
1073     // primitive store.
1074     assert(isa<NoExpansion>(Exp.get()));
1075     llvm::Value *Arg = &*AI++;
1076     if (LV.isBitField()) {
1077       EmitStoreThroughLValue(RValue::get(Arg), LV);
1078     } else {
1079       // TODO: currently there are some places are inconsistent in what LLVM
1080       // pointer type they use (see D118744). Once clang uses opaque pointers
1081       // all LLVM pointer types will be the same and we can remove this check.
1082       if (Arg->getType()->isPointerTy()) {
1083         Address Addr = LV.getAddress(*this);
1084         Arg = Builder.CreateBitCast(Arg, Addr.getElementType());
1085       }
1086       EmitStoreOfScalar(Arg, LV);
1087     }
1088   }
1089 }
1090 
1091 void CodeGenFunction::ExpandTypeToArgs(
1092     QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,
1093     SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {
1094   auto Exp = getTypeExpansion(Ty, getContext());
1095   if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1096     Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress(*this)
1097                                    : Arg.getKnownRValue().getAggregateAddress();
1098     forConstantArrayExpansion(
1099         *this, CAExp, Addr, [&](Address EltAddr) {
1100           CallArg EltArg = CallArg(
1101               convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()),
1102               CAExp->EltTy);
1103           ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs,
1104                            IRCallArgPos);
1105         });
1106   } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1107     Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress(*this)
1108                                    : Arg.getKnownRValue().getAggregateAddress();
1109     for (const CXXBaseSpecifier *BS : RExp->Bases) {
1110       // Perform a single step derived-to-base conversion.
1111       Address Base =
1112           GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,
1113                                 /*NullCheckValue=*/false, SourceLocation());
1114       CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType());
1115 
1116       // Recurse onto bases.
1117       ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs,
1118                        IRCallArgPos);
1119     }
1120 
1121     LValue LV = MakeAddrLValue(This, Ty);
1122     for (auto FD : RExp->Fields) {
1123       CallArg FldArg =
1124           CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType());
1125       ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs,
1126                        IRCallArgPos);
1127     }
1128   } else if (isa<ComplexExpansion>(Exp.get())) {
1129     ComplexPairTy CV = Arg.getKnownRValue().getComplexVal();
1130     IRCallArgs[IRCallArgPos++] = CV.first;
1131     IRCallArgs[IRCallArgPos++] = CV.second;
1132   } else {
1133     assert(isa<NoExpansion>(Exp.get()));
1134     auto RV = Arg.getKnownRValue();
1135     assert(RV.isScalar() &&
1136            "Unexpected non-scalar rvalue during struct expansion.");
1137 
1138     // Insert a bitcast as needed.
1139     llvm::Value *V = RV.getScalarVal();
1140     if (IRCallArgPos < IRFuncTy->getNumParams() &&
1141         V->getType() != IRFuncTy->getParamType(IRCallArgPos))
1142       V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
1143 
1144     IRCallArgs[IRCallArgPos++] = V;
1145   }
1146 }
1147 
1148 /// Create a temporary allocation for the purposes of coercion.
1149 static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty,
1150                                            CharUnits MinAlign,
1151                                            const Twine &Name = "tmp") {
1152   // Don't use an alignment that's worse than what LLVM would prefer.
1153   auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(Ty);
1154   CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
1155 
1156   return CGF.CreateTempAlloca(Ty, Align, Name + ".coerce");
1157 }
1158 
1159 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
1160 /// accessing some number of bytes out of it, try to gep into the struct to get
1161 /// at its inner goodness.  Dive as deep as possible without entering an element
1162 /// with an in-memory size smaller than DstSize.
1163 static Address
1164 EnterStructPointerForCoercedAccess(Address SrcPtr,
1165                                    llvm::StructType *SrcSTy,
1166                                    uint64_t DstSize, CodeGenFunction &CGF) {
1167   // We can't dive into a zero-element struct.
1168   if (SrcSTy->getNumElements() == 0) return SrcPtr;
1169 
1170   llvm::Type *FirstElt = SrcSTy->getElementType(0);
1171 
1172   // If the first elt is at least as large as what we're looking for, or if the
1173   // first element is the same size as the whole struct, we can enter it. The
1174   // comparison must be made on the store size and not the alloca size. Using
1175   // the alloca size may overstate the size of the load.
1176   uint64_t FirstEltSize =
1177     CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);
1178   if (FirstEltSize < DstSize &&
1179       FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))
1180     return SrcPtr;
1181 
1182   // GEP into the first element.
1183   SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, "coerce.dive");
1184 
1185   // If the first element is a struct, recurse.
1186   llvm::Type *SrcTy = SrcPtr.getElementType();
1187   if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
1188     return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
1189 
1190   return SrcPtr;
1191 }
1192 
1193 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
1194 /// are either integers or pointers.  This does a truncation of the value if it
1195 /// is too large or a zero extension if it is too small.
1196 ///
1197 /// This behaves as if the value were coerced through memory, so on big-endian
1198 /// targets the high bits are preserved in a truncation, while little-endian
1199 /// targets preserve the low bits.
1200 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
1201                                              llvm::Type *Ty,
1202                                              CodeGenFunction &CGF) {
1203   if (Val->getType() == Ty)
1204     return Val;
1205 
1206   if (isa<llvm::PointerType>(Val->getType())) {
1207     // If this is Pointer->Pointer avoid conversion to and from int.
1208     if (isa<llvm::PointerType>(Ty))
1209       return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
1210 
1211     // Convert the pointer to an integer so we can play with its width.
1212     Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
1213   }
1214 
1215   llvm::Type *DestIntTy = Ty;
1216   if (isa<llvm::PointerType>(DestIntTy))
1217     DestIntTy = CGF.IntPtrTy;
1218 
1219   if (Val->getType() != DestIntTy) {
1220     const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
1221     if (DL.isBigEndian()) {
1222       // Preserve the high bits on big-endian targets.
1223       // That is what memory coercion does.
1224       uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
1225       uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
1226 
1227       if (SrcSize > DstSize) {
1228         Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");
1229         Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");
1230       } else {
1231         Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");
1232         Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");
1233       }
1234     } else {
1235       // Little-endian targets preserve the low bits. No shifts required.
1236       Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
1237     }
1238   }
1239 
1240   if (isa<llvm::PointerType>(Ty))
1241     Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
1242   return Val;
1243 }
1244 
1245 
1246 
1247 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1248 /// a pointer to an object of type \arg Ty, known to be aligned to
1249 /// \arg SrcAlign bytes.
1250 ///
1251 /// This safely handles the case when the src type is smaller than the
1252 /// destination type; in this situation the values of bits which not
1253 /// present in the src are undefined.
1254 static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
1255                                       CodeGenFunction &CGF) {
1256   llvm::Type *SrcTy = Src.getElementType();
1257 
1258   // If SrcTy and Ty are the same, just do a load.
1259   if (SrcTy == Ty)
1260     return CGF.Builder.CreateLoad(Src);
1261 
1262   llvm::TypeSize DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
1263 
1264   if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
1265     Src = EnterStructPointerForCoercedAccess(Src, SrcSTy,
1266                                              DstSize.getFixedValue(), CGF);
1267     SrcTy = Src.getElementType();
1268   }
1269 
1270   llvm::TypeSize SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
1271 
1272   // If the source and destination are integer or pointer types, just do an
1273   // extension or truncation to the desired type.
1274   if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
1275       (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
1276     llvm::Value *Load = CGF.Builder.CreateLoad(Src);
1277     return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
1278   }
1279 
1280   // If load is legal, just bitcast the src pointer.
1281   if (!SrcSize.isScalable() && !DstSize.isScalable() &&
1282       SrcSize.getFixedValue() >= DstSize.getFixedValue()) {
1283     // Generally SrcSize is never greater than DstSize, since this means we are
1284     // losing bits. However, this can happen in cases where the structure has
1285     // additional padding, for example due to a user specified alignment.
1286     //
1287     // FIXME: Assert that we aren't truncating non-padding bits when have access
1288     // to that information.
1289     Src = Src.withElementType(Ty);
1290     return CGF.Builder.CreateLoad(Src);
1291   }
1292 
1293   // If coercing a fixed vector to a scalable vector for ABI compatibility, and
1294   // the types match, use the llvm.vector.insert intrinsic to perform the
1295   // conversion.
1296   if (auto *ScalableDst = dyn_cast<llvm::ScalableVectorType>(Ty)) {
1297     if (auto *FixedSrc = dyn_cast<llvm::FixedVectorType>(SrcTy)) {
1298       // If we are casting a fixed i8 vector to a scalable 16 x i1 predicate
1299       // vector, use a vector insert and bitcast the result.
1300       bool NeedsBitcast = false;
1301       auto PredType =
1302           llvm::ScalableVectorType::get(CGF.Builder.getInt1Ty(), 16);
1303       llvm::Type *OrigType = Ty;
1304       if (ScalableDst == PredType &&
1305           FixedSrc->getElementType() == CGF.Builder.getInt8Ty()) {
1306         ScalableDst = llvm::ScalableVectorType::get(CGF.Builder.getInt8Ty(), 2);
1307         NeedsBitcast = true;
1308       }
1309       if (ScalableDst->getElementType() == FixedSrc->getElementType()) {
1310         auto *Load = CGF.Builder.CreateLoad(Src);
1311         auto *UndefVec = llvm::UndefValue::get(ScalableDst);
1312         auto *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);
1313         llvm::Value *Result = CGF.Builder.CreateInsertVector(
1314             ScalableDst, UndefVec, Load, Zero, "cast.scalable");
1315         if (NeedsBitcast)
1316           Result = CGF.Builder.CreateBitCast(Result, OrigType);
1317         return Result;
1318       }
1319     }
1320   }
1321 
1322   // Otherwise do coercion through memory. This is stupid, but simple.
1323   Address Tmp =
1324       CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment(), Src.getName());
1325   CGF.Builder.CreateMemCpy(
1326       Tmp.getPointer(), Tmp.getAlignment().getAsAlign(), Src.getPointer(),
1327       Src.getAlignment().getAsAlign(),
1328       llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize.getKnownMinValue()));
1329   return CGF.Builder.CreateLoad(Tmp);
1330 }
1331 
1332 // Function to store a first-class aggregate into memory.  We prefer to
1333 // store the elements rather than the aggregate to be more friendly to
1334 // fast-isel.
1335 // FIXME: Do we need to recurse here?
1336 void CodeGenFunction::EmitAggregateStore(llvm::Value *Val, Address Dest,
1337                                          bool DestIsVolatile) {
1338   // Prefer scalar stores to first-class aggregate stores.
1339   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(Val->getType())) {
1340     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1341       Address EltPtr = Builder.CreateStructGEP(Dest, i);
1342       llvm::Value *Elt = Builder.CreateExtractValue(Val, i);
1343       Builder.CreateStore(Elt, EltPtr, DestIsVolatile);
1344     }
1345   } else {
1346     Builder.CreateStore(Val, Dest, DestIsVolatile);
1347   }
1348 }
1349 
1350 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1351 /// where the source and destination may have different types.  The
1352 /// destination is known to be aligned to \arg DstAlign bytes.
1353 ///
1354 /// This safely handles the case when the src type is larger than the
1355 /// destination type; the upper bits of the src will be lost.
1356 static void CreateCoercedStore(llvm::Value *Src,
1357                                Address Dst,
1358                                bool DstIsVolatile,
1359                                CodeGenFunction &CGF) {
1360   llvm::Type *SrcTy = Src->getType();
1361   llvm::Type *DstTy = Dst.getElementType();
1362   if (SrcTy == DstTy) {
1363     CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
1364     return;
1365   }
1366 
1367   llvm::TypeSize SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
1368 
1369   if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
1370     Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy,
1371                                              SrcSize.getFixedValue(), CGF);
1372     DstTy = Dst.getElementType();
1373   }
1374 
1375   llvm::PointerType *SrcPtrTy = llvm::dyn_cast<llvm::PointerType>(SrcTy);
1376   llvm::PointerType *DstPtrTy = llvm::dyn_cast<llvm::PointerType>(DstTy);
1377   if (SrcPtrTy && DstPtrTy &&
1378       SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) {
1379     Src = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DstTy);
1380     CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
1381     return;
1382   }
1383 
1384   // If the source and destination are integer or pointer types, just do an
1385   // extension or truncation to the desired type.
1386   if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
1387       (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
1388     Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
1389     CGF.Builder.CreateStore(Src, Dst, DstIsVolatile);
1390     return;
1391   }
1392 
1393   llvm::TypeSize DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
1394 
1395   // If store is legal, just bitcast the src pointer.
1396   if (isa<llvm::ScalableVectorType>(SrcTy) ||
1397       isa<llvm::ScalableVectorType>(DstTy) ||
1398       SrcSize.getFixedValue() <= DstSize.getFixedValue()) {
1399     Dst = Dst.withElementType(SrcTy);
1400     CGF.EmitAggregateStore(Src, Dst, DstIsVolatile);
1401   } else {
1402     // Otherwise do coercion through memory. This is stupid, but
1403     // simple.
1404 
1405     // Generally SrcSize is never greater than DstSize, since this means we are
1406     // losing bits. However, this can happen in cases where the structure has
1407     // additional padding, for example due to a user specified alignment.
1408     //
1409     // FIXME: Assert that we aren't truncating non-padding bits when have access
1410     // to that information.
1411     Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
1412     CGF.Builder.CreateStore(Src, Tmp);
1413     CGF.Builder.CreateMemCpy(
1414         Dst.getPointer(), Dst.getAlignment().getAsAlign(), Tmp.getPointer(),
1415         Tmp.getAlignment().getAsAlign(),
1416         llvm::ConstantInt::get(CGF.IntPtrTy, DstSize.getFixedValue()));
1417   }
1418 }
1419 
1420 static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr,
1421                                    const ABIArgInfo &info) {
1422   if (unsigned offset = info.getDirectOffset()) {
1423     addr = addr.withElementType(CGF.Int8Ty);
1424     addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,
1425                                              CharUnits::fromQuantity(offset));
1426     addr = addr.withElementType(info.getCoerceToType());
1427   }
1428   return addr;
1429 }
1430 
1431 namespace {
1432 
1433 /// Encapsulates information about the way function arguments from
1434 /// CGFunctionInfo should be passed to actual LLVM IR function.
1435 class ClangToLLVMArgMapping {
1436   static const unsigned InvalidIndex = ~0U;
1437   unsigned InallocaArgNo;
1438   unsigned SRetArgNo;
1439   unsigned TotalIRArgs;
1440 
1441   /// Arguments of LLVM IR function corresponding to single Clang argument.
1442   struct IRArgs {
1443     unsigned PaddingArgIndex;
1444     // Argument is expanded to IR arguments at positions
1445     // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
1446     unsigned FirstArgIndex;
1447     unsigned NumberOfArgs;
1448 
1449     IRArgs()
1450         : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
1451           NumberOfArgs(0) {}
1452   };
1453 
1454   SmallVector<IRArgs, 8> ArgInfo;
1455 
1456 public:
1457   ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,
1458                         bool OnlyRequiredArgs = false)
1459       : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
1460         ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
1461     construct(Context, FI, OnlyRequiredArgs);
1462   }
1463 
1464   bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }
1465   unsigned getInallocaArgNo() const {
1466     assert(hasInallocaArg());
1467     return InallocaArgNo;
1468   }
1469 
1470   bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
1471   unsigned getSRetArgNo() const {
1472     assert(hasSRetArg());
1473     return SRetArgNo;
1474   }
1475 
1476   unsigned totalIRArgs() const { return TotalIRArgs; }
1477 
1478   bool hasPaddingArg(unsigned ArgNo) const {
1479     assert(ArgNo < ArgInfo.size());
1480     return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
1481   }
1482   unsigned getPaddingArgNo(unsigned ArgNo) const {
1483     assert(hasPaddingArg(ArgNo));
1484     return ArgInfo[ArgNo].PaddingArgIndex;
1485   }
1486 
1487   /// Returns index of first IR argument corresponding to ArgNo, and their
1488   /// quantity.
1489   std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {
1490     assert(ArgNo < ArgInfo.size());
1491     return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
1492                           ArgInfo[ArgNo].NumberOfArgs);
1493   }
1494 
1495 private:
1496   void construct(const ASTContext &Context, const CGFunctionInfo &FI,
1497                  bool OnlyRequiredArgs);
1498 };
1499 
1500 void ClangToLLVMArgMapping::construct(const ASTContext &Context,
1501                                       const CGFunctionInfo &FI,
1502                                       bool OnlyRequiredArgs) {
1503   unsigned IRArgNo = 0;
1504   bool SwapThisWithSRet = false;
1505   const ABIArgInfo &RetAI = FI.getReturnInfo();
1506 
1507   if (RetAI.getKind() == ABIArgInfo::Indirect) {
1508     SwapThisWithSRet = RetAI.isSRetAfterThis();
1509     SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
1510   }
1511 
1512   unsigned ArgNo = 0;
1513   unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();
1514   for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;
1515        ++I, ++ArgNo) {
1516     assert(I != FI.arg_end());
1517     QualType ArgType = I->type;
1518     const ABIArgInfo &AI = I->info;
1519     // Collect data about IR arguments corresponding to Clang argument ArgNo.
1520     auto &IRArgs = ArgInfo[ArgNo];
1521 
1522     if (AI.getPaddingType())
1523       IRArgs.PaddingArgIndex = IRArgNo++;
1524 
1525     switch (AI.getKind()) {
1526     case ABIArgInfo::Extend:
1527     case ABIArgInfo::Direct: {
1528       // FIXME: handle sseregparm someday...
1529       llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());
1530       if (AI.isDirect() && AI.getCanBeFlattened() && STy) {
1531         IRArgs.NumberOfArgs = STy->getNumElements();
1532       } else {
1533         IRArgs.NumberOfArgs = 1;
1534       }
1535       break;
1536     }
1537     case ABIArgInfo::Indirect:
1538     case ABIArgInfo::IndirectAliased:
1539       IRArgs.NumberOfArgs = 1;
1540       break;
1541     case ABIArgInfo::Ignore:
1542     case ABIArgInfo::InAlloca:
1543       // ignore and inalloca doesn't have matching LLVM parameters.
1544       IRArgs.NumberOfArgs = 0;
1545       break;
1546     case ABIArgInfo::CoerceAndExpand:
1547       IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();
1548       break;
1549     case ABIArgInfo::Expand:
1550       IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);
1551       break;
1552     }
1553 
1554     if (IRArgs.NumberOfArgs > 0) {
1555       IRArgs.FirstArgIndex = IRArgNo;
1556       IRArgNo += IRArgs.NumberOfArgs;
1557     }
1558 
1559     // Skip over the sret parameter when it comes second.  We already handled it
1560     // above.
1561     if (IRArgNo == 1 && SwapThisWithSRet)
1562       IRArgNo++;
1563   }
1564   assert(ArgNo == ArgInfo.size());
1565 
1566   if (FI.usesInAlloca())
1567     InallocaArgNo = IRArgNo++;
1568 
1569   TotalIRArgs = IRArgNo;
1570 }
1571 }  // namespace
1572 
1573 /***/
1574 
1575 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
1576   const auto &RI = FI.getReturnInfo();
1577   return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());
1578 }
1579 
1580 bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {
1581   return ReturnTypeUsesSRet(FI) &&
1582          getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1583 }
1584 
1585 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
1586   if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
1587     switch (BT->getKind()) {
1588     default:
1589       return false;
1590     case BuiltinType::Float:
1591       return getTarget().useObjCFPRetForRealType(FloatModeKind::Float);
1592     case BuiltinType::Double:
1593       return getTarget().useObjCFPRetForRealType(FloatModeKind::Double);
1594     case BuiltinType::LongDouble:
1595       return getTarget().useObjCFPRetForRealType(FloatModeKind::LongDouble);
1596     }
1597   }
1598 
1599   return false;
1600 }
1601 
1602 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
1603   if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
1604     if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
1605       if (BT->getKind() == BuiltinType::LongDouble)
1606         return getTarget().useObjCFP2RetForComplexLongDouble();
1607     }
1608   }
1609 
1610   return false;
1611 }
1612 
1613 llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
1614   const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
1615   return GetFunctionType(FI);
1616 }
1617 
1618 llvm::FunctionType *
1619 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
1620 
1621   bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
1622   (void)Inserted;
1623   assert(Inserted && "Recursively being processed?");
1624 
1625   llvm::Type *resultType = nullptr;
1626   const ABIArgInfo &retAI = FI.getReturnInfo();
1627   switch (retAI.getKind()) {
1628   case ABIArgInfo::Expand:
1629   case ABIArgInfo::IndirectAliased:
1630     llvm_unreachable("Invalid ABI kind for return argument");
1631 
1632   case ABIArgInfo::Extend:
1633   case ABIArgInfo::Direct:
1634     resultType = retAI.getCoerceToType();
1635     break;
1636 
1637   case ABIArgInfo::InAlloca:
1638     if (retAI.getInAllocaSRet()) {
1639       // sret things on win32 aren't void, they return the sret pointer.
1640       QualType ret = FI.getReturnType();
1641       unsigned addressSpace = CGM.getTypes().getTargetAddressSpace(ret);
1642       resultType = llvm::PointerType::get(getLLVMContext(), addressSpace);
1643     } else {
1644       resultType = llvm::Type::getVoidTy(getLLVMContext());
1645     }
1646     break;
1647 
1648   case ABIArgInfo::Indirect:
1649   case ABIArgInfo::Ignore:
1650     resultType = llvm::Type::getVoidTy(getLLVMContext());
1651     break;
1652 
1653   case ABIArgInfo::CoerceAndExpand:
1654     resultType = retAI.getUnpaddedCoerceAndExpandType();
1655     break;
1656   }
1657 
1658   ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);
1659   SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());
1660 
1661   // Add type for sret argument.
1662   if (IRFunctionArgs.hasSRetArg()) {
1663     QualType Ret = FI.getReturnType();
1664     unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(Ret);
1665     ArgTypes[IRFunctionArgs.getSRetArgNo()] =
1666         llvm::PointerType::get(getLLVMContext(), AddressSpace);
1667   }
1668 
1669   // Add type for inalloca argument.
1670   if (IRFunctionArgs.hasInallocaArg())
1671     ArgTypes[IRFunctionArgs.getInallocaArgNo()] =
1672         llvm::PointerType::getUnqual(getLLVMContext());
1673 
1674   // Add in all of the required arguments.
1675   unsigned ArgNo = 0;
1676   CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1677                                      ie = it + FI.getNumRequiredArgs();
1678   for (; it != ie; ++it, ++ArgNo) {
1679     const ABIArgInfo &ArgInfo = it->info;
1680 
1681     // Insert a padding type to ensure proper alignment.
1682     if (IRFunctionArgs.hasPaddingArg(ArgNo))
1683       ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1684           ArgInfo.getPaddingType();
1685 
1686     unsigned FirstIRArg, NumIRArgs;
1687     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1688 
1689     switch (ArgInfo.getKind()) {
1690     case ABIArgInfo::Ignore:
1691     case ABIArgInfo::InAlloca:
1692       assert(NumIRArgs == 0);
1693       break;
1694 
1695     case ABIArgInfo::Indirect:
1696       assert(NumIRArgs == 1);
1697       // indirect arguments are always on the stack, which is alloca addr space.
1698       ArgTypes[FirstIRArg] = llvm::PointerType::get(
1699           getLLVMContext(), CGM.getDataLayout().getAllocaAddrSpace());
1700       break;
1701     case ABIArgInfo::IndirectAliased:
1702       assert(NumIRArgs == 1);
1703       ArgTypes[FirstIRArg] = llvm::PointerType::get(
1704           getLLVMContext(), ArgInfo.getIndirectAddrSpace());
1705       break;
1706     case ABIArgInfo::Extend:
1707     case ABIArgInfo::Direct: {
1708       // Fast-isel and the optimizer generally like scalar values better than
1709       // FCAs, so we flatten them if this is safe to do for this argument.
1710       llvm::Type *argType = ArgInfo.getCoerceToType();
1711       llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
1712       if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
1713         assert(NumIRArgs == st->getNumElements());
1714         for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
1715           ArgTypes[FirstIRArg + i] = st->getElementType(i);
1716       } else {
1717         assert(NumIRArgs == 1);
1718         ArgTypes[FirstIRArg] = argType;
1719       }
1720       break;
1721     }
1722 
1723     case ABIArgInfo::CoerceAndExpand: {
1724       auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1725       for (auto *EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {
1726         *ArgTypesIter++ = EltTy;
1727       }
1728       assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1729       break;
1730     }
1731 
1732     case ABIArgInfo::Expand:
1733       auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1734       getExpandedTypes(it->type, ArgTypesIter);
1735       assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1736       break;
1737     }
1738   }
1739 
1740   bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1741   assert(Erased && "Not in set?");
1742 
1743   return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());
1744 }
1745 
1746 llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
1747   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1748   const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1749 
1750   if (!isFuncTypeConvertible(FPT))
1751     return llvm::StructType::get(getLLVMContext());
1752 
1753   return GetFunctionType(GD);
1754 }
1755 
1756 static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,
1757                                                llvm::AttrBuilder &FuncAttrs,
1758                                                const FunctionProtoType *FPT) {
1759   if (!FPT)
1760     return;
1761 
1762   if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
1763       FPT->isNothrow())
1764     FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1765 }
1766 
1767 static void AddAttributesFromAssumes(llvm::AttrBuilder &FuncAttrs,
1768                                      const Decl *Callee) {
1769   if (!Callee)
1770     return;
1771 
1772   SmallVector<StringRef, 4> Attrs;
1773 
1774   for (const AssumptionAttr *AA : Callee->specific_attrs<AssumptionAttr>())
1775     AA->getAssumption().split(Attrs, ",");
1776 
1777   if (!Attrs.empty())
1778     FuncAttrs.addAttribute(llvm::AssumptionAttrKey,
1779                            llvm::join(Attrs.begin(), Attrs.end(), ","));
1780 }
1781 
1782 bool CodeGenModule::MayDropFunctionReturn(const ASTContext &Context,
1783                                           QualType ReturnType) const {
1784   // We can't just discard the return value for a record type with a
1785   // complex destructor or a non-trivially copyable type.
1786   if (const RecordType *RT =
1787           ReturnType.getCanonicalType()->getAs<RecordType>()) {
1788     if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
1789       return ClassDecl->hasTrivialDestructor();
1790   }
1791   return ReturnType.isTriviallyCopyableType(Context);
1792 }
1793 
1794 static bool HasStrictReturn(const CodeGenModule &Module, QualType RetTy,
1795                             const Decl *TargetDecl) {
1796   // As-is msan can not tolerate noundef mismatch between caller and
1797   // implementation. Mismatch is possible for e.g. indirect calls from C-caller
1798   // into C++. Such mismatches lead to confusing false reports. To avoid
1799   // expensive workaround on msan we enforce initialization event in uncommon
1800   // cases where it's allowed.
1801   if (Module.getLangOpts().Sanitize.has(SanitizerKind::Memory))
1802     return true;
1803   // C++ explicitly makes returning undefined values UB. C's rule only applies
1804   // to used values, so we never mark them noundef for now.
1805   if (!Module.getLangOpts().CPlusPlus)
1806     return false;
1807   if (TargetDecl) {
1808     if (const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(TargetDecl)) {
1809       if (FDecl->isExternC())
1810         return false;
1811     } else if (const VarDecl *VDecl = dyn_cast<VarDecl>(TargetDecl)) {
1812       // Function pointer.
1813       if (VDecl->isExternC())
1814         return false;
1815     }
1816   }
1817 
1818   // We don't want to be too aggressive with the return checking, unless
1819   // it's explicit in the code opts or we're using an appropriate sanitizer.
1820   // Try to respect what the programmer intended.
1821   return Module.getCodeGenOpts().StrictReturn ||
1822          !Module.MayDropFunctionReturn(Module.getContext(), RetTy) ||
1823          Module.getLangOpts().Sanitize.has(SanitizerKind::Return);
1824 }
1825 
1826 /// Add denormal-fp-math and denormal-fp-math-f32 as appropriate for the
1827 /// requested denormal behavior, accounting for the overriding behavior of the
1828 /// -f32 case.
1829 static void addDenormalModeAttrs(llvm::DenormalMode FPDenormalMode,
1830                                  llvm::DenormalMode FP32DenormalMode,
1831                                  llvm::AttrBuilder &FuncAttrs) {
1832   if (FPDenormalMode != llvm::DenormalMode::getDefault())
1833     FuncAttrs.addAttribute("denormal-fp-math", FPDenormalMode.str());
1834 
1835   if (FP32DenormalMode != FPDenormalMode && FP32DenormalMode.isValid())
1836     FuncAttrs.addAttribute("denormal-fp-math-f32", FP32DenormalMode.str());
1837 }
1838 
1839 /// Add default attributes to a function, which have merge semantics under
1840 /// -mlink-builtin-bitcode and should not simply overwrite any existing
1841 /// attributes in the linked library.
1842 static void
1843 addMergableDefaultFunctionAttributes(const CodeGenOptions &CodeGenOpts,
1844                                      llvm::AttrBuilder &FuncAttrs) {
1845   addDenormalModeAttrs(CodeGenOpts.FPDenormalMode, CodeGenOpts.FP32DenormalMode,
1846                        FuncAttrs);
1847 }
1848 
1849 static void getTrivialDefaultFunctionAttributes(
1850     StringRef Name, bool HasOptnone, const CodeGenOptions &CodeGenOpts,
1851     const LangOptions &LangOpts, bool AttrOnCallSite,
1852     llvm::AttrBuilder &FuncAttrs) {
1853   // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1854   if (!HasOptnone) {
1855     if (CodeGenOpts.OptimizeSize)
1856       FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1857     if (CodeGenOpts.OptimizeSize == 2)
1858       FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1859   }
1860 
1861   if (CodeGenOpts.DisableRedZone)
1862     FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
1863   if (CodeGenOpts.IndirectTlsSegRefs)
1864     FuncAttrs.addAttribute("indirect-tls-seg-refs");
1865   if (CodeGenOpts.NoImplicitFloat)
1866     FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
1867 
1868   if (AttrOnCallSite) {
1869     // Attributes that should go on the call site only.
1870     // FIXME: Look for 'BuiltinAttr' on the function rather than re-checking
1871     // the -fno-builtin-foo list.
1872     if (!CodeGenOpts.SimplifyLibCalls || LangOpts.isNoBuiltinFunc(Name))
1873       FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1874     if (!CodeGenOpts.TrapFuncName.empty())
1875       FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);
1876   } else {
1877     switch (CodeGenOpts.getFramePointer()) {
1878     case CodeGenOptions::FramePointerKind::None:
1879       // This is the default behavior.
1880       break;
1881     case CodeGenOptions::FramePointerKind::NonLeaf:
1882     case CodeGenOptions::FramePointerKind::All:
1883       FuncAttrs.addAttribute("frame-pointer",
1884                              CodeGenOptions::getFramePointerKindName(
1885                                  CodeGenOpts.getFramePointer()));
1886     }
1887 
1888     if (CodeGenOpts.LessPreciseFPMAD)
1889       FuncAttrs.addAttribute("less-precise-fpmad", "true");
1890 
1891     if (CodeGenOpts.NullPointerIsValid)
1892       FuncAttrs.addAttribute(llvm::Attribute::NullPointerIsValid);
1893 
1894     if (LangOpts.getDefaultExceptionMode() == LangOptions::FPE_Ignore)
1895       FuncAttrs.addAttribute("no-trapping-math", "true");
1896 
1897     // TODO: Are these all needed?
1898     // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1899     if (LangOpts.NoHonorInfs)
1900       FuncAttrs.addAttribute("no-infs-fp-math", "true");
1901     if (LangOpts.NoHonorNaNs)
1902       FuncAttrs.addAttribute("no-nans-fp-math", "true");
1903     if (LangOpts.ApproxFunc)
1904       FuncAttrs.addAttribute("approx-func-fp-math", "true");
1905     if (LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
1906         LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
1907         (LangOpts.getDefaultFPContractMode() ==
1908              LangOptions::FPModeKind::FPM_Fast ||
1909          LangOpts.getDefaultFPContractMode() ==
1910              LangOptions::FPModeKind::FPM_FastHonorPragmas))
1911       FuncAttrs.addAttribute("unsafe-fp-math", "true");
1912     if (CodeGenOpts.SoftFloat)
1913       FuncAttrs.addAttribute("use-soft-float", "true");
1914     FuncAttrs.addAttribute("stack-protector-buffer-size",
1915                            llvm::utostr(CodeGenOpts.SSPBufferSize));
1916     if (LangOpts.NoSignedZero)
1917       FuncAttrs.addAttribute("no-signed-zeros-fp-math", "true");
1918 
1919     // TODO: Reciprocal estimate codegen options should apply to instructions?
1920     const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
1921     if (!Recips.empty())
1922       FuncAttrs.addAttribute("reciprocal-estimates",
1923                              llvm::join(Recips, ","));
1924 
1925     if (!CodeGenOpts.PreferVectorWidth.empty() &&
1926         CodeGenOpts.PreferVectorWidth != "none")
1927       FuncAttrs.addAttribute("prefer-vector-width",
1928                              CodeGenOpts.PreferVectorWidth);
1929 
1930     if (CodeGenOpts.StackRealignment)
1931       FuncAttrs.addAttribute("stackrealign");
1932     if (CodeGenOpts.Backchain)
1933       FuncAttrs.addAttribute("backchain");
1934     if (CodeGenOpts.EnableSegmentedStacks)
1935       FuncAttrs.addAttribute("split-stack");
1936 
1937     if (CodeGenOpts.SpeculativeLoadHardening)
1938       FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
1939 
1940     // Add zero-call-used-regs attribute.
1941     switch (CodeGenOpts.getZeroCallUsedRegs()) {
1942     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip:
1943       FuncAttrs.removeAttribute("zero-call-used-regs");
1944       break;
1945     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg:
1946       FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr-arg");
1947       break;
1948     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR:
1949       FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr");
1950       break;
1951     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg:
1952       FuncAttrs.addAttribute("zero-call-used-regs", "used-arg");
1953       break;
1954     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used:
1955       FuncAttrs.addAttribute("zero-call-used-regs", "used");
1956       break;
1957     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg:
1958       FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr-arg");
1959       break;
1960     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR:
1961       FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr");
1962       break;
1963     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg:
1964       FuncAttrs.addAttribute("zero-call-used-regs", "all-arg");
1965       break;
1966     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All:
1967       FuncAttrs.addAttribute("zero-call-used-regs", "all");
1968       break;
1969     }
1970   }
1971 
1972   if (LangOpts.assumeFunctionsAreConvergent()) {
1973     // Conservatively, mark all functions and calls in CUDA and OpenCL as
1974     // convergent (meaning, they may call an intrinsically convergent op, such
1975     // as __syncthreads() / barrier(), and so can't have certain optimizations
1976     // applied around them).  LLVM will remove this attribute where it safely
1977     // can.
1978     FuncAttrs.addAttribute(llvm::Attribute::Convergent);
1979   }
1980 
1981   // TODO: NoUnwind attribute should be added for other GPU modes HIP,
1982   // OpenMP offload. AFAIK, neither of them support exceptions in device code.
1983   if ((LangOpts.CUDA && LangOpts.CUDAIsDevice) || LangOpts.OpenCL ||
1984       LangOpts.SYCLIsDevice) {
1985     FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1986   }
1987 
1988   for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
1989     StringRef Var, Value;
1990     std::tie(Var, Value) = Attr.split('=');
1991     FuncAttrs.addAttribute(Var, Value);
1992   }
1993 }
1994 
1995 /// Adds attributes to \p F according to our \p CodeGenOpts and \p LangOpts, as
1996 /// though we had emitted it ourselves. We remove any attributes on F that
1997 /// conflict with the attributes we add here.
1998 static void mergeDefaultFunctionDefinitionAttributes(
1999     llvm::Function &F, const CodeGenOptions CodeGenOpts,
2000     const LangOptions &LangOpts, const TargetOptions &TargetOpts,
2001     bool WillInternalize) {
2002 
2003   llvm::AttrBuilder FuncAttrs(F.getContext());
2004   // Here we only extract the options that are relevant compared to the version
2005   // from GetCPUAndFeaturesAttributes.
2006   if (!TargetOpts.CPU.empty())
2007     FuncAttrs.addAttribute("target-cpu", TargetOpts.CPU);
2008   if (!TargetOpts.TuneCPU.empty())
2009     FuncAttrs.addAttribute("tune-cpu", TargetOpts.TuneCPU);
2010 
2011   ::getTrivialDefaultFunctionAttributes(F.getName(), F.hasOptNone(),
2012                                         CodeGenOpts, LangOpts,
2013                                         /*AttrOnCallSite=*/false, FuncAttrs);
2014 
2015   if (!WillInternalize && F.isInterposable()) {
2016     // Do not promote "dynamic" denormal-fp-math to this translation unit's
2017     // setting for weak functions that won't be internalized. The user has no
2018     // real control for how builtin bitcode is linked, so we shouldn't assume
2019     // later copies will use a consistent mode.
2020     F.addFnAttrs(FuncAttrs);
2021     return;
2022   }
2023 
2024   llvm::AttributeMask AttrsToRemove;
2025 
2026   llvm::DenormalMode DenormModeToMerge = F.getDenormalModeRaw();
2027   llvm::DenormalMode DenormModeToMergeF32 = F.getDenormalModeF32Raw();
2028   llvm::DenormalMode Merged =
2029       CodeGenOpts.FPDenormalMode.mergeCalleeMode(DenormModeToMerge);
2030   llvm::DenormalMode MergedF32 = CodeGenOpts.FP32DenormalMode;
2031 
2032   if (DenormModeToMergeF32.isValid()) {
2033     MergedF32 =
2034         CodeGenOpts.FP32DenormalMode.mergeCalleeMode(DenormModeToMergeF32);
2035   }
2036 
2037   if (Merged == llvm::DenormalMode::getDefault()) {
2038     AttrsToRemove.addAttribute("denormal-fp-math");
2039   } else if (Merged != DenormModeToMerge) {
2040     // Overwrite existing attribute
2041     FuncAttrs.addAttribute("denormal-fp-math",
2042                            CodeGenOpts.FPDenormalMode.str());
2043   }
2044 
2045   if (MergedF32 == llvm::DenormalMode::getDefault()) {
2046     AttrsToRemove.addAttribute("denormal-fp-math-f32");
2047   } else if (MergedF32 != DenormModeToMergeF32) {
2048     // Overwrite existing attribute
2049     FuncAttrs.addAttribute("denormal-fp-math-f32",
2050                            CodeGenOpts.FP32DenormalMode.str());
2051   }
2052 
2053   F.removeFnAttrs(AttrsToRemove);
2054   addDenormalModeAttrs(Merged, MergedF32, FuncAttrs);
2055   F.addFnAttrs(FuncAttrs);
2056 }
2057 
2058 void clang::CodeGen::mergeDefaultFunctionDefinitionAttributes(
2059     llvm::Function &F, const CodeGenOptions CodeGenOpts,
2060     const LangOptions &LangOpts, const TargetOptions &TargetOpts,
2061     bool WillInternalize) {
2062 
2063   ::mergeDefaultFunctionDefinitionAttributes(F, CodeGenOpts, LangOpts,
2064                                              TargetOpts, WillInternalize);
2065 }
2066 
2067 void CodeGenModule::getTrivialDefaultFunctionAttributes(
2068     StringRef Name, bool HasOptnone, bool AttrOnCallSite,
2069     llvm::AttrBuilder &FuncAttrs) {
2070   ::getTrivialDefaultFunctionAttributes(Name, HasOptnone, getCodeGenOpts(),
2071                                         getLangOpts(), AttrOnCallSite,
2072                                         FuncAttrs);
2073 }
2074 
2075 void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
2076                                                  bool HasOptnone,
2077                                                  bool AttrOnCallSite,
2078                                                  llvm::AttrBuilder &FuncAttrs) {
2079   getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,
2080                                       FuncAttrs);
2081   // If we're just getting the default, get the default values for mergeable
2082   // attributes.
2083   if (!AttrOnCallSite)
2084     addMergableDefaultFunctionAttributes(CodeGenOpts, FuncAttrs);
2085 }
2086 
2087 void CodeGenModule::addDefaultFunctionDefinitionAttributes(llvm::Function &F) {
2088   llvm::AttrBuilder FuncAttrs(F.getContext());
2089   getDefaultFunctionAttributes(F.getName(), F.hasOptNone(),
2090                                /* AttrOnCallSite = */ false, FuncAttrs);
2091   // TODO: call GetCPUAndFeaturesAttributes?
2092   F.addFnAttrs(FuncAttrs);
2093 }
2094 
2095 /// Apply default attributes to \p F, accounting for merge semantics of
2096 /// attributes that should not overwrite existing attributes.
2097 void CodeGenModule::mergeDefaultFunctionDefinitionAttributes(
2098     llvm::Function &F, bool WillInternalize) {
2099   ::mergeDefaultFunctionDefinitionAttributes(F, getCodeGenOpts(), getLangOpts(),
2100                                              getTarget().getTargetOpts(),
2101                                              WillInternalize);
2102 }
2103 
2104 void CodeGenModule::addDefaultFunctionDefinitionAttributes(
2105     llvm::AttrBuilder &attrs) {
2106   getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,
2107                                /*for call*/ false, attrs);
2108   GetCPUAndFeaturesAttributes(GlobalDecl(), attrs);
2109 }
2110 
2111 static void addNoBuiltinAttributes(llvm::AttrBuilder &FuncAttrs,
2112                                    const LangOptions &LangOpts,
2113                                    const NoBuiltinAttr *NBA = nullptr) {
2114   auto AddNoBuiltinAttr = [&FuncAttrs](StringRef BuiltinName) {
2115     SmallString<32> AttributeName;
2116     AttributeName += "no-builtin-";
2117     AttributeName += BuiltinName;
2118     FuncAttrs.addAttribute(AttributeName);
2119   };
2120 
2121   // First, handle the language options passed through -fno-builtin.
2122   if (LangOpts.NoBuiltin) {
2123     // -fno-builtin disables them all.
2124     FuncAttrs.addAttribute("no-builtins");
2125     return;
2126   }
2127 
2128   // Then, add attributes for builtins specified through -fno-builtin-<name>.
2129   llvm::for_each(LangOpts.NoBuiltinFuncs, AddNoBuiltinAttr);
2130 
2131   // Now, let's check the __attribute__((no_builtin("...")) attribute added to
2132   // the source.
2133   if (!NBA)
2134     return;
2135 
2136   // If there is a wildcard in the builtin names specified through the
2137   // attribute, disable them all.
2138   if (llvm::is_contained(NBA->builtinNames(), "*")) {
2139     FuncAttrs.addAttribute("no-builtins");
2140     return;
2141   }
2142 
2143   // And last, add the rest of the builtin names.
2144   llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr);
2145 }
2146 
2147 static bool DetermineNoUndef(QualType QTy, CodeGenTypes &Types,
2148                              const llvm::DataLayout &DL, const ABIArgInfo &AI,
2149                              bool CheckCoerce = true) {
2150   llvm::Type *Ty = Types.ConvertTypeForMem(QTy);
2151   if (AI.getKind() == ABIArgInfo::Indirect)
2152     return true;
2153   if (AI.getKind() == ABIArgInfo::Extend)
2154     return true;
2155   if (!DL.typeSizeEqualsStoreSize(Ty))
2156     // TODO: This will result in a modest amount of values not marked noundef
2157     // when they could be. We care about values that *invisibly* contain undef
2158     // bits from the perspective of LLVM IR.
2159     return false;
2160   if (CheckCoerce && AI.canHaveCoerceToType()) {
2161     llvm::Type *CoerceTy = AI.getCoerceToType();
2162     if (llvm::TypeSize::isKnownGT(DL.getTypeSizeInBits(CoerceTy),
2163                                   DL.getTypeSizeInBits(Ty)))
2164       // If we're coercing to a type with a greater size than the canonical one,
2165       // we're introducing new undef bits.
2166       // Coercing to a type of smaller or equal size is ok, as we know that
2167       // there's no internal padding (typeSizeEqualsStoreSize).
2168       return false;
2169   }
2170   if (QTy->isBitIntType())
2171     return true;
2172   if (QTy->isReferenceType())
2173     return true;
2174   if (QTy->isNullPtrType())
2175     return false;
2176   if (QTy->isMemberPointerType())
2177     // TODO: Some member pointers are `noundef`, but it depends on the ABI. For
2178     // now, never mark them.
2179     return false;
2180   if (QTy->isScalarType()) {
2181     if (const ComplexType *Complex = dyn_cast<ComplexType>(QTy))
2182       return DetermineNoUndef(Complex->getElementType(), Types, DL, AI, false);
2183     return true;
2184   }
2185   if (const VectorType *Vector = dyn_cast<VectorType>(QTy))
2186     return DetermineNoUndef(Vector->getElementType(), Types, DL, AI, false);
2187   if (const MatrixType *Matrix = dyn_cast<MatrixType>(QTy))
2188     return DetermineNoUndef(Matrix->getElementType(), Types, DL, AI, false);
2189   if (const ArrayType *Array = dyn_cast<ArrayType>(QTy))
2190     return DetermineNoUndef(Array->getElementType(), Types, DL, AI, false);
2191 
2192   // TODO: Some structs may be `noundef`, in specific situations.
2193   return false;
2194 }
2195 
2196 /// Check if the argument of a function has maybe_undef attribute.
2197 static bool IsArgumentMaybeUndef(const Decl *TargetDecl,
2198                                  unsigned NumRequiredArgs, unsigned ArgNo) {
2199   const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
2200   if (!FD)
2201     return false;
2202 
2203   // Assume variadic arguments do not have maybe_undef attribute.
2204   if (ArgNo >= NumRequiredArgs)
2205     return false;
2206 
2207   // Check if argument has maybe_undef attribute.
2208   if (ArgNo < FD->getNumParams()) {
2209     const ParmVarDecl *Param = FD->getParamDecl(ArgNo);
2210     if (Param && Param->hasAttr<MaybeUndefAttr>())
2211       return true;
2212   }
2213 
2214   return false;
2215 }
2216 
2217 /// Test if it's legal to apply nofpclass for the given parameter type and it's
2218 /// lowered IR type.
2219 static bool canApplyNoFPClass(const ABIArgInfo &AI, QualType ParamType,
2220                               bool IsReturn) {
2221   // Should only apply to FP types in the source, not ABI promoted.
2222   if (!ParamType->hasFloatingRepresentation())
2223     return false;
2224 
2225   // The promoted-to IR type also needs to support nofpclass.
2226   llvm::Type *IRTy = AI.getCoerceToType();
2227   if (llvm::AttributeFuncs::isNoFPClassCompatibleType(IRTy))
2228     return true;
2229 
2230   if (llvm::StructType *ST = dyn_cast<llvm::StructType>(IRTy)) {
2231     return !IsReturn && AI.getCanBeFlattened() &&
2232            llvm::all_of(ST->elements(), [](llvm::Type *Ty) {
2233              return llvm::AttributeFuncs::isNoFPClassCompatibleType(Ty);
2234            });
2235   }
2236 
2237   return false;
2238 }
2239 
2240 /// Return the nofpclass mask that can be applied to floating-point parameters.
2241 static llvm::FPClassTest getNoFPClassTestMask(const LangOptions &LangOpts) {
2242   llvm::FPClassTest Mask = llvm::fcNone;
2243   if (LangOpts.NoHonorInfs)
2244     Mask |= llvm::fcInf;
2245   if (LangOpts.NoHonorNaNs)
2246     Mask |= llvm::fcNan;
2247   return Mask;
2248 }
2249 
2250 /// Construct the IR attribute list of a function or call.
2251 ///
2252 /// When adding an attribute, please consider where it should be handled:
2253 ///
2254 ///   - getDefaultFunctionAttributes is for attributes that are essentially
2255 ///     part of the global target configuration (but perhaps can be
2256 ///     overridden on a per-function basis).  Adding attributes there
2257 ///     will cause them to also be set in frontends that build on Clang's
2258 ///     target-configuration logic, as well as for code defined in library
2259 ///     modules such as CUDA's libdevice.
2260 ///
2261 ///   - ConstructAttributeList builds on top of getDefaultFunctionAttributes
2262 ///     and adds declaration-specific, convention-specific, and
2263 ///     frontend-specific logic.  The last is of particular importance:
2264 ///     attributes that restrict how the frontend generates code must be
2265 ///     added here rather than getDefaultFunctionAttributes.
2266 ///
2267 void CodeGenModule::ConstructAttributeList(StringRef Name,
2268                                            const CGFunctionInfo &FI,
2269                                            CGCalleeInfo CalleeInfo,
2270                                            llvm::AttributeList &AttrList,
2271                                            unsigned &CallingConv,
2272                                            bool AttrOnCallSite, bool IsThunk) {
2273   llvm::AttrBuilder FuncAttrs(getLLVMContext());
2274   llvm::AttrBuilder RetAttrs(getLLVMContext());
2275 
2276   // Collect function IR attributes from the CC lowering.
2277   // We'll collect the paramete and result attributes later.
2278   CallingConv = FI.getEffectiveCallingConvention();
2279   if (FI.isNoReturn())
2280     FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2281   if (FI.isCmseNSCall())
2282     FuncAttrs.addAttribute("cmse_nonsecure_call");
2283 
2284   // Collect function IR attributes from the callee prototype if we have one.
2285   AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,
2286                                      CalleeInfo.getCalleeFunctionProtoType());
2287 
2288   const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
2289 
2290   // Attach assumption attributes to the declaration. If this is a call
2291   // site, attach assumptions from the caller to the call as well.
2292   AddAttributesFromAssumes(FuncAttrs, TargetDecl);
2293 
2294   bool HasOptnone = false;
2295   // The NoBuiltinAttr attached to the target FunctionDecl.
2296   const NoBuiltinAttr *NBA = nullptr;
2297 
2298   // Some ABIs may result in additional accesses to arguments that may
2299   // otherwise not be present.
2300   auto AddPotentialArgAccess = [&]() {
2301     llvm::Attribute A = FuncAttrs.getAttribute(llvm::Attribute::Memory);
2302     if (A.isValid())
2303       FuncAttrs.addMemoryAttr(A.getMemoryEffects() |
2304                               llvm::MemoryEffects::argMemOnly());
2305   };
2306 
2307   // Collect function IR attributes based on declaration-specific
2308   // information.
2309   // FIXME: handle sseregparm someday...
2310   if (TargetDecl) {
2311     if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
2312       FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
2313     if (TargetDecl->hasAttr<NoThrowAttr>())
2314       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2315     if (TargetDecl->hasAttr<NoReturnAttr>())
2316       FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2317     if (TargetDecl->hasAttr<ColdAttr>())
2318       FuncAttrs.addAttribute(llvm::Attribute::Cold);
2319     if (TargetDecl->hasAttr<HotAttr>())
2320       FuncAttrs.addAttribute(llvm::Attribute::Hot);
2321     if (TargetDecl->hasAttr<NoDuplicateAttr>())
2322       FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
2323     if (TargetDecl->hasAttr<ConvergentAttr>())
2324       FuncAttrs.addAttribute(llvm::Attribute::Convergent);
2325 
2326     if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
2327       AddAttributesFromFunctionProtoType(
2328           getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
2329       if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
2330         // A sane operator new returns a non-aliasing pointer.
2331         auto Kind = Fn->getDeclName().getCXXOverloadedOperator();
2332         if (getCodeGenOpts().AssumeSaneOperatorNew &&
2333             (Kind == OO_New || Kind == OO_Array_New))
2334           RetAttrs.addAttribute(llvm::Attribute::NoAlias);
2335       }
2336       const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
2337       const bool IsVirtualCall = MD && MD->isVirtual();
2338       // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a
2339       // virtual function. These attributes are not inherited by overloads.
2340       if (!(AttrOnCallSite && IsVirtualCall)) {
2341         if (Fn->isNoReturn())
2342           FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2343         NBA = Fn->getAttr<NoBuiltinAttr>();
2344       }
2345     }
2346 
2347     if (isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl)) {
2348       // Only place nomerge attribute on call sites, never functions. This
2349       // allows it to work on indirect virtual function calls.
2350       if (AttrOnCallSite && TargetDecl->hasAttr<NoMergeAttr>())
2351         FuncAttrs.addAttribute(llvm::Attribute::NoMerge);
2352     }
2353 
2354     // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
2355     if (TargetDecl->hasAttr<ConstAttr>()) {
2356       FuncAttrs.addMemoryAttr(llvm::MemoryEffects::none());
2357       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2358       // gcc specifies that 'const' functions have greater restrictions than
2359       // 'pure' functions, so they also cannot have infinite loops.
2360       FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
2361     } else if (TargetDecl->hasAttr<PureAttr>()) {
2362       FuncAttrs.addMemoryAttr(llvm::MemoryEffects::readOnly());
2363       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2364       // gcc specifies that 'pure' functions cannot have infinite loops.
2365       FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
2366     } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
2367       FuncAttrs.addMemoryAttr(llvm::MemoryEffects::argMemOnly());
2368       FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2369     }
2370     if (TargetDecl->hasAttr<RestrictAttr>())
2371       RetAttrs.addAttribute(llvm::Attribute::NoAlias);
2372     if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&
2373         !CodeGenOpts.NullPointerIsValid)
2374       RetAttrs.addAttribute(llvm::Attribute::NonNull);
2375     if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
2376       FuncAttrs.addAttribute("no_caller_saved_registers");
2377     if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
2378       FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
2379     if (TargetDecl->hasAttr<LeafAttr>())
2380       FuncAttrs.addAttribute(llvm::Attribute::NoCallback);
2381 
2382     HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
2383     if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
2384       std::optional<unsigned> NumElemsParam;
2385       if (AllocSize->getNumElemsParam().isValid())
2386         NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();
2387       FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),
2388                                  NumElemsParam);
2389     }
2390 
2391     if (TargetDecl->hasAttr<OpenCLKernelAttr>()) {
2392       if (getLangOpts().OpenCLVersion <= 120) {
2393         // OpenCL v1.2 Work groups are always uniform
2394         FuncAttrs.addAttribute("uniform-work-group-size", "true");
2395       } else {
2396         // OpenCL v2.0 Work groups may be whether uniform or not.
2397         // '-cl-uniform-work-group-size' compile option gets a hint
2398         // to the compiler that the global work-size be a multiple of
2399         // the work-group size specified to clEnqueueNDRangeKernel
2400         // (i.e. work groups are uniform).
2401         FuncAttrs.addAttribute("uniform-work-group-size",
2402                                llvm::toStringRef(CodeGenOpts.UniformWGSize));
2403       }
2404     }
2405   }
2406 
2407   // Attach "no-builtins" attributes to:
2408   // * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".
2409   // * definitions: "no-builtins" or "no-builtin-<name>" only.
2410   // The attributes can come from:
2411   // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
2412   // * FunctionDecl attributes: __attribute__((no_builtin(...)))
2413   addNoBuiltinAttributes(FuncAttrs, getLangOpts(), NBA);
2414 
2415   // Collect function IR attributes based on global settiings.
2416   getDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
2417 
2418   // Override some default IR attributes based on declaration-specific
2419   // information.
2420   if (TargetDecl) {
2421     if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>())
2422       FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening);
2423     if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())
2424       FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
2425     if (TargetDecl->hasAttr<NoSplitStackAttr>())
2426       FuncAttrs.removeAttribute("split-stack");
2427     if (TargetDecl->hasAttr<ZeroCallUsedRegsAttr>()) {
2428       // A function "__attribute__((...))" overrides the command-line flag.
2429       auto Kind =
2430           TargetDecl->getAttr<ZeroCallUsedRegsAttr>()->getZeroCallUsedRegs();
2431       FuncAttrs.removeAttribute("zero-call-used-regs");
2432       FuncAttrs.addAttribute(
2433           "zero-call-used-regs",
2434           ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind));
2435     }
2436 
2437     // Add NonLazyBind attribute to function declarations when -fno-plt
2438     // is used.
2439     // FIXME: what if we just haven't processed the function definition
2440     // yet, or if it's an external definition like C99 inline?
2441     if (CodeGenOpts.NoPLT) {
2442       if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
2443         if (!Fn->isDefined() && !AttrOnCallSite) {
2444           FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
2445         }
2446       }
2447     }
2448   }
2449 
2450   // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
2451   // functions with -funique-internal-linkage-names.
2452   if (TargetDecl && CodeGenOpts.UniqueInternalLinkageNames) {
2453     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
2454       if (!FD->isExternallyVisible())
2455         FuncAttrs.addAttribute("sample-profile-suffix-elision-policy",
2456                                "selected");
2457     }
2458   }
2459 
2460   // Collect non-call-site function IR attributes from declaration-specific
2461   // information.
2462   if (!AttrOnCallSite) {
2463     if (TargetDecl && TargetDecl->hasAttr<CmseNSEntryAttr>())
2464       FuncAttrs.addAttribute("cmse_nonsecure_entry");
2465 
2466     // Whether tail calls are enabled.
2467     auto shouldDisableTailCalls = [&] {
2468       // Should this be honored in getDefaultFunctionAttributes?
2469       if (CodeGenOpts.DisableTailCalls)
2470         return true;
2471 
2472       if (!TargetDecl)
2473         return false;
2474 
2475       if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
2476           TargetDecl->hasAttr<AnyX86InterruptAttr>())
2477         return true;
2478 
2479       if (CodeGenOpts.NoEscapingBlockTailCalls) {
2480         if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))
2481           if (!BD->doesNotEscape())
2482             return true;
2483       }
2484 
2485       return false;
2486     };
2487     if (shouldDisableTailCalls())
2488       FuncAttrs.addAttribute("disable-tail-calls", "true");
2489 
2490     // CPU/feature overrides.  addDefaultFunctionDefinitionAttributes
2491     // handles these separately to set them based on the global defaults.
2492     GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
2493   }
2494 
2495   // Collect attributes from arguments and return values.
2496   ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
2497 
2498   QualType RetTy = FI.getReturnType();
2499   const ABIArgInfo &RetAI = FI.getReturnInfo();
2500   const llvm::DataLayout &DL = getDataLayout();
2501 
2502   // Determine if the return type could be partially undef
2503   if (CodeGenOpts.EnableNoundefAttrs &&
2504       HasStrictReturn(*this, RetTy, TargetDecl)) {
2505     if (!RetTy->isVoidType() && RetAI.getKind() != ABIArgInfo::Indirect &&
2506         DetermineNoUndef(RetTy, getTypes(), DL, RetAI))
2507       RetAttrs.addAttribute(llvm::Attribute::NoUndef);
2508   }
2509 
2510   switch (RetAI.getKind()) {
2511   case ABIArgInfo::Extend:
2512     if (RetAI.isSignExt())
2513       RetAttrs.addAttribute(llvm::Attribute::SExt);
2514     else
2515       RetAttrs.addAttribute(llvm::Attribute::ZExt);
2516     [[fallthrough]];
2517   case ABIArgInfo::Direct:
2518     if (RetAI.getInReg())
2519       RetAttrs.addAttribute(llvm::Attribute::InReg);
2520 
2521     if (canApplyNoFPClass(RetAI, RetTy, true))
2522       RetAttrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2523 
2524     break;
2525   case ABIArgInfo::Ignore:
2526     break;
2527 
2528   case ABIArgInfo::InAlloca:
2529   case ABIArgInfo::Indirect: {
2530     // inalloca and sret disable readnone and readonly
2531     AddPotentialArgAccess();
2532     break;
2533   }
2534 
2535   case ABIArgInfo::CoerceAndExpand:
2536     break;
2537 
2538   case ABIArgInfo::Expand:
2539   case ABIArgInfo::IndirectAliased:
2540     llvm_unreachable("Invalid ABI kind for return argument");
2541   }
2542 
2543   if (!IsThunk) {
2544     // FIXME: fix this properly, https://reviews.llvm.org/D100388
2545     if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
2546       QualType PTy = RefTy->getPointeeType();
2547       if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
2548         RetAttrs.addDereferenceableAttr(
2549             getMinimumObjectSize(PTy).getQuantity());
2550       if (getTypes().getTargetAddressSpace(PTy) == 0 &&
2551           !CodeGenOpts.NullPointerIsValid)
2552         RetAttrs.addAttribute(llvm::Attribute::NonNull);
2553       if (PTy->isObjectType()) {
2554         llvm::Align Alignment =
2555             getNaturalPointeeTypeAlignment(RetTy).getAsAlign();
2556         RetAttrs.addAlignmentAttr(Alignment);
2557       }
2558     }
2559   }
2560 
2561   bool hasUsedSRet = false;
2562   SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());
2563 
2564   // Attach attributes to sret.
2565   if (IRFunctionArgs.hasSRetArg()) {
2566     llvm::AttrBuilder SRETAttrs(getLLVMContext());
2567     SRETAttrs.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy));
2568     hasUsedSRet = true;
2569     if (RetAI.getInReg())
2570       SRETAttrs.addAttribute(llvm::Attribute::InReg);
2571     SRETAttrs.addAlignmentAttr(RetAI.getIndirectAlign().getQuantity());
2572     ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
2573         llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
2574   }
2575 
2576   // Attach attributes to inalloca argument.
2577   if (IRFunctionArgs.hasInallocaArg()) {
2578     llvm::AttrBuilder Attrs(getLLVMContext());
2579     Attrs.addInAllocaAttr(FI.getArgStruct());
2580     ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
2581         llvm::AttributeSet::get(getLLVMContext(), Attrs);
2582   }
2583 
2584   // Apply `nonnull`, `dereferencable(N)` and `align N` to the `this` argument,
2585   // unless this is a thunk function.
2586   // FIXME: fix this properly, https://reviews.llvm.org/D100388
2587   if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&
2588       !FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {
2589     auto IRArgs = IRFunctionArgs.getIRArgs(0);
2590 
2591     assert(IRArgs.second == 1 && "Expected only a single `this` pointer.");
2592 
2593     llvm::AttrBuilder Attrs(getLLVMContext());
2594 
2595     QualType ThisTy =
2596         FI.arg_begin()->type.castAs<PointerType>()->getPointeeType();
2597 
2598     if (!CodeGenOpts.NullPointerIsValid &&
2599         getTypes().getTargetAddressSpace(FI.arg_begin()->type) == 0) {
2600       Attrs.addAttribute(llvm::Attribute::NonNull);
2601       Attrs.addDereferenceableAttr(getMinimumObjectSize(ThisTy).getQuantity());
2602     } else {
2603       // FIXME dereferenceable should be correct here, regardless of
2604       // NullPointerIsValid. However, dereferenceable currently does not always
2605       // respect NullPointerIsValid and may imply nonnull and break the program.
2606       // See https://reviews.llvm.org/D66618 for discussions.
2607       Attrs.addDereferenceableOrNullAttr(
2608           getMinimumObjectSize(
2609               FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())
2610               .getQuantity());
2611     }
2612 
2613     llvm::Align Alignment =
2614         getNaturalTypeAlignment(ThisTy, /*BaseInfo=*/nullptr,
2615                                 /*TBAAInfo=*/nullptr, /*forPointeeType=*/true)
2616             .getAsAlign();
2617     Attrs.addAlignmentAttr(Alignment);
2618 
2619     ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);
2620   }
2621 
2622   unsigned ArgNo = 0;
2623   for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),
2624                                           E = FI.arg_end();
2625        I != E; ++I, ++ArgNo) {
2626     QualType ParamType = I->type;
2627     const ABIArgInfo &AI = I->info;
2628     llvm::AttrBuilder Attrs(getLLVMContext());
2629 
2630     // Add attribute for padding argument, if necessary.
2631     if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
2632       if (AI.getPaddingInReg()) {
2633         ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
2634             llvm::AttributeSet::get(
2635                 getLLVMContext(),
2636                 llvm::AttrBuilder(getLLVMContext()).addAttribute(llvm::Attribute::InReg));
2637       }
2638     }
2639 
2640     // Decide whether the argument we're handling could be partially undef
2641     if (CodeGenOpts.EnableNoundefAttrs &&
2642         DetermineNoUndef(ParamType, getTypes(), DL, AI)) {
2643       Attrs.addAttribute(llvm::Attribute::NoUndef);
2644     }
2645 
2646     // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
2647     // have the corresponding parameter variable.  It doesn't make
2648     // sense to do it here because parameters are so messed up.
2649     switch (AI.getKind()) {
2650     case ABIArgInfo::Extend:
2651       if (AI.isSignExt())
2652         Attrs.addAttribute(llvm::Attribute::SExt);
2653       else
2654         Attrs.addAttribute(llvm::Attribute::ZExt);
2655       [[fallthrough]];
2656     case ABIArgInfo::Direct:
2657       if (ArgNo == 0 && FI.isChainCall())
2658         Attrs.addAttribute(llvm::Attribute::Nest);
2659       else if (AI.getInReg())
2660         Attrs.addAttribute(llvm::Attribute::InReg);
2661       Attrs.addStackAlignmentAttr(llvm::MaybeAlign(AI.getDirectAlign()));
2662 
2663       if (canApplyNoFPClass(AI, ParamType, false))
2664         Attrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2665       break;
2666     case ABIArgInfo::Indirect: {
2667       if (AI.getInReg())
2668         Attrs.addAttribute(llvm::Attribute::InReg);
2669 
2670       if (AI.getIndirectByVal())
2671         Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));
2672 
2673       auto *Decl = ParamType->getAsRecordDecl();
2674       if (CodeGenOpts.PassByValueIsNoAlias && Decl &&
2675           Decl->getArgPassingRestrictions() == RecordDecl::APK_CanPassInRegs)
2676         // When calling the function, the pointer passed in will be the only
2677         // reference to the underlying object. Mark it accordingly.
2678         Attrs.addAttribute(llvm::Attribute::NoAlias);
2679 
2680       // TODO: We could add the byref attribute if not byval, but it would
2681       // require updating many testcases.
2682 
2683       CharUnits Align = AI.getIndirectAlign();
2684 
2685       // In a byval argument, it is important that the required
2686       // alignment of the type is honored, as LLVM might be creating a
2687       // *new* stack object, and needs to know what alignment to give
2688       // it. (Sometimes it can deduce a sensible alignment on its own,
2689       // but not if clang decides it must emit a packed struct, or the
2690       // user specifies increased alignment requirements.)
2691       //
2692       // This is different from indirect *not* byval, where the object
2693       // exists already, and the align attribute is purely
2694       // informative.
2695       assert(!Align.isZero());
2696 
2697       // For now, only add this when we have a byval argument.
2698       // TODO: be less lazy about updating test cases.
2699       if (AI.getIndirectByVal())
2700         Attrs.addAlignmentAttr(Align.getQuantity());
2701 
2702       // byval disables readnone and readonly.
2703       AddPotentialArgAccess();
2704       break;
2705     }
2706     case ABIArgInfo::IndirectAliased: {
2707       CharUnits Align = AI.getIndirectAlign();
2708       Attrs.addByRefAttr(getTypes().ConvertTypeForMem(ParamType));
2709       Attrs.addAlignmentAttr(Align.getQuantity());
2710       break;
2711     }
2712     case ABIArgInfo::Ignore:
2713     case ABIArgInfo::Expand:
2714     case ABIArgInfo::CoerceAndExpand:
2715       break;
2716 
2717     case ABIArgInfo::InAlloca:
2718       // inalloca disables readnone and readonly.
2719       AddPotentialArgAccess();
2720       continue;
2721     }
2722 
2723     if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
2724       QualType PTy = RefTy->getPointeeType();
2725       if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
2726         Attrs.addDereferenceableAttr(
2727             getMinimumObjectSize(PTy).getQuantity());
2728       if (getTypes().getTargetAddressSpace(PTy) == 0 &&
2729           !CodeGenOpts.NullPointerIsValid)
2730         Attrs.addAttribute(llvm::Attribute::NonNull);
2731       if (PTy->isObjectType()) {
2732         llvm::Align Alignment =
2733             getNaturalPointeeTypeAlignment(ParamType).getAsAlign();
2734         Attrs.addAlignmentAttr(Alignment);
2735       }
2736     }
2737 
2738     // From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:
2739     // > For arguments to a __kernel function declared to be a pointer to a
2740     // > data type, the OpenCL compiler can assume that the pointee is always
2741     // > appropriately aligned as required by the data type.
2742     if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>() &&
2743         ParamType->isPointerType()) {
2744       QualType PTy = ParamType->getPointeeType();
2745       if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2746         llvm::Align Alignment =
2747             getNaturalPointeeTypeAlignment(ParamType).getAsAlign();
2748         Attrs.addAlignmentAttr(Alignment);
2749       }
2750     }
2751 
2752     switch (FI.getExtParameterInfo(ArgNo).getABI()) {
2753     case ParameterABI::Ordinary:
2754       break;
2755 
2756     case ParameterABI::SwiftIndirectResult: {
2757       // Add 'sret' if we haven't already used it for something, but
2758       // only if the result is void.
2759       if (!hasUsedSRet && RetTy->isVoidType()) {
2760         Attrs.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType));
2761         hasUsedSRet = true;
2762       }
2763 
2764       // Add 'noalias' in either case.
2765       Attrs.addAttribute(llvm::Attribute::NoAlias);
2766 
2767       // Add 'dereferenceable' and 'alignment'.
2768       auto PTy = ParamType->getPointeeType();
2769       if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2770         auto info = getContext().getTypeInfoInChars(PTy);
2771         Attrs.addDereferenceableAttr(info.Width.getQuantity());
2772         Attrs.addAlignmentAttr(info.Align.getAsAlign());
2773       }
2774       break;
2775     }
2776 
2777     case ParameterABI::SwiftErrorResult:
2778       Attrs.addAttribute(llvm::Attribute::SwiftError);
2779       break;
2780 
2781     case ParameterABI::SwiftContext:
2782       Attrs.addAttribute(llvm::Attribute::SwiftSelf);
2783       break;
2784 
2785     case ParameterABI::SwiftAsyncContext:
2786       Attrs.addAttribute(llvm::Attribute::SwiftAsync);
2787       break;
2788     }
2789 
2790     if (FI.getExtParameterInfo(ArgNo).isNoEscape())
2791       Attrs.addAttribute(llvm::Attribute::NoCapture);
2792 
2793     if (Attrs.hasAttributes()) {
2794       unsigned FirstIRArg, NumIRArgs;
2795       std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2796       for (unsigned i = 0; i < NumIRArgs; i++)
2797         ArgAttrs[FirstIRArg + i] = ArgAttrs[FirstIRArg + i].addAttributes(
2798             getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs));
2799     }
2800   }
2801   assert(ArgNo == FI.arg_size());
2802 
2803   AttrList = llvm::AttributeList::get(
2804       getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
2805       llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
2806 }
2807 
2808 /// An argument came in as a promoted argument; demote it back to its
2809 /// declared type.
2810 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
2811                                          const VarDecl *var,
2812                                          llvm::Value *value) {
2813   llvm::Type *varType = CGF.ConvertType(var->getType());
2814 
2815   // This can happen with promotions that actually don't change the
2816   // underlying type, like the enum promotions.
2817   if (value->getType() == varType) return value;
2818 
2819   assert((varType->isIntegerTy() || varType->isFloatingPointTy())
2820          && "unexpected promotion type");
2821 
2822   if (isa<llvm::IntegerType>(varType))
2823     return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
2824 
2825   return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
2826 }
2827 
2828 /// Returns the attribute (either parameter attribute, or function
2829 /// attribute), which declares argument ArgNo to be non-null.
2830 static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
2831                                          QualType ArgType, unsigned ArgNo) {
2832   // FIXME: __attribute__((nonnull)) can also be applied to:
2833   //   - references to pointers, where the pointee is known to be
2834   //     nonnull (apparently a Clang extension)
2835   //   - transparent unions containing pointers
2836   // In the former case, LLVM IR cannot represent the constraint. In
2837   // the latter case, we have no guarantee that the transparent union
2838   // is in fact passed as a pointer.
2839   if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
2840     return nullptr;
2841   // First, check attribute on parameter itself.
2842   if (PVD) {
2843     if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
2844       return ParmNNAttr;
2845   }
2846   // Check function attributes.
2847   if (!FD)
2848     return nullptr;
2849   for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
2850     if (NNAttr->isNonNull(ArgNo))
2851       return NNAttr;
2852   }
2853   return nullptr;
2854 }
2855 
2856 namespace {
2857   struct CopyBackSwiftError final : EHScopeStack::Cleanup {
2858     Address Temp;
2859     Address Arg;
2860     CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
2861     void Emit(CodeGenFunction &CGF, Flags flags) override {
2862       llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
2863       CGF.Builder.CreateStore(errorValue, Arg);
2864     }
2865   };
2866 }
2867 
2868 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
2869                                          llvm::Function *Fn,
2870                                          const FunctionArgList &Args) {
2871   if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
2872     // Naked functions don't have prologues.
2873     return;
2874 
2875   // If this is an implicit-return-zero function, go ahead and
2876   // initialize the return value.  TODO: it might be nice to have
2877   // a more general mechanism for this that didn't require synthesized
2878   // return statements.
2879   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
2880     if (FD->hasImplicitReturnZero()) {
2881       QualType RetTy = FD->getReturnType().getUnqualifiedType();
2882       llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
2883       llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
2884       Builder.CreateStore(Zero, ReturnValue);
2885     }
2886   }
2887 
2888   // FIXME: We no longer need the types from FunctionArgList; lift up and
2889   // simplify.
2890 
2891   ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
2892   assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());
2893 
2894   // If we're using inalloca, all the memory arguments are GEPs off of the last
2895   // parameter, which is a pointer to the complete memory area.
2896   Address ArgStruct = Address::invalid();
2897   if (IRFunctionArgs.hasInallocaArg())
2898     ArgStruct = Address(Fn->getArg(IRFunctionArgs.getInallocaArgNo()),
2899                         FI.getArgStruct(), FI.getArgStructAlignment());
2900 
2901   // Name the struct return parameter.
2902   if (IRFunctionArgs.hasSRetArg()) {
2903     auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());
2904     AI->setName("agg.result");
2905     AI->addAttr(llvm::Attribute::NoAlias);
2906   }
2907 
2908   // Track if we received the parameter as a pointer (indirect, byval, or
2909   // inalloca).  If already have a pointer, EmitParmDecl doesn't need to copy it
2910   // into a local alloca for us.
2911   SmallVector<ParamValue, 16> ArgVals;
2912   ArgVals.reserve(Args.size());
2913 
2914   // Create a pointer value for every parameter declaration.  This usually
2915   // entails copying one or more LLVM IR arguments into an alloca.  Don't push
2916   // any cleanups or do anything that might unwind.  We do that separately, so
2917   // we can push the cleanups in the correct order for the ABI.
2918   assert(FI.arg_size() == Args.size() &&
2919          "Mismatch between function signature & arguments.");
2920   unsigned ArgNo = 0;
2921   CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
2922   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
2923        i != e; ++i, ++info_it, ++ArgNo) {
2924     const VarDecl *Arg = *i;
2925     const ABIArgInfo &ArgI = info_it->info;
2926 
2927     bool isPromoted =
2928       isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
2929     // We are converting from ABIArgInfo type to VarDecl type directly, unless
2930     // the parameter is promoted. In this case we convert to
2931     // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
2932     QualType Ty = isPromoted ? info_it->type : Arg->getType();
2933     assert(hasScalarEvaluationKind(Ty) ==
2934            hasScalarEvaluationKind(Arg->getType()));
2935 
2936     unsigned FirstIRArg, NumIRArgs;
2937     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2938 
2939     switch (ArgI.getKind()) {
2940     case ABIArgInfo::InAlloca: {
2941       assert(NumIRArgs == 0);
2942       auto FieldIndex = ArgI.getInAllocaFieldIndex();
2943       Address V =
2944           Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName());
2945       if (ArgI.getInAllocaIndirect())
2946         V = Address(Builder.CreateLoad(V), ConvertTypeForMem(Ty),
2947                     getContext().getTypeAlignInChars(Ty));
2948       ArgVals.push_back(ParamValue::forIndirect(V));
2949       break;
2950     }
2951 
2952     case ABIArgInfo::Indirect:
2953     case ABIArgInfo::IndirectAliased: {
2954       assert(NumIRArgs == 1);
2955       Address ParamAddr = Address(Fn->getArg(FirstIRArg), ConvertTypeForMem(Ty),
2956                                   ArgI.getIndirectAlign(), KnownNonNull);
2957 
2958       if (!hasScalarEvaluationKind(Ty)) {
2959         // Aggregates and complex variables are accessed by reference. All we
2960         // need to do is realign the value, if requested. Also, if the address
2961         // may be aliased, copy it to ensure that the parameter variable is
2962         // mutable and has a unique adress, as C requires.
2963         Address V = ParamAddr;
2964         if (ArgI.getIndirectRealign() || ArgI.isIndirectAliased()) {
2965           Address AlignedTemp = CreateMemTemp(Ty, "coerce");
2966 
2967           // Copy from the incoming argument pointer to the temporary with the
2968           // appropriate alignment.
2969           //
2970           // FIXME: We should have a common utility for generating an aggregate
2971           // copy.
2972           CharUnits Size = getContext().getTypeSizeInChars(Ty);
2973           Builder.CreateMemCpy(
2974               AlignedTemp.getPointer(), AlignedTemp.getAlignment().getAsAlign(),
2975               ParamAddr.getPointer(), ParamAddr.getAlignment().getAsAlign(),
2976               llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()));
2977           V = AlignedTemp;
2978         }
2979         ArgVals.push_back(ParamValue::forIndirect(V));
2980       } else {
2981         // Load scalar value from indirect argument.
2982         llvm::Value *V =
2983             EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());
2984 
2985         if (isPromoted)
2986           V = emitArgumentDemotion(*this, Arg, V);
2987         ArgVals.push_back(ParamValue::forDirect(V));
2988       }
2989       break;
2990     }
2991 
2992     case ABIArgInfo::Extend:
2993     case ABIArgInfo::Direct: {
2994       auto AI = Fn->getArg(FirstIRArg);
2995       llvm::Type *LTy = ConvertType(Arg->getType());
2996 
2997       // Prepare parameter attributes. So far, only attributes for pointer
2998       // parameters are prepared. See
2999       // http://llvm.org/docs/LangRef.html#paramattrs.
3000       if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&
3001           ArgI.getCoerceToType()->isPointerTy()) {
3002         assert(NumIRArgs == 1);
3003 
3004         if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
3005           // Set `nonnull` attribute if any.
3006           if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
3007                              PVD->getFunctionScopeIndex()) &&
3008               !CGM.getCodeGenOpts().NullPointerIsValid)
3009             AI->addAttr(llvm::Attribute::NonNull);
3010 
3011           QualType OTy = PVD->getOriginalType();
3012           if (const auto *ArrTy =
3013               getContext().getAsConstantArrayType(OTy)) {
3014             // A C99 array parameter declaration with the static keyword also
3015             // indicates dereferenceability, and if the size is constant we can
3016             // use the dereferenceable attribute (which requires the size in
3017             // bytes).
3018             if (ArrTy->getSizeModifier() == ArrayType::Static) {
3019               QualType ETy = ArrTy->getElementType();
3020               llvm::Align Alignment =
3021                   CGM.getNaturalTypeAlignment(ETy).getAsAlign();
3022               AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));
3023               uint64_t ArrSize = ArrTy->getSize().getZExtValue();
3024               if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
3025                   ArrSize) {
3026                 llvm::AttrBuilder Attrs(getLLVMContext());
3027                 Attrs.addDereferenceableAttr(
3028                     getContext().getTypeSizeInChars(ETy).getQuantity() *
3029                     ArrSize);
3030                 AI->addAttrs(Attrs);
3031               } else if (getContext().getTargetInfo().getNullPointerValue(
3032                              ETy.getAddressSpace()) == 0 &&
3033                          !CGM.getCodeGenOpts().NullPointerIsValid) {
3034                 AI->addAttr(llvm::Attribute::NonNull);
3035               }
3036             }
3037           } else if (const auto *ArrTy =
3038                      getContext().getAsVariableArrayType(OTy)) {
3039             // For C99 VLAs with the static keyword, we don't know the size so
3040             // we can't use the dereferenceable attribute, but in addrspace(0)
3041             // we know that it must be nonnull.
3042             if (ArrTy->getSizeModifier() == VariableArrayType::Static) {
3043               QualType ETy = ArrTy->getElementType();
3044               llvm::Align Alignment =
3045                   CGM.getNaturalTypeAlignment(ETy).getAsAlign();
3046               AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));
3047               if (!getTypes().getTargetAddressSpace(ETy) &&
3048                   !CGM.getCodeGenOpts().NullPointerIsValid)
3049                 AI->addAttr(llvm::Attribute::NonNull);
3050             }
3051           }
3052 
3053           // Set `align` attribute if any.
3054           const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
3055           if (!AVAttr)
3056             if (const auto *TOTy = OTy->getAs<TypedefType>())
3057               AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
3058           if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) {
3059             // If alignment-assumption sanitizer is enabled, we do *not* add
3060             // alignment attribute here, but emit normal alignment assumption,
3061             // so the UBSAN check could function.
3062             llvm::ConstantInt *AlignmentCI =
3063                 cast<llvm::ConstantInt>(EmitScalarExpr(AVAttr->getAlignment()));
3064             uint64_t AlignmentInt =
3065                 AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment);
3066             if (AI->getParamAlign().valueOrOne() < AlignmentInt) {
3067               AI->removeAttr(llvm::Attribute::AttrKind::Alignment);
3068               AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(
3069                   llvm::Align(AlignmentInt)));
3070             }
3071           }
3072         }
3073 
3074         // Set 'noalias' if an argument type has the `restrict` qualifier.
3075         if (Arg->getType().isRestrictQualified())
3076           AI->addAttr(llvm::Attribute::NoAlias);
3077       }
3078 
3079       // Prepare the argument value. If we have the trivial case, handle it
3080       // with no muss and fuss.
3081       if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
3082           ArgI.getCoerceToType() == ConvertType(Ty) &&
3083           ArgI.getDirectOffset() == 0) {
3084         assert(NumIRArgs == 1);
3085 
3086         // LLVM expects swifterror parameters to be used in very restricted
3087         // ways.  Copy the value into a less-restricted temporary.
3088         llvm::Value *V = AI;
3089         if (FI.getExtParameterInfo(ArgNo).getABI()
3090               == ParameterABI::SwiftErrorResult) {
3091           QualType pointeeTy = Ty->getPointeeType();
3092           assert(pointeeTy->isPointerType());
3093           Address temp =
3094             CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
3095           Address arg(V, ConvertTypeForMem(pointeeTy),
3096                       getContext().getTypeAlignInChars(pointeeTy));
3097           llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
3098           Builder.CreateStore(incomingErrorValue, temp);
3099           V = temp.getPointer();
3100 
3101           // Push a cleanup to copy the value back at the end of the function.
3102           // The convention does not guarantee that the value will be written
3103           // back if the function exits with an unwind exception.
3104           EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
3105         }
3106 
3107         // Ensure the argument is the correct type.
3108         if (V->getType() != ArgI.getCoerceToType())
3109           V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
3110 
3111         if (isPromoted)
3112           V = emitArgumentDemotion(*this, Arg, V);
3113 
3114         // Because of merging of function types from multiple decls it is
3115         // possible for the type of an argument to not match the corresponding
3116         // type in the function type. Since we are codegening the callee
3117         // in here, add a cast to the argument type.
3118         llvm::Type *LTy = ConvertType(Arg->getType());
3119         if (V->getType() != LTy)
3120           V = Builder.CreateBitCast(V, LTy);
3121 
3122         ArgVals.push_back(ParamValue::forDirect(V));
3123         break;
3124       }
3125 
3126       // VLST arguments are coerced to VLATs at the function boundary for
3127       // ABI consistency. If this is a VLST that was coerced to
3128       // a VLAT at the function boundary and the types match up, use
3129       // llvm.vector.extract to convert back to the original VLST.
3130       if (auto *VecTyTo = dyn_cast<llvm::FixedVectorType>(ConvertType(Ty))) {
3131         llvm::Value *Coerced = Fn->getArg(FirstIRArg);
3132         if (auto *VecTyFrom =
3133                 dyn_cast<llvm::ScalableVectorType>(Coerced->getType())) {
3134           // If we are casting a scalable 16 x i1 predicate vector to a fixed i8
3135           // vector, bitcast the source and use a vector extract.
3136           auto PredType =
3137               llvm::ScalableVectorType::get(Builder.getInt1Ty(), 16);
3138           if (VecTyFrom == PredType &&
3139               VecTyTo->getElementType() == Builder.getInt8Ty()) {
3140             VecTyFrom = llvm::ScalableVectorType::get(Builder.getInt8Ty(), 2);
3141             Coerced = Builder.CreateBitCast(Coerced, VecTyFrom);
3142           }
3143           if (VecTyFrom->getElementType() == VecTyTo->getElementType()) {
3144             llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
3145 
3146             assert(NumIRArgs == 1);
3147             Coerced->setName(Arg->getName() + ".coerce");
3148             ArgVals.push_back(ParamValue::forDirect(Builder.CreateExtractVector(
3149                 VecTyTo, Coerced, Zero, "cast.fixed")));
3150             break;
3151           }
3152         }
3153       }
3154 
3155       Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),
3156                                      Arg->getName());
3157 
3158       // Pointer to store into.
3159       Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
3160 
3161       // Fast-isel and the optimizer generally like scalar values better than
3162       // FCAs, so we flatten them if this is safe to do for this argument.
3163       llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
3164       if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
3165           STy->getNumElements() > 1) {
3166         llvm::TypeSize StructSize = CGM.getDataLayout().getTypeAllocSize(STy);
3167         llvm::TypeSize PtrElementSize =
3168             CGM.getDataLayout().getTypeAllocSize(Ptr.getElementType());
3169         if (StructSize.isScalable()) {
3170           assert(STy->containsHomogeneousScalableVectorTypes() &&
3171                  "ABI only supports structure with homogeneous scalable vector "
3172                  "type");
3173           assert(StructSize == PtrElementSize &&
3174                  "Only allow non-fractional movement of structure with"
3175                  "homogeneous scalable vector type");
3176           assert(STy->getNumElements() == NumIRArgs);
3177 
3178           llvm::Value *LoadedStructValue = llvm::PoisonValue::get(STy);
3179           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3180             auto *AI = Fn->getArg(FirstIRArg + i);
3181             AI->setName(Arg->getName() + ".coerce" + Twine(i));
3182             LoadedStructValue =
3183                 Builder.CreateInsertValue(LoadedStructValue, AI, i);
3184           }
3185 
3186           Builder.CreateStore(LoadedStructValue, Ptr);
3187         } else {
3188           uint64_t SrcSize = StructSize.getFixedValue();
3189           uint64_t DstSize = PtrElementSize.getFixedValue();
3190 
3191           Address AddrToStoreInto = Address::invalid();
3192           if (SrcSize <= DstSize) {
3193             AddrToStoreInto = Ptr.withElementType(STy);
3194           } else {
3195             AddrToStoreInto =
3196                 CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
3197           }
3198 
3199           assert(STy->getNumElements() == NumIRArgs);
3200           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3201             auto AI = Fn->getArg(FirstIRArg + i);
3202             AI->setName(Arg->getName() + ".coerce" + Twine(i));
3203             Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
3204             Builder.CreateStore(AI, EltPtr);
3205           }
3206 
3207           if (SrcSize > DstSize) {
3208             Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
3209           }
3210         }
3211       } else {
3212         // Simple case, just do a coerced store of the argument into the alloca.
3213         assert(NumIRArgs == 1);
3214         auto AI = Fn->getArg(FirstIRArg);
3215         AI->setName(Arg->getName() + ".coerce");
3216         CreateCoercedStore(AI, Ptr, /*DstIsVolatile=*/false, *this);
3217       }
3218 
3219       // Match to what EmitParmDecl is expecting for this type.
3220       if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
3221         llvm::Value *V =
3222             EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
3223         if (isPromoted)
3224           V = emitArgumentDemotion(*this, Arg, V);
3225         ArgVals.push_back(ParamValue::forDirect(V));
3226       } else {
3227         ArgVals.push_back(ParamValue::forIndirect(Alloca));
3228       }
3229       break;
3230     }
3231 
3232     case ABIArgInfo::CoerceAndExpand: {
3233       // Reconstruct into a temporary.
3234       Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
3235       ArgVals.push_back(ParamValue::forIndirect(alloca));
3236 
3237       auto coercionType = ArgI.getCoerceAndExpandType();
3238       alloca = alloca.withElementType(coercionType);
3239 
3240       unsigned argIndex = FirstIRArg;
3241       for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
3242         llvm::Type *eltType = coercionType->getElementType(i);
3243         if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
3244           continue;
3245 
3246         auto eltAddr = Builder.CreateStructGEP(alloca, i);
3247         auto elt = Fn->getArg(argIndex++);
3248         Builder.CreateStore(elt, eltAddr);
3249       }
3250       assert(argIndex == FirstIRArg + NumIRArgs);
3251       break;
3252     }
3253 
3254     case ABIArgInfo::Expand: {
3255       // If this structure was expanded into multiple arguments then
3256       // we need to create a temporary and reconstruct it from the
3257       // arguments.
3258       Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
3259       LValue LV = MakeAddrLValue(Alloca, Ty);
3260       ArgVals.push_back(ParamValue::forIndirect(Alloca));
3261 
3262       auto FnArgIter = Fn->arg_begin() + FirstIRArg;
3263       ExpandTypeFromArgs(Ty, LV, FnArgIter);
3264       assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);
3265       for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
3266         auto AI = Fn->getArg(FirstIRArg + i);
3267         AI->setName(Arg->getName() + "." + Twine(i));
3268       }
3269       break;
3270     }
3271 
3272     case ABIArgInfo::Ignore:
3273       assert(NumIRArgs == 0);
3274       // Initialize the local variable appropriately.
3275       if (!hasScalarEvaluationKind(Ty)) {
3276         ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
3277       } else {
3278         llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
3279         ArgVals.push_back(ParamValue::forDirect(U));
3280       }
3281       break;
3282     }
3283   }
3284 
3285   if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3286     for (int I = Args.size() - 1; I >= 0; --I)
3287       EmitParmDecl(*Args[I], ArgVals[I], I + 1);
3288   } else {
3289     for (unsigned I = 0, E = Args.size(); I != E; ++I)
3290       EmitParmDecl(*Args[I], ArgVals[I], I + 1);
3291   }
3292 }
3293 
3294 static void eraseUnusedBitCasts(llvm::Instruction *insn) {
3295   while (insn->use_empty()) {
3296     llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
3297     if (!bitcast) return;
3298 
3299     // This is "safe" because we would have used a ConstantExpr otherwise.
3300     insn = cast<llvm::Instruction>(bitcast->getOperand(0));
3301     bitcast->eraseFromParent();
3302   }
3303 }
3304 
3305 /// Try to emit a fused autorelease of a return result.
3306 static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
3307                                                     llvm::Value *result) {
3308   // We must be immediately followed the cast.
3309   llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
3310   if (BB->empty()) return nullptr;
3311   if (&BB->back() != result) return nullptr;
3312 
3313   llvm::Type *resultType = result->getType();
3314 
3315   // result is in a BasicBlock and is therefore an Instruction.
3316   llvm::Instruction *generator = cast<llvm::Instruction>(result);
3317 
3318   SmallVector<llvm::Instruction *, 4> InstsToKill;
3319 
3320   // Look for:
3321   //  %generator = bitcast %type1* %generator2 to %type2*
3322   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
3323     // We would have emitted this as a constant if the operand weren't
3324     // an Instruction.
3325     generator = cast<llvm::Instruction>(bitcast->getOperand(0));
3326 
3327     // Require the generator to be immediately followed by the cast.
3328     if (generator->getNextNode() != bitcast)
3329       return nullptr;
3330 
3331     InstsToKill.push_back(bitcast);
3332   }
3333 
3334   // Look for:
3335   //   %generator = call i8* @objc_retain(i8* %originalResult)
3336   // or
3337   //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
3338   llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
3339   if (!call) return nullptr;
3340 
3341   bool doRetainAutorelease;
3342 
3343   if (call->getCalledOperand() == CGF.CGM.getObjCEntrypoints().objc_retain) {
3344     doRetainAutorelease = true;
3345   } else if (call->getCalledOperand() ==
3346              CGF.CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue) {
3347     doRetainAutorelease = false;
3348 
3349     // If we emitted an assembly marker for this call (and the
3350     // ARCEntrypoints field should have been set if so), go looking
3351     // for that call.  If we can't find it, we can't do this
3352     // optimization.  But it should always be the immediately previous
3353     // instruction, unless we needed bitcasts around the call.
3354     if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) {
3355       llvm::Instruction *prev = call->getPrevNode();
3356       assert(prev);
3357       if (isa<llvm::BitCastInst>(prev)) {
3358         prev = prev->getPrevNode();
3359         assert(prev);
3360       }
3361       assert(isa<llvm::CallInst>(prev));
3362       assert(cast<llvm::CallInst>(prev)->getCalledOperand() ==
3363              CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker);
3364       InstsToKill.push_back(prev);
3365     }
3366   } else {
3367     return nullptr;
3368   }
3369 
3370   result = call->getArgOperand(0);
3371   InstsToKill.push_back(call);
3372 
3373   // Keep killing bitcasts, for sanity.  Note that we no longer care
3374   // about precise ordering as long as there's exactly one use.
3375   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
3376     if (!bitcast->hasOneUse()) break;
3377     InstsToKill.push_back(bitcast);
3378     result = bitcast->getOperand(0);
3379   }
3380 
3381   // Delete all the unnecessary instructions, from latest to earliest.
3382   for (auto *I : InstsToKill)
3383     I->eraseFromParent();
3384 
3385   // Do the fused retain/autorelease if we were asked to.
3386   if (doRetainAutorelease)
3387     result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
3388 
3389   // Cast back to the result type.
3390   return CGF.Builder.CreateBitCast(result, resultType);
3391 }
3392 
3393 /// If this is a +1 of the value of an immutable 'self', remove it.
3394 static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
3395                                           llvm::Value *result) {
3396   // This is only applicable to a method with an immutable 'self'.
3397   const ObjCMethodDecl *method =
3398     dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
3399   if (!method) return nullptr;
3400   const VarDecl *self = method->getSelfDecl();
3401   if (!self->getType().isConstQualified()) return nullptr;
3402 
3403   // Look for a retain call.
3404   llvm::CallInst *retainCall =
3405     dyn_cast<llvm::CallInst>(result->stripPointerCasts());
3406   if (!retainCall || retainCall->getCalledOperand() !=
3407                          CGF.CGM.getObjCEntrypoints().objc_retain)
3408     return nullptr;
3409 
3410   // Look for an ordinary load of 'self'.
3411   llvm::Value *retainedValue = retainCall->getArgOperand(0);
3412   llvm::LoadInst *load =
3413     dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
3414   if (!load || load->isAtomic() || load->isVolatile() ||
3415       load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getPointer())
3416     return nullptr;
3417 
3418   // Okay!  Burn it all down.  This relies for correctness on the
3419   // assumption that the retain is emitted as part of the return and
3420   // that thereafter everything is used "linearly".
3421   llvm::Type *resultType = result->getType();
3422   eraseUnusedBitCasts(cast<llvm::Instruction>(result));
3423   assert(retainCall->use_empty());
3424   retainCall->eraseFromParent();
3425   eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
3426 
3427   return CGF.Builder.CreateBitCast(load, resultType);
3428 }
3429 
3430 /// Emit an ARC autorelease of the result of a function.
3431 ///
3432 /// \return the value to actually return from the function
3433 static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
3434                                             llvm::Value *result) {
3435   // If we're returning 'self', kill the initial retain.  This is a
3436   // heuristic attempt to "encourage correctness" in the really unfortunate
3437   // case where we have a return of self during a dealloc and we desperately
3438   // need to avoid the possible autorelease.
3439   if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
3440     return self;
3441 
3442   // At -O0, try to emit a fused retain/autorelease.
3443   if (CGF.shouldUseFusedARCCalls())
3444     if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
3445       return fused;
3446 
3447   return CGF.EmitARCAutoreleaseReturnValue(result);
3448 }
3449 
3450 /// Heuristically search for a dominating store to the return-value slot.
3451 static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
3452   // Check if a User is a store which pointerOperand is the ReturnValue.
3453   // We are looking for stores to the ReturnValue, not for stores of the
3454   // ReturnValue to some other location.
3455   auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
3456     auto *SI = dyn_cast<llvm::StoreInst>(U);
3457     if (!SI || SI->getPointerOperand() != CGF.ReturnValue.getPointer() ||
3458         SI->getValueOperand()->getType() != CGF.ReturnValue.getElementType())
3459       return nullptr;
3460     // These aren't actually possible for non-coerced returns, and we
3461     // only care about non-coerced returns on this code path.
3462     assert(!SI->isAtomic() && !SI->isVolatile());
3463     return SI;
3464   };
3465   // If there are multiple uses of the return-value slot, just check
3466   // for something immediately preceding the IP.  Sometimes this can
3467   // happen with how we generate implicit-returns; it can also happen
3468   // with noreturn cleanups.
3469   if (!CGF.ReturnValue.getPointer()->hasOneUse()) {
3470     llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
3471     if (IP->empty()) return nullptr;
3472 
3473     // Look at directly preceding instruction, skipping bitcasts and lifetime
3474     // markers.
3475     for (llvm::Instruction &I : make_range(IP->rbegin(), IP->rend())) {
3476       if (isa<llvm::BitCastInst>(&I))
3477         continue;
3478       if (auto *II = dyn_cast<llvm::IntrinsicInst>(&I))
3479         if (II->getIntrinsicID() == llvm::Intrinsic::lifetime_end)
3480           continue;
3481 
3482       return GetStoreIfValid(&I);
3483     }
3484     return nullptr;
3485   }
3486 
3487   llvm::StoreInst *store =
3488       GetStoreIfValid(CGF.ReturnValue.getPointer()->user_back());
3489   if (!store) return nullptr;
3490 
3491   // Now do a first-and-dirty dominance check: just walk up the
3492   // single-predecessors chain from the current insertion point.
3493   llvm::BasicBlock *StoreBB = store->getParent();
3494   llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
3495   llvm::SmallPtrSet<llvm::BasicBlock *, 4> SeenBBs;
3496   while (IP != StoreBB) {
3497     if (!SeenBBs.insert(IP).second || !(IP = IP->getSinglePredecessor()))
3498       return nullptr;
3499   }
3500 
3501   // Okay, the store's basic block dominates the insertion point; we
3502   // can do our thing.
3503   return store;
3504 }
3505 
3506 // Helper functions for EmitCMSEClearRecord
3507 
3508 // Set the bits corresponding to a field having width `BitWidth` and located at
3509 // offset `BitOffset` (from the least significant bit) within a storage unit of
3510 // `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.
3511 // Use little-endian layout, i.e.`Bits[0]` is the LSB.
3512 static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int BitOffset,
3513                         int BitWidth, int CharWidth) {
3514   assert(CharWidth <= 64);
3515   assert(static_cast<unsigned>(BitWidth) <= Bits.size() * CharWidth);
3516 
3517   int Pos = 0;
3518   if (BitOffset >= CharWidth) {
3519     Pos += BitOffset / CharWidth;
3520     BitOffset = BitOffset % CharWidth;
3521   }
3522 
3523   const uint64_t Used = (uint64_t(1) << CharWidth) - 1;
3524   if (BitOffset + BitWidth >= CharWidth) {
3525     Bits[Pos++] |= (Used << BitOffset) & Used;
3526     BitWidth -= CharWidth - BitOffset;
3527     BitOffset = 0;
3528   }
3529 
3530   while (BitWidth >= CharWidth) {
3531     Bits[Pos++] = Used;
3532     BitWidth -= CharWidth;
3533   }
3534 
3535   if (BitWidth > 0)
3536     Bits[Pos++] |= (Used >> (CharWidth - BitWidth)) << BitOffset;
3537 }
3538 
3539 // Set the bits corresponding to a field having width `BitWidth` and located at
3540 // offset `BitOffset` (from the least significant bit) within a storage unit of
3541 // `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of
3542 // `Bits` corresponds to one target byte. Use target endian layout.
3543 static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int StorageOffset,
3544                         int StorageSize, int BitOffset, int BitWidth,
3545                         int CharWidth, bool BigEndian) {
3546 
3547   SmallVector<uint64_t, 8> TmpBits(StorageSize);
3548   setBitRange(TmpBits, BitOffset, BitWidth, CharWidth);
3549 
3550   if (BigEndian)
3551     std::reverse(TmpBits.begin(), TmpBits.end());
3552 
3553   for (uint64_t V : TmpBits)
3554     Bits[StorageOffset++] |= V;
3555 }
3556 
3557 static void setUsedBits(CodeGenModule &, QualType, int,
3558                         SmallVectorImpl<uint64_t> &);
3559 
3560 // Set the bits in `Bits`, which correspond to the value representations of
3561 // the actual members of the record type `RTy`. Note that this function does
3562 // not handle base classes, virtual tables, etc, since they cannot happen in
3563 // CMSE function arguments or return. The bit mask corresponds to the target
3564 // memory layout, i.e. it's endian dependent.
3565 static void setUsedBits(CodeGenModule &CGM, const RecordType *RTy, int Offset,
3566                         SmallVectorImpl<uint64_t> &Bits) {
3567   ASTContext &Context = CGM.getContext();
3568   int CharWidth = Context.getCharWidth();
3569   const RecordDecl *RD = RTy->getDecl()->getDefinition();
3570   const ASTRecordLayout &ASTLayout = Context.getASTRecordLayout(RD);
3571   const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(RD);
3572 
3573   int Idx = 0;
3574   for (auto I = RD->field_begin(), E = RD->field_end(); I != E; ++I, ++Idx) {
3575     const FieldDecl *F = *I;
3576 
3577     if (F->isUnnamedBitfield() || F->isZeroLengthBitField(Context) ||
3578         F->getType()->isIncompleteArrayType())
3579       continue;
3580 
3581     if (F->isBitField()) {
3582       const CGBitFieldInfo &BFI = Layout.getBitFieldInfo(F);
3583       setBitRange(Bits, Offset + BFI.StorageOffset.getQuantity(),
3584                   BFI.StorageSize / CharWidth, BFI.Offset,
3585                   BFI.Size, CharWidth,
3586                   CGM.getDataLayout().isBigEndian());
3587       continue;
3588     }
3589 
3590     setUsedBits(CGM, F->getType(),
3591                 Offset + ASTLayout.getFieldOffset(Idx) / CharWidth, Bits);
3592   }
3593 }
3594 
3595 // Set the bits in `Bits`, which correspond to the value representations of
3596 // the elements of an array type `ATy`.
3597 static void setUsedBits(CodeGenModule &CGM, const ConstantArrayType *ATy,
3598                         int Offset, SmallVectorImpl<uint64_t> &Bits) {
3599   const ASTContext &Context = CGM.getContext();
3600 
3601   QualType ETy = Context.getBaseElementType(ATy);
3602   int Size = Context.getTypeSizeInChars(ETy).getQuantity();
3603   SmallVector<uint64_t, 4> TmpBits(Size);
3604   setUsedBits(CGM, ETy, 0, TmpBits);
3605 
3606   for (int I = 0, N = Context.getConstantArrayElementCount(ATy); I < N; ++I) {
3607     auto Src = TmpBits.begin();
3608     auto Dst = Bits.begin() + Offset + I * Size;
3609     for (int J = 0; J < Size; ++J)
3610       *Dst++ |= *Src++;
3611   }
3612 }
3613 
3614 // Set the bits in `Bits`, which correspond to the value representations of
3615 // the type `QTy`.
3616 static void setUsedBits(CodeGenModule &CGM, QualType QTy, int Offset,
3617                         SmallVectorImpl<uint64_t> &Bits) {
3618   if (const auto *RTy = QTy->getAs<RecordType>())
3619     return setUsedBits(CGM, RTy, Offset, Bits);
3620 
3621   ASTContext &Context = CGM.getContext();
3622   if (const auto *ATy = Context.getAsConstantArrayType(QTy))
3623     return setUsedBits(CGM, ATy, Offset, Bits);
3624 
3625   int Size = Context.getTypeSizeInChars(QTy).getQuantity();
3626   if (Size <= 0)
3627     return;
3628 
3629   std::fill_n(Bits.begin() + Offset, Size,
3630               (uint64_t(1) << Context.getCharWidth()) - 1);
3631 }
3632 
3633 static uint64_t buildMultiCharMask(const SmallVectorImpl<uint64_t> &Bits,
3634                                    int Pos, int Size, int CharWidth,
3635                                    bool BigEndian) {
3636   assert(Size > 0);
3637   uint64_t Mask = 0;
3638   if (BigEndian) {
3639     for (auto P = Bits.begin() + Pos, E = Bits.begin() + Pos + Size; P != E;
3640          ++P)
3641       Mask = (Mask << CharWidth) | *P;
3642   } else {
3643     auto P = Bits.begin() + Pos + Size, End = Bits.begin() + Pos;
3644     do
3645       Mask = (Mask << CharWidth) | *--P;
3646     while (P != End);
3647   }
3648   return Mask;
3649 }
3650 
3651 // Emit code to clear the bits in a record, which aren't a part of any user
3652 // declared member, when the record is a function return.
3653 llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
3654                                                   llvm::IntegerType *ITy,
3655                                                   QualType QTy) {
3656   assert(Src->getType() == ITy);
3657   assert(ITy->getScalarSizeInBits() <= 64);
3658 
3659   const llvm::DataLayout &DataLayout = CGM.getDataLayout();
3660   int Size = DataLayout.getTypeStoreSize(ITy);
3661   SmallVector<uint64_t, 4> Bits(Size);
3662   setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);
3663 
3664   int CharWidth = CGM.getContext().getCharWidth();
3665   uint64_t Mask =
3666       buildMultiCharMask(Bits, 0, Size, CharWidth, DataLayout.isBigEndian());
3667 
3668   return Builder.CreateAnd(Src, Mask, "cmse.clear");
3669 }
3670 
3671 // Emit code to clear the bits in a record, which aren't a part of any user
3672 // declared member, when the record is a function argument.
3673 llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
3674                                                   llvm::ArrayType *ATy,
3675                                                   QualType QTy) {
3676   const llvm::DataLayout &DataLayout = CGM.getDataLayout();
3677   int Size = DataLayout.getTypeStoreSize(ATy);
3678   SmallVector<uint64_t, 16> Bits(Size);
3679   setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);
3680 
3681   // Clear each element of the LLVM array.
3682   int CharWidth = CGM.getContext().getCharWidth();
3683   int CharsPerElt =
3684       ATy->getArrayElementType()->getScalarSizeInBits() / CharWidth;
3685   int MaskIndex = 0;
3686   llvm::Value *R = llvm::PoisonValue::get(ATy);
3687   for (int I = 0, N = ATy->getArrayNumElements(); I != N; ++I) {
3688     uint64_t Mask = buildMultiCharMask(Bits, MaskIndex, CharsPerElt, CharWidth,
3689                                        DataLayout.isBigEndian());
3690     MaskIndex += CharsPerElt;
3691     llvm::Value *T0 = Builder.CreateExtractValue(Src, I);
3692     llvm::Value *T1 = Builder.CreateAnd(T0, Mask, "cmse.clear");
3693     R = Builder.CreateInsertValue(R, T1, I);
3694   }
3695 
3696   return R;
3697 }
3698 
3699 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
3700                                          bool EmitRetDbgLoc,
3701                                          SourceLocation EndLoc) {
3702   if (FI.isNoReturn()) {
3703     // Noreturn functions don't return.
3704     EmitUnreachable(EndLoc);
3705     return;
3706   }
3707 
3708   if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
3709     // Naked functions don't have epilogues.
3710     Builder.CreateUnreachable();
3711     return;
3712   }
3713 
3714   // Functions with no result always return void.
3715   if (!ReturnValue.isValid()) {
3716     Builder.CreateRetVoid();
3717     return;
3718   }
3719 
3720   llvm::DebugLoc RetDbgLoc;
3721   llvm::Value *RV = nullptr;
3722   QualType RetTy = FI.getReturnType();
3723   const ABIArgInfo &RetAI = FI.getReturnInfo();
3724 
3725   switch (RetAI.getKind()) {
3726   case ABIArgInfo::InAlloca:
3727     // Aggregates get evaluated directly into the destination.  Sometimes we
3728     // need to return the sret value in a register, though.
3729     assert(hasAggregateEvaluationKind(RetTy));
3730     if (RetAI.getInAllocaSRet()) {
3731       llvm::Function::arg_iterator EI = CurFn->arg_end();
3732       --EI;
3733       llvm::Value *ArgStruct = &*EI;
3734       llvm::Value *SRet = Builder.CreateStructGEP(
3735           FI.getArgStruct(), ArgStruct, RetAI.getInAllocaFieldIndex());
3736       llvm::Type *Ty =
3737           cast<llvm::GetElementPtrInst>(SRet)->getResultElementType();
3738       RV = Builder.CreateAlignedLoad(Ty, SRet, getPointerAlign(), "sret");
3739     }
3740     break;
3741 
3742   case ABIArgInfo::Indirect: {
3743     auto AI = CurFn->arg_begin();
3744     if (RetAI.isSRetAfterThis())
3745       ++AI;
3746     switch (getEvaluationKind(RetTy)) {
3747     case TEK_Complex: {
3748       ComplexPairTy RT =
3749         EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
3750       EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
3751                          /*isInit*/ true);
3752       break;
3753     }
3754     case TEK_Aggregate:
3755       // Do nothing; aggregates get evaluated directly into the destination.
3756       break;
3757     case TEK_Scalar: {
3758       LValueBaseInfo BaseInfo;
3759       TBAAAccessInfo TBAAInfo;
3760       CharUnits Alignment =
3761           CGM.getNaturalTypeAlignment(RetTy, &BaseInfo, &TBAAInfo);
3762       Address ArgAddr(&*AI, ConvertType(RetTy), Alignment);
3763       LValue ArgVal =
3764           LValue::MakeAddr(ArgAddr, RetTy, getContext(), BaseInfo, TBAAInfo);
3765       EmitStoreOfScalar(
3766           Builder.CreateLoad(ReturnValue), ArgVal, /*isInit*/ true);
3767       break;
3768     }
3769     }
3770     break;
3771   }
3772 
3773   case ABIArgInfo::Extend:
3774   case ABIArgInfo::Direct:
3775     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
3776         RetAI.getDirectOffset() == 0) {
3777       // The internal return value temp always will have pointer-to-return-type
3778       // type, just do a load.
3779 
3780       // If there is a dominating store to ReturnValue, we can elide
3781       // the load, zap the store, and usually zap the alloca.
3782       if (llvm::StoreInst *SI =
3783               findDominatingStoreToReturnValue(*this)) {
3784         // Reuse the debug location from the store unless there is
3785         // cleanup code to be emitted between the store and return
3786         // instruction.
3787         if (EmitRetDbgLoc && !AutoreleaseResult)
3788           RetDbgLoc = SI->getDebugLoc();
3789         // Get the stored value and nuke the now-dead store.
3790         RV = SI->getValueOperand();
3791         SI->eraseFromParent();
3792 
3793       // Otherwise, we have to do a simple load.
3794       } else {
3795         RV = Builder.CreateLoad(ReturnValue);
3796       }
3797     } else {
3798       // If the value is offset in memory, apply the offset now.
3799       Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
3800 
3801       RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
3802     }
3803 
3804     // In ARC, end functions that return a retainable type with a call
3805     // to objc_autoreleaseReturnValue.
3806     if (AutoreleaseResult) {
3807 #ifndef NDEBUG
3808       // Type::isObjCRetainabletype has to be called on a QualType that hasn't
3809       // been stripped of the typedefs, so we cannot use RetTy here. Get the
3810       // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
3811       // CurCodeDecl or BlockInfo.
3812       QualType RT;
3813 
3814       if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
3815         RT = FD->getReturnType();
3816       else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
3817         RT = MD->getReturnType();
3818       else if (isa<BlockDecl>(CurCodeDecl))
3819         RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
3820       else
3821         llvm_unreachable("Unexpected function/method type");
3822 
3823       assert(getLangOpts().ObjCAutoRefCount &&
3824              !FI.isReturnsRetained() &&
3825              RT->isObjCRetainableType());
3826 #endif
3827       RV = emitAutoreleaseOfResult(*this, RV);
3828     }
3829 
3830     break;
3831 
3832   case ABIArgInfo::Ignore:
3833     break;
3834 
3835   case ABIArgInfo::CoerceAndExpand: {
3836     auto coercionType = RetAI.getCoerceAndExpandType();
3837 
3838     // Load all of the coerced elements out into results.
3839     llvm::SmallVector<llvm::Value*, 4> results;
3840     Address addr = ReturnValue.withElementType(coercionType);
3841     for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
3842       auto coercedEltType = coercionType->getElementType(i);
3843       if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
3844         continue;
3845 
3846       auto eltAddr = Builder.CreateStructGEP(addr, i);
3847       auto elt = Builder.CreateLoad(eltAddr);
3848       results.push_back(elt);
3849     }
3850 
3851     // If we have one result, it's the single direct result type.
3852     if (results.size() == 1) {
3853       RV = results[0];
3854 
3855     // Otherwise, we need to make a first-class aggregate.
3856     } else {
3857       // Construct a return type that lacks padding elements.
3858       llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
3859 
3860       RV = llvm::PoisonValue::get(returnType);
3861       for (unsigned i = 0, e = results.size(); i != e; ++i) {
3862         RV = Builder.CreateInsertValue(RV, results[i], i);
3863       }
3864     }
3865     break;
3866   }
3867   case ABIArgInfo::Expand:
3868   case ABIArgInfo::IndirectAliased:
3869     llvm_unreachable("Invalid ABI kind for return argument");
3870   }
3871 
3872   llvm::Instruction *Ret;
3873   if (RV) {
3874     if (CurFuncDecl && CurFuncDecl->hasAttr<CmseNSEntryAttr>()) {
3875       // For certain return types, clear padding bits, as they may reveal
3876       // sensitive information.
3877       // Small struct/union types are passed as integers.
3878       auto *ITy = dyn_cast<llvm::IntegerType>(RV->getType());
3879       if (ITy != nullptr && isa<RecordType>(RetTy.getCanonicalType()))
3880         RV = EmitCMSEClearRecord(RV, ITy, RetTy);
3881     }
3882     EmitReturnValueCheck(RV);
3883     Ret = Builder.CreateRet(RV);
3884   } else {
3885     Ret = Builder.CreateRetVoid();
3886   }
3887 
3888   if (RetDbgLoc)
3889     Ret->setDebugLoc(std::move(RetDbgLoc));
3890 }
3891 
3892 void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
3893   // A current decl may not be available when emitting vtable thunks.
3894   if (!CurCodeDecl)
3895     return;
3896 
3897   // If the return block isn't reachable, neither is this check, so don't emit
3898   // it.
3899   if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())
3900     return;
3901 
3902   ReturnsNonNullAttr *RetNNAttr = nullptr;
3903   if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
3904     RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
3905 
3906   if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
3907     return;
3908 
3909   // Prefer the returns_nonnull attribute if it's present.
3910   SourceLocation AttrLoc;
3911   SanitizerMask CheckKind;
3912   SanitizerHandler Handler;
3913   if (RetNNAttr) {
3914     assert(!requiresReturnValueNullabilityCheck() &&
3915            "Cannot check nullability and the nonnull attribute");
3916     AttrLoc = RetNNAttr->getLocation();
3917     CheckKind = SanitizerKind::ReturnsNonnullAttribute;
3918     Handler = SanitizerHandler::NonnullReturn;
3919   } else {
3920     if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
3921       if (auto *TSI = DD->getTypeSourceInfo())
3922         if (auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>())
3923           AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
3924     CheckKind = SanitizerKind::NullabilityReturn;
3925     Handler = SanitizerHandler::NullabilityReturn;
3926   }
3927 
3928   SanitizerScope SanScope(this);
3929 
3930   // Make sure the "return" source location is valid. If we're checking a
3931   // nullability annotation, make sure the preconditions for the check are met.
3932   llvm::BasicBlock *Check = createBasicBlock("nullcheck");
3933   llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");
3934   llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
3935   llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);
3936   if (requiresReturnValueNullabilityCheck())
3937     CanNullCheck =
3938         Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
3939   Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
3940   EmitBlock(Check);
3941 
3942   // Now do the null check.
3943   llvm::Value *Cond = Builder.CreateIsNotNull(RV);
3944   llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
3945   llvm::Value *DynamicData[] = {SLocPtr};
3946   EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
3947 
3948   EmitBlock(NoCheck);
3949 
3950 #ifndef NDEBUG
3951   // The return location should not be used after the check has been emitted.
3952   ReturnLocation = Address::invalid();
3953 #endif
3954 }
3955 
3956 static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {
3957   const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
3958   return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
3959 }
3960 
3961 static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF,
3962                                           QualType Ty) {
3963   // FIXME: Generate IR in one pass, rather than going back and fixing up these
3964   // placeholders.
3965   llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
3966   llvm::Type *IRPtrTy = llvm::PointerType::getUnqual(CGF.getLLVMContext());
3967   llvm::Value *Placeholder = llvm::PoisonValue::get(IRPtrTy);
3968 
3969   // FIXME: When we generate this IR in one pass, we shouldn't need
3970   // this win32-specific alignment hack.
3971   CharUnits Align = CharUnits::fromQuantity(4);
3972   Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
3973 
3974   return AggValueSlot::forAddr(Address(Placeholder, IRTy, Align),
3975                                Ty.getQualifiers(),
3976                                AggValueSlot::IsNotDestructed,
3977                                AggValueSlot::DoesNotNeedGCBarriers,
3978                                AggValueSlot::IsNotAliased,
3979                                AggValueSlot::DoesNotOverlap);
3980 }
3981 
3982 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
3983                                           const VarDecl *param,
3984                                           SourceLocation loc) {
3985   // StartFunction converted the ABI-lowered parameter(s) into a
3986   // local alloca.  We need to turn that into an r-value suitable
3987   // for EmitCall.
3988   Address local = GetAddrOfLocalVar(param);
3989 
3990   QualType type = param->getType();
3991 
3992   // GetAddrOfLocalVar returns a pointer-to-pointer for references,
3993   // but the argument needs to be the original pointer.
3994   if (type->isReferenceType()) {
3995     args.add(RValue::get(Builder.CreateLoad(local)), type);
3996 
3997   // In ARC, move out of consumed arguments so that the release cleanup
3998   // entered by StartFunction doesn't cause an over-release.  This isn't
3999   // optimal -O0 code generation, but it should get cleaned up when
4000   // optimization is enabled.  This also assumes that delegate calls are
4001   // performed exactly once for a set of arguments, but that should be safe.
4002   } else if (getLangOpts().ObjCAutoRefCount &&
4003              param->hasAttr<NSConsumedAttr>() &&
4004              type->isObjCRetainableType()) {
4005     llvm::Value *ptr = Builder.CreateLoad(local);
4006     auto null =
4007       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
4008     Builder.CreateStore(null, local);
4009     args.add(RValue::get(ptr), type);
4010 
4011   // For the most part, we just need to load the alloca, except that
4012   // aggregate r-values are actually pointers to temporaries.
4013   } else {
4014     args.add(convertTempToRValue(local, type, loc), type);
4015   }
4016 
4017   // Deactivate the cleanup for the callee-destructed param that was pushed.
4018   if (type->isRecordType() && !CurFuncIsThunk &&
4019       type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee() &&
4020       param->needsDestruction(getContext())) {
4021     EHScopeStack::stable_iterator cleanup =
4022         CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));
4023     assert(cleanup.isValid() &&
4024            "cleanup for callee-destructed param not recorded");
4025     // This unreachable is a temporary marker which will be removed later.
4026     llvm::Instruction *isActive = Builder.CreateUnreachable();
4027     args.addArgCleanupDeactivation(cleanup, isActive);
4028   }
4029 }
4030 
4031 static bool isProvablyNull(llvm::Value *addr) {
4032   return isa<llvm::ConstantPointerNull>(addr);
4033 }
4034 
4035 /// Emit the actual writing-back of a writeback.
4036 static void emitWriteback(CodeGenFunction &CGF,
4037                           const CallArgList::Writeback &writeback) {
4038   const LValue &srcLV = writeback.Source;
4039   Address srcAddr = srcLV.getAddress(CGF);
4040   assert(!isProvablyNull(srcAddr.getPointer()) &&
4041          "shouldn't have writeback for provably null argument");
4042 
4043   llvm::BasicBlock *contBB = nullptr;
4044 
4045   // If the argument wasn't provably non-null, we need to null check
4046   // before doing the store.
4047   bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
4048                                               CGF.CGM.getDataLayout());
4049   if (!provablyNonNull) {
4050     llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
4051     contBB = CGF.createBasicBlock("icr.done");
4052 
4053     llvm::Value *isNull =
4054       CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
4055     CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
4056     CGF.EmitBlock(writebackBB);
4057   }
4058 
4059   // Load the value to writeback.
4060   llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
4061 
4062   // Cast it back, in case we're writing an id to a Foo* or something.
4063   value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
4064                                     "icr.writeback-cast");
4065 
4066   // Perform the writeback.
4067 
4068   // If we have a "to use" value, it's something we need to emit a use
4069   // of.  This has to be carefully threaded in: if it's done after the
4070   // release it's potentially undefined behavior (and the optimizer
4071   // will ignore it), and if it happens before the retain then the
4072   // optimizer could move the release there.
4073   if (writeback.ToUse) {
4074     assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
4075 
4076     // Retain the new value.  No need to block-copy here:  the block's
4077     // being passed up the stack.
4078     value = CGF.EmitARCRetainNonBlock(value);
4079 
4080     // Emit the intrinsic use here.
4081     CGF.EmitARCIntrinsicUse(writeback.ToUse);
4082 
4083     // Load the old value (primitively).
4084     llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
4085 
4086     // Put the new value in place (primitively).
4087     CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
4088 
4089     // Release the old value.
4090     CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
4091 
4092   // Otherwise, we can just do a normal lvalue store.
4093   } else {
4094     CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
4095   }
4096 
4097   // Jump to the continuation block.
4098   if (!provablyNonNull)
4099     CGF.EmitBlock(contBB);
4100 }
4101 
4102 static void emitWritebacks(CodeGenFunction &CGF,
4103                            const CallArgList &args) {
4104   for (const auto &I : args.writebacks())
4105     emitWriteback(CGF, I);
4106 }
4107 
4108 static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,
4109                                             const CallArgList &CallArgs) {
4110   ArrayRef<CallArgList::CallArgCleanup> Cleanups =
4111     CallArgs.getCleanupsToDeactivate();
4112   // Iterate in reverse to increase the likelihood of popping the cleanup.
4113   for (const auto &I : llvm::reverse(Cleanups)) {
4114     CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
4115     I.IsActiveIP->eraseFromParent();
4116   }
4117 }
4118 
4119 static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
4120   if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
4121     if (uop->getOpcode() == UO_AddrOf)
4122       return uop->getSubExpr();
4123   return nullptr;
4124 }
4125 
4126 /// Emit an argument that's being passed call-by-writeback.  That is,
4127 /// we are passing the address of an __autoreleased temporary; it
4128 /// might be copy-initialized with the current value of the given
4129 /// address, but it will definitely be copied out of after the call.
4130 static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
4131                              const ObjCIndirectCopyRestoreExpr *CRE) {
4132   LValue srcLV;
4133 
4134   // Make an optimistic effort to emit the address as an l-value.
4135   // This can fail if the argument expression is more complicated.
4136   if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
4137     srcLV = CGF.EmitLValue(lvExpr);
4138 
4139   // Otherwise, just emit it as a scalar.
4140   } else {
4141     Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
4142 
4143     QualType srcAddrType =
4144       CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
4145     srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
4146   }
4147   Address srcAddr = srcLV.getAddress(CGF);
4148 
4149   // The dest and src types don't necessarily match in LLVM terms
4150   // because of the crazy ObjC compatibility rules.
4151 
4152   llvm::PointerType *destType =
4153       cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
4154   llvm::Type *destElemType =
4155       CGF.ConvertTypeForMem(CRE->getType()->getPointeeType());
4156 
4157   // If the address is a constant null, just pass the appropriate null.
4158   if (isProvablyNull(srcAddr.getPointer())) {
4159     args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
4160              CRE->getType());
4161     return;
4162   }
4163 
4164   // Create the temporary.
4165   Address temp =
4166       CGF.CreateTempAlloca(destElemType, CGF.getPointerAlign(), "icr.temp");
4167   // Loading an l-value can introduce a cleanup if the l-value is __weak,
4168   // and that cleanup will be conditional if we can't prove that the l-value
4169   // isn't null, so we need to register a dominating point so that the cleanups
4170   // system will make valid IR.
4171   CodeGenFunction::ConditionalEvaluation condEval(CGF);
4172 
4173   // Zero-initialize it if we're not doing a copy-initialization.
4174   bool shouldCopy = CRE->shouldCopy();
4175   if (!shouldCopy) {
4176     llvm::Value *null =
4177         llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destElemType));
4178     CGF.Builder.CreateStore(null, temp);
4179   }
4180 
4181   llvm::BasicBlock *contBB = nullptr;
4182   llvm::BasicBlock *originBB = nullptr;
4183 
4184   // If the address is *not* known to be non-null, we need to switch.
4185   llvm::Value *finalArgument;
4186 
4187   bool provablyNonNull = llvm::isKnownNonZero(srcAddr.getPointer(),
4188                                               CGF.CGM.getDataLayout());
4189   if (provablyNonNull) {
4190     finalArgument = temp.getPointer();
4191   } else {
4192     llvm::Value *isNull =
4193       CGF.Builder.CreateIsNull(srcAddr.getPointer(), "icr.isnull");
4194 
4195     finalArgument = CGF.Builder.CreateSelect(isNull,
4196                                    llvm::ConstantPointerNull::get(destType),
4197                                              temp.getPointer(), "icr.argument");
4198 
4199     // If we need to copy, then the load has to be conditional, which
4200     // means we need control flow.
4201     if (shouldCopy) {
4202       originBB = CGF.Builder.GetInsertBlock();
4203       contBB = CGF.createBasicBlock("icr.cont");
4204       llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
4205       CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
4206       CGF.EmitBlock(copyBB);
4207       condEval.begin(CGF);
4208     }
4209   }
4210 
4211   llvm::Value *valueToUse = nullptr;
4212 
4213   // Perform a copy if necessary.
4214   if (shouldCopy) {
4215     RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
4216     assert(srcRV.isScalar());
4217 
4218     llvm::Value *src = srcRV.getScalarVal();
4219     src = CGF.Builder.CreateBitCast(src, destElemType, "icr.cast");
4220 
4221     // Use an ordinary store, not a store-to-lvalue.
4222     CGF.Builder.CreateStore(src, temp);
4223 
4224     // If optimization is enabled, and the value was held in a
4225     // __strong variable, we need to tell the optimizer that this
4226     // value has to stay alive until we're doing the store back.
4227     // This is because the temporary is effectively unretained,
4228     // and so otherwise we can violate the high-level semantics.
4229     if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
4230         srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
4231       valueToUse = src;
4232     }
4233   }
4234 
4235   // Finish the control flow if we needed it.
4236   if (shouldCopy && !provablyNonNull) {
4237     llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
4238     CGF.EmitBlock(contBB);
4239 
4240     // Make a phi for the value to intrinsically use.
4241     if (valueToUse) {
4242       llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
4243                                                       "icr.to-use");
4244       phiToUse->addIncoming(valueToUse, copyBB);
4245       phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
4246                             originBB);
4247       valueToUse = phiToUse;
4248     }
4249 
4250     condEval.end(CGF);
4251   }
4252 
4253   args.addWriteback(srcLV, temp, valueToUse);
4254   args.add(RValue::get(finalArgument), CRE->getType());
4255 }
4256 
4257 void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {
4258   assert(!StackBase);
4259 
4260   // Save the stack.
4261   llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave);
4262   StackBase = CGF.Builder.CreateCall(F, {}, "inalloca.save");
4263 }
4264 
4265 void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {
4266   if (StackBase) {
4267     // Restore the stack after the call.
4268     llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
4269     CGF.Builder.CreateCall(F, StackBase);
4270   }
4271 }
4272 
4273 void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
4274                                           SourceLocation ArgLoc,
4275                                           AbstractCallee AC,
4276                                           unsigned ParmNum) {
4277   if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
4278                          SanOpts.has(SanitizerKind::NullabilityArg)))
4279     return;
4280 
4281   // The param decl may be missing in a variadic function.
4282   auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;
4283   unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
4284 
4285   // Prefer the nonnull attribute if it's present.
4286   const NonNullAttr *NNAttr = nullptr;
4287   if (SanOpts.has(SanitizerKind::NonnullAttribute))
4288     NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
4289 
4290   bool CanCheckNullability = false;
4291   if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
4292     auto Nullability = PVD->getType()->getNullability();
4293     CanCheckNullability = Nullability &&
4294                           *Nullability == NullabilityKind::NonNull &&
4295                           PVD->getTypeSourceInfo();
4296   }
4297 
4298   if (!NNAttr && !CanCheckNullability)
4299     return;
4300 
4301   SourceLocation AttrLoc;
4302   SanitizerMask CheckKind;
4303   SanitizerHandler Handler;
4304   if (NNAttr) {
4305     AttrLoc = NNAttr->getLocation();
4306     CheckKind = SanitizerKind::NonnullAttribute;
4307     Handler = SanitizerHandler::NonnullArg;
4308   } else {
4309     AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
4310     CheckKind = SanitizerKind::NullabilityArg;
4311     Handler = SanitizerHandler::NullabilityArg;
4312   }
4313 
4314   SanitizerScope SanScope(this);
4315   llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
4316   llvm::Constant *StaticData[] = {
4317       EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
4318       llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
4319   };
4320   EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, std::nullopt);
4321 }
4322 
4323 // Check if the call is going to use the inalloca convention. This needs to
4324 // agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged
4325 // later, so we can't check it directly.
4326 static bool hasInAllocaArgs(CodeGenModule &CGM, CallingConv ExplicitCC,
4327                             ArrayRef<QualType> ArgTypes) {
4328   // The Swift calling conventions don't go through the target-specific
4329   // argument classification, they never use inalloca.
4330   // TODO: Consider limiting inalloca use to only calling conventions supported
4331   // by MSVC.
4332   if (ExplicitCC == CC_Swift || ExplicitCC == CC_SwiftAsync)
4333     return false;
4334   if (!CGM.getTarget().getCXXABI().isMicrosoft())
4335     return false;
4336   return llvm::any_of(ArgTypes, [&](QualType Ty) {
4337     return isInAllocaArgument(CGM.getCXXABI(), Ty);
4338   });
4339 }
4340 
4341 #ifndef NDEBUG
4342 // Determine whether the given argument is an Objective-C method
4343 // that may have type parameters in its signature.
4344 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
4345   const DeclContext *dc = method->getDeclContext();
4346   if (const ObjCInterfaceDecl *classDecl = dyn_cast<ObjCInterfaceDecl>(dc)) {
4347     return classDecl->getTypeParamListAsWritten();
4348   }
4349 
4350   if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
4351     return catDecl->getTypeParamList();
4352   }
4353 
4354   return false;
4355 }
4356 #endif
4357 
4358 /// EmitCallArgs - Emit call arguments for a function.
4359 void CodeGenFunction::EmitCallArgs(
4360     CallArgList &Args, PrototypeWrapper Prototype,
4361     llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
4362     AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {
4363   SmallVector<QualType, 16> ArgTypes;
4364 
4365   assert((ParamsToSkip == 0 || Prototype.P) &&
4366          "Can't skip parameters if type info is not provided");
4367 
4368   // This variable only captures *explicitly* written conventions, not those
4369   // applied by default via command line flags or target defaults, such as
4370   // thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would
4371   // require knowing if this is a C++ instance method or being able to see
4372   // unprototyped FunctionTypes.
4373   CallingConv ExplicitCC = CC_C;
4374 
4375   // First, if a prototype was provided, use those argument types.
4376   bool IsVariadic = false;
4377   if (Prototype.P) {
4378     const auto *MD = Prototype.P.dyn_cast<const ObjCMethodDecl *>();
4379     if (MD) {
4380       IsVariadic = MD->isVariadic();
4381       ExplicitCC = getCallingConventionForDecl(
4382           MD, CGM.getTarget().getTriple().isOSWindows());
4383       ArgTypes.assign(MD->param_type_begin() + ParamsToSkip,
4384                       MD->param_type_end());
4385     } else {
4386       const auto *FPT = Prototype.P.get<const FunctionProtoType *>();
4387       IsVariadic = FPT->isVariadic();
4388       ExplicitCC = FPT->getExtInfo().getCC();
4389       ArgTypes.assign(FPT->param_type_begin() + ParamsToSkip,
4390                       FPT->param_type_end());
4391     }
4392 
4393 #ifndef NDEBUG
4394     // Check that the prototyped types match the argument expression types.
4395     bool isGenericMethod = MD && isObjCMethodWithTypeParams(MD);
4396     CallExpr::const_arg_iterator Arg = ArgRange.begin();
4397     for (QualType Ty : ArgTypes) {
4398       assert(Arg != ArgRange.end() && "Running over edge of argument list!");
4399       assert(
4400           (isGenericMethod || Ty->isVariablyModifiedType() ||
4401            Ty.getNonReferenceType()->isObjCRetainableType() ||
4402            getContext()
4403                    .getCanonicalType(Ty.getNonReferenceType())
4404                    .getTypePtr() ==
4405                getContext().getCanonicalType((*Arg)->getType()).getTypePtr()) &&
4406           "type mismatch in call argument!");
4407       ++Arg;
4408     }
4409 
4410     // Either we've emitted all the call args, or we have a call to variadic
4411     // function.
4412     assert((Arg == ArgRange.end() || IsVariadic) &&
4413            "Extra arguments in non-variadic function!");
4414 #endif
4415   }
4416 
4417   // If we still have any arguments, emit them using the type of the argument.
4418   for (auto *A : llvm::drop_begin(ArgRange, ArgTypes.size()))
4419     ArgTypes.push_back(IsVariadic ? getVarArgType(A) : A->getType());
4420   assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
4421 
4422   // We must evaluate arguments from right to left in the MS C++ ABI,
4423   // because arguments are destroyed left to right in the callee. As a special
4424   // case, there are certain language constructs that require left-to-right
4425   // evaluation, and in those cases we consider the evaluation order requirement
4426   // to trump the "destruction order is reverse construction order" guarantee.
4427   bool LeftToRight =
4428       CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
4429           ? Order == EvaluationOrder::ForceLeftToRight
4430           : Order != EvaluationOrder::ForceRightToLeft;
4431 
4432   auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,
4433                                          RValue EmittedArg) {
4434     if (!AC.hasFunctionDecl() || I >= AC.getNumParams())
4435       return;
4436     auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();
4437     if (PS == nullptr)
4438       return;
4439 
4440     const auto &Context = getContext();
4441     auto SizeTy = Context.getSizeType();
4442     auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
4443     assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
4444     llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,
4445                                                      EmittedArg.getScalarVal(),
4446                                                      PS->isDynamic());
4447     Args.add(RValue::get(V), SizeTy);
4448     // If we're emitting args in reverse, be sure to do so with
4449     // pass_object_size, as well.
4450     if (!LeftToRight)
4451       std::swap(Args.back(), *(&Args.back() - 1));
4452   };
4453 
4454   // Insert a stack save if we're going to need any inalloca args.
4455   if (hasInAllocaArgs(CGM, ExplicitCC, ArgTypes)) {
4456     assert(getTarget().getTriple().getArch() == llvm::Triple::x86 &&
4457            "inalloca only supported on x86");
4458     Args.allocateArgumentMemory(*this);
4459   }
4460 
4461   // Evaluate each argument in the appropriate order.
4462   size_t CallArgsStart = Args.size();
4463   for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
4464     unsigned Idx = LeftToRight ? I : E - I - 1;
4465     CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
4466     unsigned InitialArgSize = Args.size();
4467     // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
4468     // the argument and parameter match or the objc method is parameterized.
4469     assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
4470             getContext().hasSameUnqualifiedType((*Arg)->getType(),
4471                                                 ArgTypes[Idx]) ||
4472             (isa<ObjCMethodDecl>(AC.getDecl()) &&
4473              isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&
4474            "Argument and parameter types don't match");
4475     EmitCallArg(Args, *Arg, ArgTypes[Idx]);
4476     // In particular, we depend on it being the last arg in Args, and the
4477     // objectsize bits depend on there only being one arg if !LeftToRight.
4478     assert(InitialArgSize + 1 == Args.size() &&
4479            "The code below depends on only adding one arg per EmitCallArg");
4480     (void)InitialArgSize;
4481     // Since pointer argument are never emitted as LValue, it is safe to emit
4482     // non-null argument check for r-value only.
4483     if (!Args.back().hasLValue()) {
4484       RValue RVArg = Args.back().getKnownRValue();
4485       EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
4486                           ParamsToSkip + Idx);
4487       // @llvm.objectsize should never have side-effects and shouldn't need
4488       // destruction/cleanups, so we can safely "emit" it after its arg,
4489       // regardless of right-to-leftness
4490       MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
4491     }
4492   }
4493 
4494   if (!LeftToRight) {
4495     // Un-reverse the arguments we just evaluated so they match up with the LLVM
4496     // IR function.
4497     std::reverse(Args.begin() + CallArgsStart, Args.end());
4498   }
4499 }
4500 
4501 namespace {
4502 
4503 struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
4504   DestroyUnpassedArg(Address Addr, QualType Ty)
4505       : Addr(Addr), Ty(Ty) {}
4506 
4507   Address Addr;
4508   QualType Ty;
4509 
4510   void Emit(CodeGenFunction &CGF, Flags flags) override {
4511     QualType::DestructionKind DtorKind = Ty.isDestructedType();
4512     if (DtorKind == QualType::DK_cxx_destructor) {
4513       const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
4514       assert(!Dtor->isTrivial());
4515       CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
4516                                 /*Delegating=*/false, Addr, Ty);
4517     } else {
4518       CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));
4519     }
4520   }
4521 };
4522 
4523 struct DisableDebugLocationUpdates {
4524   CodeGenFunction &CGF;
4525   bool disabledDebugInfo;
4526   DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {
4527     if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))
4528       CGF.disableDebugInfo();
4529   }
4530   ~DisableDebugLocationUpdates() {
4531     if (disabledDebugInfo)
4532       CGF.enableDebugInfo();
4533   }
4534 };
4535 
4536 } // end anonymous namespace
4537 
4538 RValue CallArg::getRValue(CodeGenFunction &CGF) const {
4539   if (!HasLV)
4540     return RV;
4541   LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty);
4542   CGF.EmitAggregateCopy(Copy, LV, Ty, AggValueSlot::DoesNotOverlap,
4543                         LV.isVolatile());
4544   IsUsed = true;
4545   return RValue::getAggregate(Copy.getAddress(CGF));
4546 }
4547 
4548 void CallArg::copyInto(CodeGenFunction &CGF, Address Addr) const {
4549   LValue Dst = CGF.MakeAddrLValue(Addr, Ty);
4550   if (!HasLV && RV.isScalar())
4551     CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*isInit=*/true);
4552   else if (!HasLV && RV.isComplex())
4553     CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);
4554   else {
4555     auto Addr = HasLV ? LV.getAddress(CGF) : RV.getAggregateAddress();
4556     LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);
4557     // We assume that call args are never copied into subobjects.
4558     CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap,
4559                           HasLV ? LV.isVolatileQualified()
4560                                 : RV.isVolatileQualified());
4561   }
4562   IsUsed = true;
4563 }
4564 
4565 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
4566                                   QualType type) {
4567   DisableDebugLocationUpdates Dis(*this, E);
4568   if (const ObjCIndirectCopyRestoreExpr *CRE
4569         = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
4570     assert(getLangOpts().ObjCAutoRefCount);
4571     return emitWritebackArg(*this, args, CRE);
4572   }
4573 
4574   assert(type->isReferenceType() == E->isGLValue() &&
4575          "reference binding to unmaterialized r-value!");
4576 
4577   if (E->isGLValue()) {
4578     assert(E->getObjectKind() == OK_Ordinary);
4579     return args.add(EmitReferenceBindingToExpr(E), type);
4580   }
4581 
4582   bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
4583 
4584   // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
4585   // However, we still have to push an EH-only cleanup in case we unwind before
4586   // we make it to the call.
4587   if (type->isRecordType() &&
4588       type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
4589     // If we're using inalloca, use the argument memory.  Otherwise, use a
4590     // temporary.
4591     AggValueSlot Slot = args.isUsingInAlloca()
4592         ? createPlaceholderSlot(*this, type) : CreateAggTemp(type, "agg.tmp");
4593 
4594     bool DestroyedInCallee = true, NeedsEHCleanup = true;
4595     if (const auto *RD = type->getAsCXXRecordDecl())
4596       DestroyedInCallee = RD->hasNonTrivialDestructor();
4597     else
4598       NeedsEHCleanup = needsEHCleanup(type.isDestructedType());
4599 
4600     if (DestroyedInCallee)
4601       Slot.setExternallyDestructed();
4602 
4603     EmitAggExpr(E, Slot);
4604     RValue RV = Slot.asRValue();
4605     args.add(RV, type);
4606 
4607     if (DestroyedInCallee && NeedsEHCleanup) {
4608       // Create a no-op GEP between the placeholder and the cleanup so we can
4609       // RAUW it successfully.  It also serves as a marker of the first
4610       // instruction where the cleanup is active.
4611       pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddress(),
4612                                               type);
4613       // This unreachable is a temporary marker which will be removed later.
4614       llvm::Instruction *IsActive = Builder.CreateUnreachable();
4615       args.addArgCleanupDeactivation(EHStack.stable_begin(), IsActive);
4616     }
4617     return;
4618   }
4619 
4620   if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
4621       cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
4622     LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
4623     assert(L.isSimple());
4624     args.addUncopiedAggregate(L, type);
4625     return;
4626   }
4627 
4628   args.add(EmitAnyExprToTemp(E), type);
4629 }
4630 
4631 QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
4632   // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
4633   // implicitly widens null pointer constants that are arguments to varargs
4634   // functions to pointer-sized ints.
4635   if (!getTarget().getTriple().isOSWindows())
4636     return Arg->getType();
4637 
4638   if (Arg->getType()->isIntegerType() &&
4639       getContext().getTypeSize(Arg->getType()) <
4640           getContext().getTargetInfo().getPointerWidth(LangAS::Default) &&
4641       Arg->isNullPointerConstant(getContext(),
4642                                  Expr::NPC_ValueDependentIsNotNull)) {
4643     return getContext().getIntPtrType();
4644   }
4645 
4646   return Arg->getType();
4647 }
4648 
4649 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4650 // optimizer it can aggressively ignore unwind edges.
4651 void
4652 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
4653   if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
4654       !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
4655     Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
4656                       CGM.getNoObjCARCExceptionsMetadata());
4657 }
4658 
4659 /// Emits a call to the given no-arguments nounwind runtime function.
4660 llvm::CallInst *
4661 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4662                                          const llvm::Twine &name) {
4663   return EmitNounwindRuntimeCall(callee, std::nullopt, name);
4664 }
4665 
4666 /// Emits a call to the given nounwind runtime function.
4667 llvm::CallInst *
4668 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4669                                          ArrayRef<llvm::Value *> args,
4670                                          const llvm::Twine &name) {
4671   llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
4672   call->setDoesNotThrow();
4673   return call;
4674 }
4675 
4676 /// Emits a simple call (never an invoke) to the given no-arguments
4677 /// runtime function.
4678 llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
4679                                                  const llvm::Twine &name) {
4680   return EmitRuntimeCall(callee, std::nullopt, name);
4681 }
4682 
4683 // Calls which may throw must have operand bundles indicating which funclet
4684 // they are nested within.
4685 SmallVector<llvm::OperandBundleDef, 1>
4686 CodeGenFunction::getBundlesForFunclet(llvm::Value *Callee) {
4687   // There is no need for a funclet operand bundle if we aren't inside a
4688   // funclet.
4689   if (!CurrentFuncletPad)
4690     return (SmallVector<llvm::OperandBundleDef, 1>());
4691 
4692   // Skip intrinsics which cannot throw (as long as they don't lower into
4693   // regular function calls in the course of IR transformations).
4694   if (auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) {
4695     if (CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow()) {
4696       auto IID = CalleeFn->getIntrinsicID();
4697       if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID))
4698         return (SmallVector<llvm::OperandBundleDef, 1>());
4699     }
4700   }
4701 
4702   SmallVector<llvm::OperandBundleDef, 1> BundleList;
4703   BundleList.emplace_back("funclet", CurrentFuncletPad);
4704   return BundleList;
4705 }
4706 
4707 /// Emits a simple call (never an invoke) to the given runtime function.
4708 llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
4709                                                  ArrayRef<llvm::Value *> args,
4710                                                  const llvm::Twine &name) {
4711   llvm::CallInst *call = Builder.CreateCall(
4712       callee, args, getBundlesForFunclet(callee.getCallee()), name);
4713   call->setCallingConv(getRuntimeCC());
4714   return call;
4715 }
4716 
4717 /// Emits a call or invoke to the given noreturn runtime function.
4718 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(
4719     llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) {
4720   SmallVector<llvm::OperandBundleDef, 1> BundleList =
4721       getBundlesForFunclet(callee.getCallee());
4722 
4723   if (getInvokeDest()) {
4724     llvm::InvokeInst *invoke =
4725       Builder.CreateInvoke(callee,
4726                            getUnreachableBlock(),
4727                            getInvokeDest(),
4728                            args,
4729                            BundleList);
4730     invoke->setDoesNotReturn();
4731     invoke->setCallingConv(getRuntimeCC());
4732   } else {
4733     llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
4734     call->setDoesNotReturn();
4735     call->setCallingConv(getRuntimeCC());
4736     Builder.CreateUnreachable();
4737   }
4738 }
4739 
4740 /// Emits a call or invoke instruction to the given nullary runtime function.
4741 llvm::CallBase *
4742 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
4743                                          const Twine &name) {
4744   return EmitRuntimeCallOrInvoke(callee, std::nullopt, name);
4745 }
4746 
4747 /// Emits a call or invoke instruction to the given runtime function.
4748 llvm::CallBase *
4749 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,
4750                                          ArrayRef<llvm::Value *> args,
4751                                          const Twine &name) {
4752   llvm::CallBase *call = EmitCallOrInvoke(callee, args, name);
4753   call->setCallingConv(getRuntimeCC());
4754   return call;
4755 }
4756 
4757 /// Emits a call or invoke instruction to the given function, depending
4758 /// on the current state of the EH stack.
4759 llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee,
4760                                                   ArrayRef<llvm::Value *> Args,
4761                                                   const Twine &Name) {
4762   llvm::BasicBlock *InvokeDest = getInvokeDest();
4763   SmallVector<llvm::OperandBundleDef, 1> BundleList =
4764       getBundlesForFunclet(Callee.getCallee());
4765 
4766   llvm::CallBase *Inst;
4767   if (!InvokeDest)
4768     Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
4769   else {
4770     llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
4771     Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
4772                                 Name);
4773     EmitBlock(ContBB);
4774   }
4775 
4776   // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4777   // optimizer it can aggressively ignore unwind edges.
4778   if (CGM.getLangOpts().ObjCAutoRefCount)
4779     AddObjCARCExceptionMetadata(Inst);
4780 
4781   return Inst;
4782 }
4783 
4784 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
4785                                                   llvm::Value *New) {
4786   DeferredReplacements.push_back(
4787       std::make_pair(llvm::WeakTrackingVH(Old), New));
4788 }
4789 
4790 namespace {
4791 
4792 /// Specify given \p NewAlign as the alignment of return value attribute. If
4793 /// such attribute already exists, re-set it to the maximal one of two options.
4794 [[nodiscard]] llvm::AttributeList
4795 maybeRaiseRetAlignmentAttribute(llvm::LLVMContext &Ctx,
4796                                 const llvm::AttributeList &Attrs,
4797                                 llvm::Align NewAlign) {
4798   llvm::Align CurAlign = Attrs.getRetAlignment().valueOrOne();
4799   if (CurAlign >= NewAlign)
4800     return Attrs;
4801   llvm::Attribute AlignAttr = llvm::Attribute::getWithAlignment(Ctx, NewAlign);
4802   return Attrs.removeRetAttribute(Ctx, llvm::Attribute::AttrKind::Alignment)
4803       .addRetAttribute(Ctx, AlignAttr);
4804 }
4805 
4806 template <typename AlignedAttrTy> class AbstractAssumeAlignedAttrEmitter {
4807 protected:
4808   CodeGenFunction &CGF;
4809 
4810   /// We do nothing if this is, or becomes, nullptr.
4811   const AlignedAttrTy *AA = nullptr;
4812 
4813   llvm::Value *Alignment = nullptr;      // May or may not be a constant.
4814   llvm::ConstantInt *OffsetCI = nullptr; // Constant, hopefully zero.
4815 
4816   AbstractAssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
4817       : CGF(CGF_) {
4818     if (!FuncDecl)
4819       return;
4820     AA = FuncDecl->getAttr<AlignedAttrTy>();
4821   }
4822 
4823 public:
4824   /// If we can, materialize the alignment as an attribute on return value.
4825   [[nodiscard]] llvm::AttributeList
4826   TryEmitAsCallSiteAttribute(const llvm::AttributeList &Attrs) {
4827     if (!AA || OffsetCI || CGF.SanOpts.has(SanitizerKind::Alignment))
4828       return Attrs;
4829     const auto *AlignmentCI = dyn_cast<llvm::ConstantInt>(Alignment);
4830     if (!AlignmentCI)
4831       return Attrs;
4832     // We may legitimately have non-power-of-2 alignment here.
4833     // If so, this is UB land, emit it via `@llvm.assume` instead.
4834     if (!AlignmentCI->getValue().isPowerOf2())
4835       return Attrs;
4836     llvm::AttributeList NewAttrs = maybeRaiseRetAlignmentAttribute(
4837         CGF.getLLVMContext(), Attrs,
4838         llvm::Align(
4839             AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment)));
4840     AA = nullptr; // We're done. Disallow doing anything else.
4841     return NewAttrs;
4842   }
4843 
4844   /// Emit alignment assumption.
4845   /// This is a general fallback that we take if either there is an offset,
4846   /// or the alignment is variable or we are sanitizing for alignment.
4847   void EmitAsAnAssumption(SourceLocation Loc, QualType RetTy, RValue &Ret) {
4848     if (!AA)
4849       return;
4850     CGF.emitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc,
4851                                 AA->getLocation(), Alignment, OffsetCI);
4852     AA = nullptr; // We're done. Disallow doing anything else.
4853   }
4854 };
4855 
4856 /// Helper data structure to emit `AssumeAlignedAttr`.
4857 class AssumeAlignedAttrEmitter final
4858     : public AbstractAssumeAlignedAttrEmitter<AssumeAlignedAttr> {
4859 public:
4860   AssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
4861       : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
4862     if (!AA)
4863       return;
4864     // It is guaranteed that the alignment/offset are constants.
4865     Alignment = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AA->getAlignment()));
4866     if (Expr *Offset = AA->getOffset()) {
4867       OffsetCI = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(Offset));
4868       if (OffsetCI->isNullValue()) // Canonicalize zero offset to no offset.
4869         OffsetCI = nullptr;
4870     }
4871   }
4872 };
4873 
4874 /// Helper data structure to emit `AllocAlignAttr`.
4875 class AllocAlignAttrEmitter final
4876     : public AbstractAssumeAlignedAttrEmitter<AllocAlignAttr> {
4877 public:
4878   AllocAlignAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl,
4879                         const CallArgList &CallArgs)
4880       : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
4881     if (!AA)
4882       return;
4883     // Alignment may or may not be a constant, and that is okay.
4884     Alignment = CallArgs[AA->getParamIndex().getLLVMIndex()]
4885                     .getRValue(CGF)
4886                     .getScalarVal();
4887   }
4888 };
4889 
4890 } // namespace
4891 
4892 static unsigned getMaxVectorWidth(const llvm::Type *Ty) {
4893   if (auto *VT = dyn_cast<llvm::VectorType>(Ty))
4894     return VT->getPrimitiveSizeInBits().getKnownMinValue();
4895   if (auto *AT = dyn_cast<llvm::ArrayType>(Ty))
4896     return getMaxVectorWidth(AT->getElementType());
4897 
4898   unsigned MaxVectorWidth = 0;
4899   if (auto *ST = dyn_cast<llvm::StructType>(Ty))
4900     for (auto *I : ST->elements())
4901       MaxVectorWidth = std::max(MaxVectorWidth, getMaxVectorWidth(I));
4902   return MaxVectorWidth;
4903 }
4904 
4905 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
4906                                  const CGCallee &Callee,
4907                                  ReturnValueSlot ReturnValue,
4908                                  const CallArgList &CallArgs,
4909                                  llvm::CallBase **callOrInvoke, bool IsMustTail,
4910                                  SourceLocation Loc) {
4911   // FIXME: We no longer need the types from CallArgs; lift up and simplify.
4912 
4913   assert(Callee.isOrdinary() || Callee.isVirtual());
4914 
4915   // Handle struct-return functions by passing a pointer to the
4916   // location that we would like to return into.
4917   QualType RetTy = CallInfo.getReturnType();
4918   const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
4919 
4920   llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);
4921 
4922   const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
4923   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
4924     // We can only guarantee that a function is called from the correct
4925     // context/function based on the appropriate target attributes,
4926     // so only check in the case where we have both always_inline and target
4927     // since otherwise we could be making a conditional call after a check for
4928     // the proper cpu features (and it won't cause code generation issues due to
4929     // function based code generation).
4930     if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&
4931         (TargetDecl->hasAttr<TargetAttr>() ||
4932          (CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>())))
4933       checkTargetFeatures(Loc, FD);
4934 
4935     // Some architectures (such as x86-64) have the ABI changed based on
4936     // attribute-target/features. Give them a chance to diagnose.
4937     CGM.getTargetCodeGenInfo().checkFunctionCallABI(
4938         CGM, Loc, dyn_cast_or_null<FunctionDecl>(CurCodeDecl), FD, CallArgs);
4939   }
4940 
4941   // 1. Set up the arguments.
4942 
4943   // If we're using inalloca, insert the allocation after the stack save.
4944   // FIXME: Do this earlier rather than hacking it in here!
4945   Address ArgMemory = Address::invalid();
4946   if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
4947     const llvm::DataLayout &DL = CGM.getDataLayout();
4948     llvm::Instruction *IP = CallArgs.getStackBase();
4949     llvm::AllocaInst *AI;
4950     if (IP) {
4951       IP = IP->getNextNode();
4952       AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),
4953                                 "argmem", IP);
4954     } else {
4955       AI = CreateTempAlloca(ArgStruct, "argmem");
4956     }
4957     auto Align = CallInfo.getArgStructAlignment();
4958     AI->setAlignment(Align.getAsAlign());
4959     AI->setUsedWithInAlloca(true);
4960     assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
4961     ArgMemory = Address(AI, ArgStruct, Align);
4962   }
4963 
4964   ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
4965   SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
4966 
4967   // If the call returns a temporary with struct return, create a temporary
4968   // alloca to hold the result, unless one is given to us.
4969   Address SRetPtr = Address::invalid();
4970   Address SRetAlloca = Address::invalid();
4971   llvm::Value *UnusedReturnSizePtr = nullptr;
4972   if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
4973     if (!ReturnValue.isNull()) {
4974       SRetPtr = ReturnValue.getValue();
4975     } else {
4976       SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca);
4977       if (HaveInsertPoint() && ReturnValue.isUnused()) {
4978         llvm::TypeSize size =
4979             CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
4980         UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer());
4981       }
4982     }
4983     if (IRFunctionArgs.hasSRetArg()) {
4984       IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
4985     } else if (RetAI.isInAlloca()) {
4986       Address Addr =
4987           Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
4988       Builder.CreateStore(SRetPtr.getPointer(), Addr);
4989     }
4990   }
4991 
4992   Address swiftErrorTemp = Address::invalid();
4993   Address swiftErrorArg = Address::invalid();
4994 
4995   // When passing arguments using temporary allocas, we need to add the
4996   // appropriate lifetime markers. This vector keeps track of all the lifetime
4997   // markers that need to be ended right after the call.
4998   SmallVector<CallLifetimeEnd, 2> CallLifetimeEndAfterCall;
4999 
5000   // Translate all of the arguments as necessary to match the IR lowering.
5001   assert(CallInfo.arg_size() == CallArgs.size() &&
5002          "Mismatch between function signature & arguments.");
5003   unsigned ArgNo = 0;
5004   CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
5005   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
5006        I != E; ++I, ++info_it, ++ArgNo) {
5007     const ABIArgInfo &ArgInfo = info_it->info;
5008 
5009     // Insert a padding argument to ensure proper alignment.
5010     if (IRFunctionArgs.hasPaddingArg(ArgNo))
5011       IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
5012           llvm::UndefValue::get(ArgInfo.getPaddingType());
5013 
5014     unsigned FirstIRArg, NumIRArgs;
5015     std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
5016 
5017     bool ArgHasMaybeUndefAttr =
5018         IsArgumentMaybeUndef(TargetDecl, CallInfo.getNumRequiredArgs(), ArgNo);
5019 
5020     switch (ArgInfo.getKind()) {
5021     case ABIArgInfo::InAlloca: {
5022       assert(NumIRArgs == 0);
5023       assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
5024       if (I->isAggregate()) {
5025         Address Addr = I->hasLValue()
5026                            ? I->getKnownLValue().getAddress(*this)
5027                            : I->getKnownRValue().getAggregateAddress();
5028         llvm::Instruction *Placeholder =
5029             cast<llvm::Instruction>(Addr.getPointer());
5030 
5031         if (!ArgInfo.getInAllocaIndirect()) {
5032           // Replace the placeholder with the appropriate argument slot GEP.
5033           CGBuilderTy::InsertPoint IP = Builder.saveIP();
5034           Builder.SetInsertPoint(Placeholder);
5035           Addr = Builder.CreateStructGEP(ArgMemory,
5036                                          ArgInfo.getInAllocaFieldIndex());
5037           Builder.restoreIP(IP);
5038         } else {
5039           // For indirect things such as overaligned structs, replace the
5040           // placeholder with a regular aggregate temporary alloca. Store the
5041           // address of this alloca into the struct.
5042           Addr = CreateMemTemp(info_it->type, "inalloca.indirect.tmp");
5043           Address ArgSlot = Builder.CreateStructGEP(
5044               ArgMemory, ArgInfo.getInAllocaFieldIndex());
5045           Builder.CreateStore(Addr.getPointer(), ArgSlot);
5046         }
5047         deferPlaceholderReplacement(Placeholder, Addr.getPointer());
5048       } else if (ArgInfo.getInAllocaIndirect()) {
5049         // Make a temporary alloca and store the address of it into the argument
5050         // struct.
5051         Address Addr = CreateMemTempWithoutCast(
5052             I->Ty, getContext().getTypeAlignInChars(I->Ty),
5053             "indirect-arg-temp");
5054         I->copyInto(*this, Addr);
5055         Address ArgSlot =
5056             Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
5057         Builder.CreateStore(Addr.getPointer(), ArgSlot);
5058       } else {
5059         // Store the RValue into the argument struct.
5060         Address Addr =
5061             Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
5062         Addr = Addr.withElementType(ConvertTypeForMem(I->Ty));
5063         I->copyInto(*this, Addr);
5064       }
5065       break;
5066     }
5067 
5068     case ABIArgInfo::Indirect:
5069     case ABIArgInfo::IndirectAliased: {
5070       assert(NumIRArgs == 1);
5071       if (!I->isAggregate()) {
5072         // Make a temporary alloca to pass the argument.
5073         Address Addr = CreateMemTempWithoutCast(
5074             I->Ty, ArgInfo.getIndirectAlign(), "indirect-arg-temp");
5075 
5076         llvm::Value *Val = Addr.getPointer();
5077         if (ArgHasMaybeUndefAttr)
5078           Val = Builder.CreateFreeze(Addr.getPointer());
5079         IRCallArgs[FirstIRArg] = Val;
5080 
5081         I->copyInto(*this, Addr);
5082       } else {
5083         // We want to avoid creating an unnecessary temporary+copy here;
5084         // however, we need one in three cases:
5085         // 1. If the argument is not byval, and we are required to copy the
5086         //    source.  (This case doesn't occur on any common architecture.)
5087         // 2. If the argument is byval, RV is not sufficiently aligned, and
5088         //    we cannot force it to be sufficiently aligned.
5089         // 3. If the argument is byval, but RV is not located in default
5090         //    or alloca address space.
5091         Address Addr = I->hasLValue()
5092                            ? I->getKnownLValue().getAddress(*this)
5093                            : I->getKnownRValue().getAggregateAddress();
5094         llvm::Value *V = Addr.getPointer();
5095         CharUnits Align = ArgInfo.getIndirectAlign();
5096         const llvm::DataLayout *TD = &CGM.getDataLayout();
5097 
5098         assert((FirstIRArg >= IRFuncTy->getNumParams() ||
5099                 IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==
5100                     TD->getAllocaAddrSpace()) &&
5101                "indirect argument must be in alloca address space");
5102 
5103         bool NeedCopy = false;
5104         if (Addr.getAlignment() < Align &&
5105             llvm::getOrEnforceKnownAlignment(V, Align.getAsAlign(), *TD) <
5106                 Align.getAsAlign()) {
5107           NeedCopy = true;
5108         } else if (I->hasLValue()) {
5109           auto LV = I->getKnownLValue();
5110           auto AS = LV.getAddressSpace();
5111 
5112           if (!ArgInfo.getIndirectByVal() ||
5113               (LV.getAlignment() < getContext().getTypeAlignInChars(I->Ty))) {
5114             NeedCopy = true;
5115           }
5116           if (!getLangOpts().OpenCL) {
5117             if ((ArgInfo.getIndirectByVal() &&
5118                 (AS != LangAS::Default &&
5119                  AS != CGM.getASTAllocaAddressSpace()))) {
5120               NeedCopy = true;
5121             }
5122           }
5123           // For OpenCL even if RV is located in default or alloca address space
5124           // we don't want to perform address space cast for it.
5125           else if ((ArgInfo.getIndirectByVal() &&
5126                     Addr.getType()->getAddressSpace() != IRFuncTy->
5127                       getParamType(FirstIRArg)->getPointerAddressSpace())) {
5128             NeedCopy = true;
5129           }
5130         }
5131 
5132         if (NeedCopy) {
5133           // Create an aligned temporary, and copy to it.
5134           Address AI = CreateMemTempWithoutCast(
5135               I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");
5136           llvm::Value *Val = AI.getPointer();
5137           if (ArgHasMaybeUndefAttr)
5138             Val = Builder.CreateFreeze(AI.getPointer());
5139           IRCallArgs[FirstIRArg] = Val;
5140 
5141           // Emit lifetime markers for the temporary alloca.
5142           llvm::TypeSize ByvalTempElementSize =
5143               CGM.getDataLayout().getTypeAllocSize(AI.getElementType());
5144           llvm::Value *LifetimeSize =
5145               EmitLifetimeStart(ByvalTempElementSize, AI.getPointer());
5146 
5147           // Add cleanup code to emit the end lifetime marker after the call.
5148           if (LifetimeSize) // In case we disabled lifetime markers.
5149             CallLifetimeEndAfterCall.emplace_back(AI, LifetimeSize);
5150 
5151           // Generate the copy.
5152           I->copyInto(*this, AI);
5153         } else {
5154           // Skip the extra memcpy call.
5155           auto *T = llvm::PointerType::get(
5156               CGM.getLLVMContext(), CGM.getDataLayout().getAllocaAddrSpace());
5157 
5158           llvm::Value *Val = getTargetHooks().performAddrSpaceCast(
5159               *this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T,
5160               true);
5161           if (ArgHasMaybeUndefAttr)
5162             Val = Builder.CreateFreeze(Val);
5163           IRCallArgs[FirstIRArg] = Val;
5164         }
5165       }
5166       break;
5167     }
5168 
5169     case ABIArgInfo::Ignore:
5170       assert(NumIRArgs == 0);
5171       break;
5172 
5173     case ABIArgInfo::Extend:
5174     case ABIArgInfo::Direct: {
5175       if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
5176           ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
5177           ArgInfo.getDirectOffset() == 0) {
5178         assert(NumIRArgs == 1);
5179         llvm::Value *V;
5180         if (!I->isAggregate())
5181           V = I->getKnownRValue().getScalarVal();
5182         else
5183           V = Builder.CreateLoad(
5184               I->hasLValue() ? I->getKnownLValue().getAddress(*this)
5185                              : I->getKnownRValue().getAggregateAddress());
5186 
5187         // Implement swifterror by copying into a new swifterror argument.
5188         // We'll write back in the normal path out of the call.
5189         if (CallInfo.getExtParameterInfo(ArgNo).getABI()
5190               == ParameterABI::SwiftErrorResult) {
5191           assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
5192 
5193           QualType pointeeTy = I->Ty->getPointeeType();
5194           swiftErrorArg = Address(V, ConvertTypeForMem(pointeeTy),
5195                                   getContext().getTypeAlignInChars(pointeeTy));
5196 
5197           swiftErrorTemp =
5198             CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
5199           V = swiftErrorTemp.getPointer();
5200           cast<llvm::AllocaInst>(V)->setSwiftError(true);
5201 
5202           llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
5203           Builder.CreateStore(errorValue, swiftErrorTemp);
5204         }
5205 
5206         // We might have to widen integers, but we should never truncate.
5207         if (ArgInfo.getCoerceToType() != V->getType() &&
5208             V->getType()->isIntegerTy())
5209           V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
5210 
5211         // If the argument doesn't match, perform a bitcast to coerce it.  This
5212         // can happen due to trivial type mismatches.
5213         if (FirstIRArg < IRFuncTy->getNumParams() &&
5214             V->getType() != IRFuncTy->getParamType(FirstIRArg))
5215           V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
5216 
5217         if (ArgHasMaybeUndefAttr)
5218           V = Builder.CreateFreeze(V);
5219         IRCallArgs[FirstIRArg] = V;
5220         break;
5221       }
5222 
5223       // FIXME: Avoid the conversion through memory if possible.
5224       Address Src = Address::invalid();
5225       if (!I->isAggregate()) {
5226         Src = CreateMemTemp(I->Ty, "coerce");
5227         I->copyInto(*this, Src);
5228       } else {
5229         Src = I->hasLValue() ? I->getKnownLValue().getAddress(*this)
5230                              : I->getKnownRValue().getAggregateAddress();
5231       }
5232 
5233       // If the value is offset in memory, apply the offset now.
5234       Src = emitAddressAtOffset(*this, Src, ArgInfo);
5235 
5236       // Fast-isel and the optimizer generally like scalar values better than
5237       // FCAs, so we flatten them if this is safe to do for this argument.
5238       llvm::StructType *STy =
5239             dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
5240       if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
5241         llvm::Type *SrcTy = Src.getElementType();
5242         llvm::TypeSize SrcTypeSize =
5243             CGM.getDataLayout().getTypeAllocSize(SrcTy);
5244         llvm::TypeSize DstTypeSize = CGM.getDataLayout().getTypeAllocSize(STy);
5245         if (SrcTypeSize.isScalable()) {
5246           assert(STy->containsHomogeneousScalableVectorTypes() &&
5247                  "ABI only supports structure with homogeneous scalable vector "
5248                  "type");
5249           assert(SrcTypeSize == DstTypeSize &&
5250                  "Only allow non-fractional movement of structure with "
5251                  "homogeneous scalable vector type");
5252           assert(NumIRArgs == STy->getNumElements());
5253 
5254           llvm::Value *StoredStructValue =
5255               Builder.CreateLoad(Src, Src.getName() + ".tuple");
5256           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5257             llvm::Value *Extract = Builder.CreateExtractValue(
5258                 StoredStructValue, i, Src.getName() + ".extract" + Twine(i));
5259             IRCallArgs[FirstIRArg + i] = Extract;
5260           }
5261         } else {
5262           uint64_t SrcSize = SrcTypeSize.getFixedValue();
5263           uint64_t DstSize = DstTypeSize.getFixedValue();
5264 
5265           // If the source type is smaller than the destination type of the
5266           // coerce-to logic, copy the source value into a temp alloca the size
5267           // of the destination type to allow loading all of it. The bits past
5268           // the source value are left undef.
5269           if (SrcSize < DstSize) {
5270             Address TempAlloca = CreateTempAlloca(STy, Src.getAlignment(),
5271                                                   Src.getName() + ".coerce");
5272             Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
5273             Src = TempAlloca;
5274           } else {
5275             Src = Src.withElementType(STy);
5276           }
5277 
5278           assert(NumIRArgs == STy->getNumElements());
5279           for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5280             Address EltPtr = Builder.CreateStructGEP(Src, i);
5281             llvm::Value *LI = Builder.CreateLoad(EltPtr);
5282             if (ArgHasMaybeUndefAttr)
5283               LI = Builder.CreateFreeze(LI);
5284             IRCallArgs[FirstIRArg + i] = LI;
5285           }
5286         }
5287       } else {
5288         // In the simple case, just pass the coerced loaded value.
5289         assert(NumIRArgs == 1);
5290         llvm::Value *Load =
5291             CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
5292 
5293         if (CallInfo.isCmseNSCall()) {
5294           // For certain parameter types, clear padding bits, as they may reveal
5295           // sensitive information.
5296           // Small struct/union types are passed as integer arrays.
5297           auto *ATy = dyn_cast<llvm::ArrayType>(Load->getType());
5298           if (ATy != nullptr && isa<RecordType>(I->Ty.getCanonicalType()))
5299             Load = EmitCMSEClearRecord(Load, ATy, I->Ty);
5300         }
5301 
5302         if (ArgHasMaybeUndefAttr)
5303           Load = Builder.CreateFreeze(Load);
5304         IRCallArgs[FirstIRArg] = Load;
5305       }
5306 
5307       break;
5308     }
5309 
5310     case ABIArgInfo::CoerceAndExpand: {
5311       auto coercionType = ArgInfo.getCoerceAndExpandType();
5312       auto layout = CGM.getDataLayout().getStructLayout(coercionType);
5313 
5314       llvm::Value *tempSize = nullptr;
5315       Address addr = Address::invalid();
5316       Address AllocaAddr = Address::invalid();
5317       if (I->isAggregate()) {
5318         addr = I->hasLValue() ? I->getKnownLValue().getAddress(*this)
5319                               : I->getKnownRValue().getAggregateAddress();
5320 
5321       } else {
5322         RValue RV = I->getKnownRValue();
5323         assert(RV.isScalar()); // complex should always just be direct
5324 
5325         llvm::Type *scalarType = RV.getScalarVal()->getType();
5326         auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
5327         auto scalarAlign = CGM.getDataLayout().getPrefTypeAlign(scalarType);
5328 
5329         // Materialize to a temporary.
5330         addr = CreateTempAlloca(
5331             RV.getScalarVal()->getType(),
5332             CharUnits::fromQuantity(std::max(layout->getAlignment(), scalarAlign)),
5333             "tmp",
5334             /*ArraySize=*/nullptr, &AllocaAddr);
5335         tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());
5336 
5337         Builder.CreateStore(RV.getScalarVal(), addr);
5338       }
5339 
5340       addr = addr.withElementType(coercionType);
5341 
5342       unsigned IRArgPos = FirstIRArg;
5343       for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
5344         llvm::Type *eltType = coercionType->getElementType(i);
5345         if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
5346         Address eltAddr = Builder.CreateStructGEP(addr, i);
5347         llvm::Value *elt = Builder.CreateLoad(eltAddr);
5348         if (ArgHasMaybeUndefAttr)
5349           elt = Builder.CreateFreeze(elt);
5350         IRCallArgs[IRArgPos++] = elt;
5351       }
5352       assert(IRArgPos == FirstIRArg + NumIRArgs);
5353 
5354       if (tempSize) {
5355         EmitLifetimeEnd(tempSize, AllocaAddr.getPointer());
5356       }
5357 
5358       break;
5359     }
5360 
5361     case ABIArgInfo::Expand: {
5362       unsigned IRArgPos = FirstIRArg;
5363       ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);
5364       assert(IRArgPos == FirstIRArg + NumIRArgs);
5365       break;
5366     }
5367     }
5368   }
5369 
5370   const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);
5371   llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();
5372 
5373   // If we're using inalloca, set up that argument.
5374   if (ArgMemory.isValid()) {
5375     llvm::Value *Arg = ArgMemory.getPointer();
5376     assert(IRFunctionArgs.hasInallocaArg());
5377     IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
5378   }
5379 
5380   // 2. Prepare the function pointer.
5381 
5382   // If the callee is a bitcast of a non-variadic function to have a
5383   // variadic function pointer type, check to see if we can remove the
5384   // bitcast.  This comes up with unprototyped functions.
5385   //
5386   // This makes the IR nicer, but more importantly it ensures that we
5387   // can inline the function at -O0 if it is marked always_inline.
5388   auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT,
5389                                    llvm::Value *Ptr) -> llvm::Function * {
5390     if (!CalleeFT->isVarArg())
5391       return nullptr;
5392 
5393     // Get underlying value if it's a bitcast
5394     if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) {
5395       if (CE->getOpcode() == llvm::Instruction::BitCast)
5396         Ptr = CE->getOperand(0);
5397     }
5398 
5399     llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr);
5400     if (!OrigFn)
5401       return nullptr;
5402 
5403     llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
5404 
5405     // If the original type is variadic, or if any of the component types
5406     // disagree, we cannot remove the cast.
5407     if (OrigFT->isVarArg() ||
5408         OrigFT->getNumParams() != CalleeFT->getNumParams() ||
5409         OrigFT->getReturnType() != CalleeFT->getReturnType())
5410       return nullptr;
5411 
5412     for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
5413       if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
5414         return nullptr;
5415 
5416     return OrigFn;
5417   };
5418 
5419   if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) {
5420     CalleePtr = OrigFn;
5421     IRFuncTy = OrigFn->getFunctionType();
5422   }
5423 
5424   // 3. Perform the actual call.
5425 
5426   // Deactivate any cleanups that we're supposed to do immediately before
5427   // the call.
5428   if (!CallArgs.getCleanupsToDeactivate().empty())
5429     deactivateArgCleanupsBeforeCall(*this, CallArgs);
5430 
5431   // Assert that the arguments we computed match up.  The IR verifier
5432   // will catch this, but this is a common enough source of problems
5433   // during IRGen changes that it's way better for debugging to catch
5434   // it ourselves here.
5435 #ifndef NDEBUG
5436   assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
5437   for (unsigned i = 0; i < IRCallArgs.size(); ++i) {
5438     // Inalloca argument can have different type.
5439     if (IRFunctionArgs.hasInallocaArg() &&
5440         i == IRFunctionArgs.getInallocaArgNo())
5441       continue;
5442     if (i < IRFuncTy->getNumParams())
5443       assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
5444   }
5445 #endif
5446 
5447   // Update the largest vector width if any arguments have vector types.
5448   for (unsigned i = 0; i < IRCallArgs.size(); ++i)
5449     LargestVectorWidth = std::max(LargestVectorWidth,
5450                                   getMaxVectorWidth(IRCallArgs[i]->getType()));
5451 
5452   // Compute the calling convention and attributes.
5453   unsigned CallingConv;
5454   llvm::AttributeList Attrs;
5455   CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
5456                              Callee.getAbstractInfo(), Attrs, CallingConv,
5457                              /*AttrOnCallSite=*/true,
5458                              /*IsThunk=*/false);
5459 
5460   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
5461     if (FD->hasAttr<StrictFPAttr>())
5462       // All calls within a strictfp function are marked strictfp
5463       Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
5464 
5465   // Add call-site nomerge attribute if exists.
5466   if (InNoMergeAttributedStmt)
5467     Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge);
5468 
5469   // Add call-site noinline attribute if exists.
5470   if (InNoInlineAttributedStmt)
5471     Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
5472 
5473   // Add call-site always_inline attribute if exists.
5474   if (InAlwaysInlineAttributedStmt)
5475     Attrs =
5476         Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
5477 
5478   // Apply some call-site-specific attributes.
5479   // TODO: work this into building the attribute set.
5480 
5481   // Apply always_inline to all calls within flatten functions.
5482   // FIXME: should this really take priority over __try, below?
5483   if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
5484       !InNoInlineAttributedStmt &&
5485       !(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) {
5486     Attrs =
5487         Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
5488   }
5489 
5490   // Disable inlining inside SEH __try blocks.
5491   if (isSEHTryScope()) {
5492     Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
5493   }
5494 
5495   // Decide whether to use a call or an invoke.
5496   bool CannotThrow;
5497   if (currentFunctionUsesSEHTry()) {
5498     // SEH cares about asynchronous exceptions, so everything can "throw."
5499     CannotThrow = false;
5500   } else if (isCleanupPadScope() &&
5501              EHPersonality::get(*this).isMSVCXXPersonality()) {
5502     // The MSVC++ personality will implicitly terminate the program if an
5503     // exception is thrown during a cleanup outside of a try/catch.
5504     // We don't need to model anything in IR to get this behavior.
5505     CannotThrow = true;
5506   } else {
5507     // Otherwise, nounwind call sites will never throw.
5508     CannotThrow = Attrs.hasFnAttr(llvm::Attribute::NoUnwind);
5509 
5510     if (auto *FPtr = dyn_cast<llvm::Function>(CalleePtr))
5511       if (FPtr->hasFnAttribute(llvm::Attribute::NoUnwind))
5512         CannotThrow = true;
5513   }
5514 
5515   // If we made a temporary, be sure to clean up after ourselves. Note that we
5516   // can't depend on being inside of an ExprWithCleanups, so we need to manually
5517   // pop this cleanup later on. Being eager about this is OK, since this
5518   // temporary is 'invisible' outside of the callee.
5519   if (UnusedReturnSizePtr)
5520     pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,
5521                                          UnusedReturnSizePtr);
5522 
5523   llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
5524 
5525   SmallVector<llvm::OperandBundleDef, 1> BundleList =
5526       getBundlesForFunclet(CalleePtr);
5527 
5528   if (SanOpts.has(SanitizerKind::KCFI) &&
5529       !isa_and_nonnull<FunctionDecl>(TargetDecl))
5530     EmitKCFIOperandBundle(ConcreteCallee, BundleList);
5531 
5532   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
5533     if (FD->hasAttr<StrictFPAttr>())
5534       // All calls within a strictfp function are marked strictfp
5535       Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
5536 
5537   AssumeAlignedAttrEmitter AssumeAlignedAttrEmitter(*this, TargetDecl);
5538   Attrs = AssumeAlignedAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
5539 
5540   AllocAlignAttrEmitter AllocAlignAttrEmitter(*this, TargetDecl, CallArgs);
5541   Attrs = AllocAlignAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
5542 
5543   // Emit the actual call/invoke instruction.
5544   llvm::CallBase *CI;
5545   if (!InvokeDest) {
5546     CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList);
5547   } else {
5548     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
5549     CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs,
5550                               BundleList);
5551     EmitBlock(Cont);
5552   }
5553   if (callOrInvoke)
5554     *callOrInvoke = CI;
5555 
5556   // If this is within a function that has the guard(nocf) attribute and is an
5557   // indirect call, add the "guard_nocf" attribute to this call to indicate that
5558   // Control Flow Guard checks should not be added, even if the call is inlined.
5559   if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
5560     if (const auto *A = FD->getAttr<CFGuardAttr>()) {
5561       if (A->getGuard() == CFGuardAttr::GuardArg::nocf && !CI->getCalledFunction())
5562         Attrs = Attrs.addFnAttribute(getLLVMContext(), "guard_nocf");
5563     }
5564   }
5565 
5566   // Apply the attributes and calling convention.
5567   CI->setAttributes(Attrs);
5568   CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
5569 
5570   // Apply various metadata.
5571 
5572   if (!CI->getType()->isVoidTy())
5573     CI->setName("call");
5574 
5575   // Update largest vector width from the return type.
5576   LargestVectorWidth =
5577       std::max(LargestVectorWidth, getMaxVectorWidth(CI->getType()));
5578 
5579   // Insert instrumentation or attach profile metadata at indirect call sites.
5580   // For more details, see the comment before the definition of
5581   // IPVK_IndirectCallTarget in InstrProfData.inc.
5582   if (!CI->getCalledFunction())
5583     PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,
5584                      CI, CalleePtr);
5585 
5586   // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
5587   // optimizer it can aggressively ignore unwind edges.
5588   if (CGM.getLangOpts().ObjCAutoRefCount)
5589     AddObjCARCExceptionMetadata(CI);
5590 
5591   // Set tail call kind if necessary.
5592   if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
5593     if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
5594       Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
5595     else if (IsMustTail)
5596       Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
5597   }
5598 
5599   // Add metadata for calls to MSAllocator functions
5600   if (getDebugInfo() && TargetDecl &&
5601       TargetDecl->hasAttr<MSAllocatorAttr>())
5602     getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy->getPointeeType(), Loc);
5603 
5604   // Add metadata if calling an __attribute__((error(""))) or warning fn.
5605   if (TargetDecl && TargetDecl->hasAttr<ErrorAttr>()) {
5606     llvm::ConstantInt *Line =
5607         llvm::ConstantInt::get(Int32Ty, Loc.getRawEncoding());
5608     llvm::ConstantAsMetadata *MD = llvm::ConstantAsMetadata::get(Line);
5609     llvm::MDTuple *MDT = llvm::MDNode::get(getLLVMContext(), {MD});
5610     CI->setMetadata("srcloc", MDT);
5611   }
5612 
5613   // 4. Finish the call.
5614 
5615   // If the call doesn't return, finish the basic block and clear the
5616   // insertion point; this allows the rest of IRGen to discard
5617   // unreachable code.
5618   if (CI->doesNotReturn()) {
5619     if (UnusedReturnSizePtr)
5620       PopCleanupBlock();
5621 
5622     // Strip away the noreturn attribute to better diagnose unreachable UB.
5623     if (SanOpts.has(SanitizerKind::Unreachable)) {
5624       // Also remove from function since CallBase::hasFnAttr additionally checks
5625       // attributes of the called function.
5626       if (auto *F = CI->getCalledFunction())
5627         F->removeFnAttr(llvm::Attribute::NoReturn);
5628       CI->removeFnAttr(llvm::Attribute::NoReturn);
5629 
5630       // Avoid incompatibility with ASan which relies on the `noreturn`
5631       // attribute to insert handler calls.
5632       if (SanOpts.hasOneOf(SanitizerKind::Address |
5633                            SanitizerKind::KernelAddress)) {
5634         SanitizerScope SanScope(this);
5635         llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
5636         Builder.SetInsertPoint(CI);
5637         auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
5638         llvm::FunctionCallee Fn =
5639             CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return");
5640         EmitNounwindRuntimeCall(Fn);
5641       }
5642     }
5643 
5644     EmitUnreachable(Loc);
5645     Builder.ClearInsertionPoint();
5646 
5647     // FIXME: For now, emit a dummy basic block because expr emitters in
5648     // generally are not ready to handle emitting expressions at unreachable
5649     // points.
5650     EnsureInsertPoint();
5651 
5652     // Return a reasonable RValue.
5653     return GetUndefRValue(RetTy);
5654   }
5655 
5656   // If this is a musttail call, return immediately. We do not branch to the
5657   // epilogue in this case.
5658   if (IsMustTail) {
5659     for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();
5660          ++it) {
5661       EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);
5662       if (!(Cleanup && Cleanup->getCleanup()->isRedundantBeforeReturn()))
5663         CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");
5664     }
5665     if (CI->getType()->isVoidTy())
5666       Builder.CreateRetVoid();
5667     else
5668       Builder.CreateRet(CI);
5669     Builder.ClearInsertionPoint();
5670     EnsureInsertPoint();
5671     return GetUndefRValue(RetTy);
5672   }
5673 
5674   // Perform the swifterror writeback.
5675   if (swiftErrorTemp.isValid()) {
5676     llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
5677     Builder.CreateStore(errorResult, swiftErrorArg);
5678   }
5679 
5680   // Emit any call-associated writebacks immediately.  Arguably this
5681   // should happen after any return-value munging.
5682   if (CallArgs.hasWritebacks())
5683     emitWritebacks(*this, CallArgs);
5684 
5685   // The stack cleanup for inalloca arguments has to run out of the normal
5686   // lexical order, so deactivate it and run it manually here.
5687   CallArgs.freeArgumentMemory(*this);
5688 
5689   // Extract the return value.
5690   RValue Ret = [&] {
5691     switch (RetAI.getKind()) {
5692     case ABIArgInfo::CoerceAndExpand: {
5693       auto coercionType = RetAI.getCoerceAndExpandType();
5694 
5695       Address addr = SRetPtr.withElementType(coercionType);
5696 
5697       assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
5698       bool requiresExtract = isa<llvm::StructType>(CI->getType());
5699 
5700       unsigned unpaddedIndex = 0;
5701       for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
5702         llvm::Type *eltType = coercionType->getElementType(i);
5703         if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;
5704         Address eltAddr = Builder.CreateStructGEP(addr, i);
5705         llvm::Value *elt = CI;
5706         if (requiresExtract)
5707           elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
5708         else
5709           assert(unpaddedIndex == 0);
5710         Builder.CreateStore(elt, eltAddr);
5711       }
5712       [[fallthrough]];
5713     }
5714 
5715     case ABIArgInfo::InAlloca:
5716     case ABIArgInfo::Indirect: {
5717       RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
5718       if (UnusedReturnSizePtr)
5719         PopCleanupBlock();
5720       return ret;
5721     }
5722 
5723     case ABIArgInfo::Ignore:
5724       // If we are ignoring an argument that had a result, make sure to
5725       // construct the appropriate return value for our caller.
5726       return GetUndefRValue(RetTy);
5727 
5728     case ABIArgInfo::Extend:
5729     case ABIArgInfo::Direct: {
5730       llvm::Type *RetIRTy = ConvertType(RetTy);
5731       if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
5732         switch (getEvaluationKind(RetTy)) {
5733         case TEK_Complex: {
5734           llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
5735           llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
5736           return RValue::getComplex(std::make_pair(Real, Imag));
5737         }
5738         case TEK_Aggregate: {
5739           Address DestPtr = ReturnValue.getValue();
5740           bool DestIsVolatile = ReturnValue.isVolatile();
5741 
5742           if (!DestPtr.isValid()) {
5743             DestPtr = CreateMemTemp(RetTy, "agg.tmp");
5744             DestIsVolatile = false;
5745           }
5746           EmitAggregateStore(CI, DestPtr, DestIsVolatile);
5747           return RValue::getAggregate(DestPtr);
5748         }
5749         case TEK_Scalar: {
5750           // If the argument doesn't match, perform a bitcast to coerce it.  This
5751           // can happen due to trivial type mismatches.
5752           llvm::Value *V = CI;
5753           if (V->getType() != RetIRTy)
5754             V = Builder.CreateBitCast(V, RetIRTy);
5755           return RValue::get(V);
5756         }
5757         }
5758         llvm_unreachable("bad evaluation kind");
5759       }
5760 
5761       // If coercing a fixed vector from a scalable vector for ABI
5762       // compatibility, and the types match, use the llvm.vector.extract
5763       // intrinsic to perform the conversion.
5764       if (auto *FixedDst = dyn_cast<llvm::FixedVectorType>(RetIRTy)) {
5765         llvm::Value *V = CI;
5766         if (auto *ScalableSrc = dyn_cast<llvm::ScalableVectorType>(V->getType())) {
5767           if (FixedDst->getElementType() == ScalableSrc->getElementType()) {
5768             llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);
5769             V = Builder.CreateExtractVector(FixedDst, V, Zero, "cast.fixed");
5770             return RValue::get(V);
5771           }
5772         }
5773       }
5774 
5775       Address DestPtr = ReturnValue.getValue();
5776       bool DestIsVolatile = ReturnValue.isVolatile();
5777 
5778       if (!DestPtr.isValid()) {
5779         DestPtr = CreateMemTemp(RetTy, "coerce");
5780         DestIsVolatile = false;
5781       }
5782 
5783       // An empty record can overlap other data (if declared with
5784       // no_unique_address); omit the store for such types - as there is no
5785       // actual data to store.
5786       if (!isEmptyRecord(getContext(), RetTy, true)) {
5787         // If the value is offset in memory, apply the offset now.
5788         Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
5789         CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
5790       }
5791 
5792       return convertTempToRValue(DestPtr, RetTy, SourceLocation());
5793     }
5794 
5795     case ABIArgInfo::Expand:
5796     case ABIArgInfo::IndirectAliased:
5797       llvm_unreachable("Invalid ABI kind for return argument");
5798     }
5799 
5800     llvm_unreachable("Unhandled ABIArgInfo::Kind");
5801   } ();
5802 
5803   // Emit the assume_aligned check on the return value.
5804   if (Ret.isScalar() && TargetDecl) {
5805     AssumeAlignedAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
5806     AllocAlignAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
5807   }
5808 
5809   // Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
5810   // we can't use the full cleanup mechanism.
5811   for (CallLifetimeEnd &LifetimeEnd : CallLifetimeEndAfterCall)
5812     LifetimeEnd.Emit(*this, /*Flags=*/{});
5813 
5814   if (!ReturnValue.isExternallyDestructed() &&
5815       RetTy.isDestructedType() == QualType::DK_nontrivial_c_struct)
5816     pushDestroy(QualType::DK_nontrivial_c_struct, Ret.getAggregateAddress(),
5817                 RetTy);
5818 
5819   return Ret;
5820 }
5821 
5822 CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const {
5823   if (isVirtual()) {
5824     const CallExpr *CE = getVirtualCallExpr();
5825     return CGF.CGM.getCXXABI().getVirtualFunctionPointer(
5826         CGF, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),
5827         CE ? CE->getBeginLoc() : SourceLocation());
5828   }
5829 
5830   return *this;
5831 }
5832 
5833 /* VarArg handling */
5834 
5835 Address CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr) {
5836   VAListAddr = VE->isMicrosoftABI()
5837                  ? EmitMSVAListRef(VE->getSubExpr())
5838                  : EmitVAListRef(VE->getSubExpr());
5839   QualType Ty = VE->getType();
5840   if (VE->isMicrosoftABI())
5841     return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty);
5842   return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty);
5843 }
5844