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/ADT/PostOrderIterator.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/RegionInfoImpl.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"
25 #include "llvm/Analysis/RegionPrinter.h"
30 #define DEBUG_TYPE "region"
33 template class RegionBase<RegionTraits<Function>>;
34 template class RegionNodeBase<RegionTraits<Function>>;
35 template class RegionInfoBase<RegionTraits<Function>>;
38 STATISTIC(numRegions, "The # of regions");
39 STATISTIC(numSimpleRegions, "The # of simple regions");
41 // Always verify if expensive checking is enabled.
43 static cl::opt<bool,true>
46 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
47 cl::desc("Verify region info (time consuming)"));
50 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
51 cl::location(RegionInfo::printStyle),
53 cl::desc("style of printing regions"),
55 clEnumValN(Region::PrintNone, "none", "print no details"),
56 clEnumValN(Region::PrintBB, "bb",
57 "print regions in detail with block_iterator"),
58 clEnumValN(Region::PrintRN, "rn",
59 "print regions in detail with element_iterator"),
63 //===----------------------------------------------------------------------===//
64 // Region implementation
67 Region::Region(BasicBlock *Entry, BasicBlock *Exit,
69 DominatorTree *DT, Region *Parent) :
70 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
76 //===----------------------------------------------------------------------===//
77 // RegionInfo implementation
80 RegionInfo::RegionInfo() :
81 RegionInfoBase<RegionTraits<Function>>() {
85 RegionInfo::~RegionInfo() {
89 void RegionInfo::updateStatistics(Region *R) {
92 // TODO: Slow. Should only be enabled if -stats is used.
97 void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
98 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
103 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
105 updateStatistics(TopLevelRegion);
110 void RegionInfo::view() { viewRegion(this); }
112 void RegionInfo::viewOnly() { viewRegionOnly(this); }
115 //===----------------------------------------------------------------------===//
116 // RegionInfoPass implementation
119 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
120 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
123 RegionInfoPass::~RegionInfoPass() {
127 bool RegionInfoPass::runOnFunction(Function &F) {
130 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
131 auto PDT = &getAnalysis<PostDominatorTree>();
132 auto DF = &getAnalysis<DominanceFrontier>();
134 RI.recalculate(F, DT, PDT, DF);
138 void RegionInfoPass::releaseMemory() {
142 void RegionInfoPass::verifyAnalysis() const {
146 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
147 AU.setPreservesAll();
148 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
149 AU.addRequired<PostDominatorTree>();
150 AU.addRequired<DominanceFrontier>();
153 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
157 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
158 void RegionInfoPass::dump() const {
163 char RegionInfoPass::ID = 0;
165 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
166 "Detect single entry single exit regions", true, true)
167 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
168 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
169 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
170 INITIALIZE_PASS_END(RegionInfoPass, "regions",
171 "Detect single entry single exit regions", true, true)
173 // Create methods available outside of this file, to use them
174 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
175 // the link time optimization.
178 FunctionPass *createRegionInfoPass() {
179 return new RegionInfoPass();