]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/destruction/destruction.factor
compiler.cfg.ssa.cssa, compiler.cfg.ssa.destruction, compiler.utilities: docs & tests
[factor.git] / basis / compiler / cfg / ssa / destruction / destruction.factor
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
13
14 ! Maps leaders to equivalence class elements.
15 SYMBOL: class-element-map
16
17 : class-elements ( vreg -- elts ) class-element-map get at ;
18
19 <PRIVATE
20
21 ! Sequence of vreg pairs
22 SYMBOL: copies
23
24 : value-of ( vreg -- value )
25     dup insn-of dup ##tagged>integer? [ nip src>> ] [ drop ] if ;
26
27 : init-coalescing ( -- )
28     defs get
29     [ [ drop dup ] assoc-map leader-map set ]
30     [ [ [ dup dup value-of ] dip <vreg-info> 1array ] assoc-map class-element-map set ] bi
31     V{ } clone copies set ;
32
33 : coalesce-leaders ( vreg1 vreg2 -- )
34     ! leader2 becomes the leader.
35     swap leader-map get set-at ;
36
37 : coalesce-elements ( merged vreg1 vreg2 -- )
38     ! delete leader1's class, and set leader2's class to merged.
39     class-element-map get [ delete-at ] [ set-at ] bi-curry bi* ;
40
41 : coalesce-vregs ( merged leader1 leader2 -- )
42     [ coalesce-leaders ] [ coalesce-elements ] 2bi ;
43
44 GENERIC: prepare-insn ( insn -- )
45
46 : maybe-eliminate-copy-later ( dst src -- )
47     2array copies get push ;
48
49 M: insn prepare-insn drop ;
50
51 M: alien-call-insn prepare-insn drop ;
52
53 M: vreg-insn prepare-insn
54     [ temp-vregs [ leader-map get conjoin ] each ]
55     [
56         [ defs-vregs ] [ uses-vregs ] bi
57         2dup [ empty? not ] both? [
58             [ first ] bi@
59             2dup [ rep-of reg-class-of ] bi@ eq?
60             [ maybe-eliminate-copy-later ] [ 2drop ] if
61         ] [ 2drop ] if
62     ] bi ;
63
64 M: ##copy prepare-insn
65     [ dst>> ] [ src>> ] bi maybe-eliminate-copy-later ;
66
67 M: ##parallel-copy prepare-insn
68     values>> [ first2 maybe-eliminate-copy-later ] each ;
69
70 : leaders ( vreg1 vreg2 -- vreg1' vreg2' )
71     [ leader ] bi@ ;
72
73 : vregs-interfere? ( vreg1 vreg2 -- merged/f ? )
74     [ class-elements ] bi@ sets-interfere? ;
75
76 ERROR: vregs-shouldn't-interfere vreg1 vreg2 ;
77
78 :: must-eliminate-copy ( vreg1 vreg2 -- )
79     ! Eliminate a copy.
80     vreg1 vreg2 eq? [
81         vreg1 vreg2 vregs-interfere?
82         [ vreg1 vreg2 vregs-shouldn't-interfere ]
83         [ vreg1 vreg2 coalesce-vregs ]
84         if
85     ] unless ;
86
87 M: ##tagged>integer prepare-insn
88     [ dst>> ] [ src>> ] bi leaders must-eliminate-copy ;
89
90 M: ##phi prepare-insn
91     [ dst>> ] [ inputs>> values ] bi
92     [ leaders must-eliminate-copy ] with each ;
93
94 : prepare-coalescing ( cfg -- )
95     init-coalescing
96     [ [ prepare-insn ] each ] simple-analysis ;
97
98 :: maybe-eliminate-copy ( vreg1 vreg2 -- )
99     ! Eliminate a copy if possible.
100     vreg1 vreg2 eq? [
101         vreg1 vreg2 vregs-interfere?
102         [ drop ] [ vreg1 vreg2 coalesce-vregs ] if
103     ] unless ;
104
105 : process-copies ( -- )
106     copies get [ leaders maybe-eliminate-copy ] assoc-each ;
107
108 GENERIC: cleanup-insn ( insn -- )
109
110 : useful-copy? ( insn -- ? )
111     [ dst>> ] [ src>> ] bi leaders eq? not ; inline
112
113 M: ##copy cleanup-insn
114     dup useful-copy? [ , ] [ drop ] if ;
115
116 M: ##parallel-copy cleanup-insn
117     values>>
118     [ first2 leaders 2array ] map [ first2 eq? not ] filter
119     [ parallel-copy-rep % ] unless-empty ;
120
121 M: ##tagged>integer cleanup-insn
122     dup useful-copy? [ , ] [ drop ] if ;
123
124 M: ##phi cleanup-insn drop ;
125
126 M: insn cleanup-insn , ;
127
128 : cleanup-cfg ( cfg -- )
129     [ [ [ cleanup-insn ] each ] V{ } make ] simple-optimization ;
130
131 PRIVATE>
132
133 : destruct-ssa ( cfg -- )
134     {
135         [ needs-dominance ]
136         [ construct-cssa ]
137         [ compute-defs ]
138         [ compute-insns ]
139         [ compute-live-sets ]
140         [ compute-live-ranges ]
141         [ prepare-coalescing ]
142         [ drop process-copies ]
143         [ cleanup-cfg ]
144         [ compute-live-sets ]
145     } cleave ;