LoopNestAnalysis.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 LLVM_EXTERNAL_VISIBILITY LoopNest {
  28. public:
  29. using InstrVectorTy = SmallVector<const Instruction *>;
  30. /// Construct a loop nest rooted by loop \p Root.
  31. LoopNest(Loop &Root, ScalarEvolution &SE);
  32. 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 a vector of instructions that prevent the LoopNest given
  49. /// by loops \p OuterLoop and \p InnerLoop from being perfect.
  50. static InstrVectorTy getInterveningInstructions(const Loop &OuterLoop,
  51. const Loop &InnerLoop,
  52. ScalarEvolution &SE);
  53. /// Return the maximum nesting depth of the loop nest rooted by loop \p Root.
  54. /// For example given the loop nest:
  55. /// \code
  56. /// for(i) // loop at level 1 and Root of the nest
  57. /// for(j) // loop at level 2
  58. /// <code>
  59. /// for(k) // loop at level 3
  60. /// \endcode
  61. /// getMaxPerfectDepth(Loop_i) would return 2.
  62. static unsigned getMaxPerfectDepth(const Loop &Root, ScalarEvolution &SE);
  63. /// Recursivelly traverse all empty 'single successor' basic blocks of \p From
  64. /// (if there are any). When \p CheckUniquePred is set to true, check if
  65. /// each of the empty single successors has a unique predecessor. Return
  66. /// the last basic block found or \p End if it was reached during the search.
  67. static const BasicBlock &skipEmptyBlockUntil(const BasicBlock *From,
  68. const BasicBlock *End,
  69. bool CheckUniquePred = false);
  70. /// Return the outermost loop in the loop nest.
  71. Loop &getOutermostLoop() const { return *Loops.front(); }
  72. /// Return the innermost loop in the loop nest if the nest has only one
  73. /// innermost loop, and a nullptr otherwise.
  74. /// Note: the innermost loop returned is not necessarily perfectly nested.
  75. Loop *getInnermostLoop() const {
  76. if (Loops.size() == 1)
  77. return Loops.back();
  78. // The loops in the 'Loops' vector have been collected in breadth first
  79. // order, therefore if the last 2 loops in it have the same nesting depth
  80. // there isn't a unique innermost loop in the nest.
  81. Loop *LastLoop = Loops.back();
  82. auto SecondLastLoopIter = ++Loops.rbegin();
  83. return (LastLoop->getLoopDepth() == (*SecondLastLoopIter)->getLoopDepth())
  84. ? nullptr
  85. : LastLoop;
  86. }
  87. /// Return the loop at the given \p Index.
  88. Loop *getLoop(unsigned Index) const {
  89. assert(Index < Loops.size() && "Index is out of bounds");
  90. return Loops[Index];
  91. }
  92. /// Get the loop index of the given loop \p L.
  93. unsigned getLoopIndex(const Loop &L) const {
  94. for (unsigned I = 0; I < getNumLoops(); ++I)
  95. if (getLoop(I) == &L)
  96. return I;
  97. llvm_unreachable("Loop not in the loop nest");
  98. }
  99. /// Return the number of loops in the nest.
  100. size_t getNumLoops() const { return Loops.size(); }
  101. /// Get the loops in the nest.
  102. ArrayRef<Loop *> getLoops() const { return Loops; }
  103. /// Get the loops in the nest at the given \p Depth.
  104. LoopVectorTy getLoopsAtDepth(unsigned Depth) const {
  105. assert(Depth >= Loops.front()->getLoopDepth() &&
  106. Depth <= Loops.back()->getLoopDepth() && "Invalid depth");
  107. LoopVectorTy Result;
  108. for (unsigned I = 0; I < getNumLoops(); ++I) {
  109. Loop *L = getLoop(I);
  110. if (L->getLoopDepth() == Depth)
  111. Result.push_back(L);
  112. else if (L->getLoopDepth() > Depth)
  113. break;
  114. }
  115. return Result;
  116. }
  117. /// Retrieve a vector of perfect loop nests contained in the current loop
  118. /// nest. For example, given the following nest containing 4 loops, this
  119. /// member function would return {{L1,L2},{L3,L4}}.
  120. /// \code
  121. /// for(i) // L1
  122. /// for(j) // L2
  123. /// <code>
  124. /// for(k) // L3
  125. /// for(l) // L4
  126. /// \endcode
  127. SmallVector<LoopVectorTy, 4> getPerfectLoops(ScalarEvolution &SE) const;
  128. /// Return the loop nest depth (i.e. the loop depth of the 'deepest' loop)
  129. /// For example given the loop nest:
  130. /// \code
  131. /// for(i) // loop at level 1 and Root of the nest
  132. /// for(j1) // loop at level 2
  133. /// for(k) // loop at level 3
  134. /// for(j2) // loop at level 2
  135. /// \endcode
  136. /// getNestDepth() would return 3.
  137. unsigned getNestDepth() const {
  138. int NestDepth =
  139. Loops.back()->getLoopDepth() - Loops.front()->getLoopDepth() + 1;
  140. assert(NestDepth > 0 && "Expecting NestDepth to be at least 1");
  141. return NestDepth;
  142. }
  143. /// Return the maximum perfect nesting depth.
  144. unsigned getMaxPerfectDepth() const { return MaxPerfectDepth; }
  145. /// Return true if all loops in the loop nest are in simplify form.
  146. bool areAllLoopsSimplifyForm() const {
  147. return all_of(Loops, [](const Loop *L) { return L->isLoopSimplifyForm(); });
  148. }
  149. /// Return true if all loops in the loop nest are in rotated form.
  150. bool areAllLoopsRotatedForm() const {
  151. return all_of(Loops, [](const Loop *L) { return L->isRotatedForm(); });
  152. }
  153. /// Return the function to which the loop-nest belongs.
  154. Function *getParent() const {
  155. return Loops.front()->getHeader()->getParent();
  156. }
  157. StringRef getName() const { return Loops.front()->getName(); }
  158. protected:
  159. const unsigned MaxPerfectDepth; // maximum perfect nesting depth level.
  160. LoopVectorTy Loops; // the loops in the nest (in breadth first order).
  161. private:
  162. enum LoopNestEnum {
  163. PerfectLoopNest,
  164. ImperfectLoopNest,
  165. InvalidLoopStructure,
  166. OuterLoopLowerBoundUnknown
  167. };
  168. static LoopNestEnum analyzeLoopNestForPerfectNest(const Loop &OuterLoop,
  169. const Loop &InnerLoop,
  170. ScalarEvolution &SE);
  171. };
  172. raw_ostream &operator<<(raw_ostream &, const LoopNest &);
  173. /// This analysis provides information for a loop nest. The analysis runs on
  174. /// demand and can be initiated via AM.getResult<LoopNestAnalysis>.
  175. class LoopNestAnalysis : public AnalysisInfoMixin<LoopNestAnalysis> {
  176. friend AnalysisInfoMixin<LoopNestAnalysis>;
  177. static AnalysisKey Key;
  178. public:
  179. using Result = LoopNest;
  180. Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
  181. };
  182. /// Printer pass for the \c LoopNest results.
  183. class LoopNestPrinterPass : public PassInfoMixin<LoopNestPrinterPass> {
  184. raw_ostream &OS;
  185. public:
  186. explicit LoopNestPrinterPass(raw_ostream &OS) : OS(OS) {}
  187. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  188. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  189. };
  190. } // namespace llvm
  191. #endif // LLVM_ANALYSIS_LOOPNESTANALYSIS_H
  192. #ifdef __GNUC__
  193. #pragma GCC diagnostic pop
  194. #endif