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/IR/Instructions.h"
45 #include "llvm/Pass.h"
51 class ScalarEvolution;
56 /// Dependence - This class represents a dependence between two memory
57 /// memory references in a function. It contains minimal information and
58 /// is used in the very common situation where the compiler is unable to
59 /// determine anything beyond the existence of a dependence; that is, it
60 /// represents a confused dependence (see also FullDependence). In most
61 /// cases (for output, flow, and anti dependences), the dependence implies
62 /// an ordering, where the source must precede the destination; in contrast,
63 /// input dependences are unordered.
65 /// When a dependence graph is built, each Dependence will be a member of
66 /// the set of predecessor edges for its destination instruction and a set
67 /// if successor edges for its source instruction. These sets are represented
68 /// as singly-linked lists, with the "next" fields stored in the dependence
72 Dependence(Instruction *Source,
73 Instruction *Destination) :
76 NextPredecessor(nullptr),
77 NextSuccessor(nullptr) {}
78 virtual ~Dependence() {}
80 /// Dependence::DVEntry - Each level in the distance/direction vector
81 /// has a direction (or perhaps a union of several directions), and
82 /// perhaps a distance.
92 unsigned char Direction : 3; // Init to ALL, then refine.
93 bool Scalar : 1; // Init to true.
94 bool PeelFirst : 1; // Peeling the first iteration will break dependence.
95 bool PeelLast : 1; // Peeling the last iteration will break the dependence.
96 bool Splitable : 1; // Splitting the loop will break dependence.
97 const SCEV *Distance; // NULL implies no distance available.
98 DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
99 PeelLast(false), Splitable(false), Distance(nullptr) { }
102 /// getSrc - Returns the source instruction for this dependence.
104 Instruction *getSrc() const { return Src; }
106 /// getDst - Returns the destination instruction for this dependence.
108 Instruction *getDst() const { return Dst; }
110 /// isInput - Returns true if this is an input dependence.
112 bool isInput() const;
114 /// isOutput - Returns true if this is an output dependence.
116 bool isOutput() const;
118 /// isFlow - Returns true if this is a flow (aka true) dependence.
122 /// isAnti - Returns true if this is an anti dependence.
126 /// isOrdered - Returns true if dependence is Output, Flow, or Anti
128 bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
130 /// isUnordered - Returns true if dependence is Input
132 bool isUnordered() const { return isInput(); }
134 /// isLoopIndependent - Returns true if this is a loop-independent
136 virtual bool isLoopIndependent() const { return true; }
138 /// isConfused - Returns true if this dependence is confused
139 /// (the compiler understands nothing and makes worst-case
141 virtual bool isConfused() const { return true; }
143 /// isConsistent - Returns true if this dependence is consistent
144 /// (occurs every time the source and destination are executed).
145 virtual bool isConsistent() const { return false; }
147 /// getLevels - Returns the number of common loops surrounding the
148 /// source and destination of the dependence.
149 virtual unsigned getLevels() const { return 0; }
151 /// getDirection - Returns the direction associated with a particular
153 virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
155 /// getDistance - Returns the distance (or NULL) associated with a
156 /// particular level.
157 virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
159 /// isPeelFirst - Returns true if peeling the first iteration from
160 /// this loop will break this dependence.
161 virtual bool isPeelFirst(unsigned Level) const { return false; }
163 /// isPeelLast - Returns true if peeling the last iteration from
164 /// this loop will break this dependence.
165 virtual bool isPeelLast(unsigned Level) const { return false; }
167 /// isSplitable - Returns true if splitting this loop will break
169 virtual bool isSplitable(unsigned Level) const { return false; }
171 /// isScalar - Returns true if a particular level is scalar; that is,
172 /// if no subscript in the source or destination mention the induction
173 /// variable associated with the loop at this level.
174 virtual bool isScalar(unsigned Level) const;
176 /// getNextPredecessor - Returns the value of the NextPredecessor
178 const Dependence *getNextPredecessor() const {
179 return NextPredecessor;
182 /// getNextSuccessor - Returns the value of the NextSuccessor
184 const Dependence *getNextSuccessor() const {
185 return NextSuccessor;
188 /// setNextPredecessor - Sets the value of the NextPredecessor
190 void setNextPredecessor(const Dependence *pred) {
191 NextPredecessor = pred;
194 /// setNextSuccessor - Sets the value of the NextSuccessor
196 void setNextSuccessor(const Dependence *succ) {
197 NextSuccessor = succ;
200 /// dump - For debugging purposes, dumps a dependence to OS.
202 void dump(raw_ostream &OS) const;
204 Instruction *Src, *Dst;
205 const Dependence *NextPredecessor, *NextSuccessor;
206 friend class DependenceAnalysis;
210 /// FullDependence - This class represents a dependence between two memory
211 /// references in a function. It contains detailed information about the
212 /// dependence (direction vectors, etc.) and is used when the compiler is
213 /// able to accurately analyze the interaction of the references; that is,
214 /// it is not a confused dependence (see Dependence). In most cases
215 /// (for output, flow, and anti dependences), the dependence implies an
216 /// ordering, where the source must precede the destination; in contrast,
217 /// input dependences are unordered.
218 class FullDependence : public Dependence {
220 FullDependence(Instruction *Src,
222 bool LoopIndependent,
225 /// isLoopIndependent - Returns true if this is a loop-independent
227 bool isLoopIndependent() const override { return LoopIndependent; }
229 /// isConfused - Returns true if this dependence is confused
230 /// (the compiler understands nothing and makes worst-case
232 bool isConfused() const override { return false; }
234 /// isConsistent - Returns true if this dependence is consistent
235 /// (occurs every time the source and destination are executed).
236 bool isConsistent() const override { return Consistent; }
238 /// getLevels - Returns the number of common loops surrounding the
239 /// source and destination of the dependence.
240 unsigned getLevels() const override { return Levels; }
242 /// getDirection - Returns the direction associated with a particular
244 unsigned getDirection(unsigned Level) const override;
246 /// getDistance - Returns the distance (or NULL) associated with a
247 /// particular level.
248 const SCEV *getDistance(unsigned Level) const override;
250 /// isPeelFirst - Returns true if peeling the first iteration from
251 /// this loop will break this dependence.
252 bool isPeelFirst(unsigned Level) const override;
254 /// isPeelLast - Returns true if peeling the last iteration from
255 /// this loop will break this dependence.
256 bool isPeelLast(unsigned Level) const override;
258 /// isSplitable - Returns true if splitting the loop will break
260 bool isSplitable(unsigned Level) const override;
262 /// isScalar - Returns true if a particular level is scalar; that is,
263 /// if no subscript in the source or destination mention the induction
264 /// variable associated with the loop at this level.
265 bool isScalar(unsigned Level) const override;
267 unsigned short Levels;
268 bool LoopIndependent;
269 bool Consistent; // Init to true, then refine.
270 std::unique_ptr<DVEntry[]> DV;
271 friend class DependenceAnalysis;
275 /// DependenceAnalysis - This class is the main dependence-analysis driver.
277 class DependenceAnalysis : public FunctionPass {
278 void operator=(const DependenceAnalysis &) = delete;
279 DependenceAnalysis(const DependenceAnalysis &) = delete;
281 /// depends - Tests for a dependence between the Src and Dst instructions.
282 /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
283 /// FullDependence) with as much information as can be gleaned.
284 /// The flag PossiblyLoopIndependent should be set by the caller
285 /// if it appears that control flow can reach from Src to Dst
286 /// without traversing a loop back edge.
287 std::unique_ptr<Dependence> depends(Instruction *Src,
289 bool PossiblyLoopIndependent);
291 /// getSplitIteration - Give a dependence that's splittable at some
292 /// particular level, return the iteration that should be used to split
295 /// Generally, the dependence analyzer will be used to build
296 /// a dependence graph for a function (basically a map from instructions
297 /// to dependences). Looking for cycles in the graph shows us loops
298 /// that cannot be trivially vectorized/parallelized.
300 /// We can try to improve the situation by examining all the dependences
301 /// that make up the cycle, looking for ones we can break.
302 /// Sometimes, peeling the first or last iteration of a loop will break
303 /// dependences, and there are flags for those possibilities.
304 /// Sometimes, splitting a loop at some other iteration will do the trick,
305 /// and we've got a flag for that case. Rather than waste the space to
306 /// record the exact iteration (since we rarely know), we provide
307 /// a method that calculates the iteration. It's a drag that it must work
308 /// from scratch, but wonderful in that it's possible.
310 /// Here's an example:
312 /// for (i = 0; i < 10; i++)
316 /// There's a loop-carried flow dependence from the store to the load,
317 /// found by the weak-crossing SIV test. The dependence will have a flag,
318 /// indicating that the dependence can be broken by splitting the loop.
319 /// Calling getSplitIteration will return 5.
320 /// Splitting the loop breaks the dependence, like so:
322 /// for (i = 0; i <= 5; i++)
325 /// for (i = 6; i < 10; i++)
329 /// breaks the dependence and allows us to vectorize/parallelize
331 const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
339 /// Subscript - This private struct represents a pair of subscripts from
340 /// a pair of potentially multi-dimensional array references. We use a
341 /// vector of them to guide subscript partitioning.
345 enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
346 SmallBitVector Loops;
347 SmallBitVector GroupLoops;
348 SmallBitVector Group;
351 struct CoefficientInfo {
355 const SCEV *Iterations;
359 const SCEV *Iterations;
360 const SCEV *Upper[8];
361 const SCEV *Lower[8];
362 unsigned char Direction;
363 unsigned char DirSet;
366 /// Constraint - This private class represents a constraint, as defined
369 /// Practical Dependence Testing
370 /// Goff, Kennedy, Tseng
373 /// There are 5 kinds of constraint, in a hierarchy.
374 /// 1) Any - indicates no constraint, any dependence is possible.
375 /// 2) Line - A line ax + by = c, where a, b, and c are parameters,
376 /// representing the dependence equation.
377 /// 3) Distance - The value d of the dependence distance;
378 /// 4) Point - A point <x, y> representing the dependence from
379 /// iteration x to iteration y.
380 /// 5) Empty - No dependence is possible.
383 enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
388 const Loop *AssociatedLoop;
390 /// isEmpty - Return true if the constraint is of kind Empty.
391 bool isEmpty() const { return Kind == Empty; }
393 /// isPoint - Return true if the constraint is of kind Point.
394 bool isPoint() const { return Kind == Point; }
396 /// isDistance - Return true if the constraint is of kind Distance.
397 bool isDistance() const { return Kind == Distance; }
399 /// isLine - Return true if the constraint is of kind Line.
400 /// Since Distance's can also be represented as Lines, we also return
401 /// true if the constraint is of kind Distance.
402 bool isLine() const { return Kind == Line || Kind == Distance; }
404 /// isAny - Return true if the constraint is of kind Any;
405 bool isAny() const { return Kind == Any; }
407 /// getX - If constraint is a point <X, Y>, returns X.
408 /// Otherwise assert.
409 const SCEV *getX() const;
411 /// getY - If constraint is a point <X, Y>, returns Y.
412 /// Otherwise assert.
413 const SCEV *getY() const;
415 /// getA - If constraint is a line AX + BY = C, returns A.
416 /// Otherwise assert.
417 const SCEV *getA() const;
419 /// getB - If constraint is a line AX + BY = C, returns B.
420 /// Otherwise assert.
421 const SCEV *getB() const;
423 /// getC - If constraint is a line AX + BY = C, returns C.
424 /// Otherwise assert.
425 const SCEV *getC() const;
427 /// getD - If constraint is a distance, returns D.
428 /// Otherwise assert.
429 const SCEV *getD() const;
431 /// getAssociatedLoop - Returns the loop associated with this constraint.
432 const Loop *getAssociatedLoop() const;
434 /// setPoint - Change a constraint to Point.
435 void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
437 /// setLine - Change a constraint to Line.
438 void setLine(const SCEV *A, const SCEV *B,
439 const SCEV *C, const Loop *CurrentLoop);
441 /// setDistance - Change a constraint to Distance.
442 void setDistance(const SCEV *D, const Loop *CurrentLoop);
444 /// setEmpty - Change a constraint to Empty.
447 /// setAny - Change a constraint to Any.
448 void setAny(ScalarEvolution *SE);
450 /// dump - For debugging purposes. Dumps the constraint
452 void dump(raw_ostream &OS) const;
456 /// establishNestingLevels - Examines the loop nesting of the Src and Dst
457 /// instructions and establishes their shared loops. Sets the variables
458 /// CommonLevels, SrcLevels, and MaxLevels.
459 /// The source and destination instructions needn't be contained in the same
460 /// loop. The routine establishNestingLevels finds the level of most deeply
461 /// nested loop that contains them both, CommonLevels. An instruction that's
462 /// not contained in a loop is at level = 0. MaxLevels is equal to the level
463 /// of the source plus the level of the destination, minus CommonLevels.
464 /// This lets us allocate vectors MaxLevels in length, with room for every
465 /// distinct loop referenced in both the source and destination subscripts.
466 /// The variable SrcLevels is the nesting depth of the source instruction.
467 /// It's used to help calculate distinct loops referenced by the destination.
468 /// Here's the map from loops to levels:
470 /// 1 - outermost common loop
471 /// ... - other common loops
472 /// CommonLevels - innermost common loop
473 /// ... - loops containing Src but not Dst
474 /// SrcLevels - innermost loop containing Src but not Dst
475 /// ... - loops containing Dst but not Src
476 /// MaxLevels - innermost loop containing Dst but not Src
477 /// Consider the follow code fragment:
494 /// If we're looking at the possibility of a dependence between the store
495 /// to A (the Src) and the load from A (the Dst), we'll note that they
496 /// have 2 loops in common, so CommonLevels will equal 2 and the direction
497 /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
498 /// A map from loop names to level indices would look like
500 /// b - 2 = CommonLevels
502 /// d - 4 = SrcLevels
505 /// g - 7 = MaxLevels
506 void establishNestingLevels(const Instruction *Src,
507 const Instruction *Dst);
509 unsigned CommonLevels, SrcLevels, MaxLevels;
511 /// mapSrcLoop - Given one of the loops containing the source, return
512 /// its level index in our numbering scheme.
513 unsigned mapSrcLoop(const Loop *SrcLoop) const;
515 /// mapDstLoop - Given one of the loops containing the destination,
516 /// return its level index in our numbering scheme.
517 unsigned mapDstLoop(const Loop *DstLoop) const;
519 /// isLoopInvariant - Returns true if Expression is loop invariant
521 bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
523 /// Makes sure both subscripts (i.e. Pair->Src and Pair->Dst) share the same
524 /// integer type by sign-extending one of them when necessary.
525 /// Sign-extending a subscript is safe because getelementptr assumes the
526 /// array subscripts are signed.
527 void unifySubscriptType(Subscript *Pair);
529 /// removeMatchingExtensions - Examines a subscript pair.
530 /// If the source and destination are identically sign (or zero)
531 /// extended, it strips off the extension in an effort to
532 /// simplify the actual analysis.
533 void removeMatchingExtensions(Subscript *Pair);
535 /// collectCommonLoops - Finds the set of loops from the LoopNest that
536 /// have a level <= CommonLevels and are referred to by the SCEV Expression.
537 void collectCommonLoops(const SCEV *Expression,
538 const Loop *LoopNest,
539 SmallBitVector &Loops) const;
541 /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
542 /// linear. Collect the set of loops mentioned by Src.
543 bool checkSrcSubscript(const SCEV *Src,
544 const Loop *LoopNest,
545 SmallBitVector &Loops);
547 /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
548 /// linear. Collect the set of loops mentioned by Dst.
549 bool checkDstSubscript(const SCEV *Dst,
550 const Loop *LoopNest,
551 SmallBitVector &Loops);
553 /// isKnownPredicate - Compare X and Y using the predicate Pred.
554 /// Basically a wrapper for SCEV::isKnownPredicate,
555 /// but tries harder, especially in the presence of sign and zero
556 /// extensions and symbolics.
557 bool isKnownPredicate(ICmpInst::Predicate Pred,
559 const SCEV *Y) const;
561 /// collectUpperBound - All subscripts are the same type (on my machine,
562 /// an i64). The loop bound may be a smaller type. collectUpperBound
563 /// find the bound, if available, and zero extends it to the Type T.
564 /// (I zero extend since the bound should always be >= 0.)
565 /// If no upper bound is available, return NULL.
566 const SCEV *collectUpperBound(const Loop *l, Type *T) const;
568 /// collectConstantUpperBound - Calls collectUpperBound(), then
569 /// attempts to cast it to SCEVConstant. If the cast fails,
571 const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
573 /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
574 /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
575 /// Collects the associated loops in a set.
576 Subscript::ClassificationKind classifyPair(const SCEV *Src,
577 const Loop *SrcLoopNest,
579 const Loop *DstLoopNest,
580 SmallBitVector &Loops);
582 /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
583 /// Returns true if any possible dependence is disproved.
584 /// If there might be a dependence, returns false.
585 /// If the dependence isn't proven to exist,
586 /// marks the Result as inconsistent.
587 bool testZIV(const SCEV *Src,
589 FullDependence &Result) const;
591 /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
592 /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
593 /// i and j are induction variables, c1 and c2 are loop invariant,
594 /// and a1 and a2 are constant.
595 /// Returns true if any possible dependence is disproved.
596 /// If there might be a dependence, returns false.
597 /// Sets appropriate direction vector entry and, when possible,
598 /// the distance vector entry.
599 /// If the dependence isn't proven to exist,
600 /// marks the Result as inconsistent.
601 bool testSIV(const SCEV *Src,
604 FullDependence &Result,
605 Constraint &NewConstraint,
606 const SCEV *&SplitIter) const;
608 /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
609 /// Things of the form [c1 + a1*i] and [c2 + a2*j]
610 /// where i and j are induction variables, c1 and c2 are loop invariant,
611 /// and a1 and a2 are constant.
612 /// With minor algebra, this test can also be used for things like
613 /// [c1 + a1*i + a2*j][c2].
614 /// Returns true if any possible dependence is disproved.
615 /// If there might be a dependence, returns false.
616 /// Marks the Result as inconsistent.
617 bool testRDIV(const SCEV *Src,
619 FullDependence &Result) const;
621 /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
622 /// Returns true if dependence disproved.
623 /// Can sometimes refine direction vectors.
624 bool testMIV(const SCEV *Src,
626 const SmallBitVector &Loops,
627 FullDependence &Result) const;
629 /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
631 /// Things of the form [c1 + a*i] and [c2 + a*i],
632 /// where i is an induction variable, c1 and c2 are loop invariant,
633 /// and a is a constant
634 /// Returns true if any possible dependence is disproved.
635 /// If there might be a dependence, returns false.
636 /// Sets appropriate direction and distance.
637 bool strongSIVtest(const SCEV *Coeff,
638 const SCEV *SrcConst,
639 const SCEV *DstConst,
640 const Loop *CurrentLoop,
642 FullDependence &Result,
643 Constraint &NewConstraint) const;
645 /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
646 /// (Src and Dst) for dependence.
647 /// Things of the form [c1 + a*i] and [c2 - a*i],
648 /// where i is an induction variable, c1 and c2 are loop invariant,
649 /// and a is a constant.
650 /// Returns true if any possible dependence is disproved.
651 /// If there might be a dependence, returns false.
652 /// Sets appropriate direction entry.
653 /// Set consistent to false.
654 /// Marks the dependence as splitable.
655 bool weakCrossingSIVtest(const SCEV *SrcCoeff,
656 const SCEV *SrcConst,
657 const SCEV *DstConst,
658 const Loop *CurrentLoop,
660 FullDependence &Result,
661 Constraint &NewConstraint,
662 const SCEV *&SplitIter) const;
664 /// ExactSIVtest - Tests the SIV subscript pair
665 /// (Src and Dst) for dependence.
666 /// Things of the form [c1 + a1*i] and [c2 + a2*i],
667 /// where i is an induction variable, c1 and c2 are loop invariant,
668 /// and a1 and a2 are constant.
669 /// Returns true if any possible dependence is disproved.
670 /// If there might be a dependence, returns false.
671 /// Sets appropriate direction entry.
672 /// Set consistent to false.
673 bool exactSIVtest(const SCEV *SrcCoeff,
674 const SCEV *DstCoeff,
675 const SCEV *SrcConst,
676 const SCEV *DstConst,
677 const Loop *CurrentLoop,
679 FullDependence &Result,
680 Constraint &NewConstraint) const;
682 /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
683 /// (Src and Dst) for dependence.
684 /// Things of the form [c1] and [c2 + a*i],
685 /// where i is an induction variable, c1 and c2 are loop invariant,
686 /// and a is a constant. See also weakZeroDstSIVtest.
687 /// Returns true if any possible dependence is disproved.
688 /// If there might be a dependence, returns false.
689 /// Sets appropriate direction entry.
690 /// Set consistent to false.
691 /// If loop peeling will break the dependence, mark appropriately.
692 bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
693 const SCEV *SrcConst,
694 const SCEV *DstConst,
695 const Loop *CurrentLoop,
697 FullDependence &Result,
698 Constraint &NewConstraint) const;
700 /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
701 /// (Src and Dst) for dependence.
702 /// Things of the form [c1 + a*i] and [c2],
703 /// where i is an induction variable, c1 and c2 are loop invariant,
704 /// and a is a constant. See also weakZeroSrcSIVtest.
705 /// Returns true if any possible dependence is disproved.
706 /// If there might be a dependence, returns false.
707 /// Sets appropriate direction entry.
708 /// Set consistent to false.
709 /// If loop peeling will break the dependence, mark appropriately.
710 bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
711 const SCEV *SrcConst,
712 const SCEV *DstConst,
713 const Loop *CurrentLoop,
715 FullDependence &Result,
716 Constraint &NewConstraint) const;
718 /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
719 /// Things of the form [c1 + a*i] and [c2 + b*j],
720 /// where i and j are induction variable, c1 and c2 are loop invariant,
721 /// and a and b are constants.
722 /// Returns true if any possible dependence is disproved.
723 /// Marks the result as inconsistent.
724 /// Works in some cases that symbolicRDIVtest doesn't,
726 bool exactRDIVtest(const SCEV *SrcCoeff,
727 const SCEV *DstCoeff,
728 const SCEV *SrcConst,
729 const SCEV *DstConst,
732 FullDependence &Result) const;
734 /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
735 /// Things of the form [c1 + a*i] and [c2 + b*j],
736 /// where i and j are induction variable, c1 and c2 are loop invariant,
737 /// and a and b are constants.
738 /// Returns true if any possible dependence is disproved.
739 /// Marks the result as inconsistent.
740 /// Works in some cases that exactRDIVtest doesn't,
741 /// and vice versa. Can also be used as a backup for
742 /// ordinary SIV tests.
743 bool symbolicRDIVtest(const SCEV *SrcCoeff,
744 const SCEV *DstCoeff,
745 const SCEV *SrcConst,
746 const SCEV *DstConst,
748 const Loop *DstLoop) const;
750 /// gcdMIVtest - Tests an MIV subscript pair for dependence.
751 /// Returns true if any possible dependence is disproved.
752 /// Marks the result as inconsistent.
753 /// Can sometimes disprove the equal direction for 1 or more loops.
754 // Can handle some symbolics that even the SIV tests don't get,
755 /// so we use it as a backup for everything.
756 bool gcdMIVtest(const SCEV *Src,
758 FullDependence &Result) const;
760 /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
761 /// Returns true if any possible dependence is disproved.
762 /// Marks the result as inconsistent.
763 /// Computes directions.
764 bool banerjeeMIVtest(const SCEV *Src,
766 const SmallBitVector &Loops,
767 FullDependence &Result) const;
769 /// collectCoefficientInfo - Walks through the subscript,
770 /// collecting each coefficient, the associated loop bounds,
771 /// and recording its positive and negative parts for later use.
772 CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
774 const SCEV *&Constant) const;
776 /// getPositivePart - X^+ = max(X, 0).
778 const SCEV *getPositivePart(const SCEV *X) const;
780 /// getNegativePart - X^- = min(X, 0).
782 const SCEV *getNegativePart(const SCEV *X) const;
784 /// getLowerBound - Looks through all the bounds info and
785 /// computes the lower bound given the current direction settings
787 const SCEV *getLowerBound(BoundInfo *Bound) const;
789 /// getUpperBound - Looks through all the bounds info and
790 /// computes the upper bound given the current direction settings
792 const SCEV *getUpperBound(BoundInfo *Bound) const;
794 /// exploreDirections - Hierarchically expands the direction vector
795 /// search space, combining the directions of discovered dependences
796 /// in the DirSet field of Bound. Returns the number of distinct
797 /// dependences discovered. If the dependence is disproved,
798 /// it will return 0.
799 unsigned exploreDirections(unsigned Level,
803 const SmallBitVector &Loops,
804 unsigned &DepthExpanded,
805 const SCEV *Delta) const;
807 /// testBounds - Returns true iff the current bounds are plausible.
809 bool testBounds(unsigned char DirKind,
812 const SCEV *Delta) const;
814 /// findBoundsALL - Computes the upper and lower bounds for level K
815 /// using the * direction. Records them in Bound.
816 void findBoundsALL(CoefficientInfo *A,
821 /// findBoundsLT - Computes the upper and lower bounds for level K
822 /// using the < direction. Records them in Bound.
823 void findBoundsLT(CoefficientInfo *A,
828 /// findBoundsGT - Computes the upper and lower bounds for level K
829 /// using the > direction. Records them in Bound.
830 void findBoundsGT(CoefficientInfo *A,
835 /// findBoundsEQ - Computes the upper and lower bounds for level K
836 /// using the = direction. Records them in Bound.
837 void findBoundsEQ(CoefficientInfo *A,
842 /// intersectConstraints - Updates X with the intersection
843 /// of the Constraints X and Y. Returns true if X has changed.
844 bool intersectConstraints(Constraint *X,
845 const Constraint *Y);
847 /// propagate - Review the constraints, looking for opportunities
848 /// to simplify a subscript pair (Src and Dst).
849 /// Return true if some simplification occurs.
850 /// If the simplification isn't exact (that is, if it is conservative
851 /// in terms of dependence), set consistent to false.
852 bool propagate(const SCEV *&Src,
854 SmallBitVector &Loops,
855 SmallVectorImpl<Constraint> &Constraints,
858 /// propagateDistance - Attempt to propagate a distance
859 /// constraint into a subscript pair (Src and Dst).
860 /// Return true if some simplification occurs.
861 /// If the simplification isn't exact (that is, if it is conservative
862 /// in terms of dependence), set consistent to false.
863 bool propagateDistance(const SCEV *&Src,
865 Constraint &CurConstraint,
868 /// propagatePoint - Attempt to propagate a point
869 /// constraint into a subscript pair (Src and Dst).
870 /// Return true if some simplification occurs.
871 bool propagatePoint(const SCEV *&Src,
873 Constraint &CurConstraint);
875 /// propagateLine - Attempt to propagate a line
876 /// constraint into a subscript pair (Src and Dst).
877 /// Return true if some simplification occurs.
878 /// If the simplification isn't exact (that is, if it is conservative
879 /// in terms of dependence), set consistent to false.
880 bool propagateLine(const SCEV *&Src,
882 Constraint &CurConstraint,
885 /// findCoefficient - Given a linear SCEV,
886 /// return the coefficient corresponding to specified loop.
887 /// If there isn't one, return the SCEV constant 0.
888 /// For example, given a*i + b*j + c*k, returning the coefficient
889 /// corresponding to the j loop would yield b.
890 const SCEV *findCoefficient(const SCEV *Expr,
891 const Loop *TargetLoop) const;
893 /// zeroCoefficient - Given a linear SCEV,
894 /// return the SCEV given by zeroing out the coefficient
895 /// corresponding to the specified loop.
896 /// For example, given a*i + b*j + c*k, zeroing the coefficient
897 /// corresponding to the j loop would yield a*i + c*k.
898 const SCEV *zeroCoefficient(const SCEV *Expr,
899 const Loop *TargetLoop) const;
901 /// addToCoefficient - Given a linear SCEV Expr,
902 /// return the SCEV given by adding some Value to the
903 /// coefficient corresponding to the specified TargetLoop.
904 /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
905 /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
906 const SCEV *addToCoefficient(const SCEV *Expr,
907 const Loop *TargetLoop,
908 const SCEV *Value) const;
910 /// updateDirection - Update direction vector entry
911 /// based on the current constraint.
912 void updateDirection(Dependence::DVEntry &Level,
913 const Constraint &CurConstraint) const;
915 bool tryDelinearize(const SCEV *SrcSCEV, const SCEV *DstSCEV,
916 SmallVectorImpl<Subscript> &Pair,
917 const SCEV *ElementSize);
920 static char ID; // Class identification, replacement for typeinfo
921 DependenceAnalysis() : FunctionPass(ID) {
922 initializeDependenceAnalysisPass(*PassRegistry::getPassRegistry());
925 bool runOnFunction(Function &F) override;
926 void releaseMemory() override;
927 void getAnalysisUsage(AnalysisUsage &) const override;
928 void print(raw_ostream &, const Module * = nullptr) const override;
929 }; // class DependenceAnalysis
931 /// createDependenceAnalysisPass - This creates an instance of the
932 /// DependenceAnalysis pass.
933 FunctionPass *createDependenceAnalysisPass();