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/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"
31 class TargetLibraryInfo;
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
39 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
40 bool LookThroughBitCast = false);
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).
44 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
45 bool LookThroughBitCast = false);
47 /// \brief Tests if a value is a call or invoke to a library function that
48 /// allocates uninitialized memory (such as malloc).
49 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
50 bool LookThroughBitCast = false);
52 /// \brief Tests if a value is a call or invoke to a library function that
53 /// allocates zero-filled memory (such as calloc).
54 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
55 bool LookThroughBitCast = false);
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).
59 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
60 bool LookThroughBitCast = false);
62 /// \brief Tests if a value is a call or invoke to a library function that
63 /// reallocates memory (such as realloc).
64 bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
65 bool LookThroughBitCast = false);
68 //===----------------------------------------------------------------------===//
69 // malloc Call Utility Functions.
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.
75 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
76 static inline CallInst *extractMallocCall(Value *I,
77 const TargetLibraryInfo *TLI) {
78 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
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.
84 const CallInst *isArrayMalloc(const Value *I, const DataLayout *TD,
85 const TargetLibraryInfo *TLI);
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.
92 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
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.
99 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
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
106 Value *getMallocArraySize(CallInst *CI, const DataLayout *TD,
107 const TargetLibraryInfo *TLI,
108 bool LookThroughSExt = false);
111 //===----------------------------------------------------------------------===//
112 // calloc Call Utility Functions.
115 /// extractCallocCall - Returns the corresponding CallInst if the instruction
116 /// is a calloc call.
117 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
118 static inline CallInst *extractCallocCall(Value *I,
119 const TargetLibraryInfo *TLI) {
120 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
124 //===----------------------------------------------------------------------===//
125 // free Call Utility Functions.
128 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
129 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
131 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
132 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
136 //===----------------------------------------------------------------------===//
137 // Utility functions to compute size of objects.
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. In this context, by
142 /// object we mean the region of memory starting at Ptr to the end of the
143 /// underlying object pointed to by Ptr.
144 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
145 /// byval arguments, and global variables.
146 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
147 const TargetLibraryInfo *TLI, bool RoundToAlign = false);
151 typedef std::pair<APInt, APInt> SizeOffsetType;
153 /// \brief Evaluate the size and offset of an object ponted by a Value*
154 /// statically. Fails if size or offset are not known at compile time.
155 class ObjectSizeOffsetVisitor
156 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
158 const DataLayout *TD;
159 const TargetLibraryInfo *TLI;
163 SmallPtrSet<Instruction *, 8> SeenInsts;
165 APInt align(APInt Size, uint64_t Align);
167 SizeOffsetType unknown() {
168 return std::make_pair(APInt(), APInt());
172 ObjectSizeOffsetVisitor(const DataLayout *TD, const TargetLibraryInfo *TLI,
173 LLVMContext &Context, bool RoundToAlign = false);
175 SizeOffsetType compute(Value *V);
177 bool knownSize(SizeOffsetType &SizeOffset) {
178 return SizeOffset.first.getBitWidth() > 1;
181 bool knownOffset(SizeOffsetType &SizeOffset) {
182 return SizeOffset.second.getBitWidth() > 1;
185 bool bothKnown(SizeOffsetType &SizeOffset) {
186 return knownSize(SizeOffset) && knownOffset(SizeOffset);
189 SizeOffsetType visitAllocaInst(AllocaInst &I);
190 SizeOffsetType visitArgument(Argument &A);
191 SizeOffsetType visitCallSite(CallSite CS);
192 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
193 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
194 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
195 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
196 SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
197 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
198 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
199 SizeOffsetType visitLoadInst(LoadInst &I);
200 SizeOffsetType visitPHINode(PHINode&);
201 SizeOffsetType visitSelectInst(SelectInst &I);
202 SizeOffsetType visitUndefValue(UndefValue&);
203 SizeOffsetType visitInstruction(Instruction &I);
206 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
209 /// \brief Evaluate the size and offset of an object ponted by a Value*.
210 /// May create code to compute the result at run-time.
211 class ObjectSizeOffsetEvaluator
212 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
214 typedef IRBuilder<true, TargetFolder> BuilderTy;
215 typedef std::pair<WeakVH, WeakVH> WeakEvalType;
216 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
217 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
219 const DataLayout *TD;
220 const TargetLibraryInfo *TLI;
221 LLVMContext &Context;
228 SizeOffsetEvalType unknown() {
229 return std::make_pair((Value*)0, (Value*)0);
231 SizeOffsetEvalType compute_(Value *V);
234 ObjectSizeOffsetEvaluator(const DataLayout *TD, const TargetLibraryInfo *TLI,
235 LLVMContext &Context);
236 SizeOffsetEvalType compute(Value *V);
238 bool knownSize(SizeOffsetEvalType SizeOffset) {
239 return SizeOffset.first;
242 bool knownOffset(SizeOffsetEvalType SizeOffset) {
243 return SizeOffset.second;
246 bool anyKnown(SizeOffsetEvalType SizeOffset) {
247 return knownSize(SizeOffset) || knownOffset(SizeOffset);
250 bool bothKnown(SizeOffsetEvalType SizeOffset) {
251 return knownSize(SizeOffset) && knownOffset(SizeOffset);
254 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
255 SizeOffsetEvalType visitCallSite(CallSite CS);
256 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
257 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
258 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
259 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
260 SizeOffsetEvalType visitLoadInst(LoadInst &I);
261 SizeOffsetEvalType visitPHINode(PHINode &PHI);
262 SizeOffsetEvalType visitSelectInst(SelectInst &I);
263 SizeOffsetEvalType visitInstruction(Instruction &I);
266 } // End llvm namespace