LoopPass.h revision 90c579de5a383cee278acc3f7e7b9d0a656e6a35
1//===- LoopPass.h - LoopPass class ----------------------------------------===//
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 defines LoopPass class. All loop optimization
11// and transformation passes are derived from LoopPass.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LOOP_PASS_H
16#define LLVM_LOOP_PASS_H
17
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Pass.h"
20#include "llvm/PassManagers.h"
21#include "llvm/Function.h"
22
23namespace llvm {
24
25class LPPassManager;
26class Function;
27class PMStack;
28
29class LoopPass : public Pass {
30public:
31  explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {}
32
33  /// getPrinterPass - Get a pass to print the function corresponding
34  /// to a Loop.
35  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
36
37  // runOnLoop - This method should be implemented by the subclass to perform
38  // whatever action is necessary for the specified Loop.
39  virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0;
40
41  // Initialization and finalization hooks.
42  virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
43    return false;
44  }
45
46  // Finalization hook does not supply Loop because at this time
47  // loop nest is completely different.
48  virtual bool doFinalization() { return false; }
49
50  // Check if this pass is suitable for the current LPPassManager, if
51  // available. This pass P is not suitable for a LPPassManager if P
52  // is not preserving higher level analysis info used by other
53  // LPPassManager passes. In such case, pop LPPassManager from the
54  // stack. This will force assignPassManager() to create new
55  // LPPassManger as expected.
56  void preparePassManager(PMStack &PMS);
57
58  /// Assign pass manager to manage this pass
59  virtual void assignPassManager(PMStack &PMS,
60                                 PassManagerType PMT = PMT_LoopPassManager);
61
62  ///  Return what kind of Pass Manager can manage this pass.
63  virtual PassManagerType getPotentialPassManagerType() const {
64    return PMT_LoopPassManager;
65  }
66
67  //===--------------------------------------------------------------------===//
68  /// SimpleAnalysis - Provides simple interface to update analysis info
69  /// maintained by various passes. Note, if required this interface can
70  /// be extracted into a separate abstract class but it would require
71  /// additional use of multiple inheritance in Pass class hierarchy, something
72  /// we are trying to avoid.
73
74  /// Each loop pass can override these simple analysis hooks to update
75  /// desired analysis information.
76  /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
77  virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {}
78
79  /// deleteAnalysisValue - Delete analysis info associated with value V.
80  virtual void deleteAnalysisValue(Value *V, Loop *L) {}
81};
82
83class LPPassManager : public FunctionPass, public PMDataManager {
84public:
85  static char ID;
86  explicit LPPassManager(int Depth);
87
88  /// run - Execute all of the passes scheduled for execution.  Keep track of
89  /// whether any of the passes modifies the module, and if so, return true.
90  bool runOnFunction(Function &F);
91
92  /// Pass Manager itself does not invalidate any analysis info.
93  // LPPassManager needs LoopInfo.
94  void getAnalysisUsage(AnalysisUsage &Info) const;
95
96  virtual const char *getPassName() const {
97    return "Loop Pass Manager";
98  }
99
100  virtual PMDataManager *getAsPMDataManager() { return this; }
101  virtual Pass *getAsPass() { return this; }
102
103  /// Print passes managed by this manager
104  void dumpPassStructure(unsigned Offset);
105
106  Pass *getContainedPass(unsigned N) {
107    assert(N < PassVector.size() && "Pass number out of range!");
108    Pass *FP = static_cast<Pass *>(PassVector[N]);
109    return FP;
110  }
111
112  virtual PassManagerType getPassManagerType() const {
113    return PMT_LoopPassManager;
114  }
115
116public:
117  // Delete loop from the loop queue and loop nest (LoopInfo).
118  void deleteLoopFromQueue(Loop *L);
119
120  // Insert loop into the loop queue and add it as a child of the
121  // given parent.
122  void insertLoop(Loop *L, Loop *ParentLoop);
123
124  // Insert a loop into the loop queue.
125  void insertLoopIntoQueue(Loop *L);
126
127  // Reoptimize this loop. LPPassManager will re-insert this loop into the
128  // queue. This allows LoopPass to change loop nest for the loop. This
129  // utility may send LPPassManager into infinite loops so use caution.
130  void redoLoop(Loop *L);
131
132  //===--------------------------------------------------------------------===//
133  /// SimpleAnalysis - Provides simple interface to update analysis info
134  /// maintained by various passes. Note, if required this interface can
135  /// be extracted into a separate abstract class but it would require
136  /// additional use of multiple inheritance in Pass class hierarchy, something
137  /// we are trying to avoid.
138
139  /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
140  /// all passes that implement simple analysis interface.
141  void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
142
143  /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
144  /// that implement simple analysis interface.
145  void deleteSimpleAnalysisValue(Value *V, Loop *L);
146
147private:
148  std::deque<Loop *> LQ;
149  bool skipThisLoop;
150  bool redoThisLoop;
151  LoopInfo *LI;
152  Loop *CurrentLoop;
153};
154
155} // End llvm namespace
156
157#endif
158