12 #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H
13 #define LLVM_ANALYSIS_REGIONINFOIMPL_H
29 #define DEBUG_TYPE "region"
35 typename Tr::RegionInfoT *RInfo, DomTreeT *dt,
37 :
RegionNodeBase<Tr>(Parent, Entry, 1), RI(RInfo), DT(dt), exit(Exit) {}
48 this->entry.setPointer(BB);
53 assert(exit &&
"No exit to replace!");
59 std::vector<RegionT *> RegionQueue;
60 BlockT *OldEntry = getEntry();
62 RegionQueue.push_back(static_cast<RegionT *>(
this));
63 while (!RegionQueue.empty()) {
64 RegionT *R = RegionQueue.back();
65 RegionQueue.pop_back();
67 R->replaceEntry(NewEntry);
68 for (std::unique_ptr<RegionT> &Child : *R) {
69 if (Child->getEntry() == OldEntry)
70 RegionQueue.push_back(Child.get());
77 std::vector<RegionT *> RegionQueue;
78 BlockT *OldExit = getExit();
80 RegionQueue.push_back(static_cast<RegionT *>(
this));
81 while (!RegionQueue.empty()) {
82 RegionT *R = RegionQueue.back();
83 RegionQueue.pop_back();
85 R->replaceExit(NewExit);
86 for (std::unique_ptr<RegionT> &Child : *R) {
87 if (Child->getExit() == OldExit)
88 RegionQueue.push_back(Child.get());
100 BlockT *entry = getEntry(), *exit = getExit();
106 return (DT->dominates(entry, BB) &&
107 !(DT->dominates(exit, BB) && DT->dominates(entry, exit)));
116 return getExit() ==
nullptr;
122 L->getExitingBlocks(ExitingBlocks);
124 for (
BlockT *BB : ExitingBlocks) {
137 while (L &&
contains(L->getParentLoop())) {
138 L = L->getParentLoop();
147 assert(LI && BB &&
"LI and BB cannot be null!");
148 LoopT *
L = LI->getLoopFor(BB);
149 return outermostLoopInRegion(L);
154 BlockT *entry = getEntry();
155 BlockT *enteringBlock =
nullptr;
158 InvBlockTraits::child_end(entry))) {
159 if (DT->getNode(Pred) && !
contains(Pred)) {
163 enteringBlock = Pred;
167 return enteringBlock;
173 BlockT *exitingBlock =
nullptr;
179 InvBlockTraits::child_end(exit))) {
193 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
198 std::string exitName;
199 std::string entryName;
201 if (getEntry()->
getName().empty()) {
204 getEntry()->printAsOperand(OS,
false);
206 entryName = getEntry()->getName();
209 if (getExit()->
getName().empty()) {
212 getExit()->printAsOperand(OS,
false);
214 exitName = getExit()->getName();
216 exitName =
"<Function Return>";
218 return entryName +
" => " + exitName;
226 BlockT *entry = getEntry(), *exit = getExit();
229 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
230 if (!
contains(Succ) && exit != Succ)
232 "to the exit node!");
236 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(BB),
237 InvBlockTraits::child_end(BB))) {
240 "go to the entry node!");
246 void RegionBase<Tr>::verifyWalk(BlockT *BB, std::set<BlockT *> *visited)
const {
247 BlockT *exit = getExit();
251 verifyBBInRegion(BB);
254 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
255 if (Succ != exit && visited->find(Succ) == visited->end())
256 verifyWalk(Succ, visited);
268 std::set<BlockT *> visited;
269 verifyWalk(getEntry(), &visited);
274 for (
const std::unique_ptr<RegionT> &R : *
this)
275 R->verifyRegionNest();
294 static_cast<const RegionT *>(
this));
301 static_cast<const RegionT *>(
this));
306 typedef typename Tr::RegionT
RegionT;
307 RegionT *R = RI->getRegionFor(BB);
315 while (
contains(R->getParent()) && R->getParent() !=
this)
318 if (R->getEntry() != BB)
328 typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
330 if (at == BBNodeMap.end()) {
332 typename BBNodeMapT::value_type V = {
333 BB, make_unique<RegionNodeT>(
static_cast<RegionT *
>(Deconst), BB)};
334 at = BBNodeMap.insert(std::move(V)).first;
336 return at->second.get();
342 if (
RegionT *Child = getSubRegionNode(BB))
343 return Child->getNode();
345 return getBBNode(BB);
350 for (std::unique_ptr<RegionT> &R : *
this) {
352 To->children.push_back(std::move(R));
359 assert(!SubRegion->parent &&
"SubRegion already has a parent!");
361 [&](
const std::unique_ptr<RegionT> &R) {
362 return R.get() == SubRegion;
363 }) == children.end() &&
364 "Subregion already exists!");
366 SubRegion->parent =
static_cast<RegionT *
>(
this);
367 children.push_back(std::unique_ptr<RegionT>(SubRegion));
372 assert(SubRegion->children.empty() &&
373 "SubRegions that contain children are not supported");
375 for (RegionNodeT *Element : elements()) {
376 if (!Element->isSubRegion()) {
377 BlockT *BB = Element->template getNodeAs<BlockT>();
379 if (SubRegion->contains(BB))
380 RI->setRegionFor(BB, SubRegion);
384 std::vector<std::unique_ptr<RegionT>> Keep;
385 for (std::unique_ptr<RegionT> &R : *
this) {
386 if (SubRegion->contains(R.get()) && R.get() != SubRegion) {
387 R->parent = SubRegion;
388 SubRegion->children.push_back(std::move(R));
390 Keep.push_back(std::move(R));
396 std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
397 std::move_iterator<typename RegionSet::iterator>(Keep.end()));
402 assert(Child->parent ==
this &&
"Child is not a child of this region!");
403 Child->parent =
nullptr;
404 typename RegionSet::iterator
I =
405 find_if(children, [&](
const std::unique_ptr<RegionT> &R) {
406 return R.get() == Child;
408 assert(I != children.end() &&
"Region does not exit. Unable to remove.");
409 children.erase(children.begin() + (I -
begin()));
425 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
427 if (NumSuccessors == 0)
430 RegionT *R = RI->getRegionFor(exit);
432 if (R->getEntry() != exit) {
434 InvBlockTraits::child_end(getExit())))
437 if (Tr::getNumSuccessors(exit) == 1)
438 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
442 while (R->getParent() && R->getParent()->getEntry() == exit)
446 InvBlockTraits::child_end(getExit()))) {
447 if (!(
contains(Pred) || R->contains(Pred)))
451 return new RegionT(getEntry(), R->getExit(), RI, DT);
458 OS.
indent(level * 2) <<
'[' << level <<
"] " << getNameStr();
460 OS.
indent(level * 2) << getNameStr();
464 if (Style != PrintNone) {
465 OS.
indent(level * 2) <<
"{\n";
468 if (Style == PrintBB) {
469 for (
const auto *BB : blocks())
470 OS << BB->getName() <<
", ";
471 }
else if (Style == PrintRN) {
472 for (
const RegionNodeT *Element : elements()) {
473 OS << *Element <<
", ";
481 for (
const std::unique_ptr<RegionT> &R : *
this)
482 R->print(OS, print_tree, level + 1, Style);
485 if (Style != PrintNone)
486 OS.
indent(level * 2) <<
"} \n";
489 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
499 for (std::unique_ptr<RegionT> &R : *
this)
509 : TopLevelRegion(nullptr) {}
512 RegionInfoBase<Tr>::~RegionInfoBase() {
517 void RegionInfoBase<Tr>::verifyBBMap(
const RegionT *R)
const {
518 assert(R &&
"Re must be non-null");
519 for (
const typename Tr::RegionNodeT *Element : R->elements()) {
520 if (Element->isSubRegion()) {
521 const RegionT *SR = Element->template getNodeAs<RegionT>();
524 BlockT *BB = Element->template getNodeAs<BlockT>();
525 if (getRegionFor(BB) != R)
532 bool RegionInfoBase<Tr>::isCommonDomFrontier(BlockT *BB, BlockT *entry,
533 BlockT *exit)
const {
534 for (BlockT *
P :
make_range(InvBlockTraits::child_begin(BB),
535 InvBlockTraits::child_end(BB))) {
536 if (DT->dominates(entry,
P) && !DT->dominates(exit,
P))
544 bool RegionInfoBase<Tr>::isRegion(BlockT *entry, BlockT *exit)
const {
545 assert(entry && exit &&
"entry and exit must not be null!");
546 typedef typename DomFrontierT::DomSetType DST;
548 DST *entrySuccs = &DF->find(entry)->second;
552 if (!DT->dominates(entry, exit)) {
553 for (
typename DST::iterator SI = entrySuccs->begin(),
554 SE = entrySuccs->end();
556 if (*SI != exit && *SI != entry)
563 DST *exitSuccs = &DF->find(exit)->second;
566 for (BlockT *Succ : *entrySuccs) {
567 if (Succ == exit || Succ == entry)
569 if (exitSuccs->find(Succ) == exitSuccs->end())
571 if (!isCommonDomFrontier(Succ, entry, exit))
576 for (BlockT *Succ : *exitSuccs) {
577 if (DT->properlyDominates(entry, Succ) && Succ != exit)
585 void RegionInfoBase<Tr>::insertShortCut(BlockT *entry, BlockT *exit,
586 BBtoBBMap *ShortCut)
const {
587 assert(entry && exit &&
"entry and exit must not be null!");
589 typename BBtoBBMap::iterator e = ShortCut->find(exit);
591 if (e == ShortCut->end())
593 (*ShortCut)[entry] = exit;
598 BlockT *BB = e->second;
599 (*ShortCut)[entry] = BB;
604 typename Tr::DomTreeNodeT *
605 RegionInfoBase<Tr>::getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const {
606 typename BBtoBBMap::iterator e = ShortCut->find(N->getBlock());
608 if (e == ShortCut->end())
611 return PDT->getNode(e->second)->getIDom();
615 bool RegionInfoBase<Tr>::isTrivialRegion(BlockT *entry, BlockT *exit)
const {
616 assert(entry && exit &&
"entry and exit must not be null!");
618 unsigned num_successors =
619 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
621 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
628 typename Tr::RegionT *RegionInfoBase<Tr>::createRegion(BlockT *entry,
630 assert(entry && exit &&
"entry and exit must not be null!");
632 if (isTrivialRegion(entry, exit))
636 new RegionT(entry, exit, static_cast<RegionInfoT *>(
this), DT);
637 BBtoRegion.insert({entry, region});
639 #ifdef EXPENSIVE_CHECKS
640 region->verifyRegion();
642 DEBUG(region->verifyRegion());
645 updateStatistics(region);
650 void RegionInfoBase<Tr>::findRegionsWithEntry(BlockT *entry,
651 BBtoBBMap *ShortCut) {
654 DomTreeNodeT *N = PDT->getNode(entry);
658 RegionT *lastRegion =
nullptr;
659 BlockT *lastExit = entry;
663 while ((N = getNextPostDom(N, ShortCut))) {
664 BlockT *exit = N->getBlock();
669 if (isRegion(entry, exit)) {
670 RegionT *newRegion = createRegion(entry, exit);
673 newRegion->addSubRegion(lastRegion);
675 lastRegion = newRegion;
680 if (!DT->dominates(entry, exit))
686 if (lastExit != entry)
687 insertShortCut(entry, lastExit, ShortCut);
691 void RegionInfoBase<Tr>::scanForRegions(FuncT &
F, BBtoBBMap *ShortCut) {
692 typedef typename std::add_pointer<FuncT>::type FuncPtrT;
693 BlockT *entry = GraphTraits<FuncPtrT>::getEntryNode(&F);
694 DomTreeNodeT *N = DT->getNode(entry);
701 findRegionsWithEntry(DomNode->getBlock(), ShortCut);
705 typename Tr::RegionT *RegionInfoBase<Tr>::getTopMostParent(RegionT *region) {
706 while (region->getParent())
707 region = region->getParent();
713 void RegionInfoBase<Tr>::buildRegionsTree(DomTreeNodeT *N, RegionT *region) {
714 BlockT *BB = N->getBlock();
717 while (BB == region->getExit())
718 region = region->getParent();
720 typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
724 if (it != BBtoRegion.end()) {
725 RegionT *newRegion = it->second;
726 region->addSubRegion(getTopMostParent(newRegion));
729 BBtoRegion[BB] = region;
732 for (DomTreeNodeBase<BlockT> *
C : *N) {
733 buildRegionsTree(
C, region);
737 #ifdef EXPENSIVE_CHECKS
739 bool RegionInfoBase<Tr>::VerifyRegionInfo =
true;
742 bool RegionInfoBase<Tr>::VerifyRegionInfo =
false;
746 typename Tr::RegionT::PrintStyle RegionInfoBase<Tr>::printStyle =
747 RegionBase<Tr>::PrintNone;
751 OS <<
"Region tree:\n";
752 TopLevelRegion->print(OS,
true, 0, printStyle);
753 OS <<
"End region tree\n";
756 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
765 delete TopLevelRegion;
766 TopLevelRegion =
nullptr;
776 TopLevelRegion->verifyRegionNest();
778 verifyBBMap(TopLevelRegion);
785 return I != BBtoRegion.end() ? I->second :
nullptr;
795 return getRegionFor(BB);
799 typename RegionInfoBase<Tr>::BlockT *
801 BlockT *Exit =
nullptr;
805 RegionT *R = getRegionFor(BB);
806 while (R && R->getParent() && R->getParent()->getEntry() == BB)
810 if (R && R->getEntry() == BB)
812 else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
813 Exit = *BlockTraits::child_begin(BB);
818 RegionT *ExitR = getRegionFor(Exit);
819 while (ExitR && ExitR->getParent() &&
820 ExitR->getParent()->getEntry() == Exit)
821 ExitR = ExitR->getParent();
823 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(Exit),
824 InvBlockTraits::child_end(Exit))) {
825 if (!R->contains(Pred) && !ExitR->contains(Pred))
830 if (DT->dominates(Exit, BB))
842 assert(A && B &&
"One of the Regions is NULL");
847 while (!B->contains(A))
854 typename Tr::RegionT *
856 RegionT *ret = Regions.
back();
859 for (RegionT *R : Regions)
860 ret = getCommonRegion(ret, R);
866 typename Tr::RegionT *
868 RegionT *ret = getRegionFor(BBs.
back());
871 for (BlockT *BB : BBs)
872 ret = getCommonRegion(ret, getRegionFor(BB));
879 typedef typename std::add_pointer<FuncT>::type FuncPtrT;
886 scanForRegions(F, &ShortCut);
888 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
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?
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.
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.
void clearNodeCache()
Clear the cache for BB RegionNodes.
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)
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
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
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.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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 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...
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")