36#ifndef LLVM_ANALYSIS_REGIONINFO_H
37#define LLVM_ANALYSIS_REGIONINFO_H
72template <
class FuncT_>
79 using BrokenT =
typename FuncT_::UnknownRegionTypeError;
112template <
class GraphType>
258 using FuncT =
typename Tr::FuncT;
259 using BlockT =
typename Tr::BlockT;
260 using RegionInfoT =
typename Tr::RegionInfoT;
261 using RegionT =
typename Tr::RegionT;
262 using RegionNodeT =
typename Tr::RegionNodeT;
263 using DomTreeT =
typename Tr::DomTreeT;
264 using LoopT =
typename Tr::LoopT;
265 using LoopInfoT =
typename Tr::LoopInfoT;
266 using InstT =
typename Tr::InstT;
270 using SuccIterTy =
typename BlockTraits::ChildIteratorType;
271 using PredIterTy =
typename InvBlockTraits::ChildIteratorType;
281 using RegionSet = std::vector<std::unique_ptr<RegionT>>;
286 using BBNodeMapT = std::map<BlockT *, std::unique_ptr<RegionNodeT>>;
289 mutable BBNodeMapT BBNodeMap;
293 void verifyBBInRegion(BlockT *BB)
const;
298 void verifyWalk(BlockT *BB, std::set<BlockT *> *visitedBB)
const;
301 void verifyRegionNest()
const;
312 RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT,
313 RegionT *Parent =
nullptr);
372 return const_cast<RegionNodeT *
>(
373 reinterpret_cast<const RegionNodeT *
>(
this));
440#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
460 return contains(SubRegion->getEntry()) &&
462 SubRegion->getExit() ==
getExit());
479 bool contains(
const LoopT *L)
const;
567 template <
bool IsConst>
569 :
public df_iterator<
570 std::conditional_t<IsConst, const BlockT, BlockT> *> {
572 df_iterator<std::conditional_t<IsConst, const BlockT, BlockT> *>;
659inline raw_ostream &
operator<<(raw_ostream &OS,
const RegionNodeBase<Tr> &
Node);
668class RegionInfoBase {
672 using BlockT =
typename Tr::BlockT;
673 using FuncT =
typename Tr::FuncT;
674 using RegionT =
typename Tr::RegionT;
675 using RegionInfoT =
typename Tr::RegionInfoT;
676 using DomTreeT =
typename Tr::DomTreeT;
677 using DomTreeNodeT =
typename Tr::DomTreeNodeT;
678 using PostDomTreeT =
typename Tr::PostDomTreeT;
679 using DomFrontierT =
typename Tr::DomFrontierT;
682 using SuccIterTy =
typename BlockTraits::ChildIteratorType;
683 using PredIterTy =
typename InvBlockTraits::ChildIteratorType;
690 RegionInfoBase(RegionInfoBase &&Arg)
692 TopLevelRegion(
std::
move(Arg.TopLevelRegion)),
693 BBtoRegion(
std::
move(Arg.BBtoRegion)) {
698 DT = std::move(
RHS.DT);
699 PDT = std::move(
RHS.PDT);
700 DF = std::move(
RHS.DF);
701 TopLevelRegion = std::move(
RHS.TopLevelRegion);
702 BBtoRegion = std::move(
RHS.BBtoRegion);
714 RegionT *TopLevelRegion =
nullptr;
717 BBtoRegionMap BBtoRegion;
724 template<
typename TheRegionT>
729 for (
auto &SubR : *R)
742 TopLevelRegion =
nullptr;
749 void verifyBBMap(
const RegionT *SR)
const;
754 bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit)
const;
758 bool isRegion(BlockT *entry, BlockT *exit)
const;
762 void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut)
const;
766 DomTreeNodeT *getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const;
769 bool isTrivialRegion(BlockT *entry, BlockT *exit)
const;
772 RegionT *createRegion(BlockT *entry, BlockT *exit);
775 void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
778 void scanForRegions(FuncT &
F, BBtoBBMap *ShortCut);
781 RegionT *getTopMostParent(RegionT *
region);
784 void buildRegionsTree(DomTreeNodeT *
N, RegionT *
region);
787 void calculate(FuncT &
F);
791 RegionInfoBase &
operator=(
const RegionInfoBase &) =
delete;
797#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
870 return this ==
reinterpret_cast<const RegionNode *
>(&RN);
883 return &RN ==
reinterpret_cast<const RegionNode *
>(
this);
889class RegionInfo :
public RegionInfoBase<RegionTraits<Function>> {
891 using Base = RegionInfoBase<RegionTraits<Function>>;
900 Base::operator=(std::move(
static_cast<Base &
>(
RHS)));
909 FunctionAnalysisManager::Invalidator &);
944 void releaseMemory()
override;
945 void verifyAnalysis()
const override;
994 return reinterpret_cast<Region *
>(Unconst);
1000 using BlockT =
typename Tr::BlockT;
1001 using RegionT =
typename Tr::RegionT;
1003 if (
Node.isSubRegion())
1004 return OS <<
Node.template getNodeAs<RegionT>()->getNameStr();
1006 return OS <<
Node.template getNodeAs<BlockT>()->getName();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_TEMPLATE_ABI
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
This header defines various interfaces for pass management in LLVM.
This file defines the PointerIntPair class.
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Marker class to iterate over the elements of a Region in flat mode.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
Represents a single loop in the control flow graph.
A Module instance is used to store all the information related to an LLVM module.
PointerIntPair - This class implements a pair of a pointer and small integer.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
typename super::value_type value_type
BlockT * operator*() const
block_iterator_wrapper< IsConst > Self
block_iterator_wrapper(super I)
block_iterator_wrapper(value_type Entry, value_type Exit)
A single entry single exit Region.
void replaceExit(BlockT *BB)
Replace the exit basic block of the region with the new basic block.
std::string getNameStr() const
Returns the name of the Region.
block_range blocks()
Returns a range view of the basic blocks in the region.
BlockT * getExit() const
Get the exit BasicBlock of the Region.
void transferChildrenTo(RegionT *To)
Move all direct child nodes of this Region to another Region.
RegionNodeT * getBBNode(BlockT *BB) const
Get the BasicBlock RegionNode for a BasicBlock.
block_iterator_wrapper< true > const_block_iterator
block_iterator block_begin()
bool getExitingBlocks(SmallVectorImpl< BlockT * > &Exitings) const
Collect all blocks of this region's single exit edge, if existing.
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
iterator_range< element_iterator > elements()
block_iterator_wrapper< false > block_iterator
const_iterator end() const
const_block_iterator block_end() const
LoopT * outermostLoopInRegion(LoopT *L) const
Get the outermost loop in the region that contains a loop.
unsigned getDepth() const
Get the nesting level of this Region.
const_block_iterator block_begin() const
void replaceEntry(BlockT *BB)
Replace the entry basic block of the region with the new basic block.
void dump() const
Print the region to stderr.
block_iterator block_end()
void replaceEntryRecursive(BlockT *NewEntry)
Recursively replace the entry basic block of the region.
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
element_iterator element_begin()
void replaceExitRecursive(BlockT *NewExit)
Recursively replace the exit basic block of the region.
void verifyRegion() const
Verify if the region is a correct region.
RegionBase & operator=(const RegionBase &)=delete
RegionT * getParent() const
Get the parent of the Region.
RegionInfoT * getRegionInfo() const
Return the RegionInfo object, that belongs to this Region.
BlockT * getEntry() const
Get the entry BasicBlock of the Region.
void addSubRegion(RegionT *SubRegion, bool moveChildren=false)
Add a new subregion to this Region.
RegionBase(const RegionBase &)=delete
const_iterator begin() const
bool contains(const InstT *Inst) const
Check if the region contains an Instruction.
typename RegionSet::iterator iterator
element_iterator element_end()
bool isSimple() const
Is this a simple region?
bool isTopLevelRegion() const
Check if a Region is the TopLevel region.
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Create a new region.
bool contains(const RegionT *SubRegion) const
Check if the region contains another region.
BlockT * getExitingBlock() const
Return the first block of this region's single exit edge, if existing.
~RegionBase()
Delete the Region and all its subregions.
iterator_range< const_block_iterator > const_block_range
df_iterator< const RegionNodeT *, df_iterator_default_set< const RegionNodeT * >, false, GraphTraits< const RegionNodeT * > > const_element_iterator
iterator_range< const_element_iterator > elements() const
PrintStyle
PrintStyle - Print region in difference ways.
const_block_range blocks() const
Returns a range view of the basic blocks in the region.
RegionT * removeSubRegion(RegionT *SubRegion)
Remove a subregion from this Region.
typename RegionSet::const_iterator const_iterator
BlockT * getEnteringBlock() const
Return the first block of this region's single entry edge, if existing.
RegionT * getExpandedRegion() const
Return a new (non-canonical) region, that is obtained by joining this region with its predecessors.
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
RegionT * getSubRegionNode(BlockT *BB) const
Get the subregion that starts at a BasicBlock.
df_iterator< RegionNodeT *, df_iterator_default_set< RegionNodeT * >, false, GraphTraits< RegionNodeT * > > element_iterator
iterator_range< block_iterator > block_range
Analysis pass that exposes the RegionInfo for a function.
LLVM_ABI RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Analysis that detects all canonical Regions.
RegionT * getCommonRegion(BlockT *A, BlockT *B) const
Find the smallest region that contains two basic blocks.
void updateRegionTree(RegionInfoT &RI, TheRegionT *R)
Update refences to a RegionInfoT held by the RegionT managed here.
BlockT * getMaxRegionExit(BlockT *BB) const
Return the exit of the maximal refined region, that starts at a BasicBlock.
static bool VerifyRegionInfo
RegionT * getCommonRegion(SmallVectorImpl< BlockT * > &BBs) const
Find the smallest region that contains a set of basic blocks.
void print(raw_ostream &OS) const
RegionT * getTopLevelRegion() const
friend class MachineRegionInfo
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
RegionInfoBase & operator=(const RegionInfoBase &)=delete
void verifyAnalysis() const
void setRegionFor(BlockT *BB, RegionT *R)
Set the smallest region that surrounds a basic block.
RegionT * operator[](BlockT *BB) const
A shortcut for getRegionFor().
RegionInfoBase(const RegionInfoBase &)=delete
static RegionT::PrintStyle printStyle
RegionT * getCommonRegion(RegionT *A, RegionT *B) const
Find the smallest region that contains two regions.
RegionT * getCommonRegion(SmallVectorImpl< RegionT * > &Regions) const
Find the smallest region that contains a set of regions.
~RegionInfoPass() override
RegionInfo & getRegionInfo()
const RegionInfo & getRegionInfo() const
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI RegionInfoPrinterPass(raw_ostream &OS)
RegionInfo(RegionInfo &&Arg)
LLVM_ABI void view()
Opens a viewer to show the GraphViz visualization of the regions.
LLVM_ABI void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
LLVM_ABI void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
RegionInfo & operator=(RegionInfo &&RHS)
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
RegionInfoBase< RegionTraits< Function > > Base
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
typename Tr::RegionT RegionT
RegionNodeBase & operator=(const RegionNodeBase &)=delete
RegionNodeBase(RegionT *Parent, BlockT *Entry, bool isSubRegion=false)
Create a RegionNode.
bool isSubRegion() const
Is this RegionNode a subregion?
RegionT * getParent() const
Get the parent Region of this RegionNode.
T * getNodeAs() const
Get the content of this RegionNode.
RegionNodeBase(const RegionNodeBase &)=delete
typename Tr::BlockT BlockT
BlockT * getEntry() const
Get the entry BasicBlock of this RegionNode.
BasicBlock * getNodeAs() const
RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion=false)
bool operator==(const Region &RN) const
bool operator==(const RegionNode &RN) const
LLVM_ABI Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT, Region *Parent=nullptr)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference operator*() const
typename GraphTraits< std::conditional_t< IsConst, const BlockT, BlockT > * >::NodeRef value_type
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
df_iterator< T > df_begin(const T &G)
DomTreeNodeBase< BasicBlock > DomTreeNode
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
df_iterator< T > df_end(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Verifier pass for the RegionInfo.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PostDominatorTree PostDomTreeT
DominanceFrontier DomFrontierT
static unsigned getNumSuccessors(BasicBlock *BB)
typename FuncT_::UnknownRegionTypeError BrokenT
A CRTP mix-in for passes that should not be skipped.