MemoryBuiltins.h 10 KB

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