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