     children>> building get length
     '[ _ >>parent-index drop ] each ;
 
-SYMBOL: roots
-
 : ready? ( node -- ? ) precedes>> assoc-empty? ;
 
-: remove-node ( node -- )
-    [ follows>> members ] keep
-    '[ [ precedes>> _ swap check-delete-at ] each ]
-    [ [ ready? ] filter roots get push-all ] bi ;
+: remove-node ( roots node -- )
+    dup follows>> [ [ precedes>> check-delete-at ] with each ] keep
+    [ ready? ] filter swap push-all ;
 
 : score ( node -- n )
     [ parent-index>> ] [ registers>> neg ] [ insn>> insn#>> ] tri 3array ;

: select ( vector quot: ( elt -- score ) -- elt )
    dupd supremum-by swap dupd remove-eq! drop ; inline

: select-instruction ( -- insn/f )
    roots get [ f ] [
        [ score ] select
        [ insn>> ]
        [ set-parent-indices ]
        [ remove-node ] tri
    ] if-empty ;

: (reorder) ( -- )
    select-instruction [
        , (reorder)
    ] when* ;

UNION: initial-insn ##phi ##inc-d ##inc-r ##callback-inputs ;


    [ [ ready? ] V{ } filter-as ] tri ;

: reorder-body ( body -- body' )
    setup-root-nodes [ (reorder) ] V{ } make reverse ;

: reorder ( insns -- insns' )
    split-insns first3 [ reorder-body ] dip 3append ;
