Skip to content

Conversation

@aengelke
Copy link
Contributor

Previously, the method would take the unique_ptr, store the pointer in its Children vector, and then return the unique_ptr. Pass the raw pointer as parameter instead.

This was added in a72d6ef when introducing unique_ptr, previosuly this was a source code size optimization.


Context: this will simplify replacing DomTreeNodes with a different data structure, see this RFC.

Previously, the method would take the unique_ptr, store the pointer in
its Children vector, and then return the unique_ptr. Pass the raw
pointer as parameter instead.

This was added in a72d6ef when introducing unique_ptr,
previosuly this was a source code size optimization.
@llvmbot
Copy link
Member

llvmbot commented Jul 29, 2024

@llvm/pr-subscribers-llvm-support

Author: Alexis Engelke (aengelke)

Changes

Previously, the method would take the unique_ptr, store the pointer in its Children vector, and then return the unique_ptr. Pass the raw pointer as parameter instead.

This was added in a72d6ef when introducing unique_ptr, previosuly this was a source code size optimization.


Context: this will simplify replacing DomTreeNodes with a different data structure, see this RFC.


Full diff: https://github.com/llvm/llvm-project/pull/101056.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/GenericDomTree.h (+7-10)
diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h
index a8e178d6461e0..5bf4a34eb9770 100644
--- a/llvm/include/llvm/Support/GenericDomTree.h
+++ b/llvm/include/llvm/Support/GenericDomTree.h
@@ -90,11 +90,7 @@ template <class NodeT> class DomTreeNodeBase {
   DomTreeNodeBase *getIDom() const { return IDom; }
   unsigned getLevel() const { return Level; }
 
-  std::unique_ptr<DomTreeNodeBase> addChild(
-      std::unique_ptr<DomTreeNodeBase> C) {
-    Children.push_back(C.get());
-    return C;
-  }
+  void addChild(DomTreeNodeBase *C) { Children.push_back(C); }
 
   bool isLeaf() const { return Children.empty(); }
   size_t getNumChildren() const { return Children.size(); }
@@ -655,8 +651,8 @@ class DominatorTreeBase {
     } else {
       assert(Roots.size() == 1);
       NodeT *OldRoot = Roots.front();
-      auto &OldNode = DomTreeNodes[OldRoot];
-      OldNode = NewNode->addChild(std::move(DomTreeNodes[OldRoot]));
+      DomTreeNodeBase<NodeT> *OldNode = getNode(OldRoot);
+      NewNode->addChild(OldNode);
       OldNode->IDom = NewNode;
       OldNode->UpdateLevel();
       Roots[0] = BB;
@@ -831,9 +827,10 @@ class DominatorTreeBase {
   void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
 
   DomTreeNodeBase<NodeT> *createChild(NodeT *BB, DomTreeNodeBase<NodeT> *IDom) {
-    return (DomTreeNodes[BB] = IDom->addChild(
-                std::make_unique<DomTreeNodeBase<NodeT>>(BB, IDom)))
-        .get();
+    auto Node = std::make_unique<DomTreeNodeBase<NodeT>>(BB, IDom);
+    auto *NodePtr = (DomTreeNodes[BB] = std::move(Node)).get();
+    IDom->addChild(NodePtr);
+    return NodePtr;
   }
 
   DomTreeNodeBase<NodeT> *createNode(NodeT *BB) {

Comment on lines 831 to 833
auto *NodePtr = (DomTreeNodes[BB] = std::move(Node)).get();
IDom->addChild(NodePtr);
return NodePtr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something like this?

Suggested change
auto *NodePtr = (DomTreeNodes[BB] = std::move(Node)).get();
IDom->addChild(NodePtr);
return NodePtr;
auto *NodePtr = Node.get();
DomTreeNodes[BB] = std::move(Node);
return NodePtr;

Really not a fan of this "using return value of move assignment operator" pattern...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. (I didn't like that either, but wanted at least some consistency with createNode below.) I now merged this method with createNode to avoid code duplication.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aengelke aengelke merged commit f6a928a into llvm:main Jul 30, 2024
@aengelke aengelke deleted the domtreecleanup1 branch July 30, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants