MemoryBuiltins.h revision 2bc689c8461dfbaa4ca9a9cc4ae5bc59cb007329
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
27namespace llvm {
28class CallInst;
29class PointerType;
30class DataLayout;
31class TargetLibraryInfo;
32class Type;
33class 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).
39bool 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).
44bool 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).
49bool 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).
54bool 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).
59bool 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).
64bool 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.
75const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
76static 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.
84const 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.
92PointerType *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.
99Type *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.
106Value *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.
117const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
118static 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()
129const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
130
131static 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.
142/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
143/// byval arguments, and global variables.
144bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
145                   const TargetLibraryInfo *TLI, bool RoundToAlign = false);
146
147/// \brief Compute the size of the underlying object pointed by Ptr. Returns
148/// true and the object size in Size if successful, and false otherwise.
149/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
150/// byval arguments, and global variables.
151bool getUnderlyingObjectSize(const Value *Ptr, uint64_t &Size,
152                             const DataLayout *TD, const TargetLibraryInfo *TLI,
153                             bool RoundToAlign = false);
154
155
156
157typedef std::pair<APInt, APInt> SizeOffsetType;
158
159/// \brief Evaluate the size and offset of an object ponted by a Value*
160/// statically. Fails if size or offset are not known at compile time.
161class ObjectSizeOffsetVisitor
162  : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
163
164  const DataLayout *TD;
165  const TargetLibraryInfo *TLI;
166  bool RoundToAlign;
167  unsigned IntTyBits;
168  APInt Zero;
169  SmallPtrSet<Instruction *, 8> SeenInsts;
170
171  APInt align(APInt Size, uint64_t Align);
172
173  SizeOffsetType unknown() {
174    return std::make_pair(APInt(), APInt());
175  }
176
177public:
178  ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI,
179                          LLVMContext &Context, bool RoundToAlign = false);
180
181  SizeOffsetType compute(Value *V);
182
183  bool knownSize(SizeOffsetType &SizeOffset) {
184    return SizeOffset.first.getBitWidth() > 1;
185  }
186
187  bool knownOffset(SizeOffsetType &SizeOffset) {
188    return SizeOffset.second.getBitWidth() > 1;
189  }
190
191  bool bothKnown(SizeOffsetType &SizeOffset) {
192    return knownSize(SizeOffset) && knownOffset(SizeOffset);
193  }
194
195  SizeOffsetType visitAllocaInst(AllocaInst &I);
196  SizeOffsetType visitArgument(Argument &A);
197  SizeOffsetType visitCallSite(CallSite CS);
198  SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
199  SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
200  SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
201  SizeOffsetType visitGEPOperator(GEPOperator &GEP);
202  SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
203  SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
204  SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
205  SizeOffsetType visitLoadInst(LoadInst &I);
206  SizeOffsetType visitPHINode(PHINode&);
207  SizeOffsetType visitSelectInst(SelectInst &I);
208  SizeOffsetType visitUndefValue(UndefValue&);
209  SizeOffsetType visitInstruction(Instruction &I);
210};
211
212typedef std::pair<Value*, Value*> SizeOffsetEvalType;
213
214
215/// \brief Evaluate the size and offset of an object ponted by a Value*.
216/// May create code to compute the result at run-time.
217class ObjectSizeOffsetEvaluator
218  : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
219
220  typedef IRBuilder<true, TargetFolder> BuilderTy;
221  typedef std::pair<WeakVH, WeakVH> WeakEvalType;
222  typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
223  typedef SmallPtrSet<const Value*, 8> PtrSetTy;
224
225  const DataLayout *TD;
226  const TargetLibraryInfo *TLI;
227  LLVMContext &Context;
228  BuilderTy Builder;
229  IntegerType *IntTy;
230  Value *Zero;
231  CacheMapTy CacheMap;
232  PtrSetTy SeenVals;
233
234  SizeOffsetEvalType unknown() {
235    return std::make_pair((Value*)0, (Value*)0);
236  }
237  SizeOffsetEvalType compute_(Value *V);
238
239public:
240  ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI,
241                            LLVMContext &Context);
242  SizeOffsetEvalType compute(Value *V);
243
244  bool knownSize(SizeOffsetEvalType SizeOffset) {
245    return SizeOffset.first;
246  }
247
248  bool knownOffset(SizeOffsetEvalType SizeOffset) {
249    return SizeOffset.second;
250  }
251
252  bool anyKnown(SizeOffsetEvalType SizeOffset) {
253    return knownSize(SizeOffset) || knownOffset(SizeOffset);
254  }
255
256  bool bothKnown(SizeOffsetEvalType SizeOffset) {
257    return knownSize(SizeOffset) && knownOffset(SizeOffset);
258  }
259
260  SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
261  SizeOffsetEvalType visitCallSite(CallSite CS);
262  SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
263  SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
264  SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
265  SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
266  SizeOffsetEvalType visitLoadInst(LoadInst &I);
267  SizeOffsetEvalType visitPHINode(PHINode &PHI);
268  SizeOffsetEvalType visitSelectInst(SelectInst &I);
269  SizeOffsetEvalType visitInstruction(Instruction &I);
270};
271
272} // End llvm namespace
273
274#endif
275