LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MemoryBuiltins.h
Go to the documentation of this file.
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/IR/IRBuilder.h"
21 #include "llvm/IR/Operator.h"
22 #include "llvm/InstVisitor.h"
23 #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 
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).
39 bool 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).
44 bool 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).
49 bool 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).
54 bool 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).
59 bool 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).
64 bool 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).
69 bool 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.
79 const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
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.
88 const 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.
96 PointerType *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.
103 Type *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.
110 Value *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.
121 const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
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()
133 const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
134 
135 static 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.
150 bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL,
151  const TargetLibraryInfo *TLI, bool RoundToAlign = false);
152 
153 
154 
155 typedef 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.
160  : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
161 
162  const DataLayout *DL;
163  const TargetLibraryInfo *TLI;
164  bool RoundToAlign;
165  unsigned IntTyBits;
166  APInt Zero;
168 
169  APInt align(APInt Size, uint64_t Align);
170 
171  SizeOffsetType unknown() {
172  return std::make_pair(APInt(), APInt());
173  }
174 
175 public:
177  LLVMContext &Context, bool RoundToAlign = false);
178 
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 
208 };
209 
210 typedef std::pair<Value*, Value*> SizeOffsetEvalType;
211 
212 
213 /// \brief Evaluate the size and offset of an object pointed to by a Value*.
214 /// May create code to compute the result at run-time.
216  : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
217 
219  typedef std::pair<WeakVH, WeakVH> WeakEvalType;
222 
223  const DataLayout *DL;
224  const TargetLibraryInfo *TLI;
225  LLVMContext &Context;
226  BuilderTy Builder;
227  IntegerType *IntTy;
228  Value *Zero;
229  CacheMapTy CacheMap;
230  PtrSetTy SeenVals;
231  bool RoundToAlign;
232 
233  SizeOffsetEvalType unknown() {
234  return std::make_pair((Value*)0, (Value*)0);
235  }
236  SizeOffsetEvalType compute_(Value *V);
237 
238 public:
240  LLVMContext &Context, bool RoundToAlign = false);
242 
243  bool knownSize(SizeOffsetEvalType SizeOffset) {
244  return SizeOffset.first;
245  }
246 
247  bool knownOffset(SizeOffsetEvalType SizeOffset) {
248  return SizeOffset.second;
249  }
250 
251  bool anyKnown(SizeOffsetEvalType SizeOffset) {
252  return knownSize(SizeOffset) || knownOffset(SizeOffset);
253  }
254 
255  bool bothKnown(SizeOffsetEvalType SizeOffset) {
256  return knownSize(SizeOffset) && knownOffset(SizeOffset);
257  }
258 
269 };
270 
271 } // End llvm namespace
272 
273 #endif
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
ObjectSizeOffsetVisitor(const DataLayout *DL, const TargetLibraryInfo *TLI, LLVMContext &Context, bool RoundToAlign=false)
LLVM Argument representation.
Definition: Argument.h:35
Base class for instruction visitors.
Definition: InstVisitor.h:81
const CallInst * extractCallocCall(const Value *I, const TargetLibraryInfo *TLI)
SizeOffsetType visitAllocaInst(AllocaInst &I)
bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that reallocates memory (such as realloc)...
SizeOffsetType visitArgument(Argument &A)
bool knownSize(SizeOffsetType &SizeOffset)
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP)
SizeOffsetType visitExtractValueInst(ExtractValueInst &I)
SizeOffsetType visitGEPOperator(GEPOperator &GEP)
bool knownSize(SizeOffsetEvalType SizeOffset)
bool isOperatorNewLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory and never returns nu...
SizeOffsetEvalType visitCallSite(CallSite CS)
SizeOffsetType visitIntToPtrInst(IntToPtrInst &)
bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a function that returns a NoAlias pointer (including malloc/c...
Evaluate the size and offset of an object pointed to by a Value*. May create code to compute the resu...
std::pair< APInt, APInt > SizeOffsetType
SizeOffsetType visitInstruction(Instruction &I)
SizeOffsetType visitGlobalVariable(GlobalVariable &GV)
SizeOffsetEvalType visitSelectInst(SelectInst &I)
const CallInst * extractMallocCall(const Value *I, const TargetLibraryInfo *TLI)
Integer representation type.
Definition: DerivedTypes.h:37
This class represents a cast from an integer to a pointer.
ObjectSizeOffsetEvaluator(const DataLayout *DL, const TargetLibraryInfo *TLI, LLVMContext &Context, bool RoundToAlign=false)
SizeOffsetType visitCallSite(CallSite CS)
PointerType * getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI)
SizeOffsetType visitSelectInst(SelectInst &I)
SizeOffsetType visitExtractElementInst(ExtractElementInst &I)
SizeOffsetType visitGlobalAlias(GlobalAlias &GA)
SizeOffsetEvalType visitInstruction(Instruction &I)
Evaluate the size and offset of an object pointed to by a Value* statically. Fails if size or offset ...
SizeOffsetType visitPHINode(PHINode &)
SizeOffsetType visitUndefValue(UndefValue &)
SizeOffsetType compute(Value *V)
Class for arbitrary precision integers.
Definition: APInt.h:75
std::pair< Value *, Value * > SizeOffsetEvalType
SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst &)
Type * getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI)
const CallInst * isArrayMalloc(const Value *I, const DataLayout *DL, const TargetLibraryInfo *TLI)
SizeOffsetEvalType visitPHINode(PHINode &PHI)
bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such ...
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
Value * getMallocArraySize(CallInst *CI, const DataLayout *DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
SizeOffsetEvalType compute(Value *V)
SizeOffsetEvalType visitLoadInst(LoadInst &I)
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as...
#define I(x, y, z)
Definition: MD5.cpp:54
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
bool knownOffset(SizeOffsetEvalType SizeOffset)
bool anyKnown(SizeOffsetEvalType SizeOffset)
bool bothKnown(SizeOffsetType &SizeOffset)
bool knownOffset(SizeOffsetType &SizeOffset)
LLVM Value Representation.
Definition: Value.h:66
SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I)
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, const TargetLibraryInfo *TLI, bool RoundToAlign=false)
Compute the size of the object pointed by Ptr. Returns true and the object size in Size if successful...
SizeOffsetType visitLoadInst(LoadInst &I)
SizeOffsetEvalType visitAllocaInst(AllocaInst &I)
bool bothKnown(SizeOffsetEvalType SizeOffset)
SizeOffsetType visitConstantPointerNull(ConstantPointerNull &)
SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I)