LoopNestAnalysis.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/LoopNestAnalysis.h -------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the interface for the loop nest analysis.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_LOOPNESTANALYSIS_H
  19. #define LLVM_ANALYSIS_LOOPNESTANALYSIS_H
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/Analysis/LoopAnalysisManager.h"
  22. #include "llvm/Analysis/LoopInfo.h"
  23. namespace llvm {
  24. using LoopVectorTy = SmallVector<Loop *, 8>;
  25. class LPMUpdater;
  26. /// This class represents a loop nest and can be used to query its properties.
  27. class LoopNest {
  28. public:
  29. /// Construct a loop nest rooted by loop \p Root.
  30. LoopNest(Loop &Root, ScalarEvolution &SE);
  31. LoopNest() = delete;
  32. LoopNest &operator=(const LoopNest &) = delete;
  33. /// Construct a LoopNest object.
  34. static std::unique_ptr<LoopNest> getLoopNest(Loop &Root, ScalarEvolution &SE);
  35. /// Return true if the given loops \p OuterLoop and \p InnerLoop are
  36. /// perfectly nested with respect to each other, and false otherwise.
  37. /// Example:
  38. /// \code
  39. /// for(i)
  40. /// for(j)
  41. /// for(k)
  42. /// \endcode
  43. /// arePerfectlyNested(loop_i, loop_j, SE) would return true.
  44. /// arePerfectlyNested(loop_j, loop_k, SE) would return true.
  45. /// arePerfectlyNested(loop_i, loop_k, SE) would return false.
  46. static bool arePerfectlyNested(const Loop &OuterLoop, const Loop &InnerLoop,
  47. ScalarEvolution &SE);
  48. /// Return the maximum nesting depth of the loop nest rooted by loop \p Root.
  49. /// For example given the loop nest:
  50. /// \code
  51. /// for(i) // loop at level 1 and Root of the nest
  52. /// for(j) // loop at level 2
  53. /// <code>
  54. /// for(k) // loop at level 3
  55. /// \endcode
  56. /// getMaxPerfectDepth(Loop_i) would return 2.
  57. static unsigned getMaxPerfectDepth(const Loop &Root, ScalarEvolution &SE);
  58. /// Recursivelly traverse all empty 'single successor' basic blocks of \p From
  59. /// (if there are any). Return the last basic block found or \p End if it was
  60. /// reached during the search.
  61. static const BasicBlock &skipEmptyBlockUntil(const BasicBlock *From,
  62. const BasicBlock *End);
  63. /// Return the outermost loop in the loop nest.
  64. Loop &getOutermostLoop() const { return *Loops.front(); }
  65. /// Return the innermost loop in the loop nest if the nest has only one
  66. /// innermost loop, and a nullptr otherwise.
  67. /// Note: the innermost loop returned is not necessarily perfectly nested.
  68. Loop *getInnermostLoop() const {
  69. if (Loops.size() == 1)
  70. return Loops.back();
  71. // The loops in the 'Loops' vector have been collected in breadth first
  72. // order, therefore if the last 2 loops in it have the same nesting depth
  73. // there isn't a unique innermost loop in the nest.
  74. Loop *LastLoop = Loops.back();
  75. auto SecondLastLoopIter = ++Loops.rbegin();
  76. return (LastLoop->getLoopDepth() == (*SecondLastLoopIter)->getLoopDepth())
  77. ? nullptr
  78. : LastLoop;
  79. }
  80. /// Return the loop at the given \p Index.
  81. Loop *getLoop(unsigned Index) const {
  82. assert(Index < Loops.size() && "Index is out of bounds");
  83. return Loops[Index];
  84. }
  85. /// Return the number of loops in the nest.
  86. size_t getNumLoops() const { return Loops.size(); }
  87. /// Get the loops in the nest.
  88. ArrayRef<Loop *> getLoops() const { return Loops; }
  89. /// Retrieve a vector of perfect loop nests contained in the current loop
  90. /// nest. For example, given the following nest containing 4 loops, this
  91. /// member function would return {{L1,L2},{L3,L4}}.
  92. /// \code
  93. /// for(i) // L1
  94. /// for(j) // L2
  95. /// <code>
  96. /// for(k) // L3
  97. /// for(l) // L4
  98. /// \endcode
  99. SmallVector<LoopVectorTy, 4> getPerfectLoops(ScalarEvolution &SE) const;
  100. /// Return the loop nest depth (i.e. the loop depth of the 'deepest' loop)
  101. /// For example given the loop nest:
  102. /// \code
  103. /// for(i) // loop at level 1 and Root of the nest
  104. /// for(j1) // loop at level 2
  105. /// for(k) // loop at level 3
  106. /// for(j2) // loop at level 2
  107. /// \endcode
  108. /// getNestDepth() would return 3.
  109. unsigned getNestDepth() const {
  110. int NestDepth =
  111. Loops.back()->getLoopDepth() - Loops.front()->getLoopDepth() + 1;
  112. assert(NestDepth > 0 && "Expecting NestDepth to be at least 1");
  113. return NestDepth;
  114. }
  115. /// Return the maximum perfect nesting depth.
  116. unsigned getMaxPerfectDepth() const { return MaxPerfectDepth; }
  117. /// Return true if all loops in the loop nest are in simplify form.
  118. bool areAllLoopsSimplifyForm() const {
  119. return all_of(Loops, [](const Loop *L) { return L->isLoopSimplifyForm(); });
  120. }
  121. /// Return true if all loops in the loop nest are in rotated form.
  122. bool areAllLoopsRotatedForm() const {
  123. return all_of(Loops, [](const Loop *L) { return L->isRotatedForm(); });
  124. }
  125. StringRef getName() const { return Loops.front()->getName(); }
  126. protected:
  127. const unsigned MaxPerfectDepth; // maximum perfect nesting depth level.
  128. LoopVectorTy Loops; // the loops in the nest (in breadth first order).
  129. };
  130. raw_ostream &operator<<(raw_ostream &, const LoopNest &);
  131. /// This analysis provides information for a loop nest. The analysis runs on
  132. /// demand and can be initiated via AM.getResult<LoopNestAnalysis>.
  133. class LoopNestAnalysis : public AnalysisInfoMixin<LoopNestAnalysis> {
  134. friend AnalysisInfoMixin<LoopNestAnalysis>;
  135. static AnalysisKey Key;
  136. public:
  137. using Result = LoopNest;
  138. Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
  139. };
  140. /// Printer pass for the \c LoopNest results.
  141. class LoopNestPrinterPass : public PassInfoMixin<LoopNestPrinterPass> {
  142. raw_ostream &OS;
  143. public:
  144. explicit LoopNestPrinterPass(raw_ostream &OS) : OS(OS) {}
  145. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  146. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  147. };
  148. } // namespace llvm
  149. #endif // LLVM_ANALYSIS_LOOPNESTANALYSIS_H
  150. #ifdef __GNUC__
  151. #pragma GCC diagnostic pop
  152. #endif