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