[MemoryBuiltins] Remove isOperatorNewLike by consolidating non-null inference handling
[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 //===----------------------------------------------------------------------===//
63 //  malloc Call Utility Functions.
64 //
65
66 /// extractMallocCall - Returns the corresponding CallInst if the instruction
67 /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
68 /// ignore InvokeInst here.
69 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
70 static inline CallInst *extractMallocCall(Value *I,
71                                           const TargetLibraryInfo *TLI) {
72   return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
73 }
74
75 /// getMallocType - Returns the PointerType resulting from the malloc call.
76 /// The PointerType depends on the number of bitcast uses of the malloc call:
77 ///   0: PointerType is the malloc calls' return type.
78 ///   1: PointerType is the bitcast's result type.
79 ///  >1: Unique PointerType cannot be determined, return NULL.
80 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
81
82 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
83 /// The Type depends on the number of bitcast uses of the malloc call:
84 ///   0: PointerType is the malloc calls' return type.
85 ///   1: PointerType is the bitcast's result type.
86 ///  >1: Unique PointerType cannot be determined, return NULL.
87 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
88
89 /// getMallocArraySize - Returns the array size of a malloc call.  If the
90 /// argument passed to malloc is a multiple of the size of the malloced type,
91 /// then return that multiple.  For non-array mallocs, the multiple is
92 /// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
93 /// determined.
94 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
95                           const TargetLibraryInfo *TLI,
96                           bool LookThroughSExt = false);
97
98 //===----------------------------------------------------------------------===//
99 //  calloc Call Utility Functions.
100 //
101
102 /// extractCallocCall - Returns the corresponding CallInst if the instruction
103 /// is a calloc call.
104 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
105 static inline CallInst *extractCallocCall(Value *I,
106                                           const TargetLibraryInfo *TLI) {
107   return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
108 }
109
110
111 //===----------------------------------------------------------------------===//
112 //  free Call Utility Functions.
113 //
114
115 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
116 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
117
118 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
119   return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
120 }
121
122
123 //===----------------------------------------------------------------------===//
124 //  Utility functions to compute size of objects.
125 //
126
127 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
128 /// object size in Size if successful, and false otherwise. In this context, by
129 /// object we mean the region of memory starting at Ptr to the end of the
130 /// underlying object pointed to by Ptr.
131 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
132 /// byval arguments, and global variables.
133 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
134                    const TargetLibraryInfo *TLI, bool RoundToAlign = false);
135
136 typedef std::pair<APInt, APInt> SizeOffsetType;
137
138 /// \brief Evaluate the size and offset of an object pointed to by a Value*
139 /// statically. Fails if size or offset are not known at compile time.
140 class ObjectSizeOffsetVisitor
141   : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
142
143   const DataLayout &DL;
144   const TargetLibraryInfo *TLI;
145   bool RoundToAlign;
146   unsigned IntTyBits;
147   APInt Zero;
148   SmallPtrSet<Instruction *, 8> SeenInsts;
149
150   APInt align(APInt Size, uint64_t Align);
151
152   SizeOffsetType unknown() {
153     return std::make_pair(APInt(), APInt());
154   }
155
156 public:
157   ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
158                           LLVMContext &Context, bool RoundToAlign = false);
159
160   SizeOffsetType compute(Value *V);
161
162   bool knownSize(SizeOffsetType &SizeOffset) {
163     return SizeOffset.first.getBitWidth() > 1;
164   }
165
166   bool knownOffset(SizeOffsetType &SizeOffset) {
167     return SizeOffset.second.getBitWidth() > 1;
168   }
169
170   bool bothKnown(SizeOffsetType &SizeOffset) {
171     return knownSize(SizeOffset) && knownOffset(SizeOffset);
172   }
173
174   // These are "private", except they can't actually be made private. Only
175   // compute() should be used by external users.
176   SizeOffsetType visitAllocaInst(AllocaInst &I);
177   SizeOffsetType visitArgument(Argument &A);
178   SizeOffsetType visitCallSite(CallSite CS);
179   SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
180   SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
181   SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
182   SizeOffsetType visitGEPOperator(GEPOperator &GEP);
183   SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
184   SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
185   SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
186   SizeOffsetType visitLoadInst(LoadInst &I);
187   SizeOffsetType visitPHINode(PHINode&);
188   SizeOffsetType visitSelectInst(SelectInst &I);
189   SizeOffsetType visitUndefValue(UndefValue&);
190   SizeOffsetType visitInstruction(Instruction &I);
191 };
192
193 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
194
195
196 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
197 /// May create code to compute the result at run-time.
198 class ObjectSizeOffsetEvaluator
199   : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
200
201   typedef IRBuilder<true, TargetFolder> BuilderTy;
202   typedef std::pair<WeakVH, WeakVH> WeakEvalType;
203   typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
204   typedef SmallPtrSet<const Value*, 8> PtrSetTy;
205
206   const DataLayout &DL;
207   const TargetLibraryInfo *TLI;
208   LLVMContext &Context;
209   BuilderTy Builder;
210   IntegerType *IntTy;
211   Value *Zero;
212   CacheMapTy CacheMap;
213   PtrSetTy SeenVals;
214   bool RoundToAlign;
215
216   SizeOffsetEvalType unknown() {
217     return std::make_pair(nullptr, nullptr);
218   }
219   SizeOffsetEvalType compute_(Value *V);
220
221 public:
222   ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
223                             LLVMContext &Context, bool RoundToAlign = false);
224   SizeOffsetEvalType compute(Value *V);
225
226   bool knownSize(SizeOffsetEvalType SizeOffset) {
227     return SizeOffset.first;
228   }
229
230   bool knownOffset(SizeOffsetEvalType SizeOffset) {
231     return SizeOffset.second;
232   }
233
234   bool anyKnown(SizeOffsetEvalType SizeOffset) {
235     return knownSize(SizeOffset) || knownOffset(SizeOffset);
236   }
237
238   bool bothKnown(SizeOffsetEvalType SizeOffset) {
239     return knownSize(SizeOffset) && knownOffset(SizeOffset);
240   }
241
242   // The individual instruction visitors should be treated as private.
243   SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
244   SizeOffsetEvalType visitCallSite(CallSite CS);
245   SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
246   SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
247   SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
248   SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
249   SizeOffsetEvalType visitLoadInst(LoadInst &I);
250   SizeOffsetEvalType visitPHINode(PHINode &PHI);
251   SizeOffsetEvalType visitSelectInst(SelectInst &I);
252   SizeOffsetEvalType visitInstruction(Instruction &I);
253 };
254
255 } // End llvm namespace
256
257 #endif