1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces arrays assocs hashtables kernel accessors fry
4 grouping sorting sets sequences locals
10 compiler.cfg.utilities
11 compiler.cfg.instructions
12 compiler.cfg.predecessors
13 compiler.cfg.gvn.alien
14 compiler.cfg.gvn.avail
15 compiler.cfg.gvn.comparisons
16 compiler.cfg.gvn.graph
18 compiler.cfg.gvn.rewrite
19 compiler.cfg.gvn.slots
21 compiler.cfg.gvn.expressions
22 compiler.cfg.gvn.redundancy-elimination ;
25 GENERIC: process-instruction ( insn -- insn' )
27 : redundant-instruction ( insn vn -- insn' )
28 [ dst>> ] dip [ swap set-vn ] [ <copy> ] 2bi ;
30 :: useful-instruction ( insn expr -- insn' )
33 vn expr exprs>vns get set-at
34 insn vn vns>insns get set-at
35 vn vn basic-block get bbs>defns get [ ?set-at ] change-at
38 : check-redundancy ( insn -- insn' )
39 dup >expr dup exprs>vns get at
40 [ redundant-instruction ] [ useful-instruction ] ?if ;
42 M: insn process-instruction
44 [ process-instruction ]
45 [ dup defs-vregs length 1 = [ check-redundancy ] when ] ?if ;
47 M: ##copy process-instruction
48 dup [ src>> vreg>vn ] [ dst>> ] bi set-vn ;
50 M: array process-instruction
51 [ process-instruction ] map ;
53 : value-numbering-step ( insns -- insns' )
54 [ process-instruction ] map flatten ;
56 : value-numbering-iteration ( cfg -- )
58 [ value-numbering-step drop ] simple-analysis ;
60 : identify-redundancies ( cfg -- )
65 _ value-numbering-iteration
69 : value-numbering ( cfg -- cfg )
72 dup identify-redundancies
73 dup eliminate-redundancies
75 cfg-changed predecessors-changed ;