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