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 or invoke to a library function that
35 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
37 bool isAllocationFn(const Value *V, bool LookThroughBitCast = false);
39 /// \brief Tests if a value is a call or invoke to a function that returns a
40 /// NoAlias pointer (including malloc/calloc/strdup-like functions).
41 bool isNoAliasFn(const Value *V, bool LookThroughBitCast = false);
43 /// \brief Tests if a value is a call or invoke to a library function that
44 /// allocates uninitialized memory (such as malloc).
45 bool isMallocLikeFn(const Value *V, bool LookThroughBitCast = false);
47 /// \brief Tests if a value is a call or invoke to a library function that
48 /// allocates zero-filled memory (such as calloc).
49 bool isCallocLikeFn(const Value *V, bool LookThroughBitCast = false);
51 /// \brief Tests if a value is a call or invoke to a library function that
52 /// allocates memory (either malloc, calloc, or strdup like).
53 bool isAllocLikeFn(const Value *V, bool LookThroughBitCast = false);
55 /// \brief Tests if a value is a call or invoke to a library function that
56 /// reallocates memory (such as realloc).
57 bool isReallocLikeFn(const Value *V, bool LookThroughBitCast = false);
60 //===----------------------------------------------------------------------===//
61 // malloc Call Utility Functions.
64 /// extractMallocCall - Returns the corresponding CallInst if the instruction
65 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
66 /// ignore InvokeInst here.
67 const CallInst *extractMallocCall(const Value *I);
68 static inline CallInst *extractMallocCall(Value *I) {
69 return const_cast<CallInst*>(extractMallocCall((const Value*)I));
72 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
73 /// instruction is a bitcast of the result of a malloc call.
74 const CallInst *extractMallocCallFromBitCast(const Value *I);
75 static inline CallInst *extractMallocCallFromBitCast(Value *I) {
76 return const_cast<CallInst*>(extractMallocCallFromBitCast((const Value*)I));
79 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
80 /// is a call to malloc whose array size can be determined and the array size
81 /// is not constant 1. Otherwise, return NULL.
82 const CallInst *isArrayMalloc(const Value *I, const TargetData *TD);
84 /// getMallocType - Returns the PointerType resulting from the malloc call.
85 /// The PointerType depends on the number of bitcast uses of the malloc call:
86 /// 0: PointerType is the malloc calls' return type.
87 /// 1: PointerType is the bitcast's result type.
88 /// >1: Unique PointerType cannot be determined, return NULL.
89 PointerType *getMallocType(const CallInst *CI);
91 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
92 /// The Type depends on the number of bitcast uses of the malloc call:
93 /// 0: PointerType is the malloc calls' return type.
94 /// 1: PointerType is the bitcast's result type.
95 /// >1: Unique PointerType cannot be determined, return NULL.
96 Type *getMallocAllocatedType(const CallInst *CI);
98 /// getMallocArraySize - Returns the array size of a malloc call. If the
99 /// argument passed to malloc is a multiple of the size of the malloced type,
100 /// then return that multiple. For non-array mallocs, the multiple is
101 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
103 Value *getMallocArraySize(CallInst *CI, const TargetData *TD,
104 bool LookThroughSExt = false);
107 //===----------------------------------------------------------------------===//
108 // calloc Call Utility Functions.
111 /// extractCallocCall - Returns the corresponding CallInst if the instruction
112 /// is a calloc call.
113 const CallInst *extractCallocCall(const Value *I);
114 static inline CallInst *extractCallocCall(Value *I) {
115 return const_cast<CallInst*>(extractCallocCall((const Value*)I));
119 //===----------------------------------------------------------------------===//
120 // free Call Utility Functions.
123 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
124 const CallInst *isFreeCall(const Value *I);
126 static inline CallInst *isFreeCall(Value *I) {
127 return const_cast<CallInst*>(isFreeCall((const Value*)I));
131 //===----------------------------------------------------------------------===//
132 // Utility functions to compute size of objects.
135 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
136 /// object size in Size if successful, and false otherwise.
137 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
138 /// byval arguments, and global variables.
139 bool getObjectSize(const Value *Ptr, uint64_t &Size, const TargetData *TD,
140 bool RoundToAlign = false);
144 typedef std::pair<APInt, APInt> SizeOffsetType;
146 /// \brief Evaluate the size and offset of an object ponted by a Value*
147 /// statically. Fails if size or offset are not known at compile time.
148 class ObjectSizeOffsetVisitor
149 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
151 const TargetData *TD;
156 APInt align(APInt Size, uint64_t Align);
158 SizeOffsetType unknown() {
159 return std::make_pair(APInt(), APInt());
163 ObjectSizeOffsetVisitor(const TargetData *TD, LLVMContext &Context,
164 bool RoundToAlign = false);
166 SizeOffsetType compute(Value *V);
168 bool knownSize(SizeOffsetType &SizeOffset) {
169 return SizeOffset.first.getBitWidth() > 1;
172 bool knownOffset(SizeOffsetType &SizeOffset) {
173 return SizeOffset.second.getBitWidth() > 1;
176 bool bothKnown(SizeOffsetType &SizeOffset) {
177 return knownSize(SizeOffset) && knownOffset(SizeOffset);
180 SizeOffsetType visitAllocaInst(AllocaInst &I);
181 SizeOffsetType visitArgument(Argument &A);
182 SizeOffsetType visitCallSite(CallSite CS);
183 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
184 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
185 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
186 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
187 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
188 SizeOffsetType visitLoadInst(LoadInst &I);
189 SizeOffsetType visitPHINode(PHINode&);
190 SizeOffsetType visitSelectInst(SelectInst &I);
191 SizeOffsetType visitUndefValue(UndefValue&);
192 SizeOffsetType visitInstruction(Instruction &I);
195 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
198 /// \brief Evaluate the size and offset of an object ponted by a Value*.
199 /// May create code to compute the result at run-time.
200 class ObjectSizeOffsetEvaluator
201 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
203 typedef IRBuilder<true, TargetFolder> BuilderTy;
204 typedef DenseMap<const Value*, SizeOffsetEvalType> CacheMapTy;
205 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
207 const TargetData *TD;
208 LLVMContext &Context;
210 ObjectSizeOffsetVisitor Visitor;
216 SizeOffsetEvalType unknown() {
217 return std::make_pair((Value*)0, (Value*)0);
219 SizeOffsetEvalType compute_(Value *V);
222 ObjectSizeOffsetEvaluator(const TargetData *TD, LLVMContext &Context);
223 SizeOffsetEvalType compute(Value *V);
225 bool knownSize(SizeOffsetEvalType &SizeOffset) {
226 return SizeOffset.first;
229 bool knownOffset(SizeOffsetEvalType &SizeOffset) {
230 return SizeOffset.second;
233 bool anyKnown(SizeOffsetEvalType &SizeOffset) {
234 return knownSize(SizeOffset) || knownOffset(SizeOffset);
237 bool bothKnown(SizeOffsetEvalType &SizeOffset) {
238 return knownSize(SizeOffset) && knownOffset(SizeOffset);
241 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
242 SizeOffsetEvalType visitCallSite(CallSite CS);
243 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
244 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
245 SizeOffsetEvalType visitLoadInst(LoadInst &I);
246 SizeOffsetEvalType visitPHINode(PHINode &PHI);
247 SizeOffsetEvalType visitSelectInst(SelectInst &I);
248 SizeOffsetEvalType visitInstruction(Instruction &I);
251 } // End llvm namespace