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
10 compiler.cfg.dominance
11 compiler.cfg.instructions
14 compiler.cfg.ssa.interference
15 compiler.cfg.ssa.interference.live-ranges
16 compiler.cfg.parallel-copy
17 compiler.cfg.utilities
19 FROM: namespaces => set ;
20 IN: compiler.cfg.ssa.destruction
22 ! Because of the design of the register allocator, this pass
23 ! has three peculiar properties.
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.
38 : leader ( vreg -- vreg' ) leader-map get compress-path ;
40 ! Maps leaders to equivalence class elements.
41 SYMBOL: class-element-map
43 : class-elements ( vreg -- elts ) class-element-map get at ;
47 ! Sequence of vreg pairs
50 : value-of ( vreg -- value )
51 dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
53 : init-coalescing ( -- )
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 ;
59 : coalesce-leaders ( vreg1 vreg2 -- )
60 ! leader2 becomes the leader.
61 swap leader-map get set-at ;
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* ;
67 : coalesce-vregs ( merged leader1 leader2 -- )
68 [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
70 GENERIC: prepare-insn ( insn -- )
72 : maybe-eliminate-copy-later ( dst src -- )
73 2array copies get push ;
75 M: insn prepare-insn drop ;
77 M: vreg-insn prepare-insn
78 [ temp-vregs [ leader-map get conjoin ] each ]
80 [ defs-vregs ] [ uses-vregs ] bi
81 2dup [ empty? not ] both? [
83 2dup [ rep-of reg-class-of ] bi@ eq?
84 [ maybe-eliminate-copy-later ] [ 2drop ] if
88 M: ##copy prepare-insn
89 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
91 M: ##parallel-copy prepare-insn
92 values>> [ first2 maybe-eliminate-copy-later ] each ;
94 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
97 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
98 [ class-elements ] bi@ sets-interfere? ;
100 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
102 :: must-eliminate-copy ( vreg1 vreg2 -- )
105 vreg1 vreg2 vregs-interfere?
106 [ vreg1 vreg2 vregs-shouldn't-interfere ]
107 [ vreg1 vreg2 coalesce-vregs ]
111 M: ##tagged>integer prepare-insn
112 [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
114 M: ##phi prepare-insn
115 [ dst>> ] [ inputs>> values ] bi
116 [ leaders must-eliminate-copy ] with each ;
118 : prepare-coalescing ( cfg -- )
120 [ [ prepare-insn ] each ] simple-analysis ;
122 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
123 ! Eliminate a copy if possible.
125 vreg1 vreg2 vregs-interfere?
126 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
129 : process-copies ( -- )
130 copies get [ leaders maybe-eliminate-copy ] assoc-each ;
132 GENERIC: cleanup-insn ( insn -- )
134 : useful-copy? ( insn -- ? )
135 [ dst>> ] [ src>> ] bi leaders eq? not ; inline
137 M: ##copy cleanup-insn
138 dup useful-copy? [ , ] [ drop ] if ;
140 M: ##parallel-copy cleanup-insn
142 [ first2 leaders 2array ] map [ first2 eq? not ] filter
143 [ parallel-copy-rep ] unless-empty ;
145 M: ##tagged>integer cleanup-insn
146 dup useful-copy? [ , ] [ drop ] if ;
148 M: ##phi cleanup-insn drop ;
150 M: insn cleanup-insn , ;
152 : cleanup-cfg ( cfg -- )
153 [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
157 : destruct-ssa ( cfg -- cfg' )
163 dup compute-live-sets
164 dup compute-live-ranges
165 dup prepare-coalescing
168 dup compute-live-sets ;