1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces arrays assocs kernel accessors fry grouping
4 sorting sets sequences locals
10 compiler.cfg.utilities
11 compiler.cfg.instructions
12 compiler.cfg.gvn.alien
13 compiler.cfg.gvn.comparisons
14 compiler.cfg.gvn.graph
16 compiler.cfg.gvn.rewrite
17 compiler.cfg.gvn.slots
19 compiler.cfg.gvn.expressions ;
22 GENERIC: process-instruction ( insn -- insn' )
24 : redundant-instruction ( insn vn -- insn' )
25 [ dst>> ] dip [ swap set-vn ] [ <copy> ] 2bi ;
27 :: useful-instruction ( insn expr -- insn' )
30 vn expr exprs>vns get set-at
31 insn vn vns>insns get set-at
34 : check-redundancy ( insn -- insn' )
35 dup >expr dup exprs>vns get at
36 [ redundant-instruction ] [ useful-instruction ] ?if ;
38 M: insn process-instruction
39 dup rewrite [ process-instruction ] [ ] ?if ;
41 M: foldable-insn process-instruction
43 [ process-instruction ]
44 [ dup defs-vregs length 1 = [ check-redundancy ] when ] ?if ;
46 M: ##copy process-instruction
47 dup [ src>> vreg>vn ] [ dst>> ] bi set-vn ;
50 [ dst>> ] [ inputs>> values [ vreg>vn ] map ] bi
55 [ first <copy> ] if-empty
58 M: ##phi process-instruction
60 [ process-instruction ] [ check-redundancy ] ?if ;
63 inputs>> values [ vreg>vn ] map \ ##phi prefix ;
65 M: array process-instruction
66 [ process-instruction ] map ;
68 : value-numbering-step ( insns -- insns' )
70 ! [ process-instruction ] map flatten ;
72 ! idea: let rewrite do the constant/copy propagation (as
73 ! that eventually leads to better VNs), but don't actually
74 ! use them here, since changing the CFG mid-optimistic-GVN
76 dup [ process-instruction drop ] each ;
78 : value-numbering-iteration ( cfg -- )
79 [ value-numbering-step ] simple-optimization ;
81 : value-numbering ( cfg -- cfg )
86 _ value-numbering-iteration
90 dup [ init-value-graph [ process-instruction ] map flatten ] simple-optimization
91 cfg-changed predecessors-changed ;