1 //===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // DependenceAnalysis is an LLVM pass that analyses dependences between memory
11 // accesses. Currently, it is an implementation of the approach described in
13 // Practical Dependence Testing
14 // Goff, Kennedy, Tseng
17 // There's a single entry point that analyzes the dependence between a pair
18 // of memory references in a function, returning either NULL, for no dependence,
19 // or a more-or-less detailed description of the dependence between them.
21 // This pass exists to support the DependenceGraph pass. There are two separate
22 // passes because there's a useful separation of concerns. A dependence exists
23 // if two conditions are met:
25 // 1) Two instructions reference the same memory location, and
26 // 2) There is a flow of control leading from one instruction to the other.
28 // DependenceAnalysis attacks the first condition; DependenceGraph will attack
29 // the second (it's not yet ready).
31 // Please note that this is work in progress and the interface is subject to
35 // Return a set of more precise dependences instead of just one dependence
38 //===----------------------------------------------------------------------===//
40 #ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
41 #define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
43 #include "llvm/ADT/SmallBitVector.h"
44 #include "llvm/ADT/ArrayRef.h"
45 #include "llvm/Analysis/AliasAnalysis.h"
46 #include "llvm/IR/Instructions.h"
47 #include "llvm/Pass.h"
52 class ScalarEvolution;
57 /// Dependence - This class represents a dependence between two memory
58 /// memory references in a function. It contains minimal information and
59 /// is used in the very common situation where the compiler is unable to
60 /// determine anything beyond the existence of a dependence; that is, it
61 /// represents a confused dependence (see also FullDependence). In most
62 /// cases (for output, flow, and anti dependences), the dependence implies
63 /// an ordering, where the source must precede the destination; in contrast,
64 /// input dependences are unordered.
66 /// When a dependence graph is built, each Dependence will be a member of
67 /// the set of predecessor edges for its destination instruction and a set
68 /// if successor edges for its source instruction. These sets are represented
69 /// as singly-linked lists, with the "next" fields stored in the dependence
73 Dependence(const Dependence &) = default;
75 // FIXME: When we move to MSVC 2015 as the base compiler for Visual Studio
76 // support, uncomment this line to allow a defaulted move constructor for
77 // Dependence. Currently, FullDependence relies on the copy constructor, but
78 // that is acceptable given the triviality of the class.
79 // Dependence(Dependence &&) = default;
82 Dependence(Instruction *Source,
83 Instruction *Destination) :
86 NextPredecessor(nullptr),
87 NextSuccessor(nullptr) {}
88 virtual ~Dependence() {}
90 /// Dependence::DVEntry - Each level in the distance/direction vector
91 /// has a direction (or perhaps a union of several directions), and
92 /// perhaps a distance.
102 unsigned char Direction : 3; // Init to ALL, then refine.
103 bool Scalar : 1; // Init to true.
104 bool PeelFirst : 1; // Peeling the first iteration will break dependence.
105 bool PeelLast : 1; // Peeling the last iteration will break the dependence.
106 bool Splitable : 1; // Splitting the loop will break dependence.
107 const SCEV *Distance; // NULL implies no distance available.
108 DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
109 PeelLast(false), Splitable(false), Distance(nullptr) { }
112 /// getSrc - Returns the source instruction for this dependence.
114 Instruction *getSrc() const { return Src; }
116 /// getDst - Returns the destination instruction for this dependence.
118 Instruction *getDst() const { return Dst; }
120 /// isInput - Returns true if this is an input dependence.
122 bool isInput() const;
124 /// isOutput - Returns true if this is an output dependence.
126 bool isOutput() const;
128 /// isFlow - Returns true if this is a flow (aka true) dependence.
132 /// isAnti - Returns true if this is an anti dependence.
136 /// isOrdered - Returns true if dependence is Output, Flow, or Anti
138 bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
140 /// isUnordered - Returns true if dependence is Input
142 bool isUnordered() const { return isInput(); }
144 /// isLoopIndependent - Returns true if this is a loop-independent
146 virtual bool isLoopIndependent() const { return true; }
148 /// isConfused - Returns true if this dependence is confused
149 /// (the compiler understands nothing and makes worst-case
151 virtual bool isConfused() const { return true; }
153 /// isConsistent - Returns true if this dependence is consistent
154 /// (occurs every time the source and destination are executed).
155 virtual bool isConsistent() const { return false; }
157 /// getLevels - Returns the number of common loops surrounding the
158 /// source and destination of the dependence.
159 virtual unsigned getLevels() const { return 0; }
161 /// getDirection - Returns the direction associated with a particular
163 virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
165 /// getDistance - Returns the distance (or NULL) associated with a
166 /// particular level.
167 virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
169 /// isPeelFirst - Returns true if peeling the first iteration from
170 /// this loop will break this dependence.
171 virtual bool isPeelFirst(unsigned Level) const { return false; }
173 /// isPeelLast - Returns true if peeling the last iteration from
174 /// this loop will break this dependence.
175 virtual bool isPeelLast(unsigned Level) const { return false; }
177 /// isSplitable - Returns true if splitting this loop will break
179 virtual bool isSplitable(unsigned Level) const { return false; }
181 /// isScalar - Returns true if a particular level is scalar; that is,
182 /// if no subscript in the source or destination mention the induction
183 /// variable associated with the loop at this level.
184 virtual bool isScalar(unsigned Level) const;
186 /// getNextPredecessor - Returns the value of the NextPredecessor
188 const Dependence *getNextPredecessor() const {
189 return NextPredecessor;
192 /// getNextSuccessor - Returns the value of the NextSuccessor
194 const Dependence *getNextSuccessor() const {
195 return NextSuccessor;
198 /// setNextPredecessor - Sets the value of the NextPredecessor
200 void setNextPredecessor(const Dependence *pred) {
201 NextPredecessor = pred;
204 /// setNextSuccessor - Sets the value of the NextSuccessor
206 void setNextSuccessor(const Dependence *succ) {
207 NextSuccessor = succ;
210 /// dump - For debugging purposes, dumps a dependence to OS.
212 void dump(raw_ostream &OS) const;
214 Instruction *Src, *Dst;
215 const Dependence *NextPredecessor, *NextSuccessor;
216 friend class DependenceAnalysis;
220 /// FullDependence - This class represents a dependence between two memory
221 /// references in a function. It contains detailed information about the
222 /// dependence (direction vectors, etc.) and is used when the compiler is
223 /// able to accurately analyze the interaction of the references; that is,
224 /// it is not a confused dependence (see Dependence). In most cases
225 /// (for output, flow, and anti dependences), the dependence implies an
226 /// ordering, where the source must precede the destination; in contrast,
227 /// input dependences are unordered.
228 class FullDependence final : public Dependence {
230 FullDependence(Instruction *Src, Instruction *Dst, bool LoopIndependent,
233 FullDependence(FullDependence &&RHS)
234 : Dependence(std::move(RHS)), Levels(RHS.Levels),
235 LoopIndependent(RHS.LoopIndependent), Consistent(RHS.Consistent),
236 DV(std::move(RHS.DV)) {}
238 /// isLoopIndependent - Returns true if this is a loop-independent
240 bool isLoopIndependent() const override { return LoopIndependent; }
242 /// isConfused - Returns true if this dependence is confused
243 /// (the compiler understands nothing and makes worst-case
245 bool isConfused() const override { return false; }
247 /// isConsistent - Returns true if this dependence is consistent
248 /// (occurs every time the source and destination are executed).
249 bool isConsistent() const override { return Consistent; }
251 /// getLevels - Returns the number of common loops surrounding the
252 /// source and destination of the dependence.
253 unsigned getLevels() const override { return Levels; }
255 /// getDirection - Returns the direction associated with a particular
257 unsigned getDirection(unsigned Level) const override;
259 /// getDistance - Returns the distance (or NULL) associated with a
260 /// particular level.
261 const SCEV *getDistance(unsigned Level) const override;
263 /// isPeelFirst - Returns true if peeling the first iteration from
264 /// this loop will break this dependence.
265 bool isPeelFirst(unsigned Level) const override;
267 /// isPeelLast - Returns true if peeling the last iteration from
268 /// this loop will break this dependence.
269 bool isPeelLast(unsigned Level) const override;
271 /// isSplitable - Returns true if splitting the loop will break
273 bool isSplitable(unsigned Level) const override;
275 /// isScalar - Returns true if a particular level is scalar; that is,
276 /// if no subscript in the source or destination mention the induction
277 /// variable associated with the loop at this level.
278 bool isScalar(unsigned Level) const override;
281 unsigned short Levels;
282 bool LoopIndependent;
283 bool Consistent; // Init to true, then refine.
284 std::unique_ptr<DVEntry[]> DV;
285 friend class DependenceAnalysis;
289 /// DependenceAnalysis - This class is the main dependence-analysis driver.
291 class DependenceAnalysis : public FunctionPass {
292 void operator=(const DependenceAnalysis &) = delete;
293 DependenceAnalysis(const DependenceAnalysis &) = delete;
295 /// depends - Tests for a dependence between the Src and Dst instructions.
296 /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
297 /// FullDependence) with as much information as can be gleaned.
298 /// The flag PossiblyLoopIndependent should be set by the caller
299 /// if it appears that control flow can reach from Src to Dst
300 /// without traversing a loop back edge.
301 std::unique_ptr<Dependence> depends(Instruction *Src,
303 bool PossiblyLoopIndependent);
305 /// getSplitIteration - Give a dependence that's splittable at some
306 /// particular level, return the iteration that should be used to split
309 /// Generally, the dependence analyzer will be used to build
310 /// a dependence graph for a function (basically a map from instructions
311 /// to dependences). Looking for cycles in the graph shows us loops
312 /// that cannot be trivially vectorized/parallelized.
314 /// We can try to improve the situation by examining all the dependences
315 /// that make up the cycle, looking for ones we can break.
316 /// Sometimes, peeling the first or last iteration of a loop will break
317 /// dependences, and there are flags for those possibilities.
318 /// Sometimes, splitting a loop at some other iteration will do the trick,
319 /// and we've got a flag for that case. Rather than waste the space to
320 /// record the exact iteration (since we rarely know), we provide
321 /// a method that calculates the iteration. It's a drag that it must work
322 /// from scratch, but wonderful in that it's possible.
324 /// Here's an example:
326 /// for (i = 0; i < 10; i++)
330 /// There's a loop-carried flow dependence from the store to the load,
331 /// found by the weak-crossing SIV test. The dependence will have a flag,
332 /// indicating that the dependence can be broken by splitting the loop.
333 /// Calling getSplitIteration will return 5.
334 /// Splitting the loop breaks the dependence, like so:
336 /// for (i = 0; i <= 5; i++)
339 /// for (i = 6; i < 10; i++)
343 /// breaks the dependence and allows us to vectorize/parallelize
345 const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
353 /// Subscript - This private struct represents a pair of subscripts from
354 /// a pair of potentially multi-dimensional array references. We use a
355 /// vector of them to guide subscript partitioning.
359 enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
360 SmallBitVector Loops;
361 SmallBitVector GroupLoops;
362 SmallBitVector Group;
365 struct CoefficientInfo {
369 const SCEV *Iterations;
373 const SCEV *Iterations;
374 const SCEV *Upper[8];
375 const SCEV *Lower[8];
376 unsigned char Direction;
377 unsigned char DirSet;
380 /// Constraint - This private class represents a constraint, as defined
383 /// Practical Dependence Testing
384 /// Goff, Kennedy, Tseng
387 /// There are 5 kinds of constraint, in a hierarchy.
388 /// 1) Any - indicates no constraint, any dependence is possible.
389 /// 2) Line - A line ax + by = c, where a, b, and c are parameters,
390 /// representing the dependence equation.
391 /// 3) Distance - The value d of the dependence distance;
392 /// 4) Point - A point <x, y> representing the dependence from
393 /// iteration x to iteration y.
394 /// 5) Empty - No dependence is possible.
397 enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
402 const Loop *AssociatedLoop;
404 /// isEmpty - Return true if the constraint is of kind Empty.
405 bool isEmpty() const { return Kind == Empty; }
407 /// isPoint - Return true if the constraint is of kind Point.
408 bool isPoint() const { return Kind == Point; }
410 /// isDistance - Return true if the constraint is of kind Distance.
411 bool isDistance() const { return Kind == Distance; }
413 /// isLine - Return true if the constraint is of kind Line.
414 /// Since Distance's can also be represented as Lines, we also return
415 /// true if the constraint is of kind Distance.
416 bool isLine() const { return Kind == Line || Kind == Distance; }
418 /// isAny - Return true if the constraint is of kind Any;
419 bool isAny() const { return Kind == Any; }
421 /// getX - If constraint is a point <X, Y>, returns X.
422 /// Otherwise assert.
423 const SCEV *getX() const;
425 /// getY - If constraint is a point <X, Y>, returns Y.
426 /// Otherwise assert.
427 const SCEV *getY() const;
429 /// getA - If constraint is a line AX + BY = C, returns A.
430 /// Otherwise assert.
431 const SCEV *getA() const;
433 /// getB - If constraint is a line AX + BY = C, returns B.
434 /// Otherwise assert.
435 const SCEV *getB() const;
437 /// getC - If constraint is a line AX + BY = C, returns C.
438 /// Otherwise assert.
439 const SCEV *getC() const;
441 /// getD - If constraint is a distance, returns D.
442 /// Otherwise assert.
443 const SCEV *getD() const;
445 /// getAssociatedLoop - Returns the loop associated with this constraint.
446 const Loop *getAssociatedLoop() const;
448 /// setPoint - Change a constraint to Point.
449 void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
451 /// setLine - Change a constraint to Line.
452 void setLine(const SCEV *A, const SCEV *B,
453 const SCEV *C, const Loop *CurrentLoop);
455 /// setDistance - Change a constraint to Distance.
456 void setDistance(const SCEV *D, const Loop *CurrentLoop);
458 /// setEmpty - Change a constraint to Empty.
461 /// setAny - Change a constraint to Any.
462 void setAny(ScalarEvolution *SE);
464 /// dump - For debugging purposes. Dumps the constraint
466 void dump(raw_ostream &OS) const;
470 /// establishNestingLevels - Examines the loop nesting of the Src and Dst
471 /// instructions and establishes their shared loops. Sets the variables
472 /// CommonLevels, SrcLevels, and MaxLevels.
473 /// The source and destination instructions needn't be contained in the same
474 /// loop. The routine establishNestingLevels finds the level of most deeply
475 /// nested loop that contains them both, CommonLevels. An instruction that's
476 /// not contained in a loop is at level = 0. MaxLevels is equal to the level
477 /// of the source plus the level of the destination, minus CommonLevels.
478 /// This lets us allocate vectors MaxLevels in length, with room for every
479 /// distinct loop referenced in both the source and destination subscripts.
480 /// The variable SrcLevels is the nesting depth of the source instruction.
481 /// It's used to help calculate distinct loops referenced by the destination.
482 /// Here's the map from loops to levels:
484 /// 1 - outermost common loop
485 /// ... - other common loops
486 /// CommonLevels - innermost common loop
487 /// ... - loops containing Src but not Dst
488 /// SrcLevels - innermost loop containing Src but not Dst
489 /// ... - loops containing Dst but not Src
490 /// MaxLevels - innermost loop containing Dst but not Src
491 /// Consider the follow code fragment:
508 /// If we're looking at the possibility of a dependence between the store
509 /// to A (the Src) and the load from A (the Dst), we'll note that they
510 /// have 2 loops in common, so CommonLevels will equal 2 and the direction
511 /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
512 /// A map from loop names to level indices would look like
514 /// b - 2 = CommonLevels
516 /// d - 4 = SrcLevels
519 /// g - 7 = MaxLevels
520 void establishNestingLevels(const Instruction *Src,
521 const Instruction *Dst);
523 unsigned CommonLevels, SrcLevels, MaxLevels;
525 /// mapSrcLoop - Given one of the loops containing the source, return
526 /// its level index in our numbering scheme.
527 unsigned mapSrcLoop(const Loop *SrcLoop) const;
529 /// mapDstLoop - Given one of the loops containing the destination,
530 /// return its level index in our numbering scheme.
531 unsigned mapDstLoop(const Loop *DstLoop) const;
533 /// isLoopInvariant - Returns true if Expression is loop invariant
535 bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
537 /// Makes sure all subscript pairs share the same integer type by
538 /// sign-extending as necessary.
539 /// Sign-extending a subscript is safe because getelementptr assumes the
540 /// array subscripts are signed.
541 void unifySubscriptType(ArrayRef<Subscript *> Pairs);
543 /// removeMatchingExtensions - Examines a subscript pair.
544 /// If the source and destination are identically sign (or zero)
545 /// extended, it strips off the extension in an effort to
546 /// simplify the actual analysis.
547 void removeMatchingExtensions(Subscript *Pair);
549 /// collectCommonLoops - Finds the set of loops from the LoopNest that
550 /// have a level <= CommonLevels and are referred to by the SCEV Expression.
551 void collectCommonLoops(const SCEV *Expression,
552 const Loop *LoopNest,
553 SmallBitVector &Loops) const;
555 /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
556 /// linear. Collect the set of loops mentioned by Src.
557 bool checkSrcSubscript(const SCEV *Src,
558 const Loop *LoopNest,
559 SmallBitVector &Loops);
561 /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
562 /// linear. Collect the set of loops mentioned by Dst.
563 bool checkDstSubscript(const SCEV *Dst,
564 const Loop *LoopNest,
565 SmallBitVector &Loops);
567 /// isKnownPredicate - Compare X and Y using the predicate Pred.
568 /// Basically a wrapper for SCEV::isKnownPredicate,
569 /// but tries harder, especially in the presence of sign and zero
570 /// extensions and symbolics.
571 bool isKnownPredicate(ICmpInst::Predicate Pred,
573 const SCEV *Y) const;
575 /// collectUpperBound - All subscripts are the same type (on my machine,
576 /// an i64). The loop bound may be a smaller type. collectUpperBound
577 /// find the bound, if available, and zero extends it to the Type T.
578 /// (I zero extend since the bound should always be >= 0.)
579 /// If no upper bound is available, return NULL.
580 const SCEV *collectUpperBound(const Loop *l, Type *T) const;
582 /// collectConstantUpperBound - Calls collectUpperBound(), then
583 /// attempts to cast it to SCEVConstant. If the cast fails,
585 const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
587 /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
588 /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
589 /// Collects the associated loops in a set.
590 Subscript::ClassificationKind classifyPair(const SCEV *Src,
591 const Loop *SrcLoopNest,
593 const Loop *DstLoopNest,
594 SmallBitVector &Loops);
596 /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
597 /// Returns true if any possible dependence is disproved.
598 /// If there might be a dependence, returns false.
599 /// If the dependence isn't proven to exist,
600 /// marks the Result as inconsistent.
601 bool testZIV(const SCEV *Src,
603 FullDependence &Result) const;
605 /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
606 /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
607 /// i and j are induction variables, c1 and c2 are loop invariant,
608 /// and a1 and a2 are constant.
609 /// Returns true if any possible dependence is disproved.
610 /// If there might be a dependence, returns false.
611 /// Sets appropriate direction vector entry and, when possible,
612 /// the distance vector entry.
613 /// If the dependence isn't proven to exist,
614 /// marks the Result as inconsistent.
615 bool testSIV(const SCEV *Src,
618 FullDependence &Result,
619 Constraint &NewConstraint,
620 const SCEV *&SplitIter) const;
622 /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
623 /// Things of the form [c1 + a1*i] and [c2 + a2*j]
624 /// where i and j are induction variables, c1 and c2 are loop invariant,
625 /// and a1 and a2 are constant.
626 /// With minor algebra, this test can also be used for things like
627 /// [c1 + a1*i + a2*j][c2].
628 /// Returns true if any possible dependence is disproved.
629 /// If there might be a dependence, returns false.
630 /// Marks the Result as inconsistent.
631 bool testRDIV(const SCEV *Src,
633 FullDependence &Result) const;
635 /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
636 /// Returns true if dependence disproved.
637 /// Can sometimes refine direction vectors.
638 bool testMIV(const SCEV *Src,
640 const SmallBitVector &Loops,
641 FullDependence &Result) const;
643 /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
645 /// Things of the form [c1 + a*i] and [c2 + a*i],
646 /// where i is an induction variable, c1 and c2 are loop invariant,
647 /// and a is a constant
648 /// Returns true if any possible dependence is disproved.
649 /// If there might be a dependence, returns false.
650 /// Sets appropriate direction and distance.
651 bool strongSIVtest(const SCEV *Coeff,
652 const SCEV *SrcConst,
653 const SCEV *DstConst,
654 const Loop *CurrentLoop,
656 FullDependence &Result,
657 Constraint &NewConstraint) const;
659 /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
660 /// (Src and Dst) for dependence.
661 /// Things of the form [c1 + a*i] and [c2 - a*i],
662 /// where i is an induction variable, c1 and c2 are loop invariant,
663 /// and a is a constant.
664 /// Returns true if any possible dependence is disproved.
665 /// If there might be a dependence, returns false.
666 /// Sets appropriate direction entry.
667 /// Set consistent to false.
668 /// Marks the dependence as splitable.
669 bool weakCrossingSIVtest(const SCEV *SrcCoeff,
670 const SCEV *SrcConst,
671 const SCEV *DstConst,
672 const Loop *CurrentLoop,
674 FullDependence &Result,
675 Constraint &NewConstraint,
676 const SCEV *&SplitIter) const;
678 /// ExactSIVtest - Tests the SIV subscript pair
679 /// (Src and Dst) for dependence.
680 /// Things of the form [c1 + a1*i] and [c2 + a2*i],
681 /// where i is an induction variable, c1 and c2 are loop invariant,
682 /// and a1 and a2 are constant.
683 /// Returns true if any possible dependence is disproved.
684 /// If there might be a dependence, returns false.
685 /// Sets appropriate direction entry.
686 /// Set consistent to false.
687 bool exactSIVtest(const SCEV *SrcCoeff,
688 const SCEV *DstCoeff,
689 const SCEV *SrcConst,
690 const SCEV *DstConst,
691 const Loop *CurrentLoop,
693 FullDependence &Result,
694 Constraint &NewConstraint) const;
696 /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
697 /// (Src and Dst) for dependence.
698 /// Things of the form [c1] and [c2 + a*i],
699 /// where i is an induction variable, c1 and c2 are loop invariant,
700 /// and a is a constant. See also weakZeroDstSIVtest.
701 /// Returns true if any possible dependence is disproved.
702 /// If there might be a dependence, returns false.
703 /// Sets appropriate direction entry.
704 /// Set consistent to false.
705 /// If loop peeling will break the dependence, mark appropriately.
706 bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
707 const SCEV *SrcConst,
708 const SCEV *DstConst,
709 const Loop *CurrentLoop,
711 FullDependence &Result,
712 Constraint &NewConstraint) const;
714 /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
715 /// (Src and Dst) for dependence.
716 /// Things of the form [c1 + a*i] and [c2],
717 /// where i is an induction variable, c1 and c2 are loop invariant,
718 /// and a is a constant. See also weakZeroSrcSIVtest.
719 /// Returns true if any possible dependence is disproved.
720 /// If there might be a dependence, returns false.
721 /// Sets appropriate direction entry.
722 /// Set consistent to false.
723 /// If loop peeling will break the dependence, mark appropriately.
724 bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
725 const SCEV *SrcConst,
726 const SCEV *DstConst,
727 const Loop *CurrentLoop,
729 FullDependence &Result,
730 Constraint &NewConstraint) const;
732 /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
733 /// Things of the form [c1 + a*i] and [c2 + b*j],
734 /// where i and j are induction variable, c1 and c2 are loop invariant,
735 /// and a and b are constants.
736 /// Returns true if any possible dependence is disproved.
737 /// Marks the result as inconsistent.
738 /// Works in some cases that symbolicRDIVtest doesn't,
740 bool exactRDIVtest(const SCEV *SrcCoeff,
741 const SCEV *DstCoeff,
742 const SCEV *SrcConst,
743 const SCEV *DstConst,
746 FullDependence &Result) const;
748 /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
749 /// Things of the form [c1 + a*i] and [c2 + b*j],
750 /// where i and j are induction variable, c1 and c2 are loop invariant,
751 /// and a and b are constants.
752 /// Returns true if any possible dependence is disproved.
753 /// Marks the result as inconsistent.
754 /// Works in some cases that exactRDIVtest doesn't,
755 /// and vice versa. Can also be used as a backup for
756 /// ordinary SIV tests.
757 bool symbolicRDIVtest(const SCEV *SrcCoeff,
758 const SCEV *DstCoeff,
759 const SCEV *SrcConst,
760 const SCEV *DstConst,
762 const Loop *DstLoop) const;
764 /// gcdMIVtest - Tests an MIV subscript pair for dependence.
765 /// Returns true if any possible dependence is disproved.
766 /// Marks the result as inconsistent.
767 /// Can sometimes disprove the equal direction for 1 or more loops.
768 // Can handle some symbolics that even the SIV tests don't get,
769 /// so we use it as a backup for everything.
770 bool gcdMIVtest(const SCEV *Src,
772 FullDependence &Result) const;
774 /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
775 /// Returns true if any possible dependence is disproved.
776 /// Marks the result as inconsistent.
777 /// Computes directions.
778 bool banerjeeMIVtest(const SCEV *Src,
780 const SmallBitVector &Loops,
781 FullDependence &Result) const;
783 /// collectCoefficientInfo - Walks through the subscript,
784 /// collecting each coefficient, the associated loop bounds,
785 /// and recording its positive and negative parts for later use.
786 CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
788 const SCEV *&Constant) const;
790 /// getPositivePart - X^+ = max(X, 0).
792 const SCEV *getPositivePart(const SCEV *X) const;
794 /// getNegativePart - X^- = min(X, 0).
796 const SCEV *getNegativePart(const SCEV *X) const;
798 /// getLowerBound - Looks through all the bounds info and
799 /// computes the lower bound given the current direction settings
801 const SCEV *getLowerBound(BoundInfo *Bound) const;
803 /// getUpperBound - Looks through all the bounds info and
804 /// computes the upper bound given the current direction settings
806 const SCEV *getUpperBound(BoundInfo *Bound) const;
808 /// exploreDirections - Hierarchically expands the direction vector
809 /// search space, combining the directions of discovered dependences
810 /// in the DirSet field of Bound. Returns the number of distinct
811 /// dependences discovered. If the dependence is disproved,
812 /// it will return 0.
813 unsigned exploreDirections(unsigned Level,
817 const SmallBitVector &Loops,
818 unsigned &DepthExpanded,
819 const SCEV *Delta) const;
821 /// testBounds - Returns true iff the current bounds are plausible.
823 bool testBounds(unsigned char DirKind,
826 const SCEV *Delta) const;
828 /// findBoundsALL - Computes the upper and lower bounds for level K
829 /// using the * direction. Records them in Bound.
830 void findBoundsALL(CoefficientInfo *A,
835 /// findBoundsLT - Computes the upper and lower bounds for level K
836 /// using the < direction. Records them in Bound.
837 void findBoundsLT(CoefficientInfo *A,
842 /// findBoundsGT - Computes the upper and lower bounds for level K
843 /// using the > direction. Records them in Bound.
844 void findBoundsGT(CoefficientInfo *A,
849 /// findBoundsEQ - Computes the upper and lower bounds for level K
850 /// using the = direction. Records them in Bound.
851 void findBoundsEQ(CoefficientInfo *A,
856 /// intersectConstraints - Updates X with the intersection
857 /// of the Constraints X and Y. Returns true if X has changed.
858 bool intersectConstraints(Constraint *X,
859 const Constraint *Y);
861 /// propagate - Review the constraints, looking for opportunities
862 /// to simplify a subscript pair (Src and Dst).
863 /// Return true if some simplification occurs.
864 /// If the simplification isn't exact (that is, if it is conservative
865 /// in terms of dependence), set consistent to false.
866 bool propagate(const SCEV *&Src,
868 SmallBitVector &Loops,
869 SmallVectorImpl<Constraint> &Constraints,
872 /// propagateDistance - Attempt to propagate a distance
873 /// constraint into a subscript pair (Src and Dst).
874 /// Return true if some simplification occurs.
875 /// If the simplification isn't exact (that is, if it is conservative
876 /// in terms of dependence), set consistent to false.
877 bool propagateDistance(const SCEV *&Src,
879 Constraint &CurConstraint,
882 /// propagatePoint - Attempt to propagate a point
883 /// constraint into a subscript pair (Src and Dst).
884 /// Return true if some simplification occurs.
885 bool propagatePoint(const SCEV *&Src,
887 Constraint &CurConstraint);
889 /// propagateLine - Attempt to propagate a line
890 /// constraint into a subscript pair (Src and Dst).
891 /// Return true if some simplification occurs.
892 /// If the simplification isn't exact (that is, if it is conservative
893 /// in terms of dependence), set consistent to false.
894 bool propagateLine(const SCEV *&Src,
896 Constraint &CurConstraint,
899 /// findCoefficient - Given a linear SCEV,
900 /// return the coefficient corresponding to specified loop.
901 /// If there isn't one, return the SCEV constant 0.
902 /// For example, given a*i + b*j + c*k, returning the coefficient
903 /// corresponding to the j loop would yield b.
904 const SCEV *findCoefficient(const SCEV *Expr,
905 const Loop *TargetLoop) const;
907 /// zeroCoefficient - Given a linear SCEV,
908 /// return the SCEV given by zeroing out the coefficient
909 /// corresponding to the specified loop.
910 /// For example, given a*i + b*j + c*k, zeroing the coefficient
911 /// corresponding to the j loop would yield a*i + c*k.
912 const SCEV *zeroCoefficient(const SCEV *Expr,
913 const Loop *TargetLoop) const;
915 /// addToCoefficient - Given a linear SCEV Expr,
916 /// return the SCEV given by adding some Value to the
917 /// coefficient corresponding to the specified TargetLoop.
918 /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
919 /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
920 const SCEV *addToCoefficient(const SCEV *Expr,
921 const Loop *TargetLoop,
922 const SCEV *Value) const;
924 /// updateDirection - Update direction vector entry
925 /// based on the current constraint.
926 void updateDirection(Dependence::DVEntry &Level,
927 const Constraint &CurConstraint) const;
929 bool tryDelinearize(Instruction *Src, Instruction *Dst,
930 SmallVectorImpl<Subscript> &Pair);
933 static char ID; // Class identification, replacement for typeinfo
934 DependenceAnalysis() : FunctionPass(ID) {
935 initializeDependenceAnalysisPass(*PassRegistry::getPassRegistry());
938 bool runOnFunction(Function &F) override;
939 void releaseMemory() override;
940 void getAnalysisUsage(AnalysisUsage &) const override;
941 void print(raw_ostream &, const Module * = nullptr) const override;
942 }; // class DependenceAnalysis
944 /// createDependenceAnalysisPass - This creates an instance of the
945 /// DependenceAnalysis pass.
946 FunctionPass *createDependenceAnalysisPass();