47#define DEBUG_TYPE "nsan"
50 "Number of instrumented floating-point loads");
53 "Number of instrumented floating-point calls");
55 "Number of instrumented floating-point returns");
57 "Number of instrumented floating-point stores");
59 "Number of instrumented non floating-point stores");
61 NumInstrumentedNonFTMemcpyStores,
62 "Number of instrumented non floating-point stores with memcpy semantics");
63STATISTIC(NumInstrumentedFCmp,
"Number of instrumented fcmps");
78class ShadowTypeConfig {
80 static std::unique_ptr<ShadowTypeConfig> fromNsanTypeId(
char TypeId);
86 virtual char getNsanTypeId()
const = 0;
88 virtual ~ShadowTypeConfig() =
default;
91template <
char NsanTypeId>
92class ShadowTypeConfigImpl :
public ShadowTypeConfig {
94 char getNsanTypeId()
const override {
return NsanTypeId; }
95 static constexpr char kNsanTypeId = NsanTypeId;
99class F64ShadowConfig :
public ShadowTypeConfigImpl<'d'> {
106class F80ShadowConfig :
public ShadowTypeConfigImpl<'l'> {
113class F128ShadowConfig :
public ShadowTypeConfigImpl<'q'> {
120class PPC128ShadowConfig :
public ShadowTypeConfigImpl<'e'> {
127std::unique_ptr<ShadowTypeConfig>
128ShadowTypeConfig::fromNsanTypeId(
const char TypeId) {
130 case F64ShadowConfig::kNsanTypeId:
131 return std::make_unique<F64ShadowConfig>();
132 case F80ShadowConfig::kNsanTypeId:
133 return std::make_unique<F80ShadowConfig>();
134 case F128ShadowConfig::kNsanTypeId:
135 return std::make_unique<F128ShadowConfig>();
136 case PPC128ShadowConfig::kNsanTypeId:
137 return std::make_unique<PPC128ShadowConfig>();
144enum FTValueType { kFloat, kDouble, kLongDouble, kNumValueTypes };
147static std::optional<FTValueType> ftValueTypeFromType(
Type *FT) {
158static Type *typeFromFTValueType(FTValueType VT,
LLVMContext &Context) {
173static const char *typeNameFromFTValueType(FTValueType VT) {
191 MappingConfig(
const InstrumentationOptions &Opts,
LLVMContext &
C)
193 if (Opts.nsan_shadow_type_mapping.size() != 3)
195 Twine(Opts.nsan_shadow_type_mapping));
196 unsigned ShadowTypeSizeBits[kNumValueTypes];
197 for (
int VT = 0; VT < kNumValueTypes; ++VT) {
199 ShadowTypeConfig::fromNsanTypeId(Opts.nsan_shadow_type_mapping[VT]);
202 Twine(Opts.nsan_shadow_type_mapping[VT]));
203 const unsigned AppTypeSize =
204 typeFromFTValueType(
static_cast<FTValueType
>(VT), Context)
205 ->getScalarSizeInBits();
206 const unsigned ShadowTypeSize =
207 Config->getType(Context)->getScalarSizeInBits();
212 "->f" +
Twine(ShadowTypeSize) +
213 ": The shadow type size should be at most " +
215 " times the application type size");
216 ShadowTypeSizeBits[VT] = ShadowTypeSize;
217 Configs[VT] = std::move(Config);
226 if (ShadowTypeSizeBits[kFloat] > ShadowTypeSizeBits[kDouble] ||
227 ShadowTypeSizeBits[kDouble] > ShadowTypeSizeBits[kLongDouble])
229 Twine(ShadowTypeSizeBits[kFloat]) +
"; double->f" +
230 Twine(ShadowTypeSizeBits[kDouble]) +
232 Twine(ShadowTypeSizeBits[kLongDouble]) +
" }");
235 const ShadowTypeConfig &byValueType(FTValueType VT)
const {
236 assert(VT < FTValueType::kNumValueTypes &&
"invalid value type");
241 Type *getExtendedFPType(
Type *FT)
const {
242 if (
const auto VT = ftValueTypeFromType(FT))
243 return Configs[*VT]->getType(Context);
247 if (VecTy->isScalableTy())
249 Type *ExtendedScalar = getExtendedFPType(VecTy->getElementType());
250 return ExtendedScalar
259 std::unique_ptr<ShadowTypeConfig> Configs[FTValueType::kNumValueTypes];
264struct MemoryExtents {
269static MemoryExtents getMemoryExtentsOrDie(
Type *FT) {
270 if (
const auto VT = ftValueTypeFromType(FT))
273 const auto ScalarExtents = getMemoryExtentsOrDie(VecTy->getElementType());
274 return {ScalarExtents.ValueType,
275 ScalarExtents.NumElts * VecTy->getElementCount().getFixedValue()};
284 static CheckLoc makeStore(
Value *Address) {
285 CheckLoc Result(kStore);
286 Result.Address = Address;
289 static CheckLoc makeLoad(
Value *Address) {
290 CheckLoc Result(kLoad);
291 Result.Address = Address;
296 static CheckLoc makeArg(
int ArgId) {
297 CheckLoc Result(kArg);
298 Result.ArgId = ArgId;
303 static CheckLoc makeRet() {
return CheckLoc(kRet); }
306 static CheckLoc makeInsert() {
return CheckLoc(kInsert); }
323 return ConstantInt::get(IntptrTy, 0);
325 return ConstantInt::get(IntptrTy, ArgId);
328 return Builder.CreatePtrToInt(Address, IntptrTy);
344 explicit CheckLoc(
CheckType CheckTy) : CheckTy(CheckTy) {}
346 Value *Address =
nullptr;
352class ValueToShadowMap {
354 explicit ValueToShadowMap(
const MappingConfig &Config) : Config(Config) {}
356 ValueToShadowMap(
const ValueToShadowMap &) =
delete;
357 ValueToShadowMap &operator=(
const ValueToShadowMap &) =
delete;
362 [[maybe_unused]]
const bool Inserted = Map.
try_emplace(&
V, &Shadow).second;
366 errs() <<
I->getFunction()->getName() <<
": ";
367 errs() <<
"duplicate shadow (" << &
V <<
"): ";
371 assert(Inserted &&
"duplicate shadow");
382 return getShadowConstant(
C);
383 return Map.
find(
V)->second;
386 bool empty()
const {
return Map.
empty(); }
391 bool LosesInfo =
false;
403 Type *Ty = Config.getExtendedFPType(CFP->getType());
404 return ConstantFP::get(
405 Ty, extendConstantFP(CFP->getValueAPF(),
406 Ty->getScalarType()->getFltSemantics()));
409 if (
C->getType()->isVectorTy()) {
415 Elements.push_back(getShadowConstant(
C->getAggregateElement(
I)));
421 const MappingConfig &Config;
434 size_t NumSizedFuncs;
441 Attr = Attr.addFnAttribute(Ctx, Attribute::NoUnwind);
444 IntegerType *IntptrTy = M.getDataLayout().getIntPtrType(Ctx);
447 NumSizedFuncs = Sized.
size();
452 M.getOrInsertFunction(Fallback, Attr, VoidTy, PtrTy, PtrTy, IntptrTy));
454 }
else if (NumArgs == 2) {
456 M.getOrInsertFunction(Fallback, Attr, VoidTy, PtrTy, IntptrTy));
462 for (
size_t i = 0; i < NumSizedFuncs; ++i)
463 Funcs.push_back(M.getOrInsertFunction(Sized[i], SizedFnTy, Attr));
469 assert(NumSizedFuncs >= 3 &&
"Unexpected number of sized functions");
472 MemOpSize == 4 ? 1 : (MemOpSize == 8 ? 2 : (MemOpSize == 16 ? 3 : 0));
477FunctionCallee NsanMemOpFn::getFallback()
const {
return Funcs[0]; }
483class NumericalStabilitySanitizer {
485 NumericalStabilitySanitizer(
const InstrumentationOptions &Opts,
Module &M);
490 void maybeAddSuffixForNsanInterface(
CallBase *CI);
491 bool addrPointsToConstantData(
Value *Addr);
493 ValueToShadowMap &Map);
496 const ValueToShadowMap &Map);
499 ValueToShadowMap &Map);
502 const ValueToShadowMap &Map);
505 const ValueToShadowMap &Map);
510 void emitFCmpCheck(
FCmpInst &FCmp,
const ValueToShadowMap &Map);
516 const ValueToShadowMap &Map,
IRBuilder<> &Builder);
519 const ValueToShadowMap &Map,
522 const ValueToShadowMap &Map,
IRBuilder<> &Builder);
524 const ValueToShadowMap &Map,
IRBuilder<> &Builder);
528 const ValueToShadowMap &Map);
530 const ValueToShadowMap &Map);
532 const InstrumentationOptions &Opts;
535 MappingConfig Config;
539 FunctionCallee NsanGetShadowPtrForStore[FTValueType::kNumValueTypes] = {};
540 FunctionCallee NsanGetShadowPtrForLoad[FTValueType::kNumValueTypes] = {};
544 NsanMemOpFn NsanCopyFns;
545 NsanMemOpFn NsanSetUnknownFns;
551 Type *NsanShadowRetType =
nullptr;
556 Type *NsanShadowArgsType =
nullptr;
559 std::optional<Regex> CheckFunctionsFilter;
573 NumericalStabilitySanitizer Nsan(Opts, M);
582 return M.getOrInsertGlobal(Name, Ty, [&M, Ty, Name] {
584 nullptr, Name,
nullptr,
589NumericalStabilitySanitizer::NumericalStabilitySanitizer(
590 const InstrumentationOptions &Opts,
Module &M)
593 NsanCopyFns(
M, {
"__nsan_copy_4",
"__nsan_copy_8",
"__nsan_copy_16"},
594 "__nsan_copy_values", 3),
596 {
"__nsan_set_value_unknown_4",
597 "__nsan_set_value_unknown_8",
598 "__nsan_set_value_unknown_16"},
599 "__nsan_set_value_unknown", 2) {
600 IntptrTy =
DL.getIntPtrType(Context);
607 Attr = Attr.addFnAttribute(Context, Attribute::NoUnwind);
611 TargetLibraryInfo::getExtAttrForI32Return(TT,
true);
613 TargetLibraryInfo::getExtAttrForI32Param(TT,
true);
618 for (
int I = 0;
I < kNumValueTypes; ++
I) {
619 const FTValueType VT =
static_cast<FTValueType
>(
I);
620 const char *VTName = typeNameFromFTValueType(VT);
621 Type *VTTy = typeFromFTValueType(VT, Context);
624 const std::string GetterPrefix =
625 std::string(
"__nsan_get_shadow_ptr_for_") + VTName;
626 NsanGetShadowPtrForStore[VT] = M.getOrInsertFunction(
627 GetterPrefix +
"_store", Attr, PtrTy, PtrTy, IntptrTy);
628 NsanGetShadowPtrForLoad[VT] = M.getOrInsertFunction(
629 GetterPrefix +
"_load", Attr, PtrTy, PtrTy, IntptrTy);
632 const auto &ShadowConfig = Config.byValueType(VT);
633 Type *ShadowTy = ShadowConfig.getType(Context);
636 AL =
AL.maybeAddRetAttribute(Context, I32RetExtAttr);
637 AL =
AL.maybeAddParamAttribute(Context, 2, I32ArgExtAttr);
639 M.getOrInsertFunction(std::string(
"__nsan_internal_check_") + VTName +
640 "_" + ShadowConfig.getNsanTypeId(),
641 AL, Int32Ty, VTTy, ShadowTy, Int32Ty, IntptrTy);
646 AL =
AL.maybeAddParamAttribute(Context, 4, I32ArgExtAttr);
647 AL =
AL.maybeAddParamAttribute(Context, 5, BoolExtAttr);
648 AL =
AL.maybeAddParamAttribute(Context, 6, BoolExtAttr);
649 NsanFCmpFail[VT] = M.getOrInsertFunction(
650 std::string(
"__nsan_fcmp_fail_") + VTName +
"_" +
651 ShadowConfig.getNsanTypeId(),
652 AL, VoidTy, VTTy, VTTy, ShadowTy, ShadowTy, Int32Ty, Int1Ty, Int1Ty);
657 NsanGetRawShadowTypePtr = M.getOrInsertFunction(
658 "__nsan_internal_get_raw_shadow_type_ptr", Attr, PtrTy, PtrTy);
659 NsanGetRawShadowPtr = M.getOrInsertFunction(
660 "__nsan_internal_get_raw_shadow_ptr", Attr, PtrTy, PtrTy);
679 if (!Opts.check_functions_filter.empty()) {
681 std::string RegexError;
683 CheckFunctionsFilter = std::move(R);
689bool NumericalStabilitySanitizer::addrPointsToConstantData(
Value *Addr) {
692 Addr =
GEP->getPointerOperand();
695 return GV->isConstant();
711void NumericalStabilitySanitizer::createShadowArguments(
712 Function &
F,
const TargetLibraryInfo &
TLI, ValueToShadowMap &Map) {
713 assert(!
F.getIntrinsicID() &&
"found a definition of an intrinsic");
719 IRBuilder<> ByValBuilder(
F.getEntryBlock().getFirstNonPHIIt());
720 for (Argument &Arg :
F.args()) {
721 if (!Arg.hasByValAttr())
723 TypeSize ByValSize =
DL.getTypeAllocSize(Arg.getParamByValType());
724 assert(!ByValSize.
isScalable() &&
"scalable byval is rejected by verifier");
728 FunctionCallee SetUnknownFn = NsanSetUnknownFns.getFunctionFor(
Size);
730 ByValBuilder.CreateCall(SetUnknownFn, {&Arg});
732 ByValBuilder.CreateCall(SetUnknownFn,
733 {&Arg, ConstantInt::get(IntptrTy,
Size)});
737 if (
all_of(
F.args(), [
this](
const Argument &Arg) {
738 return Config.getExtendedFPType(Arg.getType()) == nullptr;
742 IRBuilder<> Builder(
F.getEntryBlock().getFirstNonPHIIt());
745 Value *HasShadowArgs = Builder.CreateICmpEQ(
746 Builder.CreateLoad(IntptrTy, NsanShadowArgsTag,
false),
747 Builder.CreatePtrToInt(&
F, IntptrTy));
749 unsigned ShadowArgsOffsetBytes = 0;
750 for (Argument &Arg :
F.args()) {
752 Type *ExtendedVT = Config.getExtendedFPType(VT);
753 if (ExtendedVT ==
nullptr)
755 Value *
L = Builder.CreateAlignedLoad(
757 Builder.CreateConstGEP2_64(NsanShadowArgsType, NsanShadowArgsPtr, 0,
758 ShadowArgsOffsetBytes),
760 Value *Shadow = Builder.CreateSelect(HasShadowArgs, L,
761 Builder.CreateFPExt(&Arg, ExtendedVT));
762 Map.setShadow(Arg, *Shadow);
763 TypeSize SlotSize =
DL.getTypeStoreSize(ExtendedVT);
765 ShadowArgsOffsetBytes += SlotSize;
767 Builder.CreateStore(ConstantInt::get(IntptrTy, 0), NsanShadowArgsTag);
773 const std::optional<Regex> &CheckFunctionsFilter) {
777 if (CheckFunctionsFilter) {
781 if (CheckFunctionsFilter->match(Fn->
getName()))
794 LibFunc LFunc =
TLI.getLibFunc(*Fn);
802 if (ID == Intrinsic::fabs || LFunc == LibFunc_fabsf ||
803 LFunc == LibFunc_fabs || LFunc == LibFunc_fabsl)
804 for (
const auto &U : CI.
users())
813void NumericalStabilitySanitizer::populateShadowStack(
814 CallBase &CI,
const TargetLibraryInfo &
TLI,
const ValueToShadowMap &Map) {
821 return Config.getExtendedFPType(Arg->getType()) == nullptr;
826 SmallVector<Value *, 8> ArgShadows;
829 if (Config.getExtendedFPType(Arg->
getType()) ==
nullptr)
831 Value *ArgShadow =
Map.getShadow(Arg);
832 ArgShadows.
push_back(ShouldCheckArgs ? emitCheck(Arg, ArgShadow, Builder,
833 CheckLoc::makeArg(ArgIdx))
839 if (Fn->isIntrinsic() ||
TLI.getLibFunc(*Fn) != NotLibFunc)
847 unsigned ShadowArgId = 0;
850 Type *ExtendedVT = Config.getExtendedFPType(VT);
851 if (ExtendedVT ==
nullptr)
853 Builder.CreateAlignedStore(
854 ArgShadows[ShadowArgId++],
855 Builder.CreateConstGEP2_64(NsanShadowArgsType, NsanShadowArgsPtr, 0,
856 ShadowArgsOffsetBytes),
858 TypeSize SlotSize =
DL.getTypeStoreSize(ExtendedVT);
860 ShadowArgsOffsetBytes += SlotSize;
872Value *NumericalStabilitySanitizer::emitCheckInternal(
Value *
V,
Value *ShadowV,
877 return ConstantInt::get(
881 Type *Ty =
V->getType();
882 if (
const auto VT = ftValueTypeFromType(Ty))
885 {
V, ShadowV, Loc.getType(
Context), Loc.getValue(IntptrTy, Builder)});
891 assert(!VecTy->isScalableTy() &&
892 "Scalable vector types are not supported yet");
893 Value *CheckResult =
nullptr;
894 for (
int I = 0,
E = VecTy->getElementCount().getFixedValue();
I <
E; ++
I) {
900 Value *ComponentCheckResult =
901 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
902 CheckResult = CheckResult
903 ? Builder.
CreateOr(CheckResult, ComponentCheckResult)
904 : ComponentCheckResult;
909 Value *CheckResult =
nullptr;
913 Value *ComponentCheckResult =
914 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
915 CheckResult = CheckResult
916 ? Builder.
CreateOr(CheckResult, ComponentCheckResult)
917 : ComponentCheckResult;
922 Value *CheckResult =
nullptr;
928 Value *ComponentCheckResult =
929 emitCheckInternal(ExtractV, ExtractShadowV, Builder, Loc);
930 CheckResult = CheckResult
931 ? Builder.
CreateOr(CheckResult, ComponentCheckResult)
932 : ComponentCheckResult;
935 return ConstantInt::get(
959 if (CheckFunctionsFilter && !CheckFunctionsFilter->match(
F->getName())) {
964 Value *CheckResult = emitCheckInternal(
V, ShadowV, Builder, Loc);
970 ICmpEQ, Builder.
CreateFPExt(
V, Config.getExtendedFPType(
V->getType())),
976void NumericalStabilitySanitizer::emitFCmpCheck(FCmpInst &FCmp,
977 const ValueToShadowMap &Map) {
978 if (!Opts.nsan_instrument_fcmp)
982 if (CheckFunctionsFilter && !CheckFunctionsFilter->match(
F->getName()))
986 if (Config.getExtendedFPType(
LHS->
getType()) ==
nullptr)
1001 FCmpBuilder.SetCurrentDebugLocation(FCmp.
getDebugLoc());
1005 if (FCmp.
isEquality() && Opts.nsan_truncate_fcmp_eq) {
1007 ShadowLHS = FCmpBuilder.CreateFPExt(
1008 FCmpBuilder.CreateFPTrunc(ShadowLHS,
LHS->
getType()), Ty);
1009 ShadowRHS = FCmpBuilder.CreateFPExt(
1010 FCmpBuilder.CreateFPTrunc(ShadowRHS,
RHS->
getType()), Ty);
1013 FCmpBuilder.CreateFCmp(FCmp.
getPredicate(), ShadowLHS, ShadowRHS);
1014 Value *OriginalAndShadowFcmpMatch =
1015 FCmpBuilder.CreateICmpEQ(&FCmp, ShadowFCmp);
1021 OriginalAndShadowFcmpMatch =
1022 FCmpBuilder.CreateAndReduce(OriginalAndShadowFcmpMatch);
1027 FCmpBuilder.CreateCondBr(OriginalAndShadowFcmpMatch, NextBB, FailBB,
1028 MDBuilder(
Context).createLikelyBranchWeights());
1032 FailBuilder.SetCurrentDebugLocation(FCmp.
getDebugLoc());
1034 const auto EmitFailCall = [
this, &FCmp, &FCmpBuilder,
1037 Value *ShadowResult) {
1038 Type *FT =
L->getType();
1039 FunctionCallee *
Callee =
nullptr;
1041 Callee = &(NsanFCmpFail[kFloat]);
1043 Callee = &(NsanFCmpFail[kDouble]);
1046 Callee = &(NsanFCmpFail[kDouble]);
1047 L = FailBuilder.CreateFPTrunc(L, Type::getDoubleTy(
Context));
1048 R = FailBuilder.CreateFPTrunc(L, Type::getDoubleTy(
Context));
1052 FailBuilder.CreateCall(*Callee, {
L,
R, ShadowL, ShadowR,
1053 ConstantInt::get(FCmpBuilder.getInt32Ty(),
1062 Value *ExtractLHS = FailBuilder.CreateExtractElement(
LHS,
I);
1063 Value *ExtractRHS = FailBuilder.CreateExtractElement(
RHS,
I);
1064 Value *ExtractShaodwLHS = FailBuilder.CreateExtractElement(ShadowLHS,
I);
1065 Value *ExtractShaodwRHS = FailBuilder.CreateExtractElement(ShadowRHS,
I);
1066 Value *ExtractFCmp = FailBuilder.CreateExtractElement(&FCmp,
I);
1067 Value *ExtractShadowFCmp =
1068 FailBuilder.CreateExtractElement(ShadowFCmp,
I);
1069 EmitFailCall(ExtractLHS, ExtractRHS, ExtractShaodwLHS, ExtractShaodwRHS,
1070 ExtractFCmp, ExtractShadowFCmp);
1073 EmitFailCall(
LHS,
RHS, ShadowLHS, ShadowRHS, &FCmp, ShadowFCmp);
1075 FailBuilder.CreateBr(NextBB);
1077 ++NumInstrumentedFCmp;
1081PHINode *NumericalStabilitySanitizer::maybeCreateShadowPhi(
1082 PHINode &Phi,
const TargetLibraryInfo &
TLI) {
1084 Type *ExtendedVT = Config.getExtendedFPType(VT);
1085 if (ExtendedVT ==
nullptr)
1095Value *NumericalStabilitySanitizer::handleLoad(LoadInst &
Load,
Type *VT,
1099 if (addrPointsToConstantData(
Load.getPointerOperand())) {
1119 const auto Extents = getMemoryExtentsOrDie(VT);
1121 NsanGetShadowPtrForLoad[Extents.ValueType],
1122 {Load.getPointerOperand(), ConstantInt::get(IntptrTy, Extents.NumElts)});
1123 ++NumInstrumentedFTLoads;
1139 LoadBBBuilder.SetCurrentDebugLocation(
Load.getDebugLoc());
1140 LoadBBBuilder.CreateCondBr(LoadBBBuilder.CreateIsNull(ShadowPtr), FExtBB,
1146 ShadowLoadBBBuilder.SetCurrentDebugLocation(
Load.getDebugLoc());
1147 Value *ShadowLoad = ShadowLoadBBBuilder.CreateAlignedLoad(
1148 ExtendedVT, ShadowPtr,
Align(1),
Load.isVolatile());
1149 if (Opts.nsan_check_loads) {
1150 ShadowLoad = emitCheck(&
Load, ShadowLoad, ShadowLoadBBBuilder,
1151 CheckLoc::makeLoad(
Load.getPointerOperand()));
1153 ShadowLoadBBBuilder.CreateBr(NextBB);
1157 FExtBBBuilder.SetCurrentDebugLocation(
Load.getDebugLoc());
1158 Value *FExt = FExtBBBuilder.CreateFPExt(&
Load, ExtendedVT);
1159 FExtBBBuilder.CreateBr(NextBB);
1163 NextBBBuilder.SetCurrentDebugLocation(
Load.getDebugLoc());
1164 PHINode *ShadowPhi = NextBBBuilder.CreatePHI(ExtendedVT, 2);
1170Value *NumericalStabilitySanitizer::handleTrunc(
const FPTruncInst &
Trunc,
1172 const ValueToShadowMap &Map,
1176 Type *ExtendedSourceTy = Config.getExtendedFPType(OrigSourceTy);
1209 Value *
Source = ExtendedSourceTy ?
Map.getShadow(OrigSource) : OrigSource;
1210 Type *SourceTy = ExtendedSourceTy ? ExtendedSourceTy : OrigSourceTy;
1212 if (SourceTy == ExtendedVT)
1218Value *NumericalStabilitySanitizer::handleExt(
const FPExtInst &Ext,
Type *VT,
1220 const ValueToShadowMap &Map,
1224 Type *ExtendedSourceTy = Config.getExtendedFPType(OrigSourceTy);
1256 Value *
Source = ExtendedSourceTy ?
Map.getShadow(OrigSource) : OrigSource;
1257 Type *SourceTy = ExtendedSourceTy ? ExtendedSourceTy : OrigSourceTy;
1259 if (SourceTy == ExtendedVT)
1267struct KnownIntrinsic {
1268 struct WidenedIntrinsic {
1269 const char *NarrowName;
1271 using FnTypeFactory = FunctionType *(*)(LLVMContext &);
1272 FnTypeFactory MakeFnTy;
1275 static const char *
get(LibFunc LFunc);
1282 static const WidenedIntrinsic *widen(StringRef Name);
1287 const char *IntrinsicName;
1289 static const LFEntry kLibfuncIntrinsics[];
1291 static const WidenedIntrinsic kWidenedIntrinsics[];
1340const KnownIntrinsic::WidenedIntrinsic KnownIntrinsic::kWidenedIntrinsics[] = {
1429const KnownIntrinsic::LFEntry KnownIntrinsic::kLibfuncIntrinsics[] = {
1430 {LibFunc_sqrtf,
"llvm.sqrt.f32"},
1431 {LibFunc_sqrt,
"llvm.sqrt.f64"},
1432 {LibFunc_sqrtl,
"llvm.sqrt.f80"},
1433 {LibFunc_sinf,
"llvm.sin.f32"},
1434 {LibFunc_sin,
"llvm.sin.f64"},
1435 {LibFunc_sinl,
"llvm.sin.f80"},
1436 {LibFunc_cosf,
"llvm.cos.f32"},
1437 {LibFunc_cos,
"llvm.cos.f64"},
1438 {LibFunc_cosl,
"llvm.cos.f80"},
1439 {LibFunc_powf,
"llvm.pow.f32"},
1440 {LibFunc_pow,
"llvm.pow.f64"},
1441 {LibFunc_powl,
"llvm.pow.f80"},
1442 {LibFunc_expf,
"llvm.exp.f32"},
1443 {LibFunc_exp,
"llvm.exp.f64"},
1444 {LibFunc_expl,
"llvm.exp.f80"},
1445 {LibFunc_exp2f,
"llvm.exp2.f32"},
1446 {LibFunc_exp2,
"llvm.exp2.f64"},
1447 {LibFunc_exp2l,
"llvm.exp2.f80"},
1448 {LibFunc_logf,
"llvm.log.f32"},
1449 {LibFunc_log,
"llvm.log.f64"},
1450 {LibFunc_logl,
"llvm.log.f80"},
1451 {LibFunc_log10f,
"llvm.log10.f32"},
1452 {LibFunc_log10,
"llvm.log10.f64"},
1453 {LibFunc_log10l,
"llvm.log10.f80"},
1454 {LibFunc_log2f,
"llvm.log2.f32"},
1455 {LibFunc_log2,
"llvm.log2.f64"},
1456 {LibFunc_log2l,
"llvm.log2.f80"},
1457 {LibFunc_fabsf,
"llvm.fabs.f32"},
1458 {LibFunc_fabs,
"llvm.fabs.f64"},
1459 {LibFunc_fabsl,
"llvm.fabs.f80"},
1460 {LibFunc_copysignf,
"llvm.copysign.f32"},
1461 {LibFunc_copysign,
"llvm.copysign.f64"},
1462 {LibFunc_copysignl,
"llvm.copysign.f80"},
1463 {LibFunc_floorf,
"llvm.floor.f32"},
1464 {LibFunc_floor,
"llvm.floor.f64"},
1465 {LibFunc_floorl,
"llvm.floor.f80"},
1466 {LibFunc_fmaxf,
"llvm.maxnum.f32"},
1467 {LibFunc_fmax,
"llvm.maxnum.f64"},
1468 {LibFunc_fmaxl,
"llvm.maxnum.f80"},
1469 {LibFunc_fminf,
"llvm.minnum.f32"},
1470 {LibFunc_fmin,
"llvm.minnum.f64"},
1471 {LibFunc_fminl,
"llvm.minnum.f80"},
1472 {LibFunc_fmaximum_numf,
"llvm.maximumnum.f32"},
1473 {LibFunc_fmaximum_num,
"llvm.maximumnum.f64"},
1474 {LibFunc_fmaximum_numl,
"llvm.maximumnum.f80"},
1475 {LibFunc_fminimum_numf,
"llvm.minimumnum.f32"},
1476 {LibFunc_fminimum_num,
"llvm.minimumnum.f64"},
1477 {LibFunc_fminimum_numl,
"llvm.minimumnum.f80"},
1478 {LibFunc_ceilf,
"llvm.ceil.f32"},
1479 {LibFunc_ceil,
"llvm.ceil.f64"},
1480 {LibFunc_ceill,
"llvm.ceil.f80"},
1481 {LibFunc_truncf,
"llvm.trunc.f32"},
1482 {LibFunc_trunc,
"llvm.trunc.f64"},
1483 {LibFunc_truncl,
"llvm.trunc.f80"},
1484 {LibFunc_rintf,
"llvm.rint.f32"},
1485 {LibFunc_rint,
"llvm.rint.f64"},
1486 {LibFunc_rintl,
"llvm.rint.f80"},
1487 {LibFunc_nearbyintf,
"llvm.nearbyint.f32"},
1488 {LibFunc_nearbyint,
"llvm.nearbyint.f64"},
1489 {LibFunc_nearbyintl,
"llvm.nearbyint.f80"},
1490 {LibFunc_roundf,
"llvm.round.f32"},
1491 {LibFunc_round,
"llvm.round.f64"},
1492 {LibFunc_roundl,
"llvm.round.f80"},
1495const char *KnownIntrinsic::get(LibFunc LFunc) {
1496 for (
const auto &
E : kLibfuncIntrinsics) {
1497 if (
E.LFunc == LFunc)
1498 return E.IntrinsicName;
1503const KnownIntrinsic::WidenedIntrinsic *KnownIntrinsic::widen(StringRef Name) {
1504 for (
const auto &
E : kWidenedIntrinsics) {
1505 if (
E.NarrowName == Name)
1514 LibFunc LFunc =
TLI.getLibFunc(Fn);
1515 if (LFunc == NotLibFunc)
1518 if (
const char *Name = KnownIntrinsic::get(LFunc))
1526Value *NumericalStabilitySanitizer::maybeHandleKnownCallBase(
1527 CallBase &
Call,
Type *VT,
Type *ExtendedVT,
const TargetLibraryInfo &
TLI,
1528 const ValueToShadowMap &Map,
IRBuilder<> &Builder) {
1534 FunctionType *WidenedFnTy =
nullptr;
1536 const auto *Widened = KnownIntrinsic::widen(Fn->
getName());
1538 WidenedId = Widened->ID;
1539 WidenedFnTy = Widened->MakeFnTy(
Context);
1550 const auto *Widened = KnownIntrinsic::widen(Name);
1551 assert(Widened &&
"make sure KnownIntrinsic entries are consistent");
1552 WidenedId = Widened->ID;
1553 WidenedFnTy = Widened->MakeFnTy(
Context);
1561 [[maybe_unused]]
bool IsValid =
1563 assert(IsValid &&
"invalid widened intrinsic");
1566 SmallVector<Value *, 4>
Args;
1571 Type *IntrinsicArgTy = WidenedFnTy->getParamType(
I);
1572 if (OrigArgTy == IntrinsicArgTy) {
1573 Args.push_back(Arg);
1576 Type *ShadowArgTy = Config.getExtendedFPType(Arg->
getType());
1578 "don't know how to get the shadow value for a non-FT");
1579 Value *Shadow =
Map.getShadow(Arg);
1580 if (ShadowArgTy == IntrinsicArgTy) {
1583 Args.push_back(Shadow);
1590 return WidenedFnTy->getReturnType() == ExtendedVT
1597Value *NumericalStabilitySanitizer::handleCallBase(CallBase &
Call,
Type *VT,
1599 const TargetLibraryInfo &
TLI,
1600 const ValueToShadowMap &Map,
1610 maybeHandleKnownCallBase(
Call, VT, ExtendedVT,
TLI, Map, Builder))
1616 Builder.
CreateLoad(IntptrTy, NsanShadowRetTag,
false);
1626 ++NumInstrumentedFTCalls;
1632Value *NumericalStabilitySanitizer::createShadowValueWithOperandsAvailable(
1633 Instruction &Inst,
const TargetLibraryInfo &
TLI,
1634 const ValueToShadowMap &Map) {
1636 Type *ExtendedVT = Config.getExtendedFPType(VT);
1637 assert(ExtendedVT !=
nullptr &&
"trying to create a shadow for a non-FT");
1640 return handleLoad(*
Load, VT, ExtendedVT);
1647 return handleCallBase(*
Call, VT, ExtendedVT,
TLI, Map, Builder);
1654 BasicBlock *NextBB = Invoke->getNormalDest();
1661 Value *Shadow = handleCallBase(*Invoke, VT, ExtendedVT,
TLI, Map, Builder);
1671 return handleTrunc(*
Trunc, VT, ExtendedVT, Map, Builder);
1673 return handleExt(*Ext, VT, ExtendedVT, Map, Builder);
1676 return Builder.
CreateUnOp(UnaryOp->getOpcode(),
1677 Map.getShadow(UnaryOp->getOperand(0)));
1681 Map.getShadow(BinOp->getOperand(0)),
1682 Map.getShadow(BinOp->getOperand(1)));
1686 return Builder.
CreateCast(Cast->getOpcode(), Cast->getOperand(0),
1692 Map.getShadow(S->getTrueValue()),
1693 Map.getShadow(S->getFalseValue()));
1700 Map.getShadow(Extract->getVectorOperand()), Extract->getIndexOperand());
1709 Map.getShadow(Shuffle->getOperand(1)),
1710 Shuffle->getShuffleMask());
1727void NumericalStabilitySanitizer::maybeCreateShadowValue(
1728 Instruction &Root,
const TargetLibraryInfo &
TLI, ValueToShadowMap &Map) {
1730 Type *ExtendedVT = Config.getExtendedFPType(VT);
1731 if (ExtendedVT ==
nullptr)
1734 if (
Map.hasShadow(&Root))
1739 std::vector<Instruction *> DfsStack(1, &Root);
1740 while (!DfsStack.empty()) {
1746 if (
Map.hasShadow(
I)) {
1747 DfsStack.pop_back();
1751 bool MissingShadow =
false;
1753 Type *VT =
Op->getType();
1754 if (!Config.getExtendedFPType(VT))
1756 if (
Map.hasShadow(
Op))
1758 MissingShadow =
true;
1765 Value *Shadow = createShadowValueWithOperandsAvailable(*
I,
TLI, Map);
1766 Map.setShadow(*
I, *Shadow);
1767 DfsStack.pop_back();
1772void NumericalStabilitySanitizer::propagateFTStore(
1773 StoreInst &
Store,
Type *VT,
Type *ExtendedVT,
const ValueToShadowMap &Map) {
1774 Value *StoredValue =
Store.getValueOperand();
1777 const auto Extents = getMemoryExtentsOrDie(VT);
1779 NsanGetShadowPtrForStore[Extents.ValueType],
1780 {Store.getPointerOperand(), ConstantInt::get(IntptrTy, Extents.NumElts)});
1782 Value *StoredShadow =
Map.getShadow(StoredValue);
1783 if (!
Store.getParent()->getParent()->hasOptNone()) {
1786 if (Opts.nsan_check_stores) {
1787 StoredShadow = emitCheck(StoredValue, StoredShadow, Builder,
1788 CheckLoc::makeStore(
Store.getPointerOperand()));
1789 ++NumInstrumentedFTStores;
1794 Store.isVolatile());
1805void NumericalStabilitySanitizer::propagateNonFTStore(
1806 StoreInst &
Store,
Type *VT,
const ValueToShadowMap &Map) {
1811 TypeSize SlotSize =
DL.getTypeStoreSize(VT);
1817 ++NumInstrumentedNonFTStores;
1818 Value *StoredValue =
Store.getValueOperand();
1825 Type *ShadowTypeIntTy = Type::getIntNTy(
Context, 8 * LoadSizeBytes);
1826 Type *ShadowValueIntTy =
1830 Value *LoadSrc =
Load->getPointerOperand();
1836 Value *RawShadowType = LoadBuilder.CreateAlignedLoad(
1838 LoadBuilder.CreateCall(NsanGetRawShadowTypePtr, {LoadSrc}),
Align(1),
1840 Value *RawShadowValue = LoadBuilder.CreateAlignedLoad(
1842 LoadBuilder.CreateCall(NsanGetRawShadowPtr, {LoadSrc}),
Align(1),
1847 RawShadowType, Builder.
CreateCall(NsanGetRawShadowTypePtr, {Dst}),
1851 Builder.
CreateCall(NsanGetRawShadowPtr, {Dst}),
1855 ++NumInstrumentedNonFTMemcpyStores;
1858 if (Constant *
C; Opts.nsan_propagate_non_ft_const_stores_as_ft &&
1862 Type *BitcastTy =
nullptr;
1864 switch (
C->getType()->getScalarSizeInBits()) {
1866 BitcastTy = Type::getFloatTy(
Context);
1869 BitcastTy = Type::getDoubleTy(
Context);
1872 BitcastTy = Type::getX86_FP80Ty(
Context);
1880 BitcastTy = VectorType::get(BitcastTy, VectorTy->getElementCount());
1883 const MemoryExtents Extents = getMemoryExtentsOrDie(BitcastTy);
1885 NsanGetShadowPtrForStore[Extents.ValueType],
1886 {PtrOp, ConstantInt::get(IntptrTy, Extents.NumElts)});
1888 Type *ExtVT = Config.getExtendedFPType(BitcastTy);
1892 Store.isVolatile());
1897 Builder.
CreateCall(NsanSetUnknownFns.getFallback(), {Dst, ValueSize});
1900void NumericalStabilitySanitizer::propagateShadowValues(
1901 Instruction &Inst,
const TargetLibraryInfo &
TLI,
1902 const ValueToShadowMap &Map) {
1904 Value *StoredValue =
Store->getValueOperand();
1906 Type *ExtendedVT = Config.getExtendedFPType(VT);
1907 if (ExtendedVT ==
nullptr)
1908 return propagateNonFTStore(*
Store, VT, Map);
1909 return propagateFTStore(*
Store, VT, ExtendedVT, Map);
1913 emitFCmpCheck(*FCmp, Map);
1918 maybeAddSuffixForNsanInterface(CB);
1922 instrumentMemIntrinsic(
MI);
1925 populateShadowStack(*CB,
TLI, Map);
1930 if (!Opts.nsan_check_ret)
1933 Value *RV = RetInst->getReturnValue();
1937 Type *ExtendedVT = Config.getExtendedFPType(VT);
1938 if (ExtendedVT ==
nullptr)
1940 Value *RVShadow =
Map.getShadow(RV);
1943 RVShadow = emitCheck(RV, RVShadow, Builder, CheckLoc::makeRet());
1944 ++NumInstrumentedFTRets;
1950 Value *ShadowRetValPtr =
1958 Type *VT =
V->getType();
1959 Type *ExtendedVT = Config.getExtendedFPType(VT);
1960 if (ExtendedVT ==
nullptr)
1963 emitCheck(
V,
Map.getShadow(
V), Builder, CheckLoc::makeInsert());
1972 std::vector<Instruction *> &Instructions) {
1974#define MOVE_FLAG(attr, setter) \
1975 if (F.getFnAttribute(attr).getValueAsString() == "true") { \
1976 F.removeFnAttr(attr); \
1977 FMF.set##setter(); \
1979 MOVE_FLAG(
"no-signed-zeros-fp-math", NoSignedZeros)
1984 I->setFastMathFlags(FMF);
1987bool NumericalStabilitySanitizer::sanitizeFunction(
1989 if (!
F.hasFnAttribute(Attribute::SanitizeNumericalStability) ||
1993 if (
F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
2089 std::vector<Instruction *> OriginalInstructions;
2090 for (BasicBlock &BB :
F)
2091 for (Instruction &Inst : BB)
2092 OriginalInstructions.emplace_back(&Inst);
2095 ValueToShadowMap ValueToShadow(Config);
2100 std::vector<PHINode *> OriginalPhis;
2101 createShadowArguments(
F,
TLI, ValueToShadow);
2102 for (Instruction *
I : OriginalInstructions) {
2104 if (PHINode *Shadow = maybeCreateShadowPhi(*Phi,
TLI)) {
2105 OriginalPhis.push_back(Phi);
2106 ValueToShadow.setShadow(*Phi, *Shadow);
2112 for (Instruction *
I : OriginalInstructions)
2113 maybeCreateShadowValue(*
I,
TLI, ValueToShadow);
2116 for (Instruction *
I : OriginalInstructions)
2117 propagateShadowValues(*
I,
TLI, ValueToShadow);
2120 for (PHINode *Phi : OriginalPhis) {
2121 PHINode *ShadowPhi =
cast<PHINode>(ValueToShadow.getShadow(Phi));
2122 for (
unsigned I :
seq(
Phi->getNumOperands())) {
2124 Value *Shadow = ValueToShadow.getShadow(
V);
2136 return !ValueToShadow.empty();
2143 if (CInt && CInt->getValue().getBitWidth() <= 64)
2144 OpSize = CInt->getValue().getZExtValue();
2152bool NumericalStabilitySanitizer::instrumentMemIntrinsic(MemIntrinsic *
MI) {
2155 FunctionCallee SetUnknownFn =
2156 NsanSetUnknownFns.getFunctionFor(
GetMemOpSize(
M->getArgOperand(2)));
2158 Builder.
CreateCall(SetUnknownFn, {
M->getArgOperand(0)});
2161 {
M->getArgOperand(0),
2166 FunctionCallee CopyFn =
2167 NsanCopyFns.getFunctionFor(
GetMemOpSize(
M->getArgOperand(2)));
2171 M->getArgOperand(1)});
2174 M->getArgOperand(1),
2182void NumericalStabilitySanitizer::maybeAddSuffixForNsanInterface(CallBase *CI) {
2190 if (Fn->
getName() ==
"__nsan_dump_shadow_mem") {
2192 "invalid prototype for __nsan_dump_shadow_mem");
2197 const uint64_t shadow_value_type_ids =
2198 (
static_cast<size_t>(Config.byValueType(kLongDouble).getNsanTypeId())
2200 (
static_cast<size_t>(Config.byValueType(kDouble).getNsanTypeId())
2202 static_cast<size_t>(Config.byValueType(kFloat).getNsanTypeId());
2203 CI->
setArgOperand(3, ConstantInt::get(IntptrTy, shadow_value_type_ids));
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
Module.h This file contains the declarations for the Module class.
static constexpr Value * getValue(Ty &ValueOrUse)
Machine Check Debug Module
static GlobalValue * createThreadLocalGV(const char *Name, Module &M, Type *Ty)
static FunctionType * makeDoubleDouble(LLVMContext &C)
constexpr int kMaxVectorWidth
static bool shouldCheckArgs(CallBase &CI, const TargetLibraryInfo &TLI, const std::optional< Regex > &CheckFunctionsFilter)
static FunctionType * makeX86FP80X86FP80(LLVMContext &C)
#define MOVE_FLAG(attr, setter)
static FunctionType * makeX86FP80X86FP80I32(LLVMContext &C)
constexpr int kMaxNumArgs
constexpr int kMaxShadowTypeSizeBytes
static const char * getIntrinsicFromLibfunc(Function &Fn, Type *VT, const TargetLibraryInfo &TLI)
static FunctionType * makeX86FP80X86FP80X86FP80(LLVMContext &C)
static FunctionType * makeX86FP80X86FP80X86FP80X86FP80(LLVMContext &C)
static uint64_t GetMemOpSize(Value *V)
constexpr StringLiteral kNsanInitName("__nsan_init")
static FunctionType * makeDoubleDoubleDouble(LLVMContext &C)
constexpr int kShadowScale
constexpr StringLiteral kNsanModuleCtorName("nsan.module_ctor")
static FunctionType * makeDoubleDoubleDoubleDouble(LLVMContext &C)
static FunctionType * makeDoubleDoubleI32(LLVMContext &C)
static void moveFastMathFlags(Function &F, std::vector< Instruction * > &Instructions)
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static SymbolRef::Type getType(const Symbol *Sym)
static constexpr roundingMode rmTowardZero
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
LLVM_ABI void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New)
Update all phi nodes in this basic block's successors to refer to basic block New instead of basic bl...
iterator begin()
Instruction iterator methods.
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & back() const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
InstListType::iterator iterator
Instruction iterators...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool isInlineAsm() const
Check if this call is an inline asm statement.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getCalledOperand() const
void setArgOperand(unsigned i, Value *v)
unsigned arg_size() const
Predicate getPredicate() const
Return the predicate for this instruction.
ConstantFP - Floating Point Values [float, double].
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
A parsed version of the target data layout string in and methods for querying it.
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
This instruction compares its operands according to the predicate given to the constructor.
static bool isEquality(Predicate Pred)
This class represents an extension of floating point types.
This class represents a truncation of floating point types.
Convenience struct for specifying and reasoning about fast-math flags.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
FunctionType * getFunctionType()
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
@ ExternalLinkage
Externally visible function.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Value * CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
BasicBlock::iterator GetInsertPoint() const
Value * CreateFreeze(Value *V, const Twine &Name="")
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Value * CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
UncondBrInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Value * CreateFPExt(Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB)
Replace specified successor OldBB to point at the provided block.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
iterator_range< user_iterator > users()
const char * getOpcodeName() const
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
This is the common base class for memset/memcpy/memmove.
A Module instance is used to store all the information related to an LLVM module.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Represent a constant reference to a string, i.e.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
static Attribute::AttrKind getExtAttrForBoolParam(const Triple &T)
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static constexpr TypeSize getFixed(ScalarTy ExactSize)
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM_ABI Type * getStructElementType(unsigned N) const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isX86_FP80Ty() const
Return true if this is x86 long double.
bool isArrayTy() const
True if this is an instance of ArrayType.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
static LLVM_ABI Type * getPPC_FP128Ty(LLVMContext &C)
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
LLVM_ABI unsigned getStructNumElements() const
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
bool isStructTy() const
True if this is an instance of StructType.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
static LLVM_ABI Type * getX86_FP80Ty(LLVMContext &C)
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
'undef' values are things that do not have specified contents.
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
const ParentTy * getParent() const
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ BasicBlock
Various leaf nodes.
LLVM_ABI bool isSignatureValid(Intrinsic::ID ID, FunctionType *FT, SmallVectorImpl< Type * > &OverloadTys, raw_ostream &OS=nulls())
Returns true if FT is a valid function type for intrinsic ID.
NodeAddr< PhiNode * > Phi
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
const InstrumentationOptions & getInstrumentationOptions(const LLVMContext &C)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function lazily.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVM_ABI void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)