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/IR/LegacyPassManagers.h"
19 #include "llvm/Pass.h"
28 //===----------------------------------------------------------------------===//
29 /// A pass that runs on each Region in a function.
31 /// RegionPass is managed by RGPassManager.
32 class RegionPass
: public Pass
{
34 explicit RegionPass(char &pid
) : Pass(PT_Region
, pid
) {}
36 //===--------------------------------------------------------------------===//
37 /// @name To be implemented by every RegionPass
40 /// Run the pass on a specific Region
42 /// Accessing regions not contained in the current region is not allowed.
44 /// @param R The region this pass is run on.
45 /// @param RGM The RegionPassManager that manages this Pass.
47 /// @return True if the pass modifies this Region.
48 virtual bool runOnRegion(Region
*R
, RGPassManager
&RGM
) = 0;
50 /// Get a pass to print the LLVM IR in the region.
52 /// @param O The output stream to print the Region.
53 /// @param Banner The banner to separate different printed passes.
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
;
59 using llvm::Pass::doInitialization
;
60 using llvm::Pass::doFinalization
;
62 virtual bool doInitialization(Region
*R
, RGPassManager
&RGM
) { return false; }
63 virtual bool doFinalization() { return false; }
66 //===--------------------------------------------------------------------===//
67 /// @name PassManager API
70 void preparePassManager(PMStack
&PMS
) override
;
72 void assignPassManager(PMStack
&PMS
,
73 PassManagerType PMT
= PMT_RegionPassManager
) override
;
75 PassManagerType
getPotentialPassManagerType() const override
{
76 return PMT_RegionPassManager
;
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;
86 /// The pass manager to schedule RegionPasses.
87 class RGPassManager
: public FunctionPass
, public PMDataManager
{
88 std::deque
<Region
*> RQ
;
90 Region
*CurrentRegion
;
94 explicit RGPassManager();
96 /// Execute all of the passes scheduled for execution.
98 /// @return True if any of the passes modifies the function.
99 bool runOnFunction(Function
&F
) override
;
101 /// Pass Manager itself does not invalidate any analysis info.
102 /// RGPassManager needs RegionInfo.
103 void getAnalysisUsage(AnalysisUsage
&Info
) const override
;
105 StringRef
getPassName() const override
{ return "Region Pass Manager"; }
107 PMDataManager
*getAsPMDataManager() override
{ return this; }
108 Pass
*getAsPass() override
{ return this; }
110 /// Print passes managed by this manager.
111 void dumpPassStructure(unsigned Offset
) override
;
113 /// Get passes contained by this manager.
114 Pass
*getContainedPass(unsigned N
) {
115 assert(N
< PassVector
.size() && "Pass number out of range!");
116 Pass
*FP
= static_cast<Pass
*>(PassVector
[N
]);
120 PassManagerType
getPassManagerType() const override
{
121 return PMT_RegionPassManager
;
125 } // End llvm namespace