[PATCH] D75507: [mlir] Extended Dominance analysis with a function to find the nearest common dominator of two given blocks.

Marcel Koester via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 01:43:26 PST 2020


dfki-mako created this revision.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

The Dominance analysis currently misses a utility function to find the nearest common dominator of two given blocks. This is required for a huge variety of different control-flow analyses and transformations. This commit adds this function and moves the getNode function from DominanceInfo to DominanceInfoBase, as it also works for post dominators.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75507

Files:
  mlir/include/mlir/Analysis/Dominance.h
  mlir/lib/Analysis/Dominance.cpp


Index: mlir/lib/Analysis/Dominance.cpp
===================================================================
--- mlir/lib/Analysis/Dominance.cpp
+++ mlir/lib/Analysis/Dominance.cpp
@@ -26,6 +26,47 @@
 // DominanceInfoBase
 //===----------------------------------------------------------------------===//
 
+template <bool IsPostDom>
+Block *
+DominanceInfoBase<IsPostDom>::findNearestCommonDominator(Block *a,
+                                                         Block *b) const {
+  // If either a or b are null, then conservatively return nullptr.
+  if (!a || !b)
+    return nullptr;
+
+  // Get and verify dominance information of regionA.
+  Region *regionA = a->getParent();
+  auto infoAIt = dominanceInfos.find(regionA);
+  if (infoAIt == dominanceInfos.end())
+    return nullptr;
+
+  // If both block do not live in the same region, we will have to check their
+  // parent operations.
+  Region *regionB = b->getParent();
+  if (regionA != regionB) {
+    // Get parent operations and check whether they exist and belong to blocks.
+    Operation *parentA = regionA->getParentOp();
+    Operation *parentB = regionB->getParentOp();
+    if (!parentA || !parentA->getBlock() || !parentB || !parentB->getBlock())
+      return nullptr;
+
+// Try to find the nearest common dominator of the two parent blocks.
+    return findNearestCommonDominator(parentA->getBlock(), parentB->getBlock());
+  }
+
+  // If the blocks live in the same region, we can rely on already
+  // existing dominance functionality.
+  assert(regionA == regionB);
+  return infoAIt->second->findNearestCommonDominator(a, b);
+}
+
+template <bool IsPostDom>
+DominanceInfoNode *DominanceInfoBase<IsPostDom>::getNode(Block *a) {
+  auto *region = a->getParent();
+  assert(dominanceInfos.count(region) != 0);
+  return dominanceInfos[region]->getNode(a);
+}
+
 template <bool IsPostDom>
 void DominanceInfoBase<IsPostDom>::recalculate(Operation *op) {
   dominanceInfos.clear();
@@ -132,12 +173,6 @@
   return dominates(a.cast<BlockArgument>().getOwner(), b->getBlock());
 }
 
-DominanceInfoNode *DominanceInfo::getNode(Block *a) {
-  auto *region = a->getParent();
-  assert(dominanceInfos.count(region) != 0);
-  return dominanceInfos[region]->getNode(a);
-}
-
 void DominanceInfo::updateDFSNumbers() {
   for (auto &iter : dominanceInfos)
     iter.second->updateDFSNumbers();
Index: mlir/include/mlir/Analysis/Dominance.h
===================================================================
--- mlir/include/mlir/Analysis/Dominance.h
+++ mlir/include/mlir/Analysis/Dominance.h
@@ -34,12 +34,20 @@
   /// Recalculate the dominance info.
   void recalculate(Operation *op);
 
+  /// Finds the nearest common dominator block for the two given blocks a
+  /// and b. If no common dominator can be found, this function will return
+  /// nullptr.
+  Block *findNearestCommonDominator(Block *a, Block *b) const;
+
   /// Get the root dominance node of the given region.
   DominanceInfoNode *getRootNode(Region *region) {
     assert(dominanceInfos.count(region) != 0);
     return dominanceInfos[region]->getRootNode();
   }
 
+  /// Return the dominance node from the Region containing block A.
+  DominanceInfoNode *getNode(Block *a);
+
 protected:
   using super = DominanceInfoBase<IsPostDom>;
 
@@ -82,9 +90,6 @@
     return super::properlyDominates(a, b);
   }
 
-  /// Return the dominance node from the Region containing block A.
-  DominanceInfoNode *getNode(Block *a);
-
   /// Update the internal DFS numbers for the dominance nodes.
   void updateDFSNumbers();
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75507.247821.patch
Type: text/x-patch
Size: 3560 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200303/4ad920c0/attachment.bin>


More information about the llvm-commits mailing list