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