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