]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/copy-prop/copy-prop.factor
Switch to https urls
[factor.git] / basis / compiler / cfg / copy-prop / copy-prop.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs compiler.cfg.def-use
4 compiler.cfg.instructions compiler.cfg.predecessors
5 compiler.cfg.renaming compiler.cfg.rpo compiler.cfg.utilities
6 grouping kernel namespaces sequences ;
7 IN: compiler.cfg.copy-prop
8
9 <PRIVATE
10
11 SYMBOL: changed?
12
13 SYMBOL: copies
14
15 ! Initialized per-basic-block; a mapping from inputs to dst for
16 ! eliminating redundant ##phi instructions
17 SYMBOL: phis
18
19 : resolve ( vreg -- vreg )
20     copies get at ;
21
22 : (record-copy) ( dst src copies -- )
23     swapd maybe-set-at [ changed? on ] when ; inline
24
25 : record-copy ( dst src -- )
26     copies get (record-copy) ; inline
27
28 : record-copies ( seq -- )
29     copies get '[ dup _ (record-copy) ] each ; inline
30
31 GENERIC: visit-insn ( insn -- )
32
33 M: ##copy visit-insn
34     [ dst>> ] [ src>> resolve ] bi
35     [ record-copy ] [ drop ] if* ;
36
37 : useless-phi ( dst inputs -- ) first record-copy ;
38
39 : redundant-phi ( dst inputs -- ) phis get at record-copy ;
40
41 : record-phi ( dst inputs -- )
42     [ phis get set-at ] [ drop dup record-copy ] 2bi ;
43
44 M: ##phi visit-insn
45     [ dst>> ] [ inputs>> values [ resolve ] map ] bi
46     dup phis get key? [ redundant-phi ] [
47         dup sift
48         dup all-equal?
49         [ nip useless-phi ]
50         [ drop record-phi ] if
51     ] if ;
52
53 M: vreg-insn visit-insn
54     defs-vregs record-copies  ;
55
56 M: insn visit-insn drop ;
57
58 : (collect-copies) ( cfg -- )
59     [
60         phis get clear-assoc
61         [ visit-insn ] each
62     ] simple-analysis ;
63
64 : collect-copies ( cfg -- )
65     H{ } clone copies set
66     H{ } clone phis set
67     '[
68         changed? off
69         _ (collect-copies)
70         changed? get
71     ] loop ;
72
73 GENERIC: update-insn ( insn -- keep? )
74
75 M: ##copy update-insn drop f ;
76
77 M: ##phi update-insn
78     dup call-next-method drop
79     [ dst>> ] [ inputs>> values ] bi [ = not ] with any? ;
80
81 M: vreg-insn update-insn rename-insn-uses t ;
82
83 M: insn update-insn drop t ;
84
85 : rename-copies ( cfg -- )
86     copies get renamings set
87     [ [ update-insn ] filter! ] simple-optimization ;
88
89 PRIVATE>
90
91 ! Certain parts of the GVN pass may come together here and
92 ! sabotage the correctness of the CFG:
93 !
94 !   1) compiler.cfg.gvn.comparisons:fold-branch may remove some
95 !   predecessors of a block (hence predecessors-changed at the
96 !   end of compiler.cfg.gvn:value-numbering).
97 !
98 !   2) At the moment in compiler.cfg.gvn:value-numbering,
99 !   ##phis with equivalent inputs (i.e., identical value
100 !   numbers) will be converted into ##copy insns; thus, some
101 !   ##copies may show up *before* ##phis within a basic block,
102 !   even though ##phis should come at the very beginning of a
103 !   block.
104 !
105 ! Thus, the call to needs-predecessors in copy-propagation may
106 ! wind up failing to prune dead inputs to particular ##phis in
107 ! a block (if they're preceded by ##copies).  However,
108 ! copy-propagation will remove the ##copies that
109 ! value-numbering introduces.  So, a band-aid solution is to
110 ! suffix a predecessors-changed to copy-propagation, so that
111 ! future calls to needs-predecessors (particularly in
112 ! compiler.cfg.dce:eliminate-dead-code) will finally correct
113 ! the ##phi nodes left over after value-numbering.
114 !
115 ! A better solution (and the eventual goal) would be to have
116 ! value-numbering subsume copy-propagation, thus eliminating
117 ! this pass altogether.
118
119 USE: compiler.cfg
120
121 : copy-propagation ( cfg -- )
122     {
123         needs-predecessors
124         collect-copies
125         rename-copies
126         predecessors-changed
127     } apply-passes ;