1336734acSTobias Grosser //===- RegionInfo.cpp - SESE region detection analysis --------------------===// 2336734acSTobias Grosser // 3336734acSTobias Grosser // The LLVM Compiler Infrastructure 4336734acSTobias Grosser // 5336734acSTobias Grosser // This file is distributed under the University of Illinois Open Source 6336734acSTobias Grosser // License. See LICENSE.TXT for details. 7336734acSTobias Grosser // 8336734acSTobias Grosser //===----------------------------------------------------------------------===// 9336734acSTobias Grosser // Detects single entry single exit regions in the control flow graph. 10336734acSTobias Grosser //===----------------------------------------------------------------------===// 11336734acSTobias Grosser 12570d3020SBill Wendling #define DEBUG_TYPE "region" 13336734acSTobias Grosser #include "llvm/Analysis/RegionInfo.h" 14336734acSTobias Grosser #include "llvm/ADT/PostOrderIterator.h" 15336734acSTobias Grosser #include "llvm/ADT/Statistic.h" 16ed0881b2SChandler Carruth #include "llvm/Analysis/LoopInfo.h" 17ed0881b2SChandler Carruth #include "llvm/Analysis/RegionIterator.h" 18336734acSTobias Grosser #include "llvm/Support/CommandLine.h" 19336734acSTobias Grosser #include "llvm/Support/Debug.h" 208a8cd2baSChandler Carruth #include "llvm/Support/ErrorHandling.h" 21336734acSTobias Grosser #include <algorithm> 22*ec649acbSDavid Blaikie #include <iterator> 23570d3020SBill Wendling #include <set> 24336734acSTobias Grosser 25336734acSTobias Grosser using namespace llvm; 26336734acSTobias Grosser 27336734acSTobias Grosser // Always verify if expensive checking is enabled. 28336734acSTobias Grosser #ifdef XDEBUG 29abfafadfSDan Gohman static bool VerifyRegionInfo = true; 30336734acSTobias Grosser #else 31abfafadfSDan Gohman static bool VerifyRegionInfo = false; 32336734acSTobias Grosser #endif 33336734acSTobias Grosser 34336734acSTobias Grosser static cl::opt<bool,true> 35336734acSTobias Grosser VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo), 36336734acSTobias Grosser cl::desc("Verify region info (time consuming)")); 37336734acSTobias Grosser 38336734acSTobias Grosser STATISTIC(numRegions, "The # of regions"); 39336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions"); 40336734acSTobias Grosser 418b304ff9STobias Grosser static cl::opt<enum Region::PrintStyle> printStyle("print-region-style", 428b304ff9STobias Grosser cl::Hidden, 43336734acSTobias Grosser cl::desc("style of printing regions"), 44336734acSTobias Grosser cl::values( 458b304ff9STobias Grosser clEnumValN(Region::PrintNone, "none", "print no details"), 468b304ff9STobias Grosser clEnumValN(Region::PrintBB, "bb", 4714c05c40SHongbin Zheng "print regions in detail with block_iterator"), 488b304ff9STobias Grosser clEnumValN(Region::PrintRN, "rn", 498b304ff9STobias Grosser "print regions in detail with element_iterator"), 50336734acSTobias Grosser clEnumValEnd)); 51336734acSTobias Grosser //===----------------------------------------------------------------------===// 52336734acSTobias Grosser /// Region Implementation 53336734acSTobias Grosser Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo, 54336734acSTobias Grosser DominatorTree *dt, Region *Parent) 55336734acSTobias Grosser : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {} 56336734acSTobias Grosser 57336734acSTobias Grosser Region::~Region() { 5818e39cecSDaniel Dunbar // Free the cached nodes. 5918e39cecSDaniel Dunbar for (BBNodeMapT::iterator it = BBNodeMap.begin(), 6018e39cecSDaniel Dunbar ie = BBNodeMap.end(); it != ie; ++it) 6118e39cecSDaniel Dunbar delete it->second; 6218e39cecSDaniel Dunbar 63336734acSTobias Grosser // Only clean the cache for this Region. Caches of child Regions will be 64336734acSTobias Grosser // cleaned when the child Regions are deleted. 65336734acSTobias Grosser BBNodeMap.clear(); 66336734acSTobias Grosser } 67336734acSTobias Grosser 68648594c9STobias Grosser void Region::replaceEntry(BasicBlock *BB) { 69648594c9STobias Grosser entry.setPointer(BB); 70648594c9STobias Grosser } 71648594c9STobias Grosser 72648594c9STobias Grosser void Region::replaceExit(BasicBlock *BB) { 73648594c9STobias Grosser assert(exit && "No exit to replace!"); 74648594c9STobias Grosser exit = BB; 75648594c9STobias Grosser } 76648594c9STobias Grosser 77141cc3e8STobias Grosser void Region::replaceEntryRecursive(BasicBlock *NewEntry) { 78141cc3e8STobias Grosser std::vector<Region *> RegionQueue; 79141cc3e8STobias Grosser BasicBlock *OldEntry = getEntry(); 80141cc3e8STobias Grosser 81141cc3e8STobias Grosser RegionQueue.push_back(this); 82141cc3e8STobias Grosser while (!RegionQueue.empty()) { 83141cc3e8STobias Grosser Region *R = RegionQueue.back(); 84141cc3e8STobias Grosser RegionQueue.pop_back(); 85141cc3e8STobias Grosser 86141cc3e8STobias Grosser R->replaceEntry(NewEntry); 87141cc3e8STobias Grosser for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) 88141cc3e8STobias Grosser if ((*RI)->getEntry() == OldEntry) 89*ec649acbSDavid Blaikie RegionQueue.push_back(RI->get()); 90141cc3e8STobias Grosser } 91141cc3e8STobias Grosser } 92141cc3e8STobias Grosser 93141cc3e8STobias Grosser void Region::replaceExitRecursive(BasicBlock *NewExit) { 94141cc3e8STobias Grosser std::vector<Region *> RegionQueue; 95141cc3e8STobias Grosser BasicBlock *OldExit = getExit(); 96141cc3e8STobias Grosser 97141cc3e8STobias Grosser RegionQueue.push_back(this); 98141cc3e8STobias Grosser while (!RegionQueue.empty()) { 99141cc3e8STobias Grosser Region *R = RegionQueue.back(); 100141cc3e8STobias Grosser RegionQueue.pop_back(); 101141cc3e8STobias Grosser 102141cc3e8STobias Grosser R->replaceExit(NewExit); 103141cc3e8STobias Grosser for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI) 104141cc3e8STobias Grosser if ((*RI)->getExit() == OldExit) 105*ec649acbSDavid Blaikie RegionQueue.push_back(RI->get()); 106141cc3e8STobias Grosser } 107141cc3e8STobias Grosser } 108141cc3e8STobias Grosser 109336734acSTobias Grosser bool Region::contains(const BasicBlock *B) const { 110336734acSTobias Grosser BasicBlock *BB = const_cast<BasicBlock*>(B); 111336734acSTobias Grosser 112a7ddc982STobias Grosser if (!DT->getNode(BB)) 113a7ddc982STobias Grosser return false; 114336734acSTobias Grosser 115336734acSTobias Grosser BasicBlock *entry = getEntry(), *exit = getExit(); 116336734acSTobias Grosser 117336734acSTobias Grosser // Toplevel region. 118336734acSTobias Grosser if (!exit) 119336734acSTobias Grosser return true; 120336734acSTobias Grosser 121336734acSTobias Grosser return (DT->dominates(entry, BB) 122336734acSTobias Grosser && !(DT->dominates(exit, BB) && DT->dominates(entry, exit))); 123336734acSTobias Grosser } 124336734acSTobias Grosser 1251bec81a8STobias Grosser bool Region::contains(const Loop *L) const { 1261bec81a8STobias Grosser // BBs that are not part of any loop are element of the Loop 1271bec81a8STobias Grosser // described by the NULL pointer. This loop is not part of any region, 1281bec81a8STobias Grosser // except if the region describes the whole function. 1299f008867SCraig Topper if (!L) 1309f008867SCraig Topper return getExit() == nullptr; 1311bec81a8STobias Grosser 1321bec81a8STobias Grosser if (!contains(L->getHeader())) 1331bec81a8STobias Grosser return false; 1341bec81a8STobias Grosser 1351bec81a8STobias Grosser SmallVector<BasicBlock *, 8> ExitingBlocks; 1361bec81a8STobias Grosser L->getExitingBlocks(ExitingBlocks); 1371bec81a8STobias Grosser 1381bec81a8STobias Grosser for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(), 1391bec81a8STobias Grosser BE = ExitingBlocks.end(); BI != BE; ++BI) 1401bec81a8STobias Grosser if (!contains(*BI)) 1411bec81a8STobias Grosser return false; 1421bec81a8STobias Grosser 1431bec81a8STobias Grosser return true; 1441bec81a8STobias Grosser } 1451bec81a8STobias Grosser 1461bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(Loop *L) const { 1471bec81a8STobias Grosser if (!contains(L)) 1489f008867SCraig Topper return nullptr; 1491bec81a8STobias Grosser 1501bec81a8STobias Grosser while (L && contains(L->getParentLoop())) { 1511bec81a8STobias Grosser L = L->getParentLoop(); 1521bec81a8STobias Grosser } 1531bec81a8STobias Grosser 1541bec81a8STobias Grosser return L; 1551bec81a8STobias Grosser } 1561bec81a8STobias Grosser 1571bec81a8STobias Grosser Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const { 1581bec81a8STobias Grosser assert(LI && BB && "LI and BB cannot be null!"); 1591bec81a8STobias Grosser Loop *L = LI->getLoopFor(BB); 1601bec81a8STobias Grosser return outermostLoopInRegion(L); 1611bec81a8STobias Grosser } 1621bec81a8STobias Grosser 163b1d11c19STobias Grosser BasicBlock *Region::getEnteringBlock() const { 164b1d11c19STobias Grosser BasicBlock *entry = getEntry(); 165b1d11c19STobias Grosser BasicBlock *Pred; 1669f008867SCraig Topper BasicBlock *enteringBlock = nullptr; 167336734acSTobias Grosser 168336734acSTobias Grosser for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE; 1697fbe6cb4STobias Grosser ++PI) { 170b1d11c19STobias Grosser Pred = *PI; 1717fbe6cb4STobias Grosser if (DT->getNode(Pred) && !contains(Pred)) { 172b1d11c19STobias Grosser if (enteringBlock) 1739f008867SCraig Topper return nullptr; 174b1d11c19STobias Grosser 175b1d11c19STobias Grosser enteringBlock = Pred; 176336734acSTobias Grosser } 1777fbe6cb4STobias Grosser } 178336734acSTobias Grosser 179b1d11c19STobias Grosser return enteringBlock; 180b1d11c19STobias Grosser } 181b1d11c19STobias Grosser 182b1d11c19STobias Grosser BasicBlock *Region::getExitingBlock() const { 183b1d11c19STobias Grosser BasicBlock *exit = getExit(); 184b1d11c19STobias Grosser BasicBlock *Pred; 1859f008867SCraig Topper BasicBlock *exitingBlock = nullptr; 186b1d11c19STobias Grosser 187b1d11c19STobias Grosser if (!exit) 1889f008867SCraig Topper return nullptr; 189336734acSTobias Grosser 190336734acSTobias Grosser for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE; 191b1d11c19STobias Grosser ++PI) { 192b1d11c19STobias Grosser Pred = *PI; 193b1d11c19STobias Grosser if (contains(Pred)) { 194b1d11c19STobias Grosser if (exitingBlock) 1959f008867SCraig Topper return nullptr; 196b1d11c19STobias Grosser 197b1d11c19STobias Grosser exitingBlock = Pred; 198336734acSTobias Grosser } 199336734acSTobias Grosser } 200336734acSTobias Grosser 201b1d11c19STobias Grosser return exitingBlock; 202b1d11c19STobias Grosser } 203b1d11c19STobias Grosser 204b1d11c19STobias Grosser bool Region::isSimple() const { 205b1d11c19STobias Grosser return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock(); 206336734acSTobias Grosser } 207336734acSTobias Grosser 2081bec81a8STobias Grosser std::string Region::getNameStr() const { 2091bec81a8STobias Grosser std::string exitName; 2101bec81a8STobias Grosser std::string entryName; 2111bec81a8STobias Grosser 2121bec81a8STobias Grosser if (getEntry()->getName().empty()) { 2131bec81a8STobias Grosser raw_string_ostream OS(entryName); 2141bec81a8STobias Grosser 215d48cdbf0SChandler Carruth getEntry()->printAsOperand(OS, false); 2161bec81a8STobias Grosser } else 217184e3ceeSBenjamin Kramer entryName = getEntry()->getName(); 2181bec81a8STobias Grosser 2191bec81a8STobias Grosser if (getExit()) { 2201bec81a8STobias Grosser if (getExit()->getName().empty()) { 2211bec81a8STobias Grosser raw_string_ostream OS(exitName); 2221bec81a8STobias Grosser 223d48cdbf0SChandler Carruth getExit()->printAsOperand(OS, false); 2241bec81a8STobias Grosser } else 225184e3ceeSBenjamin Kramer exitName = getExit()->getName(); 2261bec81a8STobias Grosser } else 2271bec81a8STobias Grosser exitName = "<Function Return>"; 2281bec81a8STobias Grosser 2291bec81a8STobias Grosser return entryName + " => " + exitName; 2301bec81a8STobias Grosser } 2311bec81a8STobias Grosser 232336734acSTobias Grosser void Region::verifyBBInRegion(BasicBlock *BB) const { 233336734acSTobias Grosser if (!contains(BB)) 234336734acSTobias Grosser llvm_unreachable("Broken region found!"); 235336734acSTobias Grosser 236336734acSTobias Grosser BasicBlock *entry = getEntry(), *exit = getExit(); 237336734acSTobias Grosser 238336734acSTobias Grosser for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) 239336734acSTobias Grosser if (!contains(*SI) && exit != *SI) 240336734acSTobias Grosser llvm_unreachable("Broken region found!"); 241336734acSTobias Grosser 242336734acSTobias Grosser if (entry != BB) 243336734acSTobias Grosser for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI) 244336734acSTobias Grosser if (!contains(*SI)) 245336734acSTobias Grosser llvm_unreachable("Broken region found!"); 246336734acSTobias Grosser } 247336734acSTobias Grosser 248336734acSTobias Grosser void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const { 249336734acSTobias Grosser BasicBlock *exit = getExit(); 250336734acSTobias Grosser 251336734acSTobias Grosser visited->insert(BB); 252336734acSTobias Grosser 253336734acSTobias Grosser verifyBBInRegion(BB); 254336734acSTobias Grosser 255336734acSTobias Grosser for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) 256336734acSTobias Grosser if (*SI != exit && visited->find(*SI) == visited->end()) 257336734acSTobias Grosser verifyWalk(*SI, visited); 258336734acSTobias Grosser } 259336734acSTobias Grosser 260336734acSTobias Grosser void Region::verifyRegion() const { 261336734acSTobias Grosser // Only do verification when user wants to, otherwise this expensive 262336734acSTobias Grosser // check will be invoked by PassManager. 263336734acSTobias Grosser if (!VerifyRegionInfo) return; 264336734acSTobias Grosser 265336734acSTobias Grosser std::set<BasicBlock*> visited; 266336734acSTobias Grosser verifyWalk(getEntry(), &visited); 267336734acSTobias Grosser } 268336734acSTobias Grosser 269336734acSTobias Grosser void Region::verifyRegionNest() const { 270336734acSTobias Grosser for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI) 271336734acSTobias Grosser (*RI)->verifyRegionNest(); 272336734acSTobias Grosser 273336734acSTobias Grosser verifyRegion(); 274336734acSTobias Grosser } 275336734acSTobias Grosser 276336734acSTobias Grosser Region::element_iterator Region::element_begin() { 277336734acSTobias Grosser return GraphTraits<Region*>::nodes_begin(this); 278336734acSTobias Grosser } 279336734acSTobias Grosser 280336734acSTobias Grosser Region::element_iterator Region::element_end() { 281336734acSTobias Grosser return GraphTraits<Region*>::nodes_end(this); 282336734acSTobias Grosser } 283336734acSTobias Grosser 284336734acSTobias Grosser Region::const_element_iterator Region::element_begin() const { 285336734acSTobias Grosser return GraphTraits<const Region*>::nodes_begin(this); 286336734acSTobias Grosser } 287336734acSTobias Grosser 288336734acSTobias Grosser Region::const_element_iterator Region::element_end() const { 289336734acSTobias Grosser return GraphTraits<const Region*>::nodes_end(this); 290336734acSTobias Grosser } 291336734acSTobias Grosser 292336734acSTobias Grosser Region* Region::getSubRegionNode(BasicBlock *BB) const { 293336734acSTobias Grosser Region *R = RI->getRegionFor(BB); 294336734acSTobias Grosser 295336734acSTobias Grosser if (!R || R == this) 2969f008867SCraig Topper return nullptr; 297336734acSTobias Grosser 298336734acSTobias Grosser // If we pass the BB out of this region, that means our code is broken. 299336734acSTobias Grosser assert(contains(R) && "BB not in current region!"); 300336734acSTobias Grosser 301336734acSTobias Grosser while (contains(R->getParent()) && R->getParent() != this) 302336734acSTobias Grosser R = R->getParent(); 303336734acSTobias Grosser 304336734acSTobias Grosser if (R->getEntry() != BB) 3059f008867SCraig Topper return nullptr; 306336734acSTobias Grosser 307336734acSTobias Grosser return R; 308336734acSTobias Grosser } 309336734acSTobias Grosser 310336734acSTobias Grosser RegionNode* Region::getBBNode(BasicBlock *BB) const { 311336734acSTobias Grosser assert(contains(BB) && "Can get BB node out of this region!"); 312336734acSTobias Grosser 313336734acSTobias Grosser BBNodeMapT::const_iterator at = BBNodeMap.find(BB); 314336734acSTobias Grosser 315336734acSTobias Grosser if (at != BBNodeMap.end()) 316336734acSTobias Grosser return at->second; 317336734acSTobias Grosser 318336734acSTobias Grosser RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB); 319336734acSTobias Grosser BBNodeMap.insert(std::make_pair(BB, NewNode)); 320336734acSTobias Grosser return NewNode; 321336734acSTobias Grosser } 322336734acSTobias Grosser 323336734acSTobias Grosser RegionNode* Region::getNode(BasicBlock *BB) const { 324336734acSTobias Grosser assert(contains(BB) && "Can get BB node out of this region!"); 325336734acSTobias Grosser if (Region* Child = getSubRegionNode(BB)) 326336734acSTobias Grosser return Child->getNode(); 327336734acSTobias Grosser 328336734acSTobias Grosser return getBBNode(BB); 329336734acSTobias Grosser } 330336734acSTobias Grosser 331336734acSTobias Grosser void Region::transferChildrenTo(Region *To) { 332336734acSTobias Grosser for (iterator I = begin(), E = end(); I != E; ++I) { 333336734acSTobias Grosser (*I)->parent = To; 334*ec649acbSDavid Blaikie To->children.push_back(std::move(*I)); 335336734acSTobias Grosser } 336336734acSTobias Grosser children.clear(); 337336734acSTobias Grosser } 338336734acSTobias Grosser 339bf984fd7STobias Grosser void Region::addSubRegion(Region *SubRegion, bool moveChildren) { 3409f008867SCraig Topper assert(!SubRegion->parent && "SubRegion already has a parent!"); 341*ec649acbSDavid Blaikie assert(std::find_if(begin(), end(), [&](const std::unique_ptr<Region> &R) { 342*ec649acbSDavid Blaikie return R.get() == SubRegion; 343*ec649acbSDavid Blaikie }) == children.end() && 344*ec649acbSDavid Blaikie "Subregion already exists!"); 345bf984fd7STobias Grosser 346336734acSTobias Grosser SubRegion->parent = this; 347*ec649acbSDavid Blaikie children.push_back(std::unique_ptr<Region>(SubRegion)); 348bf984fd7STobias Grosser 349bf984fd7STobias Grosser if (!moveChildren) 350bf984fd7STobias Grosser return; 351bf984fd7STobias Grosser 352bf984fd7STobias Grosser assert(SubRegion->children.size() == 0 353bf984fd7STobias Grosser && "SubRegions that contain children are not supported"); 354bf984fd7STobias Grosser 355bf984fd7STobias Grosser for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) 356bf984fd7STobias Grosser if (!(*I)->isSubRegion()) { 357bf984fd7STobias Grosser BasicBlock *BB = (*I)->getNodeAs<BasicBlock>(); 358bf984fd7STobias Grosser 359bf984fd7STobias Grosser if (SubRegion->contains(BB)) 360bf984fd7STobias Grosser RI->setRegionFor(BB, SubRegion); 361bf984fd7STobias Grosser } 362bf984fd7STobias Grosser 363*ec649acbSDavid Blaikie std::vector<std::unique_ptr<Region>> Keep; 364bf984fd7STobias Grosser for (iterator I = begin(), E = end(); I != E; ++I) 365*ec649acbSDavid Blaikie if (SubRegion->contains(I->get()) && I->get() != SubRegion) { 366*ec649acbSDavid Blaikie SubRegion->children.push_back(std::move(*I)); 367bf984fd7STobias Grosser (*I)->parent = SubRegion; 368bf984fd7STobias Grosser } else 369*ec649acbSDavid Blaikie Keep.push_back(std::move(*I)); 370bf984fd7STobias Grosser 371bf984fd7STobias Grosser children.clear(); 372*ec649acbSDavid Blaikie children.insert(children.begin(), 373*ec649acbSDavid Blaikie std::move_iterator<RegionSet::iterator>(Keep.begin()), 374*ec649acbSDavid Blaikie std::move_iterator<RegionSet::iterator>(Keep.end())); 375336734acSTobias Grosser } 376336734acSTobias Grosser 377336734acSTobias Grosser 378336734acSTobias Grosser Region *Region::removeSubRegion(Region *Child) { 379336734acSTobias Grosser assert(Child->parent == this && "Child is not a child of this region!"); 3809f008867SCraig Topper Child->parent = nullptr; 381*ec649acbSDavid Blaikie RegionSet::iterator I = std::find_if( 382*ec649acbSDavid Blaikie children.begin(), children.end(), 383*ec649acbSDavid Blaikie [&](const std::unique_ptr<Region> &R) { return R.get() == Child; }); 384336734acSTobias Grosser assert(I != children.end() && "Region does not exit. Unable to remove."); 385336734acSTobias Grosser children.erase(children.begin()+(I-begin())); 386336734acSTobias Grosser return Child; 387336734acSTobias Grosser } 388336734acSTobias Grosser 389336734acSTobias Grosser unsigned Region::getDepth() const { 390336734acSTobias Grosser unsigned Depth = 0; 391336734acSTobias Grosser 3929f008867SCraig Topper for (Region *R = parent; R != nullptr; R = R->parent) 393336734acSTobias Grosser ++Depth; 394336734acSTobias Grosser 395336734acSTobias Grosser return Depth; 396336734acSTobias Grosser } 397336734acSTobias Grosser 398a8677226STobias Grosser Region *Region::getExpandedRegion() const { 399a8677226STobias Grosser unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors(); 400a8677226STobias Grosser 401a8677226STobias Grosser if (NumSuccessors == 0) 4029f008867SCraig Topper return nullptr; 403a8677226STobias Grosser 404a8677226STobias Grosser for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit()); 405a8677226STobias Grosser PI != PE; ++PI) 406a8677226STobias Grosser if (!DT->dominates(getEntry(), *PI)) 4079f008867SCraig Topper return nullptr; 408a8677226STobias Grosser 409a8677226STobias Grosser Region *R = RI->getRegionFor(exit); 410a8677226STobias Grosser 411a8677226STobias Grosser if (R->getEntry() != exit) { 412a8677226STobias Grosser if (exit->getTerminator()->getNumSuccessors() == 1) 413a8677226STobias Grosser return new Region(getEntry(), *succ_begin(exit), RI, DT); 414a8677226STobias Grosser else 4159f008867SCraig Topper return nullptr; 416a8677226STobias Grosser } 417a8677226STobias Grosser 418a8677226STobias Grosser while (R->getParent() && R->getParent()->getEntry() == exit) 419a8677226STobias Grosser R = R->getParent(); 420a8677226STobias Grosser 421a8677226STobias Grosser if (!DT->dominates(getEntry(), R->getExit())) 422a8677226STobias Grosser for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit()); 423a8677226STobias Grosser PI != PE; ++PI) 424a8677226STobias Grosser if (!DT->dominates(R->getExit(), *PI)) 4259f008867SCraig Topper return nullptr; 426a8677226STobias Grosser 427a8677226STobias Grosser return new Region(getEntry(), R->getExit(), RI, DT); 428a8677226STobias Grosser } 429a8677226STobias Grosser 4308b304ff9STobias Grosser void Region::print(raw_ostream &OS, bool print_tree, unsigned level, 4318b304ff9STobias Grosser enum PrintStyle Style) const { 432336734acSTobias Grosser if (print_tree) 433336734acSTobias Grosser OS.indent(level*2) << "[" << level << "] " << getNameStr(); 434336734acSTobias Grosser else 435336734acSTobias Grosser OS.indent(level*2) << getNameStr(); 436336734acSTobias Grosser 437336734acSTobias Grosser OS << "\n"; 438336734acSTobias Grosser 439336734acSTobias Grosser 4408b304ff9STobias Grosser if (Style != PrintNone) { 441336734acSTobias Grosser OS.indent(level*2) << "{\n"; 442336734acSTobias Grosser OS.indent(level*2 + 2); 443336734acSTobias Grosser 4448b304ff9STobias Grosser if (Style == PrintBB) { 4454abf9d3aSTobias Grosser for (const auto &BB : blocks()) 4464abf9d3aSTobias Grosser OS << BB->getName() << ", "; // TODO: remove the last "," 4478b304ff9STobias Grosser } else if (Style == PrintRN) { 448336734acSTobias Grosser for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I) 449336734acSTobias Grosser OS << **I << ", "; // TODO: remove the last ", 450336734acSTobias Grosser } 451336734acSTobias Grosser 452336734acSTobias Grosser OS << "\n"; 453336734acSTobias Grosser } 454336734acSTobias Grosser 455336734acSTobias Grosser if (print_tree) 456336734acSTobias Grosser for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI) 4578b304ff9STobias Grosser (*RI)->print(OS, print_tree, level+1, Style); 458336734acSTobias Grosser 4598b304ff9STobias Grosser if (Style != PrintNone) 460336734acSTobias Grosser OS.indent(level*2) << "} \n"; 461336734acSTobias Grosser } 462336734acSTobias Grosser 46349d684e1SManman Ren #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 464336734acSTobias Grosser void Region::dump() const { 4658b304ff9STobias Grosser print(dbgs(), true, getDepth(), printStyle.getValue()); 466336734acSTobias Grosser } 467c3366cceSManman Ren #endif 468336734acSTobias Grosser 469336734acSTobias Grosser void Region::clearNodeCache() { 470e910b9d9STobias Grosser // Free the cached nodes. 471e910b9d9STobias Grosser for (BBNodeMapT::iterator I = BBNodeMap.begin(), 4724c71c117STobias Grosser IE = BBNodeMap.end(); I != IE; ++I) 473e910b9d9STobias Grosser delete I->second; 474e910b9d9STobias Grosser 475336734acSTobias Grosser BBNodeMap.clear(); 476336734acSTobias Grosser for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI) 477336734acSTobias Grosser (*RI)->clearNodeCache(); 478336734acSTobias Grosser } 479336734acSTobias Grosser 480336734acSTobias Grosser //===----------------------------------------------------------------------===// 481336734acSTobias Grosser // RegionInfo implementation 482336734acSTobias Grosser // 483336734acSTobias Grosser 484336734acSTobias Grosser bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry, 485336734acSTobias Grosser BasicBlock *exit) const { 48607c8ad54SGabor Greif for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { 48707c8ad54SGabor Greif BasicBlock *P = *PI; 48807c8ad54SGabor Greif if (DT->dominates(entry, P) && !DT->dominates(exit, P)) 489336734acSTobias Grosser return false; 49007c8ad54SGabor Greif } 491336734acSTobias Grosser return true; 492336734acSTobias Grosser } 493336734acSTobias Grosser 494336734acSTobias Grosser bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const { 495336734acSTobias Grosser assert(entry && exit && "entry and exit must not be null!"); 496336734acSTobias Grosser typedef DominanceFrontier::DomSetType DST; 497336734acSTobias Grosser 498d9f48ecbSGabor Greif DST *entrySuccs = &DF->find(entry)->second; 499336734acSTobias Grosser 500336734acSTobias Grosser // Exit is the header of a loop that contains the entry. In this case, 501336734acSTobias Grosser // the dominance frontier must only contain the exit. 502336734acSTobias Grosser if (!DT->dominates(entry, exit)) { 503336734acSTobias Grosser for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end(); 504336734acSTobias Grosser SI != SE; ++SI) 505336734acSTobias Grosser if (*SI != exit && *SI != entry) 506336734acSTobias Grosser return false; 507336734acSTobias Grosser 508336734acSTobias Grosser return true; 509336734acSTobias Grosser } 510336734acSTobias Grosser 511d9f48ecbSGabor Greif DST *exitSuccs = &DF->find(exit)->second; 512336734acSTobias Grosser 513336734acSTobias Grosser // Do not allow edges leaving the region. 514336734acSTobias Grosser for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end(); 515336734acSTobias Grosser SI != SE; ++SI) { 516336734acSTobias Grosser if (*SI == exit || *SI == entry) 517336734acSTobias Grosser continue; 518336734acSTobias Grosser if (exitSuccs->find(*SI) == exitSuccs->end()) 519336734acSTobias Grosser return false; 520336734acSTobias Grosser if (!isCommonDomFrontier(*SI, entry, exit)) 521336734acSTobias Grosser return false; 522336734acSTobias Grosser } 523336734acSTobias Grosser 524336734acSTobias Grosser // Do not allow edges pointing into the region. 525336734acSTobias Grosser for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end(); 526336734acSTobias Grosser SI != SE; ++SI) 527b3aa6c71SDan Gohman if (DT->properlyDominates(entry, *SI) && *SI != exit) 528336734acSTobias Grosser return false; 529336734acSTobias Grosser 530336734acSTobias Grosser 531336734acSTobias Grosser return true; 532336734acSTobias Grosser } 533336734acSTobias Grosser 534336734acSTobias Grosser void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit, 535336734acSTobias Grosser BBtoBBMap *ShortCut) const { 536336734acSTobias Grosser assert(entry && exit && "entry and exit must not be null!"); 537336734acSTobias Grosser 538336734acSTobias Grosser BBtoBBMap::iterator e = ShortCut->find(exit); 539336734acSTobias Grosser 540336734acSTobias Grosser if (e == ShortCut->end()) 541336734acSTobias Grosser // No further region at exit available. 542336734acSTobias Grosser (*ShortCut)[entry] = exit; 543336734acSTobias Grosser else { 544336734acSTobias Grosser // We found a region e that starts at exit. Therefore (entry, e->second) 545336734acSTobias Grosser // is also a region, that is larger than (entry, exit). Insert the 546336734acSTobias Grosser // larger one. 547336734acSTobias Grosser BasicBlock *BB = e->second; 548336734acSTobias Grosser (*ShortCut)[entry] = BB; 549336734acSTobias Grosser } 550336734acSTobias Grosser } 551336734acSTobias Grosser 552336734acSTobias Grosser DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N, 553336734acSTobias Grosser BBtoBBMap *ShortCut) const { 554336734acSTobias Grosser BBtoBBMap::iterator e = ShortCut->find(N->getBlock()); 555336734acSTobias Grosser 556336734acSTobias Grosser if (e == ShortCut->end()) 557336734acSTobias Grosser return N->getIDom(); 558336734acSTobias Grosser 559336734acSTobias Grosser return PDT->getNode(e->second)->getIDom(); 560336734acSTobias Grosser } 561336734acSTobias Grosser 562336734acSTobias Grosser bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const { 563336734acSTobias Grosser assert(entry && exit && "entry and exit must not be null!"); 564336734acSTobias Grosser 565336734acSTobias Grosser unsigned num_successors = succ_end(entry) - succ_begin(entry); 566336734acSTobias Grosser 567336734acSTobias Grosser if (num_successors <= 1 && exit == *(succ_begin(entry))) 568336734acSTobias Grosser return true; 569336734acSTobias Grosser 570336734acSTobias Grosser return false; 571336734acSTobias Grosser } 572336734acSTobias Grosser 573336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) { 574336734acSTobias Grosser ++numRegions; 575336734acSTobias Grosser 576336734acSTobias Grosser // TODO: Slow. Should only be enabled if -stats is used. 577336734acSTobias Grosser if (R->isSimple()) ++numSimpleRegions; 578336734acSTobias Grosser } 579336734acSTobias Grosser 580336734acSTobias Grosser Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) { 581336734acSTobias Grosser assert(entry && exit && "entry and exit must not be null!"); 582336734acSTobias Grosser 583336734acSTobias Grosser if (isTrivialRegion(entry, exit)) 5849f008867SCraig Topper return nullptr; 585336734acSTobias Grosser 586336734acSTobias Grosser Region *region = new Region(entry, exit, this, DT); 587336734acSTobias Grosser BBtoRegion.insert(std::make_pair(entry, region)); 588336734acSTobias Grosser 589336734acSTobias Grosser #ifdef XDEBUG 590336734acSTobias Grosser region->verifyRegion(); 591336734acSTobias Grosser #else 592336734acSTobias Grosser DEBUG(region->verifyRegion()); 593336734acSTobias Grosser #endif 594336734acSTobias Grosser 595336734acSTobias Grosser updateStatistics(region); 596336734acSTobias Grosser return region; 597336734acSTobias Grosser } 598336734acSTobias Grosser 599336734acSTobias Grosser void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) { 600336734acSTobias Grosser assert(entry); 601336734acSTobias Grosser 602336734acSTobias Grosser DomTreeNode *N = PDT->getNode(entry); 603336734acSTobias Grosser 604336734acSTobias Grosser if (!N) 605336734acSTobias Grosser return; 606336734acSTobias Grosser 6079f008867SCraig Topper Region *lastRegion= nullptr; 608336734acSTobias Grosser BasicBlock *lastExit = entry; 609336734acSTobias Grosser 610336734acSTobias Grosser // As only a BasicBlock that postdominates entry can finish a region, walk the 611336734acSTobias Grosser // post dominance tree upwards. 612336734acSTobias Grosser while ((N = getNextPostDom(N, ShortCut))) { 613336734acSTobias Grosser BasicBlock *exit = N->getBlock(); 614336734acSTobias Grosser 615336734acSTobias Grosser if (!exit) 616336734acSTobias Grosser break; 617336734acSTobias Grosser 618336734acSTobias Grosser if (isRegion(entry, exit)) { 619336734acSTobias Grosser Region *newRegion = createRegion(entry, exit); 620336734acSTobias Grosser 621336734acSTobias Grosser if (lastRegion) 622336734acSTobias Grosser newRegion->addSubRegion(lastRegion); 623336734acSTobias Grosser 624336734acSTobias Grosser lastRegion = newRegion; 625336734acSTobias Grosser lastExit = exit; 626336734acSTobias Grosser } 627336734acSTobias Grosser 628336734acSTobias Grosser // This can never be a region, so stop the search. 629336734acSTobias Grosser if (!DT->dominates(entry, exit)) 630336734acSTobias Grosser break; 631336734acSTobias Grosser } 632336734acSTobias Grosser 633336734acSTobias Grosser // Tried to create regions from entry to lastExit. Next time take a 634336734acSTobias Grosser // shortcut from entry to lastExit. 635336734acSTobias Grosser if (lastExit != entry) 636336734acSTobias Grosser insertShortCut(entry, lastExit, ShortCut); 637336734acSTobias Grosser } 638336734acSTobias Grosser 639336734acSTobias Grosser void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) { 640336734acSTobias Grosser BasicBlock *entry = &(F.getEntryBlock()); 641336734acSTobias Grosser DomTreeNode *N = DT->getNode(entry); 642336734acSTobias Grosser 643336734acSTobias Grosser // Iterate over the dominance tree in post order to start with the small 644336734acSTobias Grosser // regions from the bottom of the dominance tree. If the small regions are 645336734acSTobias Grosser // detected first, detection of bigger regions is faster, as we can jump 646336734acSTobias Grosser // over the small regions. 647336734acSTobias Grosser for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE; 648336734acSTobias Grosser ++FI) { 6491a2da423SGabor Greif findRegionsWithEntry(FI->getBlock(), ShortCut); 650336734acSTobias Grosser } 651336734acSTobias Grosser } 652336734acSTobias Grosser 653336734acSTobias Grosser Region *RegionInfo::getTopMostParent(Region *region) { 654336734acSTobias Grosser while (region->parent) 655336734acSTobias Grosser region = region->getParent(); 656336734acSTobias Grosser 657336734acSTobias Grosser return region; 658336734acSTobias Grosser } 659336734acSTobias Grosser 660336734acSTobias Grosser void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) { 661336734acSTobias Grosser BasicBlock *BB = N->getBlock(); 662336734acSTobias Grosser 663336734acSTobias Grosser // Passed region exit 664336734acSTobias Grosser while (BB == region->getExit()) 665336734acSTobias Grosser region = region->getParent(); 666336734acSTobias Grosser 667336734acSTobias Grosser BBtoRegionMap::iterator it = BBtoRegion.find(BB); 668336734acSTobias Grosser 669336734acSTobias Grosser // This basic block is a start block of a region. It is already in the 670336734acSTobias Grosser // BBtoRegion relation. Only the child basic blocks have to be updated. 671336734acSTobias Grosser if (it != BBtoRegion.end()) { 6725dfe6dabSChad Rosier Region *newRegion = it->second; 673336734acSTobias Grosser region->addSubRegion(getTopMostParent(newRegion)); 674336734acSTobias Grosser region = newRegion; 675336734acSTobias Grosser } else { 676336734acSTobias Grosser BBtoRegion[BB] = region; 677336734acSTobias Grosser } 678336734acSTobias Grosser 679336734acSTobias Grosser for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI) 680336734acSTobias Grosser buildRegionsTree(*CI, region); 681336734acSTobias Grosser } 682336734acSTobias Grosser 683336734acSTobias Grosser void RegionInfo::releaseMemory() { 684336734acSTobias Grosser BBtoRegion.clear(); 685336734acSTobias Grosser if (TopLevelRegion) 686336734acSTobias Grosser delete TopLevelRegion; 6879f008867SCraig Topper TopLevelRegion = nullptr; 688336734acSTobias Grosser } 689336734acSTobias Grosser 690a7aed186SOwen Anderson RegionInfo::RegionInfo() : FunctionPass(ID) { 6916c18d1aaSOwen Anderson initializeRegionInfoPass(*PassRegistry::getPassRegistry()); 6929f008867SCraig Topper TopLevelRegion = nullptr; 693336734acSTobias Grosser } 694336734acSTobias Grosser 695336734acSTobias Grosser RegionInfo::~RegionInfo() { 696336734acSTobias Grosser releaseMemory(); 697336734acSTobias Grosser } 698336734acSTobias Grosser 699336734acSTobias Grosser void RegionInfo::Calculate(Function &F) { 700336734acSTobias Grosser // ShortCut a function where for every BB the exit of the largest region 701336734acSTobias Grosser // starting with BB is stored. These regions can be threated as single BBS. 702336734acSTobias Grosser // This improves performance on linear CFGs. 703336734acSTobias Grosser BBtoBBMap ShortCut; 704336734acSTobias Grosser 705336734acSTobias Grosser scanForRegions(F, &ShortCut); 706336734acSTobias Grosser BasicBlock *BB = &F.getEntryBlock(); 707336734acSTobias Grosser buildRegionsTree(DT->getNode(BB), TopLevelRegion); 708336734acSTobias Grosser } 709336734acSTobias Grosser 710336734acSTobias Grosser bool RegionInfo::runOnFunction(Function &F) { 711336734acSTobias Grosser releaseMemory(); 712336734acSTobias Grosser 71373523021SChandler Carruth DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 714336734acSTobias Grosser PDT = &getAnalysis<PostDominatorTree>(); 715336734acSTobias Grosser DF = &getAnalysis<DominanceFrontier>(); 716336734acSTobias Grosser 7179f008867SCraig Topper TopLevelRegion = new Region(&F.getEntryBlock(), nullptr, this, DT, nullptr); 718336734acSTobias Grosser updateStatistics(TopLevelRegion); 719336734acSTobias Grosser 720336734acSTobias Grosser Calculate(F); 721336734acSTobias Grosser 722336734acSTobias Grosser return false; 723336734acSTobias Grosser } 724336734acSTobias Grosser 725336734acSTobias Grosser void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const { 726336734acSTobias Grosser AU.setPreservesAll(); 72773523021SChandler Carruth AU.addRequiredTransitive<DominatorTreeWrapperPass>(); 728336734acSTobias Grosser AU.addRequired<PostDominatorTree>(); 729336734acSTobias Grosser AU.addRequired<DominanceFrontier>(); 730336734acSTobias Grosser } 731336734acSTobias Grosser 732336734acSTobias Grosser void RegionInfo::print(raw_ostream &OS, const Module *) const { 733336734acSTobias Grosser OS << "Region tree:\n"; 7348b304ff9STobias Grosser TopLevelRegion->print(OS, true, 0, printStyle.getValue()); 735336734acSTobias Grosser OS << "End region tree\n"; 736336734acSTobias Grosser } 737336734acSTobias Grosser 738336734acSTobias Grosser void RegionInfo::verifyAnalysis() const { 739336734acSTobias Grosser // Only do verification when user wants to, otherwise this expensive check 740336734acSTobias Grosser // will be invoked by PMDataManager::verifyPreservedAnalysis when 741336734acSTobias Grosser // a regionpass (marked PreservedAll) finish. 742336734acSTobias Grosser if (!VerifyRegionInfo) return; 743336734acSTobias Grosser 744336734acSTobias Grosser TopLevelRegion->verifyRegionNest(); 745336734acSTobias Grosser } 746336734acSTobias Grosser 747336734acSTobias Grosser // Region pass manager support. 748336734acSTobias Grosser Region *RegionInfo::getRegionFor(BasicBlock *BB) const { 749336734acSTobias Grosser BBtoRegionMap::const_iterator I= 750336734acSTobias Grosser BBtoRegion.find(BB); 7519f008867SCraig Topper return I != BBtoRegion.end() ? I->second : nullptr; 752336734acSTobias Grosser } 753336734acSTobias Grosser 7548352ce5fSTobias Grosser void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) { 7558352ce5fSTobias Grosser BBtoRegion[BB] = R; 7568352ce5fSTobias Grosser } 7578352ce5fSTobias Grosser 758336734acSTobias Grosser Region *RegionInfo::operator[](BasicBlock *BB) const { 759336734acSTobias Grosser return getRegionFor(BB); 760336734acSTobias Grosser } 761336734acSTobias Grosser 762fc763867STobias Grosser BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const { 7639f008867SCraig Topper BasicBlock *Exit = nullptr; 764fc763867STobias Grosser 765fc763867STobias Grosser while (true) { 766fc763867STobias Grosser // Get largest region that starts at BB. 767fc763867STobias Grosser Region *R = getRegionFor(BB); 768fc763867STobias Grosser while (R && R->getParent() && R->getParent()->getEntry() == BB) 769fc763867STobias Grosser R = R->getParent(); 770fc763867STobias Grosser 771fc763867STobias Grosser // Get the single exit of BB. 772fc763867STobias Grosser if (R && R->getEntry() == BB) 773fc763867STobias Grosser Exit = R->getExit(); 774fc763867STobias Grosser else if (++succ_begin(BB) == succ_end(BB)) 775fc763867STobias Grosser Exit = *succ_begin(BB); 776fc763867STobias Grosser else // No single exit exists. 777fc763867STobias Grosser return Exit; 778fc763867STobias Grosser 779fc763867STobias Grosser // Get largest region that starts at Exit. 780fc763867STobias Grosser Region *ExitR = getRegionFor(Exit); 781fc763867STobias Grosser while (ExitR && ExitR->getParent() 782fc763867STobias Grosser && ExitR->getParent()->getEntry() == Exit) 783fc763867STobias Grosser ExitR = ExitR->getParent(); 784fc763867STobias Grosser 785fc763867STobias Grosser for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE; 786fc763867STobias Grosser ++PI) 787fc763867STobias Grosser if (!R->contains(*PI) && !ExitR->contains(*PI)) 788fc763867STobias Grosser break; 789fc763867STobias Grosser 790fc763867STobias Grosser // This stops infinite cycles. 791fc763867STobias Grosser if (DT->dominates(Exit, BB)) 792fc763867STobias Grosser break; 793fc763867STobias Grosser 794fc763867STobias Grosser BB = Exit; 795fc763867STobias Grosser } 796fc763867STobias Grosser 797fc763867STobias Grosser return Exit; 798fc763867STobias Grosser } 799fc763867STobias Grosser 800336734acSTobias Grosser Region* 801336734acSTobias Grosser RegionInfo::getCommonRegion(Region *A, Region *B) const { 802336734acSTobias Grosser assert (A && B && "One of the Regions is NULL"); 803336734acSTobias Grosser 804336734acSTobias Grosser if (A->contains(B)) return A; 805336734acSTobias Grosser 806336734acSTobias Grosser while (!B->contains(A)) 807336734acSTobias Grosser B = B->getParent(); 808336734acSTobias Grosser 809336734acSTobias Grosser return B; 810336734acSTobias Grosser } 811336734acSTobias Grosser 812336734acSTobias Grosser Region* 813336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const { 814336734acSTobias Grosser Region* ret = Regions.back(); 815336734acSTobias Grosser Regions.pop_back(); 816336734acSTobias Grosser 817336734acSTobias Grosser for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(), 818336734acSTobias Grosser E = Regions.end(); I != E; ++I) 819336734acSTobias Grosser ret = getCommonRegion(ret, *I); 820336734acSTobias Grosser 821336734acSTobias Grosser return ret; 822336734acSTobias Grosser } 823336734acSTobias Grosser 824336734acSTobias Grosser Region* 825336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const { 826336734acSTobias Grosser Region* ret = getRegionFor(BBs.back()); 827336734acSTobias Grosser BBs.pop_back(); 828336734acSTobias Grosser 829336734acSTobias Grosser for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(), 830336734acSTobias Grosser E = BBs.end(); I != E; ++I) 831336734acSTobias Grosser ret = getCommonRegion(ret, getRegionFor(*I)); 832336734acSTobias Grosser 833336734acSTobias Grosser return ret; 834336734acSTobias Grosser } 835336734acSTobias Grosser 836fe92a938STobias Grosser void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB) 837fe92a938STobias Grosser { 838fe92a938STobias Grosser Region *R = getRegionFor(OldBB); 8394b0986b6STobias Grosser 840fe92a938STobias Grosser setRegionFor(NewBB, R); 841fe92a938STobias Grosser 8424b0986b6STobias Grosser while (R->getEntry() == OldBB && !R->isTopLevelRegion()) { 843fe92a938STobias Grosser R->replaceEntry(NewBB); 844fe92a938STobias Grosser R = R->getParent(); 845fe92a938STobias Grosser } 846fe92a938STobias Grosser 847fe92a938STobias Grosser setRegionFor(OldBB, R); 848fe92a938STobias Grosser } 849fe92a938STobias Grosser 850336734acSTobias Grosser char RegionInfo::ID = 0; 8518ac477ffSOwen Anderson INITIALIZE_PASS_BEGIN(RegionInfo, "regions", 8528ac477ffSOwen Anderson "Detect single entry single exit regions", true, true) 85373523021SChandler Carruth INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 8548ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(PostDominatorTree) 8558ac477ffSOwen Anderson INITIALIZE_PASS_DEPENDENCY(DominanceFrontier) 8568ac477ffSOwen Anderson INITIALIZE_PASS_END(RegionInfo, "regions", 857df7a4f25SOwen Anderson "Detect single entry single exit regions", true, true) 858336734acSTobias Grosser 859336734acSTobias Grosser // Create methods available outside of this file, to use them 860336734acSTobias Grosser // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by 861336734acSTobias Grosser // the link time optimization. 862336734acSTobias Grosser 863336734acSTobias Grosser namespace llvm { 864336734acSTobias Grosser FunctionPass *createRegionInfoPass() { 865336734acSTobias Grosser return new RegionInfo(); 866336734acSTobias Grosser } 867336734acSTobias Grosser } 868336734acSTobias Grosser 869