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/Analysis/RegionIterator.h"
14336734acSTobias Grosser 
15336734acSTobias Grosser #include "llvm/ADT/PostOrderIterator.h"
16336734acSTobias Grosser #include "llvm/ADT/Statistic.h"
17336734acSTobias Grosser #include "llvm/Support/CommandLine.h"
18336734acSTobias Grosser #include "llvm/Support/ErrorHandling.h"
191bec81a8STobias Grosser #include "llvm/Analysis/LoopInfo.h"
20bf0aa927SChris Lattner #include "llvm/Assembly/Writer.h"
21336734acSTobias Grosser 
22336734acSTobias Grosser #define DEBUG_TYPE "region"
23336734acSTobias Grosser #include "llvm/Support/Debug.h"
24336734acSTobias Grosser 
25336734acSTobias Grosser #include <set>
26336734acSTobias Grosser #include <algorithm>
27336734acSTobias Grosser 
28336734acSTobias Grosser using namespace llvm;
29336734acSTobias Grosser 
30336734acSTobias Grosser // Always verify if expensive checking is enabled.
31336734acSTobias Grosser #ifdef XDEBUG
32abfafadfSDan Gohman static bool VerifyRegionInfo = true;
33336734acSTobias Grosser #else
34abfafadfSDan Gohman static bool VerifyRegionInfo = false;
35336734acSTobias Grosser #endif
36336734acSTobias Grosser 
37336734acSTobias Grosser static cl::opt<bool,true>
38336734acSTobias Grosser VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
39336734acSTobias Grosser                 cl::desc("Verify region info (time consuming)"));
40336734acSTobias Grosser 
41336734acSTobias Grosser STATISTIC(numRegions,       "The # of regions");
42336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions");
43336734acSTobias Grosser 
448b304ff9STobias Grosser static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
458b304ff9STobias Grosser   cl::Hidden,
46336734acSTobias Grosser   cl::desc("style of printing regions"),
47336734acSTobias Grosser   cl::values(
488b304ff9STobias Grosser     clEnumValN(Region::PrintNone, "none",  "print no details"),
498b304ff9STobias Grosser     clEnumValN(Region::PrintBB, "bb",
50*14c05c40SHongbin Zheng                "print regions in detail with block_iterator"),
518b304ff9STobias Grosser     clEnumValN(Region::PrintRN, "rn",
528b304ff9STobias Grosser                "print regions in detail with element_iterator"),
53336734acSTobias Grosser     clEnumValEnd));
54336734acSTobias Grosser //===----------------------------------------------------------------------===//
55336734acSTobias Grosser /// Region Implementation
56336734acSTobias Grosser Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
57336734acSTobias Grosser                DominatorTree *dt, Region *Parent)
58336734acSTobias Grosser                : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
59336734acSTobias Grosser 
60336734acSTobias Grosser Region::~Region() {
6118e39cecSDaniel Dunbar   // Free the cached nodes.
6218e39cecSDaniel Dunbar   for (BBNodeMapT::iterator it = BBNodeMap.begin(),
6318e39cecSDaniel Dunbar          ie = BBNodeMap.end(); it != ie; ++it)
6418e39cecSDaniel Dunbar     delete it->second;
6518e39cecSDaniel Dunbar 
66336734acSTobias Grosser   // Only clean the cache for this Region. Caches of child Regions will be
67336734acSTobias Grosser   // cleaned when the child Regions are deleted.
68336734acSTobias Grosser   BBNodeMap.clear();
69336734acSTobias Grosser 
70336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
71336734acSTobias Grosser     delete *I;
72336734acSTobias Grosser }
73336734acSTobias Grosser 
74648594c9STobias Grosser void Region::replaceEntry(BasicBlock *BB) {
75648594c9STobias Grosser   entry.setPointer(BB);
76648594c9STobias Grosser }
77648594c9STobias Grosser 
78648594c9STobias Grosser void Region::replaceExit(BasicBlock *BB) {
79648594c9STobias Grosser   assert(exit && "No exit to replace!");
80648594c9STobias Grosser   exit = BB;
81648594c9STobias Grosser }
82648594c9STobias Grosser 
83336734acSTobias Grosser bool Region::contains(const BasicBlock *B) const {
84336734acSTobias Grosser   BasicBlock *BB = const_cast<BasicBlock*>(B);
85336734acSTobias Grosser 
86336734acSTobias Grosser   assert(DT->getNode(BB) && "BB not part of the dominance tree");
87336734acSTobias Grosser 
88336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
89336734acSTobias Grosser 
90336734acSTobias Grosser   // Toplevel region.
91336734acSTobias Grosser   if (!exit)
92336734acSTobias Grosser     return true;
93336734acSTobias Grosser 
94336734acSTobias Grosser   return (DT->dominates(entry, BB)
95336734acSTobias Grosser     && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
96336734acSTobias Grosser }
97336734acSTobias Grosser 
981bec81a8STobias Grosser bool Region::contains(const Loop *L) const {
991bec81a8STobias Grosser   // BBs that are not part of any loop are element of the Loop
1001bec81a8STobias Grosser   // described by the NULL pointer. This loop is not part of any region,
1011bec81a8STobias Grosser   // except if the region describes the whole function.
1021bec81a8STobias Grosser   if (L == 0)
1031bec81a8STobias Grosser     return getExit() == 0;
1041bec81a8STobias Grosser 
1051bec81a8STobias Grosser   if (!contains(L->getHeader()))
1061bec81a8STobias Grosser     return false;
1071bec81a8STobias Grosser 
1081bec81a8STobias Grosser   SmallVector<BasicBlock *, 8> ExitingBlocks;
1091bec81a8STobias Grosser   L->getExitingBlocks(ExitingBlocks);
1101bec81a8STobias Grosser 
1111bec81a8STobias Grosser   for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
1121bec81a8STobias Grosser        BE = ExitingBlocks.end(); BI != BE; ++BI)
1131bec81a8STobias Grosser     if (!contains(*BI))
1141bec81a8STobias Grosser       return false;
1151bec81a8STobias Grosser 
1161bec81a8STobias Grosser   return true;
1171bec81a8STobias Grosser }
1181bec81a8STobias Grosser 
1191bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(Loop *L) const {
1201bec81a8STobias Grosser   if (!contains(L))
1211bec81a8STobias Grosser     return 0;
1221bec81a8STobias Grosser 
1231bec81a8STobias Grosser   while (L && contains(L->getParentLoop())) {
1241bec81a8STobias Grosser     L = L->getParentLoop();
1251bec81a8STobias Grosser   }
1261bec81a8STobias Grosser 
1271bec81a8STobias Grosser   return L;
1281bec81a8STobias Grosser }
1291bec81a8STobias Grosser 
1301bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
1311bec81a8STobias Grosser   assert(LI && BB && "LI and BB cannot be null!");
1321bec81a8STobias Grosser   Loop *L = LI->getLoopFor(BB);
1331bec81a8STobias Grosser   return outermostLoopInRegion(L);
1341bec81a8STobias Grosser }
1351bec81a8STobias Grosser 
136b1d11c19STobias Grosser BasicBlock *Region::getEnteringBlock() const {
137b1d11c19STobias Grosser   BasicBlock *entry = getEntry();
138b1d11c19STobias Grosser   BasicBlock *Pred;
139b1d11c19STobias Grosser   BasicBlock *enteringBlock = 0;
140336734acSTobias Grosser 
141336734acSTobias Grosser   for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
1427fbe6cb4STobias Grosser        ++PI) {
143b1d11c19STobias Grosser     Pred = *PI;
1447fbe6cb4STobias Grosser     if (DT->getNode(Pred) && !contains(Pred)) {
145b1d11c19STobias Grosser       if (enteringBlock)
146b1d11c19STobias Grosser         return 0;
147b1d11c19STobias Grosser 
148b1d11c19STobias Grosser       enteringBlock = Pred;
149336734acSTobias Grosser     }
1507fbe6cb4STobias Grosser   }
151336734acSTobias Grosser 
152b1d11c19STobias Grosser   return enteringBlock;
153b1d11c19STobias Grosser }
154b1d11c19STobias Grosser 
155b1d11c19STobias Grosser BasicBlock *Region::getExitingBlock() const {
156b1d11c19STobias Grosser   BasicBlock *exit = getExit();
157b1d11c19STobias Grosser   BasicBlock *Pred;
158b1d11c19STobias Grosser   BasicBlock *exitingBlock = 0;
159b1d11c19STobias Grosser 
160b1d11c19STobias Grosser   if (!exit)
161b1d11c19STobias Grosser     return 0;
162336734acSTobias Grosser 
163336734acSTobias Grosser   for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
164b1d11c19STobias Grosser        ++PI) {
165b1d11c19STobias Grosser     Pred = *PI;
166b1d11c19STobias Grosser     if (contains(Pred)) {
167b1d11c19STobias Grosser       if (exitingBlock)
168b1d11c19STobias Grosser         return 0;
169b1d11c19STobias Grosser 
170b1d11c19STobias Grosser       exitingBlock = Pred;
171336734acSTobias Grosser     }
172336734acSTobias Grosser   }
173336734acSTobias Grosser 
174b1d11c19STobias Grosser   return exitingBlock;
175b1d11c19STobias Grosser }
176b1d11c19STobias Grosser 
177b1d11c19STobias Grosser bool Region::isSimple() const {
178b1d11c19STobias Grosser   return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
179336734acSTobias Grosser }
180336734acSTobias Grosser 
1811bec81a8STobias Grosser std::string Region::getNameStr() const {
1821bec81a8STobias Grosser   std::string exitName;
1831bec81a8STobias Grosser   std::string entryName;
1841bec81a8STobias Grosser 
1851bec81a8STobias Grosser   if (getEntry()->getName().empty()) {
1861bec81a8STobias Grosser     raw_string_ostream OS(entryName);
1871bec81a8STobias Grosser 
1881bec81a8STobias Grosser     WriteAsOperand(OS, getEntry(), false);
1891bec81a8STobias Grosser   } else
190184e3ceeSBenjamin Kramer     entryName = getEntry()->getName();
1911bec81a8STobias Grosser 
1921bec81a8STobias Grosser   if (getExit()) {
1931bec81a8STobias Grosser     if (getExit()->getName().empty()) {
1941bec81a8STobias Grosser       raw_string_ostream OS(exitName);
1951bec81a8STobias Grosser 
1961bec81a8STobias Grosser       WriteAsOperand(OS, getExit(), false);
1971bec81a8STobias Grosser     } else
198184e3ceeSBenjamin Kramer       exitName = getExit()->getName();
1991bec81a8STobias Grosser   } else
2001bec81a8STobias Grosser     exitName = "<Function Return>";
2011bec81a8STobias Grosser 
2021bec81a8STobias Grosser   return entryName + " => " + exitName;
2031bec81a8STobias Grosser }
2041bec81a8STobias Grosser 
205336734acSTobias Grosser void Region::verifyBBInRegion(BasicBlock *BB) const {
206336734acSTobias Grosser   if (!contains(BB))
207336734acSTobias Grosser     llvm_unreachable("Broken region found!");
208336734acSTobias Grosser 
209336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
210336734acSTobias Grosser 
211336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
212336734acSTobias Grosser     if (!contains(*SI) && exit != *SI)
213336734acSTobias Grosser       llvm_unreachable("Broken region found!");
214336734acSTobias Grosser 
215336734acSTobias Grosser   if (entry != BB)
216336734acSTobias Grosser     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
217336734acSTobias Grosser       if (!contains(*SI))
218336734acSTobias Grosser         llvm_unreachable("Broken region found!");
219336734acSTobias Grosser }
220336734acSTobias Grosser 
221336734acSTobias Grosser void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
222336734acSTobias Grosser   BasicBlock *exit = getExit();
223336734acSTobias Grosser 
224336734acSTobias Grosser   visited->insert(BB);
225336734acSTobias Grosser 
226336734acSTobias Grosser   verifyBBInRegion(BB);
227336734acSTobias Grosser 
228336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
229336734acSTobias Grosser     if (*SI != exit && visited->find(*SI) == visited->end())
230336734acSTobias Grosser         verifyWalk(*SI, visited);
231336734acSTobias Grosser }
232336734acSTobias Grosser 
233336734acSTobias Grosser void Region::verifyRegion() const {
234336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive
235336734acSTobias Grosser   // check will be invoked by PassManager.
236336734acSTobias Grosser   if (!VerifyRegionInfo) return;
237336734acSTobias Grosser 
238336734acSTobias Grosser   std::set<BasicBlock*> visited;
239336734acSTobias Grosser   verifyWalk(getEntry(), &visited);
240336734acSTobias Grosser }
241336734acSTobias Grosser 
242336734acSTobias Grosser void Region::verifyRegionNest() const {
243336734acSTobias Grosser   for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
244336734acSTobias Grosser     (*RI)->verifyRegionNest();
245336734acSTobias Grosser 
246336734acSTobias Grosser   verifyRegion();
247336734acSTobias Grosser }
248336734acSTobias Grosser 
249336734acSTobias Grosser Region::element_iterator Region::element_begin() {
250336734acSTobias Grosser   return GraphTraits<Region*>::nodes_begin(this);
251336734acSTobias Grosser }
252336734acSTobias Grosser 
253336734acSTobias Grosser Region::element_iterator Region::element_end() {
254336734acSTobias Grosser   return GraphTraits<Region*>::nodes_end(this);
255336734acSTobias Grosser }
256336734acSTobias Grosser 
257336734acSTobias Grosser Region::const_element_iterator Region::element_begin() const {
258336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_begin(this);
259336734acSTobias Grosser }
260336734acSTobias Grosser 
261336734acSTobias Grosser Region::const_element_iterator Region::element_end() const {
262336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_end(this);
263336734acSTobias Grosser }
264336734acSTobias Grosser 
265336734acSTobias Grosser Region* Region::getSubRegionNode(BasicBlock *BB) const {
266336734acSTobias Grosser   Region *R = RI->getRegionFor(BB);
267336734acSTobias Grosser 
268336734acSTobias Grosser   if (!R || R == this)
269336734acSTobias Grosser     return 0;
270336734acSTobias Grosser 
271336734acSTobias Grosser   // If we pass the BB out of this region, that means our code is broken.
272336734acSTobias Grosser   assert(contains(R) && "BB not in current region!");
273336734acSTobias Grosser 
274336734acSTobias Grosser   while (contains(R->getParent()) && R->getParent() != this)
275336734acSTobias Grosser     R = R->getParent();
276336734acSTobias Grosser 
277336734acSTobias Grosser   if (R->getEntry() != BB)
278336734acSTobias Grosser     return 0;
279336734acSTobias Grosser 
280336734acSTobias Grosser   return R;
281336734acSTobias Grosser }
282336734acSTobias Grosser 
283336734acSTobias Grosser RegionNode* Region::getBBNode(BasicBlock *BB) const {
284336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
285336734acSTobias Grosser 
286336734acSTobias Grosser   BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
287336734acSTobias Grosser 
288336734acSTobias Grosser   if (at != BBNodeMap.end())
289336734acSTobias Grosser     return at->second;
290336734acSTobias Grosser 
291336734acSTobias Grosser   RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
292336734acSTobias Grosser   BBNodeMap.insert(std::make_pair(BB, NewNode));
293336734acSTobias Grosser   return NewNode;
294336734acSTobias Grosser }
295336734acSTobias Grosser 
296336734acSTobias Grosser RegionNode* Region::getNode(BasicBlock *BB) const {
297336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
298336734acSTobias Grosser   if (Region* Child = getSubRegionNode(BB))
299336734acSTobias Grosser     return Child->getNode();
300336734acSTobias Grosser 
301336734acSTobias Grosser   return getBBNode(BB);
302336734acSTobias Grosser }
303336734acSTobias Grosser 
304336734acSTobias Grosser void Region::transferChildrenTo(Region *To) {
305336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I) {
306336734acSTobias Grosser     (*I)->parent = To;
307336734acSTobias Grosser     To->children.push_back(*I);
308336734acSTobias Grosser   }
309336734acSTobias Grosser   children.clear();
310336734acSTobias Grosser }
311336734acSTobias Grosser 
312bf984fd7STobias Grosser void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
313336734acSTobias Grosser   assert(SubRegion->parent == 0 && "SubRegion already has a parent!");
314bf984fd7STobias Grosser   assert(std::find(begin(), end(), SubRegion) == children.end()
315bf984fd7STobias Grosser          && "Subregion already exists!");
316bf984fd7STobias Grosser 
317336734acSTobias Grosser   SubRegion->parent = this;
318336734acSTobias Grosser   children.push_back(SubRegion);
319bf984fd7STobias Grosser 
320bf984fd7STobias Grosser   if (!moveChildren)
321bf984fd7STobias Grosser     return;
322bf984fd7STobias Grosser 
323bf984fd7STobias Grosser   assert(SubRegion->children.size() == 0
324bf984fd7STobias Grosser          && "SubRegions that contain children are not supported");
325bf984fd7STobias Grosser 
326bf984fd7STobias Grosser   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
327bf984fd7STobias Grosser     if (!(*I)->isSubRegion()) {
328bf984fd7STobias Grosser       BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
329bf984fd7STobias Grosser 
330bf984fd7STobias Grosser       if (SubRegion->contains(BB))
331bf984fd7STobias Grosser         RI->setRegionFor(BB, SubRegion);
332bf984fd7STobias Grosser     }
333bf984fd7STobias Grosser 
334bf984fd7STobias Grosser   std::vector<Region*> Keep;
335bf984fd7STobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
336bf984fd7STobias Grosser     if (SubRegion->contains(*I) && *I != SubRegion) {
337bf984fd7STobias Grosser       SubRegion->children.push_back(*I);
338bf984fd7STobias Grosser       (*I)->parent = SubRegion;
339bf984fd7STobias Grosser     } else
340bf984fd7STobias Grosser       Keep.push_back(*I);
341bf984fd7STobias Grosser 
342bf984fd7STobias Grosser   children.clear();
343bf984fd7STobias Grosser   children.insert(children.begin(), Keep.begin(), Keep.end());
344336734acSTobias Grosser }
345336734acSTobias Grosser 
346336734acSTobias Grosser 
347336734acSTobias Grosser Region *Region::removeSubRegion(Region *Child) {
348336734acSTobias Grosser   assert(Child->parent == this && "Child is not a child of this region!");
349336734acSTobias Grosser   Child->parent = 0;
350336734acSTobias Grosser   RegionSet::iterator I = std::find(children.begin(), children.end(), Child);
351336734acSTobias Grosser   assert(I != children.end() && "Region does not exit. Unable to remove.");
352336734acSTobias Grosser   children.erase(children.begin()+(I-begin()));
353336734acSTobias Grosser   return Child;
354336734acSTobias Grosser }
355336734acSTobias Grosser 
356336734acSTobias Grosser unsigned Region::getDepth() const {
357336734acSTobias Grosser   unsigned Depth = 0;
358336734acSTobias Grosser 
359336734acSTobias Grosser   for (Region *R = parent; R != 0; R = R->parent)
360336734acSTobias Grosser     ++Depth;
361336734acSTobias Grosser 
362336734acSTobias Grosser   return Depth;
363336734acSTobias Grosser }
364336734acSTobias Grosser 
365a8677226STobias Grosser Region *Region::getExpandedRegion() const {
366a8677226STobias Grosser   unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
367a8677226STobias Grosser 
368a8677226STobias Grosser   if (NumSuccessors == 0)
369a8677226STobias Grosser     return NULL;
370a8677226STobias Grosser 
371a8677226STobias Grosser   for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
372a8677226STobias Grosser        PI != PE; ++PI)
373a8677226STobias Grosser     if (!DT->dominates(getEntry(), *PI))
374a8677226STobias Grosser       return NULL;
375a8677226STobias Grosser 
376a8677226STobias Grosser   Region *R = RI->getRegionFor(exit);
377a8677226STobias Grosser 
378a8677226STobias Grosser   if (R->getEntry() != exit) {
379a8677226STobias Grosser     if (exit->getTerminator()->getNumSuccessors() == 1)
380a8677226STobias Grosser       return new Region(getEntry(), *succ_begin(exit), RI, DT);
381a8677226STobias Grosser     else
382a8677226STobias Grosser       return NULL;
383a8677226STobias Grosser   }
384a8677226STobias Grosser 
385a8677226STobias Grosser   while (R->getParent() && R->getParent()->getEntry() == exit)
386a8677226STobias Grosser     R = R->getParent();
387a8677226STobias Grosser 
388a8677226STobias Grosser   if (!DT->dominates(getEntry(), R->getExit()))
389a8677226STobias Grosser     for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
390a8677226STobias Grosser          PI != PE; ++PI)
391a8677226STobias Grosser     if (!DT->dominates(R->getExit(), *PI))
392a8677226STobias Grosser       return NULL;
393a8677226STobias Grosser 
394a8677226STobias Grosser   return new Region(getEntry(), R->getExit(), RI, DT);
395a8677226STobias Grosser }
396a8677226STobias Grosser 
3978b304ff9STobias Grosser void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
3988b304ff9STobias Grosser                    enum PrintStyle Style) const {
399336734acSTobias Grosser   if (print_tree)
400336734acSTobias Grosser     OS.indent(level*2) << "[" << level << "] " << getNameStr();
401336734acSTobias Grosser   else
402336734acSTobias Grosser     OS.indent(level*2) << getNameStr();
403336734acSTobias Grosser 
404336734acSTobias Grosser   OS << "\n";
405336734acSTobias Grosser 
406336734acSTobias Grosser 
4078b304ff9STobias Grosser   if (Style != PrintNone) {
408336734acSTobias Grosser     OS.indent(level*2) << "{\n";
409336734acSTobias Grosser     OS.indent(level*2 + 2);
410336734acSTobias Grosser 
4118b304ff9STobias Grosser     if (Style == PrintBB) {
412*14c05c40SHongbin Zheng       for (const_block_iterator I = block_begin(), E = block_end(); I != E; ++I)
413*14c05c40SHongbin Zheng         OS << (*I)->getName() << ", "; // TODO: remove the last ","
4148b304ff9STobias Grosser     } else if (Style == PrintRN) {
415336734acSTobias Grosser       for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
416336734acSTobias Grosser         OS << **I << ", "; // TODO: remove the last ",
417336734acSTobias Grosser     }
418336734acSTobias Grosser 
419336734acSTobias Grosser     OS << "\n";
420336734acSTobias Grosser   }
421336734acSTobias Grosser 
422336734acSTobias Grosser   if (print_tree)
423336734acSTobias Grosser     for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
4248b304ff9STobias Grosser       (*RI)->print(OS, print_tree, level+1, Style);
425336734acSTobias Grosser 
4268b304ff9STobias Grosser   if (Style != PrintNone)
427336734acSTobias Grosser     OS.indent(level*2) << "} \n";
428336734acSTobias Grosser }
429336734acSTobias Grosser 
430336734acSTobias Grosser void Region::dump() const {
4318b304ff9STobias Grosser   print(dbgs(), true, getDepth(), printStyle.getValue());
432336734acSTobias Grosser }
433336734acSTobias Grosser 
434336734acSTobias Grosser void Region::clearNodeCache() {
435e910b9d9STobias Grosser   // Free the cached nodes.
436e910b9d9STobias Grosser   for (BBNodeMapT::iterator I = BBNodeMap.begin(),
4374c71c117STobias Grosser        IE = BBNodeMap.end(); I != IE; ++I)
438e910b9d9STobias Grosser     delete I->second;
439e910b9d9STobias Grosser 
440336734acSTobias Grosser   BBNodeMap.clear();
441336734acSTobias Grosser   for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
442336734acSTobias Grosser     (*RI)->clearNodeCache();
443336734acSTobias Grosser }
444336734acSTobias Grosser 
445336734acSTobias Grosser //===----------------------------------------------------------------------===//
446336734acSTobias Grosser // RegionInfo implementation
447336734acSTobias Grosser //
448336734acSTobias Grosser 
449336734acSTobias Grosser bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
450336734acSTobias Grosser                                      BasicBlock *exit) const {
45107c8ad54SGabor Greif   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
45207c8ad54SGabor Greif     BasicBlock *P = *PI;
45307c8ad54SGabor Greif     if (DT->dominates(entry, P) && !DT->dominates(exit, P))
454336734acSTobias Grosser       return false;
45507c8ad54SGabor Greif   }
456336734acSTobias Grosser   return true;
457336734acSTobias Grosser }
458336734acSTobias Grosser 
459336734acSTobias Grosser bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
460336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
461336734acSTobias Grosser   typedef DominanceFrontier::DomSetType DST;
462336734acSTobias Grosser 
463d9f48ecbSGabor Greif   DST *entrySuccs = &DF->find(entry)->second;
464336734acSTobias Grosser 
465336734acSTobias Grosser   // Exit is the header of a loop that contains the entry. In this case,
466336734acSTobias Grosser   // the dominance frontier must only contain the exit.
467336734acSTobias Grosser   if (!DT->dominates(entry, exit)) {
468336734acSTobias Grosser     for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
469336734acSTobias Grosser          SI != SE; ++SI)
470336734acSTobias Grosser       if (*SI != exit && *SI != entry)
471336734acSTobias Grosser         return false;
472336734acSTobias Grosser 
473336734acSTobias Grosser     return true;
474336734acSTobias Grosser   }
475336734acSTobias Grosser 
476d9f48ecbSGabor Greif   DST *exitSuccs = &DF->find(exit)->second;
477336734acSTobias Grosser 
478336734acSTobias Grosser   // Do not allow edges leaving the region.
479336734acSTobias Grosser   for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
480336734acSTobias Grosser        SI != SE; ++SI) {
481336734acSTobias Grosser     if (*SI == exit || *SI == entry)
482336734acSTobias Grosser       continue;
483336734acSTobias Grosser     if (exitSuccs->find(*SI) == exitSuccs->end())
484336734acSTobias Grosser       return false;
485336734acSTobias Grosser     if (!isCommonDomFrontier(*SI, entry, exit))
486336734acSTobias Grosser       return false;
487336734acSTobias Grosser   }
488336734acSTobias Grosser 
489336734acSTobias Grosser   // Do not allow edges pointing into the region.
490336734acSTobias Grosser   for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
491336734acSTobias Grosser        SI != SE; ++SI)
492b3aa6c71SDan Gohman     if (DT->properlyDominates(entry, *SI) && *SI != exit)
493336734acSTobias Grosser       return false;
494336734acSTobias Grosser 
495336734acSTobias Grosser 
496336734acSTobias Grosser   return true;
497336734acSTobias Grosser }
498336734acSTobias Grosser 
499336734acSTobias Grosser void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
500336734acSTobias Grosser                              BBtoBBMap *ShortCut) const {
501336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
502336734acSTobias Grosser 
503336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(exit);
504336734acSTobias Grosser 
505336734acSTobias Grosser   if (e == ShortCut->end())
506336734acSTobias Grosser     // No further region at exit available.
507336734acSTobias Grosser     (*ShortCut)[entry] = exit;
508336734acSTobias Grosser   else {
509336734acSTobias Grosser     // We found a region e that starts at exit. Therefore (entry, e->second)
510336734acSTobias Grosser     // is also a region, that is larger than (entry, exit). Insert the
511336734acSTobias Grosser     // larger one.
512336734acSTobias Grosser     BasicBlock *BB = e->second;
513336734acSTobias Grosser     (*ShortCut)[entry] = BB;
514336734acSTobias Grosser   }
515336734acSTobias Grosser }
516336734acSTobias Grosser 
517336734acSTobias Grosser DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
518336734acSTobias Grosser                                         BBtoBBMap *ShortCut) const {
519336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
520336734acSTobias Grosser 
521336734acSTobias Grosser   if (e == ShortCut->end())
522336734acSTobias Grosser     return N->getIDom();
523336734acSTobias Grosser 
524336734acSTobias Grosser   return PDT->getNode(e->second)->getIDom();
525336734acSTobias Grosser }
526336734acSTobias Grosser 
527336734acSTobias Grosser bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
528336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
529336734acSTobias Grosser 
530336734acSTobias Grosser   unsigned num_successors = succ_end(entry) - succ_begin(entry);
531336734acSTobias Grosser 
532336734acSTobias Grosser   if (num_successors <= 1 && exit == *(succ_begin(entry)))
533336734acSTobias Grosser     return true;
534336734acSTobias Grosser 
535336734acSTobias Grosser   return false;
536336734acSTobias Grosser }
537336734acSTobias Grosser 
538336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) {
539336734acSTobias Grosser   ++numRegions;
540336734acSTobias Grosser 
541336734acSTobias Grosser   // TODO: Slow. Should only be enabled if -stats is used.
542336734acSTobias Grosser   if (R->isSimple()) ++numSimpleRegions;
543336734acSTobias Grosser }
544336734acSTobias Grosser 
545336734acSTobias Grosser Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
546336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
547336734acSTobias Grosser 
548336734acSTobias Grosser   if (isTrivialRegion(entry, exit))
549336734acSTobias Grosser     return 0;
550336734acSTobias Grosser 
551336734acSTobias Grosser   Region *region = new Region(entry, exit, this, DT);
552336734acSTobias Grosser   BBtoRegion.insert(std::make_pair(entry, region));
553336734acSTobias Grosser 
554336734acSTobias Grosser  #ifdef XDEBUG
555336734acSTobias Grosser     region->verifyRegion();
556336734acSTobias Grosser  #else
557336734acSTobias Grosser     DEBUG(region->verifyRegion());
558336734acSTobias Grosser  #endif
559336734acSTobias Grosser 
560336734acSTobias Grosser   updateStatistics(region);
561336734acSTobias Grosser   return region;
562336734acSTobias Grosser }
563336734acSTobias Grosser 
564336734acSTobias Grosser void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
565336734acSTobias Grosser   assert(entry);
566336734acSTobias Grosser 
567336734acSTobias Grosser   DomTreeNode *N = PDT->getNode(entry);
568336734acSTobias Grosser 
569336734acSTobias Grosser   if (!N)
570336734acSTobias Grosser     return;
571336734acSTobias Grosser 
572336734acSTobias Grosser   Region *lastRegion= 0;
573336734acSTobias Grosser   BasicBlock *lastExit = entry;
574336734acSTobias Grosser 
575336734acSTobias Grosser   // As only a BasicBlock that postdominates entry can finish a region, walk the
576336734acSTobias Grosser   // post dominance tree upwards.
577336734acSTobias Grosser   while ((N = getNextPostDom(N, ShortCut))) {
578336734acSTobias Grosser     BasicBlock *exit = N->getBlock();
579336734acSTobias Grosser 
580336734acSTobias Grosser     if (!exit)
581336734acSTobias Grosser       break;
582336734acSTobias Grosser 
583336734acSTobias Grosser     if (isRegion(entry, exit)) {
584336734acSTobias Grosser       Region *newRegion = createRegion(entry, exit);
585336734acSTobias Grosser 
586336734acSTobias Grosser       if (lastRegion)
587336734acSTobias Grosser         newRegion->addSubRegion(lastRegion);
588336734acSTobias Grosser 
589336734acSTobias Grosser       lastRegion = newRegion;
590336734acSTobias Grosser       lastExit = exit;
591336734acSTobias Grosser     }
592336734acSTobias Grosser 
593336734acSTobias Grosser     // This can never be a region, so stop the search.
594336734acSTobias Grosser     if (!DT->dominates(entry, exit))
595336734acSTobias Grosser       break;
596336734acSTobias Grosser   }
597336734acSTobias Grosser 
598336734acSTobias Grosser   // Tried to create regions from entry to lastExit.  Next time take a
599336734acSTobias Grosser   // shortcut from entry to lastExit.
600336734acSTobias Grosser   if (lastExit != entry)
601336734acSTobias Grosser     insertShortCut(entry, lastExit, ShortCut);
602336734acSTobias Grosser }
603336734acSTobias Grosser 
604336734acSTobias Grosser void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
605336734acSTobias Grosser   BasicBlock *entry = &(F.getEntryBlock());
606336734acSTobias Grosser   DomTreeNode *N = DT->getNode(entry);
607336734acSTobias Grosser 
608336734acSTobias Grosser   // Iterate over the dominance tree in post order to start with the small
609336734acSTobias Grosser   // regions from the bottom of the dominance tree.  If the small regions are
610336734acSTobias Grosser   // detected first, detection of bigger regions is faster, as we can jump
611336734acSTobias Grosser   // over the small regions.
612336734acSTobias Grosser   for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
613336734acSTobias Grosser     ++FI) {
6141a2da423SGabor Greif     findRegionsWithEntry(FI->getBlock(), ShortCut);
615336734acSTobias Grosser   }
616336734acSTobias Grosser }
617336734acSTobias Grosser 
618336734acSTobias Grosser Region *RegionInfo::getTopMostParent(Region *region) {
619336734acSTobias Grosser   while (region->parent)
620336734acSTobias Grosser     region = region->getParent();
621336734acSTobias Grosser 
622336734acSTobias Grosser   return region;
623336734acSTobias Grosser }
624336734acSTobias Grosser 
625336734acSTobias Grosser void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
626336734acSTobias Grosser   BasicBlock *BB = N->getBlock();
627336734acSTobias Grosser 
628336734acSTobias Grosser   // Passed region exit
629336734acSTobias Grosser   while (BB == region->getExit())
630336734acSTobias Grosser     region = region->getParent();
631336734acSTobias Grosser 
632336734acSTobias Grosser   BBtoRegionMap::iterator it = BBtoRegion.find(BB);
633336734acSTobias Grosser 
634336734acSTobias Grosser   // This basic block is a start block of a region. It is already in the
635336734acSTobias Grosser   // BBtoRegion relation. Only the child basic blocks have to be updated.
636336734acSTobias Grosser   if (it != BBtoRegion.end()) {
6375dfe6dabSChad Rosier     Region *newRegion = it->second;
638336734acSTobias Grosser     region->addSubRegion(getTopMostParent(newRegion));
639336734acSTobias Grosser     region = newRegion;
640336734acSTobias Grosser   } else {
641336734acSTobias Grosser     BBtoRegion[BB] = region;
642336734acSTobias Grosser   }
643336734acSTobias Grosser 
644336734acSTobias Grosser   for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
645336734acSTobias Grosser     buildRegionsTree(*CI, region);
646336734acSTobias Grosser }
647336734acSTobias Grosser 
648336734acSTobias Grosser void RegionInfo::releaseMemory() {
649336734acSTobias Grosser   BBtoRegion.clear();
650336734acSTobias Grosser   if (TopLevelRegion)
651336734acSTobias Grosser     delete TopLevelRegion;
652336734acSTobias Grosser   TopLevelRegion = 0;
653336734acSTobias Grosser }
654336734acSTobias Grosser 
655a7aed186SOwen Anderson RegionInfo::RegionInfo() : FunctionPass(ID) {
6566c18d1aaSOwen Anderson   initializeRegionInfoPass(*PassRegistry::getPassRegistry());
657336734acSTobias Grosser   TopLevelRegion = 0;
658336734acSTobias Grosser }
659336734acSTobias Grosser 
660336734acSTobias Grosser RegionInfo::~RegionInfo() {
661336734acSTobias Grosser   releaseMemory();
662336734acSTobias Grosser }
663336734acSTobias Grosser 
664336734acSTobias Grosser void RegionInfo::Calculate(Function &F) {
665336734acSTobias Grosser   // ShortCut a function where for every BB the exit of the largest region
666336734acSTobias Grosser   // starting with BB is stored. These regions can be threated as single BBS.
667336734acSTobias Grosser   // This improves performance on linear CFGs.
668336734acSTobias Grosser   BBtoBBMap ShortCut;
669336734acSTobias Grosser 
670336734acSTobias Grosser   scanForRegions(F, &ShortCut);
671336734acSTobias Grosser   BasicBlock *BB = &F.getEntryBlock();
672336734acSTobias Grosser   buildRegionsTree(DT->getNode(BB), TopLevelRegion);
673336734acSTobias Grosser }
674336734acSTobias Grosser 
675336734acSTobias Grosser bool RegionInfo::runOnFunction(Function &F) {
676336734acSTobias Grosser   releaseMemory();
677336734acSTobias Grosser 
678336734acSTobias Grosser   DT = &getAnalysis<DominatorTree>();
679336734acSTobias Grosser   PDT = &getAnalysis<PostDominatorTree>();
680336734acSTobias Grosser   DF = &getAnalysis<DominanceFrontier>();
681336734acSTobias Grosser 
682336734acSTobias Grosser   TopLevelRegion = new Region(&F.getEntryBlock(), 0, this, DT, 0);
683336734acSTobias Grosser   updateStatistics(TopLevelRegion);
684336734acSTobias Grosser 
685336734acSTobias Grosser   Calculate(F);
686336734acSTobias Grosser 
687336734acSTobias Grosser   return false;
688336734acSTobias Grosser }
689336734acSTobias Grosser 
690336734acSTobias Grosser void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
691336734acSTobias Grosser   AU.setPreservesAll();
692336734acSTobias Grosser   AU.addRequiredTransitive<DominatorTree>();
693336734acSTobias Grosser   AU.addRequired<PostDominatorTree>();
694336734acSTobias Grosser   AU.addRequired<DominanceFrontier>();
695336734acSTobias Grosser }
696336734acSTobias Grosser 
697336734acSTobias Grosser void RegionInfo::print(raw_ostream &OS, const Module *) const {
698336734acSTobias Grosser   OS << "Region tree:\n";
6998b304ff9STobias Grosser   TopLevelRegion->print(OS, true, 0, printStyle.getValue());
700336734acSTobias Grosser   OS << "End region tree\n";
701336734acSTobias Grosser }
702336734acSTobias Grosser 
703336734acSTobias Grosser void RegionInfo::verifyAnalysis() const {
704336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive check
705336734acSTobias Grosser   // will be invoked by PMDataManager::verifyPreservedAnalysis when
706336734acSTobias Grosser   // a regionpass (marked PreservedAll) finish.
707336734acSTobias Grosser   if (!VerifyRegionInfo) return;
708336734acSTobias Grosser 
709336734acSTobias Grosser   TopLevelRegion->verifyRegionNest();
710336734acSTobias Grosser }
711336734acSTobias Grosser 
712336734acSTobias Grosser // Region pass manager support.
713336734acSTobias Grosser Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
714336734acSTobias Grosser   BBtoRegionMap::const_iterator I=
715336734acSTobias Grosser     BBtoRegion.find(BB);
716336734acSTobias Grosser   return I != BBtoRegion.end() ? I->second : 0;
717336734acSTobias Grosser }
718336734acSTobias Grosser 
7198352ce5fSTobias Grosser void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
7208352ce5fSTobias Grosser   BBtoRegion[BB] = R;
7218352ce5fSTobias Grosser }
7228352ce5fSTobias Grosser 
723336734acSTobias Grosser Region *RegionInfo::operator[](BasicBlock *BB) const {
724336734acSTobias Grosser   return getRegionFor(BB);
725336734acSTobias Grosser }
726336734acSTobias Grosser 
727fc763867STobias Grosser BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
728fc763867STobias Grosser   BasicBlock *Exit = NULL;
729fc763867STobias Grosser 
730fc763867STobias Grosser   while (true) {
731fc763867STobias Grosser     // Get largest region that starts at BB.
732fc763867STobias Grosser     Region *R = getRegionFor(BB);
733fc763867STobias Grosser     while (R && R->getParent() && R->getParent()->getEntry() == BB)
734fc763867STobias Grosser       R = R->getParent();
735fc763867STobias Grosser 
736fc763867STobias Grosser     // Get the single exit of BB.
737fc763867STobias Grosser     if (R && R->getEntry() == BB)
738fc763867STobias Grosser       Exit = R->getExit();
739fc763867STobias Grosser     else if (++succ_begin(BB) == succ_end(BB))
740fc763867STobias Grosser       Exit = *succ_begin(BB);
741fc763867STobias Grosser     else // No single exit exists.
742fc763867STobias Grosser       return Exit;
743fc763867STobias Grosser 
744fc763867STobias Grosser     // Get largest region that starts at Exit.
745fc763867STobias Grosser     Region *ExitR = getRegionFor(Exit);
746fc763867STobias Grosser     while (ExitR && ExitR->getParent()
747fc763867STobias Grosser            && ExitR->getParent()->getEntry() == Exit)
748fc763867STobias Grosser       ExitR = ExitR->getParent();
749fc763867STobias Grosser 
750fc763867STobias Grosser     for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
751fc763867STobias Grosser          ++PI)
752fc763867STobias Grosser       if (!R->contains(*PI) && !ExitR->contains(*PI))
753fc763867STobias Grosser         break;
754fc763867STobias Grosser 
755fc763867STobias Grosser     // This stops infinite cycles.
756fc763867STobias Grosser     if (DT->dominates(Exit, BB))
757fc763867STobias Grosser       break;
758fc763867STobias Grosser 
759fc763867STobias Grosser     BB = Exit;
760fc763867STobias Grosser   }
761fc763867STobias Grosser 
762fc763867STobias Grosser   return Exit;
763fc763867STobias Grosser }
764fc763867STobias Grosser 
765336734acSTobias Grosser Region*
766336734acSTobias Grosser RegionInfo::getCommonRegion(Region *A, Region *B) const {
767336734acSTobias Grosser   assert (A && B && "One of the Regions is NULL");
768336734acSTobias Grosser 
769336734acSTobias Grosser   if (A->contains(B)) return A;
770336734acSTobias Grosser 
771336734acSTobias Grosser   while (!B->contains(A))
772336734acSTobias Grosser     B = B->getParent();
773336734acSTobias Grosser 
774336734acSTobias Grosser   return B;
775336734acSTobias Grosser }
776336734acSTobias Grosser 
777336734acSTobias Grosser Region*
778336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
779336734acSTobias Grosser   Region* ret = Regions.back();
780336734acSTobias Grosser   Regions.pop_back();
781336734acSTobias Grosser 
782336734acSTobias Grosser   for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
783336734acSTobias Grosser        E = Regions.end(); I != E; ++I)
784336734acSTobias Grosser       ret = getCommonRegion(ret, *I);
785336734acSTobias Grosser 
786336734acSTobias Grosser   return ret;
787336734acSTobias Grosser }
788336734acSTobias Grosser 
789336734acSTobias Grosser Region*
790336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
791336734acSTobias Grosser   Region* ret = getRegionFor(BBs.back());
792336734acSTobias Grosser   BBs.pop_back();
793336734acSTobias Grosser 
794336734acSTobias Grosser   for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
795336734acSTobias Grosser        E = BBs.end(); I != E; ++I)
796336734acSTobias Grosser       ret = getCommonRegion(ret, getRegionFor(*I));
797336734acSTobias Grosser 
798336734acSTobias Grosser   return ret;
799336734acSTobias Grosser }
800336734acSTobias Grosser 
801fe92a938STobias Grosser void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
802fe92a938STobias Grosser {
803fe92a938STobias Grosser   Region *R = getRegionFor(OldBB);
8044b0986b6STobias Grosser 
805fe92a938STobias Grosser   setRegionFor(NewBB, R);
806fe92a938STobias Grosser 
8074b0986b6STobias Grosser   while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
808fe92a938STobias Grosser     R->replaceEntry(NewBB);
809fe92a938STobias Grosser     R = R->getParent();
810fe92a938STobias Grosser   }
811fe92a938STobias Grosser 
812fe92a938STobias Grosser   setRegionFor(OldBB, R);
813fe92a938STobias Grosser }
814fe92a938STobias Grosser 
815336734acSTobias Grosser char RegionInfo::ID = 0;
8168ac477ffSOwen Anderson INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
8178ac477ffSOwen Anderson                 "Detect single entry single exit regions", true, true)
8188ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominatorTree)
8198ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
8208ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
8218ac477ffSOwen Anderson INITIALIZE_PASS_END(RegionInfo, "regions",
822df7a4f25SOwen Anderson                 "Detect single entry single exit regions", true, true)
823336734acSTobias Grosser 
824336734acSTobias Grosser // Create methods available outside of this file, to use them
825336734acSTobias Grosser // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
826336734acSTobias Grosser // the link time optimization.
827336734acSTobias Grosser 
828336734acSTobias Grosser namespace llvm {
829336734acSTobias Grosser   FunctionPass *createRegionInfoPass() {
830336734acSTobias Grosser     return new RegionInfo();
831336734acSTobias Grosser   }
832336734acSTobias Grosser }
833336734acSTobias Grosser 
834