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/Operator.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/InstVisitor.h"
23 #include "llvm/Support/IRBuilder.h"
24 #include "llvm/Support/TargetFolder.h"
34 /// \brief Tests if a value is a call to a library function that allocates or
35 /// reallocates memory (either malloc, calloc, realloc, or strdup like).
36 bool isAllocationFn(const Value *V, bool LookThroughBitCast = false);
38 /// \brief Tests if a value is a call to a function that returns a NoAlias
39 /// pointer (including malloc/calloc/strdup-like functions).
40 bool isNoAliasFn(const Value *V, bool LookThroughBitCast = false);
42 /// \brief Tests if a value is a call to a library function that allocates
43 /// uninitialized memory (such as malloc).
44 bool isMallocLikeFn(const Value *V, bool LookThroughBitCast = false);
46 /// \brief Tests if a value is a call to a library function that allocates
47 /// zero-filled memory (such as calloc).
48 bool isCallocLikeFn(const Value *V, bool LookThroughBitCast = false);
50 /// \brief Tests if a value is a call to a library function that allocates
51 /// memory (either malloc, calloc, or strdup like).
52 bool isAllocLikeFn(const Value *V, bool LookThroughBitCast = false);
54 /// \brief Tests if a value is a call to a library function that reallocates
55 /// memory (such as realloc).
56 bool isReallocLikeFn(const Value *V, bool LookThroughBitCast = false);
59 //===----------------------------------------------------------------------===//
60 // malloc Call Utility Functions.
63 /// extractMallocCall - Returns the corresponding CallInst if the instruction
64 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
65 /// ignore InvokeInst here.
66 const CallInst *extractMallocCall(const Value *I);
67 static inline CallInst *extractMallocCall(Value *I) {
68 return const_cast<CallInst*>(extractMallocCall((const Value*)I));
71 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
72 /// instruction is a bitcast of the result of a malloc call.
73 const CallInst *extractMallocCallFromBitCast(const Value *I);
74 static inline CallInst *extractMallocCallFromBitCast(Value *I) {
75 return const_cast<CallInst*>(extractMallocCallFromBitCast((const Value*)I));
78 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
79 /// is a call to malloc whose array size can be determined and the array size
80 /// is not constant 1. Otherwise, return NULL.
81 const CallInst *isArrayMalloc(const Value *I, const TargetData *TD);
83 /// getMallocType - Returns the PointerType resulting from the malloc call.
84 /// The PointerType depends on the number of bitcast uses of the malloc call:
85 /// 0: PointerType is the malloc calls' return type.
86 /// 1: PointerType is the bitcast's result type.
87 /// >1: Unique PointerType cannot be determined, return NULL.
88 PointerType *getMallocType(const CallInst *CI);
90 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
91 /// The Type depends on the number of bitcast uses of the malloc call:
92 /// 0: PointerType is the malloc calls' return type.
93 /// 1: PointerType is the bitcast's result type.
94 /// >1: Unique PointerType cannot be determined, return NULL.
95 Type *getMallocAllocatedType(const CallInst *CI);
97 /// getMallocArraySize - Returns the array size of a malloc call. If the
98 /// argument passed to malloc is a multiple of the size of the malloced type,
99 /// then return that multiple. For non-array mallocs, the multiple is
100 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
102 Value *getMallocArraySize(CallInst *CI, const TargetData *TD,
103 bool LookThroughSExt = false);
106 //===----------------------------------------------------------------------===//
107 // calloc Call Utility Functions.
110 /// extractCallocCall - Returns the corresponding CallInst if the instruction
111 /// is a calloc call.
112 const CallInst *extractCallocCall(const Value *I);
113 static inline CallInst *extractCallocCall(Value *I) {
114 return const_cast<CallInst*>(extractCallocCall((const Value*)I));
118 //===----------------------------------------------------------------------===//
119 // free Call Utility Functions.
122 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
123 const CallInst *isFreeCall(const Value *I);
125 static inline CallInst *isFreeCall(Value *I) {
126 return const_cast<CallInst*>(isFreeCall((const Value*)I));
130 //===----------------------------------------------------------------------===//
131 // Utility functions to compute size of objects.
134 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
135 /// object size in Size if successful, and false otherwise.
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 TargetData *TD,
139 bool RoundToAlign = false);
143 typedef std::pair<APInt, APInt> SizeOffsetType;
145 /// \brief Evaluate the size and offset of an object ponted by a Value*
146 /// statically. Fails if size or offset are not known at compile time.
147 class ObjectSizeOffsetVisitor
148 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
150 const TargetData *TD;
155 APInt align(APInt Size, uint64_t Align);
157 SizeOffsetType unknown() {
158 return std::make_pair(APInt(), APInt());
162 ObjectSizeOffsetVisitor(const TargetData *TD, LLVMContext &Context,
163 bool RoundToAlign = false);
165 SizeOffsetType compute(Value *V);
167 bool knownSize(SizeOffsetType &SizeOffset) {
168 return SizeOffset.first.getBitWidth() > 1;
171 bool knownOffset(SizeOffsetType &SizeOffset) {
172 return SizeOffset.second.getBitWidth() > 1;
175 bool bothKnown(SizeOffsetType &SizeOffset) {
176 return knownSize(SizeOffset) && knownOffset(SizeOffset);
179 SizeOffsetType visitAllocaInst(AllocaInst &I);
180 SizeOffsetType visitArgument(Argument &A);
181 SizeOffsetType visitCallSite(CallSite CS);
182 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
183 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
184 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
185 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
186 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
187 SizeOffsetType visitLoadInst(LoadInst &I);
188 SizeOffsetType visitPHINode(PHINode&);
189 SizeOffsetType visitSelectInst(SelectInst &I);
190 SizeOffsetType visitUndefValue(UndefValue&);
191 SizeOffsetType visitInstruction(Instruction &I);
194 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
195 typedef IRBuilder<true, TargetFolder> BuilderTy;
196 typedef DenseMap<const Value*, SizeOffsetEvalType> CacheMapTy;
197 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
200 /// \brief Evaluate the size and offset of an object ponted by a Value*.
201 /// May create code to compute the result at run-time.
202 class ObjectSizeOffsetEvaluator
203 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
205 const TargetData *TD;
206 LLVMContext &Context;
208 ObjectSizeOffsetVisitor Visitor;
214 SizeOffsetEvalType unknown() {
215 return std::make_pair((Value*)0, (Value*)0);
217 SizeOffsetEvalType compute_(Value *V);
220 ObjectSizeOffsetEvaluator(const TargetData *TD, LLVMContext &Context);
221 SizeOffsetEvalType compute(Value *V);
223 bool knownSize(SizeOffsetEvalType &SizeOffset) {
224 return SizeOffset.first;
227 bool knownOffset(SizeOffsetEvalType &SizeOffset) {
228 return SizeOffset.second;
231 bool anyKnown(SizeOffsetEvalType &SizeOffset) {
232 return knownSize(SizeOffset) || knownOffset(SizeOffset);
235 bool bothKnown(SizeOffsetEvalType &SizeOffset) {
236 return knownSize(SizeOffset) && knownOffset(SizeOffset);
239 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
240 SizeOffsetEvalType visitCallSite(CallSite CS);
241 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
242 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
243 SizeOffsetEvalType visitLoadInst(LoadInst &I);
244 SizeOffsetEvalType visitPHINode(PHINode &PHI);
245 SizeOffsetEvalType visitSelectInst(SelectInst &I);
246 SizeOffsetEvalType visitInstruction(Instruction &I);
249 } // End llvm namespace