28 #include "llvm/ADT/Hashing.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/MDBuilder.h"
34 #include "llvm/Support/ConvertUTF.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Transforms/Utils/SanitizerStats.h"
39 using namespace clang;
40 using namespace CodeGen;
47 unsigned addressSpace =
48 cast<llvm::PointerType>(value->getType())->getAddressSpace();
52 destType = llvm::Type::getInt8PtrTy(
getLLVMContext(), addressSpace);
54 if (value->getType() == destType)
return value;
86 assert(isa<llvm::AllocaInst>(Var.
getPointer()));
149 if (!ignoreResult && aggSlot.
isIgnored())
154 llvm_unreachable(
"bad evaluation kind");
195 llvm_unreachable(
"bad evaluation kind");
236 VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
257 llvm_unreachable(
"temporary cannot have dynamic storage duration");
259 llvm_unreachable(
"unknown storage duration");
267 auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
268 if (!ClassDecl->hasTrivialDestructor())
269 ReferenceTemporaryDtor = ClassDecl->getDestructor();
272 if (!ReferenceTemporaryDtor)
279 llvm::Constant *CleanupFn;
280 llvm::Constant *CleanupArg;
283 ReferenceTemporary, E->
getType(),
286 CleanupArg = llvm::Constant::getNullValue(CGF.
Int8PtrTy);
290 CleanupArg = cast<llvm::Constant>(ReferenceTemporary.
getPointer());
305 ReferenceTemporary, E->
getType(),
311 llvm_unreachable(
"temporary cannot have dynamic storage duration");
330 auto *GV =
new llvm::GlobalVariable(
332 llvm::GlobalValue::PrivateLinkage, Init,
".ref.tmp");
334 GV->setAlignment(alignment.getQuantity());
345 llvm_unreachable(
"temporary can't have dynamic storage duration");
347 llvm_unreachable(
"unknown storage duration");
360 if (
auto *Var = dyn_cast<llvm::GlobalVariable>(Object.
getPointer())) {
361 Object =
Address(llvm::ConstantExpr::getBitCast(Var,
372 if (Var->hasInitializer())
381 default: llvm_unreachable(
"expected scalar or aggregate expression");
403 for (
const auto &Ignored : CommaLHSs)
406 if (
const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
407 if (opaque->getType()->isRecordType()) {
408 assert(Adjustments.empty());
415 if (
auto *Var = dyn_cast<llvm::GlobalVariable>(Object.
getPointer())) {
416 Object =
Address(llvm::ConstantExpr::getBitCast(
422 if (!Var->hasInitializer()) {
451 for (
unsigned I = Adjustments.size();
I != 0; --
I) {
453 switch (Adjustment.
Kind) {
466 assert(LV.isSimple() &&
467 "materialized temporary field is not a simple lvalue");
468 Object = LV.getAddress();
508 const llvm::Constant *Elts) {
509 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
516 llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
518 llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
519 llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
520 llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
521 llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
522 return Builder.CreateMul(B1, KMul);
534 CharUnits Alignment,
bool SkipNullCheck) {
541 if (Ptr->getType()->getPointerAddressSpace())
547 llvm::BasicBlock *Done =
nullptr;
551 if ((
SanOpts.
has(SanitizerKind::Null) || AllowNullPointers) &&
556 if (AllowNullPointers) {
561 Builder.CreateCondBr(IsNonNull, Rest, Done);
564 Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null));
581 llvm::ConstantInt::get(
IntPtrTy, Size));
582 Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
585 uint64_t AlignVal = 0;
587 if (SanOpts.has(SanitizerKind::Alignment)) {
588 AlignVal = Alignment.getQuantity();
589 if (!Ty->isIncompleteType() && !AlignVal)
590 AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
596 llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
598 Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
599 Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
603 if (Checks.size() > 0) {
604 llvm::Constant *StaticData[] = {
605 EmitCheckSourceLocation(Loc),
606 EmitCheckTypeDescriptor(Ty),
607 llvm::ConstantInt::get(SizeTy, AlignVal),
608 llvm::ConstantInt::get(Int8Ty, TCK)
610 EmitCheck(Checks,
"type_mismatch", StaticData, Ptr);
622 if (SanOpts.has(SanitizerKind::Vptr) &&
623 (TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
624 TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference ||
625 TCK == TCK_UpcastToVirtualBase) &&
626 RD && RD->hasDefinition() && RD->isDynamicClass()) {
633 llvm::raw_svector_ostream Out(MangledName);
634 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
638 if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType(
640 llvm::hash_code TypeHash = hash_value(Out.str());
643 llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
644 llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
645 Address VPtrAddr(
Builder.CreateBitCast(Ptr, VPtrTy), getPointerAlign());
650 Hash =
Builder.CreateTrunc(Hash, IntPtrTy);
653 const int CacheSize = 128;
654 llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
656 "__ubsan_vptr_type_cache");
658 llvm::ConstantInt::get(IntPtrTy,
662 Builder.CreateAlignedLoad(
Builder.CreateInBoundsGEP(Cache, Indices),
670 llvm::Constant *StaticData[] = {
671 EmitCheckSourceLocation(Loc),
672 EmitCheckTypeDescriptor(Ty),
673 CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
674 llvm::ConstantInt::get(Int8Ty, TCK)
677 EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr),
678 "dynamic_type_cache_miss", StaticData, DynamicData);
694 if (
const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
695 if (CAT->getSize().ugt(1))
697 }
else if (!isa<IncompleteArrayType>(AT))
703 if (
const auto *ME = dyn_cast<MemberExpr>(E)) {
706 if (
const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
709 return ++FI == FD->getParent()->field_end();
723 return CGF.
Builder.getInt32(VT->getNumElements());
728 if (
const auto *CE = dyn_cast<CastExpr>(Base)) {
729 if (CE->getCastKind() == CK_ArrayToPointerDecay &&
731 IndexedType = CE->getSubExpr()->getType();
733 if (
const auto *CAT = dyn_cast<ConstantArrayType>(AT))
734 return CGF.
Builder.getInt(CAT->getSize());
735 else if (
const auto *VAT = dyn_cast<VariableArrayType>(AT))
746 assert(SanOpts.has(SanitizerKind::ArrayBounds) &&
747 "should not be called unless adding bounds checks");
759 llvm::Constant *StaticData[] = {
761 EmitCheckTypeDescriptor(IndexedType),
762 EmitCheckTypeDescriptor(IndexType)
765 :
Builder.CreateICmpULE(IndexVal, BoundVal);
766 EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds),
"out_of_bounds",
773 bool isInc,
bool isPre) {
777 if (isa<llvm::IntegerType>(InVal.first->getType())) {
778 uint64_t AmountVal = isInc ? 1 : -1;
779 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal,
true);
782 NextVal =
Builder.CreateAdd(InVal.first, NextVal, isInc ?
"inc" :
"dec");
785 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
788 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
791 NextVal =
Builder.CreateFAdd(InVal.first, NextVal, isInc ?
"inc" :
"dec");
797 EmitStoreOfComplex(IncVal, LV,
false);
801 return isPre ? IncVal : InVal;
811 DI->EmitExplicitCastType(E->
getType());
828 if (
const CastExpr *CE = dyn_cast<CastExpr>(E)) {
829 if (
const auto *ECE = dyn_cast<ExplicitCastExpr>(CE))
830 CGM.EmitExplicitCastExprType(ECE,
this);
832 switch (CE->getCastKind()) {
836 if (
auto PtrTy = CE->getSubExpr()->getType()->getAs<
PointerType>()) {
837 if (PtrTy->getPointeeType()->isVoidType())
841 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), &InnerSource);
842 if (Source) *Source = InnerSource;
846 if (isa<ExplicitCastExpr>(CE) &&
847 InnerSource != AlignmentSource::Decl) {
849 getNaturalPointeeTypeAlignment(E->getType(), Source));
852 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) &&
853 CE->getCastKind() == CK_BitCast) {
855 EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.
getPointer(),
857 CodeGenFunction::CFITCK_UnrelatedCast,
861 return Builder.CreateBitCast(Addr, ConvertType(E->getType()));
866 case CK_ArrayToPointerDecay:
867 return EmitArrayToPointerDecay(CE->getSubExpr(), Source);
870 case CK_UncheckedDerivedToBase:
871 case CK_DerivedToBase: {
872 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), Source);
873 auto Derived = CE->getSubExpr()->
getType()->getPointeeCXXRecordDecl();
874 return GetAddressOfBaseClass(Addr, Derived,
875 CE->path_begin(), CE->path_end(),
876 ShouldNullCheckClassCastValue(CE),
889 if (UO->getOpcode() == UO_AddrOf) {
890 LValue LV = EmitLValue(UO->getSubExpr());
899 CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), Source);
900 return Address(EmitScalarExpr(E), Align);
905 return RValue::get(
nullptr);
907 switch (getEvaluationKind(Ty)) {
912 return RValue::getComplex(std::make_pair(U, U));
919 Address DestPtr = CreateMemTemp(Ty,
"undef.agg.tmp");
920 return RValue::getAggregate(DestPtr);
924 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
926 llvm_unreachable(
"bad evaluation kind");
931 ErrorUnsupported(E, Name);
932 return GetUndefRValue(E->
getType());
937 ErrorUnsupported(E, Name);
938 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->
getType()));
945 if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(
E))
946 LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E),
true);
972 switch (E->getStmtClass()) {
973 default:
return EmitUnsupportedLValue(E,
"l-value expression");
975 case Expr::ObjCPropertyRefExprClass:
976 llvm_unreachable(
"cannot emit a property reference directly");
978 case Expr::ObjCSelectorExprClass:
979 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
980 case Expr::ObjCIsaExprClass:
981 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
982 case Expr::BinaryOperatorClass:
983 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
984 case Expr::CompoundAssignOperatorClass: {
987 Ty = AT->getValueType();
989 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
990 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
992 case Expr::CallExprClass:
993 case Expr::CXXMemberCallExprClass:
994 case Expr::CXXOperatorCallExprClass:
995 case Expr::UserDefinedLiteralClass:
996 return EmitCallExprLValue(cast<CallExpr>(E));
997 case Expr::VAArgExprClass:
998 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
999 case Expr::DeclRefExprClass:
1000 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
1001 case Expr::ParenExprClass:
1002 return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
1003 case Expr::GenericSelectionExprClass:
1004 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
1005 case Expr::PredefinedExprClass:
1006 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
1007 case Expr::StringLiteralClass:
1008 return EmitStringLiteralLValue(cast<StringLiteral>(E));
1009 case Expr::ObjCEncodeExprClass:
1010 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
1011 case Expr::PseudoObjectExprClass:
1012 return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
1013 case Expr::InitListExprClass:
1014 return EmitInitListLValue(cast<InitListExpr>(E));
1015 case Expr::CXXTemporaryObjectExprClass:
1016 case Expr::CXXConstructExprClass:
1017 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
1018 case Expr::CXXBindTemporaryExprClass:
1019 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
1020 case Expr::CXXUuidofExprClass:
1021 return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
1022 case Expr::LambdaExprClass:
1023 return EmitLambdaLValue(cast<LambdaExpr>(E));
1025 case Expr::ExprWithCleanupsClass: {
1026 const auto *cleanups = cast<ExprWithCleanups>(
E);
1027 enterFullExpression(cleanups);
1029 return EmitLValue(cleanups->getSubExpr());
1032 case Expr::CXXDefaultArgExprClass:
1033 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
1034 case Expr::CXXDefaultInitExprClass: {
1036 return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr());
1038 case Expr::CXXTypeidExprClass:
1039 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
1041 case Expr::ObjCMessageExprClass:
1042 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
1043 case Expr::ObjCIvarRefExprClass:
1044 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
1045 case Expr::StmtExprClass:
1046 return EmitStmtExprLValue(cast<StmtExpr>(E));
1047 case Expr::UnaryOperatorClass:
1048 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
1049 case Expr::ArraySubscriptExprClass:
1050 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
1051 case Expr::OMPArraySectionExprClass:
1052 return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E));
1053 case Expr::ExtVectorElementExprClass:
1054 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
1055 case Expr::MemberExprClass:
1056 return EmitMemberExpr(cast<MemberExpr>(E));
1057 case Expr::CompoundLiteralExprClass:
1058 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
1059 case Expr::ConditionalOperatorClass:
1060 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
1061 case Expr::BinaryConditionalOperatorClass:
1062 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
1063 case Expr::ChooseExprClass:
1064 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr());
1065 case Expr::OpaqueValueExprClass:
1066 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
1067 case Expr::SubstNonTypeTemplateParmExprClass:
1068 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
1069 case Expr::ImplicitCastExprClass:
1070 case Expr::CStyleCastExprClass:
1071 case Expr::CXXFunctionalCastExprClass:
1072 case Expr::CXXStaticCastExprClass:
1073 case Expr::CXXDynamicCastExprClass:
1074 case Expr::CXXReinterpretCastExprClass:
1075 case Expr::CXXConstCastExprClass:
1076 case Expr::ObjCBridgedCastExprClass:
1077 return EmitCastLValue(cast<CastExpr>(E));
1079 case Expr::MaterializeTemporaryExprClass:
1080 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
1092 if (!qs.hasConst() || qs.hasVolatile())
return false;
1096 if (
const auto *RT = dyn_cast<RecordType>(type))
1097 if (
const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
1098 if (RD->hasMutableFields() || !RD->isTrivial())
1119 if (
const auto *ref = dyn_cast<ReferenceType>(type)) {
1140 if (isa<ParmVarDecl>(value)) {
1142 }
else if (
auto *var = dyn_cast<VarDecl>(value)) {
1144 }
else if (isa<EnumConstantDecl>(value)) {
1152 bool resultIsReference;
1158 resultIsReference =
false;
1159 resultType = refExpr->
getType();
1164 resultIsReference =
true;
1165 resultType = value->
getType();
1177 llvm::Constant *C = CGM.EmitConstantValue(result.
Val, resultType,
this);
1181 if (isa<VarDecl>(value)) {
1182 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
1183 EmitDeclRefExprDbgValue(refExpr, C);
1185 assert(isa<EnumConstantDecl>(value));
1186 EmitDeclRefExprDbgValue(refExpr, C);
1190 if (resultIsReference)
1191 return ConstantEmission::forReference(C);
1193 return ConstantEmission::forValue(C);
1210 return ET->getDecl()->getIntegerType()->isBooleanType();
1219 llvm::APInt &Min, llvm::APInt &
End,
1222 bool IsRegularCPlusPlusEnum = CGF.
getLangOpts().CPlusPlus && StrictEnums &&
1225 if (!IsBool && !IsRegularCPlusPlusEnum)
1234 unsigned Bitwidth = LTy->getScalarSizeInBits();
1238 if (NumNegativeBits) {
1239 unsigned NumBits =
std::max(NumNegativeBits, NumPositiveBits + 1);
1240 assert(NumBits <= Bitwidth);
1241 End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
1244 assert(NumPositiveBits <= Bitwidth);
1245 End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
1246 Min = llvm::APInt(Bitwidth, 0);
1252 llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(
QualType Ty) {
1253 llvm::APInt Min,
End;
1255 CGM.getCodeGenOpts().StrictEnums))
1258 llvm::MDBuilder MDHelper(getLLVMContext());
1259 return MDHelper.createRange(Min, End);
1266 llvm::MDNode *TBAAInfo,
1268 uint64_t TBAAOffset,
1269 bool isNontemporal) {
1274 const auto *VTy = cast<llvm::VectorType>(EltTy);
1277 if (VTy->getNumElements() == 3) {
1280 llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(),
1282 Address Cast =
Builder.CreateElementBitCast(Addr, vec4Ty,
"castToVec4");
1287 V =
Builder.CreateShuffleVector(V, llvm::UndefValue::get(vec4Ty),
1288 {0, 1, 2},
"extractVec");
1289 return EmitFromMemory(V, Ty);
1295 LValue::MakeAddr(Addr, Ty, getContext(), AlignSource, TBAAInfo);
1296 if (Ty->
isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) {
1297 return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal();
1300 llvm::LoadInst *Load =
Builder.CreateLoad(Addr, Volatile);
1301 if (isNontemporal) {
1302 llvm::MDNode *
Node = llvm::MDNode::get(
1303 Load->getContext(), llvm::ConstantAsMetadata::get(
Builder.getInt32(1)));
1304 Load->setMetadata(CGM.getModule().getMDKindID(
"nontemporal"),
Node);
1307 llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1310 CGM.DecorateInstructionWithTBAA(Load, TBAAPath,
1314 bool NeedsBoolCheck =
1316 bool NeedsEnumCheck =
1318 if (NeedsBoolCheck || NeedsEnumCheck) {
1320 llvm::APInt Min,
End;
1325 Check =
Builder.CreateICmpULE(
1326 Load, llvm::ConstantInt::get(getLLVMContext(), End));
1329 Load, llvm::ConstantInt::get(getLLVMContext(), End));
1331 Load, llvm::ConstantInt::get(getLLVMContext(), Min));
1332 Check =
Builder.CreateAnd(Upper, Lower);
1334 llvm::Constant *StaticArgs[] = {
1335 EmitCheckSourceLocation(Loc),
1336 EmitCheckTypeDescriptor(Ty)
1338 SanitizerMask Kind = NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool;
1339 EmitCheck(std::make_pair(Check, Kind),
"load_invalid_value", StaticArgs,
1340 EmitCheckValue(Load));
1342 }
else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
1343 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
1344 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
1346 return EmitFromMemory(Load, Ty);
1354 if (Value->getType()->isIntegerTy(1))
1355 return Builder.CreateZExt(Value, ConvertTypeForMem(Ty),
"frombool");
1356 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1357 "wrong value rep of bool");
1366 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
1367 "wrong value rep of bool");
1368 return Builder.CreateTrunc(Value,
Builder.getInt1Ty(),
"tobool");
1377 llvm::MDNode *TBAAInfo,
1378 bool isInit,
QualType TBAABaseType,
1379 uint64_t TBAAOffset,
1380 bool isNontemporal) {
1384 llvm::Type *SrcTy = Value->getType();
1385 auto *VecTy = cast<llvm::VectorType>(SrcTy);
1387 if (VecTy->getNumElements() == 3) {
1389 llvm::Constant *Mask[] = {
Builder.getInt32(0),
Builder.getInt32(1),
1391 llvm::UndefValue::get(
Builder.getInt32Ty())};
1392 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1393 Value =
Builder.CreateShuffleVector(Value,
1394 llvm::UndefValue::get(VecTy),
1395 MaskV,
"extractVec");
1396 SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4);
1399 Addr =
Builder.CreateElementBitCast(Addr, SrcTy,
"storetmp");
1403 Value = EmitToMemory(Value, Ty);
1406 LValue::MakeAddr(Addr, Ty, getContext(), AlignSource, TBAAInfo);
1408 (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) {
1409 EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit);
1413 llvm::StoreInst *
Store =
Builder.CreateStore(Value, Addr, Volatile);
1414 if (isNontemporal) {
1415 llvm::MDNode *
Node =
1416 llvm::MDNode::get(Store->getContext(),
1417 llvm::ConstantAsMetadata::get(
Builder.getInt32(1)));
1418 Store->setMetadata(CGM.getModule().getMDKindID(
"nontemporal"),
Node);
1421 llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
1424 CGM.DecorateInstructionWithTBAA(Store, TBAAPath,
1444 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*
this,
1449 if (!getLangOpts().ObjCAutoRefCount) {
1450 return RValue::get(EmitARCLoadWeak(LV.
getAddress()));
1455 Object = EmitObjCConsumeObject(LV.
getType(), Object);
1456 return RValue::get(Object);
1463 return RValue::get(EmitLoadOfScalar(LV, Loc));
1476 return EmitLoadOfExtVectorElementLValue(LV);
1480 return EmitLoadOfGlobalRegLValue(LV);
1482 assert(LV.
isBitField() &&
"Unknown LValue type!");
1483 return EmitLoadOfBitfieldLValue(LV);
1490 llvm::Type *ResLTy = ConvertType(LV.
getType());
1499 Val =
Builder.CreateShl(Val, HighBits,
"bf.shl");
1500 if (Info.
Offset + HighBits)
1501 Val =
Builder.CreateAShr(Val, Info.
Offset + HighBits,
"bf.ashr");
1512 return RValue::get(Val);
1527 unsigned InIdx = getAccessedFieldNo(0, Elts);
1528 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
1529 return RValue::get(
Builder.CreateExtractElement(Vec, Elt));
1536 for (
unsigned i = 0; i != NumResultElts; ++i)
1537 Mask.push_back(
Builder.getInt32(getAccessedFieldNo(i, Elts)));
1539 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1540 Vec =
Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
1542 return RValue::get(Vec);
1550 llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
1552 Address CastToPointerElement =
1553 Builder.CreateElementBitCast(VectorAddress, VectorElementTy,
1554 "conv.ptr.element");
1557 unsigned ix = getAccessedFieldNo(0, Elts);
1560 Builder.CreateConstInBoundsGEP(CastToPointerElement, ix,
1561 getContext().getTypeSizeInChars(EQT),
1564 return VectorBasePtrPlusIx;
1570 "Bad type for register variable");
1571 llvm::MDNode *RegName = cast<llvm::MDNode>(
1572 cast<llvm::MetadataAsValue>(LV.
getGlobalReg())->getMetadata());
1575 llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.
getType());
1576 llvm::Type *Ty = OrigTy;
1577 if (OrigTy->isPointerTy())
1578 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
1579 llvm::Type *Types[] = { Ty };
1581 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
1583 F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
1584 if (OrigTy->isPointerTy())
1585 Call =
Builder.CreateIntToPtr(Call, OrigTy);
1586 return RValue::get(Call);
1610 return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
1613 return EmitStoreThroughGlobalRegLValue(Src, Dst);
1615 assert(Dst.
isBitField() &&
"Unknown LValue type");
1616 return EmitStoreThroughBitfieldLValue(Src, Dst);
1623 llvm_unreachable(
"present but none");
1638 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.
getType(),
1649 CGM.getObjCRuntime().EmitObjCWeakAssign(*
this, src, LvalueDst);
1659 llvm::Type *ResultType = IntPtrTy;
1662 RHS =
Builder.CreatePtrToInt(RHS, ResultType,
"sub.ptr.rhs.cast");
1665 "sub.ptr.lhs.cast");
1667 CGM.getObjCRuntime().EmitObjCIvarAssign(*
this, src, dst,
1670 CGM.getObjCRuntime().EmitObjCGlobalAssign(*
this, src, LvalueDst,
1674 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*
this, src, LvalueDst);
1678 assert(Src.
isScalar() &&
"Can't emit an agg store with this method");
1682 void CodeGenFunction::EmitStoreThroughBitfieldLValue(
RValue Src,
LValue Dst,
1685 llvm::Type *ResLTy = ConvertTypeForMem(Dst.
getType());
1692 SrcVal =
Builder.CreateIntCast(SrcVal, Ptr.getElementType(),
1705 SrcVal =
Builder.CreateAnd(SrcVal,
1721 SrcVal =
Builder.CreateOr(Val, SrcVal,
"bf.set");
1723 assert(Info.
Offset == 0);
1738 ResultVal =
Builder.CreateShl(ResultVal, HighBits,
"bf.result.shl");
1739 ResultVal =
Builder.CreateAShr(ResultVal, HighBits,
"bf.result.ashr");
1745 *Result = EmitFromMemory(ResultVal, Dst.
getType());
1749 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(
RValue Src,
1760 unsigned NumSrcElts = VTy->getNumElements();
1761 unsigned NumDstElts = Vec->getType()->getVectorNumElements();
1762 if (NumDstElts == NumSrcElts) {
1767 for (
unsigned i = 0; i != NumSrcElts; ++i)
1768 Mask[getAccessedFieldNo(i, Elts)] =
Builder.getInt32(i);
1770 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1771 Vec =
Builder.CreateShuffleVector(SrcVal,
1772 llvm::UndefValue::get(Vec->getType()),
1774 }
else if (NumDstElts > NumSrcElts) {
1780 for (
unsigned i = 0; i != NumSrcElts; ++i)
1781 ExtMask.push_back(
Builder.getInt32(i));
1782 ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty));
1783 llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
1785 Builder.CreateShuffleVector(SrcVal,
1786 llvm::UndefValue::get(SrcVal->getType()),
1790 for (
unsigned i = 0; i != NumDstElts; ++i)
1791 Mask.push_back(
Builder.getInt32(i));
1796 if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
1800 for (
unsigned i = 0; i != NumSrcElts; ++i)
1801 Mask[getAccessedFieldNo(i, Elts)] =
Builder.getInt32(i+NumDstElts);
1802 llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
1803 Vec =
Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
1806 llvm_unreachable(
"unexpected shorten vector length");
1810 unsigned InIdx = getAccessedFieldNo(0, Elts);
1811 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
1812 Vec =
Builder.CreateInsertElement(Vec, SrcVal, Elt);
1820 void CodeGenFunction::EmitStoreThroughGlobalRegLValue(
RValue Src,
LValue Dst) {
1822 "Bad type for register variable");
1823 llvm::MDNode *RegName = cast<llvm::MDNode>(
1824 cast<llvm::MetadataAsValue>(Dst.
getGlobalReg())->getMetadata());
1825 assert(RegName &&
"Register LValue is not metadata");
1828 llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.
getType());
1829 llvm::Type *Ty = OrigTy;
1830 if (OrigTy->isPointerTy())
1831 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
1832 llvm::Type *Types[] = { Ty };
1834 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
1836 if (OrigTy->isPointerTy())
1837 Value =
Builder.CreatePtrToInt(Value, Ty);
1839 F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value});
1847 bool IsMemberAccess=
false) {
1851 if (isa<ObjCIvarRefExpr>(E)) {
1864 auto *Exp = cast<ObjCIvarRefExpr>(
const_cast<Expr *
>(
E));
1870 if (
const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
1871 if (
const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
1872 if (VD->hasGlobalStorage()) {
1881 if (
const auto *Exp = dyn_cast<UnaryOperator>(E)) {
1886 if (
const auto *Exp = dyn_cast<ParenExpr>(E)) {
1900 if (
const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
1905 if (
const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
1910 if (
const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
1915 if (
const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
1920 if (
const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
1933 if (
const auto *Exp = dyn_cast<MemberExpr>(E)) {
1945 StringRef
Name = StringRef()) {
1946 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
1970 Address Addr = EmitLoadOfReference(RefAddr, RefTy, &Source);
1985 Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &Source);
2006 if (CGF.
getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>())
2052 AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
2053 assert(Asm->getLabel().size() < 64-Name.size() &&
2054 "Register name too big");
2055 Name.append(Asm->getLabel());
2056 llvm::NamedMDNode *M =
2057 CGM.
getModule().getOrInsertNamedMetadata(Name);
2058 if (M->getNumOperands() == 0) {
2061 llvm::Metadata *Ops[] = {Str};
2068 llvm::MetadataAsValue::get(CGM.
getLLVMContext(), M->getOperand(0));
2069 return LValue::MakeGlobalReg(
Address(Ptr, Alignment), VD->
getType());
2076 if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
2079 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
2084 const Expr *Init = VD->getAnyInitializer(VD);
2086 VD->isUsableInConstantExpressions(getContext()) &&
2087 VD->checkInitIsICE() &&
2090 LocalDeclMap.count(VD))) {
2091 llvm::Constant *Val =
2092 CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(),
this);
2093 assert(Val &&
"failed to emit reference constant expression");
2100 return MakeAddrLValue(
Address(Val, Alignment), T, AlignmentSource::Decl);
2105 if (
auto *FD = LambdaCaptureFields.lookup(VD))
2107 else if (CapturedStmtInfo) {
2108 auto it = LocalDeclMap.find(VD);
2109 if (it != LocalDeclMap.end()) {
2111 return EmitLoadOfReferenceLValue(it->second, RefTy);
2113 return MakeAddrLValue(it->second, T);
2117 CapturedStmtInfo->getContextValue());
2118 return MakeAddrLValue(
2120 CapLVal.
getType(), AlignmentSource::Decl);
2123 assert(isa<BlockDecl>(CurCodeDecl));
2124 Address addr = GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>());
2125 return MakeAddrLValue(addr, T, AlignmentSource::Decl);
2132 assert((ND->isUsed(
false) || !isa<VarDecl>(ND) ||
2134 "Should not use decl without marking it used!");
2136 if (ND->hasAttr<WeakRefAttr>()) {
2137 const auto *VD = cast<ValueDecl>(ND);
2139 return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl);
2142 if (
const auto *VD = dyn_cast<VarDecl>(ND)) {
2144 if (VD->hasLinkage() || VD->isStaticDataMember())
2147 Address addr = Address::invalid();
2150 auto iter = LocalDeclMap.find(VD);
2151 if (iter != LocalDeclMap.end()) {
2152 addr = iter->second;
2156 }
else if (VD->isStaticLocal()) {
2157 addr =
Address(CGM.getOrCreateStaticVarDecl(
2158 *VD, CGM.getLLVMLinkageVarDefinition(VD,
false)),
2159 getContext().getDeclAlign(VD));
2163 llvm_unreachable(
"DeclRefExpr for Decl not entered in LocalDeclMap?");
2168 if (getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
2170 *
this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()),
2175 bool isBlockByref = VD->hasAttr<BlocksAttr>();
2177 addr = emitBlockByrefAddress(addr, VD);
2183 LV = EmitLoadOfReferenceLValue(addr, RefTy);
2185 LV = MakeAddrLValue(addr, T, AlignmentSource::Decl);
2188 bool isLocalStorage = VD->hasLocalStorage();
2190 bool NonGCable = isLocalStorage &&
2191 !VD->getType()->isReferenceType() &&
2198 bool isImpreciseLifetime =
2199 (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
2200 if (isImpreciseLifetime)
2206 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
2209 llvm_unreachable(
"Unhandled DeclRefExpr");
2219 default: llvm_unreachable(
"Unknown unary operator lvalue!");
2222 assert(!T.
isNull() &&
"CodeGenFunction::EmitUnaryOpLValue: Illegal type");
2226 LValue LV = MakeAddrLValue(Addr, T, AlignSource);
2233 if (getLangOpts().ObjC1 &&
2242 assert(LV.
isSimple() &&
"real/imag on non-ordinary l-value");
2263 bool isInc = E->
getOpcode() == UO_PreInc;
2266 EmitComplexPrePostIncDec(E, LV, isInc,
true);
2268 EmitScalarPrePostIncDec(E, LV, isInc,
true);
2275 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
2276 E->
getType(), AlignmentSource::Decl);
2280 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
2281 E->
getType(), AlignmentSource::Decl);
2286 assert(SL !=
nullptr &&
"No StringLiteral name in PredefinedExpr");
2287 StringRef FnName = CurFn->getName();
2288 if (FnName.startswith(
"\01"))
2289 FnName = FnName.substr(1);
2290 StringRef NameItems[] = {
2292 std::string GVName = llvm::join(NameItems, NameItems + 2,
".");
2293 if (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)) {
2294 auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str());
2295 return MakeAddrLValue(C, E->
getType(), AlignmentSource::Decl);
2297 auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
2298 return MakeAddrLValue(C, E->
getType(), AlignmentSource::Decl);
2310 llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(
QualType T) {
2312 if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
2315 uint16_t TypeKind = -1;
2320 TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
2324 TypeInfo = getContext().getTypeSize(T);
2335 llvm::Constant *Components[] = {
2337 llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
2339 llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
2341 auto *GV =
new llvm::GlobalVariable(
2342 CGM.getModule(), Descriptor->getType(),
2343 true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
2344 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2345 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
2348 CGM.setTypeDescriptorInMap(T, GV);
2354 llvm::Type *TargetTy = IntPtrTy;
2358 if (V->getType()->isFloatingPointTy()) {
2359 unsigned Bits = V->getType()->getPrimitiveSizeInBits();
2360 if (Bits <= TargetTy->getIntegerBitWidth())
2361 V =
Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
2366 if (V->getType()->isIntegerTy() &&
2367 V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
2368 return Builder.CreateZExt(V, TargetTy);
2371 if (!V->getType()->isPointerTy()) {
2372 Address Ptr = CreateDefaultAlignTempAlloca(V->getType());
2376 return Builder.CreatePtrToInt(V, TargetTy);
2392 PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
2396 int PathComponentsToStrip =
2397 CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip;
2398 if (PathComponentsToStrip < 0) {
2399 assert(PathComponentsToStrip !=
INT_MIN);
2400 int PathComponentsToKeep = -PathComponentsToStrip;
2401 auto I = llvm::sys::path::rbegin(FilenameString);
2402 auto E = llvm::sys::path::rend(FilenameString);
2403 while (
I != E && --PathComponentsToKeep)
2406 FilenameString = FilenameString.substr(
I - E);
2407 }
else if (PathComponentsToStrip > 0) {
2410 while (
I != E && PathComponentsToStrip--)
2417 FilenameString = llvm::sys::path::filename(FilenameString);
2420 auto FilenameGV = CGM.GetAddrOfConstantCString(FilenameString,
".src");
2421 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(
2422 cast<llvm::GlobalVariable>(FilenameGV.getPointer()));
2423 Filename = FilenameGV.getPointer();
2427 Filename = llvm::Constant::getNullValue(Int8PtrTy);
2434 return llvm::ConstantStruct::getAnon(Data);
2451 assert(llvm::countPopulation(Kind) == 1);
2453 case SanitizerKind::Vptr:
2454 return CheckRecoverableKind::AlwaysRecoverable;
2455 case SanitizerKind::Return:
2456 case SanitizerKind::Unreachable:
2459 return CheckRecoverableKind::Recoverable;
2464 llvm::FunctionType *FnType,
2466 StringRef CheckName,
2468 llvm::BasicBlock *ContBB) {
2470 bool NeedsAbortSuffix =
2472 std::string FnName = (
"__ubsan_handle_" + CheckName +
2473 (NeedsAbortSuffix ?
"_abort" :
"")).str();
2475 !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
2477 llvm::AttrBuilder B;
2479 B.addAttribute(llvm::Attribute::NoReturn)
2480 .addAttribute(llvm::Attribute::NoUnwind);
2482 B.addAttribute(llvm::Attribute::UWTable);
2487 llvm::AttributeSet::FunctionIndex, B));
2490 HandlerCall->setDoesNotReturn();
2491 CGF.
Builder.CreateUnreachable();
2497 void CodeGenFunction::EmitCheck(
2498 ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked,
2501 assert(IsSanitizerScope);
2502 assert(Checked.size() > 0);
2507 for (
int i = 0, n = Checked.size(); i < n; ++i) {
2511 CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second)
2513 : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second)
2516 Cond = Cond ?
Builder.CreateAnd(Cond, Check) : Check;
2520 EmitTrapCheck(TrapCond);
2521 if (!FatalCond && !RecoverableCond)
2525 if (FatalCond && RecoverableCond)
2526 JointCond =
Builder.CreateAnd(FatalCond, RecoverableCond);
2528 JointCond = FatalCond ? FatalCond : RecoverableCond;
2532 assert(SanOpts.has(Checked[0].second));
2534 for (
int i = 1, n = Checked.size(); i < n; ++i) {
2536 "All recoverable kinds in a single check must be same!");
2537 assert(SanOpts.has(Checked[i].second));
2541 llvm::BasicBlock *Cont = createBasicBlock(
"cont");
2542 llvm::BasicBlock *Handlers = createBasicBlock(
"handler." + CheckName);
2543 llvm::Instruction *Branch =
Builder.CreateCondBr(JointCond, Cont, Handlers);
2546 llvm::MDBuilder MDHelper(getLLVMContext());
2547 llvm::MDNode *
Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
2548 Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
2549 EmitBlock(Handlers);
2556 Args.reserve(DynamicArgs.size() + 1);
2557 ArgTypes.reserve(DynamicArgs.size() + 1);
2560 if (!StaticArgs.empty()) {
2561 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
2563 new llvm::GlobalVariable(CGM.getModule(), Info->getType(),
false,
2564 llvm::GlobalVariable::PrivateLinkage, Info);
2565 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2566 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
2567 Args.push_back(
Builder.CreateBitCast(InfoPtr, Int8PtrTy));
2568 ArgTypes.push_back(Int8PtrTy);
2571 for (
size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
2572 Args.push_back(EmitCheckValue(DynamicArgs[i]));
2573 ArgTypes.push_back(IntPtrTy);
2576 llvm::FunctionType *FnType =
2577 llvm::FunctionType::get(CGM.VoidTy, ArgTypes,
false);
2579 if (!FatalCond || !RecoverableCond) {
2583 (FatalCond !=
nullptr), Cont);
2587 llvm::BasicBlock *NonFatalHandlerBB =
2588 createBasicBlock(
"non_fatal." + CheckName);
2589 llvm::BasicBlock *FatalHandlerBB = createBasicBlock(
"fatal." + CheckName);
2590 Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
2591 EmitBlock(FatalHandlerBB);
2594 EmitBlock(NonFatalHandlerBB);
2602 void CodeGenFunction::EmitCfiSlowPathCheck(
2605 llvm::BasicBlock *Cont = createBasicBlock(
"cfi.cont");
2607 llvm::BasicBlock *CheckBB = createBasicBlock(
"cfi.slowpath");
2608 llvm::BranchInst *BI =
Builder.CreateCondBr(Cond, Cont, CheckBB);
2610 llvm::MDBuilder MDHelper(getLLVMContext());
2611 llvm::MDNode *
Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
2612 BI->setMetadata(llvm::LLVMContext::MD_prof, Node);
2616 bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind);
2618 llvm::CallInst *CheckCall;
2620 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
2622 new llvm::GlobalVariable(CGM.getModule(), Info->getType(),
false,
2623 llvm::GlobalVariable::PrivateLinkage, Info);
2624 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2625 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
2627 llvm::Constant *SlowPathDiagFn = CGM.getModule().getOrInsertFunction(
2628 "__cfi_slowpath_diag",
2629 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy},
2631 CheckCall =
Builder.CreateCall(
2633 {TypeId, Ptr,
Builder.CreateBitCast(InfoPtr, Int8PtrTy)});
2635 llvm::Constant *SlowPathFn = CGM.getModule().getOrInsertFunction(
2637 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy},
false));
2638 CheckCall =
Builder.CreateCall(SlowPathFn, {TypeId, Ptr});
2641 CheckCall->setDoesNotThrow();
2653 void CodeGenFunction::EmitCfiCheckFail() {
2657 getContext().VoidPtrTy);
2659 getContext().VoidPtrTy);
2660 Args.push_back(&ArgData);
2661 Args.push_back(&ArgAddr);
2664 CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, Args);
2667 llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy},
false),
2668 llvm::GlobalValue::WeakODRLinkage,
"__cfi_check_fail", &CGM.getModule());
2671 StartFunction(
GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args,
2675 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData),
false,
2676 CGM.getContext().VoidPtrTy, ArgData.getLocation());
2678 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr),
false,
2679 CGM.getContext().VoidPtrTy, ArgAddr.getLocation());
2683 Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy));
2684 EmitTrapCheck(DataIsNotNullPtr);
2686 llvm::StructType *SourceLocationTy =
2687 llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty,
nullptr);
2688 llvm::StructType *CfiCheckFailDataTy =
2689 llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy,
nullptr);
2693 Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0,
2695 Address CheckKindAddr(V, getIntAlign());
2698 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
2699 CGM.getLLVMContext(),
2700 llvm::MDString::get(CGM.getLLVMContext(),
"all-vtables"));
2702 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
2703 {Addr, AllVtables}),
2706 const std::pair<int, SanitizerMask> CheckKinds[] = {
2707 {CFITCK_VCall, SanitizerKind::CFIVCall},
2708 {CFITCK_NVCall, SanitizerKind::CFINVCall},
2709 {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast},
2710 {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast},
2711 {CFITCK_ICall, SanitizerKind::CFIICall}};
2714 for (
auto CheckKindMaskPair : CheckKinds) {
2715 int Kind = CheckKindMaskPair.first;
2718 Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind));
2719 if (CGM.getLangOpts().Sanitize.has(Mask))
2720 EmitCheck(std::make_pair(Cond, Mask),
"cfi_check_fail", {},
2721 {Data, Addr, ValidVtable});
2723 EmitTrapCheck(Cond);
2729 CGM.addUsedGlobal(F);
2733 llvm::BasicBlock *Cont = createBasicBlock(
"cont");
2737 if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
2738 TrapBB = createBasicBlock(
"trap");
2739 Builder.CreateCondBr(Checked, Cont, TrapBB);
2741 llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap);
2742 TrapCall->setDoesNotReturn();
2743 TrapCall->setDoesNotThrow();
2746 Builder.CreateCondBr(Checked, Cont, TrapBB);
2753 llvm::CallInst *TrapCall =
Builder.CreateCall(CGM.getIntrinsic(IntrID));
2755 if (!CGM.getCodeGenOpts().TrapFuncName.empty())
2756 TrapCall->addAttribute(llvm::AttributeSet::FunctionIndex,
2758 CGM.getCodeGenOpts().TrapFuncName);
2766 "Array to pointer decay must have array source type!");
2769 LValue LV = EmitLValue(E);
2770 Address Addr = LV.getAddress();
2771 if (AlignSource) *AlignSource = LV.getAlignmentSource();
2775 llvm::Type *NewTy = ConvertType(E->
getType());
2776 Addr =
Builder.CreateElementBitCast(Addr, NewTy);
2781 assert(isa<llvm::ArrayType>(Addr.getElementType()) &&
2782 "Expected pointer to array");
2787 return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType));
2795 if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
2799 const Expr *SubExpr = CE->getSubExpr();
2810 const llvm::Twine &name =
"arrayidx") {
2812 return CGF.
Builder.CreateInBoundsGEP(ptr, indices, name);
2814 return CGF.
Builder.CreateGEP(ptr, indices, name);
2823 if (
auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) {
2824 CharUnits offset = constantIdx->getZExtValue() * eltSize;
2845 const llvm::Twine &name =
"arrayidx") {
2848 for (
auto idx : indices.drop_back())
2849 assert(isa<llvm::ConstantInt>(idx) &&
2850 cast<llvm::ConstantInt>(idx)->isZero());
2866 return Address(eltPtr, eltAlign);
2876 if (SanOpts.has(SanitizerKind::ArrayBounds))
2877 EmitBoundsCheck(E, E->
getBase(), Idx, IdxTy, Accessed);
2882 !isa<ExtVectorElementExpr>(E->
getBase())) {
2885 assert(LHS.
isSimple() &&
"Can only subscript lvalue vectors here!");
2886 return LValue::MakeVectorElt(LHS.
getAddress(), Idx,
2894 if (Idx->getType() != IntPtrTy)
2895 Idx =
Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned,
"idxprom");
2898 if (isa<ExtVectorElementExpr>(E->
getBase())) {
2900 Address Addr = EmitExtVectorElementLValue(LV);
2908 Address Addr = Address::invalid();
2910 getContext().getAsVariableArrayType(E->
getType())) {
2914 Addr = EmitPointerWithAlignment(E->
getBase(), &AlignSource);
2923 if (getLangOpts().isSignedOverflowDefined()) {
2924 Idx =
Builder.CreateMul(Idx, numElements);
2926 Idx =
Builder.CreateNSWMul(Idx, numElements);
2930 !getLangOpts().isSignedOverflowDefined());
2934 CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT);
2936 llvm::ConstantInt::get(Idx->getType(), InterfaceSize.
getQuantity());;
2941 Addr = EmitPointerWithAlignment(E->
getBase(), &AlignSource);
2947 llvm::Type *OrigBaseTy = Addr.
getType();
2948 Addr =
Builder.CreateElementBitCast(Addr, Int8Ty);
2955 Addr =
Address(EltPtr, EltAlign);
2958 Addr =
Builder.CreateBitCast(Addr, OrigBaseTy);
2964 assert(Array->getType()->isArrayType() &&
2965 "Array to pointer decay must have array source type!");
2969 if (
const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
2970 ArrayLV = EmitArraySubscriptExpr(ASE,
true);
2972 ArrayLV = EmitLValue(Array);
2978 !getLangOpts().isSignedOverflowDefined());
2979 AlignSource = ArrayLV.getAlignmentSource();
2982 Addr = EmitPointerWithAlignment(E->
getBase(), &AlignSource);
2984 !getLangOpts().isSignedOverflowDefined());
2987 LValue LV = MakeAddrLValue(Addr, E->
getType(), AlignSource);
2991 if (getLangOpts().ObjC1 &&
3002 bool IsLowerBound) {
3019 "Expected pointer to array");
3034 bool IsLowerBound) {
3042 if (
auto *AT = getContext().getAsArrayType(BaseTy))
3043 ResultExprTy = AT->getElementType();
3053 EmitScalarExpr(LowerBound), IntPtrTy,
3054 LowerBound->getType()->hasSignedIntegerRepresentation());
3056 Idx = llvm::ConstantInt::getNullValue(IntPtrTy);
3061 auto &C = CGM.getContext();
3063 llvm::APSInt ConstLength;
3066 if (
Length->isIntegerConstantExpr(ConstLength, C)) {
3067 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits);
3071 llvm::APSInt ConstLowerBound(PointerWidthInBits,
false);
3072 if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) {
3073 ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits);
3074 LowerBound =
nullptr;
3078 else if (!LowerBound)
3081 if (
Length || LowerBound) {
3082 auto *LowerBoundVal =
3085 EmitScalarExpr(LowerBound), IntPtrTy,
3086 LowerBound->getType()->hasSignedIntegerRepresentation())
3087 : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound);
3091 EmitScalarExpr(
Length), IntPtrTy,
3092 Length->getType()->hasSignedIntegerRepresentation())
3093 : llvm::ConstantInt::get(IntPtrTy, ConstLength);
3094 Idx =
Builder.CreateAdd(LowerBoundVal, LengthVal,
"lb_add_len",
3096 !getLangOpts().isSignedOverflowDefined());
3097 if (
Length && LowerBound) {
3099 Idx, llvm::ConstantInt::get(IntPtrTy, 1),
"idx_sub_1",
3100 false, !getLangOpts().isSignedOverflowDefined());
3103 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound);
3109 if (
auto *VAT = C.getAsVariableArrayType(ArrayTy)) {
3110 Length = VAT->getSizeExpr();
3111 if (
Length->isIntegerConstantExpr(ConstLength, C))
3114 auto *CAT = C.getAsConstantArrayType(ArrayTy);
3115 ConstLength = CAT->getSize();
3118 auto *LengthVal =
Builder.CreateIntCast(
3119 EmitScalarExpr(
Length), IntPtrTy,
3120 Length->getType()->hasSignedIntegerRepresentation());
3122 LengthVal, llvm::ConstantInt::get(IntPtrTy, 1),
"len_sub_1",
3123 false, !getLangOpts().isSignedOverflowDefined());
3125 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits);
3127 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength);
3133 Address EltPtr = Address::invalid();
3135 if (
auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) {
3141 VLA->getElementType(), IsLowerBound);
3149 if (getLangOpts().isSignedOverflowDefined())
3150 Idx =
Builder.CreateMul(Idx, NumElements);
3152 Idx =
Builder.CreateNSWMul(Idx, NumElements);
3154 !getLangOpts().isSignedOverflowDefined());
3160 assert(Array->getType()->isArrayType() &&
3161 "Array to pointer decay must have array source type!");
3165 if (
const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
3166 ArrayLV = EmitArraySubscriptExpr(ASE,
true);
3168 ArrayLV = EmitLValue(Array);
3173 ResultExprTy, !getLangOpts().isSignedOverflowDefined());
3174 AlignSource = ArrayLV.getAlignmentSource();
3177 BaseTy, ResultExprTy, IsLowerBound);
3179 !getLangOpts().isSignedOverflowDefined());
3182 return MakeAddrLValue(EltPtr, ResultExprTy, AlignSource);
3195 Address Ptr = EmitPointerWithAlignment(E->
getBase(), &AlignSource);
3197 Base = MakeAddrLValue(Ptr, PT->getPointeeType(), AlignSource);
3198 Base.getQuals().removeObjCGCAttr();
3203 Base = EmitLValue(E->
getBase());
3207 "Result must be a vector");
3212 Builder.CreateStore(Vec, VecMem);
3214 AlignmentSource::Decl);
3225 llvm::Constant *CV =
3226 llvm::ConstantDataVector::get(getLLVMContext(), Indices);
3230 assert(Base.
isExtVectorElt() &&
"Can only subscript lvalue vec elts here!");
3235 for (
unsigned i = 0, e = Indices.size(); i != e; ++i)
3236 CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
3237 llvm::Constant *CV = llvm::ConstantVector::get(CElts);
3249 Address Addr = EmitPointerWithAlignment(BaseExpr, &AlignSource);
3252 BaseLV = MakeAddrLValue(Addr, PtrTy, AlignSource);
3254 BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
3257 if (
auto *Field = dyn_cast<FieldDecl>(ND)) {
3258 LValue LV = EmitLValueForField(BaseLV, Field);
3263 if (
auto *VD = dyn_cast<VarDecl>(ND))
3266 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
3269 llvm_unreachable(
"Unhandled member declaration!");
3275 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda());
3276 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->
getParent());
3278 getContext().getTagDeclType(Field->
getParent());
3279 LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType);
3280 return EmitLValueForField(LambdaLV, Field);
3300 "LLVM field at index zero had non-zero offset?");
3317 CGM.getTypes().getCGRecordLayout(field->
getParent());
3326 llvm::Type *FieldIntTy =
3327 llvm::Type::getIntNTy(getLLVMContext(), Info.
StorageSize);
3329 Addr =
Builder.CreateElementBitCast(Addr, FieldIntTy);
3333 return LValue::MakeBitfield(Addr, Info, fieldType, fieldAlignSource);
3339 bool mayAlias = rec->hasAttr<MayAliasAttr>();
3343 bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA;
3355 llvm::LoadInst *load =
Builder.CreateLoad(addr,
"ref");
3360 if (CGM.shouldUseTBAA()) {
3363 tbaa = CGM.getTBAAInfo(getContext().CharTy);
3365 tbaa = CGM.getTBAAInfo(type);
3367 CGM.DecorateInstructionWithTBAA(load, tbaa);
3374 getNaturalTypeAlignment(type, &fieldAlignSource,
true);
3375 addr =
Address(load, alignment);
3388 addr =
Builder.CreateElementBitCast(addr,
3389 CGM.getTypes().ConvertTypeForMem(type),
3392 if (field->hasAttr<AnnotateAttr>())
3393 addr = EmitFieldAnnotations(field, addr);
3395 LValue LV = MakeAddrLValue(addr, type, fieldAlignSource);
3399 getContext().getASTRecordLayout(field->
getParent());
3405 getContext().getCharWidth());
3416 LV.
setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
3427 return EmitLValueForField(Base, Field);
3432 llvm::Type *llvmType = ConvertTypeForMem(FieldType);
3437 return MakeAddrLValue(V, FieldType, FieldAlignSource);
3443 return MakeAddrLValue(GlobalPtr, E->
getType(), AlignmentSource::Decl);
3447 EmitVariablyModifiedType(E->
getType());
3449 Address DeclPtr = CreateMemTemp(E->
getType(),
".compoundliteral");
3462 return EmitAggExprToLValue(E);
3465 assert(E->
getNumInits() == 1 &&
"reference init with multiple values");
3466 return EmitLValue(E->
getInit(0));
3473 const Expr *Operand) {
3474 if (
auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->
IgnoreParens())) {
3486 assert(hasAggregateEvaluationKind(expr->
getType()) &&
3487 "Unexpected conditional operator!");
3488 return EmitAggExprToLValue(expr);
3495 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
3497 if (!CondExprBool) std::swap(live, dead);
3499 if (!ContainsLabel(dead)) {
3502 incrementProfileCounter(expr);
3503 return EmitLValue(live);
3507 llvm::BasicBlock *lhsBlock = createBasicBlock(
"cond.true");
3508 llvm::BasicBlock *rhsBlock = createBasicBlock(
"cond.false");
3509 llvm::BasicBlock *contBlock = createBasicBlock(
"cond.end");
3512 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, getProfileCount(expr));
3515 EmitBlock(lhsBlock);
3516 incrementProfileCounter(expr);
3522 if (lhs && !lhs->isSimple())
3523 return EmitUnsupportedLValue(expr,
"conditional operator");
3525 lhsBlock =
Builder.GetInsertBlock();
3530 EmitBlock(rhsBlock);
3535 if (rhs && !rhs->isSimple())
3536 return EmitUnsupportedLValue(expr,
"conditional operator");
3537 rhsBlock =
Builder.GetInsertBlock();
3539 EmitBlock(contBlock);
3542 llvm::PHINode *phi =
Builder.CreatePHI(lhs->getPointer()->getType(),
3544 phi->addIncoming(lhs->getPointer(), lhsBlock);
3545 phi->addIncoming(rhs->getPointer(), rhsBlock);
3546 Address result(phi,
std::min(lhs->getAlignment(), rhs->getAlignment()));
3548 std::max(lhs->getAlignmentSource(), rhs->getAlignmentSource());
3549 return MakeAddrLValue(result, expr->
getType(), alignSource);
3551 assert((lhs || rhs) &&
3552 "both operands of glvalue conditional are throw-expressions?");
3553 return lhs ? *lhs : *rhs;
3568 case CK_ArrayToPointerDecay:
3569 case CK_FunctionToPointerDecay:
3570 case CK_NullToMemberPointer:
3571 case CK_NullToPointer:
3572 case CK_IntegralToPointer:
3573 case CK_PointerToIntegral:
3574 case CK_PointerToBoolean:
3575 case CK_VectorSplat:
3576 case CK_IntegralCast:
3577 case CK_BooleanToSignedIntegral:
3578 case CK_IntegralToBoolean:
3579 case CK_IntegralToFloating:
3580 case CK_FloatingToIntegral:
3581 case CK_FloatingToBoolean:
3582 case CK_FloatingCast:
3583 case CK_FloatingRealToComplex:
3584 case CK_FloatingComplexToReal:
3585 case CK_FloatingComplexToBoolean:
3586 case CK_FloatingComplexCast:
3587 case CK_FloatingComplexToIntegralComplex:
3588 case CK_IntegralRealToComplex:
3589 case CK_IntegralComplexToReal:
3590 case CK_IntegralComplexToBoolean:
3591 case CK_IntegralComplexCast:
3592 case CK_IntegralComplexToFloatingComplex:
3593 case CK_DerivedToBaseMemberPointer:
3594 case CK_BaseToDerivedMemberPointer:
3595 case CK_MemberPointerToBoolean:
3596 case CK_ReinterpretMemberPointer:
3597 case CK_AnyPointerToBlockPointerCast:
3598 case CK_ARCProduceObject:
3599 case CK_ARCConsumeObject:
3600 case CK_ARCReclaimReturnedObject:
3601 case CK_ARCExtendBlockObject:
3602 case CK_CopyAndAutoreleaseBlockObject:
3603 case CK_AddressSpaceConversion:
3604 return EmitUnsupportedLValue(E,
"unexpected cast lvalue");
3607 llvm_unreachable(
"dependent cast kind in IR gen!");
3609 case CK_BuiltinFnToFnPtr:
3610 llvm_unreachable(
"builtin functions are handled elsewhere");
3613 case CK_NonAtomicToAtomic:
3614 case CK_AtomicToNonAtomic:
3615 return EmitAggExprToLValue(E);
3620 const auto *DCE = cast<CXXDynamicCastExpr>(
E);
3621 return MakeNaturalAlignAddrLValue(EmitDynamicCast(V, DCE), E->
getType());
3624 case CK_ConstructorConversion:
3625 case CK_UserDefinedConversion:
3626 case CK_CPointerToObjCPointerCast:
3627 case CK_BlockPointerToObjCPointerCast:
3629 case CK_LValueToRValue:
3632 case CK_UncheckedDerivedToBase:
3633 case CK_DerivedToBase: {
3636 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->
getDecl());
3639 Address This = LV.getAddress();
3646 return MakeAddrLValue(Base, E->
getType(), LV.getAlignmentSource());
3649 return EmitAggExprToLValue(E);
3650 case CK_BaseToDerived: {
3652 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->
getDecl());
3658 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
3664 if (sanitizePerformTypeCheck())
3665 EmitTypeCheck(TCK_DowncastReference, E->
getExprLoc(),
3666 Derived.getPointer(), E->
getType());
3668 if (SanOpts.has(SanitizerKind::CFIDerivedCast))
3669 EmitVTablePtrCheckForCast(E->
getType(), Derived.getPointer(),
3671 CFITCK_DerivedCast, E->getLocStart());
3673 return MakeAddrLValue(Derived, E->
getType(), LV.getAlignmentSource());
3675 case CK_LValueBitCast: {
3677 const auto *CE = cast<ExplicitCastExpr>(
E);
3679 CGM.EmitExplicitCastExprType(CE,
this);
3682 ConvertType(CE->getTypeAsWritten()));
3684 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast))
3685 EmitVTablePtrCheckForCast(E->
getType(), V.getPointer(),
3687 CFITCK_UnrelatedCast, E->getLocStart());
3691 case CK_ObjCObjectLValueCast: {
3697 case CK_ZeroToOCLEvent:
3698 llvm_unreachable(
"NULL to OpenCL event lvalue cast is not valid");
3701 llvm_unreachable(
"Unhandled lvalue cast kind?");
3705 assert(OpaqueValueMappingData::shouldBindAsLValue(e));
3706 return getOpaqueLValueMapping(e);
3713 LValue FieldLV = EmitLValueForField(LV, FD);
3714 switch (getEvaluationKind(FT)) {
3716 return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
3724 return EmitLoadOfLValue(FieldLV, Loc);
3726 llvm_unreachable(
"bad evaluation kind");
3737 return EmitBlockCallExpr(E, ReturnValue);
3739 if (
const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
3740 return EmitCXXMemberCallExpr(CE, ReturnValue);
3742 if (
const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
3743 return EmitCUDAKernelCallExpr(CE, ReturnValue);
3746 if (
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
3747 if (
unsigned builtinID = FD->getBuiltinID())
3748 return EmitBuiltinExpr(FD, builtinID, E, ReturnValue);
3751 if (
const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
3752 if (
const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
3753 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
3755 if (
const auto *PseudoDtor =
3757 QualType DestroyedType = PseudoDtor->getDestroyedType();
3762 Expr *BaseExpr = PseudoDtor->getBase();
3763 Address BaseValue = Address::invalid();
3767 if (PseudoDtor->isArrow()) {
3768 BaseValue = EmitPointerWithAlignment(BaseExpr);
3772 LValue BaseLV = EmitLValue(BaseExpr);
3785 EmitARCRelease(
Builder.CreateLoad(BaseValue,
3786 PseudoDtor->getDestroyedType().isVolatileQualified()),
3791 EmitARCDestroyWeak(BaseValue);
3803 return RValue::get(
nullptr);
3814 EmitIgnoredExpr(E->
getLHS());
3815 EnsureInsertPoint();
3816 return EmitLValue(E->
getRHS());
3821 return EmitPointerToDataMemberBinaryExpr(E);
3823 assert(E->
getOpcode() == BO_Assign &&
"unexpected binary l-value");
3828 switch (getEvaluationKind(E->
getType())) {
3832 return EmitARCStoreStrong(E,
false).first;
3835 return EmitARCStoreAutoreleasing(E).first;
3846 EmitStoreThroughLValue(RV, LV);
3851 return EmitComplexAssignmentLValue(E);
3854 return EmitAggExprToLValue(E);
3856 llvm_unreachable(
"bad evaluation kind");
3860 RValue RV = EmitCallExpr(E);
3864 AlignmentSource::Decl);
3867 "Can't have a scalar return unless the return type is a "
3875 return EmitAggExprToLValue(E);
3880 &&
"binding l-value to type which needs a temporary");
3882 EmitCXXConstructExpr(E, Slot);
3883 return MakeAddrLValue(Slot.getAddress(), E->
getType(),
3884 AlignmentSource::Decl);
3889 return MakeNaturalAlignAddrLValue(EmitCXXTypeidExpr(E), E->
getType());
3893 return Builder.CreateElementBitCast(CGM.GetAddrOfUuidDescriptor(E),
3898 return MakeAddrLValue(EmitCXXUuidofExpr(E), E->
getType(),
3899 AlignmentSource::Decl);
3909 AlignmentSource::Decl);
3915 EmitLambdaExpr(E, Slot);
3917 AlignmentSource::Decl);
3921 RValue RV = EmitObjCMessageExpr(E);
3925 AlignmentSource::Decl);
3928 "Can't have a scalar return unless the return type is a "
3936 CGM.getObjCRuntime().GetAddrOfSelector(*
this, E->
getSelector());
3937 return MakeAddrLValue(V, E->
getType(), AlignmentSource::Decl);
3942 return CGM.getObjCRuntime().EmitIvarOffset(*
this, Interface, Ivar);
3948 unsigned CVRQualifiers) {
3949 return CGM.getObjCRuntime().EmitObjCValueForIvar(*
this, ObjectTy, BaseValue,
3950 Ivar, CVRQualifiers);
3960 BaseValue = EmitScalarExpr(BaseExpr);
3964 LValue BaseLV = EmitLValue(BaseExpr);
3966 ObjectTy = BaseExpr->
getType();
3971 EmitLValueForIvar(ObjectTy, BaseValue, E->
getDecl(),
3979 RValue RV = EmitAnyExprToTemp(E);
3981 AlignmentSource::Decl);
3990 "Call must have function pointer type!");
4001 if (
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
4008 if (TargetDecl->hasAttr<AlwaysInlineAttr>() &&
4009 TargetDecl->hasAttr<TargetAttr>())
4010 checkTargetFeatures(E, FD);
4014 const auto *FnType =
4015 cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
4017 if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) &&
4018 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
4019 if (llvm::Constant *PrefixSig =
4020 CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
4022 llvm::Constant *FTRTTIConst =
4023 CGM.GetAddrOfRTTIDescriptor(
QualType(FnType, 0),
true);
4024 llvm::Type *PrefixStructTyElems[] = {
4025 PrefixSig->getType(),
4026 FTRTTIConst->getType()
4028 llvm::StructType *PrefixStructTy = llvm::StructType::get(
4029 CGM.getLLVMContext(), PrefixStructTyElems,
true);
4032 Callee, llvm::PointerType::getUnqual(PrefixStructTy));
4034 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 0);
4036 Builder.CreateAlignedLoad(CalleeSigPtr, getIntAlign());
4039 llvm::BasicBlock *Cont = createBasicBlock(
"cont");
4040 llvm::BasicBlock *TypeCheck = createBasicBlock(
"typecheck");
4041 Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
4043 EmitBlock(TypeCheck);
4045 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 1);
4047 Builder.CreateAlignedLoad(CalleeRTTIPtr, getPointerAlign());
4049 Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
4050 llvm::Constant *StaticData[] = {
4052 EmitCheckTypeDescriptor(CalleeType)
4054 EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function),
4055 "function_type_mismatch", StaticData, Callee);
4064 if (SanOpts.has(SanitizerKind::CFIICall) &&
4065 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
4067 EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
4069 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(
QualType(FnType, 0));
4070 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
4074 CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedCallee, TypeId});
4076 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
4077 llvm::Constant *StaticData[] = {
4078 llvm::ConstantInt::get(Int8Ty, CFITCK_ICall),
4080 EmitCheckTypeDescriptor(
QualType(FnType, 0)),
4082 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
4083 EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId,
4084 CastedCallee, StaticData);
4086 EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall),
4087 "cfi_check_fail", StaticData,
4088 {CastedCallee, llvm::UndefValue::get(IntPtrTy)});
4094 Args.
add(RValue::get(
Builder.CreateBitCast(Chain, CGM.VoidPtrTy)),
4095 CGM.getContext().VoidPtrTy);
4096 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->
arguments(),
4099 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
4100 Args, FnType, Chain);
4122 if (isa<FunctionNoProtoType>(FnType) || Chain) {
4123 llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
4124 CalleeTy = CalleeTy->getPointerTo();
4125 Callee =
Builder.CreateBitCast(Callee, CalleeTy,
"callee.knr.cast");
4128 return EmitCall(FnInfo, Callee, ReturnValue, Args,
4134 Address BaseAddr = Address::invalid();
4136 BaseAddr = EmitPointerWithAlignment(E->
getLHS());
4138 BaseAddr = EmitLValue(E->
getLHS()).getAddress();
4148 EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT,
4151 return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), AlignSource);
4159 LValue lvalue = MakeAddrLValue(addr, type, AlignmentSource::Decl);
4160 switch (getEvaluationKind(type)) {
4162 return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
4166 return RValue::get(EmitLoadOfScalar(lvalue, loc));
4168 llvm_unreachable(
"bad evaluation kind");
4171 void CodeGenFunction::SetFPAccuracy(
llvm::Value *Val,
float Accuracy) {
4172 assert(Val->getType()->isFPOrFPVectorTy());
4173 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
4176 llvm::MDBuilder MDHelper(getLLVMContext());
4177 llvm::MDNode *
Node = MDHelper.createFPMath(Accuracy);
4179 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
4183 struct LValueOrRValue {
4197 LValueOrRValue result;
4201 const Expr *semantic = *i;
4205 if (
const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
4211 if (ov == resultExpr && ov->
isRValue() && !forLValue &&
4212 CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) {
4216 AlignmentSource::Decl);
4217 opaqueData = OVMA::bind(CGF, ov, LV);
4222 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
4225 if (ov == resultExpr) {
4233 opaques.push_back(opaqueData);
4237 }
else if (semantic == resultExpr) {
4250 for (
unsigned i = 0, e = opaques.size(); i != e; ++i)
4251 opaques[i].unbind(CGF);
unsigned getNumElements() const
unsigned getAddressSpace() const
Return the address space of this type.
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Defines the clang::ASTContext interface.
unsigned getNumInits() const
const Expr * getBase() const
Qualifiers getLocalQualifiers() const
Retrieve the set of qualifiers local to this particular QualType instance, not including any qualifie...
CastKind getCastKind() const
unsigned getVRQualifiers() const
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
void end(CodeGenFunction &CGF)
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
static llvm::Value * emitArraySubscriptGEP(CodeGenFunction &CGF, llvm::Value *ptr, ArrayRef< llvm::Value * > indices, bool inbounds, const llvm::Twine &name="arrayidx")
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
llvm::Value * getPointer() const
CodeGenTypes & getTypes()
unsigned getColumn() const
Return the presumed column number of this location.
llvm::Type * ConvertTypeForMem(QualType T)
Expr * getBaseIvarExp() const
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
bool isBitField() const
Determines whether this field is a bitfield.
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
llvm::Module & getModule() const
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
AlignmentSource
The source of the alignment of an l-value; an expression of confidence in the alignment actually matc...
llvm::LLVMContext & getLLVMContext()
LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
bool isArrow() const
isArrow - Return true if the base expression is a pointer to vector, return false if the base express...
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateTempAlloca - This creates a alloca and inserts it into the entry block.
Expr * GetTemporaryExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue...
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
bool isRecordType() const
Address getAddress() const
static void pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, const Expr *E, Address ReferenceTemporary)
void setTBAAInfo(llvm::MDNode *N)
const CastExpr * BasePath
const llvm::DataLayout & getDataLayout() const
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
static Destroyer destroyARCStrongPrecise
Expr * getLowerBound()
Get lower bound of array section.
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, LValue &LV, bool IsMemberAccess=false)
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e)
RValue asAggregateRValue() const
std::unique_ptr< llvm::MemoryBuffer > Buffer
Represents an array type, per C99 6.7.5.2 - Array Declarators.
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
static llvm::Value * EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, llvm::Value *V, llvm::Type *IRType, StringRef Name=StringRef())
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled.
Represents a call to a C++ constructor.
static LValue EmitThreadPrivateVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, llvm::Type *RealVarTy, SourceLocation Loc)
bool isBooleanType() const
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
StorageDuration
The storage duration for an object (per C++ [basic.stc]).
const LangOptions & getLangOpts() const
bool isBlockPointerType() const
Represents a prvalue temporary that is written into memory so that a reference can bind to it...
IdentType getIdentType() const
unsigned getLLVMFieldNo(const FieldDecl *FD) const
Return llvm::StructType element number that corresponds to the field FD.
static Destroyer destroyARCWeak
void * getAsOpaquePtr() const
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Objects with "hidden" visibility are not seen by the dynamic linker.
llvm::Type * getElementType() const
Return the type of the values stored in this address.
static bool hasBooleanRepresentation(QualType Ty)
CompoundLiteralExpr - [C99 6.5.2.5].
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
const Expr * getCallee() const
TLSKind getTLSKind() const
ObjCLifetime getObjCLifetime() const
void setTBAAOffset(uint64_t O)
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
CharUnits getNaturalTypeAlignment(QualType T, AlignmentSource *Source=nullptr, bool forPointeeType=false)
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
Address getVectorAddress() const
SourceLocation getLocation() const
QualType getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info) const
Return a K&R style C function type like 'int()'.
The collection of all-type qualifiers we support.
RecordDecl - Represents a struct/union/class.
An object to manage conditionally-evaluated expressions.
void setTBAABaseType(QualType T)
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
bool isVolatileQualified() const
Represents a class type in Objective C.
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
bool isReferenceType() const
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
LValue EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, bool IsLowerBound=true)
SourceLocation getExprLoc() const LLVM_READONLY
unsigned getCVRQualifiers() const
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
static void emitCheckHandlerCall(CodeGenFunction &CGF, llvm::FunctionType *FnType, ArrayRef< llvm::Value * > FnArgs, StringRef CheckName, CheckRecoverableKind RecoverKind, bool IsFatal, llvm::BasicBlock *ContBB)
void setBaseIvarExp(Expr *V)
bool hasStrongOrWeakObjCLifetime() const
void InitTempAlloca(Address Alloca, llvm::Value *Value)
InitTempAlloca - Provide an initial value for the given alloca which will be observable at all locati...
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, const FieldDecl *field)
Drill down to the storage of a field without walking into reference types.
static bool isFlexibleArrayMemberExpr(const Expr *E)
Determine whether this expression refers to a flexible array member in a struct.
RValue EmitAnyExprToTemp(const Expr *E)
EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will always be accessible even if ...
const Expr *const * const_semantics_iterator
void setNonGC(bool Value)
Address CreateIRTemp(QualType T, const Twine &Name="tmp")
CreateIRTemp - Create a temporary IR object of the given type, with appropriate alignment.
RValue EmitAnyExpr(const Expr *E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
EmitAnyExpr - Emit code to compute the specified expression which can have any type.
Describes an C or C++ initializer list.
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
unsigned Size
The total size of the bit-field, in bits.
static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, const PseudoObjectExpr *E, bool forLValue, AggValueSlot slot)
CharUnits getAlignment() const
const LangOptions & getLangOpts() const
Expr * getTrueExpr() const
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.
APValue Val
Val - This is the value the expression can be folded to.
const Qualifiers & getQuals() const
const ValueDecl * getExtendingDecl() const
Get the declaration which triggered the lifetime-extension of this temporary, if any.
static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E, const FunctionDecl *FD)
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, llvm::APInt &Min, llvm::APInt &End, bool StrictEnums)
path_iterator path_begin()
void addCVRQualifiers(unsigned mask)
semantics_iterator semantics_end()
A builtin binary operation expression such as "x + y" or "x <= y".
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
RecordDecl * getDecl() const
static AlignmentSource getFieldAlignmentSource(AlignmentSource Source)
Given that the base address has the given alignment source, what's our confidence in the alignment of...
virtual llvm::Value * EmitMemberPointerIsNotNull(CodeGenFunction &CGF, llvm::Value *MemPtr, const MemberPointerType *MPT)
Determine if a member pointer is non-null. Returns an i1.
Scope - A scope is a transient data structure that is used while parsing the program.
enum clang::SubobjectAdjustment::@35 Kind
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
An adjustment to be made to the temporary created when emitting a reference binding, which accesses a particular subobject of that temporary.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
unsigned Offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
Represents binding an expression to a temporary.
CXXTemporary * getTemporary()
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type...
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
unsigned getLine() const
Return the presumed line number of this location.
void setARCPreciseLifetime(ARCPreciseLifetime_t value)
bool isExtVectorElt() const
Represents an ObjC class declaration.
Checking the operand of a cast to a virtual base object.
detail::InMemoryDirectory::const_iterator I
static CharUnits getArrayElementAlign(CharUnits arrayAlign, llvm::Value *idx, CharUnits eltSize)
static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, AlignmentSource &AlignSource, QualType BaseTy, QualType ElTy, bool IsLowerBound)
void begin(CodeGenFunction &CGF)
void setThreadLocalRef(bool Value)
This object can be modified without requiring retains or releases.
LValue EmitLValueForField(LValue Base, const FieldDecl *Field)
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
EnumDecl * getDecl() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource AlignSource=AlignmentSource::Type)
OpenMP 4.0 [2.4, Array Sections].
const ArrayType * castAsArrayTypeUnsafe() const
A variant of castAs<> for array type which silently discards qualifiers from the outermost type...
static CharUnits One()
One - Construct a CharUnits quantity of one.
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Represents a prototype with parameter type info, e.g.
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
void EmitAnyExprToMem(const Expr *E, Address Location, Qualifiers Quals, bool IsInitializer)
EmitAnyExprToMem - Emits the code necessary to evaluate an arbitrary expression into the given memory...
bool isOBJCGCCandidate(ASTContext &Ctx) const
isOBJCGCCandidate - Return true if this expression may be used in a read/ write barrier.
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind)
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, QualType LValType)=0
Emit a reference to a non-local thread_local variable (including triggering the initialization of all...
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
Address getBitFieldAddress() const
Represents a call to the builtin function __builtin_va_arg.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
bool isFunctionPointerType() const
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
const ObjCMethodDecl * getMethodDecl() const
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
static TypeEvaluationKind getEvaluationKind(QualType T)
hasAggregateLLVMType - Return true if the specified AST type will map into an aggregate LLVM type or ...
llvm::Value * getPointer() const
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
llvm::Function * generateDestroyHelper(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray, const VarDecl *VD)
generateDestroyHelper - Generates a helper function which, when invoked, destroys the given object...
Expr - This represents one expression.
CGCXXABI & getCXXABI() const
bool isAnyComplexType() const
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type, where the destination type is an LLVM scalar type.
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
void setObjCArray(bool Value)
bool isAtomicType() const
Represents a C++ destructor within a class.
bool isVariableArrayType() const
static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, llvm::Value *ThisValue)
AggValueSlot CreateAggTemp(QualType T, const Twine &Name="tmp")
CreateAggTemp - Create a temporary memory object for the given aggregate type.
ASTContext & getContext() const
bool isFloatingType() const
ObjCSelectorExpr used for @selector in Objective-C.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
void add(RValue rvalue, QualType type, bool needscopy=false)
static Optional< LValue > EmitLValueOrThrowExpression(CodeGenFunction &CGF, const Expr *Operand)
Emit the operand of a glvalue conditional operator.
char __ovld __cnfn min(char x, char y)
Returns y if y < x, otherwise it returns x.
virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, llvm::Constant *Dtor, llvm::Constant *Addr)=0
Emit code to force the execution of a destructor during global teardown.
Selector getSelector() const
llvm::LLVMContext & getLLVMContext()
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Expr * getSubExpr() const
Address EmitPointerWithAlignment(const Expr *Addr, AlignmentSource *Source=nullptr)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
bool isThreadLocalRef() const
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
An expression that sends a message to the given Objective-C object or class.
Represents an unpacked "presumed" location which can be presented to the user.
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Represents a GCC generic vector type.
llvm::Value * EmitCastToVoidPtr(llvm::Value *value)
Emit a cast to void* in the appropriate address space.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
The scope of a CXXDefaultInitExpr.
QualType getElementType() const
The result type of a method or function.
ConstantEmissionKind
Can we constant-emit a load of a reference to a variable of the given type? This is different from pr...
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
forAddr - Make a slot for an aggregate value.
GlobalDecl - represents a global declaration.
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
Dynamic storage duration.
The l-value was considered opaque, so the alignment was determined from a type.
bool isNontemporal() const
There is no lifetime qualification on this type.
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeSet ExtraAttrs=llvm::AttributeSet())
Create a new runtime function with the specified type and name.
Assigning into this object requires the old value to be released and the new value to be retained...
static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM)
Named Registers are named metadata pointing to the register name which will be read from/written to a...
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
const char * getFilename() const
Return the presumed filename of this location.
llvm::Constant * EmitConstantExpr(const Expr *E, QualType DestType, CodeGenFunction *CGF=nullptr)
Try to emit the given expression as a constant; returns 0 if the expression cannot be emitted as a co...
ASTContext & getContext() const
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushDestroy - Push the standard destructor for the given type as at least a normal cleanup...
Encodes a location in the source.
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, bool IsForDefinition=false)
Return the llvm::Constant for the address of the given global variable.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums...
QualType getElementType() const
QualType withCVRQualifiers(unsigned CVR) const
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
SourceLocation getExprLoc() const LLVM_READONLY
const CGBitFieldInfo & getBitFieldInfo() const
bool isValid() const
Return true if this is a valid SourceLocation object.
llvm::MDNode * getTBAAInfo() const
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr * > VL, ArrayRef< Expr * > PL, ArrayRef< Expr * > IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.
bool refersToEnclosingVariableOrCapture() const
Does this DeclRefExpr refer to an enclosing local or a captured variable?
Checking the operand of a cast to a base object.
A scoped helper to set the current debug location to the specified location or preferred location of ...
llvm::Value * EmitLifetimeStart(uint64_t Size, llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Represents a static or instance method of a struct/union/class.
static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, const Expr *E, const VarDecl *VD)
SanitizerSet SanOpts
Sanitizers enabled for this function.
static QualType getFixedSizeElementType(const ASTContext &ctx, const VariableArrayType *vla)
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
const CodeGenOptions & getCodeGenOpts() const
TypeCheckKind
Situations in which we might emit a check for the suitability of a pointer or glvalue.
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
StringLiteral * getFunctionName()
void setObjCIvar(bool Value)
QualType getReturnType() const
const T * castAs() const
Member-template castAs<specific type>.
unsigned getAddressSpace() const
Return the address space that this address resides in.
bool isVectorType() const
llvm::Constant * getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, bool IsForDefinition=false)
Return the address of the constructor/destructor of the given type.
Assigning into this object requires a lifetime extension.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Address getExtVectorAddress() const
const Expr * getBase() const
llvm::Value * getGlobalReg() const
AlignmentSource getAlignmentSource() const
static Destroyer destroyARCStrongImprecise
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
QualType getPointeeType() const
void setExternallyDestructed(bool destructed=true)
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
decl_iterator - Iterates through the declarations stored within this context.
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
FunctionArgList - Type for representing both the decl and type of parameters to a function...
ast_type_traits::DynTypedNode Node
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
TLS with a dynamic initializer.
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.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
[C99 6.4.2.2] - A predefined identifier such as func.
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
EvalResult is a struct with detailed info about an evaluated expression.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return 0.
Address CreateMemTemp(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignment...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
static AggValueSlot ignored()
ignored - Returns an aggregate value slot indicating that the aggregate value is being ignored...
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
Checking the bound value in a reference binding.
unsigned IsSigned
Whether the bit-field is signed.
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.
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
EnumDecl - Represents an enum.
detail::InMemoryDirectory::const_iterator E
A pointer to member type per C++ 8.3.3 - Pointers to members.
semantics_iterator semantics_begin()
ExplicitCastExpr - An explicit cast written in the source code.
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), bool SkipNullCheck=false)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
const VariableArrayType * getAsVariableArrayType(QualType T) const
static Address createReferenceTemporary(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, const Expr *Inner)
static bool hasAggregateEvaluationKind(QualType T)
llvm::Constant * getExtVectorElts() const
bool HasSideEffects
Whether the evaluated expression has side effects.
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.
unsigned getNumNegativeBits() const
Returns the width in bits required to store all the negative enumerators of this enum.
const T * getAs() const
Member-template getAs<specific type>'.
Checking the operand of a static_cast to a derived pointer type.
Expr * getFalseExpr() const
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
QualType getCanonicalType() const
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
static StringRef getIdentTypeName(IdentType IT)
ObjCEncodeExpr, used for @encode in Objective-C.
virtual bool usesThreadWrapperFunction() const =0
Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, AlignmentSource *AlignSource=nullptr)
Emit the address of a field using a member data pointer.
QualType getIntegerType() const
getIntegerType - Return the integer type this enum decl corresponds to.
llvm::PointerType * Int8PtrTy
SourceLocation getLocStart() const LLVM_READONLY
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
llvm::AssertingVH< llvm::Instruction > AllocaInsertPt
AllocaInsertPoint - This is an instruction in the entry block before which we prefer to insert alloca...
bool isFunctionType() const
QualType getTBAABaseType() const
LValue EmitLoadOfReferenceLValue(Address Ref, const ReferenceType *RefTy)
static llvm::Value * emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, llvm::Value *High)
Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
Address getAddress() const
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
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...
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
void setGlobalObjCRef(bool Value)
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type, returning the result.
const Expr * getInitializer() const
unsigned getFieldIndex() const
getFieldIndex - Returns the index of this field within its record, as appropriate for passing to ASTR...
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
QualType getPointeeType() const
ObjCIvarRefExpr - A reference to an ObjC instance variable.
const Decl * getCalleeDecl()
llvm::Value * getVectorIdx() const
Reading or writing from this object requires a barrier call.
void setCurrentStmt(const Stmt *S)
If the execution count for the current statement is known, record that as the current count...
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
static bool isConstantEmittableObjectType(QualType type)
Given an object of the given canonical type, can we safely copy a value out of it based on its initia...
A non-RAII class containing all the information about a bound opaque value.
unsigned getNumPositiveBits() const
Returns the width in bits required to store all the non-negative enumerators of this enum...
Represents a C++ struct/union/class.
bool isObjCStrong() const
BoundNodesTreeBuilder *const Builder
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset...
bool isObjCObjectPointerType() const
static QualType getBaseOriginalType(const Expr *Base)
Return original type of the base expression for array section.
llvm::Type * ConvertType(QualType T)
static Destroyer destroyCXXObject
A specialization of Address that requires the address to be an LLVM Constant.
ObjCIvarDecl - Represents an ObjC instance variable.
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
static llvm::Value * getArrayIndexingBound(CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType)
If Base is known to point to the start of an array, return the length of that array.
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
std::pair< llvm::Value *, QualType > getVLASize(const VariableArrayType *vla)
getVLASize - Returns an LLVM value that corresponds to the size, in non-variably-sized elements...
StringLiteral - This represents a string literal expression, e.g.
Full-expression storage duration (for temporaries).
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
const MemberPointerType * MPT
llvm::Value * EmitScalarConversion(llvm::Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified type to the specified destination type, both of which are LLVM s...
A reference to a declared variable, function, enum, etc.
static RValue get(llvm::Value *V)
static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type)
QualType getElementType() const
SourceLocation getColonLoc() const
const Expr * getInit(unsigned Init) const
const Expr * getSubExpr() const
CodeGenTypes & getTypes() const
LValue - This represents an lvalue references.
bool isGlobalObjCRef() const
NamedDecl - This represents a decl with a name.
Represents a C array with a specified size that is not an integer-constant-expression.
bool isArithmeticType() const
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Automatic storage duration (most local variables).
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], or an enum decl which has a signed representation.
bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const
EvaluateAsLValue - Evaluate an expression to see if we can fold it to an lvalue with link time known ...
bool isNull() const
Return true if this QualType doesn't point to a type yet.
const CXXRecordDecl * DerivedClass
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, bool IsForDefinition=false)
Return the address of the given function.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
Address GetAddressOfBaseClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc)
GetAddressOfBaseClass - This function will add the necessary delta to the load of 'this' and returns ...
CallArgList - Type for representing both the value and type of arguments in a call.
uint64_t getTBAAOffset() const
CGCalleeInfo - Class to encapsulate the information about a callee to be used during the generation o...
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
Expr * getLength()
Get length of array section.
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Structure with information about how a bitfield should be accessed.
const RecordDecl * getParent() const
getParent - Returns the parent of this field declaration, which is the struct in which this method is...
CheckRecoverableKind
Specify under what conditions this check can be recovered.
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
Expr * getBase()
An array section can be written only as Base[LowerBound:Length].
bool isPointerType() const
static const Expr * isSimpleArrayDecayOperand(const Expr *E)
isSimpleArrayDecayOperand - If the specified expr is a simple decay from an array to pointer...
static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts)
getAccessedFieldNo - Given an encoded value and a result number, return the input field number being ...