         ] when
     ] when ;
 
+: constant-fold ( insn -- insn' )
+    dup dst>> vreg>expr dup constant-expr? [
+        [ dst>> ] [ value>> ] bi* \ ##load-immediate new-insn
+        dup number-values
+    ] [
+        drop
+    ] if ;
+
 : (new-imm-insn) ( insn dst src1 n op -- new-insn/insn )
     [ cell-bits bits ] dip over small-enough? [
         new-insn dup number-values nip
     ] [
         2drop 2drop
-    ] if ; inline
+    ] if constant-fold ; inline
 
 : new-imm-insn ( insn dst src n op -- n' op' )
     2dup [ sgn ] dip 2array

: combine-imm? ( insn op -- ? )
    [ src1>> vreg>expr op>> ] dip = ;

: combine-imm ( insn quot op -- insn )
    [
        {
            [ ]

        } cleave
    ] [ call ] [ ] tri* new-imm-insn ; inline

:: combine-imm ( insn quot op -- insn )
    insn op combine-imm? [
        insn quot op (combine-imm)
    ] [
        insn
    ] if ; inline

M: ##add-imm rewrite
    {
        { [ dup \ ##add-imm combine-imm? ] [ [ + ] \ ##add-imm (combine-imm) ] }
        { [ dup \ ##sub-imm combine-imm? ] [ [ - ] \ ##sub-imm (combine-imm) ] }
        [ ]
    } cond ;

M: ##sub-imm rewrite
    {
        { [ dup \ ##add-imm combine-imm? ] [ [ - ] \ ##add-imm (combine-imm) ] }
        { [ dup \ ##sub-imm combine-imm? ] [ [ + ] \ ##sub-imm (combine-imm) ] }
        [ ]
    } cond ;

