11 #ifndef LLVM_ANALYSIS_REGIONINFOIMPL_H 12 #define LLVM_ANALYSIS_REGIONINFOIMPL_H 24 #include "llvm/Config/llvm-config.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;
183 bool CoverAll =
true;
188 for (PredIterTy PI = InvBlockTraits::child_begin(exit),
189 PE = InvBlockTraits::child_end(exit);
205 BlockT *exit = getExit();
206 BlockT *exitingBlock =
nullptr;
211 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(exit),
212 InvBlockTraits::child_end(exit))) {
226 return !isTopLevelRegion() && getEnteringBlock() && getExitingBlock();
231 std::string exitName;
232 std::string entryName;
237 getEntry()->printAsOperand(OS,
false);
239 entryName = getEntry()->getName();
245 getExit()->printAsOperand(OS,
false);
247 exitName = getExit()->getName();
249 exitName =
"<Function Return>";
251 return entryName +
" => " + exitName;
259 BlockT *
entry = getEntry(), *exit = getExit();
262 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
263 if (!
contains(Succ) && exit != Succ)
265 "to the exit node!");
269 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(BB),
270 InvBlockTraits::child_end(BB))) {
273 "go to the entry node!");
280 BlockT *exit = getExit();
284 verifyBBInRegion(BB);
287 make_range(BlockTraits::child_begin(BB), BlockTraits::child_end(BB))) {
288 if (Succ != exit && visited->find(Succ) == visited->end())
289 verifyWalk(Succ, visited);
301 std::set<BlockT *> visited;
302 verifyWalk(getEntry(), &visited);
307 for (
const std::unique_ptr<RegionT> &R : *
this)
308 R->verifyRegionNest();
327 static_cast<const RegionT *>(
this));
334 static_cast<const RegionT *>(
this));
339 using RegionT =
typename Tr::RegionT;
341 RegionT *R = RI->getRegionFor(BB);
349 while (
contains(R->getParent()) && R->getParent() !=
this)
352 if (R->getEntry() != BB)
362 typename BBNodeMapT::const_iterator at = BBNodeMap.find(BB);
364 if (at == BBNodeMap.end()) {
366 typename BBNodeMapT::value_type V = {
368 std::make_unique<RegionNodeT>(
static_cast<RegionT *
>(Deconst), BB)};
369 at = BBNodeMap.insert(std::move(V)).first;
371 return at->second.get();
377 if (RegionT *Child = getSubRegionNode(BB))
378 return Child->getNode();
380 return getBBNode(BB);
385 for (std::unique_ptr<RegionT> &R : *
this) {
387 To->children.push_back(std::move(R));
394 assert(!SubRegion->parent &&
"SubRegion already has a parent!");
396 [&](
const std::unique_ptr<RegionT> &R) {
397 return R.get() == SubRegion;
399 "Subregion already exists!");
401 SubRegion->parent =
static_cast<RegionT *
>(
this);
402 children.push_back(std::unique_ptr<RegionT>(SubRegion));
407 assert(SubRegion->children.empty() &&
408 "SubRegions that contain children are not supported");
410 for (RegionNodeT *Element : elements()) {
411 if (!Element->isSubRegion()) {
412 BlockT *BB = Element->template getNodeAs<BlockT>();
414 if (SubRegion->contains(BB))
415 RI->setRegionFor(BB, SubRegion);
419 std::vector<std::unique_ptr<RegionT>> Keep;
420 for (std::unique_ptr<RegionT> &R : *
this) {
421 if (SubRegion->contains(R.get()) && R.get() != SubRegion) {
422 R->parent = SubRegion;
423 SubRegion->children.push_back(std::move(R));
425 Keep.push_back(std::move(R));
431 std::move_iterator<typename RegionSet::iterator>(Keep.begin()),
432 std::move_iterator<typename RegionSet::iterator>(Keep.end()));
437 assert(Child->parent ==
this &&
"Child is not a child of this region!");
438 Child->parent =
nullptr;
439 typename RegionSet::iterator
I =
441 return R.get() == Child;
443 assert(I !=
children.end() &&
"Region does not exit. Unable to remove.");
452 for (RegionT *R =
getParent(); R !=
nullptr; R = R->getParent())
460 unsigned NumSuccessors = Tr::getNumSuccessors(exit);
462 if (NumSuccessors == 0)
465 RegionT *R = RI->getRegionFor(exit);
467 if (R->getEntry() != exit) {
468 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(getExit()),
469 InvBlockTraits::child_end(getExit())))
472 if (Tr::getNumSuccessors(exit) == 1)
473 return new RegionT(getEntry(), *BlockTraits::child_begin(exit), RI, DT);
477 while (R->getParent() && R->getParent()->getEntry() == exit)
480 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(getExit()),
481 InvBlockTraits::child_end(getExit()))) {
482 if (!(
contains(Pred) || R->contains(Pred)))
486 return new RegionT(getEntry(), R->getExit(), RI, DT);
493 OS.
indent(level * 2) <<
'[' << level <<
"] " << getNameStr();
495 OS.
indent(level * 2) << getNameStr();
499 if (Style != PrintNone) {
500 OS.
indent(level * 2) <<
"{\n";
503 if (Style == PrintBB) {
504 for (
const auto *BB : blocks())
505 OS << BB->getName() <<
", ";
506 }
else if (Style == PrintRN) {
507 for (
const RegionNodeT *Element : elements()) {
508 OS << *Element <<
", ";
516 for (
const std::unique_ptr<RegionT> &R : *
this)
517 R->print(OS, print_tree, level + 1, Style);
520 if (Style != PrintNone)
521 OS.
indent(level * 2) <<
"} \n";
524 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 534 for (std::unique_ptr<RegionT> &R : *
this)
552 assert(R &&
"Re must be non-null");
553 for (
const typename Tr::RegionNodeT *Element : R->elements()) {
554 if (Element->isSubRegion()) {
555 const RegionT *SR = Element->template getNodeAs<RegionT>();
558 BlockT *BB = Element->template getNodeAs<BlockT>();
559 if (getRegionFor(BB) != R)
567 BlockT *exit)
const {
568 for (BlockT *
P :
make_range(InvBlockTraits::child_begin(BB),
569 InvBlockTraits::child_end(BB))) {
570 if (DT->dominates(entry,
P) && !DT->dominates(exit,
P))
579 assert(entry && exit &&
"entry and exit must not be null!");
581 using DST =
typename DomFrontierT::DomSetType;
583 DST *entrySuccs = &
DF->find(entry)->second;
587 if (!DT->dominates(entry, exit)) {
588 for (
typename DST::iterator
SI = entrySuccs->begin(),
589 SE = entrySuccs->end();
591 if (*
SI != exit && *
SI != entry)
598 DST *exitSuccs = &
DF->find(exit)->second;
601 for (BlockT *Succ : *entrySuccs) {
602 if (Succ == exit || Succ == entry)
604 if (exitSuccs->find(Succ) == exitSuccs->end())
606 if (!isCommonDomFrontier(Succ, entry, exit))
611 for (BlockT *Succ : *exitSuccs) {
612 if (DT->properlyDominates(entry, Succ) && Succ != exit)
621 BBtoBBMap *ShortCut)
const {
622 assert(entry && exit &&
"entry and exit must not be null!");
624 typename BBtoBBMap::iterator
e = ShortCut->find(exit);
626 if (e == ShortCut->end())
628 (*ShortCut)[
entry] = exit;
633 BlockT *BB = e->second;
634 (*ShortCut)[
entry] = BB;
639 typename Tr::DomTreeNodeT *
641 typename BBtoBBMap::iterator
e = ShortCut->find(N->getBlock());
643 if (e == ShortCut->end())
646 return PDT->getNode(e->second)->getIDom();
651 assert(entry && exit &&
"entry and exit must not be null!");
653 unsigned num_successors =
654 BlockTraits::child_end(entry) - BlockTraits::child_begin(entry);
656 if (num_successors <= 1 && exit == *(BlockTraits::child_begin(entry)))
665 assert(entry && exit &&
"entry and exit must not be null!");
667 if (isTrivialRegion(entry, exit))
671 new RegionT(entry, exit, static_cast<RegionInfoT *>(
this), DT);
672 BBtoRegion.insert({
entry, region});
674 #ifdef EXPENSIVE_CHECKS 675 region->verifyRegion();
680 updateStatistics(region);
686 BBtoBBMap *ShortCut) {
689 DomTreeNodeT *N = PDT->getNode(entry);
693 RegionT *lastRegion =
nullptr;
694 BlockT *lastExit =
entry;
698 while ((N = getNextPostDom(N, ShortCut))) {
699 BlockT *exit = N->getBlock();
704 if (isRegion(entry, exit)) {
705 RegionT *newRegion = createRegion(entry, exit);
708 newRegion->addSubRegion(lastRegion);
710 lastRegion = newRegion;
715 if (!DT->dominates(entry, exit))
721 if (lastExit != entry)
722 insertShortCut(entry, lastExit, ShortCut);
727 using FuncPtrT =
typename std::add_pointer<FuncT>::type;
730 DomTreeNodeT *N = DT->getNode(entry);
737 findRegionsWithEntry(DomNode->getBlock(), ShortCut);
742 while (region->getParent())
743 region = region->getParent();
750 BlockT *BB = N->getBlock();
753 while (BB == region->getExit())
754 region = region->getParent();
756 typename BBtoRegionMap::iterator it = BBtoRegion.find(BB);
760 if (it != BBtoRegion.end()) {
761 RegionT *newRegion = it->second;
762 region->addSubRegion(getTopMostParent(newRegion));
765 BBtoRegion[BB] = region;
769 buildRegionsTree(
C, region);
773 #ifdef EXPENSIVE_CHECKS 778 bool RegionInfoBase<Tr>::VerifyRegionInfo =
false;
787 OS <<
"Region tree:\n";
788 TopLevelRegion->print(OS,
true, 0, printStyle);
789 OS <<
"End region tree\n";
792 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 801 delete TopLevelRegion;
802 TopLevelRegion =
nullptr;
809 if (!RegionInfoBase<Tr>::VerifyRegionInfo)
812 TopLevelRegion->verifyRegionNest();
814 verifyBBMap(TopLevelRegion);
821 return I != BBtoRegion.end() ? I->second :
nullptr;
831 return getRegionFor(BB);
835 typename RegionInfoBase<Tr>::BlockT *
837 BlockT *Exit =
nullptr;
841 RegionT *R = getRegionFor(BB);
842 while (R && R->getParent() && R->getParent()->getEntry() == BB)
846 if (R && R->getEntry() == BB)
848 else if (++BlockTraits::child_begin(BB) == BlockTraits::child_end(BB))
849 Exit = *BlockTraits::child_begin(BB);
854 RegionT *ExitR = getRegionFor(Exit);
855 while (ExitR && ExitR->getParent() &&
856 ExitR->getParent()->getEntry() == Exit)
857 ExitR = ExitR->getParent();
859 for (BlockT *Pred :
make_range(InvBlockTraits::child_begin(Exit),
860 InvBlockTraits::child_end(Exit))) {
861 if (!R->contains(Pred) && !ExitR->contains(Pred))
866 if (DT->dominates(Exit, BB))
878 assert(A && B &&
"One of the Regions is NULL");
883 while (!B->contains(A))
890 typename Tr::RegionT *
892 RegionT *ret = Regions.
back();
895 for (RegionT *R : Regions)
896 ret = getCommonRegion(ret, R);
902 typename Tr::RegionT *
904 RegionT *ret = getRegionFor(BBs.
back());
907 for (BlockT *BB : BBs)
908 ret = getCommonRegion(ret, getRegionFor(BB));
915 using FuncPtrT =
typename std::add_pointer<FuncT>::type;
922 scanForRegions(F, &ShortCut);
924 buildRegionsTree(DT->getNode(BB), TopLevelRegion);
931 #endif // LLVM_ANALYSIS_REGIONINFOIMPL_H void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
bool getExitingBlocks(SmallVectorImpl< BlockT *> &Exitings) const
Collect all blocks of this region's single exit edge, if existing.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
BlockT * getEnteringBlock() const
Return the first block of this region's single entry edge, if existing.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
void push_back(const T &Elt)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void print(raw_ostream &OS) const
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
unsigned getDepth() const
Get the nesting level of this Region.
void setRegionFor(BlockT *BB, RegionT *R)
Set the smallest region that surrounds a basic block.
Analysis that detects all canonical Regions.
return AArch64::GPR64RegClass contains(Reg)
static void verifyRegion(const VPRegionBlock *Region)
Verify the CFG invariants of VPRegionBlock Region and its nested VPBlockBases.
RegionT * getCommonRegion(RegionT *A, RegionT *B) const
Find the smallest region that contains two regions.
void clearNodeCache()
Clear the cache for BB RegionNodes.
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
static StringRef getName(Value *V)
void verifyAnalysis() const
PrintStyle
PrintStyle - Print region in difference ways.
Base class for the actual dominator tree node.
RegionT * getExpandedRegion() const
Return a new (non-canonical) region, that is obtained by joining this region with its predecessors...
bool isSimple() const
Is this a simple region?
RegionNodeT * getBBNode(BlockT *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
BlockT * getMaxRegionExit(BlockT *BB) const
Return the exit of the maximal refined region, that starts at a BasicBlock.
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()
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
iterator_range< po_iterator< T > > post_order(const T &G)
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
void transferChildrenTo(RegionT *To)
Move all direct child nodes of this Region to another Region.
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
LoopT * outermostLoopInRegion(LoopT *L) const
Get the outermost loop in the region that contains a loop.
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.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static unsigned getDepth(const SmallVectorImpl< const MachineBasicBlock *> &Stack, const MachineBasicBlock *MBB)
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()
std::string getNameStr() const
Returns the name of the Region.
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
void replaceExitRecursive(BlockT *NewExit)
Recursively replace the exit basic block of the region.
void verifyRegion() const
Verify if the region is a correct region.
RegionT * operator[](BlockT *BB) const
A shortcut for getRegionFor().
~RegionBase()
Delete the Region and all its subregions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
print Instructions which execute on loop entry
RegionT * removeSubRegion(RegionT *SubRegion)
Remove a subregion from this Region.
RegionT * getSubRegionNode(BlockT *BB) const
Get the subregion that starts at a BasicBlock.
BlockT * getExitingBlock() const
Return the first block of this region's single exit edge, if existing.
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Create a new region.