LoopRotation.cpp 5.9 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/ADT/Statistic.h"
  14. #include "llvm/Analysis/AssumptionCache.h"
  15. #include "llvm/Analysis/InstructionSimplify.h"
  16. #include "llvm/Analysis/LoopPass.h"
  17. #include "llvm/Analysis/MemorySSA.h"
  18. #include "llvm/Analysis/MemorySSAUpdater.h"
  19. #include "llvm/Analysis/ScalarEvolution.h"
  20. #include "llvm/Analysis/TargetTransformInfo.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Transforms/Scalar.h"
  25. #include "llvm/Transforms/Scalar/LoopPassManager.h"
  26. #include "llvm/Transforms/Utils/LoopRotationUtils.h"
  27. #include "llvm/Transforms/Utils/LoopUtils.h"
  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. Optional<MemorySSAUpdater> MSSAU;
  53. if (AR.MSSA)
  54. MSSAU = MemorySSAUpdater(AR.MSSA);
  55. bool Changed =
  56. LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
  57. MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ, false,
  58. Threshold, false, PrepareForLTO || PrepareForLTOOption);
  59. if (!Changed)
  60. return PreservedAnalyses::all();
  61. if (AR.MSSA && VerifyMemorySSA)
  62. AR.MSSA->verifyMemorySSA();
  63. auto PA = getLoopPassPreservedAnalyses();
  64. if (AR.MSSA)
  65. PA.preserve<MemorySSAAnalysis>();
  66. return PA;
  67. }
  68. namespace {
  69. class LoopRotateLegacyPass : public LoopPass {
  70. unsigned MaxHeaderSize;
  71. bool PrepareForLTO;
  72. public:
  73. static char ID; // Pass ID, replacement for typeid
  74. LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1,
  75. bool PrepareForLTO = false)
  76. : LoopPass(ID), PrepareForLTO(PrepareForLTO) {
  77. initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
  78. if (SpecifiedMaxHeaderSize == -1)
  79. MaxHeaderSize = DefaultRotationThreshold;
  80. else
  81. MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
  82. }
  83. // LCSSA form makes instruction renaming easier.
  84. void getAnalysisUsage(AnalysisUsage &AU) const override {
  85. AU.addRequired<AssumptionCacheTracker>();
  86. AU.addRequired<TargetTransformInfoWrapperPass>();
  87. if (EnableMSSALoopDependency)
  88. AU.addPreserved<MemorySSAWrapperPass>();
  89. getLoopAnalysisUsage(AU);
  90. }
  91. bool runOnLoop(Loop *L, LPPassManager &LPM) override {
  92. if (skipLoop(L))
  93. return false;
  94. Function &F = *L->getHeader()->getParent();
  95. auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  96. const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  97. auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  98. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  99. auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  100. const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
  101. Optional<MemorySSAUpdater> MSSAU;
  102. if (EnableMSSALoopDependency) {
  103. // Not requiring MemorySSA and getting it only if available will split
  104. // the loop pass pipeline when LoopRotate is being run first.
  105. auto *MSSAA = getAnalysisIfAvailable<MemorySSAWrapperPass>();
  106. if (MSSAA)
  107. MSSAU = MemorySSAUpdater(&MSSAA->getMSSA());
  108. }
  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,
  116. MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
  117. false, Threshold, false,
  118. PrepareForLTO || PrepareForLTOOption);
  119. }
  120. };
  121. } // end namespace
  122. char LoopRotateLegacyPass::ID = 0;
  123. INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
  124. false, false)
  125. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  126. INITIALIZE_PASS_DEPENDENCY(LoopPass)
  127. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  128. INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
  129. INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
  130. false)
  131. Pass *llvm::createLoopRotatePass(int MaxHeaderSize, bool PrepareForLTO) {
  132. return new LoopRotateLegacyPass(MaxHeaderSize, PrepareForLTO);
  133. }