Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:31

0001 //===- GenericDomTree.h - Generic dominator trees for graphs ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This file defines a set of templates that efficiently compute a dominator
0011 /// tree over a generic graph. This is used typically in LLVM for fast
0012 /// dominance queries on the CFG, but is fully generic w.r.t. the underlying
0013 /// graph types.
0014 ///
0015 /// Unlike ADT/* graph algorithms, generic dominator tree has more requirements
0016 /// on the graph's NodeRef. The NodeRef should be a pointer and,
0017 /// either NodeRef->getParent() must return the parent node that is also a
0018 /// pointer or DomTreeNodeTraits needs to be specialized.
0019 ///
0020 /// FIXME: Maybe GenericDomTree needs a TreeTraits, instead of GraphTraits.
0021 ///
0022 //===----------------------------------------------------------------------===//
0023 
0024 #ifndef LLVM_SUPPORT_GENERICDOMTREE_H
0025 #define LLVM_SUPPORT_GENERICDOMTREE_H
0026 
0027 #include "llvm/ADT/DenseMap.h"
0028 #include "llvm/ADT/GraphTraits.h"
0029 #include "llvm/ADT/STLExtras.h"
0030 #include "llvm/ADT/SmallPtrSet.h"
0031 #include "llvm/ADT/SmallVector.h"
0032 #include "llvm/Support/CFGDiff.h"
0033 #include "llvm/Support/CFGUpdate.h"
0034 #include "llvm/Support/raw_ostream.h"
0035 #include <algorithm>
0036 #include <cassert>
0037 #include <cstddef>
0038 #include <iterator>
0039 #include <memory>
0040 #include <type_traits>
0041 #include <utility>
0042 
0043 namespace llvm {
0044 
0045 template <typename NodeT, bool IsPostDom>
0046 class DominatorTreeBase;
0047 
0048 namespace DomTreeBuilder {
0049 template <typename DomTreeT>
0050 struct SemiNCAInfo;
0051 }  // namespace DomTreeBuilder
0052 
0053 /// Base class for the actual dominator tree node.
0054 template <class NodeT> class DomTreeNodeBase {
0055   friend class PostDominatorTree;
0056   friend class DominatorTreeBase<NodeT, false>;
0057   friend class DominatorTreeBase<NodeT, true>;
0058   friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase<NodeT, false>>;
0059   friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase<NodeT, true>>;
0060 
0061   NodeT *TheBB;
0062   DomTreeNodeBase *IDom;
0063   unsigned Level;
0064   SmallVector<DomTreeNodeBase *, 4> Children;
0065   mutable unsigned DFSNumIn = ~0;
0066   mutable unsigned DFSNumOut = ~0;
0067 
0068  public:
0069   DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
0070       : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
0071 
0072   using iterator = typename SmallVector<DomTreeNodeBase *, 4>::iterator;
0073   using const_iterator =
0074       typename SmallVector<DomTreeNodeBase *, 4>::const_iterator;
0075 
0076   iterator begin() { return Children.begin(); }
0077   iterator end() { return Children.end(); }
0078   const_iterator begin() const { return Children.begin(); }
0079   const_iterator end() const { return Children.end(); }
0080 
0081   DomTreeNodeBase *const &back() const { return Children.back(); }
0082   DomTreeNodeBase *&back() { return Children.back(); }
0083 
0084   iterator_range<iterator> children() { return make_range(begin(), end()); }
0085   iterator_range<const_iterator> children() const {
0086     return make_range(begin(), end());
0087   }
0088 
0089   NodeT *getBlock() const { return TheBB; }
0090   DomTreeNodeBase *getIDom() const { return IDom; }
0091   unsigned getLevel() const { return Level; }
0092 
0093   void addChild(DomTreeNodeBase *C) { Children.push_back(C); }
0094 
0095   bool isLeaf() const { return Children.empty(); }
0096   size_t getNumChildren() const { return Children.size(); }
0097 
0098   void clearAllChildren() { Children.clear(); }
0099 
0100   bool compare(const DomTreeNodeBase *Other) const {
0101     if (getNumChildren() != Other->getNumChildren())
0102       return true;
0103 
0104     if (Level != Other->Level) return true;
0105 
0106     SmallPtrSet<const NodeT *, 4> OtherChildren;
0107     for (const DomTreeNodeBase *I : *Other) {
0108       const NodeT *Nd = I->getBlock();
0109       OtherChildren.insert(Nd);
0110     }
0111 
0112     for (const DomTreeNodeBase *I : *this) {
0113       const NodeT *N = I->getBlock();
0114       if (OtherChildren.count(N) == 0)
0115         return true;
0116     }
0117     return false;
0118   }
0119 
0120   void setIDom(DomTreeNodeBase *NewIDom) {
0121     assert(IDom && "No immediate dominator?");
0122     if (IDom == NewIDom) return;
0123 
0124     auto I = find(IDom->Children, this);
0125     assert(I != IDom->Children.end() &&
0126            "Not in immediate dominator children set!");
0127     // I am no longer your child...
0128     IDom->Children.erase(I);
0129 
0130     // Switch to new dominator
0131     IDom = NewIDom;
0132     IDom->Children.push_back(this);
0133 
0134     UpdateLevel();
0135   }
0136 
0137   /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
0138   /// in the dominator tree. They are only guaranteed valid if
0139   /// updateDFSNumbers() has been called.
0140   unsigned getDFSNumIn() const { return DFSNumIn; }
0141   unsigned getDFSNumOut() const { return DFSNumOut; }
0142 
0143 private:
0144   // Return true if this node is dominated by other. Use this only if DFS info
0145   // is valid.
0146   bool DominatedBy(const DomTreeNodeBase *other) const {
0147     return this->DFSNumIn >= other->DFSNumIn &&
0148            this->DFSNumOut <= other->DFSNumOut;
0149   }
0150 
0151   void UpdateLevel() {
0152     assert(IDom);
0153     if (Level == IDom->Level + 1) return;
0154 
0155     SmallVector<DomTreeNodeBase *, 64> WorkStack = {this};
0156 
0157     while (!WorkStack.empty()) {
0158       DomTreeNodeBase *Current = WorkStack.pop_back_val();
0159       Current->Level = Current->IDom->Level + 1;
0160 
0161       for (DomTreeNodeBase *C : *Current) {
0162         assert(C->IDom);
0163         if (C->Level != C->IDom->Level + 1) WorkStack.push_back(C);
0164       }
0165     }
0166   }
0167 };
0168 
0169 template <class NodeT>
0170 raw_ostream &operator<<(raw_ostream &O, const DomTreeNodeBase<NodeT> *Node) {
0171   if (Node->getBlock())
0172     Node->getBlock()->printAsOperand(O, false);
0173   else
0174     O << " <<exit node>>";
0175 
0176   O << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "} ["
0177     << Node->getLevel() << "]\n";
0178 
0179   return O;
0180 }
0181 
0182 template <class NodeT>
0183 void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &O,
0184                   unsigned Lev) {
0185   O.indent(2 * Lev) << "[" << Lev << "] " << N;
0186   for (const auto &I : *N)
0187     PrintDomTree<NodeT>(I, O, Lev + 1);
0188 }
0189 
0190 namespace DomTreeBuilder {
0191 // The routines below are provided in a separate header but referenced here.
0192 template <typename DomTreeT>
0193 void Calculate(DomTreeT &DT);
0194 
0195 template <typename DomTreeT>
0196 void CalculateWithUpdates(DomTreeT &DT,
0197                           ArrayRef<typename DomTreeT::UpdateType> Updates);
0198 
0199 template <typename DomTreeT>
0200 void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
0201                 typename DomTreeT::NodePtr To);
0202 
0203 template <typename DomTreeT>
0204 void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
0205                 typename DomTreeT::NodePtr To);
0206 
0207 template <typename DomTreeT>
0208 void ApplyUpdates(DomTreeT &DT,
0209                   GraphDiff<typename DomTreeT::NodePtr,
0210                             DomTreeT::IsPostDominator> &PreViewCFG,
0211                   GraphDiff<typename DomTreeT::NodePtr,
0212                             DomTreeT::IsPostDominator> *PostViewCFG);
0213 
0214 template <typename DomTreeT>
0215 bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL);
0216 }  // namespace DomTreeBuilder
0217 
0218 /// Default DomTreeNode traits for NodeT. The default implementation assume a
0219 /// Function-like NodeT. Can be specialized to support different node types.
0220 template <typename NodeT> struct DomTreeNodeTraits {
0221   using NodeType = NodeT;
0222   using NodePtr = NodeT *;
0223   using ParentPtr = decltype(std::declval<NodePtr>()->getParent());
0224   static_assert(std::is_pointer_v<ParentPtr>,
0225                 "Currently NodeT's parent must be a pointer type");
0226   using ParentType = std::remove_pointer_t<ParentPtr>;
0227 
0228   static NodeT *getEntryNode(ParentPtr Parent) { return &Parent->front(); }
0229   static ParentPtr getParent(NodePtr BB) { return BB->getParent(); }
0230 };
0231 
0232 /// Core dominator tree base class.
0233 ///
0234 /// This class is a generic template over graph nodes. It is instantiated for
0235 /// various graphs in the LLVM IR or in the code generator.
0236 template <typename NodeT, bool IsPostDom>
0237 class DominatorTreeBase {
0238  public:
0239   static_assert(std::is_pointer_v<typename GraphTraits<NodeT *>::NodeRef>,
0240                 "Currently DominatorTreeBase supports only pointer nodes");
0241   using NodeTrait = DomTreeNodeTraits<NodeT>;
0242   using NodeType = typename NodeTrait::NodeType;
0243   using NodePtr = typename NodeTrait::NodePtr;
0244   using ParentPtr = typename NodeTrait::ParentPtr;
0245   static_assert(std::is_pointer_v<ParentPtr>,
0246                 "Currently NodeT's parent must be a pointer type");
0247   using ParentType = std::remove_pointer_t<ParentPtr>;
0248   static constexpr bool IsPostDominator = IsPostDom;
0249 
0250   using UpdateType = cfg::Update<NodePtr>;
0251   using UpdateKind = cfg::UpdateKind;
0252   static constexpr UpdateKind Insert = UpdateKind::Insert;
0253   static constexpr UpdateKind Delete = UpdateKind::Delete;
0254 
0255   enum class VerificationLevel { Fast, Basic, Full };
0256 
0257 protected:
0258   // Dominators always have a single root, postdominators can have more.
0259   SmallVector<NodeT *, IsPostDom ? 4 : 1> Roots;
0260 
0261   using DomTreeNodeStorageTy =
0262       SmallVector<std::unique_ptr<DomTreeNodeBase<NodeT>>>;
0263   DomTreeNodeStorageTy DomTreeNodes;
0264   // For graphs where blocks don't have numbers, create a numbering here.
0265   // TODO: use an empty struct with [[no_unique_address]] in C++20.
0266   std::conditional_t<!GraphHasNodeNumbers<NodeT *>,
0267                      DenseMap<const NodeT *, unsigned>, std::tuple<>>
0268       NodeNumberMap;
0269   DomTreeNodeBase<NodeT> *RootNode = nullptr;
0270   ParentPtr Parent = nullptr;
0271 
0272   mutable bool DFSInfoValid = false;
0273   mutable unsigned int SlowQueries = 0;
0274   unsigned BlockNumberEpoch = 0;
0275 
0276   friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase>;
0277 
0278  public:
0279   DominatorTreeBase() = default;
0280 
0281   DominatorTreeBase(DominatorTreeBase &&Arg)
0282       : Roots(std::move(Arg.Roots)), DomTreeNodes(std::move(Arg.DomTreeNodes)),
0283         NodeNumberMap(std::move(Arg.NodeNumberMap)), RootNode(Arg.RootNode),
0284         Parent(Arg.Parent), DFSInfoValid(Arg.DFSInfoValid),
0285         SlowQueries(Arg.SlowQueries), BlockNumberEpoch(Arg.BlockNumberEpoch) {
0286     Arg.wipe();
0287   }
0288 
0289   DominatorTreeBase &operator=(DominatorTreeBase &&RHS) {
0290     if (this == &RHS)
0291       return *this;
0292     Roots = std::move(RHS.Roots);
0293     DomTreeNodes = std::move(RHS.DomTreeNodes);
0294     NodeNumberMap = std::move(RHS.NodeNumberMap);
0295     RootNode = RHS.RootNode;
0296     Parent = RHS.Parent;
0297     DFSInfoValid = RHS.DFSInfoValid;
0298     SlowQueries = RHS.SlowQueries;
0299     BlockNumberEpoch = RHS.BlockNumberEpoch;
0300     RHS.wipe();
0301     return *this;
0302   }
0303 
0304   DominatorTreeBase(const DominatorTreeBase &) = delete;
0305   DominatorTreeBase &operator=(const DominatorTreeBase &) = delete;
0306 
0307   /// Iteration over roots.
0308   ///
0309   /// This may include multiple blocks if we are computing post dominators.
0310   /// For forward dominators, this will always be a single block (the entry
0311   /// block).
0312   using root_iterator = typename SmallVectorImpl<NodeT *>::iterator;
0313   using const_root_iterator = typename SmallVectorImpl<NodeT *>::const_iterator;
0314 
0315   root_iterator root_begin() { return Roots.begin(); }
0316   const_root_iterator root_begin() const { return Roots.begin(); }
0317   root_iterator root_end() { return Roots.end(); }
0318   const_root_iterator root_end() const { return Roots.end(); }
0319 
0320   size_t root_size() const { return Roots.size(); }
0321 
0322   iterator_range<root_iterator> roots() {
0323     return make_range(root_begin(), root_end());
0324   }
0325   iterator_range<const_root_iterator> roots() const {
0326     return make_range(root_begin(), root_end());
0327   }
0328 
0329   /// isPostDominator - Returns true if analysis based of postdoms
0330   ///
0331   bool isPostDominator() const { return IsPostDominator; }
0332 
0333   /// compare - Return false if the other dominator tree base matches this
0334   /// dominator tree base. Otherwise return true.
0335   bool compare(const DominatorTreeBase &Other) const {
0336     if (Parent != Other.Parent) return true;
0337 
0338     if (Roots.size() != Other.Roots.size())
0339       return true;
0340 
0341     if (!std::is_permutation(Roots.begin(), Roots.end(), Other.Roots.begin()))
0342       return true;
0343 
0344     size_t NumNodes = 0;
0345     // All nodes we have must exist and be equal in the other tree.
0346     for (const auto &Node : DomTreeNodes) {
0347       if (!Node)
0348         continue;
0349       if (Node->compare(Other.getNode(Node->getBlock())))
0350         return true;
0351       NumNodes++;
0352     }
0353 
0354     // If the other tree has more nodes than we have, they're not equal.
0355     size_t NumOtherNodes = 0;
0356     for (const auto &OtherNode : Other.DomTreeNodes)
0357       if (OtherNode)
0358         NumOtherNodes++;
0359     return NumNodes != NumOtherNodes;
0360   }
0361 
0362 private:
0363   std::optional<unsigned> getNodeIndex(const NodeT *BB) const {
0364     if constexpr (GraphHasNodeNumbers<NodeT *>) {
0365       // BB can be nullptr, map nullptr to index 0.
0366       assert(BlockNumberEpoch ==
0367                  GraphTraits<ParentPtr>::getNumberEpoch(Parent) &&
0368              "dominator tree used with outdated block numbers");
0369       return BB ? GraphTraits<const NodeT *>::getNumber(BB) + 1 : 0;
0370     } else {
0371       if (auto It = NodeNumberMap.find(BB); It != NodeNumberMap.end())
0372         return It->second;
0373       return std::nullopt;
0374     }
0375   }
0376 
0377   unsigned getNodeIndexForInsert(const NodeT *BB) {
0378     if constexpr (GraphHasNodeNumbers<NodeT *>) {
0379       // getNodeIndex will never fail if nodes have getNumber().
0380       unsigned Idx = *getNodeIndex(BB);
0381       if (Idx >= DomTreeNodes.size()) {
0382         unsigned Max = GraphTraits<ParentPtr>::getMaxNumber(Parent);
0383         DomTreeNodes.resize(Max > Idx + 1 ? Max : Idx + 1);
0384       }
0385       return Idx;
0386     } else {
0387       // We might already have a number stored for BB.
0388       unsigned Idx =
0389           NodeNumberMap.try_emplace(BB, DomTreeNodes.size()).first->second;
0390       if (Idx >= DomTreeNodes.size())
0391         DomTreeNodes.resize(Idx + 1);
0392       return Idx;
0393     }
0394   }
0395 
0396 public:
0397   /// getNode - return the (Post)DominatorTree node for the specified basic
0398   /// block.  This is the same as using operator[] on this class.  The result
0399   /// may (but is not required to) be null for a forward (backwards)
0400   /// statically unreachable block.
0401   DomTreeNodeBase<NodeT> *getNode(const NodeT *BB) const {
0402     assert((!BB || Parent == NodeTrait::getParent(const_cast<NodeT *>(BB))) &&
0403            "cannot get DomTreeNode of block with different parent");
0404     if (auto Idx = getNodeIndex(BB); Idx && *Idx < DomTreeNodes.size())
0405       return DomTreeNodes[*Idx].get();
0406     return nullptr;
0407   }
0408 
0409   /// See getNode.
0410   DomTreeNodeBase<NodeT> *operator[](const NodeT *BB) const {
0411     return getNode(BB);
0412   }
0413 
0414   /// getRootNode - This returns the entry node for the CFG of the function.  If
0415   /// this tree represents the post-dominance relations for a function, however,
0416   /// this root may be a node with the block == NULL.  This is the case when
0417   /// there are multiple exit nodes from a particular function.  Consumers of
0418   /// post-dominance information must be capable of dealing with this
0419   /// possibility.
0420   ///
0421   DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
0422   const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
0423 
0424   /// Get all nodes dominated by R, including R itself.
0425   void getDescendants(NodeT *R, SmallVectorImpl<NodeT *> &Result) const {
0426     Result.clear();
0427     const DomTreeNodeBase<NodeT> *RN = getNode(R);
0428     if (!RN)
0429       return; // If R is unreachable, it will not be present in the DOM tree.
0430     SmallVector<const DomTreeNodeBase<NodeT> *, 8> WL;
0431     WL.push_back(RN);
0432 
0433     while (!WL.empty()) {
0434       const DomTreeNodeBase<NodeT> *N = WL.pop_back_val();
0435       Result.push_back(N->getBlock());
0436       WL.append(N->begin(), N->end());
0437     }
0438   }
0439 
0440   /// properlyDominates - Returns true iff A dominates B and A != B.
0441   /// Note that this is not a constant time operation!
0442   ///
0443   bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
0444                          const DomTreeNodeBase<NodeT> *B) const {
0445     if (!A || !B)
0446       return false;
0447     if (A == B)
0448       return false;
0449     return dominates(A, B);
0450   }
0451 
0452   bool properlyDominates(const NodeT *A, const NodeT *B) const;
0453 
0454   /// isReachableFromEntry - Return true if A is dominated by the entry
0455   /// block of the function containing it.
0456   bool isReachableFromEntry(const NodeT *A) const {
0457     assert(!this->isPostDominator() &&
0458            "This is not implemented for post dominators");
0459     return isReachableFromEntry(getNode(A));
0460   }
0461 
0462   bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const { return A; }
0463 
0464   /// dominates - Returns true iff A dominates B.  Note that this is not a
0465   /// constant time operation!
0466   ///
0467   bool dominates(const DomTreeNodeBase<NodeT> *A,
0468                  const DomTreeNodeBase<NodeT> *B) const {
0469     // A node trivially dominates itself.
0470     if (B == A)
0471       return true;
0472 
0473     // An unreachable node is dominated by anything.
0474     if (!isReachableFromEntry(B))
0475       return true;
0476 
0477     // And dominates nothing.
0478     if (!isReachableFromEntry(A))
0479       return false;
0480 
0481     if (B->getIDom() == A) return true;
0482 
0483     if (A->getIDom() == B) return false;
0484 
0485     // A can only dominate B if it is higher in the tree.
0486     if (A->getLevel() >= B->getLevel()) return false;
0487 
0488     // Compare the result of the tree walk and the dfs numbers, if expensive
0489     // checks are enabled.
0490 #ifdef EXPENSIVE_CHECKS
0491     assert((!DFSInfoValid ||
0492             (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
0493            "Tree walk disagrees with dfs numbers!");
0494 #endif
0495 
0496     if (DFSInfoValid)
0497       return B->DominatedBy(A);
0498 
0499     // If we end up with too many slow queries, just update the
0500     // DFS numbers on the theory that we are going to keep querying.
0501     SlowQueries++;
0502     if (SlowQueries > 32) {
0503       updateDFSNumbers();
0504       return B->DominatedBy(A);
0505     }
0506 
0507     return dominatedBySlowTreeWalk(A, B);
0508   }
0509 
0510   bool dominates(const NodeT *A, const NodeT *B) const;
0511 
0512   NodeT *getRoot() const {
0513     assert(this->Roots.size() == 1 && "Should always have entry node!");
0514     return this->Roots[0];
0515   }
0516 
0517   /// Find nearest common dominator basic block for basic block A and B. A and B
0518   /// must have tree nodes.
0519   NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) const {
0520     assert(A && B && "Pointers are not valid");
0521     assert(NodeTrait::getParent(A) == NodeTrait::getParent(B) &&
0522            "Two blocks are not in same function");
0523 
0524     // If either A or B is a entry block then it is nearest common dominator
0525     // (for forward-dominators).
0526     if (!isPostDominator()) {
0527       NodeT &Entry =
0528           *DomTreeNodeTraits<NodeT>::getEntryNode(NodeTrait::getParent(A));
0529       if (A == &Entry || B == &Entry)
0530         return &Entry;
0531     }
0532 
0533     DomTreeNodeBase<NodeT> *NodeA = getNode(A);
0534     DomTreeNodeBase<NodeT> *NodeB = getNode(B);
0535     assert(NodeA && "A must be in the tree");
0536     assert(NodeB && "B must be in the tree");
0537 
0538     // Use level information to go up the tree until the levels match. Then
0539     // continue going up til we arrive at the same node.
0540     while (NodeA != NodeB) {
0541       if (NodeA->getLevel() < NodeB->getLevel()) std::swap(NodeA, NodeB);
0542 
0543       NodeA = NodeA->IDom;
0544     }
0545 
0546     return NodeA->getBlock();
0547   }
0548 
0549   const NodeT *findNearestCommonDominator(const NodeT *A,
0550                                           const NodeT *B) const {
0551     // Cast away the const qualifiers here. This is ok since
0552     // const is re-introduced on the return type.
0553     return findNearestCommonDominator(const_cast<NodeT *>(A),
0554                                       const_cast<NodeT *>(B));
0555   }
0556 
0557   bool isVirtualRoot(const DomTreeNodeBase<NodeT> *A) const {
0558     return isPostDominator() && !A->getBlock();
0559   }
0560 
0561   template <typename IteratorTy>
0562   NodeT *findNearestCommonDominator(iterator_range<IteratorTy> Nodes) const {
0563     assert(!Nodes.empty() && "Nodes list is empty!");
0564 
0565     NodeT *NCD = *Nodes.begin();
0566     for (NodeT *Node : llvm::drop_begin(Nodes)) {
0567       NCD = findNearestCommonDominator(NCD, Node);
0568 
0569       // Stop when the root is reached.
0570       if (isVirtualRoot(getNode(NCD)))
0571         return nullptr;
0572     }
0573 
0574     return NCD;
0575   }
0576 
0577   //===--------------------------------------------------------------------===//
0578   // API to update (Post)DominatorTree information based on modifications to
0579   // the CFG...
0580 
0581   /// Inform the dominator tree about a sequence of CFG edge insertions and
0582   /// deletions and perform a batch update on the tree.
0583   ///
0584   /// This function should be used when there were multiple CFG updates after
0585   /// the last dominator tree update. It takes care of performing the updates
0586   /// in sync with the CFG and optimizes away the redundant operations that
0587   /// cancel each other.
0588   /// The functions expects the sequence of updates to be balanced. Eg.:
0589   ///  - {{Insert, A, B}, {Delete, A, B}, {Insert, A, B}} is fine, because
0590   ///    logically it results in a single insertions.
0591   ///  - {{Insert, A, B}, {Insert, A, B}} is invalid, because it doesn't make
0592   ///    sense to insert the same edge twice.
0593   ///
0594   /// What's more, the functions assumes that it's safe to ask every node in the
0595   /// CFG about its children and inverse children. This implies that deletions
0596   /// of CFG edges must not delete the CFG nodes before calling this function.
0597   ///
0598   /// The applyUpdates function can reorder the updates and remove redundant
0599   /// ones internally (as long as it is done in a deterministic fashion). The
0600   /// batch updater is also able to detect sequences of zero and exactly one
0601   /// update -- it's optimized to do less work in these cases.
0602   ///
0603   /// Note that for postdominators it automatically takes care of applying
0604   /// updates on reverse edges internally (so there's no need to swap the
0605   /// From and To pointers when constructing DominatorTree::UpdateType).
0606   /// The type of updates is the same for DomTreeBase<T> and PostDomTreeBase<T>
0607   /// with the same template parameter T.
0608   ///
0609   /// \param Updates An ordered sequence of updates to perform. The current CFG
0610   /// and the reverse of these updates provides the pre-view of the CFG.
0611   ///
0612   void applyUpdates(ArrayRef<UpdateType> Updates) {
0613     GraphDiff<NodePtr, IsPostDominator> PreViewCFG(
0614         Updates, /*ReverseApplyUpdates=*/true);
0615     DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, nullptr);
0616   }
0617 
0618   /// \param Updates An ordered sequence of updates to perform. The current CFG
0619   /// and the reverse of these updates provides the pre-view of the CFG.
0620   /// \param PostViewUpdates An ordered sequence of update to perform in order
0621   /// to obtain a post-view of the CFG. The DT will be updated assuming the
0622   /// obtained PostViewCFG is the desired end state.
0623   void applyUpdates(ArrayRef<UpdateType> Updates,
0624                     ArrayRef<UpdateType> PostViewUpdates) {
0625     if (Updates.empty()) {
0626       GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
0627       DomTreeBuilder::ApplyUpdates(*this, PostViewCFG, &PostViewCFG);
0628     } else {
0629       // PreViewCFG needs to merge Updates and PostViewCFG. The updates in
0630       // Updates need to be reversed, and match the direction in PostViewCFG.
0631       // The PostViewCFG is created with updates reversed (equivalent to changes
0632       // made to the CFG), so the PreViewCFG needs all the updates reverse
0633       // applied.
0634       SmallVector<UpdateType> AllUpdates(Updates);
0635       append_range(AllUpdates, PostViewUpdates);
0636       GraphDiff<NodePtr, IsPostDom> PreViewCFG(AllUpdates,
0637                                                /*ReverseApplyUpdates=*/true);
0638       GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
0639       DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, &PostViewCFG);
0640     }
0641   }
0642 
0643   /// Inform the dominator tree about a CFG edge insertion and update the tree.
0644   ///
0645   /// This function has to be called just before or just after making the update
0646   /// on the actual CFG. There cannot be any other updates that the dominator
0647   /// tree doesn't know about.
0648   ///
0649   /// Note that for postdominators it automatically takes care of inserting
0650   /// a reverse edge internally (so there's no need to swap the parameters).
0651   ///
0652   void insertEdge(NodeT *From, NodeT *To) {
0653     assert(From);
0654     assert(To);
0655     assert(NodeTrait::getParent(From) == Parent);
0656     assert(NodeTrait::getParent(To) == Parent);
0657     DomTreeBuilder::InsertEdge(*this, From, To);
0658   }
0659 
0660   /// Inform the dominator tree about a CFG edge deletion and update the tree.
0661   ///
0662   /// This function has to be called just after making the update on the actual
0663   /// CFG. An internal functions checks if the edge doesn't exist in the CFG in
0664   /// DEBUG mode. There cannot be any other updates that the
0665   /// dominator tree doesn't know about.
0666   ///
0667   /// Note that for postdominators it automatically takes care of deleting
0668   /// a reverse edge internally (so there's no need to swap the parameters).
0669   ///
0670   void deleteEdge(NodeT *From, NodeT *To) {
0671     assert(From);
0672     assert(To);
0673     assert(NodeTrait::getParent(From) == Parent);
0674     assert(NodeTrait::getParent(To) == Parent);
0675     DomTreeBuilder::DeleteEdge(*this, From, To);
0676   }
0677 
0678   /// Add a new node to the dominator tree information.
0679   ///
0680   /// This creates a new node as a child of DomBB dominator node, linking it
0681   /// into the children list of the immediate dominator.
0682   ///
0683   /// \param BB New node in CFG.
0684   /// \param DomBB CFG node that is dominator for BB.
0685   /// \returns New dominator tree node that represents new CFG node.
0686   ///
0687   DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
0688     assert(getNode(BB) == nullptr && "Block already in dominator tree!");
0689     DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
0690     assert(IDomNode && "Not immediate dominator specified for block!");
0691     DFSInfoValid = false;
0692     return createNode(BB, IDomNode);
0693   }
0694 
0695   /// Add a new node to the forward dominator tree and make it a new root.
0696   ///
0697   /// \param BB New node in CFG.
0698   /// \returns New dominator tree node that represents new CFG node.
0699   ///
0700   DomTreeNodeBase<NodeT> *setNewRoot(NodeT *BB) {
0701     assert(getNode(BB) == nullptr && "Block already in dominator tree!");
0702     assert(!this->isPostDominator() &&
0703            "Cannot change root of post-dominator tree");
0704     DFSInfoValid = false;
0705     DomTreeNodeBase<NodeT> *NewNode = createNode(BB);
0706     if (Roots.empty()) {
0707       addRoot(BB);
0708     } else {
0709       assert(Roots.size() == 1);
0710       NodeT *OldRoot = Roots.front();
0711       DomTreeNodeBase<NodeT> *OldNode = getNode(OldRoot);
0712       NewNode->addChild(OldNode);
0713       OldNode->IDom = NewNode;
0714       OldNode->UpdateLevel();
0715       Roots[0] = BB;
0716     }
0717     return RootNode = NewNode;
0718   }
0719 
0720   /// changeImmediateDominator - This method is used to update the dominator
0721   /// tree information when a node's immediate dominator changes.
0722   ///
0723   void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
0724                                 DomTreeNodeBase<NodeT> *NewIDom) {
0725     assert(N && NewIDom && "Cannot change null node pointers!");
0726     DFSInfoValid = false;
0727     N->setIDom(NewIDom);
0728   }
0729 
0730   void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
0731     changeImmediateDominator(getNode(BB), getNode(NewBB));
0732   }
0733 
0734   /// eraseNode - Removes a node from the dominator tree. Block must not
0735   /// dominate any other blocks. Removes node from its immediate dominator's
0736   /// children list. Deletes dominator node associated with basic block BB.
0737   void eraseNode(NodeT *BB) {
0738     std::optional<unsigned> IdxOpt = getNodeIndex(BB);
0739     assert(IdxOpt && DomTreeNodes[*IdxOpt] &&
0740            "Removing node that isn't in dominator tree.");
0741     DomTreeNodeBase<NodeT> *Node = DomTreeNodes[*IdxOpt].get();
0742     assert(Node->isLeaf() && "Node is not a leaf node.");
0743 
0744     DFSInfoValid = false;
0745 
0746     // Remove node from immediate dominator's children list.
0747     DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
0748     if (IDom) {
0749       const auto I = find(IDom->Children, Node);
0750       assert(I != IDom->Children.end() &&
0751              "Not in immediate dominator children set!");
0752       // I am no longer your child...
0753       std::swap(*I, IDom->Children.back());
0754       IDom->Children.pop_back();
0755     }
0756 
0757     DomTreeNodes[*IdxOpt] = nullptr;
0758     if constexpr (!GraphHasNodeNumbers<NodeT *>)
0759       NodeNumberMap.erase(BB);
0760 
0761     if (!IsPostDom) return;
0762 
0763     // Remember to update PostDominatorTree roots.
0764     auto RIt = llvm::find(Roots, BB);
0765     if (RIt != Roots.end()) {
0766       std::swap(*RIt, Roots.back());
0767       Roots.pop_back();
0768     }
0769   }
0770 
0771   /// splitBlock - BB is split and now it has one successor. Update dominator
0772   /// tree to reflect this change.
0773   void splitBlock(NodeT *NewBB) {
0774     if (IsPostDominator)
0775       Split<Inverse<NodeT *>>(NewBB);
0776     else
0777       Split<NodeT *>(NewBB);
0778   }
0779 
0780   /// print - Convert to human readable form
0781   ///
0782   void print(raw_ostream &O) const {
0783     O << "=============================--------------------------------\n";
0784     if (IsPostDominator)
0785       O << "Inorder PostDominator Tree: ";
0786     else
0787       O << "Inorder Dominator Tree: ";
0788     if (!DFSInfoValid)
0789       O << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
0790     O << "\n";
0791 
0792     // The postdom tree can have a null root if there are no returns.
0793     if (getRootNode()) PrintDomTree<NodeT>(getRootNode(), O, 1);
0794     O << "Roots: ";
0795     for (const NodePtr Block : Roots) {
0796       Block->printAsOperand(O, false);
0797       O << " ";
0798     }
0799     O << "\n";
0800   }
0801 
0802 public:
0803   /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
0804   /// dominator tree in dfs order.
0805   void updateDFSNumbers() const {
0806     if (DFSInfoValid) {
0807       SlowQueries = 0;
0808       return;
0809     }
0810 
0811     SmallVector<std::pair<const DomTreeNodeBase<NodeT> *,
0812                           typename DomTreeNodeBase<NodeT>::const_iterator>,
0813                 32> WorkStack;
0814 
0815     const DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
0816     assert((!Parent || ThisRoot) && "Empty constructed DomTree");
0817     if (!ThisRoot)
0818       return;
0819 
0820     // Both dominators and postdominators have a single root node. In the case
0821     // case of PostDominatorTree, this node is a virtual root.
0822     WorkStack.push_back({ThisRoot, ThisRoot->begin()});
0823 
0824     unsigned DFSNum = 0;
0825     ThisRoot->DFSNumIn = DFSNum++;
0826 
0827     while (!WorkStack.empty()) {
0828       const DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
0829       const auto ChildIt = WorkStack.back().second;
0830 
0831       // If we visited all of the children of this node, "recurse" back up the
0832       // stack setting the DFOutNum.
0833       if (ChildIt == Node->end()) {
0834         Node->DFSNumOut = DFSNum++;
0835         WorkStack.pop_back();
0836       } else {
0837         // Otherwise, recursively visit this child.
0838         const DomTreeNodeBase<NodeT> *Child = *ChildIt;
0839         ++WorkStack.back().second;
0840 
0841         WorkStack.push_back({Child, Child->begin()});
0842         Child->DFSNumIn = DFSNum++;
0843       }
0844     }
0845 
0846     SlowQueries = 0;
0847     DFSInfoValid = true;
0848   }
0849 
0850 private:
0851   void updateBlockNumberEpoch() {
0852     // Nothing to do for graphs that don't number their blocks.
0853     if constexpr (GraphHasNodeNumbers<NodeT *>)
0854       BlockNumberEpoch = GraphTraits<ParentPtr>::getNumberEpoch(Parent);
0855   }
0856 
0857 public:
0858   /// recalculate - compute a dominator tree for the given function
0859   void recalculate(ParentType &Func) {
0860     Parent = &Func;
0861     updateBlockNumberEpoch();
0862     DomTreeBuilder::Calculate(*this);
0863   }
0864 
0865   void recalculate(ParentType &Func, ArrayRef<UpdateType> Updates) {
0866     Parent = &Func;
0867     updateBlockNumberEpoch();
0868     DomTreeBuilder::CalculateWithUpdates(*this, Updates);
0869   }
0870 
0871   /// Update dominator tree after renumbering blocks.
0872   template <typename T = NodeT>
0873   std::enable_if_t<GraphHasNodeNumbers<T *>, void> updateBlockNumbers() {
0874     updateBlockNumberEpoch();
0875 
0876     unsigned MaxNumber = GraphTraits<ParentPtr>::getMaxNumber(Parent);
0877     DomTreeNodeStorageTy NewVector;
0878     NewVector.resize(MaxNumber + 1); // +1, because index 0 is for nullptr
0879     for (auto &Node : DomTreeNodes) {
0880       if (!Node)
0881         continue;
0882       unsigned Idx = *getNodeIndex(Node->getBlock());
0883       // getMaxNumber is not necessarily supported
0884       if (Idx >= NewVector.size())
0885         NewVector.resize(Idx + 1);
0886       NewVector[Idx] = std::move(Node);
0887     }
0888     DomTreeNodes = std::move(NewVector);
0889   }
0890 
0891   /// verify - checks if the tree is correct. There are 3 level of verification:
0892   ///  - Full --  verifies if the tree is correct by making sure all the
0893   ///             properties (including the parent and the sibling property)
0894   ///             hold.
0895   ///             Takes O(N^3) time.
0896   ///
0897   ///  - Basic -- checks if the tree is correct, but compares it to a freshly
0898   ///             constructed tree instead of checking the sibling property.
0899   ///             Takes O(N^2) time.
0900   ///
0901   ///  - Fast  -- checks basic tree structure and compares it with a freshly
0902   ///             constructed tree.
0903   ///             Takes O(N^2) time worst case, but is faster in practise (same
0904   ///             as tree construction).
0905   bool verify(VerificationLevel VL = VerificationLevel::Full) const {
0906     return DomTreeBuilder::Verify(*this, VL);
0907   }
0908 
0909   void reset() {
0910     DomTreeNodes.clear();
0911     if constexpr (!GraphHasNodeNumbers<NodeT *>)
0912       NodeNumberMap.clear();
0913     Roots.clear();
0914     RootNode = nullptr;
0915     Parent = nullptr;
0916     DFSInfoValid = false;
0917     SlowQueries = 0;
0918   }
0919 
0920 protected:
0921   void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
0922 
0923   DomTreeNodeBase<NodeT> *createNode(NodeT *BB,
0924                                      DomTreeNodeBase<NodeT> *IDom = nullptr) {
0925     auto Node = std::make_unique<DomTreeNodeBase<NodeT>>(BB, IDom);
0926     auto *NodePtr = Node.get();
0927     unsigned NodeIdx = getNodeIndexForInsert(BB);
0928     DomTreeNodes[NodeIdx] = std::move(Node);
0929     if (IDom)
0930       IDom->addChild(NodePtr);
0931     return NodePtr;
0932   }
0933 
0934   // NewBB is split and now it has one successor. Update dominator tree to
0935   // reflect this change.
0936   template <class N>
0937   void Split(typename GraphTraits<N>::NodeRef NewBB) {
0938     using GraphT = GraphTraits<N>;
0939     using NodeRef = typename GraphT::NodeRef;
0940     assert(llvm::hasSingleElement(children<N>(NewBB)) &&
0941            "NewBB should have a single successor!");
0942     NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
0943 
0944     SmallVector<NodeRef, 4> PredBlocks(inverse_children<N>(NewBB));
0945 
0946     assert(!PredBlocks.empty() && "No predblocks?");
0947 
0948     bool NewBBDominatesNewBBSucc = true;
0949     for (auto *Pred : inverse_children<N>(NewBBSucc)) {
0950       if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
0951           isReachableFromEntry(Pred)) {
0952         NewBBDominatesNewBBSucc = false;
0953         break;
0954       }
0955     }
0956 
0957     // Find NewBB's immediate dominator and create new dominator tree node for
0958     // NewBB.
0959     NodeT *NewBBIDom = nullptr;
0960     unsigned i = 0;
0961     for (i = 0; i < PredBlocks.size(); ++i)
0962       if (isReachableFromEntry(PredBlocks[i])) {
0963         NewBBIDom = PredBlocks[i];
0964         break;
0965       }
0966 
0967     // It's possible that none of the predecessors of NewBB are reachable;
0968     // in that case, NewBB itself is unreachable, so nothing needs to be
0969     // changed.
0970     if (!NewBBIDom) return;
0971 
0972     for (i = i + 1; i < PredBlocks.size(); ++i) {
0973       if (isReachableFromEntry(PredBlocks[i]))
0974         NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
0975     }
0976 
0977     // Create the new dominator tree node... and set the idom of NewBB.
0978     DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
0979 
0980     // If NewBB strictly dominates other blocks, then it is now the immediate
0981     // dominator of NewBBSucc.  Update the dominator tree as appropriate.
0982     if (NewBBDominatesNewBBSucc) {
0983       DomTreeNodeBase<NodeT> *NewBBSuccNode = getNode(NewBBSucc);
0984       changeImmediateDominator(NewBBSuccNode, NewBBNode);
0985     }
0986   }
0987 
0988  private:
0989   bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
0990                                const DomTreeNodeBase<NodeT> *B) const {
0991     assert(A != B);
0992     assert(isReachableFromEntry(B));
0993     assert(isReachableFromEntry(A));
0994 
0995     const unsigned ALevel = A->getLevel();
0996     const DomTreeNodeBase<NodeT> *IDom;
0997 
0998     // Don't walk nodes above A's subtree. When we reach A's level, we must
0999     // either find A or be in some other subtree not dominated by A.
1000     while ((IDom = B->getIDom()) != nullptr && IDom->getLevel() >= ALevel)
1001       B = IDom;  // Walk up the tree
1002 
1003     return B == A;
1004   }
1005 
1006   /// Wipe this tree's state without releasing any resources.
1007   ///
1008   /// This is essentially a post-move helper only. It leaves the object in an
1009   /// assignable and destroyable state, but otherwise invalid.
1010   void wipe() {
1011     DomTreeNodes.clear();
1012     if constexpr (!GraphHasNodeNumbers<NodeT *>)
1013       NodeNumberMap.clear();
1014     RootNode = nullptr;
1015     Parent = nullptr;
1016   }
1017 };
1018 
1019 template <typename T>
1020 using DomTreeBase = DominatorTreeBase<T, false>;
1021 
1022 template <typename T>
1023 using PostDomTreeBase = DominatorTreeBase<T, true>;
1024 
1025 // These two functions are declared out of line as a workaround for building
1026 // with old (< r147295) versions of clang because of pr11642.
1027 template <typename NodeT, bool IsPostDom>
1028 bool DominatorTreeBase<NodeT, IsPostDom>::dominates(const NodeT *A,
1029                                                     const NodeT *B) const {
1030   if (A == B)
1031     return true;
1032 
1033   return dominates(getNode(A), getNode(B));
1034 }
1035 template <typename NodeT, bool IsPostDom>
1036 bool DominatorTreeBase<NodeT, IsPostDom>::properlyDominates(
1037     const NodeT *A, const NodeT *B) const {
1038   if (A == B)
1039     return false;
1040 
1041   return dominates(getNode(A), getNode(B));
1042 }
1043 
1044 } // end namespace llvm
1045 
1046 #endif // LLVM_SUPPORT_GENERICDOMTREE_H