 compiler.cfg.value-numbering.expressions
 compiler.cfg.value-numbering.graph
 compiler.cfg.value-numbering.simplify fry kernel layouts math
-namespaces sequences ;
+namespaces sequences cpu.architecture math.bitwise locals ;
 IN: compiler.cfg.value-numbering.rewrite
 
 GENERIC: rewrite ( insn -- insn' )
 
-M: ##mul-imm rewrite
-    dup src2>> dup power-of-2? [
-        [ [ dst>> ] [ src1>> ] bi ] [ log2 ] bi* \ ##shl-imm new-insn
-        dup number-values
-    ] [ drop ] if ;
+M: insn rewrite ;
 
 : ##branch-t? ( insn -- ? )
     dup ##compare-imm-branch? [

        ] when
    ] when ;

: combine-add-imm? ( insn -- ? )
    {
        [ src1>> vreg>expr op>> \ ##add-imm = ]
        [ src2>> number? ]
    } 1&& ;

: combine-add-imm ( dst src n -- insn )
    [ vreg>expr [ in1>> vn>vreg ] [ in2>> vn>constant ] bi ] dip
    + \ ##add-imm new-insn ;

M: ##add-imm rewrite
    dup combine-add-imm? [
        [ dst>> ] [ src1>> ] [ src2>> ] tri combine-add-imm
        dup number-values
    ] when ;

M: insn rewrite ;

: flip-comparison ( insn -- insn' )
    [ dst>> ]
    [ src2>> ]
    [ src1>> vreg>constant ] tri
    cc= i \ ##compare-imm new-insn ;

M: ##compare rewrite
