[PATCH] D83092: DomTree: Add findSiblingOfUncle helper

Nicolai Hähnle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 2 14:36:17 PDT 2020


nhaehnle created this revision.
nhaehnle added reviewers: arsenm, RKSimon, mehdi_amini, courbet.
Herald added subscribers: hiraditya, wdng.
Herald added a project: LLVM.
nhaehnle added a child revision: D83090: DomTree: Add TreeNode type alias.
nhaehnle removed a child revision: D83090: DomTree: Add TreeNode type alias.
nhaehnle added a parent revision: D83090: DomTree: Add TreeNode type alias.
nhaehnle added a child revision: D83093: DomTree: Define GraphTraits for GenericDomTreeBase.

Change-Id: I0f756786e14ef6228d0275e69d94eb8d7460d94b


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83092

Files:
  llvm/include/llvm/Support/GenericDomTree.h
  llvm/lib/Support/GenericDomTree.cpp


Index: llvm/lib/Support/GenericDomTree.cpp
===================================================================
--- llvm/lib/Support/GenericDomTree.cpp
+++ llvm/lib/Support/GenericDomTree.cpp
@@ -214,6 +214,24 @@
   return Dom ? Dom->getBlock() : CfgBlockRef();
 }
 
+/// findSiblingOfUncle - Under the assumption that \p Uncle is the sibling
+/// of some ancestor of \p A in the tree, find that ancestor. Also handles
+/// the degenerate case where \p A itself is a sibling of \p Uncle.
+const GenericDomTreeNodeBase *GenericDominatorTreeBase::findSiblingOfUncle(
+    const GenericDomTreeNodeBase *A,
+    const GenericDomTreeNodeBase *Uncle) const {
+  assert(A && Uncle && "Pointers are not valid");
+
+  // Use level information to go up the tree until the levels match.
+  assert(A->getLevel() >= Uncle->getLevel());
+  while (A->getLevel() > Uncle->getLevel())
+    A = A->IDom;
+
+  assert(A->IDom == Uncle->IDom && "Uncle was not in fact an uncle");
+
+  return A;
+}
+
 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
 /// dominator tree in dfs order.
 void GenericDominatorTreeBase::updateDFSNumbers() const {
Index: llvm/include/llvm/Support/GenericDomTree.h
===================================================================
--- llvm/include/llvm/Support/GenericDomTree.h
+++ llvm/include/llvm/Support/GenericDomTree.h
@@ -307,6 +307,10 @@
   CfgBlockRef findNearestCommonDominatorBlock(CfgBlockRef A,
                                               CfgBlockRef B) const;
 
+  const GenericDomTreeNodeBase *
+  findSiblingOfUncle(const GenericDomTreeNodeBase *A,
+                     const GenericDomTreeNodeBase *Uncle) const;
+
   void updateDFSNumbers() const;
 
 private:
@@ -497,6 +501,12 @@
     return dom->getBlock();
   }
 
+  const TreeNode *findSiblingOfUncle(const TreeNode *A,
+                                     const TreeNode *Uncle) const {
+    return static_cast<const TreeNode *>(
+        GenericDominatorTreeBase::findSiblingOfUncle(A, Uncle));
+  }
+
   //===--------------------------------------------------------------------===//
   // API to update (Post)DominatorTree information based on modifications to
   // the CFG...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83092.275234.patch
Type: text/x-patch
Size: 2193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200702/75af6f5b/attachment-0001.bin>


More information about the llvm-commits mailing list