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
=
318 getCXXABI().getThisArgumentTypeForMethod(MD
);
319 return arrangeCXXMethodType(ThisType
, prototype
.getTypePtr(), MD
);
322 return arrangeFreeFunctionType(prototype
);
325 bool CodeGenTypes::inheritingCtorHasParams(
326 const InheritedConstructor
&Inherited
, CXXCtorType Type
) {
327 // Parameters are unnecessary if we're constructing a base class subobject
328 // and the inherited constructor lives in a virtual base.
329 return Type
== Ctor_Complete
||
330 !Inherited
.getShadowDecl()->constructsVirtualBase() ||
331 !Target
.getCXXABI().hasConstructorVariants();
334 const CGFunctionInfo
&
335 CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD
) {
336 auto *MD
= cast
<CXXMethodDecl
>(GD
.getDecl());
338 SmallVector
<CanQualType
, 16> argTypes
;
339 SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
341 const CXXRecordDecl
*ThisType
= getCXXABI().getThisArgumentTypeForMethod(GD
);
342 argTypes
.push_back(DeriveThisType(ThisType
, MD
));
344 bool PassParams
= true;
346 if (auto *CD
= dyn_cast
<CXXConstructorDecl
>(MD
)) {
347 // A base class inheriting constructor doesn't get forwarded arguments
348 // needed to construct a virtual base (or base class thereof).
349 if (auto Inherited
= CD
->getInheritedConstructor())
350 PassParams
= inheritingCtorHasParams(Inherited
, GD
.getCtorType());
353 CanQual
<FunctionProtoType
> FTP
= GetFormalType(MD
);
355 // Add the formal parameters.
357 appendParameterTypes(*this, argTypes
, paramInfos
, FTP
);
359 CGCXXABI::AddedStructorArgCounts AddedArgs
=
360 getCXXABI().buildStructorSignature(GD
, argTypes
);
361 if (!paramInfos
.empty()) {
362 // Note: prefix implies after the first param.
363 if (AddedArgs
.Prefix
)
364 paramInfos
.insert(paramInfos
.begin() + 1, AddedArgs
.Prefix
,
365 FunctionProtoType::ExtParameterInfo
{});
366 if (AddedArgs
.Suffix
)
367 paramInfos
.append(AddedArgs
.Suffix
,
368 FunctionProtoType::ExtParameterInfo
{});
371 RequiredArgs required
=
372 (PassParams
&& MD
->isVariadic() ? RequiredArgs(argTypes
.size())
373 : RequiredArgs::All
);
375 FunctionType::ExtInfo extInfo
= FTP
->getExtInfo();
376 CanQualType resultType
= getCXXABI().HasThisReturn(GD
) ? argTypes
.front()
377 : getCXXABI().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
= getCXXABI().HasThisReturn(GD
) ? ArgTypes
.front()
441 : getCXXABI().hasMostDerivedReturn(GD
)
442 ? CGM
.getContext().VoidPtrTy
445 FunctionType::ExtInfo Info
= FPT
->getExtInfo();
446 llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> ParamInfos
;
447 // If the prototype args are elided, we should only have ABI-specific args,
448 // which never have param info.
449 if (PassProtoArgs
&& FPT
->hasExtParameterInfos()) {
450 // ABI-specific suffix arguments are treated the same as variadic arguments.
451 addExtParameterInfosForCall(ParamInfos
, FPT
.getTypePtr(), TotalPrefixArgs
,
455 return arrangeLLVMFunctionInfo(ResultType
, FnInfoOpts::IsInstanceMethod
,
456 ArgTypes
, Info
, ParamInfos
, Required
);
459 /// Arrange the argument and result information for the declaration or
460 /// definition of the given function.
461 const CGFunctionInfo
&
462 CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl
*FD
) {
463 if (const CXXMethodDecl
*MD
= dyn_cast
<CXXMethodDecl
>(FD
))
464 if (MD
->isImplicitObjectMemberFunction())
465 return arrangeCXXMethodDeclaration(MD
);
467 CanQualType FTy
= FD
->getType()->getCanonicalTypeUnqualified();
469 assert(isa
<FunctionType
>(FTy
));
470 setCUDAKernelCallingConvention(FTy
, CGM
, FD
);
472 // When declaring a function without a prototype, always use a
473 // non-variadic type.
474 if (CanQual
<FunctionNoProtoType
> noProto
= FTy
.getAs
<FunctionNoProtoType
>()) {
475 return arrangeLLVMFunctionInfo(noProto
->getReturnType(), FnInfoOpts::None
,
476 std::nullopt
, noProto
->getExtInfo(), {},
480 return arrangeFreeFunctionType(FTy
.castAs
<FunctionProtoType
>());
483 /// Arrange the argument and result information for the declaration or
484 /// definition of an Objective-C method.
485 const CGFunctionInfo
&
486 CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl
*MD
) {
487 // It happens that this is the same as a call with no optional
488 // arguments, except also using the formal 'self' type.
489 return arrangeObjCMessageSendSignature(MD
, MD
->getSelfDecl()->getType());
492 /// Arrange the argument and result information for the function type
493 /// through which to perform a send to the given Objective-C method,
494 /// using the given receiver type. The receiver type is not always
495 /// the 'self' type of the method or even an Objective-C pointer type.
496 /// This is *not* the right method for actually performing such a
497 /// message send, due to the possibility of optional arguments.
498 const CGFunctionInfo
&
499 CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl
*MD
,
500 QualType receiverType
) {
501 SmallVector
<CanQualType
, 16> argTys
;
502 SmallVector
<FunctionProtoType::ExtParameterInfo
, 4> extParamInfos(
503 MD
->isDirectMethod() ? 1 : 2);
504 argTys
.push_back(Context
.getCanonicalParamType(receiverType
));
505 if (!MD
->isDirectMethod())
506 argTys
.push_back(Context
.getCanonicalParamType(Context
.getObjCSelType()));
508 for (const auto *I
: MD
->parameters()) {
509 argTys
.push_back(Context
.getCanonicalParamType(I
->getType()));
510 auto extParamInfo
= FunctionProtoType::ExtParameterInfo().withIsNoEscape(
511 I
->hasAttr
<NoEscapeAttr
>());
512 extParamInfos
.push_back(extParamInfo
);
515 FunctionType::ExtInfo einfo
;
516 bool IsWindows
= getContext().getTargetInfo().getTriple().isOSWindows();
517 einfo
= einfo
.withCallingConv(getCallingConventionForDecl(MD
, IsWindows
));
519 if (getContext().getLangOpts().ObjCAutoRefCount
&&
520 MD
->hasAttr
<NSReturnsRetainedAttr
>())
521 einfo
= einfo
.withProducesResult(true);
523 RequiredArgs required
=
524 (MD
->isVariadic() ? RequiredArgs(argTys
.size()) : RequiredArgs::All
);
526 return arrangeLLVMFunctionInfo(GetReturnType(MD
->getReturnType()),
527 FnInfoOpts::None
, argTys
, einfo
, extParamInfos
,
531 const CGFunctionInfo
&
532 CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType
,
533 const CallArgList
&args
) {
534 auto argTypes
= getArgTypesForCall(Context
, args
);
535 FunctionType::ExtInfo einfo
;
537 return arrangeLLVMFunctionInfo(GetReturnType(returnType
), FnInfoOpts::None
,
538 argTypes
, einfo
, {}, RequiredArgs::All
);
541 const CGFunctionInfo
&
542 CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD
) {
543 // FIXME: Do we need to handle ObjCMethodDecl?
544 const FunctionDecl
*FD
= cast
<FunctionDecl
>(GD
.getDecl());
546 if (isa
<CXXConstructorDecl
>(GD
.getDecl()) ||
547 isa
<CXXDestructorDecl
>(GD
.getDecl()))
548 return arrangeCXXStructorDeclaration(GD
);
550 return arrangeFunctionDeclaration(FD
);
553 /// Arrange a thunk that takes 'this' as the first parameter followed by
554 /// varargs. Return a void pointer, regardless of the actual return type.
555 /// The body of the thunk will end in a musttail call to a function of the
556 /// correct type, and the caller will bitcast the function to the correct
558 const CGFunctionInfo
&
559 CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl
*MD
) {
560 assert(MD
->isVirtual() && "only methods have thunks");
561 CanQual
<FunctionProtoType
> FTP
= GetFormalType(MD
);
562 CanQualType ArgTys
[] = {DeriveThisType(MD
->getParent(), MD
)};
563 return arrangeLLVMFunctionInfo(Context
.VoidTy
, FnInfoOpts::None
, ArgTys
,
564 FTP
->getExtInfo(), {}, RequiredArgs(1));
567 const CGFunctionInfo
&
568 CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl
*CD
,
570 assert(CT
== Ctor_CopyingClosure
|| CT
== Ctor_DefaultClosure
);
572 CanQual
<FunctionProtoType
> FTP
= GetFormalType(CD
);
573 SmallVector
<CanQualType
, 2> ArgTys
;
574 const CXXRecordDecl
*RD
= CD
->getParent();
575 ArgTys
.push_back(DeriveThisType(RD
, CD
));
576 if (CT
== Ctor_CopyingClosure
)
577 ArgTys
.push_back(*FTP
->param_type_begin());
578 if (RD
->getNumVBases() > 0)
579 ArgTys
.push_back(Context
.IntTy
);
580 CallingConv CC
= Context
.getDefaultCallingConvention(
581 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
582 return arrangeLLVMFunctionInfo(Context
.VoidTy
, FnInfoOpts::IsInstanceMethod
,
583 ArgTys
, FunctionType::ExtInfo(CC
), {},
587 /// Arrange a call as unto a free function, except possibly with an
588 /// additional number of formal parameters considered required.
589 static const CGFunctionInfo
&
590 arrangeFreeFunctionLikeCall(CodeGenTypes
&CGT
,
592 const CallArgList
&args
,
593 const FunctionType
*fnType
,
594 unsigned numExtraRequiredArgs
,
596 assert(args
.size() >= numExtraRequiredArgs
);
598 llvm::SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
600 // In most cases, there are no optional arguments.
601 RequiredArgs required
= RequiredArgs::All
;
603 // If we have a variadic prototype, the required arguments are the
604 // extra prefix plus the arguments in the prototype.
605 if (const FunctionProtoType
*proto
= dyn_cast
<FunctionProtoType
>(fnType
)) {
606 if (proto
->isVariadic())
607 required
= RequiredArgs::forPrototypePlus(proto
, numExtraRequiredArgs
);
609 if (proto
->hasExtParameterInfos())
610 addExtParameterInfosForCall(paramInfos
, proto
, numExtraRequiredArgs
,
613 // If we don't have a prototype at all, but we're supposed to
614 // explicitly use the variadic convention for unprototyped calls,
615 // treat all of the arguments as required but preserve the nominal
616 // possibility of variadics.
617 } else if (CGM
.getTargetCodeGenInfo()
618 .isNoProtoCallVariadic(args
,
619 cast
<FunctionNoProtoType
>(fnType
))) {
620 required
= RequiredArgs(args
.size());
624 SmallVector
<CanQualType
, 16> argTypes
;
625 for (const auto &arg
: args
)
626 argTypes
.push_back(CGT
.getContext().getCanonicalParamType(arg
.Ty
));
627 FnInfoOpts opts
= chainCall
? FnInfoOpts::IsChainCall
: FnInfoOpts::None
;
628 return CGT
.arrangeLLVMFunctionInfo(GetReturnType(fnType
->getReturnType()),
629 opts
, argTypes
, fnType
->getExtInfo(),
630 paramInfos
, required
);
633 /// Figure out the rules for calling a function with the given formal
634 /// type using the given arguments. The arguments are necessary
635 /// because the function might be unprototyped, in which case it's
636 /// target-dependent in crazy ways.
637 const CGFunctionInfo
&
638 CodeGenTypes::arrangeFreeFunctionCall(const CallArgList
&args
,
639 const FunctionType
*fnType
,
641 return arrangeFreeFunctionLikeCall(*this, CGM
, args
, fnType
,
642 chainCall
? 1 : 0, chainCall
);
645 /// A block function is essentially a free function with an
646 /// extra implicit argument.
647 const CGFunctionInfo
&
648 CodeGenTypes::arrangeBlockFunctionCall(const CallArgList
&args
,
649 const FunctionType
*fnType
) {
650 return arrangeFreeFunctionLikeCall(*this, CGM
, args
, fnType
, 1,
651 /*chainCall=*/false);
654 const CGFunctionInfo
&
655 CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType
*proto
,
656 const FunctionArgList
¶ms
) {
657 auto paramInfos
= getExtParameterInfosForCall(proto
, 1, params
.size());
658 auto argTypes
= getArgTypesForDeclaration(Context
, params
);
660 return arrangeLLVMFunctionInfo(GetReturnType(proto
->getReturnType()),
661 FnInfoOpts::None
, argTypes
,
662 proto
->getExtInfo(), paramInfos
,
663 RequiredArgs::forPrototypePlus(proto
, 1));
666 const CGFunctionInfo
&
667 CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType
,
668 const CallArgList
&args
) {
670 SmallVector
<CanQualType
, 16> argTypes
;
671 for (const auto &Arg
: args
)
672 argTypes
.push_back(Context
.getCanonicalParamType(Arg
.Ty
));
673 return arrangeLLVMFunctionInfo(GetReturnType(resultType
), FnInfoOpts::None
,
674 argTypes
, FunctionType::ExtInfo(),
675 /*paramInfos=*/{}, RequiredArgs::All
);
678 const CGFunctionInfo
&
679 CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType
,
680 const FunctionArgList
&args
) {
681 auto argTypes
= getArgTypesForDeclaration(Context
, args
);
683 return arrangeLLVMFunctionInfo(GetReturnType(resultType
), FnInfoOpts::None
,
684 argTypes
, FunctionType::ExtInfo(), {},
688 const CGFunctionInfo
&
689 CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType
,
690 ArrayRef
<CanQualType
> argTypes
) {
691 return arrangeLLVMFunctionInfo(resultType
, FnInfoOpts::None
, argTypes
,
692 FunctionType::ExtInfo(), {},
696 /// Arrange a call to a C++ method, passing the given arguments.
698 /// numPrefixArgs is the number of ABI-specific prefix arguments we have. It
699 /// does not count `this`.
700 const CGFunctionInfo
&
701 CodeGenTypes::arrangeCXXMethodCall(const CallArgList
&args
,
702 const FunctionProtoType
*proto
,
703 RequiredArgs required
,
704 unsigned numPrefixArgs
) {
705 assert(numPrefixArgs
+ 1 <= args
.size() &&
706 "Emitting a call with less args than the required prefix?");
707 // Add one to account for `this`. It's a bit awkward here, but we don't count
708 // `this` in similar places elsewhere.
710 getExtParameterInfosForCall(proto
, numPrefixArgs
+ 1, args
.size());
713 auto argTypes
= getArgTypesForCall(Context
, args
);
715 FunctionType::ExtInfo info
= proto
->getExtInfo();
716 return arrangeLLVMFunctionInfo(GetReturnType(proto
->getReturnType()),
717 FnInfoOpts::IsInstanceMethod
, argTypes
, info
,
718 paramInfos
, required
);
721 const CGFunctionInfo
&CodeGenTypes::arrangeNullaryFunction() {
722 return arrangeLLVMFunctionInfo(getContext().VoidTy
, FnInfoOpts::None
,
723 std::nullopt
, FunctionType::ExtInfo(), {},
727 const CGFunctionInfo
&
728 CodeGenTypes::arrangeCall(const CGFunctionInfo
&signature
,
729 const CallArgList
&args
) {
730 assert(signature
.arg_size() <= args
.size());
731 if (signature
.arg_size() == args
.size())
734 SmallVector
<FunctionProtoType::ExtParameterInfo
, 16> paramInfos
;
735 auto sigParamInfos
= signature
.getExtParameterInfos();
736 if (!sigParamInfos
.empty()) {
737 paramInfos
.append(sigParamInfos
.begin(), sigParamInfos
.end());
738 paramInfos
.resize(args
.size());
741 auto argTypes
= getArgTypesForCall(Context
, args
);
743 assert(signature
.getRequiredArgs().allowsOptionalArgs());
744 FnInfoOpts opts
= FnInfoOpts::None
;
745 if (signature
.isInstanceMethod())
746 opts
|= FnInfoOpts::IsInstanceMethod
;
747 if (signature
.isChainCall())
748 opts
|= FnInfoOpts::IsChainCall
;
749 if (signature
.isDelegateCall())
750 opts
|= FnInfoOpts::IsDelegateCall
;
751 return arrangeLLVMFunctionInfo(signature
.getReturnType(), opts
, argTypes
,
752 signature
.getExtInfo(), paramInfos
,
753 signature
.getRequiredArgs());
758 void computeSPIRKernelABIInfo(CodeGenModule
&CGM
, CGFunctionInfo
&FI
);
762 /// Arrange the argument and result information for an abstract value
763 /// of a given function type. This is the method which all of the
764 /// above functions ultimately defer to.
765 const CGFunctionInfo
&CodeGenTypes::arrangeLLVMFunctionInfo(
766 CanQualType resultType
, FnInfoOpts opts
, ArrayRef
<CanQualType
> argTypes
,
767 FunctionType::ExtInfo info
,
768 ArrayRef
<FunctionProtoType::ExtParameterInfo
> paramInfos
,
769 RequiredArgs required
) {
770 assert(llvm::all_of(argTypes
,
771 [](CanQualType T
) { return T
.isCanonicalAsParam(); }));
773 // Lookup or create unique function info.
774 llvm::FoldingSetNodeID ID
;
775 bool isInstanceMethod
=
776 (opts
& FnInfoOpts::IsInstanceMethod
) == FnInfoOpts::IsInstanceMethod
;
778 (opts
& FnInfoOpts::IsChainCall
) == FnInfoOpts::IsChainCall
;
779 bool isDelegateCall
=
780 (opts
& FnInfoOpts::IsDelegateCall
) == FnInfoOpts::IsDelegateCall
;
781 CGFunctionInfo::Profile(ID
, isInstanceMethod
, isChainCall
, isDelegateCall
,
782 info
, paramInfos
, required
, resultType
, argTypes
);
784 void *insertPos
= nullptr;
785 CGFunctionInfo
*FI
= FunctionInfos
.FindNodeOrInsertPos(ID
, insertPos
);
789 unsigned CC
= ClangCallConvToLLVMCallConv(info
.getCC());
791 // Construct the function info. We co-allocate the ArgInfos.
792 FI
= CGFunctionInfo::create(CC
, isInstanceMethod
, isChainCall
, isDelegateCall
,
793 info
, paramInfos
, resultType
, argTypes
, required
);
794 FunctionInfos
.InsertNode(FI
, insertPos
);
796 bool inserted
= FunctionsBeingProcessed
.insert(FI
).second
;
798 assert(inserted
&& "Recursively being processed?");
800 // Compute ABI information.
801 if (CC
== llvm::CallingConv::SPIR_KERNEL
) {
802 // Force target independent argument handling for the host visible
804 computeSPIRKernelABIInfo(CGM
, *FI
);
805 } else if (info
.getCC() == CC_Swift
|| info
.getCC() == CC_SwiftAsync
) {
806 swiftcall::computeABIInfo(CGM
, *FI
);
808 CGM
.getABIInfo().computeInfo(*FI
);
811 // Loop over all of the computed argument and return value info. If any of
812 // them are direct or extend without a specified coerce type, specify the
814 ABIArgInfo
&retInfo
= FI
->getReturnInfo();
815 if (retInfo
.canHaveCoerceToType() && retInfo
.getCoerceToType() == nullptr)
816 retInfo
.setCoerceToType(ConvertType(FI
->getReturnType()));
818 for (auto &I
: FI
->arguments())
819 if (I
.info
.canHaveCoerceToType() && I
.info
.getCoerceToType() == nullptr)
820 I
.info
.setCoerceToType(ConvertType(I
.type
));
822 bool erased
= FunctionsBeingProcessed
.erase(FI
); (void)erased
;
823 assert(erased
&& "Not in set?");
828 CGFunctionInfo
*CGFunctionInfo::create(unsigned llvmCC
, bool instanceMethod
,
829 bool chainCall
, bool delegateCall
,
830 const FunctionType::ExtInfo
&info
,
831 ArrayRef
<ExtParameterInfo
> paramInfos
,
832 CanQualType resultType
,
833 ArrayRef
<CanQualType
> argTypes
,
834 RequiredArgs required
) {
835 assert(paramInfos
.empty() || paramInfos
.size() == argTypes
.size());
836 assert(!required
.allowsOptionalArgs() ||
837 required
.getNumRequiredArgs() <= argTypes
.size());
840 operator new(totalSizeToAlloc
<ArgInfo
, ExtParameterInfo
>(
841 argTypes
.size() + 1, paramInfos
.size()));
843 CGFunctionInfo
*FI
= new(buffer
) CGFunctionInfo();
844 FI
->CallingConvention
= llvmCC
;
845 FI
->EffectiveCallingConvention
= llvmCC
;
846 FI
->ASTCallingConvention
= info
.getCC();
847 FI
->InstanceMethod
= instanceMethod
;
848 FI
->ChainCall
= chainCall
;
849 FI
->DelegateCall
= delegateCall
;
850 FI
->CmseNSCall
= info
.getCmseNSCall();
851 FI
->NoReturn
= info
.getNoReturn();
852 FI
->ReturnsRetained
= info
.getProducesResult();
853 FI
->NoCallerSavedRegs
= info
.getNoCallerSavedRegs();
854 FI
->NoCfCheck
= info
.getNoCfCheck();
855 FI
->Required
= required
;
856 FI
->HasRegParm
= info
.getHasRegParm();
857 FI
->RegParm
= info
.getRegParm();
858 FI
->ArgStruct
= nullptr;
859 FI
->ArgStructAlign
= 0;
860 FI
->NumArgs
= argTypes
.size();
861 FI
->HasExtParameterInfos
= !paramInfos
.empty();
862 FI
->getArgsBuffer()[0].type
= resultType
;
863 FI
->MaxVectorWidth
= 0;
864 for (unsigned i
= 0, e
= argTypes
.size(); i
!= e
; ++i
)
865 FI
->getArgsBuffer()[i
+ 1].type
= argTypes
[i
];
866 for (unsigned i
= 0, e
= paramInfos
.size(); i
!= e
; ++i
)
867 FI
->getExtParameterInfosBuffer()[i
] = paramInfos
[i
];
874 // ABIArgInfo::Expand implementation.
876 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded.
877 struct TypeExpansion
{
878 enum TypeExpansionKind
{
879 // Elements of constant arrays are expanded recursively.
881 // Record fields are expanded recursively (but if record is a union, only
882 // the field with the largest size is expanded).
884 // For complex types, real and imaginary parts are expanded recursively.
886 // All other types are not expandable.
890 const TypeExpansionKind Kind
;
892 TypeExpansion(TypeExpansionKind K
) : Kind(K
) {}
893 virtual ~TypeExpansion() {}
896 struct ConstantArrayExpansion
: TypeExpansion
{
900 ConstantArrayExpansion(QualType EltTy
, uint64_t NumElts
)
901 : TypeExpansion(TEK_ConstantArray
), EltTy(EltTy
), NumElts(NumElts
) {}
902 static bool classof(const TypeExpansion
*TE
) {
903 return TE
->Kind
== TEK_ConstantArray
;
907 struct RecordExpansion
: TypeExpansion
{
908 SmallVector
<const CXXBaseSpecifier
*, 1> Bases
;
910 SmallVector
<const FieldDecl
*, 1> Fields
;
912 RecordExpansion(SmallVector
<const CXXBaseSpecifier
*, 1> &&Bases
,
913 SmallVector
<const FieldDecl
*, 1> &&Fields
)
914 : TypeExpansion(TEK_Record
), Bases(std::move(Bases
)),
915 Fields(std::move(Fields
)) {}
916 static bool classof(const TypeExpansion
*TE
) {
917 return TE
->Kind
== TEK_Record
;
921 struct ComplexExpansion
: TypeExpansion
{
924 ComplexExpansion(QualType EltTy
) : TypeExpansion(TEK_Complex
), EltTy(EltTy
) {}
925 static bool classof(const TypeExpansion
*TE
) {
926 return TE
->Kind
== TEK_Complex
;
930 struct NoExpansion
: TypeExpansion
{
931 NoExpansion() : TypeExpansion(TEK_None
) {}
932 static bool classof(const TypeExpansion
*TE
) {
933 return TE
->Kind
== TEK_None
;
938 static std::unique_ptr
<TypeExpansion
>
939 getTypeExpansion(QualType Ty
, const ASTContext
&Context
) {
940 if (const ConstantArrayType
*AT
= Context
.getAsConstantArrayType(Ty
)) {
941 return std::make_unique
<ConstantArrayExpansion
>(AT
->getElementType(),
944 if (const RecordType
*RT
= Ty
->getAs
<RecordType
>()) {
945 SmallVector
<const CXXBaseSpecifier
*, 1> Bases
;
946 SmallVector
<const FieldDecl
*, 1> Fields
;
947 const RecordDecl
*RD
= RT
->getDecl();
948 assert(!RD
->hasFlexibleArrayMember() &&
949 "Cannot expand structure with flexible array.");
951 // Unions can be here only in degenerative cases - all the fields are same
952 // after flattening. Thus we have to use the "largest" field.
953 const FieldDecl
*LargestFD
= nullptr;
954 CharUnits UnionSize
= CharUnits::Zero();
956 for (const auto *FD
: RD
->fields()) {
957 if (FD
->isZeroLengthBitField(Context
))
959 assert(!FD
->isBitField() &&
960 "Cannot expand structure with bit-field members.");
961 CharUnits FieldSize
= Context
.getTypeSizeInChars(FD
->getType());
962 if (UnionSize
< FieldSize
) {
963 UnionSize
= FieldSize
;
968 Fields
.push_back(LargestFD
);
970 if (const auto *CXXRD
= dyn_cast
<CXXRecordDecl
>(RD
)) {
971 assert(!CXXRD
->isDynamicClass() &&
972 "cannot expand vtable pointers in dynamic classes");
973 llvm::append_range(Bases
, llvm::make_pointer_range(CXXRD
->bases()));
976 for (const auto *FD
: RD
->fields()) {
977 if (FD
->isZeroLengthBitField(Context
))
979 assert(!FD
->isBitField() &&
980 "Cannot expand structure with bit-field members.");
981 Fields
.push_back(FD
);
984 return std::make_unique
<RecordExpansion
>(std::move(Bases
),
987 if (const ComplexType
*CT
= Ty
->getAs
<ComplexType
>()) {
988 return std::make_unique
<ComplexExpansion
>(CT
->getElementType());
990 return std::make_unique
<NoExpansion
>();
993 static int getExpansionSize(QualType Ty
, const ASTContext
&Context
) {
994 auto Exp
= getTypeExpansion(Ty
, Context
);
995 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
996 return CAExp
->NumElts
* getExpansionSize(CAExp
->EltTy
, Context
);
998 if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1000 for (auto BS
: RExp
->Bases
)
1001 Res
+= getExpansionSize(BS
->getType(), Context
);
1002 for (auto FD
: RExp
->Fields
)
1003 Res
+= getExpansionSize(FD
->getType(), Context
);
1006 if (isa
<ComplexExpansion
>(Exp
.get()))
1008 assert(isa
<NoExpansion
>(Exp
.get()));
1013 CodeGenTypes::getExpandedTypes(QualType Ty
,
1014 SmallVectorImpl
<llvm::Type
*>::iterator
&TI
) {
1015 auto Exp
= getTypeExpansion(Ty
, Context
);
1016 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1017 for (int i
= 0, n
= CAExp
->NumElts
; i
< n
; i
++) {
1018 getExpandedTypes(CAExp
->EltTy
, TI
);
1020 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1021 for (auto BS
: RExp
->Bases
)
1022 getExpandedTypes(BS
->getType(), TI
);
1023 for (auto FD
: RExp
->Fields
)
1024 getExpandedTypes(FD
->getType(), TI
);
1025 } else if (auto CExp
= dyn_cast
<ComplexExpansion
>(Exp
.get())) {
1026 llvm::Type
*EltTy
= ConvertType(CExp
->EltTy
);
1030 assert(isa
<NoExpansion
>(Exp
.get()));
1031 *TI
++ = ConvertType(Ty
);
1035 static void forConstantArrayExpansion(CodeGenFunction
&CGF
,
1036 ConstantArrayExpansion
*CAE
,
1038 llvm::function_ref
<void(Address
)> Fn
) {
1039 for (int i
= 0, n
= CAE
->NumElts
; i
< n
; i
++) {
1040 Address EltAddr
= CGF
.Builder
.CreateConstGEP2_32(BaseAddr
, 0, i
);
1045 void CodeGenFunction::ExpandTypeFromArgs(QualType Ty
, LValue LV
,
1046 llvm::Function::arg_iterator
&AI
) {
1047 assert(LV
.isSimple() &&
1048 "Unexpected non-simple lvalue during struct expansion.");
1050 auto Exp
= getTypeExpansion(Ty
, getContext());
1051 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1052 forConstantArrayExpansion(
1053 *this, CAExp
, LV
.getAddress(), [&](Address EltAddr
) {
1054 LValue LV
= MakeAddrLValue(EltAddr
, CAExp
->EltTy
);
1055 ExpandTypeFromArgs(CAExp
->EltTy
, LV
, AI
);
1057 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1058 Address This
= LV
.getAddress();
1059 for (const CXXBaseSpecifier
*BS
: RExp
->Bases
) {
1060 // Perform a single step derived-to-base conversion.
1062 GetAddressOfBaseClass(This
, Ty
->getAsCXXRecordDecl(), &BS
, &BS
+ 1,
1063 /*NullCheckValue=*/false, SourceLocation());
1064 LValue SubLV
= MakeAddrLValue(Base
, BS
->getType());
1066 // Recurse onto bases.
1067 ExpandTypeFromArgs(BS
->getType(), SubLV
, AI
);
1069 for (auto FD
: RExp
->Fields
) {
1070 // FIXME: What are the right qualifiers here?
1071 LValue SubLV
= EmitLValueForFieldInitialization(LV
, FD
);
1072 ExpandTypeFromArgs(FD
->getType(), SubLV
, AI
);
1074 } else if (isa
<ComplexExpansion
>(Exp
.get())) {
1075 auto realValue
= &*AI
++;
1076 auto imagValue
= &*AI
++;
1077 EmitStoreOfComplex(ComplexPairTy(realValue
, imagValue
), LV
, /*init*/ true);
1079 // Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a
1081 assert(isa
<NoExpansion
>(Exp
.get()));
1082 llvm::Value
*Arg
= &*AI
++;
1083 if (LV
.isBitField()) {
1084 EmitStoreThroughLValue(RValue::get(Arg
), LV
);
1086 // TODO: currently there are some places are inconsistent in what LLVM
1087 // pointer type they use (see D118744). Once clang uses opaque pointers
1088 // all LLVM pointer types will be the same and we can remove this check.
1089 if (Arg
->getType()->isPointerTy()) {
1090 Address Addr
= LV
.getAddress();
1091 Arg
= Builder
.CreateBitCast(Arg
, Addr
.getElementType());
1093 EmitStoreOfScalar(Arg
, LV
);
1098 void CodeGenFunction::ExpandTypeToArgs(
1099 QualType Ty
, CallArg Arg
, llvm::FunctionType
*IRFuncTy
,
1100 SmallVectorImpl
<llvm::Value
*> &IRCallArgs
, unsigned &IRCallArgPos
) {
1101 auto Exp
= getTypeExpansion(Ty
, getContext());
1102 if (auto CAExp
= dyn_cast
<ConstantArrayExpansion
>(Exp
.get())) {
1103 Address Addr
= Arg
.hasLValue() ? Arg
.getKnownLValue().getAddress()
1104 : Arg
.getKnownRValue().getAggregateAddress();
1105 forConstantArrayExpansion(
1106 *this, CAExp
, Addr
, [&](Address EltAddr
) {
1107 CallArg EltArg
= CallArg(
1108 convertTempToRValue(EltAddr
, CAExp
->EltTy
, SourceLocation()),
1110 ExpandTypeToArgs(CAExp
->EltTy
, EltArg
, IRFuncTy
, IRCallArgs
,
1113 } else if (auto RExp
= dyn_cast
<RecordExpansion
>(Exp
.get())) {
1114 Address This
= Arg
.hasLValue() ? Arg
.getKnownLValue().getAddress()
1115 : Arg
.getKnownRValue().getAggregateAddress();
1116 for (const CXXBaseSpecifier
*BS
: RExp
->Bases
) {
1117 // Perform a single step derived-to-base conversion.
1119 GetAddressOfBaseClass(This
, Ty
->getAsCXXRecordDecl(), &BS
, &BS
+ 1,
1120 /*NullCheckValue=*/false, SourceLocation());
1121 CallArg BaseArg
= CallArg(RValue::getAggregate(Base
), BS
->getType());
1123 // Recurse onto bases.
1124 ExpandTypeToArgs(BS
->getType(), BaseArg
, IRFuncTy
, IRCallArgs
,
1128 LValue LV
= MakeAddrLValue(This
, Ty
);
1129 for (auto FD
: RExp
->Fields
) {
1131 CallArg(EmitRValueForField(LV
, FD
, SourceLocation()), FD
->getType());
1132 ExpandTypeToArgs(FD
->getType(), FldArg
, IRFuncTy
, IRCallArgs
,
1135 } else if (isa
<ComplexExpansion
>(Exp
.get())) {
1136 ComplexPairTy CV
= Arg
.getKnownRValue().getComplexVal();
1137 IRCallArgs
[IRCallArgPos
++] = CV
.first
;
1138 IRCallArgs
[IRCallArgPos
++] = CV
.second
;
1140 assert(isa
<NoExpansion
>(Exp
.get()));
1141 auto RV
= Arg
.getKnownRValue();
1142 assert(RV
.isScalar() &&
1143 "Unexpected non-scalar rvalue during struct expansion.");
1145 // Insert a bitcast as needed.
1146 llvm::Value
*V
= RV
.getScalarVal();
1147 if (IRCallArgPos
< IRFuncTy
->getNumParams() &&
1148 V
->getType() != IRFuncTy
->getParamType(IRCallArgPos
))
1149 V
= Builder
.CreateBitCast(V
, IRFuncTy
->getParamType(IRCallArgPos
));
1151 IRCallArgs
[IRCallArgPos
++] = V
;
1155 /// Create a temporary allocation for the purposes of coercion.
1156 static RawAddress
CreateTempAllocaForCoercion(CodeGenFunction
&CGF
,
1159 const Twine
&Name
= "tmp") {
1160 // Don't use an alignment that's worse than what LLVM would prefer.
1161 auto PrefAlign
= CGF
.CGM
.getDataLayout().getPrefTypeAlign(Ty
);
1162 CharUnits Align
= std::max(MinAlign
, CharUnits::fromQuantity(PrefAlign
));
1164 return CGF
.CreateTempAlloca(Ty
, Align
, Name
+ ".coerce");
1167 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
1168 /// accessing some number of bytes out of it, try to gep into the struct to get
1169 /// at its inner goodness. Dive as deep as possible without entering an element
1170 /// with an in-memory size smaller than DstSize.
1172 EnterStructPointerForCoercedAccess(Address SrcPtr
,
1173 llvm::StructType
*SrcSTy
,
1174 uint64_t DstSize
, CodeGenFunction
&CGF
) {
1175 // We can't dive into a zero-element struct.
1176 if (SrcSTy
->getNumElements() == 0) return SrcPtr
;
1178 llvm::Type
*FirstElt
= SrcSTy
->getElementType(0);
1180 // If the first elt is at least as large as what we're looking for, or if the
1181 // first element is the same size as the whole struct, we can enter it. The
1182 // comparison must be made on the store size and not the alloca size. Using
1183 // the alloca size may overstate the size of the load.
1184 uint64_t FirstEltSize
=
1185 CGF
.CGM
.getDataLayout().getTypeStoreSize(FirstElt
);
1186 if (FirstEltSize
< DstSize
&&
1187 FirstEltSize
< CGF
.CGM
.getDataLayout().getTypeStoreSize(SrcSTy
))
1190 // GEP into the first element.
1191 SrcPtr
= CGF
.Builder
.CreateStructGEP(SrcPtr
, 0, "coerce.dive");
1193 // If the first element is a struct, recurse.
1194 llvm::Type
*SrcTy
= SrcPtr
.getElementType();
1195 if (llvm::StructType
*SrcSTy
= dyn_cast
<llvm::StructType
>(SrcTy
))
1196 return EnterStructPointerForCoercedAccess(SrcPtr
, SrcSTy
, DstSize
, CGF
);
1201 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
1202 /// are either integers or pointers. This does a truncation of the value if it
1203 /// is too large or a zero extension if it is too small.
1205 /// This behaves as if the value were coerced through memory, so on big-endian
1206 /// targets the high bits are preserved in a truncation, while little-endian
1207 /// targets preserve the low bits.
1208 static llvm::Value
*CoerceIntOrPtrToIntOrPtr(llvm::Value
*Val
,
1210 CodeGenFunction
&CGF
) {
1211 if (Val
->getType() == Ty
)
1214 if (isa
<llvm::PointerType
>(Val
->getType())) {
1215 // If this is Pointer->Pointer avoid conversion to and from int.
1216 if (isa
<llvm::PointerType
>(Ty
))
1217 return CGF
.Builder
.CreateBitCast(Val
, Ty
, "coerce.val");
1219 // Convert the pointer to an integer so we can play with its width.
1220 Val
= CGF
.Builder
.CreatePtrToInt(Val
, CGF
.IntPtrTy
, "coerce.val.pi");
1223 llvm::Type
*DestIntTy
= Ty
;
1224 if (isa
<llvm::PointerType
>(DestIntTy
))
1225 DestIntTy
= CGF
.IntPtrTy
;
1227 if (Val
->getType() != DestIntTy
) {
1228 const llvm::DataLayout
&DL
= CGF
.CGM
.getDataLayout();
1229 if (DL
.isBigEndian()) {
1230 // Preserve the high bits on big-endian targets.
1231 // That is what memory coercion does.
1232 uint64_t SrcSize
= DL
.getTypeSizeInBits(Val
->getType());
1233 uint64_t DstSize
= DL
.getTypeSizeInBits(DestIntTy
);
1235 if (SrcSize
> DstSize
) {
1236 Val
= CGF
.Builder
.CreateLShr(Val
, SrcSize
- DstSize
, "coerce.highbits");
1237 Val
= CGF
.Builder
.CreateTrunc(Val
, DestIntTy
, "coerce.val.ii");
1239 Val
= CGF
.Builder
.CreateZExt(Val
, DestIntTy
, "coerce.val.ii");
1240 Val
= CGF
.Builder
.CreateShl(Val
, DstSize
- SrcSize
, "coerce.highbits");
1243 // Little-endian targets preserve the low bits. No shifts required.
1244 Val
= CGF
.Builder
.CreateIntCast(Val
, DestIntTy
, false, "coerce.val.ii");
1248 if (isa
<llvm::PointerType
>(Ty
))
1249 Val
= CGF
.Builder
.CreateIntToPtr(Val
, Ty
, "coerce.val.ip");
1255 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1256 /// a pointer to an object of type \arg Ty, known to be aligned to
1257 /// \arg SrcAlign bytes.
1259 /// This safely handles the case when the src type is smaller than the
1260 /// destination type; in this situation the values of bits which not
1261 /// present in the src are undefined.
1262 static llvm::Value
*CreateCoercedLoad(Address Src
, llvm::Type
*Ty
,
1263 CodeGenFunction
&CGF
) {
1264 llvm::Type
*SrcTy
= Src
.getElementType();
1266 // If SrcTy and Ty are the same, just do a load.
1268 return CGF
.Builder
.CreateLoad(Src
);
1270 llvm::TypeSize DstSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(Ty
);
1272 if (llvm::StructType
*SrcSTy
= dyn_cast
<llvm::StructType
>(SrcTy
)) {
1273 Src
= EnterStructPointerForCoercedAccess(Src
, SrcSTy
,
1274 DstSize
.getFixedValue(), CGF
);
1275 SrcTy
= Src
.getElementType();
1278 llvm::TypeSize SrcSize
= CGF
.CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
1280 // If the source and destination are integer or pointer types, just do an
1281 // extension or truncation to the desired type.
1282 if ((isa
<llvm::IntegerType
>(Ty
) || isa
<llvm::PointerType
>(Ty
)) &&
1283 (isa
<llvm::IntegerType
>(SrcTy
) || isa
<llvm::PointerType
>(SrcTy
))) {
1284 llvm::Value
*Load
= CGF
.Builder
.CreateLoad(Src
);
1285 return CoerceIntOrPtrToIntOrPtr(Load
, Ty
, CGF
);
1288 // If load is legal, just bitcast the src pointer.
1289 if (!SrcSize
.isScalable() && !DstSize
.isScalable() &&
1290 SrcSize
.getFixedValue() >= DstSize
.getFixedValue()) {
1291 // Generally SrcSize is never greater than DstSize, since this means we are
1292 // losing bits. However, this can happen in cases where the structure has
1293 // additional padding, for example due to a user specified alignment.
1295 // FIXME: Assert that we aren't truncating non-padding bits when have access
1296 // to that information.
1297 Src
= Src
.withElementType(Ty
);
1298 return CGF
.Builder
.CreateLoad(Src
);
1301 // If coercing a fixed vector to a scalable vector for ABI compatibility, and
1302 // the types match, use the llvm.vector.insert intrinsic to perform the
1304 if (auto *ScalableDstTy
= dyn_cast
<llvm::ScalableVectorType
>(Ty
)) {
1305 if (auto *FixedSrcTy
= dyn_cast
<llvm::FixedVectorType
>(SrcTy
)) {
1306 // If we are casting a fixed i8 vector to a scalable i1 predicate
1307 // vector, use a vector insert and bitcast the result.
1308 if (ScalableDstTy
->getElementType()->isIntegerTy(1) &&
1309 ScalableDstTy
->getElementCount().isKnownMultipleOf(8) &&
1310 FixedSrcTy
->getElementType()->isIntegerTy(8)) {
1311 ScalableDstTy
= llvm::ScalableVectorType::get(
1312 FixedSrcTy
->getElementType(),
1313 ScalableDstTy
->getElementCount().getKnownMinValue() / 8);
1315 if (ScalableDstTy
->getElementType() == FixedSrcTy
->getElementType()) {
1316 auto *Load
= CGF
.Builder
.CreateLoad(Src
);
1317 auto *UndefVec
= llvm::UndefValue::get(ScalableDstTy
);
1318 auto *Zero
= llvm::Constant::getNullValue(CGF
.CGM
.Int64Ty
);
1319 llvm::Value
*Result
= CGF
.Builder
.CreateInsertVector(
1320 ScalableDstTy
, UndefVec
, Load
, Zero
, "cast.scalable");
1321 if (ScalableDstTy
!= Ty
)
1322 Result
= CGF
.Builder
.CreateBitCast(Result
, Ty
);
1328 // Otherwise do coercion through memory. This is stupid, but simple.
1330 CreateTempAllocaForCoercion(CGF
, Ty
, Src
.getAlignment(), Src
.getName());
1331 CGF
.Builder
.CreateMemCpy(
1332 Tmp
.getPointer(), Tmp
.getAlignment().getAsAlign(),
1333 Src
.emitRawPointer(CGF
), Src
.getAlignment().getAsAlign(),
1334 llvm::ConstantInt::get(CGF
.IntPtrTy
, SrcSize
.getKnownMinValue()));
1335 return CGF
.Builder
.CreateLoad(Tmp
);
1338 void CodeGenFunction::CreateCoercedStore(llvm::Value
*Src
, Address Dst
,
1339 llvm::TypeSize DstSize
,
1340 bool DstIsVolatile
) {
1344 llvm::Type
*SrcTy
= Src
->getType();
1345 llvm::TypeSize SrcSize
= CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
1347 // GEP into structs to try to make types match.
1348 // FIXME: This isn't really that useful with opaque types, but it impacts a
1349 // lot of regression tests.
1350 if (SrcTy
!= Dst
.getElementType()) {
1351 if (llvm::StructType
*DstSTy
=
1352 dyn_cast
<llvm::StructType
>(Dst
.getElementType())) {
1353 assert(!SrcSize
.isScalable());
1354 Dst
= EnterStructPointerForCoercedAccess(Dst
, DstSTy
,
1355 SrcSize
.getFixedValue(), *this);
1359 if (SrcSize
.isScalable() || SrcSize
<= DstSize
) {
1360 if (SrcTy
->isIntegerTy() && Dst
.getElementType()->isPointerTy() &&
1361 SrcSize
== CGM
.getDataLayout().getTypeAllocSize(Dst
.getElementType())) {
1362 // If the value is supposed to be a pointer, convert it before storing it.
1363 Src
= CoerceIntOrPtrToIntOrPtr(Src
, Dst
.getElementType(), *this);
1364 Builder
.CreateStore(Src
, Dst
, DstIsVolatile
);
1365 } else if (llvm::StructType
*STy
=
1366 dyn_cast
<llvm::StructType
>(Src
->getType())) {
1367 // Prefer scalar stores to first-class aggregate stores.
1368 Dst
= Dst
.withElementType(SrcTy
);
1369 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
1370 Address EltPtr
= Builder
.CreateStructGEP(Dst
, i
);
1371 llvm::Value
*Elt
= Builder
.CreateExtractValue(Src
, i
);
1372 Builder
.CreateStore(Elt
, EltPtr
, DstIsVolatile
);
1375 Builder
.CreateStore(Src
, Dst
.withElementType(SrcTy
), DstIsVolatile
);
1377 } else if (SrcTy
->isIntegerTy()) {
1378 // If the source is a simple integer, coerce it directly.
1379 llvm::Type
*DstIntTy
= Builder
.getIntNTy(DstSize
.getFixedValue() * 8);
1380 Src
= CoerceIntOrPtrToIntOrPtr(Src
, DstIntTy
, *this);
1381 Builder
.CreateStore(Src
, Dst
.withElementType(DstIntTy
), DstIsVolatile
);
1383 // Otherwise do coercion through memory. This is stupid, but
1386 // Generally SrcSize is never greater than DstSize, since this means we are
1387 // losing bits. However, this can happen in cases where the structure has
1388 // additional padding, for example due to a user specified alignment.
1390 // FIXME: Assert that we aren't truncating non-padding bits when have access
1391 // to that information.
1393 CreateTempAllocaForCoercion(*this, SrcTy
, Dst
.getAlignment());
1394 Builder
.CreateStore(Src
, Tmp
);
1395 Builder
.CreateMemCpy(Dst
.emitRawPointer(*this),
1396 Dst
.getAlignment().getAsAlign(), Tmp
.getPointer(),
1397 Tmp
.getAlignment().getAsAlign(),
1398 Builder
.CreateTypeSize(IntPtrTy
, DstSize
));
1402 static Address
emitAddressAtOffset(CodeGenFunction
&CGF
, Address addr
,
1403 const ABIArgInfo
&info
) {
1404 if (unsigned offset
= info
.getDirectOffset()) {
1405 addr
= addr
.withElementType(CGF
.Int8Ty
);
1406 addr
= CGF
.Builder
.CreateConstInBoundsByteGEP(addr
,
1407 CharUnits::fromQuantity(offset
));
1408 addr
= addr
.withElementType(info
.getCoerceToType());
1415 /// Encapsulates information about the way function arguments from
1416 /// CGFunctionInfo should be passed to actual LLVM IR function.
1417 class ClangToLLVMArgMapping
{
1418 static const unsigned InvalidIndex
= ~0U;
1419 unsigned InallocaArgNo
;
1421 unsigned TotalIRArgs
;
1423 /// Arguments of LLVM IR function corresponding to single Clang argument.
1425 unsigned PaddingArgIndex
;
1426 // Argument is expanded to IR arguments at positions
1427 // [FirstArgIndex, FirstArgIndex + NumberOfArgs).
1428 unsigned FirstArgIndex
;
1429 unsigned NumberOfArgs
;
1432 : PaddingArgIndex(InvalidIndex
), FirstArgIndex(InvalidIndex
),
1436 SmallVector
<IRArgs
, 8> ArgInfo
;
1439 ClangToLLVMArgMapping(const ASTContext
&Context
, const CGFunctionInfo
&FI
,
1440 bool OnlyRequiredArgs
= false)
1441 : InallocaArgNo(InvalidIndex
), SRetArgNo(InvalidIndex
), TotalIRArgs(0),
1442 ArgInfo(OnlyRequiredArgs
? FI
.getNumRequiredArgs() : FI
.arg_size()) {
1443 construct(Context
, FI
, OnlyRequiredArgs
);
1446 bool hasInallocaArg() const { return InallocaArgNo
!= InvalidIndex
; }
1447 unsigned getInallocaArgNo() const {
1448 assert(hasInallocaArg());
1449 return InallocaArgNo
;
1452 bool hasSRetArg() const { return SRetArgNo
!= InvalidIndex
; }
1453 unsigned getSRetArgNo() const {
1454 assert(hasSRetArg());
1458 unsigned totalIRArgs() const { return TotalIRArgs
; }
1460 bool hasPaddingArg(unsigned ArgNo
) const {
1461 assert(ArgNo
< ArgInfo
.size());
1462 return ArgInfo
[ArgNo
].PaddingArgIndex
!= InvalidIndex
;
1464 unsigned getPaddingArgNo(unsigned ArgNo
) const {
1465 assert(hasPaddingArg(ArgNo
));
1466 return ArgInfo
[ArgNo
].PaddingArgIndex
;
1469 /// Returns index of first IR argument corresponding to ArgNo, and their
1471 std::pair
<unsigned, unsigned> getIRArgs(unsigned ArgNo
) const {
1472 assert(ArgNo
< ArgInfo
.size());
1473 return std::make_pair(ArgInfo
[ArgNo
].FirstArgIndex
,
1474 ArgInfo
[ArgNo
].NumberOfArgs
);
1478 void construct(const ASTContext
&Context
, const CGFunctionInfo
&FI
,
1479 bool OnlyRequiredArgs
);
1482 void ClangToLLVMArgMapping::construct(const ASTContext
&Context
,
1483 const CGFunctionInfo
&FI
,
1484 bool OnlyRequiredArgs
) {
1485 unsigned IRArgNo
= 0;
1486 bool SwapThisWithSRet
= false;
1487 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
1489 if (RetAI
.getKind() == ABIArgInfo::Indirect
) {
1490 SwapThisWithSRet
= RetAI
.isSRetAfterThis();
1491 SRetArgNo
= SwapThisWithSRet
? 1 : IRArgNo
++;
1495 unsigned NumArgs
= OnlyRequiredArgs
? FI
.getNumRequiredArgs() : FI
.arg_size();
1496 for (CGFunctionInfo::const_arg_iterator I
= FI
.arg_begin(); ArgNo
< NumArgs
;
1498 assert(I
!= FI
.arg_end());
1499 QualType ArgType
= I
->type
;
1500 const ABIArgInfo
&AI
= I
->info
;
1501 // Collect data about IR arguments corresponding to Clang argument ArgNo.
1502 auto &IRArgs
= ArgInfo
[ArgNo
];
1504 if (AI
.getPaddingType())
1505 IRArgs
.PaddingArgIndex
= IRArgNo
++;
1507 switch (AI
.getKind()) {
1508 case ABIArgInfo::Extend
:
1509 case ABIArgInfo::Direct
: {
1510 // FIXME: handle sseregparm someday...
1511 llvm::StructType
*STy
= dyn_cast
<llvm::StructType
>(AI
.getCoerceToType());
1512 if (AI
.isDirect() && AI
.getCanBeFlattened() && STy
) {
1513 IRArgs
.NumberOfArgs
= STy
->getNumElements();
1515 IRArgs
.NumberOfArgs
= 1;
1519 case ABIArgInfo::Indirect
:
1520 case ABIArgInfo::IndirectAliased
:
1521 IRArgs
.NumberOfArgs
= 1;
1523 case ABIArgInfo::Ignore
:
1524 case ABIArgInfo::InAlloca
:
1525 // ignore and inalloca doesn't have matching LLVM parameters.
1526 IRArgs
.NumberOfArgs
= 0;
1528 case ABIArgInfo::CoerceAndExpand
:
1529 IRArgs
.NumberOfArgs
= AI
.getCoerceAndExpandTypeSequence().size();
1531 case ABIArgInfo::Expand
:
1532 IRArgs
.NumberOfArgs
= getExpansionSize(ArgType
, Context
);
1536 if (IRArgs
.NumberOfArgs
> 0) {
1537 IRArgs
.FirstArgIndex
= IRArgNo
;
1538 IRArgNo
+= IRArgs
.NumberOfArgs
;
1541 // Skip over the sret parameter when it comes second. We already handled it
1543 if (IRArgNo
== 1 && SwapThisWithSRet
)
1546 assert(ArgNo
== ArgInfo
.size());
1548 if (FI
.usesInAlloca())
1549 InallocaArgNo
= IRArgNo
++;
1551 TotalIRArgs
= IRArgNo
;
1557 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo
&FI
) {
1558 const auto &RI
= FI
.getReturnInfo();
1559 return RI
.isIndirect() || (RI
.isInAlloca() && RI
.getInAllocaSRet());
1562 bool CodeGenModule::ReturnTypeHasInReg(const CGFunctionInfo
&FI
) {
1563 const auto &RI
= FI
.getReturnInfo();
1564 return RI
.getInReg();
1567 bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo
&FI
) {
1568 return ReturnTypeUsesSRet(FI
) &&
1569 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1572 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType
) {
1573 if (const BuiltinType
*BT
= ResultType
->getAs
<BuiltinType
>()) {
1574 switch (BT
->getKind()) {
1577 case BuiltinType::Float
:
1578 return getTarget().useObjCFPRetForRealType(FloatModeKind::Float
);
1579 case BuiltinType::Double
:
1580 return getTarget().useObjCFPRetForRealType(FloatModeKind::Double
);
1581 case BuiltinType::LongDouble
:
1582 return getTarget().useObjCFPRetForRealType(FloatModeKind::LongDouble
);
1589 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType
) {
1590 if (const ComplexType
*CT
= ResultType
->getAs
<ComplexType
>()) {
1591 if (const BuiltinType
*BT
= CT
->getElementType()->getAs
<BuiltinType
>()) {
1592 if (BT
->getKind() == BuiltinType::LongDouble
)
1593 return getTarget().useObjCFP2RetForComplexLongDouble();
1600 llvm::FunctionType
*CodeGenTypes::GetFunctionType(GlobalDecl GD
) {
1601 const CGFunctionInfo
&FI
= arrangeGlobalDeclaration(GD
);
1602 return GetFunctionType(FI
);
1605 llvm::FunctionType
*
1606 CodeGenTypes::GetFunctionType(const CGFunctionInfo
&FI
) {
1608 bool Inserted
= FunctionsBeingProcessed
.insert(&FI
).second
;
1610 assert(Inserted
&& "Recursively being processed?");
1612 llvm::Type
*resultType
= nullptr;
1613 const ABIArgInfo
&retAI
= FI
.getReturnInfo();
1614 switch (retAI
.getKind()) {
1615 case ABIArgInfo::Expand
:
1616 case ABIArgInfo::IndirectAliased
:
1617 llvm_unreachable("Invalid ABI kind for return argument");
1619 case ABIArgInfo::Extend
:
1620 case ABIArgInfo::Direct
:
1621 resultType
= retAI
.getCoerceToType();
1624 case ABIArgInfo::InAlloca
:
1625 if (retAI
.getInAllocaSRet()) {
1626 // sret things on win32 aren't void, they return the sret pointer.
1627 QualType ret
= FI
.getReturnType();
1628 unsigned addressSpace
= CGM
.getTypes().getTargetAddressSpace(ret
);
1629 resultType
= llvm::PointerType::get(getLLVMContext(), addressSpace
);
1631 resultType
= llvm::Type::getVoidTy(getLLVMContext());
1635 case ABIArgInfo::Indirect
:
1636 case ABIArgInfo::Ignore
:
1637 resultType
= llvm::Type::getVoidTy(getLLVMContext());
1640 case ABIArgInfo::CoerceAndExpand
:
1641 resultType
= retAI
.getUnpaddedCoerceAndExpandType();
1645 ClangToLLVMArgMapping
IRFunctionArgs(getContext(), FI
, true);
1646 SmallVector
<llvm::Type
*, 8> ArgTypes(IRFunctionArgs
.totalIRArgs());
1648 // Add type for sret argument.
1649 if (IRFunctionArgs
.hasSRetArg()) {
1650 QualType Ret
= FI
.getReturnType();
1651 unsigned AddressSpace
= CGM
.getTypes().getTargetAddressSpace(Ret
);
1652 ArgTypes
[IRFunctionArgs
.getSRetArgNo()] =
1653 llvm::PointerType::get(getLLVMContext(), AddressSpace
);
1656 // Add type for inalloca argument.
1657 if (IRFunctionArgs
.hasInallocaArg())
1658 ArgTypes
[IRFunctionArgs
.getInallocaArgNo()] =
1659 llvm::PointerType::getUnqual(getLLVMContext());
1661 // Add in all of the required arguments.
1663 CGFunctionInfo::const_arg_iterator it
= FI
.arg_begin(),
1664 ie
= it
+ FI
.getNumRequiredArgs();
1665 for (; it
!= ie
; ++it
, ++ArgNo
) {
1666 const ABIArgInfo
&ArgInfo
= it
->info
;
1668 // Insert a padding type to ensure proper alignment.
1669 if (IRFunctionArgs
.hasPaddingArg(ArgNo
))
1670 ArgTypes
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
1671 ArgInfo
.getPaddingType();
1673 unsigned FirstIRArg
, NumIRArgs
;
1674 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
1676 switch (ArgInfo
.getKind()) {
1677 case ABIArgInfo::Ignore
:
1678 case ABIArgInfo::InAlloca
:
1679 assert(NumIRArgs
== 0);
1682 case ABIArgInfo::Indirect
:
1683 assert(NumIRArgs
== 1);
1684 // indirect arguments are always on the stack, which is alloca addr space.
1685 ArgTypes
[FirstIRArg
] = llvm::PointerType::get(
1686 getLLVMContext(), CGM
.getDataLayout().getAllocaAddrSpace());
1688 case ABIArgInfo::IndirectAliased
:
1689 assert(NumIRArgs
== 1);
1690 ArgTypes
[FirstIRArg
] = llvm::PointerType::get(
1691 getLLVMContext(), ArgInfo
.getIndirectAddrSpace());
1693 case ABIArgInfo::Extend
:
1694 case ABIArgInfo::Direct
: {
1695 // Fast-isel and the optimizer generally like scalar values better than
1696 // FCAs, so we flatten them if this is safe to do for this argument.
1697 llvm::Type
*argType
= ArgInfo
.getCoerceToType();
1698 llvm::StructType
*st
= dyn_cast
<llvm::StructType
>(argType
);
1699 if (st
&& ArgInfo
.isDirect() && ArgInfo
.getCanBeFlattened()) {
1700 assert(NumIRArgs
== st
->getNumElements());
1701 for (unsigned i
= 0, e
= st
->getNumElements(); i
!= e
; ++i
)
1702 ArgTypes
[FirstIRArg
+ i
] = st
->getElementType(i
);
1704 assert(NumIRArgs
== 1);
1705 ArgTypes
[FirstIRArg
] = argType
;
1710 case ABIArgInfo::CoerceAndExpand
: {
1711 auto ArgTypesIter
= ArgTypes
.begin() + FirstIRArg
;
1712 for (auto *EltTy
: ArgInfo
.getCoerceAndExpandTypeSequence()) {
1713 *ArgTypesIter
++ = EltTy
;
1715 assert(ArgTypesIter
== ArgTypes
.begin() + FirstIRArg
+ NumIRArgs
);
1719 case ABIArgInfo::Expand
:
1720 auto ArgTypesIter
= ArgTypes
.begin() + FirstIRArg
;
1721 getExpandedTypes(it
->type
, ArgTypesIter
);
1722 assert(ArgTypesIter
== ArgTypes
.begin() + FirstIRArg
+ NumIRArgs
);
1727 bool Erased
= FunctionsBeingProcessed
.erase(&FI
); (void)Erased
;
1728 assert(Erased
&& "Not in set?");
1730 return llvm::FunctionType::get(resultType
, ArgTypes
, FI
.isVariadic());
1733 llvm::Type
*CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD
) {
1734 const CXXMethodDecl
*MD
= cast
<CXXMethodDecl
>(GD
.getDecl());
1735 const FunctionProtoType
*FPT
= MD
->getType()->castAs
<FunctionProtoType
>();
1737 if (!isFuncTypeConvertible(FPT
))
1738 return llvm::StructType::get(getLLVMContext());
1740 return GetFunctionType(GD
);
1743 static void AddAttributesFromFunctionProtoType(ASTContext
&Ctx
,
1744 llvm::AttrBuilder
&FuncAttrs
,
1745 const FunctionProtoType
*FPT
) {
1749 if (!isUnresolvedExceptionSpec(FPT
->getExceptionSpecType()) &&
1751 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
1753 unsigned SMEBits
= FPT
->getAArch64SMEAttributes();
1754 if (SMEBits
& FunctionType::SME_PStateSMEnabledMask
)
1755 FuncAttrs
.addAttribute("aarch64_pstate_sm_enabled");
1756 if (SMEBits
& FunctionType::SME_PStateSMCompatibleMask
)
1757 FuncAttrs
.addAttribute("aarch64_pstate_sm_compatible");
1760 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_Preserves
)
1761 FuncAttrs
.addAttribute("aarch64_preserves_za");
1762 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_In
)
1763 FuncAttrs
.addAttribute("aarch64_in_za");
1764 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_Out
)
1765 FuncAttrs
.addAttribute("aarch64_out_za");
1766 if (FunctionType::getArmZAState(SMEBits
) == FunctionType::ARM_InOut
)
1767 FuncAttrs
.addAttribute("aarch64_inout_za");
1770 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_Preserves
)
1771 FuncAttrs
.addAttribute("aarch64_preserves_zt0");
1772 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_In
)
1773 FuncAttrs
.addAttribute("aarch64_in_zt0");
1774 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_Out
)
1775 FuncAttrs
.addAttribute("aarch64_out_zt0");
1776 if (FunctionType::getArmZT0State(SMEBits
) == FunctionType::ARM_InOut
)
1777 FuncAttrs
.addAttribute("aarch64_inout_zt0");
1780 static void AddAttributesFromOMPAssumes(llvm::AttrBuilder
&FuncAttrs
,
1781 const Decl
*Callee
) {
1785 SmallVector
<StringRef
, 4> Attrs
;
1787 for (const OMPAssumeAttr
*AA
: Callee
->specific_attrs
<OMPAssumeAttr
>())
1788 AA
->getAssumption().split(Attrs
, ",");
1791 FuncAttrs
.addAttribute(llvm::AssumptionAttrKey
,
1792 llvm::join(Attrs
.begin(), Attrs
.end(), ","));
1795 bool CodeGenModule::MayDropFunctionReturn(const ASTContext
&Context
,
1796 QualType ReturnType
) const {
1797 // We can't just discard the return value for a record type with a
1798 // complex destructor or a non-trivially copyable type.
1799 if (const RecordType
*RT
=
1800 ReturnType
.getCanonicalType()->getAs
<RecordType
>()) {
1801 if (const auto *ClassDecl
= dyn_cast
<CXXRecordDecl
>(RT
->getDecl()))
1802 return ClassDecl
->hasTrivialDestructor();
1804 return ReturnType
.isTriviallyCopyableType(Context
);
1807 static bool HasStrictReturn(const CodeGenModule
&Module
, QualType RetTy
,
1808 const Decl
*TargetDecl
) {
1809 // As-is msan can not tolerate noundef mismatch between caller and
1810 // implementation. Mismatch is possible for e.g. indirect calls from C-caller
1811 // into C++. Such mismatches lead to confusing false reports. To avoid
1812 // expensive workaround on msan we enforce initialization event in uncommon
1813 // cases where it's allowed.
1814 if (Module
.getLangOpts().Sanitize
.has(SanitizerKind::Memory
))
1816 // C++ explicitly makes returning undefined values UB. C's rule only applies
1817 // to used values, so we never mark them noundef for now.
1818 if (!Module
.getLangOpts().CPlusPlus
)
1821 if (const FunctionDecl
*FDecl
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
1822 if (FDecl
->isExternC())
1824 } else if (const VarDecl
*VDecl
= dyn_cast
<VarDecl
>(TargetDecl
)) {
1825 // Function pointer.
1826 if (VDecl
->isExternC())
1831 // We don't want to be too aggressive with the return checking, unless
1832 // it's explicit in the code opts or we're using an appropriate sanitizer.
1833 // Try to respect what the programmer intended.
1834 return Module
.getCodeGenOpts().StrictReturn
||
1835 !Module
.MayDropFunctionReturn(Module
.getContext(), RetTy
) ||
1836 Module
.getLangOpts().Sanitize
.has(SanitizerKind::Return
);
1839 /// Add denormal-fp-math and denormal-fp-math-f32 as appropriate for the
1840 /// requested denormal behavior, accounting for the overriding behavior of the
1842 static void addDenormalModeAttrs(llvm::DenormalMode FPDenormalMode
,
1843 llvm::DenormalMode FP32DenormalMode
,
1844 llvm::AttrBuilder
&FuncAttrs
) {
1845 if (FPDenormalMode
!= llvm::DenormalMode::getDefault())
1846 FuncAttrs
.addAttribute("denormal-fp-math", FPDenormalMode
.str());
1848 if (FP32DenormalMode
!= FPDenormalMode
&& FP32DenormalMode
.isValid())
1849 FuncAttrs
.addAttribute("denormal-fp-math-f32", FP32DenormalMode
.str());
1852 /// Add default attributes to a function, which have merge semantics under
1853 /// -mlink-builtin-bitcode and should not simply overwrite any existing
1854 /// attributes in the linked library.
1856 addMergableDefaultFunctionAttributes(const CodeGenOptions
&CodeGenOpts
,
1857 llvm::AttrBuilder
&FuncAttrs
) {
1858 addDenormalModeAttrs(CodeGenOpts
.FPDenormalMode
, CodeGenOpts
.FP32DenormalMode
,
1862 static void getTrivialDefaultFunctionAttributes(
1863 StringRef Name
, bool HasOptnone
, const CodeGenOptions
&CodeGenOpts
,
1864 const LangOptions
&LangOpts
, bool AttrOnCallSite
,
1865 llvm::AttrBuilder
&FuncAttrs
) {
1866 // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.
1868 if (CodeGenOpts
.OptimizeSize
)
1869 FuncAttrs
.addAttribute(llvm::Attribute::OptimizeForSize
);
1870 if (CodeGenOpts
.OptimizeSize
== 2)
1871 FuncAttrs
.addAttribute(llvm::Attribute::MinSize
);
1874 if (CodeGenOpts
.DisableRedZone
)
1875 FuncAttrs
.addAttribute(llvm::Attribute::NoRedZone
);
1876 if (CodeGenOpts
.IndirectTlsSegRefs
)
1877 FuncAttrs
.addAttribute("indirect-tls-seg-refs");
1878 if (CodeGenOpts
.NoImplicitFloat
)
1879 FuncAttrs
.addAttribute(llvm::Attribute::NoImplicitFloat
);
1881 if (AttrOnCallSite
) {
1882 // Attributes that should go on the call site only.
1883 // FIXME: Look for 'BuiltinAttr' on the function rather than re-checking
1884 // the -fno-builtin-foo list.
1885 if (!CodeGenOpts
.SimplifyLibCalls
|| LangOpts
.isNoBuiltinFunc(Name
))
1886 FuncAttrs
.addAttribute(llvm::Attribute::NoBuiltin
);
1887 if (!CodeGenOpts
.TrapFuncName
.empty())
1888 FuncAttrs
.addAttribute("trap-func-name", CodeGenOpts
.TrapFuncName
);
1890 switch (CodeGenOpts
.getFramePointer()) {
1891 case CodeGenOptions::FramePointerKind::None
:
1892 // This is the default behavior.
1894 case CodeGenOptions::FramePointerKind::Reserved
:
1895 case CodeGenOptions::FramePointerKind::NonLeaf
:
1896 case CodeGenOptions::FramePointerKind::All
:
1897 FuncAttrs
.addAttribute("frame-pointer",
1898 CodeGenOptions::getFramePointerKindName(
1899 CodeGenOpts
.getFramePointer()));
1902 if (CodeGenOpts
.LessPreciseFPMAD
)
1903 FuncAttrs
.addAttribute("less-precise-fpmad", "true");
1905 if (CodeGenOpts
.NullPointerIsValid
)
1906 FuncAttrs
.addAttribute(llvm::Attribute::NullPointerIsValid
);
1908 if (LangOpts
.getDefaultExceptionMode() == LangOptions::FPE_Ignore
)
1909 FuncAttrs
.addAttribute("no-trapping-math", "true");
1911 // TODO: Are these all needed?
1912 // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
1913 if (LangOpts
.NoHonorInfs
)
1914 FuncAttrs
.addAttribute("no-infs-fp-math", "true");
1915 if (LangOpts
.NoHonorNaNs
)
1916 FuncAttrs
.addAttribute("no-nans-fp-math", "true");
1917 if (LangOpts
.ApproxFunc
)
1918 FuncAttrs
.addAttribute("approx-func-fp-math", "true");
1919 if (LangOpts
.AllowFPReassoc
&& LangOpts
.AllowRecip
&&
1920 LangOpts
.NoSignedZero
&& LangOpts
.ApproxFunc
&&
1921 (LangOpts
.getDefaultFPContractMode() ==
1922 LangOptions::FPModeKind::FPM_Fast
||
1923 LangOpts
.getDefaultFPContractMode() ==
1924 LangOptions::FPModeKind::FPM_FastHonorPragmas
))
1925 FuncAttrs
.addAttribute("unsafe-fp-math", "true");
1926 if (CodeGenOpts
.SoftFloat
)
1927 FuncAttrs
.addAttribute("use-soft-float", "true");
1928 FuncAttrs
.addAttribute("stack-protector-buffer-size",
1929 llvm::utostr(CodeGenOpts
.SSPBufferSize
));
1930 if (LangOpts
.NoSignedZero
)
1931 FuncAttrs
.addAttribute("no-signed-zeros-fp-math", "true");
1933 // TODO: Reciprocal estimate codegen options should apply to instructions?
1934 const std::vector
<std::string
> &Recips
= CodeGenOpts
.Reciprocals
;
1935 if (!Recips
.empty())
1936 FuncAttrs
.addAttribute("reciprocal-estimates",
1937 llvm::join(Recips
, ","));
1939 if (!CodeGenOpts
.PreferVectorWidth
.empty() &&
1940 CodeGenOpts
.PreferVectorWidth
!= "none")
1941 FuncAttrs
.addAttribute("prefer-vector-width",
1942 CodeGenOpts
.PreferVectorWidth
);
1944 if (CodeGenOpts
.StackRealignment
)
1945 FuncAttrs
.addAttribute("stackrealign");
1946 if (CodeGenOpts
.Backchain
)
1947 FuncAttrs
.addAttribute("backchain");
1948 if (CodeGenOpts
.EnableSegmentedStacks
)
1949 FuncAttrs
.addAttribute("split-stack");
1951 if (CodeGenOpts
.SpeculativeLoadHardening
)
1952 FuncAttrs
.addAttribute(llvm::Attribute::SpeculativeLoadHardening
);
1954 // Add zero-call-used-regs attribute.
1955 switch (CodeGenOpts
.getZeroCallUsedRegs()) {
1956 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip
:
1957 FuncAttrs
.removeAttribute("zero-call-used-regs");
1959 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg
:
1960 FuncAttrs
.addAttribute("zero-call-used-regs", "used-gpr-arg");
1962 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR
:
1963 FuncAttrs
.addAttribute("zero-call-used-regs", "used-gpr");
1965 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg
:
1966 FuncAttrs
.addAttribute("zero-call-used-regs", "used-arg");
1968 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used
:
1969 FuncAttrs
.addAttribute("zero-call-used-regs", "used");
1971 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg
:
1972 FuncAttrs
.addAttribute("zero-call-used-regs", "all-gpr-arg");
1974 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR
:
1975 FuncAttrs
.addAttribute("zero-call-used-regs", "all-gpr");
1977 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg
:
1978 FuncAttrs
.addAttribute("zero-call-used-regs", "all-arg");
1980 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All
:
1981 FuncAttrs
.addAttribute("zero-call-used-regs", "all");
1986 if (LangOpts
.assumeFunctionsAreConvergent()) {
1987 // Conservatively, mark all functions and calls in CUDA and OpenCL as
1988 // convergent (meaning, they may call an intrinsically convergent op, such
1989 // as __syncthreads() / barrier(), and so can't have certain optimizations
1990 // applied around them). LLVM will remove this attribute where it safely
1992 FuncAttrs
.addAttribute(llvm::Attribute::Convergent
);
1995 // TODO: NoUnwind attribute should be added for other GPU modes HIP,
1996 // OpenMP offload. AFAIK, neither of them support exceptions in device code.
1997 if ((LangOpts
.CUDA
&& LangOpts
.CUDAIsDevice
) || LangOpts
.OpenCL
||
1998 LangOpts
.SYCLIsDevice
) {
1999 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2002 for (StringRef Attr
: CodeGenOpts
.DefaultFunctionAttrs
) {
2003 StringRef Var
, Value
;
2004 std::tie(Var
, Value
) = Attr
.split('=');
2005 FuncAttrs
.addAttribute(Var
, Value
);
2008 TargetInfo::BranchProtectionInfo
BPI(LangOpts
);
2009 TargetCodeGenInfo::initBranchProtectionFnAttributes(BPI
, FuncAttrs
);
2012 /// Merges `target-features` from \TargetOpts and \F, and sets the result in
2014 /// * features from \F are always kept
2015 /// * a feature from \TargetOpts is kept if itself and its opposite are absent
2018 overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder
&FuncAttr
,
2019 const llvm::Function
&F
,
2020 const TargetOptions
&TargetOpts
) {
2021 auto FFeatures
= F
.getFnAttribute("target-features");
2023 llvm::StringSet
<> MergedNames
;
2024 SmallVector
<StringRef
> MergedFeatures
;
2025 MergedFeatures
.reserve(TargetOpts
.Features
.size());
2027 auto AddUnmergedFeatures
= [&](auto &&FeatureRange
) {
2028 for (StringRef Feature
: FeatureRange
) {
2029 if (Feature
.empty())
2031 assert(Feature
[0] == '+' || Feature
[0] == '-');
2032 StringRef Name
= Feature
.drop_front(1);
2033 bool Merged
= !MergedNames
.insert(Name
).second
;
2035 MergedFeatures
.push_back(Feature
);
2039 if (FFeatures
.isValid())
2040 AddUnmergedFeatures(llvm::split(FFeatures
.getValueAsString(), ','));
2041 AddUnmergedFeatures(TargetOpts
.Features
);
2043 if (!MergedFeatures
.empty()) {
2044 llvm::sort(MergedFeatures
);
2045 FuncAttr
.addAttribute("target-features", llvm::join(MergedFeatures
, ","));
2049 void CodeGen::mergeDefaultFunctionDefinitionAttributes(
2050 llvm::Function
&F
, const CodeGenOptions
&CodeGenOpts
,
2051 const LangOptions
&LangOpts
, const TargetOptions
&TargetOpts
,
2052 bool WillInternalize
) {
2054 llvm::AttrBuilder
FuncAttrs(F
.getContext());
2055 // Here we only extract the options that are relevant compared to the version
2056 // from GetCPUAndFeaturesAttributes.
2057 if (!TargetOpts
.CPU
.empty())
2058 FuncAttrs
.addAttribute("target-cpu", TargetOpts
.CPU
);
2059 if (!TargetOpts
.TuneCPU
.empty())
2060 FuncAttrs
.addAttribute("tune-cpu", TargetOpts
.TuneCPU
);
2062 ::getTrivialDefaultFunctionAttributes(F
.getName(), F
.hasOptNone(),
2063 CodeGenOpts
, LangOpts
,
2064 /*AttrOnCallSite=*/false, FuncAttrs
);
2066 if (!WillInternalize
&& F
.isInterposable()) {
2067 // Do not promote "dynamic" denormal-fp-math to this translation unit's
2068 // setting for weak functions that won't be internalized. The user has no
2069 // real control for how builtin bitcode is linked, so we shouldn't assume
2070 // later copies will use a consistent mode.
2071 F
.addFnAttrs(FuncAttrs
);
2075 llvm::AttributeMask AttrsToRemove
;
2077 llvm::DenormalMode DenormModeToMerge
= F
.getDenormalModeRaw();
2078 llvm::DenormalMode DenormModeToMergeF32
= F
.getDenormalModeF32Raw();
2079 llvm::DenormalMode Merged
=
2080 CodeGenOpts
.FPDenormalMode
.mergeCalleeMode(DenormModeToMerge
);
2081 llvm::DenormalMode MergedF32
= CodeGenOpts
.FP32DenormalMode
;
2083 if (DenormModeToMergeF32
.isValid()) {
2085 CodeGenOpts
.FP32DenormalMode
.mergeCalleeMode(DenormModeToMergeF32
);
2088 if (Merged
== llvm::DenormalMode::getDefault()) {
2089 AttrsToRemove
.addAttribute("denormal-fp-math");
2090 } else if (Merged
!= DenormModeToMerge
) {
2091 // Overwrite existing attribute
2092 FuncAttrs
.addAttribute("denormal-fp-math",
2093 CodeGenOpts
.FPDenormalMode
.str());
2096 if (MergedF32
== llvm::DenormalMode::getDefault()) {
2097 AttrsToRemove
.addAttribute("denormal-fp-math-f32");
2098 } else if (MergedF32
!= DenormModeToMergeF32
) {
2099 // Overwrite existing attribute
2100 FuncAttrs
.addAttribute("denormal-fp-math-f32",
2101 CodeGenOpts
.FP32DenormalMode
.str());
2104 F
.removeFnAttrs(AttrsToRemove
);
2105 addDenormalModeAttrs(Merged
, MergedF32
, FuncAttrs
);
2107 overrideFunctionFeaturesWithTargetFeatures(FuncAttrs
, F
, TargetOpts
);
2109 F
.addFnAttrs(FuncAttrs
);
2112 void CodeGenModule::getTrivialDefaultFunctionAttributes(
2113 StringRef Name
, bool HasOptnone
, bool AttrOnCallSite
,
2114 llvm::AttrBuilder
&FuncAttrs
) {
2115 ::getTrivialDefaultFunctionAttributes(Name
, HasOptnone
, getCodeGenOpts(),
2116 getLangOpts(), AttrOnCallSite
,
2120 void CodeGenModule::getDefaultFunctionAttributes(StringRef Name
,
2122 bool AttrOnCallSite
,
2123 llvm::AttrBuilder
&FuncAttrs
) {
2124 getTrivialDefaultFunctionAttributes(Name
, HasOptnone
, AttrOnCallSite
,
2126 // If we're just getting the default, get the default values for mergeable
2128 if (!AttrOnCallSite
)
2129 addMergableDefaultFunctionAttributes(CodeGenOpts
, FuncAttrs
);
2132 void CodeGenModule::addDefaultFunctionDefinitionAttributes(
2133 llvm::AttrBuilder
&attrs
) {
2134 getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,
2135 /*for call*/ false, attrs
);
2136 GetCPUAndFeaturesAttributes(GlobalDecl(), attrs
);
2139 static void addNoBuiltinAttributes(llvm::AttrBuilder
&FuncAttrs
,
2140 const LangOptions
&LangOpts
,
2141 const NoBuiltinAttr
*NBA
= nullptr) {
2142 auto AddNoBuiltinAttr
= [&FuncAttrs
](StringRef BuiltinName
) {
2143 SmallString
<32> AttributeName
;
2144 AttributeName
+= "no-builtin-";
2145 AttributeName
+= BuiltinName
;
2146 FuncAttrs
.addAttribute(AttributeName
);
2149 // First, handle the language options passed through -fno-builtin.
2150 if (LangOpts
.NoBuiltin
) {
2151 // -fno-builtin disables them all.
2152 FuncAttrs
.addAttribute("no-builtins");
2156 // Then, add attributes for builtins specified through -fno-builtin-<name>.
2157 llvm::for_each(LangOpts
.NoBuiltinFuncs
, AddNoBuiltinAttr
);
2159 // Now, let's check the __attribute__((no_builtin("...")) attribute added to
2164 // If there is a wildcard in the builtin names specified through the
2165 // attribute, disable them all.
2166 if (llvm::is_contained(NBA
->builtinNames(), "*")) {
2167 FuncAttrs
.addAttribute("no-builtins");
2171 // And last, add the rest of the builtin names.
2172 llvm::for_each(NBA
->builtinNames(), AddNoBuiltinAttr
);
2175 static bool DetermineNoUndef(QualType QTy
, CodeGenTypes
&Types
,
2176 const llvm::DataLayout
&DL
, const ABIArgInfo
&AI
,
2177 bool CheckCoerce
= true) {
2178 llvm::Type
*Ty
= Types
.ConvertTypeForMem(QTy
);
2179 if (AI
.getKind() == ABIArgInfo::Indirect
||
2180 AI
.getKind() == ABIArgInfo::IndirectAliased
)
2182 if (AI
.getKind() == ABIArgInfo::Extend
)
2184 if (!DL
.typeSizeEqualsStoreSize(Ty
))
2185 // TODO: This will result in a modest amount of values not marked noundef
2186 // when they could be. We care about values that *invisibly* contain undef
2187 // bits from the perspective of LLVM IR.
2189 if (CheckCoerce
&& AI
.canHaveCoerceToType()) {
2190 llvm::Type
*CoerceTy
= AI
.getCoerceToType();
2191 if (llvm::TypeSize::isKnownGT(DL
.getTypeSizeInBits(CoerceTy
),
2192 DL
.getTypeSizeInBits(Ty
)))
2193 // If we're coercing to a type with a greater size than the canonical one,
2194 // we're introducing new undef bits.
2195 // Coercing to a type of smaller or equal size is ok, as we know that
2196 // there's no internal padding (typeSizeEqualsStoreSize).
2199 if (QTy
->isBitIntType())
2201 if (QTy
->isReferenceType())
2203 if (QTy
->isNullPtrType())
2205 if (QTy
->isMemberPointerType())
2206 // TODO: Some member pointers are `noundef`, but it depends on the ABI. For
2207 // now, never mark them.
2209 if (QTy
->isScalarType()) {
2210 if (const ComplexType
*Complex
= dyn_cast
<ComplexType
>(QTy
))
2211 return DetermineNoUndef(Complex
->getElementType(), Types
, DL
, AI
, false);
2214 if (const VectorType
*Vector
= dyn_cast
<VectorType
>(QTy
))
2215 return DetermineNoUndef(Vector
->getElementType(), Types
, DL
, AI
, false);
2216 if (const MatrixType
*Matrix
= dyn_cast
<MatrixType
>(QTy
))
2217 return DetermineNoUndef(Matrix
->getElementType(), Types
, DL
, AI
, false);
2218 if (const ArrayType
*Array
= dyn_cast
<ArrayType
>(QTy
))
2219 return DetermineNoUndef(Array
->getElementType(), Types
, DL
, AI
, false);
2221 // TODO: Some structs may be `noundef`, in specific situations.
2225 /// Check if the argument of a function has maybe_undef attribute.
2226 static bool IsArgumentMaybeUndef(const Decl
*TargetDecl
,
2227 unsigned NumRequiredArgs
, unsigned ArgNo
) {
2228 const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
);
2232 // Assume variadic arguments do not have maybe_undef attribute.
2233 if (ArgNo
>= NumRequiredArgs
)
2236 // Check if argument has maybe_undef attribute.
2237 if (ArgNo
< FD
->getNumParams()) {
2238 const ParmVarDecl
*Param
= FD
->getParamDecl(ArgNo
);
2239 if (Param
&& Param
->hasAttr
<MaybeUndefAttr
>())
2246 /// Test if it's legal to apply nofpclass for the given parameter type and it's
2247 /// lowered IR type.
2248 static bool canApplyNoFPClass(const ABIArgInfo
&AI
, QualType ParamType
,
2250 // Should only apply to FP types in the source, not ABI promoted.
2251 if (!ParamType
->hasFloatingRepresentation())
2254 // The promoted-to IR type also needs to support nofpclass.
2255 llvm::Type
*IRTy
= AI
.getCoerceToType();
2256 if (llvm::AttributeFuncs::isNoFPClassCompatibleType(IRTy
))
2259 if (llvm::StructType
*ST
= dyn_cast
<llvm::StructType
>(IRTy
)) {
2260 return !IsReturn
&& AI
.getCanBeFlattened() &&
2261 llvm::all_of(ST
->elements(), [](llvm::Type
*Ty
) {
2262 return llvm::AttributeFuncs::isNoFPClassCompatibleType(Ty
);
2269 /// Return the nofpclass mask that can be applied to floating-point parameters.
2270 static llvm::FPClassTest
getNoFPClassTestMask(const LangOptions
&LangOpts
) {
2271 llvm::FPClassTest Mask
= llvm::fcNone
;
2272 if (LangOpts
.NoHonorInfs
)
2273 Mask
|= llvm::fcInf
;
2274 if (LangOpts
.NoHonorNaNs
)
2275 Mask
|= llvm::fcNan
;
2279 void CodeGenModule::AdjustMemoryAttribute(StringRef Name
,
2280 CGCalleeInfo CalleeInfo
,
2281 llvm::AttributeList
&Attrs
) {
2282 if (Attrs
.getMemoryEffects().getModRef() == llvm::ModRefInfo::NoModRef
) {
2283 Attrs
= Attrs
.removeFnAttribute(getLLVMContext(), llvm::Attribute::Memory
);
2284 llvm::Attribute MemoryAttr
= llvm::Attribute::getWithMemoryEffects(
2285 getLLVMContext(), llvm::MemoryEffects::writeOnly());
2286 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), MemoryAttr
);
2290 /// Construct the IR attribute list of a function or call.
2292 /// When adding an attribute, please consider where it should be handled:
2294 /// - getDefaultFunctionAttributes is for attributes that are essentially
2295 /// part of the global target configuration (but perhaps can be
2296 /// overridden on a per-function basis). Adding attributes there
2297 /// will cause them to also be set in frontends that build on Clang's
2298 /// target-configuration logic, as well as for code defined in library
2299 /// modules such as CUDA's libdevice.
2301 /// - ConstructAttributeList builds on top of getDefaultFunctionAttributes
2302 /// and adds declaration-specific, convention-specific, and
2303 /// frontend-specific logic. The last is of particular importance:
2304 /// attributes that restrict how the frontend generates code must be
2305 /// added here rather than getDefaultFunctionAttributes.
2307 void CodeGenModule::ConstructAttributeList(StringRef Name
,
2308 const CGFunctionInfo
&FI
,
2309 CGCalleeInfo CalleeInfo
,
2310 llvm::AttributeList
&AttrList
,
2311 unsigned &CallingConv
,
2312 bool AttrOnCallSite
, bool IsThunk
) {
2313 llvm::AttrBuilder
FuncAttrs(getLLVMContext());
2314 llvm::AttrBuilder
RetAttrs(getLLVMContext());
2316 // Collect function IR attributes from the CC lowering.
2317 // We'll collect the paramete and result attributes later.
2318 CallingConv
= FI
.getEffectiveCallingConvention();
2319 if (FI
.isNoReturn())
2320 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2321 if (FI
.isCmseNSCall())
2322 FuncAttrs
.addAttribute("cmse_nonsecure_call");
2324 // Collect function IR attributes from the callee prototype if we have one.
2325 AddAttributesFromFunctionProtoType(getContext(), FuncAttrs
,
2326 CalleeInfo
.getCalleeFunctionProtoType());
2328 const Decl
*TargetDecl
= CalleeInfo
.getCalleeDecl().getDecl();
2330 // Attach assumption attributes to the declaration. If this is a call
2331 // site, attach assumptions from the caller to the call as well.
2332 AddAttributesFromOMPAssumes(FuncAttrs
, TargetDecl
);
2334 bool HasOptnone
= false;
2335 // The NoBuiltinAttr attached to the target FunctionDecl.
2336 const NoBuiltinAttr
*NBA
= nullptr;
2338 // Some ABIs may result in additional accesses to arguments that may
2339 // otherwise not be present.
2340 auto AddPotentialArgAccess
= [&]() {
2341 llvm::Attribute A
= FuncAttrs
.getAttribute(llvm::Attribute::Memory
);
2343 FuncAttrs
.addMemoryAttr(A
.getMemoryEffects() |
2344 llvm::MemoryEffects::argMemOnly());
2347 // Collect function IR attributes based on declaration-specific
2349 // FIXME: handle sseregparm someday...
2351 if (TargetDecl
->hasAttr
<ReturnsTwiceAttr
>())
2352 FuncAttrs
.addAttribute(llvm::Attribute::ReturnsTwice
);
2353 if (TargetDecl
->hasAttr
<NoThrowAttr
>())
2354 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2355 if (TargetDecl
->hasAttr
<NoReturnAttr
>())
2356 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2357 if (TargetDecl
->hasAttr
<ColdAttr
>())
2358 FuncAttrs
.addAttribute(llvm::Attribute::Cold
);
2359 if (TargetDecl
->hasAttr
<HotAttr
>())
2360 FuncAttrs
.addAttribute(llvm::Attribute::Hot
);
2361 if (TargetDecl
->hasAttr
<NoDuplicateAttr
>())
2362 FuncAttrs
.addAttribute(llvm::Attribute::NoDuplicate
);
2363 if (TargetDecl
->hasAttr
<ConvergentAttr
>())
2364 FuncAttrs
.addAttribute(llvm::Attribute::Convergent
);
2366 if (const FunctionDecl
*Fn
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
2367 AddAttributesFromFunctionProtoType(
2368 getContext(), FuncAttrs
, Fn
->getType()->getAs
<FunctionProtoType
>());
2369 if (AttrOnCallSite
&& Fn
->isReplaceableGlobalAllocationFunction()) {
2370 // A sane operator new returns a non-aliasing pointer.
2371 auto Kind
= Fn
->getDeclName().getCXXOverloadedOperator();
2372 if (getCodeGenOpts().AssumeSaneOperatorNew
&&
2373 (Kind
== OO_New
|| Kind
== OO_Array_New
))
2374 RetAttrs
.addAttribute(llvm::Attribute::NoAlias
);
2376 const CXXMethodDecl
*MD
= dyn_cast
<CXXMethodDecl
>(Fn
);
2377 const bool IsVirtualCall
= MD
&& MD
->isVirtual();
2378 // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a
2379 // virtual function. These attributes are not inherited by overloads.
2380 if (!(AttrOnCallSite
&& IsVirtualCall
)) {
2381 if (Fn
->isNoReturn())
2382 FuncAttrs
.addAttribute(llvm::Attribute::NoReturn
);
2383 NBA
= Fn
->getAttr
<NoBuiltinAttr
>();
2387 if (isa
<FunctionDecl
>(TargetDecl
) || isa
<VarDecl
>(TargetDecl
)) {
2388 // Only place nomerge attribute on call sites, never functions. This
2389 // allows it to work on indirect virtual function calls.
2390 if (AttrOnCallSite
&& TargetDecl
->hasAttr
<NoMergeAttr
>())
2391 FuncAttrs
.addAttribute(llvm::Attribute::NoMerge
);
2394 // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
2395 if (TargetDecl
->hasAttr
<ConstAttr
>()) {
2396 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::none());
2397 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2398 // gcc specifies that 'const' functions have greater restrictions than
2399 // 'pure' functions, so they also cannot have infinite loops.
2400 FuncAttrs
.addAttribute(llvm::Attribute::WillReturn
);
2401 } else if (TargetDecl
->hasAttr
<PureAttr
>()) {
2402 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::readOnly());
2403 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2404 // gcc specifies that 'pure' functions cannot have infinite loops.
2405 FuncAttrs
.addAttribute(llvm::Attribute::WillReturn
);
2406 } else if (TargetDecl
->hasAttr
<NoAliasAttr
>()) {
2407 FuncAttrs
.addMemoryAttr(llvm::MemoryEffects::inaccessibleOrArgMemOnly());
2408 FuncAttrs
.addAttribute(llvm::Attribute::NoUnwind
);
2410 if (TargetDecl
->hasAttr
<RestrictAttr
>())
2411 RetAttrs
.addAttribute(llvm::Attribute::NoAlias
);
2412 if (TargetDecl
->hasAttr
<ReturnsNonNullAttr
>() &&
2413 !CodeGenOpts
.NullPointerIsValid
)
2414 RetAttrs
.addAttribute(llvm::Attribute::NonNull
);
2415 if (TargetDecl
->hasAttr
<AnyX86NoCallerSavedRegistersAttr
>())
2416 FuncAttrs
.addAttribute("no_caller_saved_registers");
2417 if (TargetDecl
->hasAttr
<AnyX86NoCfCheckAttr
>())
2418 FuncAttrs
.addAttribute(llvm::Attribute::NoCfCheck
);
2419 if (TargetDecl
->hasAttr
<LeafAttr
>())
2420 FuncAttrs
.addAttribute(llvm::Attribute::NoCallback
);
2422 HasOptnone
= TargetDecl
->hasAttr
<OptimizeNoneAttr
>();
2423 if (auto *AllocSize
= TargetDecl
->getAttr
<AllocSizeAttr
>()) {
2424 std::optional
<unsigned> NumElemsParam
;
2425 if (AllocSize
->getNumElemsParam().isValid())
2426 NumElemsParam
= AllocSize
->getNumElemsParam().getLLVMIndex();
2427 FuncAttrs
.addAllocSizeAttr(AllocSize
->getElemSizeParam().getLLVMIndex(),
2431 if (TargetDecl
->hasAttr
<OpenCLKernelAttr
>()) {
2432 if (getLangOpts().OpenCLVersion
<= 120) {
2433 // OpenCL v1.2 Work groups are always uniform
2434 FuncAttrs
.addAttribute("uniform-work-group-size", "true");
2436 // OpenCL v2.0 Work groups may be whether uniform or not.
2437 // '-cl-uniform-work-group-size' compile option gets a hint
2438 // to the compiler that the global work-size be a multiple of
2439 // the work-group size specified to clEnqueueNDRangeKernel
2440 // (i.e. work groups are uniform).
2441 FuncAttrs
.addAttribute(
2442 "uniform-work-group-size",
2443 llvm::toStringRef(getLangOpts().OffloadUniformBlock
));
2447 if (TargetDecl
->hasAttr
<CUDAGlobalAttr
>() &&
2448 getLangOpts().OffloadUniformBlock
)
2449 FuncAttrs
.addAttribute("uniform-work-group-size", "true");
2451 if (TargetDecl
->hasAttr
<ArmLocallyStreamingAttr
>())
2452 FuncAttrs
.addAttribute("aarch64_pstate_sm_body");
2455 // Attach "no-builtins" attributes to:
2456 // * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".
2457 // * definitions: "no-builtins" or "no-builtin-<name>" only.
2458 // The attributes can come from:
2459 // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
2460 // * FunctionDecl attributes: __attribute__((no_builtin(...)))
2461 addNoBuiltinAttributes(FuncAttrs
, getLangOpts(), NBA
);
2463 // Collect function IR attributes based on global settiings.
2464 getDefaultFunctionAttributes(Name
, HasOptnone
, AttrOnCallSite
, FuncAttrs
);
2466 // Override some default IR attributes based on declaration-specific
2469 if (TargetDecl
->hasAttr
<NoSpeculativeLoadHardeningAttr
>())
2470 FuncAttrs
.removeAttribute(llvm::Attribute::SpeculativeLoadHardening
);
2471 if (TargetDecl
->hasAttr
<SpeculativeLoadHardeningAttr
>())
2472 FuncAttrs
.addAttribute(llvm::Attribute::SpeculativeLoadHardening
);
2473 if (TargetDecl
->hasAttr
<NoSplitStackAttr
>())
2474 FuncAttrs
.removeAttribute("split-stack");
2475 if (TargetDecl
->hasAttr
<ZeroCallUsedRegsAttr
>()) {
2476 // A function "__attribute__((...))" overrides the command-line flag.
2478 TargetDecl
->getAttr
<ZeroCallUsedRegsAttr
>()->getZeroCallUsedRegs();
2479 FuncAttrs
.removeAttribute("zero-call-used-regs");
2480 FuncAttrs
.addAttribute(
2481 "zero-call-used-regs",
2482 ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind
));
2485 // Add NonLazyBind attribute to function declarations when -fno-plt
2487 // FIXME: what if we just haven't processed the function definition
2488 // yet, or if it's an external definition like C99 inline?
2489 if (CodeGenOpts
.NoPLT
) {
2490 if (auto *Fn
= dyn_cast
<FunctionDecl
>(TargetDecl
)) {
2491 if (!Fn
->isDefined() && !AttrOnCallSite
) {
2492 FuncAttrs
.addAttribute(llvm::Attribute::NonLazyBind
);
2498 // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
2499 // functions with -funique-internal-linkage-names.
2500 if (TargetDecl
&& CodeGenOpts
.UniqueInternalLinkageNames
) {
2501 if (const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
)) {
2502 if (!FD
->isExternallyVisible())
2503 FuncAttrs
.addAttribute("sample-profile-suffix-elision-policy",
2508 // Collect non-call-site function IR attributes from declaration-specific
2510 if (!AttrOnCallSite
) {
2511 if (TargetDecl
&& TargetDecl
->hasAttr
<CmseNSEntryAttr
>())
2512 FuncAttrs
.addAttribute("cmse_nonsecure_entry");
2514 // Whether tail calls are enabled.
2515 auto shouldDisableTailCalls
= [&] {
2516 // Should this be honored in getDefaultFunctionAttributes?
2517 if (CodeGenOpts
.DisableTailCalls
)
2523 if (TargetDecl
->hasAttr
<DisableTailCallsAttr
>() ||
2524 TargetDecl
->hasAttr
<AnyX86InterruptAttr
>())
2527 if (CodeGenOpts
.NoEscapingBlockTailCalls
) {
2528 if (const auto *BD
= dyn_cast
<BlockDecl
>(TargetDecl
))
2529 if (!BD
->doesNotEscape())
2535 if (shouldDisableTailCalls())
2536 FuncAttrs
.addAttribute("disable-tail-calls", "true");
2538 // CPU/feature overrides. addDefaultFunctionDefinitionAttributes
2539 // handles these separately to set them based on the global defaults.
2540 GetCPUAndFeaturesAttributes(CalleeInfo
.getCalleeDecl(), FuncAttrs
);
2543 // Collect attributes from arguments and return values.
2544 ClangToLLVMArgMapping
IRFunctionArgs(getContext(), FI
);
2546 QualType RetTy
= FI
.getReturnType();
2547 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
2548 const llvm::DataLayout
&DL
= getDataLayout();
2550 // Determine if the return type could be partially undef
2551 if (CodeGenOpts
.EnableNoundefAttrs
&&
2552 HasStrictReturn(*this, RetTy
, TargetDecl
)) {
2553 if (!RetTy
->isVoidType() && RetAI
.getKind() != ABIArgInfo::Indirect
&&
2554 DetermineNoUndef(RetTy
, getTypes(), DL
, RetAI
))
2555 RetAttrs
.addAttribute(llvm::Attribute::NoUndef
);
2558 switch (RetAI
.getKind()) {
2559 case ABIArgInfo::Extend
:
2560 if (RetAI
.isSignExt())
2561 RetAttrs
.addAttribute(llvm::Attribute::SExt
);
2563 RetAttrs
.addAttribute(llvm::Attribute::ZExt
);
2565 case ABIArgInfo::Direct
:
2566 if (RetAI
.getInReg())
2567 RetAttrs
.addAttribute(llvm::Attribute::InReg
);
2569 if (canApplyNoFPClass(RetAI
, RetTy
, true))
2570 RetAttrs
.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2573 case ABIArgInfo::Ignore
:
2576 case ABIArgInfo::InAlloca
:
2577 case ABIArgInfo::Indirect
: {
2578 // inalloca and sret disable readnone and readonly
2579 AddPotentialArgAccess();
2583 case ABIArgInfo::CoerceAndExpand
:
2586 case ABIArgInfo::Expand
:
2587 case ABIArgInfo::IndirectAliased
:
2588 llvm_unreachable("Invalid ABI kind for return argument");
2592 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2593 if (const auto *RefTy
= RetTy
->getAs
<ReferenceType
>()) {
2594 QualType PTy
= RefTy
->getPointeeType();
2595 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType())
2596 RetAttrs
.addDereferenceableAttr(
2597 getMinimumObjectSize(PTy
).getQuantity());
2598 if (getTypes().getTargetAddressSpace(PTy
) == 0 &&
2599 !CodeGenOpts
.NullPointerIsValid
)
2600 RetAttrs
.addAttribute(llvm::Attribute::NonNull
);
2601 if (PTy
->isObjectType()) {
2602 llvm::Align Alignment
=
2603 getNaturalPointeeTypeAlignment(RetTy
).getAsAlign();
2604 RetAttrs
.addAlignmentAttr(Alignment
);
2609 bool hasUsedSRet
= false;
2610 SmallVector
<llvm::AttributeSet
, 4> ArgAttrs(IRFunctionArgs
.totalIRArgs());
2612 // Attach attributes to sret.
2613 if (IRFunctionArgs
.hasSRetArg()) {
2614 llvm::AttrBuilder
SRETAttrs(getLLVMContext());
2615 SRETAttrs
.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy
));
2616 SRETAttrs
.addAttribute(llvm::Attribute::Writable
);
2617 SRETAttrs
.addAttribute(llvm::Attribute::DeadOnUnwind
);
2619 if (RetAI
.getInReg())
2620 SRETAttrs
.addAttribute(llvm::Attribute::InReg
);
2621 SRETAttrs
.addAlignmentAttr(RetAI
.getIndirectAlign().getQuantity());
2622 ArgAttrs
[IRFunctionArgs
.getSRetArgNo()] =
2623 llvm::AttributeSet::get(getLLVMContext(), SRETAttrs
);
2626 // Attach attributes to inalloca argument.
2627 if (IRFunctionArgs
.hasInallocaArg()) {
2628 llvm::AttrBuilder
Attrs(getLLVMContext());
2629 Attrs
.addInAllocaAttr(FI
.getArgStruct());
2630 ArgAttrs
[IRFunctionArgs
.getInallocaArgNo()] =
2631 llvm::AttributeSet::get(getLLVMContext(), Attrs
);
2634 // Apply `nonnull`, `dereferencable(N)` and `align N` to the `this` argument,
2635 // unless this is a thunk function.
2636 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2637 if (FI
.isInstanceMethod() && !IRFunctionArgs
.hasInallocaArg() &&
2638 !FI
.arg_begin()->type
->isVoidPointerType() && !IsThunk
) {
2639 auto IRArgs
= IRFunctionArgs
.getIRArgs(0);
2641 assert(IRArgs
.second
== 1 && "Expected only a single `this` pointer.");
2643 llvm::AttrBuilder
Attrs(getLLVMContext());
2646 FI
.arg_begin()->type
.getTypePtr()->getPointeeType();
2648 if (!CodeGenOpts
.NullPointerIsValid
&&
2649 getTypes().getTargetAddressSpace(FI
.arg_begin()->type
) == 0) {
2650 Attrs
.addAttribute(llvm::Attribute::NonNull
);
2651 Attrs
.addDereferenceableAttr(getMinimumObjectSize(ThisTy
).getQuantity());
2653 // FIXME dereferenceable should be correct here, regardless of
2654 // NullPointerIsValid. However, dereferenceable currently does not always
2655 // respect NullPointerIsValid and may imply nonnull and break the program.
2656 // See https://reviews.llvm.org/D66618 for discussions.
2657 Attrs
.addDereferenceableOrNullAttr(
2658 getMinimumObjectSize(
2659 FI
.arg_begin()->type
.castAs
<PointerType
>()->getPointeeType())
2663 llvm::Align Alignment
=
2664 getNaturalTypeAlignment(ThisTy
, /*BaseInfo=*/nullptr,
2665 /*TBAAInfo=*/nullptr, /*forPointeeType=*/true)
2667 Attrs
.addAlignmentAttr(Alignment
);
2669 ArgAttrs
[IRArgs
.first
] = llvm::AttributeSet::get(getLLVMContext(), Attrs
);
2673 for (CGFunctionInfo::const_arg_iterator I
= FI
.arg_begin(),
2675 I
!= E
; ++I
, ++ArgNo
) {
2676 QualType ParamType
= I
->type
;
2677 const ABIArgInfo
&AI
= I
->info
;
2678 llvm::AttrBuilder
Attrs(getLLVMContext());
2680 // Add attribute for padding argument, if necessary.
2681 if (IRFunctionArgs
.hasPaddingArg(ArgNo
)) {
2682 if (AI
.getPaddingInReg()) {
2683 ArgAttrs
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
2684 llvm::AttributeSet::get(
2686 llvm::AttrBuilder(getLLVMContext()).addAttribute(llvm::Attribute::InReg
));
2690 // Decide whether the argument we're handling could be partially undef
2691 if (CodeGenOpts
.EnableNoundefAttrs
&&
2692 DetermineNoUndef(ParamType
, getTypes(), DL
, AI
)) {
2693 Attrs
.addAttribute(llvm::Attribute::NoUndef
);
2696 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
2697 // have the corresponding parameter variable. It doesn't make
2698 // sense to do it here because parameters are so messed up.
2699 switch (AI
.getKind()) {
2700 case ABIArgInfo::Extend
:
2702 Attrs
.addAttribute(llvm::Attribute::SExt
);
2704 Attrs
.addAttribute(llvm::Attribute::ZExt
);
2706 case ABIArgInfo::Direct
:
2707 if (ArgNo
== 0 && FI
.isChainCall())
2708 Attrs
.addAttribute(llvm::Attribute::Nest
);
2709 else if (AI
.getInReg())
2710 Attrs
.addAttribute(llvm::Attribute::InReg
);
2711 Attrs
.addStackAlignmentAttr(llvm::MaybeAlign(AI
.getDirectAlign()));
2713 if (canApplyNoFPClass(AI
, ParamType
, false))
2714 Attrs
.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2716 case ABIArgInfo::Indirect
: {
2718 Attrs
.addAttribute(llvm::Attribute::InReg
);
2720 if (AI
.getIndirectByVal())
2721 Attrs
.addByValAttr(getTypes().ConvertTypeForMem(ParamType
));
2723 auto *Decl
= ParamType
->getAsRecordDecl();
2724 if (CodeGenOpts
.PassByValueIsNoAlias
&& Decl
&&
2725 Decl
->getArgPassingRestrictions() ==
2726 RecordArgPassingKind::CanPassInRegs
)
2727 // When calling the function, the pointer passed in will be the only
2728 // reference to the underlying object. Mark it accordingly.
2729 Attrs
.addAttribute(llvm::Attribute::NoAlias
);
2731 // TODO: We could add the byref attribute if not byval, but it would
2732 // require updating many testcases.
2734 CharUnits Align
= AI
.getIndirectAlign();
2736 // In a byval argument, it is important that the required
2737 // alignment of the type is honored, as LLVM might be creating a
2738 // *new* stack object, and needs to know what alignment to give
2739 // it. (Sometimes it can deduce a sensible alignment on its own,
2740 // but not if clang decides it must emit a packed struct, or the
2741 // user specifies increased alignment requirements.)
2743 // This is different from indirect *not* byval, where the object
2744 // exists already, and the align attribute is purely
2746 assert(!Align
.isZero());
2748 // For now, only add this when we have a byval argument.
2749 // TODO: be less lazy about updating test cases.
2750 if (AI
.getIndirectByVal())
2751 Attrs
.addAlignmentAttr(Align
.getQuantity());
2753 // byval disables readnone and readonly.
2754 AddPotentialArgAccess();
2757 case ABIArgInfo::IndirectAliased
: {
2758 CharUnits Align
= AI
.getIndirectAlign();
2759 Attrs
.addByRefAttr(getTypes().ConvertTypeForMem(ParamType
));
2760 Attrs
.addAlignmentAttr(Align
.getQuantity());
2763 case ABIArgInfo::Ignore
:
2764 case ABIArgInfo::Expand
:
2765 case ABIArgInfo::CoerceAndExpand
:
2768 case ABIArgInfo::InAlloca
:
2769 // inalloca disables readnone and readonly.
2770 AddPotentialArgAccess();
2774 if (const auto *RefTy
= ParamType
->getAs
<ReferenceType
>()) {
2775 QualType PTy
= RefTy
->getPointeeType();
2776 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType())
2777 Attrs
.addDereferenceableAttr(
2778 getMinimumObjectSize(PTy
).getQuantity());
2779 if (getTypes().getTargetAddressSpace(PTy
) == 0 &&
2780 !CodeGenOpts
.NullPointerIsValid
)
2781 Attrs
.addAttribute(llvm::Attribute::NonNull
);
2782 if (PTy
->isObjectType()) {
2783 llvm::Align Alignment
=
2784 getNaturalPointeeTypeAlignment(ParamType
).getAsAlign();
2785 Attrs
.addAlignmentAttr(Alignment
);
2789 // From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:
2790 // > For arguments to a __kernel function declared to be a pointer to a
2791 // > data type, the OpenCL compiler can assume that the pointee is always
2792 // > appropriately aligned as required by the data type.
2793 if (TargetDecl
&& TargetDecl
->hasAttr
<OpenCLKernelAttr
>() &&
2794 ParamType
->isPointerType()) {
2795 QualType PTy
= ParamType
->getPointeeType();
2796 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType()) {
2797 llvm::Align Alignment
=
2798 getNaturalPointeeTypeAlignment(ParamType
).getAsAlign();
2799 Attrs
.addAlignmentAttr(Alignment
);
2803 switch (FI
.getExtParameterInfo(ArgNo
).getABI()) {
2804 case ParameterABI::Ordinary
:
2807 case ParameterABI::SwiftIndirectResult
: {
2808 // Add 'sret' if we haven't already used it for something, but
2809 // only if the result is void.
2810 if (!hasUsedSRet
&& RetTy
->isVoidType()) {
2811 Attrs
.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType
));
2815 // Add 'noalias' in either case.
2816 Attrs
.addAttribute(llvm::Attribute::NoAlias
);
2818 // Add 'dereferenceable' and 'alignment'.
2819 auto PTy
= ParamType
->getPointeeType();
2820 if (!PTy
->isIncompleteType() && PTy
->isConstantSizeType()) {
2821 auto info
= getContext().getTypeInfoInChars(PTy
);
2822 Attrs
.addDereferenceableAttr(info
.Width
.getQuantity());
2823 Attrs
.addAlignmentAttr(info
.Align
.getAsAlign());
2828 case ParameterABI::SwiftErrorResult
:
2829 Attrs
.addAttribute(llvm::Attribute::SwiftError
);
2832 case ParameterABI::SwiftContext
:
2833 Attrs
.addAttribute(llvm::Attribute::SwiftSelf
);
2836 case ParameterABI::SwiftAsyncContext
:
2837 Attrs
.addAttribute(llvm::Attribute::SwiftAsync
);
2841 if (FI
.getExtParameterInfo(ArgNo
).isNoEscape())
2842 Attrs
.addAttribute(llvm::Attribute::NoCapture
);
2844 if (Attrs
.hasAttributes()) {
2845 unsigned FirstIRArg
, NumIRArgs
;
2846 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
2847 for (unsigned i
= 0; i
< NumIRArgs
; i
++)
2848 ArgAttrs
[FirstIRArg
+ i
] = ArgAttrs
[FirstIRArg
+ i
].addAttributes(
2849 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs
));
2852 assert(ArgNo
== FI
.arg_size());
2854 AttrList
= llvm::AttributeList::get(
2855 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs
),
2856 llvm::AttributeSet::get(getLLVMContext(), RetAttrs
), ArgAttrs
);
2859 /// An argument came in as a promoted argument; demote it back to its
2861 static llvm::Value
*emitArgumentDemotion(CodeGenFunction
&CGF
,
2863 llvm::Value
*value
) {
2864 llvm::Type
*varType
= CGF
.ConvertType(var
->getType());
2866 // This can happen with promotions that actually don't change the
2867 // underlying type, like the enum promotions.
2868 if (value
->getType() == varType
) return value
;
2870 assert((varType
->isIntegerTy() || varType
->isFloatingPointTy())
2871 && "unexpected promotion type");
2873 if (isa
<llvm::IntegerType
>(varType
))
2874 return CGF
.Builder
.CreateTrunc(value
, varType
, "arg.unpromote");
2876 return CGF
.Builder
.CreateFPCast(value
, varType
, "arg.unpromote");
2879 /// Returns the attribute (either parameter attribute, or function
2880 /// attribute), which declares argument ArgNo to be non-null.
2881 static const NonNullAttr
*getNonNullAttr(const Decl
*FD
, const ParmVarDecl
*PVD
,
2882 QualType ArgType
, unsigned ArgNo
) {
2883 // FIXME: __attribute__((nonnull)) can also be applied to:
2884 // - references to pointers, where the pointee is known to be
2885 // nonnull (apparently a Clang extension)
2886 // - transparent unions containing pointers
2887 // In the former case, LLVM IR cannot represent the constraint. In
2888 // the latter case, we have no guarantee that the transparent union
2889 // is in fact passed as a pointer.
2890 if (!ArgType
->isAnyPointerType() && !ArgType
->isBlockPointerType())
2892 // First, check attribute on parameter itself.
2894 if (auto ParmNNAttr
= PVD
->getAttr
<NonNullAttr
>())
2897 // Check function attributes.
2900 for (const auto *NNAttr
: FD
->specific_attrs
<NonNullAttr
>()) {
2901 if (NNAttr
->isNonNull(ArgNo
))
2908 struct CopyBackSwiftError final
: EHScopeStack::Cleanup
{
2911 CopyBackSwiftError(Address temp
, Address arg
) : Temp(temp
), Arg(arg
) {}
2912 void Emit(CodeGenFunction
&CGF
, Flags flags
) override
{
2913 llvm::Value
*errorValue
= CGF
.Builder
.CreateLoad(Temp
);
2914 CGF
.Builder
.CreateStore(errorValue
, Arg
);
2919 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo
&FI
,
2921 const FunctionArgList
&Args
) {
2922 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<NakedAttr
>())
2923 // Naked functions don't have prologues.
2926 // If this is an implicit-return-zero function, go ahead and
2927 // initialize the return value. TODO: it might be nice to have
2928 // a more general mechanism for this that didn't require synthesized
2929 // return statements.
2930 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurCodeDecl
)) {
2931 if (FD
->hasImplicitReturnZero()) {
2932 QualType RetTy
= FD
->getReturnType().getUnqualifiedType();
2933 llvm::Type
* LLVMTy
= CGM
.getTypes().ConvertType(RetTy
);
2934 llvm::Constant
* Zero
= llvm::Constant::getNullValue(LLVMTy
);
2935 Builder
.CreateStore(Zero
, ReturnValue
);
2939 // FIXME: We no longer need the types from FunctionArgList; lift up and
2942 ClangToLLVMArgMapping
IRFunctionArgs(CGM
.getContext(), FI
);
2943 assert(Fn
->arg_size() == IRFunctionArgs
.totalIRArgs());
2945 // If we're using inalloca, all the memory arguments are GEPs off of the last
2946 // parameter, which is a pointer to the complete memory area.
2947 Address ArgStruct
= Address::invalid();
2948 if (IRFunctionArgs
.hasInallocaArg())
2949 ArgStruct
= Address(Fn
->getArg(IRFunctionArgs
.getInallocaArgNo()),
2950 FI
.getArgStruct(), FI
.getArgStructAlignment());
2952 // Name the struct return parameter.
2953 if (IRFunctionArgs
.hasSRetArg()) {
2954 auto AI
= Fn
->getArg(IRFunctionArgs
.getSRetArgNo());
2955 AI
->setName("agg.result");
2956 AI
->addAttr(llvm::Attribute::NoAlias
);
2959 // Track if we received the parameter as a pointer (indirect, byval, or
2960 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
2961 // into a local alloca for us.
2962 SmallVector
<ParamValue
, 16> ArgVals
;
2963 ArgVals
.reserve(Args
.size());
2965 // Create a pointer value for every parameter declaration. This usually
2966 // entails copying one or more LLVM IR arguments into an alloca. Don't push
2967 // any cleanups or do anything that might unwind. We do that separately, so
2968 // we can push the cleanups in the correct order for the ABI.
2969 assert(FI
.arg_size() == Args
.size() &&
2970 "Mismatch between function signature & arguments.");
2972 CGFunctionInfo::const_arg_iterator info_it
= FI
.arg_begin();
2973 for (FunctionArgList::const_iterator i
= Args
.begin(), e
= Args
.end();
2974 i
!= e
; ++i
, ++info_it
, ++ArgNo
) {
2975 const VarDecl
*Arg
= *i
;
2976 const ABIArgInfo
&ArgI
= info_it
->info
;
2979 isa
<ParmVarDecl
>(Arg
) && cast
<ParmVarDecl
>(Arg
)->isKNRPromoted();
2980 // We are converting from ABIArgInfo type to VarDecl type directly, unless
2981 // the parameter is promoted. In this case we convert to
2982 // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
2983 QualType Ty
= isPromoted
? info_it
->type
: Arg
->getType();
2984 assert(hasScalarEvaluationKind(Ty
) ==
2985 hasScalarEvaluationKind(Arg
->getType()));
2987 unsigned FirstIRArg
, NumIRArgs
;
2988 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
2990 switch (ArgI
.getKind()) {
2991 case ABIArgInfo::InAlloca
: {
2992 assert(NumIRArgs
== 0);
2993 auto FieldIndex
= ArgI
.getInAllocaFieldIndex();
2995 Builder
.CreateStructGEP(ArgStruct
, FieldIndex
, Arg
->getName());
2996 if (ArgI
.getInAllocaIndirect())
2997 V
= Address(Builder
.CreateLoad(V
), ConvertTypeForMem(Ty
),
2998 getContext().getTypeAlignInChars(Ty
));
2999 ArgVals
.push_back(ParamValue::forIndirect(V
));
3003 case ABIArgInfo::Indirect
:
3004 case ABIArgInfo::IndirectAliased
: {
3005 assert(NumIRArgs
== 1);
3006 Address ParamAddr
= makeNaturalAddressForPointer(
3007 Fn
->getArg(FirstIRArg
), Ty
, ArgI
.getIndirectAlign(), false, nullptr,
3008 nullptr, KnownNonNull
);
3010 if (!hasScalarEvaluationKind(Ty
)) {
3011 // Aggregates and complex variables are accessed by reference. All we
3012 // need to do is realign the value, if requested. Also, if the address
3013 // may be aliased, copy it to ensure that the parameter variable is
3014 // mutable and has a unique adress, as C requires.
3015 if (ArgI
.getIndirectRealign() || ArgI
.isIndirectAliased()) {
3016 RawAddress AlignedTemp
= CreateMemTemp(Ty
, "coerce");
3018 // Copy from the incoming argument pointer to the temporary with the
3019 // appropriate alignment.
3021 // FIXME: We should have a common utility for generating an aggregate
3023 CharUnits Size
= getContext().getTypeSizeInChars(Ty
);
3024 Builder
.CreateMemCpy(
3025 AlignedTemp
.getPointer(), AlignedTemp
.getAlignment().getAsAlign(),
3026 ParamAddr
.emitRawPointer(*this),
3027 ParamAddr
.getAlignment().getAsAlign(),
3028 llvm::ConstantInt::get(IntPtrTy
, Size
.getQuantity()));
3029 ParamAddr
= AlignedTemp
;
3031 ArgVals
.push_back(ParamValue::forIndirect(ParamAddr
));
3033 // Load scalar value from indirect argument.
3035 EmitLoadOfScalar(ParamAddr
, false, Ty
, Arg
->getBeginLoc());
3038 V
= emitArgumentDemotion(*this, Arg
, V
);
3039 ArgVals
.push_back(ParamValue::forDirect(V
));
3044 case ABIArgInfo::Extend
:
3045 case ABIArgInfo::Direct
: {
3046 auto AI
= Fn
->getArg(FirstIRArg
);
3047 llvm::Type
*LTy
= ConvertType(Arg
->getType());
3049 // Prepare parameter attributes. So far, only attributes for pointer
3050 // parameters are prepared. See
3051 // http://llvm.org/docs/LangRef.html#paramattrs.
3052 if (ArgI
.getDirectOffset() == 0 && LTy
->isPointerTy() &&
3053 ArgI
.getCoerceToType()->isPointerTy()) {
3054 assert(NumIRArgs
== 1);
3056 if (const ParmVarDecl
*PVD
= dyn_cast
<ParmVarDecl
>(Arg
)) {
3057 // Set `nonnull` attribute if any.
3058 if (getNonNullAttr(CurCodeDecl
, PVD
, PVD
->getType(),
3059 PVD
->getFunctionScopeIndex()) &&
3060 !CGM
.getCodeGenOpts().NullPointerIsValid
)
3061 AI
->addAttr(llvm::Attribute::NonNull
);
3063 QualType OTy
= PVD
->getOriginalType();
3064 if (const auto *ArrTy
=
3065 getContext().getAsConstantArrayType(OTy
)) {
3066 // A C99 array parameter declaration with the static keyword also
3067 // indicates dereferenceability, and if the size is constant we can
3068 // use the dereferenceable attribute (which requires the size in
3070 if (ArrTy
->getSizeModifier() == ArraySizeModifier::Static
) {
3071 QualType ETy
= ArrTy
->getElementType();
3072 llvm::Align Alignment
=
3073 CGM
.getNaturalTypeAlignment(ETy
).getAsAlign();
3074 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment
));
3075 uint64_t ArrSize
= ArrTy
->getZExtSize();
3076 if (!ETy
->isIncompleteType() && ETy
->isConstantSizeType() &&
3078 llvm::AttrBuilder
Attrs(getLLVMContext());
3079 Attrs
.addDereferenceableAttr(
3080 getContext().getTypeSizeInChars(ETy
).getQuantity() *
3082 AI
->addAttrs(Attrs
);
3083 } else if (getContext().getTargetInfo().getNullPointerValue(
3084 ETy
.getAddressSpace()) == 0 &&
3085 !CGM
.getCodeGenOpts().NullPointerIsValid
) {
3086 AI
->addAttr(llvm::Attribute::NonNull
);
3089 } else if (const auto *ArrTy
=
3090 getContext().getAsVariableArrayType(OTy
)) {
3091 // For C99 VLAs with the static keyword, we don't know the size so
3092 // we can't use the dereferenceable attribute, but in addrspace(0)
3093 // we know that it must be nonnull.
3094 if (ArrTy
->getSizeModifier() == ArraySizeModifier::Static
) {
3095 QualType ETy
= ArrTy
->getElementType();
3096 llvm::Align Alignment
=
3097 CGM
.getNaturalTypeAlignment(ETy
).getAsAlign();
3098 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment
));
3099 if (!getTypes().getTargetAddressSpace(ETy
) &&
3100 !CGM
.getCodeGenOpts().NullPointerIsValid
)
3101 AI
->addAttr(llvm::Attribute::NonNull
);
3105 // Set `align` attribute if any.
3106 const auto *AVAttr
= PVD
->getAttr
<AlignValueAttr
>();
3108 if (const auto *TOTy
= OTy
->getAs
<TypedefType
>())
3109 AVAttr
= TOTy
->getDecl()->getAttr
<AlignValueAttr
>();
3110 if (AVAttr
&& !SanOpts
.has(SanitizerKind::Alignment
)) {
3111 // If alignment-assumption sanitizer is enabled, we do *not* add
3112 // alignment attribute here, but emit normal alignment assumption,
3113 // so the UBSAN check could function.
3114 llvm::ConstantInt
*AlignmentCI
=
3115 cast
<llvm::ConstantInt
>(EmitScalarExpr(AVAttr
->getAlignment()));
3116 uint64_t AlignmentInt
=
3117 AlignmentCI
->getLimitedValue(llvm::Value::MaximumAlignment
);
3118 if (AI
->getParamAlign().valueOrOne() < AlignmentInt
) {
3119 AI
->removeAttr(llvm::Attribute::AttrKind::Alignment
);
3120 AI
->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(
3121 llvm::Align(AlignmentInt
)));
3126 // Set 'noalias' if an argument type has the `restrict` qualifier.
3127 if (Arg
->getType().isRestrictQualified())
3128 AI
->addAttr(llvm::Attribute::NoAlias
);
3131 // Prepare the argument value. If we have the trivial case, handle it
3132 // with no muss and fuss.
3133 if (!isa
<llvm::StructType
>(ArgI
.getCoerceToType()) &&
3134 ArgI
.getCoerceToType() == ConvertType(Ty
) &&
3135 ArgI
.getDirectOffset() == 0) {
3136 assert(NumIRArgs
== 1);
3138 // LLVM expects swifterror parameters to be used in very restricted
3139 // ways. Copy the value into a less-restricted temporary.
3140 llvm::Value
*V
= AI
;
3141 if (FI
.getExtParameterInfo(ArgNo
).getABI()
3142 == ParameterABI::SwiftErrorResult
) {
3143 QualType pointeeTy
= Ty
->getPointeeType();
3144 assert(pointeeTy
->isPointerType());
3146 CreateMemTemp(pointeeTy
, getPointerAlign(), "swifterror.temp");
3147 Address arg
= makeNaturalAddressForPointer(
3148 V
, pointeeTy
, getContext().getTypeAlignInChars(pointeeTy
));
3149 llvm::Value
*incomingErrorValue
= Builder
.CreateLoad(arg
);
3150 Builder
.CreateStore(incomingErrorValue
, temp
);
3151 V
= temp
.getPointer();
3153 // Push a cleanup to copy the value back at the end of the function.
3154 // The convention does not guarantee that the value will be written
3155 // back if the function exits with an unwind exception.
3156 EHStack
.pushCleanup
<CopyBackSwiftError
>(NormalCleanup
, temp
, arg
);
3159 // Ensure the argument is the correct type.
3160 if (V
->getType() != ArgI
.getCoerceToType())
3161 V
= Builder
.CreateBitCast(V
, ArgI
.getCoerceToType());
3164 V
= emitArgumentDemotion(*this, Arg
, V
);
3166 // Because of merging of function types from multiple decls it is
3167 // possible for the type of an argument to not match the corresponding
3168 // type in the function type. Since we are codegening the callee
3169 // in here, add a cast to the argument type.
3170 llvm::Type
*LTy
= ConvertType(Arg
->getType());
3171 if (V
->getType() != LTy
)
3172 V
= Builder
.CreateBitCast(V
, LTy
);
3174 ArgVals
.push_back(ParamValue::forDirect(V
));
3178 // VLST arguments are coerced to VLATs at the function boundary for
3179 // ABI consistency. If this is a VLST that was coerced to
3180 // a VLAT at the function boundary and the types match up, use
3181 // llvm.vector.extract to convert back to the original VLST.
3182 if (auto *VecTyTo
= dyn_cast
<llvm::FixedVectorType
>(ConvertType(Ty
))) {
3183 llvm::Value
*Coerced
= Fn
->getArg(FirstIRArg
);
3184 if (auto *VecTyFrom
=
3185 dyn_cast
<llvm::ScalableVectorType
>(Coerced
->getType())) {
3186 // If we are casting a scalable i1 predicate vector to a fixed i8
3187 // vector, bitcast the source and use a vector extract.
3188 if (VecTyFrom
->getElementType()->isIntegerTy(1) &&
3189 VecTyFrom
->getElementCount().isKnownMultipleOf(8) &&
3190 VecTyTo
->getElementType() == Builder
.getInt8Ty()) {
3191 VecTyFrom
= llvm::ScalableVectorType::get(
3192 VecTyTo
->getElementType(),
3193 VecTyFrom
->getElementCount().getKnownMinValue() / 8);
3194 Coerced
= Builder
.CreateBitCast(Coerced
, VecTyFrom
);
3196 if (VecTyFrom
->getElementType() == VecTyTo
->getElementType()) {
3197 llvm::Value
*Zero
= llvm::Constant::getNullValue(CGM
.Int64Ty
);
3199 assert(NumIRArgs
== 1);
3200 Coerced
->setName(Arg
->getName() + ".coerce");
3201 ArgVals
.push_back(ParamValue::forDirect(Builder
.CreateExtractVector(
3202 VecTyTo
, Coerced
, Zero
, "cast.fixed")));
3208 llvm::StructType
*STy
=
3209 dyn_cast
<llvm::StructType
>(ArgI
.getCoerceToType());
3210 if (ArgI
.isDirect() && !ArgI
.getCanBeFlattened() && STy
&&
3211 STy
->getNumElements() > 1) {
3212 [[maybe_unused
]] llvm::TypeSize StructSize
=
3213 CGM
.getDataLayout().getTypeAllocSize(STy
);
3214 [[maybe_unused
]] llvm::TypeSize PtrElementSize
=
3215 CGM
.getDataLayout().getTypeAllocSize(ConvertTypeForMem(Ty
));
3216 if (STy
->containsHomogeneousScalableVectorTypes()) {
3217 assert(StructSize
== PtrElementSize
&&
3218 "Only allow non-fractional movement of structure with"
3219 "homogeneous scalable vector type");
3221 ArgVals
.push_back(ParamValue::forDirect(AI
));
3226 Address Alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
),
3229 // Pointer to store into.
3230 Address Ptr
= emitAddressAtOffset(*this, Alloca
, ArgI
);
3232 // Fast-isel and the optimizer generally like scalar values better than
3233 // FCAs, so we flatten them if this is safe to do for this argument.
3234 if (ArgI
.isDirect() && ArgI
.getCanBeFlattened() && STy
&&
3235 STy
->getNumElements() > 1) {
3236 llvm::TypeSize StructSize
= CGM
.getDataLayout().getTypeAllocSize(STy
);
3237 llvm::TypeSize PtrElementSize
=
3238 CGM
.getDataLayout().getTypeAllocSize(Ptr
.getElementType());
3239 if (StructSize
.isScalable()) {
3240 assert(STy
->containsHomogeneousScalableVectorTypes() &&
3241 "ABI only supports structure with homogeneous scalable vector "
3243 assert(StructSize
== PtrElementSize
&&
3244 "Only allow non-fractional movement of structure with"
3245 "homogeneous scalable vector type");
3246 assert(STy
->getNumElements() == NumIRArgs
);
3248 llvm::Value
*LoadedStructValue
= llvm::PoisonValue::get(STy
);
3249 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
3250 auto *AI
= Fn
->getArg(FirstIRArg
+ i
);
3251 AI
->setName(Arg
->getName() + ".coerce" + Twine(i
));
3253 Builder
.CreateInsertValue(LoadedStructValue
, AI
, i
);
3256 Builder
.CreateStore(LoadedStructValue
, Ptr
);
3258 uint64_t SrcSize
= StructSize
.getFixedValue();
3259 uint64_t DstSize
= PtrElementSize
.getFixedValue();
3261 Address AddrToStoreInto
= Address::invalid();
3262 if (SrcSize
<= DstSize
) {
3263 AddrToStoreInto
= Ptr
.withElementType(STy
);
3266 CreateTempAlloca(STy
, Alloca
.getAlignment(), "coerce");
3269 assert(STy
->getNumElements() == NumIRArgs
);
3270 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
3271 auto AI
= Fn
->getArg(FirstIRArg
+ i
);
3272 AI
->setName(Arg
->getName() + ".coerce" + Twine(i
));
3273 Address EltPtr
= Builder
.CreateStructGEP(AddrToStoreInto
, i
);
3274 Builder
.CreateStore(AI
, EltPtr
);
3277 if (SrcSize
> DstSize
) {
3278 Builder
.CreateMemCpy(Ptr
, AddrToStoreInto
, DstSize
);
3282 // Simple case, just do a coerced store of the argument into the alloca.
3283 assert(NumIRArgs
== 1);
3284 auto AI
= Fn
->getArg(FirstIRArg
);
3285 AI
->setName(Arg
->getName() + ".coerce");
3288 llvm::TypeSize::getFixed(
3289 getContext().getTypeSizeInChars(Ty
).getQuantity() -
3290 ArgI
.getDirectOffset()),
3291 /*DstIsVolatile=*/false);
3294 // Match to what EmitParmDecl is expecting for this type.
3295 if (CodeGenFunction::hasScalarEvaluationKind(Ty
)) {
3297 EmitLoadOfScalar(Alloca
, false, Ty
, Arg
->getBeginLoc());
3299 V
= emitArgumentDemotion(*this, Arg
, V
);
3300 ArgVals
.push_back(ParamValue::forDirect(V
));
3302 ArgVals
.push_back(ParamValue::forIndirect(Alloca
));
3307 case ABIArgInfo::CoerceAndExpand
: {
3308 // Reconstruct into a temporary.
3309 Address alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
));
3310 ArgVals
.push_back(ParamValue::forIndirect(alloca
));
3312 auto coercionType
= ArgI
.getCoerceAndExpandType();
3313 alloca
= alloca
.withElementType(coercionType
);
3315 unsigned argIndex
= FirstIRArg
;
3316 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
3317 llvm::Type
*eltType
= coercionType
->getElementType(i
);
3318 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
))
3321 auto eltAddr
= Builder
.CreateStructGEP(alloca
, i
);
3322 auto elt
= Fn
->getArg(argIndex
++);
3323 Builder
.CreateStore(elt
, eltAddr
);
3325 assert(argIndex
== FirstIRArg
+ NumIRArgs
);
3329 case ABIArgInfo::Expand
: {
3330 // If this structure was expanded into multiple arguments then
3331 // we need to create a temporary and reconstruct it from the
3333 Address Alloca
= CreateMemTemp(Ty
, getContext().getDeclAlign(Arg
));
3334 LValue LV
= MakeAddrLValue(Alloca
, Ty
);
3335 ArgVals
.push_back(ParamValue::forIndirect(Alloca
));
3337 auto FnArgIter
= Fn
->arg_begin() + FirstIRArg
;
3338 ExpandTypeFromArgs(Ty
, LV
, FnArgIter
);
3339 assert(FnArgIter
== Fn
->arg_begin() + FirstIRArg
+ NumIRArgs
);
3340 for (unsigned i
= 0, e
= NumIRArgs
; i
!= e
; ++i
) {
3341 auto AI
= Fn
->getArg(FirstIRArg
+ i
);
3342 AI
->setName(Arg
->getName() + "." + Twine(i
));
3347 case ABIArgInfo::Ignore
:
3348 assert(NumIRArgs
== 0);
3349 // Initialize the local variable appropriately.
3350 if (!hasScalarEvaluationKind(Ty
)) {
3351 ArgVals
.push_back(ParamValue::forIndirect(CreateMemTemp(Ty
)));
3353 llvm::Value
*U
= llvm::UndefValue::get(ConvertType(Arg
->getType()));
3354 ArgVals
.push_back(ParamValue::forDirect(U
));
3360 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3361 for (int I
= Args
.size() - 1; I
>= 0; --I
)
3362 EmitParmDecl(*Args
[I
], ArgVals
[I
], I
+ 1);
3364 for (unsigned I
= 0, E
= Args
.size(); I
!= E
; ++I
)
3365 EmitParmDecl(*Args
[I
], ArgVals
[I
], I
+ 1);
3369 static void eraseUnusedBitCasts(llvm::Instruction
*insn
) {
3370 while (insn
->use_empty()) {
3371 llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(insn
);
3372 if (!bitcast
) return;
3374 // This is "safe" because we would have used a ConstantExpr otherwise.
3375 insn
= cast
<llvm::Instruction
>(bitcast
->getOperand(0));
3376 bitcast
->eraseFromParent();
3380 /// Try to emit a fused autorelease of a return result.
3381 static llvm::Value
*tryEmitFusedAutoreleaseOfResult(CodeGenFunction
&CGF
,
3382 llvm::Value
*result
) {
3383 // We must be immediately followed the cast.
3384 llvm::BasicBlock
*BB
= CGF
.Builder
.GetInsertBlock();
3385 if (BB
->empty()) return nullptr;
3386 if (&BB
->back() != result
) return nullptr;
3388 llvm::Type
*resultType
= result
->getType();
3390 // result is in a BasicBlock and is therefore an Instruction.
3391 llvm::Instruction
*generator
= cast
<llvm::Instruction
>(result
);
3393 SmallVector
<llvm::Instruction
*, 4> InstsToKill
;
3396 // %generator = bitcast %type1* %generator2 to %type2*
3397 while (llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(generator
)) {
3398 // We would have emitted this as a constant if the operand weren't
3400 generator
= cast
<llvm::Instruction
>(bitcast
->getOperand(0));
3402 // Require the generator to be immediately followed by the cast.
3403 if (generator
->getNextNode() != bitcast
)
3406 InstsToKill
.push_back(bitcast
);
3410 // %generator = call i8* @objc_retain(i8* %originalResult)
3412 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
3413 llvm::CallInst
*call
= dyn_cast
<llvm::CallInst
>(generator
);
3414 if (!call
) return nullptr;
3416 bool doRetainAutorelease
;
3418 if (call
->getCalledOperand() == CGF
.CGM
.getObjCEntrypoints().objc_retain
) {
3419 doRetainAutorelease
= true;
3420 } else if (call
->getCalledOperand() ==
3421 CGF
.CGM
.getObjCEntrypoints().objc_retainAutoreleasedReturnValue
) {
3422 doRetainAutorelease
= false;
3424 // If we emitted an assembly marker for this call (and the
3425 // ARCEntrypoints field should have been set if so), go looking
3426 // for that call. If we can't find it, we can't do this
3427 // optimization. But it should always be the immediately previous
3428 // instruction, unless we needed bitcasts around the call.
3429 if (CGF
.CGM
.getObjCEntrypoints().retainAutoreleasedReturnValueMarker
) {
3430 llvm::Instruction
*prev
= call
->getPrevNode();
3432 if (isa
<llvm::BitCastInst
>(prev
)) {
3433 prev
= prev
->getPrevNode();
3436 assert(isa
<llvm::CallInst
>(prev
));
3437 assert(cast
<llvm::CallInst
>(prev
)->getCalledOperand() ==
3438 CGF
.CGM
.getObjCEntrypoints().retainAutoreleasedReturnValueMarker
);
3439 InstsToKill
.push_back(prev
);
3445 result
= call
->getArgOperand(0);
3446 InstsToKill
.push_back(call
);
3448 // Keep killing bitcasts, for sanity. Note that we no longer care
3449 // about precise ordering as long as there's exactly one use.
3450 while (llvm::BitCastInst
*bitcast
= dyn_cast
<llvm::BitCastInst
>(result
)) {
3451 if (!bitcast
->hasOneUse()) break;
3452 InstsToKill
.push_back(bitcast
);
3453 result
= bitcast
->getOperand(0);
3456 // Delete all the unnecessary instructions, from latest to earliest.
3457 for (auto *I
: InstsToKill
)
3458 I
->eraseFromParent();
3460 // Do the fused retain/autorelease if we were asked to.
3461 if (doRetainAutorelease
)
3462 result
= CGF
.EmitARCRetainAutoreleaseReturnValue(result
);
3464 // Cast back to the result type.
3465 return CGF
.Builder
.CreateBitCast(result
, resultType
);
3468 /// If this is a +1 of the value of an immutable 'self', remove it.
3469 static llvm::Value
*tryRemoveRetainOfSelf(CodeGenFunction
&CGF
,
3470 llvm::Value
*result
) {
3471 // This is only applicable to a method with an immutable 'self'.
3472 const ObjCMethodDecl
*method
=
3473 dyn_cast_or_null
<ObjCMethodDecl
>(CGF
.CurCodeDecl
);
3474 if (!method
) return nullptr;
3475 const VarDecl
*self
= method
->getSelfDecl();
3476 if (!self
->getType().isConstQualified()) return nullptr;
3478 // Look for a retain call. Note: stripPointerCasts looks through returned arg
3479 // functions, which would cause us to miss the retain.
3480 llvm::CallInst
*retainCall
= dyn_cast
<llvm::CallInst
>(result
);
3481 if (!retainCall
|| retainCall
->getCalledOperand() !=
3482 CGF
.CGM
.getObjCEntrypoints().objc_retain
)
3485 // Look for an ordinary load of 'self'.
3486 llvm::Value
*retainedValue
= retainCall
->getArgOperand(0);
3487 llvm::LoadInst
*load
=
3488 dyn_cast
<llvm::LoadInst
>(retainedValue
->stripPointerCasts());
3489 if (!load
|| load
->isAtomic() || load
->isVolatile() ||
3490 load
->getPointerOperand() != CGF
.GetAddrOfLocalVar(self
).getBasePointer())
3493 // Okay! Burn it all down. This relies for correctness on the
3494 // assumption that the retain is emitted as part of the return and
3495 // that thereafter everything is used "linearly".
3496 llvm::Type
*resultType
= result
->getType();
3497 eraseUnusedBitCasts(cast
<llvm::Instruction
>(result
));
3498 assert(retainCall
->use_empty());
3499 retainCall
->eraseFromParent();
3500 eraseUnusedBitCasts(cast
<llvm::Instruction
>(retainedValue
));
3502 return CGF
.Builder
.CreateBitCast(load
, resultType
);
3505 /// Emit an ARC autorelease of the result of a function.
3507 /// \return the value to actually return from the function
3508 static llvm::Value
*emitAutoreleaseOfResult(CodeGenFunction
&CGF
,
3509 llvm::Value
*result
) {
3510 // If we're returning 'self', kill the initial retain. This is a
3511 // heuristic attempt to "encourage correctness" in the really unfortunate
3512 // case where we have a return of self during a dealloc and we desperately
3513 // need to avoid the possible autorelease.
3514 if (llvm::Value
*self
= tryRemoveRetainOfSelf(CGF
, result
))
3517 // At -O0, try to emit a fused retain/autorelease.
3518 if (CGF
.shouldUseFusedARCCalls())
3519 if (llvm::Value
*fused
= tryEmitFusedAutoreleaseOfResult(CGF
, result
))
3522 return CGF
.EmitARCAutoreleaseReturnValue(result
);
3525 /// Heuristically search for a dominating store to the return-value slot.
3526 static llvm::StoreInst
*findDominatingStoreToReturnValue(CodeGenFunction
&CGF
) {
3527 llvm::Value
*ReturnValuePtr
= CGF
.ReturnValue
.getBasePointer();
3529 // Check if a User is a store which pointerOperand is the ReturnValue.
3530 // We are looking for stores to the ReturnValue, not for stores of the
3531 // ReturnValue to some other location.
3532 auto GetStoreIfValid
= [&CGF
,
3533 ReturnValuePtr
](llvm::User
*U
) -> llvm::StoreInst
* {
3534 auto *SI
= dyn_cast
<llvm::StoreInst
>(U
);
3535 if (!SI
|| SI
->getPointerOperand() != ReturnValuePtr
||
3536 SI
->getValueOperand()->getType() != CGF
.ReturnValue
.getElementType())
3538 // These aren't actually possible for non-coerced returns, and we
3539 // only care about non-coerced returns on this code path.
3540 // All memory instructions inside __try block are volatile.
3541 assert(!SI
->isAtomic() &&
3542 (!SI
->isVolatile() || CGF
.currentFunctionUsesSEHTry()));
3545 // If there are multiple uses of the return-value slot, just check
3546 // for something immediately preceding the IP. Sometimes this can
3547 // happen with how we generate implicit-returns; it can also happen
3548 // with noreturn cleanups.
3549 if (!ReturnValuePtr
->hasOneUse()) {
3550 llvm::BasicBlock
*IP
= CGF
.Builder
.GetInsertBlock();
3551 if (IP
->empty()) return nullptr;
3553 // Look at directly preceding instruction, skipping bitcasts and lifetime
3555 for (llvm::Instruction
&I
: make_range(IP
->rbegin(), IP
->rend())) {
3556 if (isa
<llvm::BitCastInst
>(&I
))
3558 if (auto *II
= dyn_cast
<llvm::IntrinsicInst
>(&I
))
3559 if (II
->getIntrinsicID() == llvm::Intrinsic::lifetime_end
)
3562 return GetStoreIfValid(&I
);
3567 llvm::StoreInst
*store
= GetStoreIfValid(ReturnValuePtr
->user_back());
3568 if (!store
) return nullptr;
3570 // Now do a first-and-dirty dominance check: just walk up the
3571 // single-predecessors chain from the current insertion point.
3572 llvm::BasicBlock
*StoreBB
= store
->getParent();
3573 llvm::BasicBlock
*IP
= CGF
.Builder
.GetInsertBlock();
3574 llvm::SmallPtrSet
<llvm::BasicBlock
*, 4> SeenBBs
;
3575 while (IP
!= StoreBB
) {
3576 if (!SeenBBs
.insert(IP
).second
|| !(IP
= IP
->getSinglePredecessor()))
3580 // Okay, the store's basic block dominates the insertion point; we
3581 // can do our thing.
3585 // Helper functions for EmitCMSEClearRecord
3587 // Set the bits corresponding to a field having width `BitWidth` and located at
3588 // offset `BitOffset` (from the least significant bit) within a storage unit of
3589 // `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.
3590 // Use little-endian layout, i.e.`Bits[0]` is the LSB.
3591 static void setBitRange(SmallVectorImpl
<uint64_t> &Bits
, int BitOffset
,
3592 int BitWidth
, int CharWidth
) {
3593 assert(CharWidth
<= 64);
3594 assert(static_cast<unsigned>(BitWidth
) <= Bits
.size() * CharWidth
);
3597 if (BitOffset
>= CharWidth
) {
3598 Pos
+= BitOffset
/ CharWidth
;
3599 BitOffset
= BitOffset
% CharWidth
;
3602 const uint64_t Used
= (uint64_t(1) << CharWidth
) - 1;
3603 if (BitOffset
+ BitWidth
>= CharWidth
) {
3604 Bits
[Pos
++] |= (Used
<< BitOffset
) & Used
;
3605 BitWidth
-= CharWidth
- BitOffset
;
3609 while (BitWidth
>= CharWidth
) {
3611 BitWidth
-= CharWidth
;
3615 Bits
[Pos
++] |= (Used
>> (CharWidth
- BitWidth
)) << BitOffset
;
3618 // Set the bits corresponding to a field having width `BitWidth` and located at
3619 // offset `BitOffset` (from the least significant bit) within a storage unit of
3620 // `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of
3621 // `Bits` corresponds to one target byte. Use target endian layout.
3622 static void setBitRange(SmallVectorImpl
<uint64_t> &Bits
, int StorageOffset
,
3623 int StorageSize
, int BitOffset
, int BitWidth
,
3624 int CharWidth
, bool BigEndian
) {
3626 SmallVector
<uint64_t, 8> TmpBits(StorageSize
);
3627 setBitRange(TmpBits
, BitOffset
, BitWidth
, CharWidth
);
3630 std::reverse(TmpBits
.begin(), TmpBits
.end());
3632 for (uint64_t V
: TmpBits
)
3633 Bits
[StorageOffset
++] |= V
;
3636 static void setUsedBits(CodeGenModule
&, QualType
, int,
3637 SmallVectorImpl
<uint64_t> &);
3639 // Set the bits in `Bits`, which correspond to the value representations of
3640 // the actual members of the record type `RTy`. Note that this function does
3641 // not handle base classes, virtual tables, etc, since they cannot happen in
3642 // CMSE function arguments or return. The bit mask corresponds to the target
3643 // memory layout, i.e. it's endian dependent.
3644 static void setUsedBits(CodeGenModule
&CGM
, const RecordType
*RTy
, int Offset
,
3645 SmallVectorImpl
<uint64_t> &Bits
) {
3646 ASTContext
&Context
= CGM
.getContext();
3647 int CharWidth
= Context
.getCharWidth();
3648 const RecordDecl
*RD
= RTy
->getDecl()->getDefinition();
3649 const ASTRecordLayout
&ASTLayout
= Context
.getASTRecordLayout(RD
);
3650 const CGRecordLayout
&Layout
= CGM
.getTypes().getCGRecordLayout(RD
);
3653 for (auto I
= RD
->field_begin(), E
= RD
->field_end(); I
!= E
; ++I
, ++Idx
) {
3654 const FieldDecl
*F
= *I
;
3656 if (F
->isUnnamedBitField() || F
->isZeroLengthBitField(Context
) ||
3657 F
->getType()->isIncompleteArrayType())
3660 if (F
->isBitField()) {
3661 const CGBitFieldInfo
&BFI
= Layout
.getBitFieldInfo(F
);
3662 setBitRange(Bits
, Offset
+ BFI
.StorageOffset
.getQuantity(),
3663 BFI
.StorageSize
/ CharWidth
, BFI
.Offset
,
3664 BFI
.Size
, CharWidth
,
3665 CGM
.getDataLayout().isBigEndian());
3669 setUsedBits(CGM
, F
->getType(),
3670 Offset
+ ASTLayout
.getFieldOffset(Idx
) / CharWidth
, Bits
);
3674 // Set the bits in `Bits`, which correspond to the value representations of
3675 // the elements of an array type `ATy`.
3676 static void setUsedBits(CodeGenModule
&CGM
, const ConstantArrayType
*ATy
,
3677 int Offset
, SmallVectorImpl
<uint64_t> &Bits
) {
3678 const ASTContext
&Context
= CGM
.getContext();
3680 QualType ETy
= Context
.getBaseElementType(ATy
);
3681 int Size
= Context
.getTypeSizeInChars(ETy
).getQuantity();
3682 SmallVector
<uint64_t, 4> TmpBits(Size
);
3683 setUsedBits(CGM
, ETy
, 0, TmpBits
);
3685 for (int I
= 0, N
= Context
.getConstantArrayElementCount(ATy
); I
< N
; ++I
) {
3686 auto Src
= TmpBits
.begin();
3687 auto Dst
= Bits
.begin() + Offset
+ I
* Size
;
3688 for (int J
= 0; J
< Size
; ++J
)
3693 // Set the bits in `Bits`, which correspond to the value representations of
3695 static void setUsedBits(CodeGenModule
&CGM
, QualType QTy
, int Offset
,
3696 SmallVectorImpl
<uint64_t> &Bits
) {
3697 if (const auto *RTy
= QTy
->getAs
<RecordType
>())
3698 return setUsedBits(CGM
, RTy
, Offset
, Bits
);
3700 ASTContext
&Context
= CGM
.getContext();
3701 if (const auto *ATy
= Context
.getAsConstantArrayType(QTy
))
3702 return setUsedBits(CGM
, ATy
, Offset
, Bits
);
3704 int Size
= Context
.getTypeSizeInChars(QTy
).getQuantity();
3708 std::fill_n(Bits
.begin() + Offset
, Size
,
3709 (uint64_t(1) << Context
.getCharWidth()) - 1);
3712 static uint64_t buildMultiCharMask(const SmallVectorImpl
<uint64_t> &Bits
,
3713 int Pos
, int Size
, int CharWidth
,
3718 for (auto P
= Bits
.begin() + Pos
, E
= Bits
.begin() + Pos
+ Size
; P
!= E
;
3720 Mask
= (Mask
<< CharWidth
) | *P
;
3722 auto P
= Bits
.begin() + Pos
+ Size
, End
= Bits
.begin() + Pos
;
3724 Mask
= (Mask
<< CharWidth
) | *--P
;
3730 // Emit code to clear the bits in a record, which aren't a part of any user
3731 // declared member, when the record is a function return.
3732 llvm::Value
*CodeGenFunction::EmitCMSEClearRecord(llvm::Value
*Src
,
3733 llvm::IntegerType
*ITy
,
3735 assert(Src
->getType() == ITy
);
3736 assert(ITy
->getScalarSizeInBits() <= 64);
3738 const llvm::DataLayout
&DataLayout
= CGM
.getDataLayout();
3739 int Size
= DataLayout
.getTypeStoreSize(ITy
);
3740 SmallVector
<uint64_t, 4> Bits(Size
);
3741 setUsedBits(CGM
, QTy
->castAs
<RecordType
>(), 0, Bits
);
3743 int CharWidth
= CGM
.getContext().getCharWidth();
3745 buildMultiCharMask(Bits
, 0, Size
, CharWidth
, DataLayout
.isBigEndian());
3747 return Builder
.CreateAnd(Src
, Mask
, "cmse.clear");
3750 // Emit code to clear the bits in a record, which aren't a part of any user
3751 // declared member, when the record is a function argument.
3752 llvm::Value
*CodeGenFunction::EmitCMSEClearRecord(llvm::Value
*Src
,
3753 llvm::ArrayType
*ATy
,
3755 const llvm::DataLayout
&DataLayout
= CGM
.getDataLayout();
3756 int Size
= DataLayout
.getTypeStoreSize(ATy
);
3757 SmallVector
<uint64_t, 16> Bits(Size
);
3758 setUsedBits(CGM
, QTy
->castAs
<RecordType
>(), 0, Bits
);
3760 // Clear each element of the LLVM array.
3761 int CharWidth
= CGM
.getContext().getCharWidth();
3763 ATy
->getArrayElementType()->getScalarSizeInBits() / CharWidth
;
3765 llvm::Value
*R
= llvm::PoisonValue::get(ATy
);
3766 for (int I
= 0, N
= ATy
->getArrayNumElements(); I
!= N
; ++I
) {
3767 uint64_t Mask
= buildMultiCharMask(Bits
, MaskIndex
, CharsPerElt
, CharWidth
,
3768 DataLayout
.isBigEndian());
3769 MaskIndex
+= CharsPerElt
;
3770 llvm::Value
*T0
= Builder
.CreateExtractValue(Src
, I
);
3771 llvm::Value
*T1
= Builder
.CreateAnd(T0
, Mask
, "cmse.clear");
3772 R
= Builder
.CreateInsertValue(R
, T1
, I
);
3778 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo
&FI
,
3780 SourceLocation EndLoc
) {
3781 if (FI
.isNoReturn()) {
3782 // Noreturn functions don't return.
3783 EmitUnreachable(EndLoc
);
3787 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<NakedAttr
>()) {
3788 // Naked functions don't have epilogues.
3789 Builder
.CreateUnreachable();
3793 // Functions with no result always return void.
3794 if (!ReturnValue
.isValid()) {
3795 Builder
.CreateRetVoid();
3799 llvm::DebugLoc RetDbgLoc
;
3800 llvm::Value
*RV
= nullptr;
3801 QualType RetTy
= FI
.getReturnType();
3802 const ABIArgInfo
&RetAI
= FI
.getReturnInfo();
3804 switch (RetAI
.getKind()) {
3805 case ABIArgInfo::InAlloca
:
3806 // Aggregates get evaluated directly into the destination. Sometimes we
3807 // need to return the sret value in a register, though.
3808 assert(hasAggregateEvaluationKind(RetTy
));
3809 if (RetAI
.getInAllocaSRet()) {
3810 llvm::Function::arg_iterator EI
= CurFn
->arg_end();
3812 llvm::Value
*ArgStruct
= &*EI
;
3813 llvm::Value
*SRet
= Builder
.CreateStructGEP(
3814 FI
.getArgStruct(), ArgStruct
, RetAI
.getInAllocaFieldIndex());
3816 cast
<llvm::GetElementPtrInst
>(SRet
)->getResultElementType();
3817 RV
= Builder
.CreateAlignedLoad(Ty
, SRet
, getPointerAlign(), "sret");
3821 case ABIArgInfo::Indirect
: {
3822 auto AI
= CurFn
->arg_begin();
3823 if (RetAI
.isSRetAfterThis())
3825 switch (getEvaluationKind(RetTy
)) {
3828 EmitLoadOfComplex(MakeAddrLValue(ReturnValue
, RetTy
), EndLoc
);
3829 EmitStoreOfComplex(RT
, MakeNaturalAlignAddrLValue(&*AI
, RetTy
),
3834 // Do nothing; aggregates get evaluated directly into the destination.
3837 LValueBaseInfo BaseInfo
;
3838 TBAAAccessInfo TBAAInfo
;
3839 CharUnits Alignment
=
3840 CGM
.getNaturalTypeAlignment(RetTy
, &BaseInfo
, &TBAAInfo
);
3841 Address
ArgAddr(&*AI
, ConvertType(RetTy
), Alignment
);
3843 LValue::MakeAddr(ArgAddr
, RetTy
, getContext(), BaseInfo
, TBAAInfo
);
3845 EmitLoadOfScalar(MakeAddrLValue(ReturnValue
, RetTy
), EndLoc
), ArgVal
,
3853 case ABIArgInfo::Extend
:
3854 case ABIArgInfo::Direct
:
3855 if (RetAI
.getCoerceToType() == ConvertType(RetTy
) &&
3856 RetAI
.getDirectOffset() == 0) {
3857 // The internal return value temp always will have pointer-to-return-type
3858 // type, just do a load.
3860 // If there is a dominating store to ReturnValue, we can elide
3861 // the load, zap the store, and usually zap the alloca.
3862 if (llvm::StoreInst
*SI
=
3863 findDominatingStoreToReturnValue(*this)) {
3864 // Reuse the debug location from the store unless there is
3865 // cleanup code to be emitted between the store and return
3867 if (EmitRetDbgLoc
&& !AutoreleaseResult
)
3868 RetDbgLoc
= SI
->getDebugLoc();
3869 // Get the stored value and nuke the now-dead store.
3870 RV
= SI
->getValueOperand();
3871 SI
->eraseFromParent();
3873 // Otherwise, we have to do a simple load.
3875 RV
= Builder
.CreateLoad(ReturnValue
);
3878 // If the value is offset in memory, apply the offset now.
3879 Address V
= emitAddressAtOffset(*this, ReturnValue
, RetAI
);
3881 RV
= CreateCoercedLoad(V
, RetAI
.getCoerceToType(), *this);
3884 // In ARC, end functions that return a retainable type with a call
3885 // to objc_autoreleaseReturnValue.
3886 if (AutoreleaseResult
) {
3888 // Type::isObjCRetainabletype has to be called on a QualType that hasn't
3889 // been stripped of the typedefs, so we cannot use RetTy here. Get the
3890 // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
3891 // CurCodeDecl or BlockInfo.
3894 if (auto *FD
= dyn_cast
<FunctionDecl
>(CurCodeDecl
))
3895 RT
= FD
->getReturnType();
3896 else if (auto *MD
= dyn_cast
<ObjCMethodDecl
>(CurCodeDecl
))
3897 RT
= MD
->getReturnType();
3898 else if (isa
<BlockDecl
>(CurCodeDecl
))
3899 RT
= BlockInfo
->BlockExpression
->getFunctionType()->getReturnType();
3901 llvm_unreachable("Unexpected function/method type");
3903 assert(getLangOpts().ObjCAutoRefCount
&&
3904 !FI
.isReturnsRetained() &&
3905 RT
->isObjCRetainableType());
3907 RV
= emitAutoreleaseOfResult(*this, RV
);
3912 case ABIArgInfo::Ignore
:
3915 case ABIArgInfo::CoerceAndExpand
: {
3916 auto coercionType
= RetAI
.getCoerceAndExpandType();
3918 // Load all of the coerced elements out into results.
3919 llvm::SmallVector
<llvm::Value
*, 4> results
;
3920 Address addr
= ReturnValue
.withElementType(coercionType
);
3921 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
3922 auto coercedEltType
= coercionType
->getElementType(i
);
3923 if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType
))
3926 auto eltAddr
= Builder
.CreateStructGEP(addr
, i
);
3927 auto elt
= Builder
.CreateLoad(eltAddr
);
3928 results
.push_back(elt
);
3931 // If we have one result, it's the single direct result type.
3932 if (results
.size() == 1) {
3935 // Otherwise, we need to make a first-class aggregate.
3937 // Construct a return type that lacks padding elements.
3938 llvm::Type
*returnType
= RetAI
.getUnpaddedCoerceAndExpandType();
3940 RV
= llvm::PoisonValue::get(returnType
);
3941 for (unsigned i
= 0, e
= results
.size(); i
!= e
; ++i
) {
3942 RV
= Builder
.CreateInsertValue(RV
, results
[i
], i
);
3947 case ABIArgInfo::Expand
:
3948 case ABIArgInfo::IndirectAliased
:
3949 llvm_unreachable("Invalid ABI kind for return argument");
3952 llvm::Instruction
*Ret
;
3954 if (CurFuncDecl
&& CurFuncDecl
->hasAttr
<CmseNSEntryAttr
>()) {
3955 // For certain return types, clear padding bits, as they may reveal
3956 // sensitive information.
3957 // Small struct/union types are passed as integers.
3958 auto *ITy
= dyn_cast
<llvm::IntegerType
>(RV
->getType());
3959 if (ITy
!= nullptr && isa
<RecordType
>(RetTy
.getCanonicalType()))
3960 RV
= EmitCMSEClearRecord(RV
, ITy
, RetTy
);
3962 EmitReturnValueCheck(RV
);
3963 Ret
= Builder
.CreateRet(RV
);
3965 Ret
= Builder
.CreateRetVoid();
3969 Ret
->setDebugLoc(std::move(RetDbgLoc
));
3972 void CodeGenFunction::EmitReturnValueCheck(llvm::Value
*RV
) {
3973 // A current decl may not be available when emitting vtable thunks.
3977 // If the return block isn't reachable, neither is this check, so don't emit
3979 if (ReturnBlock
.isValid() && ReturnBlock
.getBlock()->use_empty())
3982 ReturnsNonNullAttr
*RetNNAttr
= nullptr;
3983 if (SanOpts
.has(SanitizerKind::ReturnsNonnullAttribute
))
3984 RetNNAttr
= CurCodeDecl
->getAttr
<ReturnsNonNullAttr
>();
3986 if (!RetNNAttr
&& !requiresReturnValueNullabilityCheck())
3989 // Prefer the returns_nonnull attribute if it's present.
3990 SourceLocation AttrLoc
;
3991 SanitizerMask CheckKind
;
3992 SanitizerHandler Handler
;
3994 assert(!requiresReturnValueNullabilityCheck() &&
3995 "Cannot check nullability and the nonnull attribute");
3996 AttrLoc
= RetNNAttr
->getLocation();
3997 CheckKind
= SanitizerKind::ReturnsNonnullAttribute
;
3998 Handler
= SanitizerHandler::NonnullReturn
;
4000 if (auto *DD
= dyn_cast
<DeclaratorDecl
>(CurCodeDecl
))
4001 if (auto *TSI
= DD
->getTypeSourceInfo())
4002 if (auto FTL
= TSI
->getTypeLoc().getAsAdjusted
<FunctionTypeLoc
>())
4003 AttrLoc
= FTL
.getReturnLoc().findNullabilityLoc();
4004 CheckKind
= SanitizerKind::NullabilityReturn
;
4005 Handler
= SanitizerHandler::NullabilityReturn
;
4008 SanitizerScope
SanScope(this);
4010 // Make sure the "return" source location is valid. If we're checking a
4011 // nullability annotation, make sure the preconditions for the check are met.
4012 llvm::BasicBlock
*Check
= createBasicBlock("nullcheck");
4013 llvm::BasicBlock
*NoCheck
= createBasicBlock("no.nullcheck");
4014 llvm::Value
*SLocPtr
= Builder
.CreateLoad(ReturnLocation
, "return.sloc.load");
4015 llvm::Value
*CanNullCheck
= Builder
.CreateIsNotNull(SLocPtr
);
4016 if (requiresReturnValueNullabilityCheck())
4018 Builder
.CreateAnd(CanNullCheck
, RetValNullabilityPrecondition
);
4019 Builder
.CreateCondBr(CanNullCheck
, Check
, NoCheck
);
4022 // Now do the null check.
4023 llvm::Value
*Cond
= Builder
.CreateIsNotNull(RV
);
4024 llvm::Constant
*StaticData
[] = {EmitCheckSourceLocation(AttrLoc
)};
4025 llvm::Value
*DynamicData
[] = {SLocPtr
};
4026 EmitCheck(std::make_pair(Cond
, CheckKind
), Handler
, StaticData
, DynamicData
);
4031 // The return location should not be used after the check has been emitted.
4032 ReturnLocation
= Address::invalid();
4036 static bool isInAllocaArgument(CGCXXABI
&ABI
, QualType type
) {
4037 const CXXRecordDecl
*RD
= type
->getAsCXXRecordDecl();
4038 return RD
&& ABI
.getRecordArgABI(RD
) == CGCXXABI::RAA_DirectInMemory
;
4041 static AggValueSlot
createPlaceholderSlot(CodeGenFunction
&CGF
,
4043 // FIXME: Generate IR in one pass, rather than going back and fixing up these
4045 llvm::Type
*IRTy
= CGF
.ConvertTypeForMem(Ty
);
4046 llvm::Type
*IRPtrTy
= llvm::PointerType::getUnqual(CGF
.getLLVMContext());
4047 llvm::Value
*Placeholder
= llvm::PoisonValue::get(IRPtrTy
);
4049 // FIXME: When we generate this IR in one pass, we shouldn't need
4050 // this win32-specific alignment hack.
4051 CharUnits Align
= CharUnits::fromQuantity(4);
4052 Placeholder
= CGF
.Builder
.CreateAlignedLoad(IRPtrTy
, Placeholder
, Align
);
4054 return AggValueSlot::forAddr(Address(Placeholder
, IRTy
, Align
),
4056 AggValueSlot::IsNotDestructed
,
4057 AggValueSlot::DoesNotNeedGCBarriers
,
4058 AggValueSlot::IsNotAliased
,
4059 AggValueSlot::DoesNotOverlap
);
4062 void CodeGenFunction::EmitDelegateCallArg(CallArgList
&args
,
4063 const VarDecl
*param
,
4064 SourceLocation loc
) {
4065 // StartFunction converted the ABI-lowered parameter(s) into a
4066 // local alloca. We need to turn that into an r-value suitable
4068 Address local
= GetAddrOfLocalVar(param
);
4070 QualType type
= param
->getType();
4072 // GetAddrOfLocalVar returns a pointer-to-pointer for references,
4073 // but the argument needs to be the original pointer.
4074 if (type
->isReferenceType()) {
4075 args
.add(RValue::get(Builder
.CreateLoad(local
)), type
);
4077 // In ARC, move out of consumed arguments so that the release cleanup
4078 // entered by StartFunction doesn't cause an over-release. This isn't
4079 // optimal -O0 code generation, but it should get cleaned up when
4080 // optimization is enabled. This also assumes that delegate calls are
4081 // performed exactly once for a set of arguments, but that should be safe.
4082 } else if (getLangOpts().ObjCAutoRefCount
&&
4083 param
->hasAttr
<NSConsumedAttr
>() &&
4084 type
->isObjCRetainableType()) {
4085 llvm::Value
*ptr
= Builder
.CreateLoad(local
);
4087 llvm::ConstantPointerNull::get(cast
<llvm::PointerType
>(ptr
->getType()));
4088 Builder
.CreateStore(null
, local
);
4089 args
.add(RValue::get(ptr
), type
);
4091 // For the most part, we just need to load the alloca, except that
4092 // aggregate r-values are actually pointers to temporaries.
4094 args
.add(convertTempToRValue(local
, type
, loc
), type
);
4097 // Deactivate the cleanup for the callee-destructed param that was pushed.
4098 if (type
->isRecordType() && !CurFuncIsThunk
&&
4099 type
->castAs
<RecordType
>()->getDecl()->isParamDestroyedInCallee() &&
4100 param
->needsDestruction(getContext())) {
4101 EHScopeStack::stable_iterator cleanup
=
4102 CalleeDestructedParamCleanups
.lookup(cast
<ParmVarDecl
>(param
));
4103 assert(cleanup
.isValid() &&
4104 "cleanup for callee-destructed param not recorded");
4105 // This unreachable is a temporary marker which will be removed later.
4106 llvm::Instruction
*isActive
= Builder
.CreateUnreachable();
4107 args
.addArgCleanupDeactivation(cleanup
, isActive
);
4111 static bool isProvablyNull(llvm::Value
*addr
) {
4112 return llvm::isa_and_nonnull
<llvm::ConstantPointerNull
>(addr
);
4115 static bool isProvablyNonNull(Address Addr
, CodeGenFunction
&CGF
) {
4116 return llvm::isKnownNonZero(Addr
.getBasePointer(), CGF
.CGM
.getDataLayout());
4119 /// Emit the actual writing-back of a writeback.
4120 static void emitWriteback(CodeGenFunction
&CGF
,
4121 const CallArgList::Writeback
&writeback
) {
4122 const LValue
&srcLV
= writeback
.Source
;
4123 Address srcAddr
= srcLV
.getAddress();
4124 assert(!isProvablyNull(srcAddr
.getBasePointer()) &&
4125 "shouldn't have writeback for provably null argument");
4127 llvm::BasicBlock
*contBB
= nullptr;
4129 // If the argument wasn't provably non-null, we need to null check
4130 // before doing the store.
4131 bool provablyNonNull
= isProvablyNonNull(srcAddr
, CGF
);
4133 if (!provablyNonNull
) {
4134 llvm::BasicBlock
*writebackBB
= CGF
.createBasicBlock("icr.writeback");
4135 contBB
= CGF
.createBasicBlock("icr.done");
4137 llvm::Value
*isNull
= CGF
.Builder
.CreateIsNull(srcAddr
, "icr.isnull");
4138 CGF
.Builder
.CreateCondBr(isNull
, contBB
, writebackBB
);
4139 CGF
.EmitBlock(writebackBB
);
4142 // Load the value to writeback.
4143 llvm::Value
*value
= CGF
.Builder
.CreateLoad(writeback
.Temporary
);
4145 // Cast it back, in case we're writing an id to a Foo* or something.
4146 value
= CGF
.Builder
.CreateBitCast(value
, srcAddr
.getElementType(),
4147 "icr.writeback-cast");
4149 // Perform the writeback.
4151 // If we have a "to use" value, it's something we need to emit a use
4152 // of. This has to be carefully threaded in: if it's done after the
4153 // release it's potentially undefined behavior (and the optimizer
4154 // will ignore it), and if it happens before the retain then the
4155 // optimizer could move the release there.
4156 if (writeback
.ToUse
) {
4157 assert(srcLV
.getObjCLifetime() == Qualifiers::OCL_Strong
);
4159 // Retain the new value. No need to block-copy here: the block's
4160 // being passed up the stack.
4161 value
= CGF
.EmitARCRetainNonBlock(value
);
4163 // Emit the intrinsic use here.
4164 CGF
.EmitARCIntrinsicUse(writeback
.ToUse
);
4166 // Load the old value (primitively).
4167 llvm::Value
*oldValue
= CGF
.EmitLoadOfScalar(srcLV
, SourceLocation());
4169 // Put the new value in place (primitively).
4170 CGF
.EmitStoreOfScalar(value
, srcLV
, /*init*/ false);
4172 // Release the old value.
4173 CGF
.EmitARCRelease(oldValue
, srcLV
.isARCPreciseLifetime());
4175 // Otherwise, we can just do a normal lvalue store.
4177 CGF
.EmitStoreThroughLValue(RValue::get(value
), srcLV
);
4180 // Jump to the continuation block.
4181 if (!provablyNonNull
)
4182 CGF
.EmitBlock(contBB
);
4185 static void emitWritebacks(CodeGenFunction
&CGF
,
4186 const CallArgList
&args
) {
4187 for (const auto &I
: args
.writebacks())
4188 emitWriteback(CGF
, I
);
4191 static void deactivateArgCleanupsBeforeCall(CodeGenFunction
&CGF
,
4192 const CallArgList
&CallArgs
) {
4193 ArrayRef
<CallArgList::CallArgCleanup
> Cleanups
=
4194 CallArgs
.getCleanupsToDeactivate();
4195 // Iterate in reverse to increase the likelihood of popping the cleanup.
4196 for (const auto &I
: llvm::reverse(Cleanups
)) {
4197 CGF
.DeactivateCleanupBlock(I
.Cleanup
, I
.IsActiveIP
);
4198 I
.IsActiveIP
->eraseFromParent();
4202 static const Expr
*maybeGetUnaryAddrOfOperand(const Expr
*E
) {
4203 if (const UnaryOperator
*uop
= dyn_cast
<UnaryOperator
>(E
->IgnoreParens()))
4204 if (uop
->getOpcode() == UO_AddrOf
)
4205 return uop
->getSubExpr();
4209 /// Emit an argument that's being passed call-by-writeback. That is,
4210 /// we are passing the address of an __autoreleased temporary; it
4211 /// might be copy-initialized with the current value of the given
4212 /// address, but it will definitely be copied out of after the call.
4213 static void emitWritebackArg(CodeGenFunction
&CGF
, CallArgList
&args
,
4214 const ObjCIndirectCopyRestoreExpr
*CRE
) {
4217 // Make an optimistic effort to emit the address as an l-value.
4218 // This can fail if the argument expression is more complicated.
4219 if (const Expr
*lvExpr
= maybeGetUnaryAddrOfOperand(CRE
->getSubExpr())) {
4220 srcLV
= CGF
.EmitLValue(lvExpr
);
4222 // Otherwise, just emit it as a scalar.
4224 Address srcAddr
= CGF
.EmitPointerWithAlignment(CRE
->getSubExpr());
4226 QualType srcAddrType
=
4227 CRE
->getSubExpr()->getType()->castAs
<PointerType
>()->getPointeeType();
4228 srcLV
= CGF
.MakeAddrLValue(srcAddr
, srcAddrType
);
4230 Address srcAddr
= srcLV
.getAddress();
4232 // The dest and src types don't necessarily match in LLVM terms
4233 // because of the crazy ObjC compatibility rules.
4235 llvm::PointerType
*destType
=
4236 cast
<llvm::PointerType
>(CGF
.ConvertType(CRE
->getType()));
4237 llvm::Type
*destElemType
=
4238 CGF
.ConvertTypeForMem(CRE
->getType()->getPointeeType());
4240 // If the address is a constant null, just pass the appropriate null.
4241 if (isProvablyNull(srcAddr
.getBasePointer())) {
4242 args
.add(RValue::get(llvm::ConstantPointerNull::get(destType
)),
4247 // Create the temporary.
4249 CGF
.CreateTempAlloca(destElemType
, CGF
.getPointerAlign(), "icr.temp");
4250 // Loading an l-value can introduce a cleanup if the l-value is __weak,
4251 // and that cleanup will be conditional if we can't prove that the l-value
4252 // isn't null, so we need to register a dominating point so that the cleanups
4253 // system will make valid IR.
4254 CodeGenFunction::ConditionalEvaluation
condEval(CGF
);
4256 // Zero-initialize it if we're not doing a copy-initialization.
4257 bool shouldCopy
= CRE
->shouldCopy();
4260 llvm::ConstantPointerNull::get(cast
<llvm::PointerType
>(destElemType
));
4261 CGF
.Builder
.CreateStore(null
, temp
);
4264 llvm::BasicBlock
*contBB
= nullptr;
4265 llvm::BasicBlock
*originBB
= nullptr;
4267 // If the address is *not* known to be non-null, we need to switch.
4268 llvm::Value
*finalArgument
;
4270 bool provablyNonNull
= isProvablyNonNull(srcAddr
, CGF
);
4272 if (provablyNonNull
) {
4273 finalArgument
= temp
.emitRawPointer(CGF
);
4275 llvm::Value
*isNull
= CGF
.Builder
.CreateIsNull(srcAddr
, "icr.isnull");
4277 finalArgument
= CGF
.Builder
.CreateSelect(
4278 isNull
, llvm::ConstantPointerNull::get(destType
),
4279 temp
.emitRawPointer(CGF
), "icr.argument");
4281 // If we need to copy, then the load has to be conditional, which
4282 // means we need control flow.
4284 originBB
= CGF
.Builder
.GetInsertBlock();
4285 contBB
= CGF
.createBasicBlock("icr.cont");
4286 llvm::BasicBlock
*copyBB
= CGF
.createBasicBlock("icr.copy");
4287 CGF
.Builder
.CreateCondBr(isNull
, contBB
, copyBB
);
4288 CGF
.EmitBlock(copyBB
);
4289 condEval
.begin(CGF
);
4293 llvm::Value
*valueToUse
= nullptr;
4295 // Perform a copy if necessary.
4297 RValue srcRV
= CGF
.EmitLoadOfLValue(srcLV
, SourceLocation());
4298 assert(srcRV
.isScalar());
4300 llvm::Value
*src
= srcRV
.getScalarVal();
4301 src
= CGF
.Builder
.CreateBitCast(src
, destElemType
, "icr.cast");
4303 // Use an ordinary store, not a store-to-lvalue.
4304 CGF
.Builder
.CreateStore(src
, temp
);
4306 // If optimization is enabled, and the value was held in a
4307 // __strong variable, we need to tell the optimizer that this
4308 // value has to stay alive until we're doing the store back.
4309 // This is because the temporary is effectively unretained,
4310 // and so otherwise we can violate the high-level semantics.
4311 if (CGF
.CGM
.getCodeGenOpts().OptimizationLevel
!= 0 &&
4312 srcLV
.getObjCLifetime() == Qualifiers::OCL_Strong
) {
4317 // Finish the control flow if we needed it.
4318 if (shouldCopy
&& !provablyNonNull
) {
4319 llvm::BasicBlock
*copyBB
= CGF
.Builder
.GetInsertBlock();
4320 CGF
.EmitBlock(contBB
);
4322 // Make a phi for the value to intrinsically use.
4324 llvm::PHINode
*phiToUse
= CGF
.Builder
.CreatePHI(valueToUse
->getType(), 2,
4326 phiToUse
->addIncoming(valueToUse
, copyBB
);
4327 phiToUse
->addIncoming(llvm::UndefValue::get(valueToUse
->getType()),
4329 valueToUse
= phiToUse
;
4335 args
.addWriteback(srcLV
, temp
, valueToUse
);
4336 args
.add(RValue::get(finalArgument
), CRE
->getType());
4339 void CallArgList::allocateArgumentMemory(CodeGenFunction
&CGF
) {
4343 StackBase
= CGF
.Builder
.CreateStackSave("inalloca.save");
4346 void CallArgList::freeArgumentMemory(CodeGenFunction
&CGF
) const {
4348 // Restore the stack after the call.
4349 CGF
.Builder
.CreateStackRestore(StackBase
);
4353 void CodeGenFunction::EmitNonNullArgCheck(RValue RV
, QualType ArgType
,
4354 SourceLocation ArgLoc
,
4357 if (!AC
.getDecl() || !(SanOpts
.has(SanitizerKind::NonnullAttribute
) ||
4358 SanOpts
.has(SanitizerKind::NullabilityArg
)))
4361 // The param decl may be missing in a variadic function.
4362 auto PVD
= ParmNum
< AC
.getNumParams() ? AC
.getParamDecl(ParmNum
) : nullptr;
4363 unsigned ArgNo
= PVD
? PVD
->getFunctionScopeIndex() : ParmNum
;
4365 // Prefer the nonnull attribute if it's present.
4366 const NonNullAttr
*NNAttr
= nullptr;
4367 if (SanOpts
.has(SanitizerKind::NonnullAttribute
))
4368 NNAttr
= getNonNullAttr(AC
.getDecl(), PVD
, ArgType
, ArgNo
);
4370 bool CanCheckNullability
= false;
4371 if (SanOpts
.has(SanitizerKind::NullabilityArg
) && !NNAttr
&& PVD
&&
4372 !PVD
->getType()->isRecordType()) {
4373 auto Nullability
= PVD
->getType()->getNullability();
4374 CanCheckNullability
= Nullability
&&
4375 *Nullability
== NullabilityKind::NonNull
&&
4376 PVD
->getTypeSourceInfo();
4379 if (!NNAttr
&& !CanCheckNullability
)
4382 SourceLocation AttrLoc
;
4383 SanitizerMask CheckKind
;
4384 SanitizerHandler Handler
;
4386 AttrLoc
= NNAttr
->getLocation();
4387 CheckKind
= SanitizerKind::NonnullAttribute
;
4388 Handler
= SanitizerHandler::NonnullArg
;
4390 AttrLoc
= PVD
->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
4391 CheckKind
= SanitizerKind::NullabilityArg
;
4392 Handler
= SanitizerHandler::NullabilityArg
;
4395 SanitizerScope
SanScope(this);
4396 llvm::Value
*Cond
= EmitNonNullRValueCheck(RV
, ArgType
);
4397 llvm::Constant
*StaticData
[] = {
4398 EmitCheckSourceLocation(ArgLoc
), EmitCheckSourceLocation(AttrLoc
),
4399 llvm::ConstantInt::get(Int32Ty
, ArgNo
+ 1),
4401 EmitCheck(std::make_pair(Cond
, CheckKind
), Handler
, StaticData
, std::nullopt
);
4404 void CodeGenFunction::EmitNonNullArgCheck(Address Addr
, QualType ArgType
,
4405 SourceLocation ArgLoc
,
4406 AbstractCallee AC
, unsigned ParmNum
) {
4407 if (!AC
.getDecl() || !(SanOpts
.has(SanitizerKind::NonnullAttribute
) ||
4408 SanOpts
.has(SanitizerKind::NullabilityArg
)))
4411 EmitNonNullArgCheck(RValue::get(Addr
, *this), ArgType
, ArgLoc
, AC
, ParmNum
);
4414 // Check if the call is going to use the inalloca convention. This needs to
4415 // agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged
4416 // later, so we can't check it directly.
4417 static bool hasInAllocaArgs(CodeGenModule
&CGM
, CallingConv ExplicitCC
,
4418 ArrayRef
<QualType
> ArgTypes
) {
4419 // The Swift calling conventions don't go through the target-specific
4420 // argument classification, they never use inalloca.
4421 // TODO: Consider limiting inalloca use to only calling conventions supported
4423 if (ExplicitCC
== CC_Swift
|| ExplicitCC
== CC_SwiftAsync
)
4425 if (!CGM
.getTarget().getCXXABI().isMicrosoft())
4427 return llvm::any_of(ArgTypes
, [&](QualType Ty
) {
4428 return isInAllocaArgument(CGM
.getCXXABI(), Ty
);
4433 // Determine whether the given argument is an Objective-C method
4434 // that may have type parameters in its signature.
4435 static bool isObjCMethodWithTypeParams(const ObjCMethodDecl
*method
) {
4436 const DeclContext
*dc
= method
->getDeclContext();
4437 if (const ObjCInterfaceDecl
*classDecl
= dyn_cast
<ObjCInterfaceDecl
>(dc
)) {
4438 return classDecl
->getTypeParamListAsWritten();
4441 if (const ObjCCategoryDecl
*catDecl
= dyn_cast
<ObjCCategoryDecl
>(dc
)) {
4442 return catDecl
->getTypeParamList();
4449 /// EmitCallArgs - Emit call arguments for a function.
4450 void CodeGenFunction::EmitCallArgs(
4451 CallArgList
&Args
, PrototypeWrapper Prototype
,
4452 llvm::iterator_range
<CallExpr::const_arg_iterator
> ArgRange
,
4453 AbstractCallee AC
, unsigned ParamsToSkip
, EvaluationOrder Order
) {
4454 SmallVector
<QualType
, 16> ArgTypes
;
4456 assert((ParamsToSkip
== 0 || Prototype
.P
) &&
4457 "Can't skip parameters if type info is not provided");
4459 // This variable only captures *explicitly* written conventions, not those
4460 // applied by default via command line flags or target defaults, such as
4461 // thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would
4462 // require knowing if this is a C++ instance method or being able to see
4463 // unprototyped FunctionTypes.
4464 CallingConv ExplicitCC
= CC_C
;
4466 // First, if a prototype was provided, use those argument types.
4467 bool IsVariadic
= false;
4469 const auto *MD
= Prototype
.P
.dyn_cast
<const ObjCMethodDecl
*>();
4471 IsVariadic
= MD
->isVariadic();
4472 ExplicitCC
= getCallingConventionForDecl(
4473 MD
, CGM
.getTarget().getTriple().isOSWindows());
4474 ArgTypes
.assign(MD
->param_type_begin() + ParamsToSkip
,
4475 MD
->param_type_end());
4477 const auto *FPT
= Prototype
.P
.get
<const FunctionProtoType
*>();
4478 IsVariadic
= FPT
->isVariadic();
4479 ExplicitCC
= FPT
->getExtInfo().getCC();
4480 ArgTypes
.assign(FPT
->param_type_begin() + ParamsToSkip
,
4481 FPT
->param_type_end());
4485 // Check that the prototyped types match the argument expression types.
4486 bool isGenericMethod
= MD
&& isObjCMethodWithTypeParams(MD
);
4487 CallExpr::const_arg_iterator Arg
= ArgRange
.begin();
4488 for (QualType Ty
: ArgTypes
) {
4489 assert(Arg
!= ArgRange
.end() && "Running over edge of argument list!");
4491 (isGenericMethod
|| Ty
->isVariablyModifiedType() ||
4492 Ty
.getNonReferenceType()->isObjCRetainableType() ||
4494 .getCanonicalType(Ty
.getNonReferenceType())
4496 getContext().getCanonicalType((*Arg
)->getType()).getTypePtr()) &&
4497 "type mismatch in call argument!");
4501 // Either we've emitted all the call args, or we have a call to variadic
4503 assert((Arg
== ArgRange
.end() || IsVariadic
) &&
4504 "Extra arguments in non-variadic function!");
4508 // If we still have any arguments, emit them using the type of the argument.
4509 for (auto *A
: llvm::drop_begin(ArgRange
, ArgTypes
.size()))
4510 ArgTypes
.push_back(IsVariadic
? getVarArgType(A
) : A
->getType());
4511 assert((int)ArgTypes
.size() == (ArgRange
.end() - ArgRange
.begin()));
4513 // We must evaluate arguments from right to left in the MS C++ ABI,
4514 // because arguments are destroyed left to right in the callee. As a special
4515 // case, there are certain language constructs that require left-to-right
4516 // evaluation, and in those cases we consider the evaluation order requirement
4517 // to trump the "destruction order is reverse construction order" guarantee.
4519 CGM
.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
4520 ? Order
== EvaluationOrder::ForceLeftToRight
4521 : Order
!= EvaluationOrder::ForceRightToLeft
;
4523 auto MaybeEmitImplicitObjectSize
= [&](unsigned I
, const Expr
*Arg
,
4524 RValue EmittedArg
) {
4525 if (!AC
.hasFunctionDecl() || I
>= AC
.getNumParams())
4527 auto *PS
= AC
.getParamDecl(I
)->getAttr
<PassObjectSizeAttr
>();
4531 const auto &Context
= getContext();
4532 auto SizeTy
= Context
.getSizeType();
4533 auto T
= Builder
.getIntNTy(Context
.getTypeSize(SizeTy
));
4534 assert(EmittedArg
.getScalarVal() && "We emitted nothing for the arg?");
4535 llvm::Value
*V
= evaluateOrEmitBuiltinObjectSize(Arg
, PS
->getType(), T
,
4536 EmittedArg
.getScalarVal(),
4538 Args
.add(RValue::get(V
), SizeTy
);
4539 // If we're emitting args in reverse, be sure to do so with
4540 // pass_object_size, as well.
4542 std::swap(Args
.back(), *(&Args
.back() - 1));
4545 // Insert a stack save if we're going to need any inalloca args.
4546 if (hasInAllocaArgs(CGM
, ExplicitCC
, ArgTypes
)) {
4547 assert(getTarget().getTriple().getArch() == llvm::Triple::x86
&&
4548 "inalloca only supported on x86");
4549 Args
.allocateArgumentMemory(*this);
4552 // Evaluate each argument in the appropriate order.
4553 size_t CallArgsStart
= Args
.size();
4554 for (unsigned I
= 0, E
= ArgTypes
.size(); I
!= E
; ++I
) {
4555 unsigned Idx
= LeftToRight
? I
: E
- I
- 1;
4556 CallExpr::const_arg_iterator Arg
= ArgRange
.begin() + Idx
;
4557 unsigned InitialArgSize
= Args
.size();
4558 // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
4559 // the argument and parameter match or the objc method is parameterized.
4560 assert((!isa
<ObjCIndirectCopyRestoreExpr
>(*Arg
) ||
4561 getContext().hasSameUnqualifiedType((*Arg
)->getType(),
4563 (isa
<ObjCMethodDecl
>(AC
.getDecl()) &&
4564 isObjCMethodWithTypeParams(cast
<ObjCMethodDecl
>(AC
.getDecl())))) &&
4565 "Argument and parameter types don't match");
4566 EmitCallArg(Args
, *Arg
, ArgTypes
[Idx
]);
4567 // In particular, we depend on it being the last arg in Args, and the
4568 // objectsize bits depend on there only being one arg if !LeftToRight.
4569 assert(InitialArgSize
+ 1 == Args
.size() &&
4570 "The code below depends on only adding one arg per EmitCallArg");
4571 (void)InitialArgSize
;
4572 // Since pointer argument are never emitted as LValue, it is safe to emit
4573 // non-null argument check for r-value only.
4574 if (!Args
.back().hasLValue()) {
4575 RValue RVArg
= Args
.back().getKnownRValue();
4576 EmitNonNullArgCheck(RVArg
, ArgTypes
[Idx
], (*Arg
)->getExprLoc(), AC
,
4577 ParamsToSkip
+ Idx
);
4578 // @llvm.objectsize should never have side-effects and shouldn't need
4579 // destruction/cleanups, so we can safely "emit" it after its arg,
4580 // regardless of right-to-leftness
4581 MaybeEmitImplicitObjectSize(Idx
, *Arg
, RVArg
);
4586 // Un-reverse the arguments we just evaluated so they match up with the LLVM
4588 std::reverse(Args
.begin() + CallArgsStart
, Args
.end());
4594 struct DestroyUnpassedArg final
: EHScopeStack::Cleanup
{
4595 DestroyUnpassedArg(Address Addr
, QualType Ty
)
4596 : Addr(Addr
), Ty(Ty
) {}
4601 void Emit(CodeGenFunction
&CGF
, Flags flags
) override
{
4602 QualType::DestructionKind DtorKind
= Ty
.isDestructedType();
4603 if (DtorKind
== QualType::DK_cxx_destructor
) {
4604 const CXXDestructorDecl
*Dtor
= Ty
->getAsCXXRecordDecl()->getDestructor();
4605 assert(!Dtor
->isTrivial());
4606 CGF
.EmitCXXDestructorCall(Dtor
, Dtor_Complete
, /*for vbase*/ false,
4607 /*Delegating=*/false, Addr
, Ty
);
4609 CGF
.callCStructDestructor(CGF
.MakeAddrLValue(Addr
, Ty
));
4614 struct DisableDebugLocationUpdates
{
4615 CodeGenFunction
&CGF
;
4616 bool disabledDebugInfo
;
4617 DisableDebugLocationUpdates(CodeGenFunction
&CGF
, const Expr
*E
) : CGF(CGF
) {
4618 if ((disabledDebugInfo
= isa
<CXXDefaultArgExpr
>(E
) && CGF
.getDebugInfo()))
4619 CGF
.disableDebugInfo();
4621 ~DisableDebugLocationUpdates() {
4622 if (disabledDebugInfo
)
4623 CGF
.enableDebugInfo();
4627 } // end anonymous namespace
4629 RValue
CallArg::getRValue(CodeGenFunction
&CGF
) const {
4632 LValue Copy
= CGF
.MakeAddrLValue(CGF
.CreateMemTemp(Ty
), Ty
);
4633 CGF
.EmitAggregateCopy(Copy
, LV
, Ty
, AggValueSlot::DoesNotOverlap
,
4636 return RValue::getAggregate(Copy
.getAddress());
4639 void CallArg::copyInto(CodeGenFunction
&CGF
, Address Addr
) const {
4640 LValue Dst
= CGF
.MakeAddrLValue(Addr
, Ty
);
4641 if (!HasLV
&& RV
.isScalar())
4642 CGF
.EmitStoreOfScalar(RV
.getScalarVal(), Dst
, /*isInit=*/true);
4643 else if (!HasLV
&& RV
.isComplex())
4644 CGF
.EmitStoreOfComplex(RV
.getComplexVal(), Dst
, /*init=*/true);
4646 auto Addr
= HasLV
? LV
.getAddress() : RV
.getAggregateAddress();
4647 LValue SrcLV
= CGF
.MakeAddrLValue(Addr
, Ty
);
4648 // We assume that call args are never copied into subobjects.
4649 CGF
.EmitAggregateCopy(Dst
, SrcLV
, Ty
, AggValueSlot::DoesNotOverlap
,
4650 HasLV
? LV
.isVolatileQualified()
4651 : RV
.isVolatileQualified());
4656 void CodeGenFunction::EmitCallArg(CallArgList
&args
, const Expr
*E
,
4658 DisableDebugLocationUpdates
Dis(*this, E
);
4659 if (const ObjCIndirectCopyRestoreExpr
*CRE
4660 = dyn_cast
<ObjCIndirectCopyRestoreExpr
>(E
)) {
4661 assert(getLangOpts().ObjCAutoRefCount
);
4662 return emitWritebackArg(*this, args
, CRE
);
4665 assert(type
->isReferenceType() == E
->isGLValue() &&
4666 "reference binding to unmaterialized r-value!");
4668 if (E
->isGLValue()) {
4669 assert(E
->getObjectKind() == OK_Ordinary
);
4670 return args
.add(EmitReferenceBindingToExpr(E
), type
);
4673 bool HasAggregateEvalKind
= hasAggregateEvaluationKind(type
);
4675 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
4676 // However, we still have to push an EH-only cleanup in case we unwind before
4677 // we make it to the call.
4678 if (type
->isRecordType() &&
4679 type
->castAs
<RecordType
>()->getDecl()->isParamDestroyedInCallee()) {
4680 // If we're using inalloca, use the argument memory. Otherwise, use a
4682 AggValueSlot Slot
= args
.isUsingInAlloca()
4683 ? createPlaceholderSlot(*this, type
) : CreateAggTemp(type
, "agg.tmp");
4685 bool DestroyedInCallee
= true, NeedsCleanup
= true;
4686 if (const auto *RD
= type
->getAsCXXRecordDecl())
4687 DestroyedInCallee
= RD
->hasNonTrivialDestructor();
4689 NeedsCleanup
= type
.isDestructedType();
4691 if (DestroyedInCallee
)
4692 Slot
.setExternallyDestructed();
4694 EmitAggExpr(E
, Slot
);
4695 RValue RV
= Slot
.asRValue();
4698 if (DestroyedInCallee
&& NeedsCleanup
) {
4699 // Create a no-op GEP between the placeholder and the cleanup so we can
4700 // RAUW it successfully. It also serves as a marker of the first
4701 // instruction where the cleanup is active.
4702 pushFullExprCleanup
<DestroyUnpassedArg
>(NormalAndEHCleanup
,
4703 Slot
.getAddress(), type
);
4704 // This unreachable is a temporary marker which will be removed later.
4705 llvm::Instruction
*IsActive
=
4706 Builder
.CreateFlagLoad(llvm::Constant::getNullValue(Int8PtrTy
));
4707 args
.addArgCleanupDeactivation(EHStack
.stable_begin(), IsActive
);
4712 if (HasAggregateEvalKind
&& isa
<ImplicitCastExpr
>(E
) &&
4713 cast
<CastExpr
>(E
)->getCastKind() == CK_LValueToRValue
&&
4714 !type
->isArrayParameterType()) {
4715 LValue L
= EmitLValue(cast
<CastExpr
>(E
)->getSubExpr());
4716 assert(L
.isSimple());
4717 args
.addUncopiedAggregate(L
, type
);
4721 args
.add(EmitAnyExprToTemp(E
), type
);
4724 QualType
CodeGenFunction::getVarArgType(const Expr
*Arg
) {
4725 // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
4726 // implicitly widens null pointer constants that are arguments to varargs
4727 // functions to pointer-sized ints.
4728 if (!getTarget().getTriple().isOSWindows())
4729 return Arg
->getType();
4731 if (Arg
->getType()->isIntegerType() &&
4732 getContext().getTypeSize(Arg
->getType()) <
4733 getContext().getTargetInfo().getPointerWidth(LangAS::Default
) &&
4734 Arg
->isNullPointerConstant(getContext(),
4735 Expr::NPC_ValueDependentIsNotNull
)) {
4736 return getContext().getIntPtrType();
4739 return Arg
->getType();
4742 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4743 // optimizer it can aggressively ignore unwind edges.
4745 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction
*Inst
) {
4746 if (CGM
.getCodeGenOpts().OptimizationLevel
!= 0 &&
4747 !CGM
.getCodeGenOpts().ObjCAutoRefCountExceptions
)
4748 Inst
->setMetadata("clang.arc.no_objc_arc_exceptions",
4749 CGM
.getNoObjCARCExceptionsMetadata());
4752 /// Emits a call to the given no-arguments nounwind runtime function.
4754 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4755 const llvm::Twine
&name
) {
4756 return EmitNounwindRuntimeCall(callee
, ArrayRef
<llvm::Value
*>(), name
);
4759 /// Emits a call to the given nounwind runtime function.
4761 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4762 ArrayRef
<Address
> args
,
4763 const llvm::Twine
&name
) {
4764 SmallVector
<llvm::Value
*, 3> values
;
4765 for (auto arg
: args
)
4766 values
.push_back(arg
.emitRawPointer(*this));
4767 return EmitNounwindRuntimeCall(callee
, values
, name
);
4771 CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee
,
4772 ArrayRef
<llvm::Value
*> args
,
4773 const llvm::Twine
&name
) {
4774 llvm::CallInst
*call
= EmitRuntimeCall(callee
, args
, name
);
4775 call
->setDoesNotThrow();
4779 /// Emits a simple call (never an invoke) to the given no-arguments
4780 /// runtime function.
4781 llvm::CallInst
*CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee
,
4782 const llvm::Twine
&name
) {
4783 return EmitRuntimeCall(callee
, std::nullopt
, name
);
4786 // Calls which may throw must have operand bundles indicating which funclet
4787 // they are nested within.
4788 SmallVector
<llvm::OperandBundleDef
, 1>
4789 CodeGenFunction::getBundlesForFunclet(llvm::Value
*Callee
) {
4790 // There is no need for a funclet operand bundle if we aren't inside a
4792 if (!CurrentFuncletPad
)
4793 return (SmallVector
<llvm::OperandBundleDef
, 1>());
4795 // Skip intrinsics which cannot throw (as long as they don't lower into
4796 // regular function calls in the course of IR transformations).
4797 if (auto *CalleeFn
= dyn_cast
<llvm::Function
>(Callee
->stripPointerCasts())) {
4798 if (CalleeFn
->isIntrinsic() && CalleeFn
->doesNotThrow()) {
4799 auto IID
= CalleeFn
->getIntrinsicID();
4800 if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID
))
4801 return (SmallVector
<llvm::OperandBundleDef
, 1>());
4805 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
;
4806 BundleList
.emplace_back("funclet", CurrentFuncletPad
);
4810 /// Emits a simple call (never an invoke) to the given runtime function.
4811 llvm::CallInst
*CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee
,
4812 ArrayRef
<llvm::Value
*> args
,
4813 const llvm::Twine
&name
) {
4814 llvm::CallInst
*call
= Builder
.CreateCall(
4815 callee
, args
, getBundlesForFunclet(callee
.getCallee()), name
);
4816 call
->setCallingConv(getRuntimeCC());
4818 if (CGM
.shouldEmitConvergenceTokens() && call
->isConvergent())
4819 return addControlledConvergenceToken(call
);
4823 /// Emits a call or invoke to the given noreturn runtime function.
4824 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(
4825 llvm::FunctionCallee callee
, ArrayRef
<llvm::Value
*> args
) {
4826 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
4827 getBundlesForFunclet(callee
.getCallee());
4829 if (getInvokeDest()) {
4830 llvm::InvokeInst
*invoke
=
4831 Builder
.CreateInvoke(callee
,
4832 getUnreachableBlock(),
4836 invoke
->setDoesNotReturn();
4837 invoke
->setCallingConv(getRuntimeCC());
4839 llvm::CallInst
*call
= Builder
.CreateCall(callee
, args
, BundleList
);
4840 call
->setDoesNotReturn();
4841 call
->setCallingConv(getRuntimeCC());
4842 Builder
.CreateUnreachable();
4846 /// Emits a call or invoke instruction to the given nullary runtime function.
4848 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee
,
4849 const Twine
&name
) {
4850 return EmitRuntimeCallOrInvoke(callee
, std::nullopt
, name
);
4853 /// Emits a call or invoke instruction to the given runtime function.
4855 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee
,
4856 ArrayRef
<llvm::Value
*> args
,
4857 const Twine
&name
) {
4858 llvm::CallBase
*call
= EmitCallOrInvoke(callee
, args
, name
);
4859 call
->setCallingConv(getRuntimeCC());
4863 /// Emits a call or invoke instruction to the given function, depending
4864 /// on the current state of the EH stack.
4865 llvm::CallBase
*CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee
,
4866 ArrayRef
<llvm::Value
*> Args
,
4867 const Twine
&Name
) {
4868 llvm::BasicBlock
*InvokeDest
= getInvokeDest();
4869 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
4870 getBundlesForFunclet(Callee
.getCallee());
4872 llvm::CallBase
*Inst
;
4874 Inst
= Builder
.CreateCall(Callee
, Args
, BundleList
, Name
);
4876 llvm::BasicBlock
*ContBB
= createBasicBlock("invoke.cont");
4877 Inst
= Builder
.CreateInvoke(Callee
, ContBB
, InvokeDest
, Args
, BundleList
,
4882 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4883 // optimizer it can aggressively ignore unwind edges.
4884 if (CGM
.getLangOpts().ObjCAutoRefCount
)
4885 AddObjCARCExceptionMetadata(Inst
);
4890 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction
*Old
,
4892 DeferredReplacements
.push_back(
4893 std::make_pair(llvm::WeakTrackingVH(Old
), New
));
4898 /// Specify given \p NewAlign as the alignment of return value attribute. If
4899 /// such attribute already exists, re-set it to the maximal one of two options.
4900 [[nodiscard
]] llvm::AttributeList
4901 maybeRaiseRetAlignmentAttribute(llvm::LLVMContext
&Ctx
,
4902 const llvm::AttributeList
&Attrs
,
4903 llvm::Align NewAlign
) {
4904 llvm::Align CurAlign
= Attrs
.getRetAlignment().valueOrOne();
4905 if (CurAlign
>= NewAlign
)
4907 llvm::Attribute AlignAttr
= llvm::Attribute::getWithAlignment(Ctx
, NewAlign
);
4908 return Attrs
.removeRetAttribute(Ctx
, llvm::Attribute::AttrKind::Alignment
)
4909 .addRetAttribute(Ctx
, AlignAttr
);
4912 template <typename AlignedAttrTy
> class AbstractAssumeAlignedAttrEmitter
{
4914 CodeGenFunction
&CGF
;
4916 /// We do nothing if this is, or becomes, nullptr.
4917 const AlignedAttrTy
*AA
= nullptr;
4919 llvm::Value
*Alignment
= nullptr; // May or may not be a constant.
4920 llvm::ConstantInt
*OffsetCI
= nullptr; // Constant, hopefully zero.
4922 AbstractAssumeAlignedAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
)
4926 AA
= FuncDecl
->getAttr
<AlignedAttrTy
>();
4930 /// If we can, materialize the alignment as an attribute on return value.
4931 [[nodiscard
]] llvm::AttributeList
4932 TryEmitAsCallSiteAttribute(const llvm::AttributeList
&Attrs
) {
4933 if (!AA
|| OffsetCI
|| CGF
.SanOpts
.has(SanitizerKind::Alignment
))
4935 const auto *AlignmentCI
= dyn_cast
<llvm::ConstantInt
>(Alignment
);
4938 // We may legitimately have non-power-of-2 alignment here.
4939 // If so, this is UB land, emit it via `@llvm.assume` instead.
4940 if (!AlignmentCI
->getValue().isPowerOf2())
4942 llvm::AttributeList NewAttrs
= maybeRaiseRetAlignmentAttribute(
4943 CGF
.getLLVMContext(), Attrs
,
4945 AlignmentCI
->getLimitedValue(llvm::Value::MaximumAlignment
)));
4946 AA
= nullptr; // We're done. Disallow doing anything else.
4950 /// Emit alignment assumption.
4951 /// This is a general fallback that we take if either there is an offset,
4952 /// or the alignment is variable or we are sanitizing for alignment.
4953 void EmitAsAnAssumption(SourceLocation Loc
, QualType RetTy
, RValue
&Ret
) {
4956 CGF
.emitAlignmentAssumption(Ret
.getScalarVal(), RetTy
, Loc
,
4957 AA
->getLocation(), Alignment
, OffsetCI
);
4958 AA
= nullptr; // We're done. Disallow doing anything else.
4962 /// Helper data structure to emit `AssumeAlignedAttr`.
4963 class AssumeAlignedAttrEmitter final
4964 : public AbstractAssumeAlignedAttrEmitter
<AssumeAlignedAttr
> {
4966 AssumeAlignedAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
)
4967 : AbstractAssumeAlignedAttrEmitter(CGF_
, FuncDecl
) {
4970 // It is guaranteed that the alignment/offset are constants.
4971 Alignment
= cast
<llvm::ConstantInt
>(CGF
.EmitScalarExpr(AA
->getAlignment()));
4972 if (Expr
*Offset
= AA
->getOffset()) {
4973 OffsetCI
= cast
<llvm::ConstantInt
>(CGF
.EmitScalarExpr(Offset
));
4974 if (OffsetCI
->isNullValue()) // Canonicalize zero offset to no offset.
4980 /// Helper data structure to emit `AllocAlignAttr`.
4981 class AllocAlignAttrEmitter final
4982 : public AbstractAssumeAlignedAttrEmitter
<AllocAlignAttr
> {
4984 AllocAlignAttrEmitter(CodeGenFunction
&CGF_
, const Decl
*FuncDecl
,
4985 const CallArgList
&CallArgs
)
4986 : AbstractAssumeAlignedAttrEmitter(CGF_
, FuncDecl
) {
4989 // Alignment may or may not be a constant, and that is okay.
4990 Alignment
= CallArgs
[AA
->getParamIndex().getLLVMIndex()]
4998 static unsigned getMaxVectorWidth(const llvm::Type
*Ty
) {
4999 if (auto *VT
= dyn_cast
<llvm::VectorType
>(Ty
))
5000 return VT
->getPrimitiveSizeInBits().getKnownMinValue();
5001 if (auto *AT
= dyn_cast
<llvm::ArrayType
>(Ty
))
5002 return getMaxVectorWidth(AT
->getElementType());
5004 unsigned MaxVectorWidth
= 0;
5005 if (auto *ST
= dyn_cast
<llvm::StructType
>(Ty
))
5006 for (auto *I
: ST
->elements())
5007 MaxVectorWidth
= std::max(MaxVectorWidth
, getMaxVectorWidth(I
));
5008 return MaxVectorWidth
;
5011 RValue
CodeGenFunction::EmitCall(const CGFunctionInfo
&CallInfo
,
5012 const CGCallee
&Callee
,
5013 ReturnValueSlot ReturnValue
,
5014 const CallArgList
&CallArgs
,
5015 llvm::CallBase
**callOrInvoke
, bool IsMustTail
,
5017 bool IsVirtualFunctionPointerThunk
) {
5018 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
5020 assert(Callee
.isOrdinary() || Callee
.isVirtual());
5022 // Handle struct-return functions by passing a pointer to the
5023 // location that we would like to return into.
5024 QualType RetTy
= CallInfo
.getReturnType();
5025 const ABIArgInfo
&RetAI
= CallInfo
.getReturnInfo();
5027 llvm::FunctionType
*IRFuncTy
= getTypes().GetFunctionType(CallInfo
);
5029 const Decl
*TargetDecl
= Callee
.getAbstractInfo().getCalleeDecl().getDecl();
5030 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(TargetDecl
)) {
5031 // We can only guarantee that a function is called from the correct
5032 // context/function based on the appropriate target attributes,
5033 // so only check in the case where we have both always_inline and target
5034 // since otherwise we could be making a conditional call after a check for
5035 // the proper cpu features (and it won't cause code generation issues due to
5036 // function based code generation).
5037 if (TargetDecl
->hasAttr
<AlwaysInlineAttr
>() &&
5038 (TargetDecl
->hasAttr
<TargetAttr
>() ||
5039 (CurFuncDecl
&& CurFuncDecl
->hasAttr
<TargetAttr
>())))
5040 checkTargetFeatures(Loc
, FD
);
5043 // Some architectures (such as x86-64) have the ABI changed based on
5044 // attribute-target/features. Give them a chance to diagnose.
5045 CGM
.getTargetCodeGenInfo().checkFunctionCallABI(
5046 CGM
, Loc
, dyn_cast_or_null
<FunctionDecl
>(CurCodeDecl
),
5047 dyn_cast_or_null
<FunctionDecl
>(TargetDecl
), CallArgs
, RetTy
);
5049 // 1. Set up the arguments.
5051 // If we're using inalloca, insert the allocation after the stack save.
5052 // FIXME: Do this earlier rather than hacking it in here!
5053 RawAddress ArgMemory
= RawAddress::invalid();
5054 if (llvm::StructType
*ArgStruct
= CallInfo
.getArgStruct()) {
5055 const llvm::DataLayout
&DL
= CGM
.getDataLayout();
5056 llvm::Instruction
*IP
= CallArgs
.getStackBase();
5057 llvm::AllocaInst
*AI
;
5059 IP
= IP
->getNextNode();
5060 AI
= new llvm::AllocaInst(ArgStruct
, DL
.getAllocaAddrSpace(),
5063 AI
= CreateTempAlloca(ArgStruct
, "argmem");
5065 auto Align
= CallInfo
.getArgStructAlignment();
5066 AI
->setAlignment(Align
.getAsAlign());
5067 AI
->setUsedWithInAlloca(true);
5068 assert(AI
->isUsedWithInAlloca() && !AI
->isStaticAlloca());
5069 ArgMemory
= RawAddress(AI
, ArgStruct
, Align
);
5072 ClangToLLVMArgMapping
IRFunctionArgs(CGM
.getContext(), CallInfo
);
5073 SmallVector
<llvm::Value
*, 16> IRCallArgs(IRFunctionArgs
.totalIRArgs());
5075 // If the call returns a temporary with struct return, create a temporary
5076 // alloca to hold the result, unless one is given to us.
5077 Address SRetPtr
= Address::invalid();
5078 RawAddress SRetAlloca
= RawAddress::invalid();
5079 llvm::Value
*UnusedReturnSizePtr
= nullptr;
5080 if (RetAI
.isIndirect() || RetAI
.isInAlloca() || RetAI
.isCoerceAndExpand()) {
5081 if (IsVirtualFunctionPointerThunk
&& RetAI
.isIndirect()) {
5082 SRetPtr
= makeNaturalAddressForPointer(CurFn
->arg_begin() +
5083 IRFunctionArgs
.getSRetArgNo(),
5084 RetTy
, CharUnits::fromQuantity(1));
5085 } else if (!ReturnValue
.isNull()) {
5086 SRetPtr
= ReturnValue
.getAddress();
5088 SRetPtr
= CreateMemTemp(RetTy
, "tmp", &SRetAlloca
);
5089 if (HaveInsertPoint() && ReturnValue
.isUnused()) {
5090 llvm::TypeSize size
=
5091 CGM
.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy
));
5092 UnusedReturnSizePtr
= EmitLifetimeStart(size
, SRetAlloca
.getPointer());
5095 if (IRFunctionArgs
.hasSRetArg()) {
5096 IRCallArgs
[IRFunctionArgs
.getSRetArgNo()] =
5097 getAsNaturalPointerTo(SRetPtr
, RetTy
);
5098 } else if (RetAI
.isInAlloca()) {
5100 Builder
.CreateStructGEP(ArgMemory
, RetAI
.getInAllocaFieldIndex());
5101 Builder
.CreateStore(getAsNaturalPointerTo(SRetPtr
, RetTy
), Addr
);
5105 RawAddress swiftErrorTemp
= RawAddress::invalid();
5106 Address swiftErrorArg
= Address::invalid();
5108 // When passing arguments using temporary allocas, we need to add the
5109 // appropriate lifetime markers. This vector keeps track of all the lifetime
5110 // markers that need to be ended right after the call.
5111 SmallVector
<CallLifetimeEnd
, 2> CallLifetimeEndAfterCall
;
5113 // Translate all of the arguments as necessary to match the IR lowering.
5114 assert(CallInfo
.arg_size() == CallArgs
.size() &&
5115 "Mismatch between function signature & arguments.");
5117 CGFunctionInfo::const_arg_iterator info_it
= CallInfo
.arg_begin();
5118 for (CallArgList::const_iterator I
= CallArgs
.begin(), E
= CallArgs
.end();
5119 I
!= E
; ++I
, ++info_it
, ++ArgNo
) {
5120 const ABIArgInfo
&ArgInfo
= info_it
->info
;
5122 // Insert a padding argument to ensure proper alignment.
5123 if (IRFunctionArgs
.hasPaddingArg(ArgNo
))
5124 IRCallArgs
[IRFunctionArgs
.getPaddingArgNo(ArgNo
)] =
5125 llvm::UndefValue::get(ArgInfo
.getPaddingType());
5127 unsigned FirstIRArg
, NumIRArgs
;
5128 std::tie(FirstIRArg
, NumIRArgs
) = IRFunctionArgs
.getIRArgs(ArgNo
);
5130 bool ArgHasMaybeUndefAttr
=
5131 IsArgumentMaybeUndef(TargetDecl
, CallInfo
.getNumRequiredArgs(), ArgNo
);
5133 switch (ArgInfo
.getKind()) {
5134 case ABIArgInfo::InAlloca
: {
5135 assert(NumIRArgs
== 0);
5136 assert(getTarget().getTriple().getArch() == llvm::Triple::x86
);
5137 if (I
->isAggregate()) {
5138 RawAddress Addr
= I
->hasLValue()
5139 ? I
->getKnownLValue().getAddress()
5140 : I
->getKnownRValue().getAggregateAddress();
5141 llvm::Instruction
*Placeholder
=
5142 cast
<llvm::Instruction
>(Addr
.getPointer());
5144 if (!ArgInfo
.getInAllocaIndirect()) {
5145 // Replace the placeholder with the appropriate argument slot GEP.
5146 CGBuilderTy::InsertPoint IP
= Builder
.saveIP();
5147 Builder
.SetInsertPoint(Placeholder
);
5148 Addr
= Builder
.CreateStructGEP(ArgMemory
,
5149 ArgInfo
.getInAllocaFieldIndex());
5150 Builder
.restoreIP(IP
);
5152 // For indirect things such as overaligned structs, replace the
5153 // placeholder with a regular aggregate temporary alloca. Store the
5154 // address of this alloca into the struct.
5155 Addr
= CreateMemTemp(info_it
->type
, "inalloca.indirect.tmp");
5156 Address ArgSlot
= Builder
.CreateStructGEP(
5157 ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5158 Builder
.CreateStore(Addr
.getPointer(), ArgSlot
);
5160 deferPlaceholderReplacement(Placeholder
, Addr
.getPointer());
5161 } else if (ArgInfo
.getInAllocaIndirect()) {
5162 // Make a temporary alloca and store the address of it into the argument
5164 RawAddress Addr
= CreateMemTempWithoutCast(
5165 I
->Ty
, getContext().getTypeAlignInChars(I
->Ty
),
5166 "indirect-arg-temp");
5167 I
->copyInto(*this, Addr
);
5169 Builder
.CreateStructGEP(ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5170 Builder
.CreateStore(Addr
.getPointer(), ArgSlot
);
5172 // Store the RValue into the argument struct.
5174 Builder
.CreateStructGEP(ArgMemory
, ArgInfo
.getInAllocaFieldIndex());
5175 Addr
= Addr
.withElementType(ConvertTypeForMem(I
->Ty
));
5176 I
->copyInto(*this, Addr
);
5181 case ABIArgInfo::Indirect
:
5182 case ABIArgInfo::IndirectAliased
: {
5183 assert(NumIRArgs
== 1);
5184 if (I
->isAggregate()) {
5185 // We want to avoid creating an unnecessary temporary+copy here;
5186 // however, we need one in three cases:
5187 // 1. If the argument is not byval, and we are required to copy the
5188 // source. (This case doesn't occur on any common architecture.)
5189 // 2. If the argument is byval, RV is not sufficiently aligned, and
5190 // we cannot force it to be sufficiently aligned.
5191 // 3. If the argument is byval, but RV is not located in default
5192 // or alloca address space.
5193 Address Addr
= I
->hasLValue()
5194 ? I
->getKnownLValue().getAddress()
5195 : I
->getKnownRValue().getAggregateAddress();
5196 CharUnits Align
= ArgInfo
.getIndirectAlign();
5197 const llvm::DataLayout
*TD
= &CGM
.getDataLayout();
5199 assert((FirstIRArg
>= IRFuncTy
->getNumParams() ||
5200 IRFuncTy
->getParamType(FirstIRArg
)->getPointerAddressSpace() ==
5201 TD
->getAllocaAddrSpace()) &&
5202 "indirect argument must be in alloca address space");
5204 bool NeedCopy
= false;
5205 if (Addr
.getAlignment() < Align
&&
5206 llvm::getOrEnforceKnownAlignment(Addr
.emitRawPointer(*this),
5208 *TD
) < Align
.getAsAlign()) {
5210 } else if (I
->hasLValue()) {
5211 auto LV
= I
->getKnownLValue();
5212 auto AS
= LV
.getAddressSpace();
5215 ArgInfo
.isIndirectAliased() || ArgInfo
.getIndirectByVal();
5217 if (!isByValOrRef
||
5218 (LV
.getAlignment() < getContext().getTypeAlignInChars(I
->Ty
))) {
5221 if (!getLangOpts().OpenCL
) {
5222 if ((isByValOrRef
&&
5223 (AS
!= LangAS::Default
&&
5224 AS
!= CGM
.getASTAllocaAddressSpace()))) {
5228 // For OpenCL even if RV is located in default or alloca address space
5229 // we don't want to perform address space cast for it.
5230 else if ((isByValOrRef
&&
5231 Addr
.getType()->getAddressSpace() != IRFuncTy
->
5232 getParamType(FirstIRArg
)->getPointerAddressSpace())) {
5238 // Skip the extra memcpy call.
5239 llvm::Value
*V
= getAsNaturalPointerTo(Addr
, I
->Ty
);
5240 auto *T
= llvm::PointerType::get(
5241 CGM
.getLLVMContext(), CGM
.getDataLayout().getAllocaAddrSpace());
5243 llvm::Value
*Val
= getTargetHooks().performAddrSpaceCast(
5244 *this, V
, LangAS::Default
, CGM
.getASTAllocaAddressSpace(), T
,
5246 if (ArgHasMaybeUndefAttr
)
5247 Val
= Builder
.CreateFreeze(Val
);
5248 IRCallArgs
[FirstIRArg
] = Val
;
5253 // For non-aggregate args and aggregate args meeting conditions above
5254 // we need to create an aligned temporary, and copy to it.
5255 RawAddress AI
= CreateMemTempWithoutCast(
5256 I
->Ty
, ArgInfo
.getIndirectAlign(), "byval-temp");
5257 llvm::Value
*Val
= getAsNaturalPointerTo(AI
, I
->Ty
);
5258 if (ArgHasMaybeUndefAttr
)
5259 Val
= Builder
.CreateFreeze(Val
);
5260 IRCallArgs
[FirstIRArg
] = Val
;
5262 // Emit lifetime markers for the temporary alloca.
5263 llvm::TypeSize ByvalTempElementSize
=
5264 CGM
.getDataLayout().getTypeAllocSize(AI
.getElementType());
5265 llvm::Value
*LifetimeSize
=
5266 EmitLifetimeStart(ByvalTempElementSize
, AI
.getPointer());
5268 // Add cleanup code to emit the end lifetime marker after the call.
5269 if (LifetimeSize
) // In case we disabled lifetime markers.
5270 CallLifetimeEndAfterCall
.emplace_back(AI
, LifetimeSize
);
5272 // Generate the copy.
5273 I
->copyInto(*this, AI
);
5277 case ABIArgInfo::Ignore
:
5278 assert(NumIRArgs
== 0);
5281 case ABIArgInfo::Extend
:
5282 case ABIArgInfo::Direct
: {
5283 if (!isa
<llvm::StructType
>(ArgInfo
.getCoerceToType()) &&
5284 ArgInfo
.getCoerceToType() == ConvertType(info_it
->type
) &&
5285 ArgInfo
.getDirectOffset() == 0) {
5286 assert(NumIRArgs
== 1);
5288 if (!I
->isAggregate())
5289 V
= I
->getKnownRValue().getScalarVal();
5291 V
= Builder
.CreateLoad(
5292 I
->hasLValue() ? I
->getKnownLValue().getAddress()
5293 : I
->getKnownRValue().getAggregateAddress());
5295 // Implement swifterror by copying into a new swifterror argument.
5296 // We'll write back in the normal path out of the call.
5297 if (CallInfo
.getExtParameterInfo(ArgNo
).getABI()
5298 == ParameterABI::SwiftErrorResult
) {
5299 assert(!swiftErrorTemp
.isValid() && "multiple swifterror args");
5301 QualType pointeeTy
= I
->Ty
->getPointeeType();
5302 swiftErrorArg
= makeNaturalAddressForPointer(
5303 V
, pointeeTy
, getContext().getTypeAlignInChars(pointeeTy
));
5306 CreateMemTemp(pointeeTy
, getPointerAlign(), "swifterror.temp");
5307 V
= swiftErrorTemp
.getPointer();
5308 cast
<llvm::AllocaInst
>(V
)->setSwiftError(true);
5310 llvm::Value
*errorValue
= Builder
.CreateLoad(swiftErrorArg
);
5311 Builder
.CreateStore(errorValue
, swiftErrorTemp
);
5314 // We might have to widen integers, but we should never truncate.
5315 if (ArgInfo
.getCoerceToType() != V
->getType() &&
5316 V
->getType()->isIntegerTy())
5317 V
= Builder
.CreateZExt(V
, ArgInfo
.getCoerceToType());
5319 // If the argument doesn't match, perform a bitcast to coerce it. This
5320 // can happen due to trivial type mismatches.
5321 if (FirstIRArg
< IRFuncTy
->getNumParams() &&
5322 V
->getType() != IRFuncTy
->getParamType(FirstIRArg
))
5323 V
= Builder
.CreateBitCast(V
, IRFuncTy
->getParamType(FirstIRArg
));
5325 if (ArgHasMaybeUndefAttr
)
5326 V
= Builder
.CreateFreeze(V
);
5327 IRCallArgs
[FirstIRArg
] = V
;
5331 llvm::StructType
*STy
=
5332 dyn_cast
<llvm::StructType
>(ArgInfo
.getCoerceToType());
5333 if (STy
&& ArgInfo
.isDirect() && !ArgInfo
.getCanBeFlattened()) {
5334 llvm::Type
*SrcTy
= ConvertTypeForMem(I
->Ty
);
5335 [[maybe_unused
]] llvm::TypeSize SrcTypeSize
=
5336 CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
5337 [[maybe_unused
]] llvm::TypeSize DstTypeSize
=
5338 CGM
.getDataLayout().getTypeAllocSize(STy
);
5339 if (STy
->containsHomogeneousScalableVectorTypes()) {
5340 assert(SrcTypeSize
== DstTypeSize
&&
5341 "Only allow non-fractional movement of structure with "
5342 "homogeneous scalable vector type");
5344 IRCallArgs
[FirstIRArg
] = I
->getKnownRValue().getScalarVal();
5349 // FIXME: Avoid the conversion through memory if possible.
5350 Address Src
= Address::invalid();
5351 if (!I
->isAggregate()) {
5352 Src
= CreateMemTemp(I
->Ty
, "coerce");
5353 I
->copyInto(*this, Src
);
5355 Src
= I
->hasLValue() ? I
->getKnownLValue().getAddress()
5356 : I
->getKnownRValue().getAggregateAddress();
5359 // If the value is offset in memory, apply the offset now.
5360 Src
= emitAddressAtOffset(*this, Src
, ArgInfo
);
5362 // Fast-isel and the optimizer generally like scalar values better than
5363 // FCAs, so we flatten them if this is safe to do for this argument.
5364 if (STy
&& ArgInfo
.isDirect() && ArgInfo
.getCanBeFlattened()) {
5365 llvm::Type
*SrcTy
= Src
.getElementType();
5366 llvm::TypeSize SrcTypeSize
=
5367 CGM
.getDataLayout().getTypeAllocSize(SrcTy
);
5368 llvm::TypeSize DstTypeSize
= CGM
.getDataLayout().getTypeAllocSize(STy
);
5369 if (SrcTypeSize
.isScalable()) {
5370 assert(STy
->containsHomogeneousScalableVectorTypes() &&
5371 "ABI only supports structure with homogeneous scalable vector "
5373 assert(SrcTypeSize
== DstTypeSize
&&
5374 "Only allow non-fractional movement of structure with "
5375 "homogeneous scalable vector type");
5376 assert(NumIRArgs
== STy
->getNumElements());
5378 llvm::Value
*StoredStructValue
=
5379 Builder
.CreateLoad(Src
, Src
.getName() + ".tuple");
5380 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
5381 llvm::Value
*Extract
= Builder
.CreateExtractValue(
5382 StoredStructValue
, i
, Src
.getName() + ".extract" + Twine(i
));
5383 IRCallArgs
[FirstIRArg
+ i
] = Extract
;
5386 uint64_t SrcSize
= SrcTypeSize
.getFixedValue();
5387 uint64_t DstSize
= DstTypeSize
.getFixedValue();
5389 // If the source type is smaller than the destination type of the
5390 // coerce-to logic, copy the source value into a temp alloca the size
5391 // of the destination type to allow loading all of it. The bits past
5392 // the source value are left undef.
5393 if (SrcSize
< DstSize
) {
5394 Address TempAlloca
= CreateTempAlloca(STy
, Src
.getAlignment(),
5395 Src
.getName() + ".coerce");
5396 Builder
.CreateMemCpy(TempAlloca
, Src
, SrcSize
);
5399 Src
= Src
.withElementType(STy
);
5402 assert(NumIRArgs
== STy
->getNumElements());
5403 for (unsigned i
= 0, e
= STy
->getNumElements(); i
!= e
; ++i
) {
5404 Address EltPtr
= Builder
.CreateStructGEP(Src
, i
);
5405 llvm::Value
*LI
= Builder
.CreateLoad(EltPtr
);
5406 if (ArgHasMaybeUndefAttr
)
5407 LI
= Builder
.CreateFreeze(LI
);
5408 IRCallArgs
[FirstIRArg
+ i
] = LI
;
5412 // In the simple case, just pass the coerced loaded value.
5413 assert(NumIRArgs
== 1);
5415 CreateCoercedLoad(Src
, ArgInfo
.getCoerceToType(), *this);
5417 if (CallInfo
.isCmseNSCall()) {
5418 // For certain parameter types, clear padding bits, as they may reveal
5419 // sensitive information.
5420 // Small struct/union types are passed as integer arrays.
5421 auto *ATy
= dyn_cast
<llvm::ArrayType
>(Load
->getType());
5422 if (ATy
!= nullptr && isa
<RecordType
>(I
->Ty
.getCanonicalType()))
5423 Load
= EmitCMSEClearRecord(Load
, ATy
, I
->Ty
);
5426 if (ArgHasMaybeUndefAttr
)
5427 Load
= Builder
.CreateFreeze(Load
);
5428 IRCallArgs
[FirstIRArg
] = Load
;
5434 case ABIArgInfo::CoerceAndExpand
: {
5435 auto coercionType
= ArgInfo
.getCoerceAndExpandType();
5436 auto layout
= CGM
.getDataLayout().getStructLayout(coercionType
);
5438 llvm::Value
*tempSize
= nullptr;
5439 Address addr
= Address::invalid();
5440 RawAddress AllocaAddr
= RawAddress::invalid();
5441 if (I
->isAggregate()) {
5442 addr
= I
->hasLValue() ? I
->getKnownLValue().getAddress()
5443 : I
->getKnownRValue().getAggregateAddress();
5446 RValue RV
= I
->getKnownRValue();
5447 assert(RV
.isScalar()); // complex should always just be direct
5449 llvm::Type
*scalarType
= RV
.getScalarVal()->getType();
5450 auto scalarSize
= CGM
.getDataLayout().getTypeAllocSize(scalarType
);
5451 auto scalarAlign
= CGM
.getDataLayout().getPrefTypeAlign(scalarType
);
5453 // Materialize to a temporary.
5454 addr
= CreateTempAlloca(
5455 RV
.getScalarVal()->getType(),
5456 CharUnits::fromQuantity(std::max(layout
->getAlignment(), scalarAlign
)),
5458 /*ArraySize=*/nullptr, &AllocaAddr
);
5459 tempSize
= EmitLifetimeStart(scalarSize
, AllocaAddr
.getPointer());
5461 Builder
.CreateStore(RV
.getScalarVal(), addr
);
5464 addr
= addr
.withElementType(coercionType
);
5466 unsigned IRArgPos
= FirstIRArg
;
5467 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
5468 llvm::Type
*eltType
= coercionType
->getElementType(i
);
5469 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
)) continue;
5470 Address eltAddr
= Builder
.CreateStructGEP(addr
, i
);
5471 llvm::Value
*elt
= Builder
.CreateLoad(eltAddr
);
5472 if (ArgHasMaybeUndefAttr
)
5473 elt
= Builder
.CreateFreeze(elt
);
5474 IRCallArgs
[IRArgPos
++] = elt
;
5476 assert(IRArgPos
== FirstIRArg
+ NumIRArgs
);
5479 EmitLifetimeEnd(tempSize
, AllocaAddr
.getPointer());
5485 case ABIArgInfo::Expand
: {
5486 unsigned IRArgPos
= FirstIRArg
;
5487 ExpandTypeToArgs(I
->Ty
, *I
, IRFuncTy
, IRCallArgs
, IRArgPos
);
5488 assert(IRArgPos
== FirstIRArg
+ NumIRArgs
);
5494 const CGCallee
&ConcreteCallee
= Callee
.prepareConcreteCallee(*this);
5495 llvm::Value
*CalleePtr
= ConcreteCallee
.getFunctionPointer();
5497 // If we're using inalloca, set up that argument.
5498 if (ArgMemory
.isValid()) {
5499 llvm::Value
*Arg
= ArgMemory
.getPointer();
5500 assert(IRFunctionArgs
.hasInallocaArg());
5501 IRCallArgs
[IRFunctionArgs
.getInallocaArgNo()] = Arg
;
5504 // 2. Prepare the function pointer.
5506 // If the callee is a bitcast of a non-variadic function to have a
5507 // variadic function pointer type, check to see if we can remove the
5508 // bitcast. This comes up with unprototyped functions.
5510 // This makes the IR nicer, but more importantly it ensures that we
5511 // can inline the function at -O0 if it is marked always_inline.
5512 auto simplifyVariadicCallee
= [](llvm::FunctionType
*CalleeFT
,
5513 llvm::Value
*Ptr
) -> llvm::Function
* {
5514 if (!CalleeFT
->isVarArg())
5517 // Get underlying value if it's a bitcast
5518 if (llvm::ConstantExpr
*CE
= dyn_cast
<llvm::ConstantExpr
>(Ptr
)) {
5519 if (CE
->getOpcode() == llvm::Instruction::BitCast
)
5520 Ptr
= CE
->getOperand(0);
5523 llvm::Function
*OrigFn
= dyn_cast
<llvm::Function
>(Ptr
);
5527 llvm::FunctionType
*OrigFT
= OrigFn
->getFunctionType();
5529 // If the original type is variadic, or if any of the component types
5530 // disagree, we cannot remove the cast.
5531 if (OrigFT
->isVarArg() ||
5532 OrigFT
->getNumParams() != CalleeFT
->getNumParams() ||
5533 OrigFT
->getReturnType() != CalleeFT
->getReturnType())
5536 for (unsigned i
= 0, e
= OrigFT
->getNumParams(); i
!= e
; ++i
)
5537 if (OrigFT
->getParamType(i
) != CalleeFT
->getParamType(i
))
5543 if (llvm::Function
*OrigFn
= simplifyVariadicCallee(IRFuncTy
, CalleePtr
)) {
5545 IRFuncTy
= OrigFn
->getFunctionType();
5548 // 3. Perform the actual call.
5550 // Deactivate any cleanups that we're supposed to do immediately before
5552 if (!CallArgs
.getCleanupsToDeactivate().empty())
5553 deactivateArgCleanupsBeforeCall(*this, CallArgs
);
5555 // Assert that the arguments we computed match up. The IR verifier
5556 // will catch this, but this is a common enough source of problems
5557 // during IRGen changes that it's way better for debugging to catch
5558 // it ourselves here.
5560 assert(IRCallArgs
.size() == IRFuncTy
->getNumParams() || IRFuncTy
->isVarArg());
5561 for (unsigned i
= 0; i
< IRCallArgs
.size(); ++i
) {
5562 // Inalloca argument can have different type.
5563 if (IRFunctionArgs
.hasInallocaArg() &&
5564 i
== IRFunctionArgs
.getInallocaArgNo())
5566 if (i
< IRFuncTy
->getNumParams())
5567 assert(IRCallArgs
[i
]->getType() == IRFuncTy
->getParamType(i
));
5571 // Update the largest vector width if any arguments have vector types.
5572 for (unsigned i
= 0; i
< IRCallArgs
.size(); ++i
)
5573 LargestVectorWidth
= std::max(LargestVectorWidth
,
5574 getMaxVectorWidth(IRCallArgs
[i
]->getType()));
5576 // Compute the calling convention and attributes.
5577 unsigned CallingConv
;
5578 llvm::AttributeList Attrs
;
5579 CGM
.ConstructAttributeList(CalleePtr
->getName(), CallInfo
,
5580 Callee
.getAbstractInfo(), Attrs
, CallingConv
,
5581 /*AttrOnCallSite=*/true,
5584 if (CallingConv
== llvm::CallingConv::X86_VectorCall
&&
5585 getTarget().getTriple().isWindowsArm64EC()) {
5586 CGM
.Error(Loc
, "__vectorcall calling convention is not currently "
5590 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
)) {
5591 if (FD
->hasAttr
<StrictFPAttr
>())
5592 // All calls within a strictfp function are marked strictfp
5593 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP
);
5595 // If -ffast-math is enabled and the function is guarded by an
5596 // '__attribute__((optnone)) adjust the memory attribute so the BE emits the
5597 // library call instead of the intrinsic.
5598 if (FD
->hasAttr
<OptimizeNoneAttr
>() && getLangOpts().FastMath
)
5599 CGM
.AdjustMemoryAttribute(CalleePtr
->getName(), Callee
.getAbstractInfo(),
5602 // Add call-site nomerge attribute if exists.
5603 if (InNoMergeAttributedStmt
)
5604 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge
);
5606 // Add call-site noinline attribute if exists.
5607 if (InNoInlineAttributedStmt
)
5608 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline
);
5610 // Add call-site always_inline attribute if exists.
5611 if (InAlwaysInlineAttributedStmt
)
5613 Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline
);
5615 // Apply some call-site-specific attributes.
5616 // TODO: work this into building the attribute set.
5618 // Apply always_inline to all calls within flatten functions.
5619 // FIXME: should this really take priority over __try, below?
5620 if (CurCodeDecl
&& CurCodeDecl
->hasAttr
<FlattenAttr
>() &&
5621 !InNoInlineAttributedStmt
&&
5622 !(TargetDecl
&& TargetDecl
->hasAttr
<NoInlineAttr
>())) {
5624 Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline
);
5627 // Disable inlining inside SEH __try blocks.
5628 if (isSEHTryScope()) {
5629 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline
);
5632 // Decide whether to use a call or an invoke.
5634 if (currentFunctionUsesSEHTry()) {
5635 // SEH cares about asynchronous exceptions, so everything can "throw."
5636 CannotThrow
= false;
5637 } else if (isCleanupPadScope() &&
5638 EHPersonality::get(*this).isMSVCXXPersonality()) {
5639 // The MSVC++ personality will implicitly terminate the program if an
5640 // exception is thrown during a cleanup outside of a try/catch.
5641 // We don't need to model anything in IR to get this behavior.
5644 // Otherwise, nounwind call sites will never throw.
5645 CannotThrow
= Attrs
.hasFnAttr(llvm::Attribute::NoUnwind
);
5647 if (auto *FPtr
= dyn_cast
<llvm::Function
>(CalleePtr
))
5648 if (FPtr
->hasFnAttribute(llvm::Attribute::NoUnwind
))
5652 // If we made a temporary, be sure to clean up after ourselves. Note that we
5653 // can't depend on being inside of an ExprWithCleanups, so we need to manually
5654 // pop this cleanup later on. Being eager about this is OK, since this
5655 // temporary is 'invisible' outside of the callee.
5656 if (UnusedReturnSizePtr
)
5657 pushFullExprCleanup
<CallLifetimeEnd
>(NormalEHLifetimeMarker
, SRetAlloca
,
5658 UnusedReturnSizePtr
);
5660 llvm::BasicBlock
*InvokeDest
= CannotThrow
? nullptr : getInvokeDest();
5662 SmallVector
<llvm::OperandBundleDef
, 1> BundleList
=
5663 getBundlesForFunclet(CalleePtr
);
5665 if (SanOpts
.has(SanitizerKind::KCFI
) &&
5666 !isa_and_nonnull
<FunctionDecl
>(TargetDecl
))
5667 EmitKCFIOperandBundle(ConcreteCallee
, BundleList
);
5669 // Add the pointer-authentication bundle.
5670 EmitPointerAuthOperandBundle(ConcreteCallee
.getPointerAuthInfo(), BundleList
);
5672 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
))
5673 if (FD
->hasAttr
<StrictFPAttr
>())
5674 // All calls within a strictfp function are marked strictfp
5675 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP
);
5677 AssumeAlignedAttrEmitter
AssumeAlignedAttrEmitter(*this, TargetDecl
);
5678 Attrs
= AssumeAlignedAttrEmitter
.TryEmitAsCallSiteAttribute(Attrs
);
5680 AllocAlignAttrEmitter
AllocAlignAttrEmitter(*this, TargetDecl
, CallArgs
);
5681 Attrs
= AllocAlignAttrEmitter
.TryEmitAsCallSiteAttribute(Attrs
);
5683 // Emit the actual call/invoke instruction.
5686 CI
= Builder
.CreateCall(IRFuncTy
, CalleePtr
, IRCallArgs
, BundleList
);
5688 llvm::BasicBlock
*Cont
= createBasicBlock("invoke.cont");
5689 CI
= Builder
.CreateInvoke(IRFuncTy
, CalleePtr
, Cont
, InvokeDest
, IRCallArgs
,
5693 if (CI
->getCalledFunction() && CI
->getCalledFunction()->hasName() &&
5694 CI
->getCalledFunction()->getName().starts_with("_Z4sqrt")) {
5695 SetSqrtFPAccuracy(CI
);
5700 // If this is within a function that has the guard(nocf) attribute and is an
5701 // indirect call, add the "guard_nocf" attribute to this call to indicate that
5702 // Control Flow Guard checks should not be added, even if the call is inlined.
5703 if (const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(CurFuncDecl
)) {
5704 if (const auto *A
= FD
->getAttr
<CFGuardAttr
>()) {
5705 if (A
->getGuard() == CFGuardAttr::GuardArg::nocf
&& !CI
->getCalledFunction())
5706 Attrs
= Attrs
.addFnAttribute(getLLVMContext(), "guard_nocf");
5710 // Apply the attributes and calling convention.
5711 CI
->setAttributes(Attrs
);
5712 CI
->setCallingConv(static_cast<llvm::CallingConv::ID
>(CallingConv
));
5714 // Apply various metadata.
5716 if (!CI
->getType()->isVoidTy())
5717 CI
->setName("call");
5719 if (CGM
.shouldEmitConvergenceTokens() && CI
->isConvergent())
5720 CI
= addControlledConvergenceToken(CI
);
5722 // Update largest vector width from the return type.
5723 LargestVectorWidth
=
5724 std::max(LargestVectorWidth
, getMaxVectorWidth(CI
->getType()));
5726 // Insert instrumentation or attach profile metadata at indirect call sites.
5727 // For more details, see the comment before the definition of
5728 // IPVK_IndirectCallTarget in InstrProfData.inc.
5729 if (!CI
->getCalledFunction())
5730 PGO
.valueProfile(Builder
, llvm::IPVK_IndirectCallTarget
,
5733 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
5734 // optimizer it can aggressively ignore unwind edges.
5735 if (CGM
.getLangOpts().ObjCAutoRefCount
)
5736 AddObjCARCExceptionMetadata(CI
);
5738 // Set tail call kind if necessary.
5739 if (llvm::CallInst
*Call
= dyn_cast
<llvm::CallInst
>(CI
)) {
5740 if (TargetDecl
&& TargetDecl
->hasAttr
<NotTailCalledAttr
>())
5741 Call
->setTailCallKind(llvm::CallInst::TCK_NoTail
);
5742 else if (IsMustTail
) {
5743 if (getTarget().getTriple().isPPC()) {
5744 if (getTarget().getTriple().isOSAIX())
5745 CGM
.getDiags().Report(Loc
, diag::err_aix_musttail_unsupported
);
5746 else if (!getTarget().hasFeature("pcrelative-memops")) {
5747 if (getTarget().hasFeature("longcall"))
5748 CGM
.getDiags().Report(Loc
, diag::err_ppc_impossible_musttail
) << 0;
5749 else if (Call
->isIndirectCall())
5750 CGM
.getDiags().Report(Loc
, diag::err_ppc_impossible_musttail
) << 1;
5751 else if (isa_and_nonnull
<FunctionDecl
>(TargetDecl
)) {
5752 if (!cast
<FunctionDecl
>(TargetDecl
)->isDefined())
5753 // The undefined callee may be a forward declaration. Without
5754 // knowning all symbols in the module, we won't know the symbol is
5755 // defined or not. Collect all these symbols for later diagnosing.
5756 CGM
.addUndefinedGlobalForTailCall(
5757 {cast
<FunctionDecl
>(TargetDecl
), Loc
});
5759 llvm::GlobalValue::LinkageTypes Linkage
= CGM
.getFunctionLinkage(
5760 GlobalDecl(cast
<FunctionDecl
>(TargetDecl
)));
5761 if (llvm::GlobalValue::isWeakForLinker(Linkage
) ||
5762 llvm::GlobalValue::isDiscardableIfUnused(Linkage
))
5763 CGM
.getDiags().Report(Loc
, diag::err_ppc_impossible_musttail
)
5769 Call
->setTailCallKind(llvm::CallInst::TCK_MustTail
);
5773 // Add metadata for calls to MSAllocator functions
5774 if (getDebugInfo() && TargetDecl
&&
5775 TargetDecl
->hasAttr
<MSAllocatorAttr
>())
5776 getDebugInfo()->addHeapAllocSiteMetadata(CI
, RetTy
->getPointeeType(), Loc
);
5778 // Add metadata if calling an __attribute__((error(""))) or warning fn.
5779 if (TargetDecl
&& TargetDecl
->hasAttr
<ErrorAttr
>()) {
5780 llvm::ConstantInt
*Line
=
5781 llvm::ConstantInt::get(Int64Ty
, Loc
.getRawEncoding());
5782 llvm::ConstantAsMetadata
*MD
= llvm::ConstantAsMetadata::get(Line
);
5783 llvm::MDTuple
*MDT
= llvm::MDNode::get(getLLVMContext(), {MD
});
5784 CI
->setMetadata("srcloc", MDT
);
5787 // 4. Finish the call.
5789 // If the call doesn't return, finish the basic block and clear the
5790 // insertion point; this allows the rest of IRGen to discard
5791 // unreachable code.
5792 if (CI
->doesNotReturn()) {
5793 if (UnusedReturnSizePtr
)
5796 // Strip away the noreturn attribute to better diagnose unreachable UB.
5797 if (SanOpts
.has(SanitizerKind::Unreachable
)) {
5798 // Also remove from function since CallBase::hasFnAttr additionally checks
5799 // attributes of the called function.
5800 if (auto *F
= CI
->getCalledFunction())
5801 F
->removeFnAttr(llvm::Attribute::NoReturn
);
5802 CI
->removeFnAttr(llvm::Attribute::NoReturn
);
5804 // Avoid incompatibility with ASan which relies on the `noreturn`
5805 // attribute to insert handler calls.
5806 if (SanOpts
.hasOneOf(SanitizerKind::Address
|
5807 SanitizerKind::KernelAddress
)) {
5808 SanitizerScope
SanScope(this);
5809 llvm::IRBuilder
<>::InsertPointGuard
IPGuard(Builder
);
5810 Builder
.SetInsertPoint(CI
);
5811 auto *FnType
= llvm::FunctionType::get(CGM
.VoidTy
, /*isVarArg=*/false);
5812 llvm::FunctionCallee Fn
=
5813 CGM
.CreateRuntimeFunction(FnType
, "__asan_handle_no_return");
5814 EmitNounwindRuntimeCall(Fn
);
5818 EmitUnreachable(Loc
);
5819 Builder
.ClearInsertionPoint();
5821 // FIXME: For now, emit a dummy basic block because expr emitters in
5822 // generally are not ready to handle emitting expressions at unreachable
5824 EnsureInsertPoint();
5826 // Return a reasonable RValue.
5827 return GetUndefRValue(RetTy
);
5830 // If this is a musttail call, return immediately. We do not branch to the
5831 // epilogue in this case.
5833 for (auto it
= EHStack
.find(CurrentCleanupScopeDepth
); it
!= EHStack
.end();
5835 EHCleanupScope
*Cleanup
= dyn_cast
<EHCleanupScope
>(&*it
);
5836 if (!(Cleanup
&& Cleanup
->getCleanup()->isRedundantBeforeReturn()))
5837 CGM
.ErrorUnsupported(MustTailCall
, "tail call skipping over cleanups");
5839 if (CI
->getType()->isVoidTy())
5840 Builder
.CreateRetVoid();
5842 Builder
.CreateRet(CI
);
5843 Builder
.ClearInsertionPoint();
5844 EnsureInsertPoint();
5845 return GetUndefRValue(RetTy
);
5848 // Perform the swifterror writeback.
5849 if (swiftErrorTemp
.isValid()) {
5850 llvm::Value
*errorResult
= Builder
.CreateLoad(swiftErrorTemp
);
5851 Builder
.CreateStore(errorResult
, swiftErrorArg
);
5854 // Emit any call-associated writebacks immediately. Arguably this
5855 // should happen after any return-value munging.
5856 if (CallArgs
.hasWritebacks())
5857 emitWritebacks(*this, CallArgs
);
5859 // The stack cleanup for inalloca arguments has to run out of the normal
5860 // lexical order, so deactivate it and run it manually here.
5861 CallArgs
.freeArgumentMemory(*this);
5863 // Extract the return value.
5866 // If the current function is a virtual function pointer thunk, avoid copying
5867 // the return value of the musttail call to a temporary.
5868 if (IsVirtualFunctionPointerThunk
) {
5869 Ret
= RValue::get(CI
);
5872 switch (RetAI
.getKind()) {
5873 case ABIArgInfo::CoerceAndExpand
: {
5874 auto coercionType
= RetAI
.getCoerceAndExpandType();
5876 Address addr
= SRetPtr
.withElementType(coercionType
);
5878 assert(CI
->getType() == RetAI
.getUnpaddedCoerceAndExpandType());
5879 bool requiresExtract
= isa
<llvm::StructType
>(CI
->getType());
5881 unsigned unpaddedIndex
= 0;
5882 for (unsigned i
= 0, e
= coercionType
->getNumElements(); i
!= e
; ++i
) {
5883 llvm::Type
*eltType
= coercionType
->getElementType(i
);
5884 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType
))
5886 Address eltAddr
= Builder
.CreateStructGEP(addr
, i
);
5887 llvm::Value
*elt
= CI
;
5888 if (requiresExtract
)
5889 elt
= Builder
.CreateExtractValue(elt
, unpaddedIndex
++);
5891 assert(unpaddedIndex
== 0);
5892 Builder
.CreateStore(elt
, eltAddr
);
5897 case ABIArgInfo::InAlloca
:
5898 case ABIArgInfo::Indirect
: {
5899 RValue ret
= convertTempToRValue(SRetPtr
, RetTy
, SourceLocation());
5900 if (UnusedReturnSizePtr
)
5905 case ABIArgInfo::Ignore
:
5906 // If we are ignoring an argument that had a result, make sure to
5907 // construct the appropriate return value for our caller.
5908 return GetUndefRValue(RetTy
);
5910 case ABIArgInfo::Extend
:
5911 case ABIArgInfo::Direct
: {
5912 llvm::Type
*RetIRTy
= ConvertType(RetTy
);
5913 if (RetAI
.getCoerceToType() == RetIRTy
&&
5914 RetAI
.getDirectOffset() == 0) {
5915 switch (getEvaluationKind(RetTy
)) {
5917 llvm::Value
*Real
= Builder
.CreateExtractValue(CI
, 0);
5918 llvm::Value
*Imag
= Builder
.CreateExtractValue(CI
, 1);
5919 return RValue::getComplex(std::make_pair(Real
, Imag
));
5924 // If the argument doesn't match, perform a bitcast to coerce it.
5925 // This can happen due to trivial type mismatches.
5926 llvm::Value
*V
= CI
;
5927 if (V
->getType() != RetIRTy
)
5928 V
= Builder
.CreateBitCast(V
, RetIRTy
);
5929 return RValue::get(V
);
5934 // If coercing a fixed vector from a scalable vector for ABI
5935 // compatibility, and the types match, use the llvm.vector.extract
5936 // intrinsic to perform the conversion.
5937 if (auto *FixedDstTy
= dyn_cast
<llvm::FixedVectorType
>(RetIRTy
)) {
5938 llvm::Value
*V
= CI
;
5939 if (auto *ScalableSrcTy
=
5940 dyn_cast
<llvm::ScalableVectorType
>(V
->getType())) {
5941 if (FixedDstTy
->getElementType() ==
5942 ScalableSrcTy
->getElementType()) {
5943 llvm::Value
*Zero
= llvm::Constant::getNullValue(CGM
.Int64Ty
);
5944 V
= Builder
.CreateExtractVector(FixedDstTy
, V
, Zero
,
5946 return RValue::get(V
);
5951 Address DestPtr
= ReturnValue
.getValue();
5952 bool DestIsVolatile
= ReturnValue
.isVolatile();
5954 getContext().getTypeInfoDataSizeInChars(RetTy
).Width
.getQuantity();
5956 if (!DestPtr
.isValid()) {
5957 DestPtr
= CreateMemTemp(RetTy
, "coerce");
5958 DestIsVolatile
= false;
5959 DestSize
= getContext().getTypeSizeInChars(RetTy
).getQuantity();
5962 // An empty record can overlap other data (if declared with
5963 // no_unique_address); omit the store for such types - as there is no
5964 // actual data to store.
5965 if (!isEmptyRecord(getContext(), RetTy
, true)) {
5966 // If the value is offset in memory, apply the offset now.
5967 Address StorePtr
= emitAddressAtOffset(*this, DestPtr
, RetAI
);
5970 llvm::TypeSize::getFixed(DestSize
- RetAI
.getDirectOffset()),
5974 return convertTempToRValue(DestPtr
, RetTy
, SourceLocation());
5977 case ABIArgInfo::Expand
:
5978 case ABIArgInfo::IndirectAliased
:
5979 llvm_unreachable("Invalid ABI kind for return argument");
5982 llvm_unreachable("Unhandled ABIArgInfo::Kind");
5986 // Emit the assume_aligned check on the return value.
5987 if (Ret
.isScalar() && TargetDecl
) {
5988 AssumeAlignedAttrEmitter
.EmitAsAnAssumption(Loc
, RetTy
, Ret
);
5989 AllocAlignAttrEmitter
.EmitAsAnAssumption(Loc
, RetTy
, Ret
);
5992 // Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
5993 // we can't use the full cleanup mechanism.
5994 for (CallLifetimeEnd
&LifetimeEnd
: CallLifetimeEndAfterCall
)
5995 LifetimeEnd
.Emit(*this, /*Flags=*/{});
5997 if (!ReturnValue
.isExternallyDestructed() &&
5998 RetTy
.isDestructedType() == QualType::DK_nontrivial_c_struct
)
5999 pushDestroy(QualType::DK_nontrivial_c_struct
, Ret
.getAggregateAddress(),
6005 CGCallee
CGCallee::prepareConcreteCallee(CodeGenFunction
&CGF
) const {
6007 const CallExpr
*CE
= getVirtualCallExpr();
6008 return CGF
.CGM
.getCXXABI().getVirtualFunctionPointer(
6009 CGF
, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),
6010 CE
? CE
->getBeginLoc() : SourceLocation());
6016 /* VarArg handling */
6018 RValue
CodeGenFunction::EmitVAArg(VAArgExpr
*VE
, Address
&VAListAddr
,
6019 AggValueSlot Slot
) {
6020 VAListAddr
= VE
->isMicrosoftABI() ? EmitMSVAListRef(VE
->getSubExpr())
6021 : EmitVAListRef(VE
->getSubExpr());
6022 QualType Ty
= VE
->getType();
6023 if (VE
->isMicrosoftABI())
6024 return CGM
.getABIInfo().EmitMSVAArg(*this, VAListAddr
, Ty
, Slot
);
6025 return CGM
.getABIInfo().EmitVAArg(*this, VAListAddr
, Ty
, Slot
);