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