]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/destruction/destruction.factor
compiler.cfg: more silly optimizations
[factor.git] / basis / compiler / cfg / ssa / destruction / destruction.factor
1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs fry kernel namespaces
4 sequences sequences.deep
5 sets vectors
6 cpu.architecture
7 compiler.cfg.rpo
8 compiler.cfg.def-use
9 compiler.cfg.registers
10 compiler.cfg.dominance
11 compiler.cfg.instructions
12 compiler.cfg.liveness.ssa
13 compiler.cfg.ssa.cssa
14 compiler.cfg.ssa.interference
15 compiler.cfg.ssa.interference.live-ranges
16 compiler.cfg.utilities
17 compiler.utilities ;
18 FROM: namespaces => set ;
19 IN: compiler.cfg.ssa.destruction
20
21 ! Because of the design of the register allocator, this pass
22 ! has three peculiar properties.
23 !
24 ! 1) Instead of renaming vreg usages in the CFG, a map from
25 ! vregs to canonical representatives is computed. This allows
26 ! the register allocator to use the original SSA names to get
27 ! reaching definitions.
28 ! 2) Useless ##copy instructions, and all ##phi instructions,
29 ! are eliminated, so the register allocator does not have to
30 ! remove any redundant operations.
31 ! 3) A side effect of running this pass is that SSA liveness
32 ! information is computed, so the register allocator does not
33 ! need to compute it again.
34
35 SYMBOL: leader-map
36
37 : leader ( vreg -- vreg' ) leader-map get compress-path ;
38
39 ! Maps leaders to equivalence class elements.
40 SYMBOL: class-element-map
41
42 : class-elements ( vreg -- elts ) class-element-map get at ;
43
44 <PRIVATE
45
46 ! Sequence of vreg pairs
47 SYMBOL: copies
48
49 : init-coalescing ( -- )
50     defs get keys
51     [ [ dup ] H{ } map>assoc leader-map set ]
52     [ [ dup 1vector ] H{ } map>assoc class-element-map set ] bi
53     V{ } clone copies set ;
54
55 : classes-interfere? ( vreg1 vreg2 -- ? )
56     [ leader ] bi@ 2dup eq? [ 2drop f ] [
57         [ class-elements flatten ] bi@ sets-interfere?
58     ] if ;
59
60 : update-leaders ( vreg1 vreg2 -- )
61     swap leader-map get set-at ;
62
63 : merge-classes ( vreg1 vreg2 -- )
64     [ [ class-elements ] bi@ push ]
65     [ drop class-element-map get delete-at ] 2bi ;
66
67 : eliminate-copy ( vreg1 vreg2 -- )
68     [ leader ] bi@
69     2dup eq? [ 2drop ] [
70         [ update-leaders ]
71         [ merge-classes ]
72         2bi
73     ] if ;
74
75 GENERIC: prepare-insn ( insn -- )
76
77 : try-to-coalesce ( dst src -- ) 2array copies get push ;
78
79 M: insn prepare-insn drop ;
80
81 M: vreg-insn prepare-insn
82     [ temp-vregs [ leader-map get conjoin ] each ]
83     [
84         [ defs-vreg ] [ uses-vregs ] bi
85         2dup empty? not and [
86             first
87             2dup [ rep-of reg-class-of ] bi@ eq?
88             [ try-to-coalesce ] [ 2drop ] if
89         ] [ 2drop ] if
90     ] bi ;
91
92 M: ##copy prepare-insn
93     [ dst>> ] [ src>> ] bi try-to-coalesce ;
94
95 M: ##tagged>integer prepare-insn
96     [ dst>> ] [ src>> ] bi eliminate-copy ;
97
98 M: ##phi prepare-insn
99     [ dst>> ] [ inputs>> values ] bi
100     [ eliminate-copy ] with each ;
101
102 : prepare-block ( bb -- )
103     instructions>> [ prepare-insn ] each ;
104
105 : prepare-coalescing ( cfg -- )
106     init-coalescing
107     [ prepare-block ] each-basic-block ;
108
109 : process-copies ( -- )
110     copies get [
111         2dup classes-interfere?
112         [ 2drop ] [ eliminate-copy ] if
113     ] assoc-each ;
114
115 GENERIC: useful-insn? ( insn -- ? )
116
117 : useful-copy? ( insn -- ? )
118     [ dst>> leader ] [ src>> leader ] bi eq? not ; inline
119
120 M: ##copy useful-insn? useful-copy? ;
121
122 M: ##tagged>integer useful-insn? useful-copy? ;
123
124 M: ##phi useful-insn? drop f ;
125
126 M: insn useful-insn? drop t ;
127
128 : cleanup-cfg ( cfg -- )
129     [ [ useful-insn? ] filter! ] simple-optimization ;
130
131 PRIVATE>
132
133 : destruct-ssa ( cfg -- cfg' )
134     needs-dominance
135
136     dup construct-cssa
137     dup compute-defs
138     dup compute-ssa-live-sets
139     dup compute-live-ranges
140     dup prepare-coalescing
141     process-copies
142     dup cleanup-cfg ;