/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/include/llvm/Analysis/MemoryBuiltins.h
Line | Count | Source (jump to first uncovered line) |
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/APInt.h" |
19 | | #include "llvm/ADT/DenseMap.h" |
20 | | #include "llvm/ADT/SmallPtrSet.h" |
21 | | #include "llvm/Analysis/TargetFolder.h" |
22 | | #include "llvm/IR/CallSite.h" |
23 | | #include "llvm/IR/IRBuilder.h" |
24 | | #include "llvm/IR/InstVisitor.h" |
25 | | #include "llvm/IR/ValueHandle.h" |
26 | | #include <cstdint> |
27 | | #include <utility> |
28 | | |
29 | | namespace llvm { |
30 | | |
31 | | class AllocaInst; |
32 | | class Argument; |
33 | | class CallInst; |
34 | | class ConstantInt; |
35 | | class ConstantPointerNull; |
36 | | class DataLayout; |
37 | | class ExtractElementInst; |
38 | | class ExtractValueInst; |
39 | | class GEPOperator; |
40 | | class GlobalAlias; |
41 | | class GlobalVariable; |
42 | | class Instruction; |
43 | | class IntegerType; |
44 | | class IntrinsicInst; |
45 | | class IntToPtrInst; |
46 | | class LLVMContext; |
47 | | class LoadInst; |
48 | | class PHINode; |
49 | | class PointerType; |
50 | | class SelectInst; |
51 | | class TargetLibraryInfo; |
52 | | class Type; |
53 | | class UndefValue; |
54 | | class Value; |
55 | | |
56 | | /// \brief Tests if a value is a call or invoke to a library function that |
57 | | /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup |
58 | | /// like). |
59 | | bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, |
60 | | bool LookThroughBitCast = false); |
61 | | |
62 | | /// \brief Tests if a value is a call or invoke to a function that returns a |
63 | | /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions). |
64 | | bool isNoAliasFn(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 uninitialized memory (such as malloc). |
69 | | bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
70 | | bool LookThroughBitCast = false); |
71 | | |
72 | | /// \brief Tests if a value is a call or invoke to a library function that |
73 | | /// allocates zero-filled memory (such as calloc). |
74 | | bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
75 | | bool LookThroughBitCast = false); |
76 | | |
77 | | /// \brief Tests if a value is a call or invoke to a library function that |
78 | | /// allocates memory similar to malloc or calloc. |
79 | | bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
80 | | bool LookThroughBitCast = false); |
81 | | |
82 | | /// \brief Tests if a value is a call or invoke to a library function that |
83 | | /// allocates memory (either malloc, calloc, or strdup like). |
84 | | bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
85 | | bool LookThroughBitCast = false); |
86 | | |
87 | | //===----------------------------------------------------------------------===// |
88 | | // malloc Call Utility Functions. |
89 | | // |
90 | | |
91 | | /// extractMallocCall - Returns the corresponding CallInst if the instruction |
92 | | /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we |
93 | | /// ignore InvokeInst here. |
94 | | const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI); |
95 | | static inline CallInst *extractMallocCall(Value *I, |
96 | 3.54k | const TargetLibraryInfo *TLI) { |
97 | 3.54k | return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI)); |
98 | 3.54k | } Unexecuted instantiation: Local.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: SeparateConstOffsetFromGEP.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: NewGVN.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MergedLoadStoreMotion.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: LICM.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GVN.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: DeadStoreElimination.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: BoundsChecking.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: AddressSanitizer.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstCombineCompares.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstCombineCalls.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstructionCombining.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InferFunctionAttrs.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) GlobalOpt.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 96 | 3.54k | const TargetLibraryInfo *TLI) { | 97 | 3.54k | return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI)); | 98 | 3.54k | } |
Unexecuted instantiation: CodeGenPrepare.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryDependenceAnalysis.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryBuiltins.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstructionSimplify.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GlobalsModRef.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CFLSteensAliasAnalysis.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CFLAndersAliasAnalysis.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: BasicAliasAnalysis.cpp:llvm::extractMallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) |
99 | | |
100 | | /// getMallocType - Returns the PointerType resulting from the malloc call. |
101 | | /// The PointerType depends on the number of bitcast uses of the malloc call: |
102 | | /// 0: PointerType is the malloc calls' return type. |
103 | | /// 1: PointerType is the bitcast's result type. |
104 | | /// >1: Unique PointerType cannot be determined, return NULL. |
105 | | PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI); |
106 | | |
107 | | /// getMallocAllocatedType - Returns the Type allocated by malloc call. |
108 | | /// The Type depends on the number of bitcast uses of the malloc call: |
109 | | /// 0: PointerType is the malloc calls' return type. |
110 | | /// 1: PointerType is the bitcast's result type. |
111 | | /// >1: Unique PointerType cannot be determined, return NULL. |
112 | | Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI); |
113 | | |
114 | | /// getMallocArraySize - Returns the array size of a malloc call. If the |
115 | | /// argument passed to malloc is a multiple of the size of the malloced type, |
116 | | /// then return that multiple. For non-array mallocs, the multiple is |
117 | | /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be |
118 | | /// determined. |
119 | | Value *getMallocArraySize(CallInst *CI, const DataLayout &DL, |
120 | | const TargetLibraryInfo *TLI, |
121 | | bool LookThroughSExt = false); |
122 | | |
123 | | //===----------------------------------------------------------------------===// |
124 | | // calloc Call Utility Functions. |
125 | | // |
126 | | |
127 | | /// extractCallocCall - Returns the corresponding CallInst if the instruction |
128 | | /// is a calloc call. |
129 | | const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI); |
130 | | static inline CallInst *extractCallocCall(Value *I, |
131 | 0 | const TargetLibraryInfo *TLI) { |
132 | 0 | return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI)); |
133 | 0 | } Unexecuted instantiation: BasicAliasAnalysis.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CFLAndersAliasAnalysis.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CFLSteensAliasAnalysis.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GlobalsModRef.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstructionSimplify.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryBuiltins.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryDependenceAnalysis.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CodeGenPrepare.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GlobalOpt.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InferFunctionAttrs.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstructionCombining.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstCombineCalls.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstCombineCompares.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: AddressSanitizer.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: BoundsChecking.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: DeadStoreElimination.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GVN.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: LICM.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MergedLoadStoreMotion.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: NewGVN.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: SeparateConstOffsetFromGEP.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: Local.cpp:llvm::extractCallocCall(llvm::Value*, llvm::TargetLibraryInfo const*) |
134 | | |
135 | | |
136 | | //===----------------------------------------------------------------------===// |
137 | | // free Call Utility Functions. |
138 | | // |
139 | | |
140 | | /// isFreeCall - Returns non-null if the value is a call to the builtin free() |
141 | | const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI); |
142 | | |
143 | 119M | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { |
144 | 119M | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); |
145 | 119M | } Local.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 70.5M | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 70.5M | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 70.5M | } |
Unexecuted instantiation: SeparateConstOffsetFromGEP.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: NewGVN.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MergedLoadStoreMotion.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: LICM.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GVN.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) DeadStoreElimination.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 19.6M | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 19.6M | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 19.6M | } |
Unexecuted instantiation: BoundsChecking.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: AddressSanitizer.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstCombineCompares.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) InstCombineCalls.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 28.0M | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 28.0M | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 28.0M | } |
InstructionCombining.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 534k | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 534k | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 534k | } |
Unexecuted instantiation: InferFunctionAttrs.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: GlobalOpt.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: CodeGenPrepare.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryDependenceAnalysis.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: MemoryBuiltins.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Unexecuted instantiation: InstructionSimplify.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) GlobalsModRef.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 1.07M | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 1.07M | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 1.07M | } |
CFLSteensAliasAnalysis.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 59 | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 59 | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 59 | } |
CFLAndersAliasAnalysis.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) Line | Count | Source | 143 | 50 | static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) { | 144 | 50 | return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI)); | 145 | 50 | } |
Unexecuted instantiation: BasicAliasAnalysis.cpp:llvm::isFreeCall(llvm::Value*, llvm::TargetLibraryInfo const*) |
146 | | |
147 | | //===----------------------------------------------------------------------===// |
148 | | // Utility functions to compute size of objects. |
149 | | // |
150 | | |
151 | | /// Various options to control the behavior of getObjectSize. |
152 | | struct ObjectSizeOpts { |
153 | | /// Controls how we handle conditional statements with unknown conditions. |
154 | | enum class Mode : uint8_t { |
155 | | /// Fail to evaluate an unknown condition. |
156 | | Exact, |
157 | | /// Evaluate all branches of an unknown condition. If all evaluations |
158 | | /// succeed, pick the minimum size. |
159 | | Min, |
160 | | /// Same as Min, except we pick the maximum size of all of the branches. |
161 | | Max |
162 | | }; |
163 | | |
164 | | /// How we want to evaluate this object's size. |
165 | | Mode EvalMode = Mode::Exact; |
166 | | /// Whether to round the result up to the alignment of allocas, byval |
167 | | /// arguments, and global variables. |
168 | | bool RoundToAlign = false; |
169 | | /// If this is true, null pointers in address space 0 will be treated as |
170 | | /// though they can't be evaluated. Otherwise, null is always considered to |
171 | | /// point to a 0 byte region of memory. |
172 | | bool NullIsUnknownSize = false; |
173 | | }; |
174 | | |
175 | | /// \brief Compute the size of the object pointed by Ptr. Returns true and the |
176 | | /// object size in Size if successful, and false otherwise. In this context, by |
177 | | /// object we mean the region of memory starting at Ptr to the end of the |
178 | | /// underlying object pointed to by Ptr. |
179 | | bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, |
180 | | const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {}); |
181 | | |
182 | | /// Try to turn a call to @llvm.objectsize into an integer value of the given |
183 | | /// Type. Returns null on failure. |
184 | | /// If MustSucceed is true, this function will not return null, and may return |
185 | | /// conservative values governed by the second argument of the call to |
186 | | /// objectsize. |
187 | | ConstantInt *lowerObjectSizeCall(IntrinsicInst *ObjectSize, |
188 | | const DataLayout &DL, |
189 | | const TargetLibraryInfo *TLI, |
190 | | bool MustSucceed); |
191 | | |
192 | | using SizeOffsetType = std::pair<APInt, APInt>; |
193 | | |
194 | | /// \brief Evaluate the size and offset of an object pointed to by a Value* |
195 | | /// statically. Fails if size or offset are not known at compile time. |
196 | | class ObjectSizeOffsetVisitor |
197 | | : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> { |
198 | | const DataLayout &DL; |
199 | | const TargetLibraryInfo *TLI; |
200 | | ObjectSizeOpts Options; |
201 | | unsigned IntTyBits; |
202 | | APInt Zero; |
203 | | SmallPtrSet<Instruction *, 8> SeenInsts; |
204 | | |
205 | | APInt align(APInt Size, uint64_t Align); |
206 | | |
207 | 26.4M | SizeOffsetType unknown() { |
208 | 26.4M | return std::make_pair(APInt(), APInt()); |
209 | 26.4M | } |
210 | | |
211 | | public: |
212 | | ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, |
213 | | LLVMContext &Context, ObjectSizeOpts Options = {}); |
214 | | |
215 | | SizeOffsetType compute(Value *V); |
216 | | |
217 | 46.7M | static bool knownSize(const SizeOffsetType &SizeOffset) { |
218 | 46.7M | return SizeOffset.first.getBitWidth() > 1; |
219 | 46.7M | } |
220 | | |
221 | 20.3M | static bool knownOffset(const SizeOffsetType &SizeOffset) { |
222 | 20.3M | return SizeOffset.second.getBitWidth() > 1; |
223 | 20.3M | } |
224 | | |
225 | 46.7M | static bool bothKnown(const SizeOffsetType &SizeOffset) { |
226 | 20.3M | return knownSize(SizeOffset) && knownOffset(SizeOffset); |
227 | 46.7M | } |
228 | | |
229 | | // These are "private", except they can't actually be made private. Only |
230 | | // compute() should be used by external users. |
231 | | SizeOffsetType visitAllocaInst(AllocaInst &I); |
232 | | SizeOffsetType visitArgument(Argument &A); |
233 | | SizeOffsetType visitCallSite(CallSite CS); |
234 | | SizeOffsetType visitConstantPointerNull(ConstantPointerNull&); |
235 | | SizeOffsetType visitExtractElementInst(ExtractElementInst &I); |
236 | | SizeOffsetType visitExtractValueInst(ExtractValueInst &I); |
237 | | SizeOffsetType visitGEPOperator(GEPOperator &GEP); |
238 | | SizeOffsetType visitGlobalAlias(GlobalAlias &GA); |
239 | | SizeOffsetType visitGlobalVariable(GlobalVariable &GV); |
240 | | SizeOffsetType visitIntToPtrInst(IntToPtrInst&); |
241 | | SizeOffsetType visitLoadInst(LoadInst &I); |
242 | | SizeOffsetType visitPHINode(PHINode&); |
243 | | SizeOffsetType visitSelectInst(SelectInst &I); |
244 | | SizeOffsetType visitUndefValue(UndefValue&); |
245 | | SizeOffsetType visitInstruction(Instruction &I); |
246 | | |
247 | | private: |
248 | | bool CheckedZextOrTrunc(APInt &I); |
249 | | }; |
250 | | |
251 | | using SizeOffsetEvalType = std::pair<Value *, Value *>; |
252 | | |
253 | | /// \brief Evaluate the size and offset of an object pointed to by a Value*. |
254 | | /// May create code to compute the result at run-time. |
255 | | class ObjectSizeOffsetEvaluator |
256 | | : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> { |
257 | | using BuilderTy = IRBuilder<TargetFolder>; |
258 | | using WeakEvalType = std::pair<WeakTrackingVH, WeakTrackingVH>; |
259 | | using CacheMapTy = DenseMap<const Value *, WeakEvalType>; |
260 | | using PtrSetTy = SmallPtrSet<const Value *, 8>; |
261 | | |
262 | | const DataLayout &DL; |
263 | | const TargetLibraryInfo *TLI; |
264 | | LLVMContext &Context; |
265 | | BuilderTy Builder; |
266 | | IntegerType *IntTy; |
267 | | Value *Zero; |
268 | | CacheMapTy CacheMap; |
269 | | PtrSetTy SeenVals; |
270 | | bool RoundToAlign; |
271 | | |
272 | 12 | SizeOffsetEvalType unknown() { |
273 | 12 | return std::make_pair(nullptr, nullptr); |
274 | 12 | } |
275 | | |
276 | | SizeOffsetEvalType compute_(Value *V); |
277 | | |
278 | | public: |
279 | | ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, |
280 | | LLVMContext &Context, bool RoundToAlign = false); |
281 | | |
282 | | SizeOffsetEvalType compute(Value *V); |
283 | | |
284 | 128 | bool knownSize(SizeOffsetEvalType SizeOffset) { |
285 | 128 | return SizeOffset.first; |
286 | 128 | } |
287 | | |
288 | 101 | bool knownOffset(SizeOffsetEvalType SizeOffset) { |
289 | 101 | return SizeOffset.second; |
290 | 101 | } |
291 | | |
292 | 13 | bool anyKnown(SizeOffsetEvalType SizeOffset) { |
293 | 11 | return knownSize(SizeOffset) || knownOffset(SizeOffset); |
294 | 13 | } |
295 | | |
296 | 115 | bool bothKnown(SizeOffsetEvalType SizeOffset) { |
297 | 90 | return knownSize(SizeOffset) && knownOffset(SizeOffset); |
298 | 115 | } |
299 | | |
300 | | // The individual instruction visitors should be treated as private. |
301 | | SizeOffsetEvalType visitAllocaInst(AllocaInst &I); |
302 | | SizeOffsetEvalType visitCallSite(CallSite CS); |
303 | | SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I); |
304 | | SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I); |
305 | | SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP); |
306 | | SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&); |
307 | | SizeOffsetEvalType visitLoadInst(LoadInst &I); |
308 | | SizeOffsetEvalType visitPHINode(PHINode &PHI); |
309 | | SizeOffsetEvalType visitSelectInst(SelectInst &I); |
310 | | SizeOffsetEvalType visitInstruction(Instruction &I); |
311 | | }; |
312 | | |
313 | | } // end namespace llvm |
314 | | |
315 | | #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H |