1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math namespaces assocs hashtables sequences arrays
4 accessors vectors combinators sets classes cpu.architecture compiler.cfg
5 compiler.cfg.registers compiler.cfg.instructions
6 compiler.cfg.copy-prop compiler.cfg.rpo compiler.cfg.liveness ;
7 IN: compiler.cfg.alias-analysis
9 ! We try to eliminate redundant slot operations using some simple heuristics.
11 ! All heap-allocated objects which are loaded from the stack, or
12 ! other object slots are pessimistically assumed to belong to
13 ! the same alias class.
15 ! Freshly-allocated objects get their own alias class.
17 ! Simple pseudo-C example showing load elimination:
19 ! int *x, *y, z: inputs
20 ! int a, b, c, d, e: locals
22 ! Before alias analysis:
32 ! After alias analysis:
35 ! b = a /* ELIMINATED */
38 ! d = x[2] /* if x=y, d=z, if x!=y, d=b; NOT ELIMINATED */
39 ! e = z /* ELIMINATED */
40 ! f = c /* ELIMINATED */
42 ! Simple pseudo-C example showing store elimination:
44 ! Before alias analysis:
53 ! After alias analysis:
55 ! x[0] = a /* dead if n = 0, live otherwise; NOT ELIMINATED */
58 ! /* x[1] = d */ /* ELIMINATED */
62 ! Map vregs -> alias classes
65 ERROR: vreg-ac-not-set vreg ;
67 : vreg>ac ( vreg -- ac )
68 #! Only vregs produced by ##allot, ##peek and ##slot can
69 #! ever be used as valid inputs to ##slot and ##set-slot,
70 #! so we assert this fact by not giving alias classes to
72 vregs>acs get ?at [ vreg-ac-not-set ] unless ;
74 ! Map alias classes -> sequence of vregs
77 : ac>vregs ( ac -- vregs ) acs>vregs get at ;
79 : aliases ( vreg -- vregs )
80 #! All vregs which may contain the same value as vreg.
83 : each-alias ( vreg quot -- )
84 [ aliases ] dip each ; inline
86 ! Map vregs -> slot# -> vreg
89 ! Current instruction number
92 ! Load/store history, for dead store elimination
96 : new-action ( class -- action )
97 insn# get swap boa ; inline
99 ! Maps vreg -> slot# -> sequence of loads/stores
102 : history ( vreg -- history ) histories get at ;
104 : set-ac ( vreg ac -- )
105 #! Set alias class of newly-seen vreg.
107 [ drop H{ } clone swap histories get set-at ]
108 [ drop H{ } clone swap live-slots get set-at ]
109 [ swap vregs>acs get set-at ]
110 [ acs>vregs get push-at ]
113 : live-slot ( slot#/f vreg -- vreg' )
114 #! If the slot number is unknown, we never reuse a previous
116 over [ live-slots get at at ] [ 2drop f ] if ;
118 ERROR: vreg-has-no-slots vreg ;
120 : load-constant-slot ( value slot# vreg -- )
121 live-slots get ?at [ vreg-has-no-slots ] unless set-at ;
123 : load-slot ( value slot#/f vreg -- )
124 over [ load-constant-slot ] [ 3drop ] if ;
126 : record-constant-slot ( slot# vreg -- )
127 #! A load can potentially read every store of this slot#
128 #! in that alias class.
130 history [ load new-action swap ?push ] change-at
133 : record-computed-slot ( vreg -- )
134 #! Computed load is like a load of every slot touched so far
136 history values [ load new-action swap push ] each
139 : remember-slot ( value slot#/f vreg -- )
141 [ [ record-constant-slot ] [ load-constant-slot ] 2bi ]
142 [ 2nip record-computed-slot ] if ;
147 ac-counter [ dup 1 + ] change ;
149 ! Alias class for objects which are loaded from the data stack
150 ! or other object slots. We pessimistically assume that they
151 ! can all alias each other.
154 : set-heap-ac ( vreg -- ) heap-ac get set-ac ;
156 : set-new-ac ( vreg -- ) next-ac set-ac ;
158 : kill-constant-set-slot ( slot# vreg -- )
159 [ live-slots get at delete-at ] with each-alias ;
161 : record-constant-set-slot ( slot# vreg -- )
163 dup empty? [ dup last store? [ dup pop* ] when ] unless
164 store new-action swap ?push
167 : kill-computed-set-slot ( ac -- )
168 [ live-slots get at clear-assoc ] each-alias ;
170 : remember-set-slot ( slot#/f vreg -- )
172 [ record-constant-set-slot ]
173 [ kill-constant-set-slot ] 2bi
174 ] [ nip kill-computed-set-slot ] if ;
178 : constant ( vreg -- n/f )
179 #! Return a ##load-immediate value, or f if the vreg was not
180 #! assigned by an ##load-immediate.
181 resolve constants get at ;
183 ! We treat slot accessors and stack traffic alike
184 GENERIC: insn-slot# ( insn -- slot#/f )
185 GENERIC: insn-object ( insn -- vreg )
187 M: ##slot insn-slot# slot>> constant ;
188 M: ##slot-imm insn-slot# slot>> ;
189 M: ##set-slot insn-slot# slot>> constant ;
190 M: ##set-slot-imm insn-slot# slot>> ;
191 M: ##alien-global insn-slot# [ library>> ] [ symbol>> ] bi 2array ;
193 M: ##slot insn-object obj>> resolve ;
194 M: ##slot-imm insn-object obj>> resolve ;
195 M: ##set-slot insn-object obj>> resolve ;
196 M: ##set-slot-imm insn-object obj>> resolve ;
197 M: ##alien-global insn-object drop \ ##alien-global ;
199 : init-alias-analysis ( insns -- insns' )
200 H{ } clone histories set
201 H{ } clone vregs>acs set
202 H{ } clone acs>vregs set
203 H{ } clone live-slots set
204 H{ } clone constants set
205 H{ } clone copies set
210 dup local-live-in [ set-heap-ac ] each ;
212 GENERIC: analyze-aliases* ( insn -- insn' )
214 M: ##load-immediate analyze-aliases*
215 dup [ val>> ] [ dst>> ] bi constants get set-at ;
217 M: ##flushable analyze-aliases*
218 dup dst>> set-heap-ac ;
220 M: ##allocation analyze-aliases*
221 #! A freshly allocated object is distinct from any other
223 dup dst>> set-new-ac ;
225 M: ##read analyze-aliases*
227 dup [ dst>> ] [ insn-slot# ] [ insn-object ] tri
229 2nip any-rep \ ##copy new-insn analyze-aliases* nip
234 : idempotent? ( value slot#/f vreg -- ? )
235 #! Are we storing a value back to the same slot it was read
239 M: ##write analyze-aliases*
241 [ src>> resolve ] [ insn-slot# ] [ insn-object ] tri
242 [ remember-set-slot drop ] [ load-slot ] 3bi ;
244 M: ##copy analyze-aliases*
245 #! The output vreg gets the same alias class as the input
246 #! vreg, since they both contain the same value.
249 M: insn analyze-aliases* ;
251 : analyze-aliases ( insns -- insns' )
252 [ insn# set analyze-aliases* ] map-index sift ;
256 : compute-live-stores ( -- )
259 values [ [ store? ] filter [ insn#>> ] map ] map concat
263 GENERIC: eliminate-dead-stores* ( insn -- insn' )
265 : (eliminate-dead-stores) ( insn -- insn' )
267 insn# get live-stores get key? [
272 M: ##set-slot eliminate-dead-stores* (eliminate-dead-stores) ;
274 M: ##set-slot-imm eliminate-dead-stores* (eliminate-dead-stores) ;
276 M: insn eliminate-dead-stores* ;
278 : eliminate-dead-stores ( insns -- insns' )
279 [ insn# set eliminate-dead-stores* ] map-index sift ;
281 : alias-analysis-step ( insns -- insns' )
285 eliminate-dead-stores ;
287 : alias-analysis ( cfg -- cfg' )
288 [ alias-analysis-step ] local-optimization ;