1 ! Copyright (C) 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors compiler.cfg.instructions
4 compiler.cfg.value-numbering.graph
5 compiler.cfg.value-numbering.rewrite kernel layouts math
7 IN: compiler.cfg.value-numbering.folding
9 : binary-constant-fold? ( insn -- ? )
10 src1>> vreg>insn ##load-integer? ; inline
12 GENERIC: binary-constant-fold* ( x y insn -- z )
14 M: ##add-imm binary-constant-fold* drop + ;
15 M: ##sub-imm binary-constant-fold* drop - ;
16 M: ##mul-imm binary-constant-fold* drop * ;
17 M: ##and-imm binary-constant-fold* drop bitand ;
18 M: ##or-imm binary-constant-fold* drop bitor ;
19 M: ##xor-imm binary-constant-fold* drop bitxor ;
20 M: ##shr-imm binary-constant-fold* drop [ cell-bits 2^ wrap ] dip neg shift ;
21 M: ##sar-imm binary-constant-fold* drop neg shift ;
22 M: ##shl-imm binary-constant-fold* drop shift ;
24 : binary-constant-fold ( insn -- insn' )
26 [ [ src1>> vreg>integer ] [ src2>> ] [ ] tri binary-constant-fold* ] bi
27 ##load-integer new-insn ; inline
29 : unary-constant-fold? ( insn -- ? )
30 src>> vreg>insn ##load-integer? ; inline
32 GENERIC: unary-constant-fold* ( x insn -- y )
34 M: ##not unary-constant-fold* drop bitnot ;
35 M: ##neg unary-constant-fold* drop neg ;
37 : unary-constant-fold ( insn -- insn' )
38 [ dst>> ] [ [ src>> vreg>integer ] [ ] bi unary-constant-fold* ] bi
39 ##load-integer new-insn ; inline