GenericDomTree.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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. /// NodeRef->getParent() must return the parent node that is also a pointer.
  18. ///
  19. /// FIXME: Maybe GenericDomTree needs a TreeTraits, instead of GraphTraits.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_SUPPORT_GENERICDOMTREE_H
  23. #define LLVM_SUPPORT_GENERICDOMTREE_H
  24. #include "llvm/ADT/DenseMap.h"
  25. #include "llvm/ADT/GraphTraits.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SmallPtrSet.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/Support/CFGDiff.h"
  30. #include "llvm/Support/CFGUpdate.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <cstddef>
  35. #include <iterator>
  36. #include <memory>
  37. #include <type_traits>
  38. #include <utility>
  39. namespace llvm {
  40. template <typename NodeT, bool IsPostDom>
  41. class DominatorTreeBase;
  42. namespace DomTreeBuilder {
  43. template <typename DomTreeT>
  44. struct SemiNCAInfo;
  45. } // namespace DomTreeBuilder
  46. /// Base class for the actual dominator tree node.
  47. template <class NodeT> class DomTreeNodeBase {
  48. friend class PostDominatorTree;
  49. friend class DominatorTreeBase<NodeT, false>;
  50. friend class DominatorTreeBase<NodeT, true>;
  51. friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase<NodeT, false>>;
  52. friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase<NodeT, true>>;
  53. NodeT *TheBB;
  54. DomTreeNodeBase *IDom;
  55. unsigned Level;
  56. SmallVector<DomTreeNodeBase *, 4> Children;
  57. mutable unsigned DFSNumIn = ~0;
  58. mutable unsigned DFSNumOut = ~0;
  59. public:
  60. DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
  61. : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
  62. using iterator = typename SmallVector<DomTreeNodeBase *, 4>::iterator;
  63. using const_iterator =
  64. typename SmallVector<DomTreeNodeBase *, 4>::const_iterator;
  65. iterator begin() { return Children.begin(); }
  66. iterator end() { return Children.end(); }
  67. const_iterator begin() const { return Children.begin(); }
  68. const_iterator end() const { return Children.end(); }
  69. DomTreeNodeBase *const &back() const { return Children.back(); }
  70. DomTreeNodeBase *&back() { return Children.back(); }
  71. iterator_range<iterator> children() { return make_range(begin(), end()); }
  72. iterator_range<const_iterator> children() const {
  73. return make_range(begin(), end());
  74. }
  75. NodeT *getBlock() const { return TheBB; }
  76. DomTreeNodeBase *getIDom() const { return IDom; }
  77. unsigned getLevel() const { return Level; }
  78. std::unique_ptr<DomTreeNodeBase> addChild(
  79. std::unique_ptr<DomTreeNodeBase> C) {
  80. Children.push_back(C.get());
  81. return C;
  82. }
  83. bool isLeaf() const { return Children.empty(); }
  84. size_t getNumChildren() const { return Children.size(); }
  85. void clearAllChildren() { Children.clear(); }
  86. bool compare(const DomTreeNodeBase *Other) const {
  87. if (getNumChildren() != Other->getNumChildren())
  88. return true;
  89. if (Level != Other->Level) return true;
  90. SmallPtrSet<const NodeT *, 4> OtherChildren;
  91. for (const DomTreeNodeBase *I : *Other) {
  92. const NodeT *Nd = I->getBlock();
  93. OtherChildren.insert(Nd);
  94. }
  95. for (const DomTreeNodeBase *I : *this) {
  96. const NodeT *N = I->getBlock();
  97. if (OtherChildren.count(N) == 0)
  98. return true;
  99. }
  100. return false;
  101. }
  102. void setIDom(DomTreeNodeBase *NewIDom) {
  103. assert(IDom && "No immediate dominator?");
  104. if (IDom == NewIDom) return;
  105. auto I = find(IDom->Children, this);
  106. assert(I != IDom->Children.end() &&
  107. "Not in immediate dominator children set!");
  108. // I am no longer your child...
  109. IDom->Children.erase(I);
  110. // Switch to new dominator
  111. IDom = NewIDom;
  112. IDom->Children.push_back(this);
  113. UpdateLevel();
  114. }
  115. /// getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes
  116. /// in the dominator tree. They are only guaranteed valid if
  117. /// updateDFSNumbers() has been called.
  118. unsigned getDFSNumIn() const { return DFSNumIn; }
  119. unsigned getDFSNumOut() const { return DFSNumOut; }
  120. private:
  121. // Return true if this node is dominated by other. Use this only if DFS info
  122. // is valid.
  123. bool DominatedBy(const DomTreeNodeBase *other) const {
  124. return this->DFSNumIn >= other->DFSNumIn &&
  125. this->DFSNumOut <= other->DFSNumOut;
  126. }
  127. void UpdateLevel() {
  128. assert(IDom);
  129. if (Level == IDom->Level + 1) return;
  130. SmallVector<DomTreeNodeBase *, 64> WorkStack = {this};
  131. while (!WorkStack.empty()) {
  132. DomTreeNodeBase *Current = WorkStack.pop_back_val();
  133. Current->Level = Current->IDom->Level + 1;
  134. for (DomTreeNodeBase *C : *Current) {
  135. assert(C->IDom);
  136. if (C->Level != C->IDom->Level + 1) WorkStack.push_back(C);
  137. }
  138. }
  139. }
  140. };
  141. template <class NodeT>
  142. raw_ostream &operator<<(raw_ostream &O, const DomTreeNodeBase<NodeT> *Node) {
  143. if (Node->getBlock())
  144. Node->getBlock()->printAsOperand(O, false);
  145. else
  146. O << " <<exit node>>";
  147. O << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "} ["
  148. << Node->getLevel() << "]\n";
  149. return O;
  150. }
  151. template <class NodeT>
  152. void PrintDomTree(const DomTreeNodeBase<NodeT> *N, raw_ostream &O,
  153. unsigned Lev) {
  154. O.indent(2 * Lev) << "[" << Lev << "] " << N;
  155. for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(),
  156. E = N->end();
  157. I != E; ++I)
  158. PrintDomTree<NodeT>(*I, O, Lev + 1);
  159. }
  160. namespace DomTreeBuilder {
  161. // The routines below are provided in a separate header but referenced here.
  162. template <typename DomTreeT>
  163. void Calculate(DomTreeT &DT);
  164. template <typename DomTreeT>
  165. void CalculateWithUpdates(DomTreeT &DT,
  166. ArrayRef<typename DomTreeT::UpdateType> Updates);
  167. template <typename DomTreeT>
  168. void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
  169. typename DomTreeT::NodePtr To);
  170. template <typename DomTreeT>
  171. void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From,
  172. typename DomTreeT::NodePtr To);
  173. template <typename DomTreeT>
  174. void ApplyUpdates(DomTreeT &DT,
  175. GraphDiff<typename DomTreeT::NodePtr,
  176. DomTreeT::IsPostDominator> &PreViewCFG,
  177. GraphDiff<typename DomTreeT::NodePtr,
  178. DomTreeT::IsPostDominator> *PostViewCFG);
  179. template <typename DomTreeT>
  180. bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL);
  181. } // namespace DomTreeBuilder
  182. /// Core dominator tree base class.
  183. ///
  184. /// This class is a generic template over graph nodes. It is instantiated for
  185. /// various graphs in the LLVM IR or in the code generator.
  186. template <typename NodeT, bool IsPostDom>
  187. class DominatorTreeBase {
  188. public:
  189. static_assert(std::is_pointer<typename GraphTraits<NodeT *>::NodeRef>::value,
  190. "Currently DominatorTreeBase supports only pointer nodes");
  191. using NodeType = NodeT;
  192. using NodePtr = NodeT *;
  193. using ParentPtr = decltype(std::declval<NodeT *>()->getParent());
  194. static_assert(std::is_pointer<ParentPtr>::value,
  195. "Currently NodeT's parent must be a pointer type");
  196. using ParentType = std::remove_pointer_t<ParentPtr>;
  197. static constexpr bool IsPostDominator = IsPostDom;
  198. using UpdateType = cfg::Update<NodePtr>;
  199. using UpdateKind = cfg::UpdateKind;
  200. static constexpr UpdateKind Insert = UpdateKind::Insert;
  201. static constexpr UpdateKind Delete = UpdateKind::Delete;
  202. enum class VerificationLevel { Fast, Basic, Full };
  203. protected:
  204. // Dominators always have a single root, postdominators can have more.
  205. SmallVector<NodeT *, IsPostDom ? 4 : 1> Roots;
  206. using DomTreeNodeMapType =
  207. DenseMap<NodeT *, std::unique_ptr<DomTreeNodeBase<NodeT>>>;
  208. DomTreeNodeMapType DomTreeNodes;
  209. DomTreeNodeBase<NodeT> *RootNode = nullptr;
  210. ParentPtr Parent = nullptr;
  211. mutable bool DFSInfoValid = false;
  212. mutable unsigned int SlowQueries = 0;
  213. friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase>;
  214. public:
  215. DominatorTreeBase() {}
  216. DominatorTreeBase(DominatorTreeBase &&Arg)
  217. : Roots(std::move(Arg.Roots)),
  218. DomTreeNodes(std::move(Arg.DomTreeNodes)),
  219. RootNode(Arg.RootNode),
  220. Parent(Arg.Parent),
  221. DFSInfoValid(Arg.DFSInfoValid),
  222. SlowQueries(Arg.SlowQueries) {
  223. Arg.wipe();
  224. }
  225. DominatorTreeBase &operator=(DominatorTreeBase &&RHS) {
  226. Roots = std::move(RHS.Roots);
  227. DomTreeNodes = std::move(RHS.DomTreeNodes);
  228. RootNode = RHS.RootNode;
  229. Parent = RHS.Parent;
  230. DFSInfoValid = RHS.DFSInfoValid;
  231. SlowQueries = RHS.SlowQueries;
  232. RHS.wipe();
  233. return *this;
  234. }
  235. DominatorTreeBase(const DominatorTreeBase &) = delete;
  236. DominatorTreeBase &operator=(const DominatorTreeBase &) = delete;
  237. /// Iteration over roots.
  238. ///
  239. /// This may include multiple blocks if we are computing post dominators.
  240. /// For forward dominators, this will always be a single block (the entry
  241. /// block).
  242. using root_iterator = typename SmallVectorImpl<NodeT *>::iterator;
  243. using const_root_iterator = typename SmallVectorImpl<NodeT *>::const_iterator;
  244. root_iterator root_begin() { return Roots.begin(); }
  245. const_root_iterator root_begin() const { return Roots.begin(); }
  246. root_iterator root_end() { return Roots.end(); }
  247. const_root_iterator root_end() const { return Roots.end(); }
  248. size_t root_size() const { return Roots.size(); }
  249. iterator_range<root_iterator> roots() {
  250. return make_range(root_begin(), root_end());
  251. }
  252. iterator_range<const_root_iterator> roots() const {
  253. return make_range(root_begin(), root_end());
  254. }
  255. /// isPostDominator - Returns true if analysis based of postdoms
  256. ///
  257. bool isPostDominator() const { return IsPostDominator; }
  258. /// compare - Return false if the other dominator tree base matches this
  259. /// dominator tree base. Otherwise return true.
  260. bool compare(const DominatorTreeBase &Other) const {
  261. if (Parent != Other.Parent) return true;
  262. if (Roots.size() != Other.Roots.size())
  263. return true;
  264. if (!std::is_permutation(Roots.begin(), Roots.end(), Other.Roots.begin()))
  265. return true;
  266. const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes;
  267. if (DomTreeNodes.size() != OtherDomTreeNodes.size())
  268. return true;
  269. for (const auto &DomTreeNode : DomTreeNodes) {
  270. NodeT *BB = DomTreeNode.first;
  271. typename DomTreeNodeMapType::const_iterator OI =
  272. OtherDomTreeNodes.find(BB);
  273. if (OI == OtherDomTreeNodes.end())
  274. return true;
  275. DomTreeNodeBase<NodeT> &MyNd = *DomTreeNode.second;
  276. DomTreeNodeBase<NodeT> &OtherNd = *OI->second;
  277. if (MyNd.compare(&OtherNd))
  278. return true;
  279. }
  280. return false;
  281. }
  282. /// getNode - return the (Post)DominatorTree node for the specified basic
  283. /// block. This is the same as using operator[] on this class. The result
  284. /// may (but is not required to) be null for a forward (backwards)
  285. /// statically unreachable block.
  286. DomTreeNodeBase<NodeT> *getNode(const NodeT *BB) const {
  287. auto I = DomTreeNodes.find(BB);
  288. if (I != DomTreeNodes.end())
  289. return I->second.get();
  290. return nullptr;
  291. }
  292. /// See getNode.
  293. DomTreeNodeBase<NodeT> *operator[](const NodeT *BB) const {
  294. return getNode(BB);
  295. }
  296. /// getRootNode - This returns the entry node for the CFG of the function. If
  297. /// this tree represents the post-dominance relations for a function, however,
  298. /// this root may be a node with the block == NULL. This is the case when
  299. /// there are multiple exit nodes from a particular function. Consumers of
  300. /// post-dominance information must be capable of dealing with this
  301. /// possibility.
  302. ///
  303. DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; }
  304. const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; }
  305. /// Get all nodes dominated by R, including R itself.
  306. void getDescendants(NodeT *R, SmallVectorImpl<NodeT *> &Result) const {
  307. Result.clear();
  308. const DomTreeNodeBase<NodeT> *RN = getNode(R);
  309. if (!RN)
  310. return; // If R is unreachable, it will not be present in the DOM tree.
  311. SmallVector<const DomTreeNodeBase<NodeT> *, 8> WL;
  312. WL.push_back(RN);
  313. while (!WL.empty()) {
  314. const DomTreeNodeBase<NodeT> *N = WL.pop_back_val();
  315. Result.push_back(N->getBlock());
  316. WL.append(N->begin(), N->end());
  317. }
  318. }
  319. /// properlyDominates - Returns true iff A dominates B and A != B.
  320. /// Note that this is not a constant time operation!
  321. ///
  322. bool properlyDominates(const DomTreeNodeBase<NodeT> *A,
  323. const DomTreeNodeBase<NodeT> *B) const {
  324. if (!A || !B)
  325. return false;
  326. if (A == B)
  327. return false;
  328. return dominates(A, B);
  329. }
  330. bool properlyDominates(const NodeT *A, const NodeT *B) const;
  331. /// isReachableFromEntry - Return true if A is dominated by the entry
  332. /// block of the function containing it.
  333. bool isReachableFromEntry(const NodeT *A) const {
  334. assert(!this->isPostDominator() &&
  335. "This is not implemented for post dominators");
  336. return isReachableFromEntry(getNode(const_cast<NodeT *>(A)));
  337. }
  338. bool isReachableFromEntry(const DomTreeNodeBase<NodeT> *A) const { return A; }
  339. /// dominates - Returns true iff A dominates B. Note that this is not a
  340. /// constant time operation!
  341. ///
  342. bool dominates(const DomTreeNodeBase<NodeT> *A,
  343. const DomTreeNodeBase<NodeT> *B) const {
  344. // A node trivially dominates itself.
  345. if (B == A)
  346. return true;
  347. // An unreachable node is dominated by anything.
  348. if (!isReachableFromEntry(B))
  349. return true;
  350. // And dominates nothing.
  351. if (!isReachableFromEntry(A))
  352. return false;
  353. if (B->getIDom() == A) return true;
  354. if (A->getIDom() == B) return false;
  355. // A can only dominate B if it is higher in the tree.
  356. if (A->getLevel() >= B->getLevel()) return false;
  357. // Compare the result of the tree walk and the dfs numbers, if expensive
  358. // checks are enabled.
  359. #ifdef EXPENSIVE_CHECKS
  360. assert((!DFSInfoValid ||
  361. (dominatedBySlowTreeWalk(A, B) == B->DominatedBy(A))) &&
  362. "Tree walk disagrees with dfs numbers!");
  363. #endif
  364. if (DFSInfoValid)
  365. return B->DominatedBy(A);
  366. // If we end up with too many slow queries, just update the
  367. // DFS numbers on the theory that we are going to keep querying.
  368. SlowQueries++;
  369. if (SlowQueries > 32) {
  370. updateDFSNumbers();
  371. return B->DominatedBy(A);
  372. }
  373. return dominatedBySlowTreeWalk(A, B);
  374. }
  375. bool dominates(const NodeT *A, const NodeT *B) const;
  376. NodeT *getRoot() const {
  377. assert(this->Roots.size() == 1 && "Should always have entry node!");
  378. return this->Roots[0];
  379. }
  380. /// Find nearest common dominator basic block for basic block A and B. A and B
  381. /// must have tree nodes.
  382. NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) const {
  383. assert(A && B && "Pointers are not valid");
  384. assert(A->getParent() == B->getParent() &&
  385. "Two blocks are not in same function");
  386. // If either A or B is a entry block then it is nearest common dominator
  387. // (for forward-dominators).
  388. if (!isPostDominator()) {
  389. NodeT &Entry = A->getParent()->front();
  390. if (A == &Entry || B == &Entry)
  391. return &Entry;
  392. }
  393. DomTreeNodeBase<NodeT> *NodeA = getNode(A);
  394. DomTreeNodeBase<NodeT> *NodeB = getNode(B);
  395. assert(NodeA && "A must be in the tree");
  396. assert(NodeB && "B must be in the tree");
  397. // Use level information to go up the tree until the levels match. Then
  398. // continue going up til we arrive at the same node.
  399. while (NodeA != NodeB) {
  400. if (NodeA->getLevel() < NodeB->getLevel()) std::swap(NodeA, NodeB);
  401. NodeA = NodeA->IDom;
  402. }
  403. return NodeA->getBlock();
  404. }
  405. const NodeT *findNearestCommonDominator(const NodeT *A,
  406. const NodeT *B) const {
  407. // Cast away the const qualifiers here. This is ok since
  408. // const is re-introduced on the return type.
  409. return findNearestCommonDominator(const_cast<NodeT *>(A),
  410. const_cast<NodeT *>(B));
  411. }
  412. bool isVirtualRoot(const DomTreeNodeBase<NodeT> *A) const {
  413. return isPostDominator() && !A->getBlock();
  414. }
  415. //===--------------------------------------------------------------------===//
  416. // API to update (Post)DominatorTree information based on modifications to
  417. // the CFG...
  418. /// Inform the dominator tree about a sequence of CFG edge insertions and
  419. /// deletions and perform a batch update on the tree.
  420. ///
  421. /// This function should be used when there were multiple CFG updates after
  422. /// the last dominator tree update. It takes care of performing the updates
  423. /// in sync with the CFG and optimizes away the redundant operations that
  424. /// cancel each other.
  425. /// The functions expects the sequence of updates to be balanced. Eg.:
  426. /// - {{Insert, A, B}, {Delete, A, B}, {Insert, A, B}} is fine, because
  427. /// logically it results in a single insertions.
  428. /// - {{Insert, A, B}, {Insert, A, B}} is invalid, because it doesn't make
  429. /// sense to insert the same edge twice.
  430. ///
  431. /// What's more, the functions assumes that it's safe to ask every node in the
  432. /// CFG about its children and inverse children. This implies that deletions
  433. /// of CFG edges must not delete the CFG nodes before calling this function.
  434. ///
  435. /// The applyUpdates function can reorder the updates and remove redundant
  436. /// ones internally. The batch updater is also able to detect sequences of
  437. /// zero and exactly one update -- it's optimized to do less work in these
  438. /// cases.
  439. ///
  440. /// Note that for postdominators it automatically takes care of applying
  441. /// updates on reverse edges internally (so there's no need to swap the
  442. /// From and To pointers when constructing DominatorTree::UpdateType).
  443. /// The type of updates is the same for DomTreeBase<T> and PostDomTreeBase<T>
  444. /// with the same template parameter T.
  445. ///
  446. /// \param Updates An unordered sequence of updates to perform. The current
  447. /// CFG and the reverse of these updates provides the pre-view of the CFG.
  448. ///
  449. void applyUpdates(ArrayRef<UpdateType> Updates) {
  450. GraphDiff<NodePtr, IsPostDominator> PreViewCFG(
  451. Updates, /*ReverseApplyUpdates=*/true);
  452. DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, nullptr);
  453. }
  454. /// \param Updates An unordered sequence of updates to perform. The current
  455. /// CFG and the reverse of these updates provides the pre-view of the CFG.
  456. /// \param PostViewUpdates An unordered sequence of update to perform in order
  457. /// to obtain a post-view of the CFG. The DT will be updated assuming the
  458. /// obtained PostViewCFG is the desired end state.
  459. void applyUpdates(ArrayRef<UpdateType> Updates,
  460. ArrayRef<UpdateType> PostViewUpdates) {
  461. if (Updates.empty()) {
  462. GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
  463. DomTreeBuilder::ApplyUpdates(*this, PostViewCFG, &PostViewCFG);
  464. } else {
  465. // PreViewCFG needs to merge Updates and PostViewCFG. The updates in
  466. // Updates need to be reversed, and match the direction in PostViewCFG.
  467. // The PostViewCFG is created with updates reversed (equivalent to changes
  468. // made to the CFG), so the PreViewCFG needs all the updates reverse
  469. // applied.
  470. SmallVector<UpdateType> AllUpdates(Updates.begin(), Updates.end());
  471. append_range(AllUpdates, PostViewUpdates);
  472. GraphDiff<NodePtr, IsPostDom> PreViewCFG(AllUpdates,
  473. /*ReverseApplyUpdates=*/true);
  474. GraphDiff<NodePtr, IsPostDom> PostViewCFG(PostViewUpdates);
  475. DomTreeBuilder::ApplyUpdates(*this, PreViewCFG, &PostViewCFG);
  476. }
  477. }
  478. /// Inform the dominator tree about a CFG edge insertion and update the tree.
  479. ///
  480. /// This function has to be called just before or just after making the update
  481. /// on the actual CFG. There cannot be any other updates that the dominator
  482. /// tree doesn't know about.
  483. ///
  484. /// Note that for postdominators it automatically takes care of inserting
  485. /// a reverse edge internally (so there's no need to swap the parameters).
  486. ///
  487. void insertEdge(NodeT *From, NodeT *To) {
  488. assert(From);
  489. assert(To);
  490. assert(From->getParent() == Parent);
  491. assert(To->getParent() == Parent);
  492. DomTreeBuilder::InsertEdge(*this, From, To);
  493. }
  494. /// Inform the dominator tree about a CFG edge deletion and update the tree.
  495. ///
  496. /// This function has to be called just after making the update on the actual
  497. /// CFG. An internal functions checks if the edge doesn't exist in the CFG in
  498. /// DEBUG mode. There cannot be any other updates that the
  499. /// dominator tree doesn't know about.
  500. ///
  501. /// Note that for postdominators it automatically takes care of deleting
  502. /// a reverse edge internally (so there's no need to swap the parameters).
  503. ///
  504. void deleteEdge(NodeT *From, NodeT *To) {
  505. assert(From);
  506. assert(To);
  507. assert(From->getParent() == Parent);
  508. assert(To->getParent() == Parent);
  509. DomTreeBuilder::DeleteEdge(*this, From, To);
  510. }
  511. /// Add a new node to the dominator tree information.
  512. ///
  513. /// This creates a new node as a child of DomBB dominator node, linking it
  514. /// into the children list of the immediate dominator.
  515. ///
  516. /// \param BB New node in CFG.
  517. /// \param DomBB CFG node that is dominator for BB.
  518. /// \returns New dominator tree node that represents new CFG node.
  519. ///
  520. DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) {
  521. assert(getNode(BB) == nullptr && "Block already in dominator tree!");
  522. DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB);
  523. assert(IDomNode && "Not immediate dominator specified for block!");
  524. DFSInfoValid = false;
  525. return createChild(BB, IDomNode);
  526. }
  527. /// Add a new node to the forward dominator tree and make it a new root.
  528. ///
  529. /// \param BB New node in CFG.
  530. /// \returns New dominator tree node that represents new CFG node.
  531. ///
  532. DomTreeNodeBase<NodeT> *setNewRoot(NodeT *BB) {
  533. assert(getNode(BB) == nullptr && "Block already in dominator tree!");
  534. assert(!this->isPostDominator() &&
  535. "Cannot change root of post-dominator tree");
  536. DFSInfoValid = false;
  537. DomTreeNodeBase<NodeT> *NewNode = createNode(BB);
  538. if (Roots.empty()) {
  539. addRoot(BB);
  540. } else {
  541. assert(Roots.size() == 1);
  542. NodeT *OldRoot = Roots.front();
  543. auto &OldNode = DomTreeNodes[OldRoot];
  544. OldNode = NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
  545. OldNode->IDom = NewNode;
  546. OldNode->UpdateLevel();
  547. Roots[0] = BB;
  548. }
  549. return RootNode = NewNode;
  550. }
  551. /// changeImmediateDominator - This method is used to update the dominator
  552. /// tree information when a node's immediate dominator changes.
  553. ///
  554. void changeImmediateDominator(DomTreeNodeBase<NodeT> *N,
  555. DomTreeNodeBase<NodeT> *NewIDom) {
  556. assert(N && NewIDom && "Cannot change null node pointers!");
  557. DFSInfoValid = false;
  558. N->setIDom(NewIDom);
  559. }
  560. void changeImmediateDominator(NodeT *BB, NodeT *NewBB) {
  561. changeImmediateDominator(getNode(BB), getNode(NewBB));
  562. }
  563. /// eraseNode - Removes a node from the dominator tree. Block must not
  564. /// dominate any other blocks. Removes node from its immediate dominator's
  565. /// children list. Deletes dominator node associated with basic block BB.
  566. void eraseNode(NodeT *BB) {
  567. DomTreeNodeBase<NodeT> *Node = getNode(BB);
  568. assert(Node && "Removing node that isn't in dominator tree.");
  569. assert(Node->isLeaf() && "Node is not a leaf node.");
  570. DFSInfoValid = false;
  571. // Remove node from immediate dominator's children list.
  572. DomTreeNodeBase<NodeT> *IDom = Node->getIDom();
  573. if (IDom) {
  574. const auto I = find(IDom->Children, Node);
  575. assert(I != IDom->Children.end() &&
  576. "Not in immediate dominator children set!");
  577. // I am no longer your child...
  578. IDom->Children.erase(I);
  579. }
  580. DomTreeNodes.erase(BB);
  581. if (!IsPostDom) return;
  582. // Remember to update PostDominatorTree roots.
  583. auto RIt = llvm::find(Roots, BB);
  584. if (RIt != Roots.end()) {
  585. std::swap(*RIt, Roots.back());
  586. Roots.pop_back();
  587. }
  588. }
  589. /// splitBlock - BB is split and now it has one successor. Update dominator
  590. /// tree to reflect this change.
  591. void splitBlock(NodeT *NewBB) {
  592. if (IsPostDominator)
  593. Split<Inverse<NodeT *>>(NewBB);
  594. else
  595. Split<NodeT *>(NewBB);
  596. }
  597. /// print - Convert to human readable form
  598. ///
  599. void print(raw_ostream &O) const {
  600. O << "=============================--------------------------------\n";
  601. if (IsPostDominator)
  602. O << "Inorder PostDominator Tree: ";
  603. else
  604. O << "Inorder Dominator Tree: ";
  605. if (!DFSInfoValid)
  606. O << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
  607. O << "\n";
  608. // The postdom tree can have a null root if there are no returns.
  609. if (getRootNode()) PrintDomTree<NodeT>(getRootNode(), O, 1);
  610. O << "Roots: ";
  611. for (const NodePtr Block : Roots) {
  612. Block->printAsOperand(O, false);
  613. O << " ";
  614. }
  615. O << "\n";
  616. }
  617. public:
  618. /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
  619. /// dominator tree in dfs order.
  620. void updateDFSNumbers() const {
  621. if (DFSInfoValid) {
  622. SlowQueries = 0;
  623. return;
  624. }
  625. SmallVector<std::pair<const DomTreeNodeBase<NodeT> *,
  626. typename DomTreeNodeBase<NodeT>::const_iterator>,
  627. 32> WorkStack;
  628. const DomTreeNodeBase<NodeT> *ThisRoot = getRootNode();
  629. assert((!Parent || ThisRoot) && "Empty constructed DomTree");
  630. if (!ThisRoot)
  631. return;
  632. // Both dominators and postdominators have a single root node. In the case
  633. // case of PostDominatorTree, this node is a virtual root.
  634. WorkStack.push_back({ThisRoot, ThisRoot->begin()});
  635. unsigned DFSNum = 0;
  636. ThisRoot->DFSNumIn = DFSNum++;
  637. while (!WorkStack.empty()) {
  638. const DomTreeNodeBase<NodeT> *Node = WorkStack.back().first;
  639. const auto ChildIt = WorkStack.back().second;
  640. // If we visited all of the children of this node, "recurse" back up the
  641. // stack setting the DFOutNum.
  642. if (ChildIt == Node->end()) {
  643. Node->DFSNumOut = DFSNum++;
  644. WorkStack.pop_back();
  645. } else {
  646. // Otherwise, recursively visit this child.
  647. const DomTreeNodeBase<NodeT> *Child = *ChildIt;
  648. ++WorkStack.back().second;
  649. WorkStack.push_back({Child, Child->begin()});
  650. Child->DFSNumIn = DFSNum++;
  651. }
  652. }
  653. SlowQueries = 0;
  654. DFSInfoValid = true;
  655. }
  656. /// recalculate - compute a dominator tree for the given function
  657. void recalculate(ParentType &Func) {
  658. Parent = &Func;
  659. DomTreeBuilder::Calculate(*this);
  660. }
  661. void recalculate(ParentType &Func, ArrayRef<UpdateType> Updates) {
  662. Parent = &Func;
  663. DomTreeBuilder::CalculateWithUpdates(*this, Updates);
  664. }
  665. /// verify - checks if the tree is correct. There are 3 level of verification:
  666. /// - Full -- verifies if the tree is correct by making sure all the
  667. /// properties (including the parent and the sibling property)
  668. /// hold.
  669. /// Takes O(N^3) time.
  670. ///
  671. /// - Basic -- checks if the tree is correct, but compares it to a freshly
  672. /// constructed tree instead of checking the sibling property.
  673. /// Takes O(N^2) time.
  674. ///
  675. /// - Fast -- checks basic tree structure and compares it with a freshly
  676. /// constructed tree.
  677. /// Takes O(N^2) time worst case, but is faster in practise (same
  678. /// as tree construction).
  679. bool verify(VerificationLevel VL = VerificationLevel::Full) const {
  680. return DomTreeBuilder::Verify(*this, VL);
  681. }
  682. void reset() {
  683. DomTreeNodes.clear();
  684. Roots.clear();
  685. RootNode = nullptr;
  686. Parent = nullptr;
  687. DFSInfoValid = false;
  688. SlowQueries = 0;
  689. }
  690. protected:
  691. void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
  692. DomTreeNodeBase<NodeT> *createChild(NodeT *BB, DomTreeNodeBase<NodeT> *IDom) {
  693. return (DomTreeNodes[BB] = IDom->addChild(
  694. std::make_unique<DomTreeNodeBase<NodeT>>(BB, IDom)))
  695. .get();
  696. }
  697. DomTreeNodeBase<NodeT> *createNode(NodeT *BB) {
  698. return (DomTreeNodes[BB] =
  699. std::make_unique<DomTreeNodeBase<NodeT>>(BB, nullptr))
  700. .get();
  701. }
  702. // NewBB is split and now it has one successor. Update dominator tree to
  703. // reflect this change.
  704. template <class N>
  705. void Split(typename GraphTraits<N>::NodeRef NewBB) {
  706. using GraphT = GraphTraits<N>;
  707. using NodeRef = typename GraphT::NodeRef;
  708. assert(std::distance(GraphT::child_begin(NewBB),
  709. GraphT::child_end(NewBB)) == 1 &&
  710. "NewBB should have a single successor!");
  711. NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
  712. SmallVector<NodeRef, 4> PredBlocks(children<Inverse<N>>(NewBB));
  713. assert(!PredBlocks.empty() && "No predblocks?");
  714. bool NewBBDominatesNewBBSucc = true;
  715. for (auto Pred : children<Inverse<N>>(NewBBSucc)) {
  716. if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
  717. isReachableFromEntry(Pred)) {
  718. NewBBDominatesNewBBSucc = false;
  719. break;
  720. }
  721. }
  722. // Find NewBB's immediate dominator and create new dominator tree node for
  723. // NewBB.
  724. NodeT *NewBBIDom = nullptr;
  725. unsigned i = 0;
  726. for (i = 0; i < PredBlocks.size(); ++i)
  727. if (isReachableFromEntry(PredBlocks[i])) {
  728. NewBBIDom = PredBlocks[i];
  729. break;
  730. }
  731. // It's possible that none of the predecessors of NewBB are reachable;
  732. // in that case, NewBB itself is unreachable, so nothing needs to be
  733. // changed.
  734. if (!NewBBIDom) return;
  735. for (i = i + 1; i < PredBlocks.size(); ++i) {
  736. if (isReachableFromEntry(PredBlocks[i]))
  737. NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
  738. }
  739. // Create the new dominator tree node... and set the idom of NewBB.
  740. DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
  741. // If NewBB strictly dominates other blocks, then it is now the immediate
  742. // dominator of NewBBSucc. Update the dominator tree as appropriate.
  743. if (NewBBDominatesNewBBSucc) {
  744. DomTreeNodeBase<NodeT> *NewBBSuccNode = getNode(NewBBSucc);
  745. changeImmediateDominator(NewBBSuccNode, NewBBNode);
  746. }
  747. }
  748. private:
  749. bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
  750. const DomTreeNodeBase<NodeT> *B) const {
  751. assert(A != B);
  752. assert(isReachableFromEntry(B));
  753. assert(isReachableFromEntry(A));
  754. const unsigned ALevel = A->getLevel();
  755. const DomTreeNodeBase<NodeT> *IDom;
  756. // Don't walk nodes above A's subtree. When we reach A's level, we must
  757. // either find A or be in some other subtree not dominated by A.
  758. while ((IDom = B->getIDom()) != nullptr && IDom->getLevel() >= ALevel)
  759. B = IDom; // Walk up the tree
  760. return B == A;
  761. }
  762. /// Wipe this tree's state without releasing any resources.
  763. ///
  764. /// This is essentially a post-move helper only. It leaves the object in an
  765. /// assignable and destroyable state, but otherwise invalid.
  766. void wipe() {
  767. DomTreeNodes.clear();
  768. RootNode = nullptr;
  769. Parent = nullptr;
  770. }
  771. };
  772. template <typename T>
  773. using DomTreeBase = DominatorTreeBase<T, false>;
  774. template <typename T>
  775. using PostDomTreeBase = DominatorTreeBase<T, true>;
  776. // These two functions are declared out of line as a workaround for building
  777. // with old (< r147295) versions of clang because of pr11642.
  778. template <typename NodeT, bool IsPostDom>
  779. bool DominatorTreeBase<NodeT, IsPostDom>::dominates(const NodeT *A,
  780. const NodeT *B) const {
  781. if (A == B)
  782. return true;
  783. // Cast away the const qualifiers here. This is ok since
  784. // this function doesn't actually return the values returned
  785. // from getNode.
  786. return dominates(getNode(const_cast<NodeT *>(A)),
  787. getNode(const_cast<NodeT *>(B)));
  788. }
  789. template <typename NodeT, bool IsPostDom>
  790. bool DominatorTreeBase<NodeT, IsPostDom>::properlyDominates(
  791. const NodeT *A, const NodeT *B) const {
  792. if (A == B)
  793. return false;
  794. // Cast away the const qualifiers here. This is ok since
  795. // this function doesn't actually return the values returned
  796. // from getNode.
  797. return dominates(getNode(const_cast<NodeT *>(A)),
  798. getNode(const_cast<NodeT *>(B)));
  799. }
  800. } // end namespace llvm
  801. #endif // LLVM_SUPPORT_GENERICDOMTREE_H