1336734acSTobias Grosser //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2336734acSTobias Grosser //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336734acSTobias Grosser //
7336734acSTobias Grosser //===----------------------------------------------------------------------===//
8336734acSTobias Grosser // Detects single entry single exit regions in the control flow graph.
9336734acSTobias Grosser //===----------------------------------------------------------------------===//
10336734acSTobias Grosser 
11336734acSTobias Grosser #include "llvm/Analysis/RegionInfo.h"
12336734acSTobias Grosser #include "llvm/ADT/Statistic.h"
1305da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
1420dcc9f7SMichael Kruse #ifndef NDEBUG
1520dcc9f7SMichael Kruse #include "llvm/Analysis/RegionPrinter.h"
1620dcc9f7SMichael Kruse #endif
174f820d0eSEugene Zelenko #include "llvm/Analysis/RegionInfoImpl.h"
18432a3883SNico Weber #include "llvm/Config/llvm-config.h"
194f820d0eSEugene Zelenko #include "llvm/IR/Function.h"
204f820d0eSEugene Zelenko #include "llvm/Support/CommandLine.h"
214f820d0eSEugene Zelenko #include "llvm/Support/Compiler.h"
22336734acSTobias Grosser 
23336734acSTobias Grosser using namespace llvm;
24336734acSTobias Grosser 
25f1221bd0SChandler Carruth #define DEBUG_TYPE "region"
26f1221bd0SChandler Carruth 
271b8d8379SMatt Arsenault namespace llvm {
284f820d0eSEugene Zelenko 
291b8d8379SMatt Arsenault template class RegionBase<RegionTraits<Function>>;
301b8d8379SMatt Arsenault template class RegionNodeBase<RegionTraits<Function>>;
311b8d8379SMatt Arsenault template class RegionInfoBase<RegionTraits<Function>>;
324f820d0eSEugene Zelenko 
334f820d0eSEugene Zelenko } // end namespace llvm
34336734acSTobias Grosser 
35336734acSTobias Grosser STATISTIC(numRegions,       "The # of regions");
36336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions");
37336734acSTobias Grosser 
381b8d8379SMatt Arsenault // Always verify if expensive checking is enabled.
391b8d8379SMatt Arsenault 
401b8d8379SMatt Arsenault static cl::opt<bool,true>
411b8d8379SMatt Arsenault VerifyRegionInfoX(
421b8d8379SMatt Arsenault   "verify-region-info",
431b8d8379SMatt Arsenault   cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
441b8d8379SMatt Arsenault   cl::desc("Verify region info (time consuming)"));
451b8d8379SMatt Arsenault 
461b8d8379SMatt Arsenault static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
471b8d8379SMatt Arsenault   cl::location(RegionInfo::printStyle),
488b304ff9STobias Grosser   cl::Hidden,
49336734acSTobias Grosser   cl::desc("style of printing regions"),
50336734acSTobias Grosser   cl::values(
518b304ff9STobias Grosser     clEnumValN(Region::PrintNone, "none",  "print no details"),
528b304ff9STobias Grosser     clEnumValN(Region::PrintBB, "bb",
5314c05c40SHongbin Zheng                "print regions in detail with block_iterator"),
548b304ff9STobias Grosser     clEnumValN(Region::PrintRN, "rn",
55732afdd0SMehdi Amini                "print regions in detail with element_iterator")));
561b8d8379SMatt Arsenault 
57336734acSTobias Grosser //===----------------------------------------------------------------------===//
581b8d8379SMatt Arsenault // Region implementation
591b8d8379SMatt Arsenault //
60336734acSTobias Grosser 
611b8d8379SMatt Arsenault Region::Region(BasicBlock *Entry, BasicBlock *Exit,
621b8d8379SMatt Arsenault                RegionInfo* RI,
631b8d8379SMatt Arsenault                DominatorTree *DT, Region *Parent) :
641b8d8379SMatt Arsenault   RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
6518e39cecSDaniel Dunbar 
66336734acSTobias Grosser }
67336734acSTobias Grosser 
684f820d0eSEugene Zelenko Region::~Region() = default;
69336734acSTobias Grosser 
70336734acSTobias Grosser //===----------------------------------------------------------------------===//
71336734acSTobias Grosser // RegionInfo implementation
72336734acSTobias Grosser //
73336734acSTobias Grosser 
744f820d0eSEugene Zelenko RegionInfo::RegionInfo() = default;
751b8d8379SMatt Arsenault 
764f820d0eSEugene Zelenko RegionInfo::~RegionInfo() = default;
77336734acSTobias Grosser 
78ca68a3ecSChandler Carruth bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
79ca68a3ecSChandler Carruth                             FunctionAnalysisManager::Invalidator &) {
80ca68a3ecSChandler Carruth   // Check whether the analysis, all analyses on functions, or the function's
81a2694372SJiading Gai   // CFG has been preserved.
82ca68a3ecSChandler Carruth   auto PAC = PA.getChecker<RegionInfoAnalysis>();
83ca68a3ecSChandler Carruth   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
84ca68a3ecSChandler Carruth            PAC.preservedSet<CFGAnalyses>());
85ca68a3ecSChandler Carruth }
86ca68a3ecSChandler Carruth 
87336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) {
88336734acSTobias Grosser   ++numRegions;
89336734acSTobias Grosser 
90336734acSTobias Grosser   // TODO: Slow. Should only be enabled if -stats is used.
911b8d8379SMatt Arsenault   if (R->isSimple())
921b8d8379SMatt Arsenault     ++numSimpleRegions;
93336734acSTobias Grosser }
94336734acSTobias Grosser 
958eb82fc4SNAKAMURA Takumi void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
968eb82fc4SNAKAMURA Takumi                              PostDominatorTree *PDT_, DominanceFrontier *DF_) {
971b8d8379SMatt Arsenault   DT = DT_;
981b8d8379SMatt Arsenault   PDT = PDT_;
991b8d8379SMatt Arsenault   DF = DF_;
100336734acSTobias Grosser 
1011b8d8379SMatt Arsenault   TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
1021b8d8379SMatt Arsenault                               this, DT, nullptr);
103336734acSTobias Grosser   updateStatistics(TopLevelRegion);
1041b8d8379SMatt Arsenault   calculate(F);
1051b8d8379SMatt Arsenault }
106336734acSTobias Grosser 
10720dcc9f7SMichael Kruse #ifndef NDEBUG
10820dcc9f7SMichael Kruse void RegionInfo::view() { viewRegion(this); }
10920dcc9f7SMichael Kruse 
11020dcc9f7SMichael Kruse void RegionInfo::viewOnly() { viewRegionOnly(this); }
11120dcc9f7SMichael Kruse #endif
11220dcc9f7SMichael Kruse 
1131b8d8379SMatt Arsenault //===----------------------------------------------------------------------===//
1141b8d8379SMatt Arsenault // RegionInfoPass implementation
1151b8d8379SMatt Arsenault //
116336734acSTobias Grosser 
1171b8d8379SMatt Arsenault RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
1181b8d8379SMatt Arsenault   initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
1191b8d8379SMatt Arsenault }
1201b8d8379SMatt Arsenault 
1214f820d0eSEugene Zelenko RegionInfoPass::~RegionInfoPass() = default;
1221b8d8379SMatt Arsenault 
1231b8d8379SMatt Arsenault bool RegionInfoPass::runOnFunction(Function &F) {
1241b8d8379SMatt Arsenault   releaseMemory();
1251b8d8379SMatt Arsenault 
1261b8d8379SMatt Arsenault   auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1273f978407SHongbin Zheng   auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
128751337faSHongbin Zheng   auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
1291b8d8379SMatt Arsenault 
1301b8d8379SMatt Arsenault   RI.recalculate(F, DT, PDT, DF);
131336734acSTobias Grosser   return false;
132336734acSTobias Grosser }
133336734acSTobias Grosser 
1341b8d8379SMatt Arsenault void RegionInfoPass::releaseMemory() {
1351b8d8379SMatt Arsenault   RI.releaseMemory();
1361b8d8379SMatt Arsenault }
1371b8d8379SMatt Arsenault 
1381b8d8379SMatt Arsenault void RegionInfoPass::verifyAnalysis() const {
1391b8d8379SMatt Arsenault     RI.verifyAnalysis();
1401b8d8379SMatt Arsenault }
1411b8d8379SMatt Arsenault 
1421b8d8379SMatt Arsenault void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
143336734acSTobias Grosser   AU.setPreservesAll();
14473523021SChandler Carruth   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
1453f978407SHongbin Zheng   AU.addRequired<PostDominatorTreeWrapperPass>();
146751337faSHongbin Zheng   AU.addRequired<DominanceFrontierWrapperPass>();
147336734acSTobias Grosser }
148336734acSTobias Grosser 
1491b8d8379SMatt Arsenault void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
1501b8d8379SMatt Arsenault   RI.print(OS);
151336734acSTobias Grosser }
152336734acSTobias Grosser 
153615eb470SAaron Ballman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
154eb2a2546SYaron Keren LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
1551b8d8379SMatt Arsenault   RI.dump();
156336734acSTobias Grosser }
157118b0c78SNAKAMURA Takumi #endif
158336734acSTobias Grosser 
1591b8d8379SMatt Arsenault char RegionInfoPass::ID = 0;
160336734acSTobias Grosser 
1611b8d8379SMatt Arsenault INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
1628ac477ffSOwen Anderson                 "Detect single entry single exit regions", true, true)
16373523021SChandler Carruth INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1643f978407SHongbin Zheng INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
165751337faSHongbin Zheng INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
1661b8d8379SMatt Arsenault INITIALIZE_PASS_END(RegionInfoPass, "regions",
167df7a4f25SOwen Anderson                 "Detect single entry single exit regions", true, true)
168336734acSTobias Grosser 
169*e2024d72SArthur Eubanks // Create methods available outside of this file, to use them
170*e2024d72SArthur Eubanks // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
171*e2024d72SArthur Eubanks // the link time optimization.
172*e2024d72SArthur Eubanks 
173336734acSTobias Grosser namespace llvm {
1744f820d0eSEugene Zelenko 
175336734acSTobias Grosser   FunctionPass *createRegionInfoPass() {
1761b8d8379SMatt Arsenault     return new RegionInfoPass();
177336734acSTobias Grosser   }
1784f820d0eSEugene Zelenko 
1794f820d0eSEugene Zelenko } // end namespace llvm
180336734acSTobias Grosser 
181bc53977aSHongbin Zheng //===----------------------------------------------------------------------===//
182bc53977aSHongbin Zheng // RegionInfoAnalysis implementation
183bc53977aSHongbin Zheng //
184bc53977aSHongbin Zheng 
185dab4eae2SChandler Carruth AnalysisKey RegionInfoAnalysis::Key;
186df0cd726SNAKAMURA Takumi 
18736e0d01eSSean Silva RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
188bc53977aSHongbin Zheng   RegionInfo RI;
189b47f8010SChandler Carruth   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
190b47f8010SChandler Carruth   auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
191b47f8010SChandler Carruth   auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
192bc53977aSHongbin Zheng 
193bc53977aSHongbin Zheng   RI.recalculate(F, DT, PDT, DF);
194bc53977aSHongbin Zheng   return RI;
195bc53977aSHongbin Zheng }
196bc53977aSHongbin Zheng 
197bc53977aSHongbin Zheng RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
198bc53977aSHongbin Zheng   : OS(OS) {}
199bc53977aSHongbin Zheng 
200b47f8010SChandler Carruth PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
201b47f8010SChandler Carruth                                              FunctionAnalysisManager &AM) {
202bc53977aSHongbin Zheng   OS << "Region Tree for function: " << F.getName() << "\n";
203b47f8010SChandler Carruth   AM.getResult<RegionInfoAnalysis>(F).print(OS);
204bc53977aSHongbin Zheng 
205bc53977aSHongbin Zheng   return PreservedAnalyses::all();
206bc53977aSHongbin Zheng }
207bc53977aSHongbin Zheng 
208bc53977aSHongbin Zheng PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
20936e0d01eSSean Silva                                               FunctionAnalysisManager &AM) {
210b47f8010SChandler Carruth   AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
211bc53977aSHongbin Zheng 
212bc53977aSHongbin Zheng   return PreservedAnalyses::all();
213bc53977aSHongbin Zheng }
214