blob: 8ba38adfb0d233c561e072206f9cfefe6a398016 [file] [log] [blame]
Tobias Grosser336734a2010-07-22 07:46:31 +00001//===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tobias Grosser336734a2010-07-22 07:46:31 +00006//
7//===----------------------------------------------------------------------===//
8// Detects single entry single exit regions in the control flow graph.
9//===----------------------------------------------------------------------===//
10
11#include "llvm/Analysis/RegionInfo.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000012#include "llvm/ADT/Statistic.h"
Michael Kruse20dcc9f2015-08-10 13:21:59 +000013#ifndef NDEBUG
14#include "llvm/Analysis/RegionPrinter.h"
15#endif
Eugene Zelenko4f820d02017-06-27 21:52:05 +000016#include "llvm/Analysis/RegionInfoImpl.h"
Nico Weber432a3882018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Eugene Zelenko4f820d02017-06-27 21:52:05 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/PassManager.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/raw_ostream.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000024
25using namespace llvm;
26
Chandler Carruthf1221bd2014-04-22 02:48:03 +000027#define DEBUG_TYPE "region"
28
Matt Arsenault1b8d8372014-07-19 18:29:29 +000029namespace llvm {
Eugene Zelenko4f820d02017-06-27 21:52:05 +000030
Matt Arsenault1b8d8372014-07-19 18:29:29 +000031template class RegionBase<RegionTraits<Function>>;
32template class RegionNodeBase<RegionTraits<Function>>;
33template class RegionInfoBase<RegionTraits<Function>>;
Eugene Zelenko4f820d02017-06-27 21:52:05 +000034
35} // end namespace llvm
Tobias Grosser336734a2010-07-22 07:46:31 +000036
37STATISTIC(numRegions, "The # of regions");
38STATISTIC(numSimpleRegions, "The # of simple regions");
39
Matt Arsenault1b8d8372014-07-19 18:29:29 +000040// Always verify if expensive checking is enabled.
41
42static cl::opt<bool,true>
43VerifyRegionInfoX(
44 "verify-region-info",
45 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
46 cl::desc("Verify region info (time consuming)"));
47
Matt Arsenault1b8d8372014-07-19 18:29:29 +000048static 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
Tobias Grosser336734a2010-07-22 07:46:31 +000059//===----------------------------------------------------------------------===//
Matt Arsenault1b8d8372014-07-19 18:29:29 +000060// Region implementation
61//
Tobias Grosser336734a2010-07-22 07:46:31 +000062
Matt Arsenault1b8d8372014-07-19 18:29:29 +000063Region::Region(BasicBlock *Entry, BasicBlock *Exit,
64 RegionInfo* RI,
65 DominatorTree *DT, Region *Parent) :
66 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
Daniel Dunbar18e39ce2010-07-28 20:28:50 +000067
Tobias Grosser336734a2010-07-22 07:46:31 +000068}
69
Eugene Zelenko4f820d02017-06-27 21:52:05 +000070Region::~Region() = default;
Tobias Grosser336734a2010-07-22 07:46:31 +000071
72//===----------------------------------------------------------------------===//
73// RegionInfo implementation
74//
75
Eugene Zelenko4f820d02017-06-27 21:52:05 +000076RegionInfo::RegionInfo() = default;
Matt Arsenault1b8d8372014-07-19 18:29:29 +000077
Eugene Zelenko4f820d02017-06-27 21:52:05 +000078RegionInfo::~RegionInfo() = default;
Tobias Grosser336734a2010-07-22 07:46:31 +000079
Chandler Carruthca68a3e2017-01-15 06:32:49 +000080bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
81 FunctionAnalysisManager::Invalidator &) {
82 // Check whether the analysis, all analyses on functions, or the function's
Jiading Gaia2694372018-07-22 20:04:42 +000083 // CFG has been preserved.
Chandler Carruthca68a3e2017-01-15 06:32:49 +000084 auto PAC = PA.getChecker<RegionInfoAnalysis>();
85 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
86 PAC.preservedSet<CFGAnalyses>());
87}
88
Tobias Grosser336734a2010-07-22 07:46:31 +000089void RegionInfo::updateStatistics(Region *R) {
90 ++numRegions;
91
92 // TODO: Slow. Should only be enabled if -stats is used.
Matt Arsenault1b8d8372014-07-19 18:29:29 +000093 if (R->isSimple())
94 ++numSimpleRegions;
Tobias Grosser336734a2010-07-22 07:46:31 +000095}
96
NAKAMURA Takumi8eb82fc2014-07-20 03:57:51 +000097void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
98 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
Matt Arsenault1b8d8372014-07-19 18:29:29 +000099 DT = DT_;
100 PDT = PDT_;
101 DF = DF_;
Tobias Grosser336734a2010-07-22 07:46:31 +0000102
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000103 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
104 this, DT, nullptr);
Tobias Grosser336734a2010-07-22 07:46:31 +0000105 updateStatistics(TopLevelRegion);
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000106 calculate(F);
107}
Tobias Grosser336734a2010-07-22 07:46:31 +0000108
Michael Kruse20dcc9f2015-08-10 13:21:59 +0000109#ifndef NDEBUG
110void RegionInfo::view() { viewRegion(this); }
111
112void RegionInfo::viewOnly() { viewRegionOnly(this); }
113#endif
114
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000115//===----------------------------------------------------------------------===//
116// RegionInfoPass implementation
117//
Tobias Grosser336734a2010-07-22 07:46:31 +0000118
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000119RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
120 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
121}
122
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000123RegionInfoPass::~RegionInfoPass() = default;
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000124
125bool RegionInfoPass::runOnFunction(Function &F) {
126 releaseMemory();
127
128 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000129 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000130 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000131
132 RI.recalculate(F, DT, PDT, DF);
Tobias Grosser336734a2010-07-22 07:46:31 +0000133 return false;
134}
135
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000136void RegionInfoPass::releaseMemory() {
137 RI.releaseMemory();
138}
139
140void RegionInfoPass::verifyAnalysis() const {
141 RI.verifyAnalysis();
142}
143
144void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000145 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000146 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Hongbin Zheng3f978402016-02-25 17:54:07 +0000147 AU.addRequired<PostDominatorTreeWrapperPass>();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000148 AU.addRequired<DominanceFrontierWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000149}
150
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000151void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
152 RI.print(OS);
Tobias Grosser336734a2010-07-22 07:46:31 +0000153}
154
Aaron Ballman615eb472017-10-15 14:32:27 +0000155#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000156LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000157 RI.dump();
Tobias Grosser336734a2010-07-22 07:46:31 +0000158}
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000159#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000160
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000161char RegionInfoPass::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000162
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000163INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000164 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000165INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng3f978402016-02-25 17:54:07 +0000166INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Hongbin Zheng751337f2016-02-25 17:54:15 +0000167INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000168INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000169 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000170
171// Create methods available outside of this file, to use them
172// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
173// the link time optimization.
174
175namespace llvm {
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000176
Tobias Grosser336734a2010-07-22 07:46:31 +0000177 FunctionPass *createRegionInfoPass() {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000178 return new RegionInfoPass();
Tobias Grosser336734a2010-07-22 07:46:31 +0000179 }
Eugene Zelenko4f820d02017-06-27 21:52:05 +0000180
181} // end namespace llvm
Tobias Grosser336734a2010-07-22 07:46:31 +0000182
Hongbin Zhengbc539772016-02-25 17:54:25 +0000183//===----------------------------------------------------------------------===//
184// RegionInfoAnalysis implementation
185//
186
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000187AnalysisKey RegionInfoAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +0000188
Sean Silva36e0d012016-08-09 00:28:15 +0000189RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000190 RegionInfo RI;
Chandler Carruthb47f8012016-03-11 11:05:24 +0000191 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
192 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
193 auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000194
195 RI.recalculate(F, DT, PDT, DF);
196 return RI;
197}
198
199RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
200 : OS(OS) {}
201
Chandler Carruthb47f8012016-03-11 11:05:24 +0000202PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
203 FunctionAnalysisManager &AM) {
Hongbin Zhengbc539772016-02-25 17:54:25 +0000204 OS << "Region Tree for function: " << F.getName() << "\n";
Chandler Carruthb47f8012016-03-11 11:05:24 +0000205 AM.getResult<RegionInfoAnalysis>(F).print(OS);
Hongbin Zhengbc539772016-02-25 17:54:25 +0000206
207 return PreservedAnalyses::all();
208}
209
210PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000211 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +0000212 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
Hongbin Zhengbc539772016-02-25 17:54:25 +0000213
214 return PreservedAnalyses::all();
215}