LLVM 23.0.0git
MemoryBuiltins.h
Go to the documentation of this file.
1//==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- C++ -*-==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This family of functions identifies calls to builtin functions that allocate
10// or free memory.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
15#define LLVM_ANALYSIS_MEMORYBUILTINS_H
16
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/DenseMap.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/InstVisitor.h"
23#include "llvm/IR/ValueHandle.h"
25#include <cstdint>
26#include <optional>
27#include <utility>
28
29namespace llvm {
30
31class AllocaInst;
32class AAResults;
33class Argument;
35class DataLayout;
38class GEPOperator;
39class GlobalAlias;
40class GlobalVariable;
41class Instruction;
42class IntegerType;
43class IntrinsicInst;
44class IntToPtrInst;
45class LLVMContext;
46class LoadInst;
47class PHINode;
48class SelectInst;
49class Type;
50class UndefValue;
51class Value;
52
53/// Tests if a value is a call or invoke to a library function that
54/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
55/// like).
56LLVM_ABI bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI);
57LLVM_ABI bool
58isAllocationFn(const Value *V,
59 function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
60
61/// Tests if a value is a call or invoke to a library function that
62/// allocates memory similar to malloc or calloc.
64 const TargetLibraryInfo *TLI);
65
66/// Tests if a value is a call or invoke to a library function that
67/// allocates memory (either malloc, calloc, or strdup like).
68LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI);
69
70/// Tests if a function is a call or invoke to a library function that
71/// reallocates memory (e.g., realloc).
73
74/// If this is a call to a realloc function, return the reallocated operand.
76
77//===----------------------------------------------------------------------===//
78// free Call Utility Functions.
79//
80
81/// isLibFreeFunction - Returns true if the function is a builtin free()
82LLVM_ABI bool isLibFreeFunction(const Function *F, const LibFunc TLIFn);
83
84/// If this if a call to a free function, return the freed operand.
86 const TargetLibraryInfo *TLI);
87
88//===----------------------------------------------------------------------===//
89// Properties of allocation functions
90//
91
92/// Return true if this is a call to an allocation function that does not have
93/// side effects that we are required to preserve beyond the effect of
94/// allocating a new object.
95/// Ex: If our allocation routine has a counter for the number of objects
96/// allocated, and the program prints it on exit, can the value change due
97/// to optimization? Answer is highly language dependent.
98/// Note: *Removable* really does mean removable; it does not mean observable.
99/// A language (e.g. C++) can allow removing allocations without allowing
100/// insertion or speculative execution of allocation routines.
101LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI);
102
103/// Gets the alignment argument for an aligned_alloc-like function, using either
104/// built-in knowledge based on fuction names/signatures or allocalign
105/// attributes. Note: the Value returned may not indicate a valid alignment, per
106/// the definition of the allocalign attribute.
108 const TargetLibraryInfo *TLI);
109
110/// Return the size of the requested allocation. With a trivial mapper, this is
111/// similar to calling getObjectSize(..., Exact), but without looking through
112/// calls that return their argument. A mapper function can be used to replace
113/// one Value* (operand to the allocation) with another. This is useful when
114/// doing abstract interpretation.
115LLVM_ABI std::optional<APInt> getAllocSize(
116 const CallBase *CB, const TargetLibraryInfo *TLI,
117 function_ref<const Value *(const Value *)> Mapper = [](const Value *V) {
118 return V;
119 });
120
121/// If this is a call to an allocation function that initializes memory to a
122/// fixed value, return said value in the requested type. Otherwise, return
123/// nullptr.
125 const TargetLibraryInfo *TLI,
126 Type *Ty);
127
128/// If a function is part of an allocation family (e.g.
129/// malloc/realloc/calloc/free), return the identifier for its family
130/// of functions.
131LLVM_ABI std::optional<StringRef>
132getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI);
133
134//===----------------------------------------------------------------------===//
135// Utility functions to compute size of objects.
136//
137
138/// Various options to control the behavior of getObjectSize.
140 /// Controls how we handle conditional statements with unknown conditions.
141 enum class Mode : uint8_t {
142 /// All branches must be known and have the same size, starting from the
143 /// offset, to be merged.
145 /// All branches must be known and have the same underlying size and offset
146 /// to be merged.
148 /// Evaluate all branches of an unknown condition. If all evaluations
149 /// succeed, pick the minimum size.
151 /// Same as Min, except we pick the maximum size of all of the branches.
153 };
154
155 /// How we want to evaluate this object's size.
157 /// Whether to round the result up to the alignment of allocas, byval
158 /// arguments, and global variables.
159 bool RoundToAlign = false;
160 /// If this is true, null pointers in address space 0 will be treated as
161 /// though they can't be evaluated. Otherwise, null is always considered to
162 /// point to a 0 byte region of memory.
163 bool NullIsUnknownSize = false;
164 /// If set, used for more accurate evaluation
165 AAResults *AA = nullptr;
166};
167
168/// Compute the size of the object pointed by Ptr. Returns true and the
169/// object size in Size if successful, and false otherwise. In this context, by
170/// object we mean the region of memory starting at Ptr to the end of the
171/// underlying object pointed to by Ptr.
172///
173/// WARNING: The object size returned is the allocation size. This does not
174/// imply dereferenceability at site of use since the object may be freeed in
175/// between.
176LLVM_ABI bool getObjectSize(const Value *Ptr, uint64_t &Size,
177 const DataLayout &DL, const TargetLibraryInfo *TLI,
178 ObjectSizeOpts Opts = {});
179
180/// Like getObjectSize(), but only returns the size of base objects (like
181/// allocas, global variables and allocator calls) and std::nullopt otherwise.
182/// Requires ExactSizeFromOffset mode.
183LLVM_ABI std::optional<TypeSize> getBaseObjectSize(const Value *Ptr,
184 const DataLayout &DL,
185 const TargetLibraryInfo *TLI,
186 ObjectSizeOpts Opts = {});
187
188/// Try to turn a call to \@llvm.objectsize into an integer value of the given
189/// Type. Returns null on failure. If MustSucceed is true, this function will
190/// not return null, and may return conservative values governed by the second
191/// argument of the call to objectsize.
193 const DataLayout &DL,
194 const TargetLibraryInfo *TLI,
195 bool MustSucceed);
197 IntrinsicInst *ObjectSize, const DataLayout &DL,
198 const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
199 SmallVectorImpl<Instruction *> *InsertedInstructions = nullptr);
200
201/// SizeOffsetType - A base template class for the object size visitors. Used
202/// here as a self-documenting way to handle the values rather than using a
203/// \p std::pair.
204template <typename T, class C> struct SizeOffsetType {
205public:
208
209 SizeOffsetType() = default;
212
213 bool knownSize() const { return C::known(Size); }
214 bool knownOffset() const { return C::known(Offset); }
215 bool anyKnown() const { return knownSize() || knownOffset(); }
216 bool bothKnown() const { return knownSize() && knownOffset(); }
217
219 return Size == RHS.Size && Offset == RHS.Offset;
220 }
222 return !(*this == RHS);
223 }
224};
225
226/// SizeOffsetAPInt - Used by \p ObjectSizeOffsetVisitor, which works with
227/// \p APInts.
228struct SizeOffsetAPInt : public SizeOffsetType<APInt, SizeOffsetAPInt> {
229 SizeOffsetAPInt() = default;
232
233 static bool known(const APInt &V) { return V.getBitWidth() > 1; }
234};
235
236/// OffsetSpan - Used internally by \p ObjectSizeOffsetVisitor. Represents a
237/// point in memory as a pair of allocated bytes before and after it.
238///
239/// \c Before and \c After fields are signed values. It makes it possible to
240/// represent out-of-bound access, e.g. as a result of a GEP, at the expense of
241/// not being able to represent very large allocation.
243 APInt Before; /// Number of allocated bytes before this point.
244 APInt After; /// Number of allocated bytes after this point.
245
246 OffsetSpan() = default;
248
249 bool knownBefore() const { return known(Before); }
250 bool knownAfter() const { return known(After); }
251 bool anyKnown() const { return knownBefore() || knownAfter(); }
252 bool bothKnown() const { return knownBefore() && knownAfter(); }
253
254 bool operator==(const OffsetSpan &RHS) const {
255 return Before == RHS.Before && After == RHS.After;
256 }
257 bool operator!=(const OffsetSpan &RHS) const { return !(*this == RHS); }
258
259 static bool known(const APInt &V) { return V.getBitWidth() > 1; }
260};
261
262/// Evaluate the size and offset of an object pointed to by a Value*
263/// statically. Fails if size or offset are not known at compile time.
265 : public InstVisitor<ObjectSizeOffsetVisitor, OffsetSpan> {
266 const DataLayout &DL;
267 const TargetLibraryInfo *TLI;
268 ObjectSizeOpts Options;
269 unsigned IntTyBits;
270 APInt Zero;
272 unsigned InstructionsVisited;
273
275
276 static OffsetSpan unknown() { return OffsetSpan(); }
277
278public:
280 const TargetLibraryInfo *TLI,
281 LLVMContext &Context,
282 ObjectSizeOpts Options = {});
283
285
286 // These are "private", except they can't actually be made private. Only
287 // compute() should be used by external users.
302
303private:
305 findLoadOffsetRange(LoadInst &LoadFrom, BasicBlock &BB,
308 unsigned &ScannedInstCount);
309 OffsetSpan combineOffsetRange(OffsetSpan LHS, OffsetSpan RHS);
310 OffsetSpan computeImpl(Value *V);
311 OffsetSpan computeValue(Value *V);
312 bool CheckedZextOrTrunc(APInt &I);
313};
314
315/// SizeOffsetValue - Used by \p ObjectSizeOffsetEvaluator, which works with
316/// \p Values.
318struct SizeOffsetValue : public SizeOffsetType<Value *, SizeOffsetValue> {
319 SizeOffsetValue() : SizeOffsetType(nullptr, nullptr) {}
322
323 static bool known(Value *V) { return V != nullptr; }
324};
325
326/// SizeOffsetWeakTrackingVH - Used by \p ObjectSizeOffsetEvaluator in a
327/// \p DenseMap.
329 : public SizeOffsetType<WeakTrackingVH, SizeOffsetWeakTrackingVH> {
335
336 static bool known(WeakTrackingVH V) { return V.pointsToAliveValue(); }
337};
338
339/// Evaluate the size and offset of an object pointed to by a Value*.
340/// May create code to compute the result at run-time.
342 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetValue> {
344 using WeakEvalType = SizeOffsetWeakTrackingVH;
345 using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
346 using PtrSetTy = SmallPtrSet<const Value *, 8>;
347
348 const DataLayout &DL;
349 const TargetLibraryInfo *TLI;
350 LLVMContext &Context;
351 BuilderTy Builder;
352 IntegerType *IntTy;
353 Value *Zero;
354 CacheMapTy CacheMap;
355 PtrSetTy SeenVals;
356 ObjectSizeOpts EvalOpts;
357 SmallPtrSet<Instruction *, 8> InsertedInstructions;
358
359 SizeOffsetValue compute_(Value *V);
360
361public:
363 const TargetLibraryInfo *TLI,
364 LLVMContext &Context,
365 ObjectSizeOpts EvalOpts = {});
366
368
370
371 // The individual instruction visitors should be treated as private.
382};
383
384} // end namespace llvm
385
386#endif // LLVM_ANALYSIS_MEMORYBUILTINS_H
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:213
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
This file defines the DenseMap class.
Hexagon Common GEP
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
This file defines the SmallPtrSet class.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
A constant pointer value that points to null.
Definition Constants.h:705
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
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.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2858
Base class for instruction visitors.
Definition InstVisitor.h:78
This class represents a cast from an integer to a pointer.
Class to represent integer types.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
LLVM_ABI SizeOffsetValue visitExtractValueInst(ExtractValueInst &I)
LLVM_ABI SizeOffsetValue visitExtractElementInst(ExtractElementInst &I)
LLVM_ABI SizeOffsetValue compute(Value *V)
LLVM_ABI SizeOffsetValue visitInstruction(Instruction &I)
LLVM_ABI ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts EvalOpts={})
LLVM_ABI SizeOffsetValue visitLoadInst(LoadInst &I)
LLVM_ABI SizeOffsetValue visitGEPOperator(GEPOperator &GEP)
LLVM_ABI SizeOffsetValue visitIntToPtrInst(IntToPtrInst &)
LLVM_ABI SizeOffsetValue visitPHINode(PHINode &PHI)
LLVM_ABI SizeOffsetValue visitCallBase(CallBase &CB)
LLVM_ABI SizeOffsetValue visitSelectInst(SelectInst &I)
LLVM_ABI SizeOffsetValue visitAllocaInst(AllocaInst &I)
static SizeOffsetValue unknown()
LLVM_ABI OffsetSpan visitSelectInst(SelectInst &I)
LLVM_ABI OffsetSpan visitExtractValueInst(ExtractValueInst &I)
LLVM_ABI OffsetSpan visitConstantPointerNull(ConstantPointerNull &)
LLVM_ABI OffsetSpan visitExtractElementInst(ExtractElementInst &I)
LLVM_ABI OffsetSpan visitGlobalVariable(GlobalVariable &GV)
LLVM_ABI OffsetSpan visitCallBase(CallBase &CB)
LLVM_ABI OffsetSpan visitIntToPtrInst(IntToPtrInst &)
LLVM_ABI OffsetSpan visitAllocaInst(AllocaInst &I)
LLVM_ABI ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts Options={})
LLVM_ABI OffsetSpan visitLoadInst(LoadInst &I)
LLVM_ABI OffsetSpan visitPHINode(PHINode &)
LLVM_ABI OffsetSpan visitGlobalAlias(GlobalAlias &GA)
LLVM_ABI OffsetSpan visitInstruction(Instruction &I)
LLVM_ABI SizeOffsetAPInt compute(Value *V)
LLVM_ABI OffsetSpan visitUndefValue(UndefValue &)
LLVM_ABI OffsetSpan visitArgument(Argument &A)
This class represents the LLVM 'select' instruction.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
'undef' values are things that do not have specified contents.
Definition Constants.h:1612
LLVM Value Representation.
Definition Value.h:75
Value handle that is nullable, but tries to track the Value.
An efficient, type-erasing, non-owning reference to a callable.
Abstract Attribute helper functions.
Definition Attributor.h:165
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI 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,...
LLVM_ABI 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...
LLVM_ABI std::optional< StringRef > getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI)
If a function is part of an allocation family (e.g.
LLVM_ABI 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.
LLVM_ABI Value * getAllocAlignment(const CallBase *V, const TargetLibraryInfo *TLI)
Gets the alignment argument for an aligned_alloc-like function, using either built-in knowledge based...
LLVM_ABI bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
LLVM_ABI Value * getReallocatedOperand(const CallBase *CB)
If this is a call to a realloc function, return the reallocated operand.
LLVM_ABI std::optional< TypeSize > getBaseObjectSize(const Value *Ptr, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Like getObjectSize(), but only returns the size of base objects (like allocas, global variables and a...
LLVM_ABI 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,...
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI bool isReallocLikeFn(const Function *F)
Tests if a function is a call or invoke to a library function that reallocates memory (e....
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1916
LLVM_ABI 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...
LLVM_ABI 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.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
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.
OffsetSpan()=default
Number of allocated bytes after this point.
bool knownBefore() const
APInt After
Number of allocated bytes before this point.
bool anyKnown() const
bool knownAfter() const
static bool known(const APInt &V)
bool operator!=(const OffsetSpan &RHS) const
bool operator==(const OffsetSpan &RHS) const
OffsetSpan(APInt Before, APInt After)
bool bothKnown() const
SizeOffsetAPInt - Used by ObjectSizeOffsetVisitor, which works with APInts.
static bool known(const APInt &V)
SizeOffsetAPInt(APInt Size, APInt Offset)
bool operator!=(const SizeOffsetType< T, C > &RHS) const
bool operator==(const SizeOffsetType< T, C > &RHS) const
SizeOffsetType()=default
SizeOffsetType(T Size, T Offset)
SizeOffsetValue(Value *Size, Value *Offset)
static bool known(Value *V)
SizeOffsetWeakTrackingVH - Used by ObjectSizeOffsetEvaluator in a DenseMap.
SizeOffsetWeakTrackingVH(const SizeOffsetValue &SOV)
static bool known(WeakTrackingVH V)
SizeOffsetWeakTrackingVH(Value *Size, Value *Offset)