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: alien-call-insn prepare-insn drop ;
79 M: vreg-insn prepare-insn
80 [ temp-vregs [ leader-map get conjoin ] each ]
82 [ defs-vregs ] [ uses-vregs ] bi
83 2dup [ empty? not ] both? [
85 2dup [ rep-of reg-class-of ] bi@ eq?
86 [ maybe-eliminate-copy-later ] [ 2drop ] if
90 M: ##copy prepare-insn
91 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
93 M: ##parallel-copy prepare-insn
94 values>> [ first2 maybe-eliminate-copy-later ] each ;
96 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
99 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
100 [ class-elements ] bi@ sets-interfere? ;
102 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
104 :: must-eliminate-copy ( vreg1 vreg2 -- )
107 vreg1 vreg2 vregs-interfere?
108 [ vreg1 vreg2 vregs-shouldn't-interfere ]
109 [ vreg1 vreg2 coalesce-vregs ]
113 M: ##tagged>integer prepare-insn
114 [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
116 M: ##phi prepare-insn
117 [ dst>> ] [ inputs>> values ] bi
118 [ leaders must-eliminate-copy ] with each ;
120 : prepare-coalescing ( cfg -- )
122 [ [ prepare-insn ] each ] simple-analysis ;
124 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
125 ! Eliminate a copy if possible.
127 vreg1 vreg2 vregs-interfere?
128 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
131 : process-copies ( -- )
132 copies get [ leaders maybe-eliminate-copy ] assoc-each ;
134 GENERIC: cleanup-insn ( insn -- )
136 : useful-copy? ( insn -- ? )
137 [ dst>> ] [ src>> ] bi leaders eq? not ; inline
139 M: ##copy cleanup-insn
140 dup useful-copy? [ , ] [ drop ] if ;
142 M: ##parallel-copy cleanup-insn
144 [ first2 leaders 2array ] map [ first2 eq? not ] filter
145 [ parallel-copy-rep ] unless-empty ;
147 M: ##tagged>integer cleanup-insn
148 dup useful-copy? [ , ] [ drop ] if ;
150 M: ##phi cleanup-insn drop ;
152 M: insn cleanup-insn , ;
154 : cleanup-cfg ( cfg -- )
155 [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
159 : destruct-ssa ( cfg -- cfg' )
165 dup compute-live-sets
166 dup compute-live-ranges
167 dup prepare-coalescing
170 dup compute-live-sets ;