1 //===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===//
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 the RegionPass class. All region based analysis,
10 // optimization and transformation passes are derived from RegionPass.
11 // This class is implemented following the some ideas of the LoopPass.h class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_REGIONPASS_H
16 #define LLVM_ANALYSIS_REGIONPASS_H
17 
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/IR/LegacyPassManagers.h"
20 #include "llvm/Pass.h"
21 #include <deque>
22 
23 namespace llvm {
24 class Function;
25 class RGPassManager;
26 
27 //===----------------------------------------------------------------------===//
28 /// A pass that runs on each Region in a function.
29 ///
30 /// RegionPass is managed by RGPassManager.
31 class RegionPass : public Pass {
32 public:
33   explicit RegionPass(char &pid) : Pass(PT_Region, pid) {}
34 
35   //===--------------------------------------------------------------------===//
36   /// @name To be implemented by every RegionPass
37   ///
38   //@{
39   /// Run the pass on a specific Region
40   ///
41   /// Accessing regions not contained in the current region is not allowed.
42   ///
43   /// @param R The region this pass is run on.
44   /// @param RGM The RegionPassManager that manages this Pass.
45   ///
46   /// @return True if the pass modifies this Region.
47   virtual bool runOnRegion(Region *R, RGPassManager &RGM) = 0;
48 
49   /// Get a pass to print the LLVM IR in the region.
50   ///
51   /// @param O      The output stream to print the Region.
52   /// @param Banner The banner to separate different printed passes.
53   ///
54   /// @return The pass to print the LLVM IR in the region.
55   Pass *createPrinterPass(raw_ostream &O,
56                           const std::string &Banner) const override;
57 
58   using llvm::Pass::doInitialization;
59   using llvm::Pass::doFinalization;
60 
61   virtual bool doInitialization(Region *R, RGPassManager &RGM) { return false; }
62   virtual bool doFinalization() { return false; }
63   //@}
64 
65   //===--------------------------------------------------------------------===//
66   /// @name PassManager API
67   ///
68   //@{
69   void preparePassManager(PMStack &PMS) override;
70 
71   void assignPassManager(PMStack &PMS,
72                          PassManagerType PMT = PMT_RegionPassManager) override;
73 
74   PassManagerType getPotentialPassManagerType() const override {
75     return PMT_RegionPassManager;
76   }
77   //@}
78 
79 protected:
80   /// Optional passes call this function to check whether the pass should be
81   /// skipped. This is the case when optimization bisect is over the limit.
82   bool skipRegion(Region &R) const;
83 };
84 
85 /// The pass manager to schedule RegionPasses.
86 class RGPassManager : public FunctionPass, public PMDataManager {
87   std::deque<Region*> RQ;
88   RegionInfo *RI;
89   Region *CurrentRegion;
90 
91 public:
92   static char ID;
93   explicit RGPassManager();
94 
95   /// Execute all of the passes scheduled for execution.
96   ///
97   /// @return True if any of the passes modifies the function.
98   bool runOnFunction(Function &F) override;
99 
100   /// Pass Manager itself does not invalidate any analysis info.
101   /// RGPassManager needs RegionInfo.
102   void getAnalysisUsage(AnalysisUsage &Info) const override;
103 
104   StringRef getPassName() const override { return "Region Pass Manager"; }
105 
106   PMDataManager *getAsPMDataManager() override { return this; }
107   Pass *getAsPass() override { return this; }
108 
109   /// Print passes managed by this manager.
110   void dumpPassStructure(unsigned Offset) override;
111 
112   /// Get passes contained by this manager.
113   Pass *getContainedPass(unsigned N) {
114     assert(N < PassVector.size() && "Pass number out of range!");
115     Pass *FP = static_cast<Pass *>(PassVector[N]);
116     return FP;
117   }
118 
119   PassManagerType getPassManagerType() const override {
120     return PMT_RegionPassManager;
121   }
122 };
123 
124 } // End llvm namespace
125 
126 #endif
127