Path: blob/main/contrib/llvm-project/clang/lib/CodeGen/CGCall.cpp
35233 views
//===--- CGCall.cpp - Encapsulate calling convention details --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// These classes wrap the information about a call or function9// definition used to handle ABI compliancy.10//11//===----------------------------------------------------------------------===//1213#include "CGCall.h"14#include "ABIInfo.h"15#include "ABIInfoImpl.h"16#include "CGBlocks.h"17#include "CGCXXABI.h"18#include "CGCleanup.h"19#include "CGRecordLayout.h"20#include "CodeGenFunction.h"21#include "CodeGenModule.h"22#include "TargetInfo.h"23#include "clang/AST/Attr.h"24#include "clang/AST/Decl.h"25#include "clang/AST/DeclCXX.h"26#include "clang/AST/DeclObjC.h"27#include "clang/Basic/CodeGenOptions.h"28#include "clang/Basic/TargetInfo.h"29#include "clang/CodeGen/CGFunctionInfo.h"30#include "clang/CodeGen/SwiftCallingConv.h"31#include "llvm/ADT/StringExtras.h"32#include "llvm/Analysis/ValueTracking.h"33#include "llvm/IR/Assumptions.h"34#include "llvm/IR/AttributeMask.h"35#include "llvm/IR/Attributes.h"36#include "llvm/IR/CallingConv.h"37#include "llvm/IR/DataLayout.h"38#include "llvm/IR/InlineAsm.h"39#include "llvm/IR/IntrinsicInst.h"40#include "llvm/IR/Intrinsics.h"41#include "llvm/IR/Type.h"42#include "llvm/Transforms/Utils/Local.h"43#include <optional>44using namespace clang;45using namespace CodeGen;4647/***/4849unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {50switch (CC) {51default: return llvm::CallingConv::C;52case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;53case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;54case CC_X86RegCall: return llvm::CallingConv::X86_RegCall;55case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;56case CC_Win64: return llvm::CallingConv::Win64;57case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV;58case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;59case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;60case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;61// TODO: Add support for __pascal to LLVM.62case CC_X86Pascal: return llvm::CallingConv::C;63// TODO: Add support for __vectorcall to LLVM.64case CC_X86VectorCall: return llvm::CallingConv::X86_VectorCall;65case CC_AArch64VectorCall: return llvm::CallingConv::AArch64_VectorCall;66case CC_AArch64SVEPCS: return llvm::CallingConv::AArch64_SVE_VectorCall;67case CC_AMDGPUKernelCall: return llvm::CallingConv::AMDGPU_KERNEL;68case CC_SpirFunction: return llvm::CallingConv::SPIR_FUNC;69case CC_OpenCLKernel: return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();70case CC_PreserveMost: return llvm::CallingConv::PreserveMost;71case CC_PreserveAll: return llvm::CallingConv::PreserveAll;72case CC_Swift: return llvm::CallingConv::Swift;73case CC_SwiftAsync: return llvm::CallingConv::SwiftTail;74case CC_M68kRTD: return llvm::CallingConv::M68k_RTD;75case CC_PreserveNone: return llvm::CallingConv::PreserveNone;76// clang-format off77case CC_RISCVVectorCall: return llvm::CallingConv::RISCV_VectorCall;78// clang-format on79}80}8182/// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR83/// qualification. Either or both of RD and MD may be null. A null RD indicates84/// that there is no meaningful 'this' type, and a null MD can occur when85/// calling a method pointer.86CanQualType CodeGenTypes::DeriveThisType(const CXXRecordDecl *RD,87const CXXMethodDecl *MD) {88QualType RecTy;89if (RD)90RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();91else92RecTy = Context.VoidTy;9394if (MD)95RecTy = Context.getAddrSpaceQualType(RecTy, MD->getMethodQualifiers().getAddressSpace());96return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));97}9899/// Returns the canonical formal type of the given C++ method.100static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {101return MD->getType()->getCanonicalTypeUnqualified()102.getAs<FunctionProtoType>();103}104105/// Returns the "extra-canonicalized" return type, which discards106/// qualifiers on the return type. Codegen doesn't care about them,107/// and it makes ABI code a little easier to be able to assume that108/// all parameter and return types are top-level unqualified.109static CanQualType GetReturnType(QualType RetTy) {110return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();111}112113/// Arrange the argument and result information for a value of the given114/// unprototyped freestanding function type.115const CGFunctionInfo &116CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {117// When translating an unprototyped function type, always use a118// variadic type.119return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(),120FnInfoOpts::None, std::nullopt,121FTNP->getExtInfo(), {}, RequiredArgs(0));122}123124static void addExtParameterInfosForCall(125llvm::SmallVectorImpl<FunctionProtoType::ExtParameterInfo> ¶mInfos,126const FunctionProtoType *proto,127unsigned prefixArgs,128unsigned totalArgs) {129assert(proto->hasExtParameterInfos());130assert(paramInfos.size() <= prefixArgs);131assert(proto->getNumParams() + prefixArgs <= totalArgs);132133paramInfos.reserve(totalArgs);134135// Add default infos for any prefix args that don't already have infos.136paramInfos.resize(prefixArgs);137138// Add infos for the prototype.139for (const auto &ParamInfo : proto->getExtParameterInfos()) {140paramInfos.push_back(ParamInfo);141// pass_object_size params have no parameter info.142if (ParamInfo.hasPassObjectSize())143paramInfos.emplace_back();144}145146assert(paramInfos.size() <= totalArgs &&147"Did we forget to insert pass_object_size args?");148// Add default infos for the variadic and/or suffix arguments.149paramInfos.resize(totalArgs);150}151152/// Adds the formal parameters in FPT to the given prefix. If any parameter in153/// FPT has pass_object_size attrs, then we'll add parameters for those, too.154static void appendParameterTypes(const CodeGenTypes &CGT,155SmallVectorImpl<CanQualType> &prefix,156SmallVectorImpl<FunctionProtoType::ExtParameterInfo> ¶mInfos,157CanQual<FunctionProtoType> FPT) {158// Fast path: don't touch param info if we don't need to.159if (!FPT->hasExtParameterInfos()) {160assert(paramInfos.empty() &&161"We have paramInfos, but the prototype doesn't?");162prefix.append(FPT->param_type_begin(), FPT->param_type_end());163return;164}165166unsigned PrefixSize = prefix.size();167// In the vast majority of cases, we'll have precisely FPT->getNumParams()168// parameters; the only thing that can change this is the presence of169// pass_object_size. So, we preallocate for the common case.170prefix.reserve(prefix.size() + FPT->getNumParams());171172auto ExtInfos = FPT->getExtParameterInfos();173assert(ExtInfos.size() == FPT->getNumParams());174for (unsigned I = 0, E = FPT->getNumParams(); I != E; ++I) {175prefix.push_back(FPT->getParamType(I));176if (ExtInfos[I].hasPassObjectSize())177prefix.push_back(CGT.getContext().getSizeType());178}179180addExtParameterInfosForCall(paramInfos, FPT.getTypePtr(), PrefixSize,181prefix.size());182}183184/// Arrange the LLVM function layout for a value of the given function185/// type, on top of any implicit parameters already stored.186static const CGFunctionInfo &187arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod,188SmallVectorImpl<CanQualType> &prefix,189CanQual<FunctionProtoType> FTP) {190SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;191RequiredArgs Required = RequiredArgs::forPrototypePlus(FTP, prefix.size());192// FIXME: Kill copy.193appendParameterTypes(CGT, prefix, paramInfos, FTP);194CanQualType resultType = FTP->getReturnType().getUnqualifiedType();195196FnInfoOpts opts =197instanceMethod ? FnInfoOpts::IsInstanceMethod : FnInfoOpts::None;198return CGT.arrangeLLVMFunctionInfo(resultType, opts, prefix,199FTP->getExtInfo(), paramInfos, Required);200}201202/// Arrange the argument and result information for a value of the203/// given freestanding function type.204const CGFunctionInfo &205CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {206SmallVector<CanQualType, 16> argTypes;207return ::arrangeLLVMFunctionInfo(*this, /*instanceMethod=*/false, argTypes,208FTP);209}210211static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,212bool IsWindows) {213// Set the appropriate calling convention for the Function.214if (D->hasAttr<StdCallAttr>())215return CC_X86StdCall;216217if (D->hasAttr<FastCallAttr>())218return CC_X86FastCall;219220if (D->hasAttr<RegCallAttr>())221return CC_X86RegCall;222223if (D->hasAttr<ThisCallAttr>())224return CC_X86ThisCall;225226if (D->hasAttr<VectorCallAttr>())227return CC_X86VectorCall;228229if (D->hasAttr<PascalAttr>())230return CC_X86Pascal;231232if (PcsAttr *PCS = D->getAttr<PcsAttr>())233return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);234235if (D->hasAttr<AArch64VectorPcsAttr>())236return CC_AArch64VectorCall;237238if (D->hasAttr<AArch64SVEPcsAttr>())239return CC_AArch64SVEPCS;240241if (D->hasAttr<AMDGPUKernelCallAttr>())242return CC_AMDGPUKernelCall;243244if (D->hasAttr<IntelOclBiccAttr>())245return CC_IntelOclBicc;246247if (D->hasAttr<MSABIAttr>())248return IsWindows ? CC_C : CC_Win64;249250if (D->hasAttr<SysVABIAttr>())251return IsWindows ? CC_X86_64SysV : CC_C;252253if (D->hasAttr<PreserveMostAttr>())254return CC_PreserveMost;255256if (D->hasAttr<PreserveAllAttr>())257return CC_PreserveAll;258259if (D->hasAttr<M68kRTDAttr>())260return CC_M68kRTD;261262if (D->hasAttr<PreserveNoneAttr>())263return CC_PreserveNone;264265if (D->hasAttr<RISCVVectorCCAttr>())266return CC_RISCVVectorCall;267268return CC_C;269}270271/// Arrange the argument and result information for a call to an272/// unknown C++ non-static member function of the given abstract type.273/// (A null RD means we don't have any meaningful "this" argument type,274/// so fall back to a generic pointer type).275/// The member function must be an ordinary function, i.e. not a276/// constructor or destructor.277const CGFunctionInfo &278CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,279const FunctionProtoType *FTP,280const CXXMethodDecl *MD) {281SmallVector<CanQualType, 16> argTypes;282283// Add the 'this' pointer.284argTypes.push_back(DeriveThisType(RD, MD));285286return ::arrangeLLVMFunctionInfo(287*this, /*instanceMethod=*/true, argTypes,288FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());289}290291/// Set calling convention for CUDA/HIP kernel.292static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM,293const FunctionDecl *FD) {294if (FD->hasAttr<CUDAGlobalAttr>()) {295const FunctionType *FT = FTy->getAs<FunctionType>();296CGM.getTargetCodeGenInfo().setCUDAKernelCallingConvention(FT);297FTy = FT->getCanonicalTypeUnqualified();298}299}300301/// Arrange the argument and result information for a declaration or302/// definition of the given C++ non-static member function. The303/// member function must be an ordinary function, i.e. not a304/// constructor or destructor.305const CGFunctionInfo &306CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {307assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!");308assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");309310CanQualType FT = GetFormalType(MD).getAs<Type>();311setCUDAKernelCallingConvention(FT, CGM, MD);312auto prototype = FT.getAs<FunctionProtoType>();313314if (MD->isImplicitObjectMemberFunction()) {315// The abstract case is perfectly fine.316const CXXRecordDecl *ThisType =317getCXXABI().getThisArgumentTypeForMethod(MD);318return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);319}320321return arrangeFreeFunctionType(prototype);322}323324bool CodeGenTypes::inheritingCtorHasParams(325const InheritedConstructor &Inherited, CXXCtorType Type) {326// Parameters are unnecessary if we're constructing a base class subobject327// and the inherited constructor lives in a virtual base.328return Type == Ctor_Complete ||329!Inherited.getShadowDecl()->constructsVirtualBase() ||330!Target.getCXXABI().hasConstructorVariants();331}332333const CGFunctionInfo &334CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {335auto *MD = cast<CXXMethodDecl>(GD.getDecl());336337SmallVector<CanQualType, 16> argTypes;338SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;339340const CXXRecordDecl *ThisType = getCXXABI().getThisArgumentTypeForMethod(GD);341argTypes.push_back(DeriveThisType(ThisType, MD));342343bool PassParams = true;344345if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {346// A base class inheriting constructor doesn't get forwarded arguments347// needed to construct a virtual base (or base class thereof).348if (auto Inherited = CD->getInheritedConstructor())349PassParams = inheritingCtorHasParams(Inherited, GD.getCtorType());350}351352CanQual<FunctionProtoType> FTP = GetFormalType(MD);353354// Add the formal parameters.355if (PassParams)356appendParameterTypes(*this, argTypes, paramInfos, FTP);357358CGCXXABI::AddedStructorArgCounts AddedArgs =359getCXXABI().buildStructorSignature(GD, argTypes);360if (!paramInfos.empty()) {361// Note: prefix implies after the first param.362if (AddedArgs.Prefix)363paramInfos.insert(paramInfos.begin() + 1, AddedArgs.Prefix,364FunctionProtoType::ExtParameterInfo{});365if (AddedArgs.Suffix)366paramInfos.append(AddedArgs.Suffix,367FunctionProtoType::ExtParameterInfo{});368}369370RequiredArgs required =371(PassParams && MD->isVariadic() ? RequiredArgs(argTypes.size())372: RequiredArgs::All);373374FunctionType::ExtInfo extInfo = FTP->getExtInfo();375CanQualType resultType = getCXXABI().HasThisReturn(GD) ? argTypes.front()376: getCXXABI().hasMostDerivedReturn(GD)377? CGM.getContext().VoidPtrTy378: Context.VoidTy;379return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,380argTypes, extInfo, paramInfos, required);381}382383static SmallVector<CanQualType, 16>384getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {385SmallVector<CanQualType, 16> argTypes;386for (auto &arg : args)387argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));388return argTypes;389}390391static SmallVector<CanQualType, 16>392getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args) {393SmallVector<CanQualType, 16> argTypes;394for (auto &arg : args)395argTypes.push_back(ctx.getCanonicalParamType(arg->getType()));396return argTypes;397}398399static llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16>400getExtParameterInfosForCall(const FunctionProtoType *proto,401unsigned prefixArgs, unsigned totalArgs) {402llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> result;403if (proto->hasExtParameterInfos()) {404addExtParameterInfosForCall(result, proto, prefixArgs, totalArgs);405}406return result;407}408409/// Arrange a call to a C++ method, passing the given arguments.410///411/// ExtraPrefixArgs is the number of ABI-specific args passed after the `this`412/// parameter.413/// ExtraSuffixArgs is the number of ABI-specific args passed at the end of414/// args.415/// PassProtoArgs indicates whether `args` has args for the parameters in the416/// given CXXConstructorDecl.417const CGFunctionInfo &418CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,419const CXXConstructorDecl *D,420CXXCtorType CtorKind,421unsigned ExtraPrefixArgs,422unsigned ExtraSuffixArgs,423bool PassProtoArgs) {424// FIXME: Kill copy.425SmallVector<CanQualType, 16> ArgTypes;426for (const auto &Arg : args)427ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));428429// +1 for implicit this, which should always be args[0].430unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;431432CanQual<FunctionProtoType> FPT = GetFormalType(D);433RequiredArgs Required = PassProtoArgs434? RequiredArgs::forPrototypePlus(435FPT, TotalPrefixArgs + ExtraSuffixArgs)436: RequiredArgs::All;437438GlobalDecl GD(D, CtorKind);439CanQualType ResultType = getCXXABI().HasThisReturn(GD) ? ArgTypes.front()440: getCXXABI().hasMostDerivedReturn(GD)441? CGM.getContext().VoidPtrTy442: Context.VoidTy;443444FunctionType::ExtInfo Info = FPT->getExtInfo();445llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;446// If the prototype args are elided, we should only have ABI-specific args,447// which never have param info.448if (PassProtoArgs && FPT->hasExtParameterInfos()) {449// ABI-specific suffix arguments are treated the same as variadic arguments.450addExtParameterInfosForCall(ParamInfos, FPT.getTypePtr(), TotalPrefixArgs,451ArgTypes.size());452}453454return arrangeLLVMFunctionInfo(ResultType, FnInfoOpts::IsInstanceMethod,455ArgTypes, Info, ParamInfos, Required);456}457458/// Arrange the argument and result information for the declaration or459/// definition of the given function.460const CGFunctionInfo &461CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {462if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))463if (MD->isImplicitObjectMemberFunction())464return arrangeCXXMethodDeclaration(MD);465466CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();467468assert(isa<FunctionType>(FTy));469setCUDAKernelCallingConvention(FTy, CGM, FD);470471// When declaring a function without a prototype, always use a472// non-variadic type.473if (CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>()) {474return arrangeLLVMFunctionInfo(noProto->getReturnType(), FnInfoOpts::None,475std::nullopt, noProto->getExtInfo(), {},476RequiredArgs::All);477}478479return arrangeFreeFunctionType(FTy.castAs<FunctionProtoType>());480}481482/// Arrange the argument and result information for the declaration or483/// definition of an Objective-C method.484const CGFunctionInfo &485CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {486// It happens that this is the same as a call with no optional487// arguments, except also using the formal 'self' type.488return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());489}490491/// Arrange the argument and result information for the function type492/// through which to perform a send to the given Objective-C method,493/// using the given receiver type. The receiver type is not always494/// the 'self' type of the method or even an Objective-C pointer type.495/// This is *not* the right method for actually performing such a496/// message send, due to the possibility of optional arguments.497const CGFunctionInfo &498CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,499QualType receiverType) {500SmallVector<CanQualType, 16> argTys;501SmallVector<FunctionProtoType::ExtParameterInfo, 4> extParamInfos(502MD->isDirectMethod() ? 1 : 2);503argTys.push_back(Context.getCanonicalParamType(receiverType));504if (!MD->isDirectMethod())505argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));506// FIXME: Kill copy?507for (const auto *I : MD->parameters()) {508argTys.push_back(Context.getCanonicalParamType(I->getType()));509auto extParamInfo = FunctionProtoType::ExtParameterInfo().withIsNoEscape(510I->hasAttr<NoEscapeAttr>());511extParamInfos.push_back(extParamInfo);512}513514FunctionType::ExtInfo einfo;515bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();516einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows));517518if (getContext().getLangOpts().ObjCAutoRefCount &&519MD->hasAttr<NSReturnsRetainedAttr>())520einfo = einfo.withProducesResult(true);521522RequiredArgs required =523(MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);524525return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()),526FnInfoOpts::None, argTys, einfo, extParamInfos,527required);528}529530const CGFunctionInfo &531CodeGenTypes::arrangeUnprototypedObjCMessageSend(QualType returnType,532const CallArgList &args) {533auto argTypes = getArgTypesForCall(Context, args);534FunctionType::ExtInfo einfo;535536return arrangeLLVMFunctionInfo(GetReturnType(returnType), FnInfoOpts::None,537argTypes, einfo, {}, RequiredArgs::All);538}539540const CGFunctionInfo &541CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {542// FIXME: Do we need to handle ObjCMethodDecl?543const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());544545if (isa<CXXConstructorDecl>(GD.getDecl()) ||546isa<CXXDestructorDecl>(GD.getDecl()))547return arrangeCXXStructorDeclaration(GD);548549return arrangeFunctionDeclaration(FD);550}551552/// Arrange a thunk that takes 'this' as the first parameter followed by553/// varargs. Return a void pointer, regardless of the actual return type.554/// The body of the thunk will end in a musttail call to a function of the555/// correct type, and the caller will bitcast the function to the correct556/// prototype.557const CGFunctionInfo &558CodeGenTypes::arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD) {559assert(MD->isVirtual() && "only methods have thunks");560CanQual<FunctionProtoType> FTP = GetFormalType(MD);561CanQualType ArgTys[] = {DeriveThisType(MD->getParent(), MD)};562return arrangeLLVMFunctionInfo(Context.VoidTy, FnInfoOpts::None, ArgTys,563FTP->getExtInfo(), {}, RequiredArgs(1));564}565566const CGFunctionInfo &567CodeGenTypes::arrangeMSCtorClosure(const CXXConstructorDecl *CD,568CXXCtorType CT) {569assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);570571CanQual<FunctionProtoType> FTP = GetFormalType(CD);572SmallVector<CanQualType, 2> ArgTys;573const CXXRecordDecl *RD = CD->getParent();574ArgTys.push_back(DeriveThisType(RD, CD));575if (CT == Ctor_CopyingClosure)576ArgTys.push_back(*FTP->param_type_begin());577if (RD->getNumVBases() > 0)578ArgTys.push_back(Context.IntTy);579CallingConv CC = Context.getDefaultCallingConvention(580/*IsVariadic=*/false, /*IsCXXMethod=*/true);581return arrangeLLVMFunctionInfo(Context.VoidTy, FnInfoOpts::IsInstanceMethod,582ArgTys, FunctionType::ExtInfo(CC), {},583RequiredArgs::All);584}585586/// Arrange a call as unto a free function, except possibly with an587/// additional number of formal parameters considered required.588static const CGFunctionInfo &589arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,590CodeGenModule &CGM,591const CallArgList &args,592const FunctionType *fnType,593unsigned numExtraRequiredArgs,594bool chainCall) {595assert(args.size() >= numExtraRequiredArgs);596597llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;598599// In most cases, there are no optional arguments.600RequiredArgs required = RequiredArgs::All;601602// If we have a variadic prototype, the required arguments are the603// extra prefix plus the arguments in the prototype.604if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {605if (proto->isVariadic())606required = RequiredArgs::forPrototypePlus(proto, numExtraRequiredArgs);607608if (proto->hasExtParameterInfos())609addExtParameterInfosForCall(paramInfos, proto, numExtraRequiredArgs,610args.size());611612// If we don't have a prototype at all, but we're supposed to613// explicitly use the variadic convention for unprototyped calls,614// treat all of the arguments as required but preserve the nominal615// possibility of variadics.616} else if (CGM.getTargetCodeGenInfo()617.isNoProtoCallVariadic(args,618cast<FunctionNoProtoType>(fnType))) {619required = RequiredArgs(args.size());620}621622// FIXME: Kill copy.623SmallVector<CanQualType, 16> argTypes;624for (const auto &arg : args)625argTypes.push_back(CGT.getContext().getCanonicalParamType(arg.Ty));626FnInfoOpts opts = chainCall ? FnInfoOpts::IsChainCall : FnInfoOpts::None;627return CGT.arrangeLLVMFunctionInfo(GetReturnType(fnType->getReturnType()),628opts, argTypes, fnType->getExtInfo(),629paramInfos, required);630}631632/// Figure out the rules for calling a function with the given formal633/// type using the given arguments. The arguments are necessary634/// because the function might be unprototyped, in which case it's635/// target-dependent in crazy ways.636const CGFunctionInfo &637CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,638const FunctionType *fnType,639bool chainCall) {640return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType,641chainCall ? 1 : 0, chainCall);642}643644/// A block function is essentially a free function with an645/// extra implicit argument.646const CGFunctionInfo &647CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,648const FunctionType *fnType) {649return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1,650/*chainCall=*/false);651}652653const CGFunctionInfo &654CodeGenTypes::arrangeBlockFunctionDeclaration(const FunctionProtoType *proto,655const FunctionArgList ¶ms) {656auto paramInfos = getExtParameterInfosForCall(proto, 1, params.size());657auto argTypes = getArgTypesForDeclaration(Context, params);658659return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()),660FnInfoOpts::None, argTypes,661proto->getExtInfo(), paramInfos,662RequiredArgs::forPrototypePlus(proto, 1));663}664665const CGFunctionInfo &666CodeGenTypes::arrangeBuiltinFunctionCall(QualType resultType,667const CallArgList &args) {668// FIXME: Kill copy.669SmallVector<CanQualType, 16> argTypes;670for (const auto &Arg : args)671argTypes.push_back(Context.getCanonicalParamType(Arg.Ty));672return arrangeLLVMFunctionInfo(GetReturnType(resultType), FnInfoOpts::None,673argTypes, FunctionType::ExtInfo(),674/*paramInfos=*/{}, RequiredArgs::All);675}676677const CGFunctionInfo &678CodeGenTypes::arrangeBuiltinFunctionDeclaration(QualType resultType,679const FunctionArgList &args) {680auto argTypes = getArgTypesForDeclaration(Context, args);681682return arrangeLLVMFunctionInfo(GetReturnType(resultType), FnInfoOpts::None,683argTypes, FunctionType::ExtInfo(), {},684RequiredArgs::All);685}686687const CGFunctionInfo &688CodeGenTypes::arrangeBuiltinFunctionDeclaration(CanQualType resultType,689ArrayRef<CanQualType> argTypes) {690return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::None, argTypes,691FunctionType::ExtInfo(), {},692RequiredArgs::All);693}694695/// Arrange a call to a C++ method, passing the given arguments.696///697/// numPrefixArgs is the number of ABI-specific prefix arguments we have. It698/// does not count `this`.699const CGFunctionInfo &700CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,701const FunctionProtoType *proto,702RequiredArgs required,703unsigned numPrefixArgs) {704assert(numPrefixArgs + 1 <= args.size() &&705"Emitting a call with less args than the required prefix?");706// Add one to account for `this`. It's a bit awkward here, but we don't count707// `this` in similar places elsewhere.708auto paramInfos =709getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());710711// FIXME: Kill copy.712auto argTypes = getArgTypesForCall(Context, args);713714FunctionType::ExtInfo info = proto->getExtInfo();715return arrangeLLVMFunctionInfo(GetReturnType(proto->getReturnType()),716FnInfoOpts::IsInstanceMethod, argTypes, info,717paramInfos, required);718}719720const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {721return arrangeLLVMFunctionInfo(getContext().VoidTy, FnInfoOpts::None,722std::nullopt, FunctionType::ExtInfo(), {},723RequiredArgs::All);724}725726const CGFunctionInfo &727CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,728const CallArgList &args) {729assert(signature.arg_size() <= args.size());730if (signature.arg_size() == args.size())731return signature;732733SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;734auto sigParamInfos = signature.getExtParameterInfos();735if (!sigParamInfos.empty()) {736paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());737paramInfos.resize(args.size());738}739740auto argTypes = getArgTypesForCall(Context, args);741742assert(signature.getRequiredArgs().allowsOptionalArgs());743FnInfoOpts opts = FnInfoOpts::None;744if (signature.isInstanceMethod())745opts |= FnInfoOpts::IsInstanceMethod;746if (signature.isChainCall())747opts |= FnInfoOpts::IsChainCall;748if (signature.isDelegateCall())749opts |= FnInfoOpts::IsDelegateCall;750return arrangeLLVMFunctionInfo(signature.getReturnType(), opts, argTypes,751signature.getExtInfo(), paramInfos,752signature.getRequiredArgs());753}754755namespace clang {756namespace CodeGen {757void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);758}759}760761/// Arrange the argument and result information for an abstract value762/// of a given function type. This is the method which all of the763/// above functions ultimately defer to.764const CGFunctionInfo &CodeGenTypes::arrangeLLVMFunctionInfo(765CanQualType resultType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,766FunctionType::ExtInfo info,767ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,768RequiredArgs required) {769assert(llvm::all_of(argTypes,770[](CanQualType T) { return T.isCanonicalAsParam(); }));771772// Lookup or create unique function info.773llvm::FoldingSetNodeID ID;774bool isInstanceMethod =775(opts & FnInfoOpts::IsInstanceMethod) == FnInfoOpts::IsInstanceMethod;776bool isChainCall =777(opts & FnInfoOpts::IsChainCall) == FnInfoOpts::IsChainCall;778bool isDelegateCall =779(opts & FnInfoOpts::IsDelegateCall) == FnInfoOpts::IsDelegateCall;780CGFunctionInfo::Profile(ID, isInstanceMethod, isChainCall, isDelegateCall,781info, paramInfos, required, resultType, argTypes);782783void *insertPos = nullptr;784CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);785if (FI)786return *FI;787788unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());789790// Construct the function info. We co-allocate the ArgInfos.791FI = CGFunctionInfo::create(CC, isInstanceMethod, isChainCall, isDelegateCall,792info, paramInfos, resultType, argTypes, required);793FunctionInfos.InsertNode(FI, insertPos);794795bool inserted = FunctionsBeingProcessed.insert(FI).second;796(void)inserted;797assert(inserted && "Recursively being processed?");798799// Compute ABI information.800if (CC == llvm::CallingConv::SPIR_KERNEL) {801// Force target independent argument handling for the host visible802// kernel functions.803computeSPIRKernelABIInfo(CGM, *FI);804} else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {805swiftcall::computeABIInfo(CGM, *FI);806} else {807CGM.getABIInfo().computeInfo(*FI);808}809810// Loop over all of the computed argument and return value info. If any of811// them are direct or extend without a specified coerce type, specify the812// default now.813ABIArgInfo &retInfo = FI->getReturnInfo();814if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr)815retInfo.setCoerceToType(ConvertType(FI->getReturnType()));816817for (auto &I : FI->arguments())818if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr)819I.info.setCoerceToType(ConvertType(I.type));820821bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;822assert(erased && "Not in set?");823824return *FI;825}826827CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, bool instanceMethod,828bool chainCall, bool delegateCall,829const FunctionType::ExtInfo &info,830ArrayRef<ExtParameterInfo> paramInfos,831CanQualType resultType,832ArrayRef<CanQualType> argTypes,833RequiredArgs required) {834assert(paramInfos.empty() || paramInfos.size() == argTypes.size());835assert(!required.allowsOptionalArgs() ||836required.getNumRequiredArgs() <= argTypes.size());837838void *buffer =839operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(840argTypes.size() + 1, paramInfos.size()));841842CGFunctionInfo *FI = new(buffer) CGFunctionInfo();843FI->CallingConvention = llvmCC;844FI->EffectiveCallingConvention = llvmCC;845FI->ASTCallingConvention = info.getCC();846FI->InstanceMethod = instanceMethod;847FI->ChainCall = chainCall;848FI->DelegateCall = delegateCall;849FI->CmseNSCall = info.getCmseNSCall();850FI->NoReturn = info.getNoReturn();851FI->ReturnsRetained = info.getProducesResult();852FI->NoCallerSavedRegs = info.getNoCallerSavedRegs();853FI->NoCfCheck = info.getNoCfCheck();854FI->Required = required;855FI->HasRegParm = info.getHasRegParm();856FI->RegParm = info.getRegParm();857FI->ArgStruct = nullptr;858FI->ArgStructAlign = 0;859FI->NumArgs = argTypes.size();860FI->HasExtParameterInfos = !paramInfos.empty();861FI->getArgsBuffer()[0].type = resultType;862FI->MaxVectorWidth = 0;863for (unsigned i = 0, e = argTypes.size(); i != e; ++i)864FI->getArgsBuffer()[i + 1].type = argTypes[i];865for (unsigned i = 0, e = paramInfos.size(); i != e; ++i)866FI->getExtParameterInfosBuffer()[i] = paramInfos[i];867return FI;868}869870/***/871872namespace {873// ABIArgInfo::Expand implementation.874875// Specifies the way QualType passed as ABIArgInfo::Expand is expanded.876struct TypeExpansion {877enum TypeExpansionKind {878// Elements of constant arrays are expanded recursively.879TEK_ConstantArray,880// Record fields are expanded recursively (but if record is a union, only881// the field with the largest size is expanded).882TEK_Record,883// For complex types, real and imaginary parts are expanded recursively.884TEK_Complex,885// All other types are not expandable.886TEK_None887};888889const TypeExpansionKind Kind;890891TypeExpansion(TypeExpansionKind K) : Kind(K) {}892virtual ~TypeExpansion() {}893};894895struct ConstantArrayExpansion : TypeExpansion {896QualType EltTy;897uint64_t NumElts;898899ConstantArrayExpansion(QualType EltTy, uint64_t NumElts)900: TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}901static bool classof(const TypeExpansion *TE) {902return TE->Kind == TEK_ConstantArray;903}904};905906struct RecordExpansion : TypeExpansion {907SmallVector<const CXXBaseSpecifier *, 1> Bases;908909SmallVector<const FieldDecl *, 1> Fields;910911RecordExpansion(SmallVector<const CXXBaseSpecifier *, 1> &&Bases,912SmallVector<const FieldDecl *, 1> &&Fields)913: TypeExpansion(TEK_Record), Bases(std::move(Bases)),914Fields(std::move(Fields)) {}915static bool classof(const TypeExpansion *TE) {916return TE->Kind == TEK_Record;917}918};919920struct ComplexExpansion : TypeExpansion {921QualType EltTy;922923ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {}924static bool classof(const TypeExpansion *TE) {925return TE->Kind == TEK_Complex;926}927};928929struct NoExpansion : TypeExpansion {930NoExpansion() : TypeExpansion(TEK_None) {}931static bool classof(const TypeExpansion *TE) {932return TE->Kind == TEK_None;933}934};935} // namespace936937static std::unique_ptr<TypeExpansion>938getTypeExpansion(QualType Ty, const ASTContext &Context) {939if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {940return std::make_unique<ConstantArrayExpansion>(AT->getElementType(),941AT->getZExtSize());942}943if (const RecordType *RT = Ty->getAs<RecordType>()) {944SmallVector<const CXXBaseSpecifier *, 1> Bases;945SmallVector<const FieldDecl *, 1> Fields;946const RecordDecl *RD = RT->getDecl();947assert(!RD->hasFlexibleArrayMember() &&948"Cannot expand structure with flexible array.");949if (RD->isUnion()) {950// Unions can be here only in degenerative cases - all the fields are same951// after flattening. Thus we have to use the "largest" field.952const FieldDecl *LargestFD = nullptr;953CharUnits UnionSize = CharUnits::Zero();954955for (const auto *FD : RD->fields()) {956if (FD->isZeroLengthBitField(Context))957continue;958assert(!FD->isBitField() &&959"Cannot expand structure with bit-field members.");960CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType());961if (UnionSize < FieldSize) {962UnionSize = FieldSize;963LargestFD = FD;964}965}966if (LargestFD)967Fields.push_back(LargestFD);968} else {969if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {970assert(!CXXRD->isDynamicClass() &&971"cannot expand vtable pointers in dynamic classes");972llvm::append_range(Bases, llvm::make_pointer_range(CXXRD->bases()));973}974975for (const auto *FD : RD->fields()) {976if (FD->isZeroLengthBitField(Context))977continue;978assert(!FD->isBitField() &&979"Cannot expand structure with bit-field members.");980Fields.push_back(FD);981}982}983return std::make_unique<RecordExpansion>(std::move(Bases),984std::move(Fields));985}986if (const ComplexType *CT = Ty->getAs<ComplexType>()) {987return std::make_unique<ComplexExpansion>(CT->getElementType());988}989return std::make_unique<NoExpansion>();990}991992static int getExpansionSize(QualType Ty, const ASTContext &Context) {993auto Exp = getTypeExpansion(Ty, Context);994if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {995return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);996}997if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {998int Res = 0;999for (auto BS : RExp->Bases)1000Res += getExpansionSize(BS->getType(), Context);1001for (auto FD : RExp->Fields)1002Res += getExpansionSize(FD->getType(), Context);1003return Res;1004}1005if (isa<ComplexExpansion>(Exp.get()))1006return 2;1007assert(isa<NoExpansion>(Exp.get()));1008return 1;1009}10101011void1012CodeGenTypes::getExpandedTypes(QualType Ty,1013SmallVectorImpl<llvm::Type *>::iterator &TI) {1014auto Exp = getTypeExpansion(Ty, Context);1015if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {1016for (int i = 0, n = CAExp->NumElts; i < n; i++) {1017getExpandedTypes(CAExp->EltTy, TI);1018}1019} else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {1020for (auto BS : RExp->Bases)1021getExpandedTypes(BS->getType(), TI);1022for (auto FD : RExp->Fields)1023getExpandedTypes(FD->getType(), TI);1024} else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {1025llvm::Type *EltTy = ConvertType(CExp->EltTy);1026*TI++ = EltTy;1027*TI++ = EltTy;1028} else {1029assert(isa<NoExpansion>(Exp.get()));1030*TI++ = ConvertType(Ty);1031}1032}10331034static void forConstantArrayExpansion(CodeGenFunction &CGF,1035ConstantArrayExpansion *CAE,1036Address BaseAddr,1037llvm::function_ref<void(Address)> Fn) {1038for (int i = 0, n = CAE->NumElts; i < n; i++) {1039Address EltAddr = CGF.Builder.CreateConstGEP2_32(BaseAddr, 0, i);1040Fn(EltAddr);1041}1042}10431044void CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,1045llvm::Function::arg_iterator &AI) {1046assert(LV.isSimple() &&1047"Unexpected non-simple lvalue during struct expansion.");10481049auto Exp = getTypeExpansion(Ty, getContext());1050if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {1051forConstantArrayExpansion(1052*this, CAExp, LV.getAddress(), [&](Address EltAddr) {1053LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);1054ExpandTypeFromArgs(CAExp->EltTy, LV, AI);1055});1056} else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {1057Address This = LV.getAddress();1058for (const CXXBaseSpecifier *BS : RExp->Bases) {1059// Perform a single step derived-to-base conversion.1060Address Base =1061GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,1062/*NullCheckValue=*/false, SourceLocation());1063LValue SubLV = MakeAddrLValue(Base, BS->getType());10641065// Recurse onto bases.1066ExpandTypeFromArgs(BS->getType(), SubLV, AI);1067}1068for (auto FD : RExp->Fields) {1069// FIXME: What are the right qualifiers here?1070LValue SubLV = EmitLValueForFieldInitialization(LV, FD);1071ExpandTypeFromArgs(FD->getType(), SubLV, AI);1072}1073} else if (isa<ComplexExpansion>(Exp.get())) {1074auto realValue = &*AI++;1075auto imagValue = &*AI++;1076EmitStoreOfComplex(ComplexPairTy(realValue, imagValue), LV, /*init*/ true);1077} else {1078// Call EmitStoreOfScalar except when the lvalue is a bitfield to emit a1079// primitive store.1080assert(isa<NoExpansion>(Exp.get()));1081llvm::Value *Arg = &*AI++;1082if (LV.isBitField()) {1083EmitStoreThroughLValue(RValue::get(Arg), LV);1084} else {1085// TODO: currently there are some places are inconsistent in what LLVM1086// pointer type they use (see D118744). Once clang uses opaque pointers1087// all LLVM pointer types will be the same and we can remove this check.1088if (Arg->getType()->isPointerTy()) {1089Address Addr = LV.getAddress();1090Arg = Builder.CreateBitCast(Arg, Addr.getElementType());1091}1092EmitStoreOfScalar(Arg, LV);1093}1094}1095}10961097void CodeGenFunction::ExpandTypeToArgs(1098QualType Ty, CallArg Arg, llvm::FunctionType *IRFuncTy,1099SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) {1100auto Exp = getTypeExpansion(Ty, getContext());1101if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {1102Address Addr = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()1103: Arg.getKnownRValue().getAggregateAddress();1104forConstantArrayExpansion(1105*this, CAExp, Addr, [&](Address EltAddr) {1106CallArg EltArg = CallArg(1107convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()),1108CAExp->EltTy);1109ExpandTypeToArgs(CAExp->EltTy, EltArg, IRFuncTy, IRCallArgs,1110IRCallArgPos);1111});1112} else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {1113Address This = Arg.hasLValue() ? Arg.getKnownLValue().getAddress()1114: Arg.getKnownRValue().getAggregateAddress();1115for (const CXXBaseSpecifier *BS : RExp->Bases) {1116// Perform a single step derived-to-base conversion.1117Address Base =1118GetAddressOfBaseClass(This, Ty->getAsCXXRecordDecl(), &BS, &BS + 1,1119/*NullCheckValue=*/false, SourceLocation());1120CallArg BaseArg = CallArg(RValue::getAggregate(Base), BS->getType());11211122// Recurse onto bases.1123ExpandTypeToArgs(BS->getType(), BaseArg, IRFuncTy, IRCallArgs,1124IRCallArgPos);1125}11261127LValue LV = MakeAddrLValue(This, Ty);1128for (auto FD : RExp->Fields) {1129CallArg FldArg =1130CallArg(EmitRValueForField(LV, FD, SourceLocation()), FD->getType());1131ExpandTypeToArgs(FD->getType(), FldArg, IRFuncTy, IRCallArgs,1132IRCallArgPos);1133}1134} else if (isa<ComplexExpansion>(Exp.get())) {1135ComplexPairTy CV = Arg.getKnownRValue().getComplexVal();1136IRCallArgs[IRCallArgPos++] = CV.first;1137IRCallArgs[IRCallArgPos++] = CV.second;1138} else {1139assert(isa<NoExpansion>(Exp.get()));1140auto RV = Arg.getKnownRValue();1141assert(RV.isScalar() &&1142"Unexpected non-scalar rvalue during struct expansion.");11431144// Insert a bitcast as needed.1145llvm::Value *V = RV.getScalarVal();1146if (IRCallArgPos < IRFuncTy->getNumParams() &&1147V->getType() != IRFuncTy->getParamType(IRCallArgPos))1148V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));11491150IRCallArgs[IRCallArgPos++] = V;1151}1152}11531154/// Create a temporary allocation for the purposes of coercion.1155static RawAddress CreateTempAllocaForCoercion(CodeGenFunction &CGF,1156llvm::Type *Ty,1157CharUnits MinAlign,1158const Twine &Name = "tmp") {1159// Don't use an alignment that's worse than what LLVM would prefer.1160auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(Ty);1161CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));11621163return CGF.CreateTempAlloca(Ty, Align, Name + ".coerce");1164}11651166/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are1167/// accessing some number of bytes out of it, try to gep into the struct to get1168/// at its inner goodness. Dive as deep as possible without entering an element1169/// with an in-memory size smaller than DstSize.1170static Address1171EnterStructPointerForCoercedAccess(Address SrcPtr,1172llvm::StructType *SrcSTy,1173uint64_t DstSize, CodeGenFunction &CGF) {1174// We can't dive into a zero-element struct.1175if (SrcSTy->getNumElements() == 0) return SrcPtr;11761177llvm::Type *FirstElt = SrcSTy->getElementType(0);11781179// If the first elt is at least as large as what we're looking for, or if the1180// first element is the same size as the whole struct, we can enter it. The1181// comparison must be made on the store size and not the alloca size. Using1182// the alloca size may overstate the size of the load.1183uint64_t FirstEltSize =1184CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt);1185if (FirstEltSize < DstSize &&1186FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy))1187return SrcPtr;11881189// GEP into the first element.1190SrcPtr = CGF.Builder.CreateStructGEP(SrcPtr, 0, "coerce.dive");11911192// If the first element is a struct, recurse.1193llvm::Type *SrcTy = SrcPtr.getElementType();1194if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))1195return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);11961197return SrcPtr;1198}11991200/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both1201/// are either integers or pointers. This does a truncation of the value if it1202/// is too large or a zero extension if it is too small.1203///1204/// This behaves as if the value were coerced through memory, so on big-endian1205/// targets the high bits are preserved in a truncation, while little-endian1206/// targets preserve the low bits.1207static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,1208llvm::Type *Ty,1209CodeGenFunction &CGF) {1210if (Val->getType() == Ty)1211return Val;12121213if (isa<llvm::PointerType>(Val->getType())) {1214// If this is Pointer->Pointer avoid conversion to and from int.1215if (isa<llvm::PointerType>(Ty))1216return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");12171218// Convert the pointer to an integer so we can play with its width.1219Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");1220}12211222llvm::Type *DestIntTy = Ty;1223if (isa<llvm::PointerType>(DestIntTy))1224DestIntTy = CGF.IntPtrTy;12251226if (Val->getType() != DestIntTy) {1227const llvm::DataLayout &DL = CGF.CGM.getDataLayout();1228if (DL.isBigEndian()) {1229// Preserve the high bits on big-endian targets.1230// That is what memory coercion does.1231uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());1232uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);12331234if (SrcSize > DstSize) {1235Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits");1236Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii");1237} else {1238Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii");1239Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits");1240}1241} else {1242// Little-endian targets preserve the low bits. No shifts required.1243Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");1244}1245}12461247if (isa<llvm::PointerType>(Ty))1248Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");1249return Val;1250}1251125212531254/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as1255/// a pointer to an object of type \arg Ty, known to be aligned to1256/// \arg SrcAlign bytes.1257///1258/// This safely handles the case when the src type is smaller than the1259/// destination type; in this situation the values of bits which not1260/// present in the src are undefined.1261static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,1262CodeGenFunction &CGF) {1263llvm::Type *SrcTy = Src.getElementType();12641265// If SrcTy and Ty are the same, just do a load.1266if (SrcTy == Ty)1267return CGF.Builder.CreateLoad(Src);12681269llvm::TypeSize DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);12701271if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {1272Src = EnterStructPointerForCoercedAccess(Src, SrcSTy,1273DstSize.getFixedValue(), CGF);1274SrcTy = Src.getElementType();1275}12761277llvm::TypeSize SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);12781279// If the source and destination are integer or pointer types, just do an1280// extension or truncation to the desired type.1281if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&1282(isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {1283llvm::Value *Load = CGF.Builder.CreateLoad(Src);1284return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);1285}12861287// If load is legal, just bitcast the src pointer.1288if (!SrcSize.isScalable() && !DstSize.isScalable() &&1289SrcSize.getFixedValue() >= DstSize.getFixedValue()) {1290// Generally SrcSize is never greater than DstSize, since this means we are1291// losing bits. However, this can happen in cases where the structure has1292// additional padding, for example due to a user specified alignment.1293//1294// FIXME: Assert that we aren't truncating non-padding bits when have access1295// to that information.1296Src = Src.withElementType(Ty);1297return CGF.Builder.CreateLoad(Src);1298}12991300// If coercing a fixed vector to a scalable vector for ABI compatibility, and1301// the types match, use the llvm.vector.insert intrinsic to perform the1302// conversion.1303if (auto *ScalableDstTy = dyn_cast<llvm::ScalableVectorType>(Ty)) {1304if (auto *FixedSrcTy = dyn_cast<llvm::FixedVectorType>(SrcTy)) {1305// If we are casting a fixed i8 vector to a scalable i1 predicate1306// vector, use a vector insert and bitcast the result.1307if (ScalableDstTy->getElementType()->isIntegerTy(1) &&1308ScalableDstTy->getElementCount().isKnownMultipleOf(8) &&1309FixedSrcTy->getElementType()->isIntegerTy(8)) {1310ScalableDstTy = llvm::ScalableVectorType::get(1311FixedSrcTy->getElementType(),1312ScalableDstTy->getElementCount().getKnownMinValue() / 8);1313}1314if (ScalableDstTy->getElementType() == FixedSrcTy->getElementType()) {1315auto *Load = CGF.Builder.CreateLoad(Src);1316auto *UndefVec = llvm::UndefValue::get(ScalableDstTy);1317auto *Zero = llvm::Constant::getNullValue(CGF.CGM.Int64Ty);1318llvm::Value *Result = CGF.Builder.CreateInsertVector(1319ScalableDstTy, UndefVec, Load, Zero, "cast.scalable");1320if (ScalableDstTy != Ty)1321Result = CGF.Builder.CreateBitCast(Result, Ty);1322return Result;1323}1324}1325}13261327// Otherwise do coercion through memory. This is stupid, but simple.1328RawAddress Tmp =1329CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment(), Src.getName());1330CGF.Builder.CreateMemCpy(1331Tmp.getPointer(), Tmp.getAlignment().getAsAlign(),1332Src.emitRawPointer(CGF), Src.getAlignment().getAsAlign(),1333llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize.getKnownMinValue()));1334return CGF.Builder.CreateLoad(Tmp);1335}13361337void CodeGenFunction::CreateCoercedStore(llvm::Value *Src, Address Dst,1338llvm::TypeSize DstSize,1339bool DstIsVolatile) {1340if (!DstSize)1341return;13421343llvm::Type *SrcTy = Src->getType();1344llvm::TypeSize SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);13451346// GEP into structs to try to make types match.1347// FIXME: This isn't really that useful with opaque types, but it impacts a1348// lot of regression tests.1349if (SrcTy != Dst.getElementType()) {1350if (llvm::StructType *DstSTy =1351dyn_cast<llvm::StructType>(Dst.getElementType())) {1352assert(!SrcSize.isScalable());1353Dst = EnterStructPointerForCoercedAccess(Dst, DstSTy,1354SrcSize.getFixedValue(), *this);1355}1356}13571358if (SrcSize.isScalable() || SrcSize <= DstSize) {1359if (SrcTy->isIntegerTy() && Dst.getElementType()->isPointerTy() &&1360SrcSize == CGM.getDataLayout().getTypeAllocSize(Dst.getElementType())) {1361// If the value is supposed to be a pointer, convert it before storing it.1362Src = CoerceIntOrPtrToIntOrPtr(Src, Dst.getElementType(), *this);1363Builder.CreateStore(Src, Dst, DstIsVolatile);1364} else if (llvm::StructType *STy =1365dyn_cast<llvm::StructType>(Src->getType())) {1366// Prefer scalar stores to first-class aggregate stores.1367Dst = Dst.withElementType(SrcTy);1368for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {1369Address EltPtr = Builder.CreateStructGEP(Dst, i);1370llvm::Value *Elt = Builder.CreateExtractValue(Src, i);1371Builder.CreateStore(Elt, EltPtr, DstIsVolatile);1372}1373} else {1374Builder.CreateStore(Src, Dst.withElementType(SrcTy), DstIsVolatile);1375}1376} else if (SrcTy->isIntegerTy()) {1377// If the source is a simple integer, coerce it directly.1378llvm::Type *DstIntTy = Builder.getIntNTy(DstSize.getFixedValue() * 8);1379Src = CoerceIntOrPtrToIntOrPtr(Src, DstIntTy, *this);1380Builder.CreateStore(Src, Dst.withElementType(DstIntTy), DstIsVolatile);1381} else {1382// Otherwise do coercion through memory. This is stupid, but1383// simple.13841385// Generally SrcSize is never greater than DstSize, since this means we are1386// losing bits. However, this can happen in cases where the structure has1387// additional padding, for example due to a user specified alignment.1388//1389// FIXME: Assert that we aren't truncating non-padding bits when have access1390// to that information.1391RawAddress Tmp =1392CreateTempAllocaForCoercion(*this, SrcTy, Dst.getAlignment());1393Builder.CreateStore(Src, Tmp);1394Builder.CreateMemCpy(Dst.emitRawPointer(*this),1395Dst.getAlignment().getAsAlign(), Tmp.getPointer(),1396Tmp.getAlignment().getAsAlign(),1397Builder.CreateTypeSize(IntPtrTy, DstSize));1398}1399}14001401static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr,1402const ABIArgInfo &info) {1403if (unsigned offset = info.getDirectOffset()) {1404addr = addr.withElementType(CGF.Int8Ty);1405addr = CGF.Builder.CreateConstInBoundsByteGEP(addr,1406CharUnits::fromQuantity(offset));1407addr = addr.withElementType(info.getCoerceToType());1408}1409return addr;1410}14111412namespace {14131414/// Encapsulates information about the way function arguments from1415/// CGFunctionInfo should be passed to actual LLVM IR function.1416class ClangToLLVMArgMapping {1417static const unsigned InvalidIndex = ~0U;1418unsigned InallocaArgNo;1419unsigned SRetArgNo;1420unsigned TotalIRArgs;14211422/// Arguments of LLVM IR function corresponding to single Clang argument.1423struct IRArgs {1424unsigned PaddingArgIndex;1425// Argument is expanded to IR arguments at positions1426// [FirstArgIndex, FirstArgIndex + NumberOfArgs).1427unsigned FirstArgIndex;1428unsigned NumberOfArgs;14291430IRArgs()1431: PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),1432NumberOfArgs(0) {}1433};14341435SmallVector<IRArgs, 8> ArgInfo;14361437public:1438ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI,1439bool OnlyRequiredArgs = false)1440: InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),1441ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {1442construct(Context, FI, OnlyRequiredArgs);1443}14441445bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; }1446unsigned getInallocaArgNo() const {1447assert(hasInallocaArg());1448return InallocaArgNo;1449}14501451bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }1452unsigned getSRetArgNo() const {1453assert(hasSRetArg());1454return SRetArgNo;1455}14561457unsigned totalIRArgs() const { return TotalIRArgs; }14581459bool hasPaddingArg(unsigned ArgNo) const {1460assert(ArgNo < ArgInfo.size());1461return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;1462}1463unsigned getPaddingArgNo(unsigned ArgNo) const {1464assert(hasPaddingArg(ArgNo));1465return ArgInfo[ArgNo].PaddingArgIndex;1466}14671468/// Returns index of first IR argument corresponding to ArgNo, and their1469/// quantity.1470std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const {1471assert(ArgNo < ArgInfo.size());1472return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,1473ArgInfo[ArgNo].NumberOfArgs);1474}14751476private:1477void construct(const ASTContext &Context, const CGFunctionInfo &FI,1478bool OnlyRequiredArgs);1479};14801481void ClangToLLVMArgMapping::construct(const ASTContext &Context,1482const CGFunctionInfo &FI,1483bool OnlyRequiredArgs) {1484unsigned IRArgNo = 0;1485bool SwapThisWithSRet = false;1486const ABIArgInfo &RetAI = FI.getReturnInfo();14871488if (RetAI.getKind() == ABIArgInfo::Indirect) {1489SwapThisWithSRet = RetAI.isSRetAfterThis();1490SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;1491}14921493unsigned ArgNo = 0;1494unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size();1495for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs;1496++I, ++ArgNo) {1497assert(I != FI.arg_end());1498QualType ArgType = I->type;1499const ABIArgInfo &AI = I->info;1500// Collect data about IR arguments corresponding to Clang argument ArgNo.1501auto &IRArgs = ArgInfo[ArgNo];15021503if (AI.getPaddingType())1504IRArgs.PaddingArgIndex = IRArgNo++;15051506switch (AI.getKind()) {1507case ABIArgInfo::Extend:1508case ABIArgInfo::Direct: {1509// FIXME: handle sseregparm someday...1510llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType());1511if (AI.isDirect() && AI.getCanBeFlattened() && STy) {1512IRArgs.NumberOfArgs = STy->getNumElements();1513} else {1514IRArgs.NumberOfArgs = 1;1515}1516break;1517}1518case ABIArgInfo::Indirect:1519case ABIArgInfo::IndirectAliased:1520IRArgs.NumberOfArgs = 1;1521break;1522case ABIArgInfo::Ignore:1523case ABIArgInfo::InAlloca:1524// ignore and inalloca doesn't have matching LLVM parameters.1525IRArgs.NumberOfArgs = 0;1526break;1527case ABIArgInfo::CoerceAndExpand:1528IRArgs.NumberOfArgs = AI.getCoerceAndExpandTypeSequence().size();1529break;1530case ABIArgInfo::Expand:1531IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context);1532break;1533}15341535if (IRArgs.NumberOfArgs > 0) {1536IRArgs.FirstArgIndex = IRArgNo;1537IRArgNo += IRArgs.NumberOfArgs;1538}15391540// Skip over the sret parameter when it comes second. We already handled it1541// above.1542if (IRArgNo == 1 && SwapThisWithSRet)1543IRArgNo++;1544}1545assert(ArgNo == ArgInfo.size());15461547if (FI.usesInAlloca())1548InallocaArgNo = IRArgNo++;15491550TotalIRArgs = IRArgNo;1551}1552} // namespace15531554/***/15551556bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {1557const auto &RI = FI.getReturnInfo();1558return RI.isIndirect() || (RI.isInAlloca() && RI.getInAllocaSRet());1559}15601561bool CodeGenModule::ReturnTypeHasInReg(const CGFunctionInfo &FI) {1562const auto &RI = FI.getReturnInfo();1563return RI.getInReg();1564}15651566bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) {1567return ReturnTypeUsesSRet(FI) &&1568getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();1569}15701571bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {1572if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {1573switch (BT->getKind()) {1574default:1575return false;1576case BuiltinType::Float:1577return getTarget().useObjCFPRetForRealType(FloatModeKind::Float);1578case BuiltinType::Double:1579return getTarget().useObjCFPRetForRealType(FloatModeKind::Double);1580case BuiltinType::LongDouble:1581return getTarget().useObjCFPRetForRealType(FloatModeKind::LongDouble);1582}1583}15841585return false;1586}15871588bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {1589if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {1590if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {1591if (BT->getKind() == BuiltinType::LongDouble)1592return getTarget().useObjCFP2RetForComplexLongDouble();1593}1594}15951596return false;1597}15981599llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {1600const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);1601return GetFunctionType(FI);1602}16031604llvm::FunctionType *1605CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {16061607bool Inserted = FunctionsBeingProcessed.insert(&FI).second;1608(void)Inserted;1609assert(Inserted && "Recursively being processed?");16101611llvm::Type *resultType = nullptr;1612const ABIArgInfo &retAI = FI.getReturnInfo();1613switch (retAI.getKind()) {1614case ABIArgInfo::Expand:1615case ABIArgInfo::IndirectAliased:1616llvm_unreachable("Invalid ABI kind for return argument");16171618case ABIArgInfo::Extend:1619case ABIArgInfo::Direct:1620resultType = retAI.getCoerceToType();1621break;16221623case ABIArgInfo::InAlloca:1624if (retAI.getInAllocaSRet()) {1625// sret things on win32 aren't void, they return the sret pointer.1626QualType ret = FI.getReturnType();1627unsigned addressSpace = CGM.getTypes().getTargetAddressSpace(ret);1628resultType = llvm::PointerType::get(getLLVMContext(), addressSpace);1629} else {1630resultType = llvm::Type::getVoidTy(getLLVMContext());1631}1632break;16331634case ABIArgInfo::Indirect:1635case ABIArgInfo::Ignore:1636resultType = llvm::Type::getVoidTy(getLLVMContext());1637break;16381639case ABIArgInfo::CoerceAndExpand:1640resultType = retAI.getUnpaddedCoerceAndExpandType();1641break;1642}16431644ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true);1645SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs());16461647// Add type for sret argument.1648if (IRFunctionArgs.hasSRetArg()) {1649QualType Ret = FI.getReturnType();1650unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(Ret);1651ArgTypes[IRFunctionArgs.getSRetArgNo()] =1652llvm::PointerType::get(getLLVMContext(), AddressSpace);1653}16541655// Add type for inalloca argument.1656if (IRFunctionArgs.hasInallocaArg())1657ArgTypes[IRFunctionArgs.getInallocaArgNo()] =1658llvm::PointerType::getUnqual(getLLVMContext());16591660// Add in all of the required arguments.1661unsigned ArgNo = 0;1662CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),1663ie = it + FI.getNumRequiredArgs();1664for (; it != ie; ++it, ++ArgNo) {1665const ABIArgInfo &ArgInfo = it->info;16661667// Insert a padding type to ensure proper alignment.1668if (IRFunctionArgs.hasPaddingArg(ArgNo))1669ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =1670ArgInfo.getPaddingType();16711672unsigned FirstIRArg, NumIRArgs;1673std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);16741675switch (ArgInfo.getKind()) {1676case ABIArgInfo::Ignore:1677case ABIArgInfo::InAlloca:1678assert(NumIRArgs == 0);1679break;16801681case ABIArgInfo::Indirect:1682assert(NumIRArgs == 1);1683// indirect arguments are always on the stack, which is alloca addr space.1684ArgTypes[FirstIRArg] = llvm::PointerType::get(1685getLLVMContext(), CGM.getDataLayout().getAllocaAddrSpace());1686break;1687case ABIArgInfo::IndirectAliased:1688assert(NumIRArgs == 1);1689ArgTypes[FirstIRArg] = llvm::PointerType::get(1690getLLVMContext(), ArgInfo.getIndirectAddrSpace());1691break;1692case ABIArgInfo::Extend:1693case ABIArgInfo::Direct: {1694// Fast-isel and the optimizer generally like scalar values better than1695// FCAs, so we flatten them if this is safe to do for this argument.1696llvm::Type *argType = ArgInfo.getCoerceToType();1697llvm::StructType *st = dyn_cast<llvm::StructType>(argType);1698if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {1699assert(NumIRArgs == st->getNumElements());1700for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)1701ArgTypes[FirstIRArg + i] = st->getElementType(i);1702} else {1703assert(NumIRArgs == 1);1704ArgTypes[FirstIRArg] = argType;1705}1706break;1707}17081709case ABIArgInfo::CoerceAndExpand: {1710auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;1711for (auto *EltTy : ArgInfo.getCoerceAndExpandTypeSequence()) {1712*ArgTypesIter++ = EltTy;1713}1714assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);1715break;1716}17171718case ABIArgInfo::Expand:1719auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;1720getExpandedTypes(it->type, ArgTypesIter);1721assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);1722break;1723}1724}17251726bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;1727assert(Erased && "Not in set?");17281729return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic());1730}17311732llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {1733const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());1734const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();17351736if (!isFuncTypeConvertible(FPT))1737return llvm::StructType::get(getLLVMContext());17381739return GetFunctionType(GD);1740}17411742static void AddAttributesFromFunctionProtoType(ASTContext &Ctx,1743llvm::AttrBuilder &FuncAttrs,1744const FunctionProtoType *FPT) {1745if (!FPT)1746return;17471748if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&1749FPT->isNothrow())1750FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);17511752unsigned SMEBits = FPT->getAArch64SMEAttributes();1753if (SMEBits & FunctionType::SME_PStateSMEnabledMask)1754FuncAttrs.addAttribute("aarch64_pstate_sm_enabled");1755if (SMEBits & FunctionType::SME_PStateSMCompatibleMask)1756FuncAttrs.addAttribute("aarch64_pstate_sm_compatible");17571758// ZA1759if (FunctionType::getArmZAState(SMEBits) == FunctionType::ARM_Preserves)1760FuncAttrs.addAttribute("aarch64_preserves_za");1761if (FunctionType::getArmZAState(SMEBits) == FunctionType::ARM_In)1762FuncAttrs.addAttribute("aarch64_in_za");1763if (FunctionType::getArmZAState(SMEBits) == FunctionType::ARM_Out)1764FuncAttrs.addAttribute("aarch64_out_za");1765if (FunctionType::getArmZAState(SMEBits) == FunctionType::ARM_InOut)1766FuncAttrs.addAttribute("aarch64_inout_za");17671768// ZT01769if (FunctionType::getArmZT0State(SMEBits) == FunctionType::ARM_Preserves)1770FuncAttrs.addAttribute("aarch64_preserves_zt0");1771if (FunctionType::getArmZT0State(SMEBits) == FunctionType::ARM_In)1772FuncAttrs.addAttribute("aarch64_in_zt0");1773if (FunctionType::getArmZT0State(SMEBits) == FunctionType::ARM_Out)1774FuncAttrs.addAttribute("aarch64_out_zt0");1775if (FunctionType::getArmZT0State(SMEBits) == FunctionType::ARM_InOut)1776FuncAttrs.addAttribute("aarch64_inout_zt0");1777}17781779static void AddAttributesFromOMPAssumes(llvm::AttrBuilder &FuncAttrs,1780const Decl *Callee) {1781if (!Callee)1782return;17831784SmallVector<StringRef, 4> Attrs;17851786for (const OMPAssumeAttr *AA : Callee->specific_attrs<OMPAssumeAttr>())1787AA->getAssumption().split(Attrs, ",");17881789if (!Attrs.empty())1790FuncAttrs.addAttribute(llvm::AssumptionAttrKey,1791llvm::join(Attrs.begin(), Attrs.end(), ","));1792}17931794bool CodeGenModule::MayDropFunctionReturn(const ASTContext &Context,1795QualType ReturnType) const {1796// We can't just discard the return value for a record type with a1797// complex destructor or a non-trivially copyable type.1798if (const RecordType *RT =1799ReturnType.getCanonicalType()->getAs<RecordType>()) {1800if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl()))1801return ClassDecl->hasTrivialDestructor();1802}1803return ReturnType.isTriviallyCopyableType(Context);1804}18051806static bool HasStrictReturn(const CodeGenModule &Module, QualType RetTy,1807const Decl *TargetDecl) {1808// As-is msan can not tolerate noundef mismatch between caller and1809// implementation. Mismatch is possible for e.g. indirect calls from C-caller1810// into C++. Such mismatches lead to confusing false reports. To avoid1811// expensive workaround on msan we enforce initialization event in uncommon1812// cases where it's allowed.1813if (Module.getLangOpts().Sanitize.has(SanitizerKind::Memory))1814return true;1815// C++ explicitly makes returning undefined values UB. C's rule only applies1816// to used values, so we never mark them noundef for now.1817if (!Module.getLangOpts().CPlusPlus)1818return false;1819if (TargetDecl) {1820if (const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(TargetDecl)) {1821if (FDecl->isExternC())1822return false;1823} else if (const VarDecl *VDecl = dyn_cast<VarDecl>(TargetDecl)) {1824// Function pointer.1825if (VDecl->isExternC())1826return false;1827}1828}18291830// We don't want to be too aggressive with the return checking, unless1831// it's explicit in the code opts or we're using an appropriate sanitizer.1832// Try to respect what the programmer intended.1833return Module.getCodeGenOpts().StrictReturn ||1834!Module.MayDropFunctionReturn(Module.getContext(), RetTy) ||1835Module.getLangOpts().Sanitize.has(SanitizerKind::Return);1836}18371838/// Add denormal-fp-math and denormal-fp-math-f32 as appropriate for the1839/// requested denormal behavior, accounting for the overriding behavior of the1840/// -f32 case.1841static void addDenormalModeAttrs(llvm::DenormalMode FPDenormalMode,1842llvm::DenormalMode FP32DenormalMode,1843llvm::AttrBuilder &FuncAttrs) {1844if (FPDenormalMode != llvm::DenormalMode::getDefault())1845FuncAttrs.addAttribute("denormal-fp-math", FPDenormalMode.str());18461847if (FP32DenormalMode != FPDenormalMode && FP32DenormalMode.isValid())1848FuncAttrs.addAttribute("denormal-fp-math-f32", FP32DenormalMode.str());1849}18501851/// Add default attributes to a function, which have merge semantics under1852/// -mlink-builtin-bitcode and should not simply overwrite any existing1853/// attributes in the linked library.1854static void1855addMergableDefaultFunctionAttributes(const CodeGenOptions &CodeGenOpts,1856llvm::AttrBuilder &FuncAttrs) {1857addDenormalModeAttrs(CodeGenOpts.FPDenormalMode, CodeGenOpts.FP32DenormalMode,1858FuncAttrs);1859}18601861static void getTrivialDefaultFunctionAttributes(1862StringRef Name, bool HasOptnone, const CodeGenOptions &CodeGenOpts,1863const LangOptions &LangOpts, bool AttrOnCallSite,1864llvm::AttrBuilder &FuncAttrs) {1865// OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed.1866if (!HasOptnone) {1867if (CodeGenOpts.OptimizeSize)1868FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);1869if (CodeGenOpts.OptimizeSize == 2)1870FuncAttrs.addAttribute(llvm::Attribute::MinSize);1871}18721873if (CodeGenOpts.DisableRedZone)1874FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);1875if (CodeGenOpts.IndirectTlsSegRefs)1876FuncAttrs.addAttribute("indirect-tls-seg-refs");1877if (CodeGenOpts.NoImplicitFloat)1878FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);18791880if (AttrOnCallSite) {1881// Attributes that should go on the call site only.1882// FIXME: Look for 'BuiltinAttr' on the function rather than re-checking1883// the -fno-builtin-foo list.1884if (!CodeGenOpts.SimplifyLibCalls || LangOpts.isNoBuiltinFunc(Name))1885FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);1886if (!CodeGenOpts.TrapFuncName.empty())1887FuncAttrs.addAttribute("trap-func-name", CodeGenOpts.TrapFuncName);1888} else {1889switch (CodeGenOpts.getFramePointer()) {1890case CodeGenOptions::FramePointerKind::None:1891// This is the default behavior.1892break;1893case CodeGenOptions::FramePointerKind::Reserved:1894case CodeGenOptions::FramePointerKind::NonLeaf:1895case CodeGenOptions::FramePointerKind::All:1896FuncAttrs.addAttribute("frame-pointer",1897CodeGenOptions::getFramePointerKindName(1898CodeGenOpts.getFramePointer()));1899}19001901if (CodeGenOpts.LessPreciseFPMAD)1902FuncAttrs.addAttribute("less-precise-fpmad", "true");19031904if (CodeGenOpts.NullPointerIsValid)1905FuncAttrs.addAttribute(llvm::Attribute::NullPointerIsValid);19061907if (LangOpts.getDefaultExceptionMode() == LangOptions::FPE_Ignore)1908FuncAttrs.addAttribute("no-trapping-math", "true");19091910// TODO: Are these all needed?1911// unsafe/inf/nan/nsz are handled by instruction-level FastMathFlags.1912if (LangOpts.NoHonorInfs)1913FuncAttrs.addAttribute("no-infs-fp-math", "true");1914if (LangOpts.NoHonorNaNs)1915FuncAttrs.addAttribute("no-nans-fp-math", "true");1916if (LangOpts.ApproxFunc)1917FuncAttrs.addAttribute("approx-func-fp-math", "true");1918if (LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&1919LangOpts.NoSignedZero && LangOpts.ApproxFunc &&1920(LangOpts.getDefaultFPContractMode() ==1921LangOptions::FPModeKind::FPM_Fast ||1922LangOpts.getDefaultFPContractMode() ==1923LangOptions::FPModeKind::FPM_FastHonorPragmas))1924FuncAttrs.addAttribute("unsafe-fp-math", "true");1925if (CodeGenOpts.SoftFloat)1926FuncAttrs.addAttribute("use-soft-float", "true");1927FuncAttrs.addAttribute("stack-protector-buffer-size",1928llvm::utostr(CodeGenOpts.SSPBufferSize));1929if (LangOpts.NoSignedZero)1930FuncAttrs.addAttribute("no-signed-zeros-fp-math", "true");19311932// TODO: Reciprocal estimate codegen options should apply to instructions?1933const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;1934if (!Recips.empty())1935FuncAttrs.addAttribute("reciprocal-estimates",1936llvm::join(Recips, ","));19371938if (!CodeGenOpts.PreferVectorWidth.empty() &&1939CodeGenOpts.PreferVectorWidth != "none")1940FuncAttrs.addAttribute("prefer-vector-width",1941CodeGenOpts.PreferVectorWidth);19421943if (CodeGenOpts.StackRealignment)1944FuncAttrs.addAttribute("stackrealign");1945if (CodeGenOpts.Backchain)1946FuncAttrs.addAttribute("backchain");1947if (CodeGenOpts.EnableSegmentedStacks)1948FuncAttrs.addAttribute("split-stack");19491950if (CodeGenOpts.SpeculativeLoadHardening)1951FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);19521953// Add zero-call-used-regs attribute.1954switch (CodeGenOpts.getZeroCallUsedRegs()) {1955case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip:1956FuncAttrs.removeAttribute("zero-call-used-regs");1957break;1958case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg:1959FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr-arg");1960break;1961case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR:1962FuncAttrs.addAttribute("zero-call-used-regs", "used-gpr");1963break;1964case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg:1965FuncAttrs.addAttribute("zero-call-used-regs", "used-arg");1966break;1967case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used:1968FuncAttrs.addAttribute("zero-call-used-regs", "used");1969break;1970case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg:1971FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr-arg");1972break;1973case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR:1974FuncAttrs.addAttribute("zero-call-used-regs", "all-gpr");1975break;1976case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg:1977FuncAttrs.addAttribute("zero-call-used-regs", "all-arg");1978break;1979case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All:1980FuncAttrs.addAttribute("zero-call-used-regs", "all");1981break;1982}1983}19841985if (LangOpts.assumeFunctionsAreConvergent()) {1986// Conservatively, mark all functions and calls in CUDA and OpenCL as1987// convergent (meaning, they may call an intrinsically convergent op, such1988// as __syncthreads() / barrier(), and so can't have certain optimizations1989// applied around them). LLVM will remove this attribute where it safely1990// can.1991FuncAttrs.addAttribute(llvm::Attribute::Convergent);1992}19931994// TODO: NoUnwind attribute should be added for other GPU modes HIP,1995// OpenMP offload. AFAIK, neither of them support exceptions in device code.1996if ((LangOpts.CUDA && LangOpts.CUDAIsDevice) || LangOpts.OpenCL ||1997LangOpts.SYCLIsDevice) {1998FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);1999}20002001for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {2002StringRef Var, Value;2003std::tie(Var, Value) = Attr.split('=');2004FuncAttrs.addAttribute(Var, Value);2005}20062007TargetInfo::BranchProtectionInfo BPI(LangOpts);2008TargetCodeGenInfo::initBranchProtectionFnAttributes(BPI, FuncAttrs);2009}20102011/// Merges `target-features` from \TargetOpts and \F, and sets the result in2012/// \FuncAttr2013/// * features from \F are always kept2014/// * a feature from \TargetOpts is kept if itself and its opposite are absent2015/// from \F2016static void2017overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder &FuncAttr,2018const llvm::Function &F,2019const TargetOptions &TargetOpts) {2020auto FFeatures = F.getFnAttribute("target-features");20212022llvm::StringSet<> MergedNames;2023SmallVector<StringRef> MergedFeatures;2024MergedFeatures.reserve(TargetOpts.Features.size());20252026auto AddUnmergedFeatures = [&](auto &&FeatureRange) {2027for (StringRef Feature : FeatureRange) {2028if (Feature.empty())2029continue;2030assert(Feature[0] == '+' || Feature[0] == '-');2031StringRef Name = Feature.drop_front(1);2032bool Merged = !MergedNames.insert(Name).second;2033if (!Merged)2034MergedFeatures.push_back(Feature);2035}2036};20372038if (FFeatures.isValid())2039AddUnmergedFeatures(llvm::split(FFeatures.getValueAsString(), ','));2040AddUnmergedFeatures(TargetOpts.Features);20412042if (!MergedFeatures.empty()) {2043llvm::sort(MergedFeatures);2044FuncAttr.addAttribute("target-features", llvm::join(MergedFeatures, ","));2045}2046}20472048void CodeGen::mergeDefaultFunctionDefinitionAttributes(2049llvm::Function &F, const CodeGenOptions &CodeGenOpts,2050const LangOptions &LangOpts, const TargetOptions &TargetOpts,2051bool WillInternalize) {20522053llvm::AttrBuilder FuncAttrs(F.getContext());2054// Here we only extract the options that are relevant compared to the version2055// from GetCPUAndFeaturesAttributes.2056if (!TargetOpts.CPU.empty())2057FuncAttrs.addAttribute("target-cpu", TargetOpts.CPU);2058if (!TargetOpts.TuneCPU.empty())2059FuncAttrs.addAttribute("tune-cpu", TargetOpts.TuneCPU);20602061::getTrivialDefaultFunctionAttributes(F.getName(), F.hasOptNone(),2062CodeGenOpts, LangOpts,2063/*AttrOnCallSite=*/false, FuncAttrs);20642065if (!WillInternalize && F.isInterposable()) {2066// Do not promote "dynamic" denormal-fp-math to this translation unit's2067// setting for weak functions that won't be internalized. The user has no2068// real control for how builtin bitcode is linked, so we shouldn't assume2069// later copies will use a consistent mode.2070F.addFnAttrs(FuncAttrs);2071return;2072}20732074llvm::AttributeMask AttrsToRemove;20752076llvm::DenormalMode DenormModeToMerge = F.getDenormalModeRaw();2077llvm::DenormalMode DenormModeToMergeF32 = F.getDenormalModeF32Raw();2078llvm::DenormalMode Merged =2079CodeGenOpts.FPDenormalMode.mergeCalleeMode(DenormModeToMerge);2080llvm::DenormalMode MergedF32 = CodeGenOpts.FP32DenormalMode;20812082if (DenormModeToMergeF32.isValid()) {2083MergedF32 =2084CodeGenOpts.FP32DenormalMode.mergeCalleeMode(DenormModeToMergeF32);2085}20862087if (Merged == llvm::DenormalMode::getDefault()) {2088AttrsToRemove.addAttribute("denormal-fp-math");2089} else if (Merged != DenormModeToMerge) {2090// Overwrite existing attribute2091FuncAttrs.addAttribute("denormal-fp-math",2092CodeGenOpts.FPDenormalMode.str());2093}20942095if (MergedF32 == llvm::DenormalMode::getDefault()) {2096AttrsToRemove.addAttribute("denormal-fp-math-f32");2097} else if (MergedF32 != DenormModeToMergeF32) {2098// Overwrite existing attribute2099FuncAttrs.addAttribute("denormal-fp-math-f32",2100CodeGenOpts.FP32DenormalMode.str());2101}21022103F.removeFnAttrs(AttrsToRemove);2104addDenormalModeAttrs(Merged, MergedF32, FuncAttrs);21052106overrideFunctionFeaturesWithTargetFeatures(FuncAttrs, F, TargetOpts);21072108F.addFnAttrs(FuncAttrs);2109}21102111void CodeGenModule::getTrivialDefaultFunctionAttributes(2112StringRef Name, bool HasOptnone, bool AttrOnCallSite,2113llvm::AttrBuilder &FuncAttrs) {2114::getTrivialDefaultFunctionAttributes(Name, HasOptnone, getCodeGenOpts(),2115getLangOpts(), AttrOnCallSite,2116FuncAttrs);2117}21182119void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,2120bool HasOptnone,2121bool AttrOnCallSite,2122llvm::AttrBuilder &FuncAttrs) {2123getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,2124FuncAttrs);2125// If we're just getting the default, get the default values for mergeable2126// attributes.2127if (!AttrOnCallSite)2128addMergableDefaultFunctionAttributes(CodeGenOpts, FuncAttrs);2129}21302131void CodeGenModule::addDefaultFunctionDefinitionAttributes(2132llvm::AttrBuilder &attrs) {2133getDefaultFunctionAttributes(/*function name*/ "", /*optnone*/ false,2134/*for call*/ false, attrs);2135GetCPUAndFeaturesAttributes(GlobalDecl(), attrs);2136}21372138static void addNoBuiltinAttributes(llvm::AttrBuilder &FuncAttrs,2139const LangOptions &LangOpts,2140const NoBuiltinAttr *NBA = nullptr) {2141auto AddNoBuiltinAttr = [&FuncAttrs](StringRef BuiltinName) {2142SmallString<32> AttributeName;2143AttributeName += "no-builtin-";2144AttributeName += BuiltinName;2145FuncAttrs.addAttribute(AttributeName);2146};21472148// First, handle the language options passed through -fno-builtin.2149if (LangOpts.NoBuiltin) {2150// -fno-builtin disables them all.2151FuncAttrs.addAttribute("no-builtins");2152return;2153}21542155// Then, add attributes for builtins specified through -fno-builtin-<name>.2156llvm::for_each(LangOpts.NoBuiltinFuncs, AddNoBuiltinAttr);21572158// Now, let's check the __attribute__((no_builtin("...")) attribute added to2159// the source.2160if (!NBA)2161return;21622163// If there is a wildcard in the builtin names specified through the2164// attribute, disable them all.2165if (llvm::is_contained(NBA->builtinNames(), "*")) {2166FuncAttrs.addAttribute("no-builtins");2167return;2168}21692170// And last, add the rest of the builtin names.2171llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr);2172}21732174static bool DetermineNoUndef(QualType QTy, CodeGenTypes &Types,2175const llvm::DataLayout &DL, const ABIArgInfo &AI,2176bool CheckCoerce = true) {2177llvm::Type *Ty = Types.ConvertTypeForMem(QTy);2178if (AI.getKind() == ABIArgInfo::Indirect ||2179AI.getKind() == ABIArgInfo::IndirectAliased)2180return true;2181if (AI.getKind() == ABIArgInfo::Extend)2182return true;2183if (!DL.typeSizeEqualsStoreSize(Ty))2184// TODO: This will result in a modest amount of values not marked noundef2185// when they could be. We care about values that *invisibly* contain undef2186// bits from the perspective of LLVM IR.2187return false;2188if (CheckCoerce && AI.canHaveCoerceToType()) {2189llvm::Type *CoerceTy = AI.getCoerceToType();2190if (llvm::TypeSize::isKnownGT(DL.getTypeSizeInBits(CoerceTy),2191DL.getTypeSizeInBits(Ty)))2192// If we're coercing to a type with a greater size than the canonical one,2193// we're introducing new undef bits.2194// Coercing to a type of smaller or equal size is ok, as we know that2195// there's no internal padding (typeSizeEqualsStoreSize).2196return false;2197}2198if (QTy->isBitIntType())2199return true;2200if (QTy->isReferenceType())2201return true;2202if (QTy->isNullPtrType())2203return false;2204if (QTy->isMemberPointerType())2205// TODO: Some member pointers are `noundef`, but it depends on the ABI. For2206// now, never mark them.2207return false;2208if (QTy->isScalarType()) {2209if (const ComplexType *Complex = dyn_cast<ComplexType>(QTy))2210return DetermineNoUndef(Complex->getElementType(), Types, DL, AI, false);2211return true;2212}2213if (const VectorType *Vector = dyn_cast<VectorType>(QTy))2214return DetermineNoUndef(Vector->getElementType(), Types, DL, AI, false);2215if (const MatrixType *Matrix = dyn_cast<MatrixType>(QTy))2216return DetermineNoUndef(Matrix->getElementType(), Types, DL, AI, false);2217if (const ArrayType *Array = dyn_cast<ArrayType>(QTy))2218return DetermineNoUndef(Array->getElementType(), Types, DL, AI, false);22192220// TODO: Some structs may be `noundef`, in specific situations.2221return false;2222}22232224/// Check if the argument of a function has maybe_undef attribute.2225static bool IsArgumentMaybeUndef(const Decl *TargetDecl,2226unsigned NumRequiredArgs, unsigned ArgNo) {2227const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);2228if (!FD)2229return false;22302231// Assume variadic arguments do not have maybe_undef attribute.2232if (ArgNo >= NumRequiredArgs)2233return false;22342235// Check if argument has maybe_undef attribute.2236if (ArgNo < FD->getNumParams()) {2237const ParmVarDecl *Param = FD->getParamDecl(ArgNo);2238if (Param && Param->hasAttr<MaybeUndefAttr>())2239return true;2240}22412242return false;2243}22442245/// Test if it's legal to apply nofpclass for the given parameter type and it's2246/// lowered IR type.2247static bool canApplyNoFPClass(const ABIArgInfo &AI, QualType ParamType,2248bool IsReturn) {2249// Should only apply to FP types in the source, not ABI promoted.2250if (!ParamType->hasFloatingRepresentation())2251return false;22522253// The promoted-to IR type also needs to support nofpclass.2254llvm::Type *IRTy = AI.getCoerceToType();2255if (llvm::AttributeFuncs::isNoFPClassCompatibleType(IRTy))2256return true;22572258if (llvm::StructType *ST = dyn_cast<llvm::StructType>(IRTy)) {2259return !IsReturn && AI.getCanBeFlattened() &&2260llvm::all_of(ST->elements(), [](llvm::Type *Ty) {2261return llvm::AttributeFuncs::isNoFPClassCompatibleType(Ty);2262});2263}22642265return false;2266}22672268/// Return the nofpclass mask that can be applied to floating-point parameters.2269static llvm::FPClassTest getNoFPClassTestMask(const LangOptions &LangOpts) {2270llvm::FPClassTest Mask = llvm::fcNone;2271if (LangOpts.NoHonorInfs)2272Mask |= llvm::fcInf;2273if (LangOpts.NoHonorNaNs)2274Mask |= llvm::fcNan;2275return Mask;2276}22772278void CodeGenModule::AdjustMemoryAttribute(StringRef Name,2279CGCalleeInfo CalleeInfo,2280llvm::AttributeList &Attrs) {2281if (Attrs.getMemoryEffects().getModRef() == llvm::ModRefInfo::NoModRef) {2282Attrs = Attrs.removeFnAttribute(getLLVMContext(), llvm::Attribute::Memory);2283llvm::Attribute MemoryAttr = llvm::Attribute::getWithMemoryEffects(2284getLLVMContext(), llvm::MemoryEffects::writeOnly());2285Attrs = Attrs.addFnAttribute(getLLVMContext(), MemoryAttr);2286}2287}22882289/// Construct the IR attribute list of a function or call.2290///2291/// When adding an attribute, please consider where it should be handled:2292///2293/// - getDefaultFunctionAttributes is for attributes that are essentially2294/// part of the global target configuration (but perhaps can be2295/// overridden on a per-function basis). Adding attributes there2296/// will cause them to also be set in frontends that build on Clang's2297/// target-configuration logic, as well as for code defined in library2298/// modules such as CUDA's libdevice.2299///2300/// - ConstructAttributeList builds on top of getDefaultFunctionAttributes2301/// and adds declaration-specific, convention-specific, and2302/// frontend-specific logic. The last is of particular importance:2303/// attributes that restrict how the frontend generates code must be2304/// added here rather than getDefaultFunctionAttributes.2305///2306void CodeGenModule::ConstructAttributeList(StringRef Name,2307const CGFunctionInfo &FI,2308CGCalleeInfo CalleeInfo,2309llvm::AttributeList &AttrList,2310unsigned &CallingConv,2311bool AttrOnCallSite, bool IsThunk) {2312llvm::AttrBuilder FuncAttrs(getLLVMContext());2313llvm::AttrBuilder RetAttrs(getLLVMContext());23142315// Collect function IR attributes from the CC lowering.2316// We'll collect the paramete and result attributes later.2317CallingConv = FI.getEffectiveCallingConvention();2318if (FI.isNoReturn())2319FuncAttrs.addAttribute(llvm::Attribute::NoReturn);2320if (FI.isCmseNSCall())2321FuncAttrs.addAttribute("cmse_nonsecure_call");23222323// Collect function IR attributes from the callee prototype if we have one.2324AddAttributesFromFunctionProtoType(getContext(), FuncAttrs,2325CalleeInfo.getCalleeFunctionProtoType());23262327const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();23282329// Attach assumption attributes to the declaration. If this is a call2330// site, attach assumptions from the caller to the call as well.2331AddAttributesFromOMPAssumes(FuncAttrs, TargetDecl);23322333bool HasOptnone = false;2334// The NoBuiltinAttr attached to the target FunctionDecl.2335const NoBuiltinAttr *NBA = nullptr;23362337// Some ABIs may result in additional accesses to arguments that may2338// otherwise not be present.2339auto AddPotentialArgAccess = [&]() {2340llvm::Attribute A = FuncAttrs.getAttribute(llvm::Attribute::Memory);2341if (A.isValid())2342FuncAttrs.addMemoryAttr(A.getMemoryEffects() |2343llvm::MemoryEffects::argMemOnly());2344};23452346// Collect function IR attributes based on declaration-specific2347// information.2348// FIXME: handle sseregparm someday...2349if (TargetDecl) {2350if (TargetDecl->hasAttr<ReturnsTwiceAttr>())2351FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);2352if (TargetDecl->hasAttr<NoThrowAttr>())2353FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);2354if (TargetDecl->hasAttr<NoReturnAttr>())2355FuncAttrs.addAttribute(llvm::Attribute::NoReturn);2356if (TargetDecl->hasAttr<ColdAttr>())2357FuncAttrs.addAttribute(llvm::Attribute::Cold);2358if (TargetDecl->hasAttr<HotAttr>())2359FuncAttrs.addAttribute(llvm::Attribute::Hot);2360if (TargetDecl->hasAttr<NoDuplicateAttr>())2361FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);2362if (TargetDecl->hasAttr<ConvergentAttr>())2363FuncAttrs.addAttribute(llvm::Attribute::Convergent);23642365if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {2366AddAttributesFromFunctionProtoType(2367getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());2368if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {2369// A sane operator new returns a non-aliasing pointer.2370auto Kind = Fn->getDeclName().getCXXOverloadedOperator();2371if (getCodeGenOpts().AssumeSaneOperatorNew &&2372(Kind == OO_New || Kind == OO_Array_New))2373RetAttrs.addAttribute(llvm::Attribute::NoAlias);2374}2375const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);2376const bool IsVirtualCall = MD && MD->isVirtual();2377// Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a2378// virtual function. These attributes are not inherited by overloads.2379if (!(AttrOnCallSite && IsVirtualCall)) {2380if (Fn->isNoReturn())2381FuncAttrs.addAttribute(llvm::Attribute::NoReturn);2382NBA = Fn->getAttr<NoBuiltinAttr>();2383}2384}23852386if (isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl)) {2387// Only place nomerge attribute on call sites, never functions. This2388// allows it to work on indirect virtual function calls.2389if (AttrOnCallSite && TargetDecl->hasAttr<NoMergeAttr>())2390FuncAttrs.addAttribute(llvm::Attribute::NoMerge);2391}23922393// 'const', 'pure' and 'noalias' attributed functions are also nounwind.2394if (TargetDecl->hasAttr<ConstAttr>()) {2395FuncAttrs.addMemoryAttr(llvm::MemoryEffects::none());2396FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);2397// gcc specifies that 'const' functions have greater restrictions than2398// 'pure' functions, so they also cannot have infinite loops.2399FuncAttrs.addAttribute(llvm::Attribute::WillReturn);2400} else if (TargetDecl->hasAttr<PureAttr>()) {2401FuncAttrs.addMemoryAttr(llvm::MemoryEffects::readOnly());2402FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);2403// gcc specifies that 'pure' functions cannot have infinite loops.2404FuncAttrs.addAttribute(llvm::Attribute::WillReturn);2405} else if (TargetDecl->hasAttr<NoAliasAttr>()) {2406FuncAttrs.addMemoryAttr(llvm::MemoryEffects::inaccessibleOrArgMemOnly());2407FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);2408}2409if (TargetDecl->hasAttr<RestrictAttr>())2410RetAttrs.addAttribute(llvm::Attribute::NoAlias);2411if (TargetDecl->hasAttr<ReturnsNonNullAttr>() &&2412!CodeGenOpts.NullPointerIsValid)2413RetAttrs.addAttribute(llvm::Attribute::NonNull);2414if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())2415FuncAttrs.addAttribute("no_caller_saved_registers");2416if (TargetDecl->hasAttr<AnyX86NoCfCheckAttr>())2417FuncAttrs.addAttribute(llvm::Attribute::NoCfCheck);2418if (TargetDecl->hasAttr<LeafAttr>())2419FuncAttrs.addAttribute(llvm::Attribute::NoCallback);24202421HasOptnone = TargetDecl->hasAttr<OptimizeNoneAttr>();2422if (auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {2423std::optional<unsigned> NumElemsParam;2424if (AllocSize->getNumElemsParam().isValid())2425NumElemsParam = AllocSize->getNumElemsParam().getLLVMIndex();2426FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam().getLLVMIndex(),2427NumElemsParam);2428}24292430if (TargetDecl->hasAttr<OpenCLKernelAttr>()) {2431if (getLangOpts().OpenCLVersion <= 120) {2432// OpenCL v1.2 Work groups are always uniform2433FuncAttrs.addAttribute("uniform-work-group-size", "true");2434} else {2435// OpenCL v2.0 Work groups may be whether uniform or not.2436// '-cl-uniform-work-group-size' compile option gets a hint2437// to the compiler that the global work-size be a multiple of2438// the work-group size specified to clEnqueueNDRangeKernel2439// (i.e. work groups are uniform).2440FuncAttrs.addAttribute(2441"uniform-work-group-size",2442llvm::toStringRef(getLangOpts().OffloadUniformBlock));2443}2444}24452446if (TargetDecl->hasAttr<CUDAGlobalAttr>() &&2447getLangOpts().OffloadUniformBlock)2448FuncAttrs.addAttribute("uniform-work-group-size", "true");24492450if (TargetDecl->hasAttr<ArmLocallyStreamingAttr>())2451FuncAttrs.addAttribute("aarch64_pstate_sm_body");2452}24532454// Attach "no-builtins" attributes to:2455// * call sites: both `nobuiltin` and "no-builtins" or "no-builtin-<name>".2456// * definitions: "no-builtins" or "no-builtin-<name>" only.2457// The attributes can come from:2458// * LangOpts: -ffreestanding, -fno-builtin, -fno-builtin-<name>2459// * FunctionDecl attributes: __attribute__((no_builtin(...)))2460addNoBuiltinAttributes(FuncAttrs, getLangOpts(), NBA);24612462// Collect function IR attributes based on global settiings.2463getDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite, FuncAttrs);24642465// Override some default IR attributes based on declaration-specific2466// information.2467if (TargetDecl) {2468if (TargetDecl->hasAttr<NoSpeculativeLoadHardeningAttr>())2469FuncAttrs.removeAttribute(llvm::Attribute::SpeculativeLoadHardening);2470if (TargetDecl->hasAttr<SpeculativeLoadHardeningAttr>())2471FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);2472if (TargetDecl->hasAttr<NoSplitStackAttr>())2473FuncAttrs.removeAttribute("split-stack");2474if (TargetDecl->hasAttr<ZeroCallUsedRegsAttr>()) {2475// A function "__attribute__((...))" overrides the command-line flag.2476auto Kind =2477TargetDecl->getAttr<ZeroCallUsedRegsAttr>()->getZeroCallUsedRegs();2478FuncAttrs.removeAttribute("zero-call-used-regs");2479FuncAttrs.addAttribute(2480"zero-call-used-regs",2481ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Kind));2482}24832484// Add NonLazyBind attribute to function declarations when -fno-plt2485// is used.2486// FIXME: what if we just haven't processed the function definition2487// yet, or if it's an external definition like C99 inline?2488if (CodeGenOpts.NoPLT) {2489if (auto *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {2490if (!Fn->isDefined() && !AttrOnCallSite) {2491FuncAttrs.addAttribute(llvm::Attribute::NonLazyBind);2492}2493}2494}2495}24962497// Add "sample-profile-suffix-elision-policy" attribute for internal linkage2498// functions with -funique-internal-linkage-names.2499if (TargetDecl && CodeGenOpts.UniqueInternalLinkageNames) {2500if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {2501if (!FD->isExternallyVisible())2502FuncAttrs.addAttribute("sample-profile-suffix-elision-policy",2503"selected");2504}2505}25062507// Collect non-call-site function IR attributes from declaration-specific2508// information.2509if (!AttrOnCallSite) {2510if (TargetDecl && TargetDecl->hasAttr<CmseNSEntryAttr>())2511FuncAttrs.addAttribute("cmse_nonsecure_entry");25122513// Whether tail calls are enabled.2514auto shouldDisableTailCalls = [&] {2515// Should this be honored in getDefaultFunctionAttributes?2516if (CodeGenOpts.DisableTailCalls)2517return true;25182519if (!TargetDecl)2520return false;25212522if (TargetDecl->hasAttr<DisableTailCallsAttr>() ||2523TargetDecl->hasAttr<AnyX86InterruptAttr>())2524return true;25252526if (CodeGenOpts.NoEscapingBlockTailCalls) {2527if (const auto *BD = dyn_cast<BlockDecl>(TargetDecl))2528if (!BD->doesNotEscape())2529return true;2530}25312532return false;2533};2534if (shouldDisableTailCalls())2535FuncAttrs.addAttribute("disable-tail-calls", "true");25362537// CPU/feature overrides. addDefaultFunctionDefinitionAttributes2538// handles these separately to set them based on the global defaults.2539GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);2540}25412542// Collect attributes from arguments and return values.2543ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);25442545QualType RetTy = FI.getReturnType();2546const ABIArgInfo &RetAI = FI.getReturnInfo();2547const llvm::DataLayout &DL = getDataLayout();25482549// Determine if the return type could be partially undef2550if (CodeGenOpts.EnableNoundefAttrs &&2551HasStrictReturn(*this, RetTy, TargetDecl)) {2552if (!RetTy->isVoidType() && RetAI.getKind() != ABIArgInfo::Indirect &&2553DetermineNoUndef(RetTy, getTypes(), DL, RetAI))2554RetAttrs.addAttribute(llvm::Attribute::NoUndef);2555}25562557switch (RetAI.getKind()) {2558case ABIArgInfo::Extend:2559if (RetAI.isSignExt())2560RetAttrs.addAttribute(llvm::Attribute::SExt);2561else2562RetAttrs.addAttribute(llvm::Attribute::ZExt);2563[[fallthrough]];2564case ABIArgInfo::Direct:2565if (RetAI.getInReg())2566RetAttrs.addAttribute(llvm::Attribute::InReg);25672568if (canApplyNoFPClass(RetAI, RetTy, true))2569RetAttrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));25702571break;2572case ABIArgInfo::Ignore:2573break;25742575case ABIArgInfo::InAlloca:2576case ABIArgInfo::Indirect: {2577// inalloca and sret disable readnone and readonly2578AddPotentialArgAccess();2579break;2580}25812582case ABIArgInfo::CoerceAndExpand:2583break;25842585case ABIArgInfo::Expand:2586case ABIArgInfo::IndirectAliased:2587llvm_unreachable("Invalid ABI kind for return argument");2588}25892590if (!IsThunk) {2591// FIXME: fix this properly, https://reviews.llvm.org/D1003882592if (const auto *RefTy = RetTy->getAs<ReferenceType>()) {2593QualType PTy = RefTy->getPointeeType();2594if (!PTy->isIncompleteType() && PTy->isConstantSizeType())2595RetAttrs.addDereferenceableAttr(2596getMinimumObjectSize(PTy).getQuantity());2597if (getTypes().getTargetAddressSpace(PTy) == 0 &&2598!CodeGenOpts.NullPointerIsValid)2599RetAttrs.addAttribute(llvm::Attribute::NonNull);2600if (PTy->isObjectType()) {2601llvm::Align Alignment =2602getNaturalPointeeTypeAlignment(RetTy).getAsAlign();2603RetAttrs.addAlignmentAttr(Alignment);2604}2605}2606}26072608bool hasUsedSRet = false;2609SmallVector<llvm::AttributeSet, 4> ArgAttrs(IRFunctionArgs.totalIRArgs());26102611// Attach attributes to sret.2612if (IRFunctionArgs.hasSRetArg()) {2613llvm::AttrBuilder SRETAttrs(getLLVMContext());2614SRETAttrs.addStructRetAttr(getTypes().ConvertTypeForMem(RetTy));2615SRETAttrs.addAttribute(llvm::Attribute::Writable);2616SRETAttrs.addAttribute(llvm::Attribute::DeadOnUnwind);2617hasUsedSRet = true;2618if (RetAI.getInReg())2619SRETAttrs.addAttribute(llvm::Attribute::InReg);2620SRETAttrs.addAlignmentAttr(RetAI.getIndirectAlign().getQuantity());2621ArgAttrs[IRFunctionArgs.getSRetArgNo()] =2622llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);2623}26242625// Attach attributes to inalloca argument.2626if (IRFunctionArgs.hasInallocaArg()) {2627llvm::AttrBuilder Attrs(getLLVMContext());2628Attrs.addInAllocaAttr(FI.getArgStruct());2629ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =2630llvm::AttributeSet::get(getLLVMContext(), Attrs);2631}26322633// Apply `nonnull`, `dereferencable(N)` and `align N` to the `this` argument,2634// unless this is a thunk function.2635// FIXME: fix this properly, https://reviews.llvm.org/D1003882636if (FI.isInstanceMethod() && !IRFunctionArgs.hasInallocaArg() &&2637!FI.arg_begin()->type->isVoidPointerType() && !IsThunk) {2638auto IRArgs = IRFunctionArgs.getIRArgs(0);26392640assert(IRArgs.second == 1 && "Expected only a single `this` pointer.");26412642llvm::AttrBuilder Attrs(getLLVMContext());26432644QualType ThisTy =2645FI.arg_begin()->type.getTypePtr()->getPointeeType();26462647if (!CodeGenOpts.NullPointerIsValid &&2648getTypes().getTargetAddressSpace(FI.arg_begin()->type) == 0) {2649Attrs.addAttribute(llvm::Attribute::NonNull);2650Attrs.addDereferenceableAttr(getMinimumObjectSize(ThisTy).getQuantity());2651} else {2652// FIXME dereferenceable should be correct here, regardless of2653// NullPointerIsValid. However, dereferenceable currently does not always2654// respect NullPointerIsValid and may imply nonnull and break the program.2655// See https://reviews.llvm.org/D66618 for discussions.2656Attrs.addDereferenceableOrNullAttr(2657getMinimumObjectSize(2658FI.arg_begin()->type.castAs<PointerType>()->getPointeeType())2659.getQuantity());2660}26612662llvm::Align Alignment =2663getNaturalTypeAlignment(ThisTy, /*BaseInfo=*/nullptr,2664/*TBAAInfo=*/nullptr, /*forPointeeType=*/true)2665.getAsAlign();2666Attrs.addAlignmentAttr(Alignment);26672668ArgAttrs[IRArgs.first] = llvm::AttributeSet::get(getLLVMContext(), Attrs);2669}26702671unsigned ArgNo = 0;2672for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(),2673E = FI.arg_end();2674I != E; ++I, ++ArgNo) {2675QualType ParamType = I->type;2676const ABIArgInfo &AI = I->info;2677llvm::AttrBuilder Attrs(getLLVMContext());26782679// Add attribute for padding argument, if necessary.2680if (IRFunctionArgs.hasPaddingArg(ArgNo)) {2681if (AI.getPaddingInReg()) {2682ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =2683llvm::AttributeSet::get(2684getLLVMContext(),2685llvm::AttrBuilder(getLLVMContext()).addAttribute(llvm::Attribute::InReg));2686}2687}26882689// Decide whether the argument we're handling could be partially undef2690if (CodeGenOpts.EnableNoundefAttrs &&2691DetermineNoUndef(ParamType, getTypes(), DL, AI)) {2692Attrs.addAttribute(llvm::Attribute::NoUndef);2693}26942695// 'restrict' -> 'noalias' is done in EmitFunctionProlog when we2696// have the corresponding parameter variable. It doesn't make2697// sense to do it here because parameters are so messed up.2698switch (AI.getKind()) {2699case ABIArgInfo::Extend:2700if (AI.isSignExt())2701Attrs.addAttribute(llvm::Attribute::SExt);2702else2703Attrs.addAttribute(llvm::Attribute::ZExt);2704[[fallthrough]];2705case ABIArgInfo::Direct:2706if (ArgNo == 0 && FI.isChainCall())2707Attrs.addAttribute(llvm::Attribute::Nest);2708else if (AI.getInReg())2709Attrs.addAttribute(llvm::Attribute::InReg);2710Attrs.addStackAlignmentAttr(llvm::MaybeAlign(AI.getDirectAlign()));27112712if (canApplyNoFPClass(AI, ParamType, false))2713Attrs.addNoFPClassAttr(getNoFPClassTestMask(getLangOpts()));2714break;2715case ABIArgInfo::Indirect: {2716if (AI.getInReg())2717Attrs.addAttribute(llvm::Attribute::InReg);27182719if (AI.getIndirectByVal())2720Attrs.addByValAttr(getTypes().ConvertTypeForMem(ParamType));27212722auto *Decl = ParamType->getAsRecordDecl();2723if (CodeGenOpts.PassByValueIsNoAlias && Decl &&2724Decl->getArgPassingRestrictions() ==2725RecordArgPassingKind::CanPassInRegs)2726// When calling the function, the pointer passed in will be the only2727// reference to the underlying object. Mark it accordingly.2728Attrs.addAttribute(llvm::Attribute::NoAlias);27292730// TODO: We could add the byref attribute if not byval, but it would2731// require updating many testcases.27322733CharUnits Align = AI.getIndirectAlign();27342735// In a byval argument, it is important that the required2736// alignment of the type is honored, as LLVM might be creating a2737// *new* stack object, and needs to know what alignment to give2738// it. (Sometimes it can deduce a sensible alignment on its own,2739// but not if clang decides it must emit a packed struct, or the2740// user specifies increased alignment requirements.)2741//2742// This is different from indirect *not* byval, where the object2743// exists already, and the align attribute is purely2744// informative.2745assert(!Align.isZero());27462747// For now, only add this when we have a byval argument.2748// TODO: be less lazy about updating test cases.2749if (AI.getIndirectByVal())2750Attrs.addAlignmentAttr(Align.getQuantity());27512752// byval disables readnone and readonly.2753AddPotentialArgAccess();2754break;2755}2756case ABIArgInfo::IndirectAliased: {2757CharUnits Align = AI.getIndirectAlign();2758Attrs.addByRefAttr(getTypes().ConvertTypeForMem(ParamType));2759Attrs.addAlignmentAttr(Align.getQuantity());2760break;2761}2762case ABIArgInfo::Ignore:2763case ABIArgInfo::Expand:2764case ABIArgInfo::CoerceAndExpand:2765break;27662767case ABIArgInfo::InAlloca:2768// inalloca disables readnone and readonly.2769AddPotentialArgAccess();2770continue;2771}27722773if (const auto *RefTy = ParamType->getAs<ReferenceType>()) {2774QualType PTy = RefTy->getPointeeType();2775if (!PTy->isIncompleteType() && PTy->isConstantSizeType())2776Attrs.addDereferenceableAttr(2777getMinimumObjectSize(PTy).getQuantity());2778if (getTypes().getTargetAddressSpace(PTy) == 0 &&2779!CodeGenOpts.NullPointerIsValid)2780Attrs.addAttribute(llvm::Attribute::NonNull);2781if (PTy->isObjectType()) {2782llvm::Align Alignment =2783getNaturalPointeeTypeAlignment(ParamType).getAsAlign();2784Attrs.addAlignmentAttr(Alignment);2785}2786}27872788// From OpenCL spec v3.0.10 section 6.3.5 Alignment of Types:2789// > For arguments to a __kernel function declared to be a pointer to a2790// > data type, the OpenCL compiler can assume that the pointee is always2791// > appropriately aligned as required by the data type.2792if (TargetDecl && TargetDecl->hasAttr<OpenCLKernelAttr>() &&2793ParamType->isPointerType()) {2794QualType PTy = ParamType->getPointeeType();2795if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {2796llvm::Align Alignment =2797getNaturalPointeeTypeAlignment(ParamType).getAsAlign();2798Attrs.addAlignmentAttr(Alignment);2799}2800}28012802switch (FI.getExtParameterInfo(ArgNo).getABI()) {2803case ParameterABI::Ordinary:2804break;28052806case ParameterABI::SwiftIndirectResult: {2807// Add 'sret' if we haven't already used it for something, but2808// only if the result is void.2809if (!hasUsedSRet && RetTy->isVoidType()) {2810Attrs.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType));2811hasUsedSRet = true;2812}28132814// Add 'noalias' in either case.2815Attrs.addAttribute(llvm::Attribute::NoAlias);28162817// Add 'dereferenceable' and 'alignment'.2818auto PTy = ParamType->getPointeeType();2819if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {2820auto info = getContext().getTypeInfoInChars(PTy);2821Attrs.addDereferenceableAttr(info.Width.getQuantity());2822Attrs.addAlignmentAttr(info.Align.getAsAlign());2823}2824break;2825}28262827case ParameterABI::SwiftErrorResult:2828Attrs.addAttribute(llvm::Attribute::SwiftError);2829break;28302831case ParameterABI::SwiftContext:2832Attrs.addAttribute(llvm::Attribute::SwiftSelf);2833break;28342835case ParameterABI::SwiftAsyncContext:2836Attrs.addAttribute(llvm::Attribute::SwiftAsync);2837break;2838}28392840if (FI.getExtParameterInfo(ArgNo).isNoEscape())2841Attrs.addAttribute(llvm::Attribute::NoCapture);28422843if (Attrs.hasAttributes()) {2844unsigned FirstIRArg, NumIRArgs;2845std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);2846for (unsigned i = 0; i < NumIRArgs; i++)2847ArgAttrs[FirstIRArg + i] = ArgAttrs[FirstIRArg + i].addAttributes(2848getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), Attrs));2849}2850}2851assert(ArgNo == FI.arg_size());28522853AttrList = llvm::AttributeList::get(2854getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),2855llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);2856}28572858/// An argument came in as a promoted argument; demote it back to its2859/// declared type.2860static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,2861const VarDecl *var,2862llvm::Value *value) {2863llvm::Type *varType = CGF.ConvertType(var->getType());28642865// This can happen with promotions that actually don't change the2866// underlying type, like the enum promotions.2867if (value->getType() == varType) return value;28682869assert((varType->isIntegerTy() || varType->isFloatingPointTy())2870&& "unexpected promotion type");28712872if (isa<llvm::IntegerType>(varType))2873return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");28742875return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");2876}28772878/// Returns the attribute (either parameter attribute, or function2879/// attribute), which declares argument ArgNo to be non-null.2880static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD,2881QualType ArgType, unsigned ArgNo) {2882// FIXME: __attribute__((nonnull)) can also be applied to:2883// - references to pointers, where the pointee is known to be2884// nonnull (apparently a Clang extension)2885// - transparent unions containing pointers2886// In the former case, LLVM IR cannot represent the constraint. In2887// the latter case, we have no guarantee that the transparent union2888// is in fact passed as a pointer.2889if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType())2890return nullptr;2891// First, check attribute on parameter itself.2892if (PVD) {2893if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>())2894return ParmNNAttr;2895}2896// Check function attributes.2897if (!FD)2898return nullptr;2899for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) {2900if (NNAttr->isNonNull(ArgNo))2901return NNAttr;2902}2903return nullptr;2904}29052906namespace {2907struct CopyBackSwiftError final : EHScopeStack::Cleanup {2908Address Temp;2909Address Arg;2910CopyBackSwiftError(Address temp, Address arg) : Temp(temp), Arg(arg) {}2911void Emit(CodeGenFunction &CGF, Flags flags) override {2912llvm::Value *errorValue = CGF.Builder.CreateLoad(Temp);2913CGF.Builder.CreateStore(errorValue, Arg);2914}2915};2916}29172918void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,2919llvm::Function *Fn,2920const FunctionArgList &Args) {2921if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())2922// Naked functions don't have prologues.2923return;29242925// If this is an implicit-return-zero function, go ahead and2926// initialize the return value. TODO: it might be nice to have2927// a more general mechanism for this that didn't require synthesized2928// return statements.2929if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {2930if (FD->hasImplicitReturnZero()) {2931QualType RetTy = FD->getReturnType().getUnqualifiedType();2932llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);2933llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);2934Builder.CreateStore(Zero, ReturnValue);2935}2936}29372938// FIXME: We no longer need the types from FunctionArgList; lift up and2939// simplify.29402941ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);2942assert(Fn->arg_size() == IRFunctionArgs.totalIRArgs());29432944// If we're using inalloca, all the memory arguments are GEPs off of the last2945// parameter, which is a pointer to the complete memory area.2946Address ArgStruct = Address::invalid();2947if (IRFunctionArgs.hasInallocaArg())2948ArgStruct = Address(Fn->getArg(IRFunctionArgs.getInallocaArgNo()),2949FI.getArgStruct(), FI.getArgStructAlignment());29502951// Name the struct return parameter.2952if (IRFunctionArgs.hasSRetArg()) {2953auto AI = Fn->getArg(IRFunctionArgs.getSRetArgNo());2954AI->setName("agg.result");2955AI->addAttr(llvm::Attribute::NoAlias);2956}29572958// Track if we received the parameter as a pointer (indirect, byval, or2959// inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it2960// into a local alloca for us.2961SmallVector<ParamValue, 16> ArgVals;2962ArgVals.reserve(Args.size());29632964// Create a pointer value for every parameter declaration. This usually2965// entails copying one or more LLVM IR arguments into an alloca. Don't push2966// any cleanups or do anything that might unwind. We do that separately, so2967// we can push the cleanups in the correct order for the ABI.2968assert(FI.arg_size() == Args.size() &&2969"Mismatch between function signature & arguments.");2970unsigned ArgNo = 0;2971CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();2972for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();2973i != e; ++i, ++info_it, ++ArgNo) {2974const VarDecl *Arg = *i;2975const ABIArgInfo &ArgI = info_it->info;29762977bool isPromoted =2978isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();2979// We are converting from ABIArgInfo type to VarDecl type directly, unless2980// the parameter is promoted. In this case we convert to2981// CGFunctionInfo::ArgInfo type with subsequent argument demotion.2982QualType Ty = isPromoted ? info_it->type : Arg->getType();2983assert(hasScalarEvaluationKind(Ty) ==2984hasScalarEvaluationKind(Arg->getType()));29852986unsigned FirstIRArg, NumIRArgs;2987std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);29882989switch (ArgI.getKind()) {2990case ABIArgInfo::InAlloca: {2991assert(NumIRArgs == 0);2992auto FieldIndex = ArgI.getInAllocaFieldIndex();2993Address V =2994Builder.CreateStructGEP(ArgStruct, FieldIndex, Arg->getName());2995if (ArgI.getInAllocaIndirect())2996V = Address(Builder.CreateLoad(V), ConvertTypeForMem(Ty),2997getContext().getTypeAlignInChars(Ty));2998ArgVals.push_back(ParamValue::forIndirect(V));2999break;3000}30013002case ABIArgInfo::Indirect:3003case ABIArgInfo::IndirectAliased: {3004assert(NumIRArgs == 1);3005Address ParamAddr = makeNaturalAddressForPointer(3006Fn->getArg(FirstIRArg), Ty, ArgI.getIndirectAlign(), false, nullptr,3007nullptr, KnownNonNull);30083009if (!hasScalarEvaluationKind(Ty)) {3010// Aggregates and complex variables are accessed by reference. All we3011// need to do is realign the value, if requested. Also, if the address3012// may be aliased, copy it to ensure that the parameter variable is3013// mutable and has a unique adress, as C requires.3014if (ArgI.getIndirectRealign() || ArgI.isIndirectAliased()) {3015RawAddress AlignedTemp = CreateMemTemp(Ty, "coerce");30163017// Copy from the incoming argument pointer to the temporary with the3018// appropriate alignment.3019//3020// FIXME: We should have a common utility for generating an aggregate3021// copy.3022CharUnits Size = getContext().getTypeSizeInChars(Ty);3023Builder.CreateMemCpy(3024AlignedTemp.getPointer(), AlignedTemp.getAlignment().getAsAlign(),3025ParamAddr.emitRawPointer(*this),3026ParamAddr.getAlignment().getAsAlign(),3027llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()));3028ParamAddr = AlignedTemp;3029}3030ArgVals.push_back(ParamValue::forIndirect(ParamAddr));3031} else {3032// Load scalar value from indirect argument.3033llvm::Value *V =3034EmitLoadOfScalar(ParamAddr, false, Ty, Arg->getBeginLoc());30353036if (isPromoted)3037V = emitArgumentDemotion(*this, Arg, V);3038ArgVals.push_back(ParamValue::forDirect(V));3039}3040break;3041}30423043case ABIArgInfo::Extend:3044case ABIArgInfo::Direct: {3045auto AI = Fn->getArg(FirstIRArg);3046llvm::Type *LTy = ConvertType(Arg->getType());30473048// Prepare parameter attributes. So far, only attributes for pointer3049// parameters are prepared. See3050// http://llvm.org/docs/LangRef.html#paramattrs.3051if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&3052ArgI.getCoerceToType()->isPointerTy()) {3053assert(NumIRArgs == 1);30543055if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {3056// Set `nonnull` attribute if any.3057if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),3058PVD->getFunctionScopeIndex()) &&3059!CGM.getCodeGenOpts().NullPointerIsValid)3060AI->addAttr(llvm::Attribute::NonNull);30613062QualType OTy = PVD->getOriginalType();3063if (const auto *ArrTy =3064getContext().getAsConstantArrayType(OTy)) {3065// A C99 array parameter declaration with the static keyword also3066// indicates dereferenceability, and if the size is constant we can3067// use the dereferenceable attribute (which requires the size in3068// bytes).3069if (ArrTy->getSizeModifier() == ArraySizeModifier::Static) {3070QualType ETy = ArrTy->getElementType();3071llvm::Align Alignment =3072CGM.getNaturalTypeAlignment(ETy).getAsAlign();3073AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));3074uint64_t ArrSize = ArrTy->getZExtSize();3075if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&3076ArrSize) {3077llvm::AttrBuilder Attrs(getLLVMContext());3078Attrs.addDereferenceableAttr(3079getContext().getTypeSizeInChars(ETy).getQuantity() *3080ArrSize);3081AI->addAttrs(Attrs);3082} else if (getContext().getTargetInfo().getNullPointerValue(3083ETy.getAddressSpace()) == 0 &&3084!CGM.getCodeGenOpts().NullPointerIsValid) {3085AI->addAttr(llvm::Attribute::NonNull);3086}3087}3088} else if (const auto *ArrTy =3089getContext().getAsVariableArrayType(OTy)) {3090// For C99 VLAs with the static keyword, we don't know the size so3091// we can't use the dereferenceable attribute, but in addrspace(0)3092// we know that it must be nonnull.3093if (ArrTy->getSizeModifier() == ArraySizeModifier::Static) {3094QualType ETy = ArrTy->getElementType();3095llvm::Align Alignment =3096CGM.getNaturalTypeAlignment(ETy).getAsAlign();3097AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(Alignment));3098if (!getTypes().getTargetAddressSpace(ETy) &&3099!CGM.getCodeGenOpts().NullPointerIsValid)3100AI->addAttr(llvm::Attribute::NonNull);3101}3102}31033104// Set `align` attribute if any.3105const auto *AVAttr = PVD->getAttr<AlignValueAttr>();3106if (!AVAttr)3107if (const auto *TOTy = OTy->getAs<TypedefType>())3108AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();3109if (AVAttr && !SanOpts.has(SanitizerKind::Alignment)) {3110// If alignment-assumption sanitizer is enabled, we do *not* add3111// alignment attribute here, but emit normal alignment assumption,3112// so the UBSAN check could function.3113llvm::ConstantInt *AlignmentCI =3114cast<llvm::ConstantInt>(EmitScalarExpr(AVAttr->getAlignment()));3115uint64_t AlignmentInt =3116AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment);3117if (AI->getParamAlign().valueOrOne() < AlignmentInt) {3118AI->removeAttr(llvm::Attribute::AttrKind::Alignment);3119AI->addAttrs(llvm::AttrBuilder(getLLVMContext()).addAlignmentAttr(3120llvm::Align(AlignmentInt)));3121}3122}3123}31243125// Set 'noalias' if an argument type has the `restrict` qualifier.3126if (Arg->getType().isRestrictQualified())3127AI->addAttr(llvm::Attribute::NoAlias);3128}31293130// Prepare the argument value. If we have the trivial case, handle it3131// with no muss and fuss.3132if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&3133ArgI.getCoerceToType() == ConvertType(Ty) &&3134ArgI.getDirectOffset() == 0) {3135assert(NumIRArgs == 1);31363137// LLVM expects swifterror parameters to be used in very restricted3138// ways. Copy the value into a less-restricted temporary.3139llvm::Value *V = AI;3140if (FI.getExtParameterInfo(ArgNo).getABI()3141== ParameterABI::SwiftErrorResult) {3142QualType pointeeTy = Ty->getPointeeType();3143assert(pointeeTy->isPointerType());3144RawAddress temp =3145CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");3146Address arg = makeNaturalAddressForPointer(3147V, pointeeTy, getContext().getTypeAlignInChars(pointeeTy));3148llvm::Value *incomingErrorValue = Builder.CreateLoad(arg);3149Builder.CreateStore(incomingErrorValue, temp);3150V = temp.getPointer();31513152// Push a cleanup to copy the value back at the end of the function.3153// The convention does not guarantee that the value will be written3154// back if the function exits with an unwind exception.3155EHStack.pushCleanup<CopyBackSwiftError>(NormalCleanup, temp, arg);3156}31573158// Ensure the argument is the correct type.3159if (V->getType() != ArgI.getCoerceToType())3160V = Builder.CreateBitCast(V, ArgI.getCoerceToType());31613162if (isPromoted)3163V = emitArgumentDemotion(*this, Arg, V);31643165// Because of merging of function types from multiple decls it is3166// possible for the type of an argument to not match the corresponding3167// type in the function type. Since we are codegening the callee3168// in here, add a cast to the argument type.3169llvm::Type *LTy = ConvertType(Arg->getType());3170if (V->getType() != LTy)3171V = Builder.CreateBitCast(V, LTy);31723173ArgVals.push_back(ParamValue::forDirect(V));3174break;3175}31763177// VLST arguments are coerced to VLATs at the function boundary for3178// ABI consistency. If this is a VLST that was coerced to3179// a VLAT at the function boundary and the types match up, use3180// llvm.vector.extract to convert back to the original VLST.3181if (auto *VecTyTo = dyn_cast<llvm::FixedVectorType>(ConvertType(Ty))) {3182llvm::Value *Coerced = Fn->getArg(FirstIRArg);3183if (auto *VecTyFrom =3184dyn_cast<llvm::ScalableVectorType>(Coerced->getType())) {3185// If we are casting a scalable i1 predicate vector to a fixed i83186// vector, bitcast the source and use a vector extract.3187if (VecTyFrom->getElementType()->isIntegerTy(1) &&3188VecTyFrom->getElementCount().isKnownMultipleOf(8) &&3189VecTyTo->getElementType() == Builder.getInt8Ty()) {3190VecTyFrom = llvm::ScalableVectorType::get(3191VecTyTo->getElementType(),3192VecTyFrom->getElementCount().getKnownMinValue() / 8);3193Coerced = Builder.CreateBitCast(Coerced, VecTyFrom);3194}3195if (VecTyFrom->getElementType() == VecTyTo->getElementType()) {3196llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);31973198assert(NumIRArgs == 1);3199Coerced->setName(Arg->getName() + ".coerce");3200ArgVals.push_back(ParamValue::forDirect(Builder.CreateExtractVector(3201VecTyTo, Coerced, Zero, "cast.fixed")));3202break;3203}3204}3205}32063207llvm::StructType *STy =3208dyn_cast<llvm::StructType>(ArgI.getCoerceToType());3209if (ArgI.isDirect() && !ArgI.getCanBeFlattened() && STy &&3210STy->getNumElements() > 1) {3211[[maybe_unused]] llvm::TypeSize StructSize =3212CGM.getDataLayout().getTypeAllocSize(STy);3213[[maybe_unused]] llvm::TypeSize PtrElementSize =3214CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(Ty));3215if (STy->containsHomogeneousScalableVectorTypes()) {3216assert(StructSize == PtrElementSize &&3217"Only allow non-fractional movement of structure with"3218"homogeneous scalable vector type");32193220ArgVals.push_back(ParamValue::forDirect(AI));3221break;3222}3223}32243225Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg),3226Arg->getName());32273228// Pointer to store into.3229Address Ptr = emitAddressAtOffset(*this, Alloca, ArgI);32303231// Fast-isel and the optimizer generally like scalar values better than3232// FCAs, so we flatten them if this is safe to do for this argument.3233if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&3234STy->getNumElements() > 1) {3235llvm::TypeSize StructSize = CGM.getDataLayout().getTypeAllocSize(STy);3236llvm::TypeSize PtrElementSize =3237CGM.getDataLayout().getTypeAllocSize(Ptr.getElementType());3238if (StructSize.isScalable()) {3239assert(STy->containsHomogeneousScalableVectorTypes() &&3240"ABI only supports structure with homogeneous scalable vector "3241"type");3242assert(StructSize == PtrElementSize &&3243"Only allow non-fractional movement of structure with"3244"homogeneous scalable vector type");3245assert(STy->getNumElements() == NumIRArgs);32463247llvm::Value *LoadedStructValue = llvm::PoisonValue::get(STy);3248for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {3249auto *AI = Fn->getArg(FirstIRArg + i);3250AI->setName(Arg->getName() + ".coerce" + Twine(i));3251LoadedStructValue =3252Builder.CreateInsertValue(LoadedStructValue, AI, i);3253}32543255Builder.CreateStore(LoadedStructValue, Ptr);3256} else {3257uint64_t SrcSize = StructSize.getFixedValue();3258uint64_t DstSize = PtrElementSize.getFixedValue();32593260Address AddrToStoreInto = Address::invalid();3261if (SrcSize <= DstSize) {3262AddrToStoreInto = Ptr.withElementType(STy);3263} else {3264AddrToStoreInto =3265CreateTempAlloca(STy, Alloca.getAlignment(), "coerce");3266}32673268assert(STy->getNumElements() == NumIRArgs);3269for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {3270auto AI = Fn->getArg(FirstIRArg + i);3271AI->setName(Arg->getName() + ".coerce" + Twine(i));3272Address EltPtr = Builder.CreateStructGEP(AddrToStoreInto, i);3273Builder.CreateStore(AI, EltPtr);3274}32753276if (SrcSize > DstSize) {3277Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);3278}3279}3280} else {3281// Simple case, just do a coerced store of the argument into the alloca.3282assert(NumIRArgs == 1);3283auto AI = Fn->getArg(FirstIRArg);3284AI->setName(Arg->getName() + ".coerce");3285CreateCoercedStore(3286AI, Ptr,3287llvm::TypeSize::getFixed(3288getContext().getTypeSizeInChars(Ty).getQuantity() -3289ArgI.getDirectOffset()),3290/*DstIsVolatile=*/false);3291}32923293// Match to what EmitParmDecl is expecting for this type.3294if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {3295llvm::Value *V =3296EmitLoadOfScalar(Alloca, false, Ty, Arg->getBeginLoc());3297if (isPromoted)3298V = emitArgumentDemotion(*this, Arg, V);3299ArgVals.push_back(ParamValue::forDirect(V));3300} else {3301ArgVals.push_back(ParamValue::forIndirect(Alloca));3302}3303break;3304}33053306case ABIArgInfo::CoerceAndExpand: {3307// Reconstruct into a temporary.3308Address alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));3309ArgVals.push_back(ParamValue::forIndirect(alloca));33103311auto coercionType = ArgI.getCoerceAndExpandType();3312alloca = alloca.withElementType(coercionType);33133314unsigned argIndex = FirstIRArg;3315for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {3316llvm::Type *eltType = coercionType->getElementType(i);3317if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))3318continue;33193320auto eltAddr = Builder.CreateStructGEP(alloca, i);3321auto elt = Fn->getArg(argIndex++);3322Builder.CreateStore(elt, eltAddr);3323}3324assert(argIndex == FirstIRArg + NumIRArgs);3325break;3326}33273328case ABIArgInfo::Expand: {3329// If this structure was expanded into multiple arguments then3330// we need to create a temporary and reconstruct it from the3331// arguments.3332Address Alloca = CreateMemTemp(Ty, getContext().getDeclAlign(Arg));3333LValue LV = MakeAddrLValue(Alloca, Ty);3334ArgVals.push_back(ParamValue::forIndirect(Alloca));33353336auto FnArgIter = Fn->arg_begin() + FirstIRArg;3337ExpandTypeFromArgs(Ty, LV, FnArgIter);3338assert(FnArgIter == Fn->arg_begin() + FirstIRArg + NumIRArgs);3339for (unsigned i = 0, e = NumIRArgs; i != e; ++i) {3340auto AI = Fn->getArg(FirstIRArg + i);3341AI->setName(Arg->getName() + "." + Twine(i));3342}3343break;3344}33453346case ABIArgInfo::Ignore:3347assert(NumIRArgs == 0);3348// Initialize the local variable appropriately.3349if (!hasScalarEvaluationKind(Ty)) {3350ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));3351} else {3352llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType()));3353ArgVals.push_back(ParamValue::forDirect(U));3354}3355break;3356}3357}33583359if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {3360for (int I = Args.size() - 1; I >= 0; --I)3361EmitParmDecl(*Args[I], ArgVals[I], I + 1);3362} else {3363for (unsigned I = 0, E = Args.size(); I != E; ++I)3364EmitParmDecl(*Args[I], ArgVals[I], I + 1);3365}3366}33673368static void eraseUnusedBitCasts(llvm::Instruction *insn) {3369while (insn->use_empty()) {3370llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);3371if (!bitcast) return;33723373// This is "safe" because we would have used a ConstantExpr otherwise.3374insn = cast<llvm::Instruction>(bitcast->getOperand(0));3375bitcast->eraseFromParent();3376}3377}33783379/// Try to emit a fused autorelease of a return result.3380static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,3381llvm::Value *result) {3382// We must be immediately followed the cast.3383llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();3384if (BB->empty()) return nullptr;3385if (&BB->back() != result) return nullptr;33863387llvm::Type *resultType = result->getType();33883389// result is in a BasicBlock and is therefore an Instruction.3390llvm::Instruction *generator = cast<llvm::Instruction>(result);33913392SmallVector<llvm::Instruction *, 4> InstsToKill;33933394// Look for:3395// %generator = bitcast %type1* %generator2 to %type2*3396while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {3397// We would have emitted this as a constant if the operand weren't3398// an Instruction.3399generator = cast<llvm::Instruction>(bitcast->getOperand(0));34003401// Require the generator to be immediately followed by the cast.3402if (generator->getNextNode() != bitcast)3403return nullptr;34043405InstsToKill.push_back(bitcast);3406}34073408// Look for:3409// %generator = call i8* @objc_retain(i8* %originalResult)3410// or3411// %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)3412llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);3413if (!call) return nullptr;34143415bool doRetainAutorelease;34163417if (call->getCalledOperand() == CGF.CGM.getObjCEntrypoints().objc_retain) {3418doRetainAutorelease = true;3419} else if (call->getCalledOperand() ==3420CGF.CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue) {3421doRetainAutorelease = false;34223423// If we emitted an assembly marker for this call (and the3424// ARCEntrypoints field should have been set if so), go looking3425// for that call. If we can't find it, we can't do this3426// optimization. But it should always be the immediately previous3427// instruction, unless we needed bitcasts around the call.3428if (CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker) {3429llvm::Instruction *prev = call->getPrevNode();3430assert(prev);3431if (isa<llvm::BitCastInst>(prev)) {3432prev = prev->getPrevNode();3433assert(prev);3434}3435assert(isa<llvm::CallInst>(prev));3436assert(cast<llvm::CallInst>(prev)->getCalledOperand() ==3437CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker);3438InstsToKill.push_back(prev);3439}3440} else {3441return nullptr;3442}34433444result = call->getArgOperand(0);3445InstsToKill.push_back(call);34463447// Keep killing bitcasts, for sanity. Note that we no longer care3448// about precise ordering as long as there's exactly one use.3449while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {3450if (!bitcast->hasOneUse()) break;3451InstsToKill.push_back(bitcast);3452result = bitcast->getOperand(0);3453}34543455// Delete all the unnecessary instructions, from latest to earliest.3456for (auto *I : InstsToKill)3457I->eraseFromParent();34583459// Do the fused retain/autorelease if we were asked to.3460if (doRetainAutorelease)3461result = CGF.EmitARCRetainAutoreleaseReturnValue(result);34623463// Cast back to the result type.3464return CGF.Builder.CreateBitCast(result, resultType);3465}34663467/// If this is a +1 of the value of an immutable 'self', remove it.3468static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,3469llvm::Value *result) {3470// This is only applicable to a method with an immutable 'self'.3471const ObjCMethodDecl *method =3472dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);3473if (!method) return nullptr;3474const VarDecl *self = method->getSelfDecl();3475if (!self->getType().isConstQualified()) return nullptr;34763477// Look for a retain call. Note: stripPointerCasts looks through returned arg3478// functions, which would cause us to miss the retain.3479llvm::CallInst *retainCall = dyn_cast<llvm::CallInst>(result);3480if (!retainCall || retainCall->getCalledOperand() !=3481CGF.CGM.getObjCEntrypoints().objc_retain)3482return nullptr;34833484// Look for an ordinary load of 'self'.3485llvm::Value *retainedValue = retainCall->getArgOperand(0);3486llvm::LoadInst *load =3487dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());3488if (!load || load->isAtomic() || load->isVolatile() ||3489load->getPointerOperand() != CGF.GetAddrOfLocalVar(self).getBasePointer())3490return nullptr;34913492// Okay! Burn it all down. This relies for correctness on the3493// assumption that the retain is emitted as part of the return and3494// that thereafter everything is used "linearly".3495llvm::Type *resultType = result->getType();3496eraseUnusedBitCasts(cast<llvm::Instruction>(result));3497assert(retainCall->use_empty());3498retainCall->eraseFromParent();3499eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));35003501return CGF.Builder.CreateBitCast(load, resultType);3502}35033504/// Emit an ARC autorelease of the result of a function.3505///3506/// \return the value to actually return from the function3507static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,3508llvm::Value *result) {3509// If we're returning 'self', kill the initial retain. This is a3510// heuristic attempt to "encourage correctness" in the really unfortunate3511// case where we have a return of self during a dealloc and we desperately3512// need to avoid the possible autorelease.3513if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))3514return self;35153516// At -O0, try to emit a fused retain/autorelease.3517if (CGF.shouldUseFusedARCCalls())3518if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))3519return fused;35203521return CGF.EmitARCAutoreleaseReturnValue(result);3522}35233524/// Heuristically search for a dominating store to the return-value slot.3525static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {3526llvm::Value *ReturnValuePtr = CGF.ReturnValue.getBasePointer();35273528// Check if a User is a store which pointerOperand is the ReturnValue.3529// We are looking for stores to the ReturnValue, not for stores of the3530// ReturnValue to some other location.3531auto GetStoreIfValid = [&CGF,3532ReturnValuePtr](llvm::User *U) -> llvm::StoreInst * {3533auto *SI = dyn_cast<llvm::StoreInst>(U);3534if (!SI || SI->getPointerOperand() != ReturnValuePtr ||3535SI->getValueOperand()->getType() != CGF.ReturnValue.getElementType())3536return nullptr;3537// These aren't actually possible for non-coerced returns, and we3538// only care about non-coerced returns on this code path.3539// All memory instructions inside __try block are volatile.3540assert(!SI->isAtomic() &&3541(!SI->isVolatile() || CGF.currentFunctionUsesSEHTry()));3542return SI;3543};3544// If there are multiple uses of the return-value slot, just check3545// for something immediately preceding the IP. Sometimes this can3546// happen with how we generate implicit-returns; it can also happen3547// with noreturn cleanups.3548if (!ReturnValuePtr->hasOneUse()) {3549llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();3550if (IP->empty()) return nullptr;35513552// Look at directly preceding instruction, skipping bitcasts and lifetime3553// markers.3554for (llvm::Instruction &I : make_range(IP->rbegin(), IP->rend())) {3555if (isa<llvm::BitCastInst>(&I))3556continue;3557if (auto *II = dyn_cast<llvm::IntrinsicInst>(&I))3558if (II->getIntrinsicID() == llvm::Intrinsic::lifetime_end)3559continue;35603561return GetStoreIfValid(&I);3562}3563return nullptr;3564}35653566llvm::StoreInst *store = GetStoreIfValid(ReturnValuePtr->user_back());3567if (!store) return nullptr;35683569// Now do a first-and-dirty dominance check: just walk up the3570// single-predecessors chain from the current insertion point.3571llvm::BasicBlock *StoreBB = store->getParent();3572llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();3573llvm::SmallPtrSet<llvm::BasicBlock *, 4> SeenBBs;3574while (IP != StoreBB) {3575if (!SeenBBs.insert(IP).second || !(IP = IP->getSinglePredecessor()))3576return nullptr;3577}35783579// Okay, the store's basic block dominates the insertion point; we3580// can do our thing.3581return store;3582}35833584// Helper functions for EmitCMSEClearRecord35853586// Set the bits corresponding to a field having width `BitWidth` and located at3587// offset `BitOffset` (from the least significant bit) within a storage unit of3588// `Bits.size()` bytes. Each element of `Bits` corresponds to one target byte.3589// Use little-endian layout, i.e.`Bits[0]` is the LSB.3590static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int BitOffset,3591int BitWidth, int CharWidth) {3592assert(CharWidth <= 64);3593assert(static_cast<unsigned>(BitWidth) <= Bits.size() * CharWidth);35943595int Pos = 0;3596if (BitOffset >= CharWidth) {3597Pos += BitOffset / CharWidth;3598BitOffset = BitOffset % CharWidth;3599}36003601const uint64_t Used = (uint64_t(1) << CharWidth) - 1;3602if (BitOffset + BitWidth >= CharWidth) {3603Bits[Pos++] |= (Used << BitOffset) & Used;3604BitWidth -= CharWidth - BitOffset;3605BitOffset = 0;3606}36073608while (BitWidth >= CharWidth) {3609Bits[Pos++] = Used;3610BitWidth -= CharWidth;3611}36123613if (BitWidth > 0)3614Bits[Pos++] |= (Used >> (CharWidth - BitWidth)) << BitOffset;3615}36163617// Set the bits corresponding to a field having width `BitWidth` and located at3618// offset `BitOffset` (from the least significant bit) within a storage unit of3619// `StorageSize` bytes, located at `StorageOffset` in `Bits`. Each element of3620// `Bits` corresponds to one target byte. Use target endian layout.3621static void setBitRange(SmallVectorImpl<uint64_t> &Bits, int StorageOffset,3622int StorageSize, int BitOffset, int BitWidth,3623int CharWidth, bool BigEndian) {36243625SmallVector<uint64_t, 8> TmpBits(StorageSize);3626setBitRange(TmpBits, BitOffset, BitWidth, CharWidth);36273628if (BigEndian)3629std::reverse(TmpBits.begin(), TmpBits.end());36303631for (uint64_t V : TmpBits)3632Bits[StorageOffset++] |= V;3633}36343635static void setUsedBits(CodeGenModule &, QualType, int,3636SmallVectorImpl<uint64_t> &);36373638// Set the bits in `Bits`, which correspond to the value representations of3639// the actual members of the record type `RTy`. Note that this function does3640// not handle base classes, virtual tables, etc, since they cannot happen in3641// CMSE function arguments or return. The bit mask corresponds to the target3642// memory layout, i.e. it's endian dependent.3643static void setUsedBits(CodeGenModule &CGM, const RecordType *RTy, int Offset,3644SmallVectorImpl<uint64_t> &Bits) {3645ASTContext &Context = CGM.getContext();3646int CharWidth = Context.getCharWidth();3647const RecordDecl *RD = RTy->getDecl()->getDefinition();3648const ASTRecordLayout &ASTLayout = Context.getASTRecordLayout(RD);3649const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(RD);36503651int Idx = 0;3652for (auto I = RD->field_begin(), E = RD->field_end(); I != E; ++I, ++Idx) {3653const FieldDecl *F = *I;36543655if (F->isUnnamedBitField() || F->isZeroLengthBitField(Context) ||3656F->getType()->isIncompleteArrayType())3657continue;36583659if (F->isBitField()) {3660const CGBitFieldInfo &BFI = Layout.getBitFieldInfo(F);3661setBitRange(Bits, Offset + BFI.StorageOffset.getQuantity(),3662BFI.StorageSize / CharWidth, BFI.Offset,3663BFI.Size, CharWidth,3664CGM.getDataLayout().isBigEndian());3665continue;3666}36673668setUsedBits(CGM, F->getType(),3669Offset + ASTLayout.getFieldOffset(Idx) / CharWidth, Bits);3670}3671}36723673// Set the bits in `Bits`, which correspond to the value representations of3674// the elements of an array type `ATy`.3675static void setUsedBits(CodeGenModule &CGM, const ConstantArrayType *ATy,3676int Offset, SmallVectorImpl<uint64_t> &Bits) {3677const ASTContext &Context = CGM.getContext();36783679QualType ETy = Context.getBaseElementType(ATy);3680int Size = Context.getTypeSizeInChars(ETy).getQuantity();3681SmallVector<uint64_t, 4> TmpBits(Size);3682setUsedBits(CGM, ETy, 0, TmpBits);36833684for (int I = 0, N = Context.getConstantArrayElementCount(ATy); I < N; ++I) {3685auto Src = TmpBits.begin();3686auto Dst = Bits.begin() + Offset + I * Size;3687for (int J = 0; J < Size; ++J)3688*Dst++ |= *Src++;3689}3690}36913692// Set the bits in `Bits`, which correspond to the value representations of3693// the type `QTy`.3694static void setUsedBits(CodeGenModule &CGM, QualType QTy, int Offset,3695SmallVectorImpl<uint64_t> &Bits) {3696if (const auto *RTy = QTy->getAs<RecordType>())3697return setUsedBits(CGM, RTy, Offset, Bits);36983699ASTContext &Context = CGM.getContext();3700if (const auto *ATy = Context.getAsConstantArrayType(QTy))3701return setUsedBits(CGM, ATy, Offset, Bits);37023703int Size = Context.getTypeSizeInChars(QTy).getQuantity();3704if (Size <= 0)3705return;37063707std::fill_n(Bits.begin() + Offset, Size,3708(uint64_t(1) << Context.getCharWidth()) - 1);3709}37103711static uint64_t buildMultiCharMask(const SmallVectorImpl<uint64_t> &Bits,3712int Pos, int Size, int CharWidth,3713bool BigEndian) {3714assert(Size > 0);3715uint64_t Mask = 0;3716if (BigEndian) {3717for (auto P = Bits.begin() + Pos, E = Bits.begin() + Pos + Size; P != E;3718++P)3719Mask = (Mask << CharWidth) | *P;3720} else {3721auto P = Bits.begin() + Pos + Size, End = Bits.begin() + Pos;3722do3723Mask = (Mask << CharWidth) | *--P;3724while (P != End);3725}3726return Mask;3727}37283729// Emit code to clear the bits in a record, which aren't a part of any user3730// declared member, when the record is a function return.3731llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,3732llvm::IntegerType *ITy,3733QualType QTy) {3734assert(Src->getType() == ITy);3735assert(ITy->getScalarSizeInBits() <= 64);37363737const llvm::DataLayout &DataLayout = CGM.getDataLayout();3738int Size = DataLayout.getTypeStoreSize(ITy);3739SmallVector<uint64_t, 4> Bits(Size);3740setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);37413742int CharWidth = CGM.getContext().getCharWidth();3743uint64_t Mask =3744buildMultiCharMask(Bits, 0, Size, CharWidth, DataLayout.isBigEndian());37453746return Builder.CreateAnd(Src, Mask, "cmse.clear");3747}37483749// Emit code to clear the bits in a record, which aren't a part of any user3750// declared member, when the record is a function argument.3751llvm::Value *CodeGenFunction::EmitCMSEClearRecord(llvm::Value *Src,3752llvm::ArrayType *ATy,3753QualType QTy) {3754const llvm::DataLayout &DataLayout = CGM.getDataLayout();3755int Size = DataLayout.getTypeStoreSize(ATy);3756SmallVector<uint64_t, 16> Bits(Size);3757setUsedBits(CGM, QTy->castAs<RecordType>(), 0, Bits);37583759// Clear each element of the LLVM array.3760int CharWidth = CGM.getContext().getCharWidth();3761int CharsPerElt =3762ATy->getArrayElementType()->getScalarSizeInBits() / CharWidth;3763int MaskIndex = 0;3764llvm::Value *R = llvm::PoisonValue::get(ATy);3765for (int I = 0, N = ATy->getArrayNumElements(); I != N; ++I) {3766uint64_t Mask = buildMultiCharMask(Bits, MaskIndex, CharsPerElt, CharWidth,3767DataLayout.isBigEndian());3768MaskIndex += CharsPerElt;3769llvm::Value *T0 = Builder.CreateExtractValue(Src, I);3770llvm::Value *T1 = Builder.CreateAnd(T0, Mask, "cmse.clear");3771R = Builder.CreateInsertValue(R, T1, I);3772}37733774return R;3775}37763777void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,3778bool EmitRetDbgLoc,3779SourceLocation EndLoc) {3780if (FI.isNoReturn()) {3781// Noreturn functions don't return.3782EmitUnreachable(EndLoc);3783return;3784}37853786if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {3787// Naked functions don't have epilogues.3788Builder.CreateUnreachable();3789return;3790}37913792// Functions with no result always return void.3793if (!ReturnValue.isValid()) {3794Builder.CreateRetVoid();3795return;3796}37973798llvm::DebugLoc RetDbgLoc;3799llvm::Value *RV = nullptr;3800QualType RetTy = FI.getReturnType();3801const ABIArgInfo &RetAI = FI.getReturnInfo();38023803switch (RetAI.getKind()) {3804case ABIArgInfo::InAlloca:3805// Aggregates get evaluated directly into the destination. Sometimes we3806// need to return the sret value in a register, though.3807assert(hasAggregateEvaluationKind(RetTy));3808if (RetAI.getInAllocaSRet()) {3809llvm::Function::arg_iterator EI = CurFn->arg_end();3810--EI;3811llvm::Value *ArgStruct = &*EI;3812llvm::Value *SRet = Builder.CreateStructGEP(3813FI.getArgStruct(), ArgStruct, RetAI.getInAllocaFieldIndex());3814llvm::Type *Ty =3815cast<llvm::GetElementPtrInst>(SRet)->getResultElementType();3816RV = Builder.CreateAlignedLoad(Ty, SRet, getPointerAlign(), "sret");3817}3818break;38193820case ABIArgInfo::Indirect: {3821auto AI = CurFn->arg_begin();3822if (RetAI.isSRetAfterThis())3823++AI;3824switch (getEvaluationKind(RetTy)) {3825case TEK_Complex: {3826ComplexPairTy RT =3827EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);3828EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),3829/*isInit*/ true);3830break;3831}3832case TEK_Aggregate:3833// Do nothing; aggregates get evaluated directly into the destination.3834break;3835case TEK_Scalar: {3836LValueBaseInfo BaseInfo;3837TBAAAccessInfo TBAAInfo;3838CharUnits Alignment =3839CGM.getNaturalTypeAlignment(RetTy, &BaseInfo, &TBAAInfo);3840Address ArgAddr(&*AI, ConvertType(RetTy), Alignment);3841LValue ArgVal =3842LValue::MakeAddr(ArgAddr, RetTy, getContext(), BaseInfo, TBAAInfo);3843EmitStoreOfScalar(3844EmitLoadOfScalar(MakeAddrLValue(ReturnValue, RetTy), EndLoc), ArgVal,3845/*isInit*/ true);3846break;3847}3848}3849break;3850}38513852case ABIArgInfo::Extend:3853case ABIArgInfo::Direct:3854if (RetAI.getCoerceToType() == ConvertType(RetTy) &&3855RetAI.getDirectOffset() == 0) {3856// The internal return value temp always will have pointer-to-return-type3857// type, just do a load.38583859// If there is a dominating store to ReturnValue, we can elide3860// the load, zap the store, and usually zap the alloca.3861if (llvm::StoreInst *SI =3862findDominatingStoreToReturnValue(*this)) {3863// Reuse the debug location from the store unless there is3864// cleanup code to be emitted between the store and return3865// instruction.3866if (EmitRetDbgLoc && !AutoreleaseResult)3867RetDbgLoc = SI->getDebugLoc();3868// Get the stored value and nuke the now-dead store.3869RV = SI->getValueOperand();3870SI->eraseFromParent();38713872// Otherwise, we have to do a simple load.3873} else {3874RV = Builder.CreateLoad(ReturnValue);3875}3876} else {3877// If the value is offset in memory, apply the offset now.3878Address V = emitAddressAtOffset(*this, ReturnValue, RetAI);38793880RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);3881}38823883// In ARC, end functions that return a retainable type with a call3884// to objc_autoreleaseReturnValue.3885if (AutoreleaseResult) {3886#ifndef NDEBUG3887// Type::isObjCRetainabletype has to be called on a QualType that hasn't3888// been stripped of the typedefs, so we cannot use RetTy here. Get the3889// original return type of FunctionDecl, CurCodeDecl, and BlockDecl from3890// CurCodeDecl or BlockInfo.3891QualType RT;38923893if (auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))3894RT = FD->getReturnType();3895else if (auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))3896RT = MD->getReturnType();3897else if (isa<BlockDecl>(CurCodeDecl))3898RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();3899else3900llvm_unreachable("Unexpected function/method type");39013902assert(getLangOpts().ObjCAutoRefCount &&3903!FI.isReturnsRetained() &&3904RT->isObjCRetainableType());3905#endif3906RV = emitAutoreleaseOfResult(*this, RV);3907}39083909break;39103911case ABIArgInfo::Ignore:3912break;39133914case ABIArgInfo::CoerceAndExpand: {3915auto coercionType = RetAI.getCoerceAndExpandType();39163917// Load all of the coerced elements out into results.3918llvm::SmallVector<llvm::Value*, 4> results;3919Address addr = ReturnValue.withElementType(coercionType);3920for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {3921auto coercedEltType = coercionType->getElementType(i);3922if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))3923continue;39243925auto eltAddr = Builder.CreateStructGEP(addr, i);3926auto elt = Builder.CreateLoad(eltAddr);3927results.push_back(elt);3928}39293930// If we have one result, it's the single direct result type.3931if (results.size() == 1) {3932RV = results[0];39333934// Otherwise, we need to make a first-class aggregate.3935} else {3936// Construct a return type that lacks padding elements.3937llvm::Type *returnType = RetAI.getUnpaddedCoerceAndExpandType();39383939RV = llvm::PoisonValue::get(returnType);3940for (unsigned i = 0, e = results.size(); i != e; ++i) {3941RV = Builder.CreateInsertValue(RV, results[i], i);3942}3943}3944break;3945}3946case ABIArgInfo::Expand:3947case ABIArgInfo::IndirectAliased:3948llvm_unreachable("Invalid ABI kind for return argument");3949}39503951llvm::Instruction *Ret;3952if (RV) {3953if (CurFuncDecl && CurFuncDecl->hasAttr<CmseNSEntryAttr>()) {3954// For certain return types, clear padding bits, as they may reveal3955// sensitive information.3956// Small struct/union types are passed as integers.3957auto *ITy = dyn_cast<llvm::IntegerType>(RV->getType());3958if (ITy != nullptr && isa<RecordType>(RetTy.getCanonicalType()))3959RV = EmitCMSEClearRecord(RV, ITy, RetTy);3960}3961EmitReturnValueCheck(RV);3962Ret = Builder.CreateRet(RV);3963} else {3964Ret = Builder.CreateRetVoid();3965}39663967if (RetDbgLoc)3968Ret->setDebugLoc(std::move(RetDbgLoc));3969}39703971void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {3972// A current decl may not be available when emitting vtable thunks.3973if (!CurCodeDecl)3974return;39753976// If the return block isn't reachable, neither is this check, so don't emit3977// it.3978if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())3979return;39803981ReturnsNonNullAttr *RetNNAttr = nullptr;3982if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))3983RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();39843985if (!RetNNAttr && !requiresReturnValueNullabilityCheck())3986return;39873988// Prefer the returns_nonnull attribute if it's present.3989SourceLocation AttrLoc;3990SanitizerMask CheckKind;3991SanitizerHandler Handler;3992if (RetNNAttr) {3993assert(!requiresReturnValueNullabilityCheck() &&3994"Cannot check nullability and the nonnull attribute");3995AttrLoc = RetNNAttr->getLocation();3996CheckKind = SanitizerKind::ReturnsNonnullAttribute;3997Handler = SanitizerHandler::NonnullReturn;3998} else {3999if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))4000if (auto *TSI = DD->getTypeSourceInfo())4001if (auto FTL = TSI->getTypeLoc().getAsAdjusted<FunctionTypeLoc>())4002AttrLoc = FTL.getReturnLoc().findNullabilityLoc();4003CheckKind = SanitizerKind::NullabilityReturn;4004Handler = SanitizerHandler::NullabilityReturn;4005}40064007SanitizerScope SanScope(this);40084009// Make sure the "return" source location is valid. If we're checking a4010// nullability annotation, make sure the preconditions for the check are met.4011llvm::BasicBlock *Check = createBasicBlock("nullcheck");4012llvm::BasicBlock *NoCheck = createBasicBlock("no.nullcheck");4013llvm::Value *SLocPtr = Builder.CreateLoad(ReturnLocation, "return.sloc.load");4014llvm::Value *CanNullCheck = Builder.CreateIsNotNull(SLocPtr);4015if (requiresReturnValueNullabilityCheck())4016CanNullCheck =4017Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);4018Builder.CreateCondBr(CanNullCheck, Check, NoCheck);4019EmitBlock(Check);40204021// Now do the null check.4022llvm::Value *Cond = Builder.CreateIsNotNull(RV);4023llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};4024llvm::Value *DynamicData[] = {SLocPtr};4025EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);40264027EmitBlock(NoCheck);40284029#ifndef NDEBUG4030// The return location should not be used after the check has been emitted.4031ReturnLocation = Address::invalid();4032#endif4033}40344035static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) {4036const CXXRecordDecl *RD = type->getAsCXXRecordDecl();4037return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory;4038}40394040static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF,4041QualType Ty) {4042// FIXME: Generate IR in one pass, rather than going back and fixing up these4043// placeholders.4044llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty);4045llvm::Type *IRPtrTy = llvm::PointerType::getUnqual(CGF.getLLVMContext());4046llvm::Value *Placeholder = llvm::PoisonValue::get(IRPtrTy);40474048// FIXME: When we generate this IR in one pass, we shouldn't need4049// this win32-specific alignment hack.4050CharUnits Align = CharUnits::fromQuantity(4);4051Placeholder = CGF.Builder.CreateAlignedLoad(IRPtrTy, Placeholder, Align);40524053return AggValueSlot::forAddr(Address(Placeholder, IRTy, Align),4054Ty.getQualifiers(),4055AggValueSlot::IsNotDestructed,4056AggValueSlot::DoesNotNeedGCBarriers,4057AggValueSlot::IsNotAliased,4058AggValueSlot::DoesNotOverlap);4059}40604061void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,4062const VarDecl *param,4063SourceLocation loc) {4064// StartFunction converted the ABI-lowered parameter(s) into a4065// local alloca. We need to turn that into an r-value suitable4066// for EmitCall.4067Address local = GetAddrOfLocalVar(param);40684069QualType type = param->getType();40704071// GetAddrOfLocalVar returns a pointer-to-pointer for references,4072// but the argument needs to be the original pointer.4073if (type->isReferenceType()) {4074args.add(RValue::get(Builder.CreateLoad(local)), type);40754076// In ARC, move out of consumed arguments so that the release cleanup4077// entered by StartFunction doesn't cause an over-release. This isn't4078// optimal -O0 code generation, but it should get cleaned up when4079// optimization is enabled. This also assumes that delegate calls are4080// performed exactly once for a set of arguments, but that should be safe.4081} else if (getLangOpts().ObjCAutoRefCount &&4082param->hasAttr<NSConsumedAttr>() &&4083type->isObjCRetainableType()) {4084llvm::Value *ptr = Builder.CreateLoad(local);4085auto null =4086llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));4087Builder.CreateStore(null, local);4088args.add(RValue::get(ptr), type);40894090// For the most part, we just need to load the alloca, except that4091// aggregate r-values are actually pointers to temporaries.4092} else {4093args.add(convertTempToRValue(local, type, loc), type);4094}40954096// Deactivate the cleanup for the callee-destructed param that was pushed.4097if (type->isRecordType() && !CurFuncIsThunk &&4098type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee() &&4099param->needsDestruction(getContext())) {4100EHScopeStack::stable_iterator cleanup =4101CalleeDestructedParamCleanups.lookup(cast<ParmVarDecl>(param));4102assert(cleanup.isValid() &&4103"cleanup for callee-destructed param not recorded");4104// This unreachable is a temporary marker which will be removed later.4105llvm::Instruction *isActive = Builder.CreateUnreachable();4106args.addArgCleanupDeactivation(cleanup, isActive);4107}4108}41094110static bool isProvablyNull(llvm::Value *addr) {4111return llvm::isa_and_nonnull<llvm::ConstantPointerNull>(addr);4112}41134114static bool isProvablyNonNull(Address Addr, CodeGenFunction &CGF) {4115return llvm::isKnownNonZero(Addr.getBasePointer(), CGF.CGM.getDataLayout());4116}41174118/// Emit the actual writing-back of a writeback.4119static void emitWriteback(CodeGenFunction &CGF,4120const CallArgList::Writeback &writeback) {4121const LValue &srcLV = writeback.Source;4122Address srcAddr = srcLV.getAddress();4123assert(!isProvablyNull(srcAddr.getBasePointer()) &&4124"shouldn't have writeback for provably null argument");41254126llvm::BasicBlock *contBB = nullptr;41274128// If the argument wasn't provably non-null, we need to null check4129// before doing the store.4130bool provablyNonNull = isProvablyNonNull(srcAddr, CGF);41314132if (!provablyNonNull) {4133llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");4134contBB = CGF.createBasicBlock("icr.done");41354136llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");4137CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);4138CGF.EmitBlock(writebackBB);4139}41404141// Load the value to writeback.4142llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);41434144// Cast it back, in case we're writing an id to a Foo* or something.4145value = CGF.Builder.CreateBitCast(value, srcAddr.getElementType(),4146"icr.writeback-cast");41474148// Perform the writeback.41494150// If we have a "to use" value, it's something we need to emit a use4151// of. This has to be carefully threaded in: if it's done after the4152// release it's potentially undefined behavior (and the optimizer4153// will ignore it), and if it happens before the retain then the4154// optimizer could move the release there.4155if (writeback.ToUse) {4156assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);41574158// Retain the new value. No need to block-copy here: the block's4159// being passed up the stack.4160value = CGF.EmitARCRetainNonBlock(value);41614162// Emit the intrinsic use here.4163CGF.EmitARCIntrinsicUse(writeback.ToUse);41644165// Load the old value (primitively).4166llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation());41674168// Put the new value in place (primitively).4169CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);41704171// Release the old value.4172CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());41734174// Otherwise, we can just do a normal lvalue store.4175} else {4176CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);4177}41784179// Jump to the continuation block.4180if (!provablyNonNull)4181CGF.EmitBlock(contBB);4182}41834184static void emitWritebacks(CodeGenFunction &CGF,4185const CallArgList &args) {4186for (const auto &I : args.writebacks())4187emitWriteback(CGF, I);4188}41894190static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF,4191const CallArgList &CallArgs) {4192ArrayRef<CallArgList::CallArgCleanup> Cleanups =4193CallArgs.getCleanupsToDeactivate();4194// Iterate in reverse to increase the likelihood of popping the cleanup.4195for (const auto &I : llvm::reverse(Cleanups)) {4196CGF.DeactivateCleanupBlock(I.Cleanup, I.IsActiveIP);4197I.IsActiveIP->eraseFromParent();4198}4199}42004201static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {4202if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))4203if (uop->getOpcode() == UO_AddrOf)4204return uop->getSubExpr();4205return nullptr;4206}42074208/// Emit an argument that's being passed call-by-writeback. That is,4209/// we are passing the address of an __autoreleased temporary; it4210/// might be copy-initialized with the current value of the given4211/// address, but it will definitely be copied out of after the call.4212static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,4213const ObjCIndirectCopyRestoreExpr *CRE) {4214LValue srcLV;42154216// Make an optimistic effort to emit the address as an l-value.4217// This can fail if the argument expression is more complicated.4218if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {4219srcLV = CGF.EmitLValue(lvExpr);42204221// Otherwise, just emit it as a scalar.4222} else {4223Address srcAddr = CGF.EmitPointerWithAlignment(CRE->getSubExpr());42244225QualType srcAddrType =4226CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();4227srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);4228}4229Address srcAddr = srcLV.getAddress();42304231// The dest and src types don't necessarily match in LLVM terms4232// because of the crazy ObjC compatibility rules.42334234llvm::PointerType *destType =4235cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));4236llvm::Type *destElemType =4237CGF.ConvertTypeForMem(CRE->getType()->getPointeeType());42384239// If the address is a constant null, just pass the appropriate null.4240if (isProvablyNull(srcAddr.getBasePointer())) {4241args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),4242CRE->getType());4243return;4244}42454246// Create the temporary.4247Address temp =4248CGF.CreateTempAlloca(destElemType, CGF.getPointerAlign(), "icr.temp");4249// Loading an l-value can introduce a cleanup if the l-value is __weak,4250// and that cleanup will be conditional if we can't prove that the l-value4251// isn't null, so we need to register a dominating point so that the cleanups4252// system will make valid IR.4253CodeGenFunction::ConditionalEvaluation condEval(CGF);42544255// Zero-initialize it if we're not doing a copy-initialization.4256bool shouldCopy = CRE->shouldCopy();4257if (!shouldCopy) {4258llvm::Value *null =4259llvm::ConstantPointerNull::get(cast<llvm::PointerType>(destElemType));4260CGF.Builder.CreateStore(null, temp);4261}42624263llvm::BasicBlock *contBB = nullptr;4264llvm::BasicBlock *originBB = nullptr;42654266// If the address is *not* known to be non-null, we need to switch.4267llvm::Value *finalArgument;42684269bool provablyNonNull = isProvablyNonNull(srcAddr, CGF);42704271if (provablyNonNull) {4272finalArgument = temp.emitRawPointer(CGF);4273} else {4274llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");42754276finalArgument = CGF.Builder.CreateSelect(4277isNull, llvm::ConstantPointerNull::get(destType),4278temp.emitRawPointer(CGF), "icr.argument");42794280// If we need to copy, then the load has to be conditional, which4281// means we need control flow.4282if (shouldCopy) {4283originBB = CGF.Builder.GetInsertBlock();4284contBB = CGF.createBasicBlock("icr.cont");4285llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");4286CGF.Builder.CreateCondBr(isNull, contBB, copyBB);4287CGF.EmitBlock(copyBB);4288condEval.begin(CGF);4289}4290}42914292llvm::Value *valueToUse = nullptr;42934294// Perform a copy if necessary.4295if (shouldCopy) {4296RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation());4297assert(srcRV.isScalar());42984299llvm::Value *src = srcRV.getScalarVal();4300src = CGF.Builder.CreateBitCast(src, destElemType, "icr.cast");43014302// Use an ordinary store, not a store-to-lvalue.4303CGF.Builder.CreateStore(src, temp);43044305// If optimization is enabled, and the value was held in a4306// __strong variable, we need to tell the optimizer that this4307// value has to stay alive until we're doing the store back.4308// This is because the temporary is effectively unretained,4309// and so otherwise we can violate the high-level semantics.4310if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&4311srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {4312valueToUse = src;4313}4314}43154316// Finish the control flow if we needed it.4317if (shouldCopy && !provablyNonNull) {4318llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();4319CGF.EmitBlock(contBB);43204321// Make a phi for the value to intrinsically use.4322if (valueToUse) {4323llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,4324"icr.to-use");4325phiToUse->addIncoming(valueToUse, copyBB);4326phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),4327originBB);4328valueToUse = phiToUse;4329}43304331condEval.end(CGF);4332}43334334args.addWriteback(srcLV, temp, valueToUse);4335args.add(RValue::get(finalArgument), CRE->getType());4336}43374338void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) {4339assert(!StackBase);43404341// Save the stack.4342StackBase = CGF.Builder.CreateStackSave("inalloca.save");4343}43444345void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const {4346if (StackBase) {4347// Restore the stack after the call.4348CGF.Builder.CreateStackRestore(StackBase);4349}4350}43514352void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,4353SourceLocation ArgLoc,4354AbstractCallee AC,4355unsigned ParmNum) {4356if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||4357SanOpts.has(SanitizerKind::NullabilityArg)))4358return;43594360// The param decl may be missing in a variadic function.4361auto PVD = ParmNum < AC.getNumParams() ? AC.getParamDecl(ParmNum) : nullptr;4362unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;43634364// Prefer the nonnull attribute if it's present.4365const NonNullAttr *NNAttr = nullptr;4366if (SanOpts.has(SanitizerKind::NonnullAttribute))4367NNAttr = getNonNullAttr(AC.getDecl(), PVD, ArgType, ArgNo);43684369bool CanCheckNullability = false;4370if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD &&4371!PVD->getType()->isRecordType()) {4372auto Nullability = PVD->getType()->getNullability();4373CanCheckNullability = Nullability &&4374*Nullability == NullabilityKind::NonNull &&4375PVD->getTypeSourceInfo();4376}43774378if (!NNAttr && !CanCheckNullability)4379return;43804381SourceLocation AttrLoc;4382SanitizerMask CheckKind;4383SanitizerHandler Handler;4384if (NNAttr) {4385AttrLoc = NNAttr->getLocation();4386CheckKind = SanitizerKind::NonnullAttribute;4387Handler = SanitizerHandler::NonnullArg;4388} else {4389AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();4390CheckKind = SanitizerKind::NullabilityArg;4391Handler = SanitizerHandler::NullabilityArg;4392}43934394SanitizerScope SanScope(this);4395llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);4396llvm::Constant *StaticData[] = {4397EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),4398llvm::ConstantInt::get(Int32Ty, ArgNo + 1),4399};4400EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, std::nullopt);4401}44024403void CodeGenFunction::EmitNonNullArgCheck(Address Addr, QualType ArgType,4404SourceLocation ArgLoc,4405AbstractCallee AC, unsigned ParmNum) {4406if (!AC.getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||4407SanOpts.has(SanitizerKind::NullabilityArg)))4408return;44094410EmitNonNullArgCheck(RValue::get(Addr, *this), ArgType, ArgLoc, AC, ParmNum);4411}44124413// Check if the call is going to use the inalloca convention. This needs to4414// agree with CGFunctionInfo::usesInAlloca. The CGFunctionInfo is arranged4415// later, so we can't check it directly.4416static bool hasInAllocaArgs(CodeGenModule &CGM, CallingConv ExplicitCC,4417ArrayRef<QualType> ArgTypes) {4418// The Swift calling conventions don't go through the target-specific4419// argument classification, they never use inalloca.4420// TODO: Consider limiting inalloca use to only calling conventions supported4421// by MSVC.4422if (ExplicitCC == CC_Swift || ExplicitCC == CC_SwiftAsync)4423return false;4424if (!CGM.getTarget().getCXXABI().isMicrosoft())4425return false;4426return llvm::any_of(ArgTypes, [&](QualType Ty) {4427return isInAllocaArgument(CGM.getCXXABI(), Ty);4428});4429}44304431#ifndef NDEBUG4432// Determine whether the given argument is an Objective-C method4433// that may have type parameters in its signature.4434static bool isObjCMethodWithTypeParams(const ObjCMethodDecl *method) {4435const DeclContext *dc = method->getDeclContext();4436if (const ObjCInterfaceDecl *classDecl = dyn_cast<ObjCInterfaceDecl>(dc)) {4437return classDecl->getTypeParamListAsWritten();4438}44394440if (const ObjCCategoryDecl *catDecl = dyn_cast<ObjCCategoryDecl>(dc)) {4441return catDecl->getTypeParamList();4442}44434444return false;4445}4446#endif44474448/// EmitCallArgs - Emit call arguments for a function.4449void CodeGenFunction::EmitCallArgs(4450CallArgList &Args, PrototypeWrapper Prototype,4451llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,4452AbstractCallee AC, unsigned ParamsToSkip, EvaluationOrder Order) {4453SmallVector<QualType, 16> ArgTypes;44544455assert((ParamsToSkip == 0 || Prototype.P) &&4456"Can't skip parameters if type info is not provided");44574458// This variable only captures *explicitly* written conventions, not those4459// applied by default via command line flags or target defaults, such as4460// thiscall, aapcs, stdcall via -mrtd, etc. Computing that correctly would4461// require knowing if this is a C++ instance method or being able to see4462// unprototyped FunctionTypes.4463CallingConv ExplicitCC = CC_C;44644465// First, if a prototype was provided, use those argument types.4466bool IsVariadic = false;4467if (Prototype.P) {4468const auto *MD = Prototype.P.dyn_cast<const ObjCMethodDecl *>();4469if (MD) {4470IsVariadic = MD->isVariadic();4471ExplicitCC = getCallingConventionForDecl(4472MD, CGM.getTarget().getTriple().isOSWindows());4473ArgTypes.assign(MD->param_type_begin() + ParamsToSkip,4474MD->param_type_end());4475} else {4476const auto *FPT = Prototype.P.get<const FunctionProtoType *>();4477IsVariadic = FPT->isVariadic();4478ExplicitCC = FPT->getExtInfo().getCC();4479ArgTypes.assign(FPT->param_type_begin() + ParamsToSkip,4480FPT->param_type_end());4481}44824483#ifndef NDEBUG4484// Check that the prototyped types match the argument expression types.4485bool isGenericMethod = MD && isObjCMethodWithTypeParams(MD);4486CallExpr::const_arg_iterator Arg = ArgRange.begin();4487for (QualType Ty : ArgTypes) {4488assert(Arg != ArgRange.end() && "Running over edge of argument list!");4489assert(4490(isGenericMethod || Ty->isVariablyModifiedType() ||4491Ty.getNonReferenceType()->isObjCRetainableType() ||4492getContext()4493.getCanonicalType(Ty.getNonReferenceType())4494.getTypePtr() ==4495getContext().getCanonicalType((*Arg)->getType()).getTypePtr()) &&4496"type mismatch in call argument!");4497++Arg;4498}44994500// Either we've emitted all the call args, or we have a call to variadic4501// function.4502assert((Arg == ArgRange.end() || IsVariadic) &&4503"Extra arguments in non-variadic function!");4504#endif4505}45064507// If we still have any arguments, emit them using the type of the argument.4508for (auto *A : llvm::drop_begin(ArgRange, ArgTypes.size()))4509ArgTypes.push_back(IsVariadic ? getVarArgType(A) : A->getType());4510assert((int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));45114512// We must evaluate arguments from right to left in the MS C++ ABI,4513// because arguments are destroyed left to right in the callee. As a special4514// case, there are certain language constructs that require left-to-right4515// evaluation, and in those cases we consider the evaluation order requirement4516// to trump the "destruction order is reverse construction order" guarantee.4517bool LeftToRight =4518CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()4519? Order == EvaluationOrder::ForceLeftToRight4520: Order != EvaluationOrder::ForceRightToLeft;45214522auto MaybeEmitImplicitObjectSize = [&](unsigned I, const Expr *Arg,4523RValue EmittedArg) {4524if (!AC.hasFunctionDecl() || I >= AC.getNumParams())4525return;4526auto *PS = AC.getParamDecl(I)->getAttr<PassObjectSizeAttr>();4527if (PS == nullptr)4528return;45294530const auto &Context = getContext();4531auto SizeTy = Context.getSizeType();4532auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));4533assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");4534llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,4535EmittedArg.getScalarVal(),4536PS->isDynamic());4537Args.add(RValue::get(V), SizeTy);4538// If we're emitting args in reverse, be sure to do so with4539// pass_object_size, as well.4540if (!LeftToRight)4541std::swap(Args.back(), *(&Args.back() - 1));4542};45434544// Insert a stack save if we're going to need any inalloca args.4545if (hasInAllocaArgs(CGM, ExplicitCC, ArgTypes)) {4546assert(getTarget().getTriple().getArch() == llvm::Triple::x86 &&4547"inalloca only supported on x86");4548Args.allocateArgumentMemory(*this);4549}45504551// Evaluate each argument in the appropriate order.4552size_t CallArgsStart = Args.size();4553for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) {4554unsigned Idx = LeftToRight ? I : E - I - 1;4555CallExpr::const_arg_iterator Arg = ArgRange.begin() + Idx;4556unsigned InitialArgSize = Args.size();4557// If *Arg is an ObjCIndirectCopyRestoreExpr, check that either the types of4558// the argument and parameter match or the objc method is parameterized.4559assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||4560getContext().hasSameUnqualifiedType((*Arg)->getType(),4561ArgTypes[Idx]) ||4562(isa<ObjCMethodDecl>(AC.getDecl()) &&4563isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.getDecl())))) &&4564"Argument and parameter types don't match");4565EmitCallArg(Args, *Arg, ArgTypes[Idx]);4566// In particular, we depend on it being the last arg in Args, and the4567// objectsize bits depend on there only being one arg if !LeftToRight.4568assert(InitialArgSize + 1 == Args.size() &&4569"The code below depends on only adding one arg per EmitCallArg");4570(void)InitialArgSize;4571// Since pointer argument are never emitted as LValue, it is safe to emit4572// non-null argument check for r-value only.4573if (!Args.back().hasLValue()) {4574RValue RVArg = Args.back().getKnownRValue();4575EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,4576ParamsToSkip + Idx);4577// @llvm.objectsize should never have side-effects and shouldn't need4578// destruction/cleanups, so we can safely "emit" it after its arg,4579// regardless of right-to-leftness4580MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);4581}4582}45834584if (!LeftToRight) {4585// Un-reverse the arguments we just evaluated so they match up with the LLVM4586// IR function.4587std::reverse(Args.begin() + CallArgsStart, Args.end());4588}4589}45904591namespace {45924593struct DestroyUnpassedArg final : EHScopeStack::Cleanup {4594DestroyUnpassedArg(Address Addr, QualType Ty)4595: Addr(Addr), Ty(Ty) {}45964597Address Addr;4598QualType Ty;45994600void Emit(CodeGenFunction &CGF, Flags flags) override {4601QualType::DestructionKind DtorKind = Ty.isDestructedType();4602if (DtorKind == QualType::DK_cxx_destructor) {4603const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor();4604assert(!Dtor->isTrivial());4605CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false,4606/*Delegating=*/false, Addr, Ty);4607} else {4608CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Ty));4609}4610}4611};46124613struct DisableDebugLocationUpdates {4614CodeGenFunction &CGF;4615bool disabledDebugInfo;4616DisableDebugLocationUpdates(CodeGenFunction &CGF, const Expr *E) : CGF(CGF) {4617if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.getDebugInfo()))4618CGF.disableDebugInfo();4619}4620~DisableDebugLocationUpdates() {4621if (disabledDebugInfo)4622CGF.enableDebugInfo();4623}4624};46254626} // end anonymous namespace46274628RValue CallArg::getRValue(CodeGenFunction &CGF) const {4629if (!HasLV)4630return RV;4631LValue Copy = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty), Ty);4632CGF.EmitAggregateCopy(Copy, LV, Ty, AggValueSlot::DoesNotOverlap,4633LV.isVolatile());4634IsUsed = true;4635return RValue::getAggregate(Copy.getAddress());4636}46374638void CallArg::copyInto(CodeGenFunction &CGF, Address Addr) const {4639LValue Dst = CGF.MakeAddrLValue(Addr, Ty);4640if (!HasLV && RV.isScalar())4641CGF.EmitStoreOfScalar(RV.getScalarVal(), Dst, /*isInit=*/true);4642else if (!HasLV && RV.isComplex())4643CGF.EmitStoreOfComplex(RV.getComplexVal(), Dst, /*init=*/true);4644else {4645auto Addr = HasLV ? LV.getAddress() : RV.getAggregateAddress();4646LValue SrcLV = CGF.MakeAddrLValue(Addr, Ty);4647// We assume that call args are never copied into subobjects.4648CGF.EmitAggregateCopy(Dst, SrcLV, Ty, AggValueSlot::DoesNotOverlap,4649HasLV ? LV.isVolatileQualified()4650: RV.isVolatileQualified());4651}4652IsUsed = true;4653}46544655void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,4656QualType type) {4657DisableDebugLocationUpdates Dis(*this, E);4658if (const ObjCIndirectCopyRestoreExpr *CRE4659= dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {4660assert(getLangOpts().ObjCAutoRefCount);4661return emitWritebackArg(*this, args, CRE);4662}46634664assert(type->isReferenceType() == E->isGLValue() &&4665"reference binding to unmaterialized r-value!");46664667if (E->isGLValue()) {4668assert(E->getObjectKind() == OK_Ordinary);4669return args.add(EmitReferenceBindingToExpr(E), type);4670}46714672bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);46734674// In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.4675// However, we still have to push an EH-only cleanup in case we unwind before4676// we make it to the call.4677if (type->isRecordType() &&4678type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {4679// If we're using inalloca, use the argument memory. Otherwise, use a4680// temporary.4681AggValueSlot Slot = args.isUsingInAlloca()4682? createPlaceholderSlot(*this, type) : CreateAggTemp(type, "agg.tmp");46834684bool DestroyedInCallee = true, NeedsCleanup = true;4685if (const auto *RD = type->getAsCXXRecordDecl())4686DestroyedInCallee = RD->hasNonTrivialDestructor();4687else4688NeedsCleanup = type.isDestructedType();46894690if (DestroyedInCallee)4691Slot.setExternallyDestructed();46924693EmitAggExpr(E, Slot);4694RValue RV = Slot.asRValue();4695args.add(RV, type);46964697if (DestroyedInCallee && NeedsCleanup) {4698// Create a no-op GEP between the placeholder and the cleanup so we can4699// RAUW it successfully. It also serves as a marker of the first4700// instruction where the cleanup is active.4701pushFullExprCleanup<DestroyUnpassedArg>(NormalAndEHCleanup,4702Slot.getAddress(), type);4703// This unreachable is a temporary marker which will be removed later.4704llvm::Instruction *IsActive =4705Builder.CreateFlagLoad(llvm::Constant::getNullValue(Int8PtrTy));4706args.addArgCleanupDeactivation(EHStack.stable_begin(), IsActive);4707}4708return;4709}47104711if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&4712cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue &&4713!type->isArrayParameterType()) {4714LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());4715assert(L.isSimple());4716args.addUncopiedAggregate(L, type);4717return;4718}47194720args.add(EmitAnyExprToTemp(E), type);4721}47224723QualType CodeGenFunction::getVarArgType(const Expr *Arg) {4724// System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC4725// implicitly widens null pointer constants that are arguments to varargs4726// functions to pointer-sized ints.4727if (!getTarget().getTriple().isOSWindows())4728return Arg->getType();47294730if (Arg->getType()->isIntegerType() &&4731getContext().getTypeSize(Arg->getType()) <4732getContext().getTargetInfo().getPointerWidth(LangAS::Default) &&4733Arg->isNullPointerConstant(getContext(),4734Expr::NPC_ValueDependentIsNotNull)) {4735return getContext().getIntPtrType();4736}47374738return Arg->getType();4739}47404741// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC4742// optimizer it can aggressively ignore unwind edges.4743void4744CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {4745if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&4746!CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)4747Inst->setMetadata("clang.arc.no_objc_arc_exceptions",4748CGM.getNoObjCARCExceptionsMetadata());4749}47504751/// Emits a call to the given no-arguments nounwind runtime function.4752llvm::CallInst *4753CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,4754const llvm::Twine &name) {4755return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value *>(), name);4756}47574758/// Emits a call to the given nounwind runtime function.4759llvm::CallInst *4760CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,4761ArrayRef<Address> args,4762const llvm::Twine &name) {4763SmallVector<llvm::Value *, 3> values;4764for (auto arg : args)4765values.push_back(arg.emitRawPointer(*this));4766return EmitNounwindRuntimeCall(callee, values, name);4767}47684769llvm::CallInst *4770CodeGenFunction::EmitNounwindRuntimeCall(llvm::FunctionCallee callee,4771ArrayRef<llvm::Value *> args,4772const llvm::Twine &name) {4773llvm::CallInst *call = EmitRuntimeCall(callee, args, name);4774call->setDoesNotThrow();4775return call;4776}47774778/// Emits a simple call (never an invoke) to the given no-arguments4779/// runtime function.4780llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,4781const llvm::Twine &name) {4782return EmitRuntimeCall(callee, std::nullopt, name);4783}47844785// Calls which may throw must have operand bundles indicating which funclet4786// they are nested within.4787SmallVector<llvm::OperandBundleDef, 1>4788CodeGenFunction::getBundlesForFunclet(llvm::Value *Callee) {4789// There is no need for a funclet operand bundle if we aren't inside a4790// funclet.4791if (!CurrentFuncletPad)4792return (SmallVector<llvm::OperandBundleDef, 1>());47934794// Skip intrinsics which cannot throw (as long as they don't lower into4795// regular function calls in the course of IR transformations).4796if (auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) {4797if (CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow()) {4798auto IID = CalleeFn->getIntrinsicID();4799if (!llvm::IntrinsicInst::mayLowerToFunctionCall(IID))4800return (SmallVector<llvm::OperandBundleDef, 1>());4801}4802}48034804SmallVector<llvm::OperandBundleDef, 1> BundleList;4805BundleList.emplace_back("funclet", CurrentFuncletPad);4806return BundleList;4807}48084809/// Emits a simple call (never an invoke) to the given runtime function.4810llvm::CallInst *CodeGenFunction::EmitRuntimeCall(llvm::FunctionCallee callee,4811ArrayRef<llvm::Value *> args,4812const llvm::Twine &name) {4813llvm::CallInst *call = Builder.CreateCall(4814callee, args, getBundlesForFunclet(callee.getCallee()), name);4815call->setCallingConv(getRuntimeCC());48164817if (CGM.shouldEmitConvergenceTokens() && call->isConvergent())4818return addControlledConvergenceToken(call);4819return call;4820}48214822/// Emits a call or invoke to the given noreturn runtime function.4823void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(4824llvm::FunctionCallee callee, ArrayRef<llvm::Value *> args) {4825SmallVector<llvm::OperandBundleDef, 1> BundleList =4826getBundlesForFunclet(callee.getCallee());48274828if (getInvokeDest()) {4829llvm::InvokeInst *invoke =4830Builder.CreateInvoke(callee,4831getUnreachableBlock(),4832getInvokeDest(),4833args,4834BundleList);4835invoke->setDoesNotReturn();4836invoke->setCallingConv(getRuntimeCC());4837} else {4838llvm::CallInst *call = Builder.CreateCall(callee, args, BundleList);4839call->setDoesNotReturn();4840call->setCallingConv(getRuntimeCC());4841Builder.CreateUnreachable();4842}4843}48444845/// Emits a call or invoke instruction to the given nullary runtime function.4846llvm::CallBase *4847CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,4848const Twine &name) {4849return EmitRuntimeCallOrInvoke(callee, std::nullopt, name);4850}48514852/// Emits a call or invoke instruction to the given runtime function.4853llvm::CallBase *4854CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee,4855ArrayRef<llvm::Value *> args,4856const Twine &name) {4857llvm::CallBase *call = EmitCallOrInvoke(callee, args, name);4858call->setCallingConv(getRuntimeCC());4859return call;4860}48614862/// Emits a call or invoke instruction to the given function, depending4863/// on the current state of the EH stack.4864llvm::CallBase *CodeGenFunction::EmitCallOrInvoke(llvm::FunctionCallee Callee,4865ArrayRef<llvm::Value *> Args,4866const Twine &Name) {4867llvm::BasicBlock *InvokeDest = getInvokeDest();4868SmallVector<llvm::OperandBundleDef, 1> BundleList =4869getBundlesForFunclet(Callee.getCallee());48704871llvm::CallBase *Inst;4872if (!InvokeDest)4873Inst = Builder.CreateCall(Callee, Args, BundleList, Name);4874else {4875llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");4876Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,4877Name);4878EmitBlock(ContBB);4879}48804881// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC4882// optimizer it can aggressively ignore unwind edges.4883if (CGM.getLangOpts().ObjCAutoRefCount)4884AddObjCARCExceptionMetadata(Inst);48854886return Inst;4887}48884889void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,4890llvm::Value *New) {4891DeferredReplacements.push_back(4892std::make_pair(llvm::WeakTrackingVH(Old), New));4893}48944895namespace {48964897/// Specify given \p NewAlign as the alignment of return value attribute. If4898/// such attribute already exists, re-set it to the maximal one of two options.4899[[nodiscard]] llvm::AttributeList4900maybeRaiseRetAlignmentAttribute(llvm::LLVMContext &Ctx,4901const llvm::AttributeList &Attrs,4902llvm::Align NewAlign) {4903llvm::Align CurAlign = Attrs.getRetAlignment().valueOrOne();4904if (CurAlign >= NewAlign)4905return Attrs;4906llvm::Attribute AlignAttr = llvm::Attribute::getWithAlignment(Ctx, NewAlign);4907return Attrs.removeRetAttribute(Ctx, llvm::Attribute::AttrKind::Alignment)4908.addRetAttribute(Ctx, AlignAttr);4909}49104911template <typename AlignedAttrTy> class AbstractAssumeAlignedAttrEmitter {4912protected:4913CodeGenFunction &CGF;49144915/// We do nothing if this is, or becomes, nullptr.4916const AlignedAttrTy *AA = nullptr;49174918llvm::Value *Alignment = nullptr; // May or may not be a constant.4919llvm::ConstantInt *OffsetCI = nullptr; // Constant, hopefully zero.49204921AbstractAssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)4922: CGF(CGF_) {4923if (!FuncDecl)4924return;4925AA = FuncDecl->getAttr<AlignedAttrTy>();4926}49274928public:4929/// If we can, materialize the alignment as an attribute on return value.4930[[nodiscard]] llvm::AttributeList4931TryEmitAsCallSiteAttribute(const llvm::AttributeList &Attrs) {4932if (!AA || OffsetCI || CGF.SanOpts.has(SanitizerKind::Alignment))4933return Attrs;4934const auto *AlignmentCI = dyn_cast<llvm::ConstantInt>(Alignment);4935if (!AlignmentCI)4936return Attrs;4937// We may legitimately have non-power-of-2 alignment here.4938// If so, this is UB land, emit it via `@llvm.assume` instead.4939if (!AlignmentCI->getValue().isPowerOf2())4940return Attrs;4941llvm::AttributeList NewAttrs = maybeRaiseRetAlignmentAttribute(4942CGF.getLLVMContext(), Attrs,4943llvm::Align(4944AlignmentCI->getLimitedValue(llvm::Value::MaximumAlignment)));4945AA = nullptr; // We're done. Disallow doing anything else.4946return NewAttrs;4947}49484949/// Emit alignment assumption.4950/// This is a general fallback that we take if either there is an offset,4951/// or the alignment is variable or we are sanitizing for alignment.4952void EmitAsAnAssumption(SourceLocation Loc, QualType RetTy, RValue &Ret) {4953if (!AA)4954return;4955CGF.emitAlignmentAssumption(Ret.getScalarVal(), RetTy, Loc,4956AA->getLocation(), Alignment, OffsetCI);4957AA = nullptr; // We're done. Disallow doing anything else.4958}4959};49604961/// Helper data structure to emit `AssumeAlignedAttr`.4962class AssumeAlignedAttrEmitter final4963: public AbstractAssumeAlignedAttrEmitter<AssumeAlignedAttr> {4964public:4965AssumeAlignedAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl)4966: AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {4967if (!AA)4968return;4969// It is guaranteed that the alignment/offset are constants.4970Alignment = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AA->getAlignment()));4971if (Expr *Offset = AA->getOffset()) {4972OffsetCI = cast<llvm::ConstantInt>(CGF.EmitScalarExpr(Offset));4973if (OffsetCI->isNullValue()) // Canonicalize zero offset to no offset.4974OffsetCI = nullptr;4975}4976}4977};49784979/// Helper data structure to emit `AllocAlignAttr`.4980class AllocAlignAttrEmitter final4981: public AbstractAssumeAlignedAttrEmitter<AllocAlignAttr> {4982public:4983AllocAlignAttrEmitter(CodeGenFunction &CGF_, const Decl *FuncDecl,4984const CallArgList &CallArgs)4985: AbstractAssumeAlignedAttrEmitter(CGF_, FuncDecl) {4986if (!AA)4987return;4988// Alignment may or may not be a constant, and that is okay.4989Alignment = CallArgs[AA->getParamIndex().getLLVMIndex()]4990.getRValue(CGF)4991.getScalarVal();4992}4993};49944995} // namespace49964997static unsigned getMaxVectorWidth(const llvm::Type *Ty) {4998if (auto *VT = dyn_cast<llvm::VectorType>(Ty))4999return VT->getPrimitiveSizeInBits().getKnownMinValue();5000if (auto *AT = dyn_cast<llvm::ArrayType>(Ty))5001return getMaxVectorWidth(AT->getElementType());50025003unsigned MaxVectorWidth = 0;5004if (auto *ST = dyn_cast<llvm::StructType>(Ty))5005for (auto *I : ST->elements())5006MaxVectorWidth = std::max(MaxVectorWidth, getMaxVectorWidth(I));5007return MaxVectorWidth;5008}50095010RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,5011const CGCallee &Callee,5012ReturnValueSlot ReturnValue,5013const CallArgList &CallArgs,5014llvm::CallBase **callOrInvoke, bool IsMustTail,5015SourceLocation Loc,5016bool IsVirtualFunctionPointerThunk) {5017// FIXME: We no longer need the types from CallArgs; lift up and simplify.50185019assert(Callee.isOrdinary() || Callee.isVirtual());50205021// Handle struct-return functions by passing a pointer to the5022// location that we would like to return into.5023QualType RetTy = CallInfo.getReturnType();5024const ABIArgInfo &RetAI = CallInfo.getReturnInfo();50255026llvm::FunctionType *IRFuncTy = getTypes().GetFunctionType(CallInfo);50275028const Decl *TargetDecl = Callee.getAbstractInfo().getCalleeDecl().getDecl();5029if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {5030// We can only guarantee that a function is called from the correct5031// context/function based on the appropriate target attributes,5032// so only check in the case where we have both always_inline and target5033// since otherwise we could be making a conditional call after a check for5034// the proper cpu features (and it won't cause code generation issues due to5035// function based code generation).5036if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&5037(TargetDecl->hasAttr<TargetAttr>() ||5038(CurFuncDecl && CurFuncDecl->hasAttr<TargetAttr>())))5039checkTargetFeatures(Loc, FD);5040}50415042// Some architectures (such as x86-64) have the ABI changed based on5043// attribute-target/features. Give them a chance to diagnose.5044CGM.getTargetCodeGenInfo().checkFunctionCallABI(5045CGM, Loc, dyn_cast_or_null<FunctionDecl>(CurCodeDecl),5046dyn_cast_or_null<FunctionDecl>(TargetDecl), CallArgs, RetTy);50475048// 1. Set up the arguments.50495050// If we're using inalloca, insert the allocation after the stack save.5051// FIXME: Do this earlier rather than hacking it in here!5052RawAddress ArgMemory = RawAddress::invalid();5053if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) {5054const llvm::DataLayout &DL = CGM.getDataLayout();5055llvm::Instruction *IP = CallArgs.getStackBase();5056llvm::AllocaInst *AI;5057if (IP) {5058IP = IP->getNextNode();5059AI = new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),5060"argmem", IP);5061} else {5062AI = CreateTempAlloca(ArgStruct, "argmem");5063}5064auto Align = CallInfo.getArgStructAlignment();5065AI->setAlignment(Align.getAsAlign());5066AI->setUsedWithInAlloca(true);5067assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());5068ArgMemory = RawAddress(AI, ArgStruct, Align);5069}50705071ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);5072SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs());50735074// If the call returns a temporary with struct return, create a temporary5075// alloca to hold the result, unless one is given to us.5076Address SRetPtr = Address::invalid();5077RawAddress SRetAlloca = RawAddress::invalid();5078llvm::Value *UnusedReturnSizePtr = nullptr;5079if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {5080if (IsVirtualFunctionPointerThunk && RetAI.isIndirect()) {5081SRetPtr = makeNaturalAddressForPointer(CurFn->arg_begin() +5082IRFunctionArgs.getSRetArgNo(),5083RetTy, CharUnits::fromQuantity(1));5084} else if (!ReturnValue.isNull()) {5085SRetPtr = ReturnValue.getAddress();5086} else {5087SRetPtr = CreateMemTemp(RetTy, "tmp", &SRetAlloca);5088if (HaveInsertPoint() && ReturnValue.isUnused()) {5089llvm::TypeSize size =5090CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));5091UnusedReturnSizePtr = EmitLifetimeStart(size, SRetAlloca.getPointer());5092}5093}5094if (IRFunctionArgs.hasSRetArg()) {5095IRCallArgs[IRFunctionArgs.getSRetArgNo()] =5096getAsNaturalPointerTo(SRetPtr, RetTy);5097} else if (RetAI.isInAlloca()) {5098Address Addr =5099Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex());5100Builder.CreateStore(getAsNaturalPointerTo(SRetPtr, RetTy), Addr);5101}5102}51035104RawAddress swiftErrorTemp = RawAddress::invalid();5105Address swiftErrorArg = Address::invalid();51065107// When passing arguments using temporary allocas, we need to add the5108// appropriate lifetime markers. This vector keeps track of all the lifetime5109// markers that need to be ended right after the call.5110SmallVector<CallLifetimeEnd, 2> CallLifetimeEndAfterCall;51115112// Translate all of the arguments as necessary to match the IR lowering.5113assert(CallInfo.arg_size() == CallArgs.size() &&5114"Mismatch between function signature & arguments.");5115unsigned ArgNo = 0;5116CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();5117for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();5118I != E; ++I, ++info_it, ++ArgNo) {5119const ABIArgInfo &ArgInfo = info_it->info;51205121// Insert a padding argument to ensure proper alignment.5122if (IRFunctionArgs.hasPaddingArg(ArgNo))5123IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =5124llvm::UndefValue::get(ArgInfo.getPaddingType());51255126unsigned FirstIRArg, NumIRArgs;5127std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);51285129bool ArgHasMaybeUndefAttr =5130IsArgumentMaybeUndef(TargetDecl, CallInfo.getNumRequiredArgs(), ArgNo);51315132switch (ArgInfo.getKind()) {5133case ABIArgInfo::InAlloca: {5134assert(NumIRArgs == 0);5135assert(getTarget().getTriple().getArch() == llvm::Triple::x86);5136if (I->isAggregate()) {5137RawAddress Addr = I->hasLValue()5138? I->getKnownLValue().getAddress()5139: I->getKnownRValue().getAggregateAddress();5140llvm::Instruction *Placeholder =5141cast<llvm::Instruction>(Addr.getPointer());51425143if (!ArgInfo.getInAllocaIndirect()) {5144// Replace the placeholder with the appropriate argument slot GEP.5145CGBuilderTy::InsertPoint IP = Builder.saveIP();5146Builder.SetInsertPoint(Placeholder);5147Addr = Builder.CreateStructGEP(ArgMemory,5148ArgInfo.getInAllocaFieldIndex());5149Builder.restoreIP(IP);5150} else {5151// For indirect things such as overaligned structs, replace the5152// placeholder with a regular aggregate temporary alloca. Store the5153// address of this alloca into the struct.5154Addr = CreateMemTemp(info_it->type, "inalloca.indirect.tmp");5155Address ArgSlot = Builder.CreateStructGEP(5156ArgMemory, ArgInfo.getInAllocaFieldIndex());5157Builder.CreateStore(Addr.getPointer(), ArgSlot);5158}5159deferPlaceholderReplacement(Placeholder, Addr.getPointer());5160} else if (ArgInfo.getInAllocaIndirect()) {5161// Make a temporary alloca and store the address of it into the argument5162// struct.5163RawAddress Addr = CreateMemTempWithoutCast(5164I->Ty, getContext().getTypeAlignInChars(I->Ty),5165"indirect-arg-temp");5166I->copyInto(*this, Addr);5167Address ArgSlot =5168Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());5169Builder.CreateStore(Addr.getPointer(), ArgSlot);5170} else {5171// Store the RValue into the argument struct.5172Address Addr =5173Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex());5174Addr = Addr.withElementType(ConvertTypeForMem(I->Ty));5175I->copyInto(*this, Addr);5176}5177break;5178}51795180case ABIArgInfo::Indirect:5181case ABIArgInfo::IndirectAliased: {5182assert(NumIRArgs == 1);5183if (I->isAggregate()) {5184// We want to avoid creating an unnecessary temporary+copy here;5185// however, we need one in three cases:5186// 1. If the argument is not byval, and we are required to copy the5187// source. (This case doesn't occur on any common architecture.)5188// 2. If the argument is byval, RV is not sufficiently aligned, and5189// we cannot force it to be sufficiently aligned.5190// 3. If the argument is byval, but RV is not located in default5191// or alloca address space.5192Address Addr = I->hasLValue()5193? I->getKnownLValue().getAddress()5194: I->getKnownRValue().getAggregateAddress();5195CharUnits Align = ArgInfo.getIndirectAlign();5196const llvm::DataLayout *TD = &CGM.getDataLayout();51975198assert((FirstIRArg >= IRFuncTy->getNumParams() ||5199IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() ==5200TD->getAllocaAddrSpace()) &&5201"indirect argument must be in alloca address space");52025203bool NeedCopy = false;5204if (Addr.getAlignment() < Align &&5205llvm::getOrEnforceKnownAlignment(Addr.emitRawPointer(*this),5206Align.getAsAlign(),5207*TD) < Align.getAsAlign()) {5208NeedCopy = true;5209} else if (I->hasLValue()) {5210auto LV = I->getKnownLValue();5211auto AS = LV.getAddressSpace();52125213bool isByValOrRef =5214ArgInfo.isIndirectAliased() || ArgInfo.getIndirectByVal();52155216if (!isByValOrRef ||5217(LV.getAlignment() < getContext().getTypeAlignInChars(I->Ty))) {5218NeedCopy = true;5219}5220if (!getLangOpts().OpenCL) {5221if ((isByValOrRef &&5222(AS != LangAS::Default &&5223AS != CGM.getASTAllocaAddressSpace()))) {5224NeedCopy = true;5225}5226}5227// For OpenCL even if RV is located in default or alloca address space5228// we don't want to perform address space cast for it.5229else if ((isByValOrRef &&5230Addr.getType()->getAddressSpace() != IRFuncTy->5231getParamType(FirstIRArg)->getPointerAddressSpace())) {5232NeedCopy = true;5233}5234}52355236if (!NeedCopy) {5237// Skip the extra memcpy call.5238llvm::Value *V = getAsNaturalPointerTo(Addr, I->Ty);5239auto *T = llvm::PointerType::get(5240CGM.getLLVMContext(), CGM.getDataLayout().getAllocaAddrSpace());52415242llvm::Value *Val = getTargetHooks().performAddrSpaceCast(5243*this, V, LangAS::Default, CGM.getASTAllocaAddressSpace(), T,5244true);5245if (ArgHasMaybeUndefAttr)5246Val = Builder.CreateFreeze(Val);5247IRCallArgs[FirstIRArg] = Val;5248break;5249}5250}52515252// For non-aggregate args and aggregate args meeting conditions above5253// we need to create an aligned temporary, and copy to it.5254RawAddress AI = CreateMemTempWithoutCast(5255I->Ty, ArgInfo.getIndirectAlign(), "byval-temp");5256llvm::Value *Val = getAsNaturalPointerTo(AI, I->Ty);5257if (ArgHasMaybeUndefAttr)5258Val = Builder.CreateFreeze(Val);5259IRCallArgs[FirstIRArg] = Val;52605261// Emit lifetime markers for the temporary alloca.5262llvm::TypeSize ByvalTempElementSize =5263CGM.getDataLayout().getTypeAllocSize(AI.getElementType());5264llvm::Value *LifetimeSize =5265EmitLifetimeStart(ByvalTempElementSize, AI.getPointer());52665267// Add cleanup code to emit the end lifetime marker after the call.5268if (LifetimeSize) // In case we disabled lifetime markers.5269CallLifetimeEndAfterCall.emplace_back(AI, LifetimeSize);52705271// Generate the copy.5272I->copyInto(*this, AI);5273break;5274}52755276case ABIArgInfo::Ignore:5277assert(NumIRArgs == 0);5278break;52795280case ABIArgInfo::Extend:5281case ABIArgInfo::Direct: {5282if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&5283ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&5284ArgInfo.getDirectOffset() == 0) {5285assert(NumIRArgs == 1);5286llvm::Value *V;5287if (!I->isAggregate())5288V = I->getKnownRValue().getScalarVal();5289else5290V = Builder.CreateLoad(5291I->hasLValue() ? I->getKnownLValue().getAddress()5292: I->getKnownRValue().getAggregateAddress());52935294// Implement swifterror by copying into a new swifterror argument.5295// We'll write back in the normal path out of the call.5296if (CallInfo.getExtParameterInfo(ArgNo).getABI()5297== ParameterABI::SwiftErrorResult) {5298assert(!swiftErrorTemp.isValid() && "multiple swifterror args");52995300QualType pointeeTy = I->Ty->getPointeeType();5301swiftErrorArg = makeNaturalAddressForPointer(5302V, pointeeTy, getContext().getTypeAlignInChars(pointeeTy));53035304swiftErrorTemp =5305CreateMemTemp(pointeeTy, getPointerAlign(), "swifterror.temp");5306V = swiftErrorTemp.getPointer();5307cast<llvm::AllocaInst>(V)->setSwiftError(true);53085309llvm::Value *errorValue = Builder.CreateLoad(swiftErrorArg);5310Builder.CreateStore(errorValue, swiftErrorTemp);5311}53125313// We might have to widen integers, but we should never truncate.5314if (ArgInfo.getCoerceToType() != V->getType() &&5315V->getType()->isIntegerTy())5316V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());53175318// If the argument doesn't match, perform a bitcast to coerce it. This5319// can happen due to trivial type mismatches.5320if (FirstIRArg < IRFuncTy->getNumParams() &&5321V->getType() != IRFuncTy->getParamType(FirstIRArg))5322V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));53235324if (ArgHasMaybeUndefAttr)5325V = Builder.CreateFreeze(V);5326IRCallArgs[FirstIRArg] = V;5327break;5328}53295330llvm::StructType *STy =5331dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());5332if (STy && ArgInfo.isDirect() && !ArgInfo.getCanBeFlattened()) {5333llvm::Type *SrcTy = ConvertTypeForMem(I->Ty);5334[[maybe_unused]] llvm::TypeSize SrcTypeSize =5335CGM.getDataLayout().getTypeAllocSize(SrcTy);5336[[maybe_unused]] llvm::TypeSize DstTypeSize =5337CGM.getDataLayout().getTypeAllocSize(STy);5338if (STy->containsHomogeneousScalableVectorTypes()) {5339assert(SrcTypeSize == DstTypeSize &&5340"Only allow non-fractional movement of structure with "5341"homogeneous scalable vector type");53425343IRCallArgs[FirstIRArg] = I->getKnownRValue().getScalarVal();5344break;5345}5346}53475348// FIXME: Avoid the conversion through memory if possible.5349Address Src = Address::invalid();5350if (!I->isAggregate()) {5351Src = CreateMemTemp(I->Ty, "coerce");5352I->copyInto(*this, Src);5353} else {5354Src = I->hasLValue() ? I->getKnownLValue().getAddress()5355: I->getKnownRValue().getAggregateAddress();5356}53575358// If the value is offset in memory, apply the offset now.5359Src = emitAddressAtOffset(*this, Src, ArgInfo);53605361// Fast-isel and the optimizer generally like scalar values better than5362// FCAs, so we flatten them if this is safe to do for this argument.5363if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {5364llvm::Type *SrcTy = Src.getElementType();5365llvm::TypeSize SrcTypeSize =5366CGM.getDataLayout().getTypeAllocSize(SrcTy);5367llvm::TypeSize DstTypeSize = CGM.getDataLayout().getTypeAllocSize(STy);5368if (SrcTypeSize.isScalable()) {5369assert(STy->containsHomogeneousScalableVectorTypes() &&5370"ABI only supports structure with homogeneous scalable vector "5371"type");5372assert(SrcTypeSize == DstTypeSize &&5373"Only allow non-fractional movement of structure with "5374"homogeneous scalable vector type");5375assert(NumIRArgs == STy->getNumElements());53765377llvm::Value *StoredStructValue =5378Builder.CreateLoad(Src, Src.getName() + ".tuple");5379for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {5380llvm::Value *Extract = Builder.CreateExtractValue(5381StoredStructValue, i, Src.getName() + ".extract" + Twine(i));5382IRCallArgs[FirstIRArg + i] = Extract;5383}5384} else {5385uint64_t SrcSize = SrcTypeSize.getFixedValue();5386uint64_t DstSize = DstTypeSize.getFixedValue();53875388// If the source type is smaller than the destination type of the5389// coerce-to logic, copy the source value into a temp alloca the size5390// of the destination type to allow loading all of it. The bits past5391// the source value are left undef.5392if (SrcSize < DstSize) {5393Address TempAlloca = CreateTempAlloca(STy, Src.getAlignment(),5394Src.getName() + ".coerce");5395Builder.CreateMemCpy(TempAlloca, Src, SrcSize);5396Src = TempAlloca;5397} else {5398Src = Src.withElementType(STy);5399}54005401assert(NumIRArgs == STy->getNumElements());5402for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {5403Address EltPtr = Builder.CreateStructGEP(Src, i);5404llvm::Value *LI = Builder.CreateLoad(EltPtr);5405if (ArgHasMaybeUndefAttr)5406LI = Builder.CreateFreeze(LI);5407IRCallArgs[FirstIRArg + i] = LI;5408}5409}5410} else {5411// In the simple case, just pass the coerced loaded value.5412assert(NumIRArgs == 1);5413llvm::Value *Load =5414CreateCoercedLoad(Src, ArgInfo.getCoerceToType(), *this);54155416if (CallInfo.isCmseNSCall()) {5417// For certain parameter types, clear padding bits, as they may reveal5418// sensitive information.5419// Small struct/union types are passed as integer arrays.5420auto *ATy = dyn_cast<llvm::ArrayType>(Load->getType());5421if (ATy != nullptr && isa<RecordType>(I->Ty.getCanonicalType()))5422Load = EmitCMSEClearRecord(Load, ATy, I->Ty);5423}54245425if (ArgHasMaybeUndefAttr)5426Load = Builder.CreateFreeze(Load);5427IRCallArgs[FirstIRArg] = Load;5428}54295430break;5431}54325433case ABIArgInfo::CoerceAndExpand: {5434auto coercionType = ArgInfo.getCoerceAndExpandType();5435auto layout = CGM.getDataLayout().getStructLayout(coercionType);54365437llvm::Value *tempSize = nullptr;5438Address addr = Address::invalid();5439RawAddress AllocaAddr = RawAddress::invalid();5440if (I->isAggregate()) {5441addr = I->hasLValue() ? I->getKnownLValue().getAddress()5442: I->getKnownRValue().getAggregateAddress();54435444} else {5445RValue RV = I->getKnownRValue();5446assert(RV.isScalar()); // complex should always just be direct54475448llvm::Type *scalarType = RV.getScalarVal()->getType();5449auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);5450auto scalarAlign = CGM.getDataLayout().getPrefTypeAlign(scalarType);54515452// Materialize to a temporary.5453addr = CreateTempAlloca(5454RV.getScalarVal()->getType(),5455CharUnits::fromQuantity(std::max(layout->getAlignment(), scalarAlign)),5456"tmp",5457/*ArraySize=*/nullptr, &AllocaAddr);5458tempSize = EmitLifetimeStart(scalarSize, AllocaAddr.getPointer());54595460Builder.CreateStore(RV.getScalarVal(), addr);5461}54625463addr = addr.withElementType(coercionType);54645465unsigned IRArgPos = FirstIRArg;5466for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {5467llvm::Type *eltType = coercionType->getElementType(i);5468if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType)) continue;5469Address eltAddr = Builder.CreateStructGEP(addr, i);5470llvm::Value *elt = Builder.CreateLoad(eltAddr);5471if (ArgHasMaybeUndefAttr)5472elt = Builder.CreateFreeze(elt);5473IRCallArgs[IRArgPos++] = elt;5474}5475assert(IRArgPos == FirstIRArg + NumIRArgs);54765477if (tempSize) {5478EmitLifetimeEnd(tempSize, AllocaAddr.getPointer());5479}54805481break;5482}54835484case ABIArgInfo::Expand: {5485unsigned IRArgPos = FirstIRArg;5486ExpandTypeToArgs(I->Ty, *I, IRFuncTy, IRCallArgs, IRArgPos);5487assert(IRArgPos == FirstIRArg + NumIRArgs);5488break;5489}5490}5491}54925493const CGCallee &ConcreteCallee = Callee.prepareConcreteCallee(*this);5494llvm::Value *CalleePtr = ConcreteCallee.getFunctionPointer();54955496// If we're using inalloca, set up that argument.5497if (ArgMemory.isValid()) {5498llvm::Value *Arg = ArgMemory.getPointer();5499assert(IRFunctionArgs.hasInallocaArg());5500IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;5501}55025503// 2. Prepare the function pointer.55045505// If the callee is a bitcast of a non-variadic function to have a5506// variadic function pointer type, check to see if we can remove the5507// bitcast. This comes up with unprototyped functions.5508//5509// This makes the IR nicer, but more importantly it ensures that we5510// can inline the function at -O0 if it is marked always_inline.5511auto simplifyVariadicCallee = [](llvm::FunctionType *CalleeFT,5512llvm::Value *Ptr) -> llvm::Function * {5513if (!CalleeFT->isVarArg())5514return nullptr;55155516// Get underlying value if it's a bitcast5517if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr)) {5518if (CE->getOpcode() == llvm::Instruction::BitCast)5519Ptr = CE->getOperand(0);5520}55215522llvm::Function *OrigFn = dyn_cast<llvm::Function>(Ptr);5523if (!OrigFn)5524return nullptr;55255526llvm::FunctionType *OrigFT = OrigFn->getFunctionType();55275528// If the original type is variadic, or if any of the component types5529// disagree, we cannot remove the cast.5530if (OrigFT->isVarArg() ||5531OrigFT->getNumParams() != CalleeFT->getNumParams() ||5532OrigFT->getReturnType() != CalleeFT->getReturnType())5533return nullptr;55345535for (unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)5536if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))5537return nullptr;55385539return OrigFn;5540};55415542if (llvm::Function *OrigFn = simplifyVariadicCallee(IRFuncTy, CalleePtr)) {5543CalleePtr = OrigFn;5544IRFuncTy = OrigFn->getFunctionType();5545}55465547// 3. Perform the actual call.55485549// Deactivate any cleanups that we're supposed to do immediately before5550// the call.5551if (!CallArgs.getCleanupsToDeactivate().empty())5552deactivateArgCleanupsBeforeCall(*this, CallArgs);55535554// Assert that the arguments we computed match up. The IR verifier5555// will catch this, but this is a common enough source of problems5556// during IRGen changes that it's way better for debugging to catch5557// it ourselves here.5558#ifndef NDEBUG5559assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());5560for (unsigned i = 0; i < IRCallArgs.size(); ++i) {5561// Inalloca argument can have different type.5562if (IRFunctionArgs.hasInallocaArg() &&5563i == IRFunctionArgs.getInallocaArgNo())5564continue;5565if (i < IRFuncTy->getNumParams())5566assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));5567}5568#endif55695570// Update the largest vector width if any arguments have vector types.5571for (unsigned i = 0; i < IRCallArgs.size(); ++i)5572LargestVectorWidth = std::max(LargestVectorWidth,5573getMaxVectorWidth(IRCallArgs[i]->getType()));55745575// Compute the calling convention and attributes.5576unsigned CallingConv;5577llvm::AttributeList Attrs;5578CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,5579Callee.getAbstractInfo(), Attrs, CallingConv,5580/*AttrOnCallSite=*/true,5581/*IsThunk=*/false);55825583if (CallingConv == llvm::CallingConv::X86_VectorCall &&5584getTarget().getTriple().isWindowsArm64EC()) {5585CGM.Error(Loc, "__vectorcall calling convention is not currently "5586"supported");5587}55885589if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {5590if (FD->hasAttr<StrictFPAttr>())5591// All calls within a strictfp function are marked strictfp5592Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);55935594// If -ffast-math is enabled and the function is guarded by an5595// '__attribute__((optnone)) adjust the memory attribute so the BE emits the5596// library call instead of the intrinsic.5597if (FD->hasAttr<OptimizeNoneAttr>() && getLangOpts().FastMath)5598CGM.AdjustMemoryAttribute(CalleePtr->getName(), Callee.getAbstractInfo(),5599Attrs);5600}5601// Add call-site nomerge attribute if exists.5602if (InNoMergeAttributedStmt)5603Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoMerge);56045605// Add call-site noinline attribute if exists.5606if (InNoInlineAttributedStmt)5607Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);56085609// Add call-site always_inline attribute if exists.5610if (InAlwaysInlineAttributedStmt)5611Attrs =5612Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);56135614// Apply some call-site-specific attributes.5615// TODO: work this into building the attribute set.56165617// Apply always_inline to all calls within flatten functions.5618// FIXME: should this really take priority over __try, below?5619if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&5620!InNoInlineAttributedStmt &&5621!(TargetDecl && TargetDecl->hasAttr<NoInlineAttr>())) {5622Attrs =5623Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::AlwaysInline);5624}56255626// Disable inlining inside SEH __try blocks.5627if (isSEHTryScope()) {5628Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::NoInline);5629}56305631// Decide whether to use a call or an invoke.5632bool CannotThrow;5633if (currentFunctionUsesSEHTry()) {5634// SEH cares about asynchronous exceptions, so everything can "throw."5635CannotThrow = false;5636} else if (isCleanupPadScope() &&5637EHPersonality::get(*this).isMSVCXXPersonality()) {5638// The MSVC++ personality will implicitly terminate the program if an5639// exception is thrown during a cleanup outside of a try/catch.5640// We don't need to model anything in IR to get this behavior.5641CannotThrow = true;5642} else {5643// Otherwise, nounwind call sites will never throw.5644CannotThrow = Attrs.hasFnAttr(llvm::Attribute::NoUnwind);56455646if (auto *FPtr = dyn_cast<llvm::Function>(CalleePtr))5647if (FPtr->hasFnAttribute(llvm::Attribute::NoUnwind))5648CannotThrow = true;5649}56505651// If we made a temporary, be sure to clean up after ourselves. Note that we5652// can't depend on being inside of an ExprWithCleanups, so we need to manually5653// pop this cleanup later on. Being eager about this is OK, since this5654// temporary is 'invisible' outside of the callee.5655if (UnusedReturnSizePtr)5656pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,5657UnusedReturnSizePtr);56585659llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();56605661SmallVector<llvm::OperandBundleDef, 1> BundleList =5662getBundlesForFunclet(CalleePtr);56635664if (SanOpts.has(SanitizerKind::KCFI) &&5665!isa_and_nonnull<FunctionDecl>(TargetDecl))5666EmitKCFIOperandBundle(ConcreteCallee, BundleList);56675668// Add the pointer-authentication bundle.5669EmitPointerAuthOperandBundle(ConcreteCallee.getPointerAuthInfo(), BundleList);56705671if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))5672if (FD->hasAttr<StrictFPAttr>())5673// All calls within a strictfp function are marked strictfp5674Attrs = Attrs.addFnAttribute(getLLVMContext(), llvm::Attribute::StrictFP);56755676AssumeAlignedAttrEmitter AssumeAlignedAttrEmitter(*this, TargetDecl);5677Attrs = AssumeAlignedAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);56785679AllocAlignAttrEmitter AllocAlignAttrEmitter(*this, TargetDecl, CallArgs);5680Attrs = AllocAlignAttrEmitter.TryEmitAsCallSiteAttribute(Attrs);56815682// Emit the actual call/invoke instruction.5683llvm::CallBase *CI;5684if (!InvokeDest) {5685CI = Builder.CreateCall(IRFuncTy, CalleePtr, IRCallArgs, BundleList);5686} else {5687llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");5688CI = Builder.CreateInvoke(IRFuncTy, CalleePtr, Cont, InvokeDest, IRCallArgs,5689BundleList);5690EmitBlock(Cont);5691}5692if (CI->getCalledFunction() && CI->getCalledFunction()->hasName() &&5693CI->getCalledFunction()->getName().starts_with("_Z4sqrt")) {5694SetSqrtFPAccuracy(CI);5695}5696if (callOrInvoke)5697*callOrInvoke = CI;56985699// If this is within a function that has the guard(nocf) attribute and is an5700// indirect call, add the "guard_nocf" attribute to this call to indicate that5701// Control Flow Guard checks should not be added, even if the call is inlined.5702if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {5703if (const auto *A = FD->getAttr<CFGuardAttr>()) {5704if (A->getGuard() == CFGuardAttr::GuardArg::nocf && !CI->getCalledFunction())5705Attrs = Attrs.addFnAttribute(getLLVMContext(), "guard_nocf");5706}5707}57085709// Apply the attributes and calling convention.5710CI->setAttributes(Attrs);5711CI->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));57125713// Apply various metadata.57145715if (!CI->getType()->isVoidTy())5716CI->setName("call");57175718if (CGM.shouldEmitConvergenceTokens() && CI->isConvergent())5719CI = addControlledConvergenceToken(CI);57205721// Update largest vector width from the return type.5722LargestVectorWidth =5723std::max(LargestVectorWidth, getMaxVectorWidth(CI->getType()));57245725// Insert instrumentation or attach profile metadata at indirect call sites.5726// For more details, see the comment before the definition of5727// IPVK_IndirectCallTarget in InstrProfData.inc.5728if (!CI->getCalledFunction())5729PGO.valueProfile(Builder, llvm::IPVK_IndirectCallTarget,5730CI, CalleePtr);57315732// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC5733// optimizer it can aggressively ignore unwind edges.5734if (CGM.getLangOpts().ObjCAutoRefCount)5735AddObjCARCExceptionMetadata(CI);57365737// Set tail call kind if necessary.5738if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {5739if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())5740Call->setTailCallKind(llvm::CallInst::TCK_NoTail);5741else if (IsMustTail) {5742if (getTarget().getTriple().isPPC()) {5743if (getTarget().getTriple().isOSAIX())5744CGM.getDiags().Report(Loc, diag::err_aix_musttail_unsupported);5745else if (!getTarget().hasFeature("pcrelative-memops")) {5746if (getTarget().hasFeature("longcall"))5747CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 0;5748else if (Call->isIndirectCall())5749CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 1;5750else if (isa_and_nonnull<FunctionDecl>(TargetDecl)) {5751if (!cast<FunctionDecl>(TargetDecl)->isDefined())5752// The undefined callee may be a forward declaration. Without5753// knowning all symbols in the module, we won't know the symbol is5754// defined or not. Collect all these symbols for later diagnosing.5755CGM.addUndefinedGlobalForTailCall(5756{cast<FunctionDecl>(TargetDecl), Loc});5757else {5758llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(5759GlobalDecl(cast<FunctionDecl>(TargetDecl)));5760if (llvm::GlobalValue::isWeakForLinker(Linkage) ||5761llvm::GlobalValue::isDiscardableIfUnused(Linkage))5762CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail)5763<< 2;5764}5765}5766}5767}5768Call->setTailCallKind(llvm::CallInst::TCK_MustTail);5769}5770}57715772// Add metadata for calls to MSAllocator functions5773if (getDebugInfo() && TargetDecl &&5774TargetDecl->hasAttr<MSAllocatorAttr>())5775getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy->getPointeeType(), Loc);57765777// Add metadata if calling an __attribute__((error(""))) or warning fn.5778if (TargetDecl && TargetDecl->hasAttr<ErrorAttr>()) {5779llvm::ConstantInt *Line =5780llvm::ConstantInt::get(Int64Ty, Loc.getRawEncoding());5781llvm::ConstantAsMetadata *MD = llvm::ConstantAsMetadata::get(Line);5782llvm::MDTuple *MDT = llvm::MDNode::get(getLLVMContext(), {MD});5783CI->setMetadata("srcloc", MDT);5784}57855786// 4. Finish the call.57875788// If the call doesn't return, finish the basic block and clear the5789// insertion point; this allows the rest of IRGen to discard5790// unreachable code.5791if (CI->doesNotReturn()) {5792if (UnusedReturnSizePtr)5793PopCleanupBlock();57945795// Strip away the noreturn attribute to better diagnose unreachable UB.5796if (SanOpts.has(SanitizerKind::Unreachable)) {5797// Also remove from function since CallBase::hasFnAttr additionally checks5798// attributes of the called function.5799if (auto *F = CI->getCalledFunction())5800F->removeFnAttr(llvm::Attribute::NoReturn);5801CI->removeFnAttr(llvm::Attribute::NoReturn);58025803// Avoid incompatibility with ASan which relies on the `noreturn`5804// attribute to insert handler calls.5805if (SanOpts.hasOneOf(SanitizerKind::Address |5806SanitizerKind::KernelAddress)) {5807SanitizerScope SanScope(this);5808llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);5809Builder.SetInsertPoint(CI);5810auto *FnType = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);5811llvm::FunctionCallee Fn =5812CGM.CreateRuntimeFunction(FnType, "__asan_handle_no_return");5813EmitNounwindRuntimeCall(Fn);5814}5815}58165817EmitUnreachable(Loc);5818Builder.ClearInsertionPoint();58195820// FIXME: For now, emit a dummy basic block because expr emitters in5821// generally are not ready to handle emitting expressions at unreachable5822// points.5823EnsureInsertPoint();58245825// Return a reasonable RValue.5826return GetUndefRValue(RetTy);5827}58285829// If this is a musttail call, return immediately. We do not branch to the5830// epilogue in this case.5831if (IsMustTail) {5832for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();5833++it) {5834EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);5835if (!(Cleanup && Cleanup->getCleanup()->isRedundantBeforeReturn()))5836CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");5837}5838if (CI->getType()->isVoidTy())5839Builder.CreateRetVoid();5840else5841Builder.CreateRet(CI);5842Builder.ClearInsertionPoint();5843EnsureInsertPoint();5844return GetUndefRValue(RetTy);5845}58465847// Perform the swifterror writeback.5848if (swiftErrorTemp.isValid()) {5849llvm::Value *errorResult = Builder.CreateLoad(swiftErrorTemp);5850Builder.CreateStore(errorResult, swiftErrorArg);5851}58525853// Emit any call-associated writebacks immediately. Arguably this5854// should happen after any return-value munging.5855if (CallArgs.hasWritebacks())5856emitWritebacks(*this, CallArgs);58575858// The stack cleanup for inalloca arguments has to run out of the normal5859// lexical order, so deactivate it and run it manually here.5860CallArgs.freeArgumentMemory(*this);58615862// Extract the return value.5863RValue Ret;58645865// If the current function is a virtual function pointer thunk, avoid copying5866// the return value of the musttail call to a temporary.5867if (IsVirtualFunctionPointerThunk) {5868Ret = RValue::get(CI);5869} else {5870Ret = [&] {5871switch (RetAI.getKind()) {5872case ABIArgInfo::CoerceAndExpand: {5873auto coercionType = RetAI.getCoerceAndExpandType();58745875Address addr = SRetPtr.withElementType(coercionType);58765877assert(CI->getType() == RetAI.getUnpaddedCoerceAndExpandType());5878bool requiresExtract = isa<llvm::StructType>(CI->getType());58795880unsigned unpaddedIndex = 0;5881for (unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {5882llvm::Type *eltType = coercionType->getElementType(i);5883if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))5884continue;5885Address eltAddr = Builder.CreateStructGEP(addr, i);5886llvm::Value *elt = CI;5887if (requiresExtract)5888elt = Builder.CreateExtractValue(elt, unpaddedIndex++);5889else5890assert(unpaddedIndex == 0);5891Builder.CreateStore(elt, eltAddr);5892}5893[[fallthrough]];5894}58955896case ABIArgInfo::InAlloca:5897case ABIArgInfo::Indirect: {5898RValue ret = convertTempToRValue(SRetPtr, RetTy, SourceLocation());5899if (UnusedReturnSizePtr)5900PopCleanupBlock();5901return ret;5902}59035904case ABIArgInfo::Ignore:5905// If we are ignoring an argument that had a result, make sure to5906// construct the appropriate return value for our caller.5907return GetUndefRValue(RetTy);59085909case ABIArgInfo::Extend:5910case ABIArgInfo::Direct: {5911llvm::Type *RetIRTy = ConvertType(RetTy);5912if (RetAI.getCoerceToType() == RetIRTy &&5913RetAI.getDirectOffset() == 0) {5914switch (getEvaluationKind(RetTy)) {5915case TEK_Complex: {5916llvm::Value *Real = Builder.CreateExtractValue(CI, 0);5917llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);5918return RValue::getComplex(std::make_pair(Real, Imag));5919}5920case TEK_Aggregate:5921break;5922case TEK_Scalar: {5923// If the argument doesn't match, perform a bitcast to coerce it.5924// This can happen due to trivial type mismatches.5925llvm::Value *V = CI;5926if (V->getType() != RetIRTy)5927V = Builder.CreateBitCast(V, RetIRTy);5928return RValue::get(V);5929}5930}5931}59325933// If coercing a fixed vector from a scalable vector for ABI5934// compatibility, and the types match, use the llvm.vector.extract5935// intrinsic to perform the conversion.5936if (auto *FixedDstTy = dyn_cast<llvm::FixedVectorType>(RetIRTy)) {5937llvm::Value *V = CI;5938if (auto *ScalableSrcTy =5939dyn_cast<llvm::ScalableVectorType>(V->getType())) {5940if (FixedDstTy->getElementType() ==5941ScalableSrcTy->getElementType()) {5942llvm::Value *Zero = llvm::Constant::getNullValue(CGM.Int64Ty);5943V = Builder.CreateExtractVector(FixedDstTy, V, Zero,5944"cast.fixed");5945return RValue::get(V);5946}5947}5948}59495950Address DestPtr = ReturnValue.getValue();5951bool DestIsVolatile = ReturnValue.isVolatile();5952uint64_t DestSize =5953getContext().getTypeInfoDataSizeInChars(RetTy).Width.getQuantity();59545955if (!DestPtr.isValid()) {5956DestPtr = CreateMemTemp(RetTy, "coerce");5957DestIsVolatile = false;5958DestSize = getContext().getTypeSizeInChars(RetTy).getQuantity();5959}59605961// An empty record can overlap other data (if declared with5962// no_unique_address); omit the store for such types - as there is no5963// actual data to store.5964if (!isEmptyRecord(getContext(), RetTy, true)) {5965// If the value is offset in memory, apply the offset now.5966Address StorePtr = emitAddressAtOffset(*this, DestPtr, RetAI);5967CreateCoercedStore(5968CI, StorePtr,5969llvm::TypeSize::getFixed(DestSize - RetAI.getDirectOffset()),5970DestIsVolatile);5971}59725973return convertTempToRValue(DestPtr, RetTy, SourceLocation());5974}59755976case ABIArgInfo::Expand:5977case ABIArgInfo::IndirectAliased:5978llvm_unreachable("Invalid ABI kind for return argument");5979}59805981llvm_unreachable("Unhandled ABIArgInfo::Kind");5982}();5983}59845985// Emit the assume_aligned check on the return value.5986if (Ret.isScalar() && TargetDecl) {5987AssumeAlignedAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);5988AllocAlignAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);5989}59905991// Explicitly call CallLifetimeEnd::Emit just to re-use the code even though5992// we can't use the full cleanup mechanism.5993for (CallLifetimeEnd &LifetimeEnd : CallLifetimeEndAfterCall)5994LifetimeEnd.Emit(*this, /*Flags=*/{});59955996if (!ReturnValue.isExternallyDestructed() &&5997RetTy.isDestructedType() == QualType::DK_nontrivial_c_struct)5998pushDestroy(QualType::DK_nontrivial_c_struct, Ret.getAggregateAddress(),5999RetTy);60006001return Ret;6002}60036004CGCallee CGCallee::prepareConcreteCallee(CodeGenFunction &CGF) const {6005if (isVirtual()) {6006const CallExpr *CE = getVirtualCallExpr();6007return CGF.CGM.getCXXABI().getVirtualFunctionPointer(6008CGF, getVirtualMethodDecl(), getThisAddress(), getVirtualFunctionType(),6009CE ? CE->getBeginLoc() : SourceLocation());6010}60116012return *this;6013}60146015/* VarArg handling */60166017RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr,6018AggValueSlot Slot) {6019VAListAddr = VE->isMicrosoftABI() ? EmitMSVAListRef(VE->getSubExpr())6020: EmitVAListRef(VE->getSubExpr());6021QualType Ty = VE->getType();6022if (VE->isMicrosoftABI())6023return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);6024return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);6025}602660276028