 
 ERROR: cannot-merge-poisoned states ;
 
+: multiple-predecessors ( bb states -- state )
+    dup [ not ] any? [
+        [ <state> ] 2dip
+        sift merge-heights
+    ] [
+        dup [ poisoned?>> ] any? [
+            cannot-merge-poisoned
+        ] [
+            [ state new ] 2dip
+            [ predecessors>> ] dip
+            {
+                [ merge-locs ]
+                [ merge-actual-locs ]
+                [ merge-heights ]
+                [ merge-changed-locs ]
+            } 2cleave
+        ] if
+    ] if ;
+
 : merge-states ( bb states -- state )
     ! If any states are poisoned, save all registers
     ! to the stack in each branch
     dup length {
-        { 0 [ 2drop <state> ] }
-        { 1 [ nip first clone ] }
-        [
-            drop
-            dup [ not ] any? [
-                [ <state> ] 2dip
-                sift merge-heights
-            ] [
-                dup [ poisoned?>> ] any? [
-                    cannot-merge-poisoned
-                ] [
-                    [ state new ] 2dip
-                    [ predecessors>> ] dip
-                    {
-                        [ merge-locs ]
-                        [ merge-actual-locs ]
-                        [ merge-heights ]
-                        [ merge-changed-locs ]
-                    } 2cleave
-                ] if
-            ] if
-        ]
+        { 0 [ initial-state ] }
+        { 1 [ single-predecessor ] }
+        [ drop multiple-predecessors ]
     } case ;
 
 : block-in-state ( bb -- states )

UNION: neutral-insn
    ##flushable
    ##effect
    ##branch
    ##loop-entry
    ##conditional-branch
    ##compare-imm-branch
    ##dispatch ;

M: neutral-insn visit , ;

UNION: sync-if-back-edge
    ##branch
    ##conditional-branch
    ##compare-imm-branch ;

M: sync-if-back-edge visit
    basic-block get [ successors>> ] [ number>> ] bi '[ number>> _ < ] any?
    [ sync-state ] when
    , ;


! Maps basic-blocks to states
SYMBOLS: state-in state-out ;

: initial-state ( bb states -- state ) 2drop <state> ;

: single-predecessor ( bb states -- state ) nip first clone ;

ERROR: must-equal-failed seq ;

