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 27namespace llvm { 28class CallInst; 29class PointerType; 30class DataLayout; 31class TargetLibraryInfo; 32class Type; 33class Value; 34 35 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 38/// like). 39bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, 40 bool LookThroughBitCast = false); 41 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). 44bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, 45 bool LookThroughBitCast = false); 46 47/// \brief Tests if a value is a call or invoke to a library function that 48/// allocates uninitialized memory (such as malloc). 49bool isMallocLikeFn(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 zero-filled memory (such as calloc). 54bool isCallocLikeFn(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 memory (either malloc, calloc, or strdup like). 59bool isAllocLikeFn(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/// reallocates memory (such as realloc). 64bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, 65 bool LookThroughBitCast = false); 66 67/// \brief Tests if a value is a call or invoke to a library function that 68/// allocates memory and never returns null (such as operator new). 69bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI, 70 bool LookThroughBitCast = false); 71 72//===----------------------------------------------------------------------===// 73// malloc Call Utility Functions. 74// 75 76/// extractMallocCall - Returns the corresponding CallInst if the instruction 77/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we 78/// ignore InvokeInst here. 79const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI); 80static inline CallInst *extractMallocCall(Value *I, 81 const TargetLibraryInfo *TLI) { 82 return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI)); 83} 84 85/// isArrayMalloc - Returns the corresponding CallInst if the instruction 86/// is a call to malloc whose array size can be determined and the array size 87/// is not constant 1. Otherwise, return NULL. 88const CallInst *isArrayMalloc(const Value *I, const DataLayout *DL, 89 const TargetLibraryInfo *TLI); 90 91/// getMallocType - Returns the PointerType resulting from the malloc call. 92/// The PointerType depends on the number of bitcast uses of the malloc call: 93/// 0: PointerType is the malloc calls' return type. 94/// 1: PointerType is the bitcast's result type. 95/// >1: Unique PointerType cannot be determined, return NULL. 96PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI); 97 98/// getMallocAllocatedType - Returns the Type allocated by malloc call. 99/// The Type depends on the number of bitcast uses of the malloc call: 100/// 0: PointerType is the malloc calls' return type. 101/// 1: PointerType is the bitcast's result type. 102/// >1: Unique PointerType cannot be determined, return NULL. 103Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI); 104 105/// getMallocArraySize - Returns the array size of a malloc call. If the 106/// argument passed to malloc is a multiple of the size of the malloced type, 107/// then return that multiple. For non-array mallocs, the multiple is 108/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be 109/// determined. 110Value *getMallocArraySize(CallInst *CI, const DataLayout *DL, 111 const TargetLibraryInfo *TLI, 112 bool LookThroughSExt = false); 113 114 115//===----------------------------------------------------------------------===// 116// calloc Call Utility Functions. 117// 118 119/// extractCallocCall - Returns the corresponding CallInst if the instruction 120/// is a calloc call. 121const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI); 122static inline CallInst *extractCallocCall(Value *I, 123 const TargetLibraryInfo *TLI) { 124 return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI)); 125} 126 127 128//===----------------------------------------------------------------------===// 129// free Call Utility Functions. 130// 131 132/// isFreeCall - Returns non-null if the value is a call to the builtin free() 133const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI); 134 135static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { 136 return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); 137} 138 139 140//===----------------------------------------------------------------------===// 141// Utility functions to compute size of objects. 142// 143 144/// \brief Compute the size of the object pointed by Ptr. Returns true and the 145/// object size in Size if successful, and false otherwise. In this context, by 146/// object we mean the region of memory starting at Ptr to the end of the 147/// underlying object pointed to by Ptr. 148/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas, 149/// byval arguments, and global variables. 150bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, 151 const TargetLibraryInfo *TLI, bool RoundToAlign = false); 152 153 154 155typedef std::pair<APInt, APInt> SizeOffsetType; 156 157/// \brief Evaluate the size and offset of an object pointed to by a Value* 158/// statically. Fails if size or offset are not known at compile time. 159class ObjectSizeOffsetVisitor 160 : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> { 161 162 const DataLayout *DL; 163 const TargetLibraryInfo *TLI; 164 bool RoundToAlign; 165 unsigned IntTyBits; 166 APInt Zero; 167 SmallPtrSet<Instruction *, 8> SeenInsts; 168 169 APInt align(APInt Size, uint64_t Align); 170 171 SizeOffsetType unknown() { 172 return std::make_pair(APInt(), APInt()); 173 } 174 175public: 176 ObjectSizeOffsetVisitor(const DataLayout *DL, const TargetLibraryInfo *TLI, 177 LLVMContext &Context, bool RoundToAlign = false); 178 179 SizeOffsetType compute(Value *V); 180 181 bool knownSize(SizeOffsetType &SizeOffset) { 182 return SizeOffset.first.getBitWidth() > 1; 183 } 184 185 bool knownOffset(SizeOffsetType &SizeOffset) { 186 return SizeOffset.second.getBitWidth() > 1; 187 } 188 189 bool bothKnown(SizeOffsetType &SizeOffset) { 190 return knownSize(SizeOffset) && knownOffset(SizeOffset); 191 } 192 193 // These are "private", except they can't actually be made private. Only 194 // compute() should be used by external users. 195 SizeOffsetType visitAllocaInst(AllocaInst &I); 196 SizeOffsetType visitArgument(Argument &A); 197 SizeOffsetType visitCallSite(CallSite CS); 198 SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); 199 SizeOffsetType visitExtractElementInst(ExtractElementInst &I); 200 SizeOffsetType visitExtractValueInst(ExtractValueInst &I); 201 SizeOffsetType visitGEPOperator(GEPOperator &GEP); 202 SizeOffsetType visitGlobalAlias(GlobalAlias &GA); 203 SizeOffsetType visitGlobalVariable(GlobalVariable &GV); 204 SizeOffsetType visitIntToPtrInst(IntToPtrInst&); 205 SizeOffsetType visitLoadInst(LoadInst &I); 206 SizeOffsetType visitPHINode(PHINode&); 207 SizeOffsetType visitSelectInst(SelectInst &I); 208 SizeOffsetType visitUndefValue(UndefValue&); 209 SizeOffsetType visitInstruction(Instruction &I); 210}; 211 212typedef std::pair<Value*, Value*> SizeOffsetEvalType; 213 214 215/// \brief Evaluate the size and offset of an object pointed to by a Value*. 216/// May create code to compute the result at run-time. 217class ObjectSizeOffsetEvaluator 218 : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> { 219 220 typedef IRBuilder<true, TargetFolder> BuilderTy; 221 typedef std::pair<WeakVH, WeakVH> WeakEvalType; 222 typedef DenseMap<const Value*, WeakEvalType> CacheMapTy; 223 typedef SmallPtrSet<const Value*, 8> PtrSetTy; 224 225 const DataLayout *DL; 226 const TargetLibraryInfo *TLI; 227 LLVMContext &Context; 228 BuilderTy Builder; 229 IntegerType *IntTy; 230 Value *Zero; 231 CacheMapTy CacheMap; 232 PtrSetTy SeenVals; 233 bool RoundToAlign; 234 235 SizeOffsetEvalType unknown() { 236 return std::make_pair(nullptr, nullptr); 237 } 238 SizeOffsetEvalType compute_(Value *V); 239 240public: 241 ObjectSizeOffsetEvaluator(const DataLayout *DL, const TargetLibraryInfo *TLI, 242 LLVMContext &Context, bool RoundToAlign = false); 243 SizeOffsetEvalType compute(Value *V); 244 245 bool knownSize(SizeOffsetEvalType SizeOffset) { 246 return SizeOffset.first; 247 } 248 249 bool knownOffset(SizeOffsetEvalType SizeOffset) { 250 return SizeOffset.second; 251 } 252 253 bool anyKnown(SizeOffsetEvalType SizeOffset) { 254 return knownSize(SizeOffset) || knownOffset(SizeOffset); 255 } 256 257 bool bothKnown(SizeOffsetEvalType SizeOffset) { 258 return knownSize(SizeOffset) && knownOffset(SizeOffset); 259 } 260 261 // The individual instruction visitors should be treated as private. 262 SizeOffsetEvalType visitAllocaInst(AllocaInst &I); 263 SizeOffsetEvalType visitCallSite(CallSite CS); 264 SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I); 265 SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I); 266 SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP); 267 SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&); 268 SizeOffsetEvalType visitLoadInst(LoadInst &I); 269 SizeOffsetEvalType visitPHINode(PHINode &PHI); 270 SizeOffsetEvalType visitSelectInst(SelectInst &I); 271 SizeOffsetEvalType visitInstruction(Instruction &I); 272}; 273 274} // End llvm namespace 275 276#endif 277