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
10 compiler.cfg.instructions
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
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.
36 ! Maps leaders to equivalence class elements.
37 SYMBOL: class-element-map
39 : class-elements ( vreg -- elts ) class-element-map get at ;
43 ! Sequence of vreg pairs
46 : value-of ( vreg -- value )
47 dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
49 : init-coalescing ( -- )
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 ;
55 : coalesce-leaders ( vreg1 vreg2 -- )
56 ! leader2 becomes the leader.
57 swap leader-map get set-at ;
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* ;
63 : coalesce-vregs ( merged leader1 leader2 -- )
64 [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
66 GENERIC: prepare-insn ( insn -- )
68 : maybe-eliminate-copy-later ( dst src -- )
69 2array copies get push ;
71 M: insn prepare-insn drop ;
73 M: alien-call-insn prepare-insn drop ;
75 M: vreg-insn prepare-insn
76 [ temp-vregs [ leader-map get conjoin ] each ]
78 [ defs-vregs ] [ uses-vregs ] bi
79 2dup [ empty? not ] both? [
81 2dup [ rep-of reg-class-of ] bi@ eq?
82 [ maybe-eliminate-copy-later ] [ 2drop ] if
86 M: ##copy prepare-insn
87 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
89 M: ##parallel-copy prepare-insn
90 values>> [ first2 maybe-eliminate-copy-later ] each ;
92 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
95 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
96 [ class-elements ] bi@ sets-interfere? ;
98 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
100 :: must-eliminate-copy ( vreg1 vreg2 -- )
103 vreg1 vreg2 vregs-interfere?
104 [ vreg1 vreg2 vregs-shouldn't-interfere ]
105 [ vreg1 vreg2 coalesce-vregs ]
109 M: ##tagged>integer prepare-insn
110 [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
112 M: ##phi prepare-insn
113 [ dst>> ] [ inputs>> values ] bi
114 [ leaders must-eliminate-copy ] with each ;
116 : prepare-coalescing ( cfg -- )
118 [ [ prepare-insn ] each ] simple-analysis ;
120 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
121 ! Eliminate a copy if possible.
123 vreg1 vreg2 vregs-interfere?
124 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
127 : process-copies ( -- )
128 copies get [ leaders maybe-eliminate-copy ] assoc-each ;
130 GENERIC: cleanup-insn ( insn -- )
132 : useful-copy? ( insn -- ? )
133 [ dst>> ] [ src>> ] bi leaders eq? not ; inline
135 M: ##copy cleanup-insn
136 dup useful-copy? [ , ] [ drop ] if ;
138 M: ##parallel-copy cleanup-insn
140 [ first2 leaders 2array ] map [ first2 eq? not ] filter
141 [ parallel-copy-rep ] unless-empty ;
143 M: ##tagged>integer cleanup-insn
144 dup useful-copy? [ , ] [ drop ] if ;
146 M: ##phi cleanup-insn drop ;
148 M: insn cleanup-insn , ;
150 : cleanup-cfg ( cfg -- )
151 [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
155 : destruct-ssa ( cfg -- )
161 [ compute-live-sets ]
162 [ compute-live-ranges ]
163 [ prepare-coalescing ]
164 [ drop process-copies ]
166 [ compute-live-sets ]