87fb3efaf50e3e33a6255882328f2ff99a076757
[oota-llvm.git] / include / llvm / Analysis / MemoryBuiltins.h
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/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"
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 /// allocates memory and never returns null (such as operator new).
64 bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
65                          bool LookThroughBitCast = false);
66
67 //===----------------------------------------------------------------------===//
68 //  malloc Call Utility Functions.
69 //
70
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));
78 }
79
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);
86
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);
93
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
98 /// determined.
99 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
100                           const TargetLibraryInfo *TLI,
101                           bool LookThroughSExt = false);
102
103 //===----------------------------------------------------------------------===//
104 //  calloc Call Utility Functions.
105 //
106
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));
113 }
114
115
116 //===----------------------------------------------------------------------===//
117 //  free Call Utility Functions.
118 //
119
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);
122
123 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
124   return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
125 }
126
127
128 //===----------------------------------------------------------------------===//
129 //  Utility functions to compute size of objects.
130 //
131
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 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
139                    const TargetLibraryInfo *TLI, bool RoundToAlign = false);
140
141 typedef std::pair<APInt, APInt> SizeOffsetType;
142
143 /// \brief Evaluate the size and offset of an object pointed to by a Value*
144 /// statically. Fails if size or offset are not known at compile time.
145 class ObjectSizeOffsetVisitor
146   : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
147
148   const DataLayout &DL;
149   const TargetLibraryInfo *TLI;
150   bool RoundToAlign;
151   unsigned IntTyBits;
152   APInt Zero;
153   SmallPtrSet<Instruction *, 8> SeenInsts;
154
155   APInt align(APInt Size, uint64_t Align);
156
157   SizeOffsetType unknown() {
158     return std::make_pair(APInt(), APInt());
159   }
160
161 public:
162   ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
163                           LLVMContext &Context, bool RoundToAlign = false);
164
165   SizeOffsetType compute(Value *V);
166
167   bool knownSize(SizeOffsetType &SizeOffset) {
168     return SizeOffset.first.getBitWidth() > 1;
169   }
170
171   bool knownOffset(SizeOffsetType &SizeOffset) {
172     return SizeOffset.second.getBitWidth() > 1;
173   }
174
175   bool bothKnown(SizeOffsetType &SizeOffset) {
176     return knownSize(SizeOffset) && knownOffset(SizeOffset);
177   }
178
179   // These are "private", except they can't actually be made private. Only
180   // compute() should be used by external users.
181   SizeOffsetType visitAllocaInst(AllocaInst &I);
182   SizeOffsetType visitArgument(Argument &A);
183   SizeOffsetType visitCallSite(CallSite CS);
184   SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
185   SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
186   SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
187   SizeOffsetType visitGEPOperator(GEPOperator &GEP);
188   SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
189   SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
190   SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
191   SizeOffsetType visitLoadInst(LoadInst &I);
192   SizeOffsetType visitPHINode(PHINode&);
193   SizeOffsetType visitSelectInst(SelectInst &I);
194   SizeOffsetType visitUndefValue(UndefValue&);
195   SizeOffsetType visitInstruction(Instruction &I);
196 };
197
198 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
199
200
201 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
202 /// May create code to compute the result at run-time.
203 class ObjectSizeOffsetEvaluator
204   : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
205
206   typedef IRBuilder<true, TargetFolder> BuilderTy;
207   typedef std::pair<WeakVH, WeakVH> WeakEvalType;
208   typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
209   typedef SmallPtrSet<const Value*, 8> PtrSetTy;
210
211   const DataLayout &DL;
212   const TargetLibraryInfo *TLI;
213   LLVMContext &Context;
214   BuilderTy Builder;
215   IntegerType *IntTy;
216   Value *Zero;
217   CacheMapTy CacheMap;
218   PtrSetTy SeenVals;
219   bool RoundToAlign;
220
221   SizeOffsetEvalType unknown() {
222     return std::make_pair(nullptr, nullptr);
223   }
224   SizeOffsetEvalType compute_(Value *V);
225
226 public:
227   ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
228                             LLVMContext &Context, bool RoundToAlign = false);
229   SizeOffsetEvalType compute(Value *V);
230
231   bool knownSize(SizeOffsetEvalType SizeOffset) {
232     return SizeOffset.first;
233   }
234
235   bool knownOffset(SizeOffsetEvalType SizeOffset) {
236     return SizeOffset.second;
237   }
238
239   bool anyKnown(SizeOffsetEvalType SizeOffset) {
240     return knownSize(SizeOffset) || knownOffset(SizeOffset);
241   }
242
243   bool bothKnown(SizeOffsetEvalType SizeOffset) {
244     return knownSize(SizeOffset) && knownOffset(SizeOffset);
245   }
246
247   // The individual instruction visitors should be treated as private.
248   SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
249   SizeOffsetEvalType visitCallSite(CallSite CS);
250   SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
251   SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
252   SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
253   SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
254   SizeOffsetEvalType visitLoadInst(LoadInst &I);
255   SizeOffsetEvalType visitPHINode(PHINode &PHI);
256   SizeOffsetEvalType visitSelectInst(SelectInst &I);
257   SizeOffsetEvalType visitInstruction(Instruction &I);
258 };
259
260 } // End llvm namespace
261
262 #endif