24 #ifndef LLVM_SUPPORT_GENERICDOMTREE_H
25 #define LLVM_SUPPORT_GENERICDOMTREE_H
38 #include <type_traits>
44 template <
typename NodeT,
bool IsPostDom>
45 class DominatorTreeBase;
47 namespace DomTreeBuilder {
48 template <
class DomTreeT>
63 std::vector<DomTreeNodeBase *> Children;
64 mutable unsigned DFSNumIn = ~0;
65 mutable unsigned DFSNumOut = ~0;
69 : TheBB(BB), IDom(iDom),
Level(IDom ? IDom->
Level + 1 : 0) {}
71 using iterator =
typename std::vector<DomTreeNodeBase *>::iterator;
73 typename std::vector<DomTreeNodeBase *>::const_iterator;
84 const std::vector<DomTreeNodeBase *> &
getChildren()
const {
return Children; }
87 std::unique_ptr<DomTreeNodeBase>
C) {
88 Children.push_back(C.get());
100 if (
Level != Other->Level)
return true;
104 const NodeT *Nd =
I->getBlock();
110 if (OtherChildren.
count(N) == 0)
117 assert(IDom &&
"No immediate dominator?");
118 if (IDom == NewIDom)
return;
120 auto I =
find(IDom->Children,
this);
121 assert(
I != IDom->Children.end() &&
122 "Not in immediate dominator children set!");
124 IDom->Children.erase(
I);
128 IDom->Children.push_back(
this);
143 return this->DFSNumIn >= other->DFSNumIn &&
144 this->DFSNumOut <= other->DFSNumOut;
149 if (
Level == IDom->Level + 1)
return;
151 SmallVector<DomTreeNodeBase *, 64> WorkStack = {
this};
153 while (!WorkStack.empty()) {
155 Current->Level = Current->IDom->Level + 1;
159 if (
C->Level !=
C->IDom->Level + 1) WorkStack.push_back(
C);
165 template <
class NodeT>
166 raw_ostream &operator<<(raw_ostream &O, const DomTreeNodeBase<NodeT> *Node) {
167 if (Node->getBlock())
168 Node->getBlock()->printAsOperand(O,
false);
170 O <<
" <<exit node>>";
172 O <<
" {" << Node->getDFSNumIn() <<
"," << Node->getDFSNumOut() <<
"} ["
173 << Node->getLevel() <<
"]\n";
178 template <
class NodeT>
181 O.
indent(2 * Lev) <<
"[" << Lev <<
"] " <<
N;
185 PrintDomTree<NodeT>(*
I, O, Lev + 1);
188 namespace DomTreeBuilder {
190 template <
typename DomTreeT,
typename FuncT>
193 template <
class DomTreeT>
194 void InsertEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
195 typename DomTreeT::NodePtr To);
197 template <
class DomTreeT>
198 void DeleteEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
199 typename DomTreeT::NodePtr To);
201 template <
typename DomTreeT>
202 bool Verify(
const DomTreeT &DT);
209 template <
typename NodeT,
bool IsPostDom>
210 class DominatorTreeBase {
228 "Currently DominatorTreeBase supports only pointer nodes");
246 Roots = std::move(RHS.Roots);
272 if (
Parent != Other.Parent)
return true;
280 typename DomTreeNodeMapType::const_iterator OI =
281 OtherDomTreeNodes.find(BB);
282 if (OI == OtherDomTreeNodes.end())
304 return I->second.get();
330 while (!WL.
empty()) {
355 "This is not implemented for post dominators");
380 if (A->
getIDom() ==
B)
return false;
387 #ifdef EXPENSIVE_CHECKS
389 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
390 "Tree walk disagrees with dfs numbers!");
394 return B->DominatedBy(A);
401 return B->DominatedBy(A);
404 return dominatedBySlowTreeWalk(A, B);
407 bool dominates(
const NodeT *
A,
const NodeT *
B)
const;
410 assert(this->
Roots.size() == 1 &&
"Should always have entry node!");
411 return this->
Roots[0];
417 assert(A->getParent() == B->getParent() &&
418 "Two blocks are not in same function");
423 NodeT &Entry = A->getParent()->front();
424 if (A == &Entry || B == &Entry)
431 if (!NodeA || !NodeB)
return nullptr;
435 while (NodeA && NodeA != NodeB) {
441 return NodeA ? NodeA->
getBlock() :
nullptr;
445 const NodeT *
B)
const {
449 const_cast<NodeT *>(B));
506 assert(
getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
508 assert(IDomNode &&
"Not immediate dominator specified for block!");
520 assert(
getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
522 "Cannot change root of post-dominator tree");
525 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB,
nullptr)).get();
530 NodeT *OldRoot =
Roots.front();
533 OldNode->IDom = NewNode;
534 OldNode->UpdateLevel();
545 assert(N && NewIDom &&
"Cannot change null node pointers!");
559 assert(Node &&
"Removing node that isn't in dominator tree.");
565 typename std::vector<DomTreeNodeBase<NodeT> *>::iterator
I =
566 find(IDom->Children, Node);
567 assert(I != IDom->Children.end() &&
568 "Not in immediate dominator children set!");
570 IDom->Children.erase(I);
580 Split<Inverse<NodeT *>>(NewBB);
582 Split<NodeT *>(NewBB);
588 O <<
"=============================--------------------------------\n";
590 O <<
"Inorder PostDominator Tree: ";
592 O <<
"Inorder Dominator Tree: ";
594 O <<
"DFSNumbers invalid: " <<
SlowQueries <<
" slow queries.";
626 ThisRoot->DFSNumIn = DFSNum++;
628 while (!WorkStack.
empty()) {
630 typename DomTreeNodeBase<NodeT>::const_iterator ChildIt =
631 WorkStack.
back().second;
635 if (ChildIt == Node->
end()) {
636 Node->DFSNumOut = DFSNum++;
641 ++WorkStack.
back().second;
644 Child->DFSNumIn = DFSNum++;
660 NodeT *entry = TraitsTy::getEntryNode(&F);
664 for (
auto *Node :
nodes(&F))
665 if (TraitsTy::child_begin(Node) == TraitsTy::child_end(Node))
692 using NodeRef =
typename GraphT::NodeRef;
693 assert(std::distance(GraphT::child_begin(NewBB),
694 GraphT::child_end(NewBB)) == 1 &&
695 "NewBB should have a single successor!");
696 NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
698 std::vector<NodeRef> PredBlocks;
700 PredBlocks.push_back(Pred);
702 assert(!PredBlocks.empty() &&
"No predblocks?");
704 bool NewBBDominatesNewBBSucc =
true;
706 if (Pred != NewBB && !
dominates(NewBBSucc, Pred) &&
708 NewBBDominatesNewBBSucc =
false;
715 NodeT *NewBBIDom =
nullptr;
717 for (i = 0; i < PredBlocks.size(); ++i)
719 NewBBIDom = PredBlocks[i];
726 if (!NewBBIDom)
return;
728 for (i = i + 1; i < PredBlocks.size(); ++i) {
738 if (NewBBDominatesNewBBSucc) {
752 while ((IDom = B->
getIDom()) !=
nullptr && IDom != A && IDom != B)
754 return IDom !=
nullptr;
768 template <
typename T>
771 template <
typename T>
776 template <
typename NodeT,
bool IsPostDom>
778 const NodeT *B)
const {
785 return dominates(getNode(const_cast<NodeT *>(A)),
786 getNode(const_cast<NodeT *>(B)));
788 template <
typename NodeT,
bool IsPostDom>
790 const NodeT *A,
const NodeT *B)
const {
797 return dominates(getNode(const_cast<NodeT *>(A)),
798 getNode(const_cast<NodeT *>(B)));
803 #endif // LLVM_SUPPORT_GENERICDOMTREE_H
void push_back(const T &Elt)
typename std::vector< DomTreeNodeBase * >::const_iterator const_iterator
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
void Calculate(DomTreeT &DT, FuncT &F)
const_iterator end() const
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
unsigned getLevel() const
DomTreeNodeBase< NodeT > * RootNode
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
bool isVirtualRoot(const DomTreeNodeBase< NodeT > *A) const
DominatorTreeBase(DominatorTreeBase &&Arg)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const_iterator begin() const
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
const std::vector< DomTreeNodeBase * > & getChildren() const
DomTreeNodeBase< NodeT > * setNewRoot(NodeT *BB)
Add a new node to the forward dominator tree and make it a new root.
void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
bool isReachableFromEntry(const DomTreeNodeBase< NodeT > *A) const
LLVM_NODISCARD bool empty() const
bool verify() const
verify - check parent and sibling property
DomTreeNodeBase< NodeT > * operator[](NodeT *BB) const
See getNode.
void PrintDomTree(const DomTreeNodeBase< NodeT > *N, raw_ostream &O, unsigned Lev)
void getDescendants(NodeT *R, SmallVectorImpl< NodeT * > &Result) const
Get all nodes dominated by R, including R itself.
DomTreeNodeBase * getIDom() const
Base class for the actual dominator tree node.
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base...
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
Core dominator tree base class.
Function Alias Analysis false
bool Verify(const DomTreeT &DT)
typename GraphType::UnknownGraphTypeError NodeRef
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree...
static constexpr bool IsPostDominator
void Split(typename GraphTraits< N >::NodeRef NewBB)
void insertEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge insertion and update the tree.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
const DomTreeNodeBase< NodeT > * getRootNode() const
LLVM_NODISCARD T pop_back_val()
DominatorTreeBase & operator=(DominatorTreeBase &&RHS)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
std::vector< NodeT * > Roots
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
bool compare(const DomTreeNodeBase *Other) const
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
std::unique_ptr< DomTreeNodeBase > addChild(std::unique_ptr< DomTreeNodeBase > C)
unsigned getDFSNumOut() const
const NodeT * findNearestCommonDominator(const NodeT *A, const NodeT *B) const
iterator_range< typename GraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
void print(raw_ostream &O) const
print - Convert to human readable form
decltype(std::declval< NodeT * >() ->getParent()) ParentPtr
DomTreeNodeMapType DomTreeNodes
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void changeImmediateDominator(NodeT *BB, NodeT *NewBB)
typename std::vector< DomTreeNodeBase * >::iterator iterator
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
static const Function * getParent(const Value *V)
void recalculate(FT &F)
recalculate - compute a dominator tree for the given function
This class implements an extremely fast bulk output stream that can only output to a stream...
void updateDFSNumbers() const
updateDFSNumbers - Assign In and Out numbers to the nodes while walking dominator tree in dfs order...
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
bool isPostDominator() const
isPostDominator - Returns true if analysis based of postdoms
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
size_t getNumChildren() const
DenseMap< NodeT *, std::unique_ptr< DomTreeNodeBase< NodeT >>> DomTreeNodeMapType
const std::vector< NodeT * > & getRoots() const
getRoots - Return the root blocks of the current CFG.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
void setIDom(DomTreeNodeBase *NewIDom)