1 //===--- CGCall.cpp - Encapsulate calling convention details --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
12 //===----------------------------------------------------------------------===//
16 #include "ABIInfoImpl.h"
19 #include "CGCleanup.h"
20 #include "CGRecordLayout.h"
21 #include "CodeGenFunction.h"
22 #include "CodeGenModule.h"
23 #include "TargetInfo.h"
24 #include "clang/AST/Attr.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/AST/DeclCXX.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/Basic/CodeGenOptions.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/CodeGen/CGFunctionInfo.h"
31 #include "clang/CodeGen/SwiftCallingConv.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/Assumptions.h"
35 #include "llvm/IR/AttributeMask.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/CallingConv.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/InlineAsm.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Intrinsics.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/Transforms/Utils/Local.h"
45 using namespace clang
;
46 using namespace CodeGen
;
50 unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC
) {
52 default: return llvm::CallingConv::C
;
53 case CC_X86StdCall
: return llvm::CallingConv::X86_StdCall
;
54 case CC_X86FastCall
: return llvm::CallingConv::X86_FastCall
;
55 case CC_X86RegCall
: return llvm::CallingConv::X86_RegCall
;
56 case CC_X86ThisCall
: return llvm::CallingConv::X86_ThisCall
;
57 case CC_Win64
: return llvm::CallingConv::Win64
;
58 case CC_X86_64SysV
: return llvm::CallingConv::X86_64_SysV
;
59 case CC_AAPCS
: return llvm::CallingConv::ARM_AAPCS
;
60 case CC_AAPCS_VFP
: return llvm::CallingConv::ARM_AAPCS_VFP
;
61 case CC_IntelOclBicc
: return llvm::CallingConv::Intel_OCL_BI
;
62 // TODO: Add support for __pascal to LLVM.
63 case CC_X86Pascal
: return llvm::CallingConv::C
;
64 // TODO: Add support for __vectorcall to LLVM.
65 case CC_X86VectorCall
: return llvm::CallingConv::X86_VectorCall
;
66 case CC_AArch64VectorCall
: return llvm::CallingConv::AArch64_VectorCall
;
67 case CC_AArch64SVEPCS
: return llvm::CallingConv::AArch64_SVE_VectorCall
;
68 case CC_AMDGPUKernelCall
: return llvm::CallingConv::AMDGPU_KERNEL
;
69 case CC_SpirFunction
: return llvm::CallingConv::SPIR_FUNC
;
70 case CC_OpenCLKernel
: return CGM
.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
71 case CC_PreserveMost
: return llvm::CallingConv::PreserveMost
;
72 case CC_PreserveAll
: return llvm::CallingConv::PreserveAll
;
73 case CC_Swift
: return llvm::CallingConv::Swift
;
74 case CC_SwiftAsync
: return llvm::CallingConv::SwiftTail
;
75 case CC_M68kRTD
: return llvm::CallingConv::M68k_RTD
;
76 case CC_PreserveNone
: return llvm::CallingConv::PreserveNone
;
78 case CC_RISCVVectorCall
: return llvm::CallingConv::RISCV_VectorCall
;
83 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
84 /// qualification. Either or both of RD and MD may be null. A null RD indicates
85 /// that there is no meaningful 'this' type, and a null MD can occur when
86 /// calling a method pointer.
87 CanQualType
CodeGenTypes::DeriveThisType(const CXXRecordDecl
*RD
,
88 const CXXMethodDecl
*MD
) {
91 RecTy
= Context
.getTagDeclType(RD
)->getCanonicalTypeInternal();
93 RecTy
= Context
.VoidTy
;
96 RecTy
= Context
.getAddrSpaceQualType(RecTy
, MD
->getMethodQualifiers().getAddressSpace());
97 return Context
.getPointerType(CanQualType::CreateUnsafe(RecTy
));
100 /// Returns the canonical formal type of the given C++ method.
101 static CanQual
<FunctionProtoType
> GetFormalType(const CXXMethodDecl
*MD
) {
102 return MD
->getType()->getCanonicalTypeUnqualified()
103 .getAs
<FunctionProtoType
>();
106 /// Returns the "extra-canonicalized" return type, which discards
107 /// qualifiers on the return type. Codegen doesn't care about them,
108 /// and it makes ABI code a little easier to be able to assume that
109 /// all parameter and return types are top-level unqualified.
110 static CanQualType
GetReturnType(QualType RetTy
) {
111 return RetTy
->getCanonicalTypeUnqualified().getUnqualifiedType();
114 /// Arrange the argument and result information for a value of the given
115 /// unprototyped freestanding function type.
116 const CGFunctionInfo
&
117 CodeGenTypes::arrangeFreeFunctionType(CanQual
<FunctionNoProtoType
> FTNP
) {
118 // When translating an unprototyped function type, always use a
120 return arrangeLLVMFunctionInfo(FTNP
->getReturnType().getUnqualifiedType(),
121 FnInfoOpts::None
, std::nullopt
,
122 FTNP
->getExtInfo(), {}, RequiredArgs(0));
125 static void addExtParameterInfosForCall(
126 llvm::SmallVectorImpl
<FunctionProtoType::ExtParameterInfo
> ¶mInfos
,
127 const FunctionProtoType
*proto
,
129 unsigned totalArgs
) {
130 assert(proto
->hasExtParameterInfos());
131 assert(paramInfos
.size() <= prefixArgs
);
132 assert(proto
->getNumParams() + prefixArgs
<= totalArgs
);
134 paramInfos
.reserve(totalArgs
);
136 // Add default infos for any prefix args that don't already have infos.
137 paramInfos
.resize(prefixArgs
);
139 // Add infos for the prototype.
140 for (const auto &ParamInfo
: proto
->getExtParameterInfos()) {
141 paramInfos
.push_back(ParamInfo
);
142 // pass_object_size params have no parameter info.
143 if (ParamInfo
.hasPassObjectSize())
144 paramInfos
.emplace_back();
147 assert(paramInfos
.size() <= totalArgs
&&
148 "Did we forget to insert pass_object_size args?");
149 // Add default infos for the variadic and/or suffix arguments.
150 paramInfos
.resize(totalArgs
);
153 /// Adds the formal parameters in FPT to the given prefix. If any parameter in
154 /// FPT has pass_object_size attrs, then we'll add parameters for those, too.
155 static void appendParameterTypes(const CodeGenTypes
&CGT
,
156 SmallVectorImpl
<CanQualType
> &prefix
,
157 SmallVectorImpl
<FunctionProtoType::ExtParameterInfo
> ¶mInfos
,
158 CanQual
<FunctionProtoType
> FPT
) {
159 // Fast path: don't touch param info if we don't need to.
160 if (!FPT
->hasExtParameterInfos()) {
161 assert(paramInfos
.empty() &&
162 "We have paramInfos, but the prototype doesn't?");
163 prefix
.append(FPT
->param_type_begin(), FPT
->param_type_end());
167 unsigned PrefixSize
= prefix
.size();
168 // In the vast majority of cases, we'll have precisely FPT->getNumParams()
169 // parameters; the only thing that can change this is the presence of
170 // pass_object_size. So, we preallocate for the common case.
171 prefix
.reserve(prefix
.size() + FPT
->getNumParams());
173 auto ExtInfos
= FPT
->getExtParameterInfos();
174 assert(ExtInfos
.size() == FPT
->getNumParams());
175 for (unsigned I
= 0, E
= FPT
->getNumParams(); I
!= E
; ++I
) {
176 prefix
.push_back(FPT
->getParamType(I
));
177 if (ExtInfos
[I
].hasPassObjectSize())
178 prefix
.push_back(CGT
.getContext().getSizeType());
181 addExtParameterInfosForCall(paramInfos
, FPT
.getTypePtr(), PrefixSize
,
185 /// Arrange the LLVM function layout for a value of the given function
186 /// type, on top of any implicit parameters already stored.
187 static const CGFunctionInfo
&
188 arrangeLLVMFunctionInfo(CodeGenTypes
&CGT
, bool instanceMethod
,
189 SmallVectorImpl
<CanQualType
> &prefix
,
190 CanQual
<FunctionProtoType
> FTP
) {
191 SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
192 RequiredArgs Required
= RequiredArgs::forPrototypePlus(FTP
, prefix
.size());
194 appendParameterTypes(CGT
, prefix
, paramInfos
, FTP
);
195 CanQualType resultType
= FTP
->getReturnType().getUnqualifiedType();
198 instanceMethod
? FnInfoOpts::IsInstanceMethod
: FnInfoOpts::None
;
199 return CGT
.arrangeLLVMFunctionInfo(resultType
, opts
, prefix
,
200 FTP
->getExtInfo(), paramInfos
, Required
);
203 /// Arrange the argument and result information for a value of the
204 /// given freestanding function type.
205 const CGFunctionInfo
&
206 CodeGenTypes::arrangeFreeFunctionType(CanQual
<FunctionProtoType
> FTP
) {
207 SmallVector
<CanQualType
, 16> argTypes
;
208 return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes
,
212 static CallingConv
getCallingConventionForDecl(const ObjCMethodDecl
*D
,
214 // Set the appropriate calling convention for the Function.
215 if (D
->hasAttr
<StdCallAttr
>())
216 return CC_X86StdCall
;
218 if (D
->hasAttr
<FastCallAttr
>())
219 return CC_X86FastCall
;
221 if (D
->hasAttr
<RegCallAttr
>())
222 return CC_X86RegCall
;
224 if (D
->hasAttr
<ThisCallAttr
>())
225 return CC_X86ThisCall
;
227 if (D
->hasAttr
<VectorCallAttr
>())
228 return CC_X86VectorCall
;
230 if (D
->hasAttr
<PascalAttr
>())
233 if (PcsAttr
*PCS
= D
->getAttr
<PcsAttr
>())
234 return (PCS
->getPCS() == PcsAttr::AAPCS
? CC_AAPCS
: CC_AAPCS_VFP
);
236 if (D
->hasAttr
<AArch64VectorPcsAttr
>())
237 return CC_AArch64VectorCall
;
239 if (D
->hasAttr
<AArch64SVEPcsAttr
>())
240 return CC_AArch64SVEPCS
;
242 if (D
->hasAttr
<AMDGPUKernelCallAttr
>())
243 return CC_AMDGPUKernelCall
;
245 if (D
->hasAttr
<IntelOclBiccAttr
>())
246 return CC_IntelOclBicc
;
248 if (D
->hasAttr
<MSABIAttr
>())
249 return IsWindows
? CC_C
: CC_Win64
;
251 if (D
->hasAttr
<SysVABIAttr
>())
252 return IsWindows
? CC_X86_64SysV
: CC_C
;
254 if (D
->hasAttr
<PreserveMostAttr
>())
255 return CC_PreserveMost
;
257 if (D
->hasAttr
<PreserveAllAttr
>())
258 return CC_PreserveAll
;
260 if (D
->hasAttr
<M68kRTDAttr
>())
263 if (D
->hasAttr
<PreserveNoneAttr
>())
264 return CC_PreserveNone
;
266 if (D
->hasAttr
<RISCVVectorCCAttr
>())
267 return CC_RISCVVectorCall
;
272 /// Arrange the argument and result information for a call to an
273 /// unknown C++ non-static member function of the given abstract type.
274 /// (A null RD means we don't have any meaningful "this" argument type,
275 /// so fall back to a generic pointer type).
276 /// The member function must be an ordinary function, i.e. not a
277 /// constructor or destructor.
278 const CGFunctionInfo
&
279 CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl
*RD
,
280 const FunctionProtoType
*FTP
,
281 const CXXMethodDecl
*MD
) {
282 SmallVector
<CanQualType
, 16> argTypes
;
284 // Add the 'this' pointer.
285 argTypes
.push_back(DeriveThisType(RD
, MD
));
287 return ::arrangeLLVMFunctionInfo(
288 *this, /*instanceMethod=*/true, argTypes
,
289 FTP
->getCanonicalTypeUnqualified().getAs
<FunctionProtoType
>());
292 /// Set calling convention for CUDA/HIP kernel.
293 static void setCUDAKernelCallingConvention(CanQualType
&FTy
, CodeGenModule
&CGM
,
294 const FunctionDecl
*FD
) {
295 if (FD
->hasAttr
<CUDAGlobalAttr
>()) {
296 const FunctionType
*FT
= FTy
->getAs
<FunctionType
>();
297 CGM
.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT
);
298 FTy
= FT
->getCanonicalTypeUnqualified();
302 /// Arrange the argument and result information for a declaration or
303 /// definition of the given C++ non-static member function. The
304 /// member function must be an ordinary function, i.e. not a
305 /// constructor or destructor.
306 const CGFunctionInfo
&
307 CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl
*MD
) {
308 assert(!isa
<CXXConstructorDecl
>(MD
) && "wrong method for constructors!");
309 assert(!isa
<CXXDestructorDecl
>(MD
) && "wrong method for destructors!");
311 CanQualType FT
= GetFormalType(MD
).getAs
<Type
>();
312 setCUDAKernelCallingConvention(FT
, CGM
, MD
);
313 auto prototype
= FT
.getAs
<FunctionProtoType
>();
315 if (MD
->isImplicitObjectMemberFunction()) {
316 // The abstract case is perfectly fine.
317 const CXXRecordDecl
*ThisType
= TheCXXABI
.getThisArgumentTypeForMethod(MD
);
318 return arrangeCXXMethodType(ThisType
, prototype
.getTypePtr(), MD
);
321 return arrangeFreeFunctionType(prototype
);
324 bool CodeGenTypes::inheritingCtorHasParams(
325 const InheritedConstructor
&Inherited
, CXXCtorType Type
) {
326 // Parameters are unnecessary if we're constructing a base class subobject
327 // and the inherited constructor lives in a virtual base.
328 return Type
== Ctor_Complete
||
329 !Inherited
.getShadowDecl()->constructsVirtualBase() ||
330 !Target
.getCXXABI().hasConstructorVariants();
333 const CGFunctionInfo
&
334 CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD
) {
335 auto *MD
= cast
<CXXMethodDecl
>(GD
.getDecl());
337 SmallVector
<CanQualType
, 16> argTypes
;
338 SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
340 const CXXRecordDecl
*ThisType
= TheCXXABI
.getThisArgumentTypeForMethod(GD
);
341 argTypes
.push_back(DeriveThisType(ThisType
, MD
));
343 bool PassParams
= true;
345 if (auto *CD
= dyn_cast
<CXXConstructorDecl
>(MD
)) {
346 // A base class inheriting constructor doesn't get forwarded arguments
347 // needed to construct a virtual base (or base class thereof).
348 if (auto Inherited
= CD
->getInheritedConstructor())
349 PassParams
= inheritingCtorHasParams(Inherited
, GD
.getCtorType());
352 CanQual
<FunctionProtoType
> FTP
= GetFormalType(MD
);
354 // Add the formal parameters.
356 appendParameterTypes(*this, argTypes
, paramInfos
, FTP
);
358 CGCXXABI::AddedStructorArgCounts AddedArgs
=
359 TheCXXABI
.buildStructorSignature(GD
, argTypes
);
360 if (!paramInfos
.empty()) {
361 // Note: prefix implies after the first param.
362 if (AddedArgs
.Prefix
)
363 paramInfos
.insert(paramInfos
.begin() + 1, AddedArgs
.Prefix
,
364 FunctionProtoType::ExtParameterInfo
{});
365 if (AddedArgs
.Suffix
)
366 paramInfos
.append(AddedArgs
.Suffix
,
367 FunctionProtoType::ExtParameterInfo
{});
370 RequiredArgs required
=
371 (PassParams
&& MD
->isVariadic() ? RequiredArgs(argTypes
.size())
372 : RequiredArgs::All
);
374 FunctionType::ExtInfo extInfo
= FTP
->getExtInfo();
375 CanQualType resultType
= TheCXXABI
.HasThisReturn(GD
)
377 : TheCXXABI
.hasMostDerivedReturn(GD
)
378 ? CGM
.getContext().VoidPtrTy
380 return arrangeLLVMFunctionInfo(resultType
, FnInfoOpts::IsInstanceMethod
,
381 argTypes
, extInfo
, paramInfos
, required
);
384 static SmallVector
<CanQualType
, 16>
385 getArgTypesForCall(ASTContext
&ctx
, const CallArgList
&args
) {
386 SmallVector
<CanQualType
, 16> argTypes
;
387 for (auto &arg
: args
)
388 argTypes
.push_back(ctx
.getCanonicalParamType(arg
.Ty
));
392 static SmallVector
<CanQualType
, 16>
393 getArgTypesForDeclaration(ASTContext
&ctx
, const FunctionArgList
&args
) {
394 SmallVector
<CanQualType
, 16> argTypes
;
395 for (auto &arg
: args
)
396 argTypes
.push_back(ctx
.getCanonicalParamType(arg
->getType()));
400 static llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16>
401 getExtParameterInfosForCall(const FunctionProtoType
*proto
,
402 unsigned prefixArgs
, unsigned totalArgs
) {
403 llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> result
;
404 if (proto
->hasExtParameterInfos()) {
405 addExtParameterInfosForCall(result
, proto
, prefixArgs
, totalArgs
);
410 /// Arrange a call to a C++ method, passing the given arguments.
412 /// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
414 /// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
416 /// PassProtoArgs indicates whether `args` has args for the parameters in the
417 /// given CXXConstructorDecl.
418 const CGFunctionInfo
&
419 CodeGenTypes::arrangeCXXConstructorCall(const CallArgList
&args
,
420 const CXXConstructorDecl
*D
,
421 CXXCtorType CtorKind
,
422 unsigned ExtraPrefixArgs
,
423 unsigned ExtraSuffixArgs
,
424 bool PassProtoArgs
) {
426 SmallVector
<CanQualType
, 16> ArgTypes
;
427 for (const auto &Arg
: args
)
428 ArgTypes
.push_back(Context
.getCanonicalParamType(Arg
.Ty
));
430 // +1 for implicit this, which should always be args[0].
431 unsigned TotalPrefixArgs
= 1 + ExtraPrefixArgs
;
433 CanQual
<FunctionProtoType
> FPT
= GetFormalType(D
);
434 RequiredArgs Required
= PassProtoArgs
435 ? RequiredArgs::forPrototypePlus(
436 FPT
, TotalPrefixArgs
+ ExtraSuffixArgs
)
439 GlobalDecl
GD(D
, CtorKind
);
440 CanQualType ResultType
= TheCXXABI
.HasThisReturn(GD
)
442 : TheCXXABI
.hasMostDerivedReturn(GD
)
443 ? CGM
.getContext().VoidPtrTy
446 FunctionType::ExtInfo Info
= FPT
->getExtInfo();
447 llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> ParamInfos
;
448 // If the prototype args are elided, we should only have ABI-specific args,
449 // which never have param info.
450 if (PassProtoArgs
&& FPT
->hasExtParameterInfos()) {
451 // ABI-specific suffix arguments are treated the same as variadic arguments.
452 addExtParameterInfosForCall(ParamInfos
, FPT
.getTypePtr(), TotalPrefixArgs
,
456 return arrangeLLVMFunctionInfo(ResultType
, FnInfoOpts::IsInstanceMethod
,
457 ArgTypes
, Info
, ParamInfos
, Required
);
460 /// Arrange the argument and result information for the declaration or
461 /// definition of the given function.
462 const CGFunctionInfo
&
463 CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl
*FD
) {
464 if (const CXXMethodDecl
*MD
= dyn_cast
<CXXMethodDecl
>(FD
))
465 if (MD
->isImplicitObjectMemberFunction())
466 return arrangeCXXMethodDeclaration(MD
);
468 CanQualType FTy
= FD
->getType()->getCanonicalTypeUnqualified();
470 assert(isa
<FunctionType
>(FTy
));
471 setCUDAKernelCallingConvention(FTy
, CGM
, FD
);
473 // When declaring a function without a prototype, always use a
474 // non-variadic type.
475 if (CanQual
<FunctionNoProtoType
> noProto
= FTy
.getAs
<FunctionNoProtoType
>()) {
476 return arrangeLLVMFunctionInfo(noProto
->getReturnType(), FnInfoOpts::None
,
477 std::nullopt
, noProto
->getExtInfo(), {},
481 return arrangeFreeFunctionType(FTy
.castAs
<FunctionProtoType
>());
484 /// Arrange the argument and result information for the declaration or
485 /// definition of an Objective-C method.
486 const CGFunctionInfo
&
487 CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl
*MD
) {
488 // It happens that this is the same as a call with no optional
489 // arguments, except also using the formal 'self' type.
490 return arrangeObjCMessageSendSignature(MD
, MD
->getSelfDecl()->getType());
493 /// Arrange the argument and result information for the function type
494 /// through which to perform a send to the given Objective-C method,
495 /// using the given receiver type. The receiver type is not always
496 /// the 'self' type of the method or even an Objective-C pointer type.
497 /// This is *not* the right method for actually performing such a
498 /// message send, due to the possibility of optional arguments.
499 const CGFunctionInfo
&
500 CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl
*MD
,
501 QualType receiverType
) {
502 SmallVector
<CanQualType
, 16> argTys
;
503 SmallVector
<FunctionProtoType::ExtParameterInfo
, 4> extParamInfos(
504 MD
->isDirectMethod() ? 1 : 2);
505 argTys
.push_back(Context
.getCanonicalParamType(receiverType
));
506 if (!MD
->isDirectMethod())
507 argTys
.push_back(Context
.getCanonicalParamType(Context
.getObjCSelType()));
509 for (const auto *I
: MD
->parameters()) {
510 argTys
.push_back(Context
.getCanonicalParamType(I
->getType()));
511 auto extParamInfo
= FunctionProtoType::ExtParameterInfo().withIsNoEscape(
512 I
->hasAttr
<NoEscapeAttr
>());
513 extParamInfos
.push_back(extParamInfo
);
516 FunctionType::ExtInfo einfo
;
517 bool IsWindows
= getContext().getTargetInfo().getTriple().isOSWindows();
518 einfo
= einfo
.withCallingConv(getCallingConventionForDecl(MD
, IsWindows
));
520 if (getContext().getLangOpts().ObjCAutoRefCount
&&
521 MD
->hasAttr
<NSReturnsRetainedAttr
>())
522 einfo
= einfo
.withProducesResult(true);
524 RequiredArgs required
=
525 (MD
->isVariadic() ? RequiredArgs(argTys
.size()) : RequiredArgs::All
);
527 return arrangeLLVMFunctionInfo(GetReturnType(MD
->getReturnType()),
528 FnInfoOpts::None
, argTys
, einfo
, extParamInfos
,
532 const CGFunctionInfo
&
533 CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType
,
534 const CallArgList
&args
) {
535 auto argTypes
= getArgTypesForCall(Context
, args
);
536 FunctionType::ExtInfo einfo
;
538 return arrangeLLVMFunctionInfo(GetReturnType(returnType
), FnInfoOpts::None
,
539 argTypes
, einfo
, {}, RequiredArgs::All
);
542 const CGFunctionInfo
&
543 CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD
) {
544 // FIXME: Do we need to handle ObjCMethodDecl?
545 const FunctionDecl
*FD
= cast
<FunctionDecl
>(GD
.getDecl());
547 if (isa
<CXXConstructorDecl
>(GD
.getDecl()) ||
548 isa
<CXXDestructorDecl
>(GD
.getDecl()))
549 return arrangeCXXStructorDeclaration(GD
);
551 return arrangeFunctionDeclaration(FD
);
554 /// Arrange a thunk that takes 'this' as the first parameter followed by
555 /// varargs. Return a void pointer, regardless of the actual return type.
556 /// The body of the thunk will end in a musttail call to a function of the
557 /// correct type, and the caller will bitcast the function to the correct
559 const CGFunctionInfo
&
560 CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl
*MD
) {
561 assert(MD
->isVirtual() && "only methods have thunks");
562 CanQual
<FunctionProtoType
> FTP
= GetFormalType(MD
);
563 CanQualType ArgTys
[] = {DeriveThisType(MD
->getParent(), MD
)};
564 return arrangeLLVMFunctionInfo(Context
.VoidTy
, FnInfoOpts::None
, ArgTys
,
565 FTP
->getExtInfo(), {}, RequiredArgs(1));
568 const CGFunctionInfo
&
569 CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl
*CD
,
571 assert(CT
== Ctor_CopyingClosure
|| CT
== Ctor_DefaultClosure
);
573 CanQual
<FunctionProtoType
> FTP
= GetFormalType(CD
);
574 SmallVector
<CanQualType
, 2> ArgTys
;
575 const CXXRecordDecl
*RD
= CD
->getParent();
576 ArgTys
.push_back(DeriveThisType(RD
, CD
));
577 if (CT
== Ctor_CopyingClosure
)
578 ArgTys
.push_back(*FTP
->param_type_begin());
579 if (RD
->getNumVBases() > 0)
580 ArgTys
.push_back(Context
.IntTy
);
581 CallingConv CC
= Context
.getDefaultCallingConvention(
582 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
583 return arrangeLLVMFunctionInfo(Context
.VoidTy
, FnInfoOpts::IsInstanceMethod
,
584 ArgTys
, FunctionType::ExtInfo(CC
), {},
588 /// Arrange a call as unto a free function, except possibly with an
589 /// additional number of formal parameters considered required.
590 static const CGFunctionInfo
&
591 arrangeFreeFunctionLikeCall(CodeGenTypes
&CGT
,
593 const CallArgList
&args
,
594 const FunctionType
*fnType
,
595 unsigned numExtraRequiredArgs
,
597 assert(args
.size() >= numExtraRequiredArgs
);
599 llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
601 // In most cases, there are no optional arguments.
602 RequiredArgs required
= RequiredArgs::All
;
604 // If we have a variadic prototype, the required arguments are the
605 // extra prefix plus the arguments in the prototype.
606 if (const FunctionProtoType
*proto
= dyn_cast
<FunctionProtoType
>(fnType
)) {
607 if (proto
->isVariadic())
608 required
= RequiredArgs::forPrototypePlus(proto
, numExtraRequiredArgs
);
610 if (proto
->hasExtParameterInfos())
611 addExtParameterInfosForCall(paramInfos
, proto
, numExtraRequiredArgs
,
614 // If we don't have a prototype at all, but we're supposed to
615 // explicitly use the variadic convention for unprototyped calls,
616 // treat all of the arguments as required but preserve the nominal
617 // possibility of variadics.
618 } else if (CGM
.getTargetCodeGenInfo()
619 .isNoProtoCallVariadic(args
,
620 cast
<FunctionNoProtoType
>(fnType
))) {
621 required
= RequiredArgs(args
.size());
625 SmallVector
<CanQualType
, 16> argTypes
;
626 for (const auto &arg
: args
)
627 argTypes
.push_back(CGT
.getContext().getCanonicalParamType(arg
.Ty
));
628 FnInfoOpts opts
= chainCall
? FnInfoOpts::IsChainCall
: FnInfoOpts::None
;
629 return CGT
.arrangeLLVMFunctionInfo(GetReturnType(fnType
->getReturnType()),
630 opts
, argTypes
, fnType
->getExtInfo(),
631 paramInfos
, required
);
634 /// Figure out the rules for calling a function with the given formal
635 /// type using the given arguments. The arguments are necessary
636 /// because the function might be unprototyped, in which case it's
637 /// target-dependent in crazy ways.
638 const CGFunctionInfo
&
639 CodeGenTypes::arrangeFreeFunctionCall(const CallArgList
&args
,
640 const FunctionType
*fnType
,
642 return arrangeFreeFunctionLikeCall(*this, CGM
, args
, fnType
,
643 chainCall
? 1 : 0, chainCall
);
646 /// A block function is essentially a free function with an
647 /// extra implicit argument.
648 const CGFunctionInfo
&
649 CodeGenTypes::arrangeBlockFunctionCall(const CallArgList
&args
,
650 const FunctionType
*fnType
) {
651 return arrangeFreeFunctionLikeCall(*this, CGM
, args
, fnType
, 1,
652 /*chainCall=*/false);
655 const CGFunctionInfo
&
656 CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType
*proto
,
657 const FunctionArgList
¶ms
) {
658 auto paramInfos
= getExtParameterInfosForCall(proto
, 1, params
.size());
659 auto argTypes
= getArgTypesForDeclaration(Context
, params
);
661 return arrangeLLVMFunctionInfo(GetReturnType(proto
->getReturnType()),
662 FnInfoOpts::None
, argTypes
,
663 proto
->getExtInfo(), paramInfos
,
664 RequiredArgs::forPrototypePlus(proto
, 1));
667 const CGFunctionInfo
&
668 CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType
,
669 const CallArgList
&args
) {
671 SmallVector
<CanQualType
, 16> argTypes
;
672 for (const auto &Arg
: args
)
673 argTypes
.push_back(Context
.getCanonicalParamType(Arg
.Ty
));
674 return arrangeLLVMFunctionInfo(GetReturnType(resultType
), FnInfoOpts::None
,
675 argTypes
, FunctionType::ExtInfo(),
676 /*paramInfos=*/{}, RequiredArgs::All
);
679 const CGFunctionInfo
&
680 CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType
,
681 const FunctionArgList
&args
) {
682 auto argTypes
= getArgTypesForDeclaration(Context
, args
);
684 return arrangeLLVMFunctionInfo(GetReturnType(resultType
), FnInfoOpts::None
,
685 argTypes
, FunctionType::ExtInfo(), {},
689 const CGFunctionInfo
&
690 CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType
,
691 ArrayRef
<CanQualType
> argTypes
) {
692 return arrangeLLVMFunctionInfo(resultType
, FnInfoOpts::None
, argTypes
,
693 FunctionType::ExtInfo(), {},
697 /// Arrange a call to a C++ method, passing the given arguments.
699 /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
700 /// does not count `this`.
701 const CGFunctionInfo
&
702 CodeGenTypes::arrangeCXXMethodCall(const CallArgList
&args
,
703 const FunctionProtoType
*proto
,
704 RequiredArgs required
,
705 unsigned numPrefixArgs
) {
706 assert(numPrefixArgs
+ 1 <= args
.size() &&
707 "Emitting a call with less args than the required prefix?");
708 // Add one to account for `this`. It's a bit awkward here, but we don't count
709 // `this` in similar places elsewhere.
711 getExtParameterInfosForCall(proto
, numPrefixArgs
+ 1, args
.size());
714 auto argTypes
= getArgTypesForCall(Context
, args
);
716 FunctionType::ExtInfo info
= proto
->getExtInfo();
717 return arrangeLLVMFunctionInfo(GetReturnType(proto
->getReturnType()),
718 FnInfoOpts::IsInstanceMethod
, argTypes
, info
,
719 paramInfos
, required
);
722 const CGFunctionInfo
&CodeGenTypes::arrangeNullaryFunction() {
723 return arrangeLLVMFunctionInfo(getContext().VoidTy
, FnInfoOpts::None
,
724 std::nullopt
, FunctionType::ExtInfo(), {},
728 const CGFunctionInfo
&
729 CodeGenTypes::arrangeCall(const CGFunctionInfo
&signature
,
730 const CallArgList
&args
) {
731 assert(signature
.arg_size() <= args
.size());
732 if (signature
.arg_size() == args
.size())
735 SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
736 auto sigParamInfos
= signature
.getExtParameterInfos();
737 if (!sigParamInfos
.empty()) {
738 paramInfos
.append(sigParamInfos
.begin(), sigParamInfos
.end());
739 paramInfos
.resize(args
.size());
742 auto argTypes
= getArgTypesForCall(Context
, args
);
744 assert(signature
.getRequiredArgs().allowsOptionalArgs());
745 FnInfoOpts opts
= FnInfoOpts::None
;
746 if (signature
.isInstanceMethod())
747 opts
|= FnInfoOpts::IsInstanceMethod
;
748 if (signature
.isChainCall())
749 opts
|= FnInfoOpts::IsChainCall
;
750 if (signature
.isDelegateCall())
751 opts
|= FnInfoOpts::IsDelegateCall
;
752 return arrangeLLVMFunctionInfo(signature
.getReturnType(), opts
, argTypes
,
753 signature
.getExtInfo(), paramInfos
,
754 signature
.getRequiredArgs());
759 void computeSPIRKernelABIInfo(CodeGenModule
&CGM
, CGFunctionInfo
&FI
);
763 /// Arrange the argument and result information for an abstract value
764 /// of a given function type. This is the method which all of the
765 /// above functions ultimately defer to.
766 const CGFunctionInfo
&CodeGenTypes::arrangeLLVMFunctionInfo(
767 CanQualType resultType
, FnInfoOpts opts
, ArrayRef
<CanQualType
> argTypes
,
768 FunctionType::ExtInfo info
,
769 ArrayRef
<FunctionProtoType::ExtParameterInfo
> paramInfos
,
770 RequiredArgs required
) {
771 assert(llvm::all_of(argTypes
,
772 [](CanQualType T
) { return T
.isCanonicalAsParam(); }));
774 // Lookup or create unique function info.
775 llvm::FoldingSetNodeID ID
;
776 bool isInstanceMethod
=
777 (opts
& FnInfoOpts::IsInstanceMethod
) == FnInfoOpts::IsInstanceMethod
;
779 (opts
& FnInfoOpts::IsChainCall
) == FnInfoOpts::IsChainCall
;
780 bool isDelegateCall
=
781 (opts
& FnInfoOpts::IsDelegateCall
) == FnInfoOpts::IsDelegateCall
;
782 CGFunctionInfo::Profile(ID
, isInstanceMethod
, isChainCall
, isDelegateCall
,
783 info
, paramInfos
, required
, resultType
, argTypes
);
785 void *insertPos
= nullptr;
786 CGFunctionInfo
*FI
= FunctionInfos
.FindNodeOrInsertPos(ID
, insertPos
);
790 unsigned CC
= ClangCallConvToLLVMCallConv(info
.getCC());
792 // Construct the function info. We co-allocate the ArgInfos.
793 FI
= CGFunctionInfo::create(CC
, isInstanceMethod
, isChainCall
, isDelegateCall
,
794 info
, paramInfos
, resultType
, argTypes
, required
);
795 FunctionInfos
.InsertNode(FI
, insertPos
);
797 bool inserted
= FunctionsBeingProcessed
.insert(FI
).second
;
799 assert(inserted
&& "Recursively being processed?");
801 // Compute ABI information.
802 if (CC
== llvm::CallingConv::SPIR_KERNEL
) {
803 // Force target independent argument handling for the host visible
805 computeSPIRKernelABIInfo(CGM
, *FI
);
806 } else if (info
.getCC() == CC_Swift
|| info
.getCC() == CC_SwiftAsync
) {
807 swiftcall::computeABIInfo(CGM
, *FI
);
809 getABIInfo().computeInfo(*FI
);
812 // Loop over all of the computed argument and return value info. If any of
813 // them are direct or extend without a specified coerce type, specify the
815 ABIArgInfo
&retInfo
= FI
->getReturnInfo();
816 if (retInfo
.canHaveCoerceToType() && retInfo
.getCoerceToType() == nullptr)
817 retInfo
.setCoerceToType(ConvertType(FI
->getReturnType()));
819 for (auto &I
: FI
->arguments())
820 if (I
.info
.canHaveCoerceToType() && I
.info
.getCoerceToType() == nullptr)
821 I
.info
.setCoerceToType(ConvertType(I
.type
));
823 bool erased
= FunctionsBeingProcessed
.erase(FI
); (void)erased
;
824 assert(erased
&& "Not in set?");
829 CGFunctionInfo
*CGFunctionInfo::create(unsigned llvmCC
, bool instanceMethod
,
830 bool chainCall
, bool delegateCall
,
831 const FunctionType::ExtInfo
&info
,
832 ArrayRef
<ExtParameterInfo
> paramInfos
,
833 CanQualType resultType
,
834 ArrayRef
<CanQualType
> argTypes
,
835 RequiredArgs required
) {
836 assert(paramInfos
.empty() || paramInfos
.size() == argTypes
.size());
837 assert(!required
.allowsOptionalArgs() ||
838 required
.getNumRequiredArgs() <= argTypes
.size());
841 operator new(totalSizeToAlloc
<ArgInfo
, ExtParameterInfo
>(
842 argTypes
.size() + 1, paramInfos
.size()));
844 CGFunctionInfo
*FI
= new(buffer
) CGFunctionInfo();
845 FI
->CallingConvention
= llvmCC
;
846 FI
->EffectiveCallingConvention
= llvmCC
;
847 FI
->ASTCallingConvention
= info
.getCC();
848 FI
->InstanceMethod
= instanceMethod
;
849 FI
->ChainCall
= chainCall
;
850 FI
->DelegateCall
= delegateCall
;
851 FI
->CmseNSCall
= info
.getCmseNSCall();
852 FI
->NoReturn
= info
.getNoReturn();
853 FI
->ReturnsRetained
= info
.getProducesResult();
854 FI
->NoCallerSavedRegs
= info
.getNoCallerSavedRegs();
855 FI
->NoCfCheck
= info
.getNoCfCheck();
856 FI
->Required
= required
;
857 FI
->HasRegParm
= info
.getHasRegParm();
858 FI
->RegParm
= info
.getRegParm();
859 FI
->ArgStruct
= nullptr;
860 FI
->ArgStructAlign
= 0;
861 FI
->NumArgs
= argTypes
.size();
862 FI
->HasExtParameterInfos
= !paramInfos
.empty();
863 FI
->getArgsBuffer()[0].type
= resultType
;
864 FI
->MaxVectorWidth
= 0;
865 for (unsigned i
= 0, e
= argTypes
.size(); i
!= e
; ++i
)
866 FI
->getArgsBuffer()[i
+ 1].type
= argTypes
[i
];
867 for (unsigned i
= 0, e
= paramInfos
.size(); i
!= e
; ++i
)
868 FI
->getExtParameterInfosBuffer()[i
] = paramInfos
[i
];
875 // ABIArgInfo::Expand implementation.
877 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
878 struct TypeExpansion
{
879 enum TypeExpansionKind
{
880 // Elements of constant arrays are expanded recursively.
882 // Record fields are expanded recursively (but if record is a union, only
883 // the field with the largest size is expanded).
885 // For complex types, real and imaginary parts are expanded recursively.
887 // All other types are not expandable.
891 const TypeExpansionKind Kind
;
893 TypeExpansion(TypeExpansionKind K
) : Kind(K
) {}
894 virtual ~TypeExpansion() {}
897 struct ConstantArrayExpansion
: TypeExpansion
{
901 ConstantArrayExpansion(QualType EltTy
, uint64_t NumElts
)
902 : TypeExpansion(TEK_ConstantArray
), EltTy(EltTy
), NumElts(NumElts
) {}
903 static bool classof(const TypeExpansion
*TE
) {
904 return TE
->Kind
== TEK_ConstantArray
;
908 struct RecordExpansion
: TypeExpansion
{
909 SmallVector
<const CXXBaseSpecifier
*, 1> Bases
;
911 SmallVector
<const FieldDecl
*, 1> Fields
;
913 RecordExpansion(SmallVector
<const CXXBaseSpecifier
*, 1> &&Bases
,
914 SmallVector
<const FieldDecl
*, 1> &&Fields
)
915 : TypeExpansion(TEK_Record
), Bases(std::move(Bases
)),
916 Fields(std::move(Fields
)) {}
917 static bool classof(const TypeExpansion
*TE
) {
918 return TE
->Kind
== TEK_Record
;
922 struct ComplexExpansion
: TypeExpansion
{
925 ComplexExpansion(QualType EltTy
) : TypeExpansion(TEK_Complex
), EltTy(EltTy
) {}
926 static bool classof(const TypeExpansion
*TE
) {
927 return TE
->Kind
== TEK_Complex
;
931 struct NoExpansion
: TypeExpansion
{
932 NoExpansion() : TypeExpansion(TEK_None
) {}
933 static bool classof(const TypeExpansion
*TE
) {
934 return TE
->Kind
== TEK_None
;
939 static std::unique_ptr
<TypeExpansion
>
940 getTypeExpansion(QualType Ty
, const ASTContext
&Context
) {
941 if (const ConstantArrayType
*AT
= Context
.getAsConstantArrayType(Ty
)) {
942 return std::make_unique
<ConstantArrayExpansion
>(AT
->getElementType(),
945 if (const RecordType
*RT
= Ty
->getAs
<RecordType
>()) {
946 SmallVector
<const CXXBaseSpecifier
*, 1> Bases
;
947 SmallVector
<const FieldDecl
*, 1> Fields
;
948 const RecordDecl
*RD
= RT
->getDecl();
949 assert(!RD
->hasFlexibleArrayMember() &&
950 "Cannot expand structure with flexible array.");
952 // Unions can be here only in degenerative cases - all the fields are same
953 // after flattening. Thus we have to use the "largest" field.
954 const FieldDecl
*LargestFD
= nullptr;
955 CharUnits UnionSize
= CharUnits::Zero();
957 for (const auto *FD
: RD
->fields()) {
958 if (FD
->isZeroLengthBitField(Context
))
960 assert(!FD
->isBitField() &&
961 "Cannot expand structure with bit-field members.");
962 CharUnits FieldSize
= Context
.getTypeSizeInChars(FD
->getType());
963 if (UnionSize
< FieldSize
) {
964 UnionSize
= FieldSize
;
969 Fields
.push_back(LargestFD
);
971 if (const auto *CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
)) {
972 assert(!CXXRD
->isDynamicClass() &&
973 "cannot expand vtable pointers in dynamic classes");
974 llvm::append_range(Bases
, llvm::make_pointer_range(CXXRD
->bases()));
977 for (const auto *FD
: RD
->fields()) {
978 if (FD
->isZeroLengthBitField(Context
))
980 assert(!FD
->isBitField() &&
981 "Cannot expand structure with bit-field members.");
982 Fields
.push_back(FD
);
985 return std::make_unique
<RecordExpansion
>(std::move(Bases
),
988 if (const ComplexType
*CT
= Ty
->getAs
<ComplexType
>()) {
989 return std::make_unique
<ComplexExpansion
>(CT
->getElementType());
991 return std::make_unique
<NoExpansion
>();
994 static int getExpansionSize(QualType Ty
, const ASTContext
&Context
) {
995 auto Exp
= getTypeExpansion(Ty
, Context
);
996 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
997 return CAExp
->NumElts
* getExpansionSize(CAExp
->EltTy
, Context
);
999 if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1001 for (auto BS
: RExp
->Bases
)
1002 Res
+= getExpansionSize(BS
->getType(), Context
);
1003 for (auto FD
: RExp
->Fields
)
1004 Res
+= getExpansionSize(FD
->getType(), Context
);
1007 if (isa
<ComplexExpansion
>(Exp
.get()))
1009 assert(isa
<NoExpansion
>(Exp
.get()));
1014 CodeGenTypes::getExpandedTypes(QualType Ty
,
1015 SmallVectorImpl
<llvm::Type
*>::iterator
&TI
) {
1016 auto Exp
= getTypeExpansion(Ty
, Context
);
1017 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1018 for (int i
= 0, n
= CAExp
->NumElts
; i
< n
; i
++) {
1019 getExpandedTypes(CAExp
->EltTy
, TI
);
1021 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1022 for (auto BS
: RExp
->Bases
)
1023 getExpandedTypes(BS
->getType(), TI
);
1024 for (auto FD
: RExp
->Fields
)
1025 getExpandedTypes(FD
->getType(), TI
);
1026 } else if (auto CExp
= dyn_cast
<ComplexExpansion
>(Exp
.get())) {
1027 llvm::Type
*EltTy
= ConvertType(CExp
->EltTy
);
1031 assert(isa
<NoExpansion
>(Exp
.get()));
1032 *TI
++ = ConvertType(Ty
);
1036 static void forConstantArrayExpansion(CodeGenFunction
&CGF
,
1037 ConstantArrayExpansion
*CAE
,
1039 llvm::function_ref
<void(Address
)> Fn
) {
1040 for (int i
= 0, n
= CAE
->NumElts
; i
< n
; i
++) {
1041 Address EltAddr
= CGF
.Builder
.CreateConstGEP2_32(BaseAddr
, 0, i
);
1046 void CodeGenFunction::ExpandTypeFromArgs(QualType Ty
, LValue LV
,
1047 llvm::Function::arg_iterator
&AI
) {
1048 assert(LV
.isSimple() &&
1049 "Unexpected non-simple lvalue during struct expansion.");
1051 auto Exp
= getTypeExpansion(Ty
, getContext());
1052 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1053 forConstantArrayExpansion(
1054 *this, CAExp
, LV
.getAddress(*this), [&](Address EltAddr
) {
1055 LValue LV
= MakeAddrLValue(EltAddr
, CAExp
->EltTy
);
1056 ExpandTypeFromArgs(CAExp
->EltTy
, LV
, AI
);
1058 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1059 Address This
= LV
.getAddress(*this);
1060 for (const CXXBaseSpecifier
*BS
: RExp
->Bases
) {
1061 // Perform a single step derived-to-base conversion.
1063 GetAddressOfBaseClass(This
, Ty
->getAsCXXRecordDecl(), &BS
, &BS
+ 1,
1064 /*NullCheckValue=*/false, SourceLocation());
1065 LValue SubLV
= MakeAddrLValue(Base
, BS
->getType());
1067 // Recurse onto bases.
1068 ExpandTypeFromArgs(BS
->getType(), SubLV
, AI
);
1070 for (auto FD
: RExp
->Fields
) {
1071 // FIXME: What are the right qualifiers here?
1072 LValue SubLV
= EmitLValueForFieldInitialization(LV
, FD
);
1073 ExpandTypeFromArgs(FD
->getType(), SubLV
, AI
);
1075 } else if (isa
<ComplexExpansion
>(Exp
.get())) {
1076 auto realValue
= &*AI
++;
1077 auto imagValue
= &*AI
++;
1078 EmitStoreOfComplex(ComplexPairTy(realValue
, imagValue
), LV
, /*init*/ true);
1080 // Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
1082 assert(isa
<NoExpansion
>(Exp
.get()));
1083 llvm::Value
*Arg
= &*AI
++;
1084 if (LV
.isBitField()) {
1085 EmitStoreThroughLValue(RValue::get(Arg
), LV
);
1087 // TODO: currently there are some places are inconsistent in what LLVM
1088 // pointer type they use (see D118744). Once clang uses opaque pointers
1089 // all LLVM pointer types will be the same and we can remove this check.
1090 if (Arg
->getType()->isPointerTy()) {
1091 Address Addr
= LV
.getAddress(*this);
1092 Arg
= Builder
.CreateBitCast(Arg
, Addr
.getElementType());
1094 EmitStoreOfScalar(Arg
, LV
);
1099 void CodeGenFunction::ExpandTypeToArgs(
1100 QualType Ty
, CallArg Arg
, llvm::FunctionType
*IRFuncTy
,
1101 SmallVectorImpl
<llvm::Value
*> &IRCallArgs
, unsigned &IRCallArgPos
) {
1102 auto Exp
= getTypeExpansion(Ty
, getContext());
1103 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1104 Address Addr
= Arg
.hasLValue() ? Arg
.getKnownLValue().getAddress(*this)
1105 : Arg
.getKnownRValue().getAggregateAddress();
1106 forConstantArrayExpansion(
1107 *this, CAExp
, Addr
, [&](Address EltAddr
) {
1108 CallArg EltArg
= CallArg(
1109 convertTempToRValue(EltAddr
, CAExp
->EltTy
, SourceLocation()),
1111 ExpandTypeToArgs(CAExp
->EltTy
, EltArg
, IRFuncTy
, IRCallArgs
,
1114 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1115 Address This
= Arg
.hasLValue() ? Arg
.getKnownLValue().getAddress(*this)
1116 : Arg
.getKnownRValue().getAggregateAddress();
1117 for (const CXXBaseSpecifier
*BS
: RExp
->Bases
) {
1118 // Perform a single step derived-to-base conversion.
1120 GetAddressOfBaseClass(This
, Ty
->getAsCXXRecordDecl(), &BS
, &BS
+ 1,
1121 /*NullCheckValue=*/false, SourceLocation());
1122 CallArg BaseArg
= CallArg(RValue::getAggregate(Base
), BS
->getType());
1124 // Recurse onto bases.
1125 ExpandTypeToArgs(BS
->getType(), BaseArg
, IRFuncTy
, IRCallArgs
,
1129 LValue LV
= MakeAddrLValue(This
, Ty
);
1130 for (auto FD
: RExp
->Fields
) {
1132 CallArg(EmitRValueForField(LV
, FD
, SourceLocation()), FD
->getType());
1133 ExpandTypeToArgs(FD
->getType(), FldArg
, IRFuncTy
, IRCallArgs
,
1136 } else if (isa
<ComplexExpansion
>(Exp
.get())) {
1137 ComplexPairTy CV
= Arg
.getKnownRValue().getComplexVal();
1138 IRCallArgs
[IRCallArgPos
++] = CV
.first
;
1139 IRCallArgs
[IRCallArgPos
++] = CV
.second
;
1141 assert(isa
<NoExpansion
>(Exp
.get()));
1142 auto RV
= Arg
.getKnownRValue();
1143 assert(RV
.isScalar() &&
1144 "Unexpected non-scalar rvalue during struct expansion.");
1146 // Insert a bitcast as needed.
1147 llvm::Value
*V
= RV
.getScalarVal();
1148 if (IRCallArgPos
< IRFuncTy
->getNumParams() &&
1149 V
->getType() != IRFuncTy
->getParamType(IRCallArgPos
))
1150 V
= Builder
.CreateBitCast(V
, IRFuncTy
->getParamType(IRCallArgPos
));
1152 IRCallArgs
[IRCallArgPos
++] = V
;
1156 /// Create a temporary allocation for the purposes of coercion.
1157 static RawAddress
CreateTempAllocaForCoercion(CodeGenFunction
&CGF
,
1160 const Twine
&Name
= "tmp") {
1161 // Don't use an alignment that's worse than what LLVM would prefer.
1162 auto PrefAlign
= CGF
.CGM
.getDataLayout().getPrefTypeAlign(Ty
);
1163 CharUnits Align
= std::max(MinAlign
, CharUnits::fromQuantity(PrefAlign
));
1165 return CGF
.CreateTempAlloca(Ty
, Align
, Name
+ ".coerce");
1168 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
1169 /// accessing some number of bytes out of it, try to gep into the struct to get
1170 /// at its inner goodness. Dive as deep as possible without entering an element
1171 /// with an in-memory size smaller than DstSize.
1173 EnterStructPointerForCoercedAccess(Address SrcPtr
,
1174 llvm::StructType
*SrcSTy
,
1175 uint64_t DstSize
, CodeGenFunction
&CGF
) {
1176 // We can't dive into a zero-element struct.
1177 if (SrcSTy
->getNumElements() == 0) return SrcPtr
;
1179 llvm::Type
*FirstElt
= SrcSTy
->getElementType(0);
1181 // If the first elt is at least as large as what we're looking for, or if the
1182 // first element is the same size as the whole struct, we can enter it. The
1183 // comparison must be made on the store size and not the alloca size. Using
1184 // the alloca size may overstate the size of the load.
1185 uint64_t FirstEltSize
=
1186 CGF
.CGM
.getDataLayout().getTypeStoreSize(FirstElt
);
1187 if (FirstEltSize
< DstSize
&&
1188 FirstEltSize
< CGF
.CGM
.getDataLayout().getTypeStoreSize(SrcSTy
))
1191 // GEP into the first element.
1192 SrcPtr
= CGF
.Builder
.CreateStructGEP(SrcPtr
, 0, "coerce.dive");
1194 // If the first element is a struct, recurse.
1195 llvm::Type
*SrcTy
= SrcPtr
.getElementType();
1196 if (llvm::StructType
*SrcSTy
= dyn_cast
<llvm::StructType
>(SrcTy
))
1197 return EnterStructPointerForCoercedAccess(SrcPtr
, SrcSTy
, DstSize
, CGF
);
1202 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
1203 /// are either integers or pointers. This does a truncation of the value if it
1204 /// is too large or a zero extension if it is too small.
1206 /// This behaves as if the value were coerced through memory, so on big-endian
1207 /// targets the high bits are preserved in a truncation, while little-endian
1208 /// targets preserve the low bits.
1209 static llvm::Value
*CoerceIntOrPtrToIntOrPtr(llvm::Value
*Val
,
1211 CodeGenFunction
&CGF
) {
1212 if (Val
->getType() == Ty
)
1215 if (isa
<llvm::PointerType
>(Val
->getType())) {
1216 // If this is Pointer->Pointer avoid conversion to and from int.
1217 if (isa
<llvm::PointerType
>(Ty
))
1218 return CGF
.Builder
.CreateBitCast(Val
, Ty
, "coerce.val");
1220 // Convert the pointer to an integer so we can play with its width.
1221 Val
= CGF
.Builder
.CreatePtrToInt(Val
, CGF
.IntPtrTy
, "coerce.val.pi");
1224 llvm::Type
*DestIntTy
= Ty
;
1225 if (isa
<llvm::PointerType
>(DestIntTy
))
1226 DestIntTy
= CGF
.IntPtrTy
;
1228 if (Val
->getType() != DestIntTy
) {
1229 const llvm::DataLayout
&DL
= CGF
.CGM
.getDataLayout();
1230 if (DL
.isBigEndian()) {
1231 // Preserve the high bits on big-endian targets.
1232 // That is what memory coercion does.
1233 uint64_t SrcSize
= DL
.getTypeSizeInBits(Val
->getType());
1234 uint64_t DstSize
= DL
.getTypeSizeInBits(DestIntTy
);
1236 if (SrcSize
> DstSize
) {
1237 Val
= CGF
.Builder
.CreateLShr(Val
, SrcSize
- DstSize
, "coerce.highbits");
1238 Val
= CGF
.Builder
.CreateTrunc(Val
, DestIntTy
, "coerce.val.ii");
1240 Val
= CGF
.Builder
.CreateZExt(Val
, DestIntTy
, "coerce.val.ii");
1241 Val
= CGF
.Builder
.CreateShl(Val
, DstSize
- SrcSize
, "coerce.highbits");
1244 // Little-endian targets preserve the low bits. No shifts required.
1245 Val
= CGF
.Builder
.CreateIntCast(Val
, DestIntTy
, false, "coerce.val.ii");
1249 if (isa
<llvm::PointerType
>(Ty
))
1250 Val
= CGF
.Builder
.CreateIntToPtr(Val
, Ty
, "coerce.val.ip");
1256 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1257 /// a pointer to an object of type \arg Ty, known to be aligned to
1258 /// \arg SrcAlign bytes.
1260 /// This safely handles the case when the src type is smaller than the
1261 /// destination type; in this situation the values of bits which not
1262 /// present in the src are undefined.
1263 static llvm::Value
*CreateCoercedLoad(Address Src
, llvm::Type
*Ty
,
1264 CodeGenFunction
&CGF
) {
1265 llvm::Type
*SrcTy
= Src
.getElementType();
1267 // If SrcTy and Ty are the same, just do a load.
1269 return CGF
.Builder
.CreateLoad(Src
);
1271 llvm::TypeSize DstSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(Ty
);
1273 if (llvm::StructType
*SrcSTy
= dyn_cast
<llvm::StructType
>(SrcTy
)) {
1274 Src
= EnterStructPointerForCoercedAccess(Src
, SrcSTy
,
1275 DstSize
.getFixedValue(), CGF
);
1276 SrcTy
= Src
.getElementType();
1279 llvm::TypeSize SrcSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
1281 // If the source and destination are integer or pointer types, just do an
1282 // extension or truncation to the desired type.
1283 if ((isa
<llvm::IntegerType
>(Ty
) || isa
<llvm::PointerType
>(Ty
)) &&
1284 (isa
<llvm::IntegerType
>(SrcTy
) || isa
<llvm::PointerType
>(SrcTy
))) {
1285 llvm::Value
*Load
= CGF
.Builder
.CreateLoad(Src
);
1286 return CoerceIntOrPtrToIntOrPtr(Load
, Ty
, CGF
);
1289 // If load is legal, just bitcast the src pointer.
1290 if (!SrcSize
.isScalable() && !DstSize
.isScalable() &&
1291 SrcSize
.getFixedValue() >= DstSize
.getFixedValue()) {
1292 // Generally SrcSize is never greater than DstSize, since this means we are
1293 // losing bits. However, this can happen in cases where the structure has
1294 // additional padding, for example due to a user specified alignment.
1296 // FIXME: Assert that we aren't truncating non-padding bits when have access
1297 // to that information.
1298 Src
= Src
.withElementType(Ty
);
1299 return CGF
.Builder
.CreateLoad(Src
);
1302 // If coercing a fixed vector to a scalable vector for ABI compatibility, and
1303 // the types match, use the llvm.vector.insert intrinsic to perform the
1305 if (auto *ScalableDstTy
= dyn_cast
<llvm::ScalableVectorType
>(Ty
)) {
1306 if (auto *FixedSrcTy
= dyn_cast
<llvm::FixedVectorType
>(SrcTy
)) {
1307 // If we are casting a fixed i8 vector to a scalable i1 predicate
1308 // vector, use a vector insert and bitcast the result.
1309 if (ScalableDstTy
->getElementType()->isIntegerTy(1) &&
1310 ScalableDstTy
->getElementCount().isKnownMultipleOf(8) &&
1311 FixedSrcTy
->getElementType()->isIntegerTy(8)) {
1312 ScalableDstTy
= llvm::ScalableVectorType::get(
1313 FixedSrcTy
->getElementType(),
1314 ScalableDstTy
->getElementCount().getKnownMinValue() / 8);
1316 if (ScalableDstTy
->getElementType() == FixedSrcTy
->getElementType()) {
1317 auto *Load
= CGF
.Builder
.CreateLoad(Src
);
1318 auto *UndefVec
= llvm::UndefValue::get(ScalableDstTy
);
1319 auto *Zero
= llvm::Constant::getNullValue(CGF
.CGM
.Int64Ty
);
1320 llvm::Value
*Result
= CGF
.Builder
.CreateInsertVector(
1321 ScalableDstTy
, UndefVec
, Load
, Zero
, "cast.scalable");
1322 if (ScalableDstTy
!= Ty
)
1323 Result
= CGF
.Builder
.CreateBitCast(Result
, Ty
);
1329 // Otherwise do coercion through memory. This is stupid, but simple.
1331 CreateTempAllocaForCoercion(CGF
, Ty
, Src
.getAlignment(), Src
.getName());
1332 CGF
.Builder
.CreateMemCpy(
1333 Tmp
.getPointer(), Tmp
.getAlignment().getAsAlign(),
1334 Src
.emitRawPointer(CGF
), Src
.getAlignment().getAsAlign(),
1335 llvm::ConstantInt::get(CGF
.IntPtrTy
, SrcSize
.getKnownMinValue()));
1336 return CGF
.Builder
.CreateLoad(Tmp
);
1339 // Function to store a first-class aggregate into memory. We prefer to
1340 // store the elements rather than the aggregate to be more friendly to
1342 // FIXME: Do we need to recurse here?
1343 void CodeGenFunction::EmitAggregateStore(llvm::Value
*Val
, Address Dest
,
1344 bool DestIsVolatile
) {
1345 // Prefer scalar stores to first-class aggregate stores.
1346 if (llvm::StructType
*STy
= dyn_cast
<llvm::StructType
>(Val
->getType())) {
1347 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
1348 Address EltPtr
= Builder
.CreateStructGEP(Dest
, i
);
1349 llvm::Value
*Elt
= Builder
.CreateExtractValue(Val
, i
);
1350 Builder
.CreateStore(Elt
, EltPtr
, DestIsVolatile
);
1353 Builder
.CreateStore(Val
, Dest
, DestIsVolatile
);
1357 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1358 /// where the source and destination may have different types. The
1359 /// destination is known to be aligned to \arg DstAlign bytes.
1361 /// This safely handles the case when the src type is larger than the
1362 /// destination type; the upper bits of the src will be lost.
1363 static void CreateCoercedStore(llvm::Value
*Src
,
1366 CodeGenFunction
&CGF
) {
1367 llvm::Type
*SrcTy
= Src
->getType();
1368 llvm::Type
*DstTy
= Dst
.getElementType();
1369 if (SrcTy
== DstTy
) {
1370 CGF
.Builder
.CreateStore(Src
, Dst
, DstIsVolatile
);
1374 llvm::TypeSize SrcSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
1376 if (llvm::StructType
*DstSTy
= dyn_cast
<llvm::StructType
>(DstTy
)) {
1377 Dst
= EnterStructPointerForCoercedAccess(Dst
, DstSTy
,
1378 SrcSize
.getFixedValue(), CGF
);
1379 DstTy
= Dst
.getElementType();
1382 llvm::PointerType
*SrcPtrTy
= llvm::dyn_cast
<llvm::PointerType
>(SrcTy
);
1383 llvm::PointerType
*DstPtrTy
= llvm::dyn_cast
<llvm::PointerType
>(DstTy
);
1384 if (SrcPtrTy
&& DstPtrTy
&&
1385 SrcPtrTy
->getAddressSpace() != DstPtrTy
->getAddressSpace()) {
1386 Src
= CGF
.Builder
.CreateAddrSpaceCast(Src
, DstTy
);
1387 CGF
.Builder
.CreateStore(Src
, Dst
, DstIsVolatile
);
1391 // If the source and destination are integer or pointer types, just do an
1392 // extension or truncation to the desired type.
1393 if ((isa
<llvm::IntegerType
>(SrcTy
) || isa
<llvm::PointerType
>(SrcTy
)) &&
1394 (isa
<llvm::IntegerType
>(DstTy
) || isa
<llvm::PointerType
>(DstTy
))) {
1395 Src
= CoerceIntOrPtrToIntOrPtr(Src
, DstTy
, CGF
);
1396 CGF
.Builder
.CreateStore(Src
, Dst
, DstIsVolatile
);
1400 llvm::TypeSize DstSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(DstTy
);
1402 // If store is legal, just bitcast the src pointer.
1403 if (isa
<llvm::ScalableVectorType
>(SrcTy
) ||
1404 isa
<llvm::ScalableVectorType
>(DstTy
) ||
1405 SrcSize
.getFixedValue() <= DstSize
.getFixedValue()) {
1406 Dst
= Dst
.withElementType(SrcTy
);
1407 CGF
.EmitAggregateStore(Src
, Dst
, DstIsVolatile
);
1409 // Otherwise do coercion through memory. This is stupid, but
1412 // Generally SrcSize is never greater than DstSize, since this means we are
1413 // losing bits. However, this can happen in cases where the structure has
1414 // additional padding, for example due to a user specified alignment.
1416 // FIXME: Assert that we aren't truncating non-padding bits when have access
1417 // to that information.
1419 CreateTempAllocaForCoercion(CGF
, SrcTy
, Dst
.getAlignment());
1420 CGF
.Builder
.CreateStore(Src
, Tmp
);
1421 CGF
.Builder
.CreateMemCpy(
1422 Dst
.emitRawPointer(CGF
), Dst
.getAlignment().getAsAlign(),
1423 Tmp
.getPointer(), Tmp
.getAlignment().getAsAlign(),
1424 llvm::ConstantInt::get(CGF
.IntPtrTy
, DstSize
.getFixedValue()));
1428 static Address
emitAddressAtOffset(CodeGenFunction
&CGF
, Address addr
,
1429 const ABIArgInfo
&info
) {
1430 if (unsigned offset
= info
.getDirectOffset()) {
1431 addr
= addr
.withElementType(CGF
.Int8Ty
);
1432 addr
= CGF
.Builder
.CreateConstInBoundsByteGEP(addr
,
1433 CharUnits::fromQuantity(offset
));
1434 addr
= addr
.withElementType(info
.getCoerceToType());
1441 /// Encapsulates information about the way function arguments from
1442 /// CGFunctionInfo should be passed to actual LLVM IR function.
1443 class ClangToLLVMArgMapping
{
1444 static const unsigned InvalidIndex
= ~0U;
1445 unsigned InallocaArgNo
;
1447 unsigned TotalIRArgs
;
1449 /// Arguments of LLVM IR function corresponding to single Clang argument.
1451 unsigned PaddingArgIndex
;
1452 // Argument is expanded to IR arguments at positions
1453 // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
1454 unsigned FirstArgIndex
;
1455 unsigned NumberOfArgs
;
1458 : PaddingArgIndex(InvalidIndex
), FirstArgIndex(InvalidIndex
),
1462 SmallVector
<IRArgs
, 8> ArgInfo
;
1465 ClangToLLVMArgMapping(const ASTContext
&Context
, const CGFunctionInfo
&FI
,
1466 bool OnlyRequiredArgs
= false)
1467 : InallocaArgNo(InvalidIndex
), SRetArgNo(InvalidIndex
), TotalIRArgs(0),
1468 ArgInfo(OnlyRequiredArgs
? FI
.getNumRequiredArgs() : FI
.arg_size()) {
1469 construct(Context
, FI
, OnlyRequiredArgs
);
1472 bool hasInallocaArg() const { return InallocaArgNo
!= InvalidIndex
; }
1473 unsigned getInallocaArgNo() const {
1474 assert(hasInallocaArg());
1475 return InallocaArgNo
;
1478 bool hasSRetArg() const { return SRetArgNo
!= InvalidIndex
; }
1479 unsigned getSRetArgNo() const {
1480 assert(hasSRetArg());
1484 unsigned totalIRArgs() const { return TotalIRArgs
; }
1486 bool hasPaddingArg(unsigned ArgNo
) const {
1487 assert(ArgNo
< ArgInfo
.size());
1488 return ArgInfo
[ArgNo
].PaddingArgIndex
!= InvalidIndex
;
1490 unsigned getPaddingArgNo(unsigned ArgNo
) const {
1491 assert(hasPaddingArg(ArgNo
));
1492 return ArgInfo
[ArgNo
].PaddingArgIndex
;
1495 /// Returns index of first IR argument corresponding to ArgNo, and their
1497 std::pair
<unsigned, unsigned> getIRArgs(unsigned ArgNo
) const {
1498 assert(ArgNo
< ArgInfo
.size());
1499 return std::make_pair(ArgInfo
[ArgNo
].FirstArgIndex
,
1500 ArgInfo
[ArgNo
].NumberOfArgs
);
1504 void construct(const ASTContext
&Context
, const CGFunctionInfo
&FI
,
1505 bool OnlyRequiredArgs
);
1508 void ClangToLLVMArgMapping::construct(const ASTContext
&Context
,
1509 const CGFunctionInfo
&FI
,
1510 bool OnlyRequiredArgs
) {
1511 unsigned IRArgNo
= 0;
1512 bool SwapThisWithSRet
= false;
1513 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
1515 if (RetAI
.getKind() == ABIArgInfo::Indirect
) {
1516 SwapThisWithSRet
= RetAI
.isSRetAfterThis();
1517 SRetArgNo
= SwapThisWithSRet
? 1 : IRArgNo
++;
1521 unsigned NumArgs
= OnlyRequiredArgs
? FI
.getNumRequiredArgs() : FI
.arg_size();
1522 for (CGFunctionInfo::const_arg_iterator I
= FI
.arg_begin(); ArgNo
< NumArgs
;
1524 assert(I
!= FI
.arg_end());
1525 QualType ArgType
= I
->type
;
1526 const ABIArgInfo
&AI
= I
->info
;
1527 // Collect data about IR arguments corresponding to Clang argument ArgNo.
1528 auto &IRArgs
= ArgInfo
[ArgNo
];
1530 if (AI
.getPaddingType())
1531 IRArgs
.PaddingArgIndex
= IRArgNo
++;
1533 switch (AI
.getKind()) {
1534 case ABIArgInfo::Extend
:
1535 case ABIArgInfo::Direct
: {
1536 // FIXME: handle sseregparm someday...
1537 llvm::StructType
*STy
= dyn_cast
<llvm::StructType
>(AI
.getCoerceToType());
1538 if (AI
.isDirect() && AI
.getCanBeFlattened() && STy
) {
1539 IRArgs
.NumberOfArgs
= STy
->getNumElements();
1541 IRArgs
.NumberOfArgs
= 1;
1545 case ABIArgInfo::Indirect
:
1546 case ABIArgInfo::IndirectAliased
:
1547 IRArgs
.NumberOfArgs
= 1;
1549 case ABIArgInfo::Ignore
:
1550 case ABIArgInfo::InAlloca
:
1551 // ignore and inalloca doesn't have matching LLVM parameters.
1552 IRArgs
.NumberOfArgs
= 0;
1554 case ABIArgInfo::CoerceAndExpand
:
1555 IRArgs
.NumberOfArgs
= AI
.getCoerceAndExpandTypeSequence().size();
1557 case ABIArgInfo::Expand
:
1558 IRArgs
.NumberOfArgs
= getExpansionSize(ArgType
, Context
);
1562 if (IRArgs
.NumberOfArgs
> 0) {
1563 IRArgs
.FirstArgIndex
= IRArgNo
;
1564 IRArgNo
+= IRArgs
.NumberOfArgs
;
1567 // Skip over the sret parameter when it comes second. We already handled it
1569 if (IRArgNo
== 1 && SwapThisWithSRet
)
1572 assert(ArgNo
== ArgInfo
.size());
1574 if (FI
.usesInAlloca())
1575 InallocaArgNo
= IRArgNo
++;
1577 TotalIRArgs
= IRArgNo
;
1583 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo
&FI
) {
1584 const auto &RI
= FI
.getReturnInfo();
1585 return RI
.isIndirect() || (RI
.isInAlloca() && RI
.getInAllocaSRet());
1588 bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo
&FI
) {
1589 return ReturnTypeUsesSRet(FI
) &&
1590 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1593 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType
) {
1594 if (const BuiltinType
*BT
= ResultType
->getAs
<BuiltinType
>()) {
1595 switch (BT
->getKind()) {
1598 case BuiltinType::Float
:
1599 return getTarget().useObjCFPRetForRealType(FloatModeKind::Float
);
1600 case BuiltinType::Double
:
1601 return getTarget().useObjCFPRetForRealType(FloatModeKind::Double
);
1602 case BuiltinType::LongDouble
:
1603 return getTarget().useObjCFPRetForRealType(FloatModeKind::LongDouble
);
1610 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType
) {
1611 if (const ComplexType
*CT
= ResultType
->getAs
<ComplexType
>()) {
1612 if (const BuiltinType
*BT
= CT
->getElementType()->getAs
<BuiltinType
>()) {
1613 if (BT
->getKind() == BuiltinType::LongDouble
)
1614 return getTarget().useObjCFP2RetForComplexLongDouble();
1621 llvm::FunctionType
*CodeGenTypes::GetFunctionType(GlobalDecl GD
) {
1622 const CGFunctionInfo
&FI
= arrangeGlobalDeclaration(GD
);
1623 return GetFunctionType(FI
);
1626 llvm::FunctionType
*
1627 CodeGenTypes::GetFunctionType(const CGFunctionInfo
&FI
) {
1629 bool Inserted
= FunctionsBeingProcessed
.insert(&FI
).second
;
1631 assert(Inserted
&& "Recursively being processed?");
1633 llvm::Type
*resultType
= nullptr;
1634 const ABIArgInfo
&retAI
= FI
.getReturnInfo();
1635 switch (retAI
.getKind()) {
1636 case ABIArgInfo::Expand
:
1637 case ABIArgInfo::IndirectAliased
:
1638 llvm_unreachable("Invalid ABI kind for return argument");
1640 case ABIArgInfo::Extend
:
1641 case ABIArgInfo::Direct
:
1642 resultType
= retAI
.getCoerceToType();
1645 case ABIArgInfo::InAlloca
:
1646 if (retAI
.getInAllocaSRet()) {
1647 // sret things on win32 aren't void, they return the sret pointer.
1648 QualType ret
= FI
.getReturnType();
1649 unsigned addressSpace
= CGM
.getTypes().getTargetAddressSpace(ret
);
1650 resultType
= llvm::PointerType::get(getLLVMContext(), addressSpace
);
1652 resultType
= llvm::Type::getVoidTy(getLLVMContext());
1656 case ABIArgInfo::Indirect
:
1657 case ABIArgInfo::Ignore
:
1658 resultType
= llvm::Type::getVoidTy(getLLVMContext());
1661 case ABIArgInfo::CoerceAndExpand
:
1662 resultType
= retAI
.getUnpaddedCoerceAndExpandType();
1666 ClangToLLVMArgMapping
IRFunctionArgs(getContext(), FI
, true);
1667 SmallVector
<llvm::Type
*, 8> ArgTypes(IRFunctionArgs
.totalIRArgs());
1669 // Add type for sret argument.
1670 if (IRFunctionArgs
.hasSRetArg()) {
1671 QualType Ret
= FI
.getReturnType();
1672 unsigned AddressSpace
= CGM
.getTypes().getTargetAddressSpace(Ret
);
1673 ArgTypes
[IRFunctionArgs
.getSRetArgNo()] =
1674 llvm::PointerType::get(getLLVMContext(), AddressSpace
);
1677 // Add type for inalloca argument.
1678 if (IRFunctionArgs
.hasInallocaArg())
1679 ArgTypes
[IRFunctionArgs
.getInallocaArgNo()] =
1680 llvm::PointerType::getUnqual(getLLVMContext());
1682 // Add in all of the required arguments.
1684 CGFunctionInfo::const_arg_iterator it
= FI
.arg_begin(),
1685 ie
= it
+ FI
.getNumRequiredArgs();
1686 for (; it
!= ie
; ++it
, ++ArgNo
) {
1687 const ABIArgInfo
&ArgInfo
= it
->info
;
1689 // Insert a padding type to ensure proper alignment.
1690 if (IRFunctionArgs
.hasPaddingArg(ArgNo
))
1691 ArgTypes
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
1692 ArgInfo
.getPaddingType();
1694 unsigned FirstIRArg
, NumIRArgs
;
1695 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
1697 switch (ArgInfo
.getKind()) {
1698 case ABIArgInfo::Ignore
:
1699 case ABIArgInfo::InAlloca
:
1700 assert(NumIRArgs
== 0);
1703 case ABIArgInfo::Indirect
:
1704 assert(NumIRArgs
== 1);
1705 // indirect arguments are always on the stack, which is alloca addr space.
1706 ArgTypes
[FirstIRArg
] = llvm::PointerType::get(
1707 getLLVMContext(), CGM
.getDataLayout().getAllocaAddrSpace());
1709 case ABIArgInfo::IndirectAliased
:
1710 assert(NumIRArgs
== 1);
1711 ArgTypes
[FirstIRArg
] = llvm::PointerType::get(
1712 getLLVMContext(), ArgInfo
.getIndirectAddrSpace());
1714 case ABIArgInfo::Extend
:
1715 case ABIArgInfo::Direct
: {
1716 // Fast-isel and the optimizer generally like scalar values better than
1717 // FCAs, so we flatten them if this is safe to do for this argument.
1718 llvm::Type
*argType
= ArgInfo
.getCoerceToType();
1719 llvm::StructType
*st
= dyn_cast
<llvm::StructType
>(argType
);
1720 if (st
&& ArgInfo
.isDirect() && ArgInfo
.getCanBeFlattened()) {
1721 assert(NumIRArgs
== st
->getNumElements());
1722 for (unsigned i
= 0, e
= st
->getNumElements(); i
!= e
; ++i
)
1723 ArgTypes
[FirstIRArg
+ i
] = st
->getElementType(i
);
1725 assert(NumIRArgs
== 1);
1726 ArgTypes
[FirstIRArg
] = argType
;
1731 case ABIArgInfo::CoerceAndExpand
: {
1732 auto ArgTypesIter
= ArgTypes
.begin() + FirstIRArg
;
1733 for (auto *EltTy
: ArgInfo
.getCoerceAndExpandTypeSequence()) {
1734 *ArgTypesIter
++ = EltTy
;
1736 assert(ArgTypesIter
== ArgTypes
.begin() + FirstIRArg
+ NumIRArgs
);
1740 case ABIArgInfo::Expand
:
1741 auto ArgTypesIter
= ArgTypes
.begin() + FirstIRArg
;
1742 getExpandedTypes(it
->type
, ArgTypesIter
);
1743 assert(ArgTypesIter
== ArgTypes
.begin() + FirstIRArg
+ NumIRArgs
);
1748 bool Erased
= FunctionsBeingProcessed
.erase(&FI
); (void)Erased
;
1749 assert(Erased
&& "Not in set?");
1751 return llvm::FunctionType::get(resultType
, ArgTypes
, FI
.isVariadic());
1754 llvm::Type
*CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD
) {
1755 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(GD
.getDecl());
1756 const FunctionProtoType
*FPT
= MD
->getType()->castAs
<FunctionProtoType
>();
1758 if (!isFuncTypeConvertible(FPT
))
1759 return llvm::StructType::get(getLLVMContext());
1761 return GetFunctionType(GD
);
1764 static void AddAttributesFromFunctionProtoType(ASTContext
&Ctx
,
1765 llvm::AttrBuilder
&FuncAttrs
,
1766 const FunctionProtoType
*FPT
) {
1770 if (!isUnresolvedExceptionSpec(FPT
->getExceptionSpecType()) &&
1772 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
1774 unsigned SMEBits
= FPT
->getAArch64SMEAttributes();
1775 if (SMEBits
& FunctionType::SME_PStateSMEnabledMask
)
1776 FuncAttrs
.addAttribute("aarch64_pstate_sm_enabled");
1777 if (SMEBits
& FunctionType::SME_PStateSMCompatibleMask
)
1778 FuncAttrs
.addAttribute("aarch64_pstate_sm_compatible");
1781 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_Preserves
)
1782 FuncAttrs
.addAttribute("aarch64_preserves_za");
1783 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_In
)
1784 FuncAttrs
.addAttribute("aarch64_in_za");
1785 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_Out
)
1786 FuncAttrs
.addAttribute("aarch64_out_za");
1787 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_InOut
)
1788 FuncAttrs
.addAttribute("aarch64_inout_za");
1791 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_Preserves
)
1792 FuncAttrs
.addAttribute("aarch64_preserves_zt0");
1793 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_In
)
1794 FuncAttrs
.addAttribute("aarch64_in_zt0");
1795 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_Out
)
1796 FuncAttrs
.addAttribute("aarch64_out_zt0");
1797 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_InOut
)
1798 FuncAttrs
.addAttribute("aarch64_inout_zt0");
1801 static void AddAttributesFromOMPAssumes(llvm::AttrBuilder
&FuncAttrs
,
1802 const Decl
*Callee
) {
1806 SmallVector
<StringRef
, 4> Attrs
;
1808 for (const OMPAssumeAttr
*AA
: Callee
->specific_attrs
<OMPAssumeAttr
>())
1809 AA
->getAssumption().split(Attrs
, ",");
1812 FuncAttrs
.addAttribute(llvm::AssumptionAttrKey
,
1813 llvm::join(Attrs
.begin(), Attrs
.end(), ","));
1816 bool CodeGenModule::MayDropFunctionReturn(const ASTContext
&Context
,
1817 QualType ReturnType
) const {
1818 // We can't just discard the return value for a record type with a
1819 // complex destructor or a non-trivially copyable type.
1820 if (const RecordType
*RT
=
1821 ReturnType
.getCanonicalType()->getAs
<RecordType
>()) {
1822 if (const auto *ClassDecl
= dyn_cast
<CXXRecordDecl
>(RT
->getDecl()))
1823 return ClassDecl
->hasTrivialDestructor();
1825 return ReturnType
.isTriviallyCopyableType(Context
);
1828 static bool HasStrictReturn(const CodeGenModule
&Module
, QualType RetTy
,
1829 const Decl
*TargetDecl
) {
1830 // As-is msan can not tolerate noundef mismatch between caller and
1831 // implementation. Mismatch is possible for e.g. indirect calls from C-caller
1832 // into C++. Such mismatches lead to confusing false reports. To avoid
1833 // expensive workaround on msan we enforce initialization event in uncommon
1834 // cases where it's allowed.
1835 if (Module
.getLangOpts().Sanitize
.has(SanitizerKind::Memory
))
1837 // C++ explicitly makes returning undefined values UB. C's rule only applies
1838 // to used values, so we never mark them noundef for now.
1839 if (!Module
.getLangOpts().CPlusPlus
)
1842 if (const FunctionDecl
*FDecl
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
1843 if (FDecl
->isExternC())
1845 } else if (const VarDecl
*VDecl
= dyn_cast
<VarDecl
>(TargetDecl
)) {
1846 // Function pointer.
1847 if (VDecl
->isExternC())
1852 // We don't want to be too aggressive with the return checking, unless
1853 // it's explicit in the code opts or we're using an appropriate sanitizer.
1854 // Try to respect what the programmer intended.
1855 return Module
.getCodeGenOpts().StrictReturn
||
1856 !Module
.MayDropFunctionReturn(Module
.getContext(), RetTy
) ||
1857 Module
.getLangOpts().Sanitize
.has(SanitizerKind::Return
);
1860 /// Add denormal-fp-math and denormal-fp-math-f32 as appropriate for the
1861 /// requested denormal behavior, accounting for the overriding behavior of the
1863 static void addDenormalModeAttrs(llvm::DenormalMode FPDenormalMode
,
1864 llvm::DenormalMode FP32DenormalMode
,
1865 llvm::AttrBuilder
&FuncAttrs
) {
1866 if (FPDenormalMode
!= llvm::DenormalMode::getDefault())
1867 FuncAttrs
.addAttribute("denormal-fp-math", FPDenormalMode
.str());
1869 if (FP32DenormalMode
!= FPDenormalMode
&& FP32DenormalMode
.isValid())
1870 FuncAttrs
.addAttribute("denormal-fp-math-f32", FP32DenormalMode
.str());
1873 /// Add default attributes to a function, which have merge semantics under
1874 /// -mlink-builtin-bitcode and should not simply overwrite any existing
1875 /// attributes in the linked library.
1877 addMergableDefaultFunctionAttributes(const CodeGenOptions
&CodeGenOpts
,
1878 llvm::AttrBuilder
&FuncAttrs
) {
1879 addDenormalModeAttrs(CodeGenOpts
.FPDenormalMode
, CodeGenOpts
.FP32DenormalMode
,
1883 static void getTrivialDefaultFunctionAttributes(
1884 StringRef Name
, bool HasOptnone
, const CodeGenOptions
&CodeGenOpts
,
1885 const LangOptions
&LangOpts
, bool AttrOnCallSite
,
1886 llvm::AttrBuilder
&FuncAttrs
) {
1887 // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1889 if (CodeGenOpts
.OptimizeSize
)
1890 FuncAttrs
.addAttribute(llvm::Attribute::OptimizeForSize
);
1891 if (CodeGenOpts
.OptimizeSize
== 2)
1892 FuncAttrs
.addAttribute(llvm::Attribute::MinSize
);
1895 if (CodeGenOpts
.DisableRedZone
)
1896 FuncAttrs
.addAttribute(llvm::Attribute::NoRedZone
);
1897 if (CodeGenOpts
.IndirectTlsSegRefs
)
1898 FuncAttrs
.addAttribute("indirect-tls-seg-refs");
1899 if (CodeGenOpts
.NoImplicitFloat
)
1900 FuncAttrs
.addAttribute(llvm::Attribute::NoImplicitFloat
);
1902 if (AttrOnCallSite
) {
1903 // Attributes that should go on the call site only.
1904 // FIXME: Look for 'BuiltinAttr' on the function rather than re-checking
1905 // the -fno-builtin-foo list.
1906 if (!CodeGenOpts
.SimplifyLibCalls
|| LangOpts
.isNoBuiltinFunc(Name
))
1907 FuncAttrs
.addAttribute(llvm::Attribute::NoBuiltin
);
1908 if (!CodeGenOpts
.TrapFuncName
.empty())
1909 FuncAttrs
.addAttribute("trap-func-name", CodeGenOpts
.TrapFuncName
);
1911 switch (CodeGenOpts
.getFramePointer()) {
1912 case CodeGenOptions::FramePointerKind::None
:
1913 // This is the default behavior.
1915 case CodeGenOptions::FramePointerKind::NonLeaf
:
1916 case CodeGenOptions::FramePointerKind::All
:
1917 FuncAttrs
.addAttribute("frame-pointer",
1918 CodeGenOptions::getFramePointerKindName(
1919 CodeGenOpts
.getFramePointer()));
1922 if (CodeGenOpts
.LessPreciseFPMAD
)
1923 FuncAttrs
.addAttribute("less-precise-fpmad", "true");
1925 if (CodeGenOpts
.NullPointerIsValid
)
1926 FuncAttrs
.addAttribute(llvm::Attribute::NullPointerIsValid
);
1928 if (LangOpts
.getDefaultExceptionMode() == LangOptions::FPE_Ignore
)
1929 FuncAttrs
.addAttribute("no-trapping-math", "true");
1931 // TODO: Are these all needed?
1932 // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1933 if (LangOpts
.NoHonorInfs
)
1934 FuncAttrs
.addAttribute("no-infs-fp-math", "true");
1935 if (LangOpts
.NoHonorNaNs
)
1936 FuncAttrs
.addAttribute("no-nans-fp-math", "true");
1937 if (LangOpts
.ApproxFunc
)
1938 FuncAttrs
.addAttribute("approx-func-fp-math", "true");
1939 if (LangOpts
.AllowFPReassoc
&& LangOpts
.AllowRecip
&&
1940 LangOpts
.NoSignedZero
&& LangOpts
.ApproxFunc
&&
1941 (LangOpts
.getDefaultFPContractMode() ==
1942 LangOptions::FPModeKind::FPM_Fast
||
1943 LangOpts
.getDefaultFPContractMode() ==
1944 LangOptions::FPModeKind::FPM_FastHonorPragmas
))
1945 FuncAttrs
.addAttribute("unsafe-fp-math", "true");
1946 if (CodeGenOpts
.SoftFloat
)
1947 FuncAttrs
.addAttribute("use-soft-float", "true");
1948 FuncAttrs
.addAttribute("stack-protector-buffer-size",
1949 llvm::utostr(CodeGenOpts
.SSPBufferSize
));
1950 if (LangOpts
.NoSignedZero
)
1951 FuncAttrs
.addAttribute("no-signed-zeros-fp-math", "true");
1953 // TODO: Reciprocal estimate codegen options should apply to instructions?
1954 const std::vector
<std::string
> &Recips
= CodeGenOpts
.Reciprocals
;
1955 if (!Recips
.empty())
1956 FuncAttrs
.addAttribute("reciprocal-estimates",
1957 llvm::join(Recips
, ","));
1959 if (!CodeGenOpts
.PreferVectorWidth
.empty() &&
1960 CodeGenOpts
.PreferVectorWidth
!= "none")
1961 FuncAttrs
.addAttribute("prefer-vector-width",
1962 CodeGenOpts
.PreferVectorWidth
);
1964 if (CodeGenOpts
.StackRealignment
)
1965 FuncAttrs
.addAttribute("stackrealign");
1966 if (CodeGenOpts
.Backchain
)
1967 FuncAttrs
.addAttribute("backchain");
1968 if (CodeGenOpts
.EnableSegmentedStacks
)
1969 FuncAttrs
.addAttribute("split-stack");
1971 if (CodeGenOpts
.SpeculativeLoadHardening
)
1972 FuncAttrs
.addAttribute(llvm::Attribute::SpeculativeLoadHardening
);
1974 // Add zero-call-used-regs attribute.
1975 switch (CodeGenOpts
.getZeroCallUsedRegs()) {
1976 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip
:
1977 FuncAttrs
.removeAttribute("zero-call-used-regs");
1979 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg
:
1980 FuncAttrs
.addAttribute("zero-call-used-regs", "used-gpr-arg");
1982 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR
:
1983 FuncAttrs
.addAttribute("zero-call-used-regs", "used-gpr");
1985 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg
:
1986 FuncAttrs
.addAttribute("zero-call-used-regs", "used-arg");
1988 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used
:
1989 FuncAttrs
.addAttribute("zero-call-used-regs", "used");
1991 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg
:
1992 FuncAttrs
.addAttribute("zero-call-used-regs", "all-gpr-arg");
1994 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR
:
1995 FuncAttrs
.addAttribute("zero-call-used-regs", "all-gpr");
1997 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg
:
1998 FuncAttrs
.addAttribute("zero-call-used-regs", "all-arg");
2000 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All
:
2001 FuncAttrs
.addAttribute("zero-call-used-regs", "all");
2006 if (LangOpts
.assumeFunctionsAreConvergent()) {
2007 // Conservatively, mark all functions and calls in CUDA and OpenCL as
2008 // convergent (meaning, they may call an intrinsically convergent op, such
2009 // as __syncthreads() / barrier(), and so can't have certain optimizations
2010 // applied around them). LLVM will remove this attribute where it safely
2012 FuncAttrs
.addAttribute(llvm::Attribute::Convergent
);
2015 // TODO: NoUnwind attribute should be added for other GPU modes HIP,
2016 // OpenMP offload. AFAIK, neither of them support exceptions in device code.
2017 if ((LangOpts
.CUDA
&& LangOpts
.CUDAIsDevice
) || LangOpts
.OpenCL
||
2018 LangOpts
.SYCLIsDevice
) {
2019 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2022 for (StringRef Attr
: CodeGenOpts
.DefaultFunctionAttrs
) {
2023 StringRef Var
, Value
;
2024 std::tie(Var
, Value
) = Attr
.split('=');
2025 FuncAttrs
.addAttribute(Var
, Value
);
2029 /// Merges `target-features` from \TargetOpts and \F, and sets the result in
2031 /// * features from \F are always kept
2032 /// * a feature from \TargetOpts is kept if itself and its opposite are absent
2035 overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder
&FuncAttr
,
2036 const llvm::Function
&F
,
2037 const TargetOptions
&TargetOpts
) {
2038 auto FFeatures
= F
.getFnAttribute("target-features");
2040 llvm::StringSet
<> MergedNames
;
2041 SmallVector
<StringRef
> MergedFeatures
;
2042 MergedFeatures
.reserve(TargetOpts
.Features
.size());
2044 auto AddUnmergedFeatures
= [&](auto &&FeatureRange
) {
2045 for (StringRef Feature
: FeatureRange
) {
2046 if (Feature
.empty())
2048 assert(Feature
[0] == '+' || Feature
[0] == '-');
2049 StringRef Name
= Feature
.drop_front(1);
2050 bool Merged
= !MergedNames
.insert(Name
).second
;
2052 MergedFeatures
.push_back(Feature
);
2056 if (FFeatures
.isValid())
2057 AddUnmergedFeatures(llvm::split(FFeatures
.getValueAsString(), ','));
2058 AddUnmergedFeatures(TargetOpts
.Features
);
2060 if (!MergedFeatures
.empty()) {
2061 llvm::sort(MergedFeatures
);
2062 FuncAttr
.addAttribute("target-features", llvm::join(MergedFeatures
, ","));
2066 void CodeGen::mergeDefaultFunctionDefinitionAttributes(
2067 llvm::Function
&F
, const CodeGenOptions
&CodeGenOpts
,
2068 const LangOptions
&LangOpts
, const TargetOptions
&TargetOpts
,
2069 bool WillInternalize
) {
2071 llvm::AttrBuilder
FuncAttrs(F
.getContext());
2072 // Here we only extract the options that are relevant compared to the version
2073 // from GetCPUAndFeaturesAttributes.
2074 if (!TargetOpts
.CPU
.empty())
2075 FuncAttrs
.addAttribute("target-cpu", TargetOpts
.CPU
);
2076 if (!TargetOpts
.TuneCPU
.empty())
2077 FuncAttrs
.addAttribute("tune-cpu", TargetOpts
.TuneCPU
);
2079 ::getTrivialDefaultFunctionAttributes(F
.getName(), F
.hasOptNone(),
2080 CodeGenOpts
, LangOpts
,
2081 /*AttrOnCallSite=*/false, FuncAttrs
);
2083 if (!WillInternalize
&& F
.isInterposable()) {
2084 // Do not promote "dynamic" denormal-fp-math to this translation unit's
2085 // setting for weak functions that won't be internalized. The user has no
2086 // real control for how builtin bitcode is linked, so we shouldn't assume
2087 // later copies will use a consistent mode.
2088 F
.addFnAttrs(FuncAttrs
);
2092 llvm::AttributeMask AttrsToRemove
;
2094 llvm::DenormalMode DenormModeToMerge
= F
.getDenormalModeRaw();
2095 llvm::DenormalMode DenormModeToMergeF32
= F
.getDenormalModeF32Raw();
2096 llvm::DenormalMode Merged
=
2097 CodeGenOpts
.FPDenormalMode
.mergeCalleeMode(DenormModeToMerge
);
2098 llvm::DenormalMode MergedF32
= CodeGenOpts
.FP32DenormalMode
;
2100 if (DenormModeToMergeF32
.isValid()) {
2102 CodeGenOpts
.FP32DenormalMode
.mergeCalleeMode(DenormModeToMergeF32
);
2105 if (Merged
== llvm::DenormalMode::getDefault()) {
2106 AttrsToRemove
.addAttribute("denormal-fp-math");
2107 } else if (Merged
!= DenormModeToMerge
) {
2108 // Overwrite existing attribute
2109 FuncAttrs
.addAttribute("denormal-fp-math",
2110 CodeGenOpts
.FPDenormalMode
.str());
2113 if (MergedF32
== llvm::DenormalMode::getDefault()) {
2114 AttrsToRemove
.addAttribute("denormal-fp-math-f32");
2115 } else if (MergedF32
!= DenormModeToMergeF32
) {
2116 // Overwrite existing attribute
2117 FuncAttrs
.addAttribute("denormal-fp-math-f32",
2118 CodeGenOpts
.FP32DenormalMode
.str());
2121 F
.removeFnAttrs(AttrsToRemove
);
2122 addDenormalModeAttrs(Merged
, MergedF32
, FuncAttrs
);
2124 overrideFunctionFeaturesWithTargetFeatures(FuncAttrs
, F
, TargetOpts
);
2126 F
.addFnAttrs(FuncAttrs
);
2129 void CodeGenModule::getTrivialDefaultFunctionAttributes(
2130 StringRef Name
, bool HasOptnone
, bool AttrOnCallSite
,
2131 llvm::AttrBuilder
&FuncAttrs
) {
2132 ::getTrivialDefaultFunctionAttributes(Name
, HasOptnone
, getCodeGenOpts(),
2133 getLangOpts(), AttrOnCallSite
,
2137 void CodeGenModule::getDefaultFunctionAttributes(StringRef Name
,
2139 bool AttrOnCallSite
,
2140 llvm::AttrBuilder
&FuncAttrs
) {
2141 getTrivialDefaultFunctionAttributes(Name
, HasOptnone
, AttrOnCallSite
,
2143 // If we're just getting the default, get the default values for mergeable
2145 if (!AttrOnCallSite
)
2146 addMergableDefaultFunctionAttributes(CodeGenOpts
, FuncAttrs
);
2149 void CodeGenModule::addDefaultFunctionDefinitionAttributes(
2150 llvm::AttrBuilder
&attrs
) {
2151 getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,
2152 /*for call*/ false, attrs
);
2153 GetCPUAndFeaturesAttributes(GlobalDecl(), attrs
);
2156 static void addNoBuiltinAttributes(llvm::AttrBuilder
&FuncAttrs
,
2157 const LangOptions
&LangOpts
,
2158 const NoBuiltinAttr
*NBA
= nullptr) {
2159 auto AddNoBuiltinAttr
= [&FuncAttrs
](StringRef BuiltinName
) {
2160 SmallString
<32> AttributeName
;
2161 AttributeName
+= "no-builtin-";
2162 AttributeName
+= BuiltinName
;
2163 FuncAttrs
.addAttribute(AttributeName
);
2166 // First, handle the language options passed through -fno-builtin.
2167 if (LangOpts
.NoBuiltin
) {
2168 // -fno-builtin disables them all.
2169 FuncAttrs
.addAttribute("no-builtins");
2173 // Then, add attributes for builtins specified through -fno-builtin-<name>.
2174 llvm::for_each(LangOpts
.NoBuiltinFuncs
, AddNoBuiltinAttr
);
2176 // Now, let's check the __attribute__((no_builtin("...")) attribute added to
2181 // If there is a wildcard in the builtin names specified through the
2182 // attribute, disable them all.
2183 if (llvm::is_contained(NBA
->builtinNames(), "*")) {
2184 FuncAttrs
.addAttribute("no-builtins");
2188 // And last, add the rest of the builtin names.
2189 llvm::for_each(NBA
->builtinNames(), AddNoBuiltinAttr
);
2192 static bool DetermineNoUndef(QualType QTy
, CodeGenTypes
&Types
,
2193 const llvm::DataLayout
&DL
, const ABIArgInfo
&AI
,
2194 bool CheckCoerce
= true) {
2195 llvm::Type
*Ty
= Types
.ConvertTypeForMem(QTy
);
2196 if (AI
.getKind() == ABIArgInfo::Indirect
||
2197 AI
.getKind() == ABIArgInfo::IndirectAliased
)
2199 if (AI
.getKind() == ABIArgInfo::Extend
)
2201 if (!DL
.typeSizeEqualsStoreSize(Ty
))
2202 // TODO: This will result in a modest amount of values not marked noundef
2203 // when they could be. We care about values that *invisibly* contain undef
2204 // bits from the perspective of LLVM IR.
2206 if (CheckCoerce
&& AI
.canHaveCoerceToType()) {
2207 llvm::Type
*CoerceTy
= AI
.getCoerceToType();
2208 if (llvm::TypeSize::isKnownGT(DL
.getTypeSizeInBits(CoerceTy
),
2209 DL
.getTypeSizeInBits(Ty
)))
2210 // If we're coercing to a type with a greater size than the canonical one,
2211 // we're introducing new undef bits.
2212 // Coercing to a type of smaller or equal size is ok, as we know that
2213 // there's no internal padding (typeSizeEqualsStoreSize).
2216 if (QTy
->isBitIntType())
2218 if (QTy
->isReferenceType())
2220 if (QTy
->isNullPtrType())
2222 if (QTy
->isMemberPointerType())
2223 // TODO: Some member pointers are `noundef`, but it depends on the ABI. For
2224 // now, never mark them.
2226 if (QTy
->isScalarType()) {
2227 if (const ComplexType
*Complex
= dyn_cast
<ComplexType
>(QTy
))
2228 return DetermineNoUndef(Complex
->getElementType(), Types
, DL
, AI
, false);
2231 if (const VectorType
*Vector
= dyn_cast
<VectorType
>(QTy
))
2232 return DetermineNoUndef(Vector
->getElementType(), Types
, DL
, AI
, false);
2233 if (const MatrixType
*Matrix
= dyn_cast
<MatrixType
>(QTy
))
2234 return DetermineNoUndef(Matrix
->getElementType(), Types
, DL
, AI
, false);
2235 if (const ArrayType
*Array
= dyn_cast
<ArrayType
>(QTy
))
2236 return DetermineNoUndef(Array
->getElementType(), Types
, DL
, AI
, false);
2238 // TODO: Some structs may be `noundef`, in specific situations.
2242 /// Check if the argument of a function has maybe_undef attribute.
2243 static bool IsArgumentMaybeUndef(const Decl
*TargetDecl
,
2244 unsigned NumRequiredArgs
, unsigned ArgNo
) {
2245 const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
);
2249 // Assume variadic arguments do not have maybe_undef attribute.
2250 if (ArgNo
>= NumRequiredArgs
)
2253 // Check if argument has maybe_undef attribute.
2254 if (ArgNo
< FD
->getNumParams()) {
2255 const ParmVarDecl
*Param
= FD
->getParamDecl(ArgNo
);
2256 if (Param
&& Param
->hasAttr
<MaybeUndefAttr
>())
2263 /// Test if it's legal to apply nofpclass for the given parameter type and it's
2264 /// lowered IR type.
2265 static bool canApplyNoFPClass(const ABIArgInfo
&AI
, QualType ParamType
,
2267 // Should only apply to FP types in the source, not ABI promoted.
2268 if (!ParamType
->hasFloatingRepresentation())
2271 // The promoted-to IR type also needs to support nofpclass.
2272 llvm::Type
*IRTy
= AI
.getCoerceToType();
2273 if (llvm::AttributeFuncs::isNoFPClassCompatibleType(IRTy
))
2276 if (llvm::StructType
*ST
= dyn_cast
<llvm::StructType
>(IRTy
)) {
2277 return !IsReturn
&& AI
.getCanBeFlattened() &&
2278 llvm::all_of(ST
->elements(), [](llvm::Type
*Ty
) {
2279 return llvm::AttributeFuncs::isNoFPClassCompatibleType(Ty
);
2286 /// Return the nofpclass mask that can be applied to floating-point parameters.
2287 static llvm::FPClassTest
getNoFPClassTestMask(const LangOptions
&LangOpts
) {
2288 llvm::FPClassTest Mask
= llvm::fcNone
;
2289 if (LangOpts
.NoHonorInfs
)
2290 Mask
|= llvm::fcInf
;
2291 if (LangOpts
.NoHonorNaNs
)
2292 Mask
|= llvm::fcNan
;
2296 void CodeGenModule::AdjustMemoryAttribute(StringRef Name
,
2297 CGCalleeInfo CalleeInfo
,
2298 llvm::AttributeList
&Attrs
) {
2299 if (Attrs
.getMemoryEffects().getModRef() == llvm::ModRefInfo::NoModRef
) {
2300 Attrs
= Attrs
.removeFnAttribute(getLLVMContext(), llvm::Attribute::Memory
);
2301 llvm::Attribute MemoryAttr
= llvm::Attribute::getWithMemoryEffects(
2302 getLLVMContext(), llvm::MemoryEffects::writeOnly());
2303 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), MemoryAttr
);
2307 /// Construct the IR attribute list of a function or call.
2309 /// When adding an attribute, please consider where it should be handled:
2311 /// - getDefaultFunctionAttributes is for attributes that are essentially
2312 /// part of the global target configuration (but perhaps can be
2313 /// overridden on a per-function basis). Adding attributes there
2314 /// will cause them to also be set in frontends that build on Clang's
2315 /// target-configuration logic, as well as for code defined in library
2316 /// modules such as CUDA's libdevice.
2318 /// - ConstructAttributeList builds on top of getDefaultFunctionAttributes
2319 /// and adds declaration-specific, convention-specific, and
2320 /// frontend-specific logic. The last is of particular importance:
2321 /// attributes that restrict how the frontend generates code must be
2322 /// added here rather than getDefaultFunctionAttributes.
2324 void CodeGenModule::ConstructAttributeList(StringRef Name
,
2325 const CGFunctionInfo
&FI
,
2326 CGCalleeInfo CalleeInfo
,
2327 llvm::AttributeList
&AttrList
,
2328 unsigned &CallingConv
,
2329 bool AttrOnCallSite
, bool IsThunk
) {
2330 llvm::AttrBuilder
FuncAttrs(getLLVMContext());
2331 llvm::AttrBuilder
RetAttrs(getLLVMContext());
2333 // Collect function IR attributes from the CC lowering.
2334 // We'll collect the paramete and result attributes later.
2335 CallingConv
= FI
.getEffectiveCallingConvention();
2336 if (FI
.isNoReturn())
2337 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2338 if (FI
.isCmseNSCall())
2339 FuncAttrs
.addAttribute("cmse_nonsecure_call");
2341 // Collect function IR attributes from the callee prototype if we have one.
2342 AddAttributesFromFunctionProtoType(getContext(), FuncAttrs
,
2343 CalleeInfo
.getCalleeFunctionProtoType());
2345 const Decl
*TargetDecl
= CalleeInfo
.getCalleeDecl().getDecl();
2347 // Attach assumption attributes to the declaration. If this is a call
2348 // site, attach assumptions from the caller to the call as well.
2349 AddAttributesFromOMPAssumes(FuncAttrs
, TargetDecl
);
2351 bool HasOptnone
= false;
2352 // The NoBuiltinAttr attached to the target FunctionDecl.
2353 const NoBuiltinAttr
*NBA
= nullptr;
2355 // Some ABIs may result in additional accesses to arguments that may
2356 // otherwise not be present.
2357 auto AddPotentialArgAccess
= [&]() {
2358 llvm::Attribute A
= FuncAttrs
.getAttribute(llvm::Attribute::Memory
);
2360 FuncAttrs
.addMemoryAttr(A
.getMemoryEffects() |
2361 llvm::MemoryEffects::argMemOnly());
2364 // Collect function IR attributes based on declaration-specific
2366 // FIXME: handle sseregparm someday...
2368 if (TargetDecl
->hasAttr
<ReturnsTwiceAttr
>())
2369 FuncAttrs
.addAttribute(llvm::Attribute::ReturnsTwice
);
2370 if (TargetDecl
->hasAttr
<NoThrowAttr
>())
2371 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2372 if (TargetDecl
->hasAttr
<NoReturnAttr
>())
2373 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2374 if (TargetDecl
->hasAttr
<ColdAttr
>())
2375 FuncAttrs
.addAttribute(llvm::Attribute::Cold
);
2376 if (TargetDecl
->hasAttr
<HotAttr
>())
2377 FuncAttrs
.addAttribute(llvm::Attribute::Hot
);
2378 if (TargetDecl
->hasAttr
<NoDuplicateAttr
>())
2379 FuncAttrs
.addAttribute(llvm::Attribute::NoDuplicate
);
2380 if (TargetDecl
->hasAttr
<ConvergentAttr
>())
2381 FuncAttrs
.addAttribute(llvm::Attribute::Convergent
);
2383 if (const FunctionDecl
*Fn
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
2384 AddAttributesFromFunctionProtoType(
2385 getContext(), FuncAttrs
, Fn
->getType()->getAs
<FunctionProtoType
>());
2386 if (AttrOnCallSite
&& Fn
->isReplaceableGlobalAllocationFunction()) {
2387 // A sane operator new returns a non-aliasing pointer.
2388 auto Kind
= Fn
->getDeclName().getCXXOverloadedOperator();
2389 if (getCodeGenOpts().AssumeSaneOperatorNew
&&
2390 (Kind
== OO_New
|| Kind
== OO_Array_New
))
2391 RetAttrs
.addAttribute(llvm::Attribute::NoAlias
);
2393 const CXXMethodDecl
*MD
= dyn_cast
<CXXMethodDecl
>(Fn
);
2394 const bool IsVirtualCall
= MD
&& MD
->isVirtual();
2395 // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a
2396 // virtual function. These attributes are not inherited by overloads.
2397 if (!(AttrOnCallSite
&& IsVirtualCall
)) {
2398 if (Fn
->isNoReturn())
2399 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2400 NBA
= Fn
->getAttr
<NoBuiltinAttr
>();
2404 if (isa
<FunctionDecl
>(TargetDecl
) || isa
<VarDecl
>(TargetDecl
)) {
2405 // Only place nomerge attribute on call sites, never functions. This
2406 // allows it to work on indirect virtual function calls.
2407 if (AttrOnCallSite
&& TargetDecl
->hasAttr
<NoMergeAttr
>())
2408 FuncAttrs
.addAttribute(llvm::Attribute::NoMerge
);
2411 // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
2412 if (TargetDecl
->hasAttr
<ConstAttr
>()) {
2413 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::none());
2414 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2415 // gcc specifies that 'const' functions have greater restrictions than
2416 // 'pure' functions, so they also cannot have infinite loops.
2417 FuncAttrs
.addAttribute(llvm::Attribute::WillReturn
);
2418 } else if (TargetDecl
->hasAttr
<PureAttr
>()) {
2419 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::readOnly());
2420 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2421 // gcc specifies that 'pure' functions cannot have infinite loops.
2422 FuncAttrs
.addAttribute(llvm::Attribute::WillReturn
);
2423 } else if (TargetDecl
->hasAttr
<NoAliasAttr
>()) {
2424 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::inaccessibleOrArgMemOnly());
2425 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2427 if (TargetDecl
->hasAttr
<RestrictAttr
>())
2428 RetAttrs
.addAttribute(llvm::Attribute::NoAlias
);
2429 if (TargetDecl
->hasAttr
<ReturnsNonNullAttr
>() &&
2430 !CodeGenOpts
.NullPointerIsValid
)
2431 RetAttrs
.addAttribute(llvm::Attribute::NonNull
);
2432 if (TargetDecl
->hasAttr
<AnyX86NoCallerSavedRegistersAttr
>())
2433 FuncAttrs
.addAttribute("no_caller_saved_registers");
2434 if (TargetDecl
->hasAttr
<AnyX86NoCfCheckAttr
>())
2435 FuncAttrs
.addAttribute(llvm::Attribute::NoCfCheck
);
2436 if (TargetDecl
->hasAttr
<LeafAttr
>())
2437 FuncAttrs
.addAttribute(llvm::Attribute::NoCallback
);
2439 HasOptnone
= TargetDecl
->hasAttr
<OptimizeNoneAttr
>();
2440 if (auto *AllocSize
= TargetDecl
->getAttr
<AllocSizeAttr
>()) {
2441 std::optional
<unsigned> NumElemsParam
;
2442 if (AllocSize
->getNumElemsParam().isValid())
2443 NumElemsParam
= AllocSize
->getNumElemsParam().getLLVMIndex();
2444 FuncAttrs
.addAllocSizeAttr(AllocSize
->getElemSizeParam().getLLVMIndex(),
2448 if (TargetDecl
->hasAttr
<OpenCLKernelAttr
>()) {
2449 if (getLangOpts().OpenCLVersion
<= 120) {
2450 // OpenCL v1.2 Work groups are always uniform
2451 FuncAttrs
.addAttribute("uniform-work-group-size", "true");
2453 // OpenCL v2.0 Work groups may be whether uniform or not.
2454 // '-cl-uniform-work-group-size' compile option gets a hint
2455 // to the compiler that the global work-size be a multiple of
2456 // the work-group size specified to clEnqueueNDRangeKernel
2457 // (i.e. work groups are uniform).
2458 FuncAttrs
.addAttribute(
2459 "uniform-work-group-size",
2460 llvm::toStringRef(getLangOpts().OffloadUniformBlock
));
2464 if (TargetDecl
->hasAttr
<CUDAGlobalAttr
>() &&
2465 getLangOpts().OffloadUniformBlock
)
2466 FuncAttrs
.addAttribute("uniform-work-group-size", "true");
2468 if (TargetDecl
->hasAttr
<ArmLocallyStreamingAttr
>())
2469 FuncAttrs
.addAttribute("aarch64_pstate_sm_body");
2472 // Attach "no-builtins" attributes to:
2473 // * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".
2474 // * definitions: "no-builtins" or "no-builtin-<name>" only.
2475 // The attributes can come from:
2476 // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
2477 // * FunctionDecl attributes: __attribute__((no_builtin(...)))
2478 addNoBuiltinAttributes(FuncAttrs
, getLangOpts(), NBA
);
2480 // Collect function IR attributes based on global settiings.
2481 getDefaultFunctionAttributes(Name
, HasOptnone
, AttrOnCallSite
, FuncAttrs
);
2483 // Override some default IR attributes based on declaration-specific
2486 if (TargetDecl
->hasAttr
<NoSpeculativeLoadHardeningAttr
>())
2487 FuncAttrs
.removeAttribute(llvm::Attribute::SpeculativeLoadHardening
);
2488 if (TargetDecl
->hasAttr
<SpeculativeLoadHardeningAttr
>())
2489 FuncAttrs
.addAttribute(llvm::Attribute::SpeculativeLoadHardening
);
2490 if (TargetDecl
->hasAttr
<NoSplitStackAttr
>())
2491 FuncAttrs
.removeAttribute("split-stack");
2492 if (TargetDecl
->hasAttr
<ZeroCallUsedRegsAttr
>()) {
2493 // A function "__attribute__((...))" overrides the command-line flag.
2495 TargetDecl
->getAttr
<ZeroCallUsedRegsAttr
>()->getZeroCallUsedRegs();
2496 FuncAttrs
.removeAttribute("zero-call-used-regs");
2497 FuncAttrs
.addAttribute(
2498 "zero-call-used-regs",
2499 ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind
));
2502 // Add NonLazyBind attribute to function declarations when -fno-plt
2504 // FIXME: what if we just haven't processed the function definition
2505 // yet, or if it's an external definition like C99 inline?
2506 if (CodeGenOpts
.NoPLT
) {
2507 if (auto *Fn
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
2508 if (!Fn
->isDefined() && !AttrOnCallSite
) {
2509 FuncAttrs
.addAttribute(llvm::Attribute::NonLazyBind
);
2515 // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
2516 // functions with -funique-internal-linkage-names.
2517 if (TargetDecl
&& CodeGenOpts
.UniqueInternalLinkageNames
) {
2518 if (const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
)) {
2519 if (!FD
->isExternallyVisible())
2520 FuncAttrs
.addAttribute("sample-profile-suffix-elision-policy",
2525 // Collect non-call-site function IR attributes from declaration-specific
2527 if (!AttrOnCallSite
) {
2528 if (TargetDecl
&& TargetDecl
->hasAttr
<CmseNSEntryAttr
>())
2529 FuncAttrs
.addAttribute("cmse_nonsecure_entry");
2531 // Whether tail calls are enabled.
2532 auto shouldDisableTailCalls
= [&] {
2533 // Should this be honored in getDefaultFunctionAttributes?
2534 if (CodeGenOpts
.DisableTailCalls
)
2540 if (TargetDecl
->hasAttr
<DisableTailCallsAttr
>() ||
2541 TargetDecl
->hasAttr
<AnyX86InterruptAttr
>())
2544 if (CodeGenOpts
.NoEscapingBlockTailCalls
) {
2545 if (const auto *BD
= dyn_cast
<BlockDecl
>(TargetDecl
))
2546 if (!BD
->doesNotEscape())
2552 if (shouldDisableTailCalls())
2553 FuncAttrs
.addAttribute("disable-tail-calls", "true");
2555 // CPU/feature overrides. addDefaultFunctionDefinitionAttributes
2556 // handles these separately to set them based on the global defaults.
2557 GetCPUAndFeaturesAttributes(CalleeInfo
.getCalleeDecl(), FuncAttrs
);
2560 // Collect attributes from arguments and return values.
2561 ClangToLLVMArgMapping
IRFunctionArgs(getContext(), FI
);
2563 QualType RetTy
= FI
.getReturnType();
2564 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
2565 const llvm::DataLayout
&DL
= getDataLayout();
2567 // Determine if the return type could be partially undef
2568 if (CodeGenOpts
.EnableNoundefAttrs
&&
2569 HasStrictReturn(*this, RetTy
, TargetDecl
)) {
2570 if (!RetTy
->isVoidType() && RetAI
.getKind() != ABIArgInfo::Indirect
&&
2571 DetermineNoUndef(RetTy
, getTypes(), DL
, RetAI
))
2572 RetAttrs
.addAttribute(llvm::Attribute::NoUndef
);
2575 switch (RetAI
.getKind()) {
2576 case ABIArgInfo::Extend
:
2577 if (RetAI
.isSignExt())
2578 RetAttrs
.addAttribute(llvm::Attribute::SExt
);
2580 RetAttrs
.addAttribute(llvm::Attribute::ZExt
);
2582 case ABIArgInfo::Direct
:
2583 if (RetAI
.getInReg())
2584 RetAttrs
.addAttribute(llvm::Attribute::InReg
);
2586 if (canApplyNoFPClass(RetAI
, RetTy
, true))
2587 RetAttrs
.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2590 case ABIArgInfo::Ignore
:
2593 case ABIArgInfo::InAlloca
:
2594 case ABIArgInfo::Indirect
: {
2595 // inalloca and sret disable readnone and readonly
2596 AddPotentialArgAccess();
2600 case ABIArgInfo::CoerceAndExpand
:
2603 case ABIArgInfo::Expand
:
2604 case ABIArgInfo::IndirectAliased
:
2605 llvm_unreachable("Invalid ABI kind for return argument");
2609 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2610 if (const auto *RefTy
= RetTy
->getAs
<ReferenceType
>()) {
2611 QualType PTy
= RefTy
->getPointeeType();
2612 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType())
2613 RetAttrs
.addDereferenceableAttr(
2614 getMinimumObjectSize(PTy
).getQuantity());
2615 if (getTypes().getTargetAddressSpace(PTy
) == 0 &&
2616 !CodeGenOpts
.NullPointerIsValid
)
2617 RetAttrs
.addAttribute(llvm::Attribute::NonNull
);
2618 if (PTy
->isObjectType()) {
2619 llvm::Align Alignment
=
2620 getNaturalPointeeTypeAlignment(RetTy
).getAsAlign();
2621 RetAttrs
.addAlignmentAttr(Alignment
);
2626 bool hasUsedSRet
= false;
2627 SmallVector
<llvm::AttributeSet
, 4> ArgAttrs(IRFunctionArgs
.totalIRArgs());
2629 // Attach attributes to sret.
2630 if (IRFunctionArgs
.hasSRetArg()) {
2631 llvm::AttrBuilder
SRETAttrs(getLLVMContext());
2632 SRETAttrs
.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy
));
2633 SRETAttrs
.addAttribute(llvm::Attribute::Writable
);
2634 SRETAttrs
.addAttribute(llvm::Attribute::DeadOnUnwind
);
2636 if (RetAI
.getInReg())
2637 SRETAttrs
.addAttribute(llvm::Attribute::InReg
);
2638 SRETAttrs
.addAlignmentAttr(RetAI
.getIndirectAlign().getQuantity());
2639 ArgAttrs
[IRFunctionArgs
.getSRetArgNo()] =
2640 llvm::AttributeSet::get(getLLVMContext(), SRETAttrs
);
2643 // Attach attributes to inalloca argument.
2644 if (IRFunctionArgs
.hasInallocaArg()) {
2645 llvm::AttrBuilder
Attrs(getLLVMContext());
2646 Attrs
.addInAllocaAttr(FI
.getArgStruct());
2647 ArgAttrs
[IRFunctionArgs
.getInallocaArgNo()] =
2648 llvm::AttributeSet::get(getLLVMContext(), Attrs
);
2651 // Apply `nonnull`, `dereferencable(N)` and `align N` to the `this` argument,
2652 // unless this is a thunk function.
2653 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2654 if (FI
.isInstanceMethod() && !IRFunctionArgs
.hasInallocaArg() &&
2655 !FI
.arg_begin()->type
->isVoidPointerType() && !IsThunk
) {
2656 auto IRArgs
= IRFunctionArgs
.getIRArgs(0);
2658 assert(IRArgs
.second
== 1 && "Expected only a single `this` pointer.");
2660 llvm::AttrBuilder
Attrs(getLLVMContext());
2663 FI
.arg_begin()->type
.getTypePtr()->getPointeeType();
2665 if (!CodeGenOpts
.NullPointerIsValid
&&
2666 getTypes().getTargetAddressSpace(FI
.arg_begin()->type
) == 0) {
2667 Attrs
.addAttribute(llvm::Attribute::NonNull
);
2668 Attrs
.addDereferenceableAttr(getMinimumObjectSize(ThisTy
).getQuantity());
2670 // FIXME dereferenceable should be correct here, regardless of
2671 // NullPointerIsValid. However, dereferenceable currently does not always
2672 // respect NullPointerIsValid and may imply nonnull and break the program.
2673 // See https://reviews.llvm.org/D66618 for discussions.
2674 Attrs
.addDereferenceableOrNullAttr(
2675 getMinimumObjectSize(
2676 FI
.arg_begin()->type
.castAs
<PointerType
>()->getPointeeType())
2680 llvm::Align Alignment
=
2681 getNaturalTypeAlignment(ThisTy
, /*BaseInfo=*/nullptr,
2682 /*TBAAInfo=*/nullptr, /*forPointeeType=*/true)
2684 Attrs
.addAlignmentAttr(Alignment
);
2686 ArgAttrs
[IRArgs
.first
] = llvm::AttributeSet::get(getLLVMContext(), Attrs
);
2690 for (CGFunctionInfo::const_arg_iterator I
= FI
.arg_begin(),
2692 I
!= E
; ++I
, ++ArgNo
) {
2693 QualType ParamType
= I
->type
;
2694 const ABIArgInfo
&AI
= I
->info
;
2695 llvm::AttrBuilder
Attrs(getLLVMContext());
2697 // Add attribute for padding argument, if necessary.
2698 if (IRFunctionArgs
.hasPaddingArg(ArgNo
)) {
2699 if (AI
.getPaddingInReg()) {
2700 ArgAttrs
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
2701 llvm::AttributeSet::get(
2703 llvm::AttrBuilder(getLLVMContext()).addAttribute(llvm::Attribute::InReg
));
2707 // Decide whether the argument we're handling could be partially undef
2708 if (CodeGenOpts
.EnableNoundefAttrs
&&
2709 DetermineNoUndef(ParamType
, getTypes(), DL
, AI
)) {
2710 Attrs
.addAttribute(llvm::Attribute::NoUndef
);
2713 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
2714 // have the corresponding parameter variable. It doesn't make
2715 // sense to do it here because parameters are so messed up.
2716 switch (AI
.getKind()) {
2717 case ABIArgInfo::Extend
:
2719 Attrs
.addAttribute(llvm::Attribute::SExt
);
2721 Attrs
.addAttribute(llvm::Attribute::ZExt
);
2723 case ABIArgInfo::Direct
:
2724 if (ArgNo
== 0 && FI
.isChainCall())
2725 Attrs
.addAttribute(llvm::Attribute::Nest
);
2726 else if (AI
.getInReg())
2727 Attrs
.addAttribute(llvm::Attribute::InReg
);
2728 Attrs
.addStackAlignmentAttr(llvm::MaybeAlign(AI
.getDirectAlign()));
2730 if (canApplyNoFPClass(AI
, ParamType
, false))
2731 Attrs
.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2733 case ABIArgInfo::Indirect
: {
2735 Attrs
.addAttribute(llvm::Attribute::InReg
);
2737 if (AI
.getIndirectByVal())
2738 Attrs
.addByValAttr(getTypes().ConvertTypeForMem(ParamType
));
2740 auto *Decl
= ParamType
->getAsRecordDecl();
2741 if (CodeGenOpts
.PassByValueIsNoAlias
&& Decl
&&
2742 Decl
->getArgPassingRestrictions() ==
2743 RecordArgPassingKind::CanPassInRegs
)
2744 // When calling the function, the pointer passed in will be the only
2745 // reference to the underlying object. Mark it accordingly.
2746 Attrs
.addAttribute(llvm::Attribute::NoAlias
);
2748 // TODO: We could add the byref attribute if not byval, but it would
2749 // require updating many testcases.
2751 CharUnits Align
= AI
.getIndirectAlign();
2753 // In a byval argument, it is important that the required
2754 // alignment of the type is honored, as LLVM might be creating a
2755 // *new* stack object, and needs to know what alignment to give
2756 // it. (Sometimes it can deduce a sensible alignment on its own,
2757 // but not if clang decides it must emit a packed struct, or the
2758 // user specifies increased alignment requirements.)
2760 // This is different from indirect *not* byval, where the object
2761 // exists already, and the align attribute is purely
2763 assert(!Align
.isZero());
2765 // For now, only add this when we have a byval argument.
2766 // TODO: be less lazy about updating test cases.
2767 if (AI
.getIndirectByVal())
2768 Attrs
.addAlignmentAttr(Align
.getQuantity());
2770 // byval disables readnone and readonly.
2771 AddPotentialArgAccess();
2774 case ABIArgInfo::IndirectAliased
: {
2775 CharUnits Align
= AI
.getIndirectAlign();
2776 Attrs
.addByRefAttr(getTypes().ConvertTypeForMem(ParamType
));
2777 Attrs
.addAlignmentAttr(Align
.getQuantity());
2780 case ABIArgInfo::Ignore
:
2781 case ABIArgInfo::Expand
:
2782 case ABIArgInfo::CoerceAndExpand
:
2785 case ABIArgInfo::InAlloca
:
2786 // inalloca disables readnone and readonly.
2787 AddPotentialArgAccess();
2791 if (const auto *RefTy
= ParamType
->getAs
<ReferenceType
>()) {
2792 QualType PTy
= RefTy
->getPointeeType();
2793 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType())
2794 Attrs
.addDereferenceableAttr(
2795 getMinimumObjectSize(PTy
).getQuantity());
2796 if (getTypes().getTargetAddressSpace(PTy
) == 0 &&
2797 !CodeGenOpts
.NullPointerIsValid
)
2798 Attrs
.addAttribute(llvm::Attribute::NonNull
);
2799 if (PTy
->isObjectType()) {
2800 llvm::Align Alignment
=
2801 getNaturalPointeeTypeAlignment(ParamType
).getAsAlign();
2802 Attrs
.addAlignmentAttr(Alignment
);
2806 // From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:
2807 // > For arguments to a __kernel function declared to be a pointer to a
2808 // > data type, the OpenCL compiler can assume that the pointee is always
2809 // > appropriately aligned as required by the data type.
2810 if (TargetDecl
&& TargetDecl
->hasAttr
<OpenCLKernelAttr
>() &&
2811 ParamType
->isPointerType()) {
2812 QualType PTy
= ParamType
->getPointeeType();
2813 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType()) {
2814 llvm::Align Alignment
=
2815 getNaturalPointeeTypeAlignment(ParamType
).getAsAlign();
2816 Attrs
.addAlignmentAttr(Alignment
);
2820 switch (FI
.getExtParameterInfo(ArgNo
).getABI()) {
2821 case ParameterABI::Ordinary
:
2824 case ParameterABI::SwiftIndirectResult
: {
2825 // Add 'sret' if we haven't already used it for something, but
2826 // only if the result is void.
2827 if (!hasUsedSRet
&& RetTy
->isVoidType()) {
2828 Attrs
.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType
));
2832 // Add 'noalias' in either case.
2833 Attrs
.addAttribute(llvm::Attribute::NoAlias
);
2835 // Add 'dereferenceable' and 'alignment'.
2836 auto PTy
= ParamType
->getPointeeType();
2837 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType()) {
2838 auto info
= getContext().getTypeInfoInChars(PTy
);
2839 Attrs
.addDereferenceableAttr(info
.Width
.getQuantity());
2840 Attrs
.addAlignmentAttr(info
.Align
.getAsAlign());
2845 case ParameterABI::SwiftErrorResult
:
2846 Attrs
.addAttribute(llvm::Attribute::SwiftError
);
2849 case ParameterABI::SwiftContext
:
2850 Attrs
.addAttribute(llvm::Attribute::SwiftSelf
);
2853 case ParameterABI::SwiftAsyncContext
:
2854 Attrs
.addAttribute(llvm::Attribute::SwiftAsync
);
2858 if (FI
.getExtParameterInfo(ArgNo
).isNoEscape())
2859 Attrs
.addAttribute(llvm::Attribute::NoCapture
);
2861 if (Attrs
.hasAttributes()) {
2862 unsigned FirstIRArg
, NumIRArgs
;
2863 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
2864 for (unsigned i
= 0; i
< NumIRArgs
; i
++)
2865 ArgAttrs
[FirstIRArg
+ i
] = ArgAttrs
[FirstIRArg
+ i
].addAttributes(
2866 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs
));
2869 assert(ArgNo
== FI
.arg_size());
2871 AttrList
= llvm::AttributeList::get(
2872 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs
),
2873 llvm::AttributeSet::get(getLLVMContext(), RetAttrs
), ArgAttrs
);
2876 /// An argument came in as a promoted argument; demote it back to its
2878 static llvm::Value
*emitArgumentDemotion(CodeGenFunction
&CGF
,
2880 llvm::Value
*value
) {
2881 llvm::Type
*varType
= CGF
.ConvertType(var
->getType());
2883 // This can happen with promotions that actually don't change the
2884 // underlying type, like the enum promotions.
2885 if (value
->getType() == varType
) return value
;
2887 assert((varType
->isIntegerTy() || varType
->isFloatingPointTy())
2888 && "unexpected promotion type");
2890 if (isa
<llvm::IntegerType
>(varType
))
2891 return CGF
.Builder
.CreateTrunc(value
, varType
, "arg.unpromote");
2893 return CGF
.Builder
.CreateFPCast(value
, varType
, "arg.unpromote");
2896 /// Returns the attribute (either parameter attribute, or function
2897 /// attribute), which declares argument ArgNo to be non-null.
2898 static const NonNullAttr
*getNonNullAttr(const Decl
*FD
, const ParmVarDecl
*PVD
,
2899 QualType ArgType
, unsigned ArgNo
) {
2900 // FIXME: __attribute__((nonnull)) can also be applied to:
2901 // - references to pointers, where the pointee is known to be
2902 // nonnull (apparently a Clang extension)
2903 // - transparent unions containing pointers
2904 // In the former case, LLVM IR cannot represent the constraint. In
2905 // the latter case, we have no guarantee that the transparent union
2906 // is in fact passed as a pointer.
2907 if (!ArgType
->isAnyPointerType() && !ArgType
->isBlockPointerType())
2909 // First, check attribute on parameter itself.
2911 if (auto ParmNNAttr
= PVD
->getAttr
<NonNullAttr
>())
2914 // Check function attributes.
2917 for (const auto *NNAttr
: FD
->specific_attrs
<NonNullAttr
>()) {
2918 if (NNAttr
->isNonNull(ArgNo
))
2925 struct CopyBackSwiftError final
: EHScopeStack::Cleanup
{
2928 CopyBackSwiftError(Address temp
, Address arg
) : Temp(temp
), Arg(arg
) {}
2929 void Emit(CodeGenFunction
&CGF
, Flags flags
) override
{
2930 llvm::Value
*errorValue
= CGF
.Builder
.CreateLoad(Temp
);
2931 CGF
.Builder
.CreateStore(errorValue
, Arg
);
2936 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo
&FI
,
2938 const FunctionArgList
&Args
) {
2939 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<NakedAttr
>())
2940 // Naked functions don't have prologues.
2943 // If this is an implicit-return-zero function, go ahead and
2944 // initialize the return value. TODO: it might be nice to have
2945 // a more general mechanism for this that didn't require synthesized
2946 // return statements.
2947 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurCodeDecl
)) {
2948 if (FD
->hasImplicitReturnZero()) {
2949 QualType RetTy
= FD
->getReturnType().getUnqualifiedType();
2950 llvm::Type
* LLVMTy
= CGM
.getTypes().ConvertType(RetTy
);
2951 llvm::Constant
* Zero
= llvm::Constant::getNullValue(LLVMTy
);
2952 Builder
.CreateStore(Zero
, ReturnValue
);
2956 // FIXME: We no longer need the types from FunctionArgList; lift up and
2959 ClangToLLVMArgMapping
IRFunctionArgs(CGM
.getContext(), FI
);
2960 assert(Fn
->arg_size() == IRFunctionArgs
.totalIRArgs());
2962 // If we're using inalloca, all the memory arguments are GEPs off of the last
2963 // parameter, which is a pointer to the complete memory area.
2964 Address ArgStruct
= Address::invalid();
2965 if (IRFunctionArgs
.hasInallocaArg())
2966 ArgStruct
= Address(Fn
->getArg(IRFunctionArgs
.getInallocaArgNo()),
2967 FI
.getArgStruct(), FI
.getArgStructAlignment());
2969 // Name the struct return parameter.
2970 if (IRFunctionArgs
.hasSRetArg()) {
2971 auto AI
= Fn
->getArg(IRFunctionArgs
.getSRetArgNo());
2972 AI
->setName("agg.result");
2973 AI
->addAttr(llvm::Attribute::NoAlias
);
2976 // Track if we received the parameter as a pointer (indirect, byval, or
2977 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
2978 // into a local alloca for us.
2979 SmallVector
<ParamValue
, 16> ArgVals
;
2980 ArgVals
.reserve(Args
.size());
2982 // Create a pointer value for every parameter declaration. This usually
2983 // entails copying one or more LLVM IR arguments into an alloca. Don't push
2984 // any cleanups or do anything that might unwind. We do that separately, so
2985 // we can push the cleanups in the correct order for the ABI.
2986 assert(FI
.arg_size() == Args
.size() &&
2987 "Mismatch between function signature & arguments.");
2989 CGFunctionInfo::const_arg_iterator info_it
= FI
.arg_begin();
2990 for (FunctionArgList::const_iterator i
= Args
.begin(), e
= Args
.end();
2991 i
!= e
; ++i
, ++info_it
, ++ArgNo
) {
2992 const VarDecl
*Arg
= *i
;
2993 const ABIArgInfo
&ArgI
= info_it
->info
;
2996 isa
<ParmVarDecl
>(Arg
) && cast
<ParmVarDecl
>(Arg
)->isKNRPromoted();
2997 // We are converting from ABIArgInfo type to VarDecl type directly, unless
2998 // the parameter is promoted. In this case we convert to
2999 // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
3000 QualType Ty
= isPromoted
? info_it
->type
: Arg
->getType();
3001 assert(hasScalarEvaluationKind(Ty
) ==
3002 hasScalarEvaluationKind(Arg
->getType()));
3004 unsigned FirstIRArg
, NumIRArgs
;
3005 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
3007 switch (ArgI
.getKind()) {
3008 case ABIArgInfo::InAlloca
: {
3009 assert(NumIRArgs
== 0);
3010 auto FieldIndex
= ArgI
.getInAllocaFieldIndex();
3012 Builder
.CreateStructGEP(ArgStruct
, FieldIndex
, Arg
->getName());
3013 if (ArgI
.getInAllocaIndirect())
3014 V
= Address(Builder
.CreateLoad(V
), ConvertTypeForMem(Ty
),
3015 getContext().getTypeAlignInChars(Ty
));
3016 ArgVals
.push_back(ParamValue::forIndirect(V
));
3020 case ABIArgInfo::Indirect
:
3021 case ABIArgInfo::IndirectAliased
: {
3022 assert(NumIRArgs
== 1);
3023 Address ParamAddr
= makeNaturalAddressForPointer(
3024 Fn
->getArg(FirstIRArg
), Ty
, ArgI
.getIndirectAlign(), false, nullptr,
3025 nullptr, KnownNonNull
);
3027 if (!hasScalarEvaluationKind(Ty
)) {
3028 // Aggregates and complex variables are accessed by reference. All we
3029 // need to do is realign the value, if requested. Also, if the address
3030 // may be aliased, copy it to ensure that the parameter variable is
3031 // mutable and has a unique adress, as C requires.
3032 if (ArgI
.getIndirectRealign() || ArgI
.isIndirectAliased()) {
3033 RawAddress AlignedTemp
= CreateMemTemp(Ty
, "coerce");
3035 // Copy from the incoming argument pointer to the temporary with the
3036 // appropriate alignment.
3038 // FIXME: We should have a common utility for generating an aggregate
3040 CharUnits Size
= getContext().getTypeSizeInChars(Ty
);
3041 Builder
.CreateMemCpy(
3042 AlignedTemp
.getPointer(), AlignedTemp
.getAlignment().getAsAlign(),
3043 ParamAddr
.emitRawPointer(*this),
3044 ParamAddr
.getAlignment().getAsAlign(),
3045 llvm::ConstantInt::get(IntPtrTy
, Size
.getQuantity()));
3046 ParamAddr
= AlignedTemp
;
3048 ArgVals
.push_back(ParamValue::forIndirect(ParamAddr
));
3050 // Load scalar value from indirect argument.
3052 EmitLoadOfScalar(ParamAddr
, false, Ty
, Arg
->getBeginLoc());
3055 V
= emitArgumentDemotion(*this, Arg
, V
);
3056 ArgVals
.push_back(ParamValue::forDirect(V
));
3061 case ABIArgInfo::Extend
:
3062 case ABIArgInfo::Direct
: {
3063 auto AI
= Fn
->getArg(FirstIRArg
);
3064 llvm::Type
*LTy
= ConvertType(Arg
->getType());
3066 // Prepare parameter attributes. So far, only attributes for pointer
3067 // parameters are prepared. See
3068 // http://llvm.org/docs/LangRef.html#paramattrs.
3069 if (ArgI
.getDirectOffset() == 0 && LTy
->isPointerTy() &&
3070 ArgI
.getCoerceToType()->isPointerTy()) {
3071 assert(NumIRArgs
== 1);
3073 if (const ParmVarDecl
*PVD
= dyn_cast
<ParmVarDecl
>(Arg
)) {
3074 // Set `nonnull` attribute if any.
3075 if (getNonNullAttr(CurCodeDecl
, PVD
, PVD
->getType(),
3076 PVD
->getFunctionScopeIndex()) &&
3077 !CGM
.getCodeGenOpts().NullPointerIsValid
)
3078 AI
->addAttr(llvm::Attribute::NonNull
);
3080 QualType OTy
= PVD
->getOriginalType();
3081 if (const auto *ArrTy
=
3082 getContext().getAsConstantArrayType(OTy
)) {
3083 // A C99 array parameter declaration with the static keyword also
3084 // indicates dereferenceability, and if the size is constant we can
3085 // use the dereferenceable attribute (which requires the size in
3087 if (ArrTy
->getSizeModifier() == ArraySizeModifier::Static
) {
3088 QualType ETy
= ArrTy
->getElementType();
3089 llvm::Align Alignment
=
3090 CGM
.getNaturalTypeAlignment(ETy
).getAsAlign();
3091 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment
));
3092 uint64_t ArrSize
= ArrTy
->getZExtSize();
3093 if (!ETy
->isIncompleteType() && ETy
->isConstantSizeType() &&
3095 llvm::AttrBuilder
Attrs(getLLVMContext());
3096 Attrs
.addDereferenceableAttr(
3097 getContext().getTypeSizeInChars(ETy
).getQuantity() *
3099 AI
->addAttrs(Attrs
);
3100 } else if (getContext().getTargetInfo().getNullPointerValue(
3101 ETy
.getAddressSpace()) == 0 &&
3102 !CGM
.getCodeGenOpts().NullPointerIsValid
) {
3103 AI
->addAttr(llvm::Attribute::NonNull
);
3106 } else if (const auto *ArrTy
=
3107 getContext().getAsVariableArrayType(OTy
)) {
3108 // For C99 VLAs with the static keyword, we don't know the size so
3109 // we can't use the dereferenceable attribute, but in addrspace(0)
3110 // we know that it must be nonnull.
3111 if (ArrTy
->getSizeModifier() == ArraySizeModifier::Static
) {
3112 QualType ETy
= ArrTy
->getElementType();
3113 llvm::Align Alignment
=
3114 CGM
.getNaturalTypeAlignment(ETy
).getAsAlign();
3115 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment
));
3116 if (!getTypes().getTargetAddressSpace(ETy
) &&
3117 !CGM
.getCodeGenOpts().NullPointerIsValid
)
3118 AI
->addAttr(llvm::Attribute::NonNull
);
3122 // Set `align` attribute if any.
3123 const auto *AVAttr
= PVD
->getAttr
<AlignValueAttr
>();
3125 if (const auto *TOTy
= OTy
->getAs
<TypedefType
>())
3126 AVAttr
= TOTy
->getDecl()->getAttr
<AlignValueAttr
>();
3127 if (AVAttr
&& !SanOpts
.has(SanitizerKind::Alignment
)) {
3128 // If alignment-assumption sanitizer is enabled, we do *not* add
3129 // alignment attribute here, but emit normal alignment assumption,
3130 // so the UBSAN check could function.
3131 llvm::ConstantInt
*AlignmentCI
=
3132 cast
<llvm::ConstantInt
>(EmitScalarExpr(AVAttr
->getAlignment()));
3133 uint64_t AlignmentInt
=
3134 AlignmentCI
->getLimitedValue(llvm::Value::MaximumAlignment
);
3135 if (AI
->getParamAlign().valueOrOne() < AlignmentInt
) {
3136 AI
->removeAttr(llvm::Attribute::AttrKind::Alignment
);
3137 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(
3138 llvm::Align(AlignmentInt
)));
3143 // Set 'noalias' if an argument type has the `restrict` qualifier.
3144 if (Arg
->getType().isRestrictQualified())
3145 AI
->addAttr(llvm::Attribute::NoAlias
);
3148 // Prepare the argument value. If we have the trivial case, handle it
3149 // with no muss and fuss.
3150 if (!isa
<llvm::StructType
>(ArgI
.getCoerceToType()) &&
3151 ArgI
.getCoerceToType() == ConvertType(Ty
) &&
3152 ArgI
.getDirectOffset() == 0) {
3153 assert(NumIRArgs
== 1);
3155 // LLVM expects swifterror parameters to be used in very restricted
3156 // ways. Copy the value into a less-restricted temporary.
3157 llvm::Value
*V
= AI
;
3158 if (FI
.getExtParameterInfo(ArgNo
).getABI()
3159 == ParameterABI::SwiftErrorResult
) {
3160 QualType pointeeTy
= Ty
->getPointeeType();
3161 assert(pointeeTy
->isPointerType());
3163 CreateMemTemp(pointeeTy
, getPointerAlign(), "swifterror.temp");
3164 Address arg
= makeNaturalAddressForPointer(
3165 V
, pointeeTy
, getContext().getTypeAlignInChars(pointeeTy
));
3166 llvm::Value
*incomingErrorValue
= Builder
.CreateLoad(arg
);
3167 Builder
.CreateStore(incomingErrorValue
, temp
);
3168 V
= temp
.getPointer();
3170 // Push a cleanup to copy the value back at the end of the function.
3171 // The convention does not guarantee that the value will be written
3172 // back if the function exits with an unwind exception.
3173 EHStack
.pushCleanup
<CopyBackSwiftError
>(NormalCleanup
, temp
, arg
);
3176 // Ensure the argument is the correct type.
3177 if (V
->getType() != ArgI
.getCoerceToType())
3178 V
= Builder
.CreateBitCast(V
, ArgI
.getCoerceToType());
3181 V
= emitArgumentDemotion(*this, Arg
, V
);
3183 // Because of merging of function types from multiple decls it is
3184 // possible for the type of an argument to not match the corresponding
3185 // type in the function type. Since we are codegening the callee
3186 // in here, add a cast to the argument type.
3187 llvm::Type
*LTy
= ConvertType(Arg
->getType());
3188 if (V
->getType() != LTy
)
3189 V
= Builder
.CreateBitCast(V
, LTy
);
3191 ArgVals
.push_back(ParamValue::forDirect(V
));
3195 // VLST arguments are coerced to VLATs at the function boundary for
3196 // ABI consistency. If this is a VLST that was coerced to
3197 // a VLAT at the function boundary and the types match up, use
3198 // llvm.vector.extract to convert back to the original VLST.
3199 if (auto *VecTyTo
= dyn_cast
<llvm::FixedVectorType
>(ConvertType(Ty
))) {
3200 llvm::Value
*Coerced
= Fn
->getArg(FirstIRArg
);
3201 if (auto *VecTyFrom
=
3202 dyn_cast
<llvm::ScalableVectorType
>(Coerced
->getType())) {
3203 // If we are casting a scalable i1 predicate vector to a fixed i8
3204 // vector, bitcast the source and use a vector extract.
3205 if (VecTyFrom
->getElementType()->isIntegerTy(1) &&
3206 VecTyFrom
->getElementCount().isKnownMultipleOf(8) &&
3207 VecTyTo
->getElementType() == Builder
.getInt8Ty()) {
3208 VecTyFrom
= llvm::ScalableVectorType::get(
3209 VecTyTo
->getElementType(),
3210 VecTyFrom
->getElementCount().getKnownMinValue() / 8);
3211 Coerced
= Builder
.CreateBitCast(Coerced
, VecTyFrom
);
3213 if (VecTyFrom
->getElementType() == VecTyTo
->getElementType()) {
3214 llvm::Value
*Zero
= llvm::Constant::getNullValue(CGM
.Int64Ty
);
3216 assert(NumIRArgs
== 1);
3217 Coerced
->setName(Arg
->getName() + ".coerce");
3218 ArgVals
.push_back(ParamValue::forDirect(Builder
.CreateExtractVector(
3219 VecTyTo
, Coerced
, Zero
, "cast.fixed")));
3225 llvm::StructType
*STy
=
3226 dyn_cast
<llvm::StructType
>(ArgI
.getCoerceToType());
3227 if (ArgI
.isDirect() && !ArgI
.getCanBeFlattened() && STy
&&
3228 STy
->getNumElements() > 1) {
3229 [[maybe_unused
]] llvm::TypeSize StructSize
=
3230 CGM
.getDataLayout().getTypeAllocSize(STy
);
3231 [[maybe_unused
]] llvm::TypeSize PtrElementSize
=
3232 CGM
.getDataLayout().getTypeAllocSize(ConvertTypeForMem(Ty
));
3233 if (STy
->containsHomogeneousScalableVectorTypes()) {
3234 assert(StructSize
== PtrElementSize
&&
3235 "Only allow non-fractional movement of structure with"
3236 "homogeneous scalable vector type");
3238 ArgVals
.push_back(ParamValue::forDirect(AI
));
3243 Address Alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
),
3246 // Pointer to store into.
3247 Address Ptr
= emitAddressAtOffset(*this, Alloca
, ArgI
);
3249 // Fast-isel and the optimizer generally like scalar values better than
3250 // FCAs, so we flatten them if this is safe to do for this argument.
3251 if (ArgI
.isDirect() && ArgI
.getCanBeFlattened() && STy
&&
3252 STy
->getNumElements() > 1) {
3253 llvm::TypeSize StructSize
= CGM
.getDataLayout().getTypeAllocSize(STy
);
3254 llvm::TypeSize PtrElementSize
=
3255 CGM
.getDataLayout().getTypeAllocSize(Ptr
.getElementType());
3256 if (StructSize
.isScalable()) {
3257 assert(STy
->containsHomogeneousScalableVectorTypes() &&
3258 "ABI only supports structure with homogeneous scalable vector "
3260 assert(StructSize
== PtrElementSize
&&
3261 "Only allow non-fractional movement of structure with"
3262 "homogeneous scalable vector type");
3263 assert(STy
->getNumElements() == NumIRArgs
);
3265 llvm::Value
*LoadedStructValue
= llvm::PoisonValue::get(STy
);
3266 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
3267 auto *AI
= Fn
->getArg(FirstIRArg
+ i
);
3268 AI
->setName(Arg
->getName() + ".coerce" + Twine(i
));
3270 Builder
.CreateInsertValue(LoadedStructValue
, AI
, i
);
3273 Builder
.CreateStore(LoadedStructValue
, Ptr
);
3275 uint64_t SrcSize
= StructSize
.getFixedValue();
3276 uint64_t DstSize
= PtrElementSize
.getFixedValue();
3278 Address AddrToStoreInto
= Address::invalid();
3279 if (SrcSize
<= DstSize
) {
3280 AddrToStoreInto
= Ptr
.withElementType(STy
);
3283 CreateTempAlloca(STy
, Alloca
.getAlignment(), "coerce");
3286 assert(STy
->getNumElements() == NumIRArgs
);
3287 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
3288 auto AI
= Fn
->getArg(FirstIRArg
+ i
);
3289 AI
->setName(Arg
->getName() + ".coerce" + Twine(i
));
3290 Address EltPtr
= Builder
.CreateStructGEP(AddrToStoreInto
, i
);
3291 Builder
.CreateStore(AI
, EltPtr
);
3294 if (SrcSize
> DstSize
) {
3295 Builder
.CreateMemCpy(Ptr
, AddrToStoreInto
, DstSize
);
3299 // Simple case, just do a coerced store of the argument into the alloca.
3300 assert(NumIRArgs
== 1);
3301 auto AI
= Fn
->getArg(FirstIRArg
);
3302 AI
->setName(Arg
->getName() + ".coerce");
3303 CreateCoercedStore(AI
, Ptr
, /*DstIsVolatile=*/false, *this);
3306 // Match to what EmitParmDecl is expecting for this type.
3307 if (CodeGenFunction::hasScalarEvaluationKind(Ty
)) {
3309 EmitLoadOfScalar(Alloca
, false, Ty
, Arg
->getBeginLoc());
3311 V
= emitArgumentDemotion(*this, Arg
, V
);
3312 ArgVals
.push_back(ParamValue::forDirect(V
));
3314 ArgVals
.push_back(ParamValue::forIndirect(Alloca
));
3319 case ABIArgInfo::CoerceAndExpand
: {
3320 // Reconstruct into a temporary.
3321 Address alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
));
3322 ArgVals
.push_back(ParamValue::forIndirect(alloca
));
3324 auto coercionType
= ArgI
.getCoerceAndExpandType();
3325 alloca
= alloca
.withElementType(coercionType
);
3327 unsigned argIndex
= FirstIRArg
;
3328 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
3329 llvm::Type
*eltType
= coercionType
->getElementType(i
);
3330 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
))
3333 auto eltAddr
= Builder
.CreateStructGEP(alloca
, i
);
3334 auto elt
= Fn
->getArg(argIndex
++);
3335 Builder
.CreateStore(elt
, eltAddr
);
3337 assert(argIndex
== FirstIRArg
+ NumIRArgs
);
3341 case ABIArgInfo::Expand
: {
3342 // If this structure was expanded into multiple arguments then
3343 // we need to create a temporary and reconstruct it from the
3345 Address Alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
));
3346 LValue LV
= MakeAddrLValue(Alloca
, Ty
);
3347 ArgVals
.push_back(ParamValue::forIndirect(Alloca
));
3349 auto FnArgIter
= Fn
->arg_begin() + FirstIRArg
;
3350 ExpandTypeFromArgs(Ty
, LV
, FnArgIter
);
3351 assert(FnArgIter
== Fn
->arg_begin() + FirstIRArg
+ NumIRArgs
);
3352 for (unsigned i
= 0, e
= NumIRArgs
; i
!= e
; ++i
) {
3353 auto AI
= Fn
->getArg(FirstIRArg
+ i
);
3354 AI
->setName(Arg
->getName() + "." + Twine(i
));
3359 case ABIArgInfo::Ignore
:
3360 assert(NumIRArgs
== 0);
3361 // Initialize the local variable appropriately.
3362 if (!hasScalarEvaluationKind(Ty
)) {
3363 ArgVals
.push_back(ParamValue::forIndirect(CreateMemTemp(Ty
)));
3365 llvm::Value
*U
= llvm::UndefValue::get(ConvertType(Arg
->getType()));
3366 ArgVals
.push_back(ParamValue::forDirect(U
));
3372 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3373 for (int I
= Args
.size() - 1; I
>= 0; --I
)
3374 EmitParmDecl(*Args
[I
], ArgVals
[I
], I
+ 1);
3376 for (unsigned I
= 0, E
= Args
.size(); I
!= E
; ++I
)
3377 EmitParmDecl(*Args
[I
], ArgVals
[I
], I
+ 1);
3381 static void eraseUnusedBitCasts(llvm::Instruction
*insn
) {
3382 while (insn
->use_empty()) {
3383 llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(insn
);
3384 if (!bitcast
) return;
3386 // This is "safe" because we would have used a ConstantExpr otherwise.
3387 insn
= cast
<llvm::Instruction
>(bitcast
->getOperand(0));
3388 bitcast
->eraseFromParent();
3392 /// Try to emit a fused autorelease of a return result.
3393 static llvm::Value
*tryEmitFusedAutoreleaseOfResult(CodeGenFunction
&CGF
,
3394 llvm::Value
*result
) {
3395 // We must be immediately followed the cast.
3396 llvm::BasicBlock
*BB
= CGF
.Builder
.GetInsertBlock();
3397 if (BB
->empty()) return nullptr;
3398 if (&BB
->back() != result
) return nullptr;
3400 llvm::Type
*resultType
= result
->getType();
3402 // result is in a BasicBlock and is therefore an Instruction.
3403 llvm::Instruction
*generator
= cast
<llvm::Instruction
>(result
);
3405 SmallVector
<llvm::Instruction
*, 4> InstsToKill
;
3408 // %generator = bitcast %type1* %generator2 to %type2*
3409 while (llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(generator
)) {
3410 // We would have emitted this as a constant if the operand weren't
3412 generator
= cast
<llvm::Instruction
>(bitcast
->getOperand(0));
3414 // Require the generator to be immediately followed by the cast.
3415 if (generator
->getNextNode() != bitcast
)
3418 InstsToKill
.push_back(bitcast
);
3422 // %generator = call i8* @objc_retain(i8* %originalResult)
3424 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
3425 llvm::CallInst
*call
= dyn_cast
<llvm::CallInst
>(generator
);
3426 if (!call
) return nullptr;
3428 bool doRetainAutorelease
;
3430 if (call
->getCalledOperand() == CGF
.CGM
.getObjCEntrypoints().objc_retain
) {
3431 doRetainAutorelease
= true;
3432 } else if (call
->getCalledOperand() ==
3433 CGF
.CGM
.getObjCEntrypoints().objc_retainAutoreleasedReturnValue
) {
3434 doRetainAutorelease
= false;
3436 // If we emitted an assembly marker for this call (and the
3437 // ARCEntrypoints field should have been set if so), go looking
3438 // for that call. If we can't find it, we can't do this
3439 // optimization. But it should always be the immediately previous
3440 // instruction, unless we needed bitcasts around the call.
3441 if (CGF
.CGM
.getObjCEntrypoints().retainAutoreleasedReturnValueMarker
) {
3442 llvm::Instruction
*prev
= call
->getPrevNode();
3444 if (isa
<llvm::BitCastInst
>(prev
)) {
3445 prev
= prev
->getPrevNode();
3448 assert(isa
<llvm::CallInst
>(prev
));
3449 assert(cast
<llvm::CallInst
>(prev
)->getCalledOperand() ==
3450 CGF
.CGM
.getObjCEntrypoints().retainAutoreleasedReturnValueMarker
);
3451 InstsToKill
.push_back(prev
);
3457 result
= call
->getArgOperand(0);
3458 InstsToKill
.push_back(call
);
3460 // Keep killing bitcasts, for sanity. Note that we no longer care
3461 // about precise ordering as long as there's exactly one use.
3462 while (llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(result
)) {
3463 if (!bitcast
->hasOneUse()) break;
3464 InstsToKill
.push_back(bitcast
);
3465 result
= bitcast
->getOperand(0);
3468 // Delete all the unnecessary instructions, from latest to earliest.
3469 for (auto *I
: InstsToKill
)
3470 I
->eraseFromParent();
3472 // Do the fused retain/autorelease if we were asked to.
3473 if (doRetainAutorelease
)
3474 result
= CGF
.EmitARCRetainAutoreleaseReturnValue(result
);
3476 // Cast back to the result type.
3477 return CGF
.Builder
.CreateBitCast(result
, resultType
);
3480 /// If this is a +1 of the value of an immutable 'self', remove it.
3481 static llvm::Value
*tryRemoveRetainOfSelf(CodeGenFunction
&CGF
,
3482 llvm::Value
*result
) {
3483 // This is only applicable to a method with an immutable 'self'.
3484 const ObjCMethodDecl
*method
=
3485 dyn_cast_or_null
<ObjCMethodDecl
>(CGF
.CurCodeDecl
);
3486 if (!method
) return nullptr;
3487 const VarDecl
*self
= method
->getSelfDecl();
3488 if (!self
->getType().isConstQualified()) return nullptr;
3490 // Look for a retain call. Note: stripPointerCasts looks through returned arg
3491 // functions, which would cause us to miss the retain.
3492 llvm::CallInst
*retainCall
= dyn_cast
<llvm::CallInst
>(result
);
3493 if (!retainCall
|| retainCall
->getCalledOperand() !=
3494 CGF
.CGM
.getObjCEntrypoints().objc_retain
)
3497 // Look for an ordinary load of 'self'.
3498 llvm::Value
*retainedValue
= retainCall
->getArgOperand(0);
3499 llvm::LoadInst
*load
=
3500 dyn_cast
<llvm::LoadInst
>(retainedValue
->stripPointerCasts());
3501 if (!load
|| load
->isAtomic() || load
->isVolatile() ||
3502 load
->getPointerOperand() != CGF
.GetAddrOfLocalVar(self
).getBasePointer())
3505 // Okay! Burn it all down. This relies for correctness on the
3506 // assumption that the retain is emitted as part of the return and
3507 // that thereafter everything is used "linearly".
3508 llvm::Type
*resultType
= result
->getType();
3509 eraseUnusedBitCasts(cast
<llvm::Instruction
>(result
));
3510 assert(retainCall
->use_empty());
3511 retainCall
->eraseFromParent();
3512 eraseUnusedBitCasts(cast
<llvm::Instruction
>(retainedValue
));
3514 return CGF
.Builder
.CreateBitCast(load
, resultType
);
3517 /// Emit an ARC autorelease of the result of a function.
3519 /// \return the value to actually return from the function
3520 static llvm::Value
*emitAutoreleaseOfResult(CodeGenFunction
&CGF
,
3521 llvm::Value
*result
) {
3522 // If we're returning 'self', kill the initial retain. This is a
3523 // heuristic attempt to "encourage correctness" in the really unfortunate
3524 // case where we have a return of self during a dealloc and we desperately
3525 // need to avoid the possible autorelease.
3526 if (llvm::Value
*self
= tryRemoveRetainOfSelf(CGF
, result
))
3529 // At -O0, try to emit a fused retain/autorelease.
3530 if (CGF
.shouldUseFusedARCCalls())
3531 if (llvm::Value
*fused
= tryEmitFusedAutoreleaseOfResult(CGF
, result
))
3534 return CGF
.EmitARCAutoreleaseReturnValue(result
);
3537 /// Heuristically search for a dominating store to the return-value slot.
3538 static llvm::StoreInst
*findDominatingStoreToReturnValue(CodeGenFunction
&CGF
) {
3539 llvm::Value
*ReturnValuePtr
= CGF
.ReturnValue
.getBasePointer();
3541 // Check if a User is a store which pointerOperand is the ReturnValue.
3542 // We are looking for stores to the ReturnValue, not for stores of the
3543 // ReturnValue to some other location.
3544 auto GetStoreIfValid
= [&CGF
,
3545 ReturnValuePtr
](llvm::User
*U
) -> llvm::StoreInst
* {
3546 auto *SI
= dyn_cast
<llvm::StoreInst
>(U
);
3547 if (!SI
|| SI
->getPointerOperand() != ReturnValuePtr
||
3548 SI
->getValueOperand()->getType() != CGF
.ReturnValue
.getElementType())
3550 // These aren't actually possible for non-coerced returns, and we
3551 // only care about non-coerced returns on this code path.
3552 // All memory instructions inside __try block are volatile.
3553 assert(!SI
->isAtomic() &&
3554 (!SI
->isVolatile() || CGF
.currentFunctionUsesSEHTry()));
3557 // If there are multiple uses of the return-value slot, just check
3558 // for something immediately preceding the IP. Sometimes this can
3559 // happen with how we generate implicit-returns; it can also happen
3560 // with noreturn cleanups.
3561 if (!ReturnValuePtr
->hasOneUse()) {
3562 llvm::BasicBlock
*IP
= CGF
.Builder
.GetInsertBlock();
3563 if (IP
->empty()) return nullptr;
3565 // Look at directly preceding instruction, skipping bitcasts and lifetime
3567 for (llvm::Instruction
&I
: make_range(IP
->rbegin(), IP
->rend())) {
3568 if (isa
<llvm::BitCastInst
>(&I
))
3570 if (auto *II
= dyn_cast
<llvm::IntrinsicInst
>(&I
))
3571 if (II
->getIntrinsicID() == llvm::Intrinsic::lifetime_end
)
3574 return GetStoreIfValid(&I
);
3579 llvm::StoreInst
*store
= GetStoreIfValid(ReturnValuePtr
->user_back());
3580 if (!store
) return nullptr;
3582 // Now do a first-and-dirty dominance check: just walk up the
3583 // single-predecessors chain from the current insertion point.
3584 llvm::BasicBlock
*StoreBB
= store
->getParent();
3585 llvm::BasicBlock
*IP
= CGF
.Builder
.GetInsertBlock();
3586 llvm::SmallPtrSet
<llvm::BasicBlock
*, 4> SeenBBs
;
3587 while (IP
!= StoreBB
) {
3588 if (!SeenBBs
.insert(IP
).second
|| !(IP
= IP
->getSinglePredecessor()))
3592 // Okay, the store's basic block dominates the insertion point; we
3593 // can do our thing.
3597 // Helper functions for EmitCMSEClearRecord
3599 // Set the bits corresponding to a field having width `BitWidth` and located at
3600 // offset `BitOffset` (from the least significant bit) within a storage unit of
3601 // `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.
3602 // Use little-endian layout, i.e.`Bits[0]` is the LSB.
3603 static void setBitRange(SmallVectorImpl
<uint64_t> &Bits
, int BitOffset
,
3604 int BitWidth
, int CharWidth
) {
3605 assert(CharWidth
<= 64);
3606 assert(static_cast<unsigned>(BitWidth
) <= Bits
.size() * CharWidth
);
3609 if (BitOffset
>= CharWidth
) {
3610 Pos
+= BitOffset
/ CharWidth
;
3611 BitOffset
= BitOffset
% CharWidth
;
3614 const uint64_t Used
= (uint64_t(1) << CharWidth
) - 1;
3615 if (BitOffset
+ BitWidth
>= CharWidth
) {
3616 Bits
[Pos
++] |= (Used
<< BitOffset
) & Used
;
3617 BitWidth
-= CharWidth
- BitOffset
;
3621 while (BitWidth
>= CharWidth
) {
3623 BitWidth
-= CharWidth
;
3627 Bits
[Pos
++] |= (Used
>> (CharWidth
- BitWidth
)) << BitOffset
;
3630 // Set the bits corresponding to a field having width `BitWidth` and located at
3631 // offset `BitOffset` (from the least significant bit) within a storage unit of
3632 // `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of
3633 // `Bits` corresponds to one target byte. Use target endian layout.
3634 static void setBitRange(SmallVectorImpl
<uint64_t> &Bits
, int StorageOffset
,
3635 int StorageSize
, int BitOffset
, int BitWidth
,
3636 int CharWidth
, bool BigEndian
) {
3638 SmallVector
<uint64_t, 8> TmpBits(StorageSize
);
3639 setBitRange(TmpBits
, BitOffset
, BitWidth
, CharWidth
);
3642 std::reverse(TmpBits
.begin(), TmpBits
.end());
3644 for (uint64_t V
: TmpBits
)
3645 Bits
[StorageOffset
++] |= V
;
3648 static void setUsedBits(CodeGenModule
&, QualType
, int,
3649 SmallVectorImpl
<uint64_t> &);
3651 // Set the bits in `Bits`, which correspond to the value representations of
3652 // the actual members of the record type `RTy`. Note that this function does
3653 // not handle base classes, virtual tables, etc, since they cannot happen in
3654 // CMSE function arguments or return. The bit mask corresponds to the target
3655 // memory layout, i.e. it's endian dependent.
3656 static void setUsedBits(CodeGenModule
&CGM
, const RecordType
*RTy
, int Offset
,
3657 SmallVectorImpl
<uint64_t> &Bits
) {
3658 ASTContext
&Context
= CGM
.getContext();
3659 int CharWidth
= Context
.getCharWidth();
3660 const RecordDecl
*RD
= RTy
->getDecl()->getDefinition();
3661 const ASTRecordLayout
&ASTLayout
= Context
.getASTRecordLayout(RD
);
3662 const CGRecordLayout
&Layout
= CGM
.getTypes().getCGRecordLayout(RD
);
3665 for (auto I
= RD
->field_begin(), E
= RD
->field_end(); I
!= E
; ++I
, ++Idx
) {
3666 const FieldDecl
*F
= *I
;
3668 if (F
->isUnnamedBitfield() || F
->isZeroLengthBitField(Context
) ||
3669 F
->getType()->isIncompleteArrayType())
3672 if (F
->isBitField()) {
3673 const CGBitFieldInfo
&BFI
= Layout
.getBitFieldInfo(F
);
3674 setBitRange(Bits
, Offset
+ BFI
.StorageOffset
.getQuantity(),
3675 BFI
.StorageSize
/ CharWidth
, BFI
.Offset
,
3676 BFI
.Size
, CharWidth
,
3677 CGM
.getDataLayout().isBigEndian());
3681 setUsedBits(CGM
, F
->getType(),
3682 Offset
+ ASTLayout
.getFieldOffset(Idx
) / CharWidth
, Bits
);
3686 // Set the bits in `Bits`, which correspond to the value representations of
3687 // the elements of an array type `ATy`.
3688 static void setUsedBits(CodeGenModule
&CGM
, const ConstantArrayType
*ATy
,
3689 int Offset
, SmallVectorImpl
<uint64_t> &Bits
) {
3690 const ASTContext
&Context
= CGM
.getContext();
3692 QualType ETy
= Context
.getBaseElementType(ATy
);
3693 int Size
= Context
.getTypeSizeInChars(ETy
).getQuantity();
3694 SmallVector
<uint64_t, 4> TmpBits(Size
);
3695 setUsedBits(CGM
, ETy
, 0, TmpBits
);
3697 for (int I
= 0, N
= Context
.getConstantArrayElementCount(ATy
); I
< N
; ++I
) {
3698 auto Src
= TmpBits
.begin();
3699 auto Dst
= Bits
.begin() + Offset
+ I
* Size
;
3700 for (int J
= 0; J
< Size
; ++J
)
3705 // Set the bits in `Bits`, which correspond to the value representations of
3707 static void setUsedBits(CodeGenModule
&CGM
, QualType QTy
, int Offset
,
3708 SmallVectorImpl
<uint64_t> &Bits
) {
3709 if (const auto *RTy
= QTy
->getAs
<RecordType
>())
3710 return setUsedBits(CGM
, RTy
, Offset
, Bits
);
3712 ASTContext
&Context
= CGM
.getContext();
3713 if (const auto *ATy
= Context
.getAsConstantArrayType(QTy
))
3714 return setUsedBits(CGM
, ATy
, Offset
, Bits
);
3716 int Size
= Context
.getTypeSizeInChars(QTy
).getQuantity();
3720 std::fill_n(Bits
.begin() + Offset
, Size
,
3721 (uint64_t(1) << Context
.getCharWidth()) - 1);
3724 static uint64_t buildMultiCharMask(const SmallVectorImpl
<uint64_t> &Bits
,
3725 int Pos
, int Size
, int CharWidth
,
3730 for (auto P
= Bits
.begin() + Pos
, E
= Bits
.begin() + Pos
+ Size
; P
!= E
;
3732 Mask
= (Mask
<< CharWidth
) | *P
;
3734 auto P
= Bits
.begin() + Pos
+ Size
, End
= Bits
.begin() + Pos
;
3736 Mask
= (Mask
<< CharWidth
) | *--P
;
3742 // Emit code to clear the bits in a record, which aren't a part of any user
3743 // declared member, when the record is a function return.
3744 llvm::Value
*CodeGenFunction::EmitCMSEClearRecord(llvm::Value
*Src
,
3745 llvm::IntegerType
*ITy
,
3747 assert(Src
->getType() == ITy
);
3748 assert(ITy
->getScalarSizeInBits() <= 64);
3750 const llvm::DataLayout
&DataLayout
= CGM
.getDataLayout();
3751 int Size
= DataLayout
.getTypeStoreSize(ITy
);
3752 SmallVector
<uint64_t, 4> Bits(Size
);
3753 setUsedBits(CGM
, QTy
->castAs
<RecordType
>(), 0, Bits
);
3755 int CharWidth
= CGM
.getContext().getCharWidth();
3757 buildMultiCharMask(Bits
, 0, Size
, CharWidth
, DataLayout
.isBigEndian());
3759 return Builder
.CreateAnd(Src
, Mask
, "cmse.clear");
3762 // Emit code to clear the bits in a record, which aren't a part of any user
3763 // declared member, when the record is a function argument.
3764 llvm::Value
*CodeGenFunction::EmitCMSEClearRecord(llvm::Value
*Src
,
3765 llvm::ArrayType
*ATy
,
3767 const llvm::DataLayout
&DataLayout
= CGM
.getDataLayout();
3768 int Size
= DataLayout
.getTypeStoreSize(ATy
);
3769 SmallVector
<uint64_t, 16> Bits(Size
);
3770 setUsedBits(CGM
, QTy
->castAs
<RecordType
>(), 0, Bits
);
3772 // Clear each element of the LLVM array.
3773 int CharWidth
= CGM
.getContext().getCharWidth();
3775 ATy
->getArrayElementType()->getScalarSizeInBits() / CharWidth
;
3777 llvm::Value
*R
= llvm::PoisonValue::get(ATy
);
3778 for (int I
= 0, N
= ATy
->getArrayNumElements(); I
!= N
; ++I
) {
3779 uint64_t Mask
= buildMultiCharMask(Bits
, MaskIndex
, CharsPerElt
, CharWidth
,
3780 DataLayout
.isBigEndian());
3781 MaskIndex
+= CharsPerElt
;
3782 llvm::Value
*T0
= Builder
.CreateExtractValue(Src
, I
);
3783 llvm::Value
*T1
= Builder
.CreateAnd(T0
, Mask
, "cmse.clear");
3784 R
= Builder
.CreateInsertValue(R
, T1
, I
);
3790 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo
&FI
,
3792 SourceLocation EndLoc
) {
3793 if (FI
.isNoReturn()) {
3794 // Noreturn functions don't return.
3795 EmitUnreachable(EndLoc
);
3799 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<NakedAttr
>()) {
3800 // Naked functions don't have epilogues.
3801 Builder
.CreateUnreachable();
3805 // Functions with no result always return void.
3806 if (!ReturnValue
.isValid()) {
3807 Builder
.CreateRetVoid();
3811 llvm::DebugLoc RetDbgLoc
;
3812 llvm::Value
*RV
= nullptr;
3813 QualType RetTy
= FI
.getReturnType();
3814 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
3816 switch (RetAI
.getKind()) {
3817 case ABIArgInfo::InAlloca
:
3818 // Aggregates get evaluated directly into the destination. Sometimes we
3819 // need to return the sret value in a register, though.
3820 assert(hasAggregateEvaluationKind(RetTy
));
3821 if (RetAI
.getInAllocaSRet()) {
3822 llvm::Function::arg_iterator EI
= CurFn
->arg_end();
3824 llvm::Value
*ArgStruct
= &*EI
;
3825 llvm::Value
*SRet
= Builder
.CreateStructGEP(
3826 FI
.getArgStruct(), ArgStruct
, RetAI
.getInAllocaFieldIndex());
3828 cast
<llvm::GetElementPtrInst
>(SRet
)->getResultElementType();
3829 RV
= Builder
.CreateAlignedLoad(Ty
, SRet
, getPointerAlign(), "sret");
3833 case ABIArgInfo::Indirect
: {
3834 auto AI
= CurFn
->arg_begin();
3835 if (RetAI
.isSRetAfterThis())
3837 switch (getEvaluationKind(RetTy
)) {
3840 EmitLoadOfComplex(MakeAddrLValue(ReturnValue
, RetTy
), EndLoc
);
3841 EmitStoreOfComplex(RT
, MakeNaturalAlignAddrLValue(&*AI
, RetTy
),
3846 // Do nothing; aggregates get evaluated directly into the destination.
3849 LValueBaseInfo BaseInfo
;
3850 TBAAAccessInfo TBAAInfo
;
3851 CharUnits Alignment
=
3852 CGM
.getNaturalTypeAlignment(RetTy
, &BaseInfo
, &TBAAInfo
);
3853 Address
ArgAddr(&*AI
, ConvertType(RetTy
), Alignment
);
3855 LValue::MakeAddr(ArgAddr
, RetTy
, getContext(), BaseInfo
, TBAAInfo
);
3857 Builder
.CreateLoad(ReturnValue
), ArgVal
, /*isInit*/ true);
3864 case ABIArgInfo::Extend
:
3865 case ABIArgInfo::Direct
:
3866 if (RetAI
.getCoerceToType() == ConvertType(RetTy
) &&
3867 RetAI
.getDirectOffset() == 0) {
3868 // The internal return value temp always will have pointer-to-return-type
3869 // type, just do a load.
3871 // If there is a dominating store to ReturnValue, we can elide
3872 // the load, zap the store, and usually zap the alloca.
3873 if (llvm::StoreInst
*SI
=
3874 findDominatingStoreToReturnValue(*this)) {
3875 // Reuse the debug location from the store unless there is
3876 // cleanup code to be emitted between the store and return
3878 if (EmitRetDbgLoc
&& !AutoreleaseResult
)
3879 RetDbgLoc
= SI
->getDebugLoc();
3880 // Get the stored value and nuke the now-dead store.
3881 RV
= SI
->getValueOperand();
3882 SI
->eraseFromParent();
3884 // Otherwise, we have to do a simple load.
3886 RV
= Builder
.CreateLoad(ReturnValue
);
3889 // If the value is offset in memory, apply the offset now.
3890 Address V
= emitAddressAtOffset(*this, ReturnValue
, RetAI
);
3892 RV
= CreateCoercedLoad(V
, RetAI
.getCoerceToType(), *this);
3895 // In ARC, end functions that return a retainable type with a call
3896 // to objc_autoreleaseReturnValue.
3897 if (AutoreleaseResult
) {
3899 // Type::isObjCRetainabletype has to be called on a QualType that hasn't
3900 // been stripped of the typedefs, so we cannot use RetTy here. Get the
3901 // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
3902 // CurCodeDecl or BlockInfo.
3905 if (auto *FD
= dyn_cast
<FunctionDecl
>(CurCodeDecl
))
3906 RT
= FD
->getReturnType();
3907 else if (auto *MD
= dyn_cast
<ObjCMethodDecl
>(CurCodeDecl
))
3908 RT
= MD
->getReturnType();
3909 else if (isa
<BlockDecl
>(CurCodeDecl
))
3910 RT
= BlockInfo
->BlockExpression
->getFunctionType()->getReturnType();
3912 llvm_unreachable("Unexpected function/method type");
3914 assert(getLangOpts().ObjCAutoRefCount
&&
3915 !FI
.isReturnsRetained() &&
3916 RT
->isObjCRetainableType());
3918 RV
= emitAutoreleaseOfResult(*this, RV
);
3923 case ABIArgInfo::Ignore
:
3926 case ABIArgInfo::CoerceAndExpand
: {
3927 auto coercionType
= RetAI
.getCoerceAndExpandType();
3929 // Load all of the coerced elements out into results.
3930 llvm::SmallVector
<llvm::Value
*, 4> results
;
3931 Address addr
= ReturnValue
.withElementType(coercionType
);
3932 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
3933 auto coercedEltType
= coercionType
->getElementType(i
);
3934 if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType
))
3937 auto eltAddr
= Builder
.CreateStructGEP(addr
, i
);
3938 auto elt
= Builder
.CreateLoad(eltAddr
);
3939 results
.push_back(elt
);
3942 // If we have one result, it's the single direct result type.
3943 if (results
.size() == 1) {
3946 // Otherwise, we need to make a first-class aggregate.
3948 // Construct a return type that lacks padding elements.
3949 llvm::Type
*returnType
= RetAI
.getUnpaddedCoerceAndExpandType();
3951 RV
= llvm::PoisonValue::get(returnType
);
3952 for (unsigned i
= 0, e
= results
.size(); i
!= e
; ++i
) {
3953 RV
= Builder
.CreateInsertValue(RV
, results
[i
], i
);
3958 case ABIArgInfo::Expand
:
3959 case ABIArgInfo::IndirectAliased
:
3960 llvm_unreachable("Invalid ABI kind for return argument");
3963 llvm::Instruction
*Ret
;
3965 if (CurFuncDecl
&& CurFuncDecl
->hasAttr
<CmseNSEntryAttr
>()) {
3966 // For certain return types, clear padding bits, as they may reveal
3967 // sensitive information.
3968 // Small struct/union types are passed as integers.
3969 auto *ITy
= dyn_cast
<llvm::IntegerType
>(RV
->getType());
3970 if (ITy
!= nullptr && isa
<RecordType
>(RetTy
.getCanonicalType()))
3971 RV
= EmitCMSEClearRecord(RV
, ITy
, RetTy
);
3973 EmitReturnValueCheck(RV
);
3974 Ret
= Builder
.CreateRet(RV
);
3976 Ret
= Builder
.CreateRetVoid();
3980 Ret
->setDebugLoc(std::move(RetDbgLoc
));
3983 void CodeGenFunction::EmitReturnValueCheck(llvm::Value
*RV
) {
3984 // A current decl may not be available when emitting vtable thunks.
3988 // If the return block isn't reachable, neither is this check, so don't emit
3990 if (ReturnBlock
.isValid() && ReturnBlock
.getBlock()->use_empty())
3993 ReturnsNonNullAttr
*RetNNAttr
= nullptr;
3994 if (SanOpts
.has(SanitizerKind::ReturnsNonnullAttribute
))
3995 RetNNAttr
= CurCodeDecl
->getAttr
<ReturnsNonNullAttr
>();
3997 if (!RetNNAttr
&& !requiresReturnValueNullabilityCheck())
4000 // Prefer the returns_nonnull attribute if it's present.
4001 SourceLocation AttrLoc
;
4002 SanitizerMask CheckKind
;
4003 SanitizerHandler Handler
;
4005 assert(!requiresReturnValueNullabilityCheck() &&
4006 "Cannot check nullability and the nonnull attribute");
4007 AttrLoc
= RetNNAttr
->getLocation();
4008 CheckKind
= SanitizerKind::ReturnsNonnullAttribute
;
4009 Handler
= SanitizerHandler::NonnullReturn
;
4011 if (auto *DD
= dyn_cast
<DeclaratorDecl
>(CurCodeDecl
))
4012 if (auto *TSI
= DD
->getTypeSourceInfo())
4013 if (auto FTL
= TSI
->getTypeLoc().getAsAdjusted
<FunctionTypeLoc
>())
4014 AttrLoc
= FTL
.getReturnLoc().findNullabilityLoc();
4015 CheckKind
= SanitizerKind::NullabilityReturn
;
4016 Handler
= SanitizerHandler::NullabilityReturn
;
4019 SanitizerScope
SanScope(this);
4021 // Make sure the "return" source location is valid. If we're checking a
4022 // nullability annotation, make sure the preconditions for the check are met.
4023 llvm::BasicBlock
*Check
= createBasicBlock("nullcheck");
4024 llvm::BasicBlock
*NoCheck
= createBasicBlock("no.nullcheck");
4025 llvm::Value
*SLocPtr
= Builder
.CreateLoad(ReturnLocation
, "return.sloc.load");
4026 llvm::Value
*CanNullCheck
= Builder
.CreateIsNotNull(SLocPtr
);
4027 if (requiresReturnValueNullabilityCheck())
4029 Builder
.CreateAnd(CanNullCheck
, RetValNullabilityPrecondition
);
4030 Builder
.CreateCondBr(CanNullCheck
, Check
, NoCheck
);
4033 // Now do the null check.
4034 llvm::Value
*Cond
= Builder
.CreateIsNotNull(RV
);
4035 llvm::Constant
*StaticData
[] = {EmitCheckSourceLocation(AttrLoc
)};
4036 llvm::Value
*DynamicData
[] = {SLocPtr
};
4037 EmitCheck(std::make_pair(Cond
, CheckKind
), Handler
, StaticData
, DynamicData
);
4042 // The return location should not be used after the check has been emitted.
4043 ReturnLocation
= Address::invalid();
4047 static bool isInAllocaArgument(CGCXXABI
&ABI
, QualType type
) {
4048 const CXXRecordDecl
*RD
= type
->getAsCXXRecordDecl();
4049 return RD
&& ABI
.getRecordArgABI(RD
) == CGCXXABI::RAA_DirectInMemory
;
4052 static AggValueSlot
createPlaceholderSlot(CodeGenFunction
&CGF
,
4054 // FIXME: Generate IR in one pass, rather than going back and fixing up these
4056 llvm::Type
*IRTy
= CGF
.ConvertTypeForMem(Ty
);
4057 llvm::Type
*IRPtrTy
= llvm::PointerType::getUnqual(CGF
.getLLVMContext());
4058 llvm::Value
*Placeholder
= llvm::PoisonValue::get(IRPtrTy
);
4060 // FIXME: When we generate this IR in one pass, we shouldn't need
4061 // this win32-specific alignment hack.
4062 CharUnits Align
= CharUnits::fromQuantity(4);
4063 Placeholder
= CGF
.Builder
.CreateAlignedLoad(IRPtrTy
, Placeholder
, Align
);
4065 return AggValueSlot::forAddr(Address(Placeholder
, IRTy
, Align
),
4067 AggValueSlot::IsNotDestructed
,
4068 AggValueSlot::DoesNotNeedGCBarriers
,
4069 AggValueSlot::IsNotAliased
,
4070 AggValueSlot::DoesNotOverlap
);
4073 void CodeGenFunction::EmitDelegateCallArg(CallArgList
&args
,
4074 const VarDecl
*param
,
4075 SourceLocation loc
) {
4076 // StartFunction converted the ABI-lowered parameter(s) into a
4077 // local alloca. We need to turn that into an r-value suitable
4079 Address local
= GetAddrOfLocalVar(param
);
4081 QualType type
= param
->getType();
4083 // GetAddrOfLocalVar returns a pointer-to-pointer for references,
4084 // but the argument needs to be the original pointer.
4085 if (type
->isReferenceType()) {
4086 args
.add(RValue::get(Builder
.CreateLoad(local
)), type
);
4088 // In ARC, move out of consumed arguments so that the release cleanup
4089 // entered by StartFunction doesn't cause an over-release. This isn't
4090 // optimal -O0 code generation, but it should get cleaned up when
4091 // optimization is enabled. This also assumes that delegate calls are
4092 // performed exactly once for a set of arguments, but that should be safe.
4093 } else if (getLangOpts().ObjCAutoRefCount
&&
4094 param
->hasAttr
<NSConsumedAttr
>() &&
4095 type
->isObjCRetainableType()) {
4096 llvm::Value
*ptr
= Builder
.CreateLoad(local
);
4098 llvm::ConstantPointerNull::get(cast
<llvm::PointerType
>(ptr
->getType()));
4099 Builder
.CreateStore(null
, local
);
4100 args
.add(RValue::get(ptr
), type
);
4102 // For the most part, we just need to load the alloca, except that
4103 // aggregate r-values are actually pointers to temporaries.
4105 args
.add(convertTempToRValue(local
, type
, loc
), type
);
4108 // Deactivate the cleanup for the callee-destructed param that was pushed.
4109 if (type
->isRecordType() && !CurFuncIsThunk
&&
4110 type
->castAs
<RecordType
>()->getDecl()->isParamDestroyedInCallee() &&
4111 param
->needsDestruction(getContext())) {
4112 EHScopeStack::stable_iterator cleanup
=
4113 CalleeDestructedParamCleanups
.lookup(cast
<ParmVarDecl
>(param
));
4114 assert(cleanup
.isValid() &&
4115 "cleanup for callee-destructed param not recorded");
4116 // This unreachable is a temporary marker which will be removed later.
4117 llvm::Instruction
*isActive
= Builder
.CreateUnreachable();
4118 args
.addArgCleanupDeactivation(cleanup
, isActive
);
4122 static bool isProvablyNull(llvm::Value
*addr
) {
4123 return llvm::isa_and_nonnull
<llvm::ConstantPointerNull
>(addr
);
4126 static bool isProvablyNonNull(Address Addr
, CodeGenFunction
&CGF
) {
4127 return llvm::isKnownNonZero(Addr
.getBasePointer(), /*Depth=*/0,
4128 CGF
.CGM
.getDataLayout());
4131 /// Emit the actual writing-back of a writeback.
4132 static void emitWriteback(CodeGenFunction
&CGF
,
4133 const CallArgList::Writeback
&writeback
) {
4134 const LValue
&srcLV
= writeback
.Source
;
4135 Address srcAddr
= srcLV
.getAddress(CGF
);
4136 assert(!isProvablyNull(srcAddr
.getBasePointer()) &&
4137 "shouldn't have writeback for provably null argument");
4139 llvm::BasicBlock
*contBB
= nullptr;
4141 // If the argument wasn't provably non-null, we need to null check
4142 // before doing the store.
4143 bool provablyNonNull
= isProvablyNonNull(srcAddr
, CGF
);
4145 if (!provablyNonNull
) {
4146 llvm::BasicBlock
*writebackBB
= CGF
.createBasicBlock("icr.writeback");
4147 contBB
= CGF
.createBasicBlock("icr.done");
4149 llvm::Value
*isNull
= CGF
.Builder
.CreateIsNull(srcAddr
, "icr.isnull");
4150 CGF
.Builder
.CreateCondBr(isNull
, contBB
, writebackBB
);
4151 CGF
.EmitBlock(writebackBB
);
4154 // Load the value to writeback.
4155 llvm::Value
*value
= CGF
.Builder
.CreateLoad(writeback
.Temporary
);
4157 // Cast it back, in case we're writing an id to a Foo* or something.
4158 value
= CGF
.Builder
.CreateBitCast(value
, srcAddr
.getElementType(),
4159 "icr.writeback-cast");
4161 // Perform the writeback.
4163 // If we have a "to use" value, it's something we need to emit a use
4164 // of. This has to be carefully threaded in: if it's done after the
4165 // release it's potentially undefined behavior (and the optimizer
4166 // will ignore it), and if it happens before the retain then the
4167 // optimizer could move the release there.
4168 if (writeback
.ToUse
) {
4169 assert(srcLV
.getObjCLifetime() == Qualifiers::OCL_Strong
);
4171 // Retain the new value. No need to block-copy here: the block's
4172 // being passed up the stack.
4173 value
= CGF
.EmitARCRetainNonBlock(value
);
4175 // Emit the intrinsic use here.
4176 CGF
.EmitARCIntrinsicUse(writeback
.ToUse
);
4178 // Load the old value (primitively).
4179 llvm::Value
*oldValue
= CGF
.EmitLoadOfScalar(srcLV
, SourceLocation());
4181 // Put the new value in place (primitively).
4182 CGF
.EmitStoreOfScalar(value
, srcLV
, /*init*/ false);
4184 // Release the old value.
4185 CGF
.EmitARCRelease(oldValue
, srcLV
.isARCPreciseLifetime());
4187 // Otherwise, we can just do a normal lvalue store.
4189 CGF
.EmitStoreThroughLValue(RValue::get(value
), srcLV
);
4192 // Jump to the continuation block.
4193 if (!provablyNonNull
)
4194 CGF
.EmitBlock(contBB
);
4197 static void emitWritebacks(CodeGenFunction
&CGF
,
4198 const CallArgList
&args
) {
4199 for (const auto &I
: args
.writebacks())
4200 emitWriteback(CGF
, I
);
4203 static void deactivateArgCleanupsBeforeCall(CodeGenFunction
&CGF
,
4204 const CallArgList
&CallArgs
) {
4205 ArrayRef
<CallArgList::CallArgCleanup
> Cleanups
=
4206 CallArgs
.getCleanupsToDeactivate();
4207 // Iterate in reverse to increase the likelihood of popping the cleanup.
4208 for (const auto &I
: llvm::reverse(Cleanups
)) {
4209 CGF
.DeactivateCleanupBlock(I
.Cleanup
, I
.IsActiveIP
);
4210 I
.IsActiveIP
->eraseFromParent();
4214 static const Expr
*maybeGetUnaryAddrOfOperand(const Expr
*E
) {
4215 if (const UnaryOperator
*uop
= dyn_cast
<UnaryOperator
>(E
->IgnoreParens()))
4216 if (uop
->getOpcode() == UO_AddrOf
)
4217 return uop
->getSubExpr();
4221 /// Emit an argument that's being passed call-by-writeback. That is,
4222 /// we are passing the address of an __autoreleased temporary; it
4223 /// might be copy-initialized with the current value of the given
4224 /// address, but it will definitely be copied out of after the call.
4225 static void emitWritebackArg(CodeGenFunction
&CGF
, CallArgList
&args
,
4226 const ObjCIndirectCopyRestoreExpr
*CRE
) {
4229 // Make an optimistic effort to emit the address as an l-value.
4230 // This can fail if the argument expression is more complicated.
4231 if (const Expr
*lvExpr
= maybeGetUnaryAddrOfOperand(CRE
->getSubExpr())) {
4232 srcLV
= CGF
.EmitLValue(lvExpr
);
4234 // Otherwise, just emit it as a scalar.
4236 Address srcAddr
= CGF
.EmitPointerWithAlignment(CRE
->getSubExpr());
4238 QualType srcAddrType
=
4239 CRE
->getSubExpr()->getType()->castAs
<PointerType
>()->getPointeeType();
4240 srcLV
= CGF
.MakeAddrLValue(srcAddr
, srcAddrType
);
4242 Address srcAddr
= srcLV
.getAddress(CGF
);
4244 // The dest and src types don't necessarily match in LLVM terms
4245 // because of the crazy ObjC compatibility rules.
4247 llvm::PointerType
*destType
=
4248 cast
<llvm::PointerType
>(CGF
.ConvertType(CRE
->getType()));
4249 llvm::Type
*destElemType
=
4250 CGF
.ConvertTypeForMem(CRE
->getType()->getPointeeType());
4252 // If the address is a constant null, just pass the appropriate null.
4253 if (isProvablyNull(srcAddr
.getBasePointer())) {
4254 args
.add(RValue::get(llvm::ConstantPointerNull::get(destType
)),
4259 // Create the temporary.
4261 CGF
.CreateTempAlloca(destElemType
, CGF
.getPointerAlign(), "icr.temp");
4262 // Loading an l-value can introduce a cleanup if the l-value is __weak,
4263 // and that cleanup will be conditional if we can't prove that the l-value
4264 // isn't null, so we need to register a dominating point so that the cleanups
4265 // system will make valid IR.
4266 CodeGenFunction::ConditionalEvaluation
condEval(CGF
);
4268 // Zero-initialize it if we're not doing a copy-initialization.
4269 bool shouldCopy
= CRE
->shouldCopy();
4272 llvm::ConstantPointerNull::get(cast
<llvm::PointerType
>(destElemType
));
4273 CGF
.Builder
.CreateStore(null
, temp
);
4276 llvm::BasicBlock
*contBB
= nullptr;
4277 llvm::BasicBlock
*originBB
= nullptr;
4279 // If the address is *not* known to be non-null, we need to switch.
4280 llvm::Value
*finalArgument
;
4282 bool provablyNonNull
= isProvablyNonNull(srcAddr
, CGF
);
4284 if (provablyNonNull
) {
4285 finalArgument
= temp
.emitRawPointer(CGF
);
4287 llvm::Value
*isNull
= CGF
.Builder
.CreateIsNull(srcAddr
, "icr.isnull");
4289 finalArgument
= CGF
.Builder
.CreateSelect(
4290 isNull
, llvm::ConstantPointerNull::get(destType
),
4291 temp
.emitRawPointer(CGF
), "icr.argument");
4293 // If we need to copy, then the load has to be conditional, which
4294 // means we need control flow.
4296 originBB
= CGF
.Builder
.GetInsertBlock();
4297 contBB
= CGF
.createBasicBlock("icr.cont");
4298 llvm::BasicBlock
*copyBB
= CGF
.createBasicBlock("icr.copy");
4299 CGF
.Builder
.CreateCondBr(isNull
, contBB
, copyBB
);
4300 CGF
.EmitBlock(copyBB
);
4301 condEval
.begin(CGF
);
4305 llvm::Value
*valueToUse
= nullptr;
4307 // Perform a copy if necessary.
4309 RValue srcRV
= CGF
.EmitLoadOfLValue(srcLV
, SourceLocation());
4310 assert(srcRV
.isScalar());
4312 llvm::Value
*src
= srcRV
.getScalarVal();
4313 src
= CGF
.Builder
.CreateBitCast(src
, destElemType
, "icr.cast");
4315 // Use an ordinary store, not a store-to-lvalue.
4316 CGF
.Builder
.CreateStore(src
, temp
);
4318 // If optimization is enabled, and the value was held in a
4319 // __strong variable, we need to tell the optimizer that this
4320 // value has to stay alive until we're doing the store back.
4321 // This is because the temporary is effectively unretained,
4322 // and so otherwise we can violate the high-level semantics.
4323 if (CGF
.CGM
.getCodeGenOpts().OptimizationLevel
!= 0 &&
4324 srcLV
.getObjCLifetime() == Qualifiers::OCL_Strong
) {
4329 // Finish the control flow if we needed it.
4330 if (shouldCopy
&& !provablyNonNull
) {
4331 llvm::BasicBlock
*copyBB
= CGF
.Builder
.GetInsertBlock();
4332 CGF
.EmitBlock(contBB
);
4334 // Make a phi for the value to intrinsically use.
4336 llvm::PHINode
*phiToUse
= CGF
.Builder
.CreatePHI(valueToUse
->getType(), 2,
4338 phiToUse
->addIncoming(valueToUse
, copyBB
);
4339 phiToUse
->addIncoming(llvm::UndefValue::get(valueToUse
->getType()),
4341 valueToUse
= phiToUse
;
4347 args
.addWriteback(srcLV
, temp
, valueToUse
);
4348 args
.add(RValue::get(finalArgument
), CRE
->getType());
4351 void CallArgList::allocateArgumentMemory(CodeGenFunction
&CGF
) {
4355 StackBase
= CGF
.Builder
.CreateStackSave("inalloca.save");
4358 void CallArgList::freeArgumentMemory(CodeGenFunction
&CGF
) const {
4360 // Restore the stack after the call.
4361 CGF
.Builder
.CreateStackRestore(StackBase
);
4365 void CodeGenFunction::EmitNonNullArgCheck(RValue RV
, QualType ArgType
,
4366 SourceLocation ArgLoc
,
4369 if (!AC
.getDecl() || !(SanOpts
.has(SanitizerKind::NonnullAttribute
) ||
4370 SanOpts
.has(SanitizerKind::NullabilityArg
)))
4373 // The param decl may be missing in a variadic function.
4374 auto PVD
= ParmNum
< AC
.getNumParams() ? AC
.getParamDecl(ParmNum
) : nullptr;
4375 unsigned ArgNo
= PVD
? PVD
->getFunctionScopeIndex() : ParmNum
;
4377 // Prefer the nonnull attribute if it's present.
4378 const NonNullAttr
*NNAttr
= nullptr;
4379 if (SanOpts
.has(SanitizerKind::NonnullAttribute
))
4380 NNAttr
= getNonNullAttr(AC
.getDecl(), PVD
, ArgType
, ArgNo
);
4382 bool CanCheckNullability
= false;
4383 if (SanOpts
.has(SanitizerKind::NullabilityArg
) && !NNAttr
&& PVD
&&
4384 !PVD
->getType()->isRecordType()) {
4385 auto Nullability
= PVD
->getType()->getNullability();
4386 CanCheckNullability
= Nullability
&&
4387 *Nullability
== NullabilityKind::NonNull
&&
4388 PVD
->getTypeSourceInfo();
4391 if (!NNAttr
&& !CanCheckNullability
)
4394 SourceLocation AttrLoc
;
4395 SanitizerMask CheckKind
;
4396 SanitizerHandler Handler
;
4398 AttrLoc
= NNAttr
->getLocation();
4399 CheckKind
= SanitizerKind::NonnullAttribute
;
4400 Handler
= SanitizerHandler::NonnullArg
;
4402 AttrLoc
= PVD
->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
4403 CheckKind
= SanitizerKind::NullabilityArg
;
4404 Handler
= SanitizerHandler::NullabilityArg
;
4407 SanitizerScope
SanScope(this);
4408 llvm::Value
*Cond
= EmitNonNullRValueCheck(RV
, ArgType
);
4409 llvm::Constant
*StaticData
[] = {
4410 EmitCheckSourceLocation(ArgLoc
), EmitCheckSourceLocation(AttrLoc
),
4411 llvm::ConstantInt::get(Int32Ty
, ArgNo
+ 1),
4413 EmitCheck(std::make_pair(Cond
, CheckKind
), Handler
, StaticData
, std::nullopt
);
4416 void CodeGenFunction::EmitNonNullArgCheck(Address Addr
, QualType ArgType
,
4417 SourceLocation ArgLoc
,
4418 AbstractCallee AC
, unsigned ParmNum
) {
4419 if (!AC
.getDecl() || !(SanOpts
.has(SanitizerKind::NonnullAttribute
) ||
4420 SanOpts
.has(SanitizerKind::NullabilityArg
)))
4423 EmitNonNullArgCheck(RValue::get(Addr
, *this), ArgType
, ArgLoc
, AC
, ParmNum
);
4426 // Check if the call is going to use the inalloca convention. This needs to
4427 // agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged
4428 // later, so we can't check it directly.
4429 static bool hasInAllocaArgs(CodeGenModule
&CGM
, CallingConv ExplicitCC
,
4430 ArrayRef
<QualType
> ArgTypes
) {
4431 // The Swift calling conventions don't go through the target-specific
4432 // argument classification, they never use inalloca.
4433 // TODO: Consider limiting inalloca use to only calling conventions supported
4435 if (ExplicitCC
== CC_Swift
|| ExplicitCC
== CC_SwiftAsync
)
4437 if (!CGM
.getTarget().getCXXABI().isMicrosoft())
4439 return llvm::any_of(ArgTypes
, [&](QualType Ty
) {
4440 return isInAllocaArgument(CGM
.getCXXABI(), Ty
);
4445 // Determine whether the given argument is an Objective-C method
4446 // that may have type parameters in its signature.
4447 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl
*method
) {
4448 const DeclContext
*dc
= method
->getDeclContext();
4449 if (const ObjCInterfaceDecl
*classDecl
= dyn_cast
<ObjCInterfaceDecl
>(dc
)) {
4450 return classDecl
->getTypeParamListAsWritten();
4453 if (const ObjCCategoryDecl
*catDecl
= dyn_cast
<ObjCCategoryDecl
>(dc
)) {
4454 return catDecl
->getTypeParamList();
4461 /// EmitCallArgs - Emit call arguments for a function.
4462 void CodeGenFunction::EmitCallArgs(
4463 CallArgList
&Args
, PrototypeWrapper Prototype
,
4464 llvm::iterator_range
<CallExpr::const_arg_iterator
> ArgRange
,
4465 AbstractCallee AC
, unsigned ParamsToSkip
, EvaluationOrder Order
) {
4466 SmallVector
<QualType
, 16> ArgTypes
;
4468 assert((ParamsToSkip
== 0 || Prototype
.P
) &&
4469 "Can't skip parameters if type info is not provided");
4471 // This variable only captures *explicitly* written conventions, not those
4472 // applied by default via command line flags or target defaults, such as
4473 // thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would
4474 // require knowing if this is a C++ instance method or being able to see
4475 // unprototyped FunctionTypes.
4476 CallingConv ExplicitCC
= CC_C
;
4478 // First, if a prototype was provided, use those argument types.
4479 bool IsVariadic
= false;
4481 const auto *MD
= Prototype
.P
.dyn_cast
<const ObjCMethodDecl
*>();
4483 IsVariadic
= MD
->isVariadic();
4484 ExplicitCC
= getCallingConventionForDecl(
4485 MD
, CGM
.getTarget().getTriple().isOSWindows());
4486 ArgTypes
.assign(MD
->param_type_begin() + ParamsToSkip
,
4487 MD
->param_type_end());
4489 const auto *FPT
= Prototype
.P
.get
<const FunctionProtoType
*>();
4490 IsVariadic
= FPT
->isVariadic();
4491 ExplicitCC
= FPT
->getExtInfo().getCC();
4492 ArgTypes
.assign(FPT
->param_type_begin() + ParamsToSkip
,
4493 FPT
->param_type_end());
4497 // Check that the prototyped types match the argument expression types.
4498 bool isGenericMethod
= MD
&& isObjCMethodWithTypeParams(MD
);
4499 CallExpr::const_arg_iterator Arg
= ArgRange
.begin();
4500 for (QualType Ty
: ArgTypes
) {
4501 assert(Arg
!= ArgRange
.end() && "Running over edge of argument list!");
4503 (isGenericMethod
|| Ty
->isVariablyModifiedType() ||
4504 Ty
.getNonReferenceType()->isObjCRetainableType() ||
4506 .getCanonicalType(Ty
.getNonReferenceType())
4508 getContext().getCanonicalType((*Arg
)->getType()).getTypePtr()) &&
4509 "type mismatch in call argument!");
4513 // Either we've emitted all the call args, or we have a call to variadic
4515 assert((Arg
== ArgRange
.end() || IsVariadic
) &&
4516 "Extra arguments in non-variadic function!");
4520 // If we still have any arguments, emit them using the type of the argument.
4521 for (auto *A
: llvm::drop_begin(ArgRange
, ArgTypes
.size()))
4522 ArgTypes
.push_back(IsVariadic
? getVarArgType(A
) : A
->getType());
4523 assert((int)ArgTypes
.size() == (ArgRange
.end() - ArgRange
.begin()));
4525 // We must evaluate arguments from right to left in the MS C++ ABI,
4526 // because arguments are destroyed left to right in the callee. As a special
4527 // case, there are certain language constructs that require left-to-right
4528 // evaluation, and in those cases we consider the evaluation order requirement
4529 // to trump the "destruction order is reverse construction order" guarantee.
4531 CGM
.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
4532 ? Order
== EvaluationOrder::ForceLeftToRight
4533 : Order
!= EvaluationOrder::ForceRightToLeft
;
4535 auto MaybeEmitImplicitObjectSize
= [&](unsigned I
, const Expr
*Arg
,
4536 RValue EmittedArg
) {
4537 if (!AC
.hasFunctionDecl() || I
>= AC
.getNumParams())
4539 auto *PS
= AC
.getParamDecl(I
)->getAttr
<PassObjectSizeAttr
>();
4543 const auto &Context
= getContext();
4544 auto SizeTy
= Context
.getSizeType();
4545 auto T
= Builder
.getIntNTy(Context
.getTypeSize(SizeTy
));
4546 assert(EmittedArg
.getScalarVal() && "We emitted nothing for the arg?");
4547 llvm::Value
*V
= evaluateOrEmitBuiltinObjectSize(Arg
, PS
->getType(), T
,
4548 EmittedArg
.getScalarVal(),
4550 Args
.add(RValue::get(V
), SizeTy
);
4551 // If we're emitting args in reverse, be sure to do so with
4552 // pass_object_size, as well.
4554 std::swap(Args
.back(), *(&Args
.back() - 1));
4557 // Insert a stack save if we're going to need any inalloca args.
4558 if (hasInAllocaArgs(CGM
, ExplicitCC
, ArgTypes
)) {
4559 assert(getTarget().getTriple().getArch() == llvm::Triple::x86
&&
4560 "inalloca only supported on x86");
4561 Args
.allocateArgumentMemory(*this);
4564 // Evaluate each argument in the appropriate order.
4565 size_t CallArgsStart
= Args
.size();
4566 for (unsigned I
= 0, E
= ArgTypes
.size(); I
!= E
; ++I
) {
4567 unsigned Idx
= LeftToRight
? I
: E
- I
- 1;
4568 CallExpr::const_arg_iterator Arg
= ArgRange
.begin() + Idx
;
4569 unsigned InitialArgSize
= Args
.size();
4570 // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
4571 // the argument and parameter match or the objc method is parameterized.
4572 assert((!isa
<ObjCIndirectCopyRestoreExpr
>(*Arg
) ||
4573 getContext().hasSameUnqualifiedType((*Arg
)->getType(),
4575 (isa
<ObjCMethodDecl
>(AC
.getDecl()) &&
4576 isObjCMethodWithTypeParams(cast
<ObjCMethodDecl
>(AC
.getDecl())))) &&
4577 "Argument and parameter types don't match");
4578 EmitCallArg(Args
, *Arg
, ArgTypes
[Idx
]);
4579 // In particular, we depend on it being the last arg in Args, and the
4580 // objectsize bits depend on there only being one arg if !LeftToRight.
4581 assert(InitialArgSize
+ 1 == Args
.size() &&
4582 "The code below depends on only adding one arg per EmitCallArg");
4583 (void)InitialArgSize
;
4584 // Since pointer argument are never emitted as LValue, it is safe to emit
4585 // non-null argument check for r-value only.
4586 if (!Args
.back().hasLValue()) {
4587 RValue RVArg
= Args
.back().getKnownRValue();
4588 EmitNonNullArgCheck(RVArg
, ArgTypes
[Idx
], (*Arg
)->getExprLoc(), AC
,
4589 ParamsToSkip
+ Idx
);
4590 // @llvm.objectsize should never have side-effects and shouldn't need
4591 // destruction/cleanups, so we can safely "emit" it after its arg,
4592 // regardless of right-to-leftness
4593 MaybeEmitImplicitObjectSize(Idx
, *Arg
, RVArg
);
4598 // Un-reverse the arguments we just evaluated so they match up with the LLVM
4600 std::reverse(Args
.begin() + CallArgsStart
, Args
.end());
4606 struct DestroyUnpassedArg final
: EHScopeStack::Cleanup
{
4607 DestroyUnpassedArg(Address Addr
, QualType Ty
)
4608 : Addr(Addr
), Ty(Ty
) {}
4613 void Emit(CodeGenFunction
&CGF
, Flags flags
) override
{
4614 QualType::DestructionKind DtorKind
= Ty
.isDestructedType();
4615 if (DtorKind
== QualType::DK_cxx_destructor
) {
4616 const CXXDestructorDecl
*Dtor
= Ty
->getAsCXXRecordDecl()->getDestructor();
4617 assert(!Dtor
->isTrivial());
4618 CGF
.EmitCXXDestructorCall(Dtor
, Dtor_Complete
, /*for vbase*/ false,
4619 /*Delegating=*/false, Addr
, Ty
);
4621 CGF
.callCStructDestructor(CGF
.MakeAddrLValue(Addr
, Ty
));
4626 struct DisableDebugLocationUpdates
{
4627 CodeGenFunction
&CGF
;
4628 bool disabledDebugInfo
;
4629 DisableDebugLocationUpdates(CodeGenFunction
&CGF
, const Expr
*E
) : CGF(CGF
) {
4630 if ((disabledDebugInfo
= isa
<CXXDefaultArgExpr
>(E
) && CGF
.getDebugInfo()))
4631 CGF
.disableDebugInfo();
4633 ~DisableDebugLocationUpdates() {
4634 if (disabledDebugInfo
)
4635 CGF
.enableDebugInfo();
4639 } // end anonymous namespace
4641 RValue
CallArg::getRValue(CodeGenFunction
&CGF
) const {
4644 LValue Copy
= CGF
.MakeAddrLValue(CGF
.CreateMemTemp(Ty
), Ty
);
4645 CGF
.EmitAggregateCopy(Copy
, LV
, Ty
, AggValueSlot::DoesNotOverlap
,
4648 return RValue::getAggregate(Copy
.getAddress(CGF
));
4651 void CallArg::copyInto(CodeGenFunction
&CGF
, Address Addr
) const {
4652 LValue Dst
= CGF
.MakeAddrLValue(Addr
, Ty
);
4653 if (!HasLV
&& RV
.isScalar())
4654 CGF
.EmitStoreOfScalar(RV
.getScalarVal(), Dst
, /*isInit=*/true);
4655 else if (!HasLV
&& RV
.isComplex())
4656 CGF
.EmitStoreOfComplex(RV
.getComplexVal(), Dst
, /*init=*/true);
4658 auto Addr
= HasLV
? LV
.getAddress(CGF
) : RV
.getAggregateAddress();
4659 LValue SrcLV
= CGF
.MakeAddrLValue(Addr
, Ty
);
4660 // We assume that call args are never copied into subobjects.
4661 CGF
.EmitAggregateCopy(Dst
, SrcLV
, Ty
, AggValueSlot::DoesNotOverlap
,
4662 HasLV
? LV
.isVolatileQualified()
4663 : RV
.isVolatileQualified());
4668 void CodeGenFunction::EmitCallArg(CallArgList
&args
, const Expr
*E
,
4670 DisableDebugLocationUpdates
Dis(*this, E
);
4671 if (const ObjCIndirectCopyRestoreExpr
*CRE
4672 = dyn_cast
<ObjCIndirectCopyRestoreExpr
>(E
)) {
4673 assert(getLangOpts().ObjCAutoRefCount
);
4674 return emitWritebackArg(*this, args
, CRE
);
4677 assert(type
->isReferenceType() == E
->isGLValue() &&
4678 "reference binding to unmaterialized r-value!");
4680 if (E
->isGLValue()) {
4681 assert(E
->getObjectKind() == OK_Ordinary
);
4682 return args
.add(EmitReferenceBindingToExpr(E
), type
);
4685 bool HasAggregateEvalKind
= hasAggregateEvaluationKind(type
);
4687 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
4688 // However, we still have to push an EH-only cleanup in case we unwind before
4689 // we make it to the call.
4690 if (type
->isRecordType() &&
4691 type
->castAs
<RecordType
>()->getDecl()->isParamDestroyedInCallee()) {
4692 // If we're using inalloca, use the argument memory. Otherwise, use a
4694 AggValueSlot Slot
= args
.isUsingInAlloca()
4695 ? createPlaceholderSlot(*this, type
) : CreateAggTemp(type
, "agg.tmp");
4697 bool DestroyedInCallee
= true, NeedsEHCleanup
= true;
4698 if (const auto *RD
= type
->getAsCXXRecordDecl())
4699 DestroyedInCallee
= RD
->hasNonTrivialDestructor();
4701 NeedsEHCleanup
= needsEHCleanup(type
.isDestructedType());
4703 if (DestroyedInCallee
)
4704 Slot
.setExternallyDestructed();
4706 EmitAggExpr(E
, Slot
);
4707 RValue RV
= Slot
.asRValue();
4710 if (DestroyedInCallee
&& NeedsEHCleanup
) {
4711 // Create a no-op GEP between the placeholder and the cleanup so we can
4712 // RAUW it successfully. It also serves as a marker of the first
4713 // instruction where the cleanup is active.
4714 pushFullExprCleanup
<DestroyUnpassedArg
>(EHCleanup
, Slot
.getAddress(),
4716 // This unreachable is a temporary marker which will be removed later.
4717 llvm::Instruction
*IsActive
= Builder
.CreateUnreachable();
4718 args
.addArgCleanupDeactivation(EHStack
.stable_begin(), IsActive
);
4723 if (HasAggregateEvalKind
&& isa
<ImplicitCastExpr
>(E
) &&
4724 cast
<CastExpr
>(E
)->getCastKind() == CK_LValueToRValue
&&
4725 !type
->isArrayParameterType()) {
4726 LValue L
= EmitLValue(cast
<CastExpr
>(E
)->getSubExpr());
4727 assert(L
.isSimple());
4728 args
.addUncopiedAggregate(L
, type
);
4732 args
.add(EmitAnyExprToTemp(E
), type
);
4735 QualType
CodeGenFunction::getVarArgType(const Expr
*Arg
) {
4736 // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
4737 // implicitly widens null pointer constants that are arguments to varargs
4738 // functions to pointer-sized ints.
4739 if (!getTarget().getTriple().isOSWindows())
4740 return Arg
->getType();
4742 if (Arg
->getType()->isIntegerType() &&
4743 getContext().getTypeSize(Arg
->getType()) <
4744 getContext().getTargetInfo().getPointerWidth(LangAS::Default
) &&
4745 Arg
->isNullPointerConstant(getContext(),
4746 Expr::NPC_ValueDependentIsNotNull
)) {
4747 return getContext().getIntPtrType();
4750 return Arg
->getType();
4753 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4754 // optimizer it can aggressively ignore unwind edges.
4756 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction
*Inst
) {
4757 if (CGM
.getCodeGenOpts().OptimizationLevel
!= 0 &&
4758 !CGM
.getCodeGenOpts().ObjCAutoRefCountExceptions
)
4759 Inst
->setMetadata("clang.arc.no_objc_arc_exceptions",
4760 CGM
.getNoObjCARCExceptionsMetadata());
4763 /// Emits a call to the given no-arguments nounwind runtime function.
4765 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4766 const llvm::Twine
&name
) {
4767 return EmitNounwindRuntimeCall(callee
, ArrayRef
<llvm::Value
*>(), name
);
4770 /// Emits a call to the given nounwind runtime function.
4772 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4773 ArrayRef
<Address
> args
,
4774 const llvm::Twine
&name
) {
4775 SmallVector
<llvm::Value
*, 3> values
;
4776 for (auto arg
: args
)
4777 values
.push_back(arg
.emitRawPointer(*this));
4778 return EmitNounwindRuntimeCall(callee
, values
, name
);
4782 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4783 ArrayRef
<llvm::Value
*> args
,
4784 const llvm::Twine
&name
) {
4785 llvm::CallInst
*call
= EmitRuntimeCall(callee
, args
, name
);
4786 call
->setDoesNotThrow();
4790 /// Emits a simple call (never an invoke) to the given no-arguments
4791 /// runtime function.
4792 llvm::CallInst
*CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee
,
4793 const llvm::Twine
&name
) {
4794 return EmitRuntimeCall(callee
, std::nullopt
, name
);
4797 // Calls which may throw must have operand bundles indicating which funclet
4798 // they are nested within.
4799 SmallVector
<llvm::OperandBundleDef
, 1>
4800 CodeGenFunction::getBundlesForFunclet(llvm::Value
*Callee
) {
4801 // There is no need for a funclet operand bundle if we aren't inside a
4803 if (!CurrentFuncletPad
)
4804 return (SmallVector
<llvm::OperandBundleDef
, 1>());
4806 // Skip intrinsics which cannot throw (as long as they don't lower into
4807 // regular function calls in the course of IR transformations).
4808 if (auto *CalleeFn
= dyn_cast
<llvm::Function
>(Callee
->stripPointerCasts())) {
4809 if (CalleeFn
->isIntrinsic() && CalleeFn
->doesNotThrow()) {
4810 auto IID
= CalleeFn
->getIntrinsicID();
4811 if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID
))
4812 return (SmallVector
<llvm::OperandBundleDef
, 1>());
4816 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
;
4817 BundleList
.emplace_back("funclet", CurrentFuncletPad
);
4821 /// Emits a simple call (never an invoke) to the given runtime function.
4822 llvm::CallInst
*CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee
,
4823 ArrayRef
<llvm::Value
*> args
,
4824 const llvm::Twine
&name
) {
4825 llvm::CallInst
*call
= Builder
.CreateCall(
4826 callee
, args
, getBundlesForFunclet(callee
.getCallee()), name
);
4827 call
->setCallingConv(getRuntimeCC());
4831 /// Emits a call or invoke to the given noreturn runtime function.
4832 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(
4833 llvm::FunctionCallee callee
, ArrayRef
<llvm::Value
*> args
) {
4834 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
4835 getBundlesForFunclet(callee
.getCallee());
4837 if (getInvokeDest()) {
4838 llvm::InvokeInst
*invoke
=
4839 Builder
.CreateInvoke(callee
,
4840 getUnreachableBlock(),
4844 invoke
->setDoesNotReturn();
4845 invoke
->setCallingConv(getRuntimeCC());
4847 llvm::CallInst
*call
= Builder
.CreateCall(callee
, args
, BundleList
);
4848 call
->setDoesNotReturn();
4849 call
->setCallingConv(getRuntimeCC());
4850 Builder
.CreateUnreachable();
4854 /// Emits a call or invoke instruction to the given nullary runtime function.
4856 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee
,
4857 const Twine
&name
) {
4858 return EmitRuntimeCallOrInvoke(callee
, std::nullopt
, name
);
4861 /// Emits a call or invoke instruction to the given runtime function.
4863 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee
,
4864 ArrayRef
<llvm::Value
*> args
,
4865 const Twine
&name
) {
4866 llvm::CallBase
*call
= EmitCallOrInvoke(callee
, args
, name
);
4867 call
->setCallingConv(getRuntimeCC());
4871 /// Emits a call or invoke instruction to the given function, depending
4872 /// on the current state of the EH stack.
4873 llvm::CallBase
*CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee
,
4874 ArrayRef
<llvm::Value
*> Args
,
4875 const Twine
&Name
) {
4876 llvm::BasicBlock
*InvokeDest
= getInvokeDest();
4877 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
4878 getBundlesForFunclet(Callee
.getCallee());
4880 llvm::CallBase
*Inst
;
4882 Inst
= Builder
.CreateCall(Callee
, Args
, BundleList
, Name
);
4884 llvm::BasicBlock
*ContBB
= createBasicBlock("invoke.cont");
4885 Inst
= Builder
.CreateInvoke(Callee
, ContBB
, InvokeDest
, Args
, BundleList
,
4890 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4891 // optimizer it can aggressively ignore unwind edges.
4892 if (CGM
.getLangOpts().ObjCAutoRefCount
)
4893 AddObjCARCExceptionMetadata(Inst
);
4898 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction
*Old
,
4900 DeferredReplacements
.push_back(
4901 std::make_pair(llvm::WeakTrackingVH(Old
), New
));
4906 /// Specify given \p NewAlign as the alignment of return value attribute. If
4907 /// such attribute already exists, re-set it to the maximal one of two options.
4908 [[nodiscard
]] llvm::AttributeList
4909 maybeRaiseRetAlignmentAttribute(llvm::LLVMContext
&Ctx
,
4910 const llvm::AttributeList
&Attrs
,
4911 llvm::Align NewAlign
) {
4912 llvm::Align CurAlign
= Attrs
.getRetAlignment().valueOrOne();
4913 if (CurAlign
>= NewAlign
)
4915 llvm::Attribute AlignAttr
= llvm::Attribute::getWithAlignment(Ctx
, NewAlign
);
4916 return Attrs
.removeRetAttribute(Ctx
, llvm::Attribute::AttrKind::Alignment
)
4917 .addRetAttribute(Ctx
, AlignAttr
);
4920 template <typename AlignedAttrTy
> class AbstractAssumeAlignedAttrEmitter
{
4922 CodeGenFunction
&CGF
;
4924 /// We do nothing if this is, or becomes, nullptr.
4925 const AlignedAttrTy
*AA
= nullptr;
4927 llvm::Value
*Alignment
= nullptr; // May or may not be a constant.
4928 llvm::ConstantInt
*OffsetCI
= nullptr; // Constant, hopefully zero.
4930 AbstractAssumeAlignedAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
)
4934 AA
= FuncDecl
->getAttr
<AlignedAttrTy
>();
4938 /// If we can, materialize the alignment as an attribute on return value.
4939 [[nodiscard
]] llvm::AttributeList
4940 TryEmitAsCallSiteAttribute(const llvm::AttributeList
&Attrs
) {
4941 if (!AA
|| OffsetCI
|| CGF
.SanOpts
.has(SanitizerKind::Alignment
))
4943 const auto *AlignmentCI
= dyn_cast
<llvm::ConstantInt
>(Alignment
);
4946 // We may legitimately have non-power-of-2 alignment here.
4947 // If so, this is UB land, emit it via `@llvm.assume` instead.
4948 if (!AlignmentCI
->getValue().isPowerOf2())
4950 llvm::AttributeList NewAttrs
= maybeRaiseRetAlignmentAttribute(
4951 CGF
.getLLVMContext(), Attrs
,
4953 AlignmentCI
->getLimitedValue(llvm::Value::MaximumAlignment
)));
4954 AA
= nullptr; // We're done. Disallow doing anything else.
4958 /// Emit alignment assumption.
4959 /// This is a general fallback that we take if either there is an offset,
4960 /// or the alignment is variable or we are sanitizing for alignment.
4961 void EmitAsAnAssumption(SourceLocation Loc
, QualType RetTy
, RValue
&Ret
) {
4964 CGF
.emitAlignmentAssumption(Ret
.getScalarVal(), RetTy
, Loc
,
4965 AA
->getLocation(), Alignment
, OffsetCI
);
4966 AA
= nullptr; // We're done. Disallow doing anything else.
4970 /// Helper data structure to emit `AssumeAlignedAttr`.
4971 class AssumeAlignedAttrEmitter final
4972 : public AbstractAssumeAlignedAttrEmitter
<AssumeAlignedAttr
> {
4974 AssumeAlignedAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
)
4975 : AbstractAssumeAlignedAttrEmitter(CGF_
, FuncDecl
) {
4978 // It is guaranteed that the alignment/offset are constants.
4979 Alignment
= cast
<llvm::ConstantInt
>(CGF
.EmitScalarExpr(AA
->getAlignment()));
4980 if (Expr
*Offset
= AA
->getOffset()) {
4981 OffsetCI
= cast
<llvm::ConstantInt
>(CGF
.EmitScalarExpr(Offset
));
4982 if (OffsetCI
->isNullValue()) // Canonicalize zero offset to no offset.
4988 /// Helper data structure to emit `AllocAlignAttr`.
4989 class AllocAlignAttrEmitter final
4990 : public AbstractAssumeAlignedAttrEmitter
<AllocAlignAttr
> {
4992 AllocAlignAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
,
4993 const CallArgList
&CallArgs
)
4994 : AbstractAssumeAlignedAttrEmitter(CGF_
, FuncDecl
) {
4997 // Alignment may or may not be a constant, and that is okay.
4998 Alignment
= CallArgs
[AA
->getParamIndex().getLLVMIndex()]
5006 static unsigned getMaxVectorWidth(const llvm::Type
*Ty
) {
5007 if (auto *VT
= dyn_cast
<llvm::VectorType
>(Ty
))
5008 return VT
->getPrimitiveSizeInBits().getKnownMinValue();
5009 if (auto *AT
= dyn_cast
<llvm::ArrayType
>(Ty
))
5010 return getMaxVectorWidth(AT
->getElementType());
5012 unsigned MaxVectorWidth
= 0;
5013 if (auto *ST
= dyn_cast
<llvm::StructType
>(Ty
))
5014 for (auto *I
: ST
->elements())
5015 MaxVectorWidth
= std::max(MaxVectorWidth
, getMaxVectorWidth(I
));
5016 return MaxVectorWidth
;
5019 RValue
CodeGenFunction::EmitCall(const CGFunctionInfo
&CallInfo
,
5020 const CGCallee
&Callee
,
5021 ReturnValueSlot ReturnValue
,
5022 const CallArgList
&CallArgs
,
5023 llvm::CallBase
**callOrInvoke
, bool IsMustTail
,
5024 SourceLocation Loc
) {
5025 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
5027 assert(Callee
.isOrdinary() || Callee
.isVirtual());
5029 // Handle struct-return functions by passing a pointer to the
5030 // location that we would like to return into.
5031 QualType RetTy
= CallInfo
.getReturnType();
5032 const ABIArgInfo
&RetAI
= CallInfo
.getReturnInfo();
5034 llvm::FunctionType
*IRFuncTy
= getTypes().GetFunctionType(CallInfo
);
5036 const Decl
*TargetDecl
= Callee
.getAbstractInfo().getCalleeDecl().getDecl();
5037 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
)) {
5038 // We can only guarantee that a function is called from the correct
5039 // context/function based on the appropriate target attributes,
5040 // so only check in the case where we have both always_inline and target
5041 // since otherwise we could be making a conditional call after a check for
5042 // the proper cpu features (and it won't cause code generation issues due to
5043 // function based code generation).
5044 if (TargetDecl
->hasAttr
<AlwaysInlineAttr
>() &&
5045 (TargetDecl
->hasAttr
<TargetAttr
>() ||
5046 (CurFuncDecl
&& CurFuncDecl
->hasAttr
<TargetAttr
>())))
5047 checkTargetFeatures(Loc
, FD
);
5049 // Some architectures (such as x86-64) have the ABI changed based on
5050 // attribute-target/features. Give them a chance to diagnose.
5051 CGM
.getTargetCodeGenInfo().checkFunctionCallABI(
5052 CGM
, Loc
, dyn_cast_or_null
<FunctionDecl
>(CurCodeDecl
), FD
, CallArgs
);
5055 // 1. Set up the arguments.
5057 // If we're using inalloca, insert the allocation after the stack save.
5058 // FIXME: Do this earlier rather than hacking it in here!
5059 RawAddress ArgMemory
= RawAddress::invalid();
5060 if (llvm::StructType
*ArgStruct
= CallInfo
.getArgStruct()) {
5061 const llvm::DataLayout
&DL
= CGM
.getDataLayout();
5062 llvm::Instruction
*IP
= CallArgs
.getStackBase();
5063 llvm::AllocaInst
*AI
;
5065 IP
= IP
->getNextNode();
5066 AI
= new llvm::AllocaInst(ArgStruct
, DL
.getAllocaAddrSpace(),
5069 AI
= CreateTempAlloca(ArgStruct
, "argmem");
5071 auto Align
= CallInfo
.getArgStructAlignment();
5072 AI
->setAlignment(Align
.getAsAlign());
5073 AI
->setUsedWithInAlloca(true);
5074 assert(AI
->isUsedWithInAlloca() && !AI
->isStaticAlloca());
5075 ArgMemory
= RawAddress(AI
, ArgStruct
, Align
);
5078 ClangToLLVMArgMapping
IRFunctionArgs(CGM
.getContext(), CallInfo
);
5079 SmallVector
<llvm::Value
*, 16> IRCallArgs(IRFunctionArgs
.totalIRArgs());
5081 // If the call returns a temporary with struct return, create a temporary
5082 // alloca to hold the result, unless one is given to us.
5083 Address SRetPtr
= Address::invalid();
5084 RawAddress SRetAlloca
= RawAddress::invalid();
5085 llvm::Value
*UnusedReturnSizePtr
= nullptr;
5086 if (RetAI
.isIndirect() || RetAI
.isInAlloca() || RetAI
.isCoerceAndExpand()) {
5087 if (!ReturnValue
.isNull()) {
5088 SRetPtr
= ReturnValue
.getAddress();
5090 SRetPtr
= CreateMemTemp(RetTy
, "tmp", &SRetAlloca
);
5091 if (HaveInsertPoint() && ReturnValue
.isUnused()) {
5092 llvm::TypeSize size
=
5093 CGM
.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy
));
5094 UnusedReturnSizePtr
= EmitLifetimeStart(size
, SRetAlloca
.getPointer());
5097 if (IRFunctionArgs
.hasSRetArg()) {
5098 IRCallArgs
[IRFunctionArgs
.getSRetArgNo()] =
5099 getAsNaturalPointerTo(SRetPtr
, RetTy
);
5100 } else if (RetAI
.isInAlloca()) {
5102 Builder
.CreateStructGEP(ArgMemory
, RetAI
.getInAllocaFieldIndex());
5103 Builder
.CreateStore(getAsNaturalPointerTo(SRetPtr
, RetTy
), Addr
);
5107 RawAddress swiftErrorTemp
= RawAddress::invalid();
5108 Address swiftErrorArg
= Address::invalid();
5110 // When passing arguments using temporary allocas, we need to add the
5111 // appropriate lifetime markers. This vector keeps track of all the lifetime
5112 // markers that need to be ended right after the call.
5113 SmallVector
<CallLifetimeEnd
, 2> CallLifetimeEndAfterCall
;
5115 // Translate all of the arguments as necessary to match the IR lowering.
5116 assert(CallInfo
.arg_size() == CallArgs
.size() &&
5117 "Mismatch between function signature & arguments.");
5119 CGFunctionInfo::const_arg_iterator info_it
= CallInfo
.arg_begin();
5120 for (CallArgList::const_iterator I
= CallArgs
.begin(), E
= CallArgs
.end();
5121 I
!= E
; ++I
, ++info_it
, ++ArgNo
) {
5122 const ABIArgInfo
&ArgInfo
= info_it
->info
;
5124 // Insert a padding argument to ensure proper alignment.
5125 if (IRFunctionArgs
.hasPaddingArg(ArgNo
))
5126 IRCallArgs
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
5127 llvm::UndefValue::get(ArgInfo
.getPaddingType());
5129 unsigned FirstIRArg
, NumIRArgs
;
5130 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
5132 bool ArgHasMaybeUndefAttr
=
5133 IsArgumentMaybeUndef(TargetDecl
, CallInfo
.getNumRequiredArgs(), ArgNo
);
5135 switch (ArgInfo
.getKind()) {
5136 case ABIArgInfo::InAlloca
: {
5137 assert(NumIRArgs
== 0);
5138 assert(getTarget().getTriple().getArch() == llvm::Triple::x86
);
5139 if (I
->isAggregate()) {
5140 RawAddress Addr
= I
->hasLValue()
5141 ? I
->getKnownLValue().getAddress(*this)
5142 : I
->getKnownRValue().getAggregateAddress();
5143 llvm::Instruction
*Placeholder
=
5144 cast
<llvm::Instruction
>(Addr
.getPointer());
5146 if (!ArgInfo
.getInAllocaIndirect()) {
5147 // Replace the placeholder with the appropriate argument slot GEP.
5148 CGBuilderTy::InsertPoint IP
= Builder
.saveIP();
5149 Builder
.SetInsertPoint(Placeholder
);
5150 Addr
= Builder
.CreateStructGEP(ArgMemory
,
5151 ArgInfo
.getInAllocaFieldIndex());
5152 Builder
.restoreIP(IP
);
5154 // For indirect things such as overaligned structs, replace the
5155 // placeholder with a regular aggregate temporary alloca. Store the
5156 // address of this alloca into the struct.
5157 Addr
= CreateMemTemp(info_it
->type
, "inalloca.indirect.tmp");
5158 Address ArgSlot
= Builder
.CreateStructGEP(
5159 ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5160 Builder
.CreateStore(Addr
.getPointer(), ArgSlot
);
5162 deferPlaceholderReplacement(Placeholder
, Addr
.getPointer());
5163 } else if (ArgInfo
.getInAllocaIndirect()) {
5164 // Make a temporary alloca and store the address of it into the argument
5166 RawAddress Addr
= CreateMemTempWithoutCast(
5167 I
->Ty
, getContext().getTypeAlignInChars(I
->Ty
),
5168 "indirect-arg-temp");
5169 I
->copyInto(*this, Addr
);
5171 Builder
.CreateStructGEP(ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5172 Builder
.CreateStore(Addr
.getPointer(), ArgSlot
);
5174 // Store the RValue into the argument struct.
5176 Builder
.CreateStructGEP(ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5177 Addr
= Addr
.withElementType(ConvertTypeForMem(I
->Ty
));
5178 I
->copyInto(*this, Addr
);
5183 case ABIArgInfo::Indirect
:
5184 case ABIArgInfo::IndirectAliased
: {
5185 assert(NumIRArgs
== 1);
5186 if (!I
->isAggregate()) {
5187 // Make a temporary alloca to pass the argument.
5188 RawAddress Addr
= CreateMemTempWithoutCast(
5189 I
->Ty
, ArgInfo
.getIndirectAlign(), "indirect-arg-temp");
5191 llvm::Value
*Val
= getAsNaturalPointerTo(Addr
, I
->Ty
);
5192 if (ArgHasMaybeUndefAttr
)
5193 Val
= Builder
.CreateFreeze(Val
);
5194 IRCallArgs
[FirstIRArg
] = Val
;
5196 I
->copyInto(*this, Addr
);
5198 // We want to avoid creating an unnecessary temporary+copy here;
5199 // however, we need one in three cases:
5200 // 1. If the argument is not byval, and we are required to copy the
5201 // source. (This case doesn't occur on any common architecture.)
5202 // 2. If the argument is byval, RV is not sufficiently aligned, and
5203 // we cannot force it to be sufficiently aligned.
5204 // 3. If the argument is byval, but RV is not located in default
5205 // or alloca address space.
5206 Address Addr
= I
->hasLValue()
5207 ? I
->getKnownLValue().getAddress(*this)
5208 : I
->getKnownRValue().getAggregateAddress();
5209 CharUnits Align
= ArgInfo
.getIndirectAlign();
5210 const llvm::DataLayout
*TD
= &CGM
.getDataLayout();
5212 assert((FirstIRArg
>= IRFuncTy
->getNumParams() ||
5213 IRFuncTy
->getParamType(FirstIRArg
)->getPointerAddressSpace() ==
5214 TD
->getAllocaAddrSpace()) &&
5215 "indirect argument must be in alloca address space");
5217 bool NeedCopy
= false;
5218 if (Addr
.getAlignment() < Align
&&
5219 llvm::getOrEnforceKnownAlignment(Addr
.emitRawPointer(*this),
5221 *TD
) < Align
.getAsAlign()) {
5223 } else if (I
->hasLValue()) {
5224 auto LV
= I
->getKnownLValue();
5225 auto AS
= LV
.getAddressSpace();
5228 ArgInfo
.isIndirectAliased() || ArgInfo
.getIndirectByVal();
5230 if (!isByValOrRef
||
5231 (LV
.getAlignment() < getContext().getTypeAlignInChars(I
->Ty
))) {
5234 if (!getLangOpts().OpenCL
) {
5235 if ((isByValOrRef
&&
5236 (AS
!= LangAS::Default
&&
5237 AS
!= CGM
.getASTAllocaAddressSpace()))) {
5241 // For OpenCL even if RV is located in default or alloca address space
5242 // we don't want to perform address space cast for it.
5243 else if ((isByValOrRef
&&
5244 Addr
.getType()->getAddressSpace() != IRFuncTy
->
5245 getParamType(FirstIRArg
)->getPointerAddressSpace())) {
5251 // Create an aligned temporary, and copy to it.
5252 RawAddress AI
= CreateMemTempWithoutCast(
5253 I
->Ty
, ArgInfo
.getIndirectAlign(), "byval-temp");
5254 llvm::Value
*Val
= getAsNaturalPointerTo(AI
, I
->Ty
);
5255 if (ArgHasMaybeUndefAttr
)
5256 Val
= Builder
.CreateFreeze(Val
);
5257 IRCallArgs
[FirstIRArg
] = Val
;
5259 // Emit lifetime markers for the temporary alloca.
5260 llvm::TypeSize ByvalTempElementSize
=
5261 CGM
.getDataLayout().getTypeAllocSize(AI
.getElementType());
5262 llvm::Value
*LifetimeSize
=
5263 EmitLifetimeStart(ByvalTempElementSize
, AI
.getPointer());
5265 // Add cleanup code to emit the end lifetime marker after the call.
5266 if (LifetimeSize
) // In case we disabled lifetime markers.
5267 CallLifetimeEndAfterCall
.emplace_back(AI
, LifetimeSize
);
5269 // Generate the copy.
5270 I
->copyInto(*this, AI
);
5272 // Skip the extra memcpy call.
5273 llvm::Value
*V
= getAsNaturalPointerTo(Addr
, I
->Ty
);
5274 auto *T
= llvm::PointerType::get(
5275 CGM
.getLLVMContext(), CGM
.getDataLayout().getAllocaAddrSpace());
5277 llvm::Value
*Val
= getTargetHooks().performAddrSpaceCast(
5278 *this, V
, LangAS::Default
, CGM
.getASTAllocaAddressSpace(), T
,
5280 if (ArgHasMaybeUndefAttr
)
5281 Val
= Builder
.CreateFreeze(Val
);
5282 IRCallArgs
[FirstIRArg
] = Val
;
5288 case ABIArgInfo::Ignore
:
5289 assert(NumIRArgs
== 0);
5292 case ABIArgInfo::Extend
:
5293 case ABIArgInfo::Direct
: {
5294 if (!isa
<llvm::StructType
>(ArgInfo
.getCoerceToType()) &&
5295 ArgInfo
.getCoerceToType() == ConvertType(info_it
->type
) &&
5296 ArgInfo
.getDirectOffset() == 0) {
5297 assert(NumIRArgs
== 1);
5299 if (!I
->isAggregate())
5300 V
= I
->getKnownRValue().getScalarVal();
5302 V
= Builder
.CreateLoad(
5303 I
->hasLValue() ? I
->getKnownLValue().getAddress(*this)
5304 : I
->getKnownRValue().getAggregateAddress());
5306 // Implement swifterror by copying into a new swifterror argument.
5307 // We'll write back in the normal path out of the call.
5308 if (CallInfo
.getExtParameterInfo(ArgNo
).getABI()
5309 == ParameterABI::SwiftErrorResult
) {
5310 assert(!swiftErrorTemp
.isValid() && "multiple swifterror args");
5312 QualType pointeeTy
= I
->Ty
->getPointeeType();
5313 swiftErrorArg
= makeNaturalAddressForPointer(
5314 V
, pointeeTy
, getContext().getTypeAlignInChars(pointeeTy
));
5317 CreateMemTemp(pointeeTy
, getPointerAlign(), "swifterror.temp");
5318 V
= swiftErrorTemp
.getPointer();
5319 cast
<llvm::AllocaInst
>(V
)->setSwiftError(true);
5321 llvm::Value
*errorValue
= Builder
.CreateLoad(swiftErrorArg
);
5322 Builder
.CreateStore(errorValue
, swiftErrorTemp
);
5325 // We might have to widen integers, but we should never truncate.
5326 if (ArgInfo
.getCoerceToType() != V
->getType() &&
5327 V
->getType()->isIntegerTy())
5328 V
= Builder
.CreateZExt(V
, ArgInfo
.getCoerceToType());
5330 // If the argument doesn't match, perform a bitcast to coerce it. This
5331 // can happen due to trivial type mismatches.
5332 if (FirstIRArg
< IRFuncTy
->getNumParams() &&
5333 V
->getType() != IRFuncTy
->getParamType(FirstIRArg
))
5334 V
= Builder
.CreateBitCast(V
, IRFuncTy
->getParamType(FirstIRArg
));
5336 if (ArgHasMaybeUndefAttr
)
5337 V
= Builder
.CreateFreeze(V
);
5338 IRCallArgs
[FirstIRArg
] = V
;
5342 llvm::StructType
*STy
=
5343 dyn_cast
<llvm::StructType
>(ArgInfo
.getCoerceToType());
5344 if (STy
&& ArgInfo
.isDirect() && !ArgInfo
.getCanBeFlattened()) {
5345 llvm::Type
*SrcTy
= ConvertTypeForMem(I
->Ty
);
5346 [[maybe_unused
]] llvm::TypeSize SrcTypeSize
=
5347 CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
5348 [[maybe_unused
]] llvm::TypeSize DstTypeSize
=
5349 CGM
.getDataLayout().getTypeAllocSize(STy
);
5350 if (STy
->containsHomogeneousScalableVectorTypes()) {
5351 assert(SrcTypeSize
== DstTypeSize
&&
5352 "Only allow non-fractional movement of structure with "
5353 "homogeneous scalable vector type");
5355 IRCallArgs
[FirstIRArg
] = I
->getKnownRValue().getScalarVal();
5360 // FIXME: Avoid the conversion through memory if possible.
5361 Address Src
= Address::invalid();
5362 if (!I
->isAggregate()) {
5363 Src
= CreateMemTemp(I
->Ty
, "coerce");
5364 I
->copyInto(*this, Src
);
5366 Src
= I
->hasLValue() ? I
->getKnownLValue().getAddress(*this)
5367 : I
->getKnownRValue().getAggregateAddress();
5370 // If the value is offset in memory, apply the offset now.
5371 Src
= emitAddressAtOffset(*this, Src
, ArgInfo
);
5373 // Fast-isel and the optimizer generally like scalar values better than
5374 // FCAs, so we flatten them if this is safe to do for this argument.
5375 if (STy
&& ArgInfo
.isDirect() && ArgInfo
.getCanBeFlattened()) {
5376 llvm::Type
*SrcTy
= Src
.getElementType();
5377 llvm::TypeSize SrcTypeSize
=
5378 CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
5379 llvm::TypeSize DstTypeSize
= CGM
.getDataLayout().getTypeAllocSize(STy
);
5380 if (SrcTypeSize
.isScalable()) {
5381 assert(STy
->containsHomogeneousScalableVectorTypes() &&
5382 "ABI only supports structure with homogeneous scalable vector "
5384 assert(SrcTypeSize
== DstTypeSize
&&
5385 "Only allow non-fractional movement of structure with "
5386 "homogeneous scalable vector type");
5387 assert(NumIRArgs
== STy
->getNumElements());
5389 llvm::Value
*StoredStructValue
=
5390 Builder
.CreateLoad(Src
, Src
.getName() + ".tuple");
5391 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
5392 llvm::Value
*Extract
= Builder
.CreateExtractValue(
5393 StoredStructValue
, i
, Src
.getName() + ".extract" + Twine(i
));
5394 IRCallArgs
[FirstIRArg
+ i
] = Extract
;
5397 uint64_t SrcSize
= SrcTypeSize
.getFixedValue();
5398 uint64_t DstSize
= DstTypeSize
.getFixedValue();
5400 // If the source type is smaller than the destination type of the
5401 // coerce-to logic, copy the source value into a temp alloca the size
5402 // of the destination type to allow loading all of it. The bits past
5403 // the source value are left undef.
5404 if (SrcSize
< DstSize
) {
5405 Address TempAlloca
= CreateTempAlloca(STy
, Src
.getAlignment(),
5406 Src
.getName() + ".coerce");
5407 Builder
.CreateMemCpy(TempAlloca
, Src
, SrcSize
);
5410 Src
= Src
.withElementType(STy
);
5413 assert(NumIRArgs
== STy
->getNumElements());
5414 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
5415 Address EltPtr
= Builder
.CreateStructGEP(Src
, i
);
5416 llvm::Value
*LI
= Builder
.CreateLoad(EltPtr
);
5417 if (ArgHasMaybeUndefAttr
)
5418 LI
= Builder
.CreateFreeze(LI
);
5419 IRCallArgs
[FirstIRArg
+ i
] = LI
;
5423 // In the simple case, just pass the coerced loaded value.
5424 assert(NumIRArgs
== 1);
5426 CreateCoercedLoad(Src
, ArgInfo
.getCoerceToType(), *this);
5428 if (CallInfo
.isCmseNSCall()) {
5429 // For certain parameter types, clear padding bits, as they may reveal
5430 // sensitive information.
5431 // Small struct/union types are passed as integer arrays.
5432 auto *ATy
= dyn_cast
<llvm::ArrayType
>(Load
->getType());
5433 if (ATy
!= nullptr && isa
<RecordType
>(I
->Ty
.getCanonicalType()))
5434 Load
= EmitCMSEClearRecord(Load
, ATy
, I
->Ty
);
5437 if (ArgHasMaybeUndefAttr
)
5438 Load
= Builder
.CreateFreeze(Load
);
5439 IRCallArgs
[FirstIRArg
] = Load
;
5445 case ABIArgInfo::CoerceAndExpand
: {
5446 auto coercionType
= ArgInfo
.getCoerceAndExpandType();
5447 auto layout
= CGM
.getDataLayout().getStructLayout(coercionType
);
5449 llvm::Value
*tempSize
= nullptr;
5450 Address addr
= Address::invalid();
5451 RawAddress AllocaAddr
= RawAddress::invalid();
5452 if (I
->isAggregate()) {
5453 addr
= I
->hasLValue() ? I
->getKnownLValue().getAddress(*this)
5454 : I
->getKnownRValue().getAggregateAddress();
5457 RValue RV
= I
->getKnownRValue();
5458 assert(RV
.isScalar()); // complex should always just be direct
5460 llvm::Type
*scalarType
= RV
.getScalarVal()->getType();
5461 auto scalarSize
= CGM
.getDataLayout().getTypeAllocSize(scalarType
);
5462 auto scalarAlign
= CGM
.getDataLayout().getPrefTypeAlign(scalarType
);
5464 // Materialize to a temporary.
5465 addr
= CreateTempAlloca(
5466 RV
.getScalarVal()->getType(),
5467 CharUnits::fromQuantity(std::max(layout
->getAlignment(), scalarAlign
)),
5469 /*ArraySize=*/nullptr, &AllocaAddr
);
5470 tempSize
= EmitLifetimeStart(scalarSize
, AllocaAddr
.getPointer());
5472 Builder
.CreateStore(RV
.getScalarVal(), addr
);
5475 addr
= addr
.withElementType(coercionType
);
5477 unsigned IRArgPos
= FirstIRArg
;
5478 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
5479 llvm::Type
*eltType
= coercionType
->getElementType(i
);
5480 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
)) continue;
5481 Address eltAddr
= Builder
.CreateStructGEP(addr
, i
);
5482 llvm::Value
*elt
= Builder
.CreateLoad(eltAddr
);
5483 if (ArgHasMaybeUndefAttr
)
5484 elt
= Builder
.CreateFreeze(elt
);
5485 IRCallArgs
[IRArgPos
++] = elt
;
5487 assert(IRArgPos
== FirstIRArg
+ NumIRArgs
);
5490 EmitLifetimeEnd(tempSize
, AllocaAddr
.getPointer());
5496 case ABIArgInfo::Expand
: {
5497 unsigned IRArgPos
= FirstIRArg
;
5498 ExpandTypeToArgs(I
->Ty
, *I
, IRFuncTy
, IRCallArgs
, IRArgPos
);
5499 assert(IRArgPos
== FirstIRArg
+ NumIRArgs
);
5505 const CGCallee
&ConcreteCallee
= Callee
.prepareConcreteCallee(*this);
5506 llvm::Value
*CalleePtr
= ConcreteCallee
.getFunctionPointer();
5508 // If we're using inalloca, set up that argument.
5509 if (ArgMemory
.isValid()) {
5510 llvm::Value
*Arg
= ArgMemory
.getPointer();
5511 assert(IRFunctionArgs
.hasInallocaArg());
5512 IRCallArgs
[IRFunctionArgs
.getInallocaArgNo()] = Arg
;
5515 // 2. Prepare the function pointer.
5517 // If the callee is a bitcast of a non-variadic function to have a
5518 // variadic function pointer type, check to see if we can remove the
5519 // bitcast. This comes up with unprototyped functions.
5521 // This makes the IR nicer, but more importantly it ensures that we
5522 // can inline the function at -O0 if it is marked always_inline.
5523 auto simplifyVariadicCallee
= [](llvm::FunctionType
*CalleeFT
,
5524 llvm::Value
*Ptr
) -> llvm::Function
* {
5525 if (!CalleeFT
->isVarArg())
5528 // Get underlying value if it's a bitcast
5529 if (llvm::ConstantExpr
*CE
= dyn_cast
<llvm::ConstantExpr
>(Ptr
)) {
5530 if (CE
->getOpcode() == llvm::Instruction::BitCast
)
5531 Ptr
= CE
->getOperand(0);
5534 llvm::Function
*OrigFn
= dyn_cast
<llvm::Function
>(Ptr
);
5538 llvm::FunctionType
*OrigFT
= OrigFn
->getFunctionType();
5540 // If the original type is variadic, or if any of the component types
5541 // disagree, we cannot remove the cast.
5542 if (OrigFT
->isVarArg() ||
5543 OrigFT
->getNumParams() != CalleeFT
->getNumParams() ||
5544 OrigFT
->getReturnType() != CalleeFT
->getReturnType())
5547 for (unsigned i
= 0, e
= OrigFT
->getNumParams(); i
!= e
; ++i
)
5548 if (OrigFT
->getParamType(i
) != CalleeFT
->getParamType(i
))
5554 if (llvm::Function
*OrigFn
= simplifyVariadicCallee(IRFuncTy
, CalleePtr
)) {
5556 IRFuncTy
= OrigFn
->getFunctionType();
5559 // 3. Perform the actual call.
5561 // Deactivate any cleanups that we're supposed to do immediately before
5563 if (!CallArgs
.getCleanupsToDeactivate().empty())
5564 deactivateArgCleanupsBeforeCall(*this, CallArgs
);
5566 // Assert that the arguments we computed match up. The IR verifier
5567 // will catch this, but this is a common enough source of problems
5568 // during IRGen changes that it's way better for debugging to catch
5569 // it ourselves here.
5571 assert(IRCallArgs
.size() == IRFuncTy
->getNumParams() || IRFuncTy
->isVarArg());
5572 for (unsigned i
= 0; i
< IRCallArgs
.size(); ++i
) {
5573 // Inalloca argument can have different type.
5574 if (IRFunctionArgs
.hasInallocaArg() &&
5575 i
== IRFunctionArgs
.getInallocaArgNo())
5577 if (i
< IRFuncTy
->getNumParams())
5578 assert(IRCallArgs
[i
]->getType() == IRFuncTy
->getParamType(i
));
5582 // Update the largest vector width if any arguments have vector types.
5583 for (unsigned i
= 0; i
< IRCallArgs
.size(); ++i
)
5584 LargestVectorWidth
= std::max(LargestVectorWidth
,
5585 getMaxVectorWidth(IRCallArgs
[i
]->getType()));
5587 // Compute the calling convention and attributes.
5588 unsigned CallingConv
;
5589 llvm::AttributeList Attrs
;
5590 CGM
.ConstructAttributeList(CalleePtr
->getName(), CallInfo
,
5591 Callee
.getAbstractInfo(), Attrs
, CallingConv
,
5592 /*AttrOnCallSite=*/true,
5595 if (CallingConv
== llvm::CallingConv::X86_VectorCall
&&
5596 getTarget().getTriple().isWindowsArm64EC()) {
5597 CGM
.Error(Loc
, "__vectorcall calling convention is not currently "
5601 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
)) {
5602 if (FD
->hasAttr
<StrictFPAttr
>())
5603 // All calls within a strictfp function are marked strictfp
5604 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP
);
5606 // If -ffast-math is enabled and the function is guarded by an
5607 // '__attribute__((optnone)) adjust the memory attribute so the BE emits the
5608 // library call instead of the intrinsic.
5609 if (FD
->hasAttr
<OptimizeNoneAttr
>() && getLangOpts().FastMath
)
5610 CGM
.AdjustMemoryAttribute(CalleePtr
->getName(), Callee
.getAbstractInfo(),
5613 // Add call-site nomerge attribute if exists.
5614 if (InNoMergeAttributedStmt
)
5615 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge
);
5617 // Add call-site noinline attribute if exists.
5618 if (InNoInlineAttributedStmt
)
5619 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline
);
5621 // Add call-site always_inline attribute if exists.
5622 if (InAlwaysInlineAttributedStmt
)
5624 Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline
);
5626 // Apply some call-site-specific attributes.
5627 // TODO: work this into building the attribute set.
5629 // Apply always_inline to all calls within flatten functions.
5630 // FIXME: should this really take priority over __try, below?
5631 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<FlattenAttr
>() &&
5632 !InNoInlineAttributedStmt
&&
5633 !(TargetDecl
&& TargetDecl
->hasAttr
<NoInlineAttr
>())) {
5635 Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline
);
5638 // Disable inlining inside SEH __try blocks.
5639 if (isSEHTryScope()) {
5640 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline
);
5643 // Decide whether to use a call or an invoke.
5645 if (currentFunctionUsesSEHTry()) {
5646 // SEH cares about asynchronous exceptions, so everything can "throw."
5647 CannotThrow
= false;
5648 } else if (isCleanupPadScope() &&
5649 EHPersonality::get(*this).isMSVCXXPersonality()) {
5650 // The MSVC++ personality will implicitly terminate the program if an
5651 // exception is thrown during a cleanup outside of a try/catch.
5652 // We don't need to model anything in IR to get this behavior.
5655 // Otherwise, nounwind call sites will never throw.
5656 CannotThrow
= Attrs
.hasFnAttr(llvm::Attribute::NoUnwind
);
5658 if (auto *FPtr
= dyn_cast
<llvm::Function
>(CalleePtr
))
5659 if (FPtr
->hasFnAttribute(llvm::Attribute::NoUnwind
))
5663 // If we made a temporary, be sure to clean up after ourselves. Note that we
5664 // can't depend on being inside of an ExprWithCleanups, so we need to manually
5665 // pop this cleanup later on. Being eager about this is OK, since this
5666 // temporary is 'invisible' outside of the callee.
5667 if (UnusedReturnSizePtr
)
5668 pushFullExprCleanup
<CallLifetimeEnd
>(NormalEHLifetimeMarker
, SRetAlloca
,
5669 UnusedReturnSizePtr
);
5671 llvm::BasicBlock
*InvokeDest
= CannotThrow
? nullptr : getInvokeDest();
5673 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
5674 getBundlesForFunclet(CalleePtr
);
5676 if (SanOpts
.has(SanitizerKind::KCFI
) &&
5677 !isa_and_nonnull
<FunctionDecl
>(TargetDecl
))
5678 EmitKCFIOperandBundle(ConcreteCallee
, BundleList
);
5680 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
))
5681 if (FD
->hasAttr
<StrictFPAttr
>())
5682 // All calls within a strictfp function are marked strictfp
5683 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP
);
5685 AssumeAlignedAttrEmitter
AssumeAlignedAttrEmitter(*this, TargetDecl
);
5686 Attrs
= AssumeAlignedAttrEmitter
.TryEmitAsCallSiteAttribute(Attrs
);
5688 AllocAlignAttrEmitter
AllocAlignAttrEmitter(*this, TargetDecl
, CallArgs
);
5689 Attrs
= AllocAlignAttrEmitter
.TryEmitAsCallSiteAttribute(Attrs
);
5691 // Emit the actual call/invoke instruction.
5694 CI
= Builder
.CreateCall(IRFuncTy
, CalleePtr
, IRCallArgs
, BundleList
);
5696 llvm::BasicBlock
*Cont
= createBasicBlock("invoke.cont");
5697 CI
= Builder
.CreateInvoke(IRFuncTy
, CalleePtr
, Cont
, InvokeDest
, IRCallArgs
,
5701 if (CI
->getCalledFunction() && CI
->getCalledFunction()->hasName() &&
5702 CI
->getCalledFunction()->getName().starts_with("_Z4sqrt")) {
5703 SetSqrtFPAccuracy(CI
);
5708 // If this is within a function that has the guard(nocf) attribute and is an
5709 // indirect call, add the "guard_nocf" attribute to this call to indicate that
5710 // Control Flow Guard checks should not be added, even if the call is inlined.
5711 if (const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
)) {
5712 if (const auto *A
= FD
->getAttr
<CFGuardAttr
>()) {
5713 if (A
->getGuard() == CFGuardAttr::GuardArg::nocf
&& !CI
->getCalledFunction())
5714 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), "guard_nocf");
5718 // Apply the attributes and calling convention.
5719 CI
->setAttributes(Attrs
);
5720 CI
->setCallingConv(static_cast<llvm::CallingConv::ID
>(CallingConv
));
5722 // Apply various metadata.
5724 if (!CI
->getType()->isVoidTy())
5725 CI
->setName("call");
5727 if (getTarget().getTriple().isSPIRVLogical() && CI
->isConvergent())
5728 CI
= addControlledConvergenceToken(CI
);
5730 // Update largest vector width from the return type.
5731 LargestVectorWidth
=
5732 std::max(LargestVectorWidth
, getMaxVectorWidth(CI
->getType()));
5734 // Insert instrumentation or attach profile metadata at indirect call sites.
5735 // For more details, see the comment before the definition of
5736 // IPVK_IndirectCallTarget in InstrProfData.inc.
5737 if (!CI
->getCalledFunction())
5738 PGO
.valueProfile(Builder
, llvm::IPVK_IndirectCallTarget
,
5741 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
5742 // optimizer it can aggressively ignore unwind edges.
5743 if (CGM
.getLangOpts().ObjCAutoRefCount
)
5744 AddObjCARCExceptionMetadata(CI
);
5746 // Set tail call kind if necessary.
5747 if (llvm::CallInst
*Call
= dyn_cast
<llvm::CallInst
>(CI
)) {
5748 if (TargetDecl
&& TargetDecl
->hasAttr
<NotTailCalledAttr
>())
5749 Call
->setTailCallKind(llvm::CallInst::TCK_NoTail
);
5750 else if (IsMustTail
)
5751 Call
->setTailCallKind(llvm::CallInst::TCK_MustTail
);
5754 // Add metadata for calls to MSAllocator functions
5755 if (getDebugInfo() && TargetDecl
&&
5756 TargetDecl
->hasAttr
<MSAllocatorAttr
>())
5757 getDebugInfo()->addHeapAllocSiteMetadata(CI
, RetTy
->getPointeeType(), Loc
);
5759 // Add metadata if calling an __attribute__((error(""))) or warning fn.
5760 if (TargetDecl
&& TargetDecl
->hasAttr
<ErrorAttr
>()) {
5761 llvm::ConstantInt
*Line
=
5762 llvm::ConstantInt::get(Int32Ty
, Loc
.getRawEncoding());
5763 llvm::ConstantAsMetadata
*MD
= llvm::ConstantAsMetadata::get(Line
);
5764 llvm::MDTuple
*MDT
= llvm::MDNode::get(getLLVMContext(), {MD
});
5765 CI
->setMetadata("srcloc", MDT
);
5768 // 4. Finish the call.
5770 // If the call doesn't return, finish the basic block and clear the
5771 // insertion point; this allows the rest of IRGen to discard
5772 // unreachable code.
5773 if (CI
->doesNotReturn()) {
5774 if (UnusedReturnSizePtr
)
5777 // Strip away the noreturn attribute to better diagnose unreachable UB.
5778 if (SanOpts
.has(SanitizerKind::Unreachable
)) {
5779 // Also remove from function since CallBase::hasFnAttr additionally checks
5780 // attributes of the called function.
5781 if (auto *F
= CI
->getCalledFunction())
5782 F
->removeFnAttr(llvm::Attribute::NoReturn
);
5783 CI
->removeFnAttr(llvm::Attribute::NoReturn
);
5785 // Avoid incompatibility with ASan which relies on the `noreturn`
5786 // attribute to insert handler calls.
5787 if (SanOpts
.hasOneOf(SanitizerKind::Address
|
5788 SanitizerKind::KernelAddress
)) {
5789 SanitizerScope
SanScope(this);
5790 llvm::IRBuilder
<>::InsertPointGuard
IPGuard(Builder
);
5791 Builder
.SetInsertPoint(CI
);
5792 auto *FnType
= llvm::FunctionType::get(CGM
.VoidTy
, /*isVarArg=*/false);
5793 llvm::FunctionCallee Fn
=
5794 CGM
.CreateRuntimeFunction(FnType
, "__asan_handle_no_return");
5795 EmitNounwindRuntimeCall(Fn
);
5799 EmitUnreachable(Loc
);
5800 Builder
.ClearInsertionPoint();
5802 // FIXME: For now, emit a dummy basic block because expr emitters in
5803 // generally are not ready to handle emitting expressions at unreachable
5805 EnsureInsertPoint();
5807 // Return a reasonable RValue.
5808 return GetUndefRValue(RetTy
);
5811 // If this is a musttail call, return immediately. We do not branch to the
5812 // epilogue in this case.
5814 for (auto it
= EHStack
.find(CurrentCleanupScopeDepth
); it
!= EHStack
.end();
5816 EHCleanupScope
*Cleanup
= dyn_cast
<EHCleanupScope
>(&*it
);
5817 if (!(Cleanup
&& Cleanup
->getCleanup()->isRedundantBeforeReturn()))
5818 CGM
.ErrorUnsupported(MustTailCall
, "tail call skipping over cleanups");
5820 if (CI
->getType()->isVoidTy())
5821 Builder
.CreateRetVoid();
5823 Builder
.CreateRet(CI
);
5824 Builder
.ClearInsertionPoint();
5825 EnsureInsertPoint();
5826 return GetUndefRValue(RetTy
);
5829 // Perform the swifterror writeback.
5830 if (swiftErrorTemp
.isValid()) {
5831 llvm::Value
*errorResult
= Builder
.CreateLoad(swiftErrorTemp
);
5832 Builder
.CreateStore(errorResult
, swiftErrorArg
);
5835 // Emit any call-associated writebacks immediately. Arguably this
5836 // should happen after any return-value munging.
5837 if (CallArgs
.hasWritebacks())
5838 emitWritebacks(*this, CallArgs
);
5840 // The stack cleanup for inalloca arguments has to run out of the normal
5841 // lexical order, so deactivate it and run it manually here.
5842 CallArgs
.freeArgumentMemory(*this);
5844 // Extract the return value.
5846 switch (RetAI
.getKind()) {
5847 case ABIArgInfo::CoerceAndExpand
: {
5848 auto coercionType
= RetAI
.getCoerceAndExpandType();
5850 Address addr
= SRetPtr
.withElementType(coercionType
);
5852 assert(CI
->getType() == RetAI
.getUnpaddedCoerceAndExpandType());
5853 bool requiresExtract
= isa
<llvm::StructType
>(CI
->getType());
5855 unsigned unpaddedIndex
= 0;
5856 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
5857 llvm::Type
*eltType
= coercionType
->getElementType(i
);
5858 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
)) continue;
5859 Address eltAddr
= Builder
.CreateStructGEP(addr
, i
);
5860 llvm::Value
*elt
= CI
;
5861 if (requiresExtract
)
5862 elt
= Builder
.CreateExtractValue(elt
, unpaddedIndex
++);
5864 assert(unpaddedIndex
== 0);
5865 Builder
.CreateStore(elt
, eltAddr
);
5870 case ABIArgInfo::InAlloca
:
5871 case ABIArgInfo::Indirect
: {
5872 RValue ret
= convertTempToRValue(SRetPtr
, RetTy
, SourceLocation());
5873 if (UnusedReturnSizePtr
)
5878 case ABIArgInfo::Ignore
:
5879 // If we are ignoring an argument that had a result, make sure to
5880 // construct the appropriate return value for our caller.
5881 return GetUndefRValue(RetTy
);
5883 case ABIArgInfo::Extend
:
5884 case ABIArgInfo::Direct
: {
5885 llvm::Type
*RetIRTy
= ConvertType(RetTy
);
5886 if (RetAI
.getCoerceToType() == RetIRTy
&& RetAI
.getDirectOffset() == 0) {
5887 switch (getEvaluationKind(RetTy
)) {
5889 llvm::Value
*Real
= Builder
.CreateExtractValue(CI
, 0);
5890 llvm::Value
*Imag
= Builder
.CreateExtractValue(CI
, 1);
5891 return RValue::getComplex(std::make_pair(Real
, Imag
));
5893 case TEK_Aggregate
: {
5894 Address DestPtr
= ReturnValue
.getAddress();
5895 bool DestIsVolatile
= ReturnValue
.isVolatile();
5897 if (!DestPtr
.isValid()) {
5898 DestPtr
= CreateMemTemp(RetTy
, "agg.tmp");
5899 DestIsVolatile
= false;
5901 EmitAggregateStore(CI
, DestPtr
, DestIsVolatile
);
5902 return RValue::getAggregate(DestPtr
);
5905 // If the argument doesn't match, perform a bitcast to coerce it. This
5906 // can happen due to trivial type mismatches.
5907 llvm::Value
*V
= CI
;
5908 if (V
->getType() != RetIRTy
)
5909 V
= Builder
.CreateBitCast(V
, RetIRTy
);
5910 return RValue::get(V
);
5913 llvm_unreachable("bad evaluation kind");
5916 // If coercing a fixed vector from a scalable vector for ABI
5917 // compatibility, and the types match, use the llvm.vector.extract
5918 // intrinsic to perform the conversion.
5919 if (auto *FixedDstTy
= dyn_cast
<llvm::FixedVectorType
>(RetIRTy
)) {
5920 llvm::Value
*V
= CI
;
5921 if (auto *ScalableSrcTy
=
5922 dyn_cast
<llvm::ScalableVectorType
>(V
->getType())) {
5923 if (FixedDstTy
->getElementType() == ScalableSrcTy
->getElementType()) {
5924 llvm::Value
*Zero
= llvm::Constant::getNullValue(CGM
.Int64Ty
);
5925 V
= Builder
.CreateExtractVector(FixedDstTy
, V
, Zero
, "cast.fixed");
5926 return RValue::get(V
);
5931 Address DestPtr
= ReturnValue
.getValue();
5932 bool DestIsVolatile
= ReturnValue
.isVolatile();
5934 if (!DestPtr
.isValid()) {
5935 DestPtr
= CreateMemTemp(RetTy
, "coerce");
5936 DestIsVolatile
= false;
5939 // An empty record can overlap other data (if declared with
5940 // no_unique_address); omit the store for such types - as there is no
5941 // actual data to store.
5942 if (!isEmptyRecord(getContext(), RetTy
, true)) {
5943 // If the value is offset in memory, apply the offset now.
5944 Address StorePtr
= emitAddressAtOffset(*this, DestPtr
, RetAI
);
5945 CreateCoercedStore(CI
, StorePtr
, DestIsVolatile
, *this);
5948 return convertTempToRValue(DestPtr
, RetTy
, SourceLocation());
5951 case ABIArgInfo::Expand
:
5952 case ABIArgInfo::IndirectAliased
:
5953 llvm_unreachable("Invalid ABI kind for return argument");
5956 llvm_unreachable("Unhandled ABIArgInfo::Kind");
5959 // Emit the assume_aligned check on the return value.
5960 if (Ret
.isScalar() && TargetDecl
) {
5961 AssumeAlignedAttrEmitter
.EmitAsAnAssumption(Loc
, RetTy
, Ret
);
5962 AllocAlignAttrEmitter
.EmitAsAnAssumption(Loc
, RetTy
, Ret
);
5965 // Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
5966 // we can't use the full cleanup mechanism.
5967 for (CallLifetimeEnd
&LifetimeEnd
: CallLifetimeEndAfterCall
)
5968 LifetimeEnd
.Emit(*this, /*Flags=*/{});
5970 if (!ReturnValue
.isExternallyDestructed() &&
5971 RetTy
.isDestructedType() == QualType::DK_nontrivial_c_struct
)
5972 pushDestroy(QualType::DK_nontrivial_c_struct
, Ret
.getAggregateAddress(),
5978 CGCallee
CGCallee::prepareConcreteCallee(CodeGenFunction
&CGF
) const {
5980 const CallExpr
*CE
= getVirtualCallExpr();
5981 return CGF
.CGM
.getCXXABI().getVirtualFunctionPointer(
5982 CGF
, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),
5983 CE
? CE
->getBeginLoc() : SourceLocation());
5989 /* VarArg handling */
5991 Address
CodeGenFunction::EmitVAArg(VAArgExpr
*VE
, Address
&VAListAddr
) {
5992 VAListAddr
= VE
->isMicrosoftABI()
5993 ? EmitMSVAListRef(VE
->getSubExpr())
5994 : EmitVAListRef(VE
->getSubExpr());
5995 QualType Ty
= VE
->getType();
5996 if (VE
->isMicrosoftABI())
5997 return CGM
.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr
, Ty
);
5998 return CGM
.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr
, Ty
);