• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include "llvm/Transforms/Scalar/LoopRotation.h"
14 #include "llvm/ADT/Statistic.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 
30 #define DEBUG_TYPE "loop-rotate"
31 
32 static cl::opt<unsigned> DefaultRotationThreshold(
33     "rotation-max-header-size", cl::init(16), cl::Hidden,
34     cl::desc("The default maximum header size for automatic loop rotation"));
35 
LoopRotatePass(bool EnableHeaderDuplication)36 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
37     : EnableHeaderDuplication(EnableHeaderDuplication) {}
38 
run(Loop & L,LoopAnalysisManager & AM,LoopStandardAnalysisResults & AR,LPMUpdater &)39 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
40                                       LoopStandardAnalysisResults &AR,
41                                       LPMUpdater &) {
42   int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
43   const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
44   const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
45 
46   Optional<MemorySSAUpdater> MSSAU;
47   if (AR.MSSA)
48     MSSAU = MemorySSAUpdater(AR.MSSA);
49   bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
50                               MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
51                               SQ, false, Threshold, false);
52 
53   if (!Changed)
54     return PreservedAnalyses::all();
55 
56   if (AR.MSSA && VerifyMemorySSA)
57     AR.MSSA->verifyMemorySSA();
58 
59   auto PA = getLoopPassPreservedAnalyses();
60   if (AR.MSSA)
61     PA.preserve<MemorySSAAnalysis>();
62   return PA;
63 }
64 
65 namespace {
66 
67 class LoopRotateLegacyPass : public LoopPass {
68   unsigned MaxHeaderSize;
69 
70 public:
71   static char ID; // Pass ID, replacement for typeid
LoopRotateLegacyPass(int SpecifiedMaxHeaderSize=-1)72   LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
73     initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
74     if (SpecifiedMaxHeaderSize == -1)
75       MaxHeaderSize = DefaultRotationThreshold;
76     else
77       MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
78   }
79 
80   // LCSSA form makes instruction renaming easier.
getAnalysisUsage(AnalysisUsage & AU) const81   void getAnalysisUsage(AnalysisUsage &AU) const override {
82     AU.addRequired<AssumptionCacheTracker>();
83     AU.addRequired<TargetTransformInfoWrapperPass>();
84     if (EnableMSSALoopDependency)
85       AU.addPreserved<MemorySSAWrapperPass>();
86     getLoopAnalysisUsage(AU);
87   }
88 
runOnLoop(Loop * L,LPPassManager & LPM)89   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
90     if (skipLoop(L))
91       return false;
92     Function &F = *L->getHeader()->getParent();
93 
94     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
95     const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
96     auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
97     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
98     auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
99     const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
100     Optional<MemorySSAUpdater> MSSAU;
101     if (EnableMSSALoopDependency) {
102       // Not requiring MemorySSA and getting it only if available will split
103       // the loop pass pipeline when LoopRotate is being run first.
104       auto *MSSAA = getAnalysisIfAvailable<MemorySSAWrapperPass>();
105       if (MSSAA)
106         MSSAU = MemorySSAUpdater(&MSSAA->getMSSA());
107     }
108     return LoopRotation(L, LI, TTI, AC, &DT, &SE,
109                         MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
110                         false, MaxHeaderSize, false);
111   }
112 };
113 }
114 
115 char LoopRotateLegacyPass::ID = 0;
116 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
117                       false, false)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)118 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
119 INITIALIZE_PASS_DEPENDENCY(LoopPass)
120 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
121 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
122 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
123                     false)
124 
125 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
126   return new LoopRotateLegacyPass(MaxHeaderSize);
127 }
128