blob: f0bf59972bf3196587261bee960399290437e85d [file] [log] [blame]
Tobias Grosser336734a2010-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
Bill Wendling570d3022013-08-21 21:14:19 +000012#define DEBUG_TYPE "region"
Tobias Grosser336734a2010-07-22 07:46:31 +000013#include "llvm/Analysis/RegionInfo.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000014#include "llvm/ADT/PostOrderIterator.h"
15#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/Analysis/RegionIterator.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000018#include "llvm/Support/CommandLine.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000019#include "llvm/Support/Debug.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/Support/ErrorHandling.h"
Tobias Grosser336734a2010-07-22 07:46:31 +000021#include <algorithm>
David Blaikieec649ac2014-04-15 18:32:43 +000022#include <iterator>
Bill Wendling570d3022013-08-21 21:14:19 +000023#include <set>
Tobias Grosser336734a2010-07-22 07:46:31 +000024
25using namespace llvm;
26
27// Always verify if expensive checking is enabled.
28#ifdef XDEBUG
Dan Gohmanabfafad2010-08-02 18:50:06 +000029static bool VerifyRegionInfo = true;
Tobias Grosser336734a2010-07-22 07:46:31 +000030#else
Dan Gohmanabfafad2010-08-02 18:50:06 +000031static bool VerifyRegionInfo = false;
Tobias Grosser336734a2010-07-22 07:46:31 +000032#endif
33
34static cl::opt<bool,true>
35VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
36 cl::desc("Verify region info (time consuming)"));
37
38STATISTIC(numRegions, "The # of regions");
39STATISTIC(numSimpleRegions, "The # of simple regions");
40
Tobias Grosser8b304ff2011-04-04 07:19:18 +000041static cl::opt<enum Region::PrintStyle> printStyle("print-region-style",
42 cl::Hidden,
Tobias Grosser336734a2010-07-22 07:46:31 +000043 cl::desc("style of printing regions"),
44 cl::values(
Tobias Grosser8b304ff2011-04-04 07:19:18 +000045 clEnumValN(Region::PrintNone, "none", "print no details"),
46 clEnumValN(Region::PrintBB, "bb",
Hongbin Zheng14c05c42012-08-27 13:49:24 +000047 "print regions in detail with block_iterator"),
Tobias Grosser8b304ff2011-04-04 07:19:18 +000048 clEnumValN(Region::PrintRN, "rn",
49 "print regions in detail with element_iterator"),
Tobias Grosser336734a2010-07-22 07:46:31 +000050 clEnumValEnd));
51//===----------------------------------------------------------------------===//
52/// Region Implementation
53Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
54 DominatorTree *dt, Region *Parent)
55 : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
56
57Region::~Region() {
Daniel Dunbar18e39ce2010-07-28 20:28:50 +000058 // Free the cached nodes.
59 for (BBNodeMapT::iterator it = BBNodeMap.begin(),
60 ie = BBNodeMap.end(); it != ie; ++it)
61 delete it->second;
62
Tobias Grosser336734a2010-07-22 07:46:31 +000063 // Only clean the cache for this Region. Caches of child Regions will be
64 // cleaned when the child Regions are deleted.
65 BBNodeMap.clear();
Tobias Grosser336734a2010-07-22 07:46:31 +000066}
67
Tobias Grosser648594c2010-10-13 05:54:10 +000068void Region::replaceEntry(BasicBlock *BB) {
69 entry.setPointer(BB);
70}
71
72void Region::replaceExit(BasicBlock *BB) {
73 assert(exit && "No exit to replace!");
74 exit = BB;
75}
76
Tobias Grosser141cc3e2013-04-10 06:54:49 +000077void Region::replaceEntryRecursive(BasicBlock *NewEntry) {
78 std::vector<Region *> RegionQueue;
79 BasicBlock *OldEntry = getEntry();
80
81 RegionQueue.push_back(this);
82 while (!RegionQueue.empty()) {
83 Region *R = RegionQueue.back();
84 RegionQueue.pop_back();
85
86 R->replaceEntry(NewEntry);
87 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
88 if ((*RI)->getEntry() == OldEntry)
David Blaikieec649ac2014-04-15 18:32:43 +000089 RegionQueue.push_back(RI->get());
Tobias Grosser141cc3e2013-04-10 06:54:49 +000090 }
91}
92
93void Region::replaceExitRecursive(BasicBlock *NewExit) {
94 std::vector<Region *> RegionQueue;
95 BasicBlock *OldExit = getExit();
96
97 RegionQueue.push_back(this);
98 while (!RegionQueue.empty()) {
99 Region *R = RegionQueue.back();
100 RegionQueue.pop_back();
101
102 R->replaceExit(NewExit);
103 for (Region::const_iterator RI = R->begin(), RE = R->end(); RI != RE; ++RI)
104 if ((*RI)->getExit() == OldExit)
David Blaikieec649ac2014-04-15 18:32:43 +0000105 RegionQueue.push_back(RI->get());
Tobias Grosser141cc3e2013-04-10 06:54:49 +0000106 }
107}
108
Tobias Grosser336734a2010-07-22 07:46:31 +0000109bool Region::contains(const BasicBlock *B) const {
110 BasicBlock *BB = const_cast<BasicBlock*>(B);
111
Tobias Grossera7ddc982013-05-03 15:48:34 +0000112 if (!DT->getNode(BB))
113 return false;
Tobias Grosser336734a2010-07-22 07:46:31 +0000114
115 BasicBlock *entry = getEntry(), *exit = getExit();
116
117 // Toplevel region.
118 if (!exit)
119 return true;
120
121 return (DT->dominates(entry, BB)
122 && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
123}
124
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000125bool Region::contains(const Loop *L) const {
126 // BBs that are not part of any loop are element of the Loop
127 // described by the NULL pointer. This loop is not part of any region,
128 // except if the region describes the whole function.
Craig Topper9f008862014-04-15 04:59:12 +0000129 if (!L)
130 return getExit() == nullptr;
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000131
132 if (!contains(L->getHeader()))
133 return false;
134
135 SmallVector<BasicBlock *, 8> ExitingBlocks;
136 L->getExitingBlocks(ExitingBlocks);
137
138 for (SmallVectorImpl<BasicBlock*>::iterator BI = ExitingBlocks.begin(),
139 BE = ExitingBlocks.end(); BI != BE; ++BI)
140 if (!contains(*BI))
141 return false;
142
143 return true;
144}
145
146Loop *Region::outermostLoopInRegion(Loop *L) const {
147 if (!contains(L))
Craig Topper9f008862014-04-15 04:59:12 +0000148 return nullptr;
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000149
150 while (L && contains(L->getParentLoop())) {
151 L = L->getParentLoop();
152 }
153
154 return L;
155}
156
157Loop *Region::outermostLoopInRegion(LoopInfo *LI, BasicBlock* BB) const {
158 assert(LI && BB && "LI and BB cannot be null!");
159 Loop *L = LI->getLoopFor(BB);
160 return outermostLoopInRegion(L);
161}
162
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000163BasicBlock *Region::getEnteringBlock() const {
164 BasicBlock *entry = getEntry();
165 BasicBlock *Pred;
Craig Topper9f008862014-04-15 04:59:12 +0000166 BasicBlock *enteringBlock = nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000167
168 for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
Tobias Grosser7fbe6cb2010-08-10 09:54:35 +0000169 ++PI) {
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000170 Pred = *PI;
Tobias Grosser7fbe6cb2010-08-10 09:54:35 +0000171 if (DT->getNode(Pred) && !contains(Pred)) {
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000172 if (enteringBlock)
Craig Topper9f008862014-04-15 04:59:12 +0000173 return nullptr;
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000174
175 enteringBlock = Pred;
Tobias Grosser336734a2010-07-22 07:46:31 +0000176 }
Tobias Grosser7fbe6cb2010-08-10 09:54:35 +0000177 }
Tobias Grosser336734a2010-07-22 07:46:31 +0000178
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000179 return enteringBlock;
180}
181
182BasicBlock *Region::getExitingBlock() const {
183 BasicBlock *exit = getExit();
184 BasicBlock *Pred;
Craig Topper9f008862014-04-15 04:59:12 +0000185 BasicBlock *exitingBlock = nullptr;
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000186
187 if (!exit)
Craig Topper9f008862014-04-15 04:59:12 +0000188 return nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000189
190 for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000191 ++PI) {
192 Pred = *PI;
193 if (contains(Pred)) {
194 if (exitingBlock)
Craig Topper9f008862014-04-15 04:59:12 +0000195 return nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000196
Tobias Grosserb1d11c12011-01-13 23:18:04 +0000197 exitingBlock = Pred;
198 }
199 }
200
201 return exitingBlock;
202}
203
204bool Region::isSimple() const {
205 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
Tobias Grosser336734a2010-07-22 07:46:31 +0000206}
207
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000208std::string Region::getNameStr() const {
209 std::string exitName;
210 std::string entryName;
211
212 if (getEntry()->getName().empty()) {
213 raw_string_ostream OS(entryName);
214
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000215 getEntry()->printAsOperand(OS, false);
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000216 } else
Benjamin Kramer184e3cee2011-11-15 18:30:06 +0000217 entryName = getEntry()->getName();
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000218
219 if (getExit()) {
220 if (getExit()->getName().empty()) {
221 raw_string_ostream OS(exitName);
222
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000223 getExit()->printAsOperand(OS, false);
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000224 } else
Benjamin Kramer184e3cee2011-11-15 18:30:06 +0000225 exitName = getExit()->getName();
Tobias Grosser1bec81a2010-07-27 04:17:13 +0000226 } else
227 exitName = "<Function Return>";
228
229 return entryName + " => " + exitName;
230}
231
Tobias Grosser336734a2010-07-22 07:46:31 +0000232void Region::verifyBBInRegion(BasicBlock *BB) const {
233 if (!contains(BB))
234 llvm_unreachable("Broken region found!");
235
236 BasicBlock *entry = getEntry(), *exit = getExit();
237
238 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
239 if (!contains(*SI) && exit != *SI)
240 llvm_unreachable("Broken region found!");
241
242 if (entry != BB)
243 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
244 if (!contains(*SI))
245 llvm_unreachable("Broken region found!");
246}
247
248void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
249 BasicBlock *exit = getExit();
250
251 visited->insert(BB);
252
253 verifyBBInRegion(BB);
254
255 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
256 if (*SI != exit && visited->find(*SI) == visited->end())
257 verifyWalk(*SI, visited);
258}
259
260void Region::verifyRegion() const {
261 // Only do verification when user wants to, otherwise this expensive
262 // check will be invoked by PassManager.
263 if (!VerifyRegionInfo) return;
264
265 std::set<BasicBlock*> visited;
266 verifyWalk(getEntry(), &visited);
267}
268
269void Region::verifyRegionNest() const {
270 for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
271 (*RI)->verifyRegionNest();
272
273 verifyRegion();
274}
275
Tobias Grosser336734a2010-07-22 07:46:31 +0000276Region::element_iterator Region::element_begin() {
277 return GraphTraits<Region*>::nodes_begin(this);
278}
279
280Region::element_iterator Region::element_end() {
281 return GraphTraits<Region*>::nodes_end(this);
282}
283
284Region::const_element_iterator Region::element_begin() const {
285 return GraphTraits<const Region*>::nodes_begin(this);
286}
287
288Region::const_element_iterator Region::element_end() const {
289 return GraphTraits<const Region*>::nodes_end(this);
290}
291
292Region* Region::getSubRegionNode(BasicBlock *BB) const {
293 Region *R = RI->getRegionFor(BB);
294
295 if (!R || R == this)
Craig Topper9f008862014-04-15 04:59:12 +0000296 return nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000297
298 // If we pass the BB out of this region, that means our code is broken.
299 assert(contains(R) && "BB not in current region!");
300
301 while (contains(R->getParent()) && R->getParent() != this)
302 R = R->getParent();
303
304 if (R->getEntry() != BB)
Craig Topper9f008862014-04-15 04:59:12 +0000305 return nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000306
307 return R;
308}
309
310RegionNode* Region::getBBNode(BasicBlock *BB) const {
311 assert(contains(BB) && "Can get BB node out of this region!");
312
313 BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
314
315 if (at != BBNodeMap.end())
316 return at->second;
317
318 RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
319 BBNodeMap.insert(std::make_pair(BB, NewNode));
320 return NewNode;
321}
322
323RegionNode* Region::getNode(BasicBlock *BB) const {
324 assert(contains(BB) && "Can get BB node out of this region!");
325 if (Region* Child = getSubRegionNode(BB))
326 return Child->getNode();
327
328 return getBBNode(BB);
329}
330
331void Region::transferChildrenTo(Region *To) {
332 for (iterator I = begin(), E = end(); I != E; ++I) {
333 (*I)->parent = To;
David Blaikieec649ac2014-04-15 18:32:43 +0000334 To->children.push_back(std::move(*I));
Tobias Grosser336734a2010-07-22 07:46:31 +0000335 }
336 children.clear();
337}
338
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000339void Region::addSubRegion(Region *SubRegion, bool moveChildren) {
Craig Topper9f008862014-04-15 04:59:12 +0000340 assert(!SubRegion->parent && "SubRegion already has a parent!");
David Blaikieec649ac2014-04-15 18:32:43 +0000341 assert(std::find_if(begin(), end(), [&](const std::unique_ptr<Region> &R) {
342 return R.get() == SubRegion;
343 }) == children.end() &&
344 "Subregion already exists!");
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000345
Tobias Grosser336734a2010-07-22 07:46:31 +0000346 SubRegion->parent = this;
David Blaikieec649ac2014-04-15 18:32:43 +0000347 children.push_back(std::unique_ptr<Region>(SubRegion));
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000348
349 if (!moveChildren)
350 return;
351
352 assert(SubRegion->children.size() == 0
353 && "SubRegions that contain children are not supported");
354
355 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
356 if (!(*I)->isSubRegion()) {
357 BasicBlock *BB = (*I)->getNodeAs<BasicBlock>();
358
359 if (SubRegion->contains(BB))
360 RI->setRegionFor(BB, SubRegion);
361 }
362
David Blaikieec649ac2014-04-15 18:32:43 +0000363 std::vector<std::unique_ptr<Region>> Keep;
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000364 for (iterator I = begin(), E = end(); I != E; ++I)
David Blaikieec649ac2014-04-15 18:32:43 +0000365 if (SubRegion->contains(I->get()) && I->get() != SubRegion) {
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000366 (*I)->parent = SubRegion;
Tobias Grosser8d941ef2014-04-15 22:09:36 +0000367 SubRegion->children.push_back(std::move(*I));
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000368 } else
David Blaikieec649ac2014-04-15 18:32:43 +0000369 Keep.push_back(std::move(*I));
Tobias Grosserbf984fd2010-10-13 05:54:09 +0000370
371 children.clear();
David Blaikieec649ac2014-04-15 18:32:43 +0000372 children.insert(children.begin(),
373 std::move_iterator<RegionSet::iterator>(Keep.begin()),
374 std::move_iterator<RegionSet::iterator>(Keep.end()));
Tobias Grosser336734a2010-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!");
Craig Topper9f008862014-04-15 04:59:12 +0000380 Child->parent = nullptr;
David Blaikieec649ac2014-04-15 18:32:43 +0000381 RegionSet::iterator I = std::find_if(
382 children.begin(), children.end(),
383 [&](const std::unique_ptr<Region> &R) { return R.get() == Child; });
Tobias Grosser336734a2010-07-22 07:46:31 +0000384 assert(I != children.end() && "Region does not exit. Unable to remove.");
385 children.erase(children.begin()+(I-begin()));
386 return Child;
387}
388
389unsigned Region::getDepth() const {
390 unsigned Depth = 0;
391
Craig Topper9f008862014-04-15 04:59:12 +0000392 for (Region *R = parent; R != nullptr; R = R->parent)
Tobias Grosser336734a2010-07-22 07:46:31 +0000393 ++Depth;
394
395 return Depth;
396}
397
Tobias Grossera8677222010-10-13 05:54:11 +0000398Region *Region::getExpandedRegion() const {
399 unsigned NumSuccessors = exit->getTerminator()->getNumSuccessors();
400
401 if (NumSuccessors == 0)
Craig Topper9f008862014-04-15 04:59:12 +0000402 return nullptr;
Tobias Grossera8677222010-10-13 05:54:11 +0000403
404 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
405 PI != PE; ++PI)
406 if (!DT->dominates(getEntry(), *PI))
Craig Topper9f008862014-04-15 04:59:12 +0000407 return nullptr;
Tobias Grossera8677222010-10-13 05:54:11 +0000408
409 Region *R = RI->getRegionFor(exit);
410
411 if (R->getEntry() != exit) {
412 if (exit->getTerminator()->getNumSuccessors() == 1)
413 return new Region(getEntry(), *succ_begin(exit), RI, DT);
414 else
Craig Topper9f008862014-04-15 04:59:12 +0000415 return nullptr;
Tobias Grossera8677222010-10-13 05:54:11 +0000416 }
417
418 while (R->getParent() && R->getParent()->getEntry() == exit)
419 R = R->getParent();
420
421 if (!DT->dominates(getEntry(), R->getExit()))
422 for (pred_iterator PI = pred_begin(getExit()), PE = pred_end(getExit());
423 PI != PE; ++PI)
424 if (!DT->dominates(R->getExit(), *PI))
Craig Topper9f008862014-04-15 04:59:12 +0000425 return nullptr;
Tobias Grossera8677222010-10-13 05:54:11 +0000426
427 return new Region(getEntry(), R->getExit(), RI, DT);
428}
429
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000430void Region::print(raw_ostream &OS, bool print_tree, unsigned level,
431 enum PrintStyle Style) const {
Tobias Grosser336734a2010-07-22 07:46:31 +0000432 if (print_tree)
433 OS.indent(level*2) << "[" << level << "] " << getNameStr();
434 else
435 OS.indent(level*2) << getNameStr();
436
437 OS << "\n";
438
439
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000440 if (Style != PrintNone) {
Tobias Grosser336734a2010-07-22 07:46:31 +0000441 OS.indent(level*2) << "{\n";
442 OS.indent(level*2 + 2);
443
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000444 if (Style == PrintBB) {
Tobias Grosser4abf9d32014-03-03 13:00:39 +0000445 for (const auto &BB : blocks())
446 OS << BB->getName() << ", "; // TODO: remove the last ","
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000447 } else if (Style == PrintRN) {
Tobias Grosser336734a2010-07-22 07:46:31 +0000448 for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
449 OS << **I << ", "; // TODO: remove the last ",
450 }
451
452 OS << "\n";
453 }
454
455 if (print_tree)
456 for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000457 (*RI)->print(OS, print_tree, level+1, Style);
Tobias Grosser336734a2010-07-22 07:46:31 +0000458
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000459 if (Style != PrintNone)
Tobias Grosser336734a2010-07-22 07:46:31 +0000460 OS.indent(level*2) << "} \n";
461}
462
Manman Ren49d684e2012-09-12 05:06:18 +0000463#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Tobias Grosser336734a2010-07-22 07:46:31 +0000464void Region::dump() const {
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000465 print(dbgs(), true, getDepth(), printStyle.getValue());
Tobias Grosser336734a2010-07-22 07:46:31 +0000466}
Manman Renc3366cc2012-09-06 19:55:56 +0000467#endif
Tobias Grosser336734a2010-07-22 07:46:31 +0000468
469void Region::clearNodeCache() {
Tobias Grossere910b9d2010-10-13 00:07:59 +0000470 // Free the cached nodes.
471 for (BBNodeMapT::iterator I = BBNodeMap.begin(),
Tobias Grosser4c71c112010-10-13 08:00:53 +0000472 IE = BBNodeMap.end(); I != IE; ++I)
Tobias Grossere910b9d2010-10-13 00:07:59 +0000473 delete I->second;
474
Tobias Grosser336734a2010-07-22 07:46:31 +0000475 BBNodeMap.clear();
476 for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
477 (*RI)->clearNodeCache();
478}
479
480//===----------------------------------------------------------------------===//
481// RegionInfo implementation
482//
483
484bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
485 BasicBlock *exit) const {
Gabor Greif07c8ad52010-07-22 11:07:46 +0000486 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
487 BasicBlock *P = *PI;
488 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
Tobias Grosser336734a2010-07-22 07:46:31 +0000489 return false;
Gabor Greif07c8ad52010-07-22 11:07:46 +0000490 }
Tobias Grosser336734a2010-07-22 07:46:31 +0000491 return true;
492}
493
494bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
495 assert(entry && exit && "entry and exit must not be null!");
496 typedef DominanceFrontier::DomSetType DST;
497
Gabor Greifd9f48ec2010-07-22 11:12:32 +0000498 DST *entrySuccs = &DF->find(entry)->second;
Tobias Grosser336734a2010-07-22 07:46:31 +0000499
500 // Exit is the header of a loop that contains the entry. In this case,
501 // the dominance frontier must only contain the exit.
502 if (!DT->dominates(entry, exit)) {
503 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
504 SI != SE; ++SI)
505 if (*SI != exit && *SI != entry)
506 return false;
507
508 return true;
509 }
510
Gabor Greifd9f48ec2010-07-22 11:12:32 +0000511 DST *exitSuccs = &DF->find(exit)->second;
Tobias Grosser336734a2010-07-22 07:46:31 +0000512
513 // Do not allow edges leaving the region.
514 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
515 SI != SE; ++SI) {
516 if (*SI == exit || *SI == entry)
517 continue;
518 if (exitSuccs->find(*SI) == exitSuccs->end())
519 return false;
520 if (!isCommonDomFrontier(*SI, entry, exit))
521 return false;
522 }
523
524 // Do not allow edges pointing into the region.
525 for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
526 SI != SE; ++SI)
Dan Gohmanb3aa6c72010-07-26 17:34:05 +0000527 if (DT->properlyDominates(entry, *SI) && *SI != exit)
Tobias Grosser336734a2010-07-22 07:46:31 +0000528 return false;
529
530
531 return true;
532}
533
534void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
535 BBtoBBMap *ShortCut) const {
536 assert(entry && exit && "entry and exit must not be null!");
537
538 BBtoBBMap::iterator e = ShortCut->find(exit);
539
540 if (e == ShortCut->end())
541 // No further region at exit available.
542 (*ShortCut)[entry] = exit;
543 else {
544 // We found a region e that starts at exit. Therefore (entry, e->second)
545 // is also a region, that is larger than (entry, exit). Insert the
546 // larger one.
547 BasicBlock *BB = e->second;
548 (*ShortCut)[entry] = BB;
549 }
550}
551
552DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
553 BBtoBBMap *ShortCut) const {
554 BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
555
556 if (e == ShortCut->end())
557 return N->getIDom();
558
559 return PDT->getNode(e->second)->getIDom();
560}
561
562bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
563 assert(entry && exit && "entry and exit must not be null!");
564
565 unsigned num_successors = succ_end(entry) - succ_begin(entry);
566
567 if (num_successors <= 1 && exit == *(succ_begin(entry)))
568 return true;
569
570 return false;
571}
572
573void RegionInfo::updateStatistics(Region *R) {
574 ++numRegions;
575
576 // TODO: Slow. Should only be enabled if -stats is used.
577 if (R->isSimple()) ++numSimpleRegions;
578}
579
580Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
581 assert(entry && exit && "entry and exit must not be null!");
582
583 if (isTrivialRegion(entry, exit))
Craig Topper9f008862014-04-15 04:59:12 +0000584 return nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000585
586 Region *region = new Region(entry, exit, this, DT);
587 BBtoRegion.insert(std::make_pair(entry, region));
588
589 #ifdef XDEBUG
590 region->verifyRegion();
591 #else
592 DEBUG(region->verifyRegion());
593 #endif
594
595 updateStatistics(region);
596 return region;
597}
598
599void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
600 assert(entry);
601
602 DomTreeNode *N = PDT->getNode(entry);
603
604 if (!N)
605 return;
606
Craig Topper9f008862014-04-15 04:59:12 +0000607 Region *lastRegion= nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000608 BasicBlock *lastExit = entry;
609
610 // As only a BasicBlock that postdominates entry can finish a region, walk the
611 // post dominance tree upwards.
612 while ((N = getNextPostDom(N, ShortCut))) {
613 BasicBlock *exit = N->getBlock();
614
615 if (!exit)
616 break;
617
618 if (isRegion(entry, exit)) {
619 Region *newRegion = createRegion(entry, exit);
620
621 if (lastRegion)
622 newRegion->addSubRegion(lastRegion);
623
624 lastRegion = newRegion;
625 lastExit = exit;
626 }
627
628 // This can never be a region, so stop the search.
629 if (!DT->dominates(entry, exit))
630 break;
631 }
632
633 // Tried to create regions from entry to lastExit. Next time take a
634 // shortcut from entry to lastExit.
635 if (lastExit != entry)
636 insertShortCut(entry, lastExit, ShortCut);
637}
638
639void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
640 BasicBlock *entry = &(F.getEntryBlock());
641 DomTreeNode *N = DT->getNode(entry);
642
643 // Iterate over the dominance tree in post order to start with the small
644 // regions from the bottom of the dominance tree. If the small regions are
645 // detected first, detection of bigger regions is faster, as we can jump
646 // over the small regions.
647 for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
648 ++FI) {
Gabor Greif1a2da422010-07-22 13:49:27 +0000649 findRegionsWithEntry(FI->getBlock(), ShortCut);
Tobias Grosser336734a2010-07-22 07:46:31 +0000650 }
651}
652
653Region *RegionInfo::getTopMostParent(Region *region) {
654 while (region->parent)
655 region = region->getParent();
656
657 return region;
658}
659
660void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
661 BasicBlock *BB = N->getBlock();
662
663 // Passed region exit
664 while (BB == region->getExit())
665 region = region->getParent();
666
667 BBtoRegionMap::iterator it = BBtoRegion.find(BB);
668
669 // This basic block is a start block of a region. It is already in the
670 // BBtoRegion relation. Only the child basic blocks have to be updated.
671 if (it != BBtoRegion.end()) {
Chad Rosier5dfe6da2012-02-22 17:25:00 +0000672 Region *newRegion = it->second;
Tobias Grosser336734a2010-07-22 07:46:31 +0000673 region->addSubRegion(getTopMostParent(newRegion));
674 region = newRegion;
675 } else {
676 BBtoRegion[BB] = region;
677 }
678
679 for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
680 buildRegionsTree(*CI, region);
681}
682
683void RegionInfo::releaseMemory() {
684 BBtoRegion.clear();
685 if (TopLevelRegion)
686 delete TopLevelRegion;
Craig Topper9f008862014-04-15 04:59:12 +0000687 TopLevelRegion = nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000688}
689
Owen Andersona7aed182010-08-06 18:33:48 +0000690RegionInfo::RegionInfo() : FunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000691 initializeRegionInfoPass(*PassRegistry::getPassRegistry());
Craig Topper9f008862014-04-15 04:59:12 +0000692 TopLevelRegion = nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000693}
694
695RegionInfo::~RegionInfo() {
696 releaseMemory();
697}
698
699void RegionInfo::Calculate(Function &F) {
700 // ShortCut a function where for every BB the exit of the largest region
701 // starting with BB is stored. These regions can be threated as single BBS.
702 // This improves performance on linear CFGs.
703 BBtoBBMap ShortCut;
704
705 scanForRegions(F, &ShortCut);
706 BasicBlock *BB = &F.getEntryBlock();
707 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
708}
709
710bool RegionInfo::runOnFunction(Function &F) {
711 releaseMemory();
712
Chandler Carruth73523022014-01-13 13:07:17 +0000713 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser336734a2010-07-22 07:46:31 +0000714 PDT = &getAnalysis<PostDominatorTree>();
715 DF = &getAnalysis<DominanceFrontier>();
716
Craig Topper9f008862014-04-15 04:59:12 +0000717 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr, this, DT, nullptr);
Tobias Grosser336734a2010-07-22 07:46:31 +0000718 updateStatistics(TopLevelRegion);
719
720 Calculate(F);
721
722 return false;
723}
724
725void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
726 AU.setPreservesAll();
Chandler Carruth73523022014-01-13 13:07:17 +0000727 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
Tobias Grosser336734a2010-07-22 07:46:31 +0000728 AU.addRequired<PostDominatorTree>();
729 AU.addRequired<DominanceFrontier>();
730}
731
732void RegionInfo::print(raw_ostream &OS, const Module *) const {
733 OS << "Region tree:\n";
Tobias Grosser8b304ff2011-04-04 07:19:18 +0000734 TopLevelRegion->print(OS, true, 0, printStyle.getValue());
Tobias Grosser336734a2010-07-22 07:46:31 +0000735 OS << "End region tree\n";
736}
737
738void RegionInfo::verifyAnalysis() const {
739 // Only do verification when user wants to, otherwise this expensive check
740 // will be invoked by PMDataManager::verifyPreservedAnalysis when
741 // a regionpass (marked PreservedAll) finish.
742 if (!VerifyRegionInfo) return;
743
744 TopLevelRegion->verifyRegionNest();
745}
746
747// Region pass manager support.
748Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
749 BBtoRegionMap::const_iterator I=
750 BBtoRegion.find(BB);
Craig Topper9f008862014-04-15 04:59:12 +0000751 return I != BBtoRegion.end() ? I->second : nullptr;
Tobias Grosser336734a2010-07-22 07:46:31 +0000752}
753
Tobias Grosser8352ce52010-10-13 05:54:07 +0000754void RegionInfo::setRegionFor(BasicBlock *BB, Region *R) {
755 BBtoRegion[BB] = R;
756}
757
Tobias Grosser336734a2010-07-22 07:46:31 +0000758Region *RegionInfo::operator[](BasicBlock *BB) const {
759 return getRegionFor(BB);
760}
761
Tobias Grosserfc763862010-07-27 08:39:43 +0000762BasicBlock *RegionInfo::getMaxRegionExit(BasicBlock *BB) const {
Craig Topper9f008862014-04-15 04:59:12 +0000763 BasicBlock *Exit = nullptr;
Tobias Grosserfc763862010-07-27 08:39:43 +0000764
765 while (true) {
766 // Get largest region that starts at BB.
767 Region *R = getRegionFor(BB);
768 while (R && R->getParent() && R->getParent()->getEntry() == BB)
769 R = R->getParent();
770
771 // Get the single exit of BB.
772 if (R && R->getEntry() == BB)
773 Exit = R->getExit();
774 else if (++succ_begin(BB) == succ_end(BB))
775 Exit = *succ_begin(BB);
776 else // No single exit exists.
777 return Exit;
778
779 // Get largest region that starts at Exit.
780 Region *ExitR = getRegionFor(Exit);
781 while (ExitR && ExitR->getParent()
782 && ExitR->getParent()->getEntry() == Exit)
783 ExitR = ExitR->getParent();
784
785 for (pred_iterator PI = pred_begin(Exit), PE = pred_end(Exit); PI != PE;
786 ++PI)
787 if (!R->contains(*PI) && !ExitR->contains(*PI))
788 break;
789
790 // This stops infinite cycles.
791 if (DT->dominates(Exit, BB))
792 break;
793
794 BB = Exit;
795 }
796
797 return Exit;
798}
799
Tobias Grosser336734a2010-07-22 07:46:31 +0000800Region*
801RegionInfo::getCommonRegion(Region *A, Region *B) const {
802 assert (A && B && "One of the Regions is NULL");
803
804 if (A->contains(B)) return A;
805
806 while (!B->contains(A))
807 B = B->getParent();
808
809 return B;
810}
811
812Region*
813RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
814 Region* ret = Regions.back();
815 Regions.pop_back();
816
817 for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
818 E = Regions.end(); I != E; ++I)
819 ret = getCommonRegion(ret, *I);
820
821 return ret;
822}
823
824Region*
825RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
826 Region* ret = getRegionFor(BBs.back());
827 BBs.pop_back();
828
829 for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
830 E = BBs.end(); I != E; ++I)
831 ret = getCommonRegion(ret, getRegionFor(*I));
832
833 return ret;
834}
835
Tobias Grosserfe92a932010-10-13 05:54:13 +0000836void RegionInfo::splitBlock(BasicBlock* NewBB, BasicBlock *OldBB)
837{
838 Region *R = getRegionFor(OldBB);
Tobias Grosser4b0986b62010-10-13 11:02:44 +0000839
Tobias Grosserfe92a932010-10-13 05:54:13 +0000840 setRegionFor(NewBB, R);
841
Tobias Grosser4b0986b62010-10-13 11:02:44 +0000842 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
Tobias Grosserfe92a932010-10-13 05:54:13 +0000843 R->replaceEntry(NewBB);
844 R = R->getParent();
845 }
846
847 setRegionFor(OldBB, R);
848}
849
Tobias Grosser336734a2010-07-22 07:46:31 +0000850char RegionInfo::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000851INITIALIZE_PASS_BEGIN(RegionInfo, "regions",
852 "Detect single entry single exit regions", true, true)
Chandler Carruth73523022014-01-13 13:07:17 +0000853INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000854INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
855INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
856INITIALIZE_PASS_END(RegionInfo, "regions",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000857 "Detect single entry single exit regions", true, true)
Tobias Grosser336734a2010-07-22 07:46:31 +0000858
859// Create methods available outside of this file, to use them
860// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
861// the link time optimization.
862
863namespace llvm {
864 FunctionPass *createRegionInfoPass() {
865 return new RegionInfo();
866 }
867}
868