blob: f59d267303276759d90cf1e11e580789fcc28f62 [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"
Tobias Grosser336734a2010-07-22 07:46:31 +000018#include "llvm/Support/CommandLine.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000019#include "llvm/Support/Debug.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/Support/ErrorHandling.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000021#include <algorithm>
David Blaikieec649ac2014-04-15 18:32:43 +000022#include <iterator>
Bill Wendling570d3022013-08-21 21:14:19 +000023#include <set>
Michael Kruse20dcc9f2015-08-10 13:21:59 +000024#ifndef NDEBUG
25#include "llvm/Analysis/RegionPrinter.h"
26#endif
Tobias Grosser336734a2010-07-22 07:46:31 +000027
28using namespace llvm;
29
Chandler Carruthf1221bd2014-04-22 02:48:03 +000030#define DEBUG_TYPE "region"
31
Matt Arsenault1b8d8372014-07-19 18:29:29 +000032namespace llvm {
33template class RegionBase<RegionTraits<Function>>;
34template class RegionNodeBase<RegionTraits<Function>>;
35template class RegionInfoBase<RegionTraits<Function>>;
36}
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
49
50static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
51 cl::location(RegionInfo::printStyle),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000052 cl::Hidden,
Tobias Grosser336734a2010-07-22 07:46:31 +000053 cl::desc("style of printing regions"),
54 cl::values(
Tobias Grosser8b304ff2011-04-04 07:19:18 +000055 clEnumValN(Region::PrintNone, "none", "print no details"),
56 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng14c05c42012-08-27 13:49:24 +000057 "print regions in detail with block_iterator"),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000058 clEnumValN(Region::PrintRN, "rn",
59 "print regions in detail with element_iterator"),
Tobias Grosser336734a2010-07-22 07:46:31 +000060 clEnumValEnd));
Matt Arsenault1b8d8372014-07-19 18:29:29 +000061
62
Tobias Grosser336734a2010-07-22 07:46:31 +000063//===----------------------------------------------------------------------===//
Matt Arsenault1b8d8372014-07-19 18:29:29 +000064// Region implementation
65//
Tobias Grosser336734a2010-07-22 07:46:31 +000066
Matt Arsenault1b8d8372014-07-19 18:29:29 +000067Region::Region(BasicBlock *Entry, BasicBlock *Exit,
68 RegionInfo* RI,
69 DominatorTree *DT, Region *Parent) :
70 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
Daniel Dunbar18e39ce2010-07-28 20:28:50 +000071
Tobias Grosser336734a2010-07-22 07:46:31 +000072}
73
Matt Arsenault1b8d8372014-07-19 18:29:29 +000074Region::~Region() { }
Tobias Grosser336734a2010-07-22 07:46:31 +000075
76//===----------------------------------------------------------------------===//
77// RegionInfo implementation
78//
79
Matt Arsenault1b8d8372014-07-19 18:29:29 +000080RegionInfo::RegionInfo() :
81 RegionInfoBase<RegionTraits<Function>>() {
82
Tobias Grosser336734a2010-07-22 07:46:31 +000083}
84
Matt Arsenault1b8d8372014-07-19 18:29:29 +000085RegionInfo::~RegionInfo() {
Tobias Grosser336734a2010-07-22 07:46:31 +000086
Tobias Grosser336734a2010-07-22 07:46:31 +000087}
88
89void 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
123RegionInfoPass::~RegionInfoPass() {
124
125}
126
127bool RegionInfoPass::runOnFunction(Function &F) {
128 releaseMemory();
129
130 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
131 auto PDT = &getAnalysis<PostDominatorTree>();
132 auto DF = &getAnalysis<DominanceFrontier>();
133
134 RI.recalculate(F, DT, PDT, DF);
Tobias Grosser336734a2010-07-22 07:46:31 +0000135 return false;
136}
137
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000138void RegionInfoPass::releaseMemory() {
139 RI.releaseMemory();
140}
141
142void RegionInfoPass::verifyAnalysis() const {
143 RI.verifyAnalysis();
144}
145
146void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000147 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000148 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000149 AU.addRequired<PostDominatorTree>();
150 AU.addRequired<DominanceFrontier>();
151}
152
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000153void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
154 RI.print(OS);
Tobias Grosser336734a2010-07-22 07:46:31 +0000155}
156
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000157#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000158void RegionInfoPass::dump() const {
159 RI.dump();
Tobias Grosser336734a2010-07-22 07:46:31 +0000160}
NAKAMURA Takumi118b0c72014-07-20 00:00:42 +0000161#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000162
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000163char RegionInfoPass::ID = 0;
Tobias Grosser336734a2010-07-22 07:46:31 +0000164
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000165INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
Owen Anderson8ac477f2010-10-12 19:48:12 +0000166 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000167INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000168INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
169INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000170INITIALIZE_PASS_END(RegionInfoPass, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000171 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000172
173// Create methods available outside of this file, to use them
174// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
175// the link time optimization.
176
177namespace llvm {
178 FunctionPass *createRegionInfoPass() {
Matt Arsenault1b8d8372014-07-19 18:29:29 +0000179 return new RegionInfoPass();
Tobias Grosser336734a2010-07-22 07:46:31 +0000180 }
181}
182