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