     node insn>> temp-vregs length +
     dup node (>>registers) ;
 
-: data-dependence? ( to from -- ? )
-    ! If this takes lots of time, then refactor code
-    ! so that nodes store their data dependences
-    [ insn>> ] bi@
-    [ uses-vregs ] [ defs-vreg ] bi*
-    swap member? ;
+: attach-parent ( node parent -- )
+    [ >>parent drop ]
+    [ [ ?push ] change-children drop ] 2bi ;
 
-DEFER: follow-tree
-
-: maybe-cut-node ( node -- ? )
-    ! If this node has multiple successors
-    ! then it needs to be made into the head of a new tree
-    [ precedes>> assoc-size 1 = dup ] keep
-    '[ _ dup , follow-tree ] when ;
-
-: follow-tree ( node -- )
-    ! This is bogus: it misses nodes that aren't reachable
-    ! from the roots because of a control dependence
-    dup dup follows>> values
-    [ data-dependence? ] with filter
-    [ parent>> not ] filter
-    [ maybe-cut-node ] filter
-    
-    [ [ >>parent drop ] with each ]
-    [ >>children drop ] 2bi ;
-
-: attach-parent ( node -- )
-    drop ;
+: choose-parent ( node -- )
+    ! If a node has control dependences, it has to be a root
+    ! Otherwise, choose one of the data dependences for a parent
+    dup precedes-control>> assoc-empty? [
+        dup precedes-data>> values [ drop ] [
+            first attach-parent
+        ] if-empty
+    ] [ drop ] if ;
 
 : make-trees ( -- trees )
     nodes get
-    [ [ attach-parent ] each ]
+    [ [ choose-parent ] each ]
     [ [ parent>> not ] filter ] bi ;
 
 ERROR: node-missing-parent trees nodes ;

        node-number counter >>number
        swap >>insn
        H{ } clone >>precedes
        H{ } clone >>follows ;

: ready? ( node -- ? ) precedes>> assoc-empty? ;

USING: accessors assocs combinators compiler.cfg.def-use
compiler.cfg.instructions compiler.cfg.registers fry kernel
locals namespaces sequences sets sorting math.vectors
make math combinators.short-circuit vectors ;
IN: compiler.cfg.dependence

! Dependence graph construction
