1 //==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- C++ -*-==//
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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
15 #define LLVM_ANALYSIS_MEMORYBUILTINS_H
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/TargetFolder.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/CallSite.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/InstVisitor.h"
25 #include "llvm/IR/ValueHandle.h"
35 class ConstantPointerNull
;
37 class ExtractElementInst
;
38 class ExtractValueInst
;
51 class TargetLibraryInfo
;
56 /// Tests if a value is a call or invoke to a library function that
57 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
59 bool isAllocationFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
60 bool LookThroughBitCast
= false);
61 bool isAllocationFn(const Value
*V
,
62 function_ref
<const TargetLibraryInfo
&(Function
&)> GetTLI
,
63 bool LookThroughBitCast
= false);
65 /// Tests if a value is a call or invoke to a function that returns a
66 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
67 bool isNoAliasFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
68 bool LookThroughBitCast
= false);
70 /// Tests if a value is a call or invoke to a library function that
71 /// allocates uninitialized memory (such as malloc).
72 bool isMallocLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
73 bool LookThroughBitCast
= false);
74 bool isMallocLikeFn(const Value
*V
,
75 function_ref
<const TargetLibraryInfo
&(Function
&)> GetTLI
,
76 bool LookThroughBitCast
= false);
78 /// Tests if a value is a call or invoke to a library function that
79 /// allocates zero-filled memory (such as calloc).
80 bool isCallocLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
81 bool LookThroughBitCast
= false);
83 /// Tests if a value is a call or invoke to a library function that
84 /// allocates memory similar to malloc or calloc.
85 bool isMallocOrCallocLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
86 bool LookThroughBitCast
= false);
88 /// Tests if a value is a call or invoke to a library function that
89 /// allocates memory (either malloc, calloc, or strdup like).
90 bool isAllocLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
91 bool LookThroughBitCast
= false);
93 /// Tests if a value is a call or invoke to a library function that
94 /// reallocates memory (e.g., realloc).
95 bool isReallocLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
96 bool LookThroughBitCast
= false);
98 /// Tests if a function is a call or invoke to a library function that
99 /// reallocates memory (e.g., realloc).
100 bool isReallocLikeFn(const Function
*F
, const TargetLibraryInfo
*TLI
);
102 /// Tests if a value is a call or invoke to a library function that
103 /// allocates memory and throws if an allocation failed (e.g., new).
104 bool isOpNewLikeFn(const Value
*V
, const TargetLibraryInfo
*TLI
,
105 bool LookThroughBitCast
= false);
107 //===----------------------------------------------------------------------===//
108 // malloc Call Utility Functions.
111 /// extractMallocCall - Returns the corresponding CallInst if the instruction
112 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
113 /// ignore InvokeInst here.
115 extractMallocCall(const Value
*I
,
116 function_ref
<const TargetLibraryInfo
&(Function
&)> GetTLI
);
118 extractMallocCall(Value
*I
,
119 function_ref
<const TargetLibraryInfo
&(Function
&)> GetTLI
) {
120 return const_cast<CallInst
*>(extractMallocCall((const Value
*)I
, GetTLI
));
123 /// getMallocType - Returns the PointerType resulting from the malloc call.
124 /// The PointerType depends on the number of bitcast uses of the malloc call:
125 /// 0: PointerType is the malloc calls' return type.
126 /// 1: PointerType is the bitcast's result type.
127 /// >1: Unique PointerType cannot be determined, return NULL.
128 PointerType
*getMallocType(const CallInst
*CI
, const TargetLibraryInfo
*TLI
);
130 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
131 /// The Type depends on the number of bitcast uses of the malloc call:
132 /// 0: PointerType is the malloc calls' return type.
133 /// 1: PointerType is the bitcast's result type.
134 /// >1: Unique PointerType cannot be determined, return NULL.
135 Type
*getMallocAllocatedType(const CallInst
*CI
, const TargetLibraryInfo
*TLI
);
137 /// getMallocArraySize - Returns the array size of a malloc call. If the
138 /// argument passed to malloc is a multiple of the size of the malloced type,
139 /// then return that multiple. For non-array mallocs, the multiple is
140 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
142 Value
*getMallocArraySize(CallInst
*CI
, const DataLayout
&DL
,
143 const TargetLibraryInfo
*TLI
,
144 bool LookThroughSExt
= false);
146 //===----------------------------------------------------------------------===//
147 // calloc Call Utility Functions.
150 /// extractCallocCall - Returns the corresponding CallInst if the instruction
151 /// is a calloc call.
152 const CallInst
*extractCallocCall(const Value
*I
, const TargetLibraryInfo
*TLI
);
153 inline CallInst
*extractCallocCall(Value
*I
, const TargetLibraryInfo
*TLI
) {
154 return const_cast<CallInst
*>(extractCallocCall((const Value
*)I
, TLI
));
158 //===----------------------------------------------------------------------===//
159 // free Call Utility Functions.
162 /// isLibFreeFunction - Returns true if the function is a builtin free()
163 bool isLibFreeFunction(const Function
*F
, const LibFunc TLIFn
);
165 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
166 const CallInst
*isFreeCall(const Value
*I
, const TargetLibraryInfo
*TLI
);
168 inline CallInst
*isFreeCall(Value
*I
, const TargetLibraryInfo
*TLI
) {
169 return const_cast<CallInst
*>(isFreeCall((const Value
*)I
, TLI
));
172 //===----------------------------------------------------------------------===//
173 // Utility functions to compute size of objects.
176 /// Various options to control the behavior of getObjectSize.
177 struct ObjectSizeOpts
{
178 /// Controls how we handle conditional statements with unknown conditions.
179 enum class Mode
: uint8_t {
180 /// Fail to evaluate an unknown condition.
182 /// Evaluate all branches of an unknown condition. If all evaluations
183 /// succeed, pick the minimum size.
185 /// Same as Min, except we pick the maximum size of all of the branches.
189 /// How we want to evaluate this object's size.
190 Mode EvalMode
= Mode::Exact
;
191 /// Whether to round the result up to the alignment of allocas, byval
192 /// arguments, and global variables.
193 bool RoundToAlign
= false;
194 /// If this is true, null pointers in address space 0 will be treated as
195 /// though they can't be evaluated. Otherwise, null is always considered to
196 /// point to a 0 byte region of memory.
197 bool NullIsUnknownSize
= false;
200 /// Compute the size of the object pointed by Ptr. Returns true and the
201 /// object size in Size if successful, and false otherwise. In this context, by
202 /// object we mean the region of memory starting at Ptr to the end of the
203 /// underlying object pointed to by Ptr.
204 bool getObjectSize(const Value
*Ptr
, uint64_t &Size
, const DataLayout
&DL
,
205 const TargetLibraryInfo
*TLI
, ObjectSizeOpts Opts
= {});
207 /// Try to turn a call to \@llvm.objectsize into an integer value of the given
208 /// Type. Returns null on failure. If MustSucceed is true, this function will
209 /// not return null, and may return conservative values governed by the second
210 /// argument of the call to objectsize.
211 Value
*lowerObjectSizeCall(IntrinsicInst
*ObjectSize
, const DataLayout
&DL
,
212 const TargetLibraryInfo
*TLI
, bool MustSucceed
);
216 using SizeOffsetType
= std::pair
<APInt
, APInt
>;
218 /// Evaluate the size and offset of an object pointed to by a Value*
219 /// statically. Fails if size or offset are not known at compile time.
220 class ObjectSizeOffsetVisitor
221 : public InstVisitor
<ObjectSizeOffsetVisitor
, SizeOffsetType
> {
222 const DataLayout
&DL
;
223 const TargetLibraryInfo
*TLI
;
224 ObjectSizeOpts Options
;
227 SmallPtrSet
<Instruction
*, 8> SeenInsts
;
229 APInt
align(APInt Size
, uint64_t Align
);
231 SizeOffsetType
unknown() {
232 return std::make_pair(APInt(), APInt());
236 ObjectSizeOffsetVisitor(const DataLayout
&DL
, const TargetLibraryInfo
*TLI
,
237 LLVMContext
&Context
, ObjectSizeOpts Options
= {});
239 SizeOffsetType
compute(Value
*V
);
241 static bool knownSize(const SizeOffsetType
&SizeOffset
) {
242 return SizeOffset
.first
.getBitWidth() > 1;
245 static bool knownOffset(const SizeOffsetType
&SizeOffset
) {
246 return SizeOffset
.second
.getBitWidth() > 1;
249 static bool bothKnown(const SizeOffsetType
&SizeOffset
) {
250 return knownSize(SizeOffset
) && knownOffset(SizeOffset
);
253 // These are "private", except they can't actually be made private. Only
254 // compute() should be used by external users.
255 SizeOffsetType
visitAllocaInst(AllocaInst
&I
);
256 SizeOffsetType
visitArgument(Argument
&A
);
257 SizeOffsetType
visitCallSite(CallSite CS
);
258 SizeOffsetType
visitConstantPointerNull(ConstantPointerNull
&);
259 SizeOffsetType
visitExtractElementInst(ExtractElementInst
&I
);
260 SizeOffsetType
visitExtractValueInst(ExtractValueInst
&I
);
261 SizeOffsetType
visitGEPOperator(GEPOperator
&GEP
);
262 SizeOffsetType
visitGlobalAlias(GlobalAlias
&GA
);
263 SizeOffsetType
visitGlobalVariable(GlobalVariable
&GV
);
264 SizeOffsetType
visitIntToPtrInst(IntToPtrInst
&);
265 SizeOffsetType
visitLoadInst(LoadInst
&I
);
266 SizeOffsetType
visitPHINode(PHINode
&);
267 SizeOffsetType
visitSelectInst(SelectInst
&I
);
268 SizeOffsetType
visitUndefValue(UndefValue
&);
269 SizeOffsetType
visitInstruction(Instruction
&I
);
272 bool CheckedZextOrTrunc(APInt
&I
);
275 using SizeOffsetEvalType
= std::pair
<Value
*, Value
*>;
277 /// Evaluate the size and offset of an object pointed to by a Value*.
278 /// May create code to compute the result at run-time.
279 class ObjectSizeOffsetEvaluator
280 : public InstVisitor
<ObjectSizeOffsetEvaluator
, SizeOffsetEvalType
> {
281 using BuilderTy
= IRBuilder
<TargetFolder
, IRBuilderCallbackInserter
>;
282 using WeakEvalType
= std::pair
<WeakTrackingVH
, WeakTrackingVH
>;
283 using CacheMapTy
= DenseMap
<const Value
*, WeakEvalType
>;
284 using PtrSetTy
= SmallPtrSet
<const Value
*, 8>;
286 const DataLayout
&DL
;
287 const TargetLibraryInfo
*TLI
;
288 LLVMContext
&Context
;
294 ObjectSizeOpts EvalOpts
;
295 SmallPtrSet
<Instruction
*, 8> InsertedInstructions
;
297 SizeOffsetEvalType
compute_(Value
*V
);
300 static SizeOffsetEvalType
unknown() {
301 return std::make_pair(nullptr, nullptr);
304 ObjectSizeOffsetEvaluator(const DataLayout
&DL
, const TargetLibraryInfo
*TLI
,
305 LLVMContext
&Context
, ObjectSizeOpts EvalOpts
= {});
307 SizeOffsetEvalType
compute(Value
*V
);
309 bool knownSize(SizeOffsetEvalType SizeOffset
) {
310 return SizeOffset
.first
;
313 bool knownOffset(SizeOffsetEvalType SizeOffset
) {
314 return SizeOffset
.second
;
317 bool anyKnown(SizeOffsetEvalType SizeOffset
) {
318 return knownSize(SizeOffset
) || knownOffset(SizeOffset
);
321 bool bothKnown(SizeOffsetEvalType SizeOffset
) {
322 return knownSize(SizeOffset
) && knownOffset(SizeOffset
);
325 // The individual instruction visitors should be treated as private.
326 SizeOffsetEvalType
visitAllocaInst(AllocaInst
&I
);
327 SizeOffsetEvalType
visitCallSite(CallSite CS
);
328 SizeOffsetEvalType
visitExtractElementInst(ExtractElementInst
&I
);
329 SizeOffsetEvalType
visitExtractValueInst(ExtractValueInst
&I
);
330 SizeOffsetEvalType
visitGEPOperator(GEPOperator
&GEP
);
331 SizeOffsetEvalType
visitIntToPtrInst(IntToPtrInst
&);
332 SizeOffsetEvalType
visitLoadInst(LoadInst
&I
);
333 SizeOffsetEvalType
visitPHINode(PHINode
&PHI
);
334 SizeOffsetEvalType
visitSelectInst(SelectInst
&I
);
335 SizeOffsetEvalType
visitInstruction(Instruction
&I
);
338 } // end namespace llvm
340 #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H