37 #ifndef LLVM_ANALYSIS_REGIONINFO_H
38 #define LLVM_ANALYSIS_REGIONINFO_H
56 #include <type_traits>
61 class DominanceFrontier;
65 struct PostDominatorTree;
75 template <
class FuncT_>
82 using BrokenT =
typename FuncT_::UnknownRegionTypeError;
112 template <
class GraphType>
183 template <
class T>
inline T *
getNodeAs()
const;
255 class RegionBase :
public RegionNodeBase<Tr> {
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,
372 return const_cast<RegionNodeT *
>(
373 reinterpret_cast<const RegionNodeT *
>(
this));
435 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
444 bool contains(
const BlockT *BB)
const;
455 return contains(SubRegion->getEntry()) &&
457 SubRegion->getExit() ==
getExit());
509 RegionNodeT *
getNode(BlockT *BB)
const;
515 RegionNodeT *
getBBNode(BlockT *BB)
const;
522 void addSubRegion(RegionT *SubRegion,
bool moveChildren =
false);
569 template <
bool IsConst>
572 typename std::conditional<IsConst, const BlockT, BlockT>::type *> {
575 typename std::conditional<IsConst, const BlockT, BlockT>::type *>;
662 inline raw_ostream &operator<<(raw_ostream &OS, const RegionNodeBase<Tr> &Node);
671 class RegionInfoBase {
675 using BlockT =
typename Tr::BlockT;
676 using FuncT =
typename Tr::FuncT;
677 using RegionT =
typename Tr::RegionT;
678 using RegionInfoT =
typename Tr::RegionInfoT;
679 using DomTreeT =
typename Tr::DomTreeT;
680 using DomTreeNodeT =
typename Tr::DomTreeNodeT;
681 using PostDomTreeT =
typename Tr::PostDomTreeT;
682 using DomFrontierT =
typename Tr::DomFrontierT;
685 using SuccIterTy =
typename BlockTraits::ChildIteratorType;
686 using PredIterTy =
typename InvBlockTraits::ChildIteratorType;
694 : DT(std::move(Arg.DT)), PDT(std::move(Arg.PDT)), DF(std::move(Arg.DF)),
695 TopLevelRegion(std::move(Arg.TopLevelRegion)),
696 BBtoRegion(std::move(Arg.BBtoRegion)) {
701 DT = std::move(RHS.DT);
702 PDT = std::move(RHS.PDT);
703 DF = std::move(RHS.DF);
704 TopLevelRegion = std::move(RHS.TopLevelRegion);
705 BBtoRegion = std::move(RHS.BBtoRegion);
710 virtual ~RegionInfoBase();
717 RegionT *TopLevelRegion =
nullptr;
720 BBtoRegionMap BBtoRegion;
727 template<
typename TheRegionT>
732 for (
auto &SubR : *R)
745 TopLevelRegion =
nullptr;
752 void verifyBBMap(
const RegionT *SR)
const;
757 bool isCommonDomFrontier(BlockT *BB, BlockT *entry, BlockT *exit)
const;
761 bool isRegion(BlockT *entry, BlockT *exit)
const;
765 void insertShortCut(BlockT *entry, BlockT *exit, BBtoBBMap *ShortCut)
const;
769 DomTreeNodeT *getNextPostDom(DomTreeNodeT *
N, BBtoBBMap *ShortCut)
const;
772 bool isTrivialRegion(BlockT *entry, BlockT *exit)
const;
775 RegionT *createRegion(BlockT *entry, BlockT *exit);
778 void findRegionsWithEntry(BlockT *entry, BBtoBBMap *ShortCut);
781 void scanForRegions(FuncT &
F, BBtoBBMap *ShortCut);
784 RegionT *getTopMostParent(RegionT *region);
787 void buildRegionsTree(DomTreeNodeT *
N, RegionT *region);
790 virtual void updateStatistics(RegionT *R) = 0;
793 void calculate(FuncT &
F);
796 RegionInfoBase(
const RegionInfoBase &) =
delete;
797 RegionInfoBase &operator=(
const RegionInfoBase &) =
delete;
803 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
870 TopLevelRegion->clearNodeCache();
884 return this ==
reinterpret_cast<const RegionNode *
>(&
RN);
891 Region *Parent =
nullptr);
895 return &RN ==
reinterpret_cast<const RegionNode *
>(
this);
910 Base::operator=(std::move(static_cast<Base &>(RHS)));
996 assert(!isSubRegion() &&
"This is not a BasicBlock RegionNode!");
1004 assert(isSubRegion() &&
"This is not a subregion RegionNode!");
1006 return reinterpret_cast<Region *
>(Unconst);
1012 using BlockT =
typename Tr::BlockT;
1013 using RegionT =
typename Tr::RegionT;
1016 return OS << Node.template getNodeAs<RegionT>()->getNameStr();
1018 return OS << Node.template getNodeAs<BlockT>()->
getName();
1021 extern template class RegionBase<RegionTraits<Function>>;
1022 extern template class RegionNodeBase<RegionTraits<Function>>;
1023 extern template class RegionInfoBase<RegionTraits<Function>>;
1027 #endif // LLVM_ANALYSIS_REGIONINFO_H
RegionT * getCommonRegion(BlockT *A, BlockT *B) const
Find the smallest region that contains two basic blocks.
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
std::string getNameStr() const
Returns the name of the Region.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
block_iterator_wrapper(value_type Entry, value_type Exit)
RegionNodeT * getNode() const
Get the RegionNode representing the current Region.
iterator_range< element_iterator > elements()
bool isSimple() const
Is this a simple region?
A Module instance is used to store all the information related to an LLVM module. ...
static RegionT::PrintStyle printStyle
const RegionInfo & getRegionInfo() const
void print(raw_ostream &OS, bool printTree=true, unsigned level=0, PrintStyle Style=PrintNone) const
Print the region.
block_iterator_wrapper(super I)
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.
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
RegionNodeBase(RegionT *Parent, BlockT *Entry, bool isSubRegion=false)
Create a RegionNode.
RegionInfo & operator=(RegionInfo &&RHS)
block_iterator_wrapper< true > const_block_iterator
bool contains(const InstT *Inst) const
Check if the region contains an Instruction.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
bool contains(const RegionT *SubRegion) const
Check if the region contains another region.
BlockT * getEntry() const
Get the entry BasicBlock of this RegionNode.
bool isSubRegion() const
Is this RegionNode a subregion?
void setRegionFor(BlockT *BB, RegionT *R)
Set the smallest region that surrounds a basic block.
RegionT * getTopLevelRegion() const
void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
T * getNodeAs() const
Get the content of this RegionNode.
Analysis that detects all canonical Regions.
RegionT * getRegionFor(BlockT *BB) const
Get the smallest region that contains a BasicBlock.
RegionInfoT * getRegionInfo() const
Return the RegionInfo object, that belongs to this Region.
iterator_range< const_block_iterator > const_block_range
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
unsigned getDepth() const
Get the nesting level of this Region.
void view()
Opens a viewer to show the GraphViz visualization of the regions.
LoopT * outermostLoopInRegion(LoopT *L) const
Get the outermost loop in the region that contains a loop.
typename RegionSet::const_iterator const_iterator
typename RegionTraits< Function >::RegionT RegionT
void clearNodeCache()
Clear the cache for BB RegionNodes.
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
RegionBase & operator=(const RegionBase &)=delete
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
RegionInfoPrinterPass(raw_ostream &OS)
RegionT * getParent() const
Get the parent of the Region.
static StringRef getName(Value *V)
RegionInfo & getRegionInfo()
block_range blocks()
Returns a range view of the basic blocks in the region.
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.
df_iterator< const RegionNodeT *, df_iterator_default_set< const RegionNodeT * >, false, GraphTraits< const RegionNodeT * >> const_element_iterator
void verifyAnalysis() const
PointerTy getPointer() const
Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT, Region *Parent=nullptr)
block_iterator_wrapper< false > block_iterator
A CRTP mix-in to automatically provide informational APIs needed for passes.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Base class for the actual dominator tree node.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
void updateRegionTree(RegionInfoT &RI, TheRegionT *R)
Update refences to a RegionInfoT held by the RegionT managed here.
const_block_iterator block_begin() const
RegionT * getParent() const
Get the parent Region of this RegionNode.
Verifier pass for the RegionInfo.
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
void dump() const
Print the region to stderr.
df_iterator< T > df_end(const T &G)
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()
A CRTP mix-in that provides informational APIs needed for analysis passes.
const_iterator begin() const
typename RegionTraits< Function >::BlockT BlockT
bool operator==(const RegionNode &RN) const
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
RegionInfo run(Function &F, FunctionAnalysisManager &AM)
void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
void transferChildrenTo(RegionT *To)
Move all direct child nodes of this Region to another Region.
const_block_range blocks() const
Returns a range view of the basic blocks in the region.
df_iterator< RegionNodeT *, df_iterator_default_set< RegionNodeT * >, false, GraphTraits< RegionNodeT * >> element_iterator
iterator_range< const_element_iterator > elements() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
RegionInfo(RegionInfo &&Arg)
~RegionInfoPass() override
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.
RegionT * getExpandedRegion() const
Return a new (non-canonical) region, that is obtained by joining this region with its predecessors...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
A single entry single exit Region.
Analysis pass that exposes the RegionInfo for a function.
typename super::value_type value_type
void replaceEntry(BlockT *BB)
Replace the entry basic block of the region with the new basic block.
df_iterator< T > df_begin(const T &G)
A range adaptor for a pair of iterators.
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Create a new region.
element_iterator element_begin()
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
block_iterator block_begin()
block_iterator block_end()
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.
Represents a single loop in the control flow graph.
bool operator==(const Region &RN) const
RegionNode(Region *Parent, BasicBlock *Entry, bool isSubRegion=false)
typename FuncT_::UnknownRegionTypeError BrokenT
void verifyRegion() const
Verify if the region is a correct region.
Marker class to iterate over the elements of a Region in flat mode.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
API to communicate dependencies between analyses during invalidation.
iterator_range< block_iterator > block_range
~RegionBase()
Delete the Region and all its subregions.
BlockT * getExit() const
Get the exit BasicBlock of the Region.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
RegionT * operator[](BlockT *BB) const
A shortcut for getRegionFor().
void clearNodeCache()
Clear the Node Cache for all Regions.
DomTreeNodeBase< BasicBlock > DomTreeNode
RegionT * getCommonRegion(RegionT *A, RegionT *B) const
Find the smallest region that contains two regions.
BlockT * getEntry() const
Get the entry BasicBlock of the Region.
RegionNodeBase & operator=(const RegionNodeBase &)=delete
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
const_block_iterator block_end() const
static bool VerifyRegionInfo
void updateStatistics(Region *R) final
This class implements an extremely fast bulk output stream that can only output to a stream...
const_iterator end() const
BlockT * operator*() const
const NodeRef & operator*() const
A container for analyses that lazily runs them and caches their results.
RegionT * removeSubRegion(RegionT *SubRegion)
Remove a subregion from this Region.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
This header defines various interfaces for pass management in LLVM.
Printer pass for the RegionInfo.
typename RegionSet::iterator iterator
A special type used by analysis passes to provide an address that identifies that particular analysis...
static unsigned getNumSuccessors(BasicBlock *BB)
bool isTopLevelRegion() const
Check if a Region is the TopLevel region.