12 #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
13 #define LLVM_ANALYSIS_REGIONINFOIMPL_H
30 #define DEBUG_TYPE "region"
36 typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
38 :
RegionNodeBase<Tr>(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
43 for (
typename BBNodeMapT::iterator it = BBNodeMap.begin(),
55 this->entry.setPointer(BB);
60 assert(exit &&
"No exit to replace!");
66 std::vector<RegionT *> RegionQueue;
67 BlockT *OldEntry = getEntry();
69 RegionQueue.push_back(static_cast<RegionT *>(
this));
70 while (!RegionQueue.empty()) {
71 RegionT *R = RegionQueue.back();
72 RegionQueue.pop_back();
74 R->replaceEntry(NewEntry);
75 for (
typename RegionT::const_iterator RI = R->begin(), RE = R->end();
77 if ((*RI)->getEntry() == OldEntry)
78 RegionQueue.push_back(RI->get());
85 std::vector<RegionT *> RegionQueue;
86 BlockT *OldExit = getExit();
88 RegionQueue.push_back(static_cast<RegionT *>(
this));
89 while (!RegionQueue.empty()) {
90 RegionT *R = RegionQueue.back();
91 RegionQueue.pop_back();
93 R->replaceExit(NewExit);
94 for (
typename RegionT::const_iterator RI = R->begin(), RE = R->end();
96 if ((*RI)->getExit() == OldExit)
97 RegionQueue.push_back(RI->get());
104 BlockT *BB =
const_cast<BlockT *
>(B);
106 if (!DT->getNode(BB))
109 BlockT *entry = getEntry(), *exit = getExit();
115 return (DT->dominates(entry, BB) &&
116 !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
125 return getExit() ==
nullptr;
131 L->getExitingBlocks(ExitingBlocks);
133 for (BlockT *BB : ExitingBlocks) {
146 while (L &&
contains(L->getParentLoop())) {
147 L = L->getParentLoop();
156 assert(LI && BB &&
"LI and BB cannot be null!");
157 LoopT *L = LI->getLoopFor(BB);
158 return outermostLoopInRegion(L);
163 BlockT *entry = getEntry();
165 BlockT *enteringBlock =
nullptr;
167 for (PredIterTy PI = InvBlockTraits::child_begin(entry),
168 PE = InvBlockTraits::child_end(entry);
171 if (DT->getNode(Pred) && !
contains(Pred)) {
175 enteringBlock = Pred;
179 return enteringBlock;
184 BlockT *exit = getExit();
186 BlockT *exitingBlock =
nullptr;
191 for (PredIterTy PI = InvBlockTraits::child_begin(exit),
192 PE = InvBlockTraits::child_end(exit);
213 std::string exitName;
214 std::string entryName;
216 if (getEntry()->
getName().empty()) {
219 getEntry()->printAsOperand(OS,
false);
221 entryName = getEntry()->getName();
224 if (getExit()->
getName().empty()) {
227 getExit()->printAsOperand(OS,
false);
229 exitName = getExit()->getName();
231 exitName =
"<Function Return>";
233 return entryName +
" => " + exitName;
241 BlockT *entry = getEntry(), *exit = getExit();
243 for (SuccIterTy SI = BlockTraits::child_begin(BB),
244 SE = BlockTraits::child_end(BB);
251 for (PredIterTy SI = InvBlockTraits::child_begin(BB),
252 SE = InvBlockTraits::child_end(BB);
261 void RegionBase<Tr>::verifyWalk(BlockT *BB, std::set<BlockT *> *visited)
const {
262 BlockT *exit = getExit();
266 verifyBBInRegion(BB);
268 for (SuccIterTy SI = BlockTraits::child_begin(BB),
269 SE = BlockTraits::child_end(BB);
271 if (*SI != exit && visited->find(*SI) == visited->end())
272 verifyWalk(*SI, visited);
284 std::set<BlockT *> visited;
285 verifyWalk(getEntry(), &visited);
290 for (
typename RegionT::const_iterator RI =
begin(), RE =
end(); RI != RE;
292 (*RI)->verifyRegionNest();
311 static_cast<const RegionT *>(
this));
318 static_cast<const RegionT *>(
this));
323 typedef typename Tr::RegionT
RegionT;
324 RegionT *R = RI->getRegionFor(BB);
330 assert(
contains(R) &&
"BB not in current region!");
332 while (
contains(R->getParent()) && R->getParent() !=
this)
335 if (R->getEntry() != BB)
343 assert(
contains(BB) &&
"Can get BB node out of this region!");
345 typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
347 if (at != BBNodeMap.end())
351 RegionNodeT *NewNode =
new RegionNodeT(static_cast<RegionT *>(Deconst), BB);
352 BBNodeMap.insert(std::make_pair(BB, NewNode));
358 assert(
contains(BB) &&
"Can get BB node out of this region!");
359 if (
RegionT *Child = getSubRegionNode(BB))
360 return Child->getNode();
362 return getBBNode(BB);
369 To->children.push_back(std::move(*
I));
376 assert(!SubRegion->parent &&
"SubRegion already has a parent!");
377 assert(std::find_if(
begin(),
end(), [&](
const std::unique_ptr<RegionT> &R) {
378 return R.get() == SubRegion;
379 }) == children.end() &&
380 "Subregion already exists!");
382 SubRegion->parent =
static_cast<RegionT *
>(
this);
383 children.push_back(std::unique_ptr<RegionT>(SubRegion));
388 assert(SubRegion->children.empty() &&
389 "SubRegions that contain children are not supported");
392 if (!(*I)->isSubRegion()) {
393 BlockT *BB = (*I)->template getNodeAs<BlockT>();
395 if (SubRegion->contains(BB))
396 RI->setRegionFor(BB, SubRegion);
400 std::vector<std::unique_ptr<RegionT>> Keep;
402 if (SubRegion->contains(
I->get()) &&
I->get() != SubRegion) {
403 (*I)->parent = SubRegion;
404 SubRegion->children.push_back(std::move(*
I));
406 Keep.push_back(std::move(*
I));
412 std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
413 std::move_iterator<typename RegionSet::iterator>(Keep.end()));
418 assert(Child->parent ==
this &&
"Child is not a child of this region!");
419 Child->parent =
nullptr;
420 typename RegionSet::iterator
I = std::find_if(
421 children.begin(), children.end(),
422 [&](
const std::unique_ptr<RegionT> &R) {
return R.get() == Child; });
423 assert(I != children.end() &&
"Region does not exit. Unable to remove.");
424 children.erase(children.begin() + (I -
begin()));
440 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
442 if (NumSuccessors == 0)
445 for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
446 PE = InvBlockTraits::child_end(getExit());
448 if (!DT->dominates(getEntry(), *PI))
452 RegionT *R = RI->getRegionFor(exit);
454 if (R->getEntry() != exit) {
455 if (Tr::getNumSuccessors(exit) == 1)
456 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
460 while (R->getParent() && R->getParent()->getEntry() == exit)
463 if (!DT->dominates(getEntry(), R->getExit())) {
464 for (PredIterTy PI = InvBlockTraits::child_begin(getExit()),
465 PE = InvBlockTraits::child_end(getExit());
467 if (!DT->dominates(R->getExit(), *PI))
472 return new RegionT(getEntry(), R->getExit(), RI, DT);
479 OS.
indent(level * 2) <<
'[' << level <<
"] " << getNameStr();
481 OS.
indent(level * 2) << getNameStr();
485 if (Style != PrintNone) {
486 OS.
indent(level * 2) <<
"{\n";
489 if (Style == PrintBB) {
490 for (
const auto *BB : blocks())
491 OS << BB->getName() <<
", ";
492 }
else if (Style == PrintRN) {
504 (*RI)->print(OS, print_tree, level + 1, Style);
507 if (Style != PrintNone)
508 OS.
indent(level * 2) <<
"} \n";
511 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
521 for (
typename BBNodeMapT::iterator
I = BBNodeMap.begin(),
522 IE = BBNodeMap.end();
527 for (
typename RegionT::iterator RI =
begin(), RE =
end(); RI != RE; ++RI)
528 (*RI)->clearNodeCache();
537 : TopLevelRegion(nullptr) {}
540 RegionInfoBase<Tr>::~RegionInfoBase() {
545 bool RegionInfoBase<Tr>::isCommonDomFrontier(BlockT *BB, BlockT *entry,
546 BlockT *exit)
const {
547 for (PredIterTy PI = InvBlockTraits::child_begin(BB),
548 PE = InvBlockTraits::child_end(BB);
551 if (DT->dominates(entry, P) && !DT->dominates(exit, P))
559 bool RegionInfoBase<Tr>::isRegion(BlockT *entry, BlockT *exit)
const {
560 assert(entry && exit &&
"entry and exit must not be null!");
561 typedef typename DomFrontierT::DomSetType DST;
563 DST *entrySuccs = &DF->find(entry)->second;
567 if (!DT->dominates(entry, exit)) {
568 for (
typename DST::iterator SI = entrySuccs->begin(),
569 SE = entrySuccs->end();
571 if (*SI != exit && *SI != entry)
578 DST *exitSuccs = &DF->find(exit)->second;
581 for (
typename DST::iterator SI = entrySuccs->begin(), SE = entrySuccs->end();
583 if (*SI == exit || *SI == entry)
585 if (exitSuccs->find(*SI) == exitSuccs->end())
587 if (!isCommonDomFrontier(*SI, entry, exit))
592 for (
typename DST::iterator SI = exitSuccs->begin(), SE = exitSuccs->end();
594 if (DT->properlyDominates(entry, *SI) && *
SI != exit)
602 void RegionInfoBase<Tr>::insertShortCut(BlockT *entry, BlockT *exit,
603 BBtoBBMap *ShortCut)
const {
604 assert(entry && exit &&
"entry and exit must not be null!");
606 typename BBtoBBMap::iterator e = ShortCut->find(exit);
608 if (e == ShortCut->end())
610 (*ShortCut)[entry] = exit;
615 BlockT *BB = e->second;
616 (*ShortCut)[entry] = BB;
621 typename Tr::DomTreeNodeT *
622 RegionInfoBase<Tr>::getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const {
623 typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
625 if (e == ShortCut->end())
628 return PDT->getNode(e->second)->getIDom();
632 bool RegionInfoBase<Tr>::isTrivialRegion(BlockT *entry, BlockT *exit)
const {
633 assert(entry && exit &&
"entry and exit must not be null!");
635 unsigned num_successors =
636 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
638 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
645 typename Tr::RegionT *RegionInfoBase<Tr>::createRegion(BlockT *entry,
647 assert(entry && exit &&
"entry and exit must not be null!");
649 if (isTrivialRegion(entry, exit))
653 new RegionT(entry, exit, static_cast<RegionInfoT *>(
this), DT);
654 BBtoRegion.insert(std::make_pair(entry, region));
657 region->verifyRegion();
659 DEBUG(region->verifyRegion());
662 updateStatistics(region);
667 void RegionInfoBase<Tr>::findRegionsWithEntry(BlockT *entry,
668 BBtoBBMap *ShortCut) {
671 DomTreeNodeT *N = PDT->getNode(entry);
675 RegionT *lastRegion =
nullptr;
676 BlockT *lastExit = entry;
680 while ((N = getNextPostDom(N, ShortCut))) {
681 BlockT *exit = N->getBlock();
686 if (isRegion(entry, exit)) {
687 RegionT *newRegion = createRegion(entry, exit);
690 newRegion->addSubRegion(lastRegion);
692 lastRegion = newRegion;
697 if (!DT->dominates(entry, exit))
703 if (lastExit != entry)
704 insertShortCut(entry, lastExit, ShortCut);
708 void RegionInfoBase<Tr>::scanForRegions(FuncT &
F, BBtoBBMap *ShortCut) {
709 typedef typename std::add_pointer<FuncT>::type FuncPtrT;
710 BlockT *entry = GraphTraits<FuncPtrT>::getEntryNode(&F);
711 DomTreeNodeT *N = DT->getNode(entry);
718 findRegionsWithEntry(DomNode->getBlock(), ShortCut);
722 typename Tr::RegionT *RegionInfoBase<Tr>::getTopMostParent(RegionT *region) {
723 while (region->getParent())
724 region = region->getParent();
730 void RegionInfoBase<Tr>::buildRegionsTree(DomTreeNodeT *N, RegionT *region) {
731 BlockT *BB = N->getBlock();
734 while (BB == region->getExit())
735 region = region->getParent();
737 typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
741 if (it != BBtoRegion.end()) {
742 RegionT *newRegion = it->second;
743 region->addSubRegion(getTopMostParent(newRegion));
746 BBtoRegion[BB] = region;
749 for (
typename DomTreeNodeT::iterator CI = N->begin(),
CE = N->end(); CI !=
CE;
751 buildRegionsTree(*CI, region);
757 bool RegionInfoBase<Tr>::VerifyRegionInfo =
true;
760 bool RegionInfoBase<Tr>::VerifyRegionInfo =
false;
764 typename Tr::RegionT::PrintStyle RegionInfoBase<Tr>::printStyle =
765 RegionBase<Tr>::PrintNone;
769 OS <<
"Region tree:\n";
770 TopLevelRegion->print(OS,
true, 0, printStyle);
771 OS <<
"End region tree\n";
774 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
783 delete TopLevelRegion;
784 TopLevelRegion =
nullptr;
789 TopLevelRegion->verifyRegionNest();
796 return I != BBtoRegion.end() ? I->second :
nullptr;
806 return getRegionFor(BB);
810 typename RegionInfoBase<Tr>::BlockT *
812 BlockT *Exit =
nullptr;
816 RegionT *R = getRegionFor(BB);
817 while (R && R->getParent() && R->getParent()->getEntry() == BB)
821 if (R && R->getEntry() == BB)
823 else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
824 Exit = *BlockTraits::child_begin(BB);
829 RegionT *ExitR = getRegionFor(Exit);
830 while (ExitR && ExitR->getParent() &&
831 ExitR->getParent()->getEntry() == Exit)
832 ExitR = ExitR->getParent();
834 for (PredIterTy PI = InvBlockTraits::child_begin(Exit),
835 PE = InvBlockTraits::child_end(Exit);
837 if (!R->contains(*PI) && !ExitR->contains(*PI))
842 if (DT->dominates(Exit, BB))
854 assert(A && B &&
"One of the Regions is NULL");
859 while (!B->contains(A))
866 typename Tr::RegionT *
868 RegionT *ret = Regions.
back();
871 for (RegionT *R : Regions)
872 ret = getCommonRegion(ret, R);
878 typename Tr::RegionT *
880 RegionT *ret = getRegionFor(BBs.
back());
883 for (BlockT *BB : BBs)
884 ret = getCommonRegion(ret, getRegionFor(BB));
891 RegionT *R = getRegionFor(OldBB);
893 setRegionFor(NewBB, R);
895 while (R->getEntry() == OldBB && !R->isTopLevelRegion()) {
896 R->replaceEntry(NewBB);
900 setRegionFor(OldBB, R);
905 typedef typename std::add_pointer<FuncT>::type FuncPtrT;
912 scanForRegions(F, &ShortCut);
914 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
const_iterator end(StringRef path)
Get end iterator over path.
std::string getNameStr() const
Returns the name of the Region.
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
bool isSimple() const
Is this a simple region?
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
BlockT * getExitingBlock() const
Return the first block of this region's single exit edge, if existing.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const_iterator begin(StringRef path)
Get begin iterator over path.
void setRegionFor(BlockT *BB, RegionT *R)
Set the smallest region that surrounds a basic block.
Analysis that detects all canonical Regions.
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
unsigned getDepth() const
Get the nesting level of this Region.
LoopT * outermostLoopInRegion(LoopT *L) const
Get the outermost loop in the region that contains a loop.
void clearNodeCache()
Clear the cache for BB RegionNodes.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static StringRef getName(Value *V)
Windows NT (Windows on ARM)
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
RegionNodeT * getBBNode(BlockT *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
void print(raw_ostream &OS) const
BlockT * getMaxRegionExit(BlockT *BB) const
Return the exit of the maximal refined region, that starts at a BasicBlock.
PrintStyle
PrintStyle - Print region in difference ways.
void verifyAnalysis() const
RegionSet::iterator iterator
void dump() const
Print the region to stderr.
void addSubRegion(RegionT *SubRegion, bool moveChildren=false)
Add a new subregion to this Region.
void replaceExit(BlockT *BB)
Replace the exit basic block of the region with the new basic block.
element_iterator element_end()
iterator_range< po_iterator< T > > post_order(const T &G)
void transferChildrenTo(RegionT *To)
Move all direct child nodes of this Region to another Region.
void replaceEntryRecursive(BlockT *NewEntry)
Recursively replace the entry basic block of the region.
RegionT * getSubRegionNode(BlockT *BB) const
Get the subregion that starts at a BasicBlock.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
RegionT * getExpandedRegion() const
Return a new (non-canonical) region, that is obtained by joining this region with its predecessors...
A single entry single exit Region.
void replaceEntry(BlockT *BB)
Replace the entry basic block of the region with the new basic block.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
element_iterator element_begin()
void splitBlock(BlockT *NewBB, BlockT *OldBB)
Update RegionInfo after a basic block was split.
void replaceExitRecursive(BlockT *NewExit)
Recursively replace the exit basic block of the region.
BlockT * getEnteringBlock() const
Return the first block of this region's single entry edge, if existing.
void verifyRegion() const
Verify if the region is a correct region.
RegionSet::const_iterator const_iterator
~RegionBase()
Delete the Region and all its subregions.
RegionT * operator[](BlockT *BB) const
A shortcut for getRegionFor().
A raw_ostream that writes to an std::string.
RegionT * getCommonRegion(RegionT *A, RegionT *B) const
Find the smallest region that contains two regions.
Module * getParent()
Get the module that this global value is contained inside of...
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
RegionT * removeSubRegion(RegionT *SubRegion)
Remove a subregion from this Region.
static MachineBasicBlock * getExitingBlock(MachineLoop *L)
Return the latch block if it's one of the exiting blocks.