blob: 2bd611350f4670167c3a5933d028460886f927ab [file] [log] [blame]
Tobias Grosserf96b0062010-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 Grosserf96b0062010-07-22 07:46:31 +000013#include "llvm/ADT/Statistic.h"
Michael Kruse5864ac72015-08-10 13:21:59 +000014#ifndef NDEBUG
15#include "llvm/Analysis/RegionPrinter.h"
16#endif
Eugene Zelenko01187b32017-06-27 21:52:05 +000017#include "llvm/Analysis/RegionInfoImpl.h"
Nico Weber0f38c602018-04-30 14:59:11 +000018#include "llvm/Config/llvm-config.h"
Eugene Zelenko01187b32017-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 Grosserf96b0062010-07-22 07:46:31 +000025
26using namespace llvm;
27
Chandler Carruth4da25372014-04-22 02:48:03 +000028#define DEBUG_TYPE "region"
29
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000030namespace llvm {
Eugene Zelenko01187b32017-06-27 21:52:05 +000031
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000032template class RegionBase<RegionTraits<Function>>;
33template class RegionNodeBase<RegionTraits<Function>>;
34template class RegionInfoBase<RegionTraits<Function>>;
Eugene Zelenko01187b32017-06-27 21:52:05 +000035
36} // end namespace llvm
Tobias Grosserf96b0062010-07-22 07:46:31 +000037
38STATISTIC(numRegions, "The # of regions");
39STATISTIC(numSimpleRegions, "The # of simple regions");
40
Matt Arsenault5e1c96a2014-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 Arsenault5e1c96a2014-07-19 18:29:29 +000049static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
50 cl::location(RegionInfo::printStyle),
Tobias Grossercc5d9922011-04-04 07:19:18 +000051 cl::Hidden,
Tobias Grosserf96b0062010-07-22 07:46:31 +000052 cl::desc("style of printing regions"),
53 cl::values(
Tobias Grossercc5d9922011-04-04 07:19:18 +000054 clEnumValN(Region::PrintNone, "none", "print no details"),
55 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng23a22a22012-08-27 13:49:24 +000056 "print regions in detail with block_iterator"),
Tobias Grossercc5d9922011-04-04 07:19:18 +000057 clEnumValN(Region::PrintRN, "rn",
Mehdi Amini3ffe1132016-10-08 19:41:06 +000058 "print regions in detail with element_iterator")));
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000059
Tobias Grosserf96b0062010-07-22 07:46:31 +000060//===----------------------------------------------------------------------===//
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000061// Region implementation
62//
Tobias Grosserf96b0062010-07-22 07:46:31 +000063
Matt Arsenault5e1c96a2014-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 Dunbar329878f2010-07-28 20:28:50 +000068
Tobias Grosserf96b0062010-07-22 07:46:31 +000069}
70
Eugene Zelenko01187b32017-06-27 21:52:05 +000071Region::~Region() = default;
Tobias Grosserf96b0062010-07-22 07:46:31 +000072
73//===----------------------------------------------------------------------===//
74// RegionInfo implementation
75//
76
Eugene Zelenko01187b32017-06-27 21:52:05 +000077RegionInfo::RegionInfo() = default;
Matt Arsenault5e1c96a2014-07-19 18:29:29 +000078
Eugene Zelenko01187b32017-06-27 21:52:05 +000079RegionInfo::~RegionInfo() = default;
Tobias Grosserf96b0062010-07-22 07:46:31 +000080
Chandler Carruth10dd00c2017-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 Gaic6b3cc72018-07-22 20:04:42 +000084 // CFG has been preserved.
Chandler Carruth10dd00c2017-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 Grosserf96b0062010-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 Arsenault5e1c96a2014-07-19 18:29:29 +000094 if (R->isSimple())
95 ++numSimpleRegions;
Tobias Grosserf96b0062010-07-22 07:46:31 +000096}
97
NAKAMURA Takumi06bc9c42014-07-20 03:57:51 +000098void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
99 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000100 DT = DT_;
101 PDT = PDT_;
102 DF = DF_;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000103
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000104 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
105 this, DT, nullptr);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000106 updateStatistics(TopLevelRegion);
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000107 calculate(F);
108}
Tobias Grosserf96b0062010-07-22 07:46:31 +0000109
Michael Kruse5864ac72015-08-10 13:21:59 +0000110#ifndef NDEBUG
111void RegionInfo::view() { viewRegion(this); }
112
113void RegionInfo::viewOnly() { viewRegionOnly(this); }
114#endif
115
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000116//===----------------------------------------------------------------------===//
117// RegionInfoPass implementation
118//
Tobias Grosserf96b0062010-07-22 07:46:31 +0000119
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000120RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
121 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
122}
123
Eugene Zelenko01187b32017-06-27 21:52:05 +0000124RegionInfoPass::~RegionInfoPass() = default;
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000125
126bool RegionInfoPass::runOnFunction(Function &F) {
127 releaseMemory();
128
129 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000130 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Hongbin Zheng15969222016-02-25 17:54:15 +0000131 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000132
133 RI.recalculate(F, DT, PDT, DF);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000134 return false;
135}
136
Matt Arsenault5e1c96a2014-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 Grosserf96b0062010-07-22 07:46:31 +0000146 AU.setPreservesAll();
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000147 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000148 AU.addRequired<PostDominatorTreeWrapperPass>();
Hongbin Zheng15969222016-02-25 17:54:15 +0000149 AU.addRequired<DominanceFrontierWrapperPass>();
Tobias Grosserf96b0062010-07-22 07:46:31 +0000150}
151
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000152void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
153 RI.print(OS);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000154}
155
Aaron Ballman1d03d382017-10-15 14:32:27 +0000156#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000157LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000158 RI.dump();
Tobias Grosserf96b0062010-07-22 07:46:31 +0000159}
NAKAMURA Takumi8f64ffd2014-07-20 00:00:42 +0000160#endif
Tobias Grosserf96b0062010-07-22 07:46:31 +0000161
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000162char RegionInfoPass::ID = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000163
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000164INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson2ab36d32010-10-12 19:48:12 +0000165 "Detect single entry single exit regions", true, true)
Chandler Carruth7f2eff72014-01-13 13:07:17 +0000166INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Hongbin Zheng5d7472e2016-02-25 17:54:07 +0000167INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
Hongbin Zheng15969222016-02-25 17:54:15 +0000168INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000169INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersonce665bd2010-10-07 22:25:06 +0000170 "Detect single entry single exit regions", true, true)
Tobias Grosserf96b0062010-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 Zelenko01187b32017-06-27 21:52:05 +0000177
Tobias Grosserf96b0062010-07-22 07:46:31 +0000178 FunctionPass *createRegionInfoPass() {
Matt Arsenault5e1c96a2014-07-19 18:29:29 +0000179 return new RegionInfoPass();
Tobias Grosserf96b0062010-07-22 07:46:31 +0000180 }
Eugene Zelenko01187b32017-06-27 21:52:05 +0000181
182} // end namespace llvm
Tobias Grosserf96b0062010-07-22 07:46:31 +0000183
Hongbin Zhengedc89ca2016-02-25 17:54:25 +0000184//===----------------------------------------------------------------------===//
185// RegionInfoAnalysis implementation
186//
187
Chandler Carruth33d56812016-11-23 17:53:26 +0000188AnalysisKey RegionInfoAnalysis::Key;
NAKAMURA Takumid9b6afb2016-02-28 17:17:00 +0000189
Sean Silva20b343c2016-08-09 00:28:15 +0000190RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
Hongbin Zhengedc89ca2016-02-25 17:54:25 +0000191 RegionInfo RI;
Chandler Carruth8e27cb22016-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 Zhengedc89ca2016-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 Carruth8e27cb22016-03-11 11:05:24 +0000203PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
204 FunctionAnalysisManager &AM) {
Hongbin Zhengedc89ca2016-02-25 17:54:25 +0000205 OS << "Region Tree for function: " << F.getName() << "\n";
Chandler Carruth8e27cb22016-03-11 11:05:24 +0000206 AM.getResult<RegionInfoAnalysis>(F).print(OS);
Hongbin Zhengedc89ca2016-02-25 17:54:25 +0000207
208 return PreservedAnalyses::all();
209}
210
211PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
Sean Silva20b343c2016-08-09 00:28:15 +0000212 FunctionAnalysisManager &AM) {
Chandler Carruth8e27cb22016-03-11 11:05:24 +0000213 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
Hongbin Zhengedc89ca2016-02-25 17:54:25 +0000214
215 return PreservedAnalyses::all();
216}