1 //===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to builtin functions that allocate
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
16 #define LLVM_ANALYSIS_MEMORYBUILTINS_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/TargetFolder.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/IR/Operator.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Support/DataTypes.h"
31 class TargetLibraryInfo;
35 enum class ObjSizeMode {
41 /// \brief Tests if a value is a call or invoke to a library function that
42 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
44 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
45 bool LookThroughBitCast = false);
47 /// \brief Tests if a value is a call or invoke to a function that returns a
48 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
49 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
50 bool LookThroughBitCast = false);
52 /// \brief Tests if a value is a call or invoke to a library function that
53 /// allocates uninitialized memory (such as malloc).
54 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
55 bool LookThroughBitCast = false);
57 /// \brief Tests if a value is a call or invoke to a library function that
58 /// allocates zero-filled memory (such as calloc).
59 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
60 bool LookThroughBitCast = false);
62 /// \brief Tests if a value is a call or invoke to a library function that
63 /// allocates memory (either malloc, calloc, or strdup like).
64 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
65 bool LookThroughBitCast = false);
67 //===----------------------------------------------------------------------===//
68 // malloc Call Utility Functions.
71 /// extractMallocCall - Returns the corresponding CallInst if the instruction
72 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
73 /// ignore InvokeInst here.
74 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
75 static inline CallInst *extractMallocCall(Value *I,
76 const TargetLibraryInfo *TLI) {
77 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
80 /// getMallocType - Returns the PointerType resulting from the malloc call.
81 /// The PointerType depends on the number of bitcast uses of the malloc call:
82 /// 0: PointerType is the malloc calls' return type.
83 /// 1: PointerType is the bitcast's result type.
84 /// >1: Unique PointerType cannot be determined, return NULL.
85 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
87 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
88 /// The Type depends on the number of bitcast uses of the malloc call:
89 /// 0: PointerType is the malloc calls' return type.
90 /// 1: PointerType is the bitcast's result type.
91 /// >1: Unique PointerType cannot be determined, return NULL.
92 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
94 /// getMallocArraySize - Returns the array size of a malloc call. If the
95 /// argument passed to malloc is a multiple of the size of the malloced type,
96 /// then return that multiple. For non-array mallocs, the multiple is
97 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
99 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
100 const TargetLibraryInfo *TLI,
101 bool LookThroughSExt = false);
103 //===----------------------------------------------------------------------===//
104 // calloc Call Utility Functions.
107 /// extractCallocCall - Returns the corresponding CallInst if the instruction
108 /// is a calloc call.
109 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
110 static inline CallInst *extractCallocCall(Value *I,
111 const TargetLibraryInfo *TLI) {
112 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
116 //===----------------------------------------------------------------------===//
117 // free Call Utility Functions.
120 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
121 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
123 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
124 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
128 //===----------------------------------------------------------------------===//
129 // Utility functions to compute size of objects.
132 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
133 /// object size in Size if successful, and false otherwise. In this context, by
134 /// object we mean the region of memory starting at Ptr to the end of the
135 /// underlying object pointed to by Ptr.
136 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
137 /// byval arguments, and global variables.
138 /// If Mode is Min or Max the size will be evaluated even if it depends on
139 /// a condition and corresponding value will be returned (min or max).
140 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
141 const TargetLibraryInfo *TLI, bool RoundToAlign = false,
142 ObjSizeMode Mode = ObjSizeMode::Exact);
144 /// Try to turn a call to @llvm.objectsize into an integer value of the given
145 /// Type. Returns null on failure.
146 /// If MustSucceed is true, this function will not return null, and may return
147 /// conservative values governed by the second argument of the call to
149 ConstantInt *lowerObjectSizeCall(IntrinsicInst *ObjectSize,
150 const DataLayout &DL,
151 const TargetLibraryInfo *TLI,
154 typedef std::pair<APInt, APInt> SizeOffsetType;
156 /// \brief Evaluate the size and offset of an object pointed to by a Value*
157 /// statically. Fails if size or offset are not known at compile time.
158 class ObjectSizeOffsetVisitor
159 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
161 const DataLayout &DL;
162 const TargetLibraryInfo *TLI;
167 SmallPtrSet<Instruction *, 8> SeenInsts;
169 APInt align(APInt Size, uint64_t Align);
171 SizeOffsetType unknown() {
172 return std::make_pair(APInt(), APInt());
176 ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
177 LLVMContext &Context, bool RoundToAlign = false,
178 ObjSizeMode Mode = ObjSizeMode::Exact);
180 SizeOffsetType compute(Value *V);
182 static bool knownSize(const SizeOffsetType &SizeOffset) {
183 return SizeOffset.first.getBitWidth() > 1;
186 static bool knownOffset(const SizeOffsetType &SizeOffset) {
187 return SizeOffset.second.getBitWidth() > 1;
190 static bool bothKnown(const SizeOffsetType &SizeOffset) {
191 return knownSize(SizeOffset) && knownOffset(SizeOffset);
194 // These are "private", except they can't actually be made private. Only
195 // compute() should be used by external users.
196 SizeOffsetType visitAllocaInst(AllocaInst &I);
197 SizeOffsetType visitArgument(Argument &A);
198 SizeOffsetType visitCallSite(CallSite CS);
199 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
200 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
201 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
202 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
203 SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
204 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
205 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
206 SizeOffsetType visitLoadInst(LoadInst &I);
207 SizeOffsetType visitPHINode(PHINode&);
208 SizeOffsetType visitSelectInst(SelectInst &I);
209 SizeOffsetType visitUndefValue(UndefValue&);
210 SizeOffsetType visitInstruction(Instruction &I);
213 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
216 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
217 /// May create code to compute the result at run-time.
218 class ObjectSizeOffsetEvaluator
219 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
221 typedef IRBuilder<TargetFolder> BuilderTy;
222 typedef std::pair<WeakVH, WeakVH> WeakEvalType;
223 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
224 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
226 const DataLayout &DL;
227 const TargetLibraryInfo *TLI;
228 LLVMContext &Context;
236 SizeOffsetEvalType unknown() {
237 return std::make_pair(nullptr, nullptr);
239 SizeOffsetEvalType compute_(Value *V);
242 ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
243 LLVMContext &Context, bool RoundToAlign = false);
244 SizeOffsetEvalType compute(Value *V);
246 bool knownSize(SizeOffsetEvalType SizeOffset) {
247 return SizeOffset.first;
250 bool knownOffset(SizeOffsetEvalType SizeOffset) {
251 return SizeOffset.second;
254 bool anyKnown(SizeOffsetEvalType SizeOffset) {
255 return knownSize(SizeOffset) || knownOffset(SizeOffset);
258 bool bothKnown(SizeOffsetEvalType SizeOffset) {
259 return knownSize(SizeOffset) && knownOffset(SizeOffset);
262 // The individual instruction visitors should be treated as private.
263 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
264 SizeOffsetEvalType visitCallSite(CallSite CS);
265 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
266 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
267 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
268 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
269 SizeOffsetEvalType visitLoadInst(LoadInst &I);
270 SizeOffsetEvalType visitPHINode(PHINode &PHI);
271 SizeOffsetEvalType visitSelectInst(SelectInst &I);
272 SizeOffsetEvalType visitInstruction(Instruction &I);
275 } // End llvm namespace