1 //===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_REGIONPASS_H
16 #define LLVM_ANALYSIS_REGIONPASS_H
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/IR/LegacyPassManagers.h"
20 #include "llvm/Pass.h"
27 //===----------------------------------------------------------------------===//
28 /// A pass that runs on each Region in a function.
30 /// RegionPass is managed by RGPassManager.
31 class RegionPass : public Pass {
33 explicit RegionPass(char &pid) : Pass(PT_Region, pid) {}
35 //===--------------------------------------------------------------------===//
36 /// @name To be implemented by every RegionPass
39 /// Run the pass on a specific Region
41 /// Accessing regions not contained in the current region is not allowed.
43 /// @param R The region this pass is run on.
44 /// @param RGM The RegionPassManager that manages this Pass.
46 /// @return True if the pass modifies this Region.
47 virtual bool runOnRegion(Region *R, RGPassManager &RGM) = 0;
49 /// Get a pass to print the LLVM IR in the region.
51 /// @param O The output stream to print the Region.
52 /// @param Banner The banner to separate different printed passes.
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;
58 using llvm::Pass::doInitialization;
59 using llvm::Pass::doFinalization;
61 virtual bool doInitialization(Region *R, RGPassManager &RGM) { return false; }
62 virtual bool doFinalization() { return false; }
65 //===--------------------------------------------------------------------===//
66 /// @name PassManager API
69 void preparePassManager(PMStack &PMS) override;
71 void assignPassManager(PMStack &PMS,
72 PassManagerType PMT = PMT_RegionPassManager) override;
74 PassManagerType getPotentialPassManagerType() const override {
75 return PMT_RegionPassManager;
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;
85 /// The pass manager to schedule RegionPasses.
86 class RGPassManager : public FunctionPass, public PMDataManager {
87 std::deque<Region*> RQ;
91 Region *CurrentRegion;
95 explicit RGPassManager();
97 /// Execute all of the passes scheduled for execution.
99 /// @return True if any of the passes modifies the function.
100 bool runOnFunction(Function &F) override;
102 /// Pass Manager itself does not invalidate any analysis info.
103 /// RGPassManager needs RegionInfo.
104 void getAnalysisUsage(AnalysisUsage &Info) const override;
106 StringRef getPassName() const override { return "Region Pass Manager"; }
108 PMDataManager *getAsPMDataManager() override { return this; }
109 Pass *getAsPass() override { return this; }
111 /// Print passes managed by this manager.
112 void dumpPassStructure(unsigned Offset) override;
114 /// Get passes contained by this manager.
115 Pass *getContainedPass(unsigned N) {
116 assert(N < PassVector.size() && "Pass number out of range!");
117 Pass *FP = static_cast<Pass *>(PassVector[N]);
121 PassManagerType getPassManagerType() const override {
122 return PMT_RegionPassManager;
126 } // End llvm namespace