23 #ifndef LLVM_SUPPORT_GENERICDOMTREE_H 24 #define LLVM_SUPPORT_GENERICDOMTREE_H 39 #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>
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>
198 template <
typename DomTreeT>
200 typename DomTreeT::NodePtr To);
202 template <
typename DomTreeT>
203 void DeleteEdge(DomTreeT &DT,
typename DomTreeT::NodePtr From,
204 typename DomTreeT::NodePtr To);
206 template <
typename DomTreeT>
210 template <
typename DomTreeT>
211 bool Verify(
const DomTreeT &DT,
typename DomTreeT::VerificationLevel VL);
218 template <
typename NodeT,
bool IsPostDom>
222 "Currently DominatorTreeBase supports only pointer nodes");
226 static_assert(std::is_pointer<ParentPtr>::value,
227 "Currently NodeT's parent must be a pointer type");
228 using ParentType =
typename std::remove_pointer<ParentPtr>::type;
229 static constexpr
bool IsPostDominator = IsPostDom;
248 mutable bool DFSInfoValid =
false;
249 mutable unsigned int SlowQueries = 0;
257 : Roots(
std::move(
Arg.Roots)),
258 DomTreeNodes(
std::move(
Arg.DomTreeNodes)),
259 RootNode(
Arg.RootNode),
261 DFSInfoValid(
Arg.DFSInfoValid),
262 SlowQueries(
Arg.SlowQueries) {
267 Roots = std::move(RHS.Roots);
268 DomTreeNodes = std::move(RHS.DomTreeNodes);
269 RootNode = RHS.RootNode;
271 DFSInfoValid = RHS.DFSInfoValid;
272 SlowQueries = RHS.SlowQueries;
293 if (Parent != Other.
Parent)
return true;
302 if (DomTreeNodes.size() != OtherDomTreeNodes.size())
307 typename DomTreeNodeMapType::const_iterator OI =
308 OtherDomTreeNodes.find(BB);
309 if (OI == OtherDomTreeNodes.end())
329 auto I = DomTreeNodes.find(BB);
330 if (
I != DomTreeNodes.end())
331 return I->second.get();
359 while (!WL.
empty()) {
375 return dominates(A, B);
378 bool properlyDominates(
const NodeT *
A,
const NodeT *
B)
const;
383 assert(!this->isPostDominator() &&
384 "This is not implemented for post dominators");
385 return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
400 if (!isReachableFromEntry(B))
404 if (!isReachableFromEntry(A))
409 if (A->
getIDom() ==
B)
return false;
416 #ifdef EXPENSIVE_CHECKS 418 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
419 "Tree walk disagrees with dfs numbers!");
423 return B->DominatedBy(A);
428 if (SlowQueries > 32) {
430 return B->DominatedBy(A);
433 return dominatedBySlowTreeWalk(A, B);
436 bool dominates(
const NodeT *A,
const NodeT *B)
const;
439 assert(this->Roots.
size() == 1 &&
"Should always have entry node!");
440 return this->Roots[0];
446 assert(A && B &&
"Pointers are not valid");
447 assert(A->getParent() == B->getParent() &&
448 "Two blocks are not in same function");
452 if (!isPostDominator()) {
453 NodeT &
Entry = A->getParent()->front();
454 if (A == &Entry || B == &Entry)
461 if (!NodeA || !NodeB)
return nullptr;
465 while (NodeA && NodeA != NodeB) {
471 return NodeA ? NodeA->
getBlock() :
nullptr;
475 const NodeT *B)
const {
478 return findNearestCommonDominator(const_cast<NodeT *>(A),
479 const_cast<NodeT *>(B));
483 return isPostDominator() && !A->
getBlock();
536 assert(From->getParent() == Parent);
537 assert(To->getParent() == Parent);
554 assert(From->getParent() == Parent);
555 assert(To->getParent() == Parent);
569 assert(getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
571 assert(IDomNode &&
"Not immediate dominator specified for block!");
572 DFSInfoValid =
false;
573 return (DomTreeNodes[BB] = IDomNode->
addChild(
583 assert(getNode(BB) ==
nullptr &&
"Block already in dominator tree!");
584 assert(!this->isPostDominator() &&
585 "Cannot change root of post-dominator tree");
586 DFSInfoValid =
false;
588 std::make_unique<DomTreeNodeBase<NodeT>>(BB,
nullptr)).get();
593 NodeT *OldRoot = Roots.
front();
594 auto &OldNode = DomTreeNodes[OldRoot];
595 OldNode = NewNode->
addChild(std::move(DomTreeNodes[OldRoot]));
596 OldNode->IDom = NewNode;
597 OldNode->UpdateLevel();
600 return RootNode = NewNode;
608 assert(N && NewIDom &&
"Cannot change null node pointers!");
609 DFSInfoValid =
false;
614 changeImmediateDominator(getNode(BB), getNode(NewBB));
622 assert(Node &&
"Removing node that isn't in dominator tree.");
625 DFSInfoValid =
false;
630 const auto I =
find(IDom->Children, Node);
631 assert(
I != IDom->Children.end() &&
632 "Not in immediate dominator children set!");
634 IDom->Children.erase(
I);
637 DomTreeNodes.erase(BB);
639 if (!IsPostDom)
return;
643 if (RIt != Roots.
end()) {
653 Split<Inverse<NodeT *>>(NewBB);
655 Split<NodeT *>(NewBB);
661 O <<
"=============================--------------------------------\n";
663 O <<
"Inorder PostDominator Tree: ";
665 O <<
"Inorder Dominator Tree: ";
667 O <<
"DFSNumbers invalid: " << SlowQueries <<
" slow queries.";
671 if (getRootNode()) PrintDomTree<NodeT>(getRootNode(),
O, 1);
673 for (
const NodePtr Block : Roots) {
674 Block->printAsOperand(O,
false);
694 assert((!Parent || ThisRoot) &&
"Empty constructed DomTree");
703 ThisRoot->DFSNumIn = DFSNum++;
705 while (!WorkStack.
empty()) {
707 const auto ChildIt = WorkStack.
back().second;
711 if (ChildIt == Node->
end()) {
712 Node->DFSNumOut = DFSNum++;
717 ++WorkStack.
back().second;
720 Child->DFSNumIn = DFSNum++;
761 DomTreeNodes.clear();
765 DFSInfoValid =
false;
774 using NodeRef =
typename GraphT::NodeRef;
775 assert(std::distance(GraphT::child_begin(NewBB),
776 GraphT::child_end(NewBB)) == 1 &&
777 "NewBB should have a single successor!");
778 NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
780 std::vector<NodeRef> PredBlocks;
782 PredBlocks.push_back(Pred);
784 assert(!PredBlocks.empty() &&
"No predblocks?");
786 bool NewBBDominatesNewBBSucc =
true;
788 if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
789 isReachableFromEntry(Pred)) {
790 NewBBDominatesNewBBSucc =
false;
797 NodeT *NewBBIDom =
nullptr;
799 for (i = 0; i < PredBlocks.size(); ++i)
800 if (isReachableFromEntry(PredBlocks[i])) {
801 NewBBIDom = PredBlocks[i];
808 if (!NewBBIDom)
return;
810 for (i = i + 1; i < PredBlocks.size(); ++i) {
811 if (isReachableFromEntry(PredBlocks[i]))
812 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
820 if (NewBBDominatesNewBBSucc) {
822 changeImmediateDominator(NewBBSuccNode, NewBBNode);
830 assert(isReachableFromEntry(B));
831 assert(isReachableFromEntry(A));
833 const unsigned ALevel = A->
getLevel();
849 DomTreeNodes.clear();
855 template <
typename T>
858 template <
typename T>
863 template <
typename NodeT,
bool IsPostDom>
865 const NodeT *
B)
const {
872 return dominates(getNode(const_cast<NodeT *>(A)),
873 getNode(const_cast<NodeT *>(B)));
875 template <
typename NodeT,
bool IsPostDom>
877 const NodeT *
A,
const NodeT *
B)
const {
884 return dominates(getNode(const_cast<NodeT *>(A)),
885 getNode(const_cast<NodeT *>(B)));
890 #endif // LLVM_SUPPORT_GENERICDOMTREE_H
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.
This class represents lattice values for constants.
void push_back(const T &Elt)
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
void recalculate(ParentType &Func, ArrayRef< UpdateType > Updates)
Optional< std::vector< StOtherPiece > > Other
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
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.
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")
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
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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.
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...
void print(raw_ostream &O) const
print - Convert to human readable form
const_iterator begin() const
void CalculateWithUpdates(DomTreeT &DT, ArrayRef< typename DomTreeT::UpdateType > Updates)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
BlockVerifier::State From
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()
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.
void Calculate(DomTreeT &DT)
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...
Fast - This calling convention attempts to make calls as fast as possible (e.g.
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)