• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions identifies calls to builtin functions that allocate
11 // or free memory.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
16 #define LLVM_ANALYSIS_MEMORYBUILTINS_H
17 
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Operator.h"
22 #include "llvm/InstVisitor.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/TargetFolder.h"
25 #include "llvm/Support/ValueHandle.h"
26 
27 namespace llvm {
28 class CallInst;
29 class PointerType;
30 class DataLayout;
31 class TargetLibraryInfo;
32 class Type;
33 class Value;
34 
35 
36 /// \brief Tests if a value is a call or invoke to a library function that
37 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
38 /// like).
39 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
40                     bool LookThroughBitCast = false);
41 
42 /// \brief Tests if a value is a call or invoke to a function that returns a
43 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
44 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
45                  bool LookThroughBitCast = false);
46 
47 /// \brief Tests if a value is a call or invoke to a library function that
48 /// allocates uninitialized memory (such as malloc).
49 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
50                     bool LookThroughBitCast = false);
51 
52 /// \brief Tests if a value is a call or invoke to a library function that
53 /// allocates zero-filled memory (such as calloc).
54 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
55                     bool LookThroughBitCast = false);
56 
57 /// \brief Tests if a value is a call or invoke to a library function that
58 /// allocates memory (either malloc, calloc, or strdup like).
59 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
60                    bool LookThroughBitCast = false);
61 
62 /// \brief Tests if a value is a call or invoke to a library function that
63 /// reallocates memory (such as realloc).
64 bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
65                      bool LookThroughBitCast = false);
66 
67 
68 //===----------------------------------------------------------------------===//
69 //  malloc Call Utility Functions.
70 //
71 
72 /// extractMallocCall - Returns the corresponding CallInst if the instruction
73 /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
74 /// ignore InvokeInst here.
75 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
extractMallocCall(Value * I,const TargetLibraryInfo * TLI)76 static inline CallInst *extractMallocCall(Value *I,
77                                           const TargetLibraryInfo *TLI) {
78   return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
79 }
80 
81 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
82 /// is a call to malloc whose array size can be determined and the array size
83 /// is not constant 1.  Otherwise, return NULL.
84 const CallInst *isArrayMalloc(const Value *I, const DataLayout *TD,
85                               const TargetLibraryInfo *TLI);
86 
87 /// getMallocType - Returns the PointerType resulting from the malloc call.
88 /// The PointerType 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 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
93 
94 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
95 /// The Type depends on the number of bitcast uses of the malloc call:
96 ///   0: PointerType is the malloc calls' return type.
97 ///   1: PointerType is the bitcast's result type.
98 ///  >1: Unique PointerType cannot be determined, return NULL.
99 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
100 
101 /// getMallocArraySize - Returns the array size of a malloc call.  If the
102 /// argument passed to malloc is a multiple of the size of the malloced type,
103 /// then return that multiple.  For non-array mallocs, the multiple is
104 /// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
105 /// determined.
106 Value *getMallocArraySize(CallInst *CI, const DataLayout *TD,
107                           const TargetLibraryInfo *TLI,
108                           bool LookThroughSExt = false);
109 
110 
111 //===----------------------------------------------------------------------===//
112 //  calloc Call Utility Functions.
113 //
114 
115 /// extractCallocCall - Returns the corresponding CallInst if the instruction
116 /// is a calloc call.
117 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
extractCallocCall(Value * I,const TargetLibraryInfo * TLI)118 static inline CallInst *extractCallocCall(Value *I,
119                                           const TargetLibraryInfo *TLI) {
120   return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
121 }
122 
123 
124 //===----------------------------------------------------------------------===//
125 //  free Call Utility Functions.
126 //
127 
128 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
129 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
130 
isFreeCall(Value * I,const TargetLibraryInfo * TLI)131 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
132   return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
133 }
134 
135 
136 //===----------------------------------------------------------------------===//
137 //  Utility functions to compute size of objects.
138 //
139 
140 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
141 /// object size in Size if successful, and false otherwise. In this context, by
142 /// object we mean the region of memory starting at Ptr to the end of the
143 /// underlying object pointed to by Ptr.
144 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
145 /// byval arguments, and global variables.
146 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
147                    const TargetLibraryInfo *TLI, bool RoundToAlign = false);
148 
149 /// \brief Compute the size of the underlying object pointed by Ptr. Returns
150 /// true and the object size in Size if successful, and false otherwise.
151 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
152 /// byval arguments, and global variables.
153 bool getUnderlyingObjectSize(const Value *Ptr, uint64_t &Size,
154                              const DataLayout *TD, const TargetLibraryInfo *TLI,
155                              bool RoundToAlign = false);
156 
157 
158 
159 typedef std::pair<APInt, APInt> SizeOffsetType;
160 
161 /// \brief Evaluate the size and offset of an object ponted by a Value*
162 /// statically. Fails if size or offset are not known at compile time.
163 class ObjectSizeOffsetVisitor
164   : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
165 
166   typedef DenseMap<const Value*, SizeOffsetType> CacheMapTy;
167 
168   const DataLayout *TD;
169   const TargetLibraryInfo *TLI;
170   bool RoundToAlign;
171   unsigned IntTyBits;
172   APInt Zero;
173   CacheMapTy CacheMap;
174 
175   APInt align(APInt Size, uint64_t Align);
176 
unknown()177   SizeOffsetType unknown() {
178     return std::make_pair(APInt(), APInt());
179   }
180 
181 public:
182   ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI,
183                           LLVMContext &Context, bool RoundToAlign = false);
184 
185   SizeOffsetType compute(Value *V);
186 
knownSize(SizeOffsetType & SizeOffset)187   bool knownSize(SizeOffsetType &SizeOffset) {
188     return SizeOffset.first.getBitWidth() > 1;
189   }
190 
knownOffset(SizeOffsetType & SizeOffset)191   bool knownOffset(SizeOffsetType &SizeOffset) {
192     return SizeOffset.second.getBitWidth() > 1;
193   }
194 
bothKnown(SizeOffsetType & SizeOffset)195   bool bothKnown(SizeOffsetType &SizeOffset) {
196     return knownSize(SizeOffset) && knownOffset(SizeOffset);
197   }
198 
199   SizeOffsetType visitAllocaInst(AllocaInst &I);
200   SizeOffsetType visitArgument(Argument &A);
201   SizeOffsetType visitCallSite(CallSite CS);
202   SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
203   SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
204   SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
205   SizeOffsetType visitGEPOperator(GEPOperator &GEP);
206   SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
207   SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
208   SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
209   SizeOffsetType visitLoadInst(LoadInst &I);
210   SizeOffsetType visitPHINode(PHINode&);
211   SizeOffsetType visitSelectInst(SelectInst &I);
212   SizeOffsetType visitUndefValue(UndefValue&);
213   SizeOffsetType visitInstruction(Instruction &I);
214 };
215 
216 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
217 
218 
219 /// \brief Evaluate the size and offset of an object ponted by a Value*.
220 /// May create code to compute the result at run-time.
221 class ObjectSizeOffsetEvaluator
222   : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
223 
224   typedef IRBuilder<true, TargetFolder> BuilderTy;
225   typedef std::pair<WeakVH, WeakVH> WeakEvalType;
226   typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
227   typedef SmallPtrSet<const Value*, 8> PtrSetTy;
228 
229   const DataLayout *TD;
230   const TargetLibraryInfo *TLI;
231   LLVMContext &Context;
232   BuilderTy Builder;
233   IntegerType *IntTy;
234   Value *Zero;
235   CacheMapTy CacheMap;
236   PtrSetTy SeenVals;
237 
unknown()238   SizeOffsetEvalType unknown() {
239     return std::make_pair((Value*)0, (Value*)0);
240   }
241   SizeOffsetEvalType compute_(Value *V);
242 
243 public:
244   ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI,
245                             LLVMContext &Context);
246   SizeOffsetEvalType compute(Value *V);
247 
knownSize(SizeOffsetEvalType SizeOffset)248   bool knownSize(SizeOffsetEvalType SizeOffset) {
249     return SizeOffset.first;
250   }
251 
knownOffset(SizeOffsetEvalType SizeOffset)252   bool knownOffset(SizeOffsetEvalType SizeOffset) {
253     return SizeOffset.second;
254   }
255 
anyKnown(SizeOffsetEvalType SizeOffset)256   bool anyKnown(SizeOffsetEvalType SizeOffset) {
257     return knownSize(SizeOffset) || knownOffset(SizeOffset);
258   }
259 
bothKnown(SizeOffsetEvalType SizeOffset)260   bool bothKnown(SizeOffsetEvalType SizeOffset) {
261     return knownSize(SizeOffset) && knownOffset(SizeOffset);
262   }
263 
264   SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
265   SizeOffsetEvalType visitCallSite(CallSite CS);
266   SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
267   SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
268   SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
269   SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
270   SizeOffsetEvalType visitLoadInst(LoadInst &I);
271   SizeOffsetEvalType visitPHINode(PHINode &PHI);
272   SizeOffsetEvalType visitSelectInst(SelectInst &I);
273   SizeOffsetEvalType visitInstruction(Instruction &I);
274 };
275 
276 } // End llvm namespace
277 
278 #endif
279