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/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"
30 #define DEBUG_TYPE "loop-rotate"
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"));
36 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
37 : EnableHeaderDuplication(EnableHeaderDuplication) {}
39 PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
40 LoopStandardAnalysisResults &AR,
42 int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
43 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
44 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
46 Optional<MemorySSAUpdater> MSSAU;
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);
54 return PreservedAnalyses::all();
56 if (AR.MSSA && VerifyMemorySSA)
57 AR.MSSA->verifyMemorySSA();
59 auto PA = getLoopPassPreservedAnalyses();
61 PA.preserve<MemorySSAAnalysis>();
67 class LoopRotateLegacyPass : public LoopPass {
68 unsigned MaxHeaderSize;
71 static char ID; // Pass ID, replacement for typeid
72 LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
73 initializeLoopRotateLegacyPassPass(*PassRegistry::getPassRegistry());
74 if (SpecifiedMaxHeaderSize == -1)
75 MaxHeaderSize = DefaultRotationThreshold;
77 MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
80 // LCSSA form makes instruction renaming easier.
81 void getAnalysisUsage(AnalysisUsage &AU) const override {
82 AU.addRequired<AssumptionCacheTracker>();
83 AU.addRequired<TargetTransformInfoWrapperPass>();
84 if (EnableMSSALoopDependency)
85 AU.addPreserved<MemorySSAWrapperPass>();
86 getLoopAnalysisUsage(AU);
89 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
92 Function &F = *L->getHeader()->getParent();
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>();
106 MSSAU = MemorySSAUpdater(&MSSAA->getMSSA());
108 return LoopRotation(L, LI, TTI, AC, &DT, &SE,
109 MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
110 false, MaxHeaderSize, false);
115 char LoopRotateLegacyPass::ID = 0;
116 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
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,
125 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
126 return new LoopRotateLegacyPass(MaxHeaderSize);