1//===- LoopPass.h - LoopPass class ----------------------------------------===//
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 defines LoopPass class. All loop optimization
10// and transformation passes are derived from LoopPass.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_LOOPPASS_H
15#define LLVM_ANALYSIS_LOOPPASS_H
16
17#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/IR/LegacyPassManagers.h"
19#include "llvm/Pass.h"
20#include <deque>
21
22namespace llvm {
23
24class LPPassManager;
25class Function;
26class PMStack;
27
28class LoopPass : public Pass {
29public:
30  explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {}
31
32  /// getPrinterPass - Get a pass to print the function corresponding
33  /// to a Loop.
34  Pass *createPrinterPass(raw_ostream &O,
35                          const std::string &Banner) const override;
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  using llvm::Pass::doInitialization;
42  using llvm::Pass::doFinalization;
43
44  // Initialization and finalization hooks.
45  virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
46    return false;
47  }
48
49  // Finalization hook does not supply Loop because at this time
50  // loop nest is completely different.
51  virtual bool doFinalization() { return false; }
52
53  // Check if this pass is suitable for the current LPPassManager, if
54  // available. This pass P is not suitable for a LPPassManager if P
55  // is not preserving higher level analysis info used by other
56  // LPPassManager passes. In such case, pop LPPassManager from the
57  // stack. This will force assignPassManager() to create new
58  // LPPassManger as expected.
59  void preparePassManager(PMStack &PMS) override;
60
61  /// Assign pass manager to manage this pass
62  void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
63
64  ///  Return what kind of Pass Manager can manage this pass.
65  PassManagerType getPotentialPassManagerType() const override {
66    return PMT_LoopPassManager;
67  }
68
69  //===--------------------------------------------------------------------===//
70  /// SimpleAnalysis - Provides simple interface to update analysis info
71  /// maintained by various passes. Note, if required this interface can
72  /// be extracted into a separate abstract class but it would require
73  /// additional use of multiple inheritance in Pass class hierarchy, something
74  /// we are trying to avoid.
75
76  /// Each loop pass can override these simple analysis hooks to update
77  /// desired analysis information.
78  /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
79  virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {}
80
81  /// deleteAnalysisValue - Delete analysis info associated with value V.
82  virtual void deleteAnalysisValue(Value *V, Loop *L) {}
83
84  /// Delete analysis info associated with Loop L.
85  /// Called to notify a Pass that a loop has been deleted and any
86  /// associated analysis values can be deleted.
87  virtual void deleteAnalysisLoop(Loop *L) {}
88
89protected:
90  /// Optional passes call this function to check whether the pass should be
91  /// skipped. This is the case when Attribute::OptimizeNone is set or when
92  /// optimization bisect is over the limit.
93  bool skipLoop(const Loop *L) const;
94};
95
96class LPPassManager : public FunctionPass, public PMDataManager {
97public:
98  static char ID;
99  explicit LPPassManager();
100
101  /// run - Execute all of the passes scheduled for execution.  Keep track of
102  /// whether any of the passes modifies the module, and if so, return true.
103  bool runOnFunction(Function &F) override;
104
105  /// Pass Manager itself does not invalidate any analysis info.
106  // LPPassManager needs LoopInfo.
107  void getAnalysisUsage(AnalysisUsage &Info) const override;
108
109  StringRef getPassName() const override { return "Loop Pass Manager"; }
110
111  PMDataManager *getAsPMDataManager() override { return this; }
112  Pass *getAsPass() override { return this; }
113
114  /// Print passes managed by this manager
115  void dumpPassStructure(unsigned Offset) override;
116
117  LoopPass *getContainedPass(unsigned N) {
118    assert(N < PassVector.size() && "Pass number out of range!");
119    LoopPass *LP = static_cast<LoopPass *>(PassVector[N]);
120    return LP;
121  }
122
123  PassManagerType getPassManagerType() const override {
124    return PMT_LoopPassManager;
125  }
126
127public:
128  // Add a new loop into the loop queue.
129  void addLoop(Loop &L);
130
131  // Mark \p L as deleted.
132  void markLoopAsDeleted(Loop &L);
133
134  //===--------------------------------------------------------------------===//
135  /// SimpleAnalysis - Provides simple interface to update analysis info
136  /// maintained by various passes. Note, if required this interface can
137  /// be extracted into a separate abstract class but it would require
138  /// additional use of multiple inheritance in Pass class hierarchy, something
139  /// we are trying to avoid.
140
141  /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
142  /// all passes that implement simple analysis interface.
143  void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
144
145  /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
146  /// that implement simple analysis interface.
147  void deleteSimpleAnalysisValue(Value *V, Loop *L);
148
149  /// Invoke deleteAnalysisLoop hook for all passes that implement simple
150  /// analysis interface.
151  void deleteSimpleAnalysisLoop(Loop *L);
152
153private:
154  std::deque<Loop *> LQ;
155  LoopInfo *LI;
156  Loop *CurrentLoop;
157  bool CurrentLoopDeleted;
158};
159
160// This pass is required by the LCSSA transformation. It is used inside
161// LPPassManager to check if current pass preserves LCSSA form, and if it does
162// pass manager calls lcssa verification for the current loop.
163struct LCSSAVerificationPass : public FunctionPass {
164  static char ID;
165  LCSSAVerificationPass();
166
167  bool runOnFunction(Function &F) override { return false; }
168
169  void getAnalysisUsage(AnalysisUsage &AU) const override {
170    AU.setPreservesAll();
171  }
172};
173
174} // End llvm namespace
175
176#endif
177