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