blob: 81ef6505e611cabecdb716b08150afc465eccce3 [file] [log] [blame]
Tobias Grosserf96b0062010-07-22 07:46:31 +00001//===- 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 Grosserf96b0062010-07-22 07:46:31 +000013#include "llvm/ADT/PostOrderIterator.h"
14#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Analysis/LoopInfo.h"
16#include "llvm/Analysis/RegionIterator.h"
17#include "llvm/Assembly/Writer.h"
Tobias Grosserf96b0062010-07-22 07:46:31 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/ErrorHandling.h"
Tobias Grosserf96b0062010-07-22 07:46:31 +000020
21#define DEBUG_TYPE "region"
22#include "llvm/Support/Debug.h"
23
24#include <set>
25#include <algorithm>
26
27using namespace llvm;
28
29// Always verify if expensive checking is enabled.
30#ifdef XDEBUG
Dan Gohman811edc12010-08-02 18:50:06 +000031static bool VerifyRegionInfo = true;
Tobias Grosserf96b0062010-07-22 07:46:31 +000032#else
Dan Gohman811edc12010-08-02 18:50:06 +000033static bool VerifyRegionInfo = false;
Tobias Grosserf96b0062010-07-22 07:46:31 +000034#endif
35
36static cl::opt<bool,true>
37VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
38 cl::desc("Verify region info (time consuming)"));
39
40STATISTIC(numRegions, "The # of regions");
41STATISTIC(numSimpleRegions, "The # of simple regions");
42
Tobias Grossercc5d9922011-04-04 07:19:18 +000043static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
44 cl::Hidden,
Tobias Grosserf96b0062010-07-22 07:46:31 +000045 cl::desc("style of printing regions"),
46 cl::values(
Tobias Grossercc5d9922011-04-04 07:19:18 +000047 clEnumValN(Region::PrintNone, "none", "print no details"),
48 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng23a22a22012-08-27 13:49:24 +000049 "print regions in detail with block_iterator"),
Tobias Grossercc5d9922011-04-04 07:19:18 +000050 clEnumValN(Region::PrintRN, "rn",
51 "print regions in detail with element_iterator"),
Tobias Grosserf96b0062010-07-22 07:46:31 +000052 clEnumValEnd));
53//===----------------------------------------------------------------------===//
54/// Region Implementation
55Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
56 DominatorTree *dt, Region *Parent)
57 : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
58
59Region::~Region() {
Daniel Dunbar329878f2010-07-28 20:28:50 +000060 // Free the cached nodes.
61 for (BBNodeMapT::iterator it = BBNodeMap.begin(),
62 ie = BBNodeMap.end(); it != ie; ++it)
63 delete it->second;
64
Tobias Grosserf96b0062010-07-22 07:46:31 +000065 // 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 Grosser4bcc0222010-10-13 05:54:10 +000073void Region::replaceEntry(BasicBlock *BB) {
74 entry.setPointer(BB);
75}
76
77void Region::replaceExit(BasicBlock *BB) {
78 assert(exit && "No exit to replace!");
79 exit = BB;
80}
81
Tobias Grosserd03fdfb2013-04-10 06:54:49 +000082void 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
98void 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 Grosserf96b0062010-07-22 07:46:31 +0000114bool 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 Grosser082d5872010-07-27 04:17:13 +0000129bool 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
150Loop *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
161Loop *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 Grosser21d842c2011-01-13 23:18:04 +0000167BasicBlock *Region::getEnteringBlock() const {
168 BasicBlock *entry = getEntry();
169 BasicBlock *Pred;
170 BasicBlock *enteringBlock = 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000171
172 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
Tobias Grosser73362c82010-08-10 09:54:35 +0000173 ++PI) {
Tobias Grosser21d842c2011-01-13 23:18:04 +0000174 Pred = *PI;
Tobias Grosser73362c82010-08-10 09:54:35 +0000175 if (DT->getNode(Pred) && !contains(Pred)) {
Tobias Grosser21d842c2011-01-13 23:18:04 +0000176 if (enteringBlock)
177 return 0;
178
179 enteringBlock = Pred;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000180 }
Tobias Grosser73362c82010-08-10 09:54:35 +0000181 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000182
Tobias Grosser21d842c2011-01-13 23:18:04 +0000183 return enteringBlock;
184}
185
186BasicBlock *Region::getExitingBlock() const {
187 BasicBlock *exit = getExit();
188 BasicBlock *Pred;
189 BasicBlock *exitingBlock = 0;
190
191 if (!exit)
192 return 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000193
194 for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
Tobias Grosser21d842c2011-01-13 23:18:04 +0000195 ++PI) {
196 Pred = *PI;
197 if (contains(Pred)) {
198 if (exitingBlock)
199 return 0;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000200
Tobias Grosser21d842c2011-01-13 23:18:04 +0000201 exitingBlock = Pred;
202 }
203 }
204
205 return exitingBlock;
206}
207
208bool Region::isSimple() const {
209 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
Tobias Grosserf96b0062010-07-22 07:46:31 +0000210}
211
Tobias Grosser082d5872010-07-27 04:17:13 +0000212std::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 Grosser082d5872010-07-27 04:17:13 +0000220 } else
Benjamin Kramer2774dc02011-11-15 18:30:06 +0000221 entryName = getEntry()->getName();
Tobias Grosser082d5872010-07-27 04:17:13 +0000222
223 if (getExit()) {
224 if (getExit()->getName().empty()) {
225 raw_string_ostream OS(exitName);
226
227 WriteAsOperand(OS, getExit(), false);
Tobias Grosser082d5872010-07-27 04:17:13 +0000228 } else
Benjamin Kramer2774dc02011-11-15 18:30:06 +0000229 exitName = getExit()->getName();
Tobias Grosser082d5872010-07-27 04:17:13 +0000230 } else
231 exitName = "<Function Return>";
232
233 return entryName + " => " + exitName;
234}
235
Tobias Grosserf96b0062010-07-22 07:46:31 +0000236void 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
252void 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
264void 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
273void Region::verifyRegionNest() const {
274 for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
275 (*RI)->verifyRegionNest();
276
277 verifyRegion();
278}
279
Tobias Grosserf96b0062010-07-22 07:46:31 +0000280Region::element_iterator Region::element_begin() {
281 return GraphTraits<Region*>::nodes_begin(this);
282}
283
284Region::element_iterator Region::element_end() {
285 return GraphTraits<Region*>::nodes_end(this);
286}
287
288Region::const_element_iterator Region::element_begin() const {
289 return GraphTraits<const Region*>::nodes_begin(this);
290}
291
292Region::const_element_iterator Region::element_end() const {
293 return GraphTraits<const Region*>::nodes_end(this);
294}
295
296Region* 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
314RegionNode* 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
327RegionNode* 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
335void 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 Grosser96493902010-10-13 05:54:09 +0000343void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000344 assert(SubRegion->parent == 0 && "SubRegion already has a parent!");
Tobias Grosser96493902010-10-13 05:54:09 +0000345 assert(std::find(begin(), end(), SubRegion) == children.end()
346 && "Subregion already exists!");
347
Tobias Grosserf96b0062010-07-22 07:46:31 +0000348 SubRegion->parent = this;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000349 children.push_back(SubRegion);
Tobias Grosser96493902010-10-13 05:54:09 +0000350
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 Grosserf96b0062010-07-22 07:46:31 +0000375}
376
377
378Region *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
387unsigned 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 Grosserc69bd732010-10-13 05:54:11 +0000396Region *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 Grossercc5d9922011-04-04 07:19:18 +0000428void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
429 enum PrintStyle Style) const {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000430 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 Grossercc5d9922011-04-04 07:19:18 +0000438 if (Style != PrintNone) {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000439 OS.indent(level*2) << "{\n";
440 OS.indent(level*2 + 2);
441
Tobias Grossercc5d9922011-04-04 07:19:18 +0000442 if (Style == PrintBB) {
Hongbin Zheng23a22a22012-08-27 13:49:24 +0000443 for (const_block_iterator I = block_begin(), E = block_end(); I != E; ++I)
444 OS << (*I)->getName() << ", "; // TODO: remove the last ","
Tobias Grossercc5d9922011-04-04 07:19:18 +0000445 } else if (Style == PrintRN) {
Tobias Grosserf96b0062010-07-22 07:46:31 +0000446 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 Grossercc5d9922011-04-04 07:19:18 +0000455 (*RI)->print(OS, print_tree, level+1, Style);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000456
Tobias Grossercc5d9922011-04-04 07:19:18 +0000457 if (Style != PrintNone)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000458 OS.indent(level*2) << "} \n";
459}
460
Manman Ren286c4dc2012-09-12 05:06:18 +0000461#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000462void Region::dump() const {
Tobias Grossercc5d9922011-04-04 07:19:18 +0000463 print(dbgs(), true, getDepth(), printStyle.getValue());
Tobias Grosserf96b0062010-07-22 07:46:31 +0000464}
Manman Rencc77eec2012-09-06 19:55:56 +0000465#endif
Tobias Grosserf96b0062010-07-22 07:46:31 +0000466
467void Region::clearNodeCache() {
Tobias Grosser67be08a2010-10-13 00:07:59 +0000468 // Free the cached nodes.
469 for (BBNodeMapT::iterator I = BBNodeMap.begin(),
Tobias Grosserc5021012010-10-13 08:00:53 +0000470 IE = BBNodeMap.end(); I != IE; ++I)
Tobias Grosser67be08a2010-10-13 00:07:59 +0000471 delete I->second;
472
Tobias Grosserf96b0062010-07-22 07:46:31 +0000473 BBNodeMap.clear();
474 for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
475 (*RI)->clearNodeCache();
476}
477
478//===----------------------------------------------------------------------===//
479// RegionInfo implementation
480//
481
482bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
483 BasicBlock *exit) const {
Gabor Greif3d8586e2010-07-22 11:07:46 +0000484 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 Grosserf96b0062010-07-22 07:46:31 +0000487 return false;
Gabor Greif3d8586e2010-07-22 11:07:46 +0000488 }
Tobias Grosserf96b0062010-07-22 07:46:31 +0000489 return true;
490}
491
492bool 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 Greif11aa60d2010-07-22 11:12:32 +0000496 DST *entrySuccs = &DF->find(entry)->second;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000497
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 Greif11aa60d2010-07-22 11:12:32 +0000509 DST *exitSuccs = &DF->find(exit)->second;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000510
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 Gohmana29dbd22010-07-26 17:34:05 +0000525 if (DT->properlyDominates(entry, *SI) && *SI != exit)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000526 return false;
527
528
529 return true;
530}
531
532void 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
550DomTreeNode* 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
560bool 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
571void 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
578Region *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
597void 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
637void 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 Greiff95eef62010-07-22 13:49:27 +0000647 findRegionsWithEntry(FI->getBlock(), ShortCut);
Tobias Grosserf96b0062010-07-22 07:46:31 +0000648 }
649}
650
651Region *RegionInfo::getTopMostParent(Region *region) {
652 while (region->parent)
653 region = region->getParent();
654
655 return region;
656}
657
658void 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 Rosier90f20042012-02-22 17:25:00 +0000670 Region *newRegion = it->second;
Tobias Grosserf96b0062010-07-22 07:46:31 +0000671 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
681void RegionInfo::releaseMemory() {
682 BBtoRegion.clear();
683 if (TopLevelRegion)
684 delete TopLevelRegion;
685 TopLevelRegion = 0;
686}
687
Owen Anderson90c579d2010-08-06 18:33:48 +0000688RegionInfo::RegionInfo() : FunctionPass(ID) {
Owen Anderson081c34b2010-10-19 17:21:58 +0000689 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
Tobias Grosserf96b0062010-07-22 07:46:31 +0000690 TopLevelRegion = 0;
691}
692
693RegionInfo::~RegionInfo() {
694 releaseMemory();
695}
696
697void 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
708bool 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
723void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
724 AU.setPreservesAll();
725 AU.addRequiredTransitive<DominatorTree>();
726 AU.addRequired<PostDominatorTree>();
727 AU.addRequired<DominanceFrontier>();
728}
729
730void RegionInfo::print(raw_ostream &OS, const Module *) const {
731 OS << "Region tree:\n";
Tobias Grossercc5d9922011-04-04 07:19:18 +0000732 TopLevelRegion->print(OS, true, 0, printStyle.getValue());
Tobias Grosserf96b0062010-07-22 07:46:31 +0000733 OS << "End region tree\n";
734}
735
736void 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.
746Region *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 Grosser9ee5c502010-10-13 05:54:07 +0000752void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
753 BBtoRegion[BB] = R;
754}
755
Tobias Grosserf96b0062010-07-22 07:46:31 +0000756Region *RegionInfo::operator[](BasicBlock *BB) const {
757 return getRegionFor(BB);
758}
759
Tobias Grosser0e6fcf42010-07-27 08:39:43 +0000760BasicBlock *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 Grosserf96b0062010-07-22 07:46:31 +0000798Region*
799RegionInfo::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
810Region*
811RegionInfo::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
822Region*
823RegionInfo::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 Grosser592316c2010-10-13 05:54:13 +0000834void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
835{
836 Region *R = getRegionFor(OldBB);
Tobias Grosserb2279302010-10-13 11:02:44 +0000837
Tobias Grosser592316c2010-10-13 05:54:13 +0000838 setRegionFor(NewBB, R);
839
Tobias Grosserb2279302010-10-13 11:02:44 +0000840 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
Tobias Grosser592316c2010-10-13 05:54:13 +0000841 R->replaceEntry(NewBB);
842 R = R->getParent();
843 }
844
845 setRegionFor(OldBB, R);
846}
847
Tobias Grosserf96b0062010-07-22 07:46:31 +0000848char RegionInfo::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000849INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
850 "Detect single entry single exit regions", true, true)
851INITIALIZE_PASS_DEPENDENCY(DominatorTree)
852INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
853INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
854INITIALIZE_PASS_END(RegionInfo, "regions",
Owen Andersonce665bd2010-10-07 22:25:06 +0000855 "Detect single entry single exit regions", true, true)
Tobias Grosserf96b0062010-07-22 07:46:31 +0000856
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
861namespace llvm {
862 FunctionPass *createRegionInfoPass() {
863 return new RegionInfo();
864 }
865}
866