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/Function.h"
20 #include "llvm/IR/LegacyPassManagers.h"
21 #include "llvm/Pass.h"
29 //===----------------------------------------------------------------------===//
30 /// A pass that runs on each Region in a function.
32 /// RegionPass is managed by RGPassManager.
33 class RegionPass
: public Pass
{
35 explicit RegionPass(char &pid
) : Pass(PT_Region
, pid
) {}
37 //===--------------------------------------------------------------------===//
38 /// @name To be implemented by every RegionPass
41 /// Run the pass on a specific Region
43 /// Accessing regions not contained in the current region is not allowed.
45 /// @param R The region this pass is run on.
46 /// @param RGM The RegionPassManager that manages this Pass.
48 /// @return True if the pass modifies this Region.
49 virtual bool runOnRegion(Region
*R
, RGPassManager
&RGM
) = 0;
51 /// Get a pass to print the LLVM IR in the region.
53 /// @param O The output stream to print the Region.
54 /// @param Banner The banner to separate different printed passes.
56 /// @return The pass to print the LLVM IR in the region.
57 Pass
*createPrinterPass(raw_ostream
&O
,
58 const std::string
&Banner
) const override
;
60 using llvm::Pass::doInitialization
;
61 using llvm::Pass::doFinalization
;
63 virtual bool doInitialization(Region
*R
, RGPassManager
&RGM
) { return false; }
64 virtual bool doFinalization() { return false; }
67 //===--------------------------------------------------------------------===//
68 /// @name PassManager API
71 void preparePassManager(PMStack
&PMS
) override
;
73 void assignPassManager(PMStack
&PMS
,
74 PassManagerType PMT
= PMT_RegionPassManager
) override
;
76 PassManagerType
getPotentialPassManagerType() const override
{
77 return PMT_RegionPassManager
;
82 /// Optional passes call this function to check whether the pass should be
83 /// skipped. This is the case when optimization bisect is over the limit.
84 bool skipRegion(Region
&R
) const;
87 /// The pass manager to schedule RegionPasses.
88 class RGPassManager
: public FunctionPass
, public PMDataManager
{
89 std::deque
<Region
*> RQ
;
93 Region
*CurrentRegion
;
97 explicit RGPassManager();
99 /// Execute all of the passes scheduled for execution.
101 /// @return True if any of the passes modifies the function.
102 bool runOnFunction(Function
&F
) override
;
104 /// Pass Manager itself does not invalidate any analysis info.
105 /// RGPassManager needs RegionInfo.
106 void getAnalysisUsage(AnalysisUsage
&Info
) const override
;
108 StringRef
getPassName() const override
{ return "Region Pass Manager"; }
110 PMDataManager
*getAsPMDataManager() override
{ return this; }
111 Pass
*getAsPass() override
{ return this; }
113 /// Print passes managed by this manager.
114 void dumpPassStructure(unsigned Offset
) override
;
116 /// Get passes contained by this manager.
117 Pass
*getContainedPass(unsigned N
) {
118 assert(N
< PassVector
.size() && "Pass number out of range!");
119 Pass
*FP
= static_cast<Pass
*>(PassVector
[N
]);
123 PassManagerType
getPassManagerType() const override
{
124 return PMT_RegionPassManager
;
128 } // End llvm namespace