2 #include "llvm/CodeGen/MachineRegionInfo.h"
3 #include "llvm/ADT/Statistic.h"
4 #include "llvm/Analysis/RegionInfoImpl.h"
5 #include "llvm/CodeGen/MachinePostDominators.h"
7 #define DEBUG_TYPE "region"
11 STATISTIC(numMachineRegions, "The # of machine regions");
12 STATISTIC(numMachineSimpleRegions, "The # of simple machine regions");
15 template class RegionBase<RegionTraits<MachineFunction>>;
16 template class RegionNodeBase<RegionTraits<MachineFunction>>;
17 template class RegionInfoBase<RegionTraits<MachineFunction>>;
20 //===----------------------------------------------------------------------===//
21 // MachineRegion implementation
24 MachineRegion::MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
25 MachineRegionInfo* RI,
26 MachineDominatorTree *DT, MachineRegion *Parent) :
27 RegionBase<RegionTraits<MachineFunction>>(Entry, Exit, RI, DT, Parent) {
31 MachineRegion::~MachineRegion() { }
33 //===----------------------------------------------------------------------===//
34 // MachineRegionInfo implementation
37 MachineRegionInfo::MachineRegionInfo() :
38 RegionInfoBase<RegionTraits<MachineFunction>>() {
42 MachineRegionInfo::~MachineRegionInfo() {
46 void MachineRegionInfo::updateStatistics(MachineRegion *R) {
49 // TODO: Slow. Should only be enabled if -stats is used.
51 ++numMachineSimpleRegions;
54 void MachineRegionInfo::recalculate(MachineFunction &F,
55 MachineDominatorTree *DT_,
56 MachinePostDominatorTree *PDT_,
57 MachineDominanceFrontier *DF_) {
62 MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&F);
64 TopLevelRegion = new MachineRegion(Entry, nullptr, this, DT, nullptr);
65 updateStatistics(TopLevelRegion);
69 //===----------------------------------------------------------------------===//
70 // MachineRegionInfoPass implementation
73 MachineRegionInfoPass::MachineRegionInfoPass() : MachineFunctionPass(ID) {
74 initializeMachineRegionInfoPassPass(*PassRegistry::getPassRegistry());
77 MachineRegionInfoPass::~MachineRegionInfoPass() {
81 bool MachineRegionInfoPass::runOnMachineFunction(MachineFunction &F) {
84 auto DT = &getAnalysis<MachineDominatorTree>();
85 auto PDT = &getAnalysis<MachinePostDominatorTree>();
86 auto DF = &getAnalysis<MachineDominanceFrontier>();
88 RI.recalculate(F, DT, PDT, DF);
92 void MachineRegionInfoPass::releaseMemory() {
96 void MachineRegionInfoPass::verifyAnalysis() const {
97 // Only do verification when user wants to, otherwise this expensive check
98 // will be invoked by PMDataManager::verifyPreservedAnalysis when
99 // a regionpass (marked PreservedAll) finish.
100 if (MachineRegionInfo::VerifyRegionInfo)
104 void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesAll();
106 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
107 AU.addRequired<PostDominatorTree>();
108 AU.addRequired<DominanceFrontier>();
111 void MachineRegionInfoPass::print(raw_ostream &OS, const Module *) const {
115 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
116 void MachineRegionInfoPass::dump() const {
121 char MachineRegionInfoPass::ID = 0;
123 INITIALIZE_PASS_BEGIN(MachineRegionInfoPass, "regions",
124 "Detect single entry single exit regions", true, true)
125 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
126 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
127 INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
128 INITIALIZE_PASS_END(MachineRegionInfoPass, "regions",
129 "Detect single entry single exit regions", true, true)
131 // Create methods available outside of this file, to use them
132 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
133 // the link time optimization.
136 FunctionPass *createMachineRegionInfoPass() {
137 return new MachineRegionInfoPass();