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