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