blob: 63ef8d28d44ad26e852892b7c32126bcf3c789cc [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"
Michael Kruse20dcc9f2015-08-10 13:21:59 +000022#ifndef NDEBUG
23#include "llvm/Analysis/RegionPrinter.h"
24#endif
Tobias Grosser336734a2010-07-22 07:46:31 +000025
26using namespace llvm;
27
Chandler Carruthf1221bd2014-04-22 02:48:03 +000028#define DEBUG_TYPE "region"
29
Matt Arsenault1b8d8372014-07-19 18:29:29 +000030namespace llvm {
31template class RegionBase<RegionTraits<Function>>;
32template class RegionNodeBase<RegionTraits<Function>>;
33template class RegionInfoBase<RegionTraits<Function>>;
34}
Tobias Grosser336734a2010-07-22 07:46:31 +000035
36STATISTIC(numRegions, "The # of regions");
37STATISTIC(numSimpleRegions, "The # of simple regions");
38
Matt Arsenault1b8d8372014-07-19 18:29:29 +000039// Always verify if expensive checking is enabled.
40
41static cl::opt<bool,true>
42VerifyRegionInfoX(
43 "verify-region-info",
44 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
45 cl::desc("Verify region info (time consuming)"));
46
47
48static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
49 cl::location(RegionInfo::printStyle),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000050 cl::Hidden,
Tobias Grosser336734a2010-07-22 07:46:31 +000051 cl::desc("style of printing regions"),
52 cl::values(
Tobias Grosser8b304ff2011-04-04 07:19:18 +000053 clEnumValN(Region::PrintNone, "none", "print no details"),
54 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng14c05c42012-08-27 13:49:24 +000055 "print regions in detail with block_iterator"),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000056 clEnumValN(Region::PrintRN, "rn",
Mehdi Amini732afdd2016-10-08 19:41:06 +000057 "print regions in detail with element_iterator")));
Matt Arsenault1b8d8372014-07-19 18:29:29 +000058
59
Tobias Grosser336734a2010-07-22 07:46:31 +000060//===----------------------------------------------------------------------===//
Matt Arsenault1b8d8372014-07-19 18:29:29 +000061// Region implementation
62//
Tobias Grosser336734a2010-07-22 07:46:31 +000063
Matt Arsenault1b8d8372014-07-19 18:29:29 +000064Region::Region(BasicBlock *Entry, BasicBlock *Exit,
65 RegionInfo* RI,
66 DominatorTree *DT, Region *Parent) :
67 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
Daniel Dunbar18e39ce2010-07-28 20:28:50 +000068
Tobias Grosser336734a2010-07-22 07:46:31 +000069}
70
Matt Arsenault1b8d8372014-07-19 18:29:29 +000071Region::~Region() { }
Tobias Grosser336734a2010-07-22 07:46:31 +000072
73//===----------------------------------------------------------------------===//
74// RegionInfo implementation
75//
76
Matt Arsenault1b8d8372014-07-19 18:29:29 +000077RegionInfo::RegionInfo() :
78 RegionInfoBase<RegionTraits<Function>>() {
79
Tobias Grosser336734a2010-07-22 07:46:31 +000080}
81
Matt Arsenault1b8d8372014-07-19 18:29:29 +000082RegionInfo::~RegionInfo() {
Tobias Grosser336734a2010-07-22 07:46:31 +000083
Tobias Grosser336734a2010-07-22 07:46:31 +000084}
85
Chandler Carruthca68a3e2017-01-15 06:32:49 +000086bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
87 FunctionAnalysisManager::Invalidator &) {
88 // Check whether the analysis, all analyses on functions, or the function's
89 // CFG have been preserved.
90 auto PAC = PA.getChecker<RegionInfoAnalysis>();
91 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
92 PAC.preservedSet<CFGAnalyses>());
93}
94
Tobias Grosser336734a2010-07-22 07:46:31 +000095void RegionInfo::updateStatistics(Region *R) {
96 ++numRegions;
97
98 // TODO: Slow. Should only be enabled if -stats is used.
Matt Arsenault1b8d8372014-07-19 18:29:29 +000099 if (R->isSimple())
100 ++numSimpleRegions;
Tobias Grosser336734a2010-07-22 07:46:31 +0000101}
102
NAKAMURA Takumi8eb82fc2014-07-20 03:57:51 +0000103void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
104 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000105 DT = DT_;
106 PDT = PDT_;
107 DF = DF_;
Tobias Grosser336734a2010-07-22 07:46:31 +0000108
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000109 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
110 this, DT, nullptr);
Tobias Grosser336734a2010-07-22 07:46:31 +0000111 updateStatistics(TopLevelRegion);
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000112 calculate(F);
113}
Tobias Grosser336734a2010-07-22 07:46:31 +0000114
Michael Kruse20dcc9f2015-08-10 13:21:59 +0000115#ifndef NDEBUG
116void RegionInfo::view() { viewRegion(this); }
117
118void RegionInfo::viewOnly() { viewRegionOnly(this); }
119#endif
120
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000121//===----------------------------------------------------------------------===//
122// RegionInfoPass implementation
123//
Tobias Grosser336734a2010-07-22 07:46:31 +0000124
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000125RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
126 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
127}
128
129RegionInfoPass::~RegionInfoPass() {
130
131}
132
133bool RegionInfoPass::runOnFunction(Function &F) {
134 releaseMemory();
135
136 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000137 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000138 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000139
140 RI.recalculate(F, DT, PDT, DF);
Tobias Grosser336734a2010-07-22 07:46:31 +0000141 return false;
142}
143
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000144void RegionInfoPass::releaseMemory() {
145 RI.releaseMemory();
146}
147
148void RegionInfoPass::verifyAnalysis() const {
149 RI.verifyAnalysis();
150}
151
152void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000153 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000154 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000155 AU.addRequired<PostDominatorTreeWrapperPass>();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000156 AU.addRequired<DominanceFrontierWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000157}
158
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000159void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
160 RI.print(OS);
Tobias Grosser336734a2010-07-22 07:46:31 +0000161}
162
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000163#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000164LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000165 RI.dump();
Tobias Grosser336734a2010-07-22 07:46:31 +0000166}
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000167#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000168
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000169char RegionInfoPass::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000170
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000171INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000172 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000173INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng3f978402016-02-25 17:54:07 +0000174INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Hongbin Zheng751337f2016-02-25 17:54:15 +0000175INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000176INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000177 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000178
179// Create methods available outside of this file, to use them
180// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
181// the link time optimization.
182
183namespace llvm {
184 FunctionPass *createRegionInfoPass() {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000185 return new RegionInfoPass();
Tobias Grosser336734a2010-07-22 07:46:31 +0000186 }
187}
188
Hongbin Zhengbc539772016-02-25 17:54:25 +0000189//===----------------------------------------------------------------------===//
190// RegionInfoAnalysis implementation
191//
192
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000193AnalysisKey RegionInfoAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000194
Sean Silva36e0d012016-08-09 00:28:15 +0000195RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000196 RegionInfo RI;
Chandler Carruthb47f8012016-03-11 11:05:24 +0000197 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
198 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
199 auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000200
201 RI.recalculate(F, DT, PDT, DF);
202 return RI;
203}
204
205RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
206 : OS(OS) {}
207
Chandler Carruthb47f8012016-03-11 11:05:24 +0000208PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
209 FunctionAnalysisManager &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000210 OS << "Region Tree for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +0000211 AM.getResult<RegionInfoAnalysis>(F).print(OS);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000212
213 return PreservedAnalyses::all();
214}
215
216PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000217 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000218 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
Hongbin Zhengbc539772016-02-25 17:54:25 +0000219
220 return PreservedAnalyses::all();
221}