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