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