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