31 #include "llvm/ADT/StringExtras.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Attributes.h"
34 #include "llvm/IR/CallingConv.h"
35 #include "llvm/IR/CallSite.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/InlineAsm.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/IR/IntrinsicInst.h"
40 #include "llvm/Transforms/Utils/Local.h"
41 using namespace clang;
42 using namespace CodeGen;
46 unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(
CallingConv CC) {
53 case CC_Win64:
return llvm::CallingConv::Win64;
55 case CC_AAPCS:
return llvm::CallingConv::ARM_AAPCS;
56 case CC_AAPCS_VFP:
return llvm::CallingConv::ARM_AAPCS_VFP;
66 case CC_Swift:
return llvm::CallingConv::Swift;
108 unsigned totalArgs) {
110 assert(paramInfos.size() <= prefixArgs);
111 assert(proto->
getNumParams() + prefixArgs <= totalArgs);
113 paramInfos.reserve(totalArgs);
116 paramInfos.resize(prefixArgs);
120 paramInfos.push_back(ParamInfo);
122 if (ParamInfo.hasPassObjectSize())
123 paramInfos.emplace_back();
126 assert(paramInfos.size() <= totalArgs &&
127 "Did we forget to insert pass_object_size args?");
129 paramInfos.resize(totalArgs);
139 if (!FPT->hasExtParameterInfos()) {
140 assert(paramInfos.empty() &&
141 "We have paramInfos, but the prototype doesn't?");
142 prefix.append(FPT->param_type_begin(), FPT->param_type_end());
146 unsigned PrefixSize = prefix.size();
150 prefix.reserve(prefix.size() + FPT->getNumParams());
152 auto ExtInfos = FPT->getExtParameterInfos();
153 assert(ExtInfos.size() == FPT->getNumParams());
154 for (
unsigned I = 0,
E = FPT->getNumParams();
I !=
E; ++
I) {
155 prefix.push_back(FPT->getParamType(
I));
156 if (ExtInfos[
I].hasPassObjectSize())
180 FTP->getExtInfo(), paramInfos,
199 if (D->
hasAttr<FastCallAttr>())
205 if (D->
hasAttr<ThisCallAttr>())
208 if (D->
hasAttr<VectorCallAttr>())
214 if (PcsAttr *PCS = D->
getAttr<PcsAttr>())
217 if (D->
hasAttr<IntelOclBiccAttr>())
226 if (D->
hasAttr<PreserveMostAttr>())
229 if (D->
hasAttr<PreserveAllAttr>())
254 *
this,
true, argTypes,
264 assert(!isa<CXXConstructorDecl>(MD) &&
"wrong method for constructors!");
265 assert(!isa<CXXDestructorDecl>(MD) &&
"wrong method for destructors!");
295 bool PassParams =
true;
298 if (
auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
303 if (
auto Inherited = CD->getInheritedConstructor())
318 if (!paramInfos.empty()) {
321 paramInfos.insert(paramInfos.begin() + 1, AddedArgs.
Prefix,
324 paramInfos.append(AddedArgs.
Suffix,
329 (PassParams && MD->isVariadic() ?
RequiredArgs(argTypes.size())
333 CanQualType resultType = TheCXXABI.HasThisReturn(GD)
335 : TheCXXABI.hasMostDerivedReturn(GD)
336 ? CGM.getContext().VoidPtrTy
339 false, argTypes, extInfo,
340 paramInfos, required);
346 for (
auto &arg : args)
354 for (
auto &arg : args)
361 unsigned prefixArgs,
unsigned totalArgs) {
381 unsigned ExtraPrefixArgs,
382 unsigned ExtraSuffixArgs,
383 bool PassProtoArgs) {
386 for (
const auto &Arg : args)
390 unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
394 RequiredArgs::forPrototypePlus(FPT, TotalPrefixArgs + ExtraSuffixArgs, D);
396 CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
398 : TheCXXABI.hasMostDerivedReturn(GD)
399 ? CGM.getContext().VoidPtrTy
406 if (PassProtoArgs && FPT->hasExtParameterInfos()) {
412 false, ArgTypes, Info,
413 ParamInfos, Required);
421 if (MD->isInstance())
422 return arrangeCXXMethodDeclaration(MD);
426 assert(isa<FunctionType>(FTy));
432 noProto->getReturnType(),
false,
466 bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows();
469 if (getContext().getLangOpts().ObjCAutoRefCount &&
470 MD->
hasAttr<NSReturnsRetainedAttr>())
478 false, argTys, einfo, {}, required);
482 CodeGenTypes::arrangeUnprototypedObjCMessageSend(
QualType returnType,
503 return arrangeFunctionDeclaration(FD);
513 assert(MD->
isVirtual() &&
"only virtual memptrs have thunks");
531 ArgTys.push_back(*FTP->param_type_begin());
549 unsigned numExtraRequiredArgs,
551 assert(args.size() >= numExtraRequiredArgs);
561 if (proto->isVariadic())
562 required =
RequiredArgs(proto->getNumParams() + numExtraRequiredArgs);
564 if (proto->hasExtParameterInfos())
574 cast<FunctionNoProtoType>(fnType))) {
580 for (
const auto &arg : args)
597 chainCall ? 1 : 0, chainCall);
617 false,
false, argTypes,
619 RequiredArgs::forPrototypePlus(proto, 1,
nullptr));
623 CodeGenTypes::arrangeBuiltinFunctionCall(
QualType resultType,
627 for (
const auto &Arg : args)
636 CodeGenTypes::arrangeBuiltinFunctionDeclaration(
QualType resultType,
646 CodeGenTypes::arrangeBuiltinFunctionDeclaration(
CanQualType resultType,
649 resultType,
false,
false,
661 unsigned numPrefixArgs) {
662 assert(numPrefixArgs + 1 <= args.size() &&
663 "Emitting a call with less args than the required prefix?");
675 false, argTypes, info, paramInfos, required);
680 getContext().VoidTy,
false,
false,
687 assert(signature.
arg_size() <= args.size());
688 if (signature.
arg_size() == args.size())
693 if (!sigParamInfos.empty()) {
694 paramInfos.append(sigParamInfos.begin(), sigParamInfos.end());
695 paramInfos.resize(args.size());
727 assert(std::all_of(argTypes.begin(), argTypes.end(),
728 [](
CanQualType T) {
return T.isCanonicalAsParam(); }));
731 llvm::FoldingSetNodeID
ID;
732 CGFunctionInfo::Profile(ID, instanceMethod, chainCall, info, paramInfos,
733 required, resultType, argTypes);
735 void *insertPos =
nullptr;
736 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
740 unsigned CC = ClangCallConvToLLVMCallConv(info.
getCC());
744 paramInfos, resultType, argTypes, required);
745 FunctionInfos.InsertNode(FI, insertPos);
747 bool inserted = FunctionsBeingProcessed.insert(FI).second;
749 assert(inserted &&
"Recursively being processed?");
752 if (CC == llvm::CallingConv::SPIR_KERNEL) {
759 getABIInfo().computeInfo(*FI);
769 for (
auto &
I : FI->arguments())
770 if (
I.info.canHaveCoerceToType() &&
I.info.getCoerceToType() ==
nullptr)
771 I.info.setCoerceToType(ConvertType(
I.type));
773 bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
774 assert(erased &&
"Not in set?");
787 assert(paramInfos.empty() || paramInfos.size() == argTypes.size());
790 operator new(totalSizeToAlloc<ArgInfo, ExtParameterInfo>(
791 argTypes.size() + 1, paramInfos.size()));
794 FI->CallingConvention = llvmCC;
795 FI->EffectiveCallingConvention = llvmCC;
796 FI->ASTCallingConvention = info.
getCC();
797 FI->InstanceMethod = instanceMethod;
798 FI->ChainCall = chainCall;
802 FI->Required = required;
805 FI->ArgStruct =
nullptr;
806 FI->ArgStructAlign = 0;
807 FI->NumArgs = argTypes.size();
808 FI->HasExtParameterInfos = !paramInfos.empty();
809 FI->getArgsBuffer()[0].
type = resultType;
810 for (
unsigned i = 0, e = argTypes.size(); i != e; ++i)
811 FI->getArgsBuffer()[i + 1].
type = argTypes[i];
812 for (
unsigned i = 0, e = paramInfos.size(); i != e; ++i)
813 FI->getExtParameterInfosBuffer()[i] = paramInfos[i];
823 struct TypeExpansion {
824 enum TypeExpansionKind {
836 const TypeExpansionKind
Kind;
838 TypeExpansion(TypeExpansionKind K) :
Kind(K) {}
839 virtual ~TypeExpansion() {}
842 struct ConstantArrayExpansion : TypeExpansion {
846 ConstantArrayExpansion(
QualType EltTy, uint64_t NumElts)
847 : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {}
848 static bool classof(
const TypeExpansion *TE) {
849 return TE->Kind == TEK_ConstantArray;
853 struct RecordExpansion : TypeExpansion {
860 : TypeExpansion(TEK_Record), Bases(std::move(Bases)),
861 Fields(std::move(Fields)) {}
862 static bool classof(
const TypeExpansion *TE) {
863 return TE->Kind == TEK_Record;
867 struct ComplexExpansion : TypeExpansion {
871 static bool classof(
const TypeExpansion *TE) {
876 struct NoExpansion : TypeExpansion {
877 NoExpansion() : TypeExpansion(TEK_None) {}
878 static bool classof(
const TypeExpansion *TE) {
879 return TE->Kind == TEK_None;
884 static std::unique_ptr<TypeExpansion>
887 return llvm::make_unique<ConstantArrayExpansion>(
888 AT->getElementType(), AT->getSize().getZExtValue());
895 "Cannot expand structure with flexible array.");
902 for (
const auto *FD : RD->
fields()) {
904 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
906 assert(!FD->isBitField() &&
907 "Cannot expand structure with bit-field members.");
909 if (UnionSize < FieldSize) {
910 UnionSize = FieldSize;
915 Fields.push_back(LargestFD);
917 if (
const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
918 assert(!CXXRD->isDynamicClass() &&
919 "cannot expand vtable pointers in dynamic classes");
921 Bases.push_back(&BS);
924 for (
const auto *FD : RD->
fields()) {
926 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
928 assert(!FD->isBitField() &&
929 "Cannot expand structure with bit-field members.");
930 Fields.push_back(FD);
933 return llvm::make_unique<RecordExpansion>(std::move(Bases),
937 return llvm::make_unique<ComplexExpansion>(CT->getElementType());
939 return llvm::make_unique<NoExpansion>();
944 if (
auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
947 if (
auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
949 for (
auto BS : RExp->Bases)
951 for (
auto FD : RExp->Fields)
955 if (isa<ComplexExpansion>(Exp.get()))
957 assert(isa<NoExpansion>(Exp.get()));
965 if (
auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
966 for (
int i = 0, n = CAExp->NumElts; i < n; i++) {
967 getExpandedTypes(CAExp->EltTy, TI);
969 }
else if (
auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
970 for (
auto BS : RExp->Bases)
971 getExpandedTypes(BS->getType(), TI);
972 for (
auto FD : RExp->Fields)
973 getExpandedTypes(FD->getType(), TI);
974 }
else if (
auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) {
975 llvm::Type *EltTy = ConvertType(CExp->EltTy);
979 assert(isa<NoExpansion>(Exp.get()));
980 *TI++ = ConvertType(Ty);
985 ConstantArrayExpansion *CAE,
987 llvm::function_ref<
void(
Address)> Fn) {
992 for (
int i = 0, n = CAE->NumElts; i < n; i++) {
995 Fn(
Address(EltAddr, EltAlign));
999 void CodeGenFunction::ExpandTypeFromArgs(
1002 "Unexpected non-simple lvalue during struct expansion.");
1005 if (
auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1008 LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy);
1009 ExpandTypeFromArgs(CAExp->EltTy, LV, AI);
1011 }
else if (
auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1021 ExpandTypeFromArgs(BS->getType(), SubLV, AI);
1023 for (
auto FD : RExp->Fields) {
1025 LValue SubLV = EmitLValueForFieldInitialization(LV, FD);
1026 ExpandTypeFromArgs(FD->getType(), SubLV, AI);
1028 }
else if (isa<ComplexExpansion>(Exp.get())) {
1029 auto realValue = *AI++;
1030 auto imagValue = *AI++;
1031 EmitStoreOfComplex(
ComplexPairTy(realValue, imagValue), LV,
true);
1033 assert(isa<NoExpansion>(Exp.get()));
1034 EmitStoreThroughLValue(RValue::get(*AI++), LV);
1038 void CodeGenFunction::ExpandTypeToArgs(
1042 if (
auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
1047 ExpandTypeToArgs(CAExp->EltTy, EltRV, IRFuncTy, IRCallArgs, IRCallArgPos);
1049 }
else if (
auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
1056 RValue BaseRV = RValue::getAggregate(Base);
1059 ExpandTypeToArgs(BS->getType(), BaseRV, IRFuncTy, IRCallArgs,
1063 LValue LV = MakeAddrLValue(This, Ty);
1064 for (
auto FD : RExp->Fields) {
1066 ExpandTypeToArgs(FD->getType(), FldRV, IRFuncTy, IRCallArgs,
1069 }
else if (isa<ComplexExpansion>(Exp.get())) {
1071 IRCallArgs[IRCallArgPos++] = CV.first;
1072 IRCallArgs[IRCallArgPos++] = CV.second;
1074 assert(isa<NoExpansion>(Exp.get()));
1076 "Unexpected non-scalar rvalue during struct expansion.");
1080 if (IRCallArgPos < IRFuncTy->getNumParams() &&
1081 V->getType() != IRFuncTy->getParamType(IRCallArgPos))
1082 V =
Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos));
1084 IRCallArgs[IRCallArgPos++] = V;
1104 llvm::StructType *SrcSTy,
1107 if (SrcSTy->getNumElements() == 0)
return SrcPtr;
1115 uint64_t FirstEltSize =
1117 if (FirstEltSize < DstSize &&
1126 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
1142 if (Val->getType() == Ty)
1145 if (isa<llvm::PointerType>(Val->getType())) {
1147 if (isa<llvm::PointerType>(Ty))
1154 llvm::Type *DestIntTy = Ty;
1155 if (isa<llvm::PointerType>(DestIntTy))
1158 if (Val->getType() != DestIntTy) {
1160 if (DL.isBigEndian()) {
1163 uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType());
1164 uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy);
1166 if (SrcSize > DstSize) {
1167 Val = CGF.
Builder.CreateLShr(Val, SrcSize - DstSize,
"coerce.highbits");
1168 Val = CGF.
Builder.CreateTrunc(Val, DestIntTy,
"coerce.val.ii");
1170 Val = CGF.
Builder.CreateZExt(Val, DestIntTy,
"coerce.val.ii");
1171 Val = CGF.
Builder.CreateShl(Val, DstSize - SrcSize,
"coerce.highbits");
1175 Val = CGF.
Builder.CreateIntCast(Val, DestIntTy,
false,
"coerce.val.ii");
1179 if (isa<llvm::PointerType>(Ty))
1180 Val = CGF.
Builder.CreateIntToPtr(Val, Ty,
"coerce.val.ip");
1203 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
1205 SrcTy = Src.
getType()->getElementType();
1212 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
1213 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
1219 if (SrcSize >= DstSize) {
1235 llvm::ConstantInt::get(CGF.
IntPtrTy, SrcSize),
1245 Address Dest,
bool DestIsVolatile) {
1247 if (llvm::StructType *STy =
1248 dyn_cast<llvm::StructType>(Val->getType())) {
1249 const llvm::StructLayout *Layout =
1252 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1273 llvm::Type *SrcTy = Src->getType();
1274 llvm::Type *DstTy = Dst.
getType()->getElementType();
1275 if (SrcTy == DstTy) {
1282 if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
1284 DstTy = Dst.
getType()->getElementType();
1289 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
1290 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
1299 if (SrcSize <= DstSize) {
1317 llvm::ConstantInt::get(CGF.
IntPtrTy, DstSize),
1337 class ClangToLLVMArgMapping {
1338 static const unsigned InvalidIndex = ~0U;
1339 unsigned InallocaArgNo;
1341 unsigned TotalIRArgs;
1345 unsigned PaddingArgIndex;
1348 unsigned FirstArgIndex;
1349 unsigned NumberOfArgs;
1352 : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex),
1360 bool OnlyRequiredArgs =
false)
1361 : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0),
1362 ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
1363 construct(Context, FI, OnlyRequiredArgs);
1366 bool hasInallocaArg()
const {
return InallocaArgNo != InvalidIndex; }
1367 unsigned getInallocaArgNo()
const {
1368 assert(hasInallocaArg());
1369 return InallocaArgNo;
1372 bool hasSRetArg()
const {
return SRetArgNo != InvalidIndex; }
1373 unsigned getSRetArgNo()
const {
1374 assert(hasSRetArg());
1378 unsigned totalIRArgs()
const {
return TotalIRArgs; }
1380 bool hasPaddingArg(
unsigned ArgNo)
const {
1381 assert(ArgNo < ArgInfo.size());
1382 return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex;
1384 unsigned getPaddingArgNo(
unsigned ArgNo)
const {
1385 assert(hasPaddingArg(ArgNo));
1386 return ArgInfo[ArgNo].PaddingArgIndex;
1391 std::pair<unsigned, unsigned> getIRArgs(
unsigned ArgNo)
const {
1392 assert(ArgNo < ArgInfo.size());
1393 return std::make_pair(ArgInfo[ArgNo].FirstArgIndex,
1394 ArgInfo[ArgNo].NumberOfArgs);
1399 bool OnlyRequiredArgs);
1404 bool OnlyRequiredArgs) {
1405 unsigned IRArgNo = 0;
1406 bool SwapThisWithSRet =
false;
1409 if (RetAI.
getKind() == ABIArgInfo::Indirect) {
1411 SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++;
1422 auto &IRArgs = ArgInfo[ArgNo];
1425 IRArgs.PaddingArgIndex = IRArgNo++;
1428 case ABIArgInfo::Extend:
1429 case ABIArgInfo::Direct: {
1431 llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.
getCoerceToType());
1433 IRArgs.NumberOfArgs = STy->getNumElements();
1435 IRArgs.NumberOfArgs = 1;
1439 case ABIArgInfo::Indirect:
1440 IRArgs.NumberOfArgs = 1;
1442 case ABIArgInfo::Ignore:
1443 case ABIArgInfo::InAlloca:
1445 IRArgs.NumberOfArgs = 0;
1447 case ABIArgInfo::CoerceAndExpand:
1450 case ABIArgInfo::Expand:
1455 if (IRArgs.NumberOfArgs > 0) {
1456 IRArgs.FirstArgIndex = IRArgNo;
1457 IRArgNo += IRArgs.NumberOfArgs;
1462 if (IRArgNo == 1 && SwapThisWithSRet)
1465 assert(ArgNo == ArgInfo.size());
1468 InallocaArgNo = IRArgNo++;
1470 TotalIRArgs = IRArgNo;
1481 return ReturnTypeUsesSRet(FI) &&
1482 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs();
1485 bool CodeGenModule::ReturnTypeUsesFPRet(
QualType ResultType) {
1487 switch (BT->getKind()) {
1490 case BuiltinType::Float:
1492 case BuiltinType::Double:
1494 case BuiltinType::LongDouble:
1502 bool CodeGenModule::ReturnTypeUsesFP2Ret(
QualType ResultType) {
1505 if (BT->getKind() == BuiltinType::LongDouble)
1506 return getTarget().useObjCFP2RetForComplexLongDouble();
1513 llvm::FunctionType *CodeGenTypes::GetFunctionType(
GlobalDecl GD) {
1515 return GetFunctionType(FI);
1518 llvm::FunctionType *
1521 bool Inserted = FunctionsBeingProcessed.insert(&FI).second;
1523 assert(Inserted &&
"Recursively being processed?");
1525 llvm::Type *resultType =
nullptr;
1528 case ABIArgInfo::Expand:
1529 llvm_unreachable(
"Invalid ABI kind for return argument");
1531 case ABIArgInfo::Extend:
1532 case ABIArgInfo::Direct:
1536 case ABIArgInfo::InAlloca:
1540 llvm::Type *ty = ConvertType(ret);
1542 resultType = llvm::PointerType::get(ty, addressSpace);
1544 resultType = llvm::Type::getVoidTy(getLLVMContext());
1548 case ABIArgInfo::Indirect:
1549 case ABIArgInfo::Ignore:
1550 resultType = llvm::Type::getVoidTy(getLLVMContext());
1553 case ABIArgInfo::CoerceAndExpand:
1558 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI,
true);
1562 if (IRFunctionArgs.hasSRetArg()) {
1564 llvm::Type *Ty = ConvertType(Ret);
1566 ArgTypes[IRFunctionArgs.getSRetArgNo()] =
1567 llvm::PointerType::get(Ty, AddressSpace);
1571 if (IRFunctionArgs.hasInallocaArg()) {
1574 ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo();
1581 for (; it != ie; ++it, ++ArgNo) {
1585 if (IRFunctionArgs.hasPaddingArg(ArgNo))
1586 ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1589 unsigned FirstIRArg, NumIRArgs;
1590 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
1593 case ABIArgInfo::Ignore:
1594 case ABIArgInfo::InAlloca:
1595 assert(NumIRArgs == 0);
1598 case ABIArgInfo::Indirect: {
1599 assert(NumIRArgs == 1);
1601 llvm::Type *LTy = ConvertTypeForMem(it->
type);
1602 ArgTypes[FirstIRArg] = LTy->getPointerTo(
1603 CGM.getDataLayout().getAllocaAddrSpace());
1607 case ABIArgInfo::Extend:
1608 case ABIArgInfo::Direct: {
1612 llvm::StructType *st = dyn_cast<llvm::StructType>(argType);
1614 assert(NumIRArgs == st->getNumElements());
1615 for (
unsigned i = 0, e = st->getNumElements(); i != e; ++i)
1616 ArgTypes[FirstIRArg + i] = st->getElementType(i);
1618 assert(NumIRArgs == 1);
1619 ArgTypes[FirstIRArg] = argType;
1624 case ABIArgInfo::CoerceAndExpand: {
1625 auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1627 *ArgTypesIter++ = EltTy;
1629 assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1633 case ABIArgInfo::Expand:
1634 auto ArgTypesIter = ArgTypes.begin() + FirstIRArg;
1635 getExpandedTypes(it->
type, ArgTypesIter);
1636 assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs);
1641 bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
1642 assert(Erased &&
"Not in set?");
1644 return llvm::FunctionType::get(resultType, ArgTypes, FI.
isVariadic());
1647 llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(
GlobalDecl GD) {
1651 if (!isFuncTypeConvertible(FPT))
1652 return llvm::StructType::get(getLLVMContext());
1655 if (isa<CXXDestructorDecl>(MD))
1659 Info = &arrangeCXXMethodDeclaration(MD);
1660 return GetFunctionType(*Info);
1664 llvm::AttrBuilder &FuncAttrs,
1671 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1674 void CodeGenModule::ConstructDefaultFnAttrList(StringRef
Name,
bool HasOptnone,
1675 bool AttrOnCallSite,
1676 llvm::AttrBuilder &FuncAttrs) {
1679 if (CodeGenOpts.OptimizeSize)
1680 FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1681 if (CodeGenOpts.OptimizeSize == 2)
1682 FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1685 if (CodeGenOpts.DisableRedZone)
1686 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
1687 if (CodeGenOpts.NoImplicitFloat)
1688 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
1690 if (AttrOnCallSite) {
1692 if (!CodeGenOpts.SimplifyLibCalls ||
1693 CodeGenOpts.isNoBuiltinFunc(Name.data()))
1694 FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1695 if (!CodeGenOpts.TrapFuncName.empty())
1696 FuncAttrs.addAttribute(
"trap-func-name", CodeGenOpts.TrapFuncName);
1699 if (!CodeGenOpts.DisableFPElim) {
1700 FuncAttrs.addAttribute(
"no-frame-pointer-elim",
"false");
1701 }
else if (CodeGenOpts.OmitLeafFramePointer) {
1702 FuncAttrs.addAttribute(
"no-frame-pointer-elim",
"false");
1703 FuncAttrs.addAttribute(
"no-frame-pointer-elim-non-leaf");
1705 FuncAttrs.addAttribute(
"no-frame-pointer-elim",
"true");
1706 FuncAttrs.addAttribute(
"no-frame-pointer-elim-non-leaf");
1709 FuncAttrs.addAttribute(
"less-precise-fpmad",
1710 llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
1712 if (!CodeGenOpts.FPDenormalMode.empty())
1713 FuncAttrs.addAttribute(
"denormal-fp-math", CodeGenOpts.FPDenormalMode);
1715 FuncAttrs.addAttribute(
"no-trapping-math",
1716 llvm::toStringRef(CodeGenOpts.NoTrappingMath));
1720 FuncAttrs.addAttribute(
"no-infs-fp-math",
1721 llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
1722 FuncAttrs.addAttribute(
"no-nans-fp-math",
1723 llvm::toStringRef(CodeGenOpts.NoNaNsFPMath));
1724 FuncAttrs.addAttribute(
"unsafe-fp-math",
1725 llvm::toStringRef(CodeGenOpts.UnsafeFPMath));
1726 FuncAttrs.addAttribute(
"use-soft-float",
1727 llvm::toStringRef(CodeGenOpts.SoftFloat));
1728 FuncAttrs.addAttribute(
"stack-protector-buffer-size",
1729 llvm::utostr(CodeGenOpts.SSPBufferSize));
1730 FuncAttrs.addAttribute(
"no-signed-zeros-fp-math",
1731 llvm::toStringRef(CodeGenOpts.NoSignedZeros));
1732 FuncAttrs.addAttribute(
1733 "correctly-rounded-divide-sqrt-fp-math",
1734 llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt));
1737 std::vector<std::string> &Recips = getTarget().getTargetOpts().Reciprocals;
1738 if (!Recips.empty())
1739 FuncAttrs.addAttribute(
"reciprocal-estimates",
1740 llvm::join(Recips.begin(), Recips.end(),
","));
1742 if (CodeGenOpts.StackRealignment)
1743 FuncAttrs.addAttribute(
"stackrealign");
1744 if (CodeGenOpts.Backchain)
1745 FuncAttrs.addAttribute(
"backchain");
1748 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
1753 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
1756 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1759 if (getLangOpts().CUDADeviceFlushDenormalsToZero)
1760 FuncAttrs.addAttribute(
"nvptx-f32ftz",
"true");
1764 void CodeGenModule::AddDefaultFnAttrs(llvm::Function &F) {
1765 llvm::AttrBuilder FuncAttrs;
1766 ConstructDefaultFnAttrList(F.getName(),
1767 F.hasFnAttribute(llvm::Attribute::OptimizeNone),
1769 F.addAttributes(llvm::AttributeList::FunctionIndex, FuncAttrs);
1772 void CodeGenModule::ConstructAttributeList(
1774 llvm::AttributeList &AttrList,
unsigned &
CallingConv,
bool AttrOnCallSite) {
1775 llvm::AttrBuilder FuncAttrs;
1776 llvm::AttrBuilder RetAttrs;
1780 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1789 bool HasOptnone =
false;
1792 if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
1793 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
1794 if (TargetDecl->hasAttr<NoThrowAttr>())
1795 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1796 if (TargetDecl->hasAttr<NoReturnAttr>())
1797 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1798 if (TargetDecl->hasAttr<ColdAttr>())
1799 FuncAttrs.addAttribute(llvm::Attribute::Cold);
1800 if (TargetDecl->hasAttr<NoDuplicateAttr>())
1801 FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate);
1802 if (TargetDecl->hasAttr<ConvergentAttr>())
1803 FuncAttrs.addAttribute(llvm::Attribute::Convergent);
1805 if (
const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
1811 if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->
isVirtual()))
1812 FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1816 if (TargetDecl->hasAttr<ConstAttr>()) {
1817 FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1818 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1819 }
else if (TargetDecl->hasAttr<PureAttr>()) {
1820 FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1821 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1822 }
else if (TargetDecl->hasAttr<NoAliasAttr>()) {
1823 FuncAttrs.addAttribute(llvm::Attribute::ArgMemOnly);
1824 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1826 if (TargetDecl->hasAttr<RestrictAttr>())
1827 RetAttrs.addAttribute(llvm::Attribute::NoAlias);
1828 if (TargetDecl->hasAttr<ReturnsNonNullAttr>())
1829 RetAttrs.addAttribute(llvm::Attribute::NonNull);
1830 if (TargetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>())
1831 FuncAttrs.addAttribute(
"no_caller_saved_registers");
1833 HasOptnone = TargetDecl->
hasAttr<OptimizeNoneAttr>();
1834 if (
auto *AllocSize = TargetDecl->getAttr<AllocSizeAttr>()) {
1837 if (
unsigned N = AllocSize->getNumElemsParam())
1838 NumElemsParam = N - 1;
1839 FuncAttrs.addAllocSizeAttr(AllocSize->getElemSizeParam() - 1,
1844 ConstructDefaultFnAttrList(Name, HasOptnone, AttrOnCallSite, FuncAttrs);
1846 if (CodeGenOpts.EnableSegmentedStacks &&
1847 !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>()))
1848 FuncAttrs.addAttribute(
"split-stack");
1850 if (!AttrOnCallSite) {
1851 bool DisableTailCalls =
1852 CodeGenOpts.DisableTailCalls ||
1853 (TargetDecl && (TargetDecl->hasAttr<DisableTailCallsAttr>() ||
1854 TargetDecl->hasAttr<AnyX86InterruptAttr>()));
1855 FuncAttrs.addAttribute(
"disable-tail-calls",
1856 llvm::toStringRef(DisableTailCalls));
1861 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
1862 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
1863 if (FD && FD->
hasAttr<TargetAttr>()) {
1864 llvm::StringMap<bool> FeatureMap;
1865 getFunctionFeatureMap(FeatureMap, FD);
1868 std::vector<std::string> Features;
1869 for (llvm::StringMap<bool>::const_iterator it = FeatureMap.begin(),
1870 ie = FeatureMap.end();
1872 Features.push_back((it->second ?
"+" :
"-") + it->first().str());
1878 const auto *TD = FD->
getAttr<TargetAttr>();
1879 TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
1880 if (ParsedAttr.Architecture !=
"")
1881 TargetCPU = ParsedAttr.Architecture;
1882 if (TargetCPU !=
"")
1883 FuncAttrs.addAttribute(
"target-cpu", TargetCPU);
1884 if (!Features.empty()) {
1885 std::sort(Features.begin(), Features.end());
1886 FuncAttrs.addAttribute(
1888 llvm::join(Features.begin(), Features.end(),
","));
1893 std::vector<std::string> &Features = getTarget().getTargetOpts().Features;
1894 if (TargetCPU !=
"")
1895 FuncAttrs.addAttribute(
"target-cpu", TargetCPU);
1896 if (!Features.empty()) {
1897 std::sort(Features.begin(), Features.end());
1898 FuncAttrs.addAttribute(
1900 llvm::join(Features.begin(), Features.end(),
","));
1905 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI);
1910 case ABIArgInfo::Extend:
1912 RetAttrs.addAttribute(llvm::Attribute::SExt);
1914 RetAttrs.addAttribute(llvm::Attribute::ZExt);
1916 case ABIArgInfo::Direct:
1918 RetAttrs.addAttribute(llvm::Attribute::InReg);
1920 case ABIArgInfo::Ignore:
1923 case ABIArgInfo::InAlloca:
1924 case ABIArgInfo::Indirect: {
1926 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1927 .removeAttribute(llvm::Attribute::ReadNone);
1931 case ABIArgInfo::CoerceAndExpand:
1934 case ABIArgInfo::Expand:
1935 llvm_unreachable(
"Invalid ABI kind for return argument");
1941 RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
1943 else if (getContext().getTargetAddressSpace(PTy) == 0)
1944 RetAttrs.addAttribute(llvm::Attribute::NonNull);
1947 bool hasUsedSRet =
false;
1951 if (IRFunctionArgs.hasSRetArg()) {
1952 llvm::AttrBuilder SRETAttrs;
1953 SRETAttrs.addAttribute(llvm::Attribute::StructRet);
1956 SRETAttrs.addAttribute(llvm::Attribute::InReg);
1957 ArgAttrs[IRFunctionArgs.getSRetArgNo()] =
1958 llvm::AttributeSet::get(getLLVMContext(), SRETAttrs);
1962 if (IRFunctionArgs.hasInallocaArg()) {
1963 llvm::AttrBuilder Attrs;
1964 Attrs.addAttribute(llvm::Attribute::InAlloca);
1965 ArgAttrs[IRFunctionArgs.getInallocaArgNo()] =
1966 llvm::AttributeSet::get(getLLVMContext(), Attrs);
1972 I !=
E; ++
I, ++ArgNo) {
1975 llvm::AttrBuilder Attrs;
1978 if (IRFunctionArgs.hasPaddingArg(ArgNo)) {
1980 ArgAttrs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
1981 llvm::AttributeSet::get(
1983 llvm::AttrBuilder().addAttribute(llvm::Attribute::InReg));
1991 case ABIArgInfo::Extend:
1993 Attrs.addAttribute(llvm::Attribute::SExt);
1995 if (getTypes().getABIInfo().shouldSignExtUnsignedType(ParamType))
1996 Attrs.addAttribute(llvm::Attribute::SExt);
1998 Attrs.addAttribute(llvm::Attribute::ZExt);
2001 case ABIArgInfo::Direct:
2003 Attrs.addAttribute(llvm::Attribute::Nest);
2005 Attrs.addAttribute(llvm::Attribute::InReg);
2008 case ABIArgInfo::Indirect: {
2010 Attrs.addAttribute(llvm::Attribute::InReg);
2013 Attrs.addAttribute(llvm::Attribute::ByVal);
2035 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
2036 .removeAttribute(llvm::Attribute::ReadNone);
2039 case ABIArgInfo::Ignore:
2040 case ABIArgInfo::Expand:
2041 case ABIArgInfo::CoerceAndExpand:
2044 case ABIArgInfo::InAlloca:
2046 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
2047 .removeAttribute(llvm::Attribute::ReadNone);
2054 Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy)
2056 else if (getContext().getTargetAddressSpace(PTy) == 0)
2057 Attrs.addAttribute(llvm::Attribute::NonNull);
2068 Attrs.addAttribute(llvm::Attribute::StructRet);
2073 Attrs.addAttribute(llvm::Attribute::NoAlias);
2077 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
2078 auto info = getContext().getTypeInfoInChars(PTy);
2079 Attrs.addDereferenceableAttr(info.first.getQuantity());
2080 Attrs.addAttribute(llvm::Attribute::getWithAlignment(getLLVMContext(),
2081 info.second.getQuantity()));
2087 Attrs.addAttribute(llvm::Attribute::SwiftError);
2091 Attrs.addAttribute(llvm::Attribute::SwiftSelf);
2095 if (Attrs.hasAttributes()) {
2096 unsigned FirstIRArg, NumIRArgs;
2097 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2098 for (
unsigned i = 0; i < NumIRArgs; i++)
2099 ArgAttrs[FirstIRArg + i] =
2100 llvm::AttributeSet::get(getLLVMContext(), Attrs);
2105 AttrList = llvm::AttributeList::get(
2106 getLLVMContext(), llvm::AttributeSet::get(getLLVMContext(), FuncAttrs),
2107 llvm::AttributeSet::get(getLLVMContext(), RetAttrs), ArgAttrs);
2119 if (value->getType() == varType)
return value;
2121 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
2122 &&
"unexpected promotion type");
2124 if (isa<llvm::IntegerType>(varType))
2125 return CGF.
Builder.CreateTrunc(value, varType,
"arg.unpromote");
2127 return CGF.
Builder.CreateFPCast(value, varType,
"arg.unpromote");
2133 QualType ArgType,
unsigned ArgNo) {
2145 if (
auto ParmNNAttr = PVD->
getAttr<NonNullAttr>())
2152 if (NNAttr->isNonNull(ArgNo))
2162 CopyBackSwiftError(
Address temp,
Address arg) : Temp(temp), Arg(arg) {}
2173 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>())
2181 if (
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
2182 if (FD->hasImplicitReturnZero()) {
2183 QualType RetTy = FD->getReturnType().getUnqualifiedType();
2184 llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
2185 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
2186 Builder.CreateStore(Zero, ReturnValue);
2193 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI);
2196 FnArgs.reserve(IRFunctionArgs.totalIRArgs());
2197 for (
auto &Arg : Fn->args()) {
2198 FnArgs.push_back(&Arg);
2200 assert(FnArgs.size() == IRFunctionArgs.totalIRArgs());
2204 Address ArgStruct = Address::invalid();
2205 const llvm::StructLayout *ArgStructLayout =
nullptr;
2206 if (IRFunctionArgs.hasInallocaArg()) {
2207 ArgStructLayout = CGM.getDataLayout().getStructLayout(FI.
getArgStruct());
2208 ArgStruct =
Address(FnArgs[IRFunctionArgs.getInallocaArgNo()],
2211 assert(ArgStruct.getType() == FI.
getArgStruct()->getPointerTo());
2215 if (IRFunctionArgs.hasSRetArg()) {
2216 auto AI = cast<llvm::Argument>(FnArgs[IRFunctionArgs.getSRetArgNo()]);
2217 AI->setName(
"agg.result");
2218 AI->addAttr(llvm::Attribute::NoAlias);
2225 ArgVals.reserve(Args.size());
2231 assert(FI.
arg_size() == Args.size() &&
2232 "Mismatch between function signature & arguments.");
2235 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
2236 i != e; ++i, ++info_it, ++ArgNo) {
2242 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
2244 unsigned FirstIRArg, NumIRArgs;
2245 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
2248 case ABIArgInfo::InAlloca: {
2249 assert(NumIRArgs == 0);
2253 Address V =
Builder.CreateStructGEP(ArgStruct, FieldIndex, FieldOffset,
2255 ArgVals.push_back(ParamValue::forIndirect(V));
2259 case ABIArgInfo::Indirect: {
2260 assert(NumIRArgs == 1);
2263 if (!hasScalarEvaluationKind(Ty)) {
2268 Address AlignedTemp = CreateMemTemp(Ty,
"coerce");
2275 CharUnits Size = getContext().getTypeSizeInChars(Ty);
2276 auto SizeVal = llvm::ConstantInt::get(IntPtrTy, Size.
getQuantity());
2279 Builder.CreateMemCpy(Dst, Src, SizeVal,
false);
2282 ArgVals.push_back(ParamValue::forIndirect(V));
2286 EmitLoadOfScalar(ParamAddr,
false, Ty, Arg->
getLocStart());
2290 ArgVals.push_back(ParamValue::forDirect(V));
2295 case ABIArgInfo::Extend:
2296 case ABIArgInfo::Direct: {
2302 assert(NumIRArgs == 1);
2304 auto AI = cast<llvm::Argument>(V);
2306 if (
const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
2308 PVD->getFunctionScopeIndex()))
2309 AI->addAttr(llvm::Attribute::NonNull);
2311 QualType OTy = PVD->getOriginalType();
2312 if (
const auto *ArrTy =
2313 getContext().getAsConstantArrayType(OTy)) {
2319 QualType ETy = ArrTy->getElementType();
2320 uint64_t ArrSize = ArrTy->getSize().getZExtValue();
2323 llvm::AttrBuilder Attrs;
2324 Attrs.addDereferenceableAttr(
2325 getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize);
2326 AI->addAttrs(Attrs);
2327 }
else if (getContext().getTargetAddressSpace(ETy) == 0) {
2328 AI->addAttr(llvm::Attribute::NonNull);
2331 }
else if (
const auto *ArrTy =
2332 getContext().getAsVariableArrayType(OTy)) {
2337 !getContext().getTargetAddressSpace(ArrTy->getElementType()))
2338 AI->addAttr(llvm::Attribute::NonNull);
2341 const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
2343 if (
const auto *TOTy = dyn_cast<TypedefType>(OTy))
2344 AVAttr = TOTy->getDecl()->getAttr<AlignValueAttr>();
2347 EmitScalarExpr(AVAttr->getAlignment());
2348 llvm::ConstantInt *AlignmentCI =
2349 cast<llvm::ConstantInt>(AlignmentValue);
2350 unsigned Alignment =
std::min((
unsigned)AlignmentCI->getZExtValue(),
2351 +llvm::Value::MaximumAlignment);
2352 AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment));
2357 AI->addAttr(llvm::Attribute::NoAlias);
2366 CreateMemTemp(pointeeTy, getPointerAlign(),
"swifterror.temp");
2367 Address arg =
Address(V, getContext().getTypeAlignInChars(pointeeTy));
2369 Builder.CreateStore(incomingErrorValue, temp);
2370 V = temp.getPointer();
2375 EHStack.pushCleanup<CopyBackSwiftError>(
NormalCleanup, temp, arg);
2389 llvm::Type *LTy = ConvertType(Arg->
getType());
2390 if (V->getType() != LTy)
2391 V =
Builder.CreateBitCast(V, LTy);
2393 ArgVals.push_back(ParamValue::forDirect(V));
2405 llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.
getCoerceToType());
2407 STy->getNumElements() > 1) {
2408 auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
2409 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
2410 llvm::Type *DstTy = Ptr.getElementType();
2411 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
2413 Address AddrToStoreInto = Address::invalid();
2414 if (SrcSize <= DstSize) {
2415 AddrToStoreInto =
Builder.CreateElementBitCast(Ptr, STy);
2418 CreateTempAlloca(STy, Alloca.
getAlignment(),
"coerce");
2421 assert(STy->getNumElements() == NumIRArgs);
2422 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2423 auto AI = FnArgs[FirstIRArg + i];
2424 AI->setName(Arg->
getName() +
".coerce" + Twine(i));
2428 Builder.CreateStore(AI, EltPtr);
2431 if (SrcSize > DstSize) {
2432 Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
2437 assert(NumIRArgs == 1);
2438 auto AI = FnArgs[FirstIRArg];
2439 AI->setName(Arg->
getName() +
".coerce");
2444 if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
2446 EmitLoadOfScalar(Alloca,
false, Ty, Arg->
getLocStart());
2449 ArgVals.push_back(ParamValue::forDirect(V));
2451 ArgVals.push_back(ParamValue::forIndirect(Alloca));
2456 case ABIArgInfo::CoerceAndExpand: {
2459 ArgVals.push_back(ParamValue::forIndirect(alloca));
2462 alloca =
Builder.CreateElementBitCast(alloca, coercionType);
2463 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
2465 unsigned argIndex = FirstIRArg;
2466 for (
unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2467 llvm::Type *eltType = coercionType->getElementType(i);
2468 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
2471 auto eltAddr =
Builder.CreateStructGEP(alloca, i, layout);
2472 auto elt = FnArgs[argIndex++];
2473 Builder.CreateStore(elt, eltAddr);
2475 assert(argIndex == FirstIRArg + NumIRArgs);
2479 case ABIArgInfo::Expand: {
2484 LValue LV = MakeAddrLValue(Alloca, Ty);
2485 ArgVals.push_back(ParamValue::forIndirect(Alloca));
2487 auto FnArgIter = FnArgs.begin() + FirstIRArg;
2488 ExpandTypeFromArgs(Ty, LV, FnArgIter);
2489 assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs);
2490 for (
unsigned i = 0, e = NumIRArgs; i != e; ++i) {
2491 auto AI = FnArgs[FirstIRArg + i];
2492 AI->setName(Arg->
getName() +
"." + Twine(i));
2497 case ABIArgInfo::Ignore:
2498 assert(NumIRArgs == 0);
2500 if (!hasScalarEvaluationKind(Ty)) {
2501 ArgVals.push_back(ParamValue::forIndirect(CreateMemTemp(Ty)));
2504 ArgVals.push_back(ParamValue::forDirect(U));
2510 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2511 for (
int I = Args.size() - 1;
I >= 0; --
I)
2512 EmitParmDecl(*Args[
I], ArgVals[I], I + 1);
2514 for (
unsigned I = 0,
E = Args.size();
I !=
E; ++
I)
2515 EmitParmDecl(*Args[
I], ArgVals[I], I + 1);
2520 while (insn->use_empty()) {
2521 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
2522 if (!bitcast)
return;
2525 insn = cast<llvm::Instruction>(bitcast->getOperand(0));
2526 bitcast->eraseFromParent();
2534 llvm::BasicBlock *BB = CGF.
Builder.GetInsertBlock();
2535 if (BB->empty())
return nullptr;
2536 if (&BB->back() != result)
return nullptr;
2538 llvm::Type *resultType = result->getType();
2541 llvm::Instruction *generator = cast<llvm::Instruction>(result);
2547 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
2550 generator = cast<llvm::Instruction>(bitcast->getOperand(0));
2553 if (generator->getNextNode() != bitcast)
2556 InstsToKill.push_back(bitcast);
2563 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
2564 if (!call)
return nullptr;
2566 bool doRetainAutorelease;
2569 doRetainAutorelease =
true;
2572 doRetainAutorelease =
false;
2580 llvm::Instruction *prev = call->getPrevNode();
2582 if (isa<llvm::BitCastInst>(prev)) {
2583 prev = prev->getPrevNode();
2586 assert(isa<llvm::CallInst>(prev));
2587 assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
2589 InstsToKill.push_back(prev);
2595 result = call->getArgOperand(0);
2596 InstsToKill.push_back(call);
2600 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
2601 if (!bitcast->hasOneUse())
break;
2602 InstsToKill.push_back(bitcast);
2603 result = bitcast->getOperand(0);
2607 for (
auto *
I : InstsToKill)
2608 I->eraseFromParent();
2611 if (doRetainAutorelease)
2623 dyn_cast_or_null<ObjCMethodDecl>(CGF.
CurCodeDecl);
2624 if (!method)
return nullptr;
2626 if (!self->getType().isConstQualified())
return nullptr;
2629 llvm::CallInst *retainCall =
2630 dyn_cast<llvm::CallInst>(result->stripPointerCasts());
2636 llvm::Value *retainedValue = retainCall->getArgOperand(0);
2637 llvm::LoadInst *load =
2638 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
2639 if (!load || load->isAtomic() || load->isVolatile() ||
2646 llvm::Type *resultType = result->getType();
2648 assert(retainCall->use_empty());
2649 retainCall->eraseFromParent();
2680 auto GetStoreIfValid = [&CGF](llvm::User *U) -> llvm::StoreInst * {
2681 auto *SI = dyn_cast<llvm::StoreInst>(U);
2686 assert(!SI->isAtomic() && !SI->isVolatile());
2694 llvm::BasicBlock *IP = CGF.
Builder.GetInsertBlock();
2695 if (IP->empty())
return nullptr;
2696 llvm::Instruction *
I = &IP->back();
2699 for (llvm::BasicBlock::reverse_iterator II = IP->rbegin(),
2702 if (llvm::IntrinsicInst *Intrinsic =
2703 dyn_cast<llvm::IntrinsicInst>(&*II)) {
2704 if (Intrinsic->getIntrinsicID() == llvm::Intrinsic::lifetime_end) {
2705 const llvm::Value *CastAddr = Intrinsic->getArgOperand(1);
2709 if (isa<llvm::BitCastInst>(&*II) && (CastAddr == &*II))
2717 return GetStoreIfValid(I);
2720 llvm::StoreInst *store =
2722 if (!store)
return nullptr;
2726 llvm::BasicBlock *StoreBB = store->getParent();
2727 llvm::BasicBlock *IP = CGF.
Builder.GetInsertBlock();
2728 while (IP != StoreBB) {
2729 if (!(IP = IP->getSinglePredecessor()))
2741 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) {
2748 if (!ReturnValue.isValid()) {
2753 llvm::DebugLoc RetDbgLoc;
2759 case ABIArgInfo::InAlloca:
2762 assert(hasAggregateEvaluationKind(RetTy));
2764 llvm::Function::arg_iterator EI = CurFn->arg_end();
2769 RV =
Builder.CreateAlignedLoad(SRet, getPointerAlign(),
"sret");
2773 case ABIArgInfo::Indirect: {
2774 auto AI = CurFn->arg_begin();
2777 switch (getEvaluationKind(RetTy)) {
2780 EmitLoadOfComplex(MakeAddrLValue(ReturnValue, RetTy), EndLoc);
2781 EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(&*AI, RetTy),
2789 EmitStoreOfScalar(
Builder.CreateLoad(ReturnValue),
2790 MakeNaturalAlignAddrLValue(&*AI, RetTy),
2797 case ABIArgInfo::Extend:
2798 case ABIArgInfo::Direct:
2806 if (llvm::StoreInst *SI =
2811 if (EmitRetDbgLoc && !AutoreleaseResult)
2812 RetDbgLoc = SI->getDebugLoc();
2814 RV = SI->getValueOperand();
2815 SI->eraseFromParent();
2818 auto returnValueInst = ReturnValue.getPointer();
2819 if (returnValueInst->use_empty()) {
2820 if (
auto alloca = dyn_cast<llvm::AllocaInst>(returnValueInst)) {
2821 alloca->eraseFromParent();
2822 ReturnValue = Address::invalid();
2828 RV =
Builder.CreateLoad(ReturnValue);
2839 if (AutoreleaseResult) {
2847 if (
auto *FD = dyn_cast<FunctionDecl>(CurCodeDecl))
2848 RT = FD->getReturnType();
2849 else if (
auto *MD = dyn_cast<ObjCMethodDecl>(CurCodeDecl))
2850 RT = MD->getReturnType();
2851 else if (isa<BlockDecl>(CurCodeDecl))
2852 RT = BlockInfo->BlockExpression->getFunctionType()->getReturnType();
2854 llvm_unreachable(
"Unexpected function/method type");
2856 assert(getLangOpts().ObjCAutoRefCount &&
2865 case ABIArgInfo::Ignore:
2868 case ABIArgInfo::CoerceAndExpand: {
2870 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
2874 Address addr =
Builder.CreateElementBitCast(ReturnValue, coercionType);
2875 for (
unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
2876 auto coercedEltType = coercionType->getElementType(i);
2877 if (ABIArgInfo::isPaddingForCoerceAndExpand(coercedEltType))
2880 auto eltAddr =
Builder.CreateStructGEP(addr, i, layout);
2881 auto elt =
Builder.CreateLoad(eltAddr);
2882 results.push_back(elt);
2886 if (results.size() == 1) {
2894 RV = llvm::UndefValue::get(returnType);
2895 for (
unsigned i = 0, e = results.size(); i != e; ++i) {
2896 RV =
Builder.CreateInsertValue(RV, results[i], i);
2902 case ABIArgInfo::Expand:
2903 llvm_unreachable(
"Invalid ABI kind for return argument");
2906 llvm::Instruction *Ret;
2908 EmitReturnValueCheck(RV);
2911 Ret =
Builder.CreateRetVoid();
2915 Ret->setDebugLoc(std::move(RetDbgLoc));
2923 ReturnsNonNullAttr *RetNNAttr =
nullptr;
2924 if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
2925 RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
2927 if (!RetNNAttr && !requiresReturnValueNullabilityCheck())
2935 assert(!requiresReturnValueNullabilityCheck() &&
2936 "Cannot check nullability and the nonnull attribute");
2937 AttrLoc = RetNNAttr->getLocation();
2938 CheckKind = SanitizerKind::ReturnsNonnullAttribute;
2939 Handler = SanitizerHandler::NonnullReturn;
2941 if (
auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
2942 if (
auto *TSI = DD->getTypeSourceInfo())
2944 AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
2945 CheckKind = SanitizerKind::NullabilityReturn;
2946 Handler = SanitizerHandler::NullabilityReturn;
2953 llvm::BasicBlock *Check = createBasicBlock(
"nullcheck");
2954 llvm::BasicBlock *NoCheck = createBasicBlock(
"no.nullcheck");
2957 if (requiresReturnValueNullabilityCheck())
2959 Builder.CreateAnd(CanNullCheck, RetValNullabilityPrecondition);
2960 Builder.CreateCondBr(CanNullCheck, Check, NoCheck);
2965 llvm::Constant *StaticData[] = {EmitCheckSourceLocation(AttrLoc)};
2967 EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, DynamicData);
2973 ReturnLocation = Address::invalid();
2987 llvm::Type *IRPtrTy = IRTy->getPointerTo();
2988 llvm::Value *Placeholder = llvm::UndefValue::get(IRPtrTy->getPointerTo());
2995 return AggValueSlot::forAddr(
Address(Placeholder, Align),
2997 AggValueSlot::IsNotDestructed,
2998 AggValueSlot::DoesNotNeedGCBarriers,
2999 AggValueSlot::IsNotAliased);
3008 Address local = GetAddrOfLocalVar(param);
3013 "cannot emit delegate call arguments for inalloca arguments!");
3018 args.
add(RValue::get(
Builder.CreateLoad(local)), type);
3025 }
else if (getLangOpts().ObjCAutoRefCount &&
3026 param->
hasAttr<NSConsumedAttr>() &&
3030 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(ptr->getType()));
3031 Builder.CreateStore(null, local);
3032 args.
add(RValue::get(ptr), type);
3037 args.
add(convertTempToRValue(local, type, loc), type);
3042 return isa<llvm::ConstantPointerNull>(addr);
3051 "shouldn't have writeback for provably null argument");
3053 llvm::BasicBlock *contBB =
nullptr;
3057 bool provablyNonNull = llvm::isKnownNonNull(srcAddr.
getPointer());
3058 if (!provablyNonNull) {
3064 CGF.
Builder.CreateCondBr(isNull, contBB, writebackBB);
3073 "icr.writeback-cast");
3082 if (writeback.
ToUse) {
3107 if (!provablyNonNull)
3123 for (
const auto &
I : llvm::reverse(Cleanups)) {
3125 I.IsActiveIP->eraseFromParent();
3131 if (uop->getOpcode() == UO_AddrOf)
3132 return uop->getSubExpr();
3162 llvm::PointerType *destType =
3167 args.
add(RValue::get(llvm::ConstantPointerNull::get(destType)),
3186 llvm::ConstantPointerNull::get(
3187 cast<llvm::PointerType>(destType->getElementType()));
3191 llvm::BasicBlock *contBB =
nullptr;
3192 llvm::BasicBlock *originBB =
nullptr;
3197 bool provablyNonNull = llvm::isKnownNonNull(srcAddr.
getPointer());
3198 if (provablyNonNull) {
3204 finalArgument = CGF.
Builder.CreateSelect(isNull,
3205 llvm::ConstantPointerNull::get(destType),
3211 originBB = CGF.
Builder.GetInsertBlock();
3214 CGF.
Builder.CreateCondBr(isNull, contBB, copyBB);
3216 condEval.
begin(CGF);
3246 if (shouldCopy && !provablyNonNull) {
3247 llvm::BasicBlock *copyBB = CGF.
Builder.GetInsertBlock();
3252 llvm::PHINode *phiToUse = CGF.
Builder.CreatePHI(valueToUse->getType(), 2,
3254 phiToUse->addIncoming(valueToUse, copyBB);
3255 phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
3257 valueToUse = phiToUse;
3264 args.
add(RValue::get(finalArgument), CRE->
getType());
3272 StackBase = CGF.
Builder.CreateCall(F, {},
"inalloca.save");
3279 CGF.
Builder.CreateCall(F, StackBase);
3287 if (!AC.
getDecl() || !(SanOpts.has(SanitizerKind::NonnullAttribute) ||
3288 SanOpts.has(SanitizerKind::NullabilityArg)))
3293 unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum;
3296 const NonNullAttr *NNAttr =
nullptr;
3297 if (SanOpts.has(SanitizerKind::NonnullAttribute))
3300 bool CanCheckNullability =
false;
3301 if (SanOpts.has(SanitizerKind::NullabilityArg) && !NNAttr && PVD) {
3302 auto Nullability = PVD->getType()->getNullability(getContext());
3305 PVD->getTypeSourceInfo();
3308 if (!NNAttr && !CanCheckNullability)
3315 AttrLoc = NNAttr->getLocation();
3316 CheckKind = SanitizerKind::NonnullAttribute;
3317 Handler = SanitizerHandler::NonnullArg;
3319 AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
3320 CheckKind = SanitizerKind::NullabilityArg;
3321 Handler = SanitizerHandler::NullabilityArg;
3328 Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
3329 llvm::Constant *StaticData[] = {
3330 EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
3331 llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
3333 EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData,
None);
3336 void CodeGenFunction::EmitCallArgs(
3338 llvm::iterator_range<CallExpr::const_arg_iterator> ArgRange,
3340 assert((
int)ArgTypes.size() == (ArgRange.end() - ArgRange.begin()));
3348 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()
3349 ? Order == EvaluationOrder::ForceLeftToRight
3350 : Order != EvaluationOrder::ForceRightToLeft;
3352 auto MaybeEmitImplicitObjectSize = [&](
unsigned I,
const Expr *Arg,
3360 const auto &Context = getContext();
3363 assert(EmittedArg.getScalarVal() &&
"We emitted nothing for the arg?");
3364 llvm::Value *V = evaluateOrEmitBuiltinObjectSize(Arg, PS->getType(), T,
3365 EmittedArg.getScalarVal());
3366 Args.
add(RValue::get(V), SizeTy);
3370 std::swap(Args.back(), *(&Args.back() - 1));
3374 bool HasInAllocaArgs =
false;
3375 if (CGM.getTarget().getCXXABI().isMicrosoft()) {
3377 I !=
E && !HasInAllocaArgs; ++
I)
3379 if (HasInAllocaArgs) {
3380 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3386 size_t CallArgsStart = Args.size();
3387 for (
unsigned I = 0,
E = ArgTypes.size(); I !=
E; ++
I) {
3388 unsigned Idx = LeftToRight ? I :
E - I - 1;
3390 unsigned InitialArgSize = Args.size();
3393 assert((!isa<ObjCIndirectCopyRestoreExpr>(*Arg) ||
3394 getContext().hasSameUnqualifiedType((*Arg)->getType(),
3396 (isa<ObjCMethodDecl>(AC.
getDecl()) &&
3397 isObjCMethodWithTypeParams(cast<ObjCMethodDecl>(AC.
getDecl())))) &&
3398 "Argument and parameter types don't match");
3399 EmitCallArg(Args, *Arg, ArgTypes[Idx]);
3402 assert(InitialArgSize + 1 == Args.size() &&
3403 "The code below depends on only adding one arg per EmitCallArg");
3404 (void)InitialArgSize;
3405 RValue RVArg = Args.back().RV;
3406 EmitNonNullArgCheck(RVArg, ArgTypes[Idx], (*Arg)->getExprLoc(), AC,
3407 ParamsToSkip + Idx);
3411 MaybeEmitImplicitObjectSize(Idx, *Arg, RVArg);
3417 std::reverse(Args.begin() + CallArgsStart, Args.end());
3425 : Addr(Addr), Ty(Ty) {}
3438 struct DisableDebugLocationUpdates {
3440 bool disabledDebugInfo;
3442 if ((disabledDebugInfo = isa<CXXDefaultArgExpr>(E) && CGF.
getDebugInfo()))
3445 ~DisableDebugLocationUpdates() {
3446 if (disabledDebugInfo)
3455 DisableDebugLocationUpdates Dis(*
this, E);
3457 = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
3458 assert(getLangOpts().ObjCAutoRefCount);
3463 "reference binding to unmaterialized r-value!");
3467 return args.
add(EmitReferenceBindingToExpr(E), type);
3470 bool HasAggregateEvalKind = hasAggregateEvaluationKind(type);
3475 if (HasAggregateEvalKind &&
3476 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
3483 Slot = CreateAggTemp(type,
"agg.tmp");
3486 bool DestroyedInCallee =
3488 CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default;
3489 if (DestroyedInCallee)
3492 EmitAggExpr(E, Slot);
3496 if (DestroyedInCallee) {
3503 llvm::Instruction *IsActive =
Builder.CreateUnreachable();
3509 if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) &&
3510 cast<CastExpr>(
E)->getCastKind() == CK_LValueToRValue) {
3511 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
3513 if (L.
getAlignment() >= getContext().getTypeAlignInChars(type)) {
3518 Address tmp = CreateMemTemp(type);
3520 args.
add(RValue::getAggregate(tmp), type);
3525 args.
add(EmitAnyExprToTemp(E), type);
3528 QualType CodeGenFunction::getVarArgType(
const Expr *Arg) {
3532 if (!getTarget().getTriple().isOSWindows())
3536 getContext().getTypeSize(Arg->
getType()) <
3537 getContext().getTargetInfo().getPointerWidth(0) &&
3540 return getContext().getIntPtrType();
3549 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
3550 if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
3551 !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
3552 Inst->setMetadata(
"clang.arc.no_objc_arc_exceptions",
3553 CGM.getNoObjCARCExceptionsMetadata());
3558 CodeGenFunction::EmitNounwindRuntimeCall(
llvm::Value *callee,
3559 const llvm::Twine &name) {
3560 return EmitNounwindRuntimeCall(callee, None, name);
3565 CodeGenFunction::EmitNounwindRuntimeCall(
llvm::Value *callee,
3567 const llvm::Twine &name) {
3568 llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
3569 call->setDoesNotThrow();
3576 CodeGenFunction::EmitRuntimeCall(
llvm::Value *callee,
3577 const llvm::Twine &name) {
3578 return EmitRuntimeCall(callee, None, name);
3588 if (!CurrentFuncletPad)
3592 auto *CalleeFn = dyn_cast<llvm::Function>(Callee->stripPointerCasts());
3593 if (CalleeFn && CalleeFn->isIntrinsic() && CalleeFn->doesNotThrow())
3596 BundleList.emplace_back(
"funclet", CurrentFuncletPad);
3601 CodeGenFunction::EmitRuntimeCall(
llvm::Value *callee,
3603 const llvm::Twine &name) {
3607 llvm::CallInst *call =
Builder.CreateCall(callee, args, BundleList, name);
3608 call->setCallingConv(getRuntimeCC());
3613 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(
llvm::Value *callee,
3618 if (getInvokeDest()) {
3619 llvm::InvokeInst *invoke =
3621 getUnreachableBlock(),
3625 invoke->setDoesNotReturn();
3626 invoke->setCallingConv(getRuntimeCC());
3628 llvm::CallInst *call =
Builder.CreateCall(callee, args, BundleList);
3629 call->setDoesNotReturn();
3630 call->setCallingConv(getRuntimeCC());
3638 const Twine &name) {
3639 return EmitRuntimeCallOrInvoke(callee,
None, name);
3646 const Twine &name) {
3647 llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
3648 callSite.setCallingConv(getRuntimeCC());
3657 const Twine &Name) {
3658 llvm::BasicBlock *InvokeDest = getInvokeDest();
3662 llvm::Instruction *Inst;
3664 Inst =
Builder.CreateCall(Callee, Args, BundleList, Name);
3666 llvm::BasicBlock *ContBB = createBasicBlock(
"invoke.cont");
3667 Inst =
Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, BundleList,
3674 if (CGM.getLangOpts().ObjCAutoRefCount)
3675 AddObjCARCExceptionMetadata(Inst);
3677 return llvm::CallSite(Inst);
3690 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old,
3692 DeferredReplacements.push_back(std::make_pair(Old, New));
3699 llvm::Instruction **callOrInvoke) {
3715 Address ArgMemory = Address::invalid();
3716 const llvm::StructLayout *ArgMemoryLayout =
nullptr;
3717 if (llvm::StructType *ArgStruct = CallInfo.
getArgStruct()) {
3718 const llvm::DataLayout &DL = CGM.getDataLayout();
3719 ArgMemoryLayout = DL.getStructLayout(ArgStruct);
3721 llvm::AllocaInst *AI;
3723 IP = IP->getNextNode();
3724 AI =
new llvm::AllocaInst(ArgStruct, DL.getAllocaAddrSpace(),
3727 AI = CreateTempAlloca(ArgStruct,
"argmem");
3730 AI->setAlignment(Align.getQuantity());
3731 AI->setUsedWithInAlloca(
true);
3732 assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca());
3733 ArgMemory =
Address(AI, Align);
3737 auto createInAllocaStructGEP = [&](
unsigned FieldIndex) ->
Address {
3740 return Builder.CreateStructGEP(ArgMemory, FieldIndex, FieldOffset);
3743 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo);
3748 Address SRetPtr = Address::invalid();
3749 size_t UnusedReturnSize = 0;
3751 if (!ReturnValue.
isNull()) {
3754 SRetPtr = CreateMemTemp(RetTy);
3755 if (HaveInsertPoint() && ReturnValue.
isUnused()) {
3757 CGM.getDataLayout().getTypeAllocSize(ConvertTypeForMem(RetTy));
3758 if (EmitLifetimeStart(size, SRetPtr.getPointer()))
3759 UnusedReturnSize = size;
3762 if (IRFunctionArgs.hasSRetArg()) {
3763 IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr.getPointer();
3766 Builder.CreateStore(SRetPtr.getPointer(), Addr);
3770 Address swiftErrorTemp = Address::invalid();
3771 Address swiftErrorArg = Address::invalid();
3774 assert(CallInfo.
arg_size() == CallArgs.size() &&
3775 "Mismatch between function signature & arguments.");
3778 for (CallArgList::const_iterator
I = CallArgs.begin(), E = CallArgs.end();
3779 I !=
E; ++
I, ++info_it, ++ArgNo) {
3784 if (IRFunctionArgs.hasPaddingArg(ArgNo))
3785 IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] =
3788 unsigned FirstIRArg, NumIRArgs;
3789 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo);
3792 case ABIArgInfo::InAlloca: {
3793 assert(NumIRArgs == 0);
3794 assert(getTarget().getTriple().getArch() == llvm::Triple::x86);
3797 llvm::Instruction *Placeholder =
3799 CGBuilderTy::InsertPoint IP =
Builder.saveIP();
3800 Builder.SetInsertPoint(Placeholder);
3803 deferPlaceholderReplacement(Placeholder, Addr.
getPointer());
3807 unsigned AS = Addr.
getType()->getPointerAddressSpace();
3808 llvm::Type *MemType = ConvertTypeForMem(
I->Ty)->getPointerTo(AS);
3812 if (Addr.
getType() != MemType)
3813 Addr =
Builder.CreateBitCast(Addr, MemType);
3814 LValue argLV = MakeAddrLValue(Addr,
I->Ty);
3820 case ABIArgInfo::Indirect: {
3821 assert(NumIRArgs == 1);
3825 "indirect-arg-temp",
false);
3828 LValue argLV = MakeAddrLValue(Addr,
I->Ty);
3841 const llvm::DataLayout *TD = &CGM.getDataLayout();
3842 const unsigned RVAddrSpace = Addr.
getType()->getAddressSpace();
3843 const unsigned ArgAddrSpace =
3844 (FirstIRArg < IRFuncTy->getNumParams()
3845 ? IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace()
3849 llvm::getOrEnforceKnownAlignment(Addr.
getPointer(),
3855 "byval-temp",
false);
3866 case ABIArgInfo::Ignore:
3867 assert(NumIRArgs == 0);
3870 case ABIArgInfo::Extend:
3871 case ABIArgInfo::Direct: {
3875 assert(NumIRArgs == 1);
3886 assert(!swiftErrorTemp.
isValid() &&
"multiple swifterror args");
3888 QualType pointeeTy =
I->Ty->getPointeeType();
3890 Address(V, getContext().getTypeAlignInChars(pointeeTy));
3893 CreateMemTemp(pointeeTy, getPointerAlign(),
"swifterror.temp");
3895 cast<llvm::AllocaInst>(V)->setSwiftError(
true);
3898 Builder.CreateStore(errorValue, swiftErrorTemp);
3903 V->getType()->isIntegerTy())
3908 if (FirstIRArg < IRFuncTy->getNumParams() &&
3909 V->getType() != IRFuncTy->getParamType(FirstIRArg))
3910 V =
Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
3912 IRCallArgs[FirstIRArg] = V;
3917 Address Src = Address::invalid();
3919 Src = CreateMemTemp(
I->Ty,
"coerce");
3920 LValue SrcLV = MakeAddrLValue(Src,
I->Ty);
3931 llvm::StructType *STy =
3934 llvm::Type *SrcTy = Src.
getType()->getElementType();
3935 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
3936 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
3942 if (SrcSize < DstSize) {
3946 Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
3949 Src =
Builder.CreateBitCast(Src, llvm::PointerType::getUnqual(STy));
3952 auto SrcLayout = CGM.getDataLayout().getStructLayout(STy);
3953 assert(NumIRArgs == STy->getNumElements());
3954 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3958 IRCallArgs[FirstIRArg + i] = LI;
3962 assert(NumIRArgs == 1);
3963 IRCallArgs[FirstIRArg] =
3970 case ABIArgInfo::CoerceAndExpand: {
3972 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
3975 Address addr = Address::invalid();
3982 auto scalarSize = CGM.getDataLayout().getTypeAllocSize(scalarType);
3983 auto scalarAlign = CGM.getDataLayout().getPrefTypeAlignment(scalarType);
3985 tempSize = llvm::ConstantInt::get(CGM.Int64Ty, scalarSize);
3991 EmitLifetimeStart(scalarSize, addr.
getPointer());
3996 addr =
Builder.CreateElementBitCast(addr, coercionType);
3998 unsigned IRArgPos = FirstIRArg;
3999 for (
unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4000 llvm::Type *eltType = coercionType->getElementType(i);
4001 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
continue;
4004 IRCallArgs[IRArgPos++] = elt;
4006 assert(IRArgPos == FirstIRArg + NumIRArgs);
4009 EmitLifetimeEnd(tempSize, addr.
getPointer());
4015 case ABIArgInfo::Expand:
4016 unsigned IRArgPos = FirstIRArg;
4017 ExpandTypeToArgs(
I->Ty, RV, IRFuncTy, IRCallArgs, IRArgPos);
4018 assert(IRArgPos == FirstIRArg + NumIRArgs);
4033 unsigned CalleeAS = CalleePtr->getType()->getPointerAddressSpace();
4034 auto FnTy = getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS);
4035 CalleePtr =
Builder.CreateBitCast(CalleePtr, FnTy);
4037 llvm::Type *LastParamTy =
4038 IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1);
4039 if (Arg->getType() != LastParamTy) {
4043 llvm::StructType *DeclaredTy = cast<llvm::StructType>(
4044 cast<llvm::PointerType>(LastParamTy)->getElementType());
4045 assert(DeclaredTy->getNumElements() == FullTy->getNumElements());
4046 for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(),
4047 DE = DeclaredTy->element_end(),
4048 FI = FullTy->element_begin();
4049 DI != DE; ++DI, ++FI)
4052 Arg =
Builder.CreateBitCast(Arg, LastParamTy);
4055 assert(IRFunctionArgs.hasInallocaArg());
4056 IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg;
4068 llvm::FunctionType *CalleeFT =
4069 cast<llvm::FunctionType>(Ptr->getType()->getPointerElementType());
4070 if (!CalleeFT->isVarArg())
4073 llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Ptr);
4074 if (!CE || CE->getOpcode() != llvm::Instruction::BitCast)
4077 llvm::Function *OrigFn = dyn_cast<llvm::Function>(CE->getOperand(0));
4081 llvm::FunctionType *OrigFT = OrigFn->getFunctionType();
4085 if (OrigFT->isVarArg() ||
4086 OrigFT->getNumParams() != CalleeFT->getNumParams() ||
4087 OrigFT->getReturnType() != CalleeFT->getReturnType())
4090 for (
unsigned i = 0, e = OrigFT->getNumParams(); i != e; ++i)
4091 if (OrigFT->getParamType(i) != CalleeFT->getParamType(i))
4096 CalleePtr = simplifyVariadicCallee(CalleePtr);
4110 assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg());
4111 for (
unsigned i = 0; i < IRCallArgs.size(); ++i) {
4113 if (IRFunctionArgs.hasInallocaArg() &&
4114 i == IRFunctionArgs.getInallocaArgNo())
4116 if (i < IRFuncTy->getNumParams())
4117 assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i));
4123 llvm::AttributeList Attrs;
4124 CGM.ConstructAttributeList(CalleePtr->getName(), CallInfo,
4133 if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() &&
4137 Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
4138 llvm::Attribute::AlwaysInline);
4142 if (isSEHTryScope()) {
4144 Attrs.addAttribute(getLLVMContext(), llvm::AttributeList::FunctionIndex,
4145 llvm::Attribute::NoInline);
4150 if (currentFunctionUsesSEHTry()) {
4152 CannotThrow =
false;
4153 }
else if (isCleanupPadScope() &&
4154 EHPersonality::get(*this).isMSVCXXPersonality()) {
4161 CannotThrow = Attrs.hasAttribute(llvm::AttributeList::FunctionIndex,
4162 llvm::Attribute::NoUnwind);
4164 llvm::BasicBlock *InvokeDest = CannotThrow ?
nullptr : getInvokeDest();
4172 CS =
Builder.CreateCall(CalleePtr, IRCallArgs, BundleList);
4174 llvm::BasicBlock *Cont = createBasicBlock(
"invoke.cont");
4175 CS =
Builder.CreateInvoke(CalleePtr, Cont, InvokeDest, IRCallArgs,
4179 llvm::Instruction *CI = CS.getInstruction();
4184 CS.setAttributes(Attrs);
4185 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
4189 if (!CI->getType()->isVoidTy())
4190 CI->setName(
"call");
4195 if (!CS.getCalledFunction())
4196 PGO.valueProfile(
Builder, llvm::IPVK_IndirectCallTarget,
4201 if (CGM.getLangOpts().ObjCAutoRefCount)
4202 AddObjCARCExceptionMetadata(CI);
4205 if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
4207 if (TargetDecl && TargetDecl->
hasAttr<NotTailCalledAttr>())
4208 Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
4216 if (CS.doesNotReturn()) {
4217 if (UnusedReturnSize)
4218 EmitLifetimeEnd(llvm::ConstantInt::get(Int64Ty, UnusedReturnSize),
4219 SRetPtr.getPointer());
4222 Builder.ClearInsertionPoint();
4227 EnsureInsertPoint();
4230 return GetUndefRValue(RetTy);
4234 if (swiftErrorTemp.
isValid()) {
4236 Builder.CreateStore(errorResult, swiftErrorArg);
4251 case ABIArgInfo::CoerceAndExpand: {
4253 auto layout = CGM.getDataLayout().getStructLayout(coercionType);
4256 addr =
Builder.CreateElementBitCast(addr, coercionType);
4259 bool requiresExtract = isa<llvm::StructType>(CI->getType());
4261 unsigned unpaddedIndex = 0;
4262 for (
unsigned i = 0, e = coercionType->getNumElements(); i != e; ++i) {
4263 llvm::Type *eltType = coercionType->getElementType(i);
4264 if (ABIArgInfo::isPaddingForCoerceAndExpand(eltType))
continue;
4267 if (requiresExtract)
4268 elt =
Builder.CreateExtractValue(elt, unpaddedIndex++);
4270 assert(unpaddedIndex == 0);
4271 Builder.CreateStore(elt, eltAddr);
4277 case ABIArgInfo::InAlloca:
4278 case ABIArgInfo::Indirect: {
4280 if (UnusedReturnSize)
4281 EmitLifetimeEnd(llvm::ConstantInt::get(Int64Ty, UnusedReturnSize),
4282 SRetPtr.getPointer());
4286 case ABIArgInfo::Ignore:
4289 return GetUndefRValue(RetTy);
4291 case ABIArgInfo::Extend:
4292 case ABIArgInfo::Direct: {
4293 llvm::Type *RetIRTy = ConvertType(RetTy);
4295 switch (getEvaluationKind(RetTy)) {
4299 return RValue::getComplex(std::make_pair(Real, Imag));
4303 bool DestIsVolatile = ReturnValue.
isVolatile();
4306 DestPtr = CreateMemTemp(RetTy,
"agg.tmp");
4307 DestIsVolatile =
false;
4310 return RValue::getAggregate(DestPtr);
4316 if (V->getType() != RetIRTy)
4317 V =
Builder.CreateBitCast(V, RetIRTy);
4318 return RValue::get(V);
4321 llvm_unreachable(
"bad evaluation kind");
4325 bool DestIsVolatile = ReturnValue.
isVolatile();
4328 DestPtr = CreateMemTemp(RetTy,
"coerce");
4329 DestIsVolatile =
false;
4339 case ABIArgInfo::Expand:
4340 llvm_unreachable(
"Invalid ABI kind for return argument");
4343 llvm_unreachable(
"Unhandled ABIArgInfo::Kind");
4348 if (Ret.
isScalar() && TargetDecl) {
4349 if (
const auto *AA = TargetDecl->
getAttr<AssumeAlignedAttr>()) {
4351 if (
const auto *
Offset = AA->getOffset())
4352 OffsetValue = EmitScalarExpr(
Offset);
4354 llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment());
4355 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment);
4356 EmitAlignmentAssumption(Ret.
getScalarVal(), AlignmentCI->getZExtValue(),
4358 }
else if (
const auto *AA = TargetDecl->
getAttr<AllocAlignAttr>()) {
4360 CallArgs[AA->getParamIndex() - 1].RV.getScalarVal();
4376 return CGM.getTypes().getABIInfo().EmitMSVAArg(*
this, VAListAddr, Ty);
4377 return CGM.getTypes().getABIInfo().EmitVAArg(*
this, VAListAddr, Ty);
static CanQual< Type > CreateUnsafe(QualType Other)
Builds a canonical type from a QualType.
ObjCIndirectCopyRestoreExpr - Represents the passing of a function argument by indirect copy-restore ...
ReturnValueSlot - Contains the address where the return value of a function can be stored...
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
void end(CodeGenFunction &CGF)
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
ObjCEntrypoints & getObjCEntrypoints() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
llvm::Type * ConvertTypeForMem(QualType T)
CanQualType getReturnType() const
ExtInfo withCallingConv(CallingConv cc) const
static CanQual< FunctionProtoType > GetFormalType(const CXXMethodDecl *MD)
Returns the canonical formal type of the given C++ method.
CXXCtorType getCtorType() const
unsigned getInAllocaFieldIndex() const
static void emitWriteback(CodeGenFunction &CGF, const CallArgList::Writeback &writeback)
Emit the actual writing-back of a writeback.
static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty, CharUnits MinAlign)
Create a temporary allocation for the purposes of coercion.
static llvm::Value * emitAutoreleaseOfResult(CodeGenFunction &CGF, llvm::Value *result)
Emit an ARC autorelease of the result of a function.
static const CGFunctionInfo & arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, CodeGenModule &CGM, const CallArgList &args, const FunctionType *fnType, unsigned numExtraRequiredArgs, bool chainCall)
Arrange a call as unto a free function, except possibly with an additional number of formal parameter...
CanQual< T > getUnqualifiedType() const
Retrieve the unqualified form of this type.
FunctionType - C99 6.7.5.3 - Function Declarators.
const TargetInfo & getTarget() const
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty, const FunctionDecl *FD)
Arrange the argument and result information for a value of the given freestanding function type...
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
bool isCoerceAndExpand() const
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static bool isProvablyNull(llvm::Value *addr)
Decl - This represents one declaration (or definition), e.g.
Address getAddress() const
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
bool hasFlexibleArrayMember() const
ASTContext & getContext() const
const llvm::DataLayout & getDataLayout() const
bool hasExtParameterInfos() const
Is there any interesting extra information for any of the parameters of this function type...
The base class of the type hierarchy.
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
RValue asAggregateRValue() const
static const NonNullAttr * getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, QualType ArgType, unsigned ArgNo)
Returns the attribute (either parameter attribute, or function attribute), which declares argument Ar...
static int getExpansionSize(QualType Ty, const ASTContext &Context)
bool isBlockPointerType() const
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
CanProxy< U > castAs() const
static llvm::SmallVector< FunctionProtoType::ExtParameterInfo, 16 > getExtParameterInfosForCall(const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
virtual AddedStructorArgs buildStructorSignature(const CXXMethodDecl *MD, StructorType T, SmallVectorImpl< CanQualType > &ArgTys)=0
Build the signature of the given constructor or destructor variant by adding any required parameters...
bool hasUnsignedIntegerRepresentation() const
Determine whether this type has an unsigned integer representation of some sort, e.g., it is an unsigned integer type or a vector.
static llvm::Value * emitArgumentDemotion(CodeGenFunction &CGF, const VarDecl *var, llvm::Value *value)
An argument came in as a promoted argument; demote it back to its declared type.
void freeArgumentMemory(CodeGenFunction &CGF) const
Default closure variant of a ctor.
virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args, const FunctionNoProtoType *fnType) const
Determine whether a call to an unprototyped functions under the given calling convention should use t...
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
VarDecl - An instance of this class is created to represent a variable declaration or definition...
llvm::Type * getElementType() const
Return the type of the values stored in this address.
static void addExtParameterInfosForCall(llvm::SmallVectorImpl< FunctionProtoType::ExtParameterInfo > ¶mInfos, const FunctionProtoType *proto, unsigned prefixArgs, unsigned totalArgs)
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
bool areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change...
static llvm::Value * CreateCoercedLoad(Address Src, llvm::Type *Ty, CodeGenFunction &CGF)
CreateCoercedLoad - Create a load from.
void setCoerceToType(llvm::Type *T)
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
ObjCMethodDecl - Represents an instance or class method declaration.
static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src, LValue Dst)
Store a non-aggregate value to an address to initialize it.
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
bool shouldUseFusedARCCalls()
const CGFunctionInfo & arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, StructorType Type)
ParmVarDecl - Represents a parameter to a function.
bool isObjCRetainableType() const
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant...
unsigned getNumParams() const
RecordDecl - Represents a struct/union/class.
const_arg_iterator arg_end() const
An object to manage conditionally-evaluated expressions.
Description of a constructor that was inherited from a base class.
ExtInfo withProducesResult(bool producesResult) const
static void emitWritebacks(CodeGenFunction &CGF, const CallArgList &args)
ParameterABI getABI() const
Return the ABI treatment of this parameter.
CGDebugInfo * getDebugInfo()
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::Constant * objc_retainAutoreleasedReturnValue
id objc_retainAutoreleasedReturnValue(id);
bool isReferenceType() const
llvm::Value * EmitARCRetainAutoreleaseReturnValue(llvm::Value *value)
Do a fused retain/autorelease of the given object.
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
bool isAnyPointerType() const
StructorType getFromDtorType(CXXDtorType T)
virtual unsigned getOpenCLKernelCallingConv() const
Get LLVM calling convention for OpenCL kernel.
bool getInAllocaSRet() const
Return true if this field of an inalloca struct should be returned to implement a struct return calli...
Interesting information about a specific parameter that can't simply be reflected in parameter's type...
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
llvm::Value * EmitARCAutoreleaseReturnValue(llvm::Value *value)
Autorelease the given object.
static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, const ObjCIndirectCopyRestoreExpr *CRE)
Emit an argument that's being passed call-by-writeback.
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
llvm::StructType * getArgStruct() const
Get the struct type used to represent all the arguments in memory.
llvm::Type * getCoerceToType() const
bool getCanBeFlattened() const
unsigned getRegParm() const
const Decl * getDecl() const
bool shouldCopy() const
shouldCopy - True if we should do the 'copy' part of the copy-restore.
void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *IsActiveIP)
unsigned getEffectiveCallingConvention() const
getEffectiveCallingConvention - Return the actual calling convention to use, which may depend on the ...
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type), llvm::MDNode *TBAAInfo=nullptr, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
CharUnits getArgStructAlignment() const
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment...
const CGFunctionInfo & arrangeFreeFunctionType(CodeGenModule &CGM, CanQual< FunctionProtoType > Ty, const FunctionDecl *FD)
CharUnits getAlignment() const
static llvm::Value * tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, llvm::Value *result)
Try to emit a fused autorelease of a return result.
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
CharUnits - This is an opaque type for sizes expressed in character units.
Qualifiers::ObjCLifetime getObjCLifetime() const
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed...
QualType getReturnType() const
Wrapper for source info for functions.
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
field_range fields() const
const Expr * getSubExpr() const
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static void appendParameterTypes(const CodeGenTypes &CGT, SmallVectorImpl< CanQualType > &prefix, SmallVectorImpl< FunctionProtoType::ExtParameterInfo > ¶mInfos, CanQual< FunctionProtoType > FPT)
Adds the formal parameters in FPT to the given prefix.
bool allowsOptionalArgs() const
Values of this type can never be null.
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
An ordinary object is located at an address in memory.
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
detail::InMemoryDirectory::const_iterator I
llvm::StructType * getCoerceAndExpandType() const
QualType getCanonicalTypeInternal() const
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
void begin(CodeGenFunction &CGF)
static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD)
Derives the 'this' type for codegen purposes, i.e.
const CGFunctionInfo & arrangeObjCMessageSendSignature(CodeGenModule &CGM, const ObjCMethodDecl *MD, QualType receiverType)
Represents a K&R-style 'int foo()' function, which has no information available about its arguments...
unsigned getNumRequiredArgs() const
llvm::Type * getUnpaddedCoerceAndExpandType() const
Const iterator for iterating over Stmt * arrays that contain only Expr *.
ExtInfo getExtInfo() const
CanQualType getCanonicalTypeUnqualified() const
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Represents a prototype with parameter type info, e.g.
ExceptionSpecificationType getExceptionSpecType() const
Get the kind of exception specification on this function.
const TargetCodeGenInfo & getTargetCodeGenInfo()
const Decl * getCalleeDecl() const
writeback_const_range writebacks() const
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
ArrayRef< CallArgCleanup > getCleanupsToDeactivate() const
bool hasConstructorVariants() const
Does this ABI have different entrypoints for complete-object and base-subobject constructors?
void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse)
const FunctionProtoType * getCalleeFunctionProtoType() const
Represents a call to the builtin function __builtin_va_arg.
bool getNoCallerSavedRegs() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Address Temporary
The temporary alloca.
llvm::Value * ToUse
A value to "use" after the writeback, or null.
ArrayRef< llvm::Type * > getCoerceAndExpandTypeSequence() const
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
llvm::Value * getPointer() const
static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty)
Expr - This represents one expression.
CXXDtorType getDtorType() const
static bool isInAllocaArgument(CGCXXABI &ABI, QualType type)
static CanQualType GetReturnType(QualType RetTy)
Returns the "extra-canonicalized" return type, which discards qualifiers on the return type...
const CGCalleeInfo & getAbstractInfo() const
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
Represents a C++ destructor within a class.
llvm::Constant * objc_retain
id objc_retain(id);
CharUnits getIndirectAlign() const
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
ImplicitParamDecl * getSelfDecl() const
void add(RValue rvalue, QualType type, bool needscopy=false)
static SmallVector< CanQualType, 16 > getArgTypesForCall(ASTContext &ctx, const CallArgList &args)
llvm::Value * getFunctionPointer() const
static void eraseUnusedBitCasts(llvm::Instruction *insn)
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type), llvm::MDNode *TBAAInfo=nullptr, bool isInit=false, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
char __ovld __cnfn min(char x, char y)
Returns y if y < x, otherwise it returns x.
A class for recording the number of arguments that a function signature requires. ...
static Address emitAddressAtOffset(CodeGenFunction &CGF, Address addr, const ABIArgInfo &info)
unsigned getNumParams() const
static llvm::StoreInst * findDominatingStoreToReturnValue(CodeGenFunction &CGF)
Heuristically search for a dominating store to the return-value slot.
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
llvm::Type * getPaddingType() const
ARCPreciseLifetime_t isARCPreciseLifetime() const
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
static llvm::Value * CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, llvm::Type *Ty, CodeGenFunction &CGF)
CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both are either integers or p...
CallingConv
CallingConv - Specifies the calling convention that a function uses.
bool hasWritebacks() const
GlobalDecl - represents a global declaration.
static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, Address Dest, bool DestIsVolatile)
ArrayRef< ParmVarDecl * > parameters() const
static void CreateCoercedStore(llvm::Value *Src, Address Dst, bool DstIsVolatile, CodeGenFunction &CGF)
CreateCoercedStore - Create a store to.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Assigning into this object requires the old value to be released and the new value to be retained...
FunctionType::ExtInfo getExtInfo() const
static const Expr * maybeGetUnaryAddrOfOperand(const Expr *E)
ASTContext & getContext() const
RequiredArgs getRequiredArgs() const
Encodes a location in the source.
CharUnits getPointerAlign() const
void EmitARCIntrinsicUse(ArrayRef< llvm::Value * > values)
Given a number of pointers, inform the optimizer that they're being intrinsically used up until this ...
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6.7.5p3.
void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup, llvm::Instruction *DominatingIP)
DeactivateCleanupBlock - Deactivates the given cleanup block.
bool getPaddingInReg() const
Represents a static or instance method of a struct/union/class.
bool isReturnsRetained() const
In ARC, whether this function retains its return value.
void computeABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Compute the ABI information of a swiftcall function.
CanQualType getCanonicalParamType(QualType T) const
Return the canonical parameter type corresponding to the specific potentially non-canonical one...
const CGFunctionInfo & arrangeFreeFunctionCall(CodeGenModule &CGM, CanQualType returnType, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, RequiredArgs args)
void allocateArgumentMemory(CodeGenFunction &CGF)
const ConstantArrayType * getAsConstantArrayType(QualType T) const
bool isNothrow(const ASTContext &Ctx, bool ResultIfDependent=false) const
Determine whether this function type has a non-throwing exception specification.
Specifies that a value-dependent expression should be considered to never be a null pointer constant...
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
llvm::InlineAsm * retainAutoreleasedReturnValueMarker
A void(void) inline asm to use to mark that the return value of a call will be immediately retain...
const CodeGenOptions & getCodeGenOpts() const
QualType getReturnType() const
const T * castAs() const
Member-template castAs<specific type>.
All available information about a concrete callee.
static SmallVector< CanQualType, 16 > getArgTypesForDeclaration(ASTContext &ctx, const FunctionArgList &args)
static void AddAttributesFromFunctionProtoType(ASTContext &Ctx, llvm::AttrBuilder &FuncAttrs, const FunctionProtoType *FPT)
ConstructorUsingShadowDecl * getShadowDecl() const
llvm::StringRef getName() const
Return the IR name of the pointer value.
CXXCtorType
C++ constructor types.
void setExternallyDestructed(bool destructed=true)
QualType getObjCSelType() const
Retrieve the type that corresponds to the predefined Objective-C 'SEL' type.
static Address EnterStructPointerForCoercedAccess(Address SrcPtr, llvm::StructType *SrcSTy, uint64_t DstSize, CodeGenFunction &CGF)
EnterStructPointerForCoercedAccess - Given a struct pointer that we are accessing some number of byte...
bool isSRetAfterThis() const
FunctionArgList - Type for representing both the decl and type of parameters to a function...
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits getAlignment() const
Return the alignment of this pointer.
This class organizes the cross-function state that is used while generating LLVM code.
bool canHaveCoerceToType() const
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
llvm::Value * getAggregatePointer() const
bool isZero() const
isZero - Test whether the quantity equals zero.
unsigned getDirectOffset() const
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
CodeGenFunction::ComplexPairTy ComplexPairTy
bool isInstanceMethod() const
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
CXXDtorType toCXXDtorType(StructorType T)
LValue Source
The original argument.
bool getProducesResult() const
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
static void forConstantArrayExpansion(CodeGenFunction &CGF, ConstantArrayExpansion *CAE, Address BaseAddr, llvm::function_ref< void(Address)> Fn)
static const CGFunctionInfo & arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod, SmallVectorImpl< CanQualType > &prefix, CanQual< FunctionProtoType > FTP, const FunctionDecl *FD)
Arrange the LLVM function layout for a value of the given function type, on top of any implicit param...
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
llvm::IntegerType * IntPtrTy
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CallingConv getCC() const
SourceLocation getLocStart() const LLVM_READONLY
detail::InMemoryDirectory::const_iterator E
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
static void getBundlesForFunclet(llvm::Value *Callee, llvm::Instruction *CurrentFuncletPad, SmallVectorImpl< llvm::OperandBundleDef > &BundleList)
const Decl * getDecl() const
bool hasFunctionDecl() const
llvm::PointerType * getType() const
Return the type of the pointer value.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Complex values, per C99 6.2.5p11.
StructorType getFromCtorType(CXXCtorType T)
static bool classof(const OMPClause *T)
const T * getAs() const
Member-template getAs<specific type>'.
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
static llvm::Value * tryRemoveRetainOfSelf(CodeGenFunction &CGF, llvm::Value *result)
If this is a +1 of the value of an immutable 'self', remove it.
Implements C++ ABI-specific code generation functions.
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
llvm::PointerType * Int8PtrTy
Address getAddress() const
Base for LValueReferenceType and RValueReferenceType.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
static std::unique_ptr< TypeExpansion > getTypeExpansion(QualType Ty, const ASTContext &Context)
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
const ParmVarDecl * getParamDecl(unsigned I) const
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
ABIArgInfo & getReturnInfo()
Represents a base class of a C++ class.
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Represents a C++ struct/union/class.
BoundNodesTreeBuilder *const Builder
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
virtual const CXXRecordDecl * getThisArgumentTypeForMethod(const CXXMethodDecl *MD)
Get the type of the implicit "this" parameter used by a method.
bool isUsingInAlloca() const
Returns if we're using an inalloca struct to pass arguments in memory.
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
llvm::Type * ConvertType(QualType T)
LValue MakeAddrLValue(Address Addr, QualType T, LValueBaseInfo BaseInfo=LValueBaseInfo(AlignmentSource::Type))
CallingConv getDefaultCallingConvention(bool isVariadic, bool IsCXXMethod) const
Retrieves the default calling convention for the current target.
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const =0
Returns how an argument of the given record type should be passed.
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::Instruction * getStackBase() const
This class is used for builtin types like 'int'.
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Copying closure variant of a ctor.
Defines the clang::TargetInfo interface.
const_arg_iterator arg_begin() const
bool getIndirectByVal() const
static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows)
bool getHasRegParm() const
bool isMicrosoftABI() const
Returns whether this is really a Win64 ABI va_arg expression.
llvm::FunctionType * getFunctionType() const
unsigned getTargetAddressSpace(QualType T) const
static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, const CallArgList &CallArgs)
bool constructsVirtualBase() const
Returns true if the constructed base class is a virtual base class subobject of this declaration's cl...
unsigned arg_size() const
static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype, unsigned additional, const FunctionDecl *FD)
Compute the arguments required by the given formal prototype, given that there may be some additional...
bool isVolatileQualified() const
CXXCtorType toCXXCtorType(StructorType T)
bool getIndirectRealign() const
void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
bool isUnresolvedExceptionSpec(ExceptionSpecificationType ESpecType)
LValue - This represents an lvalue references.
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
An abstract representation of regular/ObjC call/message targets.
Information for lazily generating a cleanup.
Notes how many arguments were added to the beginning (Prefix) and ending (Suffix) of an arg list...
ExtParameterInfo getExtParameterInfo(unsigned argIndex) const
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g., it is an signed integer type or a vector.
CallArgList - Type for representing both the value and type of arguments in a call.
Represents the canonical version of C arrays with a specified constant size.
Abstract information about a function or function prototype.
A class which abstracts out some details necessary for making a call.
This parameter (which must have pointer type) is a Swift indirect result parameter.
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign, ASTContext &Context)
A helper function to get the alignment of a Decl referred to by DeclRefExpr or MemberExpr.
bool isPointerType() const