[PATCH] D155420: [PostDom] add findNearestCommonDominator for instructions

Kohei Asano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 16 23:06:50 PDT 2023


khei4 created this revision.
khei4 added a reviewer: nikic.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
khei4 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Implement PostDom version findNearestCommonDominator for instructions.

This seems symmetric to DominatorTree but contains some differences, e.g. returned ptr is nullable, and the use-case is only https://reviews.llvm.org/D155406. (TODO: search the use-cases).


https://reviews.llvm.org/D155420

Files:
  llvm/include/llvm/Analysis/PostDominators.h
  llvm/lib/Analysis/PostDominators.cpp


Index: llvm/lib/Analysis/PostDominators.cpp
===================================================================
--- llvm/lib/Analysis/PostDominators.cpp
+++ llvm/lib/Analysis/PostDominators.cpp
@@ -73,6 +73,23 @@
   return &*I == I2;
 }
 
+Instruction *
+PostDominatorTree::findNearestCommonDominator(Instruction *I1,
+                                              Instruction *I2) const {
+  BasicBlock *BB1 = I1->getParent();
+  BasicBlock *BB2 = I2->getParent();
+  if (BB1 == BB2)
+    return I1->comesBefore(I2) ? I2 : I1;
+  BasicBlock *PDomBB = findNearestCommonDominator(BB1, BB2);
+  if (!PDomBB)
+    return nullptr;
+  if (BB1 == PDomBB)
+    return I2;
+  if (BB1 == PDomBB)
+    return I1;
+  return PDomBB->getFirstNonPHI();
+}
+
 bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) {
   DT.recalculate(F);
   return false;
Index: llvm/include/llvm/Analysis/PostDominators.h
===================================================================
--- llvm/include/llvm/Analysis/PostDominators.h
+++ llvm/include/llvm/Analysis/PostDominators.h
@@ -41,6 +41,14 @@
   /// Return true if \p I1 dominates \p I2. This checks if \p I2 comes before
   /// \p I1 if they belongs to the same basic block.
   bool dominates(const Instruction *I1, const Instruction *I2) const;
+
+  // Ensure base class overloads are visible.
+  using Base::findNearestCommonDominator;
+
+  /// Find the nearest instruction I that post-dominates both I1 and I2, in the
+  /// sense that a result produced after I will be available at both I1 and I2.
+  Instruction *findNearestCommonDominator(Instruction *I1,
+                                          Instruction *I2) const;
 };
 
 /// Analysis pass which computes a \c PostDominatorTree.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155420.540867.patch
Type: text/x-patch
Size: 1733 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230717/406feb47/attachment.bin>


More information about the llvm-commits mailing list