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 
12336734acSTobias Grosser #include "llvm/Analysis/RegionInfo.h"
13336734acSTobias Grosser #include "llvm/ADT/PostOrderIterator.h"
14336734acSTobias Grosser #include "llvm/ADT/Statistic.h"
15ed0881b2SChandler Carruth #include "llvm/Analysis/LoopInfo.h"
16ed0881b2SChandler Carruth #include "llvm/Analysis/RegionIterator.h"
17ed0881b2SChandler Carruth #include "llvm/Assembly/Writer.h"
18336734acSTobias Grosser #include "llvm/Support/CommandLine.h"
19336734acSTobias Grosser #include "llvm/Support/ErrorHandling.h"
20336734acSTobias Grosser 
21336734acSTobias Grosser #define DEBUG_TYPE "region"
22336734acSTobias Grosser #include "llvm/Support/Debug.h"
23336734acSTobias Grosser 
24336734acSTobias Grosser #include <set>
25336734acSTobias Grosser #include <algorithm>
26336734acSTobias Grosser 
27336734acSTobias Grosser using namespace llvm;
28336734acSTobias Grosser 
29336734acSTobias Grosser // Always verify if expensive checking is enabled.
30336734acSTobias Grosser #ifdef XDEBUG
31abfafadfSDan Gohman static bool VerifyRegionInfo = true;
32336734acSTobias Grosser #else
33abfafadfSDan Gohman static bool VerifyRegionInfo = false;
34336734acSTobias Grosser #endif
35336734acSTobias Grosser 
36336734acSTobias Grosser static cl::opt<bool,true>
37336734acSTobias Grosser VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
38336734acSTobias Grosser                 cl::desc("Verify region info (time consuming)"));
39336734acSTobias Grosser 
40336734acSTobias Grosser STATISTIC(numRegions,       "The # of regions");
41336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions");
42336734acSTobias Grosser 
438b304ff9STobias Grosser static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
448b304ff9STobias Grosser   cl::Hidden,
45336734acSTobias Grosser   cl::desc("style of printing regions"),
46336734acSTobias Grosser   cl::values(
478b304ff9STobias Grosser     clEnumValN(Region::PrintNone, "none",  "print no details"),
488b304ff9STobias Grosser     clEnumValN(Region::PrintBB, "bb",
4914c05c40SHongbin Zheng                "print regions in detail with block_iterator"),
508b304ff9STobias Grosser     clEnumValN(Region::PrintRN, "rn",
518b304ff9STobias Grosser                "print regions in detail with element_iterator"),
52336734acSTobias Grosser     clEnumValEnd));
53336734acSTobias Grosser //===----------------------------------------------------------------------===//
54336734acSTobias Grosser /// Region Implementation
55336734acSTobias Grosser Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
56336734acSTobias Grosser                DominatorTree *dt, Region *Parent)
57336734acSTobias Grosser                : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
58336734acSTobias Grosser 
59336734acSTobias Grosser Region::~Region() {
6018e39cecSDaniel Dunbar   // Free the cached nodes.
6118e39cecSDaniel Dunbar   for (BBNodeMapT::iterator it = BBNodeMap.begin(),
6218e39cecSDaniel Dunbar          ie = BBNodeMap.end(); it != ie; ++it)
6318e39cecSDaniel Dunbar     delete it->second;
6418e39cecSDaniel Dunbar 
65336734acSTobias Grosser   // Only clean the cache for this Region. Caches of child Regions will be
66336734acSTobias Grosser   // cleaned when the child Regions are deleted.
67336734acSTobias Grosser   BBNodeMap.clear();
68336734acSTobias Grosser 
69336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
70336734acSTobias Grosser     delete *I;
71336734acSTobias Grosser }
72336734acSTobias Grosser 
73648594c9STobias Grosser void Region::replaceEntry(BasicBlock *BB) {
74648594c9STobias Grosser   entry.setPointer(BB);
75648594c9STobias Grosser }
76648594c9STobias Grosser 
77648594c9STobias Grosser void Region::replaceExit(BasicBlock *BB) {
78648594c9STobias Grosser   assert(exit && "No exit to replace!");
79648594c9STobias Grosser   exit = BB;
80648594c9STobias Grosser }
81648594c9STobias Grosser 
82*141cc3e8STobias Grosser void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
83*141cc3e8STobias Grosser   std::vector<Region *> RegionQueue;
84*141cc3e8STobias Grosser   BasicBlock *OldEntry = getEntry();
85*141cc3e8STobias Grosser 
86*141cc3e8STobias Grosser   RegionQueue.push_back(this);
87*141cc3e8STobias Grosser   while (!RegionQueue.empty()) {
88*141cc3e8STobias Grosser     Region *R = RegionQueue.back();
89*141cc3e8STobias Grosser     RegionQueue.pop_back();
90*141cc3e8STobias Grosser 
91*141cc3e8STobias Grosser     R->replaceEntry(NewEntry);
92*141cc3e8STobias Grosser     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
93*141cc3e8STobias Grosser       if ((*RI)->getEntry() == OldEntry)
94*141cc3e8STobias Grosser         RegionQueue.push_back(*RI);
95*141cc3e8STobias Grosser   }
96*141cc3e8STobias Grosser }
97*141cc3e8STobias Grosser 
98*141cc3e8STobias Grosser void Region::replaceExitRecursive(BasicBlock *NewExit) {
99*141cc3e8STobias Grosser   std::vector<Region *> RegionQueue;
100*141cc3e8STobias Grosser   BasicBlock *OldExit = getExit();
101*141cc3e8STobias Grosser 
102*141cc3e8STobias Grosser   RegionQueue.push_back(this);
103*141cc3e8STobias Grosser   while (!RegionQueue.empty()) {
104*141cc3e8STobias Grosser     Region *R = RegionQueue.back();
105*141cc3e8STobias Grosser     RegionQueue.pop_back();
106*141cc3e8STobias Grosser 
107*141cc3e8STobias Grosser     R->replaceExit(NewExit);
108*141cc3e8STobias Grosser     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
109*141cc3e8STobias Grosser       if ((*RI)->getExit() == OldExit)
110*141cc3e8STobias Grosser         RegionQueue.push_back(*RI);
111*141cc3e8STobias Grosser   }
112*141cc3e8STobias Grosser }
113*141cc3e8STobias Grosser 
114336734acSTobias Grosser bool Region::contains(const BasicBlock *B) const {
115336734acSTobias Grosser   BasicBlock *BB = const_cast<BasicBlock*>(B);
116336734acSTobias Grosser 
117336734acSTobias Grosser   assert(DT->getNode(BB) && "BB not part of the dominance tree");
118336734acSTobias Grosser 
119336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
120336734acSTobias Grosser 
121336734acSTobias Grosser   // Toplevel region.
122336734acSTobias Grosser   if (!exit)
123336734acSTobias Grosser     return true;
124336734acSTobias Grosser 
125336734acSTobias Grosser   return (DT->dominates(entry, BB)
126336734acSTobias Grosser     && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
127336734acSTobias Grosser }
128336734acSTobias Grosser 
1291bec81a8STobias Grosser bool Region::contains(const Loop *L) const {
1301bec81a8STobias Grosser   // BBs that are not part of any loop are element of the Loop
1311bec81a8STobias Grosser   // described by the NULL pointer. This loop is not part of any region,
1321bec81a8STobias Grosser   // except if the region describes the whole function.
1331bec81a8STobias Grosser   if (L == 0)
1341bec81a8STobias Grosser     return getExit() == 0;
1351bec81a8STobias Grosser 
1361bec81a8STobias Grosser   if (!contains(L->getHeader()))
1371bec81a8STobias Grosser     return false;
1381bec81a8STobias Grosser 
1391bec81a8STobias Grosser   SmallVector<BasicBlock *, 8> ExitingBlocks;
1401bec81a8STobias Grosser   L->getExitingBlocks(ExitingBlocks);
1411bec81a8STobias Grosser 
1421bec81a8STobias Grosser   for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
1431bec81a8STobias Grosser        BE = ExitingBlocks.end(); BI != BE; ++BI)
1441bec81a8STobias Grosser     if (!contains(*BI))
1451bec81a8STobias Grosser       return false;
1461bec81a8STobias Grosser 
1471bec81a8STobias Grosser   return true;
1481bec81a8STobias Grosser }
1491bec81a8STobias Grosser 
1501bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(Loop *L) const {
1511bec81a8STobias Grosser   if (!contains(L))
1521bec81a8STobias Grosser     return 0;
1531bec81a8STobias Grosser 
1541bec81a8STobias Grosser   while (L && contains(L->getParentLoop())) {
1551bec81a8STobias Grosser     L = L->getParentLoop();
1561bec81a8STobias Grosser   }
1571bec81a8STobias Grosser 
1581bec81a8STobias Grosser   return L;
1591bec81a8STobias Grosser }
1601bec81a8STobias Grosser 
1611bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
1621bec81a8STobias Grosser   assert(LI && BB && "LI and BB cannot be null!");
1631bec81a8STobias Grosser   Loop *L = LI->getLoopFor(BB);
1641bec81a8STobias Grosser   return outermostLoopInRegion(L);
1651bec81a8STobias Grosser }
1661bec81a8STobias Grosser 
167b1d11c19STobias Grosser BasicBlock *Region::getEnteringBlock() const {
168b1d11c19STobias Grosser   BasicBlock *entry = getEntry();
169b1d11c19STobias Grosser   BasicBlock *Pred;
170b1d11c19STobias Grosser   BasicBlock *enteringBlock = 0;
171336734acSTobias Grosser 
172336734acSTobias Grosser   for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
1737fbe6cb4STobias Grosser        ++PI) {
174b1d11c19STobias Grosser     Pred = *PI;
1757fbe6cb4STobias Grosser     if (DT->getNode(Pred) && !contains(Pred)) {
176b1d11c19STobias Grosser       if (enteringBlock)
177b1d11c19STobias Grosser         return 0;
178b1d11c19STobias Grosser 
179b1d11c19STobias Grosser       enteringBlock = Pred;
180336734acSTobias Grosser     }
1817fbe6cb4STobias Grosser   }
182336734acSTobias Grosser 
183b1d11c19STobias Grosser   return enteringBlock;
184b1d11c19STobias Grosser }
185b1d11c19STobias Grosser 
186b1d11c19STobias Grosser BasicBlock *Region::getExitingBlock() const {
187b1d11c19STobias Grosser   BasicBlock *exit = getExit();
188b1d11c19STobias Grosser   BasicBlock *Pred;
189b1d11c19STobias Grosser   BasicBlock *exitingBlock = 0;
190b1d11c19STobias Grosser 
191b1d11c19STobias Grosser   if (!exit)
192b1d11c19STobias Grosser     return 0;
193336734acSTobias Grosser 
194336734acSTobias Grosser   for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
195b1d11c19STobias Grosser        ++PI) {
196b1d11c19STobias Grosser     Pred = *PI;
197b1d11c19STobias Grosser     if (contains(Pred)) {
198b1d11c19STobias Grosser       if (exitingBlock)
199b1d11c19STobias Grosser         return 0;
200b1d11c19STobias Grosser 
201b1d11c19STobias Grosser       exitingBlock = Pred;
202336734acSTobias Grosser     }
203336734acSTobias Grosser   }
204336734acSTobias Grosser 
205b1d11c19STobias Grosser   return exitingBlock;
206b1d11c19STobias Grosser }
207b1d11c19STobias Grosser 
208b1d11c19STobias Grosser bool Region::isSimple() const {
209b1d11c19STobias Grosser   return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
210336734acSTobias Grosser }
211336734acSTobias Grosser 
2121bec81a8STobias Grosser std::string Region::getNameStr() const {
2131bec81a8STobias Grosser   std::string exitName;
2141bec81a8STobias Grosser   std::string entryName;
2151bec81a8STobias Grosser 
2161bec81a8STobias Grosser   if (getEntry()->getName().empty()) {
2171bec81a8STobias Grosser     raw_string_ostream OS(entryName);
2181bec81a8STobias Grosser 
2191bec81a8STobias Grosser     WriteAsOperand(OS, getEntry(), false);
2201bec81a8STobias Grosser   } else
221184e3ceeSBenjamin Kramer     entryName = getEntry()->getName();
2221bec81a8STobias Grosser 
2231bec81a8STobias Grosser   if (getExit()) {
2241bec81a8STobias Grosser     if (getExit()->getName().empty()) {
2251bec81a8STobias Grosser       raw_string_ostream OS(exitName);
2261bec81a8STobias Grosser 
2271bec81a8STobias Grosser       WriteAsOperand(OS, getExit(), false);
2281bec81a8STobias Grosser     } else
229184e3ceeSBenjamin Kramer       exitName = getExit()->getName();
2301bec81a8STobias Grosser   } else
2311bec81a8STobias Grosser     exitName = "<Function Return>";
2321bec81a8STobias Grosser 
2331bec81a8STobias Grosser   return entryName + " => " + exitName;
2341bec81a8STobias Grosser }
2351bec81a8STobias Grosser 
236336734acSTobias Grosser void Region::verifyBBInRegion(BasicBlock *BB) const {
237336734acSTobias Grosser   if (!contains(BB))
238336734acSTobias Grosser     llvm_unreachable("Broken region found!");
239336734acSTobias Grosser 
240336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
241336734acSTobias Grosser 
242336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
243336734acSTobias Grosser     if (!contains(*SI) && exit != *SI)
244336734acSTobias Grosser       llvm_unreachable("Broken region found!");
245336734acSTobias Grosser 
246336734acSTobias Grosser   if (entry != BB)
247336734acSTobias Grosser     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
248336734acSTobias Grosser       if (!contains(*SI))
249336734acSTobias Grosser         llvm_unreachable("Broken region found!");
250336734acSTobias Grosser }
251336734acSTobias Grosser 
252336734acSTobias Grosser void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
253336734acSTobias Grosser   BasicBlock *exit = getExit();
254336734acSTobias Grosser 
255336734acSTobias Grosser   visited->insert(BB);
256336734acSTobias Grosser 
257336734acSTobias Grosser   verifyBBInRegion(BB);
258336734acSTobias Grosser 
259336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
260336734acSTobias Grosser     if (*SI != exit && visited->find(*SI) == visited->end())
261336734acSTobias Grosser         verifyWalk(*SI, visited);
262336734acSTobias Grosser }
263336734acSTobias Grosser 
264336734acSTobias Grosser void Region::verifyRegion() const {
265336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive
266336734acSTobias Grosser   // check will be invoked by PassManager.
267336734acSTobias Grosser   if (!VerifyRegionInfo) return;
268336734acSTobias Grosser 
269336734acSTobias Grosser   std::set<BasicBlock*> visited;
270336734acSTobias Grosser   verifyWalk(getEntry(), &visited);
271336734acSTobias Grosser }
272336734acSTobias Grosser 
273336734acSTobias Grosser void Region::verifyRegionNest() const {
274336734acSTobias Grosser   for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
275336734acSTobias Grosser     (*RI)->verifyRegionNest();
276336734acSTobias Grosser 
277336734acSTobias Grosser   verifyRegion();
278336734acSTobias Grosser }
279336734acSTobias Grosser 
280336734acSTobias Grosser Region::element_iterator Region::element_begin() {
281336734acSTobias Grosser   return GraphTraits<Region*>::nodes_begin(this);
282336734acSTobias Grosser }
283336734acSTobias Grosser 
284336734acSTobias Grosser Region::element_iterator Region::element_end() {
285336734acSTobias Grosser   return GraphTraits<Region*>::nodes_end(this);
286336734acSTobias Grosser }
287336734acSTobias Grosser 
288336734acSTobias Grosser Region::const_element_iterator Region::element_begin() const {
289336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_begin(this);
290336734acSTobias Grosser }
291336734acSTobias Grosser 
292336734acSTobias Grosser Region::const_element_iterator Region::element_end() const {
293336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_end(this);
294336734acSTobias Grosser }
295336734acSTobias Grosser 
296336734acSTobias Grosser Region* Region::getSubRegionNode(BasicBlock *BB) const {
297336734acSTobias Grosser   Region *R = RI->getRegionFor(BB);
298336734acSTobias Grosser 
299336734acSTobias Grosser   if (!R || R == this)
300336734acSTobias Grosser     return 0;
301336734acSTobias Grosser 
302336734acSTobias Grosser   // If we pass the BB out of this region, that means our code is broken.
303336734acSTobias Grosser   assert(contains(R) && "BB not in current region!");
304336734acSTobias Grosser 
305336734acSTobias Grosser   while (contains(R->getParent()) && R->getParent() != this)
306336734acSTobias Grosser     R = R->getParent();
307336734acSTobias Grosser 
308336734acSTobias Grosser   if (R->getEntry() != BB)
309336734acSTobias Grosser     return 0;
310336734acSTobias Grosser 
311336734acSTobias Grosser   return R;
312336734acSTobias Grosser }
313336734acSTobias Grosser 
314336734acSTobias Grosser RegionNode* Region::getBBNode(BasicBlock *BB) const {
315336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
316336734acSTobias Grosser 
317336734acSTobias Grosser   BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
318336734acSTobias Grosser 
319336734acSTobias Grosser   if (at != BBNodeMap.end())
320336734acSTobias Grosser     return at->second;
321336734acSTobias Grosser 
322336734acSTobias Grosser   RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
323336734acSTobias Grosser   BBNodeMap.insert(std::make_pair(BB, NewNode));
324336734acSTobias Grosser   return NewNode;
325336734acSTobias Grosser }
326336734acSTobias Grosser 
327336734acSTobias Grosser RegionNode* Region::getNode(BasicBlock *BB) const {
328336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
329336734acSTobias Grosser   if (Region* Child = getSubRegionNode(BB))
330336734acSTobias Grosser     return Child->getNode();
331336734acSTobias Grosser 
332336734acSTobias Grosser   return getBBNode(BB);
333336734acSTobias Grosser }
334336734acSTobias Grosser 
335336734acSTobias Grosser void Region::transferChildrenTo(Region *To) {
336336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I) {
337336734acSTobias Grosser     (*I)->parent = To;
338336734acSTobias Grosser     To->children.push_back(*I);
339336734acSTobias Grosser   }
340336734acSTobias Grosser   children.clear();
341336734acSTobias Grosser }
342336734acSTobias Grosser 
343bf984fd7STobias Grosser void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
344336734acSTobias Grosser   assert(SubRegion->parent == 0 && "SubRegion already has a parent!");
345bf984fd7STobias Grosser   assert(std::find(begin(), end(), SubRegion) == children.end()
346bf984fd7STobias Grosser          && "Subregion already exists!");
347bf984fd7STobias Grosser 
348336734acSTobias Grosser   SubRegion->parent = this;
349336734acSTobias Grosser   children.push_back(SubRegion);
350bf984fd7STobias Grosser 
351bf984fd7STobias Grosser   if (!moveChildren)
352bf984fd7STobias Grosser     return;
353bf984fd7STobias Grosser 
354bf984fd7STobias Grosser   assert(SubRegion->children.size() == 0
355bf984fd7STobias Grosser          && "SubRegions that contain children are not supported");
356bf984fd7STobias Grosser 
357bf984fd7STobias Grosser   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
358bf984fd7STobias Grosser     if (!(*I)->isSubRegion()) {
359bf984fd7STobias Grosser       BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
360bf984fd7STobias Grosser 
361bf984fd7STobias Grosser       if (SubRegion->contains(BB))
362bf984fd7STobias Grosser         RI->setRegionFor(BB, SubRegion);
363bf984fd7STobias Grosser     }
364bf984fd7STobias Grosser 
365bf984fd7STobias Grosser   std::vector<Region*> Keep;
366bf984fd7STobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
367bf984fd7STobias Grosser     if (SubRegion->contains(*I) && *I != SubRegion) {
368bf984fd7STobias Grosser       SubRegion->children.push_back(*I);
369bf984fd7STobias Grosser       (*I)->parent = SubRegion;
370bf984fd7STobias Grosser     } else
371bf984fd7STobias Grosser       Keep.push_back(*I);
372bf984fd7STobias Grosser 
373bf984fd7STobias Grosser   children.clear();
374bf984fd7STobias Grosser   children.insert(children.begin(), Keep.begin(), Keep.end());
375336734acSTobias Grosser }
376336734acSTobias Grosser 
377336734acSTobias Grosser 
378336734acSTobias Grosser Region *Region::removeSubRegion(Region *Child) {
379336734acSTobias Grosser   assert(Child->parent == this && "Child is not a child of this region!");
380336734acSTobias Grosser   Child->parent = 0;
381336734acSTobias Grosser   RegionSet::iterator I = std::find(children.begin(), children.end(), Child);
382336734acSTobias Grosser   assert(I != children.end() && "Region does not exit. Unable to remove.");
383336734acSTobias Grosser   children.erase(children.begin()+(I-begin()));
384336734acSTobias Grosser   return Child;
385336734acSTobias Grosser }
386336734acSTobias Grosser 
387336734acSTobias Grosser unsigned Region::getDepth() const {
388336734acSTobias Grosser   unsigned Depth = 0;
389336734acSTobias Grosser 
390336734acSTobias Grosser   for (Region *R = parent; R != 0; R = R->parent)
391336734acSTobias Grosser     ++Depth;
392336734acSTobias Grosser 
393336734acSTobias Grosser   return Depth;
394336734acSTobias Grosser }
395336734acSTobias Grosser 
396a8677226STobias Grosser Region *Region::getExpandedRegion() const {
397a8677226STobias Grosser   unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
398a8677226STobias Grosser 
399a8677226STobias Grosser   if (NumSuccessors == 0)
400a8677226STobias Grosser     return NULL;
401a8677226STobias Grosser 
402a8677226STobias Grosser   for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
403a8677226STobias Grosser        PI != PE; ++PI)
404a8677226STobias Grosser     if (!DT->dominates(getEntry(), *PI))
405a8677226STobias Grosser       return NULL;
406a8677226STobias Grosser 
407a8677226STobias Grosser   Region *R = RI->getRegionFor(exit);
408a8677226STobias Grosser 
409a8677226STobias Grosser   if (R->getEntry() != exit) {
410a8677226STobias Grosser     if (exit->getTerminator()->getNumSuccessors() == 1)
411a8677226STobias Grosser       return new Region(getEntry(), *succ_begin(exit), RI, DT);
412a8677226STobias Grosser     else
413a8677226STobias Grosser       return NULL;
414a8677226STobias Grosser   }
415a8677226STobias Grosser 
416a8677226STobias Grosser   while (R->getParent() && R->getParent()->getEntry() == exit)
417a8677226STobias Grosser     R = R->getParent();
418a8677226STobias Grosser 
419a8677226STobias Grosser   if (!DT->dominates(getEntry(), R->getExit()))
420a8677226STobias Grosser     for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
421a8677226STobias Grosser          PI != PE; ++PI)
422a8677226STobias Grosser     if (!DT->dominates(R->getExit(), *PI))
423a8677226STobias Grosser       return NULL;
424a8677226STobias Grosser 
425a8677226STobias Grosser   return new Region(getEntry(), R->getExit(), RI, DT);
426a8677226STobias Grosser }
427a8677226STobias Grosser 
4288b304ff9STobias Grosser void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
4298b304ff9STobias Grosser                    enum PrintStyle Style) const {
430336734acSTobias Grosser   if (print_tree)
431336734acSTobias Grosser     OS.indent(level*2) << "[" << level << "] " << getNameStr();
432336734acSTobias Grosser   else
433336734acSTobias Grosser     OS.indent(level*2) << getNameStr();
434336734acSTobias Grosser 
435336734acSTobias Grosser   OS << "\n";
436336734acSTobias Grosser 
437336734acSTobias Grosser 
4388b304ff9STobias Grosser   if (Style != PrintNone) {
439336734acSTobias Grosser     OS.indent(level*2) << "{\n";
440336734acSTobias Grosser     OS.indent(level*2 + 2);
441336734acSTobias Grosser 
4428b304ff9STobias Grosser     if (Style == PrintBB) {
44314c05c40SHongbin Zheng       for (const_block_iterator I = block_begin(), E = block_end(); I != E; ++I)
44414c05c40SHongbin Zheng         OS << (*I)->getName() << ", "; // TODO: remove the last ","
4458b304ff9STobias Grosser     } else if (Style == PrintRN) {
446336734acSTobias Grosser       for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
447336734acSTobias Grosser         OS << **I << ", "; // TODO: remove the last ",
448336734acSTobias Grosser     }
449336734acSTobias Grosser 
450336734acSTobias Grosser     OS << "\n";
451336734acSTobias Grosser   }
452336734acSTobias Grosser 
453336734acSTobias Grosser   if (print_tree)
454336734acSTobias Grosser     for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
4558b304ff9STobias Grosser       (*RI)->print(OS, print_tree, level+1, Style);
456336734acSTobias Grosser 
4578b304ff9STobias Grosser   if (Style != PrintNone)
458336734acSTobias Grosser     OS.indent(level*2) << "} \n";
459336734acSTobias Grosser }
460336734acSTobias Grosser 
46149d684e1SManman Ren #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
462336734acSTobias Grosser void Region::dump() const {
4638b304ff9STobias Grosser   print(dbgs(), true, getDepth(), printStyle.getValue());
464336734acSTobias Grosser }
465c3366cceSManman Ren #endif
466336734acSTobias Grosser 
467336734acSTobias Grosser void Region::clearNodeCache() {
468e910b9d9STobias Grosser   // Free the cached nodes.
469e910b9d9STobias Grosser   for (BBNodeMapT::iterator I = BBNodeMap.begin(),
4704c71c117STobias Grosser        IE = BBNodeMap.end(); I != IE; ++I)
471e910b9d9STobias Grosser     delete I->second;
472e910b9d9STobias Grosser 
473336734acSTobias Grosser   BBNodeMap.clear();
474336734acSTobias Grosser   for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
475336734acSTobias Grosser     (*RI)->clearNodeCache();
476336734acSTobias Grosser }
477336734acSTobias Grosser 
478336734acSTobias Grosser //===----------------------------------------------------------------------===//
479336734acSTobias Grosser // RegionInfo implementation
480336734acSTobias Grosser //
481336734acSTobias Grosser 
482336734acSTobias Grosser bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
483336734acSTobias Grosser                                      BasicBlock *exit) const {
48407c8ad54SGabor Greif   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
48507c8ad54SGabor Greif     BasicBlock *P = *PI;
48607c8ad54SGabor Greif     if (DT->dominates(entry, P) && !DT->dominates(exit, P))
487336734acSTobias Grosser       return false;
48807c8ad54SGabor Greif   }
489336734acSTobias Grosser   return true;
490336734acSTobias Grosser }
491336734acSTobias Grosser 
492336734acSTobias Grosser bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
493336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
494336734acSTobias Grosser   typedef DominanceFrontier::DomSetType DST;
495336734acSTobias Grosser 
496d9f48ecbSGabor Greif   DST *entrySuccs = &DF->find(entry)->second;
497336734acSTobias Grosser 
498336734acSTobias Grosser   // Exit is the header of a loop that contains the entry. In this case,
499336734acSTobias Grosser   // the dominance frontier must only contain the exit.
500336734acSTobias Grosser   if (!DT->dominates(entry, exit)) {
501336734acSTobias Grosser     for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
502336734acSTobias Grosser          SI != SE; ++SI)
503336734acSTobias Grosser       if (*SI != exit && *SI != entry)
504336734acSTobias Grosser         return false;
505336734acSTobias Grosser 
506336734acSTobias Grosser     return true;
507336734acSTobias Grosser   }
508336734acSTobias Grosser 
509d9f48ecbSGabor Greif   DST *exitSuccs = &DF->find(exit)->second;
510336734acSTobias Grosser 
511336734acSTobias Grosser   // Do not allow edges leaving the region.
512336734acSTobias Grosser   for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
513336734acSTobias Grosser        SI != SE; ++SI) {
514336734acSTobias Grosser     if (*SI == exit || *SI == entry)
515336734acSTobias Grosser       continue;
516336734acSTobias Grosser     if (exitSuccs->find(*SI) == exitSuccs->end())
517336734acSTobias Grosser       return false;
518336734acSTobias Grosser     if (!isCommonDomFrontier(*SI, entry, exit))
519336734acSTobias Grosser       return false;
520336734acSTobias Grosser   }
521336734acSTobias Grosser 
522336734acSTobias Grosser   // Do not allow edges pointing into the region.
523336734acSTobias Grosser   for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
524336734acSTobias Grosser        SI != SE; ++SI)
525b3aa6c71SDan Gohman     if (DT->properlyDominates(entry, *SI) && *SI != exit)
526336734acSTobias Grosser       return false;
527336734acSTobias Grosser 
528336734acSTobias Grosser 
529336734acSTobias Grosser   return true;
530336734acSTobias Grosser }
531336734acSTobias Grosser 
532336734acSTobias Grosser void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
533336734acSTobias Grosser                              BBtoBBMap *ShortCut) const {
534336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
535336734acSTobias Grosser 
536336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(exit);
537336734acSTobias Grosser 
538336734acSTobias Grosser   if (e == ShortCut->end())
539336734acSTobias Grosser     // No further region at exit available.
540336734acSTobias Grosser     (*ShortCut)[entry] = exit;
541336734acSTobias Grosser   else {
542336734acSTobias Grosser     // We found a region e that starts at exit. Therefore (entry, e->second)
543336734acSTobias Grosser     // is also a region, that is larger than (entry, exit). Insert the
544336734acSTobias Grosser     // larger one.
545336734acSTobias Grosser     BasicBlock *BB = e->second;
546336734acSTobias Grosser     (*ShortCut)[entry] = BB;
547336734acSTobias Grosser   }
548336734acSTobias Grosser }
549336734acSTobias Grosser 
550336734acSTobias Grosser DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
551336734acSTobias Grosser                                         BBtoBBMap *ShortCut) const {
552336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
553336734acSTobias Grosser 
554336734acSTobias Grosser   if (e == ShortCut->end())
555336734acSTobias Grosser     return N->getIDom();
556336734acSTobias Grosser 
557336734acSTobias Grosser   return PDT->getNode(e->second)->getIDom();
558336734acSTobias Grosser }
559336734acSTobias Grosser 
560336734acSTobias Grosser bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
561336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
562336734acSTobias Grosser 
563336734acSTobias Grosser   unsigned num_successors = succ_end(entry) - succ_begin(entry);
564336734acSTobias Grosser 
565336734acSTobias Grosser   if (num_successors <= 1 && exit == *(succ_begin(entry)))
566336734acSTobias Grosser     return true;
567336734acSTobias Grosser 
568336734acSTobias Grosser   return false;
569336734acSTobias Grosser }
570336734acSTobias Grosser 
571336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) {
572336734acSTobias Grosser   ++numRegions;
573336734acSTobias Grosser 
574336734acSTobias Grosser   // TODO: Slow. Should only be enabled if -stats is used.
575336734acSTobias Grosser   if (R->isSimple()) ++numSimpleRegions;
576336734acSTobias Grosser }
577336734acSTobias Grosser 
578336734acSTobias Grosser Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
579336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
580336734acSTobias Grosser 
581336734acSTobias Grosser   if (isTrivialRegion(entry, exit))
582336734acSTobias Grosser     return 0;
583336734acSTobias Grosser 
584336734acSTobias Grosser   Region *region = new Region(entry, exit, this, DT);
585336734acSTobias Grosser   BBtoRegion.insert(std::make_pair(entry, region));
586336734acSTobias Grosser 
587336734acSTobias Grosser  #ifdef XDEBUG
588336734acSTobias Grosser     region->verifyRegion();
589336734acSTobias Grosser  #else
590336734acSTobias Grosser     DEBUG(region->verifyRegion());
591336734acSTobias Grosser  #endif
592336734acSTobias Grosser 
593336734acSTobias Grosser   updateStatistics(region);
594336734acSTobias Grosser   return region;
595336734acSTobias Grosser }
596336734acSTobias Grosser 
597336734acSTobias Grosser void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
598336734acSTobias Grosser   assert(entry);
599336734acSTobias Grosser 
600336734acSTobias Grosser   DomTreeNode *N = PDT->getNode(entry);
601336734acSTobias Grosser 
602336734acSTobias Grosser   if (!N)
603336734acSTobias Grosser     return;
604336734acSTobias Grosser 
605336734acSTobias Grosser   Region *lastRegion= 0;
606336734acSTobias Grosser   BasicBlock *lastExit = entry;
607336734acSTobias Grosser 
608336734acSTobias Grosser   // As only a BasicBlock that postdominates entry can finish a region, walk the
609336734acSTobias Grosser   // post dominance tree upwards.
610336734acSTobias Grosser   while ((N = getNextPostDom(N, ShortCut))) {
611336734acSTobias Grosser     BasicBlock *exit = N->getBlock();
612336734acSTobias Grosser 
613336734acSTobias Grosser     if (!exit)
614336734acSTobias Grosser       break;
615336734acSTobias Grosser 
616336734acSTobias Grosser     if (isRegion(entry, exit)) {
617336734acSTobias Grosser       Region *newRegion = createRegion(entry, exit);
618336734acSTobias Grosser 
619336734acSTobias Grosser       if (lastRegion)
620336734acSTobias Grosser         newRegion->addSubRegion(lastRegion);
621336734acSTobias Grosser 
622336734acSTobias Grosser       lastRegion = newRegion;
623336734acSTobias Grosser       lastExit = exit;
624336734acSTobias Grosser     }
625336734acSTobias Grosser 
626336734acSTobias Grosser     // This can never be a region, so stop the search.
627336734acSTobias Grosser     if (!DT->dominates(entry, exit))
628336734acSTobias Grosser       break;
629336734acSTobias Grosser   }
630336734acSTobias Grosser 
631336734acSTobias Grosser   // Tried to create regions from entry to lastExit.  Next time take a
632336734acSTobias Grosser   // shortcut from entry to lastExit.
633336734acSTobias Grosser   if (lastExit != entry)
634336734acSTobias Grosser     insertShortCut(entry, lastExit, ShortCut);
635336734acSTobias Grosser }
636336734acSTobias Grosser 
637336734acSTobias Grosser void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
638336734acSTobias Grosser   BasicBlock *entry = &(F.getEntryBlock());
639336734acSTobias Grosser   DomTreeNode *N = DT->getNode(entry);
640336734acSTobias Grosser 
641336734acSTobias Grosser   // Iterate over the dominance tree in post order to start with the small
642336734acSTobias Grosser   // regions from the bottom of the dominance tree.  If the small regions are
643336734acSTobias Grosser   // detected first, detection of bigger regions is faster, as we can jump
644336734acSTobias Grosser   // over the small regions.
645336734acSTobias Grosser   for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
646336734acSTobias Grosser     ++FI) {
6471a2da423SGabor Greif     findRegionsWithEntry(FI->getBlock(), ShortCut);
648336734acSTobias Grosser   }
649336734acSTobias Grosser }
650336734acSTobias Grosser 
651336734acSTobias Grosser Region *RegionInfo::getTopMostParent(Region *region) {
652336734acSTobias Grosser   while (region->parent)
653336734acSTobias Grosser     region = region->getParent();
654336734acSTobias Grosser 
655336734acSTobias Grosser   return region;
656336734acSTobias Grosser }
657336734acSTobias Grosser 
658336734acSTobias Grosser void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
659336734acSTobias Grosser   BasicBlock *BB = N->getBlock();
660336734acSTobias Grosser 
661336734acSTobias Grosser   // Passed region exit
662336734acSTobias Grosser   while (BB == region->getExit())
663336734acSTobias Grosser     region = region->getParent();
664336734acSTobias Grosser 
665336734acSTobias Grosser   BBtoRegionMap::iterator it = BBtoRegion.find(BB);
666336734acSTobias Grosser 
667336734acSTobias Grosser   // This basic block is a start block of a region. It is already in the
668336734acSTobias Grosser   // BBtoRegion relation. Only the child basic blocks have to be updated.
669336734acSTobias Grosser   if (it != BBtoRegion.end()) {
6705dfe6dabSChad Rosier     Region *newRegion = it->second;
671336734acSTobias Grosser     region->addSubRegion(getTopMostParent(newRegion));
672336734acSTobias Grosser     region = newRegion;
673336734acSTobias Grosser   } else {
674336734acSTobias Grosser     BBtoRegion[BB] = region;
675336734acSTobias Grosser   }
676336734acSTobias Grosser 
677336734acSTobias Grosser   for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
678336734acSTobias Grosser     buildRegionsTree(*CI, region);
679336734acSTobias Grosser }
680336734acSTobias Grosser 
681336734acSTobias Grosser void RegionInfo::releaseMemory() {
682336734acSTobias Grosser   BBtoRegion.clear();
683336734acSTobias Grosser   if (TopLevelRegion)
684336734acSTobias Grosser     delete TopLevelRegion;
685336734acSTobias Grosser   TopLevelRegion = 0;
686336734acSTobias Grosser }
687336734acSTobias Grosser 
688a7aed186SOwen Anderson RegionInfo::RegionInfo() : FunctionPass(ID) {
6896c18d1aaSOwen Anderson   initializeRegionInfoPass(*PassRegistry::getPassRegistry());
690336734acSTobias Grosser   TopLevelRegion = 0;
691336734acSTobias Grosser }
692336734acSTobias Grosser 
693336734acSTobias Grosser RegionInfo::~RegionInfo() {
694336734acSTobias Grosser   releaseMemory();
695336734acSTobias Grosser }
696336734acSTobias Grosser 
697336734acSTobias Grosser void RegionInfo::Calculate(Function &F) {
698336734acSTobias Grosser   // ShortCut a function where for every BB the exit of the largest region
699336734acSTobias Grosser   // starting with BB is stored. These regions can be threated as single BBS.
700336734acSTobias Grosser   // This improves performance on linear CFGs.
701336734acSTobias Grosser   BBtoBBMap ShortCut;
702336734acSTobias Grosser 
703336734acSTobias Grosser   scanForRegions(F, &ShortCut);
704336734acSTobias Grosser   BasicBlock *BB = &F.getEntryBlock();
705336734acSTobias Grosser   buildRegionsTree(DT->getNode(BB), TopLevelRegion);
706336734acSTobias Grosser }
707336734acSTobias Grosser 
708336734acSTobias Grosser bool RegionInfo::runOnFunction(Function &F) {
709336734acSTobias Grosser   releaseMemory();
710336734acSTobias Grosser 
711336734acSTobias Grosser   DT = &getAnalysis<DominatorTree>();
712336734acSTobias Grosser   PDT = &getAnalysis<PostDominatorTree>();
713336734acSTobias Grosser   DF = &getAnalysis<DominanceFrontier>();
714336734acSTobias Grosser 
715336734acSTobias Grosser   TopLevelRegion = new Region(&F.getEntryBlock(), 0, this, DT, 0);
716336734acSTobias Grosser   updateStatistics(TopLevelRegion);
717336734acSTobias Grosser 
718336734acSTobias Grosser   Calculate(F);
719336734acSTobias Grosser 
720336734acSTobias Grosser   return false;
721336734acSTobias Grosser }
722336734acSTobias Grosser 
723336734acSTobias Grosser void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
724336734acSTobias Grosser   AU.setPreservesAll();
725336734acSTobias Grosser   AU.addRequiredTransitive<DominatorTree>();
726336734acSTobias Grosser   AU.addRequired<PostDominatorTree>();
727336734acSTobias Grosser   AU.addRequired<DominanceFrontier>();
728336734acSTobias Grosser }
729336734acSTobias Grosser 
730336734acSTobias Grosser void RegionInfo::print(raw_ostream &OS, const Module *) const {
731336734acSTobias Grosser   OS << "Region tree:\n";
7328b304ff9STobias Grosser   TopLevelRegion->print(OS, true, 0, printStyle.getValue());
733336734acSTobias Grosser   OS << "End region tree\n";
734336734acSTobias Grosser }
735336734acSTobias Grosser 
736336734acSTobias Grosser void RegionInfo::verifyAnalysis() const {
737336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive check
738336734acSTobias Grosser   // will be invoked by PMDataManager::verifyPreservedAnalysis when
739336734acSTobias Grosser   // a regionpass (marked PreservedAll) finish.
740336734acSTobias Grosser   if (!VerifyRegionInfo) return;
741336734acSTobias Grosser 
742336734acSTobias Grosser   TopLevelRegion->verifyRegionNest();
743336734acSTobias Grosser }
744336734acSTobias Grosser 
745336734acSTobias Grosser // Region pass manager support.
746336734acSTobias Grosser Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
747336734acSTobias Grosser   BBtoRegionMap::const_iterator I=
748336734acSTobias Grosser     BBtoRegion.find(BB);
749336734acSTobias Grosser   return I != BBtoRegion.end() ? I->second : 0;
750336734acSTobias Grosser }
751336734acSTobias Grosser 
7528352ce5fSTobias Grosser void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
7538352ce5fSTobias Grosser   BBtoRegion[BB] = R;
7548352ce5fSTobias Grosser }
7558352ce5fSTobias Grosser 
756336734acSTobias Grosser Region *RegionInfo::operator[](BasicBlock *BB) const {
757336734acSTobias Grosser   return getRegionFor(BB);
758336734acSTobias Grosser }
759336734acSTobias Grosser 
760fc763867STobias Grosser BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
761fc763867STobias Grosser   BasicBlock *Exit = NULL;
762fc763867STobias Grosser 
763fc763867STobias Grosser   while (true) {
764fc763867STobias Grosser     // Get largest region that starts at BB.
765fc763867STobias Grosser     Region *R = getRegionFor(BB);
766fc763867STobias Grosser     while (R && R->getParent() && R->getParent()->getEntry() == BB)
767fc763867STobias Grosser       R = R->getParent();
768fc763867STobias Grosser 
769fc763867STobias Grosser     // Get the single exit of BB.
770fc763867STobias Grosser     if (R && R->getEntry() == BB)
771fc763867STobias Grosser       Exit = R->getExit();
772fc763867STobias Grosser     else if (++succ_begin(BB) == succ_end(BB))
773fc763867STobias Grosser       Exit = *succ_begin(BB);
774fc763867STobias Grosser     else // No single exit exists.
775fc763867STobias Grosser       return Exit;
776fc763867STobias Grosser 
777fc763867STobias Grosser     // Get largest region that starts at Exit.
778fc763867STobias Grosser     Region *ExitR = getRegionFor(Exit);
779fc763867STobias Grosser     while (ExitR && ExitR->getParent()
780fc763867STobias Grosser            && ExitR->getParent()->getEntry() == Exit)
781fc763867STobias Grosser       ExitR = ExitR->getParent();
782fc763867STobias Grosser 
783fc763867STobias Grosser     for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
784fc763867STobias Grosser          ++PI)
785fc763867STobias Grosser       if (!R->contains(*PI) && !ExitR->contains(*PI))
786fc763867STobias Grosser         break;
787fc763867STobias Grosser 
788fc763867STobias Grosser     // This stops infinite cycles.
789fc763867STobias Grosser     if (DT->dominates(Exit, BB))
790fc763867STobias Grosser       break;
791fc763867STobias Grosser 
792fc763867STobias Grosser     BB = Exit;
793fc763867STobias Grosser   }
794fc763867STobias Grosser 
795fc763867STobias Grosser   return Exit;
796fc763867STobias Grosser }
797fc763867STobias Grosser 
798336734acSTobias Grosser Region*
799336734acSTobias Grosser RegionInfo::getCommonRegion(Region *A, Region *B) const {
800336734acSTobias Grosser   assert (A && B && "One of the Regions is NULL");
801336734acSTobias Grosser 
802336734acSTobias Grosser   if (A->contains(B)) return A;
803336734acSTobias Grosser 
804336734acSTobias Grosser   while (!B->contains(A))
805336734acSTobias Grosser     B = B->getParent();
806336734acSTobias Grosser 
807336734acSTobias Grosser   return B;
808336734acSTobias Grosser }
809336734acSTobias Grosser 
810336734acSTobias Grosser Region*
811336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
812336734acSTobias Grosser   Region* ret = Regions.back();
813336734acSTobias Grosser   Regions.pop_back();
814336734acSTobias Grosser 
815336734acSTobias Grosser   for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
816336734acSTobias Grosser        E = Regions.end(); I != E; ++I)
817336734acSTobias Grosser       ret = getCommonRegion(ret, *I);
818336734acSTobias Grosser 
819336734acSTobias Grosser   return ret;
820336734acSTobias Grosser }
821336734acSTobias Grosser 
822336734acSTobias Grosser Region*
823336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
824336734acSTobias Grosser   Region* ret = getRegionFor(BBs.back());
825336734acSTobias Grosser   BBs.pop_back();
826336734acSTobias Grosser 
827336734acSTobias Grosser   for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
828336734acSTobias Grosser        E = BBs.end(); I != E; ++I)
829336734acSTobias Grosser       ret = getCommonRegion(ret, getRegionFor(*I));
830336734acSTobias Grosser 
831336734acSTobias Grosser   return ret;
832336734acSTobias Grosser }
833336734acSTobias Grosser 
834fe92a938STobias Grosser void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
835fe92a938STobias Grosser {
836fe92a938STobias Grosser   Region *R = getRegionFor(OldBB);
8374b0986b6STobias Grosser 
838fe92a938STobias Grosser   setRegionFor(NewBB, R);
839fe92a938STobias Grosser 
8404b0986b6STobias Grosser   while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
841fe92a938STobias Grosser     R->replaceEntry(NewBB);
842fe92a938STobias Grosser     R = R->getParent();
843fe92a938STobias Grosser   }
844fe92a938STobias Grosser 
845fe92a938STobias Grosser   setRegionFor(OldBB, R);
846fe92a938STobias Grosser }
847fe92a938STobias Grosser 
848336734acSTobias Grosser char RegionInfo::ID = 0;
8498ac477ffSOwen Anderson INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
8508ac477ffSOwen Anderson                 "Detect single entry single exit regions", true, true)
8518ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominatorTree)
8528ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
8538ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
8548ac477ffSOwen Anderson INITIALIZE_PASS_END(RegionInfo, "regions",
855df7a4f25SOwen Anderson                 "Detect single entry single exit regions", true, true)
856336734acSTobias Grosser 
857336734acSTobias Grosser // Create methods available outside of this file, to use them
858336734acSTobias Grosser // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
859336734acSTobias Grosser // the link time optimization.
860336734acSTobias Grosser 
861336734acSTobias Grosser namespace llvm {
862336734acSTobias Grosser   FunctionPass *createRegionInfoPass() {
863336734acSTobias Grosser     return new RegionInfo();
864336734acSTobias Grosser   }
865336734acSTobias Grosser }
866336734acSTobias Grosser 
867