LoopRotation.cpp 6.0 KB

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