blob: 2bd611350f4670167c3a5933d028460886f927ab [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/Statistic.h"
Michael Kruse20dcc9f2015-08-10 13:21:59 +000014#ifndef NDEBUG
15#include "llvm/Analysis/RegionPrinter.h"
16#endif
Eugene Zelenko4f820d02017-06-27 21:52:05 +000017#include "llvm/Analysis/RegionInfoImpl.h"
Nico Weber432a3882018-04-30 14:59:11 +000018#include "llvm/Config/llvm-config.h"
Eugene Zelenko4f820d02017-06-27 21:52:05 +000019#include "llvm/IR/Function.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/raw_ostream.h"
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 {
Eugene Zelenko4f820d02017-06-27 21:52:05 +000031
Matt Arsenault1b8d8372014-07-19 18:29:29 +000032template class RegionBase<RegionTraits<Function>>;
33template class RegionNodeBase<RegionTraits<Function>>;
34template class RegionInfoBase<RegionTraits<Function>>;
Eugene Zelenko4f820d02017-06-27 21:52:05 +000035
36} // end namespace llvm
Tobias Grosser336734a2010-07-22 07:46:31 +000037
38STATISTIC(numRegions, "The # of regions");
39STATISTIC(numSimpleRegions, "The # of simple regions");
40
Matt Arsenault1b8d8372014-07-19 18:29:29 +000041// Always verify if expensive checking is enabled.
42
43static cl::opt<bool,true>
44VerifyRegionInfoX(
45 "verify-region-info",
46 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
47 cl::desc("Verify region info (time consuming)"));
48
Matt Arsenault1b8d8372014-07-19 18:29:29 +000049static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
50 cl::location(RegionInfo::printStyle),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000051 cl::Hidden,
Tobias Grosser336734a2010-07-22 07:46:31 +000052 cl::desc("style of printing regions"),
53 cl::values(
Tobias Grosser8b304ff2011-04-04 07:19:18 +000054 clEnumValN(Region::PrintNone, "none", "print no details"),
55 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng14c05c42012-08-27 13:49:24 +000056 "print regions in detail with block_iterator"),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000057 clEnumValN(Region::PrintRN, "rn",
Mehdi Amini732afdd2016-10-08 19:41:06 +000058 "print regions in detail with element_iterator")));
Matt Arsenault1b8d8372014-07-19 18:29:29 +000059
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
Eugene Zelenko4f820d02017-06-27 21:52:05 +000071Region::~Region() = default;
Tobias Grosser336734a2010-07-22 07:46:31 +000072
73//===----------------------------------------------------------------------===//
74// RegionInfo implementation
75//
76
Eugene Zelenko4f820d02017-06-27 21:52:05 +000077RegionInfo::RegionInfo() = default;
Matt Arsenault1b8d8372014-07-19 18:29:29 +000078
Eugene Zelenko4f820d02017-06-27 21:52:05 +000079RegionInfo::~RegionInfo() = default;
Tobias Grosser336734a2010-07-22 07:46:31 +000080
Chandler Carruthca68a3e2017-01-15 06:32:49 +000081bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
82 FunctionAnalysisManager::Invalidator &) {
83 // Check whether the analysis, all analyses on functions, or the function's
Jiading Gaia2694372018-07-22 20:04:42 +000084 // CFG has been preserved.
Chandler Carruthca68a3e2017-01-15 06:32:49 +000085 auto PAC = PA.getChecker<RegionInfoAnalysis>();
86 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
87 PAC.preservedSet<CFGAnalyses>());
88}
89
Tobias Grosser336734a2010-07-22 07:46:31 +000090void 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
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000124RegionInfoPass::~RegionInfoPass() = default;
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000125
126bool RegionInfoPass::runOnFunction(Function &F) {
127 releaseMemory();
128
129 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000130 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000131 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000132
133 RI.recalculate(F, DT, PDT, DF);
Tobias Grosser336734a2010-07-22 07:46:31 +0000134 return false;
135}
136
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000137void RegionInfoPass::releaseMemory() {
138 RI.releaseMemory();
139}
140
141void RegionInfoPass::verifyAnalysis() const {
142 RI.verifyAnalysis();
143}
144
145void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000146 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000147 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000148 AU.addRequired<PostDominatorTreeWrapperPass>();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000149 AU.addRequired<DominanceFrontierWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000150}
151
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000152void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
153 RI.print(OS);
Tobias Grosser336734a2010-07-22 07:46:31 +0000154}
155
Aaron Ballman615eb472017-10-15 14:32:27 +0000156#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000157LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000158 RI.dump();
Tobias Grosser336734a2010-07-22 07:46:31 +0000159}
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000160#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000161
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000162char RegionInfoPass::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000163
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000164INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000165 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000166INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng3f978402016-02-25 17:54:07 +0000167INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Hongbin Zheng751337f2016-02-25 17:54:15 +0000168INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000169INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000170 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000171
172// Create methods available outside of this file, to use them
173// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
174// the link time optimization.
175
176namespace llvm {
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000177
Tobias Grosser336734a2010-07-22 07:46:31 +0000178 FunctionPass *createRegionInfoPass() {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000179 return new RegionInfoPass();
Tobias Grosser336734a2010-07-22 07:46:31 +0000180 }
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000181
182} // end namespace llvm
Tobias Grosser336734a2010-07-22 07:46:31 +0000183
Hongbin Zhengbc539772016-02-25 17:54:25 +0000184//===----------------------------------------------------------------------===//
185// RegionInfoAnalysis implementation
186//
187
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000188AnalysisKey RegionInfoAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000189
Sean Silva36e0d012016-08-09 00:28:15 +0000190RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &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,
Sean Silva36e0d012016-08-09 00:28:15 +0000212 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000213 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
Hongbin Zhengbc539772016-02-25 17:54:25 +0000214
215 return PreservedAnalyses::all();
216}