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/Debug.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Scalar/LoopPassManager.h"
25 #include "llvm/Transforms/Utils/LoopRotationUtils.h"
26 #include "llvm/Transforms/Utils/LoopUtils.h"
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "loop-rotate"
30 
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 
35 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
36     : EnableHeaderDuplication(EnableHeaderDuplication) {}
37 
38 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
39                                       LoopStandardAnalysisResults &AR,
40                                       LPMUpdater &) {
41   int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
42   const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
43   const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
44 
45   Optional<MemorySSAUpdater> MSSAU;
46   if (AR.MSSA)
47     MSSAU = MemorySSAUpdater(AR.MSSA);
48   bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
49                               MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
50                               SQ, false, Threshold, false);
51 
52   if (!Changed)
53     return PreservedAnalyses::all();
54 
55   if (AR.MSSA && VerifyMemorySSA)
56     AR.MSSA->verifyMemorySSA();
57 
58   auto PA = getLoopPassPreservedAnalyses();
59   if (AR.MSSA)
60     PA.preserve<MemorySSAAnalysis>();
61   return PA;
62 }
63 
64 namespace {
65 
66 class LoopRotateLegacyPass : public LoopPass {
67   unsigned MaxHeaderSize;
68 
69 public:
70   static char ID; // Pass ID, replacement for typeid
71   LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
72     initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
73     if (SpecifiedMaxHeaderSize == -1)
74       MaxHeaderSize = DefaultRotationThreshold;
75     else
76       MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
77   }
78 
79   // LCSSA form makes instruction renaming easier.
80   void getAnalysisUsage(AnalysisUsage &AU) const override {
81     AU.addRequired<AssumptionCacheTracker>();
82     AU.addRequired<TargetTransformInfoWrapperPass>();
83     if (EnableMSSALoopDependency) {
84       AU.addRequired<MemorySSAWrapperPass>();
85       AU.addPreserved<MemorySSAWrapperPass>();
86     }
87     getLoopAnalysisUsage(AU);
88   }
89 
90   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
91     if (skipLoop(L))
92       return false;
93     Function &F = *L->getHeader()->getParent();
94 
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       MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
104       MSSAU = MemorySSAUpdater(MSSA);
105     }
106     return LoopRotation(L, LI, TTI, AC, &DT, &SE,
107                         MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
108                         false, MaxHeaderSize, false);
109   }
110 };
111 }
112 
113 char LoopRotateLegacyPass::ID = 0;
114 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
115                       false, false)
116 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
117 INITIALIZE_PASS_DEPENDENCY(LoopPass)
118 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
119 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
120 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
121                     false)
122 
123 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
124   return new LoopRotateLegacyPass(MaxHeaderSize);
125 }
126