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 /// \brief Tests if a value is a call or invoke to a library function that
63 /// allocates memory and never returns null (such as operator new).
64 bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
65 bool LookThroughBitCast = false);
67 //===----------------------------------------------------------------------===//
68 // malloc Call Utility Functions.
71 /// extractMallocCall - Returns the corresponding CallInst if the instruction
72 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
73 /// ignore InvokeInst here.
74 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
75 static inline CallInst *extractMallocCall(Value *I,
76 const TargetLibraryInfo *TLI) {
77 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
80 /// getMallocType - Returns the PointerType resulting from the malloc call.
81 /// The PointerType depends on the number of bitcast uses of the malloc call:
82 /// 0: PointerType is the malloc calls' return type.
83 /// 1: PointerType is the bitcast's result type.
84 /// >1: Unique PointerType cannot be determined, return NULL.
85 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
87 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
88 /// The Type 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 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
94 /// getMallocArraySize - Returns the array size of a malloc call. If the
95 /// argument passed to malloc is a multiple of the size of the malloced type,
96 /// then return that multiple. For non-array mallocs, the multiple is
97 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
99 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
100 const TargetLibraryInfo *TLI,
101 bool LookThroughSExt = false);
103 //===----------------------------------------------------------------------===//
104 // calloc Call Utility Functions.
107 /// extractCallocCall - Returns the corresponding CallInst if the instruction
108 /// is a calloc call.
109 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
110 static inline CallInst *extractCallocCall(Value *I,
111 const TargetLibraryInfo *TLI) {
112 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
116 //===----------------------------------------------------------------------===//
117 // free Call Utility Functions.
120 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
121 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
123 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
124 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
128 //===----------------------------------------------------------------------===//
129 // Utility functions to compute size of objects.
132 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
133 /// object size in Size if successful, and false otherwise. In this context, by
134 /// object we mean the region of memory starting at Ptr to the end of the
135 /// underlying object pointed to by Ptr.
136 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
137 /// byval arguments, and global variables.
138 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
139 const TargetLibraryInfo *TLI, bool RoundToAlign = false);
141 typedef std::pair<APInt, APInt> SizeOffsetType;
143 /// \brief Evaluate the size and offset of an object pointed to by a Value*
144 /// statically. Fails if size or offset are not known at compile time.
145 class ObjectSizeOffsetVisitor
146 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
148 const DataLayout &DL;
149 const TargetLibraryInfo *TLI;
153 SmallPtrSet<Instruction *, 8> SeenInsts;
155 APInt align(APInt Size, uint64_t Align);
157 SizeOffsetType unknown() {
158 return std::make_pair(APInt(), APInt());
162 ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
163 LLVMContext &Context, bool RoundToAlign = false);
165 SizeOffsetType compute(Value *V);
167 bool knownSize(SizeOffsetType &SizeOffset) {
168 return SizeOffset.first.getBitWidth() > 1;
171 bool knownOffset(SizeOffsetType &SizeOffset) {
172 return SizeOffset.second.getBitWidth() > 1;
175 bool bothKnown(SizeOffsetType &SizeOffset) {
176 return knownSize(SizeOffset) && knownOffset(SizeOffset);
179 // These are "private", except they can't actually be made private. Only
180 // compute() should be used by external users.
181 SizeOffsetType visitAllocaInst(AllocaInst &I);
182 SizeOffsetType visitArgument(Argument &A);
183 SizeOffsetType visitCallSite(CallSite CS);
184 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
185 SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
186 SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
187 SizeOffsetType visitGEPOperator(GEPOperator &GEP);
188 SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
189 SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
190 SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
191 SizeOffsetType visitLoadInst(LoadInst &I);
192 SizeOffsetType visitPHINode(PHINode&);
193 SizeOffsetType visitSelectInst(SelectInst &I);
194 SizeOffsetType visitUndefValue(UndefValue&);
195 SizeOffsetType visitInstruction(Instruction &I);
198 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
201 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
202 /// May create code to compute the result at run-time.
203 class ObjectSizeOffsetEvaluator
204 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
206 typedef IRBuilder<true, TargetFolder> BuilderTy;
207 typedef std::pair<WeakVH, WeakVH> WeakEvalType;
208 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
209 typedef SmallPtrSet<const Value*, 8> PtrSetTy;
211 const DataLayout &DL;
212 const TargetLibraryInfo *TLI;
213 LLVMContext &Context;
221 SizeOffsetEvalType unknown() {
222 return std::make_pair(nullptr, nullptr);
224 SizeOffsetEvalType compute_(Value *V);
227 ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
228 LLVMContext &Context, bool RoundToAlign = false);
229 SizeOffsetEvalType compute(Value *V);
231 bool knownSize(SizeOffsetEvalType SizeOffset) {
232 return SizeOffset.first;
235 bool knownOffset(SizeOffsetEvalType SizeOffset) {
236 return SizeOffset.second;
239 bool anyKnown(SizeOffsetEvalType SizeOffset) {
240 return knownSize(SizeOffset) || knownOffset(SizeOffset);
243 bool bothKnown(SizeOffsetEvalType SizeOffset) {
244 return knownSize(SizeOffset) && knownOffset(SizeOffset);
247 // The individual instruction visitors should be treated as private.
248 SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
249 SizeOffsetEvalType visitCallSite(CallSite CS);
250 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
251 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
252 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
253 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
254 SizeOffsetEvalType visitLoadInst(LoadInst &I);
255 SizeOffsetEvalType visitPHINode(PHINode &PHI);
256 SizeOffsetEvalType visitSelectInst(SelectInst &I);
257 SizeOffsetEvalType visitInstruction(Instruction &I);
260 } // End llvm namespace