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/InlineAsm.h"
42#include "llvm/IR/IntrinsicInst.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
45#include "llvm/Transforms/Utils/Local.h"
46#include <optional>
47using namespace clang;
48using namespace CodeGen;
49
50/***/
51
53 switch (CC) {
54 default:
55 return llvm::CallingConv::C;
56 case CC_X86StdCall:
57 return llvm::CallingConv::X86_StdCall;
58 case CC_X86FastCall:
59 return llvm::CallingConv::X86_FastCall;
60 case CC_X86RegCall:
61 return llvm::CallingConv::X86_RegCall;
62 case CC_X86ThisCall:
63 return llvm::CallingConv::X86_ThisCall;
64 case CC_Win64:
65 return llvm::CallingConv::Win64;
66 case CC_X86_64SysV:
67 return llvm::CallingConv::X86_64_SysV;
68 case CC_AAPCS:
69 return llvm::CallingConv::ARM_AAPCS;
70 case CC_AAPCS_VFP:
71 return llvm::CallingConv::ARM_AAPCS_VFP;
72 case CC_IntelOclBicc:
73 return llvm::CallingConv::Intel_OCL_BI;
74 // TODO: Add support for __pascal to LLVM.
75 case CC_X86Pascal:
76 return llvm::CallingConv::C;
77 // TODO: Add support for __vectorcall to LLVM.
79 return llvm::CallingConv::X86_VectorCall;
81 return llvm::CallingConv::AArch64_VectorCall;
83 return llvm::CallingConv::AArch64_SVE_VectorCall;
84 case CC_SpirFunction:
85 return llvm::CallingConv::SPIR_FUNC;
86 case CC_DeviceKernel:
88 case CC_PreserveMost:
89 return llvm::CallingConv::PreserveMost;
90 case CC_PreserveAll:
91 return llvm::CallingConv::PreserveAll;
92 case CC_Swift:
93 return llvm::CallingConv::Swift;
94 case CC_SwiftAsync:
95 return llvm::CallingConv::SwiftTail;
96 case CC_M68kRTD:
97 return llvm::CallingConv::M68k_RTD;
98 case CC_PreserveNone:
99 return llvm::CallingConv::PreserveNone;
100 // clang-format off
101 case CC_RISCVVectorCall: return llvm::CallingConv::RISCV_VectorCall;
102 // clang-format on
103#define CC_VLS_CASE(ABI_VLEN) \
104 case CC_RISCVVLSCall_##ABI_VLEN: \
105 return llvm::CallingConv::RISCV_VLSCall_##ABI_VLEN;
106 CC_VLS_CASE(32)
107 CC_VLS_CASE(64)
108 CC_VLS_CASE(128)
109 CC_VLS_CASE(256)
110 CC_VLS_CASE(512)
111 CC_VLS_CASE(1024)
112 CC_VLS_CASE(2048)
113 CC_VLS_CASE(4096)
114 CC_VLS_CASE(8192)
115 CC_VLS_CASE(16384)
116 CC_VLS_CASE(32768)
117 CC_VLS_CASE(65536)
118#undef CC_VLS_CASE
119 }
120}
121
122/// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
123/// qualification. Either or both of RD and MD may be null. A null RD indicates
124/// that there is no meaningful 'this' type, and a null MD can occur when
125/// calling a method pointer.
127 const CXXMethodDecl *MD) {
128 CanQualType RecTy;
129 if (RD)
130 RecTy = Context.getCanonicalTagType(RD);
131 else
132 RecTy = Context.VoidTy;
133
134 if (MD)
136 RecTy, MD->getMethodQualifiers().getAddressSpace()));
137 return Context.getPointerType(RecTy);
138}
139
140/// Returns the canonical formal type of the given C++ method.
142 return MD->getType()
145}
146
147/// Returns the "extra-canonicalized" return type, which discards
148/// qualifiers on the return type. Codegen doesn't care about them,
149/// and it makes ABI code a little easier to be able to assume that
150/// all parameter and return types are top-level unqualified.
153}
154
155/// Arrange the argument and result information for a value of the given
156/// unprototyped freestanding function type.
157const CGFunctionInfo &
159 // When translating an unprototyped function type, always use a
160 // variadic type.
161 return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),
162 FnInfoOpts::None, {}, FTNP->getExtInfo(), {},
163 RequiredArgs(0));
164}
165
168 const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs) {
169 assert(proto->hasExtParameterInfos());
170 assert(paramInfos.size() <= prefixArgs);
171 assert(proto->getNumParams() + prefixArgs <= totalArgs);
172
173 paramInfos.reserve(totalArgs);
174
175 // Add default infos for any prefix args that don't already have infos.
176 paramInfos.resize(prefixArgs);
177
178 // Add infos for the prototype.
179 for (const auto &ParamInfo : proto->getExtParameterInfos()) {
180 paramInfos.push_back(ParamInfo);
181 // pass_object_size params have no parameter info.
182 if (ParamInfo.hasPassObjectSize())
183 paramInfos.emplace_back();
184 }
185
186 assert(paramInfos.size() <= totalArgs &&
187 "Did we forget to insert pass_object_size args?");
188 // Add default infos for the variadic and/or suffix arguments.
189 paramInfos.resize(totalArgs);
190}
191
192/// Adds the formal parameters in FPT to the given prefix. If any parameter in
193/// FPT has pass_object_size attrs, then we'll add parameters for those, too.
195 const CodeGenTypes &CGT, SmallVectorImpl<CanQualType> &prefix,
198 // Fast path: don't touch param info if we don't need to.
199 if (!FPT->hasExtParameterInfos()) {
200 assert(paramInfos.empty() &&
201 "We have paramInfos, but the prototype doesn't?");
202 prefix.append(FPT->param_type_begin(), FPT->param_type_end());
203 return;
204 }
205
206 unsigned PrefixSize = prefix.size();
207 // In the vast majority of cases, we'll have precisely FPT->getNumParams()
208 // parameters; the only thing that can change this is the presence of
209 // pass_object_size. So, we preallocate for the common case.
210 prefix.reserve(prefix.size() + FPT->getNumParams());
211
212 auto ExtInfos = FPT->getExtParameterInfos();
213 assert(ExtInfos.size() == FPT->getNumParams());
214 for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {
215 prefix.push_back(FPT->getParamType(I));
216 if (ExtInfos[I].hasPassObjectSize())
217 prefix.push_back(CGT.getContext().getCanonicalSizeType());
218 }
219
220 addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,
221 prefix.size());
222}
223
226
227/// Arrange the LLVM function layout for a value of the given function
228/// type, on top of any implicit parameters already stored.
229static const CGFunctionInfo &
230arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,
233 ExtParameterInfoList paramInfos;
235 appendParameterTypes(CGT, prefix, paramInfos, FTP);
236 CanQualType resultType = FTP->getReturnType().getUnqualifiedType();
237
238 FnInfoOpts opts =
240 return CGT.arrangeLLVMFunctionInfo(resultType, opts, prefix,
241 FTP->getExtInfo(), paramInfos, Required);
242}
243
245
246/// Arrange the argument and result information for a value of the
247/// given freestanding function type.
248const CGFunctionInfo &
250 CanQualTypeList argTypes;
251 return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,
252 FTP);
253}
254
256 bool IsTargetDefaultMSABI) {
257 // Set the appropriate calling convention for the Function.
258 if (D->hasAttr<StdCallAttr>())
259 return CC_X86StdCall;
260
261 if (D->hasAttr<FastCallAttr>())
262 return CC_X86FastCall;
263
264 if (D->hasAttr<RegCallAttr>())
265 return CC_X86RegCall;
266
267 if (D->hasAttr<ThisCallAttr>())
268 return CC_X86ThisCall;
269
270 if (D->hasAttr<VectorCallAttr>())
271 return CC_X86VectorCall;
272
273 if (D->hasAttr<PascalAttr>())
274 return CC_X86Pascal;
275
276 if (PcsAttr *PCS = D->getAttr<PcsAttr>())
277 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
278
279 if (D->hasAttr<AArch64VectorPcsAttr>())
281
282 if (D->hasAttr<AArch64SVEPcsAttr>())
283 return CC_AArch64SVEPCS;
284
285 if (D->hasAttr<DeviceKernelAttr>())
286 return CC_DeviceKernel;
287
288 if (D->hasAttr<IntelOclBiccAttr>())
289 return CC_IntelOclBicc;
290
291 if (D->hasAttr<MSABIAttr>())
292 return IsTargetDefaultMSABI ? CC_C : CC_Win64;
293
294 if (D->hasAttr<SysVABIAttr>())
295 return IsTargetDefaultMSABI ? CC_X86_64SysV : CC_C;
296
297 if (D->hasAttr<PreserveMostAttr>())
298 return CC_PreserveMost;
299
300 if (D->hasAttr<PreserveAllAttr>())
301 return CC_PreserveAll;
302
303 if (D->hasAttr<M68kRTDAttr>())
304 return CC_M68kRTD;
305
306 if (D->hasAttr<PreserveNoneAttr>())
307 return CC_PreserveNone;
308
309 if (D->hasAttr<RISCVVectorCCAttr>())
310 return CC_RISCVVectorCall;
311
312 if (RISCVVLSCCAttr *PCS = D->getAttr<RISCVVLSCCAttr>()) {
313 switch (PCS->getVectorWidth()) {
314 default:
315 llvm_unreachable("Invalid RISC-V VLS ABI VLEN");
316#define CC_VLS_CASE(ABI_VLEN) \
317 case ABI_VLEN: \
318 return CC_RISCVVLSCall_##ABI_VLEN;
319 CC_VLS_CASE(32)
320 CC_VLS_CASE(64)
321 CC_VLS_CASE(128)
322 CC_VLS_CASE(256)
323 CC_VLS_CASE(512)
324 CC_VLS_CASE(1024)
325 CC_VLS_CASE(2048)
326 CC_VLS_CASE(4096)
327 CC_VLS_CASE(8192)
328 CC_VLS_CASE(16384)
329 CC_VLS_CASE(32768)
330 CC_VLS_CASE(65536)
331#undef CC_VLS_CASE
332 }
333 }
334
335 return CC_C;
336}
337
338/// Arrange the argument and result information for a call to an
339/// unknown C++ non-static member function of the given abstract type.
340/// (A null RD means we don't have any meaningful "this" argument type,
341/// so fall back to a generic pointer type).
342/// The member function must be an ordinary function, i.e. not a
343/// constructor or destructor.
344const CGFunctionInfo &
346 const FunctionProtoType *FTP,
347 const CXXMethodDecl *MD) {
348 CanQualTypeList argTypes;
349
350 // Add the 'this' pointer.
351 argTypes.push_back(DeriveThisType(RD, MD));
352
353 return ::arrangeLLVMFunctionInfo(
354 *this, /*instanceMethod=*/true, argTypes,
356}
357
358/// Set calling convention for CUDA/HIP kernel.
360 const FunctionDecl *FD) {
361 if (FD->hasAttr<CUDAGlobalAttr>()) {
362 const FunctionType *FT = FTy->getAs<FunctionType>();
364 FTy = FT->getCanonicalTypeUnqualified();
365 }
366}
367
368/// Arrange the argument and result information for a declaration or
369/// definition of the given C++ non-static member function. The
370/// member function must be an ordinary function, i.e. not a
371/// constructor or destructor.
372const CGFunctionInfo &
374 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");
375 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
376
379 auto prototype = FT.getAs<FunctionProtoType>();
380
382 // The abstract case is perfectly fine.
383 const CXXRecordDecl *ThisType =
385 return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
386 }
387
388 return arrangeFreeFunctionType(prototype);
389}
390
392 const InheritedConstructor &Inherited, CXXCtorType Type) {
393 // Parameters are unnecessary if we're constructing a base class subobject
394 // and the inherited constructor lives in a virtual base.
395 return Type == Ctor_Complete ||
396 !Inherited.getShadowDecl()->constructsVirtualBase() ||
397 !Target.getCXXABI().hasConstructorVariants();
398}
399
400const CGFunctionInfo &
402 auto *MD = cast<CXXMethodDecl>(GD.getDecl());
403
404 CanQualTypeList argTypes;
405 ExtParameterInfoList paramInfos;
406
408 argTypes.push_back(DeriveThisType(ThisType, MD));
409
410 bool PassParams = true;
411
412 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
413 // A base class inheriting constructor doesn't get forwarded arguments
414 // needed to construct a virtual base (or base class thereof).
415 if (auto Inherited = CD->getInheritedConstructor())
416 PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType());
417 }
418
420
421 // Add the formal parameters.
422 if (PassParams)
423 appendParameterTypes(*this, argTypes, paramInfos, FTP);
424
426 getCXXABI().buildStructorSignature(GD, argTypes);
427 if (!paramInfos.empty()) {
428 // Note: prefix implies after the first param.
429 if (AddedArgs.Prefix)
430 paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,
432 if (AddedArgs.Suffix)
433 paramInfos.append(AddedArgs.Suffix,
435 }
436
437 RequiredArgs required =
438 (PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())
440
441 FunctionType::ExtInfo extInfo = FTP->getExtInfo();
442 CanQualType resultType = getCXXABI().HasThisReturn(GD) ? argTypes.front()
444 ? CGM.getContext().VoidPtrTy
445 : Context.VoidTy;
447 argTypes, extInfo, paramInfos, required);
448}
449
451 const CallArgList &args) {
452 CanQualTypeList argTypes;
453 for (auto &arg : args)
454 argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
455 return argTypes;
456}
457
459 const FunctionArgList &args) {
460 CanQualTypeList argTypes;
461 for (auto &arg : args)
462 argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));
463 return argTypes;
464}
465
467getExtParameterInfosForCall(const FunctionProtoType *proto, unsigned prefixArgs,
468 unsigned totalArgs) {
470 if (proto->hasExtParameterInfos()) {
471 addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);
472 }
473 return result;
474}
475
476/// Arrange a call to a C++ method, passing the given arguments.
477///
478/// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`
479/// parameter.
480/// ExtraSuffixArgs is the number of ABI-specific args passed at the end of
481/// args.
482/// PassProtoArgs indicates whether `args` has args for the parameters in the
483/// given CXXConstructorDecl.
485 const CallArgList &args, const CXXConstructorDecl *D, CXXCtorType CtorKind,
486 unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs) {
487 CanQualTypeList ArgTypes;
488 for (const auto &Arg : args)
489 ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
490
491 // +1 for implicit this, which should always be args[0].
492 unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
493
495 RequiredArgs Required = PassProtoArgs
497 FPT, TotalPrefixArgs + ExtraSuffixArgs)
499
500 GlobalDecl GD(D, CtorKind);
501 CanQualType ResultType = getCXXABI().HasThisReturn(GD) ? ArgTypes.front()
503 ? CGM.getContext().VoidPtrTy
504 : Context.VoidTy;
505
506 FunctionType::ExtInfo Info = FPT->getExtInfo();
507 ExtParameterInfoList ParamInfos;
508 // If the prototype args are elided, we should only have ABI-specific args,
509 // which never have param info.
510 if (PassProtoArgs && FPT->hasExtParameterInfos()) {
511 // ABI-specific suffix arguments are treated the same as variadic arguments.
512 addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,
513 ArgTypes.size());
514 }
515
517 ArgTypes, Info, ParamInfos, Required);
518}
519
520/// Arrange the argument and result information for the declaration or
521/// definition of the given function.
522const CGFunctionInfo &
524 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
525 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
526 if (MD->isImplicitObjectMemberFunction())
528
530
531 assert(isa<FunctionType>(FTy));
532 setCUDAKernelCallingConvention(FTy, CGM, FD);
533
534 if (DeviceKernelAttr::isOpenCLSpelling(FD->getAttr<DeviceKernelAttr>()) &&
536 const FunctionType *FT = FTy->getAs<FunctionType>();
538 FTy = FT->getCanonicalTypeUnqualified();
539 }
540
541 // When declaring a function without a prototype, always use a
542 // non-variadic type.
544 return arrangeLLVMFunctionInfo(noProto->getReturnType(), FnInfoOpts::None,
545 {}, noProto->getExtInfo(), {},
547 }
548
550}
551
552/// Arrange the argument and result information for the declaration or
553/// definition of an Objective-C method.
554const CGFunctionInfo &
556 // It happens that this is the same as a call with no optional
557 // arguments, except also using the formal 'self' type.
559}
560
561/// Arrange the argument and result information for the function type
562/// through which to perform a send to the given Objective-C method,
563/// using the given receiver type. The receiver type is not always
564/// the 'self' type of the method or even an Objective-C pointer type.
565/// This is *not* the right method for actually performing such a
566/// message send, due to the possibility of optional arguments.
567const CGFunctionInfo &
569 QualType receiverType) {
570 CanQualTypeList argTys;
571 ExtParameterInfoList extParamInfos(MD->isDirectMethod() ? 1 : 2);
572 argTys.push_back(Context.getCanonicalParamType(receiverType));
573 if (!MD->isDirectMethod())
574 argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
575 for (const auto *I : MD->parameters()) {
576 argTys.push_back(Context.getCanonicalParamType(I->getType()));
578 I->hasAttr<NoEscapeAttr>());
579 extParamInfos.push_back(extParamInfo);
580 }
581
583 bool IsTargetDefaultMSABI =
584 getContext().getTargetInfo().getTriple().isOSWindows() ||
585 getContext().getTargetInfo().getTriple().isUEFI();
586 einfo = einfo.withCallingConv(
587 getCallingConventionForDecl(MD, IsTargetDefaultMSABI));
588
589 if (getContext().getLangOpts().ObjCAutoRefCount &&
590 MD->hasAttr<NSReturnsRetainedAttr>())
591 einfo = einfo.withProducesResult(true);
592
593 RequiredArgs required =
594 (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
595
597 FnInfoOpts::None, argTys, einfo, extParamInfos,
598 required);
599}
600
601const CGFunctionInfo &
603 const CallArgList &args) {
604 CanQualTypeList argTypes = getArgTypesForCall(Context, args);
606
608 argTypes, einfo, {}, RequiredArgs::All);
609}
610
612 // FIXME: Do we need to handle ObjCMethodDecl?
613 if (isa<CXXConstructorDecl>(GD.getDecl()) ||
614 isa<CXXDestructorDecl>(GD.getDecl()))
616
618}
619
620/// Arrange a thunk that takes 'this' as the first parameter followed by
621/// varargs. Return a void pointer, regardless of the actual return type.
622/// The body of the thunk will end in a musttail call to a function of the
623/// correct type, and the caller will bitcast the function to the correct
624/// prototype.
625const CGFunctionInfo &
627 assert(MD->isVirtual() && "only methods have thunks");
629 CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)};
630 return arrangeLLVMFunctionInfo(Context.VoidTy, FnInfoOpts::None, ArgTys,
631 FTP->getExtInfo(), {}, RequiredArgs(1));
632}
633
634const CGFunctionInfo &
636 CXXCtorType CT) {
637 assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
638
641 const CXXRecordDecl *RD = CD->getParent();
642 ArgTys.push_back(DeriveThisType(RD, CD));
643 if (CT == Ctor_CopyingClosure)
644 ArgTys.push_back(*FTP->param_type_begin());
645 if (RD->getNumVBases() > 0)
646 ArgTys.push_back(Context.IntTy);
648 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
650 ArgTys, FunctionType::ExtInfo(CC), {},
652}
653
654/// Arrange a call as unto a free function, except possibly with an
655/// additional number of formal parameters considered required.
656static const CGFunctionInfo &
658 const CallArgList &args, const FunctionType *fnType,
659 unsigned numExtraRequiredArgs, bool chainCall) {
660 assert(args.size() >= numExtraRequiredArgs);
661
662 ExtParameterInfoList paramInfos;
663
664 // In most cases, there are no optional arguments.
666
667 // If we have a variadic prototype, the required arguments are the
668 // extra prefix plus the arguments in the prototype.
669 if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
670 if (proto->isVariadic())
671 required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs);
672
673 if (proto->hasExtParameterInfos())
674 addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,
675 args.size());
676
677 // If we don't have a prototype at all, but we're supposed to
678 // explicitly use the variadic convention for unprototyped calls,
679 // treat all of the arguments as required but preserve the nominal
680 // possibility of variadics.
682 args, cast<FunctionNoProtoType>(fnType))) {
683 required = RequiredArgs(args.size());
684 }
685
686 CanQualTypeList argTypes;
687 for (const auto &arg : args)
688 argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));
691 opts, argTypes, fnType->getExtInfo(),
692 paramInfos, required);
693}
694
695/// Figure out the rules for calling a function with the given formal
696/// type using the given arguments. The arguments are necessary
697/// because the function might be unprototyped, in which case it's
698/// target-dependent in crazy ways.
700 const CallArgList &args, const FunctionType *fnType, bool chainCall) {
701 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,
702 chainCall ? 1 : 0, chainCall);
703}
704
705/// A block function is essentially a free function with an
706/// extra implicit argument.
707const CGFunctionInfo &
709 const FunctionType *fnType) {
710 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,
711 /*chainCall=*/false);
712}
713
714const CGFunctionInfo &
716 const FunctionArgList &params) {
717 ExtParameterInfoList paramInfos =
718 getExtParameterInfosForCall(proto, 1, params.size());
719 CanQualTypeList argTypes = getArgTypesForDeclaration(Context, params);
720
722 FnInfoOpts::None, argTypes,
723 proto->getExtInfo(), paramInfos,
725}
726
727const CGFunctionInfo &
729 const CallArgList &args) {
730 CanQualTypeList argTypes;
731 for (const auto &Arg : args)
732 argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
734 argTypes, FunctionType::ExtInfo(),
735 /*paramInfos=*/{}, RequiredArgs::All);
736}
737
738const CGFunctionInfo &
740 const FunctionArgList &args) {
741 CanQualTypeList argTypes = getArgTypesForDeclaration(Context, args);
742
744 argTypes, FunctionType::ExtInfo(), {},
746}
747
749 CanQualType resultType, ArrayRef<CanQualType> argTypes) {
750 return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::None, argTypes,
753}
754
755const CGFunctionInfo &
757 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
792}
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 {
975
977
978 RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,
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?
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()) {
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())) {
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())) {
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.
1340 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
1341 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
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) &&
1667}
1668
1670 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
1671 switch (BT->getKind()) {
1672 default:
1673 return false;
1674 case BuiltinType::Float:
1676 case BuiltinType::Double:
1678 case BuiltinType::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)
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->getOriginalDecl()))
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;
1996 FuncAttrs.addAttribute("frame-pointer",
1998 CodeGenOpts.getFramePointer()));
1999 }
2000
2001 if (CodeGenOpts.LessPreciseFPMAD)
2002 FuncAttrs.addAttribute("less-precise-fpmad", "true");
2003
2004 if (CodeGenOpts.NullPointerIsValid)
2005 FuncAttrs.addAttribute(llvm::Attribute::NullPointerIsValid);
2006
2008 FuncAttrs.addAttribute("no-trapping-math", "true");
2009
2010 // TODO: Are these all needed?
2011 // unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.
2012 if (LangOpts.NoHonorInfs)
2013 FuncAttrs.addAttribute("no-infs-fp-math", "true");
2014 if (LangOpts.NoHonorNaNs)
2015 FuncAttrs.addAttribute("no-nans-fp-math", "true");
2016 if (LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
2017 LangOpts.NoSignedZero && LangOpts.ApproxFunc &&
2018 (LangOpts.getDefaultFPContractMode() ==
2020 LangOpts.getDefaultFPContractMode() ==
2022 FuncAttrs.addAttribute("unsafe-fp-math", "true");
2023 if (CodeGenOpts.SoftFloat)
2024 FuncAttrs.addAttribute("use-soft-float", "true");
2025 FuncAttrs.addAttribute("stack-protector-buffer-size",
2026 llvm::utostr(CodeGenOpts.SSPBufferSize));
2027 if (LangOpts.NoSignedZero)
2028 FuncAttrs.addAttribute("no-signed-zeros-fp-math", "true");
2029
2030 // TODO: Reciprocal estimate codegen options should apply to instructions?
2031 const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
2032 if (!Recips.empty())
2033 FuncAttrs.addAttribute("reciprocal-estimates", llvm::join(Recips, ","));
2034
2035 if (!CodeGenOpts.PreferVectorWidth.empty() &&
2036 CodeGenOpts.PreferVectorWidth != "none")
2037 FuncAttrs.addAttribute("prefer-vector-width",
2038 CodeGenOpts.PreferVectorWidth);
2039
2040 if (CodeGenOpts.StackRealignment)
2041 FuncAttrs.addAttribute("stackrealign");
2042 if (CodeGenOpts.Backchain)
2043 FuncAttrs.addAttribute("backchain");
2044 if (CodeGenOpts.EnableSegmentedStacks)
2045 FuncAttrs.addAttribute("split-stack");
2046
2047 if (CodeGenOpts.SpeculativeLoadHardening)
2048 FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
2049
2050 // Add zero-call-used-regs attribute.
2051 switch (CodeGenOpts.getZeroCallUsedRegs()) {
2052 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip:
2053 FuncAttrs.removeAttribute("zero-call-used-regs");
2054 break;
2055 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg:
2056 FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr-arg");
2057 break;
2058 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR:
2059 FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr");
2060 break;
2061 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg:
2062 FuncAttrs.addAttribute("zero-call-used-regs", "used-arg");
2063 break;
2064 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used:
2065 FuncAttrs.addAttribute("zero-call-used-regs", "used");
2066 break;
2067 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg:
2068 FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr-arg");
2069 break;
2070 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR:
2071 FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr");
2072 break;
2073 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg:
2074 FuncAttrs.addAttribute("zero-call-used-regs", "all-arg");
2075 break;
2076 case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All:
2077 FuncAttrs.addAttribute("zero-call-used-regs", "all");
2078 break;
2079 }
2080 }
2081
2082 if (LangOpts.assumeFunctionsAreConvergent()) {
2083 // Conservatively, mark all functions and calls in CUDA and OpenCL as
2084 // convergent (meaning, they may call an intrinsically convergent op, such
2085 // as __syncthreads() / barrier(), and so can't have certain optimizations
2086 // applied around them). LLVM will remove this attribute where it safely
2087 // can.
2088 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
2089 }
2090
2091 // TODO: NoUnwind attribute should be added for other GPU modes HIP,
2092 // OpenMP offload. AFAIK, neither of them support exceptions in device code.
2093 if ((LangOpts.CUDA && LangOpts.CUDAIsDevice) || LangOpts.OpenCL ||
2094 LangOpts.SYCLIsDevice) {
2095 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2096 }
2097
2098 if (CodeGenOpts.SaveRegParams && !AttrOnCallSite)
2099 FuncAttrs.addAttribute("save-reg-params");
2100
2101 for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {
2102 StringRef Var, Value;
2103 std::tie(Var, Value) = Attr.split('=');
2104 FuncAttrs.addAttribute(Var, Value);
2105 }
2106
2109}
2110
2111/// Merges `target-features` from \TargetOpts and \F, and sets the result in
2112/// \FuncAttr
2113/// * features from \F are always kept
2114/// * a feature from \TargetOpts is kept if itself and its opposite are absent
2115/// from \F
2116static void
2118 const llvm::Function &F,
2119 const TargetOptions &TargetOpts) {
2120 auto FFeatures = F.getFnAttribute("target-features");
2121
2122 llvm::StringSet<> MergedNames;
2123 SmallVector<StringRef> MergedFeatures;
2124 MergedFeatures.reserve(TargetOpts.Features.size());
2125
2126 auto AddUnmergedFeatures = [&](auto &&FeatureRange) {
2127 for (StringRef Feature : FeatureRange) {
2128 if (Feature.empty())
2129 continue;
2130 assert(Feature[0] == '+' || Feature[0] == '-');
2131 StringRef Name = Feature.drop_front(1);
2132 bool Merged = !MergedNames.insert(Name).second;
2133 if (!Merged)
2134 MergedFeatures.push_back(Feature);
2135 }
2136 };
2137
2138 if (FFeatures.isValid())
2139 AddUnmergedFeatures(llvm::split(FFeatures.getValueAsString(), ','));
2140 AddUnmergedFeatures(TargetOpts.Features);
2141
2142 if (!MergedFeatures.empty()) {
2143 llvm::sort(MergedFeatures);
2144 FuncAttr.addAttribute("target-features", llvm::join(MergedFeatures, ","));
2145 }
2146}
2147
2149 llvm::Function &F, const CodeGenOptions &CodeGenOpts,
2150 const LangOptions &LangOpts, const TargetOptions &TargetOpts,
2151 bool WillInternalize) {
2152
2153 llvm::AttrBuilder FuncAttrs(F.getContext());
2154 // Here we only extract the options that are relevant compared to the version
2155 // from GetCPUAndFeaturesAttributes.
2156 if (!TargetOpts.CPU.empty())
2157 FuncAttrs.addAttribute("target-cpu", TargetOpts.CPU);
2158 if (!TargetOpts.TuneCPU.empty())
2159 FuncAttrs.addAttribute("tune-cpu", TargetOpts.TuneCPU);
2160
2161 ::getTrivialDefaultFunctionAttributes(F.getName(), F.hasOptNone(),
2162 CodeGenOpts, LangOpts,
2163 /*AttrOnCallSite=*/false, FuncAttrs);
2164
2165 if (!WillInternalize && F.isInterposable()) {
2166 // Do not promote "dynamic" denormal-fp-math to this translation unit's
2167 // setting for weak functions that won't be internalized. The user has no
2168 // real control for how builtin bitcode is linked, so we shouldn't assume
2169 // later copies will use a consistent mode.
2170 F.addFnAttrs(FuncAttrs);
2171 return;
2172 }
2173
2174 llvm::AttributeMask AttrsToRemove;
2175
2176 llvm::DenormalMode DenormModeToMerge = F.getDenormalModeRaw();
2177 llvm::DenormalMode DenormModeToMergeF32 = F.getDenormalModeF32Raw();
2178 llvm::DenormalMode Merged =
2179 CodeGenOpts.FPDenormalMode.mergeCalleeMode(DenormModeToMerge);
2180 llvm::DenormalMode MergedF32 = CodeGenOpts.FP32DenormalMode;
2181
2182 if (DenormModeToMergeF32.isValid()) {
2183 MergedF32 =
2184 CodeGenOpts.FP32DenormalMode.mergeCalleeMode(DenormModeToMergeF32);
2185 }
2186
2187 if (Merged == llvm::DenormalMode::getDefault()) {
2188 AttrsToRemove.addAttribute("denormal-fp-math");
2189 } else if (Merged != DenormModeToMerge) {
2190 // Overwrite existing attribute
2191 FuncAttrs.addAttribute("denormal-fp-math",
2192 CodeGenOpts.FPDenormalMode.str());
2193 }
2194
2195 if (MergedF32 == llvm::DenormalMode::getDefault()) {
2196 AttrsToRemove.addAttribute("denormal-fp-math-f32");
2197 } else if (MergedF32 != DenormModeToMergeF32) {
2198 // Overwrite existing attribute
2199 FuncAttrs.addAttribute("denormal-fp-math-f32",
2200 CodeGenOpts.FP32DenormalMode.str());
2201 }
2202
2203 F.removeFnAttrs(AttrsToRemove);
2204 addDenormalModeAttrs(Merged, MergedF32, FuncAttrs);
2205
2206 overrideFunctionFeaturesWithTargetFeatures(FuncAttrs, F, TargetOpts);
2207
2208 F.addFnAttrs(FuncAttrs);
2209}
2210
2211void CodeGenModule::getTrivialDefaultFunctionAttributes(
2212 StringRef Name, bool HasOptnone, bool AttrOnCallSite,
2213 llvm::AttrBuilder &FuncAttrs) {
2214 ::getTrivialDefaultFunctionAttributes(Name, HasOptnone, getCodeGenOpts(),
2215 getLangOpts(), AttrOnCallSite,
2216 FuncAttrs);
2217}
2218
2219void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
2220 bool HasOptnone,
2221 bool AttrOnCallSite,
2222 llvm::AttrBuilder &FuncAttrs) {
2223 getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,
2224 FuncAttrs);
2225
2226 if (!AttrOnCallSite)
2228 FuncAttrs);
2229
2230 // If we're just getting the default, get the default values for mergeable
2231 // attributes.
2232 if (!AttrOnCallSite)
2233 addMergableDefaultFunctionAttributes(CodeGenOpts, FuncAttrs);
2234}
2235
2237 llvm::AttrBuilder &attrs) {
2238 getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,
2239 /*for call*/ false, attrs);
2240 GetCPUAndFeaturesAttributes(GlobalDecl(), attrs);
2241}
2242
2243static void addNoBuiltinAttributes(llvm::AttrBuilder &FuncAttrs,
2244 const LangOptions &LangOpts,
2245 const NoBuiltinAttr *NBA = nullptr) {
2246 auto AddNoBuiltinAttr = [&FuncAttrs](StringRef BuiltinName) {
2247 SmallString<32> AttributeName;
2248 AttributeName += "no-builtin-";
2249 AttributeName += BuiltinName;
2250 FuncAttrs.addAttribute(AttributeName);
2251 };
2252
2253 // First, handle the language options passed through -fno-builtin.
2254 if (LangOpts.NoBuiltin) {
2255 // -fno-builtin disables them all.
2256 FuncAttrs.addAttribute("no-builtins");
2257 return;
2258 }
2259
2260 // Then, add attributes for builtins specified through -fno-builtin-<name>.
2261 llvm::for_each(LangOpts.NoBuiltinFuncs, AddNoBuiltinAttr);
2262
2263 // Now, let's check the __attribute__((no_builtin("...")) attribute added to
2264 // the source.
2265 if (!NBA)
2266 return;
2267
2268 // If there is a wildcard in the builtin names specified through the
2269 // attribute, disable them all.
2270 if (llvm::is_contained(NBA->builtinNames(), "*")) {
2271 FuncAttrs.addAttribute("no-builtins");
2272 return;
2273 }
2274
2275 // And last, add the rest of the builtin names.
2276 llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr);
2277}
2278
2280 const llvm::DataLayout &DL, const ABIArgInfo &AI,
2281 bool CheckCoerce = true) {
2282 llvm::Type *Ty = Types.ConvertTypeForMem(QTy);
2283 if (AI.getKind() == ABIArgInfo::Indirect ||
2285 return true;
2286 if (AI.getKind() == ABIArgInfo::Extend && !AI.isNoExt())
2287 return true;
2288 if (!DL.typeSizeEqualsStoreSize(Ty))
2289 // TODO: This will result in a modest amount of values not marked noundef
2290 // when they could be. We care about values that *invisibly* contain undef
2291 // bits from the perspective of LLVM IR.
2292 return false;
2293 if (CheckCoerce && AI.canHaveCoerceToType()) {
2294 llvm::Type *CoerceTy = AI.getCoerceToType();
2295 if (llvm::TypeSize::isKnownGT(DL.getTypeSizeInBits(CoerceTy),
2296 DL.getTypeSizeInBits(Ty)))
2297 // If we're coercing to a type with a greater size than the canonical one,
2298 // we're introducing new undef bits.
2299 // Coercing to a type of smaller or equal size is ok, as we know that
2300 // there's no internal padding (typeSizeEqualsStoreSize).
2301 return false;
2302 }
2303 if (QTy->isBitIntType())
2304 return true;
2305 if (QTy->isReferenceType())
2306 return true;
2307 if (QTy->isNullPtrType())
2308 return false;
2309 if (QTy->isMemberPointerType())
2310 // TODO: Some member pointers are `noundef`, but it depends on the ABI. For
2311 // now, never mark them.
2312 return false;
2313 if (QTy->isScalarType()) {
2314 if (const ComplexType *Complex = dyn_cast<ComplexType>(QTy))
2315 return DetermineNoUndef(Complex->getElementType(), Types, DL, AI, false);
2316 return true;
2317 }
2318 if (const VectorType *Vector = dyn_cast<VectorType>(QTy))
2319 return DetermineNoUndef(Vector->getElementType(), Types, DL, AI, false);
2320 if (const MatrixType *Matrix = dyn_cast<MatrixType>(QTy))
2321 return DetermineNoUndef(Matrix->getElementType(), Types, DL, AI, false);
2322 if (const ArrayType *Array = dyn_cast<ArrayType>(QTy))
2323 return DetermineNoUndef(Array->getElementType(), Types, DL, AI, false);
2324
2325 // TODO: Some structs may be `noundef`, in specific situations.
2326 return false;
2327}
2328
2329/// Check if the argument of a function has maybe_undef attribute.
2330static bool IsArgumentMaybeUndef(const Decl *TargetDecl,
2331 unsigned NumRequiredArgs, unsigned ArgNo) {
2332 const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
2333 if (!FD)
2334 return false;
2335
2336 // Assume variadic arguments do not have maybe_undef attribute.
2337 if (ArgNo >= NumRequiredArgs)
2338 return false;
2339
2340 // Check if argument has maybe_undef attribute.
2341 if (ArgNo < FD->getNumParams()) {
2342 const ParmVarDecl *Param = FD->getParamDecl(ArgNo);
2343 if (Param && Param->hasAttr<MaybeUndefAttr>())
2344 return true;
2345 }
2346
2347 return false;
2348}
2349
2350/// Test if it's legal to apply nofpclass for the given parameter type and it's
2351/// lowered IR type.
2352static bool canApplyNoFPClass(const ABIArgInfo &AI, QualType ParamType,
2353 bool IsReturn) {
2354 // Should only apply to FP types in the source, not ABI promoted.
2355 if (!ParamType->hasFloatingRepresentation())
2356 return false;
2357
2358 // The promoted-to IR type also needs to support nofpclass.
2359 llvm::Type *IRTy = AI.getCoerceToType();
2360 if (llvm::AttributeFuncs::isNoFPClassCompatibleType(IRTy))
2361 return true;
2362
2363 if (llvm::StructType *ST = dyn_cast<llvm::StructType>(IRTy)) {
2364 return !IsReturn && AI.getCanBeFlattened() &&
2365 llvm::all_of(ST->elements(),
2366 llvm::AttributeFuncs::isNoFPClassCompatibleType);
2367 }
2368
2369 return false;
2370}
2371
2372/// Return the nofpclass mask that can be applied to floating-point parameters.
2373static llvm::FPClassTest getNoFPClassTestMask(const LangOptions &LangOpts) {
2374 llvm::FPClassTest Mask = llvm::fcNone;
2375 if (LangOpts.NoHonorInfs)
2376 Mask |= llvm::fcInf;
2377 if (LangOpts.NoHonorNaNs)
2378 Mask |= llvm::fcNan;
2379 return Mask;
2380}
2381
2383 CGCalleeInfo CalleeInfo,
2384 llvm::AttributeList &Attrs) {
2385 if (Attrs.getMemoryEffects().getModRef() == llvm::ModRefInfo::NoModRef) {
2386 Attrs = Attrs.removeFnAttribute(getLLVMContext(), llvm::Attribute::Memory);
2387 llvm::Attribute MemoryAttr = llvm::Attribute::getWithMemoryEffects(
2388 getLLVMContext(), llvm::MemoryEffects::writeOnly());
2389 Attrs = Attrs.addFnAttribute(getLLVMContext(), MemoryAttr);
2390 }
2391}
2392
2393/// Construct the IR attribute list of a function or call.
2394///
2395/// When adding an attribute, please consider where it should be handled:
2396///
2397/// - getDefaultFunctionAttributes is for attributes that are essentially
2398/// part of the global target configuration (but perhaps can be
2399/// overridden on a per-function basis). Adding attributes there
2400/// will cause them to also be set in frontends that build on Clang's
2401/// target-configuration logic, as well as for code defined in library
2402/// modules such as CUDA's libdevice.
2403///
2404/// - ConstructAttributeList builds on top of getDefaultFunctionAttributes
2405/// and adds declaration-specific, convention-specific, and
2406/// frontend-specific logic. The last is of particular importance:
2407/// attributes that restrict how the frontend generates code must be
2408/// added here rather than getDefaultFunctionAttributes.
2409///
2411 const CGFunctionInfo &FI,
2412 CGCalleeInfo CalleeInfo,
2413 llvm::AttributeList &AttrList,
2414 unsigned &CallingConv,
2415 bool AttrOnCallSite, bool IsThunk) {
2416 llvm::AttrBuilder FuncAttrs(getLLVMContext());
2417 llvm::AttrBuilder RetAttrs(getLLVMContext());
2418
2419 // Collect function IR attributes from the CC lowering.
2420 // We'll collect the paramete and result attributes later.
2422 if (FI.isNoReturn())
2423 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2424 if (FI.isCmseNSCall())
2425 FuncAttrs.addAttribute("cmse_nonsecure_call");
2426
2427 // Collect function IR attributes from the callee prototype if we have one.
2429 CalleeInfo.getCalleeFunctionProtoType());
2430 const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
2431
2432 // Attach assumption attributes to the declaration. If this is a call
2433 // site, attach assumptions from the caller to the call as well.
2434 AddAttributesFromOMPAssumes(FuncAttrs, TargetDecl);
2435
2436 bool HasOptnone = false;
2437 // The NoBuiltinAttr attached to the target FunctionDecl.
2438 const NoBuiltinAttr *NBA = nullptr;
2439
2440 // Some ABIs may result in additional accesses to arguments that may
2441 // otherwise not be present.
2442 auto AddPotentialArgAccess = [&]() {
2443 llvm::Attribute A = FuncAttrs.getAttribute(llvm::Attribute::Memory);
2444 if (A.isValid())
2445 FuncAttrs.addMemoryAttr(A.getMemoryEffects() |
2446 llvm::MemoryEffects::argMemOnly());
2447 };
2448
2449 // Collect function IR attributes based on declaration-specific
2450 // information.
2451 // FIXME: handle sseregparm someday...
2452 if (TargetDecl) {
2453 if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
2454 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
2455 if (TargetDecl->hasAttr<NoThrowAttr>())
2456 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2457 if (TargetDecl->hasAttr<NoReturnAttr>())
2458 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2459 if (TargetDecl->hasAttr<ColdAttr>())
2460 FuncAttrs.addAttribute(llvm::Attribute::Cold);
2461 if (TargetDecl->hasAttr<HotAttr>())
2462 FuncAttrs.addAttribute(llvm::Attribute::Hot);
2463 if (TargetDecl->hasAttr<NoDuplicateAttr>())
2464 FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
2465 if (TargetDecl->hasAttr<ConvergentAttr>())
2466 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
2467
2468 if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
2470 getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
2471 if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
2472 // A sane operator new returns a non-aliasing pointer.
2473 auto Kind = Fn->getDeclName().getCXXOverloadedOperator();
2474 if (getCodeGenOpts().AssumeSaneOperatorNew &&
2475 (Kind == OO_New || Kind == OO_Array_New))
2476 RetAttrs.addAttribute(llvm::Attribute::NoAlias);
2477 }
2478 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
2479 const bool IsVirtualCall = MD && MD->isVirtual();
2480 // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a
2481 // virtual function. These attributes are not inherited by overloads.
2482 if (!(AttrOnCallSite && IsVirtualCall)) {
2483 if (Fn->isNoReturn())
2484 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
2485 NBA = Fn->getAttr<NoBuiltinAttr>();
2486 }
2487 }
2488
2489 if (isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl)) {
2490 // Only place nomerge attribute on call sites, never functions. This
2491 // allows it to work on indirect virtual function calls.
2492 if (AttrOnCallSite && TargetDecl->hasAttr<NoMergeAttr>())
2493 FuncAttrs.addAttribute(llvm::Attribute::NoMerge);
2494 }
2495
2496 // 'const', 'pure' and 'noalias' attributed functions are also nounwind.
2497 if (TargetDecl->hasAttr<ConstAttr>()) {
2498 FuncAttrs.addMemoryAttr(llvm::MemoryEffects::none());
2499 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2500 // gcc specifies that 'const' functions have greater restrictions than
2501 // 'pure' functions, so they also cannot have infinite loops.
2502 FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
2503 } else if (TargetDecl->hasAttr<PureAttr>()) {
2504 FuncAttrs.addMemoryAttr(llvm::MemoryEffects::readOnly());
2505 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2506 // gcc specifies that 'pure' functions cannot have infinite loops.
2507 FuncAttrs.addAttribute(llvm::Attribute::WillReturn);
2508 } else if (TargetDecl->hasAttr<NoAliasAttr>()) {
2509 FuncAttrs.addMemoryAttr(llvm::MemoryEffects::inaccessibleOrArgMemOnly());
2510 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
2511 }
2512 if (const auto *RA = TargetDecl->getAttr<RestrictAttr>();
2513 RA && RA->getDeallocator() == nullptr)
2514 RetAttrs.addAttribute(llvm::Attribute::NoAlias);
2515 if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&
2516 !CodeGenOpts.NullPointerIsValid)
2517 RetAttrs.addAttribute(llvm::Attribute::NonNull);
2518 if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
2519 FuncAttrs.addAttribute("no_caller_saved_registers");
2520 if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())
2521 FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);
2522 if (TargetDecl->hasAttr<LeafAttr>())
2523 FuncAttrs.addAttribute(llvm::Attribute::NoCallback);
2524 if (TargetDecl->hasAttr<BPFFastCallAttr>())
2525 FuncAttrs.addAttribute("bpf_fastcall");
2526
2527 HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();
2528 if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
2529 std::optional<unsigned> NumElemsParam;
2530 if (AllocSize->getNumElemsParam().isValid())
2531 NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();
2532 FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),
2533 NumElemsParam);
2534 }
2535
2536 if (DeviceKernelAttr::isOpenCLSpelling(
2537 TargetDecl->getAttr<DeviceKernelAttr>()) &&
2540 // Check CallingConv to avoid adding uniform-work-group-size attribute to
2541 // OpenCL Kernel Stub
2542 if (getLangOpts().OpenCLVersion <= 120) {
2543 // OpenCL v1.2 Work groups are always uniform
2544 FuncAttrs.addAttribute("uniform-work-group-size", "true");
2545 } else {
2546 // OpenCL v2.0 Work groups may be whether uniform or not.
2547 // '-cl-uniform-work-group-size' compile option gets a hint
2548 // to the compiler that the global work-size be a multiple of
2549 // the work-group size specified to clEnqueueNDRangeKernel
2550 // (i.e. work groups are uniform).
2551 FuncAttrs.addAttribute(
2552 "uniform-work-group-size",
2553 llvm::toStringRef(getLangOpts().OffloadUniformBlock));
2554 }
2555 }
2556
2557 if (TargetDecl->hasAttr<CUDAGlobalAttr>() &&
2558 getLangOpts().OffloadUniformBlock)
2559 FuncAttrs.addAttribute("uniform-work-group-size", "true");
2560
2561 if (TargetDecl->hasAttr<ArmLocallyStreamingAttr>())
2562 FuncAttrs.addAttribute("aarch64_pstate_sm_body");
2563 }
2564
2565 // Attach "no-builtins" attributes to:
2566 // * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".
2567 // * definitions: "no-builtins" or "no-builtin-<name>" only.
2568 // The attributes can come from:
2569 // * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>
2570 // * FunctionDecl attributes: __attribute__((no_builtin(...)))
2571 addNoBuiltinAttributes(FuncAttrs, getLangOpts(), NBA);
2572
2573 // Collect function IR attributes based on global settiings.
2574 getDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
2575
2576 // Override some default IR attributes based on declaration-specific
2577 // information.
2578 if (TargetDecl) {
2579 if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>())
2580 FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening);
2581 if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())
2582 FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
2583 if (TargetDecl->hasAttr<NoSplitStackAttr>())
2584 FuncAttrs.removeAttribute("split-stack");
2585 if (TargetDecl->hasAttr<ZeroCallUsedRegsAttr>()) {
2586 // A function "__attribute__((...))" overrides the command-line flag.
2587 auto Kind =
2588 TargetDecl->getAttr<ZeroCallUsedRegsAttr>()->getZeroCallUsedRegs();
2589 FuncAttrs.removeAttribute("zero-call-used-regs");
2590 FuncAttrs.addAttribute(
2591 "zero-call-used-regs",
2592 ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind));
2593 }
2594
2595 // Add NonLazyBind attribute to function declarations when -fno-plt
2596 // is used.
2597 // FIXME: what if we just haven't processed the function definition
2598 // yet, or if it's an external definition like C99 inline?
2599 if (CodeGenOpts.NoPLT) {
2600 if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
2601 if (!Fn->isDefined() && !AttrOnCallSite) {
2602 FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);
2603 }
2604 }
2605 }
2606 // Remove 'convergent' if requested.
2607 if (TargetDecl->hasAttr<NoConvergentAttr>())
2608 FuncAttrs.removeAttribute(llvm::Attribute::Convergent);
2609 }
2610
2611 // Add "sample-profile-suffix-elision-policy" attribute for internal linkage
2612 // functions with -funique-internal-linkage-names.
2613 if (TargetDecl && CodeGenOpts.UniqueInternalLinkageNames) {
2614 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
2615 if (!FD->isExternallyVisible())
2616 FuncAttrs.addAttribute("sample-profile-suffix-elision-policy",
2617 "selected");
2618 }
2619 }
2620
2621 // Collect non-call-site function IR attributes from declaration-specific
2622 // information.
2623 if (!AttrOnCallSite) {
2624 if (TargetDecl && TargetDecl->hasAttr<CmseNSEntryAttr>())
2625 FuncAttrs.addAttribute("cmse_nonsecure_entry");
2626
2627 // Whether tail calls are enabled.
2628 auto shouldDisableTailCalls = [&] {
2629 // Should this be honored in getDefaultFunctionAttributes?
2630 if (CodeGenOpts.DisableTailCalls)
2631 return true;
2632
2633 if (!TargetDecl)
2634 return false;
2635
2636 if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
2637 TargetDecl->hasAttr<AnyX86InterruptAttr>())
2638 return true;
2639
2640 if (CodeGenOpts.NoEscapingBlockTailCalls) {
2641 if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))
2642 if (!BD->doesNotEscape())
2643 return true;
2644 }
2645
2646 return false;
2647 };
2648 if (shouldDisableTailCalls())
2649 FuncAttrs.addAttribute("disable-tail-calls", "true");
2650
2651 // These functions require the returns_twice attribute for correct codegen,
2652 // but the attribute may not be added if -fno-builtin is specified. We
2653 // explicitly add that attribute here.
2654 static const llvm::StringSet<> ReturnsTwiceFn{
2655 "_setjmpex", "setjmp", "_setjmp", "vfork",
2656 "sigsetjmp", "__sigsetjmp", "savectx", "getcontext"};
2657 if (ReturnsTwiceFn.contains(Name))
2658 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
2659
2660 // CPU/feature overrides. addDefaultFunctionDefinitionAttributes
2661 // handles these separately to set them based on the global defaults.
2662 GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
2663
2664 // Windows hotpatching support
2665 if (!MSHotPatchFunctions.empty()) {
2666 bool IsHotPatched = llvm::binary_search(MSHotPatchFunctions, Name);
2667 if (IsHotPatched)
2668 FuncAttrs.addAttribute("marked_for_windows_hot_patching");
2669 }
2670 }
2671
2672 // Mark functions that are replaceable by the loader.
2673 if (CodeGenOpts.isLoaderReplaceableFunctionName(Name))
2674 FuncAttrs.addAttribute("loader-replaceable");
2675
2676 // Collect attributes from arguments and return values.
2677 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
2678
2679 QualType RetTy = FI.getReturnType();
2680 const ABIArgInfo &RetAI = FI.getReturnInfo();
2681 const llvm::DataLayout &DL = getDataLayout();
2682
2683 // Determine if the return type could be partially undef
2684 if (CodeGenOpts.EnableNoundefAttrs &&
2685 HasStrictReturn(*this, RetTy, TargetDecl)) {
2686 if (!RetTy->isVoidType() && RetAI.getKind() != ABIArgInfo::Indirect &&
2687 DetermineNoUndef(RetTy, getTypes(), DL, RetAI))
2688 RetAttrs.addAttribute(llvm::Attribute::NoUndef);
2689 }
2690
2691 switch (RetAI.getKind()) {
2692 case ABIArgInfo::Extend:
2693 if (RetAI.isSignExt())
2694 RetAttrs.addAttribute(llvm::Attribute::SExt);
2695 else if (RetAI.isZeroExt())
2696 RetAttrs.addAttribute(llvm::Attribute::ZExt);
2697 else
2698 RetAttrs.addAttribute(llvm::Attribute::NoExt);
2699 [[fallthrough]];
2701 case ABIArgInfo::Direct:
2702 if (RetAI.getInReg())
2703 RetAttrs.addAttribute(llvm::Attribute::InReg);
2704
2705 if (canApplyNoFPClass(RetAI, RetTy, true))
2706 RetAttrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2707
2708 break;
2709 case ABIArgInfo::Ignore:
2710 break;
2711
2713 case ABIArgInfo::Indirect: {
2714 // inalloca and sret disable readnone and readonly
2715 AddPotentialArgAccess();
2716 break;
2717 }
2718
2720 break;
2721
2722 case ABIArgInfo::Expand:
2724 llvm_unreachable("Invalid ABI kind for return argument");
2725 }
2726
2727 if (!IsThunk) {
2728 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2729 if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {
2730 QualType PTy = RefTy->getPointeeType();
2731 if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
2732 RetAttrs.addDereferenceableAttr(
2733 getMinimumObjectSize(PTy).getQuantity());
2734 if (getTypes().getTargetAddressSpace(PTy) == 0 &&
2735 !CodeGenOpts.NullPointerIsValid)
2736 RetAttrs.addAttribute(llvm::Attribute::NonNull);
2737 if (PTy->isObjectType()) {
2738 llvm::Align Alignment =
2740 RetAttrs.addAlignmentAttr(Alignment);
2741 }
2742 }
2743 }
2744
2745 bool hasUsedSRet = false;
2746 SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());
2747
2748 // Attach attributes to sret.
2749 if (IRFunctionArgs.hasSRetArg()) {
2750 llvm::AttrBuilder SRETAttrs(getLLVMContext());
2751 SRETAttrs.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy));
2752 SRETAttrs.addAttribute(llvm::Attribute::Writable);
2753 SRETAttrs.addAttribute(llvm::Attribute::DeadOnUnwind);
2754 hasUsedSRet = true;
2755 if (RetAI.getInReg())
2756 SRETAttrs.addAttribute(llvm::Attribute::InReg);
2757 SRETAttrs.addAlignmentAttr(RetAI.getIndirectAlign().getQuantity());
2758 ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
2759 llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
2760 }
2761
2762 // Attach attributes to inalloca argument.
2763 if (IRFunctionArgs.hasInallocaArg()) {
2764 llvm::AttrBuilder Attrs(getLLVMContext());
2765 Attrs.addInAllocaAttr(FI.getArgStruct());
2766 ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
2767 llvm::AttributeSet::get(getLLVMContext(), Attrs);
2768 }
2769
2770 // Apply `nonnull`, `dereferenceable(N)` and `align N` to the `this` argument,
2771 // unless this is a thunk function.
2772 // FIXME: fix this properly, https://reviews.llvm.org/D100388
2773 if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&
2774 !FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {
2775 auto IRArgs = IRFunctionArgs.getIRArgs(0);
2776
2777 assert(IRArgs.second == 1 && "Expected only a single `this` pointer.");
2778
2779 llvm::AttrBuilder Attrs(getLLVMContext());
2780
2781 QualType ThisTy = FI.arg_begin()->type.getTypePtr()->getPointeeType();
2782
2783 if (!CodeGenOpts.NullPointerIsValid &&
2784 getTypes().getTargetAddressSpace(FI.arg_begin()->type) == 0) {
2785 Attrs.addAttribute(llvm::Attribute::NonNull);
2786 Attrs.addDereferenceableAttr(getMinimumObjectSize(ThisTy).getQuantity());
2787 } else {
2788 // FIXME dereferenceable should be correct here, regardless of
2789 // NullPointerIsValid. However, dereferenceable currently does not always
2790 // respect NullPointerIsValid and may imply nonnull and break the program.
2791 // See https://reviews.llvm.org/D66618 for discussions.
2792 Attrs.addDereferenceableOrNullAttr(
2795 .getQuantity());
2796 }
2797
2798 llvm::Align Alignment =
2799 getNaturalTypeAlignment(ThisTy, /*BaseInfo=*/nullptr,
2800 /*TBAAInfo=*/nullptr, /*forPointeeType=*/true)
2801 .getAsAlign();
2802 Attrs.addAlignmentAttr(Alignment);
2803
2804 ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);
2805 }
2806
2807 unsigned ArgNo = 0;
2809 I != E; ++I, ++ArgNo) {
2810 QualType ParamType = I->type;
2811 const ABIArgInfo &AI = I->info;
2812 llvm::AttrBuilder Attrs(getLLVMContext());
2813
2814 // Add attribute for padding argument, if necessary.
2815 if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
2816 if (AI.getPaddingInReg()) {
2817 ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
2818 llvm::AttributeSet::get(getLLVMContext(),
2819 llvm::AttrBuilder(getLLVMContext())
2820 .addAttribute(llvm::Attribute::InReg));
2821 }
2822 }
2823
2824 // Decide whether the argument we're handling could be partially undef
2825 if (CodeGenOpts.EnableNoundefAttrs &&
2826 DetermineNoUndef(ParamType, getTypes(), DL, AI)) {
2827 Attrs.addAttribute(llvm::Attribute::NoUndef);
2828 }
2829
2830 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
2831 // have the corresponding parameter variable. It doesn't make
2832 // sense to do it here because parameters are so messed up.
2833 switch (AI.getKind()) {
2834 case ABIArgInfo::Extend:
2835 if (AI.isSignExt())
2836 Attrs.addAttribute(llvm::Attribute::SExt);
2837 else if (AI.isZeroExt())
2838 Attrs.addAttribute(llvm::Attribute::ZExt);
2839 else
2840 Attrs.addAttribute(llvm::Attribute::NoExt);
2841 [[fallthrough]];
2843 case ABIArgInfo::Direct:
2844 if (ArgNo == 0 && FI.isChainCall())
2845 Attrs.addAttribute(llvm::Attribute::Nest);
2846 else if (AI.getInReg())
2847 Attrs.addAttribute(llvm::Attribute::InReg);
2848 Attrs.addStackAlignmentAttr(llvm::MaybeAlign(AI.getDirectAlign()));
2849
2850 if (canApplyNoFPClass(AI, ParamType, false))
2851 Attrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));
2852 break;
2853 case ABIArgInfo::Indirect: {
2854 if (AI.getInReg())
2855 Attrs.addAttribute(llvm::Attribute::InReg);
2856
2857 // HLSL out and inout parameters must not be marked with ByVal or
2858 // DeadOnReturn attributes because stores to these parameters by the
2859 // callee are visible to the caller.
2860 if (auto ParamABI = FI.getExtParameterInfo(ArgNo).getABI();
2861 ParamABI != ParameterABI::HLSLOut &&
2862 ParamABI != ParameterABI::HLSLInOut) {
2863
2864 // Depending on the ABI, this may be either a byval or a dead_on_return
2865 // argument.
2866 if (AI.getIndirectByVal()) {
2867 Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));
2868 } else {
2869 // Add dead_on_return when the object's lifetime ends in the callee.
2870 // This includes trivially-destructible objects, as well as objects
2871 // whose destruction / clean-up is carried out within the callee
2872 // (e.g., Obj-C ARC-managed structs, MSVC callee-destroyed objects).
2873 if (!ParamType.isDestructedType() || !ParamType->isRecordType() ||
2875 Attrs.addAttribute(llvm::Attribute::DeadOnReturn);
2876 }
2877 }
2878
2879 auto *Decl = ParamType->getAsRecordDecl();
2880 if (CodeGenOpts.PassByValueIsNoAlias && Decl &&
2881 Decl->getArgPassingRestrictions() ==
2883 // When calling the function, the pointer passed in will be the only
2884 // reference to the underlying object. Mark it accordingly.
2885 Attrs.addAttribute(llvm::Attribute::NoAlias);
2886
2887 // TODO: We could add the byref attribute if not byval, but it would
2888 // require updating many testcases.
2889
2890 CharUnits Align = AI.getIndirectAlign();
2891
2892 // In a byval argument, it is important that the required
2893 // alignment of the type is honored, as LLVM might be creating a
2894 // *new* stack object, and needs to know what alignment to give
2895 // it. (Sometimes it can deduce a sensible alignment on its own,
2896 // but not if clang decides it must emit a packed struct, or the
2897 // user specifies increased alignment requirements.)
2898 //
2899 // This is different from indirect *not* byval, where the object
2900 // exists already, and the align attribute is purely
2901 // informative.
2902 assert(!Align.isZero());
2903
2904 // For now, only add this when we have a byval argument.
2905 // TODO: be less lazy about updating test cases.
2906 if (AI.getIndirectByVal())
2907 Attrs.addAlignmentAttr(Align.getQuantity());
2908
2909 // byval disables readnone and readonly.
2910 AddPotentialArgAccess();
2911 break;
2912 }
2914 CharUnits Align = AI.getIndirectAlign();
2915 Attrs.addByRefAttr(getTypes().ConvertTypeForMem(ParamType));
2916 Attrs.addAlignmentAttr(Align.getQuantity());
2917 break;
2918 }
2919 case ABIArgInfo::Ignore:
2920 case ABIArgInfo::Expand:
2922 break;
2923
2925 // inalloca disables readnone and readonly.
2926 AddPotentialArgAccess();
2927 continue;
2928 }
2929
2930 if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {
2931 QualType PTy = RefTy->getPointeeType();
2932 if (!PTy->isIncompleteType() && PTy->isConstantSizeType())
2933 Attrs.addDereferenceableAttr(getMinimumObjectSize(PTy).getQuantity());
2934 if (getTypes().getTargetAddressSpace(PTy) == 0 &&
2935 !CodeGenOpts.NullPointerIsValid)
2936 Attrs.addAttribute(llvm::Attribute::NonNull);
2937 if (PTy->isObjectType()) {
2938 llvm::Align Alignment =
2940 Attrs.addAlignmentAttr(Alignment);
2941 }
2942 }
2943
2944 // From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:
2945 // > For arguments to a __kernel function declared to be a pointer to a
2946 // > data type, the OpenCL compiler can assume that the pointee is always
2947 // > appropriately aligned as required by the data type.
2948 if (TargetDecl &&
2949 DeviceKernelAttr::isOpenCLSpelling(
2950 TargetDecl->getAttr<DeviceKernelAttr>()) &&
2951 ParamType->isPointerType()) {
2952 QualType PTy = ParamType->getPointeeType();
2953 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2954 llvm::Align Alignment =
2956 Attrs.addAlignmentAttr(Alignment);
2957 }
2958 }
2959
2960 switch (FI.getExtParameterInfo(ArgNo).getABI()) {
2963 Attrs.addAttribute(llvm::Attribute::NoAlias);
2964 break;
2966 break;
2967
2969 // Add 'sret' if we haven't already used it for something, but
2970 // only if the result is void.
2971 if (!hasUsedSRet && RetTy->isVoidType()) {
2972 Attrs.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType));
2973 hasUsedSRet = true;
2974 }
2975
2976 // Add 'noalias' in either case.
2977 Attrs.addAttribute(llvm::Attribute::NoAlias);
2978
2979 // Add 'dereferenceable' and 'alignment'.
2980 auto PTy = ParamType->getPointeeType();
2981 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2982 auto info = getContext().getTypeInfoInChars(PTy);
2983 Attrs.addDereferenceableAttr(info.Width.getQuantity());
2984 Attrs.addAlignmentAttr(info.Align.getAsAlign());
2985 }
2986 break;
2987 }
2988
2990 Attrs.addAttribute(llvm::Attribute::SwiftError);
2991 break;
2992
2994 Attrs.addAttribute(llvm::Attribute::SwiftSelf);
2995 break;
2996
2998 Attrs.addAttribute(llvm::Attribute::SwiftAsync);
2999 break;
3000 }
3001
3002 if (FI.getExtParameterInfo(ArgNo).isNoEscape())
3003 Attrs.addCapturesAttr(llvm::CaptureInfo::none());
3004
3005 if (Attrs.hasAttributes()) {
3006 unsigned FirstIRArg, NumIRArgs;
3007 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
3008 for (unsigned i = 0; i < NumIRArgs; i++)
3009 ArgAttrs[FirstIRArg + i] = ArgAttrs[FirstIRArg + i].addAttributes(
3010 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs));
3011 }
3012 }
3013 assert(ArgNo == FI.arg_size());
3014
3015 AttrList = llvm::AttributeList::get(
3016 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
3017 llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
3018}
3019
3020/// An argument came in as a promoted argument; demote it back to its
3021/// declared type.
3022static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
3023 const VarDecl *var,
3024 llvm::Value *value) {
3025 llvm::Type *varType = CGF.ConvertType(var->getType());
3026
3027 // This can happen with promotions that actually don't change the
3028 // underlying type, like the enum promotions.
3029 if (value->getType() == varType)
3030 return value;
3031
3032 assert((varType->isIntegerTy() || varType->isFloatingPointTy()) &&
3033 "unexpected promotion type");
3034
3035 if (isa<llvm::IntegerType>(varType))
3036 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
3037
3038 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
3039}
3040
3041/// Returns the attribute (either parameter attribute, or function
3042/// attribute), which declares argument ArgNo to be non-null.
3043static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,
3044 QualType ArgType, unsigned ArgNo) {
3045 // FIXME: __attribute__((nonnull)) can also be applied to:
3046 // - references to pointers, where the pointee is known to be
3047 // nonnull (apparently a Clang extension)
3048 // - transparent unions containing pointers
3049 // In the former case, LLVM IR cannot represent the constraint. In
3050 // the latter case, we have no guarantee that the transparent union
3051 // is in fact passed as a pointer.
3052 if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())
3053 return nullptr;
3054 // First, check attribute on parameter itself.
3055 if (PVD) {
3056 if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())
3057 return ParmNNAttr;
3058 }
3059 // Check function attributes.
3060 if (!FD)
3061 return nullptr;
3062 for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {
3063 if (NNAttr->isNonNull(ArgNo))
3064 return NNAttr;
3065 }
3066 return nullptr;
3067}
3068
3069namespace {
3070struct CopyBackSwiftError final : EHScopeStack::Cleanup {
3071 Address Temp;
3072 Address Arg;
3073 CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}
3074 void Emit(CodeGenFunction &CGF, Flags flags) override {
3075 llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);
3076 CGF.Builder.CreateStore(errorValue, Arg);
3077 }
3078};
3079} // namespace
3080
3082 llvm::Function *Fn,
3083 const FunctionArgList &Args) {
3084 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
3085 // Naked functions don't have prologues.
3086 return;
3087
3088 // If this is an implicit-return-zero function, go ahead and
3089 // initialize the return value. TODO: it might be nice to have
3090 // a more general mechanism for this that didn't require synthesized
3091 // return statements.
3092 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
3093 if (FD->hasImplicitReturnZero()) {
3094 QualType RetTy = FD->getReturnType().getUnqualifiedType();
3095 llvm::Type *LLVMTy = CGM.getTypes().ConvertType(RetTy);
3096 llvm::Constant *Zero = llvm::Constant::getNullValue(LLVMTy);
3098 }
3099 }
3100
3101 // FIXME: We no longer need the types from FunctionArgList; lift up and
3102 // simplify.
3103
3104 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
3105 assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());
3106
3107 // If we're using inalloca, all the memory arguments are GEPs off of the last
3108 // parameter, which is a pointer to the complete memory area.
3109 Address ArgStruct = Address::invalid();
3110 if (IRFunctionArgs.hasInallocaArg())
3111 ArgStruct = Address(Fn->getArg(IRFunctionArgs.getInallocaArgNo()),
3113
3114 // Name the struct return parameter.
3115 if (IRFunctionArgs.hasSRetArg()) {
3116 auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());
3117 AI->setName("agg.result");
3118 AI->addAttr(llvm::Attribute::NoAlias);
3119 }
3120
3121 // Track if we received the parameter as a pointer (indirect, byval, or
3122 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it
3123 // into a local alloca for us.
3125 ArgVals.reserve(Args.size());
3126
3127 // Create a pointer value for every parameter declaration. This usually
3128 // entails copying one or more LLVM IR arguments into an alloca. Don't push
3129 // any cleanups or do anything that might unwind. We do that separately, so
3130 // we can push the cleanups in the correct order for the ABI.
3131 assert(FI.arg_size() == Args.size() &&
3132 "Mismatch between function signature & arguments.");
3133 unsigned ArgNo = 0;
3135 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); i != e;
3136 ++i, ++info_it, ++ArgNo) {
3137 const VarDecl *Arg = *i;
3138 const ABIArgInfo &ArgI = info_it->info;
3139
3140 bool isPromoted =
3141 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
3142 // We are converting from ABIArgInfo type to VarDecl type directly, unless
3143 // the parameter is promoted. In this case we convert to
3144 // CGFunctionInfo::ArgInfo type with subsequent argument demotion.
3145 QualType Ty = isPromoted ? info_it->type : Arg->getType();
3146 assert(hasScalarEvaluationKind(Ty) ==
3148
3149 unsigned FirstIRArg, NumIRArgs;
3150 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
3151
3152 switch (ArgI.getKind()) {
3153 case ABIArgInfo::InAlloca: {
3154 assert(NumIRArgs == 0);
3155 auto FieldIndex = ArgI.getInAllocaFieldIndex();
3156 Address V =
3157 Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName());
3158 if (ArgI.getInAllocaIndirect())
3160 getContext().getTypeAlignInChars(Ty));
3161 ArgVals.push_back(ParamValue::forIndirect(V));
3162 break;
3163 }
3164
3167 assert(NumIRArgs == 1);
3169 Fn->getArg(FirstIRArg), Ty, ArgI.getIndirectAlign(), false, nullptr,
3170 nullptr, KnownNonNull);
3171
3172 if (!hasScalarEvaluationKind(Ty)) {
3173 // Aggregates and complex variables are accessed by reference. All we
3174 // need to do is realign the value, if requested. Also, if the address
3175 // may be aliased, copy it to ensure that the parameter variable is
3176 // mutable and has a unique adress, as C requires.
3177 if (ArgI.getIndirectRealign() || ArgI.isIndirectAliased()) {
3178 RawAddress AlignedTemp = CreateMemTemp(Ty, "coerce");
3179
3180 // Copy from the incoming argument pointer to the temporary with the
3181 // appropriate alignment.
3182 //
3183 // FIXME: We should have a common utility for generating an aggregate
3184 // copy.
3187 AlignedTemp.getPointer(), AlignedTemp.getAlignment().getAsAlign(),
3188 ParamAddr.emitRawPointer(*this),
3189 ParamAddr.getAlignment().getAsAlign(),
3190 llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()));
3191 ParamAddr = AlignedTemp;
3192 }
3193 ArgVals.push_back(ParamValue::forIndirect(ParamAddr));
3194 } else {
3195 // Load scalar value from indirect argument.
3196 llvm::Value *V =
3197 EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());
3198
3199 if (isPromoted)
3200 V = emitArgumentDemotion(*this, Arg, V);
3201 ArgVals.push_back(ParamValue::forDirect(V));
3202 }
3203 break;
3204 }
3205
3206 case ABIArgInfo::Extend:
3207 case ABIArgInfo::Direct: {
3208 auto AI = Fn->getArg(FirstIRArg);
3209 llvm::Type *LTy = ConvertType(Arg->getType());
3210
3211 // Prepare parameter attributes. So far, only attributes for pointer
3212 // parameters are prepared. See
3213 // http://llvm.org/docs/LangRef.html#paramattrs.
3214 if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&
3215 ArgI.getCoerceToType()->isPointerTy()) {
3216 assert(NumIRArgs == 1);
3217
3218 if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
3219 // Set `nonnull` attribute if any.
3220 if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
3221 PVD->getFunctionScopeIndex()) &&
3222 !CGM.getCodeGenOpts().NullPointerIsValid)
3223 AI->addAttr(llvm::Attribute::NonNull);
3224
3225 QualType OTy = PVD->getOriginalType();
3226 if (const auto *ArrTy = getContext().getAsConstantArrayType(OTy)) {
3227 // A C99 array parameter declaration with the static keyword also
3228 // indicates dereferenceability, and if the size is constant we can
3229 // use the dereferenceable attribute (which requires the size in
3230 // bytes).
3231 if (ArrTy->getSizeModifier() == ArraySizeModifier::Static) {
3232 QualType ETy = ArrTy->getElementType();
3233 llvm::Align Alignment =
3235 AI->addAttrs(llvm::AttrBuilder(getLLVMContext())
3236 .addAlignmentAttr(Alignment));
3237 uint64_t ArrSize = ArrTy->getZExtSize();
3238 if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
3239 ArrSize) {
3240 llvm::AttrBuilder Attrs(getLLVMContext());
3241 Attrs.addDereferenceableAttr(
3242 getContext().getTypeSizeInChars(ETy).getQuantity() *
3243 ArrSize);
3244 AI->addAttrs(Attrs);
3245 } else if (getContext().getTargetInfo().getNullPointerValue(
3246 ETy.getAddressSpace()) == 0 &&
3247 !CGM.getCodeGenOpts().NullPointerIsValid) {
3248 AI->addAttr(llvm::Attribute::NonNull);
3249 }
3250 }
3251 } else if (const auto *ArrTy =
3252 getContext().getAsVariableArrayType(OTy)) {
3253 // For C99 VLAs with the static keyword, we don't know the size so
3254 // we can't use the dereferenceable attribute, but in addrspace(0)
3255 // we know that it must be nonnull.
3256 if (ArrTy->getSizeModifier() == ArraySizeModifier::Static) {
3257 QualType ETy = ArrTy->getElementType();
3258 llvm::Align Alignment =
3260 AI->addAttrs(llvm::AttrBuilder(getLLVMContext())
3261 .addAlignmentAttr(Alignment));
3262 if (!getTypes().getTargetAddressSpace(ETy) &&
3263 !CGM.getCodeGenOpts().NullPointerIsValid)
3264 AI->addAttr(llvm::Attribute::NonNull);
3265 }
3266 }
3267
3268 // Set `align` attribute if any.
3269 const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
3270 if (!AVAttr)
3271 if (const auto *TOTy = OTy->getAs<TypedefType>())
3272 AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
3273 if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) {
3274 // If alignment-assumption sanitizer is enabled, we do *not* add
3275 // alignment attribute here, but emit normal alignment assumption,
3276 // so the UBSAN check could function.
3277 llvm::ConstantInt *AlignmentCI =
3278 cast<llvm::ConstantInt>(EmitScalarExpr(AVAttr->getAlignment()));
3279 uint64_t AlignmentInt =
3280 AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment);
3281 if (AI->getParamAlign().valueOrOne() < AlignmentInt) {
3282 AI->removeAttr(llvm::Attribute::AttrKind::Alignment);
3283 AI->addAttrs(llvm::AttrBuilder(getLLVMContext())
3284 .addAlignmentAttr(llvm::Align(AlignmentInt)));
3285 }
3286 }
3287 }
3288
3289 // Set 'noalias' if an argument type has the `restrict` qualifier.
3290 if (Arg->getType().isRestrictQualified())
3291 AI->addAttr(llvm::Attribute::NoAlias);
3292 }
3293
3294 // Prepare the argument value. If we have the trivial case, handle it
3295 // with no muss and fuss.
3296 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
3297 ArgI.getCoerceToType() == ConvertType(Ty) &&
3298 ArgI.getDirectOffset() == 0) {
3299 assert(NumIRArgs == 1);
3300
3301 // LLVM expects swifterror parameters to be used in very restricted
3302 // ways. Copy the value into a less-restricted temporary.
3303 llvm::Value *V = AI;
3304 if (FI.getExtParameterInfo(ArgNo).getABI() ==
3306 QualType pointeeTy = Ty->getPointeeType();
3307 assert(pointeeTy->isPointerType());
3308 RawAddress temp =
3309 CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
3311 V, pointeeTy, getContext().getTypeAlignInChars(pointeeTy));
3312 llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);
3313 Builder.CreateStore(incomingErrorValue, temp);
3314 V = temp.getPointer();
3315
3316 // Push a cleanup to copy the value back at the end of the function.
3317 // The convention does not guarantee that the value will be written
3318 // back if the function exits with an unwind exception.
3319 EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);
3320 }
3321
3322 // Ensure the argument is the correct type.
3323 if (V->getType() != ArgI.getCoerceToType())
3324 V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
3325
3326 if (isPromoted)
3327 V = emitArgumentDemotion(*this, Arg, V);
3328
3329 // Because of merging of function types from multiple decls it is
3330 // possible for the type of an argument to not match the corresponding
3331 // type in the function type. Since we are codegening the callee
3332 // in here, add a cast to the argument type.
3333 llvm::Type *LTy = ConvertType(Arg->getType());
3334 if (V->getType() != LTy)
3335 V = Builder.CreateBitCast(V, LTy);
3336
3337 ArgVals.push_back(ParamValue::forDirect(V));
3338 break;
3339 }
3340
3341 // VLST arguments are coerced to VLATs at the function boundary for
3342 // ABI consistency. If this is a VLST that was coerced to
3343 // a VLAT at the function boundary and the types match up, use
3344 // llvm.vector.extract to convert back to the original VLST.
3345 if (auto *VecTyTo = dyn_cast<llvm::FixedVectorType>(ConvertType(Ty))) {
3346 llvm::Value *ArgVal = Fn->getArg(FirstIRArg);
3347 if (auto *VecTyFrom =
3348 dyn_cast<llvm::ScalableVectorType>(ArgVal->getType())) {
3349 auto [Coerced, Extracted] = CoerceScalableToFixed(
3350 *this, VecTyTo, VecTyFrom, ArgVal, Arg->getName());
3351 if (Extracted) {
3352 assert(NumIRArgs == 1);
3353 ArgVals.push_back(ParamValue::forDirect(Coerced));
3354 break;
3355 }
3356 }
3357 }
3358
3359 llvm::StructType *STy =
3360 dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
3361 Address Alloca =
3362 CreateMemTemp(Ty, getContext().getDeclAlign(Arg), Arg->getName());
3363
3364 // Pointer to store into.
3365 Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
3366
3367 // Fast-isel and the optimizer generally like scalar values better than
3368 // FCAs, so we flatten them if this is safe to do for this argument.
3369 if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
3370 STy->getNumElements() > 1) {
3371 llvm::TypeSize StructSize = CGM.getDataLayout().getTypeAllocSize(STy);
3372 llvm::TypeSize PtrElementSize =
3373 CGM.getDataLayout().getTypeAllocSize(Ptr.getElementType());
3374 if (StructSize.isScalable()) {
3375 assert(STy->containsHomogeneousScalableVectorTypes() &&
3376 "ABI only supports structure with homogeneous scalable vector "
3377 "type");
3378 assert(StructSize == PtrElementSize &&
3379 "Only allow non-fractional movement of structure with"
3380 "homogeneous scalable vector type");
3381 assert(STy->getNumElements() == NumIRArgs);
3382
3383 llvm::Value *LoadedStructValue = llvm::PoisonValue::get(STy);
3384 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3385 auto *AI = Fn->getArg(FirstIRArg + i);
3386 AI->setName(Arg->getName() + ".coerce" + Twine(i));
3387 LoadedStructValue =
3388 Builder.CreateInsertValue(LoadedStructValue, AI, i);
3389 }
3390
3391 Builder.CreateStore(LoadedStructValue, Ptr);
3392 } else {
3393 uint64_t SrcSize = StructSize.getFixedValue();
3394 uint64_t DstSize = PtrElementSize.getFixedValue();
3395
3396 Address AddrToStoreInto = Address::invalid();
3397 if (SrcSize <= DstSize) {
3398 AddrToStoreInto = Ptr.withElementType(STy);
3399 } else {
3400 AddrToStoreInto =
3401 CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");
3402 }
3403
3404 assert(STy->getNumElements() == NumIRArgs);
3405 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3406 auto AI = Fn->getArg(FirstIRArg + i);
3407 AI->setName(Arg->getName() + ".coerce" + Twine(i));
3408 Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);
3409 Builder.CreateStore(AI, EltPtr);
3410 }
3411
3412 if (SrcSize > DstSize) {
3413 Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
3414 }
3415 }
3416 } else {
3417 // Simple case, just do a coerced store of the argument into the alloca.
3418 assert(NumIRArgs == 1);
3419 auto AI = Fn->getArg(FirstIRArg);
3420 AI->setName(Arg->getName() + ".coerce");
3422 AI, Ptr,
3423 llvm::TypeSize::getFixed(
3424 getContext().getTypeSizeInChars(Ty).getQuantity() -
3425 ArgI.getDirectOffset()),
3426 /*DstIsVolatile=*/false);
3427 }
3428
3429 // Match to what EmitParmDecl is expecting for this type.
3431 llvm::Value *V =
3432 EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
3433 if (isPromoted)
3434 V = emitArgumentDemotion(*this, Arg, V);
3435 ArgVals.push_back(ParamValue::forDirect(V));
3436 } else {
3437 ArgVals.push_back(ParamValue::forIndirect(Alloca));
3438 }
3439 break;
3440 }
3441
3443 // Reconstruct into a temporary.
3444 Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
3445 ArgVals.push_back(ParamValue::forIndirect(alloca));
3446
3447 auto coercionType = ArgI.getCoerceAndExpandType();
3448 auto unpaddedCoercionType = ArgI.getUnpaddedCoerceAndExpandType();
3449 auto *unpaddedStruct = dyn_cast<llvm::StructType>(unpaddedCoercionType);
3450
3451 alloca = alloca.withElementType(coercionType);
3452
3453 unsigned argIndex = FirstIRArg;
3454 unsigned unpaddedIndex = 0;
3455 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
3456 llvm::Type *eltType = coercionType->getElementType(i);
3458 continue;
3459
3460 auto eltAddr = Builder.CreateStructGEP(alloca, i);
3461 llvm::Value *elt = Fn->getArg(argIndex++);
3462
3463 auto paramType = unpaddedStruct
3464 ? unpaddedStruct->getElementType(unpaddedIndex++)
3465 : unpaddedCoercionType;
3466
3467 if (auto *VecTyTo = dyn_cast<llvm::FixedVectorType>(eltType)) {
3468 if (auto *VecTyFrom = dyn_cast<llvm::ScalableVectorType>(paramType)) {
3469 bool Extracted;
3470 std::tie(elt, Extracted) = CoerceScalableToFixed(
3471 *this, VecTyTo, VecTyFrom, elt, elt->getName());
3472 assert(Extracted && "Unexpected scalable to fixed vector coercion");
3473 }
3474 }
3475 Builder.CreateStore(elt, eltAddr);
3476 }
3477 assert(argIndex == FirstIRArg + NumIRArgs);
3478 break;
3479 }
3480
3481 case ABIArgInfo::Expand: {
3482 // If this structure was expanded into multiple arguments then
3483 // we need to create a temporary and reconstruct it from the
3484 // arguments.
3485 Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));
3486 LValue LV = MakeAddrLValue(Alloca, Ty);
3487 ArgVals.push_back(ParamValue::forIndirect(Alloca));
3488
3489 auto FnArgIter = Fn->arg_begin() + FirstIRArg;
3490 ExpandTypeFromArgs(Ty, LV, FnArgIter);
3491 assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);
3492 for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {
3493 auto AI = Fn->getArg(FirstIRArg + i);
3494 AI->setName(Arg->getName() + "." + Twine(i));
3495 }
3496 break;
3497 }
3498
3500 auto *AI = Fn->getArg(FirstIRArg);
3501 AI->setName(Arg->getName() + ".target_coerce");
3502 Address Alloca =
3503 CreateMemTemp(Ty, getContext().getDeclAlign(Arg), Arg->getName());
3504 Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);
3505 CGM.getABIInfo().createCoercedStore(AI, Ptr, ArgI, false, *this);
3507 llvm::Value *V =
3508 EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());
3509 if (isPromoted) {
3510 V = emitArgumentDemotion(*this, Arg, V);
3511 }
3512 ArgVals.push_back(ParamValue::forDirect(V));
3513 } else {
3514 ArgVals.push_back(ParamValue::forIndirect(Alloca));
3515 }
3516 break;
3517 }
3518 case ABIArgInfo::Ignore:
3519 assert(NumIRArgs == 0);
3520 // Initialize the local variable appropriately.
3521 if (!hasScalarEvaluationKind(Ty)) {
3522 ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
3523 } else {
3524 llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));
3525 ArgVals.push_back(ParamValue::forDirect(U));
3526 }
3527 break;
3528 }
3529 }
3530
3531 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3532 for (int I = Args.size() - 1; I >= 0; --I)
3533 EmitParmDecl(*Args[I], ArgVals[I], I + 1);
3534 } else {
3535 for (unsigned I = 0, E = Args.size(); I != E; ++I)
3536 EmitParmDecl(*Args[I], ArgVals[I], I + 1);
3537 }
3538}
3539
3540static void eraseUnusedBitCasts(llvm::Instruction *insn) {
3541 while (insn->use_empty()) {
3542 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
3543 if (!bitcast)
3544 return;
3545
3546 // This is "safe" because we would have used a ConstantExpr otherwise.
3547 insn = cast<llvm::Instruction>(bitcast->getOperand(0));
3548 bitcast->eraseFromParent();
3549 }
3550}
3551
3552/// Try to emit a fused autorelease of a return result.
3554 llvm::Value *result) {
3555 // We must be immediately followed the cast.
3556 llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
3557 if (BB->empty())
3558 return nullptr;
3559 if (&BB->back() != result)
3560 return nullptr;
3561
3562 llvm::Type *resultType = result->getType();
3563
3564 // result is in a BasicBlock and is therefore an Instruction.
3565 llvm::Instruction *generator = cast<llvm::Instruction>(result);
3566
3568
3569 // Look for:
3570 // %generator = bitcast %type1* %generator2 to %type2*
3571 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
3572 // We would have emitted this as a constant if the operand weren't
3573 // an Instruction.
3574 generator = cast<llvm::Instruction>(bitcast->getOperand(0));
3575
3576 // Require the generator to be immediately followed by the cast.
3577 if (generator->getNextNode() != bitcast)
3578 return nullptr;
3579
3580 InstsToKill.push_back(bitcast);
3581 }
3582
3583 // Look for:
3584 // %generator = call i8* @objc_retain(i8* %originalResult)
3585 // or
3586 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
3587 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
3588 if (!call)
3589 return nullptr;
3590
3591 bool doRetainAutorelease;
3592
3593 if (call->getCalledOperand() == CGF.CGM.getObjCEntrypoints().objc_retain) {
3594 doRetainAutorelease = true;
3595 } else if (call->getCalledOperand() ==
3597 doRetainAutorelease = false;
3598
3599 // If we emitted an assembly marker for this call (and the
3600 // ARCEntrypoints field should have been set if so), go looking
3601 // for that call. If we can't find it, we can't do this
3602 // optimization. But it should always be the immediately previous
3603 // instruction, unless we needed bitcasts around the call.
3605 llvm::Instruction *prev = call->getPrevNode();
3606 assert(prev);
3607 if (isa<llvm::BitCastInst>(prev)) {
3608 prev = prev->getPrevNode();
3609 assert(prev);
3610 }
3611 assert(isa<llvm::CallInst>(prev));
3612 assert(cast<llvm::CallInst>(prev)->getCalledOperand() ==
3614 InstsToKill.push_back(prev);
3615 }
3616 } else {
3617 return nullptr;
3618 }
3619
3620 result = call->getArgOperand(0);
3621 InstsToKill.push_back(call);
3622
3623 // Keep killing bitcasts, for sanity. Note that we no longer care
3624 // about precise ordering as long as there's exactly one use.
3625 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
3626 if (!bitcast->hasOneUse())
3627 break;
3628 InstsToKill.push_back(bitcast);
3629 result = bitcast->getOperand(0);
3630 }
3631
3632 // Delete all the unnecessary instructions, from latest to earliest.
3633 for (auto *I : InstsToKill)
3634 I->eraseFromParent();
3635
3636 // Do the fused retain/autorelease if we were asked to.
3637 if (doRetainAutorelease)
3638 result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
3639
3640 // Cast back to the result type.
3641 return CGF.Builder.CreateBitCast(result, resultType);
3642}
3643
3644/// If this is a +1 of the value of an immutable 'self', remove it.
3646 llvm::Value *result) {
3647 // This is only applicable to a method with an immutable 'self'.
3648 const ObjCMethodDecl *method =
3649 dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
3650 if (!method)
3651 return nullptr;
3652 const VarDecl *self = method->getSelfDecl();
3653 if (!self->getType().isConstQualified())
3654 return nullptr;
3655
3656 // Look for a retain call. Note: stripPointerCasts looks through returned arg
3657 // functions, which would cause us to miss the retain.
3658 llvm::CallInst *retainCall = dyn_cast<llvm::CallInst>(result);
3659 if (!retainCall || retainCall->getCalledOperand() !=
3661 return nullptr;
3662
3663 // Look for an ordinary load of 'self'.
3664 llvm::Value *retainedValue = retainCall->getArgOperand(0);
3665 llvm::LoadInst *load =
3666 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
3667 if (!load || load->isAtomic() || load->isVolatile() ||
3668 load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getBasePointer())
3669 return nullptr;
3670
3671 // Okay! Burn it all down. This relies for correctness on the
3672 // assumption that the retain is emitted as part of the return and
3673 // that thereafter everything is used "linearly".
3674 llvm::Type *resultType = result->getType();
3675 eraseUnusedBitCasts(cast<llvm::Instruction>(result));
3676 assert(retainCall->use_empty());
3677 retainCall->eraseFromParent();
3678 eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
3679
3680 return CGF.Builder.CreateBitCast(load, resultType);
3681}
3682
3683/// Emit an ARC autorelease of the result of a function.
3684///
3685/// \return the value to actually return from the function
3687 llvm::Value *result) {
3688 // If we're returning 'self', kill the initial retain. This is a
3689 // heuristic attempt to "encourage correctness" in the really unfortunate
3690 // case where we have a return of self during a dealloc and we desperately
3691 // need to avoid the possible autorelease.
3692 if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
3693 return self;
3694
3695 // At -O0, try to emit a fused retain/autorelease.
3696 if (CGF.shouldUseFusedARCCalls())
3697 if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
3698 return fused;
3699
3700 return CGF.EmitARCAutoreleaseReturnValue(result);
3701}
3702
3703/// Heuristically search for a dominating store to the return-value slot.
3705 llvm::Value *ReturnValuePtr = CGF.ReturnValue.getBasePointer();
3706
3707 // Check if a User is a store which pointerOperand is the ReturnValue.
3708 // We are looking for stores to the ReturnValue, not for stores of the
3709 // ReturnValue to some other location.
3710 auto GetStoreIfValid = [&CGF,
3711 ReturnValuePtr](llvm::User *U) -> llvm::StoreInst * {
3712 auto *SI = dyn_cast<llvm::StoreInst>(U);
3713 if (!SI || SI->getPointerOperand() != ReturnValuePtr ||
3714 SI->getValueOperand()->getType() != CGF.ReturnValue.getElementType())
3715 return nullptr;
3716 // These aren't actually possible for non-coerced returns, and we
3717 // only care about non-coerced returns on this code path.
3718 // All memory instructions inside __try block are volatile.
3719 assert(!SI->isAtomic() &&
3720 (!SI->isVolatile() || CGF.currentFunctionUsesSEHTry()));
3721 return SI;
3722 };
3723 // If there are multiple uses of the return-value slot, just check
3724 // for something immediately preceding the IP. Sometimes this can
3725 // happen with how we generate implicit-returns; it can also happen
3726 // with noreturn cleanups.
3727 if (!ReturnValuePtr->hasOneUse()) {
3728 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
3729 if (IP->empty())
3730 return nullptr;
3731
3732 // Look at directly preceding instruction, skipping bitcasts, lifetime
3733 // markers, and fake uses and their operands.
3734 const llvm::Instruction *LoadIntoFakeUse = nullptr;
3735 for (llvm::Instruction &I : llvm::reverse(*IP)) {
3736 // Ignore instructions that are just loads for fake uses; the load should
3737 // immediately precede the fake use, so we only need to remember the
3738 // operand for the last fake use seen.
3739 if (LoadIntoFakeUse == &I)
3740 continue;
3741 if (isa<llvm::BitCastInst>(&I))
3742 continue;
3743 if (auto *II = dyn_cast<llvm::IntrinsicInst>(&I)) {
3744 if (II->getIntrinsicID() == llvm::Intrinsic::lifetime_end)
3745 continue;
3746
3747 if (II->getIntrinsicID() == llvm::Intrinsic::fake_use) {
3748 LoadIntoFakeUse = dyn_cast<llvm::Instruction>(II->getArgOperand(0));
3749 continue;
3750 }
3751 }
3752 return GetStoreIfValid(&I);
3753 }
3754 return nullptr;
3755 }
3756
3757 llvm::StoreInst *store = GetStoreIfValid(ReturnValuePtr->user_back());
3758 if (!store)
3759 return nullptr;
3760
3761 // Now do a first-and-dirty dominance check: just walk up the
3762 // single-predecessors chain from the current insertion point.
3763 llvm::BasicBlock *StoreBB = store->getParent();
3764 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
3766 while (IP != StoreBB) {
3767 if (!SeenBBs.insert(IP).second || !(IP = IP->getSinglePredecessor()))
3768 return nullptr;
3769 }
3770
3771 // Okay, the store's basic block dominates the insertion point; we
3772 // can do our thing.
3773 return store;
3774}
3775
3776// Helper functions for EmitCMSEClearRecord
3777
3778// Set the bits corresponding to a field having width `BitWidth` and located at
3779// offset `BitOffset` (from the least significant bit) within a storage unit of
3780// `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.
3781// Use little-endian layout, i.e.`Bits[0]` is the LSB.
3782static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int BitOffset,
3783 int BitWidth, int CharWidth) {
3784 assert(CharWidth <= 64);
3785 assert(static_cast<unsigned>(BitWidth) <= Bits.size() * CharWidth);
3786
3787 int Pos = 0;
3788 if (BitOffset >= CharWidth) {
3789 Pos += BitOffset / CharWidth;
3790 BitOffset = BitOffset % CharWidth;
3791 }
3792
3793 const uint64_t Used = (uint64_t(1) << CharWidth) - 1;
3794 if (BitOffset + BitWidth >= CharWidth) {
3795 Bits[Pos++] |= (Used << BitOffset) & Used;
3796 BitWidth -= CharWidth - BitOffset;
3797 BitOffset = 0;
3798 }
3799
3800 while (BitWidth >= CharWidth) {
3801 Bits[Pos++] = Used;
3802 BitWidth -= CharWidth;
3803 }
3804
3805 if (BitWidth > 0)
3806 Bits[Pos++] |= (Used >> (CharWidth - BitWidth)) << BitOffset;
3807}
3808
3809// Set the bits corresponding to a field having width `BitWidth` and located at
3810// offset `BitOffset` (from the least significant bit) within a storage unit of
3811// `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of
3812// `Bits` corresponds to one target byte. Use target endian layout.
3813static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int StorageOffset,
3814 int StorageSize, int BitOffset, int BitWidth,
3815 int CharWidth, bool BigEndian) {
3816
3817 SmallVector<uint64_t, 8> TmpBits(StorageSize);
3818 setBitRange(TmpBits, BitOffset, BitWidth, CharWidth);
3819
3820 if (BigEndian)
3821 std::reverse(TmpBits.begin(), TmpBits.end());
3822
3823 for (uint64_t V : TmpBits)
3824 Bits[StorageOffset++] |= V;
3825}
3826
3827static void setUsedBits(CodeGenModule &, QualType, int,
3829
3830// Set the bits in `Bits`, which correspond to the value representations of
3831// the actual members of the record type `RTy`. Note that this function does
3832// not handle base classes, virtual tables, etc, since they cannot happen in
3833// CMSE function arguments or return. The bit mask corresponds to the target
3834// memory layout, i.e. it's endian dependent.
3835static void setUsedBits(CodeGenModule &CGM, const RecordType *RTy, int Offset,
3837 ASTContext &Context = CGM.getContext();
3838 int CharWidth = Context.getCharWidth();
3839 const RecordDecl *RD = RTy->getOriginalDecl()->getDefinition();
3840 const ASTRecordLayout &ASTLayout = Context.getASTRecordLayout(RD);
3841 const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(RD);
3842
3843 int Idx = 0;
3844 for (auto I = RD->field_begin(), E = RD->field_end(); I != E; ++I, ++Idx) {
3845 const FieldDecl *F = *I;
3846
3847 if (F->isUnnamedBitField() || F->isZeroLengthBitField() ||
3849 continue;
3850
3851 if (F->isBitField()) {
3852 const CGBitFieldInfo &BFI = Layout.getBitFieldInfo(F);
3853 setBitRange(Bits, Offset + BFI.StorageOffset.getQuantity(),
3854 BFI.StorageSize / CharWidth, BFI.Offset, BFI.Size, CharWidth,
3855 CGM.getDataLayout().isBigEndian());
3856 continue;
3857 }
3858
3859 setUsedBits(CGM, F->getType(),
3860 Offset + ASTLayout.getFieldOffset(Idx) / CharWidth, Bits);
3861 }
3862}
3863
3864// Set the bits in `Bits`, which correspond to the value representations of
3865// the elements of an array type `ATy`.
3866static void setUsedBits(CodeGenModule &CGM, const ConstantArrayType *ATy,
3867 int Offset, SmallVectorImpl<uint64_t> &Bits) {
3868 const ASTContext &Context = CGM.getContext();
3869
3870 QualType ETy = Context.getBaseElementType(ATy);
3871 int Size = Context.getTypeSizeInChars(ETy).getQuantity();
3872 SmallVector<uint64_t, 4> TmpBits(Size);
3873 setUsedBits(CGM, ETy, 0, TmpBits);
3874
3875 for (int I = 0, N = Context.getConstantArrayElementCount(ATy); I < N; ++I) {
3876 auto Src = TmpBits.begin();
3877 auto Dst = Bits.begin() + Offset + I * Size;
3878 for (int J = 0; J < Size; ++J)
3879 *Dst++ |= *Src++;
3880 }
3881}
3882
3883// Set the bits in `Bits`, which correspond to the value representations of
3884// the type `QTy`.
3885static void setUsedBits(CodeGenModule &CGM, QualType QTy, int Offset,
3887 if (const auto *RTy = QTy->getAsCanonical<RecordType>())
3888 return setUsedBits(CGM, RTy, Offset, Bits);
3889
3890 ASTContext &Context = CGM.getContext();
3891 if (const auto *ATy = Context.getAsConstantArrayType(QTy))
3892 return setUsedBits(CGM, ATy, Offset, Bits);
3893
3894 int Size = Context.getTypeSizeInChars(QTy).getQuantity();
3895 if (Size <= 0)
3896 return;
3897
3898 std::fill_n(Bits.begin() + Offset, Size,
3899 (uint64_t(1) << Context.getCharWidth()) - 1);
3900}
3901
3903 int Pos, int Size, int CharWidth,
3904 bool BigEndian) {
3905 assert(Size > 0);
3906 uint64_t Mask = 0;
3907 if (BigEndian) {
3908 for (auto P = Bits.begin() + Pos, E = Bits.begin() + Pos + Size; P != E;
3909 ++P)
3910 Mask = (Mask << CharWidth) | *P;
3911 } else {
3912 auto P = Bits.begin() + Pos + Size, End = Bits.begin() + Pos;
3913 do
3914 Mask = (Mask << CharWidth) | *--P;
3915 while (P != End);
3916 }
3917 return Mask;
3918}
3919
3920// Emit code to clear the bits in a record, which aren't a part of any user
3921// declared member, when the record is a function return.
3922llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
3923 llvm::IntegerType *ITy,
3924 QualType QTy) {
3925 assert(Src->getType() == ITy);
3926 assert(ITy->getScalarSizeInBits() <= 64);
3927
3928 const llvm::DataLayout &DataLayout = CGM.getDataLayout();
3929 int Size = DataLayout.getTypeStoreSize(ITy);
3930 SmallVector<uint64_t, 4> Bits(Size);
3931 setUsedBits(CGM, QTy->castAsCanonical<RecordType>(), 0, Bits);
3932
3933 int CharWidth = CGM.getContext().getCharWidth();
3934 uint64_t Mask =
3935 buildMultiCharMask(Bits, 0, Size, CharWidth, DataLayout.isBigEndian());
3936
3937 return Builder.CreateAnd(Src, Mask, "cmse.clear");
3938}
3939
3940// Emit code to clear the bits in a record, which aren't a part of any user
3941// declared member, when the record is a function argument.
3942llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,
3943 llvm::ArrayType *ATy,
3944 QualType QTy) {
3945 const llvm::DataLayout &DataLayout = CGM.getDataLayout();
3946 int Size = DataLayout.getTypeStoreSize(ATy);
3947 SmallVector<uint64_t, 16> Bits(Size);
3948 setUsedBits(CGM, QTy->castAsCanonical<RecordType>(), 0, Bits);
3949
3950 // Clear each element of the LLVM array.
3951 int CharWidth = CGM.getContext().getCharWidth();
3952 int CharsPerElt =
3953 ATy->getArrayElementType()->getScalarSizeInBits() / CharWidth;
3954 int MaskIndex = 0;
3955 llvm::Value *R = llvm::PoisonValue::get(ATy);
3956 for (int I = 0, N = ATy->getArrayNumElements(); I != N; ++I) {
3957 uint64_t Mask = buildMultiCharMask(Bits, MaskIndex, CharsPerElt, CharWidth,
3958 DataLayout.isBigEndian());
3959 MaskIndex += CharsPerElt;
3960 llvm::Value *T0 = Builder.CreateExtractValue(Src, I);
3961 llvm::Value *T1 = Builder.CreateAnd(T0, Mask, "cmse.clear");
3962 R = Builder.CreateInsertValue(R, T1, I);
3963 }
3964
3965 return R;
3966}
3967
3969 const CGFunctionInfo &FI, bool EmitRetDbgLoc, SourceLocation EndLoc,
3970 uint64_t RetKeyInstructionsSourceAtom) {
3971 if (FI.isNoReturn()) {
3972 // Noreturn functions don't return.
3973 EmitUnreachable(EndLoc);
3974 return;
3975 }
3976
3977 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
3978 // Naked functions don't have epilogues.
3979 Builder.CreateUnreachable();
3980 return;
3981 }
3982
3983 // Functions with no result always return void.
3984 if (!ReturnValue.isValid()) {
3985 auto *I = Builder.CreateRetVoid();
3986 if (RetKeyInstructionsSourceAtom)
3987 addInstToSpecificSourceAtom(I, nullptr, RetKeyInstructionsSourceAtom);
3988 else
3989 addInstToNewSourceAtom(I, nullptr);
3990 return;
3991 }
3992
3993 llvm::DebugLoc RetDbgLoc;
3994 llvm::Value *RV = nullptr;
3995 QualType RetTy = FI.getReturnType();
3996 const ABIArgInfo &RetAI = FI.getReturnInfo();
3997
3998 switch (RetAI.getKind()) {
4000 // Aggregates get evaluated directly into the destination. Sometimes we
4001 // need to return the sret value in a register, though.
4002 assert(hasAggregateEvaluationKind(RetTy));
4003 if (RetAI.getInAllocaSRet()) {
4004 llvm::Function::arg_iterator EI = CurFn->arg_end();
4005 --EI;
4006 llvm::Value *ArgStruct = &*EI;
4007 llvm::Value *SRet = Builder.CreateStructGEP(
4008 FI.getArgStruct(), ArgStruct, RetAI.getInAllocaFieldIndex());
4009 llvm::Type *Ty =
4010 cast<llvm::GetElementPtrInst>(SRet)->getResultElementType();
4011 RV = Builder.CreateAlignedLoad(Ty, SRet, getPointerAlign(), "sret");
4012 }
4013 break;
4014
4015 case ABIArgInfo::Indirect: {
4016 auto AI = CurFn->arg_begin();
4017 if (RetAI.isSRetAfterThis())
4018 ++AI;
4019 switch (getEvaluationKind(RetTy)) {
4020 case TEK_Complex: {
4021 ComplexPairTy RT =
4024 /*isInit*/ true);
4025 break;
4026 }
4027 case TEK_Aggregate:
4028 // Do nothing; aggregates get evaluated directly into the destination.
4029 break;
4030 case TEK_Scalar: {
4031 LValueBaseInfo BaseInfo;
4032 TBAAAccessInfo TBAAInfo;
4033 CharUnits Alignment =
4034 CGM.getNaturalTypeAlignment(RetTy, &BaseInfo, &TBAAInfo);
4035 Address ArgAddr(&*AI, ConvertType(RetTy), Alignment);
4036 LValue ArgVal =
4037 LValue::MakeAddr(ArgAddr, RetTy, getContext(), BaseInfo, TBAAInfo);
4039 EmitLoadOfScalar(MakeAddrLValue(ReturnValue, RetTy), EndLoc), ArgVal,
4040 /*isInit*/ true);
4041 break;
4042 }
4043 }
4044 break;
4045 }
4046
4047 case ABIArgInfo::Extend:
4048 case ABIArgInfo::Direct:
4049 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
4050 RetAI.getDirectOffset() == 0) {
4051 // The internal return value temp always will have pointer-to-return-type
4052 // type, just do a load.
4053
4054 // If there is a dominating store to ReturnValue, we can elide
4055 // the load, zap the store, and usually zap the alloca.
4056 if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
4057 // Reuse the debug location from the store unless there is
4058 // cleanup code to be emitted between the store and return
4059 // instruction.
4060 if (EmitRetDbgLoc && !AutoreleaseResult)
4061 RetDbgLoc = SI->getDebugLoc();
4062 // Get the stored value and nuke the now-dead store.
4063 RV = SI->getValueOperand();
4064 SI->eraseFromParent();
4065
4066 // Otherwise, we have to do a simple load.
4067 } else {
4069 }
4070 } else {
4071 // If the value is offset in memory, apply the offset now.
4072 Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
4073
4074 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
4075 }
4076
4077 // In ARC, end functions that return a retainable type with a call
4078 // to objc_autoreleaseReturnValue.
4079 if (AutoreleaseResult) {
4080#ifndef NDEBUG
4081 // Type::isObjCRetainabletype has to be called on a QualType that hasn't
4082 // been stripped of the typedefs, so we cannot use RetTy here. Get the
4083 // original return type of FunctionDecl, CurCodeDecl, and BlockDecl from
4084 // CurCodeDecl or BlockInfo.
4085 QualType RT;
4086
4087 if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
4088 RT = FD->getReturnType();
4089 else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
4090 RT = MD->getReturnType();
4091 else if (isa<BlockDecl>(CurCodeDecl))
4093 else
4094 llvm_unreachable("Unexpected function/method type");
4095
4096 assert(getLangOpts().ObjCAutoRefCount && !FI.isReturnsRetained() &&
4097 RT->isObjCRetainableType());
4098#endif
4099 RV = emitAutoreleaseOfResult(*this, RV);
4100 }
4101
4102 break;
4103
4104 case ABIArgInfo::Ignore:
4105 break;
4106
4108 auto coercionType = RetAI.getCoerceAndExpandType();
4109 auto unpaddedCoercionType = RetAI.getUnpaddedCoerceAndExpandType();
4110 auto *unpaddedStruct = dyn_cast<llvm::StructType>(unpaddedCoercionType);
4111
4112 // Load all of the coerced elements out into results.
4114 Address addr = ReturnValue.withElementType(coercionType);
4115 unsigned unpaddedIndex = 0;
4116 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4117 auto coercedEltType = coercionType->getElementType(i);
4118 if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
4119 continue;
4120
4121 auto eltAddr = Builder.CreateStructGEP(addr, i);
4122 llvm::Value *elt = CreateCoercedLoad(
4123 eltAddr,
4124 unpaddedStruct ? unpaddedStruct->getElementType(unpaddedIndex++)
4125 : unpaddedCoercionType,
4126 *this);
4127 results.push_back(elt);
4128 }
4129
4130 // If we have one result, it's the single direct result type.
4131 if (results.size() == 1) {
4132 RV = results[0];
4133
4134 // Otherwise, we need to make a first-class aggregate.
4135 } else {
4136 // Construct a return type that lacks padding elements.
4137 llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();
4138
4139 RV = llvm::PoisonValue::get(returnType);
4140 for (unsigned i = 0, e = results.size(); i != e; ++i) {
4141 RV = Builder.CreateInsertValue(RV, results[i], i);
4142 }
4143 }
4144 break;
4145 }
4147 Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);
4148 RV = CGM.getABIInfo().createCoercedLoad(V, RetAI, *this);
4149 break;
4150 }
4151 case ABIArgInfo::Expand:
4153 llvm_unreachable("Invalid ABI kind for return argument");
4154 }
4155
4156 llvm::Instruction *Ret;
4157 if (RV) {
4158 if (CurFuncDecl && CurFuncDecl->hasAttr<CmseNSEntryAttr>()) {
4159 // For certain return types, clear padding bits, as they may reveal
4160 // sensitive information.
4161 // Small struct/union types are passed as integers.
4162 auto *ITy = dyn_cast<llvm::IntegerType>(RV->getType());
4163 if (ITy != nullptr && isa<RecordType>(RetTy.getCanonicalType()))
4164 RV = EmitCMSEClearRecord(RV, ITy, RetTy);
4165 }
4167 Ret = Builder.CreateRet(RV);
4168 } else {
4169 Ret = Builder.CreateRetVoid();
4170 }
4171
4172 if (RetDbgLoc)
4173 Ret->setDebugLoc(std::move(RetDbgLoc));
4174
4175 llvm::Value *Backup = RV ? Ret->getOperand(0) : nullptr;
4176 if (RetKeyInstructionsSourceAtom)
4177 addInstToSpecificSourceAtom(Ret, Backup, RetKeyInstructionsSourceAtom);
4178 else
4179 addInstToNewSourceAtom(Ret, Backup);
4180}
4181
4183 // A current decl may not be available when emitting vtable thunks.
4184 if (!CurCodeDecl)
4185 return;
4186
4187 // If the return block isn't reachable, neither is this check, so don't emit
4188 // it.
4189 if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())
4190 return;
4191
4192 ReturnsNonNullAttr *RetNNAttr = nullptr;
4193 if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
4194 RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
4195
4196 if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
4197 return;
4198
4199 // Prefer the returns_nonnull attribute if it's present.
4200 SourceLocation AttrLoc;
4202 SanitizerHandler Handler;
4203 if (RetNNAttr) {
4204 assert(!requiresReturnValueNullabilityCheck() &&
4205 "Cannot check nullability and the nonnull attribute");
4206 AttrLoc = RetNNAttr->getLocation();
4207 CheckKind = SanitizerKind::SO_ReturnsNonnullAttribute;
4208 Handler = SanitizerHandler::NonnullReturn;
4209 } else {
4210 if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
4211 if (auto *TSI = DD->getTypeSourceInfo())
4212 if (auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>())
4213 AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
4214 CheckKind = SanitizerKind::SO_NullabilityReturn;
4215 Handler = SanitizerHandler::NullabilityReturn;
4216 }
4217
4218 SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
4219
4220 // Make sure the "return" source location is valid. If we're checking a
4221 // nullability annotation, make sure the preconditions for the check are met.
4222 llvm::BasicBlock *Check = createBasicBlock("nullcheck");
4223 llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");
4224 llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");
4225 llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);
4226 if (requiresReturnValueNullabilityCheck())
4227 CanNullCheck =
4228 Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
4229 Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
4230 EmitBlock(Check);
4231
4232 // Now do the null check.
4233 llvm::Value *Cond = Builder.CreateIsNotNull(RV);
4234 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
4235 llvm::Value *DynamicData[] = {SLocPtr};
4236 EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
4237
4238 EmitBlock(NoCheck);
4239
4240#ifndef NDEBUG
4241 // The return location should not be used after the check has been emitted.
4242 ReturnLocation = Address::invalid();
4243#endif
4244}
4245
4247 const CXXRecordDecl *RD = type->getAsCXXRecordDecl();
4248 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;
4249}
4250
4252 // FIXME: Generate IR in one pass, rather than going back and fixing up these
4253 // placeholders.
4254 llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);
4255 llvm::Type *IRPtrTy = llvm::PointerType::getUnqual(CGF.getLLVMContext());
4256 llvm::Value *Placeholder = llvm::PoisonValue::get(IRPtrTy);
4257
4258 // FIXME: When we generate this IR in one pass, we shouldn't need
4259 // this win32-specific alignment hack.
4261 Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);
4262
4263 return AggValueSlot::forAddr(
4264 Address(Placeholder, IRTy, Align), Ty.getQualifiers(),
4267}
4268
4270 const VarDecl *param,
4271 SourceLocation loc) {
4272 // StartFunction converted the ABI-lowered parameter(s) into a
4273 // local alloca. We need to turn that into an r-value suitable
4274 // for EmitCall.
4275 Address local = GetAddrOfLocalVar(param);
4276
4277 QualType type = param->getType();
4278
4279 // GetAddrOfLocalVar returns a pointer-to-pointer for references,
4280 // but the argument needs to be the original pointer.
4281 if (type->isReferenceType()) {
4282 args.add(RValue::get(Builder.CreateLoad(local)), type);
4283
4284 // In ARC, move out of consumed arguments so that the release cleanup
4285 // entered by StartFunction doesn't cause an over-release. This isn't
4286 // optimal -O0 code generation, but it should get cleaned up when
4287 // optimization is enabled. This also assumes that delegate calls are
4288 // performed exactly once for a set of arguments, but that should be safe.
4289 } else if (getLangOpts().ObjCAutoRefCount &&
4290 param->hasAttr<NSConsumedAttr>() && type->isObjCRetainableType()) {
4291 llvm::Value *ptr = Builder.CreateLoad(local);
4292 auto null =
4293 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
4294 Builder.CreateStore(null, local);
4295 args.add(RValue::get(ptr), type);
4296
4297 // For the most part, we just need to load the alloca, except that
4298 // aggregate r-values are actually pointers to temporaries.
4299 } else {
4300 args.add(convertTempToRValue(local, type, loc), type);
4301 }
4302
4303 // Deactivate the cleanup for the callee-destructed param that was pushed.
4304 if (type->isRecordType() && !CurFuncIsThunk &&
4305 type->castAsRecordDecl()->isParamDestroyedInCallee() &&
4306 param->needsDestruction(getContext())) {
4308 CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));
4309 assert(cleanup.isValid() &&
4310 "cleanup for callee-destructed param not recorded");
4311 // This unreachable is a temporary marker which will be removed later.
4312 llvm::Instruction *isActive = Builder.CreateUnreachable();
4313 args.addArgCleanupDeactivation(cleanup, isActive);
4314 }
4315}
4316
4317static bool isProvablyNull(llvm::Value *addr) {
4318 return llvm::isa_and_nonnull<llvm::ConstantPointerNull>(addr);
4319}
4320
4322 return llvm::isKnownNonZero(Addr.getBasePointer(), CGF.CGM.getDataLayout());
4323}
4324
4325/// Emit the actual writing-back of a writeback.
4327 const CallArgList::Writeback &writeback) {
4328 const LValue &srcLV = writeback.Source;
4329 Address srcAddr = srcLV.getAddress();
4330 assert(!isProvablyNull(srcAddr.getBasePointer()) &&
4331 "shouldn't have writeback for provably null argument");
4332
4333 if (writeback.WritebackExpr) {
4334 CGF.EmitIgnoredExpr(writeback.WritebackExpr);
4335 CGF.EmitLifetimeEnd(writeback.Temporary.getBasePointer());
4336 return;
4337 }
4338
4339 llvm::BasicBlock *contBB = nullptr;
4340
4341 // If the argument wasn't provably non-null, we need to null check
4342 // before doing the store.
4343 bool provablyNonNull = isProvablyNonNull(srcAddr, CGF);
4344
4345 if (!provablyNonNull) {
4346 llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
4347 contBB = CGF.createBasicBlock("icr.done");
4348
4349 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
4350 CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
4351 CGF.EmitBlock(writebackBB);
4352 }
4353
4354 // Load the value to writeback.
4355 llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
4356
4357 // Cast it back, in case we're writing an id to a Foo* or something.
4358 value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),
4359 "icr.writeback-cast");
4360
4361 // Perform the writeback.
4362
4363 // If we have a "to use" value, it's something we need to emit a use
4364 // of. This has to be carefully threaded in: if it's done after the
4365 // release it's potentially undefined behavior (and the optimizer
4366 // will ignore it), and if it happens before the retain then the
4367 // optimizer could move the release there.
4368 if (writeback.ToUse) {
4369 assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
4370
4371 // Retain the new value. No need to block-copy here: the block's
4372 // being passed up the stack.
4373 value = CGF.EmitARCRetainNonBlock(value);
4374
4375 // Emit the intrinsic use here.
4376 CGF.EmitARCIntrinsicUse(writeback.ToUse);
4377
4378 // Load the old value (primitively).
4379 llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());
4380
4381 // Put the new value in place (primitively).
4382 CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
4383
4384 // Release the old value.
4385 CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
4386
4387 // Otherwise, we can just do a normal lvalue store.
4388 } else {
4389 CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
4390 }
4391
4392 // Jump to the continuation block.
4393 if (!provablyNonNull)
4394 CGF.EmitBlock(contBB);
4395}
4396
4398 const CallArgList &CallArgs) {
4400 CallArgs.getCleanupsToDeactivate();
4401 // Iterate in reverse to increase the likelihood of popping the cleanup.
4402 for (const auto &I : llvm::reverse(Cleanups)) {
4403 CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);
4404 I.IsActiveIP->eraseFromParent();
4405 }
4406}
4407
4408static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
4409 if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
4410 if (uop->getOpcode() == UO_AddrOf)
4411 return uop->getSubExpr();
4412 return nullptr;
4413}
4414
4415/// Emit an argument that's being passed call-by-writeback. That is,
4416/// we are passing the address of an __autoreleased temporary; it
4417/// might be copy-initialized with the current value of the given
4418/// address, but it will definitely be copied out of after the call.
4420 const ObjCIndirectCopyRestoreExpr *CRE) {
4421 LValue srcLV;
4422
4423 // Make an optimistic effort to emit the address as an l-value.
4424 // This can fail if the argument expression is more complicated.
4425 if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
4426 srcLV = CGF.EmitLValue(lvExpr);
4427
4428 // Otherwise, just emit it as a scalar.
4429 } else {
4430 Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());
4431
4432 QualType srcAddrType =
4434 srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
4435 }
4436 Address srcAddr = srcLV.getAddress();
4437
4438 // The dest and src types don't necessarily match in LLVM terms
4439 // because of the crazy ObjC compatibility rules.
4440
4441 llvm::PointerType *destType =
4442 cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
4443 llvm::Type *destElemType =
4445
4446 // If the address is a constant null, just pass the appropriate null.
4447 if (isProvablyNull(srcAddr.getBasePointer())) {
4448 args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
4449 CRE->getType());
4450 return;
4451 }
4452
4453 // Create the temporary.
4454 Address temp =
4455 CGF.CreateTempAlloca(destElemType, CGF.getPointerAlign(), "icr.temp");
4456 // Loading an l-value can introduce a cleanup if the l-value is __weak,
4457 // and that cleanup will be conditional if we can't prove that the l-value
4458 // isn't null, so we need to register a dominating point so that the cleanups
4459 // system will make valid IR.
4461
4462 // Zero-initialize it if we're not doing a copy-initialization.
4463 bool shouldCopy = CRE->shouldCopy();
4464 if (!shouldCopy) {
4465 llvm::Value *null =
4466 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destElemType));
4467 CGF.Builder.CreateStore(null, temp);
4468 }
4469
4470 llvm::BasicBlock *contBB = nullptr;
4471 llvm::BasicBlock *originBB = nullptr;
4472
4473 // If the address is *not* known to be non-null, we need to switch.
4474 llvm::Value *finalArgument;
4475
4476 bool provablyNonNull = isProvablyNonNull(srcAddr, CGF);
4477
4478 if (provablyNonNull) {
4479 finalArgument = temp.emitRawPointer(CGF);
4480 } else {
4481 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
4482
4483 finalArgument = CGF.Builder.CreateSelect(
4484 isNull, llvm::ConstantPointerNull::get(destType),
4485 temp.emitRawPointer(CGF), "icr.argument");
4486
4487 // If we need to copy, then the load has to be conditional, which
4488 // means we need control flow.
4489 if (shouldCopy) {
4490 originBB = CGF.Builder.GetInsertBlock();
4491 contBB = CGF.createBasicBlock("icr.cont");
4492 llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
4493 CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
4494 CGF.EmitBlock(copyBB);
4495 condEval.begin(CGF);
4496 }
4497 }
4498
4499 llvm::Value *valueToUse = nullptr;
4500
4501 // Perform a copy if necessary.
4502 if (shouldCopy) {
4503 RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());
4504 assert(srcRV.isScalar());
4505
4506 llvm::Value *src = srcRV.getScalarVal();
4507 src = CGF.Builder.CreateBitCast(src, destElemType, "icr.cast");
4508
4509 // Use an ordinary store, not a store-to-lvalue.
4510 CGF.Builder.CreateStore(src, temp);
4511
4512 // If optimization is enabled, and the value was held in a
4513 // __strong variable, we need to tell the optimizer that this
4514 // value has to stay alive until we're doing the store back.
4515 // This is because the temporary is effectively unretained,
4516 // and so otherwise we can violate the high-level semantics.
4517 if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
4519 valueToUse = src;
4520 }
4521 }
4522
4523 // Finish the control flow if we needed it.
4524 if (shouldCopy && !provablyNonNull) {
4525 llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
4526 CGF.EmitBlock(contBB);
4527
4528 // Make a phi for the value to intrinsically use.
4529 if (valueToUse) {
4530 llvm::PHINode *phiToUse =
4531 CGF.Builder.CreatePHI(valueToUse->getType(), 2, "icr.to-use");
4532 phiToUse->addIncoming(valueToUse, copyBB);
4533 phiToUse->addIncoming(llvm::PoisonValue::get(valueToUse->getType()),
4534 originBB);
4535 valueToUse = phiToUse;
4536 }
4537
4538 condEval.end(CGF);
4539 }
4540
4541 args.addWriteback(srcLV, temp, valueToUse);
4542 args.add(RValue::get(finalArgument), CRE->getType());
4543}
4544
4546 assert(!StackBase);
4547
4548 // Save the stack.
4549 StackBase = CGF.Builder.CreateStackSave("inalloca.save");
4550}
4551
4553 if (StackBase) {
4554 // Restore the stack after the call.
4555 CGF.Builder.CreateStackRestore(StackBase);
4556 }
4557}
4558
4560 SourceLocation ArgLoc,
4561 AbstractCallee AC, unsigned ParmNum) {
4562 if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
4563 SanOpts.has(SanitizerKind::NullabilityArg)))
4564 return;
4565
4566 // The param decl may be missing in a variadic function.
4567 auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;
4568 unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
4569
4570 // Prefer the nonnull attribute if it's present.
4571 const NonNullAttr *NNAttr = nullptr;
4572 if (SanOpts.has(SanitizerKind::NonnullAttribute))
4573 NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);
4574
4575 bool CanCheckNullability = false;
4576 if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD &&
4577 !PVD->getType()->isRecordType()) {
4578 auto Nullability = PVD->getType()->getNullability();
4579 CanCheckNullability = Nullability &&
4580 *Nullability == NullabilityKind::NonNull &&
4581 PVD->getTypeSourceInfo();
4582 }
4583
4584 if (!NNAttr && !CanCheckNullability)
4585 return;
4586
4587 SourceLocation AttrLoc;
4589 SanitizerHandler Handler;
4590 if (NNAttr) {
4591 AttrLoc = NNAttr->getLocation();
4592 CheckKind = SanitizerKind::SO_NonnullAttribute;
4593 Handler = SanitizerHandler::NonnullArg;
4594 } else {
4595 AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
4596 CheckKind = SanitizerKind::SO_NullabilityArg;
4597 Handler = SanitizerHandler::NullabilityArg;
4598 }
4599
4600 SanitizerDebugLocation SanScope(this, {CheckKind}, Handler);
4601 llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
4602 llvm::Constant *StaticData[] = {
4604 EmitCheckSourceLocation(AttrLoc),
4605 llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
4606 };
4607 EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, {});
4608}
4609
4611 SourceLocation ArgLoc,
4612 AbstractCallee AC, unsigned ParmNum) {
4613 if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
4614 SanOpts.has(SanitizerKind::NullabilityArg)))
4615 return;
4616
4617 EmitNonNullArgCheck(RValue::get(Addr, *this), ArgType, ArgLoc, AC, ParmNum);
4618}
4619
4620// Check if the call is going to use the inalloca convention. This needs to
4621// agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged
4622// later, so we can't check it directly.
4623static bool hasInAllocaArgs(CodeGenModule &CGM, CallingConv ExplicitCC,
4624 ArrayRef<QualType> ArgTypes) {
4625 // The Swift calling conventions don't go through the target-specific
4626 // argument classification, they never use inalloca.
4627 // TODO: Consider limiting inalloca use to only calling conventions supported
4628 // by MSVC.
4629 if (ExplicitCC == CC_Swift || ExplicitCC == CC_SwiftAsync)
4630 return false;
4631 if (!CGM.getTarget().getCXXABI().isMicrosoft())
4632 return false;
4633 return llvm::any_of(ArgTypes, [&](QualType Ty) {
4634 return isInAllocaArgument(CGM.getCXXABI(), Ty);
4635 });
4636}
4637
4638#ifndef NDEBUG
4639// Determine whether the given argument is an Objective-C method
4640// that may have type parameters in its signature.
4641static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {
4642 const DeclContext *dc = method->getDeclContext();
4643 if (const ObjCInterfaceDecl *classDecl = dyn_cast<ObjCInterfaceDecl>(dc)) {
4644 return classDecl->getTypeParamListAsWritten();
4645 }
4646
4647 if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {
4648 return catDecl->getTypeParamList();
4649 }
4650
4651 return false;
4652}
4653#endif
4654
4655/// EmitCallArgs - Emit call arguments for a function.
4658 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
4659 AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {
4661
4662 assert((ParamsToSkip == 0 || Prototype.P) &&
4663 "Can't skip parameters if type info is not provided");
4664
4665 // This variable only captures *explicitly* written conventions, not those
4666 // applied by default via command line flags or target defaults, such as
4667 // thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would
4668 // require knowing if this is a C++ instance method or being able to see
4669 // unprototyped FunctionTypes.
4670 CallingConv ExplicitCC = CC_C;
4671
4672 // First, if a prototype was provided, use those argument types.
4673 bool IsVariadic = false;
4674 if (Prototype.P) {
4675 const auto *MD = dyn_cast<const ObjCMethodDecl *>(Prototype.P);
4676 if (MD) {
4677 IsVariadic = MD->isVariadic();
4678 ExplicitCC = getCallingConventionForDecl(
4679 MD, CGM.getTarget().getTriple().isOSWindows());
4680 ArgTypes.assign(MD->param_type_begin() + ParamsToSkip,
4681 MD->param_type_end());
4682 } else {
4683 const auto *FPT = cast<const FunctionProtoType *>(Prototype.P);
4684 IsVariadic = FPT->isVariadic();
4685 ExplicitCC = FPT->getExtInfo().getCC();
4686 ArgTypes.assign(FPT->param_type_begin() + ParamsToSkip,
4687 FPT->param_type_end());
4688 }
4689
4690#ifndef NDEBUG
4691 // Check that the prototyped types match the argument expression types.
4692 bool isGenericMethod = MD && isObjCMethodWithTypeParams(MD);
4693 CallExpr::const_arg_iterator Arg = ArgRange.begin();
4694 for (QualType Ty : ArgTypes) {
4695 assert(Arg != ArgRange.end() && "Running over edge of argument list!");
4696 assert(
4697 (isGenericMethod || Ty->isVariablyModifiedType() ||
4699 getContext()
4700 .getCanonicalType(Ty.getNonReferenceType())
4701 .getTypePtr() ==
4702 getContext().getCanonicalType((*Arg)->getType()).getTypePtr()) &&
4703 "type mismatch in call argument!");
4704 ++Arg;
4705 }
4706
4707 // Either we've emitted all the call args, or we have a call to variadic
4708 // function.
4709 assert((Arg == ArgRange.end() || IsVariadic) &&
4710 "Extra arguments in non-variadic function!");
4711#endif
4712 }
4713
4714 // If we still have any arguments, emit them using the type of the argument.
4715 for (auto *A : llvm::drop_begin(ArgRange, ArgTypes.size()))
4716 ArgTypes.push_back(IsVariadic ? getVarArgType(A) : A->getType());
4717 assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
4718
4719 // We must evaluate arguments from right to left in the MS C++ ABI,
4720 // because arguments are destroyed left to right in the callee. As a special
4721 // case, there are certain language constructs that require left-to-right
4722 // evaluation, and in those cases we consider the evaluation order requirement
4723 // to trump the "destruction order is reverse construction order" guarantee.
4724 bool LeftToRight =
4728
4729 auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,
4730 RValue EmittedArg) {
4731 if (!AC.hasFunctionDecl() || I >= AC.getNumParams())
4732 return;
4733 auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();
4734 if (PS == nullptr)
4735 return;
4736
4737 const auto &Context = getContext();
4738 auto SizeTy = Context.getSizeType();
4739 auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
4740 assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
4741 llvm::Value *V = evaluateOrEmitBuiltinObjectSize(
4742 Arg, PS->getType(), T, EmittedArg.getScalarVal(), PS->isDynamic());
4743 Args.add(RValue::get(V), SizeTy);
4744 // If we're emitting args in reverse, be sure to do so with
4745 // pass_object_size, as well.
4746 if (!LeftToRight)
4747 std::swap(Args.back(), *(&Args.back() - 1));
4748 };
4749
4750 // Insert a stack save if we're going to need any inalloca args.
4751 if (hasInAllocaArgs(CGM, ExplicitCC, ArgTypes)) {
4752 assert(getTarget().getTriple().getArch() == llvm::Triple::x86 &&
4753 "inalloca only supported on x86");
4754 Args.allocateArgumentMemory(*this);
4755 }
4756
4757 // Evaluate each argument in the appropriate order.
4758 size_t CallArgsStart = Args.size();
4759 for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {
4760 unsigned Idx = LeftToRight ? I : E - I - 1;
4761 CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;
4762 unsigned InitialArgSize = Args.size();
4763 // If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of
4764 // the argument and parameter match or the objc method is parameterized.
4765 assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
4766 getContext().hasSameUnqualifiedType((*Arg)->getType(),
4767 ArgTypes[Idx]) ||
4768 (isa<ObjCMethodDecl>(AC.getDecl()) &&
4769 isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&
4770 "Argument and parameter types don't match");
4771 EmitCallArg(Args, *Arg, ArgTypes[Idx]);
4772 // In particular, we depend on it being the last arg in Args, and the
4773 // objectsize bits depend on there only being one arg if !LeftToRight.
4774 assert(InitialArgSize + 1 == Args.size() &&
4775 "The code below depends on only adding one arg per EmitCallArg");
4776 (void)InitialArgSize;
4777 // Since pointer argument are never emitted as LValue, it is safe to emit
4778 // non-null argument check for r-value only.
4779 if (!Args.back().hasLValue()) {
4780 RValue RVArg = Args.back().getKnownRValue();
4781 EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
4782 ParamsToSkip + Idx);
4783 // @llvm.objectsize should never have side-effects and shouldn't need
4784 // destruction/cleanups, so we can safely "emit" it after its arg,
4785 // regardless of right-to-leftness
4786 MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
4787 }
4788 }
4789
4790 if (!LeftToRight) {
4791 // Un-reverse the arguments we just evaluated so they match up with the LLVM
4792 // IR function.
4793 std::reverse(Args.begin() + CallArgsStart, Args.end());
4794
4795 // Reverse the writebacks to match the MSVC ABI.
4796 Args.reverseWritebacks();
4797 }
4798}
4799
4800namespace {
4801
4802struct DestroyUnpassedArg final : EHScopeStack::Cleanup {
4803 DestroyUnpassedArg(Address Addr, QualType Ty) : Addr(Addr), Ty(Ty) {}
4804
4805 Address Addr;
4806 QualType Ty;
4807
4808 void Emit(CodeGenFunction &CGF, Flags flags) override {
4810 if (DtorKind == QualType::DK_cxx_destructor) {
4811 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();
4812 assert(!Dtor->isTrivial());
4813 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,
4814 /*Delegating=*/false, Addr, Ty);
4815 } else {
4817 }
4818 }
4819};
4820
4821} // end anonymous namespace
4822
4824 if (!HasLV)
4825 return RV;
4828 LV.isVolatile());
4829 IsUsed = true;
4830 return RValue::getAggregate(Copy.getAddress());
4831}
4832
4834 LValue Dst = CGF.MakeAddrLValue(Addr, Ty);
4835 if (!HasLV && RV.isScalar())
4836 CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*isInit=*/true);
4837 else if (!HasLV && RV.isComplex())
4838 CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);
4839 else {
4840 auto Addr = HasLV ? LV.getAddress() : RV.getAggregateAddress();
4841 LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);
4842 // We assume that call args are never copied into subobjects.
4844 HasLV ? LV.isVolatileQualified()
4846 }
4847 IsUsed = true;
4848}
4849
4851 for (const auto &I : args.writebacks())
4852 emitWriteback(*this, I);
4853}
4854
4856 QualType type) {
4857 std::optional<DisableDebugLocationUpdates> Dis;
4858 if (isa<CXXDefaultArgExpr>(E))
4859 Dis.emplace(*this);
4860 if (const ObjCIndirectCopyRestoreExpr *CRE =
4861 dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
4862 assert(getLangOpts().ObjCAutoRefCount);
4863 return emitWritebackArg(*this, args, CRE);
4864 }
4865
4866 // Add writeback for HLSLOutParamExpr.
4867 // Needs to be before the assert below because HLSLOutArgExpr is an LValue
4868 // and is not a reference.
4869 if (const HLSLOutArgExpr *OE = dyn_cast<HLSLOutArgExpr>(E)) {
4870 EmitHLSLOutArgExpr(OE, args, type);
4871 return;
4872 }
4873
4874 assert(type->isReferenceType() == E->isGLValue() &&
4875 "reference binding to unmaterialized r-value!");
4876
4877 if (E->isGLValue()) {
4878 assert(E->getObjectKind() == OK_Ordinary);
4879 return args.add(EmitReferenceBindingToExpr(E), type);
4880 }
4881
4882 bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
4883
4884 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
4885 // However, we still have to push an EH-only cleanup in case we unwind before
4886 // we make it to the call.
4887 if (type->isRecordType() &&
4888 type->castAsRecordDecl()->isParamDestroyedInCallee()) {
4889 // If we're using inalloca, use the argument memory. Otherwise, use a
4890 // temporary.
4891 AggValueSlot Slot = args.isUsingInAlloca()
4892 ? createPlaceholderSlot(*this, type)
4893 : CreateAggTemp(type, "agg.tmp");
4894
4895 bool DestroyedInCallee = true, NeedsCleanup = true;
4896 if (const auto *RD = type->getAsCXXRecordDecl())
4897 DestroyedInCallee = RD->hasNonTrivialDestructor();
4898 else
4899 NeedsCleanup = type.isDestructedType();
4900
4901 if (DestroyedInCallee)
4903
4904 EmitAggExpr(E, Slot);
4905 RValue RV = Slot.asRValue();
4906 args.add(RV, type);
4907
4908 if (DestroyedInCallee && NeedsCleanup) {
4909 // Create a no-op GEP between the placeholder and the cleanup so we can
4910 // RAUW it successfully. It also serves as a marker of the first
4911 // instruction where the cleanup is active.
4912 pushFullExprCleanup<DestroyUnpassedArg>(NormalAndEHCleanup,
4913 Slot.getAddress(), type);
4914 // This unreachable is a temporary marker which will be removed later.
4915 llvm::Instruction *IsActive =
4916 Builder.CreateFlagLoad(llvm::Constant::getNullValue(Int8PtrTy));
4918 }
4919 return;
4920 }
4921
4922 if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
4923 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue &&
4924 !type->isArrayParameterType() && !type.isNonTrivialToPrimitiveCopy()) {
4925 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
4926 assert(L.isSimple());
4927 args.addUncopiedAggregate(L, type);
4928 return;
4929 }
4930
4931 args.add(EmitAnyExprToTemp(E), type);
4932}
4933
4934QualType CodeGenFunction::getVarArgType(const Expr *Arg) {
4935 // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC
4936 // implicitly widens null pointer constants that are arguments to varargs
4937 // functions to pointer-sized ints.
4938 if (!getTarget().getTriple().isOSWindows())
4939 return Arg->getType();
4940
4941 if (Arg->getType()->isIntegerType() &&
4942 getContext().getTypeSize(Arg->getType()) <
4943 getContext().getTargetInfo().getPointerWidth(LangAS::Default) &&
4946 return getContext().getIntPtrType();
4947 }
4948
4949 return Arg->getType();
4950}
4951
4952// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
4953// optimizer it can aggressively ignore unwind edges.
4954void CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
4955 if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
4956 !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
4957 Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
4959}
4960
4961/// Emits a call to the given no-arguments nounwind runtime function.
4962llvm::CallInst *
4963CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4964 const llvm::Twine &name) {
4965 return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value *>(), name);
4966}
4967
4968/// Emits a call to the given nounwind runtime function.
4969llvm::CallInst *
4970CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4971 ArrayRef<Address> args,
4972 const llvm::Twine &name) {
4974 for (auto arg : args)
4975 values.push_back(arg.emitRawPointer(*this));
4976 return EmitNounwindRuntimeCall(callee, values, name);
4977}
4978
4979llvm::CallInst *
4980CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,
4982 const llvm::Twine &name) {
4983 llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
4984 call->setDoesNotThrow();
4985 return call;
4986}
4987
4988/// Emits a simple call (never an invoke) to the given no-arguments
4989/// runtime function.
4990llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
4991 const llvm::Twine &name) {
4992 return EmitRuntimeCall(callee, {}, name);
4993}
4994
4995// Calls which may throw must have operand bundles indicating which funclet
4996// they are nested within.
4999 // There is no need for a funclet operand bundle if we aren't inside a
5000 // funclet.
5001 if (!CurrentFuncletPad)
5003
5004 // Skip intrinsics which cannot throw (as long as they don't lower into
5005 // regular function calls in the course of IR transformations).
5006 if (auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) {
5007 if (CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow()) {
5008 auto IID = CalleeFn->getIntrinsicID();
5009 if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID))
5011 }
5012 }
5013
5015 BundleList.emplace_back("funclet", CurrentFuncletPad);
5016 return BundleList;
5017}
5018
5019/// Emits a simple call (never an invoke) to the given runtime function.
5020llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,
5022 const llvm::Twine &name) {
5023 llvm::CallInst *call = Builder.CreateCall(
5024 callee, args, getBundlesForFunclet(callee.getCallee()), name);
5025 call->setCallingConv(getRuntimeCC());
5026
5027 if (CGM.shouldEmitConvergenceTokens() && call->isConvergent())
5028 return cast<llvm::CallInst>(addConvergenceControlToken(call));
5029 return call;
5030}
5031
5032/// Emits a call or invoke to the given noreturn runtime function.
5034 llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) {
5036 getBundlesForFunclet(callee.getCallee());
5037
5038 if (getInvokeDest()) {
5039 llvm::InvokeInst *invoke = Builder.CreateInvoke(
5040 callee, getUnreachableBlock(), getInvokeDest(), args, BundleList);
5041 invoke->setDoesNotReturn();
5042 invoke->setCallingConv(getRuntimeCC());
5043 } else {
5044 llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);
5045 call->setDoesNotReturn();
5046 call->setCallingConv(getRuntimeCC());
5047 Builder.CreateUnreachable();
5048 }
5049}
5050
5051/// Emits a call or invoke instruction to the given nullary runtime function.
5052llvm::CallBase *
5054 const Twine &name) {
5055 return EmitRuntimeCallOrInvoke(callee, {}, name);
5056}
5057
5058/// Emits a call or invoke instruction to the given runtime function.
5059llvm::CallBase *
5062 const Twine &name) {
5063 llvm::CallBase *call = EmitCallOrInvoke(callee, args, name);
5064 call->setCallingConv(getRuntimeCC());
5065 return call;
5066}
5067
5068/// Emits a call or invoke instruction to the given function, depending
5069/// on the current state of the EH stack.
5070llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee,
5072 const Twine &Name) {
5073 llvm::BasicBlock *InvokeDest = getInvokeDest();
5075 getBundlesForFunclet(Callee.getCallee());
5076
5077 llvm::CallBase *Inst;
5078 if (!InvokeDest)
5079 Inst = Builder.CreateCall(Callee, Args, BundleList, Name);
5080 else {
5081 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
5082 Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
5083 Name);
5084 EmitBlock(ContBB);
5085 }
5086
5087 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
5088 // optimizer it can aggressively ignore unwind edges.
5089 if (CGM.getLangOpts().ObjCAutoRefCount)
5090 AddObjCARCExceptionMetadata(Inst);
5091
5092 return Inst;
5093}
5094
5095void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
5096 llvm::Value *New) {
5097 DeferredReplacements.push_back(
5098 std::make_pair(llvm::WeakTrackingVH(Old), New));
5099}
5100
5101namespace {
5102
5103/// Specify given \p NewAlign as the alignment of return value attribute. If
5104/// such attribute already exists, re-set it to the maximal one of two options.
5105[[nodiscard]] llvm::AttributeList
5106maybeRaiseRetAlignmentAttribute(llvm::LLVMContext &Ctx,
5107 const llvm::AttributeList &Attrs,
5108 llvm::Align NewAlign) {
5109 llvm::Align CurAlign = Attrs.getRetAlignment().valueOrOne();
5110 if (CurAlign >= NewAlign)
5111 return Attrs;
5112 llvm::Attribute AlignAttr = llvm::Attribute::getWithAlignment(Ctx, NewAlign);
5113 return Attrs.removeRetAttribute(Ctx, llvm::Attribute::AttrKind::Alignment)
5114 .addRetAttribute(Ctx, AlignAttr);
5115}
5116
5117template <typename AlignedAttrTy> class AbstractAssumeAlignedAttrEmitter {
5118protected:
5119 CodeGenFunction &CGF;
5120
5121 /// We do nothing if this is, or becomes, nullptr.
5122 const AlignedAttrTy *AA = nullptr;
5123
5124 llvm::Value *Alignment = nullptr; // May or may not be a constant.
5125 llvm::ConstantInt *OffsetCI = nullptr; // Constant, hopefully zero.
5126
5127 AbstractAssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
5128 : CGF(CGF_) {
5129 if (!FuncDecl)
5130 return;
5131 AA = FuncDecl->getAttr<AlignedAttrTy>();
5132 }
5133
5134public:
5135 /// If we can, materialize the alignment as an attribute on return value.
5136 [[nodiscard]] llvm::AttributeList
5137 TryEmitAsCallSiteAttribute(const llvm::AttributeList &Attrs) {
5138 if (!AA || OffsetCI || CGF.SanOpts.has(SanitizerKind::Alignment))
5139 return Attrs;
5140 const auto *AlignmentCI = dyn_cast<llvm::ConstantInt>(Alignment);
5141 if (!AlignmentCI)
5142 return Attrs;
5143 // We may legitimately have non-power-of-2 alignment here.
5144 // If so, this is UB land, emit it via `@llvm.assume` instead.
5145 if (!AlignmentCI->getValue().isPowerOf2())
5146 return Attrs;
5147 llvm::AttributeList NewAttrs = maybeRaiseRetAlignmentAttribute(
5148 CGF.getLLVMContext(), Attrs,
5149 llvm::Align(
5150 AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment)));
5151 AA = nullptr; // We're done. Disallow doing anything else.
5152 return NewAttrs;
5153 }
5154
5155 /// Emit alignment assumption.
5156 /// This is a general fallback that we take if either there is an offset,
5157 /// or the alignment is variable or we are sanitizing for alignment.
5158 void EmitAsAnAssumption(SourceLocation Loc, QualType RetTy, RValue &Ret) {
5159 if (!AA)
5160 return;
5161 CGF.emitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc,
5162 AA->getLocation(), Alignment, OffsetCI);
5163 AA = nullptr; // We're done. Disallow doing anything else.
5164 }
5165};
5166
5167/// Helper data structure to emit `AssumeAlignedAttr`.
5168class AssumeAlignedAttrEmitter final
5169 : public AbstractAssumeAlignedAttrEmitter<AssumeAlignedAttr> {
5170public:
5171 AssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)
5172 : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
5173 if (!AA)
5174 return;
5175 // It is guaranteed that the alignment/offset are constants.
5176 Alignment = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AA->getAlignment()));
5177 if (Expr *Offset = AA->getOffset()) {
5178 OffsetCI = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(Offset));
5179 if (OffsetCI->isNullValue()) // Canonicalize zero offset to no offset.
5180 OffsetCI = nullptr;
5181 }
5182 }
5183};
5184
5185/// Helper data structure to emit `AllocAlignAttr`.
5186class AllocAlignAttrEmitter final
5187 : public AbstractAssumeAlignedAttrEmitter<AllocAlignAttr> {
5188public:
5189 AllocAlignAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl,
5190 const CallArgList &CallArgs)
5191 : AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {
5192 if (!AA)
5193 return;
5194 // Alignment may or may not be a constant, and that is okay.
5195 Alignment = CallArgs[AA->getParamIndex().getLLVMIndex()]
5196 .getRValue(CGF)
5197 .getScalarVal();
5198 }
5199};
5200
5201} // namespace
5202
5203static unsigned getMaxVectorWidth(const llvm::Type *Ty) {
5204 if (auto *VT = dyn_cast<llvm::VectorType>(Ty))
5205 return VT->getPrimitiveSizeInBits().getKnownMinValue();
5206 if (auto *AT = dyn_cast<llvm::ArrayType>(Ty))
5207 return getMaxVectorWidth(AT->getElementType());
5208
5209 unsigned MaxVectorWidth = 0;
5210 if (auto *ST = dyn_cast<llvm::StructType>(Ty))
5211 for (auto *I : ST->elements())
5212 MaxVectorWidth = std::max(MaxVectorWidth, getMaxVectorWidth(I));
5213 return MaxVectorWidth;
5214}
5215
5217 const CGCallee &Callee,
5218 ReturnValueSlot ReturnValue,
5219 const CallArgList &CallArgs,
5220 llvm::CallBase **callOrInvoke, bool IsMustTail,
5222 bool IsVirtualFunctionPointerThunk) {
5223 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
5224
5225 assert(Callee.isOrdinary() || Callee.isVirtual());
5226
5227 // Handle struct-return functions by passing a pointer to the
5228 // location that we would like to return into.
5229 QualType RetTy = CallInfo.getReturnType();
5230 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
5231
5232 llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);
5233
5234 const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();
5235 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
5236 // We can only guarantee that a function is called from the correct
5237 // context/function based on the appropriate target attributes,
5238 // so only check in the case where we have both always_inline and target
5239 // since otherwise we could be making a conditional call after a check for
5240 // the proper cpu features (and it won't cause code generation issues due to
5241 // function based code generation).
5242 if ((TargetDecl->hasAttr<AlwaysInlineAttr>() &&
5243 (TargetDecl->hasAttr<TargetAttr>() ||
5244 (CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>()))) ||
5245 (CurFuncDecl && CurFuncDecl->hasAttr<FlattenAttr>() &&
5246 (CurFuncDecl->hasAttr<TargetAttr>() ||
5247 TargetDecl->hasAttr<TargetAttr>())))
5249 }
5250
5251 // Some architectures (such as x86-64) have the ABI changed based on
5252 // attribute-target/features. Give them a chance to diagnose.
5253 const FunctionDecl *CallerDecl = dyn_cast_or_null<FunctionDecl>(CurCodeDecl);
5254 const FunctionDecl *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl);
5256 CalleeDecl, CallArgs, RetTy);
5257
5258 // 1. Set up the arguments.
5259
5260 // If we're using inalloca, insert the allocation after the stack save.
5261 // FIXME: Do this earlier rather than hacking it in here!
5262 RawAddress ArgMemory = RawAddress::invalid();
5263 if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {
5264 const llvm::DataLayout &DL = CGM.getDataLayout();
5265 llvm::Instruction *IP = CallArgs.getStackBase();
5266 llvm::AllocaInst *AI;
5267 if (IP) {
5268 IP = IP->getNextNode();
5269 AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(), "argmem",
5270 IP->getIterator());
5271 } else {
5272 AI = CreateTempAlloca(ArgStruct, "argmem");
5273 }
5274 auto Align = CallInfo.getArgStructAlignment();
5275 AI->setAlignment(Align.getAsAlign());
5276 AI->setUsedWithInAlloca(true);
5277 assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
5278 ArgMemory = RawAddress(AI, ArgStruct, Align);
5279 }
5280
5281 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
5282 SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());
5283
5284 // If the call returns a temporary with struct return, create a temporary
5285 // alloca to hold the result, unless one is given to us.
5286 Address SRetPtr = Address::invalid();
5287 bool NeedSRetLifetimeEnd = false;
5288 if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
5289 // For virtual function pointer thunks and musttail calls, we must always
5290 // forward an incoming SRet pointer to the callee, because a local alloca
5291 // would be de-allocated before the call. These cases both guarantee that
5292 // there will be an incoming SRet argument of the correct type.
5293 if ((IsVirtualFunctionPointerThunk || IsMustTail) && RetAI.isIndirect()) {
5294 SRetPtr = makeNaturalAddressForPointer(CurFn->arg_begin() +
5295 IRFunctionArgs.getSRetArgNo(),
5296 RetTy, CharUnits::fromQuantity(1));
5297 } else if (!ReturnValue.isNull()) {
5298 SRetPtr = ReturnValue.getAddress();
5299 } else {
5300 SRetPtr = CreateMemTempWithoutCast(RetTy, "tmp");
5301 if (HaveInsertPoint() && ReturnValue.isUnused())
5302 NeedSRetLifetimeEnd = EmitLifetimeStart(SRetPtr.getBasePointer());
5303 }
5304 if (IRFunctionArgs.hasSRetArg()) {
5305 // A mismatch between the allocated return value's AS and the target's
5306 // chosen IndirectAS can happen e.g. when passing the this pointer through
5307 // a chain involving stores to / loads from the DefaultAS; we address this
5308 // here, symmetrically with the handling we have for normal pointer args.
5309 if (SRetPtr.getAddressSpace() != RetAI.getIndirectAddrSpace()) {
5310 llvm::Value *V = SRetPtr.getBasePointer();
5312 llvm::Type *Ty = llvm::PointerType::get(getLLVMContext(),
5313 RetAI.getIndirectAddrSpace());
5314
5315 SRetPtr = SRetPtr.withPointer(
5316 getTargetHooks().performAddrSpaceCast(*this, V, SAS, Ty, true),
5317 SRetPtr.isKnownNonNull());
5318 }
5319 IRCallArgs[IRFunctionArgs.getSRetArgNo()] =
5320 getAsNaturalPointerTo(SRetPtr, RetTy);
5321 } else if (RetAI.isInAlloca()) {
5322 Address Addr =
5323 Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());
5325 }
5326 }
5327
5328 RawAddress swiftErrorTemp = RawAddress::invalid();
5329 Address swiftErrorArg = Address::invalid();
5330
5331 // When passing arguments using temporary allocas, we need to add the
5332 // appropriate lifetime markers. This vector keeps track of all the lifetime
5333 // markers that need to be ended right after the call.
5334 SmallVector<CallLifetimeEnd, 2> CallLifetimeEndAfterCall;
5335
5336 // Translate all of the arguments as necessary to match the IR lowering.
5337 assert(CallInfo.arg_size() == CallArgs.size() &&
5338 "Mismatch between function signature & arguments.");
5339 unsigned ArgNo = 0;
5340 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
5341 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
5342 I != E; ++I, ++info_it, ++ArgNo) {
5343 const ABIArgInfo &ArgInfo = info_it->info;
5344
5345 // Insert a padding argument to ensure proper alignment.
5346 if (IRFunctionArgs.hasPaddingArg(ArgNo))
5347 IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
5348 llvm::UndefValue::get(ArgInfo.getPaddingType());
5349
5350 unsigned FirstIRArg, NumIRArgs;
5351 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
5352
5353 bool ArgHasMaybeUndefAttr =
5354 IsArgumentMaybeUndef(TargetDecl, CallInfo.getNumRequiredArgs(), ArgNo);
5355
5356 switch (ArgInfo.getKind()) {
5357 case ABIArgInfo::InAlloca: {
5358 assert(NumIRArgs == 0);
5359 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
5360 if (I->isAggregate()) {
5361 RawAddress Addr = I->hasLValue()
5362 ? I->getKnownLValue().getAddress()
5363 : I->getKnownRValue().getAggregateAddress();
5364 llvm::Instruction *Placeholder =
5365 cast<llvm::Instruction>(Addr.getPointer());
5366
5367 if (!ArgInfo.getInAllocaIndirect()) {
5368 // Replace the placeholder with the appropriate argument slot GEP.
5369 CGBuilderTy::InsertPoint IP = Builder.saveIP();
5370 Builder.SetInsertPoint(Placeholder);
5371 Addr = Builder.CreateStructGEP(ArgMemory,
5372 ArgInfo.getInAllocaFieldIndex());
5373 Builder.restoreIP(IP);
5374 } else {
5375 // For indirect things such as overaligned structs, replace the
5376 // placeholder with a regular aggregate temporary alloca. Store the
5377 // address of this alloca into the struct.
5378 Addr = CreateMemTemp(info_it->type, "inalloca.indirect.tmp");
5380 ArgMemory, ArgInfo.getInAllocaFieldIndex());
5381 Builder.CreateStore(Addr.getPointer(), ArgSlot);
5382 }
5383 deferPlaceholderReplacement(Placeholder, Addr.getPointer());
5384 } else if (ArgInfo.getInAllocaIndirect()) {
5385 // Make a temporary alloca and store the address of it into the argument
5386 // struct.
5388 I->Ty, getContext().getTypeAlignInChars(I->Ty),
5389 "indirect-arg-temp");
5390 I->copyInto(*this, Addr);
5391 Address ArgSlot =
5392 Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
5393 Builder.CreateStore(Addr.getPointer(), ArgSlot);
5394 } else {
5395 // Store the RValue into the argument struct.
5396 Address Addr =
5397 Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());
5398 Addr = Addr.withElementType(ConvertTypeForMem(I->Ty));
5399 I->copyInto(*this, Addr);
5400 }
5401 break;
5402 }
5403
5406 assert(NumIRArgs == 1);
5407 if (I->isAggregate()) {
5408 // We want to avoid creating an unnecessary temporary+copy here;
5409 // however, we need one in three cases:
5410 // 1. If the argument is not byval, and we are required to copy the
5411 // source. (This case doesn't occur on any common architecture.)
5412 // 2. If the argument is byval, RV is not sufficiently aligned, and
5413 // we cannot force it to be sufficiently aligned.
5414 // 3. If the argument is byval, but RV is not located in default
5415 // or alloca address space.
5416 Address Addr = I->hasLValue()
5417 ? I->getKnownLValue().getAddress()
5418 : I->getKnownRValue().getAggregateAddress();
5419 CharUnits Align = ArgInfo.getIndirectAlign();
5420 const llvm::DataLayout *TD = &CGM.getDataLayout();
5421
5422 assert((FirstIRArg >= IRFuncTy->getNumParams() ||
5423 IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==
5424 TD->getAllocaAddrSpace()) &&
5425 "indirect argument must be in alloca address space");
5426
5427 bool NeedCopy = false;
5428 if (Addr.getAlignment() < Align &&
5429 llvm::getOrEnforceKnownAlignment(Addr.emitRawPointer(*this),
5430 Align.getAsAlign(),
5431 *TD) < Align.getAsAlign()) {
5432 NeedCopy = true;
5433 } else if (I->hasLValue()) {
5434 auto LV = I->getKnownLValue();
5435
5436 bool isByValOrRef =
5437 ArgInfo.isIndirectAliased() || ArgInfo.getIndirectByVal();
5438
5439 if (!isByValOrRef ||
5440 (LV.getAlignment() < getContext().getTypeAlignInChars(I->Ty))) {
5441 NeedCopy = true;
5442 }
5443
5444 if (isByValOrRef && Addr.getType()->getAddressSpace() !=
5445 ArgInfo.getIndirectAddrSpace()) {
5446 NeedCopy = true;
5447 }
5448 }
5449
5450 if (!NeedCopy) {
5451 // Skip the extra memcpy call.
5452 llvm::Value *V = getAsNaturalPointerTo(Addr, I->Ty);
5453 auto *T = llvm::PointerType::get(CGM.getLLVMContext(),
5454 ArgInfo.getIndirectAddrSpace());
5455
5456 // FIXME: This should not depend on the language address spaces, and
5457 // only the contextual values. If the address space mismatches, see if
5458 // we can look through a cast to a compatible address space value,
5459 // otherwise emit a copy.
5460 llvm::Value *Val = getTargetHooks().performAddrSpaceCast(
5461 *this, V, I->Ty.getAddressSpace(), T, true);
5462 if (ArgHasMaybeUndefAttr)
5463 Val = Builder.CreateFreeze(Val);
5464 IRCallArgs[FirstIRArg] = Val;
5465 break;
5466 }
5467 } else if (I->getType()->isArrayParameterType()) {
5468 // Don't produce a temporary for ArrayParameterType arguments.
5469 // ArrayParameterType arguments are only created from
5470 // HLSL_ArrayRValue casts and HLSLOutArgExpr expressions, both
5471 // of which create temporaries already. This allows us to just use the
5472 // scalar for the decayed array pointer as the argument directly.
5473 IRCallArgs[FirstIRArg] = I->getKnownRValue().getScalarVal();
5474 break;
5475 }
5476
5477 // For non-aggregate args and aggregate args meeting conditions above
5478 // we need to create an aligned temporary, and copy to it.
5480 I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");
5481 llvm::Value *Val = getAsNaturalPointerTo(AI, I->Ty);
5482 if (ArgHasMaybeUndefAttr)
5483 Val = Builder.CreateFreeze(Val);
5484 IRCallArgs[FirstIRArg] = Val;
5485
5486 // Emit lifetime markers for the temporary alloca and add cleanup code to
5487 // emit the end lifetime marker after the call.
5488 if (EmitLifetimeStart(AI.getPointer()))
5489 CallLifetimeEndAfterCall.emplace_back(AI);
5490
5491 // Generate the copy.
5492 I->copyInto(*this, AI);
5493 break;
5494 }
5495
5496 case ABIArgInfo::Ignore:
5497 assert(NumIRArgs == 0);
5498 break;
5499
5500 case ABIArgInfo::Extend:
5501 case ABIArgInfo::Direct: {
5502 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
5503 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
5504 ArgInfo.getDirectOffset() == 0) {
5505 assert(NumIRArgs == 1);
5506 llvm::Value *V;
5507 if (!I->isAggregate())
5508 V = I->getKnownRValue().getScalarVal();
5509 else
5511 I->hasLValue() ? I->getKnownLValue().getAddress()
5512 : I->getKnownRValue().getAggregateAddress());
5513
5514 // Implement swifterror by copying into a new swifterror argument.
5515 // We'll write back in the normal path out of the call.
5516 if (CallInfo.getExtParameterInfo(ArgNo).getABI() ==
5518 assert(!swiftErrorTemp.isValid() && "multiple swifterror args");
5519
5520 QualType pointeeTy = I->Ty->getPointeeType();
5521 swiftErrorArg = makeNaturalAddressForPointer(
5522 V, pointeeTy, getContext().getTypeAlignInChars(pointeeTy));
5523
5524 swiftErrorTemp =
5525 CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");
5526 V = swiftErrorTemp.getPointer();
5527 cast<llvm::AllocaInst>(V)->setSwiftError(true);
5528
5529 llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);
5530 Builder.CreateStore(errorValue, swiftErrorTemp);
5531 }
5532
5533 // We might have to widen integers, but we should never truncate.
5534 if (ArgInfo.getCoerceToType() != V->getType() &&
5535 V->getType()->isIntegerTy())
5536 V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
5537
5538 // The only plausible mismatch here would be for pointer address spaces.
5539 // We assume that the target has a reasonable mapping for the DefaultAS
5540 // (it can be casted to from incoming specific ASes), and insert an AS
5541 // cast to address the mismatch.
5542 if (FirstIRArg < IRFuncTy->getNumParams() &&
5543 V->getType() != IRFuncTy->getParamType(FirstIRArg)) {
5544 assert(V->getType()->isPointerTy() && "Only pointers can mismatch!");
5545 auto ActualAS = I->Ty.getAddressSpace();
5547 *this, V, ActualAS, IRFuncTy->getParamType(FirstIRArg));
5548 }
5549
5550 if (ArgHasMaybeUndefAttr)
5551 V = Builder.CreateFreeze(V);
5552 IRCallArgs[FirstIRArg] = V;
5553 break;
5554 }
5555
5556 llvm::StructType *STy =
5557 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
5558
5559 // FIXME: Avoid the conversion through memory if possible.
5560 Address Src = Address::invalid();
5561 if (!I->isAggregate()) {
5562 Src = CreateMemTemp(I->Ty, "coerce");
5563 I->copyInto(*this, Src);
5564 } else {
5565 Src = I->hasLValue() ? I->getKnownLValue().getAddress()
5566 : I->getKnownRValue().getAggregateAddress();
5567 }
5568
5569 // If the value is offset in memory, apply the offset now.
5570 Src = emitAddressAtOffset(*this, Src, ArgInfo);
5571
5572 // Fast-isel and the optimizer generally like scalar values better than
5573 // FCAs, so we flatten them if this is safe to do for this argument.
5574 if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
5575 llvm::Type *SrcTy = Src.getElementType();
5576 llvm::TypeSize SrcTypeSize =
5577 CGM.getDataLayout().getTypeAllocSize(SrcTy);
5578 llvm::TypeSize DstTypeSize = CGM.getDataLayout().getTypeAllocSize(STy);
5579 if (SrcTypeSize.isScalable()) {
5580 assert(STy->containsHomogeneousScalableVectorTypes() &&
5581 "ABI only supports structure with homogeneous scalable vector "
5582 "type");
5583 assert(SrcTypeSize == DstTypeSize &&
5584 "Only allow non-fractional movement of structure with "
5585 "homogeneous scalable vector type");
5586 assert(NumIRArgs == STy->getNumElements());
5587
5588 llvm::Value *StoredStructValue =
5589 Builder.CreateLoad(Src, Src.getName() + ".tuple");
5590 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5591 llvm::Value *Extract = Builder.CreateExtractValue(
5592 StoredStructValue, i, Src.getName() + ".extract" + Twine(i));
5593 IRCallArgs[FirstIRArg + i] = Extract;
5594 }
5595 } else {
5596 uint64_t SrcSize = SrcTypeSize.getFixedValue();
5597 uint64_t DstSize = DstTypeSize.getFixedValue();
5598
5599 // If the source type is smaller than the destination type of the
5600 // coerce-to logic, copy the source value into a temp alloca the size
5601 // of the destination type to allow loading all of it. The bits past
5602 // the source value are left undef.
5603 if (SrcSize < DstSize) {
5604 Address TempAlloca = CreateTempAlloca(STy, Src.getAlignment(),
5605 Src.getName() + ".coerce");
5606 Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
5607 Src = TempAlloca;
5608 } else {
5609 Src = Src.withElementType(STy);
5610 }
5611
5612 assert(NumIRArgs == STy->getNumElements());
5613 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5614 Address EltPtr = Builder.CreateStructGEP(Src, i);
5615 llvm::Value *LI = Builder.CreateLoad(EltPtr);
5616 if (ArgHasMaybeUndefAttr)
5617 LI = Builder.CreateFreeze(LI);
5618 IRCallArgs[FirstIRArg + i] = LI;
5619 }
5620 }
5621 } else {
5622 // In the simple case, just pass the coerced loaded value.
5623 assert(NumIRArgs == 1);
5624 llvm::Value *Load =
5625 CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);
5626
5627 if (CallInfo.isCmseNSCall()) {
5628 // For certain parameter types, clear padding bits, as they may reveal
5629 // sensitive information.
5630 // Small struct/union types are passed as integer arrays.
5631 auto *ATy = dyn_cast<llvm::ArrayType>(Load->getType());
5632 if (ATy != nullptr && isa<RecordType>(I->Ty.getCanonicalType()))
5633 Load = EmitCMSEClearRecord(Load, ATy, I->Ty);
5634 }
5635
5636 if (ArgHasMaybeUndefAttr)
5637 Load = Builder.CreateFreeze(Load);
5638 IRCallArgs[FirstIRArg] = Load;
5639 }
5640
5641 break;
5642 }
5643
5645 auto coercionType = ArgInfo.getCoerceAndExpandType();
5646 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
5647 auto unpaddedCoercionType = ArgInfo.getUnpaddedCoerceAndExpandType();
5648 auto *unpaddedStruct = dyn_cast<llvm::StructType>(unpaddedCoercionType);
5649
5650 Address addr = Address::invalid();
5651 RawAddress AllocaAddr = RawAddress::invalid();
5652 bool NeedLifetimeEnd = false;
5653 if (I->isAggregate()) {
5654 addr = I->hasLValue() ? I->getKnownLValue().getAddress()
5655 : I->getKnownRValue().getAggregateAddress();
5656
5657 } else {
5658 RValue RV = I->getKnownRValue();
5659 assert(RV.isScalar()); // complex should always just be direct
5660
5661 llvm::Type *scalarType = RV.getScalarVal()->getType();
5662 auto scalarAlign = CGM.getDataLayout().getPrefTypeAlign(scalarType);
5663
5664 // Materialize to a temporary.
5665 addr = CreateTempAlloca(RV.getScalarVal()->getType(),
5666 CharUnits::fromQuantity(std::max(
5667 layout->getAlignment(), scalarAlign)),
5668 "tmp",
5669 /*ArraySize=*/nullptr, &AllocaAddr);
5670 NeedLifetimeEnd = EmitLifetimeStart(AllocaAddr.getPointer());
5671
5672 Builder.CreateStore(RV.getScalarVal(), addr);
5673 }
5674
5675 addr = addr.withElementType(coercionType);
5676
5677 unsigned IRArgPos = FirstIRArg;
5678 unsigned unpaddedIndex = 0;
5679 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
5680 llvm::Type *eltType = coercionType->getElementType(i);
5682 continue;
5683 Address eltAddr = Builder.CreateStructGEP(addr, i);
5684 llvm::Value *elt = CreateCoercedLoad(
5685 eltAddr,
5686 unpaddedStruct ? unpaddedStruct->getElementType(unpaddedIndex++)
5687 : unpaddedCoercionType,
5688 *this);
5689 if (ArgHasMaybeUndefAttr)
5690 elt = Builder.CreateFreeze(elt);
5691 IRCallArgs[IRArgPos++] = elt;
5692 }
5693 assert(IRArgPos == FirstIRArg + NumIRArgs);
5694
5695 if (NeedLifetimeEnd)
5696 EmitLifetimeEnd(AllocaAddr.getPointer());
5697 break;
5698 }
5699
5700 case ABIArgInfo::Expand: {
5701 unsigned IRArgPos = FirstIRArg;
5702 ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);
5703 assert(IRArgPos == FirstIRArg + NumIRArgs);
5704 break;
5705 }
5706
5708 Address Src = Address::invalid();
5709 if (!I->isAggregate()) {
5710 Src = CreateMemTemp(I->Ty, "target_coerce");
5711 I->copyInto(*this, Src);
5712 } else {
5713 Src = I->hasLValue() ? I->getKnownLValue().getAddress()
5714 : I->getKnownRValue().getAggregateAddress();
5715 }
5716
5717 // If the value is offset in memory, apply the offset now.
5718 Src = emitAddressAtOffset(*this, Src, ArgInfo);
5719 llvm::Value *Load =
5720 CGM.getABIInfo().createCoercedLoad(Src, ArgInfo, *this);
5721 IRCallArgs[FirstIRArg] = Load;
5722 break;
5723 }
5724 }
5725 }
5726
5727 const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);
5728 llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();
5729
5730 // If we're using inalloca, set up that argument.
5731 if (ArgMemory.isValid()) {
5732 llvm::Value *Arg = ArgMemory.getPointer();
5733 assert(IRFunctionArgs.hasInallocaArg());
5734 IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
5735 }
5736
5737 // 2. Prepare the function pointer.
5738
5739 // If the callee is a bitcast of a non-variadic function to have a
5740 // variadic function pointer type, check to see if we can remove the
5741 // bitcast. This comes up with unprototyped functions.
5742 //
5743 // This makes the IR nicer, but more importantly it ensures that we
5744 // can inline the function at -O0 if it is marked always_inline.
5745 auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT,
5746 llvm::Value *Ptr) -> llvm::Function * {
5747 if (!CalleeFT->isVarArg())
5748 return nullptr;
5749
5750 // Get underlying value if it's a bitcast
5751 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) {
5752 if (CE->getOpcode() == llvm::Instruction::BitCast)
5753 Ptr = CE->getOperand(0);
5754 }
5755
5756 llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr);
5757 if (!OrigFn)
5758 return nullptr;
5759
5760 llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
5761
5762 // If the original type is variadic, or if any of the component types
5763 // disagree, we cannot remove the cast.
5764 if (OrigFT->isVarArg() ||
5765 OrigFT->getNumParams() != CalleeFT->getNumParams() ||
5766 OrigFT->getReturnType() != CalleeFT->getReturnType())
5767 return nullptr;
5768
5769 for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
5770 if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
5771 return nullptr;
5772
5773 return OrigFn;
5774 };
5775
5776 if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) {
5777 CalleePtr = OrigFn;
5778 IRFuncTy = OrigFn->getFunctionType();
5779 }
5780
5781 // 3. Perform the actual call.
5782
5783 // Deactivate any cleanups that we're supposed to do immediately before
5784 // the call.
5785 if (!CallArgs.getCleanupsToDeactivate().empty())
5786 deactivateArgCleanupsBeforeCall(*this, CallArgs);
5787
5788 // Update the largest vector width if any arguments have vector types.
5789 for (unsigned i = 0; i < IRCallArgs.size(); ++i)
5790 LargestVectorWidth = std::max(LargestVectorWidth,
5791 getMaxVectorWidth(IRCallArgs[i]->getType()));
5792
5793 // Compute the calling convention and attributes.
5794 unsigned CallingConv;
5795 llvm::AttributeList Attrs;
5796 CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
5797 Callee.getAbstractInfo(), Attrs, CallingConv,
5798 /*AttrOnCallSite=*/true,
5799 /*IsThunk=*/false);
5800
5801 if (CallingConv == llvm::CallingConv::X86_VectorCall &&
5802 getTarget().getTriple().isWindowsArm64EC()) {
5803 CGM.Error(Loc, "__vectorcall calling convention is not currently "
5804 "supported");
5805 }
5806
5807 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
5808 if (FD->hasAttr<StrictFPAttr>())
5809 // All calls within a strictfp function are marked strictfp
5810 Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
5811
5812 // If -ffast-math is enabled and the function is guarded by an
5813 // '__attribute__((optnone)) adjust the memory attribute so the BE emits the
5814 // library call instead of the intrinsic.
5815 if (FD->hasAttr<OptimizeNoneAttr>() && getLangOpts().FastMath)
5816 CGM.AdjustMemoryAttribute(CalleePtr->getName(), Callee.getAbstractInfo(),
5817 Attrs);
5818 }
5819 // Add call-site nomerge attribute if exists.
5821 Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge);
5822
5823 // Add call-site noinline attribute if exists.
5825 Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
5826
5827 // Add call-site always_inline attribute if exists.
5828 // Note: This corresponds to the [[clang::always_inline]] statement attribute.
5831 CallerDecl, CalleeDecl))
5832 Attrs =
5833 Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
5834
5835 // Remove call-site convergent attribute if requested.
5837 Attrs =
5838 Attrs.removeFnAttribute(getLLVMContext(), llvm::Attribute::Convergent);
5839
5840 // Apply some call-site-specific attributes.
5841 // TODO: work this into building the attribute set.
5842
5843 // Apply always_inline to all calls within flatten functions.
5844 // FIXME: should this really take priority over __try, below?
5845 if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
5847 !(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>()) &&
5849 CallerDecl, CalleeDecl)) {
5850 Attrs =
5851 Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);
5852 }
5853
5854 // Disable inlining inside SEH __try blocks.
5855 if (isSEHTryScope()) {
5856 Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);
5857 }
5858
5859 // Decide whether to use a call or an invoke.
5860 bool CannotThrow;
5862 // SEH cares about asynchronous exceptions, so everything can "throw."
5863 CannotThrow = false;
5864 } else if (isCleanupPadScope() &&
5866 // The MSVC++ personality will implicitly terminate the program if an
5867 // exception is thrown during a cleanup outside of a try/catch.
5868 // We don't need to model anything in IR to get this behavior.
5869 CannotThrow = true;
5870 } else {
5871 // Otherwise, nounwind call sites will never throw.
5872 CannotThrow = Attrs.hasFnAttr(llvm::Attribute::NoUnwind);
5873
5874 if (auto *FPtr = dyn_cast<llvm::Function>(CalleePtr))
5875 if (FPtr->hasFnAttribute(llvm::Attribute::NoUnwind))
5876 CannotThrow = true;
5877 }
5878
5879 // If we made a temporary, be sure to clean up after ourselves. Note that we
5880 // can't depend on being inside of an ExprWithCleanups, so we need to manually
5881 // pop this cleanup later on. Being eager about this is OK, since this
5882 // temporary is 'invisible' outside of the callee.
5883 if (NeedSRetLifetimeEnd)
5884 pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetPtr);
5885
5886 llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
5887
5889 getBundlesForFunclet(CalleePtr);
5890
5891 if (SanOpts.has(SanitizerKind::KCFI) &&
5892 !isa_and_nonnull<FunctionDecl>(TargetDecl))
5893 EmitKCFIOperandBundle(ConcreteCallee, BundleList);
5894
5895 // Add the pointer-authentication bundle.
5896 EmitPointerAuthOperandBundle(ConcreteCallee.getPointerAuthInfo(), BundleList);
5897
5898 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
5899 if (FD->hasAttr<StrictFPAttr>())
5900 // All calls within a strictfp function are marked strictfp
5901 Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);
5902
5903 AssumeAlignedAttrEmitter AssumeAlignedAttrEmitter(*this, TargetDecl);
5904 Attrs = AssumeAlignedAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
5905
5906 AllocAlignAttrEmitter AllocAlignAttrEmitter(*this, TargetDecl, CallArgs);
5907 Attrs = AllocAlignAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);
5908
5909 // Emit the actual call/invoke instruction.
5910 llvm::CallBase *CI;
5911 if (!InvokeDest) {
5912 CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList);
5913 } else {
5914 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
5915 CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs,
5916 BundleList);
5917 EmitBlock(Cont);
5918 }
5919 if (CI->getCalledFunction() && CI->getCalledFunction()->hasName() &&
5920 CI->getCalledFunction()->getName().starts_with("_Z4sqrt")) {
5922 }
5923 if (callOrInvoke)
5924 *callOrInvoke = CI;
5925
5926 // If this is within a function that has the guard(nocf) attribute and is an
5927 // indirect call, add the "guard_nocf" attribute to this call to indicate that
5928 // Control Flow Guard checks should not be added, even if the call is inlined.
5929 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
5930 if (const auto *A = FD->getAttr<CFGuardAttr>()) {
5931 if (A->getGuard() == CFGuardAttr::GuardArg::nocf &&
5932 !CI->getCalledFunction())
5933 Attrs = Attrs.addFnAttribute(getLLVMContext(), "guard_nocf");
5934 }
5935 }
5936
5937 // Apply the attributes and calling convention.
5938 CI->setAttributes(Attrs);
5939 CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
5940
5941 // Apply various metadata.
5942
5943 if (!CI->getType()->isVoidTy())
5944 CI->setName("call");
5945
5946 if (CGM.shouldEmitConvergenceTokens() && CI->isConvergent())
5947 CI = addConvergenceControlToken(CI);
5948
5949 // Update largest vector width from the return type.
5950 LargestVectorWidth =
5951 std::max(LargestVectorWidth, getMaxVectorWidth(CI->getType()));
5952
5953 // Insert instrumentation or attach profile metadata at indirect call sites.
5954 // For more details, see the comment before the definition of
5955 // IPVK_IndirectCallTarget in InstrProfData.inc.
5956 if (!CI->getCalledFunction())
5957 PGO->valueProfile(Builder, llvm::IPVK_IndirectCallTarget, CI, CalleePtr);
5958
5959 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
5960 // optimizer it can aggressively ignore unwind edges.
5961 if (CGM.getLangOpts().ObjCAutoRefCount)
5962 AddObjCARCExceptionMetadata(CI);
5963
5964 // Set tail call kind if necessary.
5965 if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
5966 if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
5967 Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
5968 else if (IsMustTail) {
5969 if (getTarget().getTriple().isPPC()) {
5970 if (getTarget().getTriple().isOSAIX())
5971 CGM.getDiags().Report(Loc, diag::err_aix_musttail_unsupported);
5972 else if (!getTarget().hasFeature("pcrelative-memops")) {
5973 if (getTarget().hasFeature("longcall"))
5974 CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 0;
5975 else if (Call->isIndirectCall())
5976 CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 1;
5977 else if (isa_and_nonnull<FunctionDecl>(TargetDecl)) {
5978 if (!cast<FunctionDecl>(TargetDecl)->isDefined())
5979 // The undefined callee may be a forward declaration. Without
5980 // knowning all symbols in the module, we won't know the symbol is
5981 // defined or not. Collect all these symbols for later diagnosing.
5983 {cast<FunctionDecl>(TargetDecl), Loc});
5984 else {
5985 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(
5986 GlobalDecl(cast<FunctionDecl>(TargetDecl)));
5987 if (llvm::GlobalValue::isWeakForLinker(Linkage) ||
5988 llvm::GlobalValue::isDiscardableIfUnused(Linkage))
5989 CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail)
5990 << 2;
5991 }
5992 }
5993 }
5994 }
5995 Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
5996 }
5997 }
5998
5999 // Add metadata for calls to MSAllocator functions
6000 if (getDebugInfo() && TargetDecl && TargetDecl->hasAttr<MSAllocatorAttr>())
6002
6003 // Add metadata if calling an __attribute__((error(""))) or warning fn.
6004 if (TargetDecl && TargetDecl->hasAttr<ErrorAttr>()) {
6005 llvm::ConstantInt *Line =
6006 llvm::ConstantInt::get(Int64Ty, Loc.getRawEncoding());
6007 llvm::ConstantAsMetadata *MD = llvm::ConstantAsMetadata::get(Line);
6008 llvm::MDTuple *MDT = llvm::MDNode::get(getLLVMContext(), {MD});
6009 CI->setMetadata("srcloc", MDT);
6010 }
6011
6012 // 4. Finish the call.
6013
6014 // If the call doesn't return, finish the basic block and clear the
6015 // insertion point; this allows the rest of IRGen to discard
6016 // unreachable code.
6017 if (CI->doesNotReturn()) {
6018 if (NeedSRetLifetimeEnd)
6020
6021 // Strip away the noreturn attribute to better diagnose unreachable UB.
6022 if (SanOpts.has(SanitizerKind::Unreachable)) {
6023 // Also remove from function since CallBase::hasFnAttr additionally checks
6024 // attributes of the called function.
6025 if (auto *F = CI->getCalledFunction())
6026 F->removeFnAttr(llvm::Attribute::NoReturn);
6027 CI->removeFnAttr(llvm::Attribute::NoReturn);
6028
6029 // Avoid incompatibility with ASan which relies on the `noreturn`
6030 // attribute to insert handler calls.
6031 if (SanOpts.hasOneOf(SanitizerKind::Address |
6032 SanitizerKind::KernelAddress)) {
6033 SanitizerScope SanScope(this);
6034 llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
6035 Builder.SetInsertPoint(CI);
6036 auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
6037 llvm::FunctionCallee Fn =
6038 CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return");
6040 }
6041 }
6042
6044 Builder.ClearInsertionPoint();
6045
6046 // FIXME: For now, emit a dummy basic block because expr emitters in
6047 // generally are not ready to handle emitting expressions at unreachable
6048 // points.
6050
6051 // Return a reasonable RValue.
6052 return GetUndefRValue(RetTy);
6053 }
6054
6055 // If this is a musttail call, return immediately. We do not branch to the
6056 // epilogue in this case.
6057 if (IsMustTail) {
6058 for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();
6059 ++it) {
6060 EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);
6061 // Fake uses can be safely emitted immediately prior to the tail call, so
6062 // we choose to emit them just before the call here.
6063 if (Cleanup && Cleanup->isFakeUse()) {
6064 CGBuilderTy::InsertPointGuard IPG(Builder);
6065 Builder.SetInsertPoint(CI);
6066 Cleanup->getCleanup()->Emit(*this, EHScopeStack::Cleanup::Flags());
6067 } else if (!(Cleanup &&
6068 Cleanup->getCleanup()->isRedundantBeforeReturn())) {
6069 CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");
6070 }
6071 }
6072 if (CI->getType()->isVoidTy())
6073 Builder.CreateRetVoid();
6074 else
6075 Builder.CreateRet(CI);
6076 Builder.ClearInsertionPoint();
6078 return GetUndefRValue(RetTy);
6079 }
6080
6081 // Perform the swifterror writeback.
6082 if (swiftErrorTemp.isValid()) {
6083 llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);
6084 Builder.CreateStore(errorResult, swiftErrorArg);
6085 }
6086
6087 // Emit any call-associated writebacks immediately. Arguably this
6088 // should happen after any return-value munging.
6089 if (CallArgs.hasWritebacks())
6090 EmitWritebacks(CallArgs);
6091
6092 // The stack cleanup for inalloca arguments has to run out of the normal
6093 // lexical order, so deactivate it and run it manually here.
6094 CallArgs.freeArgumentMemory(*this);
6095
6096 // Extract the return value.
6097 RValue Ret;
6098
6099 // If the current function is a virtual function pointer thunk, avoid copying
6100 // the return value of the musttail call to a temporary.
6101 if (IsVirtualFunctionPointerThunk) {
6102 Ret = RValue::get(CI);
6103 } else {
6104 Ret = [&] {
6105 switch (RetAI.getKind()) {
6107 auto coercionType = RetAI.getCoerceAndExpandType();
6108
6109 Address addr = SRetPtr.withElementType(coercionType);
6110
6111 assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());
6112 bool requiresExtract = isa<llvm::StructType>(CI->getType());
6113
6114 unsigned unpaddedIndex = 0;
6115 for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
6116 llvm::Type *eltType = coercionType->getElementType(i);
6118 continue;
6119 Address eltAddr = Builder.CreateStructGEP(addr, i);
6120 llvm::Value *elt = CI;
6121 if (requiresExtract)
6122 elt = Builder.CreateExtractValue(elt, unpaddedIndex++);
6123 else
6124 assert(unpaddedIndex == 0);
6125 Builder.CreateStore(elt, eltAddr);
6126 }
6127 [[fallthrough]];
6128 }
6129
6131 case ABIArgInfo::Indirect: {
6132 RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());
6133 if (NeedSRetLifetimeEnd)
6135 return ret;
6136 }
6137
6138 case ABIArgInfo::Ignore:
6139 // If we are ignoring an argument that had a result, make sure to
6140 // construct the appropriate return value for our caller.
6141 return GetUndefRValue(RetTy);
6142
6143 case ABIArgInfo::Extend:
6144 case ABIArgInfo::Direct: {
6145 llvm::Type *RetIRTy = ConvertType(RetTy);
6146 if (RetAI.getCoerceToType() == RetIRTy &&
6147 RetAI.getDirectOffset() == 0) {
6148 switch (getEvaluationKind(RetTy)) {
6149 case TEK_Complex: {
6150 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
6151 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
6152 return RValue::getComplex(std::make_pair(Real, Imag));
6153 }
6154 case TEK_Aggregate:
6155 break;
6156 case TEK_Scalar: {
6157 // If the argument doesn't match, perform a bitcast to coerce it.
6158 // This can happen due to trivial type mismatches.
6159 llvm::Value *V = CI;
6160 if (V->getType() != RetIRTy)
6161 V = Builder.CreateBitCast(V, RetIRTy);
6162 return RValue::get(V);
6163 }
6164 }
6165 }
6166
6167 // If coercing a fixed vector from a scalable vector for ABI
6168 // compatibility, and the types match, use the llvm.vector.extract
6169 // intrinsic to perform the conversion.
6170 if (auto *FixedDstTy = dyn_cast<llvm::FixedVectorType>(RetIRTy)) {
6171 llvm::Value *V = CI;
6172 if (auto *ScalableSrcTy =
6173 dyn_cast<llvm::ScalableVectorType>(V->getType())) {
6174 if (FixedDstTy->getElementType() ==
6175 ScalableSrcTy->getElementType()) {
6176 V = Builder.CreateExtractVector(FixedDstTy, V, uint64_t(0),
6177 "cast.fixed");
6178 return RValue::get(V);
6179 }
6180 }
6181 }
6182
6183 Address DestPtr = ReturnValue.getValue();
6184 bool DestIsVolatile = ReturnValue.isVolatile();
6185 uint64_t DestSize =
6187
6188 if (!DestPtr.isValid()) {
6189 DestPtr = CreateMemTemp(RetTy, "coerce");
6190 DestIsVolatile = false;
6191 DestSize = getContext().getTypeSizeInChars(RetTy).getQuantity();
6192 }
6193
6194 // An empty record can overlap other data (if declared with
6195 // no_unique_address); omit the store for such types - as there is no
6196 // actual data to store.
6197 if (!isEmptyRecord(getContext(), RetTy, true)) {
6198 // If the value is offset in memory, apply the offset now.
6199 Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
6201 CI, StorePtr,
6202 llvm::TypeSize::getFixed(DestSize - RetAI.getDirectOffset()),
6203 DestIsVolatile);
6204 }
6205
6206 return convertTempToRValue(DestPtr, RetTy, SourceLocation());
6207 }
6208
6210 Address DestPtr = ReturnValue.getValue();
6211 Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);
6212 bool DestIsVolatile = ReturnValue.isVolatile();
6213 if (!DestPtr.isValid()) {
6214 DestPtr = CreateMemTemp(RetTy, "target_coerce");
6215 DestIsVolatile = false;
6216 }
6217 CGM.getABIInfo().createCoercedStore(CI, StorePtr, RetAI, DestIsVolatile,
6218 *this);
6219 return convertTempToRValue(DestPtr, RetTy, SourceLocation());
6220 }
6221
6222 case ABIArgInfo::Expand:
6224 llvm_unreachable("Invalid ABI kind for return argument");
6225 }
6226
6227 llvm_unreachable("Unhandled ABIArgInfo::Kind");
6228 }();
6229 }
6230
6231 // Emit the assume_aligned check on the return value.
6232 if (Ret.isScalar() && TargetDecl) {
6233 AssumeAlignedAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
6234 AllocAlignAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
6235 }
6236
6237 // Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
6238 // we can't use the full cleanup mechanism.
6239 for (CallLifetimeEnd &LifetimeEnd : CallLifetimeEndAfterCall)
6240 LifetimeEnd.Emit(*this, /*Flags=*/{});
6241
6242 if (!ReturnValue.isExternallyDestructed() &&
6244 pushDestroy(QualType::DK_nontrivial_c_struct, Ret.getAggregateAddress(),
6245 RetTy);
6246
6247 return Ret;
6248}
6249
6251 if (isVirtual()) {
6252 const CallExpr *CE = getVirtualCallExpr();
6255 CE ? CE->getBeginLoc() : SourceLocation());
6256 }
6257
6258 return *this;
6259}
6260
6261/* VarArg handling */
6262
6264 AggValueSlot Slot) {
6265 VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())
6266 : EmitVAListRef(VE->getSubExpr());
6267 QualType Ty = VE->getType();
6268 if (Ty->isVariablyModifiedType())
6270 if (VE->isMicrosoftABI())
6271 return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
6272 return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
6273}
6274
6276 : CGF(CGF) {
6278}
6279
6282}
#define V(N, I)
Definition: ASTContext.h:3597
StringRef P
static ExtParameterInfoList getExtParameterInfosForCall(const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
Definition: CGCall.cpp:467
static bool isInAllocaArgument(CGCXXABI &ABI, QualType type)
Definition: CGCall.cpp:4246
static uint64_t buildMultiCharMask(const SmallVectorImpl< uint64_t > &Bits, int Pos, int Size, int CharWidth, bool BigEndian)
Definition: CGCall.cpp:3902
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:3645
static CanQualType GetReturnType(QualType RetTy)
Returns the "extra-canonicalized" return type, which discards qualifiers on the return type.
Definition: CGCall.cpp:151
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:3043
static CanQualTypeList getArgTypesForCall(ASTContext &ctx, const CallArgList &args)
Definition: CGCall.cpp:450
static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr, const ABIArgInfo &info)
Definition: CGCall.cpp:1470
static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty)
Definition: CGCall.cpp:4251
static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D, bool IsTargetDefaultMSABI)
Definition: CGCall.cpp:255
static void setBitRange(SmallVectorImpl< uint64_t > &Bits, int BitOffset, int BitWidth, int CharWidth)
Definition: CGCall.cpp:3782
static bool isProvablyNull(llvm::Value *addr)
Definition: CGCall.cpp:4317
static void AddAttributesFromFunctionProtoType(ASTContext &Ctx, llvm::AttrBuilder &FuncAttrs, const FunctionProtoType *FPT)
Definition: CGCall.cpp:1840
static void eraseUnusedBitCasts(llvm::Instruction *insn)
Definition: CGCall.cpp:3540
static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method)
Definition: CGCall.cpp:4641
static void addNoBuiltinAttributes(llvm::AttrBuilder &FuncAttrs, const LangOptions &LangOpts, const NoBuiltinAttr *NBA=nullptr)
Definition: CGCall.cpp:2243
static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, const ObjCIndirectCopyRestoreExpr *CRE)
Emit an argument that's being passed call-by-writeback.
Definition: CGCall.cpp:4419
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:2117
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:141
static bool DetermineNoUndef(QualType QTy, CodeGenTypes &Types, const llvm::DataLayout &DL, const ABIArgInfo &AI, bool CheckCoerce=true)
Definition: CGCall.cpp:2279
static const Expr * maybeGetUnaryAddrOfOperand(const Expr *E)
Definition: CGCall.cpp:4408
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:4397
static bool isProvablyNonNull(Address Addr, CodeGenFunction &CGF)
Definition: CGCall.cpp:4321
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:3022
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:230
static void addExtParameterInfosForCall(llvm::SmallVectorImpl< FunctionProtoType::ExtParameterInfo > &paramInfos, const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
Definition: CGCall.cpp:166
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:2352
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:2373
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:2330
static bool hasInAllocaArgs(CodeGenModule &CGM, CallingConv ExplicitCC, ArrayRef< QualType > ArgTypes)
Definition: CGCall.cpp:4623
static std::unique_ptr< TypeExpansion > getTypeExpansion(QualType Ty, const ASTContext &Context)
Definition: CGCall.cpp:1003
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:3885
static llvm::StoreInst * findDominatingStoreToReturnValue(CodeGenFunction &CGF)
Heuristically search for a dominating store to the return-value slot.
Definition: CGCall.cpp:3704
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition: CGCall.cpp:359
static llvm::Value * tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, llvm::Value *result)
Try to emit a fused autorelease of a return result.
Definition: CGCall.cpp:3553
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:3686
static void emitWriteback(CodeGenFunction &CGF, const CallArgList::Writeback &writeback)
Emit the actual writing-back of a writeback.
Definition: CGCall.cpp:4326
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:458
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:5203
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.
Definition: CIRGenCall.cpp:165
static const CIRGenFunctionInfo & arrangeFreeFunctionLikeCall(CIRGenTypes &cgt, CIRGenModule &cgm, const CallArgList &args, const FunctionType *fnType)
Definition: CIRGenCall.cpp:298
const Decl * D
enum clang::sema::@1840::IndirectLocalPathEntry::EntryKind Kind
Expr * E
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define CC_VLS_CASE(ABI_VLEN)
llvm::MachO::Target Target
Definition: MachO.h:51
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
SourceLocation Loc
Definition: SemaObjC.cpp:754
static QualType getPointeeType(const MemRegion *R)
a trap message and trap category.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:3056
CanQualType getCanonicalParamType(QualType T) const
Return the canonical parameter type corresponding to the specific potentially non-canonical one.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CanQualType VoidPtrTy
Definition: ASTContext.h:1249
CallingConv getDefaultCallingConvention(bool IsVariadic, bool IsCXXMethod) const
Retrieves the default calling convention for the current context.
CanQualType getCanonicalSizeType() const
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C 'SEL' type.
Definition: ASTContext.h:2344
CanQualType IntTy
Definition: ASTContext.h:1231
TypeInfoChars getTypeInfoDataSizeInChars(QualType T) const
TypeInfoChars getTypeInfoInChars(const Type *T) const
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2625
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
Definition: ASTContext.h:1222
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:859
QualType getAddrSpaceQualType(QualType T, LangAS AddressSpace) const
Return the uniqued reference to the type for an address space qualified type with the specified type ...
uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const
Return number of constant array elements.
CanQualType getCanonicalTagType(const TagDecl *TD) const
QualType getIntPtrType() const
Return a type compatible with "intptr_t" (C99 7.18.1.4), as defined by the target.
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2629
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:201
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: TypeBase.h:3738
Attr - This represents one attribute.
Definition: Attr.h:44
const FunctionProtoType * getFunctionType() const
getFunctionType - Return the underlying function type for this block.
Definition: Expr.cpp:2527
This class is used for builtin types like 'int'.
Definition: TypeBase.h:3182
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:249
Represents a C++ constructor within a class.
Definition: DeclCXX.h: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:2879
SourceLocation getBeginLoc() const
Definition: Expr.h:3213
static CanQual< Type > CreateUnsafe(QualType Other)
Builds a canonical type from a QualType.
CanProxy< U > castAs() const
CanQual< T > getUnqualifiedType() const
Retrieve the unqualified form of this type.
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:84
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
PointerAuthOptions PointerAuth
Configuration for pointer-signing.
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.
bool isLoaderReplaceableFunctionName(StringRef FuncName) const
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
virtual void createCoercedStore(llvm::Value *Val, Address DstAddr, const ABIArgInfo &AI, bool DestIsVolatile, CodeGenFunction &CGF) const
Definition: ABIInfo.cpp:251
virtual RValue EmitMSVAArg(CodeGen::CodeGenFunction &CGF, CodeGen::Address VAListAddr, QualType Ty, AggValueSlot Slot) const
Emit the target dependent code to load a value of.
Definition: ABIInfo.cpp:42
virtual llvm::Value * createCoercedLoad(Address SrcAddr, const ABIArgInfo &AI, CodeGenFunction &CGF) const
Definition: ABIInfo.cpp:246
virtual RValue EmitVAArg(CodeGen::CodeGenFunction &CGF, CodeGen::Address VAListAddr, QualType Ty, AggValueSlot Slot) const =0
EmitVAArg - Emit the target dependent code to load a value of.
virtual void computeInfo(CodeGen::CGFunctionInfo &FI) const =0
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h: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:504
Address getAddress() const
Definition: CGValue.h:644
void setExternallyDestructed(bool destructed=true)
Definition: CGValue.h:613
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:587
RValue asRValue() const
Definition: CGValue.h:666
const BlockExpr * BlockExpression
Definition: CGBlocks.h:279
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::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:162
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:6250
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
void addHeapAllocSiteMetadata(llvm::CallBase *CallSite, QualType AllocatedTy, SourceLocation Loc)
Add heapallocsite metadata for MSAllocator calls.
CGFunctionInfo - Class to encapsulate the information about a function definition.
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
FunctionType::ExtInfo getExtInfo() const
bool isReturnsRetained() const
In ARC, whether this function retains its return value.
void Profile(llvm::FoldingSetNodeID &ID)
const_arg_iterator arg_begin() const
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
CanQualType getReturnType() const
static CGFunctionInfo * create(unsigned llvmCC, bool instanceMethod, bool chainCall, bool delegateCall, const FunctionType::ExtInfo &extInfo, ArrayRef< ExtParameterInfo > paramInfos, CanQualType resultType, ArrayRef< CanQualType > argTypes, RequiredArgs required)
Definition: CGCall.cpp: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
unsigned getNumRequiredArgs() const
llvm::StructType * getArgStruct() const
Get the struct type used to represent all the arguments in memory.
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h: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:4545
void freeArgumentMemory(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4552
writeback_const_range writebacks() const
Definition: CGCall.h:331
An abstract representation of regular/ObjC call/message targets.
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
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:5070
void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args)
Emits a call or invoke to the given noreturn runtime function.
Definition: CGCall.cpp:5033
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:5060
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.
Address GetAddressOfBaseClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc)
GetAddressOfBaseClass - This function will add the necessary delta to the load of 'this' and returns ...
Definition: CGClass.cpp:286
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:6635
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:3649
void SetSqrtFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Definition: CGExpr.cpp:6661
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:6263
void EmitReturnValueCheck(llvm::Value *RV)
Emit a test that checks if the return value RV is nonnull.
Definition: CGCall.cpp:4182
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:4269
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:684
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:2279
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:2502
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:4559
void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, AggValueSlot::Overlap_t MayOverlap, bool isVolatile=false)
EmitAggregateCopy - Emit an aggregate copy.
Definition: CGExprAgg.cpp:2283
const TargetInfo & getTarget() const
LValue EmitHLSLOutArgExpr(const HLSLOutArgExpr *E, CallArgList &Args, QualType Ty)
Definition: CGExpr.cpp:5854
void EmitWritebacks(const CallArgList &Args)
EmitWriteback - Emit callbacks for function.
Definition: CGCall.cpp:4850
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
Definition: CGExpr.cpp:242
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition: CGExpr.cpp:2336
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
DeactivateCleanupBlock - Deactivates the given cleanup block.
Definition: CGCleanup.cpp:1293
llvm::BasicBlock * getInvokeDest()
void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType)
EmitCallArg - Emit a single call argument.
Definition: CGCall.cpp:4855
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:3789
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.
llvm::Value * getTypeSize(QualType Ty)
Returns calculated size of the specified type.
bool EmitLifetimeStart(llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition: CGDecl.cpp:1357
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
EmitLValueForFieldInitialization - Like EmitLValueForField, except that if the Field is a reference,...
Definition: CGExpr.cpp:5427
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:151
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:5216
const TargetCodeGenInfo & getTargetHooks() const
void EmitLifetimeEnd(llvm::Value *Addr)
Definition: CGDecl.cpp:1369
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:215
bool InNoInlineAttributedStmt
True if the current statement has noinline attribute.
SmallVector< llvm::OperandBundleDef, 1 > getBundlesForFunclet(llvm::Value *Callee)
Definition: CGCall.cpp:4998
RValue EmitAnyExprToTemp(const Expr *E)
EmitAnyExprToTemp - Similarly to EmitAnyExpr(), however, the result will always be accessible even if...
Definition: CGExpr.cpp:283
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:3081
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:2533
Address EmitVAListRef(const Expr *E)
RValue GetUndefRValue(QualType Ty)
GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
Definition: CGExpr.cpp:1532
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition: CGDecl.cpp:2654
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:3968
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:1515
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:186
RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc)
Definition: CGExpr.cpp:5904
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
Definition: CGExprAgg.cpp:2205
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:4656
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:4129
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:1631
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:1525
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:3922
void PopCleanupBlock(bool FallThroughIsBranchThrough=false, bool ForDeactivation=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:652
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
This class organizes the cross-function state that is used while generating LLVM code.
llvm::MDNode * getNoObjCARCExceptionsMetadata()
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
bool ReturnTypeUsesFPRet(QualType ResultType)
Return true iff the given type uses 'fpret' when used as a return type.
Definition: CGCall.cpp:1669
DiagnosticsEngine & getDiags() const
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
const LangOptions & getLangOpts() const
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
const TargetInfo & getTarget() const
const llvm::DataLayout & getDataLayout() const
void addUndefinedGlobalForTailCall(std::pair< const FunctionDecl *, SourceLocation > Global)
ObjCEntrypoints & getObjCEntrypoints() const
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
bool shouldEmitConvergenceTokens() const
CGCXXABI & getCXXABI() const
bool ReturnTypeUsesFP2Ret(QualType ResultType)
Return true iff the given type uses 'fp2ret' when used as a return type.
Definition: CGCall.cpp:1686
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD)
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:2382
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:2410
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:2236
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.
Definition: CodeGenTypes.h:54
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
Definition: CGCall.cpp:345
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeSYCLKernelCallerDeclaration(QualType resultType, const FunctionArgList &args)
A SYCL kernel caller function is an offload device entry point function with a target device dependen...
Definition: CGCall.cpp:756
CGCXXABI & getCXXABI() const
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:373
ASTContext & getContext() const
Definition: CodeGenTypes.h:103
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:249
CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD)
Derives the 'this' type for codegen purposes, i.e.
Definition: CGCall.cpp:126
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:391
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:708
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:739
const CGFunctionInfo & arrangeUnprototypedObjCMessageSend(QualType returnType, const CallArgList &args)
Definition: CGCall.cpp:602
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
unsigned getTargetAddressSpace(QualType T) const
void getExpandedTypes(QualType Ty, SmallVectorImpl< llvm::Type * >::iterator &TI)
getExpandedTypes - Expand the type
Definition: CGCall.cpp:1075
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition: CGCall.cpp:555
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:106
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:611
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition: CGCall.cpp:626
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:699
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition: CGCall.cpp:728
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
Definition: CGCall.cpp:715
unsigned ClangCallConvToLLVMCallConv(CallingConv CC)
Convert clang calling convention to LLVM callilng convention.
Definition: CGCall.cpp:52
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:484
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:568
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:401
const CGFunctionInfo & arrangeFunctionDeclaration(const GlobalDecl GD)
Free functions are functions that are compatible with an ordinary C function pointer type.
Definition: CGCall.cpp:523
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition: CGCall.cpp:635
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
EHScopeStack::Cleanup * getCleanup()
Definition: CGCleanup.h:426
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:146
virtual void Emit(CodeGenFunction &CGF, Flags flags)=0
Emit the cleanup.
A saved depth on the scope stack.
Definition: EHScopeStack.h:106
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:398
iterator end() const
Returns an iterator pointing to the outermost EH scope.
Definition: CGCleanup.h:627
iterator find(stable_iterator save) const
Turn a stable reference to a scope depth into a unstable pointer to the EH stack.
Definition: CGCleanup.h:647
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:182
bool isBitField() const
Definition: CGValue.h:280
bool isSimple() const
Definition: CGValue.h:278
bool isVolatileQualified() const
Definition: CGValue.h:285
CharUnits getAlignment() const
Definition: CGValue.h:343
static LValue MakeAddr(Address Addr, QualType type, ASTContext &Context, LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo)
Definition: CGValue.h:432
bool isVolatile() const
Definition: CGValue.h:328
Address getAddress() const
Definition: CGValue.h:361
ARCPreciseLifetime_t isARCPreciseLifetime() const
Definition: CGValue.h:312
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:293
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:98
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
Definition: CGValue.h:125
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:108
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition: CGValue.h:83
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:71
bool isComplex() const
Definition: CGValue.h:65
bool isVolatileQualified() const
Definition: CGValue.h:68
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition: CGValue.h:78
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
bool isValid() const
Definition: Address.h:62
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 unsigned getDeviceKernelCallingConv() const
Get LLVM calling convention for device kernels.
Definition: TargetInfo.cpp:113
virtual bool doesReturnSlotInterfereWithArgs() const
doesReturnSlotInterfereWithArgs - Return true if the target uses an argument slot for an 'sret' type.
Definition: TargetInfo.h:217
virtual bool wouldInliningViolateFunctionCallABI(const FunctionDecl *Caller, const FunctionDecl *Callee) const
Returns true if inlining the function call would produce incorrect code for the current target and sh...
Definition: TargetInfo.h:118
virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const
Definition: TargetInfo.h:405
virtual void setOCLKernelStubCallingConvention(const FunctionType *&FT) const
Definition: TargetInfo.cpp:130
Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, LangAS SrcAddr, llvm::Type *DestTy, bool IsNonNull=false) const
virtual void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller, const FunctionDecl *Callee, const CallArgList &Args, QualType ReturnType) const
Any further codegen related checks that need to be done on a function call in a target specific manne...
Definition: TargetInfo.h:99
static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts, llvm::AttrBuilder &FuncAttrs)
Definition: TargetInfo.cpp:286
static void initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI, llvm::AttrBuilder &FuncAttrs)
Definition: TargetInfo.cpp:255
virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args, const FunctionNoProtoType *fnType) const
Determine whether a call to an unprototyped functions under the given calling convention should use t...
Definition: TargetInfo.cpp:94
Complex values, per C99 6.2.5p11.
Definition: TypeBase.h:3293
Represents the canonical version of C arrays with a specified constant size.
Definition: TypeBase.h:3776
bool constructsVirtualBase() const
Returns true if the constructed base class is a virtual base class subobject of this declaration's cl...
Definition: DeclCXX.h:3771
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
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:830
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1529
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:3069
@ NPC_ValueDependentIsNotNull
Specifies that a value-dependent expression should be considered to never be a null pointer constant.
Definition: Expr.h:837
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:4001
QualType getType() const
Definition: Expr.h:144
Represents a member of a struct/union/class.
Definition: Decl.h:3157
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:3260
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
Definition: Decl.h:3263
bool isZeroLengthBitField() const
Is this a zero-length bit-field? Such bit-fields aren't really bit-fields at all and instead act as a...
Definition: Decl.cpp:4702
Represents a function declaration or definition.
Definition: Decl.h:1999
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2376
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: TypeBase.h:4860
Represents a prototype with parameter type info, e.g.
Definition: TypeBase.h:5282
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
Definition: TypeBase.h:5589
unsigned getNumParams() const
Definition: TypeBase.h:5560
unsigned getAArch64SMEAttributes() const
Return a bitmask describing the SME attributes on the function type, see AArch64SMETypeAttributes for...
Definition: TypeBase.h:5779
bool isNothrow(bool ResultIfDependent=false) const
Determine whether this function type has a non-throwing exception specification.
Definition: TypeBase.h:5681
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
Definition: TypeBase.h:5755
bool hasExtParameterInfos() const
Is there any interesting extra information for any of the parameters of this function type?
Definition: TypeBase.h:5751
Wrapper for source info for functions.
Definition: TypeLoc.h:1624
A class which abstracts out some details necessary for making a call.
Definition: TypeBase.h:4589
ExtInfo withCallingConv(CallingConv cc) const
Definition: TypeBase.h:4701
CallingConv getCC() const
Definition: TypeBase.h:4648
ExtInfo withProducesResult(bool producesResult) const
Definition: TypeBase.h:4667
unsigned getRegParm() const
Definition: TypeBase.h:4641
bool getNoCallerSavedRegs() const
Definition: TypeBase.h:4637
bool getProducesResult() const
Definition: TypeBase.h:4635
Interesting information about a specific parameter that can't simply be reflected in parameter's type...
Definition: TypeBase.h:4504
ParameterABI getABI() const
Return the ABI treatment of this parameter.
Definition: TypeBase.h:4517
ExtParameterInfo withIsNoEscape(bool NoEscape) const
Definition: TypeBase.h:4544
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: TypeBase.h:4478
ExtInfo getExtInfo() const
Definition: TypeBase.h:4834
static ArmStateValue getArmZT0State(unsigned AttrBits)
Definition: TypeBase.h:4787
static ArmStateValue getArmZAState(unsigned AttrBits)
Definition: TypeBase.h:4783
QualType getReturnType() const
Definition: TypeBase.h:4818
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:7258
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.
Definition: LangOptions.h:229
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
Definition: LangOptions.h:501
FPExceptionModeKind getDefaultExceptionMode() const
Definition: LangOptions.h:749
bool isNoBuiltinFunc(StringRef Name) const
Is this a libc/libm function that is no longer recognized as a builtin because a -fno-builtin-* optio...
Definition: LangOptions.cpp:51
bool assumeFunctionsAreConvergent() const
Definition: LangOptions.h:644
Represents a matrix type, as defined in the Matrix Types clang extensions.
Definition: TypeBase.h:4353
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:300
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2329
ObjCIndirectCopyRestoreExpr - Represents the passing of a function argument by indirect copy-restore ...
Definition: ExprObjC.h:1582
bool shouldCopy() const
shouldCopy - True if we should do the 'copy' part of the copy-restore.
Definition: ExprObjC.h:1610
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:1789
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: TypeBase.h:3346
QualType getPointeeType() const
Definition: TypeBase.h:3356
A (possibly-)qualified type.
Definition: TypeBase.h:937
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition: TypeBase.h:8421
bool isTriviallyCopyableType(const ASTContext &Context) const
Return true if this is a trivially copyable type (C++0x [basic.types]p9)
Definition: Type.cpp:2871
@ DK_nontrivial_c_struct
Definition: TypeBase.h:1538
LangAS getAddressSpace() const
Return the address space of this type.
Definition: TypeBase.h:8469
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: TypeBase.h:8383
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:8528
QualType getCanonicalType() const
Definition: TypeBase.h:8395
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: TypeBase.h:8416
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:4309
field_iterator field_end() const
Definition: Decl.h:4515
bool isParamDestroyedInCallee() const
Definition: Decl.h:4459
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4493
field_iterator field_begin() const
Definition: Decl.cpp:5154
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
RecordDecl * getOriginalDecl() const
Definition: TypeBase.h:6509
Base for LValueReferenceType and RValueReferenceType.
Definition: TypeBase.h:3589
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 areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change,...
Definition: TargetCXXABI.h:188
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:136
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1288
bool useObjCFPRetForRealType(FloatModeKind T) const
Check whether the given real type should use the "fpret" flavor of Objective-C message passing on thi...
Definition: TargetInfo.h:1001
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1360
bool useObjCFP2RetForComplexLongDouble() const
Check whether _Complex long double should use the "fp2ret" flavor of Objective-C message passing on t...
Definition: TargetInfo.h:1007
Options for controlling the target.
Definition: TargetOptions.h:26
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
Definition: TargetOptions.h:58
std::string TuneCPU
If given, the name of the target CPU to tune code for.
Definition: TargetOptions.h:39
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:36
The base class of the type hierarchy.
Definition: TypeBase.h:1833
bool isBlockPointerType() const
Definition: TypeBase.h:8600
bool isVoidType() const
Definition: TypeBase.h:8936
bool isIncompleteArrayType() const
Definition: TypeBase.h:8687
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:2430
bool isPointerType() const
Definition: TypeBase.h:8580
CanQualType getCanonicalTypeUnqualified() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition: TypeBase.h:8980
const T * castAs() const
Member-template castAs<specific type>.
Definition: TypeBase.h:9226
bool isReferenceType() const
Definition: TypeBase.h:8604
bool isScalarType() const
Definition: TypeBase.h:9038
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:8845
RecordDecl * castAsRecordDecl() const
Definition: Type.h:48
bool isMemberPointerType() const
Definition: TypeBase.h:8661
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: TypeBase.h:2818
bool isObjectType() const
Determine whether this type is an object type.
Definition: TypeBase.h:2528
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2440
bool hasFloatingRepresentation() const
Determine whether this type has a floating-point representation of some sort, e.g....
Definition: Type.cpp:2316
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition: TypeBase.h:2939
const T * castAsCanonical() const
Return this type's canonical type cast to the specified type.
Definition: TypeBase.h:2946
bool isAnyPointerType() const
Definition: TypeBase.h:8588
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
bool isNullPtrType() const
Definition: TypeBase.h:8973
bool isRecordType() const
Definition: TypeBase.h:8707
bool isObjCRetainableType() const
Definition: Type.cpp:5336
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2246
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:4893
bool isMicrosoftABI() const
Returns whether this is really a Win64 ABI va_arg expression.
Definition: Expr.h:4914
const Expr * getSubExpr() const
Definition: Expr.h:4909
QualType getType() const
Definition: Decl.h:722
Represents a variable declaration or definition.
Definition: Decl.h:925
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition: Decl.cpp:2851
Represents a GCC generic vector type.
Definition: TypeBase.h:4191
Defines the clang::TargetInfo interface.
void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Compute the ABI information of a swiftcall function.
void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Definition: SPIR.cpp:213
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:84
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:2148
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
bool Ret(InterpState &S, CodePtr &PC)
Definition: Interp.h:312
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.
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 isUnresolvedExceptionSpec(ExceptionSpecificationType ESpecType)
bool isInstanceMethod(const Decl *D)
Definition: Attr.h:120
@ NonNull
Values of this type can never be null.
@ 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.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:24
@ Result
The result type of a method or function.
@ SwiftAsyncContext
This parameter (which must have pointer type) uses the special Swift asynchronous context-pointer ABI...
@ SwiftErrorResult
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
@ Ordinary
This parameter uses ordinary ABI rules for its type.
@ SwiftIndirectResult
This parameter (which must have pointer type) is a Swift indirect result parameter.
@ SwiftContext
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment.
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
@ CanPassInRegs
The argument of this type can be passed directly in registers.
const FunctionProtoType * T
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h: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_C
Definition: Specifiers.h:279
@ 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
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:90
unsigned long uint64_t
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
Definition: complex_cmath.h:40
Structure with information about how a bitfield should be accessed.
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
unsigned Offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned Size
The total size of the bit-field, in bits.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
Similar to AddedStructorArgs, but only notes the number of additional arguments.
Definition: CGCXXABI.h: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:4833
bool hasLValue() const
Definition: CGCall.h:247
RValue getRValue(CodeGenFunction &CGF) const
Definition: CGCall.cpp:4823
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::CallingConv::ID getRuntimeCC() const
DisableDebugLocationUpdates(CodeGenFunction &CGF)
Definition: CGCall.cpp:6275
bool isMSVCXXPersonality() const
Definition: CGCleanup.h:703
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
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:184
Iterator for iterating over Stmt * arrays that contain only T *.
Definition: Stmt.h:1430