1336734acSTobias Grosser //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2336734acSTobias Grosser //
3336734acSTobias Grosser //                     The LLVM Compiler Infrastructure
4336734acSTobias Grosser //
5336734acSTobias Grosser // This file is distributed under the University of Illinois Open Source
6336734acSTobias Grosser // License. See LICENSE.TXT for details.
7336734acSTobias Grosser //
8336734acSTobias Grosser //===----------------------------------------------------------------------===//
9336734acSTobias Grosser // Detects single entry single exit regions in the control flow graph.
10336734acSTobias Grosser //===----------------------------------------------------------------------===//
11336734acSTobias Grosser 
12570d3020SBill Wendling #define DEBUG_TYPE "region"
13336734acSTobias Grosser #include "llvm/Analysis/RegionInfo.h"
14336734acSTobias Grosser #include "llvm/ADT/PostOrderIterator.h"
15336734acSTobias Grosser #include "llvm/ADT/Statistic.h"
16ed0881b2SChandler Carruth #include "llvm/Analysis/LoopInfo.h"
17ed0881b2SChandler Carruth #include "llvm/Analysis/RegionIterator.h"
18336734acSTobias Grosser #include "llvm/Support/CommandLine.h"
19336734acSTobias Grosser #include "llvm/Support/Debug.h"
208a8cd2baSChandler Carruth #include "llvm/Support/ErrorHandling.h"
21336734acSTobias Grosser #include <algorithm>
22570d3020SBill Wendling #include <set>
23336734acSTobias Grosser 
24336734acSTobias Grosser using namespace llvm;
25336734acSTobias Grosser 
26336734acSTobias Grosser // Always verify if expensive checking is enabled.
27336734acSTobias Grosser #ifdef XDEBUG
28abfafadfSDan Gohman static bool VerifyRegionInfo = true;
29336734acSTobias Grosser #else
30abfafadfSDan Gohman static bool VerifyRegionInfo = false;
31336734acSTobias Grosser #endif
32336734acSTobias Grosser 
33336734acSTobias Grosser static cl::opt<bool,true>
34336734acSTobias Grosser VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
35336734acSTobias Grosser                 cl::desc("Verify region info (time consuming)"));
36336734acSTobias Grosser 
37336734acSTobias Grosser STATISTIC(numRegions,       "The # of regions");
38336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions");
39336734acSTobias Grosser 
408b304ff9STobias Grosser static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
418b304ff9STobias Grosser   cl::Hidden,
42336734acSTobias Grosser   cl::desc("style of printing regions"),
43336734acSTobias Grosser   cl::values(
448b304ff9STobias Grosser     clEnumValN(Region::PrintNone, "none",  "print no details"),
458b304ff9STobias Grosser     clEnumValN(Region::PrintBB, "bb",
4614c05c40SHongbin Zheng                "print regions in detail with block_iterator"),
478b304ff9STobias Grosser     clEnumValN(Region::PrintRN, "rn",
488b304ff9STobias Grosser                "print regions in detail with element_iterator"),
49336734acSTobias Grosser     clEnumValEnd));
50336734acSTobias Grosser //===----------------------------------------------------------------------===//
51336734acSTobias Grosser /// Region Implementation
52336734acSTobias Grosser Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
53336734acSTobias Grosser                DominatorTree *dt, Region *Parent)
54336734acSTobias Grosser                : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
55336734acSTobias Grosser 
56336734acSTobias Grosser Region::~Region() {
5718e39cecSDaniel Dunbar   // Free the cached nodes.
5818e39cecSDaniel Dunbar   for (BBNodeMapT::iterator it = BBNodeMap.begin(),
5918e39cecSDaniel Dunbar          ie = BBNodeMap.end(); it != ie; ++it)
6018e39cecSDaniel Dunbar     delete it->second;
6118e39cecSDaniel Dunbar 
62336734acSTobias Grosser   // Only clean the cache for this Region. Caches of child Regions will be
63336734acSTobias Grosser   // cleaned when the child Regions are deleted.
64336734acSTobias Grosser   BBNodeMap.clear();
65336734acSTobias Grosser 
66336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
67336734acSTobias Grosser     delete *I;
68336734acSTobias Grosser }
69336734acSTobias Grosser 
70648594c9STobias Grosser void Region::replaceEntry(BasicBlock *BB) {
71648594c9STobias Grosser   entry.setPointer(BB);
72648594c9STobias Grosser }
73648594c9STobias Grosser 
74648594c9STobias Grosser void Region::replaceExit(BasicBlock *BB) {
75648594c9STobias Grosser   assert(exit && "No exit to replace!");
76648594c9STobias Grosser   exit = BB;
77648594c9STobias Grosser }
78648594c9STobias Grosser 
79141cc3e8STobias Grosser void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
80141cc3e8STobias Grosser   std::vector<Region *> RegionQueue;
81141cc3e8STobias Grosser   BasicBlock *OldEntry = getEntry();
82141cc3e8STobias Grosser 
83141cc3e8STobias Grosser   RegionQueue.push_back(this);
84141cc3e8STobias Grosser   while (!RegionQueue.empty()) {
85141cc3e8STobias Grosser     Region *R = RegionQueue.back();
86141cc3e8STobias Grosser     RegionQueue.pop_back();
87141cc3e8STobias Grosser 
88141cc3e8STobias Grosser     R->replaceEntry(NewEntry);
89141cc3e8STobias Grosser     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
90141cc3e8STobias Grosser       if ((*RI)->getEntry() == OldEntry)
91141cc3e8STobias Grosser         RegionQueue.push_back(*RI);
92141cc3e8STobias Grosser   }
93141cc3e8STobias Grosser }
94141cc3e8STobias Grosser 
95141cc3e8STobias Grosser void Region::replaceExitRecursive(BasicBlock *NewExit) {
96141cc3e8STobias Grosser   std::vector<Region *> RegionQueue;
97141cc3e8STobias Grosser   BasicBlock *OldExit = getExit();
98141cc3e8STobias Grosser 
99141cc3e8STobias Grosser   RegionQueue.push_back(this);
100141cc3e8STobias Grosser   while (!RegionQueue.empty()) {
101141cc3e8STobias Grosser     Region *R = RegionQueue.back();
102141cc3e8STobias Grosser     RegionQueue.pop_back();
103141cc3e8STobias Grosser 
104141cc3e8STobias Grosser     R->replaceExit(NewExit);
105141cc3e8STobias Grosser     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
106141cc3e8STobias Grosser       if ((*RI)->getExit() == OldExit)
107141cc3e8STobias Grosser         RegionQueue.push_back(*RI);
108141cc3e8STobias Grosser   }
109141cc3e8STobias Grosser }
110141cc3e8STobias Grosser 
111336734acSTobias Grosser bool Region::contains(const BasicBlock *B) const {
112336734acSTobias Grosser   BasicBlock *BB = const_cast<BasicBlock*>(B);
113336734acSTobias Grosser 
114a7ddc982STobias Grosser   if (!DT->getNode(BB))
115a7ddc982STobias Grosser     return false;
116336734acSTobias Grosser 
117336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
118336734acSTobias Grosser 
119336734acSTobias Grosser   // Toplevel region.
120336734acSTobias Grosser   if (!exit)
121336734acSTobias Grosser     return true;
122336734acSTobias Grosser 
123336734acSTobias Grosser   return (DT->dominates(entry, BB)
124336734acSTobias Grosser     && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
125336734acSTobias Grosser }
126336734acSTobias Grosser 
1271bec81a8STobias Grosser bool Region::contains(const Loop *L) const {
1281bec81a8STobias Grosser   // BBs that are not part of any loop are element of the Loop
1291bec81a8STobias Grosser   // described by the NULL pointer. This loop is not part of any region,
1301bec81a8STobias Grosser   // except if the region describes the whole function.
131*9f008867SCraig Topper   if (!L)
132*9f008867SCraig Topper     return getExit() == nullptr;
1331bec81a8STobias Grosser 
1341bec81a8STobias Grosser   if (!contains(L->getHeader()))
1351bec81a8STobias Grosser     return false;
1361bec81a8STobias Grosser 
1371bec81a8STobias Grosser   SmallVector<BasicBlock *, 8> ExitingBlocks;
1381bec81a8STobias Grosser   L->getExitingBlocks(ExitingBlocks);
1391bec81a8STobias Grosser 
1401bec81a8STobias Grosser   for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
1411bec81a8STobias Grosser        BE = ExitingBlocks.end(); BI != BE; ++BI)
1421bec81a8STobias Grosser     if (!contains(*BI))
1431bec81a8STobias Grosser       return false;
1441bec81a8STobias Grosser 
1451bec81a8STobias Grosser   return true;
1461bec81a8STobias Grosser }
1471bec81a8STobias Grosser 
1481bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(Loop *L) const {
1491bec81a8STobias Grosser   if (!contains(L))
150*9f008867SCraig Topper     return nullptr;
1511bec81a8STobias Grosser 
1521bec81a8STobias Grosser   while (L && contains(L->getParentLoop())) {
1531bec81a8STobias Grosser     L = L->getParentLoop();
1541bec81a8STobias Grosser   }
1551bec81a8STobias Grosser 
1561bec81a8STobias Grosser   return L;
1571bec81a8STobias Grosser }
1581bec81a8STobias Grosser 
1591bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
1601bec81a8STobias Grosser   assert(LI && BB && "LI and BB cannot be null!");
1611bec81a8STobias Grosser   Loop *L = LI->getLoopFor(BB);
1621bec81a8STobias Grosser   return outermostLoopInRegion(L);
1631bec81a8STobias Grosser }
1641bec81a8STobias Grosser 
165b1d11c19STobias Grosser BasicBlock *Region::getEnteringBlock() const {
166b1d11c19STobias Grosser   BasicBlock *entry = getEntry();
167b1d11c19STobias Grosser   BasicBlock *Pred;
168*9f008867SCraig Topper   BasicBlock *enteringBlock = nullptr;
169336734acSTobias Grosser 
170336734acSTobias Grosser   for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
1717fbe6cb4STobias Grosser        ++PI) {
172b1d11c19STobias Grosser     Pred = *PI;
1737fbe6cb4STobias Grosser     if (DT->getNode(Pred) && !contains(Pred)) {
174b1d11c19STobias Grosser       if (enteringBlock)
175*9f008867SCraig Topper         return nullptr;
176b1d11c19STobias Grosser 
177b1d11c19STobias Grosser       enteringBlock = Pred;
178336734acSTobias Grosser     }
1797fbe6cb4STobias Grosser   }
180336734acSTobias Grosser 
181b1d11c19STobias Grosser   return enteringBlock;
182b1d11c19STobias Grosser }
183b1d11c19STobias Grosser 
184b1d11c19STobias Grosser BasicBlock *Region::getExitingBlock() const {
185b1d11c19STobias Grosser   BasicBlock *exit = getExit();
186b1d11c19STobias Grosser   BasicBlock *Pred;
187*9f008867SCraig Topper   BasicBlock *exitingBlock = nullptr;
188b1d11c19STobias Grosser 
189b1d11c19STobias Grosser   if (!exit)
190*9f008867SCraig Topper     return nullptr;
191336734acSTobias Grosser 
192336734acSTobias Grosser   for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
193b1d11c19STobias Grosser        ++PI) {
194b1d11c19STobias Grosser     Pred = *PI;
195b1d11c19STobias Grosser     if (contains(Pred)) {
196b1d11c19STobias Grosser       if (exitingBlock)
197*9f008867SCraig Topper         return nullptr;
198b1d11c19STobias Grosser 
199b1d11c19STobias Grosser       exitingBlock = Pred;
200336734acSTobias Grosser     }
201336734acSTobias Grosser   }
202336734acSTobias Grosser 
203b1d11c19STobias Grosser   return exitingBlock;
204b1d11c19STobias Grosser }
205b1d11c19STobias Grosser 
206b1d11c19STobias Grosser bool Region::isSimple() const {
207b1d11c19STobias Grosser   return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
208336734acSTobias Grosser }
209336734acSTobias Grosser 
2101bec81a8STobias Grosser std::string Region::getNameStr() const {
2111bec81a8STobias Grosser   std::string exitName;
2121bec81a8STobias Grosser   std::string entryName;
2131bec81a8STobias Grosser 
2141bec81a8STobias Grosser   if (getEntry()->getName().empty()) {
2151bec81a8STobias Grosser     raw_string_ostream OS(entryName);
2161bec81a8STobias Grosser 
217d48cdbf0SChandler Carruth     getEntry()->printAsOperand(OS, false);
2181bec81a8STobias Grosser   } else
219184e3ceeSBenjamin Kramer     entryName = getEntry()->getName();
2201bec81a8STobias Grosser 
2211bec81a8STobias Grosser   if (getExit()) {
2221bec81a8STobias Grosser     if (getExit()->getName().empty()) {
2231bec81a8STobias Grosser       raw_string_ostream OS(exitName);
2241bec81a8STobias Grosser 
225d48cdbf0SChandler Carruth       getExit()->printAsOperand(OS, false);
2261bec81a8STobias Grosser     } else
227184e3ceeSBenjamin Kramer       exitName = getExit()->getName();
2281bec81a8STobias Grosser   } else
2291bec81a8STobias Grosser     exitName = "<Function Return>";
2301bec81a8STobias Grosser 
2311bec81a8STobias Grosser   return entryName + " => " + exitName;
2321bec81a8STobias Grosser }
2331bec81a8STobias Grosser 
234336734acSTobias Grosser void Region::verifyBBInRegion(BasicBlock *BB) const {
235336734acSTobias Grosser   if (!contains(BB))
236336734acSTobias Grosser     llvm_unreachable("Broken region found!");
237336734acSTobias Grosser 
238336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
239336734acSTobias Grosser 
240336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
241336734acSTobias Grosser     if (!contains(*SI) && exit != *SI)
242336734acSTobias Grosser       llvm_unreachable("Broken region found!");
243336734acSTobias Grosser 
244336734acSTobias Grosser   if (entry != BB)
245336734acSTobias Grosser     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
246336734acSTobias Grosser       if (!contains(*SI))
247336734acSTobias Grosser         llvm_unreachable("Broken region found!");
248336734acSTobias Grosser }
249336734acSTobias Grosser 
250336734acSTobias Grosser void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
251336734acSTobias Grosser   BasicBlock *exit = getExit();
252336734acSTobias Grosser 
253336734acSTobias Grosser   visited->insert(BB);
254336734acSTobias Grosser 
255336734acSTobias Grosser   verifyBBInRegion(BB);
256336734acSTobias Grosser 
257336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
258336734acSTobias Grosser     if (*SI != exit && visited->find(*SI) == visited->end())
259336734acSTobias Grosser         verifyWalk(*SI, visited);
260336734acSTobias Grosser }
261336734acSTobias Grosser 
262336734acSTobias Grosser void Region::verifyRegion() const {
263336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive
264336734acSTobias Grosser   // check will be invoked by PassManager.
265336734acSTobias Grosser   if (!VerifyRegionInfo) return;
266336734acSTobias Grosser 
267336734acSTobias Grosser   std::set<BasicBlock*> visited;
268336734acSTobias Grosser   verifyWalk(getEntry(), &visited);
269336734acSTobias Grosser }
270336734acSTobias Grosser 
271336734acSTobias Grosser void Region::verifyRegionNest() const {
272336734acSTobias Grosser   for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
273336734acSTobias Grosser     (*RI)->verifyRegionNest();
274336734acSTobias Grosser 
275336734acSTobias Grosser   verifyRegion();
276336734acSTobias Grosser }
277336734acSTobias Grosser 
278336734acSTobias Grosser Region::element_iterator Region::element_begin() {
279336734acSTobias Grosser   return GraphTraits<Region*>::nodes_begin(this);
280336734acSTobias Grosser }
281336734acSTobias Grosser 
282336734acSTobias Grosser Region::element_iterator Region::element_end() {
283336734acSTobias Grosser   return GraphTraits<Region*>::nodes_end(this);
284336734acSTobias Grosser }
285336734acSTobias Grosser 
286336734acSTobias Grosser Region::const_element_iterator Region::element_begin() const {
287336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_begin(this);
288336734acSTobias Grosser }
289336734acSTobias Grosser 
290336734acSTobias Grosser Region::const_element_iterator Region::element_end() const {
291336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_end(this);
292336734acSTobias Grosser }
293336734acSTobias Grosser 
294336734acSTobias Grosser Region* Region::getSubRegionNode(BasicBlock *BB) const {
295336734acSTobias Grosser   Region *R = RI->getRegionFor(BB);
296336734acSTobias Grosser 
297336734acSTobias Grosser   if (!R || R == this)
298*9f008867SCraig Topper     return nullptr;
299336734acSTobias Grosser 
300336734acSTobias Grosser   // If we pass the BB out of this region, that means our code is broken.
301336734acSTobias Grosser   assert(contains(R) && "BB not in current region!");
302336734acSTobias Grosser 
303336734acSTobias Grosser   while (contains(R->getParent()) && R->getParent() != this)
304336734acSTobias Grosser     R = R->getParent();
305336734acSTobias Grosser 
306336734acSTobias Grosser   if (R->getEntry() != BB)
307*9f008867SCraig Topper     return nullptr;
308336734acSTobias Grosser 
309336734acSTobias Grosser   return R;
310336734acSTobias Grosser }
311336734acSTobias Grosser 
312336734acSTobias Grosser RegionNode* Region::getBBNode(BasicBlock *BB) const {
313336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
314336734acSTobias Grosser 
315336734acSTobias Grosser   BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
316336734acSTobias Grosser 
317336734acSTobias Grosser   if (at != BBNodeMap.end())
318336734acSTobias Grosser     return at->second;
319336734acSTobias Grosser 
320336734acSTobias Grosser   RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
321336734acSTobias Grosser   BBNodeMap.insert(std::make_pair(BB, NewNode));
322336734acSTobias Grosser   return NewNode;
323336734acSTobias Grosser }
324336734acSTobias Grosser 
325336734acSTobias Grosser RegionNode* Region::getNode(BasicBlock *BB) const {
326336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
327336734acSTobias Grosser   if (Region* Child = getSubRegionNode(BB))
328336734acSTobias Grosser     return Child->getNode();
329336734acSTobias Grosser 
330336734acSTobias Grosser   return getBBNode(BB);
331336734acSTobias Grosser }
332336734acSTobias Grosser 
333336734acSTobias Grosser void Region::transferChildrenTo(Region *To) {
334336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I) {
335336734acSTobias Grosser     (*I)->parent = To;
336336734acSTobias Grosser     To->children.push_back(*I);
337336734acSTobias Grosser   }
338336734acSTobias Grosser   children.clear();
339336734acSTobias Grosser }
340336734acSTobias Grosser 
341bf984fd7STobias Grosser void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
342*9f008867SCraig Topper   assert(!SubRegion->parent && "SubRegion already has a parent!");
343bf984fd7STobias Grosser   assert(std::find(begin(), end(), SubRegion) == children.end()
344bf984fd7STobias Grosser          && "Subregion already exists!");
345bf984fd7STobias Grosser 
346336734acSTobias Grosser   SubRegion->parent = this;
347336734acSTobias Grosser   children.push_back(SubRegion);
348bf984fd7STobias Grosser 
349bf984fd7STobias Grosser   if (!moveChildren)
350bf984fd7STobias Grosser     return;
351bf984fd7STobias Grosser 
352bf984fd7STobias Grosser   assert(SubRegion->children.size() == 0
353bf984fd7STobias Grosser          && "SubRegions that contain children are not supported");
354bf984fd7STobias Grosser 
355bf984fd7STobias Grosser   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
356bf984fd7STobias Grosser     if (!(*I)->isSubRegion()) {
357bf984fd7STobias Grosser       BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
358bf984fd7STobias Grosser 
359bf984fd7STobias Grosser       if (SubRegion->contains(BB))
360bf984fd7STobias Grosser         RI->setRegionFor(BB, SubRegion);
361bf984fd7STobias Grosser     }
362bf984fd7STobias Grosser 
363bf984fd7STobias Grosser   std::vector<Region*> Keep;
364bf984fd7STobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
365bf984fd7STobias Grosser     if (SubRegion->contains(*I) && *I != SubRegion) {
366bf984fd7STobias Grosser       SubRegion->children.push_back(*I);
367bf984fd7STobias Grosser       (*I)->parent = SubRegion;
368bf984fd7STobias Grosser     } else
369bf984fd7STobias Grosser       Keep.push_back(*I);
370bf984fd7STobias Grosser 
371bf984fd7STobias Grosser   children.clear();
372bf984fd7STobias Grosser   children.insert(children.begin(), Keep.begin(), Keep.end());
373336734acSTobias Grosser }
374336734acSTobias Grosser 
375336734acSTobias Grosser 
376336734acSTobias Grosser Region *Region::removeSubRegion(Region *Child) {
377336734acSTobias Grosser   assert(Child->parent == this && "Child is not a child of this region!");
378*9f008867SCraig Topper   Child->parent = nullptr;
379336734acSTobias Grosser   RegionSet::iterator I = std::find(children.begin(), children.end(), Child);
380336734acSTobias Grosser   assert(I != children.end() && "Region does not exit. Unable to remove.");
381336734acSTobias Grosser   children.erase(children.begin()+(I-begin()));
382336734acSTobias Grosser   return Child;
383336734acSTobias Grosser }
384336734acSTobias Grosser 
385336734acSTobias Grosser unsigned Region::getDepth() const {
386336734acSTobias Grosser   unsigned Depth = 0;
387336734acSTobias Grosser 
388*9f008867SCraig Topper   for (Region *R = parent; R != nullptr; R = R->parent)
389336734acSTobias Grosser     ++Depth;
390336734acSTobias Grosser 
391336734acSTobias Grosser   return Depth;
392336734acSTobias Grosser }
393336734acSTobias Grosser 
394a8677226STobias Grosser Region *Region::getExpandedRegion() const {
395a8677226STobias Grosser   unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
396a8677226STobias Grosser 
397a8677226STobias Grosser   if (NumSuccessors == 0)
398*9f008867SCraig Topper     return nullptr;
399a8677226STobias Grosser 
400a8677226STobias Grosser   for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
401a8677226STobias Grosser        PI != PE; ++PI)
402a8677226STobias Grosser     if (!DT->dominates(getEntry(), *PI))
403*9f008867SCraig Topper       return nullptr;
404a8677226STobias Grosser 
405a8677226STobias Grosser   Region *R = RI->getRegionFor(exit);
406a8677226STobias Grosser 
407a8677226STobias Grosser   if (R->getEntry() != exit) {
408a8677226STobias Grosser     if (exit->getTerminator()->getNumSuccessors() == 1)
409a8677226STobias Grosser       return new Region(getEntry(), *succ_begin(exit), RI, DT);
410a8677226STobias Grosser     else
411*9f008867SCraig Topper       return nullptr;
412a8677226STobias Grosser   }
413a8677226STobias Grosser 
414a8677226STobias Grosser   while (R->getParent() && R->getParent()->getEntry() == exit)
415a8677226STobias Grosser     R = R->getParent();
416a8677226STobias Grosser 
417a8677226STobias Grosser   if (!DT->dominates(getEntry(), R->getExit()))
418a8677226STobias Grosser     for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
419a8677226STobias Grosser          PI != PE; ++PI)
420a8677226STobias Grosser     if (!DT->dominates(R->getExit(), *PI))
421*9f008867SCraig Topper       return nullptr;
422a8677226STobias Grosser 
423a8677226STobias Grosser   return new Region(getEntry(), R->getExit(), RI, DT);
424a8677226STobias Grosser }
425a8677226STobias Grosser 
4268b304ff9STobias Grosser void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
4278b304ff9STobias Grosser                    enum PrintStyle Style) const {
428336734acSTobias Grosser   if (print_tree)
429336734acSTobias Grosser     OS.indent(level*2) << "[" << level << "] " << getNameStr();
430336734acSTobias Grosser   else
431336734acSTobias Grosser     OS.indent(level*2) << getNameStr();
432336734acSTobias Grosser 
433336734acSTobias Grosser   OS << "\n";
434336734acSTobias Grosser 
435336734acSTobias Grosser 
4368b304ff9STobias Grosser   if (Style != PrintNone) {
437336734acSTobias Grosser     OS.indent(level*2) << "{\n";
438336734acSTobias Grosser     OS.indent(level*2 + 2);
439336734acSTobias Grosser 
4408b304ff9STobias Grosser     if (Style == PrintBB) {
4414abf9d3aSTobias Grosser       for (const auto &BB : blocks())
4424abf9d3aSTobias Grosser         OS << BB->getName() << ", "; // TODO: remove the last ","
4438b304ff9STobias Grosser     } else if (Style == PrintRN) {
444336734acSTobias Grosser       for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
445336734acSTobias Grosser         OS << **I << ", "; // TODO: remove the last ",
446336734acSTobias Grosser     }
447336734acSTobias Grosser 
448336734acSTobias Grosser     OS << "\n";
449336734acSTobias Grosser   }
450336734acSTobias Grosser 
451336734acSTobias Grosser   if (print_tree)
452336734acSTobias Grosser     for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
4538b304ff9STobias Grosser       (*RI)->print(OS, print_tree, level+1, Style);
454336734acSTobias Grosser 
4558b304ff9STobias Grosser   if (Style != PrintNone)
456336734acSTobias Grosser     OS.indent(level*2) << "} \n";
457336734acSTobias Grosser }
458336734acSTobias Grosser 
45949d684e1SManman Ren #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
460336734acSTobias Grosser void Region::dump() const {
4618b304ff9STobias Grosser   print(dbgs(), true, getDepth(), printStyle.getValue());
462336734acSTobias Grosser }
463c3366cceSManman Ren #endif
464336734acSTobias Grosser 
465336734acSTobias Grosser void Region::clearNodeCache() {
466e910b9d9STobias Grosser   // Free the cached nodes.
467e910b9d9STobias Grosser   for (BBNodeMapT::iterator I = BBNodeMap.begin(),
4684c71c117STobias Grosser        IE = BBNodeMap.end(); I != IE; ++I)
469e910b9d9STobias Grosser     delete I->second;
470e910b9d9STobias Grosser 
471336734acSTobias Grosser   BBNodeMap.clear();
472336734acSTobias Grosser   for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
473336734acSTobias Grosser     (*RI)->clearNodeCache();
474336734acSTobias Grosser }
475336734acSTobias Grosser 
476336734acSTobias Grosser //===----------------------------------------------------------------------===//
477336734acSTobias Grosser // RegionInfo implementation
478336734acSTobias Grosser //
479336734acSTobias Grosser 
480336734acSTobias Grosser bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
481336734acSTobias Grosser                                      BasicBlock *exit) const {
48207c8ad54SGabor Greif   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
48307c8ad54SGabor Greif     BasicBlock *P = *PI;
48407c8ad54SGabor Greif     if (DT->dominates(entry, P) && !DT->dominates(exit, P))
485336734acSTobias Grosser       return false;
48607c8ad54SGabor Greif   }
487336734acSTobias Grosser   return true;
488336734acSTobias Grosser }
489336734acSTobias Grosser 
490336734acSTobias Grosser bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
491336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
492336734acSTobias Grosser   typedef DominanceFrontier::DomSetType DST;
493336734acSTobias Grosser 
494d9f48ecbSGabor Greif   DST *entrySuccs = &DF->find(entry)->second;
495336734acSTobias Grosser 
496336734acSTobias Grosser   // Exit is the header of a loop that contains the entry. In this case,
497336734acSTobias Grosser   // the dominance frontier must only contain the exit.
498336734acSTobias Grosser   if (!DT->dominates(entry, exit)) {
499336734acSTobias Grosser     for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
500336734acSTobias Grosser          SI != SE; ++SI)
501336734acSTobias Grosser       if (*SI != exit && *SI != entry)
502336734acSTobias Grosser         return false;
503336734acSTobias Grosser 
504336734acSTobias Grosser     return true;
505336734acSTobias Grosser   }
506336734acSTobias Grosser 
507d9f48ecbSGabor Greif   DST *exitSuccs = &DF->find(exit)->second;
508336734acSTobias Grosser 
509336734acSTobias Grosser   // Do not allow edges leaving the region.
510336734acSTobias Grosser   for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
511336734acSTobias Grosser        SI != SE; ++SI) {
512336734acSTobias Grosser     if (*SI == exit || *SI == entry)
513336734acSTobias Grosser       continue;
514336734acSTobias Grosser     if (exitSuccs->find(*SI) == exitSuccs->end())
515336734acSTobias Grosser       return false;
516336734acSTobias Grosser     if (!isCommonDomFrontier(*SI, entry, exit))
517336734acSTobias Grosser       return false;
518336734acSTobias Grosser   }
519336734acSTobias Grosser 
520336734acSTobias Grosser   // Do not allow edges pointing into the region.
521336734acSTobias Grosser   for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
522336734acSTobias Grosser        SI != SE; ++SI)
523b3aa6c71SDan Gohman     if (DT->properlyDominates(entry, *SI) && *SI != exit)
524336734acSTobias Grosser       return false;
525336734acSTobias Grosser 
526336734acSTobias Grosser 
527336734acSTobias Grosser   return true;
528336734acSTobias Grosser }
529336734acSTobias Grosser 
530336734acSTobias Grosser void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
531336734acSTobias Grosser                              BBtoBBMap *ShortCut) const {
532336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
533336734acSTobias Grosser 
534336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(exit);
535336734acSTobias Grosser 
536336734acSTobias Grosser   if (e == ShortCut->end())
537336734acSTobias Grosser     // No further region at exit available.
538336734acSTobias Grosser     (*ShortCut)[entry] = exit;
539336734acSTobias Grosser   else {
540336734acSTobias Grosser     // We found a region e that starts at exit. Therefore (entry, e->second)
541336734acSTobias Grosser     // is also a region, that is larger than (entry, exit). Insert the
542336734acSTobias Grosser     // larger one.
543336734acSTobias Grosser     BasicBlock *BB = e->second;
544336734acSTobias Grosser     (*ShortCut)[entry] = BB;
545336734acSTobias Grosser   }
546336734acSTobias Grosser }
547336734acSTobias Grosser 
548336734acSTobias Grosser DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
549336734acSTobias Grosser                                         BBtoBBMap *ShortCut) const {
550336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
551336734acSTobias Grosser 
552336734acSTobias Grosser   if (e == ShortCut->end())
553336734acSTobias Grosser     return N->getIDom();
554336734acSTobias Grosser 
555336734acSTobias Grosser   return PDT->getNode(e->second)->getIDom();
556336734acSTobias Grosser }
557336734acSTobias Grosser 
558336734acSTobias Grosser bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
559336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
560336734acSTobias Grosser 
561336734acSTobias Grosser   unsigned num_successors = succ_end(entry) - succ_begin(entry);
562336734acSTobias Grosser 
563336734acSTobias Grosser   if (num_successors <= 1 && exit == *(succ_begin(entry)))
564336734acSTobias Grosser     return true;
565336734acSTobias Grosser 
566336734acSTobias Grosser   return false;
567336734acSTobias Grosser }
568336734acSTobias Grosser 
569336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) {
570336734acSTobias Grosser   ++numRegions;
571336734acSTobias Grosser 
572336734acSTobias Grosser   // TODO: Slow. Should only be enabled if -stats is used.
573336734acSTobias Grosser   if (R->isSimple()) ++numSimpleRegions;
574336734acSTobias Grosser }
575336734acSTobias Grosser 
576336734acSTobias Grosser Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
577336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
578336734acSTobias Grosser 
579336734acSTobias Grosser   if (isTrivialRegion(entry, exit))
580*9f008867SCraig Topper     return nullptr;
581336734acSTobias Grosser 
582336734acSTobias Grosser   Region *region = new Region(entry, exit, this, DT);
583336734acSTobias Grosser   BBtoRegion.insert(std::make_pair(entry, region));
584336734acSTobias Grosser 
585336734acSTobias Grosser  #ifdef XDEBUG
586336734acSTobias Grosser     region->verifyRegion();
587336734acSTobias Grosser  #else
588336734acSTobias Grosser     DEBUG(region->verifyRegion());
589336734acSTobias Grosser  #endif
590336734acSTobias Grosser 
591336734acSTobias Grosser   updateStatistics(region);
592336734acSTobias Grosser   return region;
593336734acSTobias Grosser }
594336734acSTobias Grosser 
595336734acSTobias Grosser void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
596336734acSTobias Grosser   assert(entry);
597336734acSTobias Grosser 
598336734acSTobias Grosser   DomTreeNode *N = PDT->getNode(entry);
599336734acSTobias Grosser 
600336734acSTobias Grosser   if (!N)
601336734acSTobias Grosser     return;
602336734acSTobias Grosser 
603*9f008867SCraig Topper   Region *lastRegion= nullptr;
604336734acSTobias Grosser   BasicBlock *lastExit = entry;
605336734acSTobias Grosser 
606336734acSTobias Grosser   // As only a BasicBlock that postdominates entry can finish a region, walk the
607336734acSTobias Grosser   // post dominance tree upwards.
608336734acSTobias Grosser   while ((N = getNextPostDom(N, ShortCut))) {
609336734acSTobias Grosser     BasicBlock *exit = N->getBlock();
610336734acSTobias Grosser 
611336734acSTobias Grosser     if (!exit)
612336734acSTobias Grosser       break;
613336734acSTobias Grosser 
614336734acSTobias Grosser     if (isRegion(entry, exit)) {
615336734acSTobias Grosser       Region *newRegion = createRegion(entry, exit);
616336734acSTobias Grosser 
617336734acSTobias Grosser       if (lastRegion)
618336734acSTobias Grosser         newRegion->addSubRegion(lastRegion);
619336734acSTobias Grosser 
620336734acSTobias Grosser       lastRegion = newRegion;
621336734acSTobias Grosser       lastExit = exit;
622336734acSTobias Grosser     }
623336734acSTobias Grosser 
624336734acSTobias Grosser     // This can never be a region, so stop the search.
625336734acSTobias Grosser     if (!DT->dominates(entry, exit))
626336734acSTobias Grosser       break;
627336734acSTobias Grosser   }
628336734acSTobias Grosser 
629336734acSTobias Grosser   // Tried to create regions from entry to lastExit.  Next time take a
630336734acSTobias Grosser   // shortcut from entry to lastExit.
631336734acSTobias Grosser   if (lastExit != entry)
632336734acSTobias Grosser     insertShortCut(entry, lastExit, ShortCut);
633336734acSTobias Grosser }
634336734acSTobias Grosser 
635336734acSTobias Grosser void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
636336734acSTobias Grosser   BasicBlock *entry = &(F.getEntryBlock());
637336734acSTobias Grosser   DomTreeNode *N = DT->getNode(entry);
638336734acSTobias Grosser 
639336734acSTobias Grosser   // Iterate over the dominance tree in post order to start with the small
640336734acSTobias Grosser   // regions from the bottom of the dominance tree.  If the small regions are
641336734acSTobias Grosser   // detected first, detection of bigger regions is faster, as we can jump
642336734acSTobias Grosser   // over the small regions.
643336734acSTobias Grosser   for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
644336734acSTobias Grosser     ++FI) {
6451a2da423SGabor Greif     findRegionsWithEntry(FI->getBlock(), ShortCut);
646336734acSTobias Grosser   }
647336734acSTobias Grosser }
648336734acSTobias Grosser 
649336734acSTobias Grosser Region *RegionInfo::getTopMostParent(Region *region) {
650336734acSTobias Grosser   while (region->parent)
651336734acSTobias Grosser     region = region->getParent();
652336734acSTobias Grosser 
653336734acSTobias Grosser   return region;
654336734acSTobias Grosser }
655336734acSTobias Grosser 
656336734acSTobias Grosser void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
657336734acSTobias Grosser   BasicBlock *BB = N->getBlock();
658336734acSTobias Grosser 
659336734acSTobias Grosser   // Passed region exit
660336734acSTobias Grosser   while (BB == region->getExit())
661336734acSTobias Grosser     region = region->getParent();
662336734acSTobias Grosser 
663336734acSTobias Grosser   BBtoRegionMap::iterator it = BBtoRegion.find(BB);
664336734acSTobias Grosser 
665336734acSTobias Grosser   // This basic block is a start block of a region. It is already in the
666336734acSTobias Grosser   // BBtoRegion relation. Only the child basic blocks have to be updated.
667336734acSTobias Grosser   if (it != BBtoRegion.end()) {
6685dfe6dabSChad Rosier     Region *newRegion = it->second;
669336734acSTobias Grosser     region->addSubRegion(getTopMostParent(newRegion));
670336734acSTobias Grosser     region = newRegion;
671336734acSTobias Grosser   } else {
672336734acSTobias Grosser     BBtoRegion[BB] = region;
673336734acSTobias Grosser   }
674336734acSTobias Grosser 
675336734acSTobias Grosser   for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
676336734acSTobias Grosser     buildRegionsTree(*CI, region);
677336734acSTobias Grosser }
678336734acSTobias Grosser 
679336734acSTobias Grosser void RegionInfo::releaseMemory() {
680336734acSTobias Grosser   BBtoRegion.clear();
681336734acSTobias Grosser   if (TopLevelRegion)
682336734acSTobias Grosser     delete TopLevelRegion;
683*9f008867SCraig Topper   TopLevelRegion = nullptr;
684336734acSTobias Grosser }
685336734acSTobias Grosser 
686a7aed186SOwen Anderson RegionInfo::RegionInfo() : FunctionPass(ID) {
6876c18d1aaSOwen Anderson   initializeRegionInfoPass(*PassRegistry::getPassRegistry());
688*9f008867SCraig Topper   TopLevelRegion = nullptr;
689336734acSTobias Grosser }
690336734acSTobias Grosser 
691336734acSTobias Grosser RegionInfo::~RegionInfo() {
692336734acSTobias Grosser   releaseMemory();
693336734acSTobias Grosser }
694336734acSTobias Grosser 
695336734acSTobias Grosser void RegionInfo::Calculate(Function &F) {
696336734acSTobias Grosser   // ShortCut a function where for every BB the exit of the largest region
697336734acSTobias Grosser   // starting with BB is stored. These regions can be threated as single BBS.
698336734acSTobias Grosser   // This improves performance on linear CFGs.
699336734acSTobias Grosser   BBtoBBMap ShortCut;
700336734acSTobias Grosser 
701336734acSTobias Grosser   scanForRegions(F, &ShortCut);
702336734acSTobias Grosser   BasicBlock *BB = &F.getEntryBlock();
703336734acSTobias Grosser   buildRegionsTree(DT->getNode(BB), TopLevelRegion);
704336734acSTobias Grosser }
705336734acSTobias Grosser 
706336734acSTobias Grosser bool RegionInfo::runOnFunction(Function &F) {
707336734acSTobias Grosser   releaseMemory();
708336734acSTobias Grosser 
70973523021SChandler Carruth   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
710336734acSTobias Grosser   PDT = &getAnalysis<PostDominatorTree>();
711336734acSTobias Grosser   DF = &getAnalysis<DominanceFrontier>();
712336734acSTobias Grosser 
713*9f008867SCraig Topper   TopLevelRegion = new Region(&F.getEntryBlock(), nullptr, this, DT, nullptr);
714336734acSTobias Grosser   updateStatistics(TopLevelRegion);
715336734acSTobias Grosser 
716336734acSTobias Grosser   Calculate(F);
717336734acSTobias Grosser 
718336734acSTobias Grosser   return false;
719336734acSTobias Grosser }
720336734acSTobias Grosser 
721336734acSTobias Grosser void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
722336734acSTobias Grosser   AU.setPreservesAll();
72373523021SChandler Carruth   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
724336734acSTobias Grosser   AU.addRequired<PostDominatorTree>();
725336734acSTobias Grosser   AU.addRequired<DominanceFrontier>();
726336734acSTobias Grosser }
727336734acSTobias Grosser 
728336734acSTobias Grosser void RegionInfo::print(raw_ostream &OS, const Module *) const {
729336734acSTobias Grosser   OS << "Region tree:\n";
7308b304ff9STobias Grosser   TopLevelRegion->print(OS, true, 0, printStyle.getValue());
731336734acSTobias Grosser   OS << "End region tree\n";
732336734acSTobias Grosser }
733336734acSTobias Grosser 
734336734acSTobias Grosser void RegionInfo::verifyAnalysis() const {
735336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive check
736336734acSTobias Grosser   // will be invoked by PMDataManager::verifyPreservedAnalysis when
737336734acSTobias Grosser   // a regionpass (marked PreservedAll) finish.
738336734acSTobias Grosser   if (!VerifyRegionInfo) return;
739336734acSTobias Grosser 
740336734acSTobias Grosser   TopLevelRegion->verifyRegionNest();
741336734acSTobias Grosser }
742336734acSTobias Grosser 
743336734acSTobias Grosser // Region pass manager support.
744336734acSTobias Grosser Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
745336734acSTobias Grosser   BBtoRegionMap::const_iterator I=
746336734acSTobias Grosser     BBtoRegion.find(BB);
747*9f008867SCraig Topper   return I != BBtoRegion.end() ? I->second : nullptr;
748336734acSTobias Grosser }
749336734acSTobias Grosser 
7508352ce5fSTobias Grosser void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
7518352ce5fSTobias Grosser   BBtoRegion[BB] = R;
7528352ce5fSTobias Grosser }
7538352ce5fSTobias Grosser 
754336734acSTobias Grosser Region *RegionInfo::operator[](BasicBlock *BB) const {
755336734acSTobias Grosser   return getRegionFor(BB);
756336734acSTobias Grosser }
757336734acSTobias Grosser 
758fc763867STobias Grosser BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
759*9f008867SCraig Topper   BasicBlock *Exit = nullptr;
760fc763867STobias Grosser 
761fc763867STobias Grosser   while (true) {
762fc763867STobias Grosser     // Get largest region that starts at BB.
763fc763867STobias Grosser     Region *R = getRegionFor(BB);
764fc763867STobias Grosser     while (R && R->getParent() && R->getParent()->getEntry() == BB)
765fc763867STobias Grosser       R = R->getParent();
766fc763867STobias Grosser 
767fc763867STobias Grosser     // Get the single exit of BB.
768fc763867STobias Grosser     if (R && R->getEntry() == BB)
769fc763867STobias Grosser       Exit = R->getExit();
770fc763867STobias Grosser     else if (++succ_begin(BB) == succ_end(BB))
771fc763867STobias Grosser       Exit = *succ_begin(BB);
772fc763867STobias Grosser     else // No single exit exists.
773fc763867STobias Grosser       return Exit;
774fc763867STobias Grosser 
775fc763867STobias Grosser     // Get largest region that starts at Exit.
776fc763867STobias Grosser     Region *ExitR = getRegionFor(Exit);
777fc763867STobias Grosser     while (ExitR && ExitR->getParent()
778fc763867STobias Grosser            && ExitR->getParent()->getEntry() == Exit)
779fc763867STobias Grosser       ExitR = ExitR->getParent();
780fc763867STobias Grosser 
781fc763867STobias Grosser     for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
782fc763867STobias Grosser          ++PI)
783fc763867STobias Grosser       if (!R->contains(*PI) && !ExitR->contains(*PI))
784fc763867STobias Grosser         break;
785fc763867STobias Grosser 
786fc763867STobias Grosser     // This stops infinite cycles.
787fc763867STobias Grosser     if (DT->dominates(Exit, BB))
788fc763867STobias Grosser       break;
789fc763867STobias Grosser 
790fc763867STobias Grosser     BB = Exit;
791fc763867STobias Grosser   }
792fc763867STobias Grosser 
793fc763867STobias Grosser   return Exit;
794fc763867STobias Grosser }
795fc763867STobias Grosser 
796336734acSTobias Grosser Region*
797336734acSTobias Grosser RegionInfo::getCommonRegion(Region *A, Region *B) const {
798336734acSTobias Grosser   assert (A && B && "One of the Regions is NULL");
799336734acSTobias Grosser 
800336734acSTobias Grosser   if (A->contains(B)) return A;
801336734acSTobias Grosser 
802336734acSTobias Grosser   while (!B->contains(A))
803336734acSTobias Grosser     B = B->getParent();
804336734acSTobias Grosser 
805336734acSTobias Grosser   return B;
806336734acSTobias Grosser }
807336734acSTobias Grosser 
808336734acSTobias Grosser Region*
809336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
810336734acSTobias Grosser   Region* ret = Regions.back();
811336734acSTobias Grosser   Regions.pop_back();
812336734acSTobias Grosser 
813336734acSTobias Grosser   for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
814336734acSTobias Grosser        E = Regions.end(); I != E; ++I)
815336734acSTobias Grosser       ret = getCommonRegion(ret, *I);
816336734acSTobias Grosser 
817336734acSTobias Grosser   return ret;
818336734acSTobias Grosser }
819336734acSTobias Grosser 
820336734acSTobias Grosser Region*
821336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
822336734acSTobias Grosser   Region* ret = getRegionFor(BBs.back());
823336734acSTobias Grosser   BBs.pop_back();
824336734acSTobias Grosser 
825336734acSTobias Grosser   for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
826336734acSTobias Grosser        E = BBs.end(); I != E; ++I)
827336734acSTobias Grosser       ret = getCommonRegion(ret, getRegionFor(*I));
828336734acSTobias Grosser 
829336734acSTobias Grosser   return ret;
830336734acSTobias Grosser }
831336734acSTobias Grosser 
832fe92a938STobias Grosser void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
833fe92a938STobias Grosser {
834fe92a938STobias Grosser   Region *R = getRegionFor(OldBB);
8354b0986b6STobias Grosser 
836fe92a938STobias Grosser   setRegionFor(NewBB, R);
837fe92a938STobias Grosser 
8384b0986b6STobias Grosser   while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
839fe92a938STobias Grosser     R->replaceEntry(NewBB);
840fe92a938STobias Grosser     R = R->getParent();
841fe92a938STobias Grosser   }
842fe92a938STobias Grosser 
843fe92a938STobias Grosser   setRegionFor(OldBB, R);
844fe92a938STobias Grosser }
845fe92a938STobias Grosser 
846336734acSTobias Grosser char RegionInfo::ID = 0;
8478ac477ffSOwen Anderson INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
8488ac477ffSOwen Anderson                 "Detect single entry single exit regions", true, true)
84973523021SChandler Carruth INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
8508ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
8518ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
8528ac477ffSOwen Anderson INITIALIZE_PASS_END(RegionInfo, "regions",
853df7a4f25SOwen Anderson                 "Detect single entry single exit regions", true, true)
854336734acSTobias Grosser 
855336734acSTobias Grosser // Create methods available outside of this file, to use them
856336734acSTobias Grosser // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
857336734acSTobias Grosser // the link time optimization.
858336734acSTobias Grosser 
859336734acSTobias Grosser namespace llvm {
860336734acSTobias Grosser   FunctionPass *createRegionInfoPass() {
861336734acSTobias Grosser     return new RegionInfo();
862336734acSTobias Grosser   }
863336734acSTobias Grosser }
864336734acSTobias Grosser 
865