LLVM  5.0.0
LoopPassManager.h
Go to the documentation of this file.
1 //===- LoopPassManager.h - Loop pass management -----------------*- C++ -*-===//
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 /// \file
10 ///
11 /// This header provides classes for managing a pipeline of passes over loops
12 /// in LLVM IR.
13 ///
14 /// The primary loop pass pipeline is managed in a very particular way to
15 /// provide a set of core guarantees:
16 /// 1) Loops are, where possible, in simplified form.
17 /// 2) Loops are *always* in LCSSA form.
18 /// 3) A collection of Loop-specific analysis results are available:
19 /// - LoopInfo
20 /// - DominatorTree
21 /// - ScalarEvolution
22 /// - AAManager
23 /// 4) All loop passes preserve #1 (where possible), #2, and #3.
24 /// 5) Loop passes run over each loop in the loop nest from the innermost to
25 /// the outermost. Specifically, all inner loops are processed before
26 /// passes run over outer loops. When running the pipeline across an inner
27 /// loop creates new inner loops, those are added and processed in this
28 /// order as well.
29 ///
30 /// This process is designed to facilitate transformations which simplify,
31 /// reduce, and remove loops. For passes which are more oriented towards
32 /// optimizing loops, especially optimizing loop *nests* instead of single
33 /// loops in isolation, this framework is less interesting.
34 ///
35 //===----------------------------------------------------------------------===//
36 
37 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
38 #define LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
39 
42 #include "llvm/ADT/STLExtras.h"
47 #include "llvm/Analysis/LoopInfo.h"
52 #include "llvm/IR/Dominators.h"
53 #include "llvm/IR/PassManager.h"
56 
57 namespace llvm {
58 
59 // Forward declarations of an update tracking API used in the pass manager.
60 class LPMUpdater;
61 
62 // Explicit specialization and instantiation declarations for the pass manager.
63 // See the comments on the definition of the specialization for details on how
64 // it differs from the primary template.
65 template <>
66 PreservedAnalyses
67 PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
68  LPMUpdater &>::run(Loop &InitialL, LoopAnalysisManager &AM,
69  LoopStandardAnalysisResults &AnalysisResults,
70  LPMUpdater &U);
71 extern template class PassManager<Loop, LoopAnalysisManager,
72  LoopStandardAnalysisResults &, LPMUpdater &>;
73 
74 /// \brief The Loop pass manager.
75 ///
76 /// See the documentation for the PassManager template for details. It runs
77 /// a sequence of Loop passes over each Loop that the manager is run over. This
78 /// typedef serves as a convenient way to refer to this construct.
79 typedef PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
80  LPMUpdater &>
82 
83 /// A partial specialization of the require analysis template pass to forward
84 /// the extra parameters from a transformation's run method to the
85 /// AnalysisManager's getResult.
86 template <typename AnalysisT>
89  : PassInfoMixin<
94  (void)AM.template getResult<AnalysisT>(L, AR);
95  return PreservedAnalyses::all();
96  }
97 };
98 
99 /// An alias template to easily name a require analysis loop pass.
100 template <typename AnalysisT>
102  RequireAnalysisPass<AnalysisT, Loop, LoopAnalysisManager,
104 
105 namespace internal {
106 /// Helper to implement appending of loops onto a worklist.
107 ///
108 /// We want to process loops in postorder, but the worklist is a LIFO data
109 /// structure, so we append to it in *reverse* postorder.
110 ///
111 /// For trees, a preorder traversal is a viable reverse postorder, so we
112 /// actually append using a preorder walk algorithm.
113 template <typename RangeT>
114 inline void appendLoopsToWorklist(RangeT &&Loops,
116  // We use an internal worklist to build up the preorder traversal without
117  // recursion.
118  SmallVector<Loop *, 4> PreOrderLoops, PreOrderWorklist;
119 
120  // We walk the initial sequence of loops in reverse because we generally want
121  // to visit defs before uses and the worklist is LIFO.
122  for (Loop *RootL : reverse(Loops)) {
123  assert(PreOrderLoops.empty() && "Must start with an empty preorder walk.");
124  assert(PreOrderWorklist.empty() &&
125  "Must start with an empty preorder walk worklist.");
126  PreOrderWorklist.push_back(RootL);
127  do {
128  Loop *L = PreOrderWorklist.pop_back_val();
129  PreOrderWorklist.append(L->begin(), L->end());
130  PreOrderLoops.push_back(L);
131  } while (!PreOrderWorklist.empty());
132 
133  Worklist.insert(std::move(PreOrderLoops));
134  PreOrderLoops.clear();
135  }
136 }
137 }
138 
139 template <typename LoopPassT> class FunctionToLoopPassAdaptor;
140 
141 /// This class provides an interface for updating the loop pass manager based
142 /// on mutations to the loop nest.
143 ///
144 /// A reference to an instance of this class is passed as an argument to each
145 /// Loop pass, and Loop passes should use it to update LPM infrastructure if
146 /// they modify the loop nest structure.
147 class LPMUpdater {
148 public:
149  /// This can be queried by loop passes which run other loop passes (like pass
150  /// managers) to know whether the loop needs to be skipped due to updates to
151  /// the loop nest.
152  ///
153  /// If this returns true, the loop object may have been deleted, so passes
154  /// should take care not to touch the object.
155  bool skipCurrentLoop() const { return SkipCurrentLoop; }
156 
157  /// Loop passes should use this method to indicate they have deleted a loop
158  /// from the nest.
159  ///
160  /// Note that this loop must either be the current loop or a subloop of the
161  /// current loop. This routine must be called prior to removing the loop from
162  /// the loop nest.
163  ///
164  /// If this is called for the current loop, in addition to clearing any
165  /// state, this routine will mark that the current loop should be skipped by
166  /// the rest of the pass management infrastructure.
168  LAM.clear(L);
169  assert(CurrentL->contains(&L) && "Cannot delete a loop outside of the "
170  "subloop tree currently being processed.");
171  if (&L == CurrentL)
172  SkipCurrentLoop = true;
173  }
174 
175  /// Loop passes should use this method to indicate they have added new child
176  /// loops of the current loop.
177  ///
178  /// \p NewChildLoops must contain only the immediate children. Any nested
179  /// loops within them will be visited in postorder as usual for the loop pass
180  /// manager.
181  void addChildLoops(ArrayRef<Loop *> NewChildLoops) {
182  // Insert ourselves back into the worklist first, as this loop should be
183  // revisited after all the children have been processed.
184  Worklist.insert(CurrentL);
185 
186 #ifndef NDEBUG
187  for (Loop *NewL : NewChildLoops)
188  assert(NewL->getParentLoop() == CurrentL && "All of the new loops must "
189  "be immediate children of "
190  "the current loop!");
191 #endif
192 
193  internal::appendLoopsToWorklist(NewChildLoops, Worklist);
194 
195  // Also skip further processing of the current loop--it will be revisited
196  // after all of its newly added children are accounted for.
197  SkipCurrentLoop = true;
198  }
199 
200  /// Loop passes should use this method to indicate they have added new
201  /// sibling loops to the current loop.
202  ///
203  /// \p NewSibLoops must only contain the immediate sibling loops. Any nested
204  /// loops within them will be visited in postorder as usual for the loop pass
205  /// manager.
206  void addSiblingLoops(ArrayRef<Loop *> NewSibLoops) {
207 #ifndef NDEBUG
208  for (Loop *NewL : NewSibLoops)
209  assert(NewL->getParentLoop() == ParentL &&
210  "All of the new loops must be siblings of the current loop!");
211 #endif
212 
213  internal::appendLoopsToWorklist(NewSibLoops, Worklist);
214 
215  // No need to skip the current loop or revisit it, as sibling loops
216  // shouldn't impact anything.
217  }
218 
219 private:
220  template <typename LoopPassT> friend class llvm::FunctionToLoopPassAdaptor;
221 
222  /// The \c FunctionToLoopPassAdaptor's worklist of loops to process.
224 
225  /// The analysis manager for use in the current loop nest.
226  LoopAnalysisManager &LAM;
227 
228  Loop *CurrentL;
229  bool SkipCurrentLoop;
230 
231 #ifndef NDEBUG
232  // In debug builds we also track the parent loop to implement asserts even in
233  // the face of loop deletion.
234  Loop *ParentL;
235 #endif
236 
238  LoopAnalysisManager &LAM)
239  : Worklist(Worklist), LAM(LAM) {}
240 };
241 
242 /// \brief Adaptor that maps from a function to its loops.
243 ///
244 /// Designed to allow composition of a LoopPass(Manager) and a
245 /// FunctionPassManager. Note that if this pass is constructed with a \c
246 /// FunctionAnalysisManager it will run the \c LoopAnalysisManagerFunctionProxy
247 /// analysis prior to running the loop passes over the function to enable a \c
248 /// LoopAnalysisManager to be used within this run safely.
249 template <typename LoopPassT>
250 class FunctionToLoopPassAdaptor
251  : public PassInfoMixin<FunctionToLoopPassAdaptor<LoopPassT>> {
252 public:
253  explicit FunctionToLoopPassAdaptor(LoopPassT Pass) : Pass(std::move(Pass)) {
254  LoopCanonicalizationFPM.addPass(LoopSimplifyPass());
255  LoopCanonicalizationFPM.addPass(LCSSAPass());
256  }
257 
258  /// \brief Runs the loop passes across every loop in the function.
260  // Before we even compute any loop analyses, first run a miniature function
261  // pass pipeline to put loops into their canonical form. Note that we can
262  // directly build up function analyses after this as the function pass
263  // manager handles all the invalidation at that layer.
264  PreservedAnalyses PA = LoopCanonicalizationFPM.run(F, AM);
265 
266  // Get the loop structure for this function
267  LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
268 
269  // If there are no loops, there is nothing to do here.
270  if (LI.empty())
271  return PA;
272 
273  // Get the analysis results needed by loop passes.
277  AM.getResult<LoopAnalysis>(F),
281 
282  // Setup the loop analysis manager from its proxy. It is important that
283  // this is only done when there are loops to process and we have built the
284  // LoopStandardAnalysisResults object. The loop analyses cached in this
285  // manager have access to those analysis results and so it must invalidate
286  // itself when they go away.
287  LoopAnalysisManager &LAM =
288  AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager();
289 
290  // A postorder worklist of loops to process.
292 
293  // Register the worklist and loop analysis manager so that loop passes can
294  // update them when they mutate the loop nest structure.
295  LPMUpdater Updater(Worklist, LAM);
296 
297  // Add the loop nests in the reverse order of LoopInfo. For some reason,
298  // they are stored in RPO w.r.t. the control flow graph in LoopInfo. For
299  // the purpose of unrolling, loop deletion, and LICM, we largely want to
300  // work forward across the CFG so that we visit defs before uses and can
301  // propagate simplifications from one loop nest into the next.
302  // FIXME: Consider changing the order in LoopInfo.
304 
305  do {
306  Loop *L = Worklist.pop_back_val();
307 
308  // Reset the update structure for this loop.
309  Updater.CurrentL = L;
310  Updater.SkipCurrentLoop = false;
311 
312 #ifndef NDEBUG
313  // Save a parent loop pointer for asserts.
314  Updater.ParentL = L->getParentLoop();
315 
316  // Verify the loop structure and LCSSA form before visiting the loop.
317  L->verifyLoop();
318  assert(L->isRecursivelyLCSSAForm(LAR.DT, LI) &&
319  "Loops must remain in LCSSA form!");
320 #endif
321 
322  PreservedAnalyses PassPA = Pass.run(*L, LAM, LAR, Updater);
323  // FIXME: We should verify the set of analyses relevant to Loop passes
324  // are preserved.
325 
326  // If the loop hasn't been deleted, we need to handle invalidation here.
327  if (!Updater.skipCurrentLoop())
328  // We know that the loop pass couldn't have invalidated any other
329  // loop's analyses (that's the contract of a loop pass), so directly
330  // handle the loop analysis manager's invalidation here.
331  LAM.invalidate(*L, PassPA);
332 
333  // Then intersect the preserved set so that invalidation of module
334  // analyses will eventually occur when the module pass completes.
335  PA.intersect(std::move(PassPA));
336  } while (!Worklist.empty());
337 
338  // By definition we preserve the proxy. We also preserve all analyses on
339  // Loops. This precludes *any* invalidation of loop analyses by the proxy,
340  // but that's OK because we've taken care to invalidate analyses in the
341  // loop analysis manager incrementally above.
344  // We also preserve the set of standard analyses.
346  PA.preserve<LoopAnalysis>();
348  // FIXME: What we really want to do here is preserve an AA category, but
349  // that concept doesn't exist yet.
350  PA.preserve<AAManager>();
351  PA.preserve<BasicAA>();
352  PA.preserve<GlobalsAA>();
353  PA.preserve<SCEVAA>();
354  return PA;
355  }
356 
357 private:
358  LoopPassT Pass;
359 
360  FunctionPassManager LoopCanonicalizationFPM;
361 };
362 
363 /// \brief A function to deduce a loop pass type and wrap it in the templated
364 /// adaptor.
365 template <typename LoopPassT>
368  return FunctionToLoopPassAdaptor<LoopPassT>(std::move(Pass));
369 }
370 
371 /// \brief Pass for printing a loop's contents as textual IR.
372 class PrintLoopPass : public PassInfoMixin<PrintLoopPass> {
373  raw_ostream &OS;
374  std::string Banner;
375 
376 public:
377  PrintLoopPass();
378  PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
379 
382 };
383 }
384 
385 #endif // LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
This file provides a priority worklist.
Converts loops into loop-closed SSA form.
Definition: LCSSA.h:38
bool empty() const
Definition: LoopInfo.h:564
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs...ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Definition: PassManager.h:444
bool insert(const T &X)
Insert a new element into the PriorityWorklist.
void intersect(const PreservedAnalyses &Arg)
Intersect this set with another in place.
Definition: PassManager.h:225
This is the interface for a simple mod/ref and alias analysis over globals.
This pass is responsible for loop canonicalization.
Definition: LoopSimplify.h:50
Analysis pass providing the TargetTransformInfo.
LoopT * getParentLoop() const
Definition: LoopInfo.h:104
RequireAnalysisPass< AnalysisT, Loop, LoopAnalysisManager, LoopStandardAnalysisResults &, LPMUpdater & > RequireAnalysisLoopPass
An alias template to easily name a require analysis loop pass.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:232
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &)
A utility pass template to force an analysis result to be available.
Definition: PassManager.h:1246
Hexagon Hardware Loops
This is the interface for a SCEV-based alias analysis.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:820
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:250
#define F(x, y, z)
Definition: MD5.cpp:55
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Adaptor that maps from a function to its loops.
This header provides classes for managing per-loop analyses.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void verifyLoop() const
Verify loop structure.
Definition: LoopInfoImpl.h:215
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
iterator begin() const
Definition: LoopInfo.h:133
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
FunctionToLoopPassAdaptor(LoopPassT Pass)
void addChildLoops(ArrayRef< Loop * > NewChildLoops)
Loop passes should use this method to indicate they have added new child loops of the current loop...
A manager for alias analyses.
bool isRecursivelyLCSSAForm(DominatorTree &DT, const LoopInfo &LI) const
Return true if this Loop and all inner subloops are in LCSSA form.
Definition: LoopInfo.cpp:181
iterator end() const
Definition: LoopInfo.h:134
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
Analysis pass providing a never-invalidated alias analysis result.
Analysis pass providing a never-invalidated alias analysis result.
A version of PriorityWorklist that selects small size optimized data structures for the vector and ma...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
void addSiblingLoops(ArrayRef< Loop * > NewSibLoops)
Loop passes should use this method to indicate they have added new sibling loops to the current loop...
FunctionToLoopPassAdaptor< LoopPassT > createFunctionToLoopPassAdaptor(LoopPassT Pass)
A function to deduce a loop pass type and wrap it in the templated adaptor.
A function analysis which provides an AssumptionCache.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Pass for printing a loop's contents as textual IR.
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Runs the loop passes across every loop in the function.
Analysis pass that exposes the ScalarEvolution for a function.
Analysis pass providing a never-invalidated alias analysis result.
PassManager< Loop, LoopAnalysisManager, LoopStandardAnalysisResults &, LPMUpdater & > LoopPassManager
The Loop pass manager.
void clear(IRUnitT &IR)
Clear any cached analysis results for a single unit of IR.
Definition: PassManager.h:657
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:360
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:189
void appendLoopsToWorklist(RangeT &&Loops, SmallPriorityWorklist< Loop *, 4 > &Worklist)
Helper to implement appending of loops onto a worklist.
bool skipCurrentLoop() const
This can be queried by loop passes which run other loop passes (like pass managers) to know whether t...
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:174
Analysis pass providing the TargetLibraryInfo.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over \<a particular IR unit\>" (e...
Definition: PassManager.h:91
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
This is the interface for LLVM's primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
void addPass(PassT Pass)
Definition: PassManager.h:485
This pass exposes codegen information to IR-level passes.
This header defines various interfaces for pass management in LLVM.
void markLoopAsDeleted(Loop &L)
Loop passes should use this method to indicate they have deleted a loop from the nest.
bool empty() const
Determine if the PriorityWorklist is empty or not.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:946