1 //===- llvm/Analysis/MemoryBuiltins.h- Calls to memory builtins -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This family of functions identifies calls to builtin functions that allocate 11 // or free memory. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H 16 #define LLVM_ANALYSIS_MEMORYBUILTINS_H 17 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" 26 27 namespace llvm { 28 class CallInst; 29 class PointerType; 30 class DataLayout; 31 class TargetLibraryInfo; 32 class Type; 33 class Value; 34 35 enum class ObjSizeMode { 36 Exact = 0, 37 Min = 1, 38 Max = 2 39 }; 40 41 /// \brief Tests if a value is a call or invoke to a library function that 42 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup 43 /// like). 44 bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, 45 bool LookThroughBitCast = false); 46 47 /// \brief Tests if a value is a call or invoke to a function that returns a 48 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions). 49 bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, 50 bool LookThroughBitCast = false); 51 52 /// \brief Tests if a value is a call or invoke to a library function that 53 /// allocates uninitialized memory (such as malloc). 54 bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 55 bool LookThroughBitCast = false); 56 57 /// \brief Tests if a value is a call or invoke to a library function that 58 /// allocates zero-filled memory (such as calloc). 59 bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 60 bool LookThroughBitCast = false); 61 62 /// \brief Tests if a value is a call or invoke to a library function that 63 /// allocates memory (either malloc, calloc, or strdup like). 64 bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 65 bool LookThroughBitCast = false); 66 67 //===----------------------------------------------------------------------===// 68 // malloc Call Utility Functions. 69 // 70 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)); 78 } 79 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); 86 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); 93 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 98 /// determined. 99 Value *getMallocArraySize(CallInst *CI, const DataLayout &DL, 100 const TargetLibraryInfo *TLI, 101 bool LookThroughSExt = false); 102 103 //===----------------------------------------------------------------------===// 104 // calloc Call Utility Functions. 105 // 106 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)); 113 } 114 115 116 //===----------------------------------------------------------------------===// 117 // free Call Utility Functions. 118 // 119 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); 122 123 static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { 124 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); 125 } 126 127 128 //===----------------------------------------------------------------------===// 129 // Utility functions to compute size of objects. 130 // 131 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 /// If Mode is Min or Max the size will be evaluated even if it depends on 139 /// a condition and corresponding value will be returned (min or max). 140 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, 141 const TargetLibraryInfo *TLI, bool RoundToAlign = false, 142 ObjSizeMode Mode = ObjSizeMode::Exact); 143 144 typedef std::pair<APInt, APInt> SizeOffsetType; 145 146 /// \brief Evaluate the size and offset of an object pointed to by a Value* 147 /// statically. Fails if size or offset are not known at compile time. 148 class ObjectSizeOffsetVisitor 149 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> { 150 151 const DataLayout &DL; 152 const TargetLibraryInfo *TLI; 153 bool RoundToAlign; 154 ObjSizeMode Mode; 155 unsigned IntTyBits; 156 APInt Zero; 157 SmallPtrSet<Instruction *, 8> SeenInsts; 158 159 APInt align(APInt Size, uint64_t Align); 160 161 SizeOffsetType unknown() { 162 return std::make_pair(APInt(), APInt()); 163 } 164 165 public: 166 ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, 167 LLVMContext &Context, bool RoundToAlign = false, 168 ObjSizeMode Mode = ObjSizeMode::Exact); 169 170 SizeOffsetType compute(Value *V); 171 172 static bool knownSize(const SizeOffsetType &SizeOffset) { 173 return SizeOffset.first.getBitWidth() > 1; 174 } 175 176 static bool knownOffset(const SizeOffsetType &SizeOffset) { 177 return SizeOffset.second.getBitWidth() > 1; 178 } 179 180 static bool bothKnown(const SizeOffsetType &SizeOffset) { 181 return knownSize(SizeOffset) && knownOffset(SizeOffset); 182 } 183 184 // These are "private", except they can't actually be made private. Only 185 // compute() should be used by external users. 186 SizeOffsetType visitAllocaInst(AllocaInst &I); 187 SizeOffsetType visitArgument(Argument &A); 188 SizeOffsetType visitCallSite(CallSite CS); 189 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); 190 SizeOffsetType visitExtractElementInst(ExtractElementInst &I); 191 SizeOffsetType visitExtractValueInst(ExtractValueInst &I); 192 SizeOffsetType visitGEPOperator(GEPOperator &GEP); 193 SizeOffsetType visitGlobalAlias(GlobalAlias &GA); 194 SizeOffsetType visitGlobalVariable(GlobalVariable &GV); 195 SizeOffsetType visitIntToPtrInst(IntToPtrInst&); 196 SizeOffsetType visitLoadInst(LoadInst &I); 197 SizeOffsetType visitPHINode(PHINode&); 198 SizeOffsetType visitSelectInst(SelectInst &I); 199 SizeOffsetType visitUndefValue(UndefValue&); 200 SizeOffsetType visitInstruction(Instruction &I); 201 }; 202 203 typedef std::pair<Value*, Value*> SizeOffsetEvalType; 204 205 206 /// \brief Evaluate the size and offset of an object pointed to by a Value*. 207 /// May create code to compute the result at run-time. 208 class ObjectSizeOffsetEvaluator 209 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> { 210 211 typedef IRBuilder<TargetFolder> BuilderTy; 212 typedef std::pair<WeakVH, WeakVH> WeakEvalType; 213 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy; 214 typedef SmallPtrSet<const Value*, 8> PtrSetTy; 215 216 const DataLayout &DL; 217 const TargetLibraryInfo *TLI; 218 LLVMContext &Context; 219 BuilderTy Builder; 220 IntegerType *IntTy; 221 Value *Zero; 222 CacheMapTy CacheMap; 223 PtrSetTy SeenVals; 224 bool RoundToAlign; 225 226 SizeOffsetEvalType unknown() { 227 return std::make_pair(nullptr, nullptr); 228 } 229 SizeOffsetEvalType compute_(Value *V); 230 231 public: 232 ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, 233 LLVMContext &Context, bool RoundToAlign = false); 234 SizeOffsetEvalType compute(Value *V); 235 236 bool knownSize(SizeOffsetEvalType SizeOffset) { 237 return SizeOffset.first; 238 } 239 240 bool knownOffset(SizeOffsetEvalType SizeOffset) { 241 return SizeOffset.second; 242 } 243 244 bool anyKnown(SizeOffsetEvalType SizeOffset) { 245 return knownSize(SizeOffset) || knownOffset(SizeOffset); 246 } 247 248 bool bothKnown(SizeOffsetEvalType SizeOffset) { 249 return knownSize(SizeOffset) && knownOffset(SizeOffset); 250 } 251 252 // The individual instruction visitors should be treated as private. 253 SizeOffsetEvalType visitAllocaInst(AllocaInst &I); 254 SizeOffsetEvalType visitCallSite(CallSite CS); 255 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I); 256 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I); 257 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP); 258 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&); 259 SizeOffsetEvalType visitLoadInst(LoadInst &I); 260 SizeOffsetEvalType visitPHINode(PHINode &PHI); 261 SizeOffsetEvalType visitSelectInst(SelectInst &I); 262 SizeOffsetEvalType visitInstruction(Instruction &I); 263 }; 264 265 } // End llvm namespace 266 267 #endif 268