1336734acSTobias Grosser //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2336734acSTobias Grosser //
3336734acSTobias Grosser //                     The LLVM Compiler Infrastructure
4336734acSTobias Grosser //
5336734acSTobias Grosser // This file is distributed under the University of Illinois Open Source
6336734acSTobias Grosser // License. See LICENSE.TXT for details.
7336734acSTobias Grosser //
8336734acSTobias Grosser //===----------------------------------------------------------------------===//
9336734acSTobias Grosser // Detects single entry single exit regions in the control flow graph.
10336734acSTobias Grosser //===----------------------------------------------------------------------===//
11336734acSTobias Grosser 
12336734acSTobias Grosser #include "llvm/Analysis/RegionInfo.h"
13336734acSTobias Grosser #include "llvm/Analysis/RegionIterator.h"
14336734acSTobias Grosser 
15336734acSTobias Grosser #include "llvm/ADT/PostOrderIterator.h"
16336734acSTobias Grosser #include "llvm/ADT/Statistic.h"
17336734acSTobias Grosser #include "llvm/Support/CommandLine.h"
18336734acSTobias Grosser #include "llvm/Support/ErrorHandling.h"
19336734acSTobias Grosser #include "llvm/Support/raw_ostream.h"
20336734acSTobias Grosser 
21336734acSTobias Grosser #define DEBUG_TYPE "region"
22336734acSTobias Grosser #include "llvm/Support/Debug.h"
23336734acSTobias Grosser 
24336734acSTobias Grosser #include <set>
25336734acSTobias Grosser #include <algorithm>
26336734acSTobias Grosser 
27336734acSTobias Grosser using namespace llvm;
28336734acSTobias Grosser 
29336734acSTobias Grosser // Always verify if expensive checking is enabled.
30336734acSTobias Grosser #ifdef XDEBUG
31336734acSTobias Grosser bool VerifyRegionInfo = true;
32336734acSTobias Grosser #else
33336734acSTobias Grosser bool VerifyRegionInfo = false;
34336734acSTobias Grosser #endif
35336734acSTobias Grosser 
36336734acSTobias Grosser static cl::opt<bool,true>
37336734acSTobias Grosser VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo),
38336734acSTobias Grosser                 cl::desc("Verify region info (time consuming)"));
39336734acSTobias Grosser 
40336734acSTobias Grosser STATISTIC(numRegions,       "The # of regions");
41336734acSTobias Grosser STATISTIC(numSimpleRegions, "The # of simple regions");
42336734acSTobias Grosser 
43336734acSTobias Grosser //===----------------------------------------------------------------------===//
44336734acSTobias Grosser /// PrintStyle - Print region in difference ways.
45336734acSTobias Grosser enum PrintStyle { PrintNone, PrintBB, PrintRN  };
46336734acSTobias Grosser 
47336734acSTobias Grosser cl::opt<enum PrintStyle> printStyle("print-region-style", cl::Hidden,
48336734acSTobias Grosser   cl::desc("style of printing regions"),
49336734acSTobias Grosser   cl::values(
50336734acSTobias Grosser     clEnumValN(PrintNone, "none",  "print no details"),
51336734acSTobias Grosser     clEnumValN(PrintBB, "bb",  "print regions in detail with block_iterator"),
52336734acSTobias Grosser     clEnumValN(PrintRN, "rn",  "print regions in detail with element_iterator"),
53336734acSTobias Grosser     clEnumValEnd));
54336734acSTobias Grosser //===----------------------------------------------------------------------===//
55336734acSTobias Grosser /// Region Implementation
56336734acSTobias Grosser Region::Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo* RInfo,
57336734acSTobias Grosser                DominatorTree *dt, Region *Parent)
58336734acSTobias Grosser                : RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
59336734acSTobias Grosser 
60336734acSTobias Grosser Region::~Region() {
61336734acSTobias Grosser   // Only clean the cache for this Region. Caches of child Regions will be
62336734acSTobias Grosser   // cleaned when the child Regions are deleted.
63336734acSTobias Grosser   BBNodeMap.clear();
64336734acSTobias Grosser 
65336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I)
66336734acSTobias Grosser     delete *I;
67336734acSTobias Grosser }
68336734acSTobias Grosser 
69336734acSTobias Grosser bool Region::contains(const BasicBlock *B) const {
70336734acSTobias Grosser   BasicBlock *BB = const_cast<BasicBlock*>(B);
71336734acSTobias Grosser 
72336734acSTobias Grosser   assert(DT->getNode(BB) && "BB not part of the dominance tree");
73336734acSTobias Grosser 
74336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
75336734acSTobias Grosser 
76336734acSTobias Grosser   // Toplevel region.
77336734acSTobias Grosser   if (!exit)
78336734acSTobias Grosser     return true;
79336734acSTobias Grosser 
80336734acSTobias Grosser   return (DT->dominates(entry, BB)
81336734acSTobias Grosser     && !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
82336734acSTobias Grosser }
83336734acSTobias Grosser 
84336734acSTobias Grosser bool Region::isSimple() const {
85336734acSTobias Grosser   bool isSimple = true;
86336734acSTobias Grosser   bool found = false;
87336734acSTobias Grosser 
88336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
89336734acSTobias Grosser 
90336734acSTobias Grosser   // TopLevelRegion
91336734acSTobias Grosser   if (!exit)
92336734acSTobias Grosser     return false;
93336734acSTobias Grosser 
94336734acSTobias Grosser   for (pred_iterator PI = pred_begin(entry), PE = pred_end(entry); PI != PE;
95336734acSTobias Grosser        ++PI)
96336734acSTobias Grosser     if (!contains(*PI)) {
97336734acSTobias Grosser       if (found) {
98336734acSTobias Grosser         isSimple = false;
99336734acSTobias Grosser         break;
100336734acSTobias Grosser       }
101336734acSTobias Grosser       found = true;
102336734acSTobias Grosser     }
103336734acSTobias Grosser 
104336734acSTobias Grosser   found = false;
105336734acSTobias Grosser 
106336734acSTobias Grosser   for (pred_iterator PI = pred_begin(exit), PE = pred_end(exit); PI != PE;
107336734acSTobias Grosser        ++PI)
108336734acSTobias Grosser     if (contains(*PI)) {
109336734acSTobias Grosser       if (found) {
110336734acSTobias Grosser         isSimple = false;
111336734acSTobias Grosser         break;
112336734acSTobias Grosser       }
113336734acSTobias Grosser       found = true;
114336734acSTobias Grosser     }
115336734acSTobias Grosser 
116336734acSTobias Grosser   return isSimple;
117336734acSTobias Grosser }
118336734acSTobias Grosser 
119336734acSTobias Grosser void Region::verifyBBInRegion(BasicBlock *BB) const {
120336734acSTobias Grosser   if (!contains(BB))
121336734acSTobias Grosser     llvm_unreachable("Broken region found!");
122336734acSTobias Grosser 
123336734acSTobias Grosser   BasicBlock *entry = getEntry(), *exit = getExit();
124336734acSTobias Grosser 
125336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
126336734acSTobias Grosser     if (!contains(*SI) && exit != *SI)
127336734acSTobias Grosser       llvm_unreachable("Broken region found!");
128336734acSTobias Grosser 
129336734acSTobias Grosser   if (entry != BB)
130336734acSTobias Grosser     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); SI != SE; ++SI)
131336734acSTobias Grosser       if (!contains(*SI))
132336734acSTobias Grosser         llvm_unreachable("Broken region found!");
133336734acSTobias Grosser }
134336734acSTobias Grosser 
135336734acSTobias Grosser void Region::verifyWalk(BasicBlock *BB, std::set<BasicBlock*> *visited) const {
136336734acSTobias Grosser   BasicBlock *exit = getExit();
137336734acSTobias Grosser 
138336734acSTobias Grosser   visited->insert(BB);
139336734acSTobias Grosser 
140336734acSTobias Grosser   verifyBBInRegion(BB);
141336734acSTobias Grosser 
142336734acSTobias Grosser   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
143336734acSTobias Grosser     if (*SI != exit && visited->find(*SI) == visited->end())
144336734acSTobias Grosser         verifyWalk(*SI, visited);
145336734acSTobias Grosser }
146336734acSTobias Grosser 
147336734acSTobias Grosser void Region::verifyRegion() const {
148336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive
149336734acSTobias Grosser   // check will be invoked by PassManager.
150336734acSTobias Grosser   if (!VerifyRegionInfo) return;
151336734acSTobias Grosser 
152336734acSTobias Grosser   std::set<BasicBlock*> visited;
153336734acSTobias Grosser   verifyWalk(getEntry(), &visited);
154336734acSTobias Grosser }
155336734acSTobias Grosser 
156336734acSTobias Grosser void Region::verifyRegionNest() const {
157336734acSTobias Grosser   for (Region::const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
158336734acSTobias Grosser     (*RI)->verifyRegionNest();
159336734acSTobias Grosser 
160336734acSTobias Grosser   verifyRegion();
161336734acSTobias Grosser }
162336734acSTobias Grosser 
163336734acSTobias Grosser Region::block_iterator Region::block_begin() {
164336734acSTobias Grosser   return GraphTraits<FlatIt<Region*> >::nodes_begin(this);
165336734acSTobias Grosser }
166336734acSTobias Grosser 
167336734acSTobias Grosser Region::block_iterator Region::block_end() {
168336734acSTobias Grosser   return GraphTraits<FlatIt<Region*> >::nodes_end(this);
169336734acSTobias Grosser }
170336734acSTobias Grosser 
171336734acSTobias Grosser Region::const_block_iterator Region::block_begin() const {
172336734acSTobias Grosser   return GraphTraits<FlatIt<const Region*> >::nodes_begin(this);
173336734acSTobias Grosser }
174336734acSTobias Grosser 
175336734acSTobias Grosser Region::const_block_iterator Region::block_end() const {
176336734acSTobias Grosser   return GraphTraits<FlatIt<const Region*> >::nodes_end(this);
177336734acSTobias Grosser }
178336734acSTobias Grosser 
179336734acSTobias Grosser Region::element_iterator Region::element_begin() {
180336734acSTobias Grosser   return GraphTraits<Region*>::nodes_begin(this);
181336734acSTobias Grosser }
182336734acSTobias Grosser 
183336734acSTobias Grosser Region::element_iterator Region::element_end() {
184336734acSTobias Grosser   return GraphTraits<Region*>::nodes_end(this);
185336734acSTobias Grosser }
186336734acSTobias Grosser 
187336734acSTobias Grosser Region::const_element_iterator Region::element_begin() const {
188336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_begin(this);
189336734acSTobias Grosser }
190336734acSTobias Grosser 
191336734acSTobias Grosser Region::const_element_iterator Region::element_end() const {
192336734acSTobias Grosser   return GraphTraits<const Region*>::nodes_end(this);
193336734acSTobias Grosser }
194336734acSTobias Grosser 
195336734acSTobias Grosser Region* Region::getSubRegionNode(BasicBlock *BB) const {
196336734acSTobias Grosser   Region *R = RI->getRegionFor(BB);
197336734acSTobias Grosser 
198336734acSTobias Grosser   if (!R || R == this)
199336734acSTobias Grosser     return 0;
200336734acSTobias Grosser 
201336734acSTobias Grosser   // If we pass the BB out of this region, that means our code is broken.
202336734acSTobias Grosser   assert(contains(R) && "BB not in current region!");
203336734acSTobias Grosser 
204336734acSTobias Grosser   while (contains(R->getParent()) && R->getParent() != this)
205336734acSTobias Grosser     R = R->getParent();
206336734acSTobias Grosser 
207336734acSTobias Grosser   if (R->getEntry() != BB)
208336734acSTobias Grosser     return 0;
209336734acSTobias Grosser 
210336734acSTobias Grosser   return R;
211336734acSTobias Grosser }
212336734acSTobias Grosser 
213336734acSTobias Grosser RegionNode* Region::getBBNode(BasicBlock *BB) const {
214336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
215336734acSTobias Grosser 
216336734acSTobias Grosser   BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
217336734acSTobias Grosser 
218336734acSTobias Grosser   if (at != BBNodeMap.end())
219336734acSTobias Grosser     return at->second;
220336734acSTobias Grosser 
221336734acSTobias Grosser   RegionNode *NewNode = new RegionNode(const_cast<Region*>(this), BB);
222336734acSTobias Grosser   BBNodeMap.insert(std::make_pair(BB, NewNode));
223336734acSTobias Grosser   return NewNode;
224336734acSTobias Grosser }
225336734acSTobias Grosser 
226336734acSTobias Grosser RegionNode* Region::getNode(BasicBlock *BB) const {
227336734acSTobias Grosser   assert(contains(BB) && "Can get BB node out of this region!");
228336734acSTobias Grosser   if (Region* Child = getSubRegionNode(BB))
229336734acSTobias Grosser     return Child->getNode();
230336734acSTobias Grosser 
231336734acSTobias Grosser   return getBBNode(BB);
232336734acSTobias Grosser }
233336734acSTobias Grosser 
234336734acSTobias Grosser void Region::transferChildrenTo(Region *To) {
235336734acSTobias Grosser   for (iterator I = begin(), E = end(); I != E; ++I) {
236336734acSTobias Grosser     (*I)->parent = To;
237336734acSTobias Grosser     To->children.push_back(*I);
238336734acSTobias Grosser   }
239336734acSTobias Grosser   children.clear();
240336734acSTobias Grosser }
241336734acSTobias Grosser 
242336734acSTobias Grosser void Region::addSubRegion(Region *SubRegion) {
243336734acSTobias Grosser   assert(SubRegion->parent == 0 && "SubRegion already has a parent!");
244336734acSTobias Grosser   SubRegion->parent = this;
245336734acSTobias Grosser   // Set up the region node.
246336734acSTobias Grosser   assert(std::find(children.begin(), children.end(), SubRegion) == children.end()
247336734acSTobias Grosser          && "Node already exist!");
248336734acSTobias Grosser   children.push_back(SubRegion);
249336734acSTobias Grosser }
250336734acSTobias Grosser 
251336734acSTobias Grosser 
252336734acSTobias Grosser Region *Region::removeSubRegion(Region *Child) {
253336734acSTobias Grosser   assert(Child->parent == this && "Child is not a child of this region!");
254336734acSTobias Grosser   Child->parent = 0;
255336734acSTobias Grosser   RegionSet::iterator I = std::find(children.begin(), children.end(), Child);
256336734acSTobias Grosser   assert(I != children.end() && "Region does not exit. Unable to remove.");
257336734acSTobias Grosser   children.erase(children.begin()+(I-begin()));
258336734acSTobias Grosser   return Child;
259336734acSTobias Grosser }
260336734acSTobias Grosser 
261336734acSTobias Grosser unsigned Region::getDepth() const {
262336734acSTobias Grosser   unsigned Depth = 0;
263336734acSTobias Grosser 
264336734acSTobias Grosser   for (Region *R = parent; R != 0; R = R->parent)
265336734acSTobias Grosser     ++Depth;
266336734acSTobias Grosser 
267336734acSTobias Grosser   return Depth;
268336734acSTobias Grosser }
269336734acSTobias Grosser 
270336734acSTobias Grosser void Region::print(raw_ostream &OS, bool print_tree, unsigned level) const {
271336734acSTobias Grosser   if (print_tree)
272336734acSTobias Grosser     OS.indent(level*2) << "[" << level << "] " << getNameStr();
273336734acSTobias Grosser   else
274336734acSTobias Grosser     OS.indent(level*2) << getNameStr();
275336734acSTobias Grosser 
276336734acSTobias Grosser   OS << "\n";
277336734acSTobias Grosser 
278336734acSTobias Grosser 
279336734acSTobias Grosser   if (printStyle != PrintNone) {
280336734acSTobias Grosser     OS.indent(level*2) << "{\n";
281336734acSTobias Grosser     OS.indent(level*2 + 2);
282336734acSTobias Grosser 
283336734acSTobias Grosser     if (printStyle == PrintBB) {
284336734acSTobias Grosser       for (const_block_iterator I = block_begin(), E = block_end(); I!=E; ++I)
285336734acSTobias Grosser         OS << **I << ", "; // TODO: remove the last ","
286336734acSTobias Grosser     } else if (printStyle == PrintRN) {
287336734acSTobias Grosser       for (const_element_iterator I = element_begin(), E = element_end(); I!=E; ++I)
288336734acSTobias Grosser         OS << **I << ", "; // TODO: remove the last ",
289336734acSTobias Grosser     }
290336734acSTobias Grosser 
291336734acSTobias Grosser     OS << "\n";
292336734acSTobias Grosser   }
293336734acSTobias Grosser 
294336734acSTobias Grosser   if (print_tree)
295336734acSTobias Grosser     for (const_iterator RI = begin(), RE = end(); RI != RE; ++RI)
296336734acSTobias Grosser       (*RI)->print(OS, print_tree, level+1);
297336734acSTobias Grosser 
298336734acSTobias Grosser   if (printStyle != PrintNone)
299336734acSTobias Grosser     OS.indent(level*2) << "} \n";
300336734acSTobias Grosser }
301336734acSTobias Grosser 
302336734acSTobias Grosser void Region::dump() const {
303336734acSTobias Grosser   print(dbgs(), true, getDepth());
304336734acSTobias Grosser }
305336734acSTobias Grosser 
306336734acSTobias Grosser void Region::clearNodeCache() {
307336734acSTobias Grosser   BBNodeMap.clear();
308336734acSTobias Grosser   for (Region::iterator RI = begin(), RE = end(); RI != RE; ++RI)
309336734acSTobias Grosser     (*RI)->clearNodeCache();
310336734acSTobias Grosser }
311336734acSTobias Grosser 
312336734acSTobias Grosser //===----------------------------------------------------------------------===//
313336734acSTobias Grosser // RegionInfo implementation
314336734acSTobias Grosser //
315336734acSTobias Grosser 
316336734acSTobias Grosser bool RegionInfo::isCommonDomFrontier(BasicBlock *BB, BasicBlock *entry,
317336734acSTobias Grosser                                      BasicBlock *exit) const {
31807c8ad54SGabor Greif   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
31907c8ad54SGabor Greif     BasicBlock *P = *PI;
32007c8ad54SGabor Greif     if (DT->dominates(entry, P) && !DT->dominates(exit, P))
321336734acSTobias Grosser       return false;
32207c8ad54SGabor Greif   }
323336734acSTobias Grosser   return true;
324336734acSTobias Grosser }
325336734acSTobias Grosser 
326336734acSTobias Grosser bool RegionInfo::isRegion(BasicBlock *entry, BasicBlock *exit) const {
327336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
328336734acSTobias Grosser   typedef DominanceFrontier::DomSetType DST;
329336734acSTobias Grosser 
330d9f48ecbSGabor Greif   DST *entrySuccs = &DF->find(entry)->second;
331336734acSTobias Grosser 
332336734acSTobias Grosser   // Exit is the header of a loop that contains the entry. In this case,
333336734acSTobias Grosser   // the dominance frontier must only contain the exit.
334336734acSTobias Grosser   if (!DT->dominates(entry, exit)) {
335336734acSTobias Grosser     for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
336336734acSTobias Grosser          SI != SE; ++SI)
337336734acSTobias Grosser       if (*SI != exit && *SI != entry)
338336734acSTobias Grosser         return false;
339336734acSTobias Grosser 
340336734acSTobias Grosser     return true;
341336734acSTobias Grosser   }
342336734acSTobias Grosser 
343d9f48ecbSGabor Greif   DST *exitSuccs = &DF->find(exit)->second;
344336734acSTobias Grosser 
345336734acSTobias Grosser   // Do not allow edges leaving the region.
346336734acSTobias Grosser   for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
347336734acSTobias Grosser        SI != SE; ++SI) {
348336734acSTobias Grosser     if (*SI == exit || *SI == entry)
349336734acSTobias Grosser       continue;
350336734acSTobias Grosser     if (exitSuccs->find(*SI) == exitSuccs->end())
351336734acSTobias Grosser       return false;
352336734acSTobias Grosser     if (!isCommonDomFrontier(*SI, entry, exit))
353336734acSTobias Grosser       return false;
354336734acSTobias Grosser   }
355336734acSTobias Grosser 
356336734acSTobias Grosser   // Do not allow edges pointing into the region.
357336734acSTobias Grosser   for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
358336734acSTobias Grosser        SI != SE; ++SI)
359336734acSTobias Grosser     if (DT->dominates(entry, *SI) && *SI != entry && *SI != exit)
360336734acSTobias Grosser       return false;
361336734acSTobias Grosser 
362336734acSTobias Grosser 
363336734acSTobias Grosser   return true;
364336734acSTobias Grosser }
365336734acSTobias Grosser 
366336734acSTobias Grosser void RegionInfo::insertShortCut(BasicBlock *entry, BasicBlock *exit,
367336734acSTobias Grosser                              BBtoBBMap *ShortCut) const {
368336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
369336734acSTobias Grosser 
370336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(exit);
371336734acSTobias Grosser 
372336734acSTobias Grosser   if (e == ShortCut->end())
373336734acSTobias Grosser     // No further region at exit available.
374336734acSTobias Grosser     (*ShortCut)[entry] = exit;
375336734acSTobias Grosser   else {
376336734acSTobias Grosser     // We found a region e that starts at exit. Therefore (entry, e->second)
377336734acSTobias Grosser     // is also a region, that is larger than (entry, exit). Insert the
378336734acSTobias Grosser     // larger one.
379336734acSTobias Grosser     BasicBlock *BB = e->second;
380336734acSTobias Grosser     (*ShortCut)[entry] = BB;
381336734acSTobias Grosser   }
382336734acSTobias Grosser }
383336734acSTobias Grosser 
384336734acSTobias Grosser DomTreeNode* RegionInfo::getNextPostDom(DomTreeNode* N,
385336734acSTobias Grosser                                         BBtoBBMap *ShortCut) const {
386336734acSTobias Grosser   BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
387336734acSTobias Grosser 
388336734acSTobias Grosser   if (e == ShortCut->end())
389336734acSTobias Grosser     return N->getIDom();
390336734acSTobias Grosser 
391336734acSTobias Grosser   return PDT->getNode(e->second)->getIDom();
392336734acSTobias Grosser }
393336734acSTobias Grosser 
394336734acSTobias Grosser bool RegionInfo::isTrivialRegion(BasicBlock *entry, BasicBlock *exit) const {
395336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
396336734acSTobias Grosser 
397336734acSTobias Grosser   unsigned num_successors = succ_end(entry) - succ_begin(entry);
398336734acSTobias Grosser 
399336734acSTobias Grosser   if (num_successors <= 1 && exit == *(succ_begin(entry)))
400336734acSTobias Grosser     return true;
401336734acSTobias Grosser 
402336734acSTobias Grosser   return false;
403336734acSTobias Grosser }
404336734acSTobias Grosser 
405336734acSTobias Grosser void RegionInfo::updateStatistics(Region *R) {
406336734acSTobias Grosser   ++numRegions;
407336734acSTobias Grosser 
408336734acSTobias Grosser   // TODO: Slow. Should only be enabled if -stats is used.
409336734acSTobias Grosser   if (R->isSimple()) ++numSimpleRegions;
410336734acSTobias Grosser }
411336734acSTobias Grosser 
412336734acSTobias Grosser Region *RegionInfo::createRegion(BasicBlock *entry, BasicBlock *exit) {
413336734acSTobias Grosser   assert(entry && exit && "entry and exit must not be null!");
414336734acSTobias Grosser 
415336734acSTobias Grosser   if (isTrivialRegion(entry, exit))
416336734acSTobias Grosser     return 0;
417336734acSTobias Grosser 
418336734acSTobias Grosser   Region *region = new Region(entry, exit, this, DT);
419336734acSTobias Grosser   BBtoRegion.insert(std::make_pair(entry, region));
420336734acSTobias Grosser 
421336734acSTobias Grosser  #ifdef XDEBUG
422336734acSTobias Grosser     region->verifyRegion();
423336734acSTobias Grosser  #else
424336734acSTobias Grosser     DEBUG(region->verifyRegion());
425336734acSTobias Grosser  #endif
426336734acSTobias Grosser 
427336734acSTobias Grosser   updateStatistics(region);
428336734acSTobias Grosser   return region;
429336734acSTobias Grosser }
430336734acSTobias Grosser 
431336734acSTobias Grosser void RegionInfo::findRegionsWithEntry(BasicBlock *entry, BBtoBBMap *ShortCut) {
432336734acSTobias Grosser   assert(entry);
433336734acSTobias Grosser 
434336734acSTobias Grosser   DomTreeNode *N = PDT->getNode(entry);
435336734acSTobias Grosser 
436336734acSTobias Grosser   if (!N)
437336734acSTobias Grosser     return;
438336734acSTobias Grosser 
439336734acSTobias Grosser   Region *lastRegion= 0;
440336734acSTobias Grosser   BasicBlock *lastExit = entry;
441336734acSTobias Grosser 
442336734acSTobias Grosser   // As only a BasicBlock that postdominates entry can finish a region, walk the
443336734acSTobias Grosser   // post dominance tree upwards.
444336734acSTobias Grosser   while ((N = getNextPostDom(N, ShortCut))) {
445336734acSTobias Grosser     BasicBlock *exit = N->getBlock();
446336734acSTobias Grosser 
447336734acSTobias Grosser     if (!exit)
448336734acSTobias Grosser       break;
449336734acSTobias Grosser 
450336734acSTobias Grosser     if (isRegion(entry, exit)) {
451336734acSTobias Grosser       Region *newRegion = createRegion(entry, exit);
452336734acSTobias Grosser 
453336734acSTobias Grosser       if (lastRegion)
454336734acSTobias Grosser         newRegion->addSubRegion(lastRegion);
455336734acSTobias Grosser 
456336734acSTobias Grosser       lastRegion = newRegion;
457336734acSTobias Grosser       lastExit = exit;
458336734acSTobias Grosser     }
459336734acSTobias Grosser 
460336734acSTobias Grosser     // This can never be a region, so stop the search.
461336734acSTobias Grosser     if (!DT->dominates(entry, exit))
462336734acSTobias Grosser       break;
463336734acSTobias Grosser   }
464336734acSTobias Grosser 
465336734acSTobias Grosser   // Tried to create regions from entry to lastExit.  Next time take a
466336734acSTobias Grosser   // shortcut from entry to lastExit.
467336734acSTobias Grosser   if (lastExit != entry)
468336734acSTobias Grosser     insertShortCut(entry, lastExit, ShortCut);
469336734acSTobias Grosser }
470336734acSTobias Grosser 
471336734acSTobias Grosser void RegionInfo::scanForRegions(Function &F, BBtoBBMap *ShortCut) {
472336734acSTobias Grosser   BasicBlock *entry = &(F.getEntryBlock());
473336734acSTobias Grosser   DomTreeNode *N = DT->getNode(entry);
474336734acSTobias Grosser 
475336734acSTobias Grosser   // Iterate over the dominance tree in post order to start with the small
476336734acSTobias Grosser   // regions from the bottom of the dominance tree.  If the small regions are
477336734acSTobias Grosser   // detected first, detection of bigger regions is faster, as we can jump
478336734acSTobias Grosser   // over the small regions.
479336734acSTobias Grosser   for (po_iterator<DomTreeNode*> FI = po_begin(N), FE = po_end(N); FI != FE;
480336734acSTobias Grosser     ++FI) {
481*1a2da423SGabor Greif     findRegionsWithEntry(FI->getBlock(), ShortCut);
482336734acSTobias Grosser   }
483336734acSTobias Grosser }
484336734acSTobias Grosser 
485336734acSTobias Grosser Region *RegionInfo::getTopMostParent(Region *region) {
486336734acSTobias Grosser   while (region->parent)
487336734acSTobias Grosser     region = region->getParent();
488336734acSTobias Grosser 
489336734acSTobias Grosser   return region;
490336734acSTobias Grosser }
491336734acSTobias Grosser 
492336734acSTobias Grosser void RegionInfo::buildRegionsTree(DomTreeNode *N, Region *region) {
493336734acSTobias Grosser   BasicBlock *BB = N->getBlock();
494336734acSTobias Grosser 
495336734acSTobias Grosser   // Passed region exit
496336734acSTobias Grosser   while (BB == region->getExit())
497336734acSTobias Grosser     region = region->getParent();
498336734acSTobias Grosser 
499336734acSTobias Grosser   BBtoRegionMap::iterator it = BBtoRegion.find(BB);
500336734acSTobias Grosser 
501336734acSTobias Grosser   // This basic block is a start block of a region. It is already in the
502336734acSTobias Grosser   // BBtoRegion relation. Only the child basic blocks have to be updated.
503336734acSTobias Grosser   if (it != BBtoRegion.end()) {
504336734acSTobias Grosser     Region *newRegion = it->second;;
505336734acSTobias Grosser     region->addSubRegion(getTopMostParent(newRegion));
506336734acSTobias Grosser     region = newRegion;
507336734acSTobias Grosser   } else {
508336734acSTobias Grosser     BBtoRegion[BB] = region;
509336734acSTobias Grosser   }
510336734acSTobias Grosser 
511336734acSTobias Grosser   for (DomTreeNode::iterator CI = N->begin(), CE = N->end(); CI != CE; ++CI)
512336734acSTobias Grosser     buildRegionsTree(*CI, region);
513336734acSTobias Grosser }
514336734acSTobias Grosser 
515336734acSTobias Grosser void RegionInfo::releaseMemory() {
516336734acSTobias Grosser   BBtoRegion.clear();
517336734acSTobias Grosser   if (TopLevelRegion)
518336734acSTobias Grosser     delete TopLevelRegion;
519336734acSTobias Grosser   TopLevelRegion = 0;
520336734acSTobias Grosser }
521336734acSTobias Grosser 
522336734acSTobias Grosser RegionInfo::RegionInfo() : FunctionPass(&ID) {
523336734acSTobias Grosser   TopLevelRegion = 0;
524336734acSTobias Grosser }
525336734acSTobias Grosser 
526336734acSTobias Grosser RegionInfo::~RegionInfo() {
527336734acSTobias Grosser   releaseMemory();
528336734acSTobias Grosser }
529336734acSTobias Grosser 
530336734acSTobias Grosser void RegionInfo::Calculate(Function &F) {
531336734acSTobias Grosser   // ShortCut a function where for every BB the exit of the largest region
532336734acSTobias Grosser   // starting with BB is stored. These regions can be threated as single BBS.
533336734acSTobias Grosser   // This improves performance on linear CFGs.
534336734acSTobias Grosser   BBtoBBMap ShortCut;
535336734acSTobias Grosser 
536336734acSTobias Grosser   scanForRegions(F, &ShortCut);
537336734acSTobias Grosser   BasicBlock *BB = &F.getEntryBlock();
538336734acSTobias Grosser   buildRegionsTree(DT->getNode(BB), TopLevelRegion);
539336734acSTobias Grosser }
540336734acSTobias Grosser 
541336734acSTobias Grosser bool RegionInfo::runOnFunction(Function &F) {
542336734acSTobias Grosser   releaseMemory();
543336734acSTobias Grosser 
544336734acSTobias Grosser   DT = &getAnalysis<DominatorTree>();
545336734acSTobias Grosser   PDT = &getAnalysis<PostDominatorTree>();
546336734acSTobias Grosser   DF = &getAnalysis<DominanceFrontier>();
547336734acSTobias Grosser 
548336734acSTobias Grosser   TopLevelRegion = new Region(&F.getEntryBlock(), 0, this, DT, 0);
549336734acSTobias Grosser   updateStatistics(TopLevelRegion);
550336734acSTobias Grosser 
551336734acSTobias Grosser   Calculate(F);
552336734acSTobias Grosser 
553336734acSTobias Grosser   return false;
554336734acSTobias Grosser }
555336734acSTobias Grosser 
556336734acSTobias Grosser void RegionInfo::getAnalysisUsage(AnalysisUsage &AU) const {
557336734acSTobias Grosser   AU.setPreservesAll();
558336734acSTobias Grosser   AU.addRequiredTransitive<DominatorTree>();
559336734acSTobias Grosser   AU.addRequired<PostDominatorTree>();
560336734acSTobias Grosser   AU.addRequired<DominanceFrontier>();
561336734acSTobias Grosser }
562336734acSTobias Grosser 
563336734acSTobias Grosser void RegionInfo::print(raw_ostream &OS, const Module *) const {
564336734acSTobias Grosser   OS << "Region tree:\n";
565336734acSTobias Grosser   TopLevelRegion->print(OS, true, 0);
566336734acSTobias Grosser   OS << "End region tree\n";
567336734acSTobias Grosser }
568336734acSTobias Grosser 
569336734acSTobias Grosser void RegionInfo::verifyAnalysis() const {
570336734acSTobias Grosser   // Only do verification when user wants to, otherwise this expensive check
571336734acSTobias Grosser   // will be invoked by PMDataManager::verifyPreservedAnalysis when
572336734acSTobias Grosser   // a regionpass (marked PreservedAll) finish.
573336734acSTobias Grosser   if (!VerifyRegionInfo) return;
574336734acSTobias Grosser 
575336734acSTobias Grosser   TopLevelRegion->verifyRegionNest();
576336734acSTobias Grosser }
577336734acSTobias Grosser 
578336734acSTobias Grosser // Region pass manager support.
579336734acSTobias Grosser Region *RegionInfo::getRegionFor(BasicBlock *BB) const {
580336734acSTobias Grosser   BBtoRegionMap::const_iterator I=
581336734acSTobias Grosser     BBtoRegion.find(BB);
582336734acSTobias Grosser   return I != BBtoRegion.end() ? I->second : 0;
583336734acSTobias Grosser }
584336734acSTobias Grosser 
585336734acSTobias Grosser Region *RegionInfo::operator[](BasicBlock *BB) const {
586336734acSTobias Grosser   return getRegionFor(BB);
587336734acSTobias Grosser }
588336734acSTobias Grosser 
589336734acSTobias Grosser Region*
590336734acSTobias Grosser RegionInfo::getCommonRegion(Region *A, Region *B) const {
591336734acSTobias Grosser   assert (A && B && "One of the Regions is NULL");
592336734acSTobias Grosser 
593336734acSTobias Grosser   if (A->contains(B)) return A;
594336734acSTobias Grosser 
595336734acSTobias Grosser   while (!B->contains(A))
596336734acSTobias Grosser     B = B->getParent();
597336734acSTobias Grosser 
598336734acSTobias Grosser   return B;
599336734acSTobias Grosser }
600336734acSTobias Grosser 
601336734acSTobias Grosser Region*
602336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<Region*> &Regions) const {
603336734acSTobias Grosser   Region* ret = Regions.back();
604336734acSTobias Grosser   Regions.pop_back();
605336734acSTobias Grosser 
606336734acSTobias Grosser   for (SmallVectorImpl<Region*>::const_iterator I = Regions.begin(),
607336734acSTobias Grosser        E = Regions.end(); I != E; ++I)
608336734acSTobias Grosser       ret = getCommonRegion(ret, *I);
609336734acSTobias Grosser 
610336734acSTobias Grosser   return ret;
611336734acSTobias Grosser }
612336734acSTobias Grosser 
613336734acSTobias Grosser Region*
614336734acSTobias Grosser RegionInfo::getCommonRegion(SmallVectorImpl<BasicBlock*> &BBs) const {
615336734acSTobias Grosser   Region* ret = getRegionFor(BBs.back());
616336734acSTobias Grosser   BBs.pop_back();
617336734acSTobias Grosser 
618336734acSTobias Grosser   for (SmallVectorImpl<BasicBlock*>::const_iterator I = BBs.begin(),
619336734acSTobias Grosser        E = BBs.end(); I != E; ++I)
620336734acSTobias Grosser       ret = getCommonRegion(ret, getRegionFor(*I));
621336734acSTobias Grosser 
622336734acSTobias Grosser   return ret;
623336734acSTobias Grosser }
624336734acSTobias Grosser 
625336734acSTobias Grosser char RegionInfo::ID = 0;
626336734acSTobias Grosser INITIALIZE_PASS(RegionInfo, "regions",
627336734acSTobias Grosser                 "Detect single entry single exit regions", true, true);
628336734acSTobias Grosser 
629336734acSTobias Grosser // Create methods available outside of this file, to use them
630336734acSTobias Grosser // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
631336734acSTobias Grosser // the link time optimization.
632336734acSTobias Grosser 
633336734acSTobias Grosser namespace llvm {
634336734acSTobias Grosser   FunctionPass *createRegionInfoPass() {
635336734acSTobias Grosser     return new RegionInfo();
636336734acSTobias Grosser   }
637336734acSTobias Grosser }
638336734acSTobias Grosser 
639