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.destruction.leaders
15 compiler.cfg.ssa.interference
16 compiler.cfg.ssa.interference.live-ranges
17 compiler.cfg.parallel-copy
18 compiler.cfg.utilities
20 FROM: namespaces => set ;
21 IN: compiler.cfg.ssa.destruction
23 ! Because of the design of the register allocator, this pass
24 ! has three peculiar properties.
26 ! 1) Instead of renaming vreg usages in the CFG, a map from
27 ! vregs to canonical representatives is computed. This allows
28 ! the register allocator to use the original SSA names to get
29 ! reaching definitions.
30 ! 2) Useless ##copy instructions, and all ##phi instructions,
31 ! are eliminated, so the register allocator does not have to
32 ! remove any redundant operations.
33 ! 3) This pass computes live sets and fills out GC maps with
34 ! compiler.cfg.liveness, so the linear scan register allocator
35 ! does not need to compute liveness again.
37 ! Maps leaders to equivalence class elements.
38 SYMBOL: class-element-map
40 : class-elements ( vreg -- elts ) class-element-map get at ;
44 ! Sequence of vreg pairs
47 : value-of ( vreg -- value )
48 dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
50 : init-coalescing ( -- )
52 [ [ drop dup ] assoc-map leader-map set ]
53 [ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
54 V{ } clone copies set ;
56 : coalesce-leaders ( vreg1 vreg2 -- )
57 ! leader2 becomes the leader.
58 swap leader-map get set-at ;
60 : coalesce-elements ( merged vreg1 vreg2 -- )
61 ! delete leader1's class, and set leader2's class to merged.
62 class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
64 : coalesce-vregs ( merged leader1 leader2 -- )
65 [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
67 GENERIC: prepare-insn ( insn -- )
69 : maybe-eliminate-copy-later ( dst src -- )
70 2array copies get push ;
72 M: insn prepare-insn drop ;
74 M: alien-call-insn prepare-insn drop ;
76 M: vreg-insn prepare-insn
77 [ temp-vregs [ leader-map get conjoin ] each ]
79 [ defs-vregs ] [ uses-vregs ] bi
80 2dup [ empty? not ] both? [
82 2dup [ rep-of reg-class-of ] bi@ eq?
83 [ maybe-eliminate-copy-later ] [ 2drop ] if
87 M: ##copy prepare-insn
88 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
90 M: ##parallel-copy prepare-insn
91 values>> [ first2 maybe-eliminate-copy-later ] each ;
93 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
96 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
97 [ class-elements ] bi@ sets-interfere? ;
99 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
101 :: must-eliminate-copy ( vreg1 vreg2 -- )
104 vreg1 vreg2 vregs-interfere?
105 [ vreg1 vreg2 vregs-shouldn't-interfere ]
106 [ vreg1 vreg2 coalesce-vregs ]
110 M: ##tagged>integer prepare-insn
111 [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
113 M: ##phi prepare-insn
114 [ dst>> ] [ inputs>> values ] bi
115 [ leaders must-eliminate-copy ] with each ;
117 : prepare-coalescing ( cfg -- )
119 [ [ prepare-insn ] each ] simple-analysis ;
121 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
122 ! Eliminate a copy if possible.
124 vreg1 vreg2 vregs-interfere?
125 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
128 : process-copies ( -- )
129 copies get [ leaders maybe-eliminate-copy ] assoc-each ;
131 GENERIC: cleanup-insn ( insn -- )
133 : useful-copy? ( insn -- ? )
134 [ dst>> ] [ src>> ] bi leaders eq? not ; inline
136 M: ##copy cleanup-insn
137 dup useful-copy? [ , ] [ drop ] if ;
139 M: ##parallel-copy cleanup-insn
141 [ first2 leaders 2array ] map [ first2 eq? not ] filter
142 [ parallel-copy-rep ] unless-empty ;
144 M: ##tagged>integer cleanup-insn
145 dup useful-copy? [ , ] [ drop ] if ;
147 M: ##phi cleanup-insn drop ;
149 M: insn cleanup-insn , ;
151 : cleanup-cfg ( cfg -- )
152 [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
156 : destruct-ssa ( cfg -- cfg' )
161 dup compute-live-sets
162 dup compute-live-ranges
163 dup prepare-coalescing
166 dup compute-live-sets ;