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
14 ! Because of the design of the register allocator, this pass
15 ! has three peculiar properties.
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.
28 ! Maps leaders to equivalence class elements.
29 SYMBOL: class-element-map
31 : class-elements ( vreg -- elts ) class-element-map get at ;
35 ! Sequence of vreg pairs
38 : value-of ( vreg -- value )
39 dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
41 : init-coalescing ( -- )
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 ;
47 : coalesce-leaders ( vreg1 vreg2 -- )
48 ! leader2 becomes the leader.
49 swap leader-map get set-at ;
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* ;
55 : coalesce-vregs ( merged leader1 leader2 -- )
56 [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
58 GENERIC: prepare-insn ( insn -- )
60 : maybe-eliminate-copy-later ( dst src -- )
61 2array copies get push ;
63 M: insn prepare-insn drop ;
65 M: alien-call-insn prepare-insn drop ;
67 M: vreg-insn prepare-insn
68 [ temp-vregs [ leader-map get conjoin ] each ]
70 [ defs-vregs ] [ uses-vregs ] bi
71 2dup [ empty? not ] both? [
73 2dup [ rep-of reg-class-of ] bi@ eq?
74 [ maybe-eliminate-copy-later ] [ 2drop ] if
78 M: ##copy prepare-insn
79 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
81 M: ##parallel-copy prepare-insn
82 values>> [ first2 maybe-eliminate-copy-later ] each ;
84 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
87 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
88 [ class-elements ] bi@ sets-interfere? ;
90 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
92 :: must-eliminate-copy ( vreg1 vreg2 -- )
95 vreg1 vreg2 vregs-interfere?
96 [ vreg1 vreg2 vregs-shouldn't-interfere ]
97 [ vreg1 vreg2 coalesce-vregs ]
101 M: ##tagged>integer prepare-insn
102 [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
104 M: ##phi prepare-insn
105 [ dst>> ] [ inputs>> values ] bi
106 [ leaders must-eliminate-copy ] with each ;
108 : prepare-coalescing ( cfg -- )
110 [ [ prepare-insn ] each ] simple-analysis ;
112 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
113 ! Eliminate a copy if possible.
115 vreg1 vreg2 vregs-interfere?
116 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
119 : process-copies ( -- )
120 copies get [ leaders maybe-eliminate-copy ] assoc-each ;
122 GENERIC: cleanup-insn ( insn -- )
124 : useful-copy? ( insn -- ? )
125 [ dst>> ] [ src>> ] bi leaders eq? not ; inline
127 M: ##copy cleanup-insn
128 dup useful-copy? [ , ] [ drop ] if ;
130 M: ##parallel-copy cleanup-insn
132 [ first2 leaders 2array ] map [ first2 eq? not ] filter
133 [ parallel-copy-rep ] unless-empty ;
135 M: ##tagged>integer cleanup-insn
136 dup useful-copy? [ , ] [ drop ] if ;
138 M: ##phi cleanup-insn drop ;
140 M: insn cleanup-insn , ;
142 : cleanup-cfg ( cfg -- )
143 [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
147 : destruct-ssa ( cfg -- )
153 [ compute-live-sets ]
154 [ compute-live-ranges ]
155 [ prepare-coalescing ]
156 [ drop process-copies ]
158 [ compute-live-sets ]