LoopRotation.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
  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. // This file implements Loop Rotation Pass.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Scalar/LoopRotation.h"
  13. #include "llvm/ADT/Statistic.h"
  14. #include "llvm/Analysis/AssumptionCache.h"
  15. #include "llvm/Analysis/InstructionSimplify.h"
  16. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  17. #include "llvm/Analysis/LoopPass.h"
  18. #include "llvm/Analysis/MemorySSA.h"
  19. #include "llvm/Analysis/MemorySSAUpdater.h"
  20. #include "llvm/Analysis/ScalarEvolution.h"
  21. #include "llvm/Analysis/TargetTransformInfo.h"
  22. #include "llvm/InitializePasses.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Transforms/Scalar.h"
  26. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  27. #include "llvm/Transforms/Utils/LoopRotationUtils.h"
  28. #include "llvm/Transforms/Utils/LoopUtils.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "loop-rotate"
  31. static cl::opt<unsigned> DefaultRotationThreshold(
  32. "rotation-max-header-size", cl::init(16), cl::Hidden,
  33. cl::desc("The default maximum header size for automatic loop rotation"));
  34. static cl::opt<bool> PrepareForLTOOption(
  35. "rotation-prepare-for-lto", cl::init(false), cl::Hidden,
  36. cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
  37. "should be used for testing only."));
  38. LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)
  39. : EnableHeaderDuplication(EnableHeaderDuplication),
  40. PrepareForLTO(PrepareForLTO) {}
  41. PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
  42. LoopStandardAnalysisResults &AR,
  43. LPMUpdater &) {
  44. // Vectorization requires loop-rotation. Use default threshold for loops the
  45. // user explicitly marked for vectorization, even when header duplication is
  46. // disabled.
  47. int Threshold = EnableHeaderDuplication ||
  48. hasVectorizeTransformation(&L) == TM_ForcedByUser
  49. ? DefaultRotationThreshold
  50. : 0;
  51. const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
  52. const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
  53. Optional<MemorySSAUpdater> MSSAU;
  54. if (AR.MSSA)
  55. MSSAU = MemorySSAUpdater(AR.MSSA);
  56. bool Changed =
  57. LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
  58. MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ, false,
  59. Threshold, false, PrepareForLTO || PrepareForLTOOption);
  60. if (!Changed)
  61. return PreservedAnalyses::all();
  62. if (AR.MSSA && VerifyMemorySSA)
  63. AR.MSSA->verifyMemorySSA();
  64. auto PA = getLoopPassPreservedAnalyses();
  65. if (AR.MSSA)
  66. PA.preserve<MemorySSAAnalysis>();
  67. return PA;
  68. }
  69. namespace {
  70. class LoopRotateLegacyPass : public LoopPass {
  71. unsigned MaxHeaderSize;
  72. bool PrepareForLTO;
  73. public:
  74. static char ID; // Pass ID, replacement for typeid
  75. LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1,
  76. bool PrepareForLTO = false)
  77. : LoopPass(ID), PrepareForLTO(PrepareForLTO) {
  78. initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
  79. if (SpecifiedMaxHeaderSize == -1)
  80. MaxHeaderSize = DefaultRotationThreshold;
  81. else
  82. MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
  83. }
  84. // LCSSA form makes instruction renaming easier.
  85. void getAnalysisUsage(AnalysisUsage &AU) const override {
  86. AU.addRequired<AssumptionCacheTracker>();
  87. AU.addRequired<TargetTransformInfoWrapperPass>();
  88. AU.addPreserved<MemorySSAWrapperPass>();
  89. getLoopAnalysisUsage(AU);
  90. // Lazy BFI and BPI are marked as preserved here so LoopRotate
  91. // can remain part of the same loop pass manager as LICM.
  92. AU.addPreserved<LazyBlockFrequencyInfoPass>();
  93. AU.addPreserved<LazyBranchProbabilityInfoPass>();
  94. }
  95. bool runOnLoop(Loop *L, LPPassManager &LPM) override {
  96. if (skipLoop(L))
  97. return false;
  98. Function &F = *L->getHeader()->getParent();
  99. auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  100. const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  101. auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  102. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  103. auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  104. const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
  105. Optional<MemorySSAUpdater> MSSAU;
  106. // Not requiring MemorySSA and getting it only if available will split
  107. // the loop pass pipeline when LoopRotate is being run first.
  108. auto *MSSAA = getAnalysisIfAvailable<MemorySSAWrapperPass>();
  109. if (MSSAA)
  110. MSSAU = MemorySSAUpdater(&MSSAA->getMSSA());
  111. // Vectorization requires loop-rotation. Use default threshold for loops the
  112. // user explicitly marked for vectorization, even when header duplication is
  113. // disabled.
  114. int Threshold = hasVectorizeTransformation(L) == TM_ForcedByUser
  115. ? DefaultRotationThreshold
  116. : MaxHeaderSize;
  117. return LoopRotation(L, LI, TTI, AC, &DT, &SE,
  118. MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
  119. false, Threshold, false,
  120. PrepareForLTO || PrepareForLTOOption);
  121. }
  122. };
  123. } // end namespace
  124. char LoopRotateLegacyPass::ID = 0;
  125. INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
  126. false, false)
  127. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  128. INITIALIZE_PASS_DEPENDENCY(LoopPass)
  129. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  130. INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
  131. INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
  132. false)
  133. Pass *llvm::createLoopRotatePass(int MaxHeaderSize, bool PrepareForLTO) {
  134. return new LoopRotateLegacyPass(MaxHeaderSize, PrepareForLTO);
  135. }