LLVM  8.0.1
LoopRotation.cpp
Go to the documentation of this file.
1 //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Loop Rotation Pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Transforms/Scalar.h"
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "loop-rotate"
30 
32  "rotation-max-header-size", cl::init(16), cl::Hidden,
33  cl::desc("The default maximum header size for automatic loop rotation"));
34 
35 LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication)
36  : EnableHeaderDuplication(EnableHeaderDuplication) {}
37 
40  LPMUpdater &) {
41  int Threshold = EnableHeaderDuplication ? DefaultRotationThreshold : 0;
42  const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
43  const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);
44 
46  if (AR.MSSA)
47  MSSAU = MemorySSAUpdater(AR.MSSA);
48  bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
49  MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
50  SQ, false, Threshold, false);
51 
52  if (!Changed)
53  return PreservedAnalyses::all();
54 
55  if (AR.MSSA && VerifyMemorySSA)
56  AR.MSSA->verifyMemorySSA();
57 
59 }
60 
61 namespace {
62 
63 class LoopRotateLegacyPass : public LoopPass {
64  unsigned MaxHeaderSize;
65 
66 public:
67  static char ID; // Pass ID, replacement for typeid
68  LoopRotateLegacyPass(int SpecifiedMaxHeaderSize = -1) : LoopPass(ID) {
70  if (SpecifiedMaxHeaderSize == -1)
71  MaxHeaderSize = DefaultRotationThreshold;
72  else
73  MaxHeaderSize = unsigned(SpecifiedMaxHeaderSize);
74  }
75 
76  // LCSSA form makes instruction renaming easier.
77  void getAnalysisUsage(AnalysisUsage &AU) const override {
83  }
85  }
86 
87  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
88  if (skipLoop(L))
89  return false;
90  Function &F = *L->getHeader()->getParent();
91 
92  auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
93  const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
94  auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
95  auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
96  auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
97  auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
98  auto *SE = SEWP ? &SEWP->getSE() : nullptr;
99  const SimplifyQuery SQ = getBestSimplifyQuery(*this, F);
102  MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
103  MSSAU = MemorySSAUpdater(MSSA);
104  }
105  return LoopRotation(L, LI, TTI, AC, DT, SE,
106  MSSAU.hasValue() ? MSSAU.getPointer() : nullptr, SQ,
107  false, MaxHeaderSize, false);
108  }
109 };
110 }
111 
112 char LoopRotateLegacyPass::ID = 0;
113 INITIALIZE_PASS_BEGIN(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops",
114  false, false)
119 INITIALIZE_PASS_END(LoopRotateLegacyPass, "loop-rotate", "Rotate Loops", false,
120  false)
121 
122 Pass *llvm::createLoopRotatePass(int MaxHeaderSize) {
123  return new LoopRotateLegacyPass(MaxHeaderSize);
124 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
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:83
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:134
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:956
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:371
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:701
BlockT * getHeader() const
Definition: LoopInfo.h:100
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
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:153
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:1776
loop rotate
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:165
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:465
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:107
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass&#39;s AnalysisUsage.
Definition: LoopUtils.cpp:132
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"))
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.