1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes kernel math namespaces combinators
4 compiler.cfg.instructions compiler.cfg.value-numbering.graph ;
5 IN: compiler.cfg.value-numbering.expressions
7 ! Referentially-transparent expressions
9 TUPLE: unary-expr < expr in ;
10 TUPLE: binary-expr < expr in1 in2 ;
11 TUPLE: commutative-expr < binary-expr ;
12 TUPLE: compare-expr < binary-expr cc ;
13 TUPLE: constant-expr < expr value ;
15 : <constant> ( constant -- expr )
16 f swap constant-expr boa ; inline
18 M: constant-expr equal?
21 [ [ value>> class ] bi@ = ] 2bi
25 SYMBOL: input-expr-counter
27 : next-input-expr ( -- n )
28 input-expr-counter [ dup 1 + ] change ;
30 ! Expressions whose values are inputs to the basic block. We
31 ! can eliminate a second computation having the same 'n' as
32 ! the first one; we can also eliminate input-exprs whose
34 TUPLE: input-expr < expr n ;
36 : constant>vn ( constant -- vn ) <constant> expr>vn ; inline
38 GENERIC: >expr ( insn -- expr )
40 M: ##load-immediate >expr val>> <constant> ;
42 M: ##load-indirect >expr obj>> <constant> ;
45 [ class ] [ src>> vreg>vn ] bi unary-expr boa ;
48 [ class ] [ src1>> vreg>vn ] [ src2>> vreg>vn ] tri
52 [ class ] [ src1>> vreg>vn ] [ src2>> constant>vn ] tri
55 M: ##commutative >expr
56 [ class ] [ src1>> vreg>vn ] [ src2>> vreg>vn ] tri
57 commutative-expr boa ;
59 M: ##commutative-imm >expr
60 [ class ] [ src1>> vreg>vn ] [ src2>> constant>vn ] tri
61 commutative-expr boa ;
63 : compare>expr ( insn -- expr )
69 } cleave compare-expr boa ; inline
71 M: ##compare >expr compare>expr ;
73 : compare-imm>expr ( insn -- expr )
77 [ src2>> constant>vn ]
79 } cleave compare-expr boa ; inline
81 M: ##compare-imm >expr compare-imm>expr ;
83 M: ##compare-float >expr compare>expr ;
85 M: ##flushable >expr class next-input-expr input-expr boa ;
87 : init-expressions ( -- )
88 0 input-expr-counter set ;