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