]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/value-numbering/folding/folding.factor
Switch to https urls
[factor.git] / basis / compiler / cfg / value-numbering / folding / folding.factor
1 ! Copyright (C) 2010 Slava Pestov.
2 ! See https://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
6 math.bitwise ;
7 IN: compiler.cfg.value-numbering.folding
8
9 : binary-constant-fold? ( insn -- ? )
10     src1>> vreg>insn ##load-integer? ; inline
11
12 GENERIC: binary-constant-fold* ( x y insn -- z )
13
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 ;
23
24 : binary-constant-fold ( insn -- insn' )
25     [ dst>> ]
26     [ [ src1>> vreg>integer ] [ src2>> ] [ ] tri binary-constant-fold* ] bi
27     ##load-integer new-insn ; inline
28
29 : unary-constant-fold? ( insn -- ? )
30     src>> vreg>insn ##load-integer? ; inline
31
32 GENERIC: unary-constant-fold* ( x insn -- y )
33
34 M: ##not unary-constant-fold* drop bitnot ;
35 M: ##neg unary-constant-fold* drop neg ;
36
37 : unary-constant-fold ( insn -- insn' )
38     [ dst>> ] [ [ src>> vreg>integer ] [ ] bi unary-constant-fold* ] bi
39     ##load-integer new-insn ; inline