LLVM API Documentation

RegionPass.h
Go to the documentation of this file.
00001 //===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the RegionPass class. All region based analysis,
00011 // optimization and transformation passes are derived from RegionPass.
00012 // This class is implemented following the some ideas of the LoopPass.h class.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_ANALYSIS_REGIONPASS_H
00017 #define LLVM_ANALYSIS_REGIONPASS_H
00018 
00019 #include "llvm/Analysis/RegionInfo.h"
00020 #include "llvm/IR/Function.h"
00021 #include "llvm/IR/LegacyPassManagers.h"
00022 #include "llvm/Pass.h"
00023 #include <deque>
00024 
00025 namespace llvm {
00026 
00027 class RGPassManager;
00028 class Function;
00029 
00030 //===----------------------------------------------------------------------===//
00031 /// @brief A pass that runs on each Region in a function.
00032 ///
00033 /// RegionPass is managed by RGPassManager.
00034 class RegionPass : public Pass {
00035 public:
00036   explicit RegionPass(char &pid) : Pass(PT_Region, pid) {}
00037 
00038   //===--------------------------------------------------------------------===//
00039   /// @name To be implemented by every RegionPass
00040   ///
00041   //@{
00042   /// @brief Run the pass on a specific Region
00043   ///
00044   /// Accessing regions not contained in the current region is not allowed.
00045   ///
00046   /// @param R The region this pass is run on.
00047   /// @param RGM The RegionPassManager that manages this Pass.
00048   ///
00049   /// @return True if the pass modifies this Region.
00050   virtual bool runOnRegion(Region *R, RGPassManager &RGM) = 0;
00051 
00052   /// @brief Get a pass to print the LLVM IR in the region.
00053   ///
00054   /// @param O      The output stream to print the Region.
00055   /// @param Banner The banner to separate different printed passes.
00056   ///
00057   /// @return The pass to print the LLVM IR in the region.
00058   Pass *createPrinterPass(raw_ostream &O,
00059                           const std::string &Banner) const override;
00060 
00061   using llvm::Pass::doInitialization;
00062   using llvm::Pass::doFinalization;
00063 
00064   virtual bool doInitialization(Region *R, RGPassManager &RGM) { return false; }
00065   virtual bool doFinalization() { return false; }
00066   //@}
00067 
00068   //===--------------------------------------------------------------------===//
00069   /// @name PassManager API
00070   ///
00071   //@{
00072   void preparePassManager(PMStack &PMS) override;
00073 
00074   void assignPassManager(PMStack &PMS,
00075                          PassManagerType PMT = PMT_RegionPassManager) override;
00076 
00077   PassManagerType getPotentialPassManagerType() const override {
00078     return PMT_RegionPassManager;
00079   }
00080   //@}
00081 };
00082 
00083 /// @brief The pass manager to schedule RegionPasses.
00084 class RGPassManager : public FunctionPass, public PMDataManager {
00085   std::deque<Region*> RQ;
00086   bool skipThisRegion;
00087   bool redoThisRegion;
00088   RegionInfo *RI;
00089   Region *CurrentRegion;
00090 
00091 public:
00092   static char ID;
00093   explicit RGPassManager();
00094 
00095   /// @brief Execute all of the passes scheduled for execution.
00096   ///
00097   /// @return True if any of the passes modifies the function.
00098   bool runOnFunction(Function &F) override;
00099 
00100   /// Pass Manager itself does not invalidate any analysis info.
00101   /// RGPassManager needs RegionInfo.
00102   void getAnalysisUsage(AnalysisUsage &Info) const override;
00103 
00104   const char *getPassName() const override {
00105     return "Region Pass Manager";
00106   }
00107 
00108   PMDataManager *getAsPMDataManager() override { return this; }
00109   Pass *getAsPass() override { return this; }
00110 
00111   /// @brief Print passes managed by this manager.
00112   void dumpPassStructure(unsigned Offset) override;
00113 
00114   /// @brief Get passes contained by this manager.
00115   Pass *getContainedPass(unsigned N) {
00116     assert(N < PassVector.size() && "Pass number out of range!");
00117     Pass *FP = static_cast<Pass *>(PassVector[N]);
00118     return FP;
00119   }
00120 
00121   PassManagerType getPassManagerType() const override {
00122     return PMT_RegionPassManager;
00123   }
00124 };
00125 
00126 } // End llvm namespace
00127 
00128 #endif