]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/destruction/destruction.factor
compiler: cleanup cfg passes to have stack effect ( 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 combinators fry locals kernel
4 make namespaces sequences sequences.deep sets vectors
5 cpu.architecture
6 compiler.cfg.rpo
7 compiler.cfg.def-use
8 compiler.cfg.registers
9 compiler.cfg.dominance
10 compiler.cfg.instructions
11 compiler.cfg.liveness
12 compiler.cfg.ssa.cssa
13 compiler.cfg.ssa.destruction.leaders
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 ! Maps leaders to equivalence class elements.
37 SYMBOL: class-element-map
38
39 : class-elements ( vreg -- elts ) class-element-map get at ;
40
41 <PRIVATE
42
43 ! Sequence of vreg pairs
44 SYMBOL: copies
45
46 : value-of ( vreg -- value )
47     dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
48
49 : init-coalescing ( -- )
50     defs get
51     [ [ drop dup ] assoc-map leader-map set ]
52     [ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
53     V{ } clone copies set ;
54
55 : coalesce-leaders ( vreg1 vreg2 -- )
56     ! leader2 becomes the leader.
57     swap leader-map get set-at ;
58
59 : coalesce-elements ( merged vreg1 vreg2 -- )
60     ! delete leader1's class, and set leader2's class to merged.
61     class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
62
63 : coalesce-vregs ( merged leader1 leader2 -- )
64     [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
65
66 GENERIC: prepare-insn ( insn -- )
67
68 : maybe-eliminate-copy-later ( dst src -- )
69     2array copies get push ;
70
71 M: insn prepare-insn drop ;
72
73 M: alien-call-insn prepare-insn drop ;
74
75 M: vreg-insn prepare-insn
76     [ temp-vregs [ leader-map get conjoin ] each ]
77     [
78         [ defs-vregs ] [ uses-vregs ] bi
79         2dup [ empty? not ] both? [
80             [ first ] bi@
81             2dup [ rep-of reg-class-of ] bi@ eq?
82             [ maybe-eliminate-copy-later ] [ 2drop ] if
83         ] [ 2drop ] if
84     ] bi ;
85
86 M: ##copy prepare-insn
87     [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
88
89 M: ##parallel-copy prepare-insn
90     values>> [ first2 maybe-eliminate-copy-later ] each ;
91
92 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
93     [ leader ] bi@ ;
94
95 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
96     [ class-elements ] bi@ sets-interfere? ;
97
98 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
99
100 :: must-eliminate-copy ( vreg1 vreg2 -- )
101     ! Eliminate a copy.
102     vreg1 vreg2 eq? [
103         vreg1 vreg2 vregs-interfere?
104         [ vreg1 vreg2 vregs-shouldn't-interfere ]
105         [ vreg1 vreg2 coalesce-vregs ]
106         if
107     ] unless ;
108
109 M: ##tagged>integer prepare-insn
110     [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
111
112 M: ##phi prepare-insn
113     [ dst>> ] [ inputs>> values ] bi
114     [ leaders must-eliminate-copy ] with each ;
115
116 : prepare-coalescing ( cfg -- )
117     init-coalescing
118     [ [ prepare-insn ] each ] simple-analysis ;
119
120 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
121     ! Eliminate a copy if possible.
122     vreg1 vreg2 eq? [
123         vreg1 vreg2 vregs-interfere?
124         [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
125     ] unless ;
126
127 : process-copies ( -- )
128     copies get [ leaders maybe-eliminate-copy ] assoc-each ;
129
130 GENERIC: cleanup-insn ( insn -- )
131
132 : useful-copy? ( insn -- ? )
133     [ dst>> ] [ src>> ] bi leaders eq? not ; inline
134
135 M: ##copy cleanup-insn
136     dup useful-copy? [ , ] [ drop ] if ;
137
138 M: ##parallel-copy cleanup-insn
139     values>>
140     [ first2 leaders 2array ] map [ first2 eq? not ] filter
141     [ parallel-copy-rep ] unless-empty ;
142
143 M: ##tagged>integer cleanup-insn
144     dup useful-copy? [ , ] [ drop ] if ;
145
146 M: ##phi cleanup-insn drop ;
147
148 M: insn cleanup-insn , ;
149
150 : cleanup-cfg ( cfg -- )
151     [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
152
153 PRIVATE>
154
155 : destruct-ssa ( cfg -- )
156     {
157         [ needs-dominance ]
158         [ construct-cssa ]
159         [ compute-defs ]
160         [ compute-insns ]
161         [ compute-live-sets ]
162         [ compute-live-ranges ]
163         [ prepare-coalescing ]
164         [ drop process-copies ]
165         [ cleanup-cfg ]
166         [ compute-live-sets ]
167     } cleave ;