LLVM 24.0.0git
DependenceAnalysis.h
Go to the documentation of this file.
1//===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DependenceAnalysis is an LLVM pass that analyses dependences between memory
10// accesses. Currently, it is an implementation of the approach described in
11//
12// Practical Dependence Testing
13// Goff, Kennedy, Tseng
14// PLDI 1991
15//
16// There's a single entry point that analyzes the dependence between a pair
17// of memory references in a function, returning either NULL, for no dependence,
18// or a more-or-less detailed description of the dependence between them.
19//
20// This pass exists to support the DependenceGraph pass. There are two separate
21// passes because there's a useful separation of concerns. A dependence exists
22// if two conditions are met:
23//
24// 1) Two instructions reference the same memory location, and
25// 2) There is a flow of control leading from one instruction to the other.
26//
27// DependenceAnalysis attacks the first condition; DependenceGraph will attack
28// the second (it's not yet ready).
29//
30// Please note that this is work in progress and the interface is subject to
31// change.
32//
33// Plausible changes:
34// Return a set of more precise dependences instead of just one dependence
35// summarizing all.
36//
37//===----------------------------------------------------------------------===//
38
39#ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
40#define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
41
46#include "llvm/IR/PassManager.h"
47#include "llvm/Pass.h"
49
50namespace llvm {
51class AAResults;
52struct AnalysisOptions;
53template <typename T> class ArrayRef;
54class Loop;
55class LoopInfo;
56class MDNode;
57class raw_ostream;
58
59/// Dependence - This class represents a dependence between two memory
60/// memory references in a function. It contains minimal information and
61/// is used in the very common situation where the compiler is unable to
62/// determine anything beyond the existence of a dependence; that is, it
63/// represents a confused dependence (see also FullDependence). In most
64/// cases (for output, flow, and anti dependences), the dependence implies
65/// an ordering, where the source must precede the destination; in contrast,
66/// input dependences are unordered.
67///
68/// When a dependence graph is built, each Dependence will be a member of
69/// the set of predecessor edges for its destination instruction and a set
70/// if successor edges for its source instruction. These sets are represented
71/// as singly-linked lists, with the "next" fields stored in the dependence
72/// itelf.
74protected:
75 Dependence(Dependence &&) = default;
77
78public:
79 Dependence(Instruction *Source, Instruction *Destination,
80 const SCEVUnionPredicate &A)
81 : Src(Source), Dst(Destination), Assumptions(A) {}
82 virtual ~Dependence() = default;
83
84 /// Dependence::DVEntry - Each level in the distance/direction vector
85 /// has a direction (or perhaps a union of several directions), and
86 /// perhaps a distance.
87 /// The dependency information could be across a single loop level or across
88 /// two separate levels that have the same trip count and nesting depth,
89 /// which helps to provide information for loop fusion candidation.
90 /// For example, loops b and c have the same iteration count and depth:
91 /// for (a = ...) {
92 /// for (b = 0; b < 10; b++) {
93 /// }
94 /// for (c = 0; c < 10; c++) {
95 /// }
96 /// }
97 struct DVEntry {
98 enum : unsigned char {
99 NONE = 0,
100 LT = 1,
101 EQ = 2,
102 LE = 3,
103 GT = 4,
104 NE = 5,
105 GE = 6,
106 ALL = 7
107 };
108 unsigned char Direction : 3; // Init to ALL, then refine.
109 bool Scalar : 1; // Init to true.
110 const SCEV *Distance = nullptr; // NULL implies no distance available.
112 };
113
114 /// getSrc - Returns the source instruction for this dependence.
115 Instruction *getSrc() const { return Src; }
116
117 /// getDst - Returns the destination instruction for this dependence.
118 Instruction *getDst() const { return Dst; }
119
120 /// isInput - Returns true if this is an input dependence.
121 bool isInput() const;
122
123 /// isOutput - Returns true if this is an output dependence.
124 bool isOutput() const;
125
126 /// isFlow - Returns true if this is a flow (aka true) dependence.
127 bool isFlow() const;
128
129 /// isAnti - Returns true if this is an anti dependence.
130 bool isAnti() const;
131
132 /// isOrdered - Returns true if dependence is Output, Flow, or Anti
133 bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
134
135 /// isUnordered - Returns true if dependence is Input
136 bool isUnordered() const { return isInput(); }
137
138 /// isLoopIndependent - Returns true if this is a loop-independent
139 /// dependence.
140 virtual bool isLoopIndependent() const { return true; }
141
142 /// isConfused - Returns true if this dependence is confused
143 /// (the compiler understands nothing and makes worst-case assumptions).
144 virtual bool isConfused() const { return true; }
145
146 /// getLevels - Returns the number of common loops surrounding the
147 /// source and destination of the dependence.
148 virtual unsigned getLevels() const { return 0; }
149
150 /// getSameSDLevels - Returns the number of separate SameSD loops surrounding
151 /// the source and destination of the dependence.
152 virtual unsigned getSameSDLevels() const { return 0; }
153
154 /// getDirection - Returns the direction associated with a particular
155 /// common or SameSD level.
156 virtual unsigned getDirection(unsigned Level, bool SameSD = false) const {
157 return DVEntry::ALL;
158 }
159
160 /// getDistance - Returns the distance (or NULL) associated with a
161 /// particular common or SameSD level.
162 virtual const SCEV *getDistance(unsigned Level, bool SameSD = false) const {
163 return nullptr;
164 }
165
166 /// Check if the direction vector is negative. A negative direction
167 /// vector means Src and Dst are reversed in the actual program.
168 virtual bool isDirectionNegative() const { return false; }
169
170 /// Negate the dependence by swapping the source and destination, and
171 /// reversing the direction and distance information.
172 virtual void negate(ScalarEvolution &SE) {}
173
174 /// If the direction vector is negative, normalize the direction
175 /// vector to make it non-negative. Normalization is done by reversing
176 /// Src and Dst, plus reversing the dependence directions and distances
177 /// in the vector.
178 virtual bool normalize(ScalarEvolution *SE) { return false; }
179
180 /// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
181 /// performed across two separate loop nests that have the Same Iteration and
182 /// Depth.
183 virtual bool inSameSDLoops(unsigned Level) const { return false; }
184
185 /// isScalar - Returns true if a particular regular or SameSD level is
186 /// scalar; that is, if no subscript in the source or destination mention
187 /// the induction variable associated with the loop at this level.
188 virtual bool isScalar(unsigned Level, bool SameSD = false) const;
189
190 /// getNextPredecessor - Returns the value of the NextPredecessor field.
191 const Dependence *getNextPredecessor() const { return NextPredecessor; }
192
193 /// getNextSuccessor - Returns the value of the NextSuccessor field.
194 const Dependence *getNextSuccessor() const { return NextSuccessor; }
195
196 /// setNextPredecessor - Sets the value of the NextPredecessor
197 /// field.
198 void setNextPredecessor(const Dependence *pred) { NextPredecessor = pred; }
199
200 /// setNextSuccessor - Sets the value of the NextSuccessor field.
201 void setNextSuccessor(const Dependence *succ) { NextSuccessor = succ; }
202
203 /// getRuntimeAssumptions - Returns the runtime assumptions under which this
204 /// Dependence relation is valid.
205 SCEVUnionPredicate getRuntimeAssumptions() const { return Assumptions; }
206
207 /// dump - For debugging purposes, dumps a dependence to OS.
208 void dump(raw_ostream &OS) const;
209
210 /// dumpImp - For debugging purposes. Dumps a dependence to OS with or
211 /// without considering the SameSD levels.
212 void dumpImp(raw_ostream &OS, bool IsSameSD = false) const;
213
214protected:
216
217private:
218 SCEVUnionPredicate Assumptions;
219 const Dependence *NextPredecessor = nullptr, *NextSuccessor = nullptr;
220 friend class DependenceInfo;
221};
222
223/// FullDependence - This class represents a dependence between two memory
224/// references in a function. It contains detailed information about the
225/// dependence (direction vectors, etc.) and is used when the compiler is
226/// able to accurately analyze the interaction of the references; that is,
227/// it is not a confused dependence (see Dependence). In most cases
228/// (for output, flow, and anti dependences), the dependence implies an
229/// ordering, where the source must precede the destination; in contrast,
230/// input dependences are unordered.
231class LLVM_ABI FullDependence final : public Dependence {
232public:
233 FullDependence(Instruction *Source, Instruction *Destination,
234 const SCEVUnionPredicate &Assumes,
235 bool PossiblyLoopIndependent, unsigned Levels);
236
237 /// isLoopIndependent - Returns true if this is a loop-independent
238 /// dependence.
239 bool isLoopIndependent() const override { return LoopIndependent; }
240
241 /// isConfused - Returns true if this dependence is confused
242 /// (the compiler understands nothing and makes worst-case
243 /// assumptions).
244 bool isConfused() const override { return false; }
245
246 /// getLevels - Returns the number of common loops surrounding the
247 /// source and destination of the dependence.
248 unsigned getLevels() const override { return Levels; }
249
250 /// getSameSDLevels - Returns the number of separate SameSD loops surrounding
251 /// the source and destination of the dependence.
252 unsigned getSameSDLevels() const override { return SameSDLevels; }
253
254 /// getDVEntry - Returns the DV entry associated with a regular or a
255 /// SameSD level.
256 DVEntry getDVEntry(unsigned Level, bool IsSameSD) const {
257 if (!IsSameSD) {
258 assert(0 < Level && Level <= Levels && "Level out of range");
259 return DV[Level - 1];
260 } else {
261 assert(Levels < Level &&
262 Level <= static_cast<unsigned>(Levels) + SameSDLevels &&
263 "isSameSD level out of range");
264 return DVSameSD[Level - Levels - 1];
265 }
266 }
267
268 /// getDirection - Returns the direction associated with a particular
269 /// common or SameSD level.
270 unsigned getDirection(unsigned Level, bool SameSD = false) const override;
271
272 /// getDistance - Returns the distance (or NULL) associated with a
273 /// particular common or SameSD level.
274 const SCEV *getDistance(unsigned Level, bool SameSD = false) const override;
275
276 /// Check if the direction vector is negative. A negative direction
277 /// vector means Src and Dst are reversed in the actual program.
278 bool isDirectionNegative() const override;
279
280 void negate(ScalarEvolution &SE) override;
281
282 /// If the direction vector is negative, normalize the direction
283 /// vector to make it non-negative. Normalization is done by reversing
284 /// Src and Dst, plus reversing the dependence directions and distances
285 /// in the vector.
286 bool normalize(ScalarEvolution *SE) override;
287
288 /// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
289 /// performed across two separate loop nests that have the Same Iteration and
290 /// Depth.
291 bool inSameSDLoops(unsigned Level) const override;
292
293 /// isScalar - Returns true if a particular regular or SameSD level is
294 /// scalar; that is, if no subscript in the source or destination mention
295 /// the induction variable associated with the loop at this level.
296 bool isScalar(unsigned Level, bool SameSD = false) const override;
297
298private:
299 unsigned short Levels;
300 unsigned short SameSDLevels;
301 bool LoopIndependent;
302 std::unique_ptr<DVEntry[]> DV;
303 std::unique_ptr<DVEntry[]> DVSameSD; // DV entries on SameSD levels
304 friend class DependenceInfo;
305};
306
307/// DependenceInfo - This class is the main dependence-analysis driver.
309public:
311 LoopInfo *LI);
312
313 /// Handle transitive invalidation when the cached analysis results go away.
315 FunctionAnalysisManager::Invalidator &Inv);
316
317 /// depends - Tests for a dependence between the Src and Dst instructions.
318 /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
319 /// FullDependence) with as much information as can be gleaned. By default,
320 /// the dependence test collects a set of runtime assumptions that cannot be
321 /// solved at compilation time. By default UnderRuntimeAssumptions is false
322 /// for a safe approximation of the dependence relation that does not
323 /// require runtime checks.
324 LLVM_ABI std::unique_ptr<Dependence>
326 bool UnderRuntimeAssumptions = false);
327
328 Function *getFunction() const { return F; }
329
330private:
331 const AnalysisOptions &Opts;
332 AAResults *AA;
333 ScalarEvolution *SE;
334 LoopInfo *LI;
335 Function *F;
336
337 SmallPtrSet<const MDNode *, 4> LoopAliasScopes;
338 SmallPtrSet<const Loop *, 8> AlreadyCheckedLoops;
339
340 void updateLoopAliasScopes(const Loop *L);
341
342 /// Subscript - This private struct represents a pair of subscripts from
343 /// a pair of potentially multi-dimensional array references. We use a
344 /// vector of them to guide subscript partitioning.
345 struct Subscript {
346 const SCEV *Src;
347 const SCEV *Dst;
348 enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
349 SmallBitVector Loops;
350 };
351
352 struct CoefficientInfo {
353 const SCEV *SrcCoeff;
354 const SCEV *DstCoeff;
355 const SCEV *MaxIterIndex;
356 };
357
358 struct BoundInfo {
359 const SCEV *Upper[8];
360 const SCEV *Lower[8];
361 unsigned char Direction;
362 unsigned char DirSet;
363 };
364
365 /// Returns true if two loops have the Same iteration Space and Depth. To be
366 /// more specific, two loops have SameSD if they are in the same nesting
367 /// depth and have the same backedge count. SameSD stands for Same iteration
368 /// Space and Depth.
369 bool haveSameSD(const Loop *SrcLoop, const Loop *DstLoop) const;
370
371 /// establishNestingLevels - Examines the loop nesting of the Src and Dst
372 /// instructions and establishes their shared loops. Sets the variables
373 /// CommonLevels, SrcLevels, and MaxLevels.
374 /// The source and destination instructions needn't be contained in the same
375 /// loop. The routine establishNestingLevels finds the level of most deeply
376 /// nested loop that contains them both, CommonLevels. An instruction that's
377 /// not contained in a loop is at level = 0. MaxLevels is equal to the level
378 /// of the source plus the level of the destination, minus CommonLevels.
379 /// This lets us allocate vectors MaxLevels in length, with room for every
380 /// distinct loop referenced in both the source and destination subscripts.
381 /// The variable SrcLevels is the nesting depth of the source instruction.
382 /// It's used to help calculate distinct loops referenced by the destination.
383 /// Here's the map from loops to levels:
384 /// 0 - unused
385 /// 1 - outermost common loop
386 /// ... - other common loops
387 /// CommonLevels - innermost common loop
388 /// ... - loops containing Src but not Dst
389 /// SrcLevels - innermost loop containing Src but not Dst
390 /// ... - loops containing Dst but not Src
391 /// MaxLevels - innermost loop containing Dst but not Src
392 /// Consider the follow code fragment:
393 /// for (a = ...) {
394 /// for (b = ...) {
395 /// for (c = ...) {
396 /// for (d = ...) {
397 /// A[] = ...;
398 /// }
399 /// }
400 /// for (e = ...) {
401 /// for (f = ...) {
402 /// for (g = ...) {
403 /// ... = A[];
404 /// }
405 /// }
406 /// }
407 /// }
408 /// }
409 /// If we're looking at the possibility of a dependence between the store
410 /// to A (the Src) and the load from A (the Dst), we'll note that they
411 /// have 2 loops in common, so CommonLevels will equal 2 and the direction
412 /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
413 /// A map from loop names to level indices would look like
414 /// a - 1
415 /// b - 2 = CommonLevels
416 /// c - 3
417 /// d - 4 = SrcLevels
418 /// e - 5
419 /// f - 6
420 /// g - 7 = MaxLevels
421 /// SameSDLevels counts the number of levels after common levels that are
422 /// not common but have the same iteration space and depth. Internally this
423 /// is checked using haveSameSD. Assume that in this code fragment, levels c
424 /// and e have the same iteration space and depth, but levels d and f does
425 /// not. Then SameSDLevels is set to 1. In that case the level numbers for the
426 /// previous code look like
427 /// a - 1
428 /// b - 2
429 /// c,e - 3 = CommonLevels
430 /// d - 4 = SrcLevels
431 /// f - 5
432 /// g - 6 = MaxLevels
433 void establishNestingLevels(const Instruction *Src, const Instruction *Dst);
434
435 unsigned CommonLevels, SrcLevels, MaxLevels, SameSDLevels;
436
437 /// mapSrcLoop - Given one of the loops containing the source, return
438 /// its level index in our numbering scheme.
439 unsigned mapSrcLoop(const Loop *SrcLoop) const;
440
441 /// mapDstLoop - Given one of the loops containing the destination,
442 /// return its level index in our numbering scheme.
443 unsigned mapDstLoop(const Loop *DstLoop) const;
444
445 /// isLoopInvariant - Returns true if Expression is loop invariant
446 /// in LoopNest.
447 bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
448
449 /// collectCommonLoops - Finds the set of loops from the LoopNest that
450 /// have a level <= CommonLevels and are referred to by the SCEV Expression.
451 void collectCommonLoops(const SCEV *Expression, const Loop *LoopNest,
452 SmallBitVector &Loops) const;
453
454 /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
455 /// linear. Collect the set of loops mentioned by Src.
456 bool checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
457 SmallBitVector &Loops);
458
459 /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
460 /// linear. Collect the set of loops mentioned by Dst.
461 bool checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
462 SmallBitVector &Loops);
463
464 /// collectUpperBound - All subscripts are the same type (on my machine,
465 /// an i64). The loop bound may be a smaller type. collectUpperBound
466 /// find the bound, if available, and zero extends it to the Type T.
467 /// (I zero extend since the bound should always be >= 0.)
468 /// If no upper bound is available, return NULL.
469 const SCEV *collectUpperBound(const Loop *l, Type *T) const;
470
471 /// collectNonNegativeConstantUpperBound - Calls collectUpperBound(), then
472 /// attempts to cast it to APInt. If the cast fails, or the value is negative,
473 /// returns std::nullopt.
474 std::optional<APInt> collectNonNegativeConstantUpperBound(const Loop *L,
475 Type *T) const;
476
477 /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
478 /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
479 /// Collects the associated loops in a set.
480 Subscript::ClassificationKind
481 classifyPair(const SCEV *Src, const Loop *SrcLoopNest, const SCEV *Dst,
482 const Loop *DstLoopNest, SmallBitVector &Loops);
483
484 /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
485 /// Returns true if any possible dependence is disproved.
486 /// If there might be a dependence, returns false.
487 /// If the dependence isn't proven to exist,
488 bool testZIV(const SCEV *Src, const SCEV *Dst, FullDependence &Result) const;
489
490 /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
491 /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
492 /// i and j are induction variables, c1 and c2 are loop invariant,
493 /// and a1 and a2 are constant.
494 /// Returns true if any possible dependence is disproved.
495 /// If there might be a dependence, returns false.
496 /// Sets appropriate direction vector entry and, when possible,
497 /// the distance vector entry.
498 /// If the dependence isn't proven to exist,
499 bool testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
500 FullDependence &Result, bool UnderRuntimeAssumptions);
501
502 /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
503 /// Things of the form [c1 + a1*i] and [c2 + a2*j]
504 /// where i and j are induction variables, c1 and c2 are loop invariant,
505 /// and a1 and a2 are constant.
506 /// With minor algebra, this test can also be used for things like
507 /// [c1 + a1*i + a2*j][c2].
508 /// Returns true if any possible dependence is disproved.
509 /// If there might be a dependence, returns false.
510 bool testRDIV(const SCEV *Src, const SCEV *Dst, FullDependence &Result) const;
511
512 /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
513 /// Returns true if dependence disproved.
514 /// Can sometimes refine direction vectors.
515 bool testMIV(const SCEV *Src, const SCEV *Dst, const SmallBitVector &Loops,
516 FullDependence &Result) const;
517
518 /// strongSIVtest - Tests the strong SIV subscript pair (\p Src and \p Dst)
519 /// for dependence.
520 /// Things of the form [c1 + a*i] and [c2 + a*i],
521 /// where i is an induction variable, c1 and c2 are loop invariant,
522 /// and a is a constant
523 /// Returns true if any possible dependence is disproved.
524 /// If there might be a dependence, returns false.
525 /// Sets appropriate direction and distance.
526 bool strongSIVtest(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
527 unsigned Level, FullDependence &Result,
528 bool UnderRuntimeAssumptions);
529
530 /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
531 /// (Src and Dst) for dependence.
532 /// Things of the form [c1 + a*i] and [c2 - a*i],
533 /// where i is an induction variable, c1 and c2 are loop invariant,
534 /// and a is a constant.
535 /// Returns true if any possible dependence is disproved.
536 /// If there might be a dependence, returns false.
537 /// Sets appropriate direction entry.
538 bool weakCrossingSIVtest(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
539 unsigned Level, FullDependence &Result) const;
540
541 /// ExactSIVtest - Tests the SIV subscript pair
542 /// (Src and Dst) for dependence.
543 /// Things of the form [c1 + a1*i] and [c2 + a2*i],
544 /// where i is an induction variable, c1 and c2 are loop invariant,
545 /// and a1 and a2 are constant.
546 /// Returns true if any possible dependence is disproved.
547 /// If there might be a dependence, returns false.
548 /// Sets appropriate direction entry.
549 bool exactSIVtest(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
550 unsigned Level, FullDependence &Result) const;
551
552 /// weakZeroSIVtestImpl - Core implementation for weakZeroSrcSIVtest and
553 /// weakZeroDstSIVtest.
554 bool weakZeroSIVtestImpl(const SCEVAddRecExpr *AR, const SCEV *Const,
555 unsigned Level, FullDependence &Result) const;
556
557 /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
558 /// (Src and Dst) for dependence.
559 /// Things of the form [c1] and [c2 + a*i],
560 /// where i is an induction variable, c1 and c2 are loop invariant,
561 /// and a is a constant. See also weakZeroDstSIVtest.
562 /// Returns true if any possible dependence is disproved.
563 /// If there might be a dependence, returns false.
564 /// Sets appropriate direction entry.
565 bool weakZeroSrcSIVtest(const SCEV *SrcConst, const SCEVAddRecExpr *Dst,
566 unsigned Level, FullDependence &Result) const;
567
568 /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
569 /// (Src and Dst) for dependence.
570 /// Things of the form [c1 + a*i] and [c2],
571 /// where i is an induction variable, c1 and c2 are loop invariant,
572 /// and a is a constant. See also weakZeroSrcSIVtest.
573 /// Returns true if any possible dependence is disproved.
574 /// If there might be a dependence, returns false.
575 /// Sets appropriate direction entry.
576 bool weakZeroDstSIVtest(const SCEVAddRecExpr *Src, const SCEV *DstConst,
577 unsigned Level, FullDependence &Result) const;
578
579 /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
580 /// Things of the form [c1 + a*i] and [c2 + b*j],
581 /// where i and j are induction variable, c1 and c2 are loop invariant,
582 /// and a and b are constants.
583 /// Returns true if any possible dependence is disproved.
584 /// Works in some cases that symbolicRDIVtest doesn't,
585 /// and vice versa.
586 bool exactRDIVtest(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
587 FullDependence &Result) const;
588
589 /// exactTestImpl - Core implementation shared by the Exact SIV test and the
590 /// Exact RDIV test. Returns true if any possible dependence is disproved. If
591 /// \p Level is provided, this function will also attempt to explore
592 /// directions and refine \p Result for the given level.
593 bool exactTestImpl(const SCEVAddRecExpr *Src, const SCEVAddRecExpr *Dst,
594 FullDependence &Result,
595 std::optional<unsigned> Level) const;
596
597 /// gcdMIVtest - Tests an MIV subscript pair for dependence.
598 /// Returns true if any possible dependence is disproved.
599 /// Can sometimes disprove the equal direction for 1 or more loops.
600 // Can handle some symbolics that even the SIV tests don't get,
601 /// so we use it as a backup for everything.
602 bool gcdMIVtest(const SCEV *Src, const SCEV *Dst,
603 FullDependence &Result) const;
604
605 /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
606 /// Returns true if any possible dependence is disproved.
607 /// Computes directions.
608 bool banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
609 const SmallBitVector &Loops,
610 FullDependence &Result) const;
611
612 /// collectCoeffInfo - Walks through the subscript, collecting each
613 /// coefficient and the associated maximum iteration index in the
614 /// widened analysis type. Returns the widened constant term.
615 const SCEV *collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
616 Type *WideType,
618
619 /// Given \p Expr of the form
620 ///
621 /// c_0*X_0*i_0 + c_1*X_1*i_1 + ...c_n*X_n*i_n + C
622 ///
623 /// compute
624 ///
625 /// RunningGCD = gcd(RunningGCD, c_0, c_1, ..., c_n)
626 ///
627 /// where c_0, c_1, ..., and c_n are the constant values. The result is stored
628 /// in \p RunningGCD. Also, the initial value of \p RunningGCD affects the
629 /// result. If we find a term like (c_k * X_k * i_k), where i_k is the
630 /// induction variable of \p CurLoop, c_k is stored in \p CurLoopCoeff and not
631 /// included in the GCD computation. Returns nullptr if we fail to find a
632 /// constant coefficient for some loop, e.g., when a term like (X+Y)*i is
633 /// present. Otherwise returns the remaining constant term C.
634 const SCEV *accumulateCoefficientsGCD(const SCEV *Expr, const Loop *CurLoop,
635 const SCEV *&CurLoopCoeff,
636 APInt &RunningGCD) const;
637
638 /// getLowerBound - Looks through all the bounds info and
639 /// computes the lower bound given the current direction settings
640 /// at each level.
641 const SCEV *getLowerBound(ArrayRef<BoundInfo> Bound) const;
642
643 /// getUpperBound - Looks through all the bounds info and
644 /// computes the upper bound given the current direction settings
645 /// at each level.
646 const SCEV *getUpperBound(ArrayRef<BoundInfo> Bound) const;
647
648 /// exploreDirections - Hierarchically expands the direction vector
649 /// search space, combining the directions of discovered dependences
650 /// in the DirSet field of Bound. Returns the number of distinct
651 /// dependences discovered. If the dependence is disproved,
652 /// it will return 0.
653 unsigned exploreDirections(unsigned Level, MutableArrayRef<BoundInfo> Bound,
654 const SmallBitVector &Loops, const SCEV *Delta,
655 const FullDependence &Result) const;
656
657 /// testBounds - Returns true when the current bounds may be feasible
658 /// or their feasibility is unknown.
659 bool testBounds(unsigned char DirKind, unsigned Level,
660 MutableArrayRef<BoundInfo> Bound, const SCEV *Delta) const;
661
662 /// findBoundsALL - Computes the upper and lower bounds for level K
663 /// using the * direction. Records them in Bound.
664 void findBoundsALL(ArrayRef<CoefficientInfo> CI,
665 MutableArrayRef<BoundInfo> Bound, unsigned K) const;
666
667 /// findBoundsLT - Computes the upper and lower bounds for level K
668 /// using the < direction. Records them in Bound.
669 void findBoundsLT(ArrayRef<CoefficientInfo> CI,
670 MutableArrayRef<BoundInfo> Bound, unsigned K) const;
671
672 /// findBoundsGT - Computes the upper and lower bounds for level K
673 /// using the > direction. Records them in Bound.
674 void findBoundsGT(ArrayRef<CoefficientInfo> CI,
675 MutableArrayRef<BoundInfo> Bound, unsigned K) const;
676
677 /// findBoundsEQ - Computes the upper and lower bounds for level K
678 /// using the = direction. Records them in Bound.
679 void findBoundsEQ(ArrayRef<CoefficientInfo> CI,
680 MutableArrayRef<BoundInfo> Bound, unsigned K) const;
681
682 /// Given a linear access function, tries to recover subscripts
683 /// for each dimension of the array element access.
684 bool tryDelinearize(Instruction *Src, Instruction *Dst,
685 SmallVectorImpl<Subscript> &Pair);
686
687 /// Tries to delinearize \p Src and \p Dst access functions for a fixed size
688 /// multi-dimensional array. Calls delinearizeFixedSizeArray() to delinearize
689 /// \p Src and \p Dst separately,
690 bool tryDelinearizeFixedSize(Instruction *Src, Instruction *Dst,
691 const SCEV *SrcAccessFn, const SCEV *DstAccessFn,
692 SmallVectorImpl<const SCEV *> &SrcSubscripts,
693 SmallVectorImpl<const SCEV *> &DstSubscripts);
694
695 /// Tries to delinearize access function for a multi-dimensional array with
696 /// symbolic runtime sizes.
697 /// Returns true upon success and false otherwise.
698 bool
699 tryDelinearizeParametricSize(Instruction *Src, Instruction *Dst,
700 const SCEV *SrcAccessFn, const SCEV *DstAccessFn,
701 SmallVectorImpl<const SCEV *> &SrcSubscripts,
702 SmallVectorImpl<const SCEV *> &DstSubscripts);
703
704 /// checkSubscript - Helper function for checkSrcSubscript and
705 /// checkDstSubscript to avoid duplicate code
706 bool checkSubscript(const SCEV *Expr, const Loop *LoopNest,
707 SmallBitVector &Loops, bool IsSrc);
708}; // class DependenceInfo
709
710/// AnalysisPass to compute dependence information in a function
711class DependenceAnalysis : public AnalysisInfoMixin<DependenceAnalysis> {
712public:
715
716private:
719}; // class DependenceAnalysis
720
721/// Printer pass to dump DA results.
723 : public RequiredPassInfoMixin<DependenceAnalysisPrinterPass> {
724 DependenceAnalysisPrinterPass(raw_ostream &OS, bool NormalizeResults = false)
725 : OS(OS), NormalizeResults(NormalizeResults) {}
726
728 LLVM_ABI void
730 function_ref<StringRef(StringRef)> MapClassName2PassName);
731
732private:
733 raw_ostream &OS;
734 bool NormalizeResults;
735}; // class DependenceAnalysisPrinterPass
736
737} // namespace llvm
738
739#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
Hexagon Hardware Loops
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define T
static bool isInput(const ArrayRef< StringRef > &Prefixes, StringRef Arg)
Definition OptTable.cpp:143
FunctionAnalysisManager FAM
This file implements the SmallBitVector class.
This file defines the SmallPtrSet class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
AnalysisPass to compute dependence information in a function.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &FAM)
DependenceInfo - This class is the main dependence-analysis driver.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle transitive invalidation when the cached analysis results go away.
Function * getFunction() const
LLVM_ABI DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE, LoopInfo *LI)
LLVM_ABI std::unique_ptr< Dependence > depends(Instruction *Src, Instruction *Dst, bool UnderRuntimeAssumptions=false)
depends - Tests for a dependence between the Src and Dst instructions.
Instruction * getDst() const
getDst - Returns the destination instruction for this dependence.
Dependence & operator=(Dependence &&)=default
bool isOrdered() const
isOrdered - Returns true if dependence is Output, Flow, or Anti
void setNextSuccessor(const Dependence *succ)
setNextSuccessor - Sets the value of the NextSuccessor field.
friend class DependenceInfo
Dependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &A)
Dependence(Dependence &&)=default
bool isUnordered() const
isUnordered - Returns true if dependence is Input
SCEVUnionPredicate getRuntimeAssumptions() const
getRuntimeAssumptions - Returns the runtime assumptions under which this Dependence relation is valid...
virtual bool isConfused() const
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
virtual unsigned getSameSDLevels() const
getSameSDLevels - Returns the number of separate SameSD loops surrounding the source and destination ...
virtual const SCEV * getDistance(unsigned Level, bool SameSD=false) const
getDistance - Returns the distance (or NULL) associated with a particular common or SameSD level.
virtual void negate(ScalarEvolution &SE)
Negate the dependence by swapping the source and destination, and reversing the direction and distanc...
virtual unsigned getLevels() const
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
const Dependence * getNextPredecessor() const
getNextPredecessor - Returns the value of the NextPredecessor field.
virtual unsigned getDirection(unsigned Level, bool SameSD=false) const
getDirection - Returns the direction associated with a particular common or SameSD level.
bool isFlow() const
isFlow - Returns true if this is a flow (aka true) dependence.
virtual ~Dependence()=default
virtual bool normalize(ScalarEvolution *SE)
If the direction vector is negative, normalize the direction vector to make it non-negative.
bool isAnti() const
isAnti - Returns true if this is an anti dependence.
const Dependence * getNextSuccessor() const
getNextSuccessor - Returns the value of the NextSuccessor field.
virtual bool isDirectionNegative() const
Check if the direction vector is negative.
Instruction * getSrc() const
getSrc - Returns the source instruction for this dependence.
virtual bool isLoopIndependent() const
isLoopIndependent - Returns true if this is a loop-independent dependence.
bool isOutput() const
isOutput - Returns true if this is an output dependence.
void setNextPredecessor(const Dependence *pred)
setNextPredecessor - Sets the value of the NextPredecessor field.
virtual bool inSameSDLoops(unsigned Level) const
inSameSDLoops - Returns true if this level is an SameSD level, i.e., performed across two separate lo...
FullDependence(Instruction *Source, Instruction *Destination, const SCEVUnionPredicate &Assumes, bool PossiblyLoopIndependent, unsigned Levels)
bool isConfused() const override
isConfused - Returns true if this dependence is confused (the compiler understands nothing and makes ...
DVEntry getDVEntry(unsigned Level, bool IsSameSD) const
getDVEntry - Returns the DV entry associated with a regular or a SameSD level.
bool isLoopIndependent() const override
isLoopIndependent - Returns true if this is a loop-independent dependence.
unsigned getSameSDLevels() const override
getSameSDLevels - Returns the number of separate SameSD loops surrounding the source and destination ...
unsigned getLevels() const override
getLevels - Returns the number of common loops surrounding the source and destination of the dependen...
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1087
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This class represents an analyzed expression in the program.
The main scalar evolution driver.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Abstract Attribute helper functions.
Definition Attributor.h:166
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
ArrayRef(const T &OneElt) -> ArrayRef< T >
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
DependenceAnalysisPrinterPass(raw_ostream &OS, bool NormalizeResults=false)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Dependence::DVEntry - Each level in the distance/direction vector has a direction (or perhaps a union...
A CRTP mix-in for passes that should not be skipped.