24 #ifndef LLVM_SUPPORT_GENERICDOMTREE_H 25 #define LLVM_SUPPORT_GENERICDOMTREE_H 32 #include <type_traits> 45 template <
typename NodeT,
bool IsPostDom>
46 class DominatorTreeBase;
48 namespace DomTreeBuilder {
49 template <
typename DomTreeT>
64 std::vector<DomTreeNodeBase *> Children;
65 mutable unsigned DFSNumIn = ~0;
66 mutable unsigned DFSNumOut = ~0;
70 : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
72 using iterator =
typename std::vector<DomTreeNodeBase *>::iterator;
74 typename std::vector<DomTreeNodeBase *>::const_iterator;
85 const std::vector<DomTreeNodeBase *> &
getChildren()
const {
return Children; }
88 std::unique_ptr<DomTreeNodeBase>
C) {
89 Children.push_back(C.get());
101 if (Level != Other->Level)
return true;
105 const NodeT *Nd =
I->getBlock();
111 if (OtherChildren.
count(N) == 0)
118 assert(IDom &&
"No immediate dominator?");
119 if (IDom == NewIDom)
return;
121 auto I =
find(IDom->Children,
this);
122 assert(
I != IDom->Children.end() &&
123 "Not in immediate dominator children set!");
125 IDom->Children.erase(
I);
129 IDom->Children.push_back(
this);
144 return this->DFSNumIn >= other->DFSNumIn &&
145 this->DFSNumOut <= other->DFSNumOut;
150 if (Level == IDom->Level + 1)
return;
154 while (!WorkStack.
empty()) {
156 Current->Level = Current->IDom->Level + 1;
160 if (
C->Level !=
C->IDom->Level + 1) WorkStack.
push_back(
C);
166 template <
class NodeT>
167 raw_ostream &operator<<(raw_ostream &O, const DomTreeNodeBase<NodeT> *Node) {
168 if (Node->getBlock())
169 Node->getBlock()->printAsOperand(
O,
false);
171 O <<
" <<exit node>>";
173 O <<
" {" << Node->getDFSNumIn() <<
"," << Node->getDFSNumOut() <<
"} [" 174 << Node->getLevel() <<
"]\n";
179 template <
class NodeT>
182 O.
indent(2 * Lev) <<
"[" << Lev <<
"] " <<
N;
186 PrintDomTree<NodeT>(*
I, O, Lev + 1);
189 namespace DomTreeBuilder {
191 template <
typename DomTreeT>
194 template <
typename DomTreeT>
195 void InsertEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
196 typename DomTreeT::NodePtr To);
198 template <
typename DomTreeT>
199 void DeleteEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
200 typename DomTreeT::NodePtr To);
206 template <
typename NodePtr>
214 : From(From), ToAndKind(To, Kind) {}
225 U.
getFrom()->printAsOperand(OS,
false);
227 U.
getTo()->printAsOperand(OS,
false);
232 template <
typename DomTreeT>
236 template <
typename DomTreeT>
237 bool Verify(
const DomTreeT &DT,
typename DomTreeT::VerificationLevel VL);
244 template <
typename NodeT,
bool IsPostDom>
248 "Currently DominatorTreeBase supports only pointer nodes");
252 static_assert(std::is_pointer<ParentPtr>::value,
253 "Currently NodeT's parent must be a pointer type");
254 using ParentType =
typename std::remove_pointer<ParentPtr>::type;
255 static constexpr
bool IsPostDominator = IsPostDom;
274 mutable bool DFSInfoValid =
false;
275 mutable unsigned int SlowQueries = 0;
283 : Roots(
std::move(
Arg.Roots)),
284 DomTreeNodes(
std::move(
Arg.DomTreeNodes)),
285 RootNode(
Arg.RootNode),
287 DFSInfoValid(
Arg.DFSInfoValid),
288 SlowQueries(
Arg.SlowQueries) {
293 Roots = std::move(RHS.Roots);
294 DomTreeNodes = std::move(RHS.DomTreeNodes);
295 RootNode = RHS.RootNode;
297 DFSInfoValid = RHS.DFSInfoValid;
298 SlowQueries = RHS.SlowQueries;
319 if (Parent != Other.
Parent)
return true;
328 if (DomTreeNodes.size() != OtherDomTreeNodes.size())
333 typename DomTreeNodeMapType::const_iterator OI =
334 OtherDomTreeNodes.find(BB);
335 if (OI == OtherDomTreeNodes.end())
355 auto I = DomTreeNodes.find(BB);
356 if (
I != DomTreeNodes.end())
357 return I->second.get();
385 while (!WL.
empty()) {
401 return dominates(A, B);
404 bool properlyDominates(
const NodeT *
A,
const NodeT *
B)
const;
409 assert(!this->isPostDominator() &&
410 "This is not implemented for post dominators");
411 return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
426 if (!isReachableFromEntry(B))
430 if (!isReachableFromEntry(A))
435 if (A->
getIDom() ==
B)
return false;
442 #ifdef EXPENSIVE_CHECKS 444 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
445 "Tree walk disagrees with dfs numbers!");
449 return B->DominatedBy(A);
454 if (SlowQueries > 32) {
456 return B->DominatedBy(A);
459 return dominatedBySlowTreeWalk(A, B);
462 bool dominates(
const NodeT *A,
const NodeT *B)
const;
465 assert(this->Roots.
size() == 1 &&
"Should always have entry node!");
466 return this->Roots[0];
472 assert(A && B &&
"Pointers are not valid");
473 assert(A->getParent() == B->getParent() &&
474 "Two blocks are not in same function");
478 if (!isPostDominator()) {
479 NodeT &Entry = A->getParent()->front();
480 if (A == &Entry || B == &Entry)
487 if (!NodeA || !NodeB)
return nullptr;
491 while (NodeA && NodeA != NodeB) {
497 return NodeA ? NodeA->
getBlock() :
nullptr;
501 const NodeT *B)
const {
504 return findNearestCommonDominator(const_cast<NodeT *>(A),
505 const_cast<NodeT *>(B));
509 return isPostDominator() && !A->
getBlock();
562 assert(From->getParent() == Parent);
563 assert(To->getParent() == Parent);
580 assert(From->getParent() == Parent);
581 assert(To->getParent() == Parent);
595 assert(getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
597 assert(IDomNode &&
"Not immediate dominator specified for block!");
598 DFSInfoValid =
false;
599 return (DomTreeNodes[BB] = IDomNode->
addChild(
609 assert(getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
610 assert(!this->isPostDominator() &&
611 "Cannot change root of post-dominator tree");
612 DFSInfoValid =
false;
614 llvm::make_unique<DomTreeNodeBase<NodeT>>(BB,
nullptr)).get();
619 NodeT *OldRoot = Roots.
front();
620 auto &OldNode = DomTreeNodes[OldRoot];
621 OldNode = NewNode->
addChild(std::move(DomTreeNodes[OldRoot]));
622 OldNode->IDom = NewNode;
623 OldNode->UpdateLevel();
626 return RootNode = NewNode;
634 assert(N && NewIDom &&
"Cannot change null node pointers!");
635 DFSInfoValid =
false;
640 changeImmediateDominator(getNode(BB), getNode(NewBB));
648 assert(Node &&
"Removing node that isn't in dominator tree.");
651 DFSInfoValid =
false;
656 const auto I =
find(IDom->Children, Node);
657 assert(
I != IDom->Children.end() &&
658 "Not in immediate dominator children set!");
660 IDom->Children.erase(
I);
663 DomTreeNodes.erase(BB);
665 if (!IsPostDom)
return;
669 if (RIt != Roots.
end()) {
679 Split<Inverse<NodeT *>>(NewBB);
681 Split<NodeT *>(NewBB);
687 O <<
"=============================--------------------------------\n";
689 O <<
"Inorder PostDominator Tree: ";
691 O <<
"Inorder Dominator Tree: ";
693 O <<
"DFSNumbers invalid: " << SlowQueries <<
" slow queries.";
697 if (getRootNode()) PrintDomTree<NodeT>(getRootNode(),
O, 1);
698 if (IsPostDominator) {
700 for (
const NodePtr Block : Roots) {
701 Block->printAsOperand(O,
false);
722 assert((!Parent || ThisRoot) &&
"Empty constructed DomTree");
731 ThisRoot->DFSNumIn = DFSNum++;
733 while (!WorkStack.
empty()) {
735 const auto ChildIt = WorkStack.
back().second;
739 if (ChildIt == Node->
end()) {
740 Node->DFSNumOut = DFSNum++;
745 ++WorkStack.
back().second;
748 Child->DFSNumIn = DFSNum++;
784 DomTreeNodes.clear();
788 DFSInfoValid =
false;
797 using NodeRef =
typename GraphT::NodeRef;
798 assert(std::distance(GraphT::child_begin(NewBB),
799 GraphT::child_end(NewBB)) == 1 &&
800 "NewBB should have a single successor!");
801 NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
803 std::vector<NodeRef> PredBlocks;
805 PredBlocks.push_back(Pred);
807 assert(!PredBlocks.empty() &&
"No predblocks?");
809 bool NewBBDominatesNewBBSucc =
true;
811 if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
812 isReachableFromEntry(Pred)) {
813 NewBBDominatesNewBBSucc =
false;
820 NodeT *NewBBIDom =
nullptr;
822 for (i = 0; i < PredBlocks.size(); ++i)
823 if (isReachableFromEntry(PredBlocks[i])) {
824 NewBBIDom = PredBlocks[i];
831 if (!NewBBIDom)
return;
833 for (i = i + 1; i < PredBlocks.size(); ++i) {
834 if (isReachableFromEntry(PredBlocks[i]))
835 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
843 if (NewBBDominatesNewBBSucc) {
845 changeImmediateDominator(NewBBSuccNode, NewBBNode);
853 assert(isReachableFromEntry(B));
854 assert(isReachableFromEntry(A));
856 const unsigned ALevel = A->
getLevel();
872 DomTreeNodes.clear();
878 template <
typename T>
881 template <
typename T>
886 template <
typename NodeT,
bool IsPostDom>
888 const NodeT *
B)
const {
895 return dominates(getNode(const_cast<NodeT *>(A)),
896 getNode(const_cast<NodeT *>(B)));
898 template <
typename NodeT,
bool IsPostDom>
900 const NodeT *
A,
const NodeT *
B)
const {
907 return dominates(getNode(const_cast<NodeT *>(A)),
908 getNode(const_cast<NodeT *>(B)));
913 #endif // LLVM_SUPPORT_GENERICDOMTREE_H
UpdateKind getKind() const
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 splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
Compute iterated dominance frontiers using a linear time algorithm.
PointerTy getPointer() const
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B...
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
block Block Frequency true
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 eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
SmallVector< NodeT *, IsPostDom ? 4 :1 > Roots
void Split(typename GraphTraits< N >::NodeRef NewBB)
const NodeT * findNearestCommonDominator(const NodeT *A, const NodeT *B) const
bool isPostDominator() const
isPostDominator - Returns true if analysis based of postdoms
void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
DominatorTreeBase & operator=(DominatorTreeBase &&RHS)
void changeImmediateDominator(NodeT *BB, NodeT *NewBB)
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
const SmallVectorImpl< NodeT * > & getRoots() const
getRoots - Return the root blocks of the current CFG.
decltype(std::declval< BasicBlock *>() ->getParent()) ParentPtr
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base...
void PrintDomTree(const DomTreeNodeBase< NodeT > *N, raw_ostream &O, unsigned Lev)
const DomTreeNodeBase< NodeT > * getRootNode() const
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
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(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge insertion and update the tree.
Update(UpdateKind Kind, NodePtr From, NodePtr To)
Core dominator tree base class.
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree...
typename GraphType::UnknownGraphTypeError NodeRef
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
bool isReachableFromEntry(const DomTreeNodeBase< NodeT > *A) const
void getDescendants(NodeT *R, SmallVectorImpl< NodeT *> &Result) const
Get all nodes dominated by R, including R itself.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase * getIDom() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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.
typename std::remove_pointer< ParentPtr >::type ParentType
unsigned getDFSNumOut() const
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
DomTreeNodeBase< NodeT > * operator[](const NodeT *BB) const
See getNode.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
bool operator==(const Update &RHS) const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Fast - This calling convention attempts to make calls as fast as possible (e.g.
void print(raw_ostream &O) const
print - Convert to human readable form
const_iterator begin() const
friend raw_ostream & operator<<(raw_ostream &OS, const Update &U)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
size_t getNumChildren() const
LLVM_NODISCARD T pop_back_val()
DomTreeNodeBase< NodeT > * RootNode
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL)
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
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)
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
amdgpu Simplify well known AMD library false Value Value * Arg
void Calculate(DomTreeT &DT)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
bool compare(const DomTreeNodeBase *Other) const
LLVM_NODISCARD bool empty() const
DomTreeNodeMapType DomTreeNodes
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
void ApplyUpdates(DomTreeT &DT, ArrayRef< typename DomTreeT::UpdateType > Updates)
DominatorTreeBase(DominatorTreeBase &&Arg)
bool isVirtualRoot(const DomTreeNodeBase< NodeT > *A) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void updateDFSNumbers() const
updateDFSNumbers - Assign In and Out numbers to the nodes while walking dominator tree in dfs order...
typename std::vector< DomTreeNodeBase *>::iterator iterator
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
unsigned getLevel() const
const_iterator end() const
void setIDom(DomTreeNodeBase *NewIDom)