LLVM 23.0.0git
GenericDomTree.h
Go to the documentation of this file.
1//===- GenericDomTree.h - Generic dominator trees for graphs ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file defines a set of templates that efficiently compute a dominator
11/// tree over a generic graph. This is used typically in LLVM for fast
12/// dominance queries on the CFG, but is fully generic w.r.t. the underlying
13/// graph types.
14///
15/// Unlike ADT/* graph algorithms, generic dominator tree has more requirements
16/// on the graph's NodeRef. The NodeRef should be a pointer and,
17/// either NodeRef->getParent() must return the parent node that is also a
18/// pointer or DomTreeNodeTraits needs to be specialized.
19///
20/// FIXME: Maybe GenericDomTree needs a TreeTraits, instead of GraphTraits.
21///
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_SUPPORT_GENERICDOMTREE_H
25#define LLVM_SUPPORT_GENERICDOMTREE_H
26
27#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/STLExtras.h"
36#include <algorithm>
37#include <cassert>
38#include <cstddef>
39#include <memory>
40#include <new>
41#include <type_traits>
42#include <utility>
43
44namespace llvm {
45
46template <typename NodeT, bool IsPostDom>
48
49namespace DomTreeBuilder {
50template <typename DomTreeT>
51struct SemiNCAInfo;
52} // namespace DomTreeBuilder
53
54/// Base class for the actual dominator tree node.
55template <class NodeT> class DomTreeNodeBase {
56 friend class PostDominatorTree;
57 friend class DominatorTreeBase<NodeT, false>;
58 friend class DominatorTreeBase<NodeT, true>;
61
62 NodeT *TheBB;
63 DomTreeNodeBase *IDom;
64 unsigned Level;
65 DomTreeNodeBase *FirstChild = nullptr;
66 DomTreeNodeBase *Sibling = nullptr;
67 DomTreeNodeBase **AppendPtr = &FirstChild;
68 mutable unsigned DFSNumIn = ~0;
69 mutable unsigned DFSNumOut = ~0;
70
71 public:
73 : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
74
77
79 : public iterator_facade_base<const_iterator, std::forward_iterator_tag,
80 DomTreeNodeBase *> {
81 DomTreeNodeBase *Node;
82
83 public:
84 const_iterator(DomTreeNodeBase *Node = nullptr) : Node(Node) {}
85 bool operator==(const const_iterator &Other) const {
86 return Other.Node == Node;
87 }
88 DomTreeNodeBase *operator*() const { return Node; }
90 Node = Node->Sibling;
91 return *this;
92 }
94 const_iterator cp = *this;
95 ++*this;
96 return cp;
97 }
98 };
99 // We don't permit modifications through the iterator.
100 using iterator = const_iterator;
101
102 iterator begin() const { return iterator{FirstChild}; }
103 iterator end() const { return iterator{}; }
104
107 return make_range(begin(), end());
108 }
109
110 NodeT *getBlock() const { return TheBB; }
111 DomTreeNodeBase *getIDom() const { return IDom; }
112 unsigned getLevel() const { return Level; }
113
114 // TODO: make these private once NewGVN doesn't require these anymore.
116 assert(!C->Sibling && "cannot add child that already has siblings");
117 assert(!*AppendPtr && "sibling of last child must be nullptr");
118 *AppendPtr = C;
119 AppendPtr = &C->Sibling;
120 }
121
122 // TODO: make these private once NewGVN doesn't require these anymore.
124 DomTreeNodeBase **It = &FirstChild;
125 while (*It != C) {
126 assert(*It != nullptr && "Not in immediate dominator children list!");
127 It = &(*It)->Sibling;
128 }
129 assert(!*AppendPtr && "sibling of last child must be nullptr");
130 assert(C->Sibling || AppendPtr == &C->Sibling);
131 *It = C->Sibling;
132 if (C->Sibling)
133 C->Sibling = nullptr;
134 else
135 AppendPtr = It;
136 }
137
138 bool isLeaf() const { return FirstChild == nullptr; }
139
140 bool compare(const DomTreeNodeBase *Other) const {
141 if (Level != Other->Level) return true;
142
143 SmallPtrSet<const NodeT *, 4> OtherChildren;
144 for (const DomTreeNodeBase *I : *Other) {
145 const NodeT *Nd = I->getBlock();
146 OtherChildren.insert(Nd);
147 }
148
149 size_t OwnCount = 0;
150 for (const DomTreeNodeBase *I : *this) {
151 const NodeT *N = I->getBlock();
152 if (OtherChildren.count(N) == 0)
153 return true;
154 ++OwnCount;
155 }
156 return OwnCount != OtherChildren.size();
157 }
158
159 void setIDom(DomTreeNodeBase *NewIDom) {
160 assert(IDom && "No immediate dominator?");
161 if (IDom == NewIDom) return;
162 IDom->removeChild(this);
163
164 // Switch to new dominator
165 IDom = NewIDom;
166 IDom->addChild(this);
167
168 UpdateLevel();
169 }
170
171 /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
172 /// in the dominator tree. They are only guaranteed valid if
173 /// updateDFSNumbers() has been called.
174 unsigned getDFSNumIn() const { return DFSNumIn; }
175 unsigned getDFSNumOut() const { return DFSNumOut; }
176
177private:
178 // Return true if this node is dominated by other. Use this only if DFS info
179 // is valid.
180 bool DominatedBy(const DomTreeNodeBase *other) const {
181 return this->DFSNumIn >= other->DFSNumIn &&
182 this->DFSNumOut <= other->DFSNumOut;
183 }
184
185 void UpdateLevel() {
186 assert(IDom);
187 if (Level == IDom->Level + 1) return;
188
189 SmallVector<DomTreeNodeBase *, 64> WorkStack = {this};
190
191 while (!WorkStack.empty()) {
192 DomTreeNodeBase *Current = WorkStack.pop_back_val();
193 Current->Level = Current->IDom->Level + 1;
194
195 for (DomTreeNodeBase *C : *Current) {
196 assert(C->IDom);
197 if (C->Level != C->IDom->Level + 1) WorkStack.push_back(C);
198 }
199 }
200 }
201};
202
203template <class NodeT>
205 if (Node->getBlock())
206 Node->getBlock()->printAsOperand(O, false);
207 else
208 O << " <<exit node>>";
209
210 O << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "} ["
211 << Node->getLevel() << "]\n";
212
213 return O;
214}
215
216template <class NodeT>
218 unsigned Lev) {
219 O.indent(2 * Lev) << "[" << Lev << "] " << N;
220 for (const auto &I : *N)
221 PrintDomTree<NodeT>(I, O, Lev + 1);
222}
223
224namespace DomTreeBuilder {
225// The routines below are provided in a separate header but referenced here.
226template <typename DomTreeT>
227void Calculate(DomTreeT &DT);
228
229template <typename DomTreeT>
230void CalculateWithUpdates(DomTreeT &DT,
232
233template <typename DomTreeT>
234void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
235 typename DomTreeT::NodePtr To);
236
237template <typename DomTreeT>
238void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
239 typename DomTreeT::NodePtr To);
240
241template <typename DomTreeT>
242void ApplyUpdates(DomTreeT &DT,
243 GraphDiff<typename DomTreeT::NodePtr,
244 DomTreeT::IsPostDominator> &PreViewCFG,
245 GraphDiff<typename DomTreeT::NodePtr,
246 DomTreeT::IsPostDominator> *PostViewCFG);
247
248template <typename DomTreeT>
249bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL);
250} // namespace DomTreeBuilder
251
252/// Default DomTreeNode traits for NodeT. The default implementation assume a
253/// Function-like NodeT. Can be specialized to support different node types.
254template <typename NodeT> struct DomTreeNodeTraits {
255 using NodeType = NodeT;
256 using NodePtr = NodeT *;
257 using ParentPtr = decltype(std::declval<NodePtr>()->getParent());
258 static_assert(std::is_pointer_v<ParentPtr>,
259 "Currently NodeT's parent must be a pointer type");
260 using ParentType = std::remove_pointer_t<ParentPtr>;
261
262 static NodeT *getEntryNode(ParentPtr Parent) { return &Parent->front(); }
263 static ParentPtr getParent(NodePtr BB) { return BB->getParent(); }
264};
265
266/// Core dominator tree base class.
267///
268/// This class is a generic template over graph nodes. It is instantiated for
269/// various graphs in the LLVM IR or in the code generator.
270template <typename NodeT, bool IsPostDom>
272 public:
273 static_assert(std::is_pointer_v<typename GraphTraits<NodeT *>::NodeRef>,
274 "Currently DominatorTreeBase supports only pointer nodes");
277 using NodePtr = typename NodeTrait::NodePtr;
279 static_assert(std::is_pointer_v<ParentPtr>,
280 "Currently NodeT's parent must be a pointer type");
281 using ParentType = std::remove_pointer_t<ParentPtr>;
282 static constexpr bool IsPostDominator = IsPostDom;
283
286 static constexpr UpdateKind Insert = UpdateKind::Insert;
287 static constexpr UpdateKind Delete = UpdateKind::Delete;
288
290
291protected:
292 // Dominators always have a single root, postdominators can have more.
294
297 // For graphs where blocks don't have numbers, create a numbering here.
298 // TODO: use an empty struct with [[no_unique_address]] in C++20.
299 std::conditional_t<!GraphHasNodeNumbers<NodeT *>,
303 ParentPtr Parent = nullptr;
304
305 // Use small slab size to reduce memory waste for modules with many small
306 // functions. Compensate with a short GrowthDelay. This is relevant for
307 // ThinLTO on modules with many functions (not uncommon in C++), where all
308 // dominator trees are live at the same time.
309 static constexpr size_t SlabSize = 8 * sizeof(DomTreeNodeBase<NodeT>);
311 /*GrowthDelay=*/2>
313
314 mutable bool DFSInfoValid = false;
315 mutable unsigned int SlowQueries = 0;
316 unsigned BlockNumberEpoch = 0;
317
319
320 public:
321 DominatorTreeBase() = default;
322
325
328
329 /// Iteration over roots.
330 ///
331 /// This may include multiple blocks if we are computing post dominators.
332 /// For forward dominators, this will always be a single block (the entry
333 /// block).
336
337 root_iterator root_begin() { return Roots.begin(); }
338 const_root_iterator root_begin() const { return Roots.begin(); }
339 root_iterator root_end() { return Roots.end(); }
340 const_root_iterator root_end() const { return Roots.end(); }
341
342 size_t root_size() const { return Roots.size(); }
343
350
351 /// isPostDominator - Returns true if analysis based of postdoms
352 ///
353 bool isPostDominator() const { return IsPostDominator; }
354
355 /// compare - Return false if the other dominator tree base matches this
356 /// dominator tree base. Otherwise return true.
357 bool compare(const DominatorTreeBase &Other) const {
358 if (Parent != Other.Parent) return true;
359
360 if (Roots.size() != Other.Roots.size())
361 return true;
362
363 if (!std::is_permutation(Roots.begin(), Roots.end(), Other.Roots.begin()))
364 return true;
365
366 size_t NumNodes = 0;
367 // All nodes we have must exist and be equal in the other tree.
368 for (const auto &Node : DomTreeNodes) {
369 if (!Node)
370 continue;
371 if (Node->compare(Other.getNode(Node->getBlock())))
372 return true;
373 NumNodes++;
374 }
375
376 // If the other tree has more nodes than we have, they're not equal.
377 size_t NumOtherNodes = 0;
378 for (const auto &OtherNode : Other.DomTreeNodes)
379 if (OtherNode)
380 NumOtherNodes++;
381 return NumNodes != NumOtherNodes;
382 }
383
384private:
385 std::optional<unsigned> getNodeIndex(const NodeT *BB) const {
386 if constexpr (GraphHasNodeNumbers<NodeT *>) {
387 // BB can be nullptr, map nullptr to index 0.
390 "dominator tree used with outdated block numbers");
391 return BB ? GraphTraits<const NodeT *>::getNumber(BB) + 1 : 0;
392 } else {
393 if (auto It = NodeNumberMap.find(BB); It != NodeNumberMap.end())
394 return It->second;
395 return std::nullopt;
396 }
397 }
398
399 unsigned getNodeIndexForInsert(const NodeT *BB) {
400 if constexpr (GraphHasNodeNumbers<NodeT *>) {
401 // getNodeIndex will never fail if nodes have getNumber().
402 unsigned Idx = *getNodeIndex(BB);
403 if (Idx >= DomTreeNodes.size()) {
404 unsigned Max = GraphTraits<ParentPtr>::getMaxNumber(Parent);
405 DomTreeNodes.resize(Max > Idx + 1 ? Max : Idx + 1);
406 }
407 return Idx;
408 } else {
409 // We might already have a number stored for BB.
410 unsigned Idx =
411 NodeNumberMap.try_emplace(BB, DomTreeNodes.size()).first->second;
412 if (Idx >= DomTreeNodes.size())
413 DomTreeNodes.resize(Idx + 1);
414 return Idx;
415 }
416 }
417
418public:
419 /// getNode - return the (Post)DominatorTree node for the specified basic
420 /// block. This is the same as using operator[] on this class. The result
421 /// may (but is not required to) be null for a forward (backwards)
422 /// statically unreachable block.
423 DomTreeNodeBase<NodeT> *getNode(const NodeT *BB) const {
424 assert((!BB || Parent == NodeTrait::getParent(const_cast<NodeT *>(BB))) &&
425 "cannot get DomTreeNode of block with different parent");
426 if (auto Idx = getNodeIndex(BB); Idx && *Idx < DomTreeNodes.size())
427 return DomTreeNodes[*Idx];
428 return nullptr;
429 }
430
431 /// See getNode.
432 DomTreeNodeBase<NodeT> *operator[](const NodeT *BB) const {
433 return getNode(BB);
434 }
435
436 /// getRootNode - This returns the entry node for the CFG of the function. If
437 /// this tree represents the post-dominance relations for a function, however,
438 /// this root may be a node with the block == NULL. This is the case when
439 /// there are multiple exit nodes from a particular function. Consumers of
440 /// post-dominance information must be capable of dealing with this
441 /// possibility.
442 ///
444 const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
445
446 /// Get all nodes dominated by R, including R itself.
447 void getDescendants(NodeT *R, SmallVectorImpl<NodeT *> &Result) const {
448 Result.clear();
449 const DomTreeNodeBase<NodeT> *RN = getNode(R);
450 if (!RN)
451 return; // If R is unreachable, it will not be present in the DOM tree.
453 WL.push_back(RN);
454
455 while (!WL.empty()) {
457 Result.push_back(N->getBlock());
458 WL.append(N->begin(), N->end());
459 }
460 }
461
462 /// properlyDominates - Returns true iff A dominates B and A != B.
463 /// Note that this is not a constant time operation!
464 ///
466 const DomTreeNodeBase<NodeT> *B) const {
467 if (!A || !B)
468 return false;
469 if (A == B)
470 return false;
471 return dominates(A, B);
472 }
473
474 bool properlyDominates(const NodeT *A, const NodeT *B) const;
475
476 /// isReachableFromEntry - Return true if A is dominated by the entry
477 /// block of the function containing it.
478 bool isReachableFromEntry(const NodeT *A) const {
479 assert(!this->isPostDominator() &&
480 "This is not implemented for post dominators");
481 return isReachableFromEntry(getNode(A));
482 }
483
484 bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const { return A; }
485
486 /// dominates - Returns true iff A dominates B. Note that this is not a
487 /// constant time operation!
488 ///
490 const DomTreeNodeBase<NodeT> *B) const {
491 // A node trivially dominates itself.
492 if (B == A)
493 return true;
494
495 // An unreachable node is dominated by anything.
497 return true;
498
499 // And dominates nothing.
501 return false;
502
503 if (B->getIDom() == A) return true;
504
505 if (A->getIDom() == B) return false;
506
507 // A can only dominate B if it is higher in the tree.
508 if (A->getLevel() >= B->getLevel()) return false;
509
510 // Compare the result of the tree walk and the dfs numbers, if expensive
511 // checks are enabled.
512#ifdef EXPENSIVE_CHECKS
514 (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
515 "Tree walk disagrees with dfs numbers!");
516#endif
517
518 if (DFSInfoValid)
519 return B->DominatedBy(A);
520
521 // If we end up with too many slow queries, just update the
522 // DFS numbers on the theory that we are going to keep querying.
523 SlowQueries++;
524 if (SlowQueries > 32) {
526 return B->DominatedBy(A);
527 }
528
529 return dominatedBySlowTreeWalk(A, B);
530 }
531
532 bool dominates(const NodeT *A, const NodeT *B) const;
533
534 NodeT *getRoot() const {
535 assert(this->Roots.size() == 1 && "Should always have entry node!");
536 return this->Roots[0];
537 }
538
539 /// Find nearest common dominator basic block for basic block A and B. A and B
540 /// must have tree nodes.
541 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) const {
542 assert(A && B && "Pointers are not valid");
544 "Two blocks are not in same function");
545
546 // If either A or B is a entry block then it is nearest common dominator
547 // (for forward-dominators).
548 if (!isPostDominator()) {
549 NodeT &Entry =
551 if (A == &Entry || B == &Entry)
552 return &Entry;
553 }
554
557 assert(NodeA && "A must be in the tree");
558 assert(NodeB && "B must be in the tree");
559
560 // Use level information to go up the tree until the levels match. Then
561 // continue going up til we arrive at the same node.
562 while (NodeA != NodeB) {
563 if (NodeA->getLevel() < NodeB->getLevel()) std::swap(NodeA, NodeB);
564
565 NodeA = NodeA->IDom;
566 }
567
568 return NodeA->getBlock();
569 }
570
571 const NodeT *findNearestCommonDominator(const NodeT *A,
572 const NodeT *B) const {
573 // Cast away the const qualifiers here. This is ok since
574 // const is re-introduced on the return type.
575 return findNearestCommonDominator(const_cast<NodeT *>(A),
576 const_cast<NodeT *>(B));
577 }
578
580 return isPostDominator() && !A->getBlock();
581 }
582
583 template <typename IteratorTy>
585 assert(!Nodes.empty() && "Nodes list is empty!");
586
587 NodeT *NCD = *Nodes.begin();
588 for (NodeT *Node : llvm::drop_begin(Nodes)) {
590
591 // Stop when the root is reached.
592 if (isVirtualRoot(getNode(NCD)))
593 return nullptr;
594 }
595
596 return NCD;
597 }
598
599 //===--------------------------------------------------------------------===//
600 // API to update (Post)DominatorTree information based on modifications to
601 // the CFG...
602
603 /// Inform the dominator tree about a sequence of CFG edge insertions and
604 /// deletions and perform a batch update on the tree.
605 ///
606 /// This function should be used when there were multiple CFG updates after
607 /// the last dominator tree update. It takes care of performing the updates
608 /// in sync with the CFG and optimizes away the redundant operations that
609 /// cancel each other.
610 /// The functions expects the sequence of updates to be balanced. Eg.:
611 /// - {{Insert, A, B}, {Delete, A, B}, {Insert, A, B}} is fine, because
612 /// logically it results in a single insertions.
613 /// - {{Insert, A, B}, {Insert, A, B}} is invalid, because it doesn't make
614 /// sense to insert the same edge twice.
615 ///
616 /// What's more, the functions assumes that it's safe to ask every node in the
617 /// CFG about its children and inverse children. This implies that deletions
618 /// of CFG edges must not delete the CFG nodes before calling this function.
619 ///
620 /// The applyUpdates function can reorder the updates and remove redundant
621 /// ones internally (as long as it is done in a deterministic fashion). The
622 /// batch updater is also able to detect sequences of zero and exactly one
623 /// update -- it's optimized to do less work in these cases.
624 ///
625 /// Note that for postdominators it automatically takes care of applying
626 /// updates on reverse edges internally (so there's no need to swap the
627 /// From and To pointers when constructing DominatorTree::UpdateType).
628 /// The type of updates is the same for DomTreeBase<T> and PostDomTreeBase<T>
629 /// with the same template parameter T.
630 ///
631 /// \param Updates An ordered sequence of updates to perform. The current CFG
632 /// and the reverse of these updates provides the pre-view of the CFG.
633 ///
636 Updates, /*ReverseApplyUpdates=*/true);
637 DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, nullptr);
638 }
639
640 /// \param Updates An ordered sequence of updates to perform. The current CFG
641 /// and the reverse of these updates provides the pre-view of the CFG.
642 /// \param PostViewUpdates An ordered sequence of update to perform in order
643 /// to obtain a post-view of the CFG. The DT will be updated assuming the
644 /// obtained PostViewCFG is the desired end state.
646 ArrayRef<UpdateType> PostViewUpdates) {
647 if (Updates.empty()) {
648 GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
649 DomTreeBuilder::ApplyUpdates(*this, PostViewCFG, &PostViewCFG);
650 } else {
651 // PreViewCFG needs to merge Updates and PostViewCFG. The updates in
652 // Updates need to be reversed, and match the direction in PostViewCFG.
653 // The PostViewCFG is created with updates reversed (equivalent to changes
654 // made to the CFG), so the PreViewCFG needs all the updates reverse
655 // applied.
656 SmallVector<UpdateType> AllUpdates(Updates);
657 append_range(AllUpdates, PostViewUpdates);
658 GraphDiff<NodePtr, IsPostDom> PreViewCFG(AllUpdates,
659 /*ReverseApplyUpdates=*/true);
660 GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
661 DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, &PostViewCFG);
662 }
663 }
664
665 /// Inform the dominator tree about a CFG edge insertion and update the tree.
666 ///
667 /// This function has to be called just before or just after making the update
668 /// on the actual CFG. There cannot be any other updates that the dominator
669 /// tree doesn't know about.
670 ///
671 /// Note that for postdominators it automatically takes care of inserting
672 /// a reverse edge internally (so there's no need to swap the parameters).
673 ///
674 void insertEdge(NodeT *From, NodeT *To) {
675 assert(From);
676 assert(To);
679 DomTreeBuilder::InsertEdge(*this, From, To);
680 }
681
682 /// Inform the dominator tree about a CFG edge deletion and update the tree.
683 ///
684 /// This function has to be called just after making the update on the actual
685 /// CFG. An internal functions checks if the edge doesn't exist in the CFG in
686 /// DEBUG mode. There cannot be any other updates that the
687 /// dominator tree doesn't know about.
688 ///
689 /// Note that for postdominators it automatically takes care of deleting
690 /// a reverse edge internally (so there's no need to swap the parameters).
691 ///
692 void deleteEdge(NodeT *From, NodeT *To) {
693 assert(From);
694 assert(To);
697 DomTreeBuilder::DeleteEdge(*this, From, To);
698 }
699
700 /// Add a new node to the dominator tree information.
701 ///
702 /// This creates a new node as a child of DomBB dominator node, linking it
703 /// into the children list of the immediate dominator.
704 ///
705 /// \param BB New node in CFG.
706 /// \param DomBB CFG node that is dominator for BB.
707 /// \returns New dominator tree node that represents new CFG node.
708 ///
709 DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
710 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
711 DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
712 assert(IDomNode && "Not immediate dominator specified for block!");
713 DFSInfoValid = false;
714 return createNode(BB, IDomNode);
715 }
716
717 /// Add a new node to the forward dominator tree and make it a new root.
718 ///
719 /// \param BB New node in CFG.
720 /// \returns New dominator tree node that represents new CFG node.
721 ///
723 assert(getNode(BB) == nullptr && "Block already in dominator tree!");
724 assert(!this->isPostDominator() &&
725 "Cannot change root of post-dominator tree");
726 DFSInfoValid = false;
727 DomTreeNodeBase<NodeT> *NewNode = createNode(BB);
728 if (Roots.empty()) {
729 addRoot(BB);
730 } else {
731 assert(Roots.size() == 1);
732 NodeT *OldRoot = Roots.front();
733 DomTreeNodeBase<NodeT> *OldNode = getNode(OldRoot);
734 NewNode->addChild(OldNode);
735 OldNode->IDom = NewNode;
736 OldNode->UpdateLevel();
737 Roots[0] = BB;
738 }
739 return RootNode = NewNode;
740 }
741
742 /// changeImmediateDominator - This method is used to update the dominator
743 /// tree information when a node's immediate dominator changes.
744 ///
746 DomTreeNodeBase<NodeT> *NewIDom) {
747 assert(N && NewIDom && "Cannot change null node pointers!");
748 DFSInfoValid = false;
749 N->setIDom(NewIDom);
750 }
751
752 void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
754 }
755
756 /// eraseNode - Removes a node from the dominator tree. Block must not
757 /// dominate any other blocks. Removes node from its immediate dominator's
758 /// children list. Deletes dominator node associated with basic block BB.
759 void eraseNode(NodeT *BB) {
760 std::optional<unsigned> IdxOpt = getNodeIndex(BB);
761 assert(IdxOpt && DomTreeNodes[*IdxOpt] &&
762 "Removing node that isn't in dominator tree.");
764 assert(Node->isLeaf() && "Node is not a leaf node.");
765
766 DFSInfoValid = false;
767
768 // Remove node from immediate dominator's children list.
769 if (DomTreeNodeBase<NodeT> *IDom = Node->getIDom())
770 IDom->removeChild(Node);
771
772 DomTreeNodes[*IdxOpt] = nullptr;
773 if constexpr (!GraphHasNodeNumbers<NodeT *>)
774 NodeNumberMap.erase(BB);
775
776 if (!IsPostDom) return;
777
778 // Remember to update PostDominatorTree roots.
779 auto RIt = llvm::find(Roots, BB);
780 if (RIt != Roots.end()) {
781 std::swap(*RIt, Roots.back());
782 Roots.pop_back();
783 }
784 }
785
786 /// splitBlock - BB is split and now it has one successor. Update dominator
787 /// tree to reflect this change.
788 void splitBlock(NodeT *NewBB) {
789 if (IsPostDominator)
791 else
792 Split<NodeT *>(NewBB);
793 }
794
795 /// print - Convert to human readable form
796 ///
797 void print(raw_ostream &O) const {
798 O << "=============================--------------------------------\n";
799 if (IsPostDominator)
800 O << "Inorder PostDominator Tree: ";
801 else
802 O << "Inorder Dominator Tree: ";
803 if (!DFSInfoValid)
804 O << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
805 O << "\n";
806
807 // The postdom tree can have a null root if there are no returns.
809 O << "Roots: ";
810 for (const NodePtr Block : Roots) {
811 Block->printAsOperand(O, false);
812 O << " ";
813 }
814 O << "\n";
815 }
816
817public:
818 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
819 /// dominator tree in dfs order.
820 void updateDFSNumbers() const {
821 if (DFSInfoValid) {
822 SlowQueries = 0;
823 return;
824 }
825
828 32> WorkStack;
829
830 const DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
831 assert((!Parent || ThisRoot) && "Empty constructed DomTree");
832 if (!ThisRoot)
833 return;
834
835 // Both dominators and postdominators have a single root node. In the case
836 // case of PostDominatorTree, this node is a virtual root.
837 WorkStack.push_back({ThisRoot, ThisRoot->begin()});
838
839 unsigned DFSNum = 0;
840 ThisRoot->DFSNumIn = DFSNum++;
841
842 while (!WorkStack.empty()) {
843 const DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
844 const auto ChildIt = WorkStack.back().second;
845
846 // If we visited all of the children of this node, "recurse" back up the
847 // stack setting the DFOutNum.
848 if (ChildIt == Node->end()) {
849 Node->DFSNumOut = DFSNum++;
850 WorkStack.pop_back();
851 } else {
852 // Otherwise, recursively visit this child.
853 const DomTreeNodeBase<NodeT> *Child = *ChildIt;
854 ++WorkStack.back().second;
855
856 WorkStack.push_back({Child, Child->begin()});
857 Child->DFSNumIn = DFSNum++;
858 }
859 }
860
861 SlowQueries = 0;
862 DFSInfoValid = true;
863 }
864
865private:
866 void updateBlockNumberEpoch() {
867 // Nothing to do for graphs that don't number their blocks.
868 if constexpr (GraphHasNodeNumbers<NodeT *>)
870 }
871
872public:
873 /// recalculate - compute a dominator tree for the given function
875 Parent = &Func;
876 updateBlockNumberEpoch();
878 }
879
881 Parent = &Func;
882 updateBlockNumberEpoch();
884 }
885
886 /// Update dominator tree after renumbering blocks.
887 template <typename T = NodeT>
888 std::enable_if_t<GraphHasNodeNumbers<T *>, void> updateBlockNumbers() {
889 updateBlockNumberEpoch();
890
891 unsigned MaxNumber = GraphTraits<ParentPtr>::getMaxNumber(Parent);
892 DomTreeNodeStorageTy NewVector;
893 NewVector.resize(MaxNumber + 1); // +1, because index 0 is for nullptr
894 for (auto &Node : DomTreeNodes) {
895 if (!Node)
896 continue;
897 unsigned Idx = *getNodeIndex(Node->getBlock());
898 // getMaxNumber is not necessarily supported
899 if (Idx >= NewVector.size())
900 NewVector.resize(Idx + 1);
901 NewVector[Idx] = std::move(Node);
902 }
903 DomTreeNodes = std::move(NewVector);
904 }
905
906 /// verify - checks if the tree is correct. There are 3 level of verification:
907 /// - Full -- verifies if the tree is correct by making sure all the
908 /// properties (including the parent and the sibling property)
909 /// hold.
910 /// Takes O(N^3) time.
911 ///
912 /// - Basic -- checks if the tree is correct, but compares it to a freshly
913 /// constructed tree instead of checking the sibling property.
914 /// Takes O(N^2) time.
915 ///
916 /// - Fast -- checks basic tree structure and compares it with a freshly
917 /// constructed tree.
918 /// Takes O(N^2) time worst case, but is faster in practise (same
919 /// as tree construction).
921 return DomTreeBuilder::Verify(*this, VL);
922 }
923
924 void reset() {
925 DomTreeNodes.clear();
926 if constexpr (!GraphHasNodeNumbers<NodeT *>)
927 NodeNumberMap.clear();
928 Roots.clear();
929 RootNode = nullptr;
930 Parent = nullptr;
931 DFSInfoValid = false;
932 NodeAllocator.Reset();
933 SlowQueries = 0;
934 }
935
936protected:
937 inline void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
938
940 DomTreeNodeBase<NodeT> *IDom = nullptr) {
941 static_assert(std::is_trivially_destructible_v<DomTreeNodeBase<NodeT>>);
942 auto *Node = new (NodeAllocator) DomTreeNodeBase<NodeT>(BB, IDom);
943 unsigned NodeIdx = getNodeIndexForInsert(BB);
944 DomTreeNodes[NodeIdx] = Node;
945 if (IDom)
946 IDom->addChild(Node);
947 return Node;
948 }
949
950 // NewBB is split and now it has one successor. Update dominator tree to
951 // reflect this change.
952 template <class N>
953 void Split(typename GraphTraits<N>::NodeRef NewBB) {
954 using GraphT = GraphTraits<N>;
955 using NodeRef = typename GraphT::NodeRef;
957 "NewBB should have a single successor!");
958 NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
959
961
962 assert(!PredBlocks.empty() && "No predblocks?");
963
964 bool NewBBDominatesNewBBSucc = true;
965 for (auto *Pred : inverse_children<N>(NewBBSucc)) {
966 if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
967 isReachableFromEntry(Pred)) {
968 NewBBDominatesNewBBSucc = false;
969 break;
970 }
971 }
972
973 // Find NewBB's immediate dominator and create new dominator tree node for
974 // NewBB.
975 NodeT *NewBBIDom = nullptr;
976 unsigned i = 0;
977 for (i = 0; i < PredBlocks.size(); ++i)
978 if (isReachableFromEntry(PredBlocks[i])) {
979 NewBBIDom = PredBlocks[i];
980 break;
981 }
982
983 // It's possible that none of the predecessors of NewBB are reachable;
984 // in that case, NewBB itself is unreachable, so nothing needs to be
985 // changed.
986 if (!NewBBIDom) return;
987
988 for (i = i + 1; i < PredBlocks.size(); ++i) {
989 if (isReachableFromEntry(PredBlocks[i]))
990 NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
991 }
992
993 // Create the new dominator tree node... and set the idom of NewBB.
994 DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
995
996 // If NewBB strictly dominates other blocks, then it is now the immediate
997 // dominator of NewBBSucc. Update the dominator tree as appropriate.
998 if (NewBBDominatesNewBBSucc) {
999 DomTreeNodeBase<NodeT> *NewBBSuccNode = getNode(NewBBSucc);
1000 changeImmediateDominator(NewBBSuccNode, NewBBNode);
1001 }
1002 }
1003
1004 private:
1005 bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
1006 const DomTreeNodeBase<NodeT> *B) const {
1007 assert(A != B);
1010
1011 const unsigned ALevel = A->getLevel();
1012 const DomTreeNodeBase<NodeT> *IDom;
1013
1014 // Don't walk nodes above A's subtree. When we reach A's level, we must
1015 // either find A or be in some other subtree not dominated by A.
1016 while ((IDom = B->getIDom()) != nullptr && IDom->getLevel() >= ALevel)
1017 B = IDom; // Walk up the tree
1018
1019 return B == A;
1020 }
1021};
1022
1023template <typename T>
1025
1026template <typename T>
1028
1029// These two functions are declared out of line as a workaround for building
1030// with old (< r147295) versions of clang because of pr11642.
1031template <typename NodeT, bool IsPostDom>
1033 const NodeT *B) const {
1034 if (A == B)
1035 return true;
1036
1037 return dominates(getNode(A), getNode(B));
1038}
1039template <typename NodeT, bool IsPostDom>
1041 const NodeT *A, const NodeT *B) const {
1042 if (A == B)
1043 return false;
1044
1045 return dominates(getNode(A), getNode(B));
1046}
1047
1048} // end namespace llvm
1049
1050#endif // LLVM_SUPPORT_GENERICDOMTREE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
#define I(x, y, z)
Definition MD5.cpp:57
ppc ctr loops PowerPC CTR Loops Verify
static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A, const MachineInstr &B)
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Value * RHS
void printAsOperand(OutputBuffer &OB, Prec P=Prec::Default, bool StrictlyWorse=false) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition Allocator.h:67
bool operator==(const const_iterator &Other) const
DomTreeNodeBase * operator*() const
const_iterator(DomTreeNodeBase *Node=nullptr)
Base class for the actual dominator tree node.
iterator_range< iterator > children()
DomTreeNodeBase(const DomTreeNodeBase &)=delete
void setIDom(DomTreeNodeBase *NewIDom)
void removeChild(DomTreeNodeBase *C)
DomTreeNodeBase * getIDom() const
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree.
iterator begin() const
DomTreeNodeBase & operator=(const DomTreeNodeBase &)=delete
DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
bool compare(const DomTreeNodeBase *Other) const
NodeT * getBlock() const
unsigned getLevel() const
iterator end() const
iterator_range< const_iterator > children() const
unsigned getDFSNumOut() const
void addChild(DomTreeNodeBase *C)
Core dominator tree base class.
DominatorTreeBase(DominatorTreeBase &&Arg)=default
DomTreeNodeTraits< BlockT > NodeTrait
bool isReachableFromEntry(const DomTreeNodeBase< NodeT > *A) const
void print(raw_ostream &O) const
print - Convert to human readable form
typename NodeTrait::NodeType NodeType
DomTreeNodeBase< NodeT > * operator[](const NodeT *BB) const
See getNode.
typename SmallVectorImpl< BlockT * >::iterator root_iterator
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
void changeImmediateDominator(NodeT *BB, NodeT *NewBB)
NodeT * findNearestCommonDominator(NodeT *A, NodeT *B) const
Find nearest common dominator basic block for basic block A and B.
void Split(typename GraphTraits< N >::NodeRef NewBB)
iterator_range< root_iterator > roots()
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
std::remove_pointer_t< ParentPtr > ParentType
NodeT * findNearestCommonDominator(iterator_range< IteratorTy > Nodes) const
BumpPtrAllocatorImpl< MallocAllocator, SlabSize, SlabSize, 2 > NodeAllocator
bool isPostDominator() const
isPostDominator - Returns true if analysis based of postdoms
bool dominates(const NodeT *A, const NodeT *B) const
const NodeT * findNearestCommonDominator(const NodeT *A, const NodeT *B) const
void getDescendants(NodeT *R, SmallVectorImpl< NodeT * > &Result) const
Get all nodes dominated by R, including R itself.
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * createNode(NodeT *BB, DomTreeNodeBase< NodeT > *IDom=nullptr)
void applyUpdates(ArrayRef< UpdateType > Updates)
Inform the dominator tree about a sequence of CFG edge insertions and deletions and perform a batch u...
void insertEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge insertion and update the tree.
std::enable_if_t< GraphHasNodeNumbers< T * >, void > updateBlockNumbers()
Update dominator tree after renumbering blocks.
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
iterator_range< const_root_iterator > roots() const
const_root_iterator root_end() const
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
void recalculate(ParentType &Func, ArrayRef< UpdateType > Updates)
void updateDFSNumbers() const
updateDFSNumbers - Assign In and Out numbers to the nodes while walking dominator tree in dfs order.
typename SmallVectorImpl< BlockT * >::const_iterator const_root_iterator
bool compare(const DominatorTreeBase &Other) const
compare - Return false if the other dominator tree base matches this dominator tree base.
DominatorTreeBase & operator=(DominatorTreeBase &&RHS)=default
DomTreeNodeBase< NodeT > * setNewRoot(NodeT *BB)
Add a new node to the forward dominator tree and make it a new root.
SmallVector< DomTreeNodeBase< BlockT > * > DomTreeNodeStorageTy
root_iterator root_begin()
DominatorTreeBase(const DominatorTreeBase &)=delete
const_root_iterator root_begin() const
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
SmallVector< BlockT *, IsPostDom ? 4 :1 > Roots
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
void deleteEdge(NodeT *From, NodeT *To)
Inform the dominator tree about a CFG edge deletion and update the tree.
const DomTreeNodeBase< NodeT > * getRootNode() const
std::conditional_t<!GraphHasNodeNumbers< BlockT * >, DenseMap< const BlockT *, unsigned >, std::tuple<> > NodeNumberMap
DomTreeNodeBase< BlockT > * RootNode
typename NodeTrait::NodePtr NodePtr
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
void applyUpdates(ArrayRef< UpdateType > Updates, ArrayRef< UpdateType > PostViewUpdates)
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool properlyDominates(const NodeT *A, const NodeT *B) const
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
bool isVirtualRoot(const DomTreeNodeBase< NodeT > *A) const
typename NodeTrait::ParentPtr ParentPtr
DominatorTreeBase & operator=(const DominatorTreeBase &)=delete
size_type size() const
Definition SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
typename SuperClass::const_iterator const_iterator
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
typename SuperClass::iterator iterator
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
A range adaptor for a pair of iterators.
IteratorT begin() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL)
void CalculateWithUpdates(DomTreeT &DT, ArrayRef< typename DomTreeT::UpdateType > Updates)
void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
void ApplyUpdates(DomTreeT &DT, GraphDiff< typename DomTreeT::NodePtr, DomTreeT::IsPostDominator > &PreViewCFG, GraphDiff< typename DomTreeT::NodePtr, DomTreeT::IsPostDominator > *PostViewCFG)
void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1763
void PrintDomTree(const DomTreeNodeBase< NodeT > *N, raw_ostream &O, unsigned Lev)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr bool GraphHasNodeNumbers
Indicate whether a GraphTraits<NodeT>::getNumber() is supported.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
DominatorTreeBase< T, true > PostDomTreeBase
DominatorTreeBase< T, false > DomTreeBase
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Other
Any other memory.
Definition ModRef.h:68
iterator_range< typename GraphTraits< Inverse< GraphType > >::ChildIteratorType > inverse_children(const typename GraphTraits< GraphType >::NodeRef &G)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
Default DomTreeNode traits for NodeT.
static NodeT * getEntryNode(ParentPtr Parent)
std::remove_pointer_t< ParentPtr > ParentType
static ParentPtr getParent(NodePtr BB)
decltype(std::declval< NodePtr >() ->getParent()) ParentPtr
typename GraphType::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95