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/Analysis/AliasAnalysis.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/Pass.h"
49 template <typename T> class ArrayRef;
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(Dependence &&) = default;
74 Dependence &operator=(Dependence &&) = default;
77 Dependence(Instruction *Source,
78 Instruction *Destination) :
81 NextPredecessor(nullptr),
82 NextSuccessor(nullptr) {}
83 virtual ~Dependence() {}
85 /// Dependence::DVEntry - Each level in the distance/direction vector
86 /// has a direction (or perhaps a union of several directions), and
87 /// perhaps a distance.
97 unsigned char Direction : 3; // Init to ALL, then refine.
98 bool Scalar : 1; // Init to true.
99 bool PeelFirst : 1; // Peeling the first iteration will break dependence.
100 bool PeelLast : 1; // Peeling the last iteration will break the dependence.
101 bool Splitable : 1; // Splitting the loop will break dependence.
102 const SCEV *Distance; // NULL implies no distance available.
103 DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
104 PeelLast(false), Splitable(false), Distance(nullptr) { }
107 /// getSrc - Returns the source instruction for this dependence.
109 Instruction *getSrc() const { return Src; }
111 /// getDst - Returns the destination instruction for this dependence.
113 Instruction *getDst() const { return Dst; }
115 /// isInput - Returns true if this is an input dependence.
117 bool isInput() const;
119 /// isOutput - Returns true if this is an output dependence.
121 bool isOutput() const;
123 /// isFlow - Returns true if this is a flow (aka true) dependence.
127 /// isAnti - Returns true if this is an anti dependence.
131 /// isOrdered - Returns true if dependence is Output, Flow, or Anti
133 bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
135 /// isUnordered - Returns true if dependence is Input
137 bool isUnordered() const { return isInput(); }
139 /// isLoopIndependent - Returns true if this is a loop-independent
141 virtual bool isLoopIndependent() const { return true; }
143 /// isConfused - Returns true if this dependence is confused
144 /// (the compiler understands nothing and makes worst-case
146 virtual bool isConfused() const { return true; }
148 /// isConsistent - Returns true if this dependence is consistent
149 /// (occurs every time the source and destination are executed).
150 virtual bool isConsistent() const { return false; }
152 /// getLevels - Returns the number of common loops surrounding the
153 /// source and destination of the dependence.
154 virtual unsigned getLevels() const { return 0; }
156 /// getDirection - Returns the direction associated with a particular
158 virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
160 /// getDistance - Returns the distance (or NULL) associated with a
161 /// particular level.
162 virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
164 /// isPeelFirst - Returns true if peeling the first iteration from
165 /// this loop will break this dependence.
166 virtual bool isPeelFirst(unsigned Level) const { return false; }
168 /// isPeelLast - Returns true if peeling the last iteration from
169 /// this loop will break this dependence.
170 virtual bool isPeelLast(unsigned Level) const { return false; }
172 /// isSplitable - Returns true if splitting this loop will break
174 virtual bool isSplitable(unsigned Level) const { return false; }
176 /// isScalar - Returns true if a particular level is scalar; that is,
177 /// if no subscript in the source or destination mention the induction
178 /// variable associated with the loop at this level.
179 virtual bool isScalar(unsigned Level) const;
181 /// getNextPredecessor - Returns the value of the NextPredecessor
183 const Dependence *getNextPredecessor() const { return NextPredecessor; }
185 /// getNextSuccessor - Returns the value of the NextSuccessor
187 const Dependence *getNextSuccessor() const { return NextSuccessor; }
189 /// setNextPredecessor - Sets the value of the NextPredecessor
191 void setNextPredecessor(const Dependence *pred) { NextPredecessor = pred; }
193 /// setNextSuccessor - Sets the value of the NextSuccessor
195 void setNextSuccessor(const Dependence *succ) { NextSuccessor = succ; }
197 /// dump - For debugging purposes, dumps a dependence to OS.
199 void dump(raw_ostream &OS) const;
202 Instruction *Src, *Dst;
203 const Dependence *NextPredecessor, *NextSuccessor;
204 friend class DependenceInfo;
207 /// FullDependence - This class represents a dependence between two memory
208 /// references in a function. It contains detailed information about the
209 /// dependence (direction vectors, etc.) and is used when the compiler is
210 /// able to accurately analyze the interaction of the references; that is,
211 /// it is not a confused dependence (see Dependence). In most cases
212 /// (for output, flow, and anti dependences), the dependence implies an
213 /// ordering, where the source must precede the destination; in contrast,
214 /// input dependences are unordered.
215 class FullDependence final : public Dependence {
217 FullDependence(Instruction *Src, Instruction *Dst, bool LoopIndependent,
220 /// isLoopIndependent - Returns true if this is a loop-independent
222 bool isLoopIndependent() const override { return LoopIndependent; }
224 /// isConfused - Returns true if this dependence is confused
225 /// (the compiler understands nothing and makes worst-case
227 bool isConfused() const override { return false; }
229 /// isConsistent - Returns true if this dependence is consistent
230 /// (occurs every time the source and destination are executed).
231 bool isConsistent() const override { return Consistent; }
233 /// getLevels - Returns the number of common loops surrounding the
234 /// source and destination of the dependence.
235 unsigned getLevels() const override { return Levels; }
237 /// getDirection - Returns the direction associated with a particular
239 unsigned getDirection(unsigned Level) const override;
241 /// getDistance - Returns the distance (or NULL) associated with a
242 /// particular level.
243 const SCEV *getDistance(unsigned Level) const override;
245 /// isPeelFirst - Returns true if peeling the first iteration from
246 /// this loop will break this dependence.
247 bool isPeelFirst(unsigned Level) const override;
249 /// isPeelLast - Returns true if peeling the last iteration from
250 /// this loop will break this dependence.
251 bool isPeelLast(unsigned Level) const override;
253 /// isSplitable - Returns true if splitting the loop will break
255 bool isSplitable(unsigned Level) const override;
257 /// isScalar - Returns true if a particular level is scalar; that is,
258 /// if no subscript in the source or destination mention the induction
259 /// variable associated with the loop at this level.
260 bool isScalar(unsigned Level) const override;
263 unsigned short Levels;
264 bool LoopIndependent;
265 bool Consistent; // Init to true, then refine.
266 std::unique_ptr<DVEntry[]> DV;
267 friend class DependenceInfo;
270 /// DependenceInfo - This class is the main dependence-analysis driver.
272 class DependenceInfo {
274 DependenceInfo(Function *F, AliasAnalysis *AA, ScalarEvolution *SE,
276 : AA(AA), SE(SE), LI(LI), F(F) {}
278 /// depends - Tests for a dependence between the Src and Dst instructions.
279 /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
280 /// FullDependence) with as much information as can be gleaned.
281 /// The flag PossiblyLoopIndependent should be set by the caller
282 /// if it appears that control flow can reach from Src to Dst
283 /// without traversing a loop back edge.
284 std::unique_ptr<Dependence> depends(Instruction *Src,
286 bool PossiblyLoopIndependent);
288 /// getSplitIteration - Give a dependence that's splittable at some
289 /// particular level, return the iteration that should be used to split
292 /// Generally, the dependence analyzer will be used to build
293 /// a dependence graph for a function (basically a map from instructions
294 /// to dependences). Looking for cycles in the graph shows us loops
295 /// that cannot be trivially vectorized/parallelized.
297 /// We can try to improve the situation by examining all the dependences
298 /// that make up the cycle, looking for ones we can break.
299 /// Sometimes, peeling the first or last iteration of a loop will break
300 /// dependences, and there are flags for those possibilities.
301 /// Sometimes, splitting a loop at some other iteration will do the trick,
302 /// and we've got a flag for that case. Rather than waste the space to
303 /// record the exact iteration (since we rarely know), we provide
304 /// a method that calculates the iteration. It's a drag that it must work
305 /// from scratch, but wonderful in that it's possible.
307 /// Here's an example:
309 /// for (i = 0; i < 10; i++)
313 /// There's a loop-carried flow dependence from the store to the load,
314 /// found by the weak-crossing SIV test. The dependence will have a flag,
315 /// indicating that the dependence can be broken by splitting the loop.
316 /// Calling getSplitIteration will return 5.
317 /// Splitting the loop breaks the dependence, like so:
319 /// for (i = 0; i <= 5; i++)
322 /// for (i = 6; i < 10; i++)
326 /// breaks the dependence and allows us to vectorize/parallelize
328 const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
330 Function *getFunction() const { return F; }
338 /// Subscript - This private struct represents a pair of subscripts from
339 /// a pair of potentially multi-dimensional array references. We use a
340 /// vector of them to guide subscript partitioning.
344 enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
345 SmallBitVector Loops;
346 SmallBitVector GroupLoops;
347 SmallBitVector Group;
350 struct CoefficientInfo {
354 const SCEV *Iterations;
358 const SCEV *Iterations;
359 const SCEV *Upper[8];
360 const SCEV *Lower[8];
361 unsigned char Direction;
362 unsigned char DirSet;
365 /// Constraint - This private class represents a constraint, as defined
368 /// Practical Dependence Testing
369 /// Goff, Kennedy, Tseng
372 /// There are 5 kinds of constraint, in a hierarchy.
373 /// 1) Any - indicates no constraint, any dependence is possible.
374 /// 2) Line - A line ax + by = c, where a, b, and c are parameters,
375 /// representing the dependence equation.
376 /// 3) Distance - The value d of the dependence distance;
377 /// 4) Point - A point <x, y> representing the dependence from
378 /// iteration x to iteration y.
379 /// 5) Empty - No dependence is possible.
382 enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
387 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;
455 /// establishNestingLevels - Examines the loop nesting of the Src and Dst
456 /// instructions and establishes their shared loops. Sets the variables
457 /// CommonLevels, SrcLevels, and MaxLevels.
458 /// The source and destination instructions needn't be contained in the same
459 /// loop. The routine establishNestingLevels finds the level of most deeply
460 /// nested loop that contains them both, CommonLevels. An instruction that's
461 /// not contained in a loop is at level = 0. MaxLevels is equal to the level
462 /// of the source plus the level of the destination, minus CommonLevels.
463 /// This lets us allocate vectors MaxLevels in length, with room for every
464 /// distinct loop referenced in both the source and destination subscripts.
465 /// The variable SrcLevels is the nesting depth of the source instruction.
466 /// It's used to help calculate distinct loops referenced by the destination.
467 /// Here's the map from loops to levels:
469 /// 1 - outermost common loop
470 /// ... - other common loops
471 /// CommonLevels - innermost common loop
472 /// ... - loops containing Src but not Dst
473 /// SrcLevels - innermost loop containing Src but not Dst
474 /// ... - loops containing Dst but not Src
475 /// MaxLevels - innermost loop containing Dst but not Src
476 /// Consider the follow code fragment:
493 /// If we're looking at the possibility of a dependence between the store
494 /// to A (the Src) and the load from A (the Dst), we'll note that they
495 /// have 2 loops in common, so CommonLevels will equal 2 and the direction
496 /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
497 /// A map from loop names to level indices would look like
499 /// b - 2 = CommonLevels
501 /// d - 4 = SrcLevels
504 /// g - 7 = MaxLevels
505 void establishNestingLevels(const Instruction *Src,
506 const Instruction *Dst);
508 unsigned CommonLevels, SrcLevels, MaxLevels;
510 /// mapSrcLoop - Given one of the loops containing the source, return
511 /// its level index in our numbering scheme.
512 unsigned mapSrcLoop(const Loop *SrcLoop) const;
514 /// mapDstLoop - Given one of the loops containing the destination,
515 /// return its level index in our numbering scheme.
516 unsigned mapDstLoop(const Loop *DstLoop) const;
518 /// isLoopInvariant - Returns true if Expression is loop invariant
520 bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
522 /// Makes sure all subscript pairs share the same integer type by
523 /// sign-extending as necessary.
524 /// Sign-extending a subscript is safe because getelementptr assumes the
525 /// array subscripts are signed.
526 void unifySubscriptType(ArrayRef<Subscript *> Pairs);
528 /// removeMatchingExtensions - Examines a subscript pair.
529 /// If the source and destination are identically sign (or zero)
530 /// extended, it strips off the extension in an effort to
531 /// simplify the actual analysis.
532 void removeMatchingExtensions(Subscript *Pair);
534 /// collectCommonLoops - Finds the set of loops from the LoopNest that
535 /// have a level <= CommonLevels and are referred to by the SCEV Expression.
536 void collectCommonLoops(const SCEV *Expression,
537 const Loop *LoopNest,
538 SmallBitVector &Loops) const;
540 /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
541 /// linear. Collect the set of loops mentioned by Src.
542 bool checkSrcSubscript(const SCEV *Src,
543 const Loop *LoopNest,
544 SmallBitVector &Loops);
546 /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
547 /// linear. Collect the set of loops mentioned by Dst.
548 bool checkDstSubscript(const SCEV *Dst,
549 const Loop *LoopNest,
550 SmallBitVector &Loops);
552 /// isKnownPredicate - Compare X and Y using the predicate Pred.
553 /// Basically a wrapper for SCEV::isKnownPredicate,
554 /// but tries harder, especially in the presence of sign and zero
555 /// extensions and symbolics.
556 bool isKnownPredicate(ICmpInst::Predicate Pred,
558 const SCEV *Y) const;
560 /// collectUpperBound - All subscripts are the same type (on my machine,
561 /// an i64). The loop bound may be a smaller type. collectUpperBound
562 /// find the bound, if available, and zero extends it to the Type T.
563 /// (I zero extend since the bound should always be >= 0.)
564 /// If no upper bound is available, return NULL.
565 const SCEV *collectUpperBound(const Loop *l, Type *T) const;
567 /// collectConstantUpperBound - Calls collectUpperBound(), then
568 /// attempts to cast it to SCEVConstant. If the cast fails,
570 const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
572 /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
573 /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
574 /// Collects the associated loops in a set.
575 Subscript::ClassificationKind classifyPair(const SCEV *Src,
576 const Loop *SrcLoopNest,
578 const Loop *DstLoopNest,
579 SmallBitVector &Loops);
581 /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
582 /// Returns true if any possible dependence is disproved.
583 /// If there might be a dependence, returns false.
584 /// If the dependence isn't proven to exist,
585 /// marks the Result as inconsistent.
586 bool testZIV(const SCEV *Src,
588 FullDependence &Result) const;
590 /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
591 /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
592 /// i and j are induction variables, c1 and c2 are loop invariant,
593 /// and a1 and a2 are constant.
594 /// Returns true if any possible dependence is disproved.
595 /// If there might be a dependence, returns false.
596 /// Sets appropriate direction vector entry and, when possible,
597 /// the distance vector entry.
598 /// If the dependence isn't proven to exist,
599 /// marks the Result as inconsistent.
600 bool testSIV(const SCEV *Src,
603 FullDependence &Result,
604 Constraint &NewConstraint,
605 const SCEV *&SplitIter) const;
607 /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
608 /// Things of the form [c1 + a1*i] and [c2 + a2*j]
609 /// where i and j are induction variables, c1 and c2 are loop invariant,
610 /// and a1 and a2 are constant.
611 /// With minor algebra, this test can also be used for things like
612 /// [c1 + a1*i + a2*j][c2].
613 /// Returns true if any possible dependence is disproved.
614 /// If there might be a dependence, returns false.
615 /// Marks the Result as inconsistent.
616 bool testRDIV(const SCEV *Src,
618 FullDependence &Result) const;
620 /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
621 /// Returns true if dependence disproved.
622 /// Can sometimes refine direction vectors.
623 bool testMIV(const SCEV *Src,
625 const SmallBitVector &Loops,
626 FullDependence &Result) const;
628 /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
630 /// Things of the form [c1 + a*i] and [c2 + a*i],
631 /// where i is an induction variable, c1 and c2 are loop invariant,
632 /// and a is a constant
633 /// Returns true if any possible dependence is disproved.
634 /// If there might be a dependence, returns false.
635 /// Sets appropriate direction and distance.
636 bool strongSIVtest(const SCEV *Coeff,
637 const SCEV *SrcConst,
638 const SCEV *DstConst,
639 const Loop *CurrentLoop,
641 FullDependence &Result,
642 Constraint &NewConstraint) const;
644 /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
645 /// (Src and Dst) for dependence.
646 /// Things of the form [c1 + a*i] and [c2 - a*i],
647 /// where i is an induction variable, c1 and c2 are loop invariant,
648 /// and a is a constant.
649 /// Returns true if any possible dependence is disproved.
650 /// If there might be a dependence, returns false.
651 /// Sets appropriate direction entry.
652 /// Set consistent to false.
653 /// Marks the dependence as splitable.
654 bool weakCrossingSIVtest(const SCEV *SrcCoeff,
655 const SCEV *SrcConst,
656 const SCEV *DstConst,
657 const Loop *CurrentLoop,
659 FullDependence &Result,
660 Constraint &NewConstraint,
661 const SCEV *&SplitIter) const;
663 /// ExactSIVtest - Tests the SIV subscript pair
664 /// (Src and Dst) for dependence.
665 /// Things of the form [c1 + a1*i] and [c2 + a2*i],
666 /// where i is an induction variable, c1 and c2 are loop invariant,
667 /// and a1 and a2 are constant.
668 /// Returns true if any possible dependence is disproved.
669 /// If there might be a dependence, returns false.
670 /// Sets appropriate direction entry.
671 /// Set consistent to false.
672 bool exactSIVtest(const SCEV *SrcCoeff,
673 const SCEV *DstCoeff,
674 const SCEV *SrcConst,
675 const SCEV *DstConst,
676 const Loop *CurrentLoop,
678 FullDependence &Result,
679 Constraint &NewConstraint) const;
681 /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
682 /// (Src and Dst) for dependence.
683 /// Things of the form [c1] and [c2 + a*i],
684 /// where i is an induction variable, c1 and c2 are loop invariant,
685 /// and a is a constant. See also weakZeroDstSIVtest.
686 /// Returns true if any possible dependence is disproved.
687 /// If there might be a dependence, returns false.
688 /// Sets appropriate direction entry.
689 /// Set consistent to false.
690 /// If loop peeling will break the dependence, mark appropriately.
691 bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
692 const SCEV *SrcConst,
693 const SCEV *DstConst,
694 const Loop *CurrentLoop,
696 FullDependence &Result,
697 Constraint &NewConstraint) const;
699 /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
700 /// (Src and Dst) for dependence.
701 /// Things of the form [c1 + a*i] and [c2],
702 /// where i is an induction variable, c1 and c2 are loop invariant,
703 /// and a is a constant. See also weakZeroSrcSIVtest.
704 /// Returns true if any possible dependence is disproved.
705 /// If there might be a dependence, returns false.
706 /// Sets appropriate direction entry.
707 /// Set consistent to false.
708 /// If loop peeling will break the dependence, mark appropriately.
709 bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
710 const SCEV *SrcConst,
711 const SCEV *DstConst,
712 const Loop *CurrentLoop,
714 FullDependence &Result,
715 Constraint &NewConstraint) const;
717 /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
718 /// Things of the form [c1 + a*i] and [c2 + b*j],
719 /// where i and j are induction variable, c1 and c2 are loop invariant,
720 /// and a and b are constants.
721 /// Returns true if any possible dependence is disproved.
722 /// Marks the result as inconsistent.
723 /// Works in some cases that symbolicRDIVtest doesn't,
725 bool exactRDIVtest(const SCEV *SrcCoeff,
726 const SCEV *DstCoeff,
727 const SCEV *SrcConst,
728 const SCEV *DstConst,
731 FullDependence &Result) const;
733 /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
734 /// Things of the form [c1 + a*i] and [c2 + b*j],
735 /// where i and j are induction variable, c1 and c2 are loop invariant,
736 /// and a and b are constants.
737 /// Returns true if any possible dependence is disproved.
738 /// Marks the result as inconsistent.
739 /// Works in some cases that exactRDIVtest doesn't,
740 /// and vice versa. Can also be used as a backup for
741 /// ordinary SIV tests.
742 bool symbolicRDIVtest(const SCEV *SrcCoeff,
743 const SCEV *DstCoeff,
744 const SCEV *SrcConst,
745 const SCEV *DstConst,
747 const Loop *DstLoop) const;
749 /// gcdMIVtest - Tests an MIV subscript pair for dependence.
750 /// Returns true if any possible dependence is disproved.
751 /// Marks the result as inconsistent.
752 /// Can sometimes disprove the equal direction for 1 or more loops.
753 // Can handle some symbolics that even the SIV tests don't get,
754 /// so we use it as a backup for everything.
755 bool gcdMIVtest(const SCEV *Src,
757 FullDependence &Result) const;
759 /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
760 /// Returns true if any possible dependence is disproved.
761 /// Marks the result as inconsistent.
762 /// Computes directions.
763 bool banerjeeMIVtest(const SCEV *Src,
765 const SmallBitVector &Loops,
766 FullDependence &Result) const;
768 /// collectCoefficientInfo - Walks through the subscript,
769 /// collecting each coefficient, the associated loop bounds,
770 /// and recording its positive and negative parts for later use.
771 CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
773 const SCEV *&Constant) const;
775 /// getPositivePart - X^+ = max(X, 0).
777 const SCEV *getPositivePart(const SCEV *X) const;
779 /// getNegativePart - X^- = min(X, 0).
781 const SCEV *getNegativePart(const SCEV *X) const;
783 /// getLowerBound - Looks through all the bounds info and
784 /// computes the lower bound given the current direction settings
786 const SCEV *getLowerBound(BoundInfo *Bound) const;
788 /// getUpperBound - Looks through all the bounds info and
789 /// computes the upper bound given the current direction settings
791 const SCEV *getUpperBound(BoundInfo *Bound) const;
793 /// exploreDirections - Hierarchically expands the direction vector
794 /// search space, combining the directions of discovered dependences
795 /// in the DirSet field of Bound. Returns the number of distinct
796 /// dependences discovered. If the dependence is disproved,
797 /// it will return 0.
798 unsigned exploreDirections(unsigned Level,
802 const SmallBitVector &Loops,
803 unsigned &DepthExpanded,
804 const SCEV *Delta) const;
806 /// testBounds - Returns true iff the current bounds are plausible.
807 bool testBounds(unsigned char DirKind,
810 const SCEV *Delta) const;
812 /// findBoundsALL - Computes the upper and lower bounds for level K
813 /// using the * direction. Records them in Bound.
814 void findBoundsALL(CoefficientInfo *A,
819 /// findBoundsLT - Computes the upper and lower bounds for level K
820 /// using the < direction. Records them in Bound.
821 void findBoundsLT(CoefficientInfo *A,
826 /// findBoundsGT - Computes the upper and lower bounds for level K
827 /// using the > direction. Records them in Bound.
828 void findBoundsGT(CoefficientInfo *A,
833 /// findBoundsEQ - Computes the upper and lower bounds for level K
834 /// using the = direction. Records them in Bound.
835 void findBoundsEQ(CoefficientInfo *A,
840 /// intersectConstraints - Updates X with the intersection
841 /// of the Constraints X and Y. Returns true if X has changed.
842 bool intersectConstraints(Constraint *X,
843 const Constraint *Y);
845 /// propagate - Review the constraints, looking for opportunities
846 /// to simplify a subscript pair (Src and Dst).
847 /// Return true if some simplification occurs.
848 /// If the simplification isn't exact (that is, if it is conservative
849 /// in terms of dependence), set consistent to false.
850 bool propagate(const SCEV *&Src,
852 SmallBitVector &Loops,
853 SmallVectorImpl<Constraint> &Constraints,
856 /// propagateDistance - Attempt to propagate a distance
857 /// constraint into a subscript pair (Src and Dst).
858 /// Return true if some simplification occurs.
859 /// If the simplification isn't exact (that is, if it is conservative
860 /// in terms of dependence), set consistent to false.
861 bool propagateDistance(const SCEV *&Src,
863 Constraint &CurConstraint,
866 /// propagatePoint - Attempt to propagate a point
867 /// constraint into a subscript pair (Src and Dst).
868 /// Return true if some simplification occurs.
869 bool propagatePoint(const SCEV *&Src,
871 Constraint &CurConstraint);
873 /// propagateLine - Attempt to propagate a line
874 /// constraint into a subscript pair (Src and Dst).
875 /// Return true if some simplification occurs.
876 /// If the simplification isn't exact (that is, if it is conservative
877 /// in terms of dependence), set consistent to false.
878 bool propagateLine(const SCEV *&Src,
880 Constraint &CurConstraint,
883 /// findCoefficient - Given a linear SCEV,
884 /// return the coefficient corresponding to specified loop.
885 /// If there isn't one, return the SCEV constant 0.
886 /// For example, given a*i + b*j + c*k, returning the coefficient
887 /// corresponding to the j loop would yield b.
888 const SCEV *findCoefficient(const SCEV *Expr,
889 const Loop *TargetLoop) const;
891 /// zeroCoefficient - Given a linear SCEV,
892 /// return the SCEV given by zeroing out the coefficient
893 /// corresponding to the specified loop.
894 /// For example, given a*i + b*j + c*k, zeroing the coefficient
895 /// corresponding to the j loop would yield a*i + c*k.
896 const SCEV *zeroCoefficient(const SCEV *Expr,
897 const Loop *TargetLoop) const;
899 /// addToCoefficient - Given a linear SCEV Expr,
900 /// return the SCEV given by adding some Value to the
901 /// coefficient corresponding to the specified TargetLoop.
902 /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
903 /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
904 const SCEV *addToCoefficient(const SCEV *Expr,
905 const Loop *TargetLoop,
906 const SCEV *Value) const;
908 /// updateDirection - Update direction vector entry
909 /// based on the current constraint.
910 void updateDirection(Dependence::DVEntry &Level,
911 const Constraint &CurConstraint) const;
913 bool tryDelinearize(Instruction *Src, Instruction *Dst,
914 SmallVectorImpl<Subscript> &Pair);
915 }; // class DependenceInfo
917 /// \brief AnalysisPass to compute dependence information in a function
918 class DependenceAnalysis : public AnalysisInfoMixin<DependenceAnalysis> {
920 typedef DependenceInfo Result;
921 Result run(Function &F, FunctionAnalysisManager &FAM);
924 static AnalysisKey Key;
925 friend struct AnalysisInfoMixin<DependenceAnalysis>;
926 }; // class DependenceAnalysis
928 /// \brief Legacy pass manager pass to access dependence information
929 class DependenceAnalysisWrapperPass : public FunctionPass {
931 static char ID; // Class identification, replacement for typeinfo
932 DependenceAnalysisWrapperPass() : FunctionPass(ID) {
933 initializeDependenceAnalysisWrapperPassPass(
934 *PassRegistry::getPassRegistry());
937 bool runOnFunction(Function &F) override;
938 void releaseMemory() override;
939 void getAnalysisUsage(AnalysisUsage &) const override;
940 void print(raw_ostream &, const Module * = nullptr) const override;
941 DependenceInfo &getDI() const;
944 std::unique_ptr<DependenceInfo> info;
945 }; // class DependenceAnalysisWrapperPass
947 /// createDependenceAnalysisPass - This creates an instance of the
948 /// DependenceAnalysis wrapper pass.
949 FunctionPass *createDependenceAnalysisWrapperPass();