]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/destruction/destruction.factor
compiler.cfg.ssa.destruction: alien-call-insns are too hairy to coalesce (issue #22)
[factor.git] / basis / compiler / cfg / ssa / destruction / destruction.factor
1 ! Copyright (C) 2009, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs fry locals kernel make
4 namespaces 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
13 compiler.cfg.ssa.cssa
14 compiler.cfg.ssa.interference
15 compiler.cfg.ssa.interference.live-ranges
16 compiler.cfg.parallel-copy
17 compiler.cfg.utilities
18 compiler.utilities ;
19 FROM: namespaces => set ;
20 IN: compiler.cfg.ssa.destruction
21
22 ! Because of the design of the register allocator, this pass
23 ! has three peculiar properties.
24 !
25 ! 1) Instead of renaming vreg usages in the CFG, a map from
26 ! vregs to canonical representatives is computed. This allows
27 ! the register allocator to use the original SSA names to get
28 ! reaching definitions.
29 ! 2) Useless ##copy instructions, and all ##phi instructions,
30 ! are eliminated, so the register allocator does not have to
31 ! remove any redundant operations.
32 ! 3) This pass computes live sets and fills out GC maps with
33 ! compiler.cfg.liveness, so the linear scan register allocator
34 ! does not need to compute liveness again.
35
36 SYMBOL: leader-map
37
38 : leader ( vreg -- vreg' ) leader-map get compress-path ;
39
40 ! Maps leaders to equivalence class elements.
41 SYMBOL: class-element-map
42
43 : class-elements ( vreg -- elts ) class-element-map get at ;
44
45 <PRIVATE
46
47 ! Sequence of vreg pairs
48 SYMBOL: copies
49
50 : value-of ( vreg -- value )
51     dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
52
53 : init-coalescing ( -- )
54     defs get
55     [ [ drop dup ] assoc-map leader-map set ]
56     [ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
57     V{ } clone copies set ;
58
59 : coalesce-leaders ( vreg1 vreg2 -- )
60     ! leader2 becomes the leader.
61     swap leader-map get set-at ;
62
63 : coalesce-elements ( merged vreg1 vreg2 -- )
64     ! delete leader1's class, and set leader2's class to merged.
65     class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
66
67 : coalesce-vregs ( merged leader1 leader2 -- )
68     [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
69
70 GENERIC: prepare-insn ( insn -- )
71
72 : maybe-eliminate-copy-later ( dst src -- )
73     2array copies get push ;
74
75 M: insn prepare-insn drop ;
76
77 M: alien-call-insn prepare-insn drop ;
78
79 M: vreg-insn prepare-insn
80     [ temp-vregs [ leader-map get conjoin ] each ]
81     [
82         [ defs-vregs ] [ uses-vregs ] bi
83         2dup [ empty? not ] both? [
84             [ first ] bi@
85             2dup [ rep-of reg-class-of ] bi@ eq?
86             [ maybe-eliminate-copy-later ] [ 2drop ] if
87         ] [ 2drop ] if
88     ] bi ;
89
90 M: ##copy prepare-insn
91     [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
92
93 M: ##parallel-copy prepare-insn
94     values>> [ first2 maybe-eliminate-copy-later ] each ;
95
96 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
97     [ leader ] bi@ ;
98
99 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
100     [ class-elements ] bi@ sets-interfere? ;
101
102 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
103
104 :: must-eliminate-copy ( vreg1 vreg2 -- )
105     ! Eliminate a copy.
106     vreg1 vreg2 eq? [
107         vreg1 vreg2 vregs-interfere?
108         [ vreg1 vreg2 vregs-shouldn't-interfere ]
109         [ vreg1 vreg2 coalesce-vregs ]
110         if
111     ] unless ;
112
113 M: ##tagged>integer prepare-insn
114     [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
115
116 M: ##phi prepare-insn
117     [ dst>> ] [ inputs>> values ] bi
118     [ leaders must-eliminate-copy ] with each ;
119
120 : prepare-coalescing ( cfg -- )
121     init-coalescing
122     [ [ prepare-insn ] each ] simple-analysis ;
123
124 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
125     ! Eliminate a copy if possible.
126     vreg1 vreg2 eq? [
127         vreg1 vreg2 vregs-interfere?
128         [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
129     ] unless ;
130
131 : process-copies ( -- )
132     copies get [ leaders maybe-eliminate-copy ] assoc-each ;
133
134 GENERIC: cleanup-insn ( insn -- )
135
136 : useful-copy? ( insn -- ? )
137     [ dst>> ] [ src>> ] bi leaders eq? not ; inline
138
139 M: ##copy cleanup-insn
140     dup useful-copy? [ , ] [ drop ] if ;
141
142 M: ##parallel-copy cleanup-insn
143     values>>
144     [ first2 leaders 2array ] map [ first2 eq? not ] filter
145     [ parallel-copy-rep ] unless-empty ;
146
147 M: ##tagged>integer cleanup-insn
148     dup useful-copy? [ , ] [ drop ] if ;
149
150 M: ##phi cleanup-insn drop ;
151
152 M: insn cleanup-insn , ;
153
154 : cleanup-cfg ( cfg -- )
155     [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
156
157 PRIVATE>
158
159 : destruct-ssa ( cfg -- cfg' )
160     needs-dominance
161
162     dup construct-cssa
163     dup compute-defs
164     dup compute-insns
165     dup compute-live-sets
166     dup compute-live-ranges
167     dup prepare-coalescing
168     process-copies
169     dup cleanup-cfg
170     dup compute-live-sets ;