]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps --*- 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 file defines the MemoryDependenceAnalysis analysis pass. | |
11 | // | |
12 | //===----------------------------------------------------------------------===// | |
13 | ||
970d7e83 LB |
14 | #ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H |
15 | #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H | |
223e47cc | 16 | |
223e47cc | 17 | #include "llvm/ADT/DenseMap.h" |
223e47cc | 18 | #include "llvm/ADT/PointerIntPair.h" |
970d7e83 LB |
19 | #include "llvm/ADT/SmallPtrSet.h" |
20 | #include "llvm/Analysis/AliasAnalysis.h" | |
21 | #include "llvm/IR/BasicBlock.h" | |
1a4d82fc | 22 | #include "llvm/IR/ValueHandle.h" |
970d7e83 | 23 | #include "llvm/Pass.h" |
223e47cc LB |
24 | |
25 | namespace llvm { | |
26 | class Function; | |
27 | class FunctionPass; | |
28 | class Instruction; | |
29 | class CallSite; | |
30 | class AliasAnalysis; | |
85aaf69f | 31 | class AssumptionCache; |
970d7e83 | 32 | class DataLayout; |
223e47cc LB |
33 | class MemoryDependenceAnalysis; |
34 | class PredIteratorCache; | |
35 | class DominatorTree; | |
36 | class PHITransAddr; | |
970d7e83 | 37 | |
223e47cc LB |
38 | /// MemDepResult - A memory dependence query can return one of three different |
39 | /// answers, described below. | |
40 | class MemDepResult { | |
41 | enum DepType { | |
42 | /// Invalid - Clients of MemDep never see this. | |
43 | Invalid = 0, | |
970d7e83 | 44 | |
223e47cc LB |
45 | /// Clobber - This is a dependence on the specified instruction which |
46 | /// clobbers the desired value. The pointer member of the MemDepResult | |
47 | /// pair holds the instruction that clobbers the memory. For example, | |
48 | /// this occurs when we see a may-aliased store to the memory location we | |
49 | /// care about. | |
50 | /// | |
51 | /// There are several cases that may be interesting here: | |
52 | /// 1. Loads are clobbered by may-alias stores. | |
53 | /// 2. Loads are considered clobbered by partially-aliased loads. The | |
54 | /// client may choose to analyze deeper into these cases. | |
55 | Clobber, | |
56 | ||
57 | /// Def - This is a dependence on the specified instruction which | |
58 | /// defines/produces the desired memory location. The pointer member of | |
59 | /// the MemDepResult pair holds the instruction that defines the memory. | |
60 | /// Cases of interest: | |
61 | /// 1. This could be a load or store for dependence queries on | |
62 | /// load/store. The value loaded or stored is the produced value. | |
63 | /// Note that the pointer operand may be different than that of the | |
64 | /// queried pointer due to must aliases and phi translation. Note | |
65 | /// that the def may not be the same type as the query, the pointers | |
66 | /// may just be must aliases. | |
67 | /// 2. For loads and stores, this could be an allocation instruction. In | |
68 | /// this case, the load is loading an undef value or a store is the | |
69 | /// first store to (that part of) the allocation. | |
70 | /// 3. Dependence queries on calls return Def only when they are | |
71 | /// readonly calls or memory use intrinsics with identical callees | |
72 | /// and no intervening clobbers. No validation is done that the | |
73 | /// operands to the calls are the same. | |
74 | Def, | |
970d7e83 | 75 | |
223e47cc LB |
76 | /// Other - This marker indicates that the query has no known dependency |
77 | /// in the specified block. More detailed state info is encoded in the | |
78 | /// upper part of the pair (i.e. the Instruction*) | |
79 | Other | |
80 | }; | |
81 | /// If DepType is "Other", the upper part of the pair | |
82 | /// (i.e. the Instruction* part) is instead used to encode more detailed | |
83 | /// type information as follows | |
84 | enum OtherType { | |
85 | /// NonLocal - This marker indicates that the query has no dependency in | |
86 | /// the specified block. To find out more, the client should query other | |
87 | /// predecessor blocks. | |
88 | NonLocal = 0x4, | |
89 | /// NonFuncLocal - This marker indicates that the query has no | |
90 | /// dependency in the specified function. | |
91 | NonFuncLocal = 0x8, | |
92 | /// Unknown - This marker indicates that the query dependency | |
93 | /// is unknown. | |
94 | Unknown = 0xc | |
95 | }; | |
96 | ||
97 | typedef PointerIntPair<Instruction*, 2, DepType> PairTy; | |
98 | PairTy Value; | |
99 | explicit MemDepResult(PairTy V) : Value(V) {} | |
100 | public: | |
1a4d82fc | 101 | MemDepResult() : Value(nullptr, Invalid) {} |
970d7e83 | 102 | |
223e47cc LB |
103 | /// get methods: These are static ctor methods for creating various |
104 | /// MemDepResult kinds. | |
105 | static MemDepResult getDef(Instruction *Inst) { | |
106 | assert(Inst && "Def requires inst"); | |
107 | return MemDepResult(PairTy(Inst, Def)); | |
108 | } | |
109 | static MemDepResult getClobber(Instruction *Inst) { | |
110 | assert(Inst && "Clobber requires inst"); | |
111 | return MemDepResult(PairTy(Inst, Clobber)); | |
112 | } | |
113 | static MemDepResult getNonLocal() { | |
114 | return MemDepResult( | |
115 | PairTy(reinterpret_cast<Instruction*>(NonLocal), Other)); | |
116 | } | |
117 | static MemDepResult getNonFuncLocal() { | |
118 | return MemDepResult( | |
119 | PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other)); | |
120 | } | |
121 | static MemDepResult getUnknown() { | |
122 | return MemDepResult( | |
123 | PairTy(reinterpret_cast<Instruction*>(Unknown), Other)); | |
124 | } | |
125 | ||
126 | /// isClobber - Return true if this MemDepResult represents a query that is | |
127 | /// an instruction clobber dependency. | |
128 | bool isClobber() const { return Value.getInt() == Clobber; } | |
129 | ||
130 | /// isDef - Return true if this MemDepResult represents a query that is | |
131 | /// an instruction definition dependency. | |
132 | bool isDef() const { return Value.getInt() == Def; } | |
970d7e83 | 133 | |
223e47cc LB |
134 | /// isNonLocal - Return true if this MemDepResult represents a query that |
135 | /// is transparent to the start of the block, but where a non-local hasn't | |
136 | /// been done. | |
137 | bool isNonLocal() const { | |
138 | return Value.getInt() == Other | |
139 | && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal); | |
140 | } | |
141 | ||
142 | /// isNonFuncLocal - Return true if this MemDepResult represents a query | |
143 | /// that is transparent to the start of the function. | |
144 | bool isNonFuncLocal() const { | |
145 | return Value.getInt() == Other | |
146 | && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal); | |
147 | } | |
970d7e83 | 148 | |
223e47cc LB |
149 | /// isUnknown - Return true if this MemDepResult represents a query which |
150 | /// cannot and/or will not be computed. | |
151 | bool isUnknown() const { | |
152 | return Value.getInt() == Other | |
153 | && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown); | |
154 | } | |
155 | ||
156 | /// getInst() - If this is a normal dependency, return the instruction that | |
157 | /// is depended on. Otherwise, return null. | |
158 | Instruction *getInst() const { | |
1a4d82fc | 159 | if (Value.getInt() == Other) return nullptr; |
223e47cc LB |
160 | return Value.getPointer(); |
161 | } | |
970d7e83 | 162 | |
223e47cc LB |
163 | bool operator==(const MemDepResult &M) const { return Value == M.Value; } |
164 | bool operator!=(const MemDepResult &M) const { return Value != M.Value; } | |
165 | bool operator<(const MemDepResult &M) const { return Value < M.Value; } | |
166 | bool operator>(const MemDepResult &M) const { return Value > M.Value; } | |
167 | private: | |
168 | friend class MemoryDependenceAnalysis; | |
169 | /// Dirty - Entries with this marker occur in a LocalDeps map or | |
170 | /// NonLocalDeps map when the instruction they previously referenced was | |
171 | /// removed from MemDep. In either case, the entry may include an | |
172 | /// instruction pointer. If so, the pointer is an instruction in the | |
173 | /// block where scanning can start from, saving some work. | |
174 | /// | |
175 | /// In a default-constructed MemDepResult object, the type will be Dirty | |
176 | /// and the instruction pointer will be null. | |
177 | /// | |
970d7e83 | 178 | |
223e47cc LB |
179 | /// isDirty - Return true if this is a MemDepResult in its dirty/invalid. |
180 | /// state. | |
181 | bool isDirty() const { return Value.getInt() == Invalid; } | |
970d7e83 | 182 | |
223e47cc LB |
183 | static MemDepResult getDirty(Instruction *Inst) { |
184 | return MemDepResult(PairTy(Inst, Invalid)); | |
185 | } | |
186 | }; | |
187 | ||
188 | /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For | |
189 | /// each BasicBlock (the BB entry) it keeps a MemDepResult. | |
190 | class NonLocalDepEntry { | |
191 | BasicBlock *BB; | |
192 | MemDepResult Result; | |
193 | public: | |
194 | NonLocalDepEntry(BasicBlock *bb, MemDepResult result) | |
195 | : BB(bb), Result(result) {} | |
196 | ||
197 | // This is used for searches. | |
198 | NonLocalDepEntry(BasicBlock *bb) : BB(bb) {} | |
199 | ||
200 | // BB is the sort key, it can't be changed. | |
201 | BasicBlock *getBB() const { return BB; } | |
970d7e83 | 202 | |
223e47cc LB |
203 | void setResult(const MemDepResult &R) { Result = R; } |
204 | ||
205 | const MemDepResult &getResult() const { return Result; } | |
970d7e83 | 206 | |
223e47cc LB |
207 | bool operator<(const NonLocalDepEntry &RHS) const { |
208 | return BB < RHS.BB; | |
209 | } | |
210 | }; | |
970d7e83 | 211 | |
223e47cc LB |
212 | /// NonLocalDepResult - This is a result from a NonLocal dependence query. |
213 | /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the | |
214 | /// (potentially phi translated) address that was live in the block. | |
215 | class NonLocalDepResult { | |
216 | NonLocalDepEntry Entry; | |
217 | Value *Address; | |
218 | public: | |
219 | NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address) | |
220 | : Entry(bb, result), Address(address) {} | |
970d7e83 | 221 | |
223e47cc LB |
222 | // BB is the sort key, it can't be changed. |
223 | BasicBlock *getBB() const { return Entry.getBB(); } | |
970d7e83 | 224 | |
223e47cc LB |
225 | void setResult(const MemDepResult &R, Value *Addr) { |
226 | Entry.setResult(R); | |
227 | Address = Addr; | |
228 | } | |
970d7e83 | 229 | |
223e47cc | 230 | const MemDepResult &getResult() const { return Entry.getResult(); } |
970d7e83 | 231 | |
223e47cc LB |
232 | /// getAddress - Return the address of this pointer in this block. This can |
233 | /// be different than the address queried for the non-local result because | |
234 | /// of phi translation. This returns null if the address was not available | |
235 | /// in a block (i.e. because phi translation failed) or if this is a cached | |
236 | /// result and that address was deleted. | |
237 | /// | |
238 | /// The address is always null for a non-local 'call' dependence. | |
239 | Value *getAddress() const { return Address; } | |
240 | }; | |
970d7e83 | 241 | |
223e47cc LB |
242 | /// MemoryDependenceAnalysis - This is an analysis that determines, for a |
243 | /// given memory operation, what preceding memory operations it depends on. | |
244 | /// It builds on alias analysis information, and tries to provide a lazy, | |
245 | /// caching interface to a common kind of alias information query. | |
246 | /// | |
247 | /// The dependency information returned is somewhat unusual, but is pragmatic. | |
248 | /// If queried about a store or call that might modify memory, the analysis | |
249 | /// will return the instruction[s] that may either load from that memory or | |
250 | /// store to it. If queried with a load or call that can never modify memory, | |
251 | /// the analysis will return calls and stores that might modify the pointer, | |
252 | /// but generally does not return loads unless a) they are volatile, or | |
253 | /// b) they load from *must-aliased* pointers. Returning a dependence on | |
254 | /// must-alias'd pointers instead of all pointers interacts well with the | |
255 | /// internal caching mechanism. | |
256 | /// | |
257 | class MemoryDependenceAnalysis : public FunctionPass { | |
258 | // A map from instructions to their dependency. | |
259 | typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType; | |
260 | LocalDepMapType LocalDeps; | |
261 | ||
262 | public: | |
263 | typedef std::vector<NonLocalDepEntry> NonLocalDepInfo; | |
264 | private: | |
265 | /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if | |
266 | /// the dependence is a read only dependence, false if read/write. | |
267 | typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair; | |
268 | ||
269 | /// BBSkipFirstBlockPair - This pair is used when caching information for a | |
270 | /// block. If the pointer is null, the cache value is not a full query that | |
271 | /// starts at the specified block. If non-null, the bool indicates whether | |
272 | /// or not the contents of the block was skipped. | |
273 | typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair; | |
274 | ||
275 | /// NonLocalPointerInfo - This record is the information kept for each | |
276 | /// (value, is load) pair. | |
277 | struct NonLocalPointerInfo { | |
278 | /// Pair - The pair of the block and the skip-first-block flag. | |
279 | BBSkipFirstBlockPair Pair; | |
280 | /// NonLocalDeps - The results of the query for each relevant block. | |
281 | NonLocalDepInfo NonLocalDeps; | |
282 | /// Size - The maximum size of the dereferences of the | |
283 | /// pointer. May be UnknownSize if the sizes are unknown. | |
284 | uint64_t Size; | |
1a4d82fc JJ |
285 | /// AATags - The AA tags associated with dereferences of the |
286 | /// pointer. The members may be null if there are no tags or | |
287 | /// conflicting tags. | |
288 | AAMDNodes AATags; | |
223e47cc | 289 | |
1a4d82fc | 290 | NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize) {} |
223e47cc LB |
291 | }; |
292 | ||
293 | /// CachedNonLocalPointerInfo - This map stores the cached results of doing | |
294 | /// a pointer lookup at the bottom of a block. The key of this map is the | |
295 | /// pointer+isload bit, the value is a list of <bb->result> mappings. | |
296 | typedef DenseMap<ValueIsLoadPair, | |
297 | NonLocalPointerInfo> CachedNonLocalPointerInfo; | |
298 | CachedNonLocalPointerInfo NonLocalPointerDeps; | |
299 | ||
300 | // A map from instructions to their non-local pointer dependencies. | |
970d7e83 | 301 | typedef DenseMap<Instruction*, |
223e47cc LB |
302 | SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy; |
303 | ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps; | |
304 | ||
970d7e83 | 305 | |
223e47cc LB |
306 | /// PerInstNLInfo - This is the instruction we keep for each cached access |
307 | /// that we have for an instruction. The pointer is an owning pointer and | |
308 | /// the bool indicates whether we have any dirty bits in the set. | |
309 | typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo; | |
970d7e83 | 310 | |
223e47cc LB |
311 | // A map from instructions to their non-local dependencies. |
312 | typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType; | |
970d7e83 | 313 | |
223e47cc | 314 | NonLocalDepMapType NonLocalDeps; |
970d7e83 | 315 | |
223e47cc LB |
316 | // A reverse mapping from dependencies to the dependees. This is |
317 | // used when removing instructions to keep the cache coherent. | |
318 | typedef DenseMap<Instruction*, | |
319 | SmallPtrSet<Instruction*, 4> > ReverseDepMapType; | |
320 | ReverseDepMapType ReverseLocalDeps; | |
970d7e83 | 321 | |
223e47cc LB |
322 | // A reverse mapping from dependencies to the non-local dependees. |
323 | ReverseDepMapType ReverseNonLocalDeps; | |
970d7e83 | 324 | |
223e47cc LB |
325 | /// Current AA implementation, just a cache. |
326 | AliasAnalysis *AA; | |
1a4d82fc | 327 | const DataLayout *DL; |
223e47cc | 328 | DominatorTree *DT; |
85aaf69f | 329 | AssumptionCache *AC; |
1a4d82fc JJ |
330 | std::unique_ptr<PredIteratorCache> PredCache; |
331 | ||
223e47cc LB |
332 | public: |
333 | MemoryDependenceAnalysis(); | |
334 | ~MemoryDependenceAnalysis(); | |
335 | static char ID; | |
336 | ||
337 | /// Pass Implementation stuff. This doesn't do any analysis eagerly. | |
1a4d82fc | 338 | bool runOnFunction(Function &) override; |
970d7e83 | 339 | |
223e47cc | 340 | /// Clean up memory in between runs |
1a4d82fc | 341 | void releaseMemory() override; |
970d7e83 | 342 | |
223e47cc LB |
343 | /// getAnalysisUsage - Does not modify anything. It uses Value Numbering |
344 | /// and Alias Analysis. | |
345 | /// | |
1a4d82fc | 346 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
970d7e83 | 347 | |
223e47cc LB |
348 | /// getDependency - Return the instruction on which a memory operation |
349 | /// depends. See the class comment for more details. It is illegal to call | |
350 | /// this on non-memory instructions. | |
351 | MemDepResult getDependency(Instruction *QueryInst); | |
352 | ||
353 | /// getNonLocalCallDependency - Perform a full dependency query for the | |
354 | /// specified call, returning the set of blocks that the value is | |
355 | /// potentially live across. The returned set of results will include a | |
356 | /// "NonLocal" result for all blocks where the value is live across. | |
357 | /// | |
358 | /// This method assumes the instruction returns a "NonLocal" dependency | |
359 | /// within its own block. | |
360 | /// | |
361 | /// This returns a reference to an internal data structure that may be | |
362 | /// invalidated on the next non-local query or when an instruction is | |
363 | /// removed. Clients must copy this data if they want it around longer than | |
364 | /// that. | |
365 | const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS); | |
970d7e83 LB |
366 | |
367 | ||
223e47cc | 368 | /// getNonLocalPointerDependency - Perform a full dependency query for an |
85aaf69f SL |
369 | /// access to the QueryInst's specified memory location, returning the set |
370 | /// of instructions that either define or clobber the value. | |
223e47cc | 371 | /// |
85aaf69f SL |
372 | /// Warning: For a volatile query instruction, the dependencies will be |
373 | /// accurate, and thus usable for reordering, but it is never legal to | |
374 | /// remove the query instruction. | |
375 | /// | |
376 | /// This method assumes the pointer has a "NonLocal" dependency within | |
377 | /// QueryInst's parent basic block. | |
378 | void getNonLocalPointerDependency(Instruction *QueryInst, | |
223e47cc LB |
379 | SmallVectorImpl<NonLocalDepResult> &Result); |
380 | ||
381 | /// removeInstruction - Remove an instruction from the dependence analysis, | |
382 | /// updating the dependence of instructions that previously depended on it. | |
383 | void removeInstruction(Instruction *InstToRemove); | |
970d7e83 | 384 | |
223e47cc LB |
385 | /// invalidateCachedPointerInfo - This method is used to invalidate cached |
386 | /// information about the specified pointer, because it may be too | |
387 | /// conservative in memdep. This is an optional call that can be used when | |
388 | /// the client detects an equivalence between the pointer and some other | |
389 | /// value and replaces the other value with ptr. This can make Ptr available | |
390 | /// in more places that cached info does not necessarily keep. | |
391 | void invalidateCachedPointerInfo(Value *Ptr); | |
392 | ||
393 | /// invalidateCachedPredecessors - Clear the PredIteratorCache info. | |
394 | /// This needs to be done when the CFG changes, e.g., due to splitting | |
395 | /// critical edges. | |
396 | void invalidateCachedPredecessors(); | |
970d7e83 | 397 | |
223e47cc LB |
398 | /// getPointerDependencyFrom - Return the instruction on which a memory |
399 | /// location depends. If isLoad is true, this routine ignores may-aliases | |
400 | /// with read-only operations. If isLoad is false, this routine ignores | |
970d7e83 LB |
401 | /// may-aliases with reads from read-only locations. If possible, pass |
402 | /// the query instruction as well; this function may take advantage of | |
403 | /// the metadata annotated to the query instruction to refine the result. | |
223e47cc LB |
404 | /// |
405 | /// Note that this is an uncached query, and thus may be inefficient. | |
406 | /// | |
407 | MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc, | |
970d7e83 | 408 | bool isLoad, |
223e47cc | 409 | BasicBlock::iterator ScanIt, |
970d7e83 | 410 | BasicBlock *BB, |
1a4d82fc | 411 | Instruction *QueryInst = nullptr); |
970d7e83 LB |
412 | |
413 | ||
223e47cc LB |
414 | /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that |
415 | /// looks at a memory location for a load (specified by MemLocBase, Offs, | |
416 | /// and Size) and compares it against a load. If the specified load could | |
417 | /// be safely widened to a larger integer load that is 1) still efficient, | |
418 | /// 2) safe for the target, and 3) would provide the specified memory | |
419 | /// location value, then this function returns the size in bytes of the | |
420 | /// load width to use. If not, this returns zero. | |
421 | static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase, | |
422 | int64_t MemLocOffs, | |
423 | unsigned MemLocSize, | |
424 | const LoadInst *LI, | |
1a4d82fc | 425 | const DataLayout &DL); |
970d7e83 | 426 | |
223e47cc LB |
427 | private: |
428 | MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall, | |
429 | BasicBlock::iterator ScanIt, | |
430 | BasicBlock *BB); | |
431 | bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer, | |
432 | const AliasAnalysis::Location &Loc, | |
433 | bool isLoad, BasicBlock *BB, | |
434 | SmallVectorImpl<NonLocalDepResult> &Result, | |
435 | DenseMap<BasicBlock*, Value*> &Visited, | |
436 | bool SkipFirstBlock = false); | |
437 | MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc, | |
438 | bool isLoad, BasicBlock *BB, | |
439 | NonLocalDepInfo *Cache, | |
440 | unsigned NumSortedEntries); | |
441 | ||
442 | void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P); | |
970d7e83 | 443 | |
223e47cc LB |
444 | /// verifyRemoved - Verify that the specified instruction does not occur |
445 | /// in our internal data structures. | |
446 | void verifyRemoved(Instruction *Inst) const; | |
970d7e83 | 447 | |
223e47cc LB |
448 | }; |
449 | ||
450 | } // End llvm namespace | |
451 | ||
452 | #endif |