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"
28using namespace llvm;
29
30#define DEBUG_TYPE "loop-rotate"
31
32static 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"));
35
36static cl::opt<bool> PrepareForLTOOption(
37 "rotation-prepare-for-lto", cl::init(false), cl::Hidden,
38 cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
39 "should be used for testing only."));
40
41LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)
42 : EnableHeaderDuplication(EnableHeaderDuplication),
43 PrepareForLTO(PrepareForLTO) {}
44
45PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
46 LoopStandardAnalysisResults &AR,
47 LPMUpdater &) {
48 // Vectorization requires loop-rotation. Use default threshold for loops the
49 // user explicitly marked for vectorization, even when header duplication is
50 // disabled.
51 int Threshold = EnableHeaderDuplication ||
52 hasVectorizeTransformation(&L) == TM_ForcedByUser
53 ? DefaultRotationThreshold
54 : 0;
55 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
56 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
57
58 Optional<MemorySSAUpdater> MSSAU;
59 if (AR.MSSA)
60 MSSAU = MemorySSAUpdater(AR.MSSA);
61 bool Changed =
62 LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
63 MSSAU ? MSSAU.getPointer() : 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
78namespace {
79
80class LoopRotateLegacyPass : public LoopPass {
81 unsigned MaxHeaderSize;
82 bool PrepareForLTO;
83
84public:
85 static char ID; // Pass ID, replacement for typeid
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.
97 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
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 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,
134 MSSAU ? MSSAU.getPointer() : nullptr, SQ, false,
135 Threshold, false, PrepareForLTO || PrepareForLTOOption);
136 }
137};
138} // end namespace
139
140char LoopRotateLegacyPass::ID = 0;
141INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
142 false, false)
143INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
144INITIALIZE_PASS_DEPENDENCY(LoopPass)
145INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
146INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
147INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
148 false)
149
150Pass *llvm::createLoopRotatePass(int MaxHeaderSize, bool PrepareForLTO) {
151 return new LoopRotateLegacyPass(MaxHeaderSize, PrepareForLTO);
152}
153

source code of llvm/lib/Transforms/Scalar/LoopRotation.cpp