1//===- MemoryBuiltins.cpp - Identify calls to memory builtins -------------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This family of functions identifies calls to builtin functions that allocate 12//===----------------------------------------------------------------------===// 51#define DEBUG_TYPE "memory-builtins" 54"object-size-offset-visitor-max-visit-instructions",
55cl::desc(
"Maximum number of instructions for ObjectSizeOffsetVisitor to " 87return"_ZnwmSt11align_val_t";
91return"_ZnamSt11align_val_t";
99return"__kmpc_alloc_shared";
107// First and Second size parameters (or -1 if unused) 109// Alignment parameter for aligned_alloc and aligned new 111// Name of default allocator function to group malloc/free calls by family 116// FIXME: certain users need more information. E.g., SimplifyLibCalls needs to 117// know which functions are nounwind, noalias, nocapture parameters, etc. 130 {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, {
MallocLike, 4, 0, -1, 1,
MallocFamily::CPPNewAligned}},
// new(unsigned long, align_val_t, nothrow, __hot_cold_t) 142 {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, {
MallocLike, 4, 0, -1, 1,
MallocFamily::CPPNewAligned}},
// new[](unsigned long, align_val_t, nothrow, __hot_cold_t) 160// Don't care about intrinsics in this case. 161if (isa<IntrinsicInst>(V))
164constauto *CB = dyn_cast<CallBase>(V);
168if (CB->isNoBuiltin())
171return CB->getCalledFunction();
174/// Returns the allocation data for the given value if it's a call to a known 175/// allocation function. 176static std::optional<AllocFnsTy>
179// Don't perform a slow TLI lookup, if this function doesn't return a pointer 180// and thus can't be an allocation function. 181if (!Callee->getReturnType()->isPointerTy())
184// Make sure that the function is available. 186if (!TLI || !TLI->
getLibFunc(*Callee, TLIFn) || !TLI->
has(TLIFn))
191returnP.first == TLIFn;
201// Check function prototype. 206if (FTy->getReturnType()->isPointerTy() &&
207 FTy->getNumParams() == FnData->
NumParams &&
209 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
210 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
212 FTy->getParamType(SndParam)->isIntegerTy(32) ||
213 FTy->getParamType(SndParam)->isIntegerTy(64)))
218static std::optional<AllocFnsTy>
226static std::optional<AllocFnsTy>
231 Callee, AllocTy, &GetTLI(
const_cast<Function &
>(*Callee)));
235static std::optional<AllocFnsTy>
238// Prefer to use existing information over allocsize. This will give us an 240if (std::optional<AllocFnsTy> Data =
249 std::pair<unsigned, std::optional<unsigned>> Args = Attr.
getAllocSizeArgs();
252// Because allocsize only tells us how many bytes are allocated, we're not 253// really allowed to assume anything, so we use MallocLike. 256 Result.FstParam = Args.first;
257 Result.SndParam = Args.second.value_or(-1);
258// Allocsize has no way to specify an alignment argument 259 Result.AlignParam = -1;
264if (
constauto *CB = dyn_cast<CallBase>(V)) {
265Attribute Attr = CB->getFnAttr(Attribute::AllocKind);
269return AllocFnKind::Unknown;
273returnF->getAttributes().getAllocKind();
284/// Tests if a value is a call or invoke to a library function that 285/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup 298/// Tests if a value is a call or invoke to a library function that 299/// allocates memory via new. 304/// Tests if a value is a call or invoke to a library function that 305/// allocates memory similar to malloc or calloc. 307// TODO: Function behavior does not match name. 311/// Tests if a value is a call or invoke to a library function that 312/// allocates memory (either malloc, calloc, or strdup like). 318/// Tests if a functions is a call or invoke to a library function that 319/// reallocates memory (e.g., realloc). 331// Note: Removability is highly dependent on the source language. For 332// example, recent C++ requires direct calls to the global allocation 333// [basic.stc.dynamic.allocation] to be observable unless part of a new 334// expression [expr.new paragraph 13]. 336// Historically we've treated the C family allocation routines and operator 344if (FnData && FnData->AlignParam >= 0) {
345return V->getOperand(FnData->AlignParam);
347return V->getArgOperandWithAttribute(Attribute::AllocAlign);
350/// When we're compiling N-bit code, and the user uses parameters that are 351/// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into 352/// trouble with APInt size issues. This function handles resizing + overflow 353/// checks for us. Check and zext or trunc \p I depending on IntTyBits and 356// More bits than we can handle. Checking the bit width isn't necessary, but 357// it's faster than checking active bits, and should give `false` in the 358// vast majority of cases. 359if (
I.getBitWidth() > IntTyBits &&
I.getActiveBits() > IntTyBits)
361if (
I.getBitWidth() != IntTyBits)
362I =
I.zextOrTrunc(IntTyBits);
369// Note: This handles both explicitly listed allocation functions and 370// allocsize. The code structure could stand to be cleaned up a bit. 375// Get the index type for this address space, results and intermediate 376// computations are performed at that width. 378constunsigned IntTyBits =
DL.getIndexTypeSizeInBits(CB->
getType());
380// Handle strdup-like functions separately. 386// Strndup limits strlen. 387if (FnData->FstParam > 0) {
389 dyn_cast<ConstantInt>(Mapper(CB->
getArgOperand(FnData->FstParam)));
394if (
Size.ugt(MaxSize))
401 dyn_cast<ConstantInt>(Mapper(CB->
getArgOperand(FnData->FstParam)));
409// Size is determined by just 1 parameter. 410if (FnData->SndParam < 0)
413 Arg = dyn_cast<ConstantInt>(Mapper(CB->
getArgOperand(FnData->SndParam)));
422Size =
Size.umul_ov(NumElems, Overflow);
431auto *Alloc = dyn_cast<CallBase>(V);
435// malloc are uninitialized (undef) 440if ((AK & AllocFnKind::Uninitialized) != AllocFnKind::Unknown)
442if ((AK & AllocFnKind::Zeroed) != AllocFnKind::Unknown)
450// Name of default allocator function to group malloc/free calls by family 492returnP.first == TLIFn;
499std::optional<StringRef>
503if (TLI && TLI->
getLibFunc(*Callee, TLIFn) && TLI->
has(TLIFn)) {
504// Callee is some known library function. 515// Callee isn't a known library function, still check attributes. 517 AllocFnKind::Realloc)) {
518Attribute Attr = cast<CallBase>(
I)->getFnAttr(
"alloc-family");
525/// isLibFreeFunction - Returns true if the function is a builtin free() 531// Check free prototype. 532// FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 533// attribute will exist. 548if (TLI && TLI->
getLibFunc(*Callee, TLIFn) && TLI->
has(TLIFn) &&
550// All currently supported free functions free the first argument. 561//===----------------------------------------------------------------------===// 562// Utility functions to compute size of objects. 574/// Compute the size of the object pointed by Ptr. Returns true and the 575/// object size in Size if successful, and false otherwise. 576/// If RoundToAlign is true, then Size is rounded up to the alignment of 577/// allocas, byval arguments, and global variables. 582if (!Data.bothKnown())
602"ObjectSize must be a call to llvm.objectsize!");
604bool MaxVal = cast<ConstantInt>(ObjectSize->
getArgOperand(1))->isZero();
608// Unless we have to fold this to something, try to be as accurate as 612 MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
614 EvalOptions.
EvalMode = ObjectSizeOpts::Mode::ExactSizeFromOffset;
619auto *ResultType = cast<IntegerType>(ObjectSize->
getType());
620bool StaticOnly = cast<ConstantInt>(ObjectSize->
getArgOperand(3))->isZero();
622// FIXME: Does it make sense to just return a failure value if the size won't 623// fit in the output and `!MustSucceed`? 627return ConstantInt::get(ResultType,
Size);
636if (InsertedInstructions)
644// If we've outside the end of the object, then we can always access 650 UseZero, ConstantInt::get(ResultType, 0), ResultSize);
652// The non-constant size expression cannot evaluate to -1. 653if (!isa<Constant>(
Size) || !isa<Constant>(
Offset))
655 Builder.
CreateICmpNE(Ret, ConstantInt::get(ResultType, -1)));
669"Number of arguments with unsolved size and offset");
671"Number of load instructions with unsolved size and offset");
673static std::optional<APInt>
675 std::optional<APInt> RHS,
679if (EvalMode == ObjectSizeOpts::Mode::Max)
687constexprunsigned maxRecursionDepth = 4;
688if (recursionDepth == maxRecursionDepth)
691if (
constauto *CI = dyn_cast<ConstantInt>(V)) {
692return CI->getValue();
693 }
elseif (
constauto *SI = dyn_cast<SelectInst>(V)) {
700 }
elseif (
constauto *PN = dyn_cast<PHINode>(V)) {
701unsigned Count = PN->getNumIncomingValues();
705 PN->getIncomingValue(0), EvalMode, recursionDepth + 1);
706for (
unsignedI = 1; Acc &&
I < Count; ++
I) {
708 PN->getIncomingValue(
I), EvalMode, recursionDepth + 1);
717static std::optional<APInt>
719if (
auto *CI = dyn_cast<ConstantInt>(V))
720return CI->getValue();
722if (EvalMode != ObjectSizeOpts::Mode::Min &&
723 EvalMode != ObjectSizeOpts::Mode::Max)
726// Not using computeConstantRange here because we cannot guarantee it's not 727// doing optimization based on UB which we want to avoid when expanding 728// __builtin_object_size. 732/// Align \p Size according to \p Alignment. If \p Size is greater than 733/// getSignedMaxValue(), set it as unknown as we can only represent signed value 747// Pointer size must be rechecked for each object visited since it could have 748// a different address space. 752 InstructionsVisited = 0;
755// In ExactSizeFromOffset mode, we don't care about the Before Field, so allow 756// us to overwrite it if needs be. 770// Stripping pointer casts can strip address space casts which can change the 771// index type size. The invariant is that we use the value type to determine 772// the index type size and if we stripped address space casts we have to 773// readjust the APInt as we pass it upwards in order for the APInt to match 774// the type the caller passed in. 776 V = V->stripAndAccumulateConstantOffsets(
777 DL,
Offset,
/* AllowNonInbounds */true,
/* AllowInvariantGroup */true);
779// Give it another try with approximated analysis. We don't start with this 780// one because stripAndAccumulateConstantOffsets behaves differently wrt. 781// overflows if we provide an external Analysis. 784 isa<GEPOperator>(V)) {
785// External Analysis used to compute the Min/Max value of individual Offsets 792if (
auto PossibleOffset =
800V =
V->stripAndAccumulateConstantOffsets(
801 DL,
Offset,
/* AllowNonInbounds */true,
/* AllowInvariantGroup */true,
802/*ExternalAnalysis=*/OffsetRangeAnalysis);
805// Later we use the index type size and zero but it will match the type of the 806// value that is passed to computeImpl. 811bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
812if (!IndexTypeSizeChanged &&
Offset.isZero())
815// We stripped an address space cast that changed the index type size or we 816// accumulated some constant offset (or both). Readjust the bit width to match 817// the argument index type size and apply the offset, as required. 818if (IndexTypeSizeChanged) {
820 !::CheckedZextOrTrunc(ORT.
Before, InitialIntTyBits))
822if (ORT.
knownAfter() && !::CheckedZextOrTrunc(ORT.
After, InitialIntTyBits))
825// If the computed bound is "unknown" we cannot add the stripped offset. 839// We end up pointing on a location that's outside of the original object. 841// This means that we *may* be accessing memory before the allocation. 842// Conservatively return an unknown size. 844// TODO: working with ranges instead of value would make it possible to take 848return ObjectSizeOffsetVisitor::unknown();
850// Otherwise it's fine, caller can handle negative offset. 857// If we have already seen this instruction, bail out. Cycles can happen in 858// unreachable code after constant propagation. 859autoP = SeenInsts.try_emplace(
I, ObjectSizeOffsetVisitor::unknown());
861returnP.first->second;
862 ++InstructionsVisited;
864return ObjectSizeOffsetVisitor::unknown();
866// Cache the result for later visits. If we happened to visit this during 867// the above recursion, we would consider it unknown until now. 882LLVM_DEBUG(
dbgs() <<
"ObjectSizeOffsetVisitor::compute() unhandled value: " 884return ObjectSizeOffsetVisitor::unknown();
887bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(
APInt &
I) {
888 return ::CheckedZextOrTrunc(
I, IntTyBits);
894return ObjectSizeOffsetVisitor::unknown();
896return ObjectSizeOffsetVisitor::unknown();
899if (!
I.isArrayAllocation())
902Value *ArraySize =
I.getArraySize();
903if (
auto PossibleSize =
905APInt NumElems = *PossibleSize;
906if (!CheckedZextOrTrunc(NumElems))
907return ObjectSizeOffsetVisitor::unknown();
910Size =
Size.umul_ov(NumElems, Overflow);
912return Overflow ? ObjectSizeOffsetVisitor::unknown()
915return ObjectSizeOffsetVisitor::unknown();
919Type *MemoryTy =
A.getPointeeInMemoryValueType();
920// No interprocedural analysis is done at the moment. 921if (!MemoryTy|| !MemoryTy->
isSized()) {
922 ++ObjectVisitorArgument;
923return ObjectSizeOffsetVisitor::unknown();
931auto Mapper = [
this](
constValue *V) ->
constValue * {
932if (!V->getType()->isIntegerTy())
935if (
auto PossibleBound =
937return ConstantInt::get(V->getType(), *PossibleBound);
943// Very large unsigned value cannot be represented as OffsetSpan. 944if (
Size->isNegative())
945return ObjectSizeOffsetVisitor::unknown();
948return ObjectSizeOffsetVisitor::unknown();
953// If null is unknown, there's nothing we can do. Additionally, non-zero 954// address spaces can make use of null, so we don't presume to know anything 957// TODO: How should this work with address space casts? We currently just drop 958// them on the floor, but it's unclear what we should do when a NULL from 959// addrspace(1) gets casted to addrspace(0) (or vice-versa). 961return ObjectSizeOffsetVisitor::unknown();
967return ObjectSizeOffsetVisitor::unknown();
971// Easy cases were already folded by previous passes. 972return ObjectSizeOffsetVisitor::unknown();
977return ObjectSizeOffsetVisitor::unknown();
985return ObjectSizeOffsetVisitor::unknown();
993return ObjectSizeOffsetVisitor::unknown();
996OffsetSpan ObjectSizeOffsetVisitor::findLoadOffsetRange(
999unsigned &ScannedInstCount) {
1000constexprunsigned MaxInstsToScan = 128;
1002auto Where = VisitedBlocks.
find(&BB);
1003if (Where != VisitedBlocks.
end())
1004return Where->second;
1006autoUnknown = [&BB, &VisitedBlocks]() {
1007return VisitedBlocks[&BB] = ObjectSizeOffsetVisitor::unknown();
1009auto Known = [&BB, &VisitedBlocks](
OffsetSpan SO) {
1010return VisitedBlocks[&BB] = SO;
1016if (
I.isDebugOrPseudoInst())
1019if (++ScannedInstCount > MaxInstsToScan)
1022if (!
I.mayWriteToMemory())
1025if (
auto *SI = dyn_cast<StoreInst>(&
I)) {
1027 Options.
AA->
alias(
SI->getPointerOperand(),
Load.getPointerOperand());
1032if (
SI->getValueOperand()->getType()->isPointerTy())
1033return Known(computeImpl(
SI->getValueOperand()));
1035returnUnknown();
// No handling of non-pointer values by `compute`. 1041if (
auto *CB = dyn_cast<CallBase>(&
I)) {
1043// Bail out on indirect call. 1052// TODO: There's probably more interesting case to support here. 1053if (TLIFn != LibFunc_posix_memalign)
1067// Is the error status of posix_memalign correctly checked? If not it 1068// would be incorrect to assume it succeeds and load doesn't see the 1072if (!Checked || !*Checked)
1076auto *
C = dyn_cast<ConstantInt>(
Size);
1080APInt CSize =
C->getValue();
1092 PredecessorSizeOffsets.
push_back(findLoadOffsetRange(
1094 VisitedBlocks, ScannedInstCount));
1095if (!PredecessorSizeOffsets.
back().bothKnown())
1099if (PredecessorSizeOffsets.
empty())
1102return Known(std::accumulate(
1103 PredecessorSizeOffsets.
begin() + 1, PredecessorSizeOffsets.
end(),
1105 return combineOffsetRange(LHS, RHS);
1111 ++ObjectVisitorLoad;
1112return ObjectSizeOffsetVisitor::unknown();
1116unsigned ScannedInstCount = 0;
1119 VisitedBlocks, ScannedInstCount);
1121 ++ObjectVisitorLoad;
1127if (!
LHS.bothKnown() || !
RHS.bothKnown())
1128return ObjectSizeOffsetVisitor::unknown();
1132return {
LHS.Before.slt(
RHS.Before) ?
LHS.Before :
RHS.Before,
1135return {
LHS.Before.sgt(
RHS.Before) ?
LHS.Before :
RHS.Before,
1142return (LHS == RHS) ?
LHS : ObjectSizeOffsetVisitor::unknown();
1149return ObjectSizeOffsetVisitor::unknown();
1151return std::accumulate(IncomingValues.begin() + 1, IncomingValues.end(),
1152 computeImpl(*IncomingValues.begin()),
1154 return combineOffsetRange(LHS, computeImpl(VRHS));
1159return combineOffsetRange(computeImpl(
I.getTrueValue()),
1160 computeImpl(
I.getFalseValue()));
1170return ObjectSizeOffsetVisitor::unknown();
1173// Just set these right here... 1180 :
DL(
DL), TLI(TLI), Context(Context),
1184 EvalOpts(EvalOpts) {
1185// IntTy and Zero must be set for each compute() since the address space may 1186// be different for later objects. 1190// XXX - Are vectors of pointers possible here? 1191 IntTy = cast<IntegerType>(DL.
getIndexType(V->getType()));
1192 Zero = ConstantInt::get(IntTy, 0);
1196if (!Result.bothKnown()) {
1197// Erase everything that was computed in this iteration from the cache, so 1198// that no dangling references are left behind. We could be a bit smarter if 1199// we kept a dependency graph. It's probably not worth the complexity. 1200for (
constValue *SeenVal : SeenVals) {
1202// non-computable results can be safely cached 1203if (CacheIt != CacheMap.
end() && CacheIt->second.anyKnown())
1204 CacheMap.
erase(CacheIt);
1207// Erase any instructions we inserted as part of the traversal. 1210I->eraseFromParent();
1215 InsertedInstructions.clear();
1221// Only trust ObjectSizeOffsetVisitor in exact mode, otherwise fallback on 1222// dynamic computation. 1228if (Const.bothKnown())
1230 ConstantInt::get(Context, Const.Offset));
1232 V = V->stripPointerCasts();
1236if (CacheIt != CacheMap.
end())
1237return CacheIt->second;
1239// Always generate code immediately before the instruction being 1240// processed, so that the generated code dominates the same BBs. 1245// Now compute the size and offset. 1248// Record the pointers that were handled in this run, so that they can be 1249// cleaned later if something fails. We also use this set to break cycles that 1250// can occur in dead code. 1251if (!SeenVals.
insert(V).second) {
1257 }
elseif (isa<Argument>(V) ||
1258 (isa<ConstantExpr>(V) &&
1259 cast<ConstantExpr>(V)->
getOpcode() == Instruction::IntToPtr) ||
1260 isa<GlobalAlias>(V) ||
1261 isa<GlobalVariable>(V)) {
1262// Ignore values where we cannot do more than ObjectSizeVisitor. 1266dbgs() <<
"ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
1271// Don't reuse CacheIt since it may be invalid at this point. 1277if (!
I.getAllocatedType()->isSized())
1280// must be a VLA or vscale. 1281assert(
I.isArrayAllocation() ||
I.getAllocatedType()->isScalableTy());
1283// If needed, adjust the alloca's operand size to match the pointer indexing 1284// size. Subsequent math operations expect the types to match. 1289"Expected zero constant to have pointer index type");
1302// Handle strdup-like functions separately. 1304// TODO: implement evaluation of strdup/strndup 1310if (FnData->SndParam < 0)
1349// Create 2 PHIs: one for size and another for offset. 1353// Insert right away in the cache to handle recursive PHIs. 1356// Compute offset/size for each PHI incoming pointer. 1357for (
unsigned i = 0, e =
PHI.getNumIncomingValues(); i != e; ++i) {
1365 InsertedInstructions.erase(OffsetPHI);
1368 InsertedInstructions.erase(SizePHI);
1380 InsertedInstructions.erase(SizePHI);
1386 InsertedInstructions.erase(OffsetPHI);
1397if (TrueSide == FalseSide)
1408LLVM_DEBUG(
dbgs() <<
"ObjectSizeOffsetEvaluator unknown instruction:" <<
I This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static std::optional< APInt > combinePossibleConstantValues(std::optional< APInt > LHS, std::optional< APInt > RHS, ObjectSizeOpts::Mode EvalMode)
static AllocFnKind getAllocFnKind(const Value *V)
static std::optional< AllocFnsTy > getAllocationDataForFunction(const Function *Callee, AllocType AllocTy, const TargetLibraryInfo *TLI)
Returns the allocation data for the given value if it's a call to a known allocation function.
static std::optional< AllocFnsTy > getAllocationData(const Value *V, AllocType AllocTy, const TargetLibraryInfo *TLI)
std::optional< FreeFnsTy > getFreeFunctionDataForFunction(const Function *Callee, const LibFunc TLIFn)
static bool checkFnAllocKind(const Value *V, AllocFnKind Wanted)
StringRef mangledNameForMallocFamily(const MallocFamily &Family)
static std::optional< AllocFnsTy > getAllocationSize(const CallBase *CB, const TargetLibraryInfo *TLI)
static bool CheckedZextOrTrunc(APInt &I, unsigned IntTyBits)
When we're compiling N-bit code, and the user uses parameters that are greater than N bits (e....
static const std::pair< LibFunc, FreeFnsTy > FreeFnData[]
static const Function * getCalledFunction(const Value *V)
static std::optional< APInt > aggregatePossibleConstantValues(const Value *V, ObjectSizeOpts::Mode EvalMode)
static cl::opt< unsigned > ObjectSizeOffsetVisitorMaxVisitInstructions("object-size-offset-visitor-max-visit-instructions", cl::desc("Maximum number of instructions for ObjectSizeOffsetVisitor to " "look at"), cl::init(100))
static std::optional< APInt > aggregatePossibleConstantValuesImpl(const Value *V, ObjectSizeOpts::Mode EvalMode, unsigned recursionDepth)
static const std::pair< LibFunc, AllocFnsTy > AllocationFnData[]
static APInt getSizeWithOverflow(const SizeOffsetAPInt &Data)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
Class for arbitrary precision integers.
APInt zext(unsigned width) const
Zero extend to a new width.
uint64_t getZExtValue() const
Get zero extended value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool isNegative() const
Determine sign of this APInt.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
The possible results of an alias query.
@ NoAlias
The two locations do not alias at all.
@ MustAlias
The two locations precisely alias each other.
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
uint64_t getValueAsInt() const
Return the attribute's value as an integer.
std::pair< unsigned, std::optional< unsigned > > getAllocSizeArgs() const
Returns the argument numbers for the allocsize attribute.
StringRef getValueAsString() const
Return the attribute's value as a string.
bool isValid() const
Return true if the attribute is any kind of attribute.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
InstListType::iterator iterator
Instruction iterators...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
Value * getArgOperand(unsigned i) const
Value * getArgOperandWithAttribute(Attribute::AttrKind Kind) const
If one of the arguments has the specified attribute, returns its operand value.
unsigned arg_size() const
This is the shared class of boolean and integer constants.
const APInt & getValue() const
Return the constant as an APInt value reference.
A constant pointer value that points to null.
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
This is an important base class in LLVM.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
unsigned getAllocaAddrSpace() const
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
const Constant * getAliasee() const
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
bool hasExternalWeakLinkage() const
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Type * getValueType() const
bool hasInitializer() const
Definitions have initializers, declarations don't.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
CallInst * CreateAssumption(Value *Cond, ArrayRef< OperandBundleDef > OpBundles={})
Create an assume intrinsic call that allows the optimizer to assume that the provided condition will ...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateTypeSize(Type *DstType, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void visit(Iterator Start, Iterator End)
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
const Function * getFunction() const
Return the function this instruction belongs to.
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
This class represents a cast from an integer to a pointer.
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Evaluate the size and offset of an object pointed to by a Value*.
SizeOffsetValue visitExtractValueInst(ExtractValueInst &I)
SizeOffsetValue visitExtractElementInst(ExtractElementInst &I)
SizeOffsetValue compute(Value *V)
SizeOffsetValue visitInstruction(Instruction &I)
ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts EvalOpts={})
SizeOffsetValue visitLoadInst(LoadInst &I)
SizeOffsetValue visitGEPOperator(GEPOperator &GEP)
SizeOffsetValue visitIntToPtrInst(IntToPtrInst &)
SizeOffsetValue visitPHINode(PHINode &PHI)
SizeOffsetValue visitCallBase(CallBase &CB)
SizeOffsetValue visitSelectInst(SelectInst &I)
SizeOffsetValue visitAllocaInst(AllocaInst &I)
static SizeOffsetValue unknown()
Evaluate the size and offset of an object pointed to by a Value* statically.
OffsetSpan visitSelectInst(SelectInst &I)
OffsetSpan visitExtractValueInst(ExtractValueInst &I)
OffsetSpan visitConstantPointerNull(ConstantPointerNull &)
OffsetSpan visitExtractElementInst(ExtractElementInst &I)
OffsetSpan visitGlobalVariable(GlobalVariable &GV)
OffsetSpan visitCallBase(CallBase &CB)
OffsetSpan visitIntToPtrInst(IntToPtrInst &)
OffsetSpan visitAllocaInst(AllocaInst &I)
ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts Options={})
OffsetSpan visitLoadInst(LoadInst &I)
OffsetSpan visitPHINode(PHINode &)
OffsetSpan visitGlobalAlias(GlobalAlias &GA)
OffsetSpan visitInstruction(Instruction &I)
SizeOffsetAPInt compute(Value *V)
OffsetSpan visitUndefValue(UndefValue &)
OffsetSpan visitArgument(Argument &A)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
Value * hasConstantValue() const
If the specified PHI node always merges together the same value, return the value,...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents the LLVM 'select' instruction.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
TargetFolder - Create constants with target dependent folding.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
bool isVoidTy() const
Return true if this is 'void'.
'undef' values are things that do not have specified contents.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)
Return true if this is a call to an allocation function that does not have side effects that we are r...
std::optional< StringRef > getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI)
If a function is part of an allocation family (e.g.
Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
Value * getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI)
Gets the alignment argument for an aligned_alloc-like function, using either built-in knowledge based...
bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
Value * getReallocatedOperand(const CallBase *CB)
If this is a call to a realloc function, return the reallocated operand.
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory similar to malloc or...
bool isReallocLikeFn(const Function *F)
Tests if a function is a call or invoke to a library function that reallocates memory (e....
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
auto predecessors(const MachineBasicBlock *BB)
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
std::optional< APInt > getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI, function_ref< const Value *(const Value *)> Mapper=[](const Value *V) { return V;})
Return the size of the requested allocation.
std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
bool isNewLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory via new.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
Mode EvalMode
How we want to evaluate this object's size.
AAResults * AA
If set, used for more accurate evaluation.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
Mode
Controls how we handle conditional statements with unknown conditions.
@ ExactUnderlyingSizeAndOffset
All branches must be known and have the same underlying size and offset to be merged.
@ Max
Same as Min, except we pick the maximum size of all of the branches.
@ Min
Evaluate all branches of an unknown condition.
@ ExactSizeFromOffset
All branches must be known and have the same size, starting from the offset, to be merged.
OffsetSpan - Used internally by ObjectSizeOffsetVisitor.
APInt After
Number of allocated bytes before this point.
SizeOffsetAPInt - Used by ObjectSizeOffsetVisitor, which works with APInts.
SizeOffsetType - A base template class for the object size visitors.
SizeOffsetWeakTrackingVH - Used by ObjectSizeOffsetEvaluator in a DenseMap.