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