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