LLVM  10.0.0
LoopRotation.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/InitializePasses.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Transforms/Scalar.h"
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "loop-rotate"
31 
33  "rotation-max-header-size", cl::init(16), cl::Hidden,
34  cl::desc("The default maximum header size for automatic loop rotation"));
35 
36 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
37  : EnableHeaderDuplication(EnableHeaderDuplication) {}
38 
41  LPMUpdater &) {
42  int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
43  const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
44  const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
45 
47  if (AR.MSSA)
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);
52 
53  if (!Changed)
54  return PreservedAnalyses::all();
55 
56  if (AR.MSSA && VerifyMemorySSA)
57  AR.MSSA->verifyMemorySSA();
58 
59  auto PA = getLoopPassPreservedAnalyses();
60  if (AR.MSSA)
61  PA.preserve<MemorySSAAnalysis>();
62  return PA;
63 }
64 
65 namespace {
66 
67 class LoopRotateLegacyPass : public LoopPass {
68  unsigned MaxHeaderSize;
69 
70 public:
71  static char ID; // Pass ID, replacement for typeid
72  LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
74  if (SpecifiedMaxHeaderSize == -1)
75  MaxHeaderSize = DefaultRotationThreshold;
76  else
77  MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
78  }
79 
80  // LCSSA form makes instruction renaming easier.
81  void getAnalysisUsage(AnalysisUsage &AU) const override {
87  }
88 
89  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
90  if (skipLoop(L))
91  return false;
92  Function &F = *L->getHeader()->getParent();
93 
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);
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>();
105  if (MSSAA)
106  MSSAU = MemorySSAUpdater(&MSSAA->getMSSA());
107  }
108  return LoopRotation(L, LI, TTI, AC, &DT, &SE,
109  MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
110  false, MaxHeaderSize, false);
111  }
112 };
113 }
114 
115 char LoopRotateLegacyPass::ID = 0;
116 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
117  false, false)
122 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
123  false)
124 
125 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
126  return new LoopRotateLegacyPass(MaxHeaderSize);
127 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:78
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:84
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
An immutable pass that tracks lazily created AssumptionCache objects.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
F(f)
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:140
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:964
INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false, false) INITIALIZE_PASS_END(LoopRotateLegacyPass
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
BlockT * getHeader() const
Definition: LoopInfo.h:105
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
Wrapper pass for TargetTransformInfo.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
Represent the analysis usage information of a pass.
const T * getPointer() const
Definition: Optional.h:253
bool LoopRotation(Loop *L, LoopInfo *LI, const TargetTransformInfo *TTI, AssumptionCache *AC, DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU, const SimplifyQuery &SQ, bool RotationOnly, unsigned Threshold, bool IsUtilMode)
Convert a loop into a loop with bottom test.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
void initializeLoopRotateLegacyPassPass(PassRegistry &)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
loop Rotate Loops
void verifyMemorySSA() const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1872
loop rotate
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:925
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
bool hasValue() const
Definition: Optional.h:259
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:509
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass&#39;s AnalysisUsage.
Definition: LoopUtils.cpp:138
LoopRotatePass(bool EnableHeaderDuplication=true)
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
A container for analyses that lazily runs them and caches their results.
This pass exposes codegen information to IR-level passes.
Pass * createLoopRotatePass(int MaxHeaderSize=-1)
static cl::opt< unsigned > DefaultRotationThreshold("rotation-max-header-size", cl::init(16), cl::Hidden, cl::desc("The default maximum header size for automatic loop rotation"))