[llvm] [GenericDomTreeConstruction] Speed up Semi-NCA construction (PR #207474)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 4 08:27:18 PDT 2026


================
@@ -111,19 +111,25 @@ template <typename DomTreeT> struct SemiNCAInfo {
   static SmallVector<NodePtr, 8> getChildren(NodePtr N, BatchUpdatePtr BUI) {
     if (BUI)
       return BUI->PreViewCFG.template getChildren<Inversed>(N);
-    return getChildren<Inversed>(N);
+    // Force the element type to NodePtr. some graphs (clang's
+    // CFGBlock::AdjacentBlock) yield a proxy convertible to NodePtr rather than
+    // NodePtr itself.
+    auto Children = getChildren<Inversed>(N);
+    return SmallVector<NodePtr, 8>(Children.begin(), Children.end());
   }
 
-  template <bool Inversed>
-  static SmallVector<NodePtr, 8> getChildren(NodePtr N) {
+  // Returns a lazy range over N's children, reversed for non-inverted graphs so
+  // a LIFO worklist visits them in their natural order. Graphs whose children
+  // aren't NodePtr (clang's CFGBlock::AdjacentBlock) may include nullptr, which
+  // are filtered out.
+  template <bool Inversed> static auto getChildren(NodePtr N) {
     using DirectedNodeT =
         std::conditional_t<Inversed, Inverse<NodePtr>, NodePtr>;
-    auto R = children<DirectedNodeT>(N);
-    SmallVector<NodePtr, 8> Res(detail::reverse_if<!Inversed>(R));
-
-    // Remove nullptr children for clang.
-    llvm::erase(Res, nullptr);
-    return Res;
+    auto R = detail::reverse_if<!Inversed>(children<DirectedNodeT>(N));
+    if constexpr (std::is_same_v<std::decay_t<decltype(*R.begin())>, NodePtr>)
----------------
MaskRay wrote:

Improved comment

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


More information about the llvm-commits mailing list