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