1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Detects single entry single exit regions in the control flow graph.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Analysis/RegionInfo.h"
13 #include "llvm/Analysis/RegionInfoImpl.h"
14 #include "llvm/ADT/PostOrderIterator.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/RegionIterator.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
27 #define DEBUG_TYPE "region"
30 template class RegionBase<RegionTraits<Function>>;
31 template class RegionNodeBase<RegionTraits<Function>>;
32 template class RegionInfoBase<RegionTraits<Function>>;
35 STATISTIC(numRegions, "The # of regions");
36 STATISTIC(numSimpleRegions, "The # of simple regions");
38 // Always verify if expensive checking is enabled.
40 static cl::opt<bool,true>
43 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
44 cl::desc("Verify region info (time consuming)"));
47 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
48 cl::location(RegionInfo::printStyle),
50 cl::desc("style of printing regions"),
52 clEnumValN(Region::PrintNone, "none", "print no details"),
53 clEnumValN(Region::PrintBB, "bb",
54 "print regions in detail with block_iterator"),
55 clEnumValN(Region::PrintRN, "rn",
56 "print regions in detail with element_iterator"),
60 //===----------------------------------------------------------------------===//
61 // Region implementation
64 Region::Region(BasicBlock *Entry, BasicBlock *Exit,
66 DominatorTree *DT, Region *Parent) :
67 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
73 //===----------------------------------------------------------------------===//
74 // RegionInfo implementation
77 RegionInfo::RegionInfo() :
78 RegionInfoBase<RegionTraits<Function>>() {
82 RegionInfo::~RegionInfo() {
86 void RegionInfo::updateStatistics(Region *R) {
89 // TODO: Slow. Should only be enabled if -stats is used.
94 void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
95 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
100 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
102 updateStatistics(TopLevelRegion);
106 //===----------------------------------------------------------------------===//
107 // RegionInfoPass implementation
110 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
111 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
114 RegionInfoPass::~RegionInfoPass() {
118 bool RegionInfoPass::runOnFunction(Function &F) {
121 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
122 auto PDT = &getAnalysis<PostDominatorTree>();
123 auto DF = &getAnalysis<DominanceFrontier>();
125 RI.recalculate(F, DT, PDT, DF);
129 void RegionInfoPass::releaseMemory() {
133 void RegionInfoPass::verifyAnalysis() const {
137 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
138 AU.setPreservesAll();
139 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
140 AU.addRequired<PostDominatorTree>();
141 AU.addRequired<DominanceFrontier>();
144 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
148 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
149 void RegionInfoPass::dump() const {
154 char RegionInfoPass::ID = 0;
156 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
157 "Detect single entry single exit regions", true, true)
158 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
159 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
160 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
161 INITIALIZE_PASS_END(RegionInfoPass, "regions",
162 "Detect single entry single exit regions", true, true)
164 // Create methods available outside of this file, to use them
165 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
166 // the link time optimization.
169 FunctionPass *createRegionInfoPass() {
170 return new RegionInfoPass();