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 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
73 /// is a call to malloc whose array size can be determined and the array size
74 /// is not constant 1. Otherwise, return NULL.
75 const CallInst *isArrayMalloc(const Value *I, const TargetData *TD);
77 /// getMallocType - Returns the PointerType resulting from the malloc call.
78 /// The PointerType depends on the number of bitcast uses of the malloc call:
79 /// 0: PointerType is the malloc calls' return type.
80 /// 1: PointerType is the bitcast's result type.
81 /// >1: Unique PointerType cannot be determined, return NULL.
82 PointerType *getMallocType(const CallInst *CI);
84 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
85 /// The Type 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 Type *getMallocAllocatedType(const CallInst *CI);
91 /// getMallocArraySize - Returns the array size of a malloc call. If the
92 /// argument passed to malloc is a multiple of the size of the malloced type,
93 /// then return that multiple. For non-array mallocs, the multiple is
94 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
96 Value *getMallocArraySize(CallInst *CI, const TargetData *TD,
97 bool LookThroughSExt = false);
100 //===----------------------------------------------------------------------===//
101 // calloc Call Utility Functions.
104 /// extractCallocCall - Returns the corresponding CallInst if the instruction
105 /// is a calloc call.
106 const CallInst *extractCallocCall(const Value *I);
107 static inline CallInst *extractCallocCall(Value *I) {
108 return const_cast<CallInst*>(extractCallocCall((const Value*)I));
112 //===----------------------------------------------------------------------===//
113 // free Call Utility Functions.
116 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
117 const CallInst *isFreeCall(const Value *I);
119 static inline CallInst *isFreeCall(Value *I) {
120 return const_cast<CallInst*>(isFreeCall((const Value*)I));
124 //===----------------------------------------------------------------------===//
125 // Utility functions to compute size of objects.
128 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
129 /// object size in Size if successful, and false otherwise.
130 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
131 /// byval arguments, and global variables.
132 bool getObjectSize(const Value *Ptr, uint64_t &Size, const TargetData *TD,
133 bool RoundToAlign = false);
137 typedef std::pair<APInt, APInt> SizeOffsetType;
139 /// \brief Evaluate the size and offset of an object ponted by a Value*
140 /// statically. Fails if size or offset are not known at compile time.
141 class ObjectSizeOffsetVisitor
142 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
144 const TargetData *TD;
149 APInt align(APInt Size, uint64_t Align);
151 SizeOffsetType unknown() {
152 return std::make_pair(APInt(), APInt());
156 ObjectSizeOffsetVisitor(const TargetData *TD, LLVMContext &Context,
157 bool RoundToAlign = false);
159 SizeOffsetType compute(Value *V);
161 bool knownSize(SizeOffsetType &SizeOffset) {
162 return SizeOffset.first.getBitWidth() > 1;
165 bool knownOffset(SizeOffsetType &SizeOffset) {
166 return SizeOffset.second.getBitWidth() > 1;
169 bool bothKnown(SizeOffsetType &SizeOffset) {
170 return knownSize(SizeOffset) && knownOffset(SizeOffset);
173 SizeOffsetType visitAllocaInst(AllocaInst &I);
174 SizeOffsetType visitArgument(Argument &A);
175 SizeOffsetType visitCallSite(CallSite CS);
176 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
177 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
178 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
179 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
180 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
181 SizeOffsetType visitLoadInst(LoadInst &I);
182 SizeOffsetType visitPHINode(PHINode&);
183 SizeOffsetType visitSelectInst(SelectInst &I);
184 SizeOffsetType visitUndefValue(UndefValue&);
185 SizeOffsetType visitInstruction(Instruction &I);
188 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
191 /// \brief Evaluate the size and offset of an object ponted by a Value*.
192 /// May create code to compute the result at run-time.
193 class ObjectSizeOffsetEvaluator
194 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
196 typedef IRBuilder<true, TargetFolder> BuilderTy;
197 typedef DenseMap<const Value*, SizeOffsetEvalType> CacheMapTy;
198 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
200 const TargetData *TD;
201 LLVMContext &Context;
203 ObjectSizeOffsetVisitor Visitor;
209 SizeOffsetEvalType unknown() {
210 return std::make_pair((Value*)0, (Value*)0);
212 SizeOffsetEvalType compute_(Value *V);
215 ObjectSizeOffsetEvaluator(const TargetData *TD, LLVMContext &Context);
216 SizeOffsetEvalType compute(Value *V);
218 bool knownSize(SizeOffsetEvalType &SizeOffset) {
219 return SizeOffset.first;
222 bool knownOffset(SizeOffsetEvalType &SizeOffset) {
223 return SizeOffset.second;
226 bool anyKnown(SizeOffsetEvalType &SizeOffset) {
227 return knownSize(SizeOffset) || knownOffset(SizeOffset);
230 bool bothKnown(SizeOffsetEvalType &SizeOffset) {
231 return knownSize(SizeOffset) && knownOffset(SizeOffset);
234 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
235 SizeOffsetEvalType visitCallSite(CallSite CS);
236 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
237 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
238 SizeOffsetEvalType visitLoadInst(LoadInst &I);
239 SizeOffsetEvalType visitPHINode(PHINode &PHI);
240 SizeOffsetEvalType visitSelectInst(SelectInst &I);
241 SizeOffsetEvalType visitInstruction(Instruction &I);
244 } // End llvm namespace