GenericDomTree.h 33 KB

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