Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManagers.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | #include <deque> |
| 22 | |
| 23 | namespace llvm { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | class Function; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 25 | class RGPassManager; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | |
| 27 | //===----------------------------------------------------------------------===// |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 28 | /// A pass that runs on each Region in a function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | //@{ |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 39 | /// Run the pass on a specific Region |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | /// Get a pass to print the LLVM IR in the region. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 85 | /// The pass manager to schedule RegionPasses. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 86 | class RGPassManager : public FunctionPass, public PMDataManager { |
| 87 | std::deque<Region*> RQ; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | RegionInfo *RI; |
| 89 | Region *CurrentRegion; |
| 90 | |
| 91 | public: |
| 92 | static char ID; |
| 93 | explicit RGPassManager(); |
| 94 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 95 | /// Execute all of the passes scheduled for execution. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 109 | /// Print passes managed by this manager. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 110 | void dumpPassStructure(unsigned Offset) override; |
| 111 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 112 | /// Get passes contained by this manager. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 |