1 //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements Loop Rotation Pass.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Scalar/LoopRotation.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/AssumptionCache.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/LazyBlockFrequencyInfo.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/Support/Debug.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Scalar/LoopPassManager.h"
28 #include "llvm/Transforms/Utils/LoopRotationUtils.h"
29 #include "llvm/Transforms/Utils/LoopUtils.h"
32 #define DEBUG_TYPE "loop-rotate"
34 static cl::opt
<unsigned> DefaultRotationThreshold(
35 "rotation-max-header-size", cl::init(16), cl::Hidden
,
36 cl::desc("The default maximum header size for automatic loop rotation"));
38 static cl::opt
<bool> PrepareForLTOOption(
39 "rotation-prepare-for-lto", cl::init(false), cl::Hidden
,
40 cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
41 "should be used for testing only."));
43 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication
, bool PrepareForLTO
)
44 : EnableHeaderDuplication(EnableHeaderDuplication
),
45 PrepareForLTO(PrepareForLTO
) {}
47 PreservedAnalyses
LoopRotatePass::run(Loop
&L
, LoopAnalysisManager
&AM
,
48 LoopStandardAnalysisResults
&AR
,
50 // Vectorization requires loop-rotation. Use default threshold for loops the
51 // user explicitly marked for vectorization, even when header duplication is
53 int Threshold
= EnableHeaderDuplication
||
54 hasVectorizeTransformation(&L
) == TM_ForcedByUser
55 ? DefaultRotationThreshold
57 const DataLayout
&DL
= L
.getHeader()->getModule()->getDataLayout();
58 const SimplifyQuery SQ
= getBestSimplifyQuery(AR
, DL
);
60 Optional
<MemorySSAUpdater
> MSSAU
;
62 MSSAU
= MemorySSAUpdater(AR
.MSSA
);
64 LoopRotation(&L
, &AR
.LI
, &AR
.TTI
, &AR
.AC
, &AR
.DT
, &AR
.SE
,
65 MSSAU
.hasValue() ? MSSAU
.getPointer() : nullptr, SQ
, false,
66 Threshold
, false, PrepareForLTO
|| PrepareForLTOOption
);
69 return PreservedAnalyses::all();
71 if (AR
.MSSA
&& VerifyMemorySSA
)
72 AR
.MSSA
->verifyMemorySSA();
74 auto PA
= getLoopPassPreservedAnalyses();
76 PA
.preserve
<MemorySSAAnalysis
>();
82 class LoopRotateLegacyPass
: public LoopPass
{
83 unsigned MaxHeaderSize
;
87 static char ID
; // Pass ID, replacement for typeid
88 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize
= -1,
89 bool PrepareForLTO
= false)
90 : LoopPass(ID
), PrepareForLTO(PrepareForLTO
) {
91 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
92 if (SpecifiedMaxHeaderSize
== -1)
93 MaxHeaderSize
= DefaultRotationThreshold
;
95 MaxHeaderSize
= unsigned(SpecifiedMaxHeaderSize
);
98 // LCSSA form makes instruction renaming easier.
99 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
100 AU
.addRequired
<AssumptionCacheTracker
>();
101 AU
.addRequired
<TargetTransformInfoWrapperPass
>();
102 AU
.addPreserved
<MemorySSAWrapperPass
>();
103 getLoopAnalysisUsage(AU
);
105 // Lazy BFI and BPI are marked as preserved here so LoopRotate
106 // can remain part of the same loop pass manager as LICM.
107 AU
.addPreserved
<LazyBlockFrequencyInfoPass
>();
108 AU
.addPreserved
<LazyBranchProbabilityInfoPass
>();
111 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
{
114 Function
&F
= *L
->getHeader()->getParent();
116 auto *LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
117 const auto *TTI
= &getAnalysis
<TargetTransformInfoWrapperPass
>().getTTI(F
);
118 auto *AC
= &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
119 auto &DT
= getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
120 auto &SE
= getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
121 const SimplifyQuery SQ
= getBestSimplifyQuery(*this, F
);
122 Optional
<MemorySSAUpdater
> MSSAU
;
123 // Not requiring MemorySSA and getting it only if available will split
124 // the loop pass pipeline when LoopRotate is being run first.
125 auto *MSSAA
= getAnalysisIfAvailable
<MemorySSAWrapperPass
>();
127 MSSAU
= MemorySSAUpdater(&MSSAA
->getMSSA());
128 // Vectorization requires loop-rotation. Use default threshold for loops the
129 // user explicitly marked for vectorization, even when header duplication is
131 int Threshold
= hasVectorizeTransformation(L
) == TM_ForcedByUser
132 ? DefaultRotationThreshold
135 return LoopRotation(L
, LI
, TTI
, AC
, &DT
, &SE
,
136 MSSAU
.hasValue() ? MSSAU
.getPointer() : nullptr, SQ
,
137 false, Threshold
, false,
138 PrepareForLTO
|| PrepareForLTOOption
);
143 char LoopRotateLegacyPass::ID
= 0;
144 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass
, "loop-rotate", "Rotate Loops",
146 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
147 INITIALIZE_PASS_DEPENDENCY(LoopPass
)
148 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass
)
149 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass
)
150 INITIALIZE_PASS_END(LoopRotateLegacyPass
, "loop-rotate", "Rotate Loops", false,
153 Pass
*llvm::createLoopRotatePass(int MaxHeaderSize
, bool PrepareForLTO
) {
154 return new LoopRotateLegacyPass(MaxHeaderSize
, PrepareForLTO
);