1 ! Copyright (C) 2011 Alex Vondrak.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs compiler.cfg compiler.cfg.graphviz
4 compiler.cfg.gvn compiler.cfg.gvn.expressions compiler.cfg.gvn.graph
5 compiler.cfg.optimizer compiler.cfg.utilities compiler.test
6 continuations formatting graphviz.notation graphviz.render
7 io.directories kernel math.parser namespaces prettyprint sequences
8 sorting splitting tools.annotations ;
9 IN: compiler.cfg.gvn.testing
11 GENERIC: expr>str ( expr -- str )
13 M: integer-expr expr>str value>> number>string ;
15 M: reference-expr expr>str value>> unparse ;
17 M: sequence expr>str [ unparse ] map " " join ;
19 M: object expr>str unparse ;
21 : value-mapping ( from to -- str )
22 over exprs>vns get value-at* [
23 expr>str "%d -> <%d> (%s)\\l" sprintf
25 drop "%d -> <%d>\\l" sprintf
29 vregs>vns get >alist natural-sort [
33 : invert-assoc ( assoc -- inverted )
35 [ push-at ] curry assoc-each
38 : congruence-classes ( -- str )
39 vregs>vns get invert-assoc >alist natural-sort [
41 natural-sort [ number>string ] map ", " join
42 over exprs>vns get value-at expr>str
43 "<%d> : {%s} (%s)\\l" sprintf
46 : basic-block# ( -- n )
47 basic-block get number>> ;
49 : add-gvns ( graph -- graph' )
51 congruence-classes =label
54 "gvns" 0 [add-edge "invis" =style ];
55 basic-block# [add-node "bold" =style ];
60 : iteration-dir ( -- path )
61 iteration get number>string "gvn-iter" prepend ;
63 : new-iteration ( -- )
64 iteration inc iteration-dir make-directories ;
66 : draw-annotated-cfg ( -- )
68 cfg get cfgviz add-gvns
69 basic-block# number>string "bb" prepend svg
73 \ value-numbering-iteration
74 [ [ new-iteration ] prepend ] annotate
75 \ value-numbering-step
76 [ [ draw-annotated-cfg ] append ] annotate ;
79 \ value-numbering-iteration reset
80 \ value-numbering-step reset ;
82 ! Replace compiler.cfg.value-numbering:value-numbering with
83 ! compiler.cfg.gvn:value-numbering
85 : gvn-passes ( -- passes )
86 \ optimize-cfg def>> [
87 name>> "value-numbering" =
88 ] split-when [ value-numbering ] join ;
90 : test-gvn ( path quot -- )
92 0 iteration [ watch-optimizer* ] with-variable
95 : watch-gvn ( path quot -- )
96 annotate-gvn [ test-gvn ] [ reset-gvn ] finally ;
98 : watch-gvn-cfg ( path cfg -- )
100 { value-numbering } passes [
101 0 iteration [ watch-cfg ] with-variable
103 ] [ reset-gvn ] finally ;
105 : watch-gvn-bb ( path insns -- )
106 0 test-bb 0 get block>cfg watch-gvn-cfg ;