]> gitweb.factorcode.org Git - factor.git/blob - extra/compiler/cfg/gvn/graph/graph.factor
compiler.cfg.gvn: refactor
[factor.git] / extra / compiler / cfg / gvn / graph / graph.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math namespaces assocs ;
4 IN: compiler.cfg.gvn.graph
5
6 SYMBOL: input-expr-counter
7
8 ! assoc mapping vregs to *optimistic* value numbers
9 ! initialized per iteration of global value numbering
10 ! this is the identity on canonical representatives
11 SYMBOL: vregs>vns
12
13 ! assoc mapping expressions to value numbers
14 SYMBOL: exprs>vns
15
16 ! assoc mapping value numbers to instructions
17 SYMBOL: vns>insns
18
19 ! assoc mapping vregs to value numbers
20 ! once this stops changing, we know the value numbers are sound
21 SYMBOL: valid-vns
22
23 ! boolean to track whether valid-vns changes
24 SYMBOL: changed?
25
26 : vn>insn ( vn -- insn ) vns>insns get at ;
27
28 : vreg>vn ( vreg -- vn ) valid-vns get at ;
29
30 : optimistic-vn ( default-vn vreg -- vn )
31     vregs>vns get ?at
32     [ nip ]
33     [ dupd vregs>vns get set-at ] if ;
34
35 : set-vn ( default-vn vreg -- )
36     [ optimistic-vn ] keep
37     valid-vns get maybe-set-at [ changed? on ] when ;
38
39 : vreg>insn ( vreg -- insn ) vreg>vn vn>insn ;
40
41 : clear-optimistic-value-graph ( -- )
42     vregs>vns get clear-assoc
43     exprs>vns get clear-assoc
44     vns>insns get clear-assoc ;
45
46 : init-value-graph ( -- )
47     0 input-expr-counter set
48     H{ } clone valid-vns set
49     H{ } clone vregs>vns set
50     H{ } clone exprs>vns set
51     H{ } clone vns>insns set ;