blob: 5c7fa5f56693add47ec5dd745aab69f2ca3f1188 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/IR/LegacyPassManagers.h"
20#include "llvm/Pass.h"
21#include <deque>
22
23namespace llvm {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024class Function;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020025class RGPassManager;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026
27//===----------------------------------------------------------------------===//
Andrew Scullcdfcccc2018-10-05 20:58:37 +010028/// A pass that runs on each Region in a function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029///
30/// RegionPass is managed by RGPassManager.
31class RegionPass : public Pass {
32public:
33 explicit RegionPass(char &pid) : Pass(PT_Region, pid) {}
34
35 //===--------------------------------------------------------------------===//
36 /// @name To be implemented by every RegionPass
37 ///
38 //@{
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039 /// Run the pass on a specific Region
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040 ///
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 Scullcdfcccc2018-10-05 20:58:37 +010049 /// Get a pass to print the LLVM IR in the region.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 ///
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
79protected:
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 Scullcdfcccc2018-10-05 20:58:37 +010085/// The pass manager to schedule RegionPasses.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086class RGPassManager : public FunctionPass, public PMDataManager {
87 std::deque<Region*> RQ;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088 RegionInfo *RI;
89 Region *CurrentRegion;
90
91public:
92 static char ID;
93 explicit RGPassManager();
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 /// Execute all of the passes scheduled for execution.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 ///
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 Scullcdfcccc2018-10-05 20:58:37 +0100109 /// Print passes managed by this manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110 void dumpPassStructure(unsigned Offset) override;
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Get passes contained by this manager.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 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