]> gitweb.factorcode.org Git - factor.git/blob - extra/compiler/cfg/gvn/gvn.factor
compiler.cfg.gvn.redundancy-elimination: horrific tinkering that doesn't even work
[factor.git] / extra / compiler / cfg / gvn / gvn.factor
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
5 cpu.architecture
6 sequences.deep
7 compiler.cfg
8 compiler.cfg.rpo
9 compiler.cfg.def-use
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
17 compiler.cfg.gvn.math
18 compiler.cfg.gvn.rewrite
19 compiler.cfg.gvn.slots
20 compiler.cfg.gvn.misc
21 compiler.cfg.gvn.expressions
22 compiler.cfg.gvn.redundancy-elimination ;
23 IN: compiler.cfg.gvn
24
25 GENERIC: process-instruction ( insn -- insn' )
26
27 : redundant-instruction ( insn vn -- insn' )
28     [ dst>> ] dip [ swap set-vn ] [ <copy> ] 2bi ;
29
30 :: useful-instruction ( insn expr -- insn' )
31     insn dst>> :> vn
32     vn vn set-vn
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
36     insn ;
37
38 : check-redundancy ( insn -- insn' )
39     dup >expr dup exprs>vns get at
40     [ redundant-instruction ] [ useful-instruction ] ?if ;
41
42 M: insn process-instruction
43     dup rewrite
44     [ process-instruction ]
45     [ dup defs-vregs length 1 = [ check-redundancy ] when ] ?if ;
46
47 M: ##copy process-instruction
48     dup [ src>> vreg>vn ] [ dst>> ] bi set-vn ;
49
50 M: array process-instruction
51     [ process-instruction ] map ;
52
53 : value-numbering-step ( insns -- insns' )
54     [ process-instruction ] map flatten ;
55
56 : value-numbering-iteration ( cfg -- )
57     clear-exprs
58     [ value-numbering-step drop ] simple-analysis ;
59
60 : identify-redundancies ( cfg -- )
61     final-iteration? off
62     init-value-graph
63     '[
64         changed? off
65         _ value-numbering-iteration
66         changed? get
67     ] loop ;
68
69 : value-numbering ( cfg -- cfg )
70     needs-predecessors
71
72     dup identify-redundancies
73     dup eliminate-redundancies
74
75     cfg-changed predecessors-changed ;