1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs fry locals kernel namespaces
4 sequences sequences.deep
10 compiler.cfg.dominance
11 compiler.cfg.instructions
12 compiler.cfg.liveness.ssa
14 compiler.cfg.ssa.interference
15 compiler.cfg.ssa.interference.live-ranges
16 compiler.cfg.utilities
18 FROM: namespaces => set ;
19 IN: compiler.cfg.ssa.destruction
21 ! Because of the design of the register allocator, this pass
22 ! has three peculiar properties.
24 ! 1) Instead of renaming vreg usages in the CFG, a map from
25 ! vregs to canonical representatives is computed. This allows
26 ! the register allocator to use the original SSA names to get
27 ! reaching definitions.
28 ! 2) Useless ##copy instructions, and all ##phi instructions,
29 ! are eliminated, so the register allocator does not have to
30 ! remove any redundant operations.
31 ! 3) A side effect of running this pass is that SSA liveness
32 ! information is computed, so the register allocator does not
33 ! need to compute it again.
37 : leader ( vreg -- vreg' ) leader-map get compress-path ;
39 ! Maps leaders to equivalence class elements.
40 SYMBOL: class-element-map
42 : class-elements ( vreg -- elts ) class-element-map get at ;
46 ! Sequence of vreg pairs
49 : value-of ( vreg -- value )
50 dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
52 : init-coalescing ( -- )
54 [ [ drop dup ] assoc-map leader-map set ]
55 [ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
56 V{ } clone copies set ;
58 : coalesce-leaders ( vreg1 vreg2 -- )
59 ! leader2 becomes the leader.
60 swap leader-map get set-at ;
62 : coalesce-elements ( merged vreg1 vreg2 -- )
63 ! delete leader1's class, and set leader2's class to merged.
64 class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
66 : coalesce-vregs ( merged leader1 leader2 -- )
67 [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
69 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
70 ! Eliminate a copy of possible.
74 vreg1 class-elements vreg2 class-elements sets-interfere?
75 [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
78 GENERIC: prepare-insn ( insn -- )
80 : maybe-eliminate-copy-later ( dst src -- )
81 2array copies get push ;
83 M: insn prepare-insn drop ;
85 M: vreg-insn prepare-insn
86 [ temp-vregs [ leader-map get conjoin ] each ]
88 [ defs-vregs ] [ uses-vregs ] bi
89 2dup [ empty? not ] both? [
91 2dup [ rep-of reg-class-of ] bi@ eq?
92 [ maybe-eliminate-copy-later ] [ 2drop ] if
96 M: ##copy prepare-insn
97 [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
99 M: ##tagged>integer prepare-insn
100 [ dst>> ] [ src>> ] bi maybe-eliminate-copy ;
102 M: ##phi prepare-insn
103 [ dst>> ] [ inputs>> values ] bi
104 [ maybe-eliminate-copy ] with each ;
106 : prepare-block ( bb -- )
107 instructions>> [ prepare-insn ] each ;
109 : prepare-coalescing ( cfg -- )
111 [ prepare-block ] each-basic-block ;
113 : process-copies ( -- )
114 copies get [ maybe-eliminate-copy ] assoc-each ;
116 GENERIC: useful-insn? ( insn -- ? )
118 : useful-copy? ( insn -- ? )
119 [ dst>> leader ] [ src>> leader ] bi eq? not ; inline
121 M: ##copy useful-insn? useful-copy? ;
123 M: ##tagged>integer useful-insn? useful-copy? ;
125 M: ##phi useful-insn? drop f ;
127 M: insn useful-insn? drop t ;
129 : cleanup-cfg ( cfg -- )
130 [ [ useful-insn? ] filter! ] simple-optimization ;
134 : destruct-ssa ( cfg -- cfg' )
140 dup compute-ssa-live-sets
141 dup compute-live-ranges
142 dup prepare-coalescing