12 #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
13 #define LLVM_ANALYSIS_REGIONINFOIMPL_H
34 #include <type_traits>
37 #define DEBUG_TYPE "region"
45 typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
47 : RegionNodeBase<Tr>(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
58 this->entry.setPointer(BB);
63 assert(exit &&
"No exit to replace!");
69 std::vector<RegionT *> RegionQueue;
70 BlockT *OldEntry = getEntry();
72 RegionQueue.push_back(static_cast<RegionT *>(
this));
73 while (!RegionQueue.empty()) {
74 RegionT *R = RegionQueue.back();
75 RegionQueue.pop_back();
77 R->replaceEntry(NewEntry);
78 for (std::unique_ptr<RegionT> &Child : *R) {
79 if (Child->getEntry() == OldEntry)
80 RegionQueue.push_back(Child.get());
87 std::vector<RegionT *> RegionQueue;
88 BlockT *OldExit = getExit();
90 RegionQueue.push_back(static_cast<RegionT *>(
this));
91 while (!RegionQueue.empty()) {
92 RegionT *R = RegionQueue.back();
93 RegionQueue.pop_back();
95 R->replaceExit(NewExit);
96 for (std::unique_ptr<RegionT> &Child : *R) {
97 if (Child->getExit() == OldExit)
98 RegionQueue.push_back(Child.get());
105 BlockT *BB =
const_cast<BlockT *
>(
B);
107 if (!DT->getNode(BB))
110 BlockT *entry = getEntry(), *exit = getExit();
116 return (DT->dominates(entry, BB) &&
117 !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
126 return getExit() ==
nullptr;
132 L->getExitingBlocks(ExitingBlocks);
134 for (BlockT *BB : ExitingBlocks) {
147 while (L &&
contains(L->getParentLoop())) {
148 L = L->getParentLoop();
157 assert(LI && BB &&
"LI and BB cannot be null!");
158 LoopT *
L = LI->getLoopFor(BB);
159 return outermostLoopInRegion(L);
164 BlockT *entry = getEntry();
165 BlockT *enteringBlock =
nullptr;
167 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(entry),
168 InvBlockTraits::child_end(entry))) {
169 if (DT->getNode(Pred) && !
contains(Pred)) {
173 enteringBlock = Pred;
177 return enteringBlock;
182 BlockT *exit = getExit();
183 BlockT *exitingBlock =
nullptr;
188 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(exit),
189 InvBlockTraits::child_end(exit))) {
203 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
208 std::string exitName;
209 std::string entryName;
211 if (getEntry()->
getName().empty()) {
214 getEntry()->printAsOperand(OS,
false);
216 entryName = getEntry()->getName();
219 if (getExit()->
getName().empty()) {
222 getExit()->printAsOperand(OS,
false);
224 exitName = getExit()->getName();
226 exitName =
"<Function Return>";
228 return entryName +
" => " + exitName;
236 BlockT *entry = getEntry(), *exit = getExit();
239 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
240 if (!
contains(Succ) && exit != Succ)
242 "to the exit node!");
246 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(BB),
247 InvBlockTraits::child_end(BB))) {
250 "go to the entry node!");
256 void RegionBase<Tr>::verifyWalk(BlockT *BB, std::set<BlockT *> *visited)
const {
257 BlockT *exit = getExit();
261 verifyBBInRegion(BB);
264 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
265 if (Succ != exit && visited->find(Succ) == visited->end())
266 verifyWalk(Succ, visited);
278 std::set<BlockT *> visited;
279 verifyWalk(getEntry(), &visited);
284 for (
const std::unique_ptr<RegionT> &R : *
this)
285 R->verifyRegionNest();
304 static_cast<const RegionT *>(
this));
311 static_cast<const RegionT *>(
this));
316 using RegionT =
typename Tr::RegionT;
318 RegionT *R = RI->getRegionFor(BB);
326 while (
contains(R->getParent()) && R->getParent() !=
this)
329 if (R->getEntry() != BB)
339 typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
341 if (at == BBNodeMap.end()) {
343 typename BBNodeMapT::value_type V = {
345 llvm::make_unique<RegionNodeT>(
static_cast<RegionT *
>(Deconst), BB)};
346 at = BBNodeMap.insert(std::move(V)).first;
348 return at->second.get();
354 if (
RegionT *Child = getSubRegionNode(BB))
355 return Child->getNode();
357 return getBBNode(BB);
362 for (std::unique_ptr<RegionT> &R : *
this) {
364 To->children.push_back(std::move(R));
371 assert(!SubRegion->parent &&
"SubRegion already has a parent!");
373 [&](
const std::unique_ptr<RegionT> &R) {
374 return R.get() == SubRegion;
376 "Subregion already exists!");
378 SubRegion->parent =
static_cast<RegionT *
>(
this);
379 children.push_back(std::unique_ptr<RegionT>(SubRegion));
384 assert(SubRegion->children.empty() &&
385 "SubRegions that contain children are not supported");
387 for (RegionNodeT *Element : elements()) {
388 if (!Element->isSubRegion()) {
389 BlockT *BB = Element->template getNodeAs<BlockT>();
391 if (SubRegion->contains(BB))
392 RI->setRegionFor(BB, SubRegion);
396 std::vector<std::unique_ptr<RegionT>> Keep;
397 for (std::unique_ptr<RegionT> &R : *
this) {
398 if (SubRegion->contains(R.get()) && R.get() != SubRegion) {
399 R->parent = SubRegion;
400 SubRegion->children.push_back(std::move(R));
402 Keep.push_back(std::move(R));
408 std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
409 std::move_iterator<typename RegionSet::iterator>(Keep.end()));
414 assert(Child->parent ==
this &&
"Child is not a child of this region!");
415 Child->parent =
nullptr;
416 typename RegionSet::iterator
I =
418 return R.get() == Child;
420 assert(I !=
children.end() &&
"Region does not exit. Unable to remove.");
437 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
439 if (NumSuccessors == 0)
442 RegionT *R = RI->getRegionFor(exit);
444 if (R->getEntry() != exit) {
445 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(getExit()),
446 InvBlockTraits::child_end(getExit())))
449 if (Tr::getNumSuccessors(exit) == 1)
450 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
454 while (R->getParent() && R->getParent()->getEntry() == exit)
457 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(getExit()),
458 InvBlockTraits::child_end(getExit()))) {
459 if (!(
contains(Pred) || R->contains(Pred)))
463 return new RegionT(getEntry(), R->getExit(), RI, DT);
470 OS.
indent(level * 2) <<
'[' << level <<
"] " << getNameStr();
472 OS.
indent(level * 2) << getNameStr();
476 if (Style != PrintNone) {
477 OS.
indent(level * 2) <<
"{\n";
480 if (Style == PrintBB) {
481 for (
const auto *BB : blocks())
482 OS << BB->getName() <<
", ";
483 }
else if (Style == PrintRN) {
484 for (
const RegionNodeT *Element : elements()) {
485 OS << *Element <<
", ";
493 for (
const std::unique_ptr<RegionT> &R : *
this)
494 R->print(OS, print_tree, level + 1, Style);
497 if (Style != PrintNone)
498 OS.
indent(level * 2) <<
"} \n";
501 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
511 for (std::unique_ptr<RegionT> &R : *
this)
528 void RegionInfoBase<Tr>::verifyBBMap(
const RegionT *R)
const {
529 assert(R &&
"Re must be non-null");
530 for (
const typename Tr::RegionNodeT *Element : R->elements()) {
531 if (Element->isSubRegion()) {
532 const RegionT *SR = Element->template getNodeAs<RegionT>();
535 BlockT *BB = Element->template getNodeAs<BlockT>();
536 if (getRegionFor(BB) != R)
543 bool RegionInfoBase<Tr>::isCommonDomFrontier(BlockT *BB, BlockT *entry,
544 BlockT *exit)
const {
545 for (BlockT *
P :
make_range(InvBlockTraits::child_begin(BB),
546 InvBlockTraits::child_end(BB))) {
547 if (DT->dominates(entry,
P) && !DT->dominates(exit,
P))
555 bool RegionInfoBase<Tr>::isRegion(BlockT *entry, BlockT *exit)
const {
556 assert(entry && exit &&
"entry and exit must not be null!");
558 using DST =
typename DomFrontierT::DomSetType;
560 DST *entrySuccs = &DF->find(entry)->second;
564 if (!DT->dominates(entry, exit)) {
565 for (
typename DST::iterator SI = entrySuccs->begin(),
566 SE = entrySuccs->end();
568 if (*SI != exit && *SI != entry)
575 DST *exitSuccs = &DF->find(exit)->second;
578 for (BlockT *Succ : *entrySuccs) {
579 if (Succ == exit || Succ == entry)
581 if (exitSuccs->find(Succ) == exitSuccs->end())
583 if (!isCommonDomFrontier(Succ, entry, exit))
588 for (BlockT *Succ : *exitSuccs) {
589 if (DT->properlyDominates(entry, Succ) && Succ != exit)
597 void RegionInfoBase<Tr>::insertShortCut(BlockT *entry, BlockT *exit,
598 BBtoBBMap *ShortCut)
const {
599 assert(entry && exit &&
"entry and exit must not be null!");
601 typename BBtoBBMap::iterator e = ShortCut->find(exit);
603 if (e == ShortCut->end())
605 (*ShortCut)[entry] = exit;
610 BlockT *BB = e->second;
611 (*ShortCut)[entry] = BB;
616 typename Tr::DomTreeNodeT *
617 RegionInfoBase<Tr>::getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const {
618 typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
620 if (e == ShortCut->end())
623 return PDT->getNode(e->second)->getIDom();
627 bool RegionInfoBase<Tr>::isTrivialRegion(BlockT *entry, BlockT *exit)
const {
628 assert(entry && exit &&
"entry and exit must not be null!");
630 unsigned num_successors =
631 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
633 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
640 typename Tr::RegionT *RegionInfoBase<Tr>::createRegion(BlockT *entry,
642 assert(entry && exit &&
"entry and exit must not be null!");
644 if (isTrivialRegion(entry, exit))
648 new RegionT(entry, exit, static_cast<RegionInfoT *>(
this), DT);
649 BBtoRegion.insert({entry, region});
651 #ifdef EXPENSIVE_CHECKS
652 region->verifyRegion();
654 DEBUG(region->verifyRegion());
657 updateStatistics(region);
662 void RegionInfoBase<Tr>::findRegionsWithEntry(BlockT *entry,
663 BBtoBBMap *ShortCut) {
666 DomTreeNodeT *N = PDT->getNode(entry);
670 RegionT *lastRegion =
nullptr;
671 BlockT *lastExit = entry;
675 while ((N = getNextPostDom(N, ShortCut))) {
676 BlockT *exit = N->getBlock();
681 if (isRegion(entry, exit)) {
682 RegionT *newRegion = createRegion(entry, exit);
685 newRegion->addSubRegion(lastRegion);
687 lastRegion = newRegion;
692 if (!DT->dominates(entry, exit))
698 if (lastExit != entry)
699 insertShortCut(entry, lastExit, ShortCut);
703 void RegionInfoBase<Tr>::scanForRegions(FuncT &
F, BBtoBBMap *ShortCut) {
704 using FuncPtrT =
typename std::add_pointer<FuncT>::type;
706 BlockT *entry = GraphTraits<FuncPtrT>::getEntryNode(&F);
707 DomTreeNodeT *N = DT->getNode(entry);
714 findRegionsWithEntry(DomNode->getBlock(), ShortCut);
718 typename Tr::RegionT *RegionInfoBase<Tr>::getTopMostParent(RegionT *region) {
719 while (region->getParent())
720 region = region->getParent();
726 void RegionInfoBase<Tr>::buildRegionsTree(DomTreeNodeT *N, RegionT *region) {
727 BlockT *BB = N->getBlock();
730 while (BB == region->getExit())
731 region = region->getParent();
733 typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
737 if (it != BBtoRegion.end()) {
738 RegionT *newRegion = it->second;
739 region->addSubRegion(getTopMostParent(newRegion));
742 BBtoRegion[BB] = region;
745 for (DomTreeNodeBase<BlockT> *
C : *N) {
746 buildRegionsTree(
C, region);
750 #ifdef EXPENSIVE_CHECKS
752 bool RegionInfoBase<Tr>::VerifyRegionInfo =
true;
755 bool RegionInfoBase<Tr>::VerifyRegionInfo =
false;
759 typename Tr::RegionT::PrintStyle RegionInfoBase<Tr>::printStyle =
760 RegionBase<Tr>::PrintNone;
764 OS <<
"Region tree:\n";
765 TopLevelRegion->print(OS,
true, 0, printStyle);
766 OS <<
"End region tree\n";
769 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
778 delete TopLevelRegion;
779 TopLevelRegion =
nullptr;
789 TopLevelRegion->verifyRegionNest();
791 verifyBBMap(TopLevelRegion);
798 return I != BBtoRegion.end() ? I->second :
nullptr;
808 return getRegionFor(BB);
812 typename RegionInfoBase<Tr>::BlockT *
814 BlockT *Exit =
nullptr;
818 RegionT *R = getRegionFor(BB);
819 while (R && R->getParent() && R->getParent()->getEntry() == BB)
823 if (R && R->getEntry() == BB)
825 else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
826 Exit = *BlockTraits::child_begin(BB);
831 RegionT *ExitR = getRegionFor(Exit);
832 while (ExitR && ExitR->getParent() &&
833 ExitR->getParent()->getEntry() == Exit)
834 ExitR = ExitR->getParent();
836 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(Exit),
837 InvBlockTraits::child_end(Exit))) {
838 if (!R->contains(Pred) && !ExitR->contains(Pred))
843 if (DT->dominates(Exit, BB))
855 assert(A && B &&
"One of the Regions is NULL");
860 while (!B->contains(A))
867 typename Tr::RegionT *
869 RegionT *ret = Regions.
back();
872 for (RegionT *R : Regions)
873 ret = getCommonRegion(ret, R);
879 typename Tr::RegionT *
881 RegionT *ret = getRegionFor(BBs.
back());
884 for (BlockT *BB : BBs)
885 ret = getCommonRegion(ret, getRegionFor(BB));
892 using FuncPtrT =
typename std::add_pointer<FuncT>::type;
899 scanForRegions(F, &ShortCut);
901 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
908 #endif // LLVM_ANALYSIS_REGIONINFOIMPL_H
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
std::string getNameStr() const
Returns the name of the Region.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
bool isSimple() const
Is this a simple region?
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
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.
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.
return AArch64::GPR64RegClass contains(Reg)
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.
typename Tr::RegionT RegionT
void clearNodeCache()
Clear the cache for BB RegionNodes.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static StringRef getName(Value *V)
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
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.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
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.
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Create a new region.
element_iterator element_begin()
llvm_unreachable("Unknown RegBank!")
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.
~RegionBase()
Delete the Region and all its subregions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...