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/Analysis/TargetFolder.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/IR/Operator.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Support/DataTypes.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 //===----------------------------------------------------------------------===//
63 // malloc Call Utility Functions.
66 /// extractMallocCall - Returns the corresponding CallInst if the instruction
67 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
68 /// ignore InvokeInst here.
69 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
70 static inline CallInst *extractMallocCall(Value *I,
71 const TargetLibraryInfo *TLI) {
72 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
75 /// getMallocType - Returns the PointerType resulting from the malloc call.
76 /// The PointerType depends on the number of bitcast uses of the malloc call:
77 /// 0: PointerType is the malloc calls' return type.
78 /// 1: PointerType is the bitcast's result type.
79 /// >1: Unique PointerType cannot be determined, return NULL.
80 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
82 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
83 /// The Type depends on the number of bitcast uses of the malloc call:
84 /// 0: PointerType is the malloc calls' return type.
85 /// 1: PointerType is the bitcast's result type.
86 /// >1: Unique PointerType cannot be determined, return NULL.
87 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
89 /// getMallocArraySize - Returns the array size of a malloc call. If the
90 /// argument passed to malloc is a multiple of the size of the malloced type,
91 /// then return that multiple. For non-array mallocs, the multiple is
92 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
94 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
95 const TargetLibraryInfo *TLI,
96 bool LookThroughSExt = false);
98 //===----------------------------------------------------------------------===//
99 // calloc Call Utility Functions.
102 /// extractCallocCall - Returns the corresponding CallInst if the instruction
103 /// is a calloc call.
104 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
105 static inline CallInst *extractCallocCall(Value *I,
106 const TargetLibraryInfo *TLI) {
107 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
111 //===----------------------------------------------------------------------===//
112 // free Call Utility Functions.
115 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
116 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
118 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
119 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
123 //===----------------------------------------------------------------------===//
124 // Utility functions to compute size of objects.
127 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
128 /// object size in Size if successful, and false otherwise. In this context, by
129 /// object we mean the region of memory starting at Ptr to the end of the
130 /// underlying object pointed to by Ptr.
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 DataLayout &DL,
134 const TargetLibraryInfo *TLI, bool RoundToAlign = false);
136 typedef std::pair<APInt, APInt> SizeOffsetType;
138 /// \brief Evaluate the size and offset of an object pointed to by a Value*
139 /// statically. Fails if size or offset are not known at compile time.
140 class ObjectSizeOffsetVisitor
141 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
143 const DataLayout &DL;
144 const TargetLibraryInfo *TLI;
148 SmallPtrSet<Instruction *, 8> SeenInsts;
150 APInt align(APInt Size, uint64_t Align);
152 SizeOffsetType unknown() {
153 return std::make_pair(APInt(), APInt());
157 ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
158 LLVMContext &Context, 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 // These are "private", except they can't actually be made private. Only
175 // compute() should be used by external users.
176 SizeOffsetType visitAllocaInst(AllocaInst &I);
177 SizeOffsetType visitArgument(Argument &A);
178 SizeOffsetType visitCallSite(CallSite CS);
179 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
180 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
181 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
182 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
183 SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
184 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
185 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
186 SizeOffsetType visitLoadInst(LoadInst &I);
187 SizeOffsetType visitPHINode(PHINode&);
188 SizeOffsetType visitSelectInst(SelectInst &I);
189 SizeOffsetType visitUndefValue(UndefValue&);
190 SizeOffsetType visitInstruction(Instruction &I);
193 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
196 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
197 /// May create code to compute the result at run-time.
198 class ObjectSizeOffsetEvaluator
199 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
201 typedef IRBuilder<true, TargetFolder> BuilderTy;
202 typedef std::pair<WeakVH, WeakVH> WeakEvalType;
203 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
204 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
206 const DataLayout &DL;
207 const TargetLibraryInfo *TLI;
208 LLVMContext &Context;
216 SizeOffsetEvalType unknown() {
217 return std::make_pair(nullptr, nullptr);
219 SizeOffsetEvalType compute_(Value *V);
222 ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
223 LLVMContext &Context, bool RoundToAlign = false);
224 SizeOffsetEvalType compute(Value *V);
226 bool knownSize(SizeOffsetEvalType SizeOffset) {
227 return SizeOffset.first;
230 bool knownOffset(SizeOffsetEvalType SizeOffset) {
231 return SizeOffset.second;
234 bool anyKnown(SizeOffsetEvalType SizeOffset) {
235 return knownSize(SizeOffset) || knownOffset(SizeOffset);
238 bool bothKnown(SizeOffsetEvalType SizeOffset) {
239 return knownSize(SizeOffset) && knownOffset(SizeOffset);
242 // The individual instruction visitors should be treated as private.
243 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
244 SizeOffsetEvalType visitCallSite(CallSite CS);
245 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
246 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
247 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
248 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
249 SizeOffsetEvalType visitLoadInst(LoadInst &I);
250 SizeOffsetEvalType visitPHINode(PHINode &PHI);
251 SizeOffsetEvalType visitSelectInst(SelectInst &I);
252 SizeOffsetEvalType visitInstruction(Instruction &I);
255 } // End llvm namespace