1 //===- lib/Codegen/MachineRegionInfo.cpp ----------------------------------===//
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 #include "llvm/CodeGen/MachineRegionInfo.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/Analysis/RegionInfoImpl.h"
12 #include "llvm/CodeGen/MachinePostDominators.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/Pass.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
18 #define DEBUG_TYPE "machine-region-info"
22 STATISTIC(numMachineRegions
, "The # of machine regions");
23 STATISTIC(numMachineSimpleRegions
, "The # of simple machine regions");
27 template class RegionBase
<RegionTraits
<MachineFunction
>>;
28 template class RegionNodeBase
<RegionTraits
<MachineFunction
>>;
29 template class RegionInfoBase
<RegionTraits
<MachineFunction
>>;
31 } // end namespace llvm
33 //===----------------------------------------------------------------------===//
34 // MachineRegion implementation
36 MachineRegion::MachineRegion(MachineBasicBlock
*Entry
, MachineBasicBlock
*Exit
,
37 MachineRegionInfo
* RI
,
38 MachineDominatorTree
*DT
, MachineRegion
*Parent
) :
39 RegionBase
<RegionTraits
<MachineFunction
>>(Entry
, Exit
, RI
, DT
, Parent
) {}
41 MachineRegion::~MachineRegion() = default;
43 //===----------------------------------------------------------------------===//
44 // MachineRegionInfo implementation
46 MachineRegionInfo::MachineRegionInfo() = default;
48 MachineRegionInfo::~MachineRegionInfo() = default;
50 void MachineRegionInfo::updateStatistics(MachineRegion
*R
) {
53 // TODO: Slow. Should only be enabled if -stats is used.
55 ++numMachineSimpleRegions
;
58 void MachineRegionInfo::recalculate(MachineFunction
&F
,
59 MachineDominatorTree
*DT_
,
60 MachinePostDominatorTree
*PDT_
,
61 MachineDominanceFrontier
*DF_
) {
66 MachineBasicBlock
*Entry
= GraphTraits
<MachineFunction
*>::getEntryNode(&F
);
68 TopLevelRegion
= new MachineRegion(Entry
, nullptr, this, DT
, nullptr);
69 updateStatistics(TopLevelRegion
);
73 //===----------------------------------------------------------------------===//
74 // MachineRegionInfoPass implementation
77 MachineRegionInfoPass::MachineRegionInfoPass() : MachineFunctionPass(ID
) {
78 initializeMachineRegionInfoPassPass(*PassRegistry::getPassRegistry());
81 MachineRegionInfoPass::~MachineRegionInfoPass() = default;
83 bool MachineRegionInfoPass::runOnMachineFunction(MachineFunction
&F
) {
86 auto DT
= &getAnalysis
<MachineDominatorTree
>();
87 auto PDT
= &getAnalysis
<MachinePostDominatorTree
>();
88 auto DF
= &getAnalysis
<MachineDominanceFrontier
>();
90 RI
.recalculate(F
, DT
, PDT
, DF
);
92 LLVM_DEBUG(RI
.dump());
97 void MachineRegionInfoPass::releaseMemory() {
101 void MachineRegionInfoPass::verifyAnalysis() const {
102 // Only do verification when user wants to, otherwise this expensive check
103 // will be invoked by PMDataManager::verifyPreservedAnalysis when
104 // a regionpass (marked PreservedAll) finish.
105 if (MachineRegionInfo::VerifyRegionInfo
)
109 void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
110 AU
.setPreservesAll();
111 AU
.addRequired
<MachineDominatorTree
>();
112 AU
.addRequired
<MachinePostDominatorTree
>();
113 AU
.addRequired
<MachineDominanceFrontier
>();
114 MachineFunctionPass::getAnalysisUsage(AU
);
117 void MachineRegionInfoPass::print(raw_ostream
&OS
, const Module
*) const {
121 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122 LLVM_DUMP_METHOD
void MachineRegionInfoPass::dump() const {
127 char MachineRegionInfoPass::ID
= 0;
128 char &MachineRegionInfoPassID
= MachineRegionInfoPass::ID
;
130 INITIALIZE_PASS_BEGIN(MachineRegionInfoPass
, DEBUG_TYPE
,
131 "Detect single entry single exit regions", true, true)
132 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree
)
133 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree
)
134 INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier
)
135 INITIALIZE_PASS_END(MachineRegionInfoPass
, DEBUG_TYPE
,
136 "Detect single entry single exit regions", true, true)
138 // Create methods available outside of this file, to use them
139 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
140 // the link time optimization.
144 FunctionPass
*createMachineRegionInfoPass() {
145 return new MachineRegionInfoPass();
148 } // end namespace llvm