]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/destruction/destruction.factor
compiler.cfg.ssa.detruction: coalesce different representations in more cases
[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
80     [ temp-vregs [ leader-map get conjoin ] each ]
81     [
82         [ defs-vreg ] [ uses-vregs ] bi
83         2dup empty? not and [
84             first
85             2dup [ rep-of reg-class-of ] bi@ eq?
86             [ try-to-coalesce ] [ 2drop ] if
87         ] [ 2drop ] if
88     ] bi ;
89
90 M: ##copy prepare-insn
91     [ dst>> ] [ src>> ] bi try-to-coalesce ;
92
93 M: ##tagged>integer prepare-insn
94     [ dst>> ] [ src>> ] bi eliminate-copy ;
95
96 M: ##phi prepare-insn
97     [ dst>> ] [ inputs>> values ] bi
98     [ eliminate-copy ] with each ;
99
100 : prepare-block ( bb -- )
101     instructions>> [ prepare-insn ] each ;
102
103 : prepare-coalescing ( cfg -- )
104     init-coalescing
105     [ prepare-block ] each-basic-block ;
106
107 : process-copies ( -- )
108     copies get [
109         2dup classes-interfere?
110         [ 2drop ] [ eliminate-copy ] if
111     ] assoc-each ;
112
113 GENERIC: useful-insn? ( insn -- ? )
114
115 : useful-copy? ( insn -- ? )
116     [ dst>> leader ] [ src>> leader ] bi eq? not ; inline
117
118 M: ##copy useful-insn? useful-copy? ;
119
120 M: ##tagged>integer useful-insn? useful-copy? ;
121
122 M: ##phi useful-insn? drop f ;
123
124 M: insn useful-insn? drop t ;
125
126 : cleanup-block ( bb -- )
127     instructions>> [ useful-insn? ] filter! drop ;
128
129 : cleanup-cfg ( cfg -- )
130     [ cleanup-block ] each-basic-block ;
131
132 PRIVATE>
133
134 : destruct-ssa ( cfg -- cfg' )
135     needs-dominance
136
137     dup construct-cssa
138     dup compute-defs
139     dup compute-ssa-live-sets
140     dup compute-live-ranges
141     dup prepare-coalescing
142     process-copies
143     dup cleanup-cfg ;