blob: 2e9325e08ebca8707fce1fee67fd09f78e917f59 [file] [log] [blame]
Tobias Grosser336734a2010-07-22 07:46:31 +00001//===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Detects single entry single exit regions in the control flow graph.
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Analysis/RegionInfo.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000013#include "llvm/ADT/PostOrderIterator.h"
14#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "llvm/Analysis/RegionInfoImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Analysis/RegionIterator.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000018#include "llvm/IR/PassManager.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000019#include "llvm/Support/CommandLine.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000020#include "llvm/Support/Debug.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/Support/ErrorHandling.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000022#include <algorithm>
David Blaikieec649ac2014-04-15 18:32:43 +000023#include <iterator>
Bill Wendling570d3022013-08-21 21:14:19 +000024#include <set>
Michael Kruse20dcc9f2015-08-10 13:21:59 +000025#ifndef NDEBUG
26#include "llvm/Analysis/RegionPrinter.h"
27#endif
Tobias Grosser336734a2010-07-22 07:46:31 +000028
29using namespace llvm;
30
Chandler Carruthf1221bd2014-04-22 02:48:03 +000031#define DEBUG_TYPE "region"
32
Matt Arsenault1b8d8372014-07-19 18:29:29 +000033namespace llvm {
34template class RegionBase<RegionTraits<Function>>;
35template class RegionNodeBase<RegionTraits<Function>>;
36template class RegionInfoBase<RegionTraits<Function>>;
37}
Tobias Grosser336734a2010-07-22 07:46:31 +000038
39STATISTIC(numRegions, "The # of regions");
40STATISTIC(numSimpleRegions, "The # of simple regions");
41
Matt Arsenault1b8d8372014-07-19 18:29:29 +000042// Always verify if expensive checking is enabled.
43
44static cl::opt<bool,true>
45VerifyRegionInfoX(
46 "verify-region-info",
47 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
48 cl::desc("Verify region info (time consuming)"));
49
50
51static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
52 cl::location(RegionInfo::printStyle),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000053 cl::Hidden,
Tobias Grosser336734a2010-07-22 07:46:31 +000054 cl::desc("style of printing regions"),
55 cl::values(
Tobias Grosser8b304ff2011-04-04 07:19:18 +000056 clEnumValN(Region::PrintNone, "none", "print no details"),
57 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng14c05c42012-08-27 13:49:24 +000058 "print regions in detail with block_iterator"),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000059 clEnumValN(Region::PrintRN, "rn",
60 "print regions in detail with element_iterator"),
Tobias Grosser336734a2010-07-22 07:46:31 +000061 clEnumValEnd));
Matt Arsenault1b8d8372014-07-19 18:29:29 +000062
63
Tobias Grosser336734a2010-07-22 07:46:31 +000064//===----------------------------------------------------------------------===//
Matt Arsenault1b8d8372014-07-19 18:29:29 +000065// Region implementation
66//
Tobias Grosser336734a2010-07-22 07:46:31 +000067
Matt Arsenault1b8d8372014-07-19 18:29:29 +000068Region::Region(BasicBlock *Entry, BasicBlock *Exit,
69 RegionInfo* RI,
70 DominatorTree *DT, Region *Parent) :
71 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
Daniel Dunbar18e39ce2010-07-28 20:28:50 +000072
Tobias Grosser336734a2010-07-22 07:46:31 +000073}
74
Matt Arsenault1b8d8372014-07-19 18:29:29 +000075Region::~Region() { }
Tobias Grosser336734a2010-07-22 07:46:31 +000076
77//===----------------------------------------------------------------------===//
78// RegionInfo implementation
79//
80
Matt Arsenault1b8d8372014-07-19 18:29:29 +000081RegionInfo::RegionInfo() :
82 RegionInfoBase<RegionTraits<Function>>() {
83
Tobias Grosser336734a2010-07-22 07:46:31 +000084}
85
Matt Arsenault1b8d8372014-07-19 18:29:29 +000086RegionInfo::~RegionInfo() {
Tobias Grosser336734a2010-07-22 07:46:31 +000087
Tobias Grosser336734a2010-07-22 07:46:31 +000088}
89
90void RegionInfo::updateStatistics(Region *R) {
91 ++numRegions;
92
93 // TODO: Slow. Should only be enabled if -stats is used.
Matt Arsenault1b8d8372014-07-19 18:29:29 +000094 if (R->isSimple())
95 ++numSimpleRegions;
Tobias Grosser336734a2010-07-22 07:46:31 +000096}
97
NAKAMURA Takumi8eb82fc2014-07-20 03:57:51 +000098void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
99 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000100 DT = DT_;
101 PDT = PDT_;
102 DF = DF_;
Tobias Grosser336734a2010-07-22 07:46:31 +0000103
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000104 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
105 this, DT, nullptr);
Tobias Grosser336734a2010-07-22 07:46:31 +0000106 updateStatistics(TopLevelRegion);
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000107 calculate(F);
108}
Tobias Grosser336734a2010-07-22 07:46:31 +0000109
Michael Kruse20dcc9f2015-08-10 13:21:59 +0000110#ifndef NDEBUG
111void RegionInfo::view() { viewRegion(this); }
112
113void RegionInfo::viewOnly() { viewRegionOnly(this); }
114#endif
115
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000116//===----------------------------------------------------------------------===//
117// RegionInfoPass implementation
118//
Tobias Grosser336734a2010-07-22 07:46:31 +0000119
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000120RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
121 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
122}
123
124RegionInfoPass::~RegionInfoPass() {
125
126}
127
128bool RegionInfoPass::runOnFunction(Function &F) {
129 releaseMemory();
130
131 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000132 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000133 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000134
135 RI.recalculate(F, DT, PDT, DF);
Tobias Grosser336734a2010-07-22 07:46:31 +0000136 return false;
137}
138
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000139void RegionInfoPass::releaseMemory() {
140 RI.releaseMemory();
141}
142
143void RegionInfoPass::verifyAnalysis() const {
144 RI.verifyAnalysis();
145}
146
147void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000148 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000149 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000150 AU.addRequired<PostDominatorTreeWrapperPass>();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000151 AU.addRequired<DominanceFrontierWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000152}
153
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000154void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
155 RI.print(OS);
Tobias Grosser336734a2010-07-22 07:46:31 +0000156}
157
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000158#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000159LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000160 RI.dump();
Tobias Grosser336734a2010-07-22 07:46:31 +0000161}
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000162#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000163
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000164char RegionInfoPass::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000165
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000166INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000167 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000168INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng3f978402016-02-25 17:54:07 +0000169INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Hongbin Zheng751337f2016-02-25 17:54:15 +0000170INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000171INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000172 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000173
174// Create methods available outside of this file, to use them
175// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
176// the link time optimization.
177
178namespace llvm {
179 FunctionPass *createRegionInfoPass() {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000180 return new RegionInfoPass();
Tobias Grosser336734a2010-07-22 07:46:31 +0000181 }
182}
183
Hongbin Zhengbc539772016-02-25 17:54:25 +0000184//===----------------------------------------------------------------------===//
185// RegionInfoAnalysis implementation
186//
187
Chandler Carruthb4faf132016-03-11 10:22:49 +0000188char RegionInfoAnalysis::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000189
Chandler Carruthb47f8012016-03-11 11:05:24 +0000190RegionInfo RegionInfoAnalysis::run(Function &F, AnalysisManager<Function> &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000191 RegionInfo RI;
Chandler Carruthb47f8012016-03-11 11:05:24 +0000192 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
193 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
194 auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000195
196 RI.recalculate(F, DT, PDT, DF);
197 return RI;
198}
199
200RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
201 : OS(OS) {}
202
Chandler Carruthb47f8012016-03-11 11:05:24 +0000203PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
204 FunctionAnalysisManager &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000205 OS << "Region Tree for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +0000206 AM.getResult<RegionInfoAnalysis>(F).print(OS);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000207
208 return PreservedAnalyses::all();
209}
210
211PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
Chandler Carruthb47f8012016-03-11 11:05:24 +0000212 AnalysisManager<Function> &AM) {
213 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
Hongbin Zhengbc539772016-02-25 17:54:25 +0000214
215 return PreservedAnalyses::all();
216}