1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Detects single entry single exit regions in the control flow graph.
10 //===----------------------------------------------------------------------===//
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"
21 #define DEBUG_TYPE "region"
22 #include "llvm/Support/Debug.h"
29 // Always verify if expensive checking is enabled.
31 static bool VerifyRegionInfo = true;
33 static bool VerifyRegionInfo = false;
36 static cl::opt<bool,true>
37 VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
38 cl::desc("Verify region info (time consuming)"));
40 STATISTIC(numRegions, "The # of regions");
41 STATISTIC(numSimpleRegions, "The # of simple regions");
43 static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
45 cl::desc("style of printing regions"),
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"),
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) {}
60 // Free the cached nodes.
61 for (BBNodeMapT::iterator it = BBNodeMap.begin(),
62 ie = BBNodeMap.end(); it != ie; ++it)
65 // Only clean the cache for this Region. Caches of child Regions will be
66 // cleaned when the child Regions are deleted.
69 for (iterator I = begin(), E = end(); I != E; ++I)
73 void Region::replaceEntry(BasicBlock *BB) {
77 void Region::replaceExit(BasicBlock *BB) {
78 assert(exit && "No exit to replace!");
82 void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
83 std::vector<Region *> RegionQueue;
84 BasicBlock *OldEntry = getEntry();
86 RegionQueue.push_back(this);
87 while (!RegionQueue.empty()) {
88 Region *R = RegionQueue.back();
89 RegionQueue.pop_back();
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);
98 void Region::replaceExitRecursive(BasicBlock *NewExit) {
99 std::vector<Region *> RegionQueue;
100 BasicBlock *OldExit = getExit();
102 RegionQueue.push_back(this);
103 while (!RegionQueue.empty()) {
104 Region *R = RegionQueue.back();
105 RegionQueue.pop_back();
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);
114 bool Region::contains(const BasicBlock *B) const {
115 BasicBlock *BB = const_cast<BasicBlock*>(B);
117 assert(DT->getNode(BB) && "BB not part of the dominance tree");
119 BasicBlock *entry = getEntry(), *exit = getExit();
125 return (DT->dominates(entry, BB)
126 && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
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.
134 return getExit() == 0;
136 if (!contains(L->getHeader()))
139 SmallVector<BasicBlock *, 8> ExitingBlocks;
140 L->getExitingBlocks(ExitingBlocks);
142 for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
143 BE = ExitingBlocks.end(); BI != BE; ++BI)
150 Loop *Region::outermostLoopInRegion(Loop *L) const {
154 while (L && contains(L->getParentLoop())) {
155 L = L->getParentLoop();
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);
167 BasicBlock *Region::getEnteringBlock() const {
168 BasicBlock *entry = getEntry();
170 BasicBlock *enteringBlock = 0;
172 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
175 if (DT->getNode(Pred) && !contains(Pred)) {
179 enteringBlock = Pred;
183 return enteringBlock;
186 BasicBlock *Region::getExitingBlock() const {
187 BasicBlock *exit = getExit();
189 BasicBlock *exitingBlock = 0;
194 for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
197 if (contains(Pred)) {
208 bool Region::isSimple() const {
209 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
212 std::string Region::getNameStr() const {
213 std::string exitName;
214 std::string entryName;
216 if (getEntry()->getName().empty()) {
217 raw_string_ostream OS(entryName);
219 WriteAsOperand(OS, getEntry(), false);
221 entryName = getEntry()->getName();
224 if (getExit()->getName().empty()) {
225 raw_string_ostream OS(exitName);
227 WriteAsOperand(OS, getExit(), false);
229 exitName = getExit()->getName();
231 exitName = "<Function Return>";
233 return entryName + " => " + exitName;
236 void Region::verifyBBInRegion(BasicBlock *BB) const {
238 llvm_unreachable("Broken region found!");
240 BasicBlock *entry = getEntry(), *exit = getExit();
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!");
247 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
249 llvm_unreachable("Broken region found!");
252 void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
253 BasicBlock *exit = getExit();
257 verifyBBInRegion(BB);
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);
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;
269 std::set<BasicBlock*> visited;
270 verifyWalk(getEntry(), &visited);
273 void Region::verifyRegionNest() const {
274 for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
275 (*RI)->verifyRegionNest();
280 Region::element_iterator Region::element_begin() {
281 return GraphTraits<Region*>::nodes_begin(this);
284 Region::element_iterator Region::element_end() {
285 return GraphTraits<Region*>::nodes_end(this);
288 Region::const_element_iterator Region::element_begin() const {
289 return GraphTraits<const Region*>::nodes_begin(this);
292 Region::const_element_iterator Region::element_end() const {
293 return GraphTraits<const Region*>::nodes_end(this);
296 Region* Region::getSubRegionNode(BasicBlock *BB) const {
297 Region *R = RI->getRegionFor(BB);
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!");
305 while (contains(R->getParent()) && R->getParent() != this)
308 if (R->getEntry() != BB)
314 RegionNode* Region::getBBNode(BasicBlock *BB) const {
315 assert(contains(BB) && "Can get BB node out of this region!");
317 BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
319 if (at != BBNodeMap.end())
322 RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
323 BBNodeMap.insert(std::make_pair(BB, NewNode));
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();
332 return getBBNode(BB);
335 void Region::transferChildrenTo(Region *To) {
336 for (iterator I = begin(), E = end(); I != E; ++I) {
338 To->children.push_back(*I);
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!");
348 SubRegion->parent = this;
349 children.push_back(SubRegion);
354 assert(SubRegion->children.size() == 0
355 && "SubRegions that contain children are not supported");
357 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
358 if (!(*I)->isSubRegion()) {
359 BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
361 if (SubRegion->contains(BB))
362 RI->setRegionFor(BB, SubRegion);
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;
374 children.insert(children.begin(), Keep.begin(), Keep.end());
378 Region *Region::removeSubRegion(Region *Child) {
379 assert(Child->parent == this && "Child is not a child of this region!");
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()));
387 unsigned Region::getDepth() const {
390 for (Region *R = parent; R != 0; R = R->parent)
396 Region *Region::getExpandedRegion() const {
397 unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
399 if (NumSuccessors == 0)
402 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
404 if (!DT->dominates(getEntry(), *PI))
407 Region *R = RI->getRegionFor(exit);
409 if (R->getEntry() != exit) {
410 if (exit->getTerminator()->getNumSuccessors() == 1)
411 return new Region(getEntry(), *succ_begin(exit), RI, DT);
416 while (R->getParent() && R->getParent()->getEntry() == exit)
419 if (!DT->dominates(getEntry(), R->getExit()))
420 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
422 if (!DT->dominates(R->getExit(), *PI))
425 return new Region(getEntry(), R->getExit(), RI, DT);
428 void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
429 enum PrintStyle Style) const {
431 OS.indent(level*2) << "[" << level << "] " << getNameStr();
433 OS.indent(level*2) << getNameStr();
438 if (Style != PrintNone) {
439 OS.indent(level*2) << "{\n";
440 OS.indent(level*2 + 2);
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 ",
454 for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
455 (*RI)->print(OS, print_tree, level+1, Style);
457 if (Style != PrintNone)
458 OS.indent(level*2) << "} \n";
461 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
462 void Region::dump() const {
463 print(dbgs(), true, getDepth(), printStyle.getValue());
467 void Region::clearNodeCache() {
468 // Free the cached nodes.
469 for (BBNodeMapT::iterator I = BBNodeMap.begin(),
470 IE = BBNodeMap.end(); I != IE; ++I)
474 for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
475 (*RI)->clearNodeCache();
478 //===----------------------------------------------------------------------===//
479 // RegionInfo implementation
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) {
486 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
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;
496 DST *entrySuccs = &DF->find(entry)->second;
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();
503 if (*SI != exit && *SI != entry)
509 DST *exitSuccs = &DF->find(exit)->second;
511 // Do not allow edges leaving the region.
512 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
514 if (*SI == exit || *SI == entry)
516 if (exitSuccs->find(*SI) == exitSuccs->end())
518 if (!isCommonDomFrontier(*SI, entry, exit))
522 // Do not allow edges pointing into the region.
523 for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
525 if (DT->properlyDominates(entry, *SI) && *SI != exit)
532 void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
533 BBtoBBMap *ShortCut) const {
534 assert(entry && exit && "entry and exit must not be null!");
536 BBtoBBMap::iterator e = ShortCut->find(exit);
538 if (e == ShortCut->end())
539 // No further region at exit available.
540 (*ShortCut)[entry] = exit;
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
545 BasicBlock *BB = e->second;
546 (*ShortCut)[entry] = BB;
550 DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
551 BBtoBBMap *ShortCut) const {
552 BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
554 if (e == ShortCut->end())
557 return PDT->getNode(e->second)->getIDom();
560 bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
561 assert(entry && exit && "entry and exit must not be null!");
563 unsigned num_successors = succ_end(entry) - succ_begin(entry);
565 if (num_successors <= 1 && exit == *(succ_begin(entry)))
571 void RegionInfo::updateStatistics(Region *R) {
574 // TODO: Slow. Should only be enabled if -stats is used.
575 if (R->isSimple()) ++numSimpleRegions;
578 Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
579 assert(entry && exit && "entry and exit must not be null!");
581 if (isTrivialRegion(entry, exit))
584 Region *region = new Region(entry, exit, this, DT);
585 BBtoRegion.insert(std::make_pair(entry, region));
588 region->verifyRegion();
590 DEBUG(region->verifyRegion());
593 updateStatistics(region);
597 void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
600 DomTreeNode *N = PDT->getNode(entry);
605 Region *lastRegion= 0;
606 BasicBlock *lastExit = entry;
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();
616 if (isRegion(entry, exit)) {
617 Region *newRegion = createRegion(entry, exit);
620 newRegion->addSubRegion(lastRegion);
622 lastRegion = newRegion;
626 // This can never be a region, so stop the search.
627 if (!DT->dominates(entry, exit))
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);
637 void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
638 BasicBlock *entry = &(F.getEntryBlock());
639 DomTreeNode *N = DT->getNode(entry);
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;
647 findRegionsWithEntry(FI->getBlock(), ShortCut);
651 Region *RegionInfo::getTopMostParent(Region *region) {
652 while (region->parent)
653 region = region->getParent();
658 void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
659 BasicBlock *BB = N->getBlock();
661 // Passed region exit
662 while (BB == region->getExit())
663 region = region->getParent();
665 BBtoRegionMap::iterator it = BBtoRegion.find(BB);
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));
674 BBtoRegion[BB] = region;
677 for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
678 buildRegionsTree(*CI, region);
681 void RegionInfo::releaseMemory() {
684 delete TopLevelRegion;
688 RegionInfo::RegionInfo() : FunctionPass(ID) {
689 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
693 RegionInfo::~RegionInfo() {
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.
703 scanForRegions(F, &ShortCut);
704 BasicBlock *BB = &F.getEntryBlock();
705 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
708 bool RegionInfo::runOnFunction(Function &F) {
711 DT = &getAnalysis<DominatorTree>();
712 PDT = &getAnalysis<PostDominatorTree>();
713 DF = &getAnalysis<DominanceFrontier>();
715 TopLevelRegion = new Region(&F.getEntryBlock(), 0, this, DT, 0);
716 updateStatistics(TopLevelRegion);
723 void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
724 AU.setPreservesAll();
725 AU.addRequiredTransitive<DominatorTree>();
726 AU.addRequired<PostDominatorTree>();
727 AU.addRequired<DominanceFrontier>();
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";
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;
742 TopLevelRegion->verifyRegionNest();
745 // Region pass manager support.
746 Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
747 BBtoRegionMap::const_iterator I=
749 return I != BBtoRegion.end() ? I->second : 0;
752 void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
756 Region *RegionInfo::operator[](BasicBlock *BB) const {
757 return getRegionFor(BB);
760 BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
761 BasicBlock *Exit = NULL;
764 // Get largest region that starts at BB.
765 Region *R = getRegionFor(BB);
766 while (R && R->getParent() && R->getParent()->getEntry() == BB)
769 // Get the single exit of BB.
770 if (R && R->getEntry() == BB)
772 else if (++succ_begin(BB) == succ_end(BB))
773 Exit = *succ_begin(BB);
774 else // No single exit exists.
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();
783 for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
785 if (!R->contains(*PI) && !ExitR->contains(*PI))
788 // This stops infinite cycles.
789 if (DT->dominates(Exit, BB))
799 RegionInfo::getCommonRegion(Region *A, Region *B) const {
800 assert (A && B && "One of the Regions is NULL");
802 if (A->contains(B)) return A;
804 while (!B->contains(A))
811 RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
812 Region* ret = Regions.back();
815 for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
816 E = Regions.end(); I != E; ++I)
817 ret = getCommonRegion(ret, *I);
823 RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
824 Region* ret = getRegionFor(BBs.back());
827 for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
828 E = BBs.end(); I != E; ++I)
829 ret = getCommonRegion(ret, getRegionFor(*I));
834 void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
836 Region *R = getRegionFor(OldBB);
838 setRegionFor(NewBB, R);
840 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
841 R->replaceEntry(NewBB);
845 setRegionFor(OldBB, R);
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)
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.
862 FunctionPass *createRegionInfoPass() {
863 return new RegionInfo();