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