12 #define DEBUG_TYPE "region"
36 cl::desc(
"Verify region info (time consuming)"));
38 STATISTIC(numRegions,
"The # of regions");
39 STATISTIC(numSimpleRegions,
"The # of simple regions");
43 cl::desc(
"style of printing regions"),
47 "print regions in detail with block_iterator"),
49 "print regions in detail with element_iterator"),
55 :
RegionNode(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
59 for (BBNodeMapT::iterator it = BBNodeMap.begin(),
60 ie = BBNodeMap.end(); it != ie; ++it)
76 assert(exit &&
"No exit to replace!");
81 std::vector<Region *> RegionQueue;
84 RegionQueue.push_back(
this);
85 while (!RegionQueue.empty()) {
86 Region *R = RegionQueue.back();
87 RegionQueue.pop_back();
91 if ((*RI)->getEntry() == OldEntry)
92 RegionQueue.push_back(*RI);
97 std::vector<Region *> RegionQueue;
100 RegionQueue.push_back(
this);
101 while (!RegionQueue.empty()) {
102 Region *R = RegionQueue.back();
103 RegionQueue.pop_back();
107 if ((*RI)->getExit() == OldExit)
108 RegionQueue.push_back(*RI);
142 BE = ExitingBlocks.
end(); BI != BE; ++BI)
161 assert(LI && BB &&
"LI and BB cannot be null!");
178 enteringBlock = Pred;
182 return enteringBlock;
212 std::string exitName;
213 std::string entryName;
230 exitName =
"<Function Return>";
232 return entryName +
" => " + exitName;
235 void Region::verifyBBInRegion(
BasicBlock *BB)
const {
251 void Region::verifyWalk(
BasicBlock *BB, std::set<BasicBlock*> *visited)
const {
256 verifyBBInRegion(BB);
259 if (*SI != exit && visited->find(*SI) == visited->
end())
260 verifyWalk(*SI, visited);
268 std::set<BasicBlock*> visited;
272 void Region::verifyRegionNest()
const {
274 (*RI)->verifyRegionNest();
302 assert(
contains(R) &&
"BB not in current region!");
314 assert(
contains(BB) &&
"Can get BB node out of this region!");
316 BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
318 if (at != BBNodeMap.end())
322 BBNodeMap.insert(std::make_pair(BB, NewNode));
327 assert(
contains(BB) &&
"Can get BB node out of this region!");
329 return Child->getNode();
337 To->children.push_back(*
I);
343 assert(SubRegion->
parent == 0 &&
"SubRegion already has a parent!");
344 assert(std::find(
begin(),
end(), SubRegion) == children.end()
345 &&
"Subregion already exists!");
348 children.push_back(SubRegion);
353 assert(SubRegion->children.size() == 0
354 &&
"SubRegions that contain children are not supported");
357 if (!(*I)->isSubRegion()) {
364 std::vector<Region*> Keep;
366 if (SubRegion->
contains(*
I) && *
I != SubRegion) {
367 SubRegion->children.push_back(*
I);
368 (*I)->parent = SubRegion;
373 children.insert(children.begin(), Keep.begin(), Keep.end());
378 assert(Child->
parent ==
this &&
"Child is not a child of this region!");
380 RegionSet::iterator
I = std::find(children.begin(), children.end(), Child);
381 assert(I != children.end() &&
"Region does not exit. Unable to remove.");
382 children.erase(children.begin()+(I-
begin()));
398 if (NumSuccessors == 0)
438 OS.
indent(level*2) <<
"{\n";
443 OS << (*I)->getName() <<
", ";
454 (*RI)->print(OS, print_tree, level+1, Style);
457 OS.
indent(level*2) <<
"} \n";
460 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
468 for (BBNodeMapT::iterator
I = BBNodeMap.begin(),
469 IE = BBNodeMap.end();
I !=
IE; ++
I)
474 (*RI)->clearNodeCache();
492 assert(entry && exit &&
"entry and exit must not be null!");
495 DST *entrySuccs = &
DF->find(entry)->second;
500 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
502 if (*SI != exit && *SI != entry)
508 DST *exitSuccs = &
DF->find(exit)->second;
511 for (DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
513 if (*SI == exit || *SI == entry)
515 if (exitSuccs->find(*SI) == exitSuccs->end())
517 if (!isCommonDomFrontier(*SI, entry, exit))
522 for (DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
532 BBtoBBMap *ShortCut)
const {
533 assert(entry && exit &&
"entry and exit must not be null!");
537 if (e == ShortCut->end())
539 (*ShortCut)[entry] = exit;
545 (*ShortCut)[entry] = BB;
550 BBtoBBMap *ShortCut)
const {
553 if (e == ShortCut->end())
560 assert(entry && exit &&
"entry and exit must not be null!");
564 if (num_successors <= 1 && exit == *(
succ_begin(entry)))
570 void RegionInfo::updateStatistics(
Region *R) {
574 if (R->
isSimple()) ++numSimpleRegions;
578 assert(entry && exit &&
"entry and exit must not be null!");
580 if (isTrivialRegion(entry, exit))
584 BBtoRegion.
insert(std::make_pair(entry, region));
592 updateStatistics(region);
596 void RegionInfo::findRegionsWithEntry(
BasicBlock *entry, BBtoBBMap *ShortCut) {
609 while ((N = getNextPostDom(N, ShortCut))) {
615 if (isRegion(entry, exit)) {
616 Region *newRegion = createRegion(entry, exit);
621 lastRegion = newRegion;
632 if (lastExit != entry)
633 insertShortCut(entry, lastExit, ShortCut);
636 void RegionInfo::scanForRegions(
Function &
F, BBtoBBMap *ShortCut) {
646 findRegionsWithEntry(FI->getBlock(), ShortCut);
661 while (BB == region->
getExit())
668 if (it != BBtoRegion.
end()) {
669 Region *newRegion = it->second;
673 BBtoRegion[BB] = region;
677 buildRegionsTree(*CI, region);
680 void RegionInfo::releaseMemory() {
683 delete TopLevelRegion;
702 scanForRegions(F, &ShortCut);
704 buildRegionsTree(DT->
getNode(BB), TopLevelRegion);
710 DT = &getAnalysis<DominatorTree>();
711 PDT = &getAnalysis<PostDominatorTree>();
712 DF = &getAnalysis<DominanceFrontier>();
715 updateStatistics(TopLevelRegion);
730 OS <<
"Region tree:\n";
732 OS <<
"End region tree\n";
741 TopLevelRegion->verifyRegionNest();
748 return I != BBtoRegion.
end() ? I->second : 0;
799 assert (A && B &&
"One of the Regions is NULL");
815 E = Regions.
end();
I != E; ++
I)
827 E = BBs.
end();
I != E; ++
I)
849 "Detect single entry single exit regions",
true,
true)
854 "Detect single entry single exit regions",
true, true)
862 return new RegionInfo();
bool contains(const BasicBlock *BB) const
Check if the region contains a BasicBlock.
DomTreeNode * getNode(BasicBlock *BB) const
unsigned getDepth() const
Get the nesting level of this Region.
static PassRegistry * getPassRegistry()
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, enum PrintStyle Style=PrintNone) const
Print the region.
static cl::opt< enum Region::PrintStyle > printStyle("print-region-style", cl::Hidden, cl::desc("style of printing regions"), cl::values(clEnumValN(Region::PrintNone,"none","print no details"), clEnumValN(Region::PrintBB,"bb","print regions in detail with block_iterator"), clEnumValN(Region::PrintRN,"rn","print regions in detail with element_iterator"), clEnumValEnd))
std::string getNameStr() const
Returns the name of the Region.
The main container class for the LLVM Intermediate Representation.
Region * getExpandedRegion() const
Return a new (non canonical) region, that is obtained by joining this region with its predecessors...
void clearNodeCache()
Clear the cache for BB RegionNodes.
block_iterator block_begin()
Region * getRegionFor(BasicBlock *BB) const
Get the smallest region that contains a BasicBlock.
RegionSet::const_iterator const_iterator
element_iterator element_begin()
void replaceExitRecursive(BasicBlock *NewExit)
Recursively replace the exit basic block of the region.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Region * operator[](BasicBlock *BB) const
A shortcut for getRegionFor().
ValuesClass< DataType > END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
FunctionPass * createRegionInfoPass()
bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const
BasicBlock * getEntry() const
Get the entry BasicBlock of the Region.
Region * getCommonRegion(Region *A, Region *B) const
Find the smallest region that contains two regions.
LoopT * getParentLoop() const
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
bool isTopLevelRegion() const
Check if a Region is the TopLevel region.
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
StringRef getName() const
DomTreeNodeBase< NodeT > * getIDom() const
void WriteAsOperand(raw_ostream &, const Value *, bool PrintTy=true, const Module *Context=0)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void replaceExit(BasicBlock *BB)
Replace the exit basic block of the region with the new basic block.
STATISTIC(numRegions,"The # of regions")
Detect single entry single exit true
#define llvm_unreachable(msg)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
virtual bool runOnFunction(Function &F)
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
void setPointer(PointerTy PtrVal)
po_iterator< T > po_begin(T G)
block_iterator block_end()
ID
LLVM Calling Convention Representation.
RegionSet::iterator iterator
Interval::succ_iterator succ_begin(Interval *I)
BasicBlock * getEnteringBlock() const
Return the first block of this region's single entry edge, if existing.
Loop * outermostLoopInRegion(Loop *L) const
Get the outermost loop in the region that contains a loop.
Loop * getLoopFor(const BasicBlock *BB) const
Region * getParent() const
Get the parent of the Region.
Interval::succ_iterator succ_end(Interval *I)
virtual void print(raw_ostream &OS, const Module *) const
std::set< BasicBlock * > DomSetType
unsigned getNumSuccessors() const
void replaceEntry(BasicBlock *BB)
Replace the entry basic block of the region with the new basic block.
LLVM Basic Block Representation.
void splitBlock(BasicBlock *NewBB, BasicBlock *OldBB)
Update RegionInfo after a basic block was split.
A single entry single exit Region.
Interval::pred_iterator pred_begin(Interval *I)
~Region()
Delete the Region and all its subregions.
element_iterator element_end()
Interval::pred_iterator pred_end(Interval *I)
bool isSimple() const
Is this a simple region?
PrintStyle
PrintStyle - Print region in difference ways.
BasicBlock * getMaxRegionExit(BasicBlock *BB) const
Return the exit of the maximal refined region, that starts at a BasicBlock.
Region * removeSubRegion(Region *SubRegion)
Remove a subregion from this Region.
bool dominates(const DomTreeNode *A, const DomTreeNode *B) const
void transferChildrenTo(Region *To)
Move all direct child nodes of this Region to another Region.
void initializeRegionInfoPass(PassRegistry &)
BasicBlock * getExit() const
Get the exit BasicBlock of the Region.
void Calculate(DominatorTreeBase< typename GraphTraits< NodeT >::NodeType > &DT, FuncT &F)
static bool VerifyRegionInfo
void setRegionFor(BasicBlock *BB, Region *R)
Set the smallest region that surrounds a basic block.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
virtual void verifyAnalysis() const
RegionNode * getNode() const
Get the RegionNode representing the current Region.
void dump() const
Print the region to stderr.
Region * parent
The parent Region of this RegionNode.
RegionNode * getBBNode(BasicBlock *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
Region * getSubRegionNode(BasicBlock *BB) const
Get the subregion that starts at a BasicBlock.
const BasicBlock & getEntryBlock() const
virtual void getAnalysisUsage(AnalysisUsage &AU) const
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
DenseMapIterator< KeyT, ValueT, KeyInfoT > iterator
PointerIntPair< BasicBlock *, 1, bool > entry
std::string getName(ID id, ArrayRef< Type * > Tys=None)
po_iterator< T > po_end(T G)
static cl::opt< bool, true > VerifyRegionInfoX("verify-region-info", cl::location(VerifyRegionInfo), cl::desc("Verify region info (time consuming)"))
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
AnalysisUsage & addRequiredTransitive()
void addSubRegion(Region *SubRegion, bool moveChildren=false)
Add a new subregion to this Region.
void verifyRegion() const
Verify if the region is a correct region.
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
BasicBlock * getExitingBlock() const
Return the first block of this region's single exit edge, if existing.
INITIALIZE_PASS_BEGIN(RegionInfo,"regions","Detect single entry single exit regions", true, true) INITIALIZE_PASS_END(RegionInfo
iterator find(const KeyT &Val)
DomTreeNode * getNode(BasicBlock *BB) const
LocationClass< Ty > location(Ty &L)
Analysis that detects all canonical Regions.
void replaceEntryRecursive(BasicBlock *NewEntry)
Recursively replace the entry basic block of the region.