RegionInfo: Add helpers to replace entry/exit recursively
[oota-llvm.git] / lib / Analysis / RegionInfo.cpp
1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Detects single entry single exit regions in the control flow graph.
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Analysis/RegionInfo.h"
13 #include "llvm/ADT/PostOrderIterator.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/RegionIterator.h"
17 #include "llvm/Assembly/Writer.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ErrorHandling.h"
20
21 #define DEBUG_TYPE "region"
22 #include "llvm/Support/Debug.h"
23
24 #include <set>
25 #include <algorithm>
26
27 using namespace llvm;
28
29 // Always verify if expensive checking is enabled.
30 #ifdef XDEBUG
31 static bool VerifyRegionInfo = true;
32 #else
33 static bool VerifyRegionInfo = false;
34 #endif
35
36 static cl::opt<bool,true>
37 VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
38                 cl::desc("Verify region info (time consuming)"));
39
40 STATISTIC(numRegions,       "The # of regions");
41 STATISTIC(numSimpleRegions, "The # of simple regions");
42
43 static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
44   cl::Hidden,
45   cl::desc("style of printing regions"),
46   cl::values(
47     clEnumValN(Region::PrintNone, "none",  "print no details"),
48     clEnumValN(Region::PrintBB, "bb",
49                "print regions in detail with block_iterator"),
50     clEnumValN(Region::PrintRN, "rn",
51                "print regions in detail with element_iterator"),
52     clEnumValEnd));
53 //===----------------------------------------------------------------------===//
54 /// Region Implementation
55 Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
56                DominatorTree *dt, Region *Parent)
57                : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
58
59 Region::~Region() {
60   // Free the cached nodes.
61   for (BBNodeMapT::iterator it = BBNodeMap.begin(),
62          ie = BBNodeMap.end(); it != ie; ++it)
63     delete it->second;
64
65   // Only clean the cache for this Region. Caches of child Regions will be
66   // cleaned when the child Regions are deleted.
67   BBNodeMap.clear();
68
69   for (iterator I = begin(), E = end(); I != E; ++I)
70     delete *I;
71 }
72
73 void Region::replaceEntry(BasicBlock *BB) {
74   entry.setPointer(BB);
75 }
76
77 void Region::replaceExit(BasicBlock *BB) {
78   assert(exit && "No exit to replace!");
79   exit = BB;
80 }
81
82 void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
83   std::vector<Region *> RegionQueue;
84   BasicBlock *OldEntry = getEntry();
85
86   RegionQueue.push_back(this);
87   while (!RegionQueue.empty()) {
88     Region *R = RegionQueue.back();
89     RegionQueue.pop_back();
90
91     R->replaceEntry(NewEntry);
92     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
93       if ((*RI)->getEntry() == OldEntry)
94         RegionQueue.push_back(*RI);
95   }
96 }
97
98 void Region::replaceExitRecursive(BasicBlock *NewExit) {
99   std::vector<Region *> RegionQueue;
100   BasicBlock *OldExit = getExit();
101
102   RegionQueue.push_back(this);
103   while (!RegionQueue.empty()) {
104     Region *R = RegionQueue.back();
105     RegionQueue.pop_back();
106
107     R->replaceExit(NewExit);
108     for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
109       if ((*RI)->getExit() == OldExit)
110         RegionQueue.push_back(*RI);
111   }
112 }
113
114 bool Region::contains(const BasicBlock *B) const {
115   BasicBlock *BB = const_cast<BasicBlock*>(B);
116
117   assert(DT->getNode(BB) && "BB not part of the dominance tree");
118
119   BasicBlock *entry = getEntry(), *exit = getExit();
120
121   // Toplevel region.
122   if (!exit)
123     return true;
124
125   return (DT->dominates(entry, BB)
126     && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
127 }
128
129 bool Region::contains(const Loop *L) const {
130   // BBs that are not part of any loop are element of the Loop
131   // described by the NULL pointer. This loop is not part of any region,
132   // except if the region describes the whole function.
133   if (L == 0)
134     return getExit() == 0;
135
136   if (!contains(L->getHeader()))
137     return false;
138
139   SmallVector<BasicBlock *, 8> ExitingBlocks;
140   L->getExitingBlocks(ExitingBlocks);
141
142   for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
143        BE = ExitingBlocks.end(); BI != BE; ++BI)
144     if (!contains(*BI))
145       return false;
146
147   return true;
148 }
149
150 Loop *Region::outermostLoopInRegion(Loop *L) const {
151   if (!contains(L))
152     return 0;
153
154   while (L && contains(L->getParentLoop())) {
155     L = L->getParentLoop();
156   }
157
158   return L;
159 }
160
161 Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
162   assert(LI && BB && "LI and BB cannot be null!");
163   Loop *L = LI->getLoopFor(BB);
164   return outermostLoopInRegion(L);
165 }
166
167 BasicBlock *Region::getEnteringBlock() const {
168   BasicBlock *entry = getEntry();
169   BasicBlock *Pred;
170   BasicBlock *enteringBlock = 0;
171
172   for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
173        ++PI) {
174     Pred = *PI;
175     if (DT->getNode(Pred) && !contains(Pred)) {
176       if (enteringBlock)
177         return 0;
178
179       enteringBlock = Pred;
180     }
181   }
182
183   return enteringBlock;
184 }
185
186 BasicBlock *Region::getExitingBlock() const {
187   BasicBlock *exit = getExit();
188   BasicBlock *Pred;
189   BasicBlock *exitingBlock = 0;
190
191   if (!exit)
192     return 0;
193
194   for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
195        ++PI) {
196     Pred = *PI;
197     if (contains(Pred)) {
198       if (exitingBlock)
199         return 0;
200
201       exitingBlock = Pred;
202     }
203   }
204
205   return exitingBlock;
206 }
207
208 bool Region::isSimple() const {
209   return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
210 }
211
212 std::string Region::getNameStr() const {
213   std::string exitName;
214   std::string entryName;
215
216   if (getEntry()->getName().empty()) {
217     raw_string_ostream OS(entryName);
218
219     WriteAsOperand(OS, getEntry(), false);
220   } else
221     entryName = getEntry()->getName();
222
223   if (getExit()) {
224     if (getExit()->getName().empty()) {
225       raw_string_ostream OS(exitName);
226
227       WriteAsOperand(OS, getExit(), false);
228     } else
229       exitName = getExit()->getName();
230   } else
231     exitName = "<Function Return>";
232
233   return entryName + " => " + exitName;
234 }
235
236 void Region::verifyBBInRegion(BasicBlock *BB) const {
237   if (!contains(BB))
238     llvm_unreachable("Broken region found!");
239
240   BasicBlock *entry = getEntry(), *exit = getExit();
241
242   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
243     if (!contains(*SI) && exit != *SI)
244       llvm_unreachable("Broken region found!");
245
246   if (entry != BB)
247     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
248       if (!contains(*SI))
249         llvm_unreachable("Broken region found!");
250 }
251
252 void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
253   BasicBlock *exit = getExit();
254
255   visited->insert(BB);
256
257   verifyBBInRegion(BB);
258
259   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
260     if (*SI != exit && visited->find(*SI) == visited->end())
261         verifyWalk(*SI, visited);
262 }
263
264 void Region::verifyRegion() const {
265   // Only do verification when user wants to, otherwise this expensive
266   // check will be invoked by PassManager.
267   if (!VerifyRegionInfo) return;
268
269   std::set<BasicBlock*> visited;
270   verifyWalk(getEntry(), &visited);
271 }
272
273 void Region::verifyRegionNest() const {
274   for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
275     (*RI)->verifyRegionNest();
276
277   verifyRegion();
278 }
279
280 Region::element_iterator Region::element_begin() {
281   return GraphTraits<Region*>::nodes_begin(this);
282 }
283
284 Region::element_iterator Region::element_end() {
285   return GraphTraits<Region*>::nodes_end(this);
286 }
287
288 Region::const_element_iterator Region::element_begin() const {
289   return GraphTraits<const Region*>::nodes_begin(this);
290 }
291
292 Region::const_element_iterator Region::element_end() const {
293   return GraphTraits<const Region*>::nodes_end(this);
294 }
295
296 Region* Region::getSubRegionNode(BasicBlock *BB) const {
297   Region *R = RI->getRegionFor(BB);
298
299   if (!R || R == this)
300     return 0;
301
302   // If we pass the BB out of this region, that means our code is broken.
303   assert(contains(R) && "BB not in current region!");
304
305   while (contains(R->getParent()) && R->getParent() != this)
306     R = R->getParent();
307
308   if (R->getEntry() != BB)
309     return 0;
310
311   return R;
312 }
313
314 RegionNode* Region::getBBNode(BasicBlock *BB) const {
315   assert(contains(BB) && "Can get BB node out of this region!");
316
317   BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
318
319   if (at != BBNodeMap.end())
320     return at->second;
321
322   RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
323   BBNodeMap.insert(std::make_pair(BB, NewNode));
324   return NewNode;
325 }
326
327 RegionNode* Region::getNode(BasicBlock *BB) const {
328   assert(contains(BB) && "Can get BB node out of this region!");
329   if (Region* Child = getSubRegionNode(BB))
330     return Child->getNode();
331
332   return getBBNode(BB);
333 }
334
335 void Region::transferChildrenTo(Region *To) {
336   for (iterator I = begin(), E = end(); I != E; ++I) {
337     (*I)->parent = To;
338     To->children.push_back(*I);
339   }
340   children.clear();
341 }
342
343 void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
344   assert(SubRegion->parent == 0 && "SubRegion already has a parent!");
345   assert(std::find(begin(), end(), SubRegion) == children.end()
346          && "Subregion already exists!");
347
348   SubRegion->parent = this;
349   children.push_back(SubRegion);
350
351   if (!moveChildren)
352     return;
353
354   assert(SubRegion->children.size() == 0
355          && "SubRegions that contain children are not supported");
356
357   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
358     if (!(*I)->isSubRegion()) {
359       BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
360
361       if (SubRegion->contains(BB))
362         RI->setRegionFor(BB, SubRegion);
363     }
364
365   std::vector<Region*> Keep;
366   for (iterator I = begin(), E = end(); I != E; ++I)
367     if (SubRegion->contains(*I) && *I != SubRegion) {
368       SubRegion->children.push_back(*I);
369       (*I)->parent = SubRegion;
370     } else
371       Keep.push_back(*I);
372
373   children.clear();
374   children.insert(children.begin(), Keep.begin(), Keep.end());
375 }
376
377
378 Region *Region::removeSubRegion(Region *Child) {
379   assert(Child->parent == this && "Child is not a child of this region!");
380   Child->parent = 0;
381   RegionSet::iterator I = std::find(children.begin(), children.end(), Child);
382   assert(I != children.end() && "Region does not exit. Unable to remove.");
383   children.erase(children.begin()+(I-begin()));
384   return Child;
385 }
386
387 unsigned Region::getDepth() const {
388   unsigned Depth = 0;
389
390   for (Region *R = parent; R != 0; R = R->parent)
391     ++Depth;
392
393   return Depth;
394 }
395
396 Region *Region::getExpandedRegion() const {
397   unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
398
399   if (NumSuccessors == 0)
400     return NULL;
401
402   for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
403        PI != PE; ++PI)
404     if (!DT->dominates(getEntry(), *PI))
405       return NULL;
406
407   Region *R = RI->getRegionFor(exit);
408
409   if (R->getEntry() != exit) {
410     if (exit->getTerminator()->getNumSuccessors() == 1)
411       return new Region(getEntry(), *succ_begin(exit), RI, DT);
412     else
413       return NULL;
414   }
415
416   while (R->getParent() && R->getParent()->getEntry() == exit)
417     R = R->getParent();
418
419   if (!DT->dominates(getEntry(), R->getExit()))
420     for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
421          PI != PE; ++PI)
422     if (!DT->dominates(R->getExit(), *PI))
423       return NULL;
424
425   return new Region(getEntry(), R->getExit(), RI, DT);
426 }
427
428 void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
429                    enum PrintStyle Style) const {
430   if (print_tree)
431     OS.indent(level*2) << "[" << level << "] " << getNameStr();
432   else
433     OS.indent(level*2) << getNameStr();
434
435   OS << "\n";
436
437
438   if (Style != PrintNone) {
439     OS.indent(level*2) << "{\n";
440     OS.indent(level*2 + 2);
441
442     if (Style == PrintBB) {
443       for (const_block_iterator I = block_begin(), E = block_end(); I != E; ++I)
444         OS << (*I)->getName() << ", "; // TODO: remove the last ","
445     } else if (Style == PrintRN) {
446       for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
447         OS << **I << ", "; // TODO: remove the last ",
448     }
449
450     OS << "\n";
451   }
452
453   if (print_tree)
454     for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
455       (*RI)->print(OS, print_tree, level+1, Style);
456
457   if (Style != PrintNone)
458     OS.indent(level*2) << "} \n";
459 }
460
461 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
462 void Region::dump() const {
463   print(dbgs(), true, getDepth(), printStyle.getValue());
464 }
465 #endif
466
467 void Region::clearNodeCache() {
468   // Free the cached nodes.
469   for (BBNodeMapT::iterator I = BBNodeMap.begin(),
470        IE = BBNodeMap.end(); I != IE; ++I)
471     delete I->second;
472
473   BBNodeMap.clear();
474   for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
475     (*RI)->clearNodeCache();
476 }
477
478 //===----------------------------------------------------------------------===//
479 // RegionInfo implementation
480 //
481
482 bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
483                                      BasicBlock *exit) const {
484   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
485     BasicBlock *P = *PI;
486     if (DT->dominates(entry, P) && !DT->dominates(exit, P))
487       return false;
488   }
489   return true;
490 }
491
492 bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
493   assert(entry && exit && "entry and exit must not be null!");
494   typedef DominanceFrontier::DomSetType DST;
495
496   DST *entrySuccs = &DF->find(entry)->second;
497
498   // Exit is the header of a loop that contains the entry. In this case,
499   // the dominance frontier must only contain the exit.
500   if (!DT->dominates(entry, exit)) {
501     for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
502          SI != SE; ++SI)
503       if (*SI != exit && *SI != entry)
504         return false;
505
506     return true;
507   }
508
509   DST *exitSuccs = &DF->find(exit)->second;
510
511   // Do not allow edges leaving the region.
512   for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
513        SI != SE; ++SI) {
514     if (*SI == exit || *SI == entry)
515       continue;
516     if (exitSuccs->find(*SI) == exitSuccs->end())
517       return false;
518     if (!isCommonDomFrontier(*SI, entry, exit))
519       return false;
520   }
521
522   // Do not allow edges pointing into the region.
523   for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
524        SI != SE; ++SI)
525     if (DT->properlyDominates(entry, *SI) && *SI != exit)
526       return false;
527
528
529   return true;
530 }
531
532 void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
533                              BBtoBBMap *ShortCut) const {
534   assert(entry && exit && "entry and exit must not be null!");
535
536   BBtoBBMap::iterator e = ShortCut->find(exit);
537
538   if (e == ShortCut->end())
539     // No further region at exit available.
540     (*ShortCut)[entry] = exit;
541   else {
542     // We found a region e that starts at exit. Therefore (entry, e->second)
543     // is also a region, that is larger than (entry, exit). Insert the
544     // larger one.
545     BasicBlock *BB = e->second;
546     (*ShortCut)[entry] = BB;
547   }
548 }
549
550 DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
551                                         BBtoBBMap *ShortCut) const {
552   BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
553
554   if (e == ShortCut->end())
555     return N->getIDom();
556
557   return PDT->getNode(e->second)->getIDom();
558 }
559
560 bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
561   assert(entry && exit && "entry and exit must not be null!");
562
563   unsigned num_successors = succ_end(entry) - succ_begin(entry);
564
565   if (num_successors <= 1 && exit == *(succ_begin(entry)))
566     return true;
567
568   return false;
569 }
570
571 void RegionInfo::updateStatistics(Region *R) {
572   ++numRegions;
573
574   // TODO: Slow. Should only be enabled if -stats is used.
575   if (R->isSimple()) ++numSimpleRegions;
576 }
577
578 Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
579   assert(entry && exit && "entry and exit must not be null!");
580
581   if (isTrivialRegion(entry, exit))
582     return 0;
583
584   Region *region = new Region(entry, exit, this, DT);
585   BBtoRegion.insert(std::make_pair(entry, region));
586
587  #ifdef XDEBUG
588     region->verifyRegion();
589  #else
590     DEBUG(region->verifyRegion());
591  #endif
592
593   updateStatistics(region);
594   return region;
595 }
596
597 void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
598   assert(entry);
599
600   DomTreeNode *N = PDT->getNode(entry);
601
602   if (!N)
603     return;
604
605   Region *lastRegion= 0;
606   BasicBlock *lastExit = entry;
607
608   // As only a BasicBlock that postdominates entry can finish a region, walk the
609   // post dominance tree upwards.
610   while ((N = getNextPostDom(N, ShortCut))) {
611     BasicBlock *exit = N->getBlock();
612
613     if (!exit)
614       break;
615
616     if (isRegion(entry, exit)) {
617       Region *newRegion = createRegion(entry, exit);
618
619       if (lastRegion)
620         newRegion->addSubRegion(lastRegion);
621
622       lastRegion = newRegion;
623       lastExit = exit;
624     }
625
626     // This can never be a region, so stop the search.
627     if (!DT->dominates(entry, exit))
628       break;
629   }
630
631   // Tried to create regions from entry to lastExit.  Next time take a
632   // shortcut from entry to lastExit.
633   if (lastExit != entry)
634     insertShortCut(entry, lastExit, ShortCut);
635 }
636
637 void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
638   BasicBlock *entry = &(F.getEntryBlock());
639   DomTreeNode *N = DT->getNode(entry);
640
641   // Iterate over the dominance tree in post order to start with the small
642   // regions from the bottom of the dominance tree.  If the small regions are
643   // detected first, detection of bigger regions is faster, as we can jump
644   // over the small regions.
645   for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
646     ++FI) {
647     findRegionsWithEntry(FI->getBlock(), ShortCut);
648   }
649 }
650
651 Region *RegionInfo::getTopMostParent(Region *region) {
652   while (region->parent)
653     region = region->getParent();
654
655   return region;
656 }
657
658 void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
659   BasicBlock *BB = N->getBlock();
660
661   // Passed region exit
662   while (BB == region->getExit())
663     region = region->getParent();
664
665   BBtoRegionMap::iterator it = BBtoRegion.find(BB);
666
667   // This basic block is a start block of a region. It is already in the
668   // BBtoRegion relation. Only the child basic blocks have to be updated.
669   if (it != BBtoRegion.end()) {
670     Region *newRegion = it->second;
671     region->addSubRegion(getTopMostParent(newRegion));
672     region = newRegion;
673   } else {
674     BBtoRegion[BB] = region;
675   }
676
677   for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
678     buildRegionsTree(*CI, region);
679 }
680
681 void RegionInfo::releaseMemory() {
682   BBtoRegion.clear();
683   if (TopLevelRegion)
684     delete TopLevelRegion;
685   TopLevelRegion = 0;
686 }
687
688 RegionInfo::RegionInfo() : FunctionPass(ID) {
689   initializeRegionInfoPass(*PassRegistry::getPassRegistry());
690   TopLevelRegion = 0;
691 }
692
693 RegionInfo::~RegionInfo() {
694   releaseMemory();
695 }
696
697 void RegionInfo::Calculate(Function &F) {
698   // ShortCut a function where for every BB the exit of the largest region
699   // starting with BB is stored. These regions can be threated as single BBS.
700   // This improves performance on linear CFGs.
701   BBtoBBMap ShortCut;
702
703   scanForRegions(F, &ShortCut);
704   BasicBlock *BB = &F.getEntryBlock();
705   buildRegionsTree(DT->getNode(BB), TopLevelRegion);
706 }
707
708 bool RegionInfo::runOnFunction(Function &F) {
709   releaseMemory();
710
711   DT = &getAnalysis<DominatorTree>();
712   PDT = &getAnalysis<PostDominatorTree>();
713   DF = &getAnalysis<DominanceFrontier>();
714
715   TopLevelRegion = new Region(&F.getEntryBlock(), 0, this, DT, 0);
716   updateStatistics(TopLevelRegion);
717
718   Calculate(F);
719
720   return false;
721 }
722
723 void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
724   AU.setPreservesAll();
725   AU.addRequiredTransitive<DominatorTree>();
726   AU.addRequired<PostDominatorTree>();
727   AU.addRequired<DominanceFrontier>();
728 }
729
730 void RegionInfo::print(raw_ostream &OS, const Module *) const {
731   OS << "Region tree:\n";
732   TopLevelRegion->print(OS, true, 0, printStyle.getValue());
733   OS << "End region tree\n";
734 }
735
736 void RegionInfo::verifyAnalysis() const {
737   // Only do verification when user wants to, otherwise this expensive check
738   // will be invoked by PMDataManager::verifyPreservedAnalysis when
739   // a regionpass (marked PreservedAll) finish.
740   if (!VerifyRegionInfo) return;
741
742   TopLevelRegion->verifyRegionNest();
743 }
744
745 // Region pass manager support.
746 Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
747   BBtoRegionMap::const_iterator I=
748     BBtoRegion.find(BB);
749   return I != BBtoRegion.end() ? I->second : 0;
750 }
751
752 void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
753   BBtoRegion[BB] = R;
754 }
755
756 Region *RegionInfo::operator[](BasicBlock *BB) const {
757   return getRegionFor(BB);
758 }
759
760 BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
761   BasicBlock *Exit = NULL;
762
763   while (true) {
764     // Get largest region that starts at BB.
765     Region *R = getRegionFor(BB);
766     while (R && R->getParent() && R->getParent()->getEntry() == BB)
767       R = R->getParent();
768
769     // Get the single exit of BB.
770     if (R && R->getEntry() == BB)
771       Exit = R->getExit();
772     else if (++succ_begin(BB) == succ_end(BB))
773       Exit = *succ_begin(BB);
774     else // No single exit exists.
775       return Exit;
776
777     // Get largest region that starts at Exit.
778     Region *ExitR = getRegionFor(Exit);
779     while (ExitR && ExitR->getParent()
780            && ExitR->getParent()->getEntry() == Exit)
781       ExitR = ExitR->getParent();
782
783     for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
784          ++PI)
785       if (!R->contains(*PI) && !ExitR->contains(*PI))
786         break;
787
788     // This stops infinite cycles.
789     if (DT->dominates(Exit, BB))
790       break;
791
792     BB = Exit;
793   }
794
795   return Exit;
796 }
797
798 Region*
799 RegionInfo::getCommonRegion(Region *A, Region *B) const {
800   assert (A && B && "One of the Regions is NULL");
801
802   if (A->contains(B)) return A;
803
804   while (!B->contains(A))
805     B = B->getParent();
806
807   return B;
808 }
809
810 Region*
811 RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
812   Region* ret = Regions.back();
813   Regions.pop_back();
814
815   for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
816        E = Regions.end(); I != E; ++I)
817       ret = getCommonRegion(ret, *I);
818
819   return ret;
820 }
821
822 Region*
823 RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
824   Region* ret = getRegionFor(BBs.back());
825   BBs.pop_back();
826
827   for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
828        E = BBs.end(); I != E; ++I)
829       ret = getCommonRegion(ret, getRegionFor(*I));
830
831   return ret;
832 }
833
834 void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
835 {
836   Region *R = getRegionFor(OldBB);
837
838   setRegionFor(NewBB, R);
839
840   while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
841     R->replaceEntry(NewBB);
842     R = R->getParent();
843   }
844
845   setRegionFor(OldBB, R);
846 }
847
848 char RegionInfo::ID = 0;
849 INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
850                 "Detect single entry single exit regions", true, true)
851 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
852 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
853 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
854 INITIALIZE_PASS_END(RegionInfo, "regions",
855                 "Detect single entry single exit regions", true, true)
856
857 // Create methods available outside of this file, to use them
858 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
859 // the link time optimization.
860
861 namespace llvm {
862   FunctionPass *createRegionInfoPass() {
863     return new RegionInfo();
864   }
865 }
866