[llvm] [GenericDomTree][NFC] Use llvm algorithms (PR #97104)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 3 05:11:51 PDT 2024


https://github.com/MagentaTreehouse updated https://github.com/llvm/llvm-project/pull/97104

>From e9acff2abdbe627834d755ec90e248d26d33a7d8 Mon Sep 17 00:00:00 2001
From: Mingyi Chen <cmingyi01 at gmail.com>
Date: Fri, 28 Jun 2024 15:45:13 -0400
Subject: [PATCH 1/2] Use llvm algorithms

---
 llvm/include/llvm/Support/GenericDomTree.h    | 33 ++++++++-----------
 .../llvm/Support/GenericDomTreeConstruction.h | 12 +++----
 2 files changed, 18 insertions(+), 27 deletions(-)

diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h
index a8e178d6461e0..30dd2d6dc2bad 100644
--- a/llvm/include/llvm/Support/GenericDomTree.h
+++ b/llvm/include/llvm/Support/GenericDomTree.h
@@ -65,7 +65,7 @@ template <class NodeT> class DomTreeNodeBase {
   mutable unsigned DFSNumIn = ~0;
   mutable unsigned DFSNumOut = ~0;
 
- public:
+public:
   DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
       : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
 
@@ -113,12 +113,10 @@ template <class NodeT> class DomTreeNodeBase {
       OtherChildren.insert(Nd);
     }
 
-    for (const DomTreeNodeBase *I : *this) {
+    return llvm::any_of(*this, [&](const DomTreeNodeBase *I) {
       const NodeT *N = I->getBlock();
-      if (OtherChildren.count(N) == 0)
-        return true;
-    }
-    return false;
+      return OtherChildren.count(N) == 0;
+    });
   }
 
   void setIDom(DomTreeNodeBase *NewIDom) {
@@ -239,7 +237,7 @@ template <typename NodeT> struct DomTreeNodeTraits {
 /// various graphs in the LLVM IR or in the code generator.
 template <typename NodeT, bool IsPostDom>
 class DominatorTreeBase {
- public:
+public:
   static_assert(std::is_pointer_v<typename GraphTraits<NodeT *>::NodeRef>,
                 "Currently DominatorTreeBase supports only pointer nodes");
   using NodeTrait = DomTreeNodeTraits<NodeT>;
@@ -273,7 +271,7 @@ class DominatorTreeBase {
 
   friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase>;
 
- public:
+public:
   DominatorTreeBase() = default;
 
   DominatorTreeBase(DominatorTreeBase &&Arg)
@@ -852,19 +850,10 @@ class DominatorTreeBase {
            "NewBB should have a single successor!");
     NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
 
-    SmallVector<NodeRef, 4> PredBlocks(inverse_children<N>(NewBB));
+    const SmallVector<NodeRef, 4> PredBlocks(inverse_children<N>(NewBB));
 
     assert(!PredBlocks.empty() && "No predblocks?");
 
-    bool NewBBDominatesNewBBSucc = true;
-    for (auto *Pred : inverse_children<N>(NewBBSucc)) {
-      if (Pred != NewBB && !dominates(NewBBSucc, Pred) &&
-          isReachableFromEntry(Pred)) {
-        NewBBDominatesNewBBSucc = false;
-        break;
-      }
-    }
-
     // Find NewBB's immediate dominator and create new dominator tree node for
     // NewBB.
     NodeT *NewBBIDom = nullptr;
@@ -885,6 +874,12 @@ class DominatorTreeBase {
         NewBBIDom = findNearestCommonDominator(NewBBIDom, PredBlocks[i]);
     }
 
+    bool NewBBDominatesNewBBSucc =
+      llvm::none_of(inverse_children<N>(NewBBSucc), [=](const auto *Pred) {
+        return Pred != NewBB && !dominates(NewBBSucc, Pred) &&
+          isReachableFromEntry(Pred);
+      });
+
     // Create the new dominator tree node... and set the idom of NewBB.
     DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
 
@@ -896,7 +891,7 @@ class DominatorTreeBase {
     }
   }
 
- private:
+private:
   bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
                                const DomTreeNodeBase<NodeT> *B) const {
     assert(A != B);
diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
index 57cbe993d8739..35b1ef7001ed0 100644
--- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h
+++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
@@ -197,10 +197,9 @@ struct SemiNCAInfo {
       constexpr bool Direction = IsReverse != IsPostDom;  // XOR.
       auto Successors = getChildren<Direction>(BB, BatchUpdates);
       if (SuccOrder && Successors.size() > 1)
-        llvm::sort(
-            Successors.begin(), Successors.end(), [=](NodePtr A, NodePtr B) {
-              return SuccOrder->find(A)->second < SuccOrder->find(B)->second;
-            });
+        llvm::sort(Successors, [=](NodePtr A, NodePtr B) {
+          return SuccOrder->find(A)->second < SuccOrder->find(B)->second;
+        });
 
       for (const NodePtr Succ : Successors) {
         if (!Condition(BB, Succ)) continue;
@@ -682,10 +681,7 @@ struct SemiNCAInfo {
     if (A.size() != B.size())
       return false;
     SmallPtrSet<NodePtr, 4> Set(A.begin(), A.end());
-    for (NodePtr N : B)
-      if (Set.count(N) == 0)
-        return false;
-    return true;
+    return llvm::none_of(B, [&](NodePtr N) { return Set.count(N) == 0; });
   }
 
   // Updates the set of roots after insertion or deletion. This ensures that

>From 162c8f5ffcf0ca3254fb17733925a3ab60665260 Mon Sep 17 00:00:00 2001
From: Mingyi Chen <cmingyi01 at gmail.com>
Date: Tue, 2 Jul 2024 23:40:04 -0400
Subject: [PATCH 2/2] Revert unrelated changes and avoid capture-default `=`

---
 llvm/include/llvm/Support/GenericDomTree.h     | 18 +++++++++---------
 .../llvm/Support/GenericDomTreeConstruction.h  |  2 +-
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h
index 30dd2d6dc2bad..ec596f3b94a87 100644
--- a/llvm/include/llvm/Support/GenericDomTree.h
+++ b/llvm/include/llvm/Support/GenericDomTree.h
@@ -65,7 +65,7 @@ template <class NodeT> class DomTreeNodeBase {
   mutable unsigned DFSNumIn = ~0;
   mutable unsigned DFSNumOut = ~0;
 
-public:
+ public:
   DomTreeNodeBase(NodeT *BB, DomTreeNodeBase *iDom)
       : TheBB(BB), IDom(iDom), Level(IDom ? IDom->Level + 1 : 0) {}
 
@@ -237,7 +237,7 @@ template <typename NodeT> struct DomTreeNodeTraits {
 /// various graphs in the LLVM IR or in the code generator.
 template <typename NodeT, bool IsPostDom>
 class DominatorTreeBase {
-public:
+ public:
   static_assert(std::is_pointer_v<typename GraphTraits<NodeT *>::NodeRef>,
                 "Currently DominatorTreeBase supports only pointer nodes");
   using NodeTrait = DomTreeNodeTraits<NodeT>;
@@ -271,7 +271,7 @@ class DominatorTreeBase {
 
   friend struct DomTreeBuilder::SemiNCAInfo<DominatorTreeBase>;
 
-public:
+ public:
   DominatorTreeBase() = default;
 
   DominatorTreeBase(DominatorTreeBase &&Arg)
@@ -850,7 +850,7 @@ class DominatorTreeBase {
            "NewBB should have a single successor!");
     NodeRef NewBBSucc = *GraphT::child_begin(NewBB);
 
-    const SmallVector<NodeRef, 4> PredBlocks(inverse_children<N>(NewBB));
+    SmallVector<NodeRef, 4> PredBlocks(inverse_children<N>(NewBB));
 
     assert(!PredBlocks.empty() && "No predblocks?");
 
@@ -875,10 +875,10 @@ class DominatorTreeBase {
     }
 
     bool NewBBDominatesNewBBSucc =
-      llvm::none_of(inverse_children<N>(NewBBSucc), [=](const auto *Pred) {
-        return Pred != NewBB && !dominates(NewBBSucc, Pred) &&
-          isReachableFromEntry(Pred);
-      });
+        llvm::none_of(inverse_children<N>(NewBBSucc), [&](const auto *Pred) {
+          return Pred != NewBB && !dominates(NewBBSucc, Pred) &&
+                 isReachableFromEntry(Pred);
+        });
 
     // Create the new dominator tree node... and set the idom of NewBB.
     DomTreeNodeBase<NodeT> *NewBBNode = addNewBlock(NewBB, NewBBIDom);
@@ -891,7 +891,7 @@ class DominatorTreeBase {
     }
   }
 
-private:
+ private:
   bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A,
                                const DomTreeNodeBase<NodeT> *B) const {
     assert(A != B);
diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
index 35b1ef7001ed0..415b8bd30161a 100644
--- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h
+++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
@@ -197,7 +197,7 @@ struct SemiNCAInfo {
       constexpr bool Direction = IsReverse != IsPostDom;  // XOR.
       auto Successors = getChildren<Direction>(BB, BatchUpdates);
       if (SuccOrder && Successors.size() > 1)
-        llvm::sort(Successors, [=](NodePtr A, NodePtr B) {
+        llvm::sort(Successors, [SuccOrder](NodePtr A, NodePtr B) {
           return SuccOrder->find(A)->second < SuccOrder->find(B)->second;
         });
 



More information about the llvm-commits mailing list