[llvm] [MachineOutliner] Leaf Descendants (PR #90275)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 16:02:29 PDT 2024


================
@@ -105,6 +114,68 @@ void SuffixTree::setSuffixIndices() {
   }
 }
 
+void SuffixTree::setLeafNodes() {
+  // A stack that keeps track of nodes to visit for post-order DFS traversal.
+  std::stack<SuffixTreeNode *> ToVisit;
+  ToVisit.push(Root);
+
+  // This keeps track of the index of the next leaf node to be added to
+  // the LeafNodes vector of the suffix tree.
+  unsigned LeafCounter = 0;
+
+  // This keeps track of nodes whose children have been added to the stack
+  // during the post-order depth-first traversal of the tree.
+  llvm::SmallPtrSet<SuffixTreeInternalNode *, 32> ChildrenAddedToStack;
+
+  // Traverse the tree in post-order.
+  while (!ToVisit.empty()) {
+    SuffixTreeNode *CurrNode = ToVisit.top();
+    ToVisit.pop();
+    if (auto *CurrInternalNode = dyn_cast<SuffixTreeInternalNode>(CurrNode)) {
+      // The current node is an internal node.
+      if (ChildrenAddedToStack.find(CurrInternalNode) !=
+          ChildrenAddedToStack.end()) {
----------------
ellishg wrote:

```suggestion
      auto [it, wasInserted] = ChildrenAddedToStack.insert(CurrInternalNode);
      if (!wasInserted) {
```
Then we can delete `ChildrenAddedToStack.insert(CurrInternalNode)` in the else case. In fact, I might also flip the condition so the else case comes first, since that is much simpler.

https://github.com/llvm/llvm-project/pull/90275


More information about the llvm-commits mailing list