LLVM API Documentation
00001 //===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This family of functions identifies calls to builtin functions that allocate 00011 // or free memory. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H 00016 #define LLVM_ANALYSIS_MEMORYBUILTINS_H 00017 00018 #include "llvm/ADT/DenseMap.h" 00019 #include "llvm/ADT/SmallPtrSet.h" 00020 #include "llvm/Analysis/TargetFolder.h" 00021 #include "llvm/IR/IRBuilder.h" 00022 #include "llvm/IR/InstVisitor.h" 00023 #include "llvm/IR/Operator.h" 00024 #include "llvm/IR/ValueHandle.h" 00025 #include "llvm/Support/DataTypes.h" 00026 00027 namespace llvm { 00028 class CallInst; 00029 class PointerType; 00030 class DataLayout; 00031 class TargetLibraryInfo; 00032 class Type; 00033 class Value; 00034 00035 00036 /// \brief Tests if a value is a call or invoke to a library function that 00037 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup 00038 /// like). 00039 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, 00040 bool LookThroughBitCast = false); 00041 00042 /// \brief Tests if a value is a call or invoke to a function that returns a 00043 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions). 00044 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, 00045 bool LookThroughBitCast = false); 00046 00047 /// \brief Tests if a value is a call or invoke to a library function that 00048 /// allocates uninitialized memory (such as malloc). 00049 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 00050 bool LookThroughBitCast = false); 00051 00052 /// \brief Tests if a value is a call or invoke to a library function that 00053 /// allocates zero-filled memory (such as calloc). 00054 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 00055 bool LookThroughBitCast = false); 00056 00057 /// \brief Tests if a value is a call or invoke to a library function that 00058 /// allocates memory (either malloc, calloc, or strdup like). 00059 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 00060 bool LookThroughBitCast = false); 00061 00062 /// \brief Tests if a value is a call or invoke to a library function that 00063 /// reallocates memory (such as realloc). 00064 bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 00065 bool LookThroughBitCast = false); 00066 00067 /// \brief Tests if a value is a call or invoke to a library function that 00068 /// allocates memory and never returns null (such as operator new). 00069 bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI, 00070 bool LookThroughBitCast = false); 00071 00072 //===----------------------------------------------------------------------===// 00073 // malloc Call Utility Functions. 00074 // 00075 00076 /// extractMallocCall - Returns the corresponding CallInst if the instruction 00077 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we 00078 /// ignore InvokeInst here. 00079 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI); 00080 static inline CallInst *extractMallocCall(Value *I, 00081 const TargetLibraryInfo *TLI) { 00082 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI)); 00083 } 00084 00085 /// isArrayMalloc - Returns the corresponding CallInst if the instruction 00086 /// is a call to malloc whose array size can be determined and the array size 00087 /// is not constant 1. Otherwise, return NULL. 00088 const CallInst *isArrayMalloc(const Value *I, const DataLayout *DL, 00089 const TargetLibraryInfo *TLI); 00090 00091 /// getMallocType - Returns the PointerType resulting from the malloc call. 00092 /// The PointerType depends on the number of bitcast uses of the malloc call: 00093 /// 0: PointerType is the malloc calls' return type. 00094 /// 1: PointerType is the bitcast's result type. 00095 /// >1: Unique PointerType cannot be determined, return NULL. 00096 PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI); 00097 00098 /// getMallocAllocatedType - Returns the Type allocated by malloc call. 00099 /// The Type depends on the number of bitcast uses of the malloc call: 00100 /// 0: PointerType is the malloc calls' return type. 00101 /// 1: PointerType is the bitcast's result type. 00102 /// >1: Unique PointerType cannot be determined, return NULL. 00103 Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI); 00104 00105 /// getMallocArraySize - Returns the array size of a malloc call. If the 00106 /// argument passed to malloc is a multiple of the size of the malloced type, 00107 /// then return that multiple. For non-array mallocs, the multiple is 00108 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be 00109 /// determined. 00110 Value *getMallocArraySize(CallInst *CI, const DataLayout *DL, 00111 const TargetLibraryInfo *TLI, 00112 bool LookThroughSExt = false); 00113 00114 00115 //===----------------------------------------------------------------------===// 00116 // calloc Call Utility Functions. 00117 // 00118 00119 /// extractCallocCall - Returns the corresponding CallInst if the instruction 00120 /// is a calloc call. 00121 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI); 00122 static inline CallInst *extractCallocCall(Value *I, 00123 const TargetLibraryInfo *TLI) { 00124 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI)); 00125 } 00126 00127 00128 //===----------------------------------------------------------------------===// 00129 // free Call Utility Functions. 00130 // 00131 00132 /// isFreeCall - Returns non-null if the value is a call to the builtin free() 00133 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI); 00134 00135 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { 00136 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); 00137 } 00138 00139 00140 //===----------------------------------------------------------------------===// 00141 // Utility functions to compute size of objects. 00142 // 00143 00144 /// \brief Compute the size of the object pointed by Ptr. Returns true and the 00145 /// object size in Size if successful, and false otherwise. In this context, by 00146 /// object we mean the region of memory starting at Ptr to the end of the 00147 /// underlying object pointed to by Ptr. 00148 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas, 00149 /// byval arguments, and global variables. 00150 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, 00151 const TargetLibraryInfo *TLI, bool RoundToAlign = false); 00152 00153 00154 00155 typedef std::pair<APInt, APInt> SizeOffsetType; 00156 00157 /// \brief Evaluate the size and offset of an object pointed to by a Value* 00158 /// statically. Fails if size or offset are not known at compile time. 00159 class ObjectSizeOffsetVisitor 00160 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> { 00161 00162 const DataLayout *DL; 00163 const TargetLibraryInfo *TLI; 00164 bool RoundToAlign; 00165 unsigned IntTyBits; 00166 APInt Zero; 00167 SmallPtrSet<Instruction *, 8> SeenInsts; 00168 00169 APInt align(APInt Size, uint64_t Align); 00170 00171 SizeOffsetType unknown() { 00172 return std::make_pair(APInt(), APInt()); 00173 } 00174 00175 public: 00176 ObjectSizeOffsetVisitor(const DataLayout *DL, const TargetLibraryInfo *TLI, 00177 LLVMContext &Context, bool RoundToAlign = false); 00178 00179 SizeOffsetType compute(Value *V); 00180 00181 bool knownSize(SizeOffsetType &SizeOffset) { 00182 return SizeOffset.first.getBitWidth() > 1; 00183 } 00184 00185 bool knownOffset(SizeOffsetType &SizeOffset) { 00186 return SizeOffset.second.getBitWidth() > 1; 00187 } 00188 00189 bool bothKnown(SizeOffsetType &SizeOffset) { 00190 return knownSize(SizeOffset) && knownOffset(SizeOffset); 00191 } 00192 00193 // These are "private", except they can't actually be made private. Only 00194 // compute() should be used by external users. 00195 SizeOffsetType visitAllocaInst(AllocaInst &I); 00196 SizeOffsetType visitArgument(Argument &A); 00197 SizeOffsetType visitCallSite(CallSite CS); 00198 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); 00199 SizeOffsetType visitExtractElementInst(ExtractElementInst &I); 00200 SizeOffsetType visitExtractValueInst(ExtractValueInst &I); 00201 SizeOffsetType visitGEPOperator(GEPOperator &GEP); 00202 SizeOffsetType visitGlobalAlias(GlobalAlias &GA); 00203 SizeOffsetType visitGlobalVariable(GlobalVariable &GV); 00204 SizeOffsetType visitIntToPtrInst(IntToPtrInst&); 00205 SizeOffsetType visitLoadInst(LoadInst &I); 00206 SizeOffsetType visitPHINode(PHINode&); 00207 SizeOffsetType visitSelectInst(SelectInst &I); 00208 SizeOffsetType visitUndefValue(UndefValue&); 00209 SizeOffsetType visitInstruction(Instruction &I); 00210 }; 00211 00212 typedef std::pair<Value*, Value*> SizeOffsetEvalType; 00213 00214 00215 /// \brief Evaluate the size and offset of an object pointed to by a Value*. 00216 /// May create code to compute the result at run-time. 00217 class ObjectSizeOffsetEvaluator 00218 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> { 00219 00220 typedef IRBuilder<true, TargetFolder> BuilderTy; 00221 typedef std::pair<WeakVH, WeakVH> WeakEvalType; 00222 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy; 00223 typedef SmallPtrSet<const Value*, 8> PtrSetTy; 00224 00225 const DataLayout *DL; 00226 const TargetLibraryInfo *TLI; 00227 LLVMContext &Context; 00228 BuilderTy Builder; 00229 IntegerType *IntTy; 00230 Value *Zero; 00231 CacheMapTy CacheMap; 00232 PtrSetTy SeenVals; 00233 bool RoundToAlign; 00234 00235 SizeOffsetEvalType unknown() { 00236 return std::make_pair(nullptr, nullptr); 00237 } 00238 SizeOffsetEvalType compute_(Value *V); 00239 00240 public: 00241 ObjectSizeOffsetEvaluator(const DataLayout *DL, const TargetLibraryInfo *TLI, 00242 LLVMContext &Context, bool RoundToAlign = false); 00243 SizeOffsetEvalType compute(Value *V); 00244 00245 bool knownSize(SizeOffsetEvalType SizeOffset) { 00246 return SizeOffset.first; 00247 } 00248 00249 bool knownOffset(SizeOffsetEvalType SizeOffset) { 00250 return SizeOffset.second; 00251 } 00252 00253 bool anyKnown(SizeOffsetEvalType SizeOffset) { 00254 return knownSize(SizeOffset) || knownOffset(SizeOffset); 00255 } 00256 00257 bool bothKnown(SizeOffsetEvalType SizeOffset) { 00258 return knownSize(SizeOffset) && knownOffset(SizeOffset); 00259 } 00260 00261 // The individual instruction visitors should be treated as private. 00262 SizeOffsetEvalType visitAllocaInst(AllocaInst &I); 00263 SizeOffsetEvalType visitCallSite(CallSite CS); 00264 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I); 00265 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I); 00266 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP); 00267 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&); 00268 SizeOffsetEvalType visitLoadInst(LoadInst &I); 00269 SizeOffsetEvalType visitPHINode(PHINode &PHI); 00270 SizeOffsetEvalType visitSelectInst(SelectInst &I); 00271 SizeOffsetEvalType visitInstruction(Instruction &I); 00272 }; 00273 00274 } // End llvm namespace 00275 00276 #endif