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;
574 template <
bool IsConst>
576 :
public df_iterator<
577 std::conditional_t<IsConst, const BlockT, BlockT> *> {
579 df_iterator<std::conditional_t<IsConst, const BlockT, BlockT> *>;
666inline raw_ostream &
operator<<(raw_ostream &OS,
const RegionNodeBase<Tr> &
Node);
675class RegionInfoBase {
679 using BlockT =
typename Tr::BlockT;
680 using FuncT =
typename Tr::FuncT;
681 using RegionT =
typename Tr::RegionT;
682 using RegionInfoT =
typename Tr::RegionInfoT;
683 using DomTreeT =
typename Tr::DomTreeT;
684 using DomTreeNodeT =
typename Tr::DomTreeNodeT;
685 using PostDomTreeT =
typename Tr::PostDomTreeT;
686 using DomFrontierT =
typename Tr::DomFrontierT;
689 using SuccIterTy =
typename BlockTraits::ChildIteratorType;
690 using PredIterTy =
typename InvBlockTraits::ChildIteratorType;
697 RegionInfoBase(RegionInfoBase &&Arg)
699 TopLevelRegion(
std::
move(Arg.TopLevelRegion)),
700 BBtoRegion(
std::
move(Arg.BBtoRegion)) {
705 DT = std::move(
RHS.DT);
706 PDT = std::move(
RHS.PDT);
707 DF = std::move(
RHS.DF);
708 TopLevelRegion = std::move(
RHS.TopLevelRegion);
709 BBtoRegion = std::move(
RHS.BBtoRegion);
721 RegionT *TopLevelRegion =
nullptr;
724 BBtoRegionMap BBtoRegion;
731 template<
typename TheRegionT>
736 for (
auto &SubR : *R)
749 TopLevelRegion =
nullptr;
756 void verifyBBMap(
const RegionT *SR)
const;
761 bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit)
const;
765 bool isRegion(BlockT *entry, BlockT *exit)
const;
769 void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut)
const;
773 DomTreeNodeT *getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const;
776 bool isTrivialRegion(BlockT *entry, BlockT *exit)
const;
779 RegionT *createRegion(BlockT *entry, BlockT *exit);
782 void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
785 void scanForRegions(FuncT &
F, BBtoBBMap *ShortCut);
788 RegionT *getTopMostParent(RegionT *
region);
791 void buildRegionsTree(DomTreeNodeT *
N, RegionT *
region);
794 void calculate(FuncT &
F);
798 RegionInfoBase &
operator=(
const RegionInfoBase &) =
delete;
804#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
871 TopLevelRegion->clearNodeCache();
885 return this ==
reinterpret_cast<const RegionNode *
>(&RN);
898 return &RN ==
reinterpret_cast<const RegionNode *
>(
this);
904class RegionInfo :
public RegionInfoBase<RegionTraits<Function>> {
906 using Base = RegionInfoBase<RegionTraits<Function>>;
915 Base::operator=(std::move(
static_cast<Base &
>(
RHS)));
924 FunctionAnalysisManager::Invalidator &);
959 void releaseMemory()
override;
960 void verifyAnalysis()
const override;
1009 return reinterpret_cast<Region *
>(Unconst);
1015 using BlockT =
typename Tr::BlockT;
1016 using RegionT =
typename Tr::RegionT;
1018 if (
Node.isSubRegion())
1019 return OS <<
Node.template getNodeAs<RegionT>()->getNameStr();
1021 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.
void clearNodeCache()
Clear the cache for BB RegionNodes.
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.
void clearNodeCache()
Clear the Node Cache for all Regions.
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.