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/IRBuilder.h"
19 #include "llvm/Operator.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/InstVisitor.h"
24 #include "llvm/Support/TargetFolder.h"
25 #include "llvm/Support/ValueHandle.h"
35 /// \brief Tests if a value is a call or invoke to a library function that
36 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
38 bool isAllocationFn(const Value *V, bool LookThroughBitCast = false);
40 /// \brief Tests if a value is a call or invoke to a function that returns a
41 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
42 bool isNoAliasFn(const Value *V, bool LookThroughBitCast = false);
44 /// \brief Tests if a value is a call or invoke to a library function that
45 /// allocates uninitialized memory (such as malloc).
46 bool isMallocLikeFn(const Value *V, bool LookThroughBitCast = false);
48 /// \brief Tests if a value is a call or invoke to a library function that
49 /// allocates zero-filled memory (such as calloc).
50 bool isCallocLikeFn(const Value *V, bool LookThroughBitCast = false);
52 /// \brief Tests if a value is a call or invoke to a library function that
53 /// allocates memory (either malloc, calloc, or strdup like).
54 bool isAllocLikeFn(const Value *V, bool LookThroughBitCast = false);
56 /// \brief Tests if a value is a call or invoke to a library function that
57 /// reallocates memory (such as realloc).
58 bool isReallocLikeFn(const Value *V, bool LookThroughBitCast = false);
61 //===----------------------------------------------------------------------===//
62 // malloc Call Utility Functions.
65 /// extractMallocCall - Returns the corresponding CallInst if the instruction
66 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
67 /// ignore InvokeInst here.
68 const CallInst *extractMallocCall(const Value *I);
69 static inline CallInst *extractMallocCall(Value *I) {
70 return const_cast<CallInst*>(extractMallocCall((const Value*)I));
73 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
74 /// is a call to malloc whose array size can be determined and the array size
75 /// is not constant 1. Otherwise, return NULL.
76 const CallInst *isArrayMalloc(const Value *I, const TargetData *TD);
78 /// getMallocType - Returns the PointerType resulting from the malloc call.
79 /// The PointerType depends on the number of bitcast uses of the malloc call:
80 /// 0: PointerType is the malloc calls' return type.
81 /// 1: PointerType is the bitcast's result type.
82 /// >1: Unique PointerType cannot be determined, return NULL.
83 PointerType *getMallocType(const CallInst *CI);
85 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
86 /// The Type depends on the number of bitcast uses of the malloc call:
87 /// 0: PointerType is the malloc calls' return type.
88 /// 1: PointerType is the bitcast's result type.
89 /// >1: Unique PointerType cannot be determined, return NULL.
90 Type *getMallocAllocatedType(const CallInst *CI);
92 /// getMallocArraySize - Returns the array size of a malloc call. If the
93 /// argument passed to malloc is a multiple of the size of the malloced type,
94 /// then return that multiple. For non-array mallocs, the multiple is
95 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
97 Value *getMallocArraySize(CallInst *CI, const TargetData *TD,
98 bool LookThroughSExt = false);
101 //===----------------------------------------------------------------------===//
102 // calloc Call Utility Functions.
105 /// extractCallocCall - Returns the corresponding CallInst if the instruction
106 /// is a calloc call.
107 const CallInst *extractCallocCall(const Value *I);
108 static inline CallInst *extractCallocCall(Value *I) {
109 return const_cast<CallInst*>(extractCallocCall((const Value*)I));
113 //===----------------------------------------------------------------------===//
114 // free Call Utility Functions.
117 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
118 const CallInst *isFreeCall(const Value *I);
120 static inline CallInst *isFreeCall(Value *I) {
121 return const_cast<CallInst*>(isFreeCall((const Value*)I));
125 //===----------------------------------------------------------------------===//
126 // Utility functions to compute size of objects.
129 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
130 /// object size in Size if successful, and false otherwise.
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 TargetData *TD,
134 bool RoundToAlign = false);
138 typedef std::pair<APInt, APInt> SizeOffsetType;
140 /// \brief Evaluate the size and offset of an object ponted by a Value*
141 /// statically. Fails if size or offset are not known at compile time.
142 class ObjectSizeOffsetVisitor
143 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
145 const TargetData *TD;
150 APInt align(APInt Size, uint64_t Align);
152 SizeOffsetType unknown() {
153 return std::make_pair(APInt(), APInt());
157 ObjectSizeOffsetVisitor(const TargetData *TD, LLVMContext &Context,
158 bool RoundToAlign = false);
160 SizeOffsetType compute(Value *V);
162 bool knownSize(SizeOffsetType &SizeOffset) {
163 return SizeOffset.first.getBitWidth() > 1;
166 bool knownOffset(SizeOffsetType &SizeOffset) {
167 return SizeOffset.second.getBitWidth() > 1;
170 bool bothKnown(SizeOffsetType &SizeOffset) {
171 return knownSize(SizeOffset) && knownOffset(SizeOffset);
174 SizeOffsetType visitAllocaInst(AllocaInst &I);
175 SizeOffsetType visitArgument(Argument &A);
176 SizeOffsetType visitCallSite(CallSite CS);
177 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
178 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
179 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
180 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
181 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
182 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
183 SizeOffsetType visitLoadInst(LoadInst &I);
184 SizeOffsetType visitPHINode(PHINode&);
185 SizeOffsetType visitSelectInst(SelectInst &I);
186 SizeOffsetType visitUndefValue(UndefValue&);
187 SizeOffsetType visitInstruction(Instruction &I);
190 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
193 /// \brief Evaluate the size and offset of an object ponted by a Value*.
194 /// May create code to compute the result at run-time.
195 class ObjectSizeOffsetEvaluator
196 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
198 typedef IRBuilder<true, TargetFolder> BuilderTy;
199 typedef std::pair<WeakVH, WeakVH> WeakEvalType;
200 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
201 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
203 const TargetData *TD;
204 const TargetLibraryInfo *TLI;
205 LLVMContext &Context;
207 ObjectSizeOffsetVisitor Visitor;
213 SizeOffsetEvalType unknown() {
214 return std::make_pair((Value*)0, (Value*)0);
216 SizeOffsetEvalType compute_(Value *V);
219 ObjectSizeOffsetEvaluator(const TargetData *TD, const TargetLibraryInfo *TLI,
220 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 visitExtractElementInst(ExtractElementInst &I);
242 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
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