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/IR/PassManager.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Analysis/RegionPrinter.h"
28 #define DEBUG_TYPE "region"
31 template class RegionBase<RegionTraits<Function>>;
32 template class RegionNodeBase<RegionTraits<Function>>;
33 template class RegionInfoBase<RegionTraits<Function>>;
36 STATISTIC(numRegions, "The # of regions");
37 STATISTIC(numSimpleRegions, "The # of simple regions");
39 // Always verify if expensive checking is enabled.
41 static cl::opt<bool,true>
44 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
45 cl::desc("Verify region info (time consuming)"));
48 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
49 cl::location(RegionInfo::printStyle),
51 cl::desc("style of printing regions"),
53 clEnumValN(Region::PrintNone, "none", "print no details"),
54 clEnumValN(Region::PrintBB, "bb",
55 "print regions in detail with block_iterator"),
56 clEnumValN(Region::PrintRN, "rn",
57 "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);
107 void RegionInfo::view() { viewRegion(this); }
109 void RegionInfo::viewOnly() { viewRegionOnly(this); }
112 //===----------------------------------------------------------------------===//
113 // RegionInfoPass implementation
116 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
117 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
120 RegionInfoPass::~RegionInfoPass() {
124 bool RegionInfoPass::runOnFunction(Function &F) {
127 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
128 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
129 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
131 RI.recalculate(F, DT, PDT, DF);
135 void RegionInfoPass::releaseMemory() {
139 void RegionInfoPass::verifyAnalysis() const {
143 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
144 AU.setPreservesAll();
145 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
146 AU.addRequired<PostDominatorTreeWrapperPass>();
147 AU.addRequired<DominanceFrontierWrapperPass>();
150 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
154 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
155 LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
160 char RegionInfoPass::ID = 0;
162 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
163 "Detect single entry single exit regions", true, true)
164 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
165 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
166 INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
167 INITIALIZE_PASS_END(RegionInfoPass, "regions",
168 "Detect single entry single exit regions", true, true)
170 // Create methods available outside of this file, to use them
171 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
172 // the link time optimization.
175 FunctionPass *createRegionInfoPass() {
176 return new RegionInfoPass();
180 //===----------------------------------------------------------------------===//
181 // RegionInfoAnalysis implementation
184 AnalysisKey RegionInfoAnalysis::Key;
186 RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
188 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
189 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
190 auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
192 RI.recalculate(F, DT, PDT, DF);
196 RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
199 PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
200 FunctionAnalysisManager &AM) {
201 OS << "Region Tree for function: " << F.getName() << "\n";
202 AM.getResult<RegionInfoAnalysis>(F).print(OS);
204 return PreservedAnalyses::all();
207 PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
208 FunctionAnalysisManager &AM) {
209 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
211 return PreservedAnalyses::all();