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/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/Support/Debug.h"
22 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Transforms/Scalar/LoopPassManager.h"
24 #include "llvm/Transforms/Utils/LoopRotationUtils.h"
25 #include "llvm/Transforms/Utils/LoopUtils.h"
28 #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"));
34 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication
)
35 : EnableHeaderDuplication(EnableHeaderDuplication
) {}
37 PreservedAnalyses
LoopRotatePass::run(Loop
&L
, LoopAnalysisManager
&AM
,
38 LoopStandardAnalysisResults
&AR
,
40 int Threshold
= EnableHeaderDuplication
? DefaultRotationThreshold
: 0;
41 const DataLayout
&DL
= L
.getHeader()->getModule()->getDataLayout();
42 const SimplifyQuery SQ
= getBestSimplifyQuery(AR
, DL
);
44 Optional
<MemorySSAUpdater
> MSSAU
;
46 MSSAU
= MemorySSAUpdater(AR
.MSSA
);
47 bool Changed
= LoopRotation(&L
, &AR
.LI
, &AR
.TTI
, &AR
.AC
, &AR
.DT
, &AR
.SE
,
48 MSSAU
.hasValue() ? MSSAU
.getPointer() : nullptr,
49 SQ
, false, Threshold
, false);
52 return PreservedAnalyses::all();
54 if (AR
.MSSA
&& VerifyMemorySSA
)
55 AR
.MSSA
->verifyMemorySSA();
57 return getLoopPassPreservedAnalyses();
62 class LoopRotateLegacyPass
: public LoopPass
{
63 unsigned MaxHeaderSize
;
66 static char ID
; // Pass ID, replacement for typeid
67 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize
= -1) : LoopPass(ID
) {
68 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
69 if (SpecifiedMaxHeaderSize
== -1)
70 MaxHeaderSize
= DefaultRotationThreshold
;
72 MaxHeaderSize
= unsigned(SpecifiedMaxHeaderSize
);
75 // LCSSA form makes instruction renaming easier.
76 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
77 AU
.addRequired
<AssumptionCacheTracker
>();
78 AU
.addRequired
<TargetTransformInfoWrapperPass
>();
79 if (EnableMSSALoopDependency
) {
80 AU
.addRequired
<MemorySSAWrapperPass
>();
81 AU
.addPreserved
<MemorySSAWrapperPass
>();
83 getLoopAnalysisUsage(AU
);
86 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
{
89 Function
&F
= *L
->getHeader()->getParent();
91 auto *LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
92 const auto *TTI
= &getAnalysis
<TargetTransformInfoWrapperPass
>().getTTI(F
);
93 auto *AC
= &getAnalysis
<AssumptionCacheTracker
>().getAssumptionCache(F
);
94 auto *DTWP
= getAnalysisIfAvailable
<DominatorTreeWrapperPass
>();
95 auto *DT
= DTWP
? &DTWP
->getDomTree() : nullptr;
96 auto *SEWP
= getAnalysisIfAvailable
<ScalarEvolutionWrapperPass
>();
97 auto *SE
= SEWP
? &SEWP
->getSE() : nullptr;
98 const SimplifyQuery SQ
= getBestSimplifyQuery(*this, F
);
99 Optional
<MemorySSAUpdater
> MSSAU
;
100 if (EnableMSSALoopDependency
) {
101 MemorySSA
*MSSA
= &getAnalysis
<MemorySSAWrapperPass
>().getMSSA();
102 MSSAU
= MemorySSAUpdater(MSSA
);
104 return LoopRotation(L
, LI
, TTI
, AC
, DT
, SE
,
105 MSSAU
.hasValue() ? MSSAU
.getPointer() : nullptr, SQ
,
106 false, MaxHeaderSize
, false);
111 char LoopRotateLegacyPass::ID
= 0;
112 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass
, "loop-rotate", "Rotate Loops",
114 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker
)
115 INITIALIZE_PASS_DEPENDENCY(LoopPass
)
116 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass
)
117 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass
)
118 INITIALIZE_PASS_END(LoopRotateLegacyPass
, "loop-rotate", "Rotate Loops", false,
121 Pass
*llvm::createLoopRotatePass(int MaxHeaderSize
) {
122 return new LoopRotateLegacyPass(MaxHeaderSize
);