1 ! Copyright (C) 2008, 2010 Slava Pestov, 2011 Alex Vondrak.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs kernel namespaces ;
4 IN: compiler.cfg.gvn.graph
6 SYMBOL: input-expr-counter
8 ! assoc mapping vregs to value numbers
9 ! this is the identity on canonical representatives
12 ! assoc mapping expressions to value numbers
15 ! assoc mapping value numbers to instructions
18 ! assoc mapping each value number to a sequence of vregs
19 ! sharing that value number (i.e., the congruence class)
22 ! boolean to track whether vregs>vns changes
25 ! boolean to track when it's safe to alter the CFG in a rewrite
26 ! method (i.e., after vregs>vns stops changing)
27 SYMBOL: final-iteration?
29 : vn>insn ( vn -- insn ) vns>insns get at ;
31 : vreg>vn ( vreg -- vn ) vregs>vns get at ;
33 : set-vn ( vn vreg -- )
34 vregs>vns get maybe-set-at [ changed? on ] when ;
36 : vreg>insn ( vreg -- insn ) vreg>vn vn>insn ;
38 : congruence-class ( vreg -- vregs )
39 vreg>vn vns>vregs get at ;
42 exprs>vns get clear-assoc
43 vns>insns get clear-assoc ;
45 : compute-congruence-classes ( -- )
46 vregs>vns get H{ } clone [
47 [ push-at ] curry assoc-each
48 ] keep vns>vregs set ;
50 : init-value-graph ( -- )
51 0 input-expr-counter set
52 H{ } clone vregs>vns set
53 H{ } clone exprs>vns set
54 H{ } clone vns>insns set ;