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 words vectors combinators combinators.short-circuit
5 sets classes layouts cpu.architecture
10 compiler.cfg.copy-prop
11 compiler.cfg.registers
12 compiler.cfg.comparisons
13 compiler.cfg.instructions
14 compiler.cfg.representations.preferred ;
15 FROM: namespaces => set ;
16 IN: compiler.cfg.alias-analysis
18 ! We try to eliminate redundant slot operations using some simple heuristics.
20 ! All heap-allocated objects which are loaded from the stack, or
21 ! other object slots are pessimistically assumed to belong to
22 ! the same alias class.
24 ! Freshly-allocated objects get their own alias class.
26 ! Simple pseudo-C example showing load elimination:
28 ! int *x, *y, z: inputs
29 ! int a, b, c, d, e: locals
31 ! Before alias analysis:
41 ! After alias analysis:
44 ! b = a /* ELIMINATED */
47 ! d = x[2] /* if x=y, d=z, if x!=y, d=b; NOT ELIMINATED */
48 ! e = z /* ELIMINATED */
49 ! f = c /* ELIMINATED */
51 ! Simple pseudo-C example showing store elimination:
53 ! Before alias analysis:
62 ! After alias analysis:
64 ! x[0] = a /* dead if n = 0, live otherwise; NOT ELIMINATED */
67 ! /* x[1] = d */ /* ELIMINATED */
71 ! Map vregs -> alias classes
74 ERROR: vreg-ac-not-set vreg ;
76 : vreg>ac ( vreg -- ac )
77 #! Only vregs produced by ##allot, ##peek and ##slot can
78 #! ever be used as valid inputs to ##slot and ##set-slot,
79 #! so we assert this fact by not giving alias classes to
81 vregs>acs get ?at [ vreg-ac-not-set ] unless ;
83 ! Map alias classes -> sequence of vregs
86 : ac>vregs ( ac -- vregs ) acs>vregs get at ;
88 GENERIC: aliases ( vreg -- vregs )
91 #! All vregs which may contain the same value as vreg.
97 : each-alias ( vreg quot -- )
98 [ aliases ] dip each ; inline
100 ! Map vregs -> slot# -> vreg
103 ! Current instruction number
106 ! Load/store history, for dead store elimination
110 : new-action ( class -- action )
111 insn# get swap boa ; inline
113 ! Maps vreg -> slot# -> sequence of loads/stores
116 : history ( vreg -- history ) histories get at ;
118 : set-ac ( vreg ac -- )
119 #! Set alias class of newly-seen vreg.
121 [ drop H{ } clone swap histories get set-at ]
122 [ drop H{ } clone swap live-slots get set-at ]
123 [ swap vregs>acs get set-at ]
124 [ acs>vregs get push-at ]
127 : live-slot ( slot#/f vreg -- vreg' )
128 #! If the slot number is unknown, we never reuse a previous
130 over [ live-slots get at at ] [ 2drop f ] if ;
132 ERROR: vreg-has-no-slots vreg ;
134 : load-constant-slot ( value slot# vreg -- )
135 live-slots get ?at [ vreg-has-no-slots ] unless set-at ;
137 : load-slot ( value slot#/f vreg -- )
138 over [ load-constant-slot ] [ 3drop ] if ;
140 : record-constant-slot ( slot# vreg -- )
141 #! A load can potentially read every store of this slot#
142 #! in that alias class.
144 history [ load new-action swap ?push ] change-at
147 : record-computed-slot ( vreg -- )
148 #! Computed load is like a load of every slot touched so far
150 history values [ load new-action swap push ] each
153 : remember-slot ( value slot#/f vreg -- )
155 [ [ record-constant-slot ] [ load-constant-slot ] 2bi ]
156 [ 2nip record-computed-slot ] if ;
161 ac-counter [ dup 1 + ] change ;
163 ! Alias class for objects which are loaded from the data stack
164 ! or other object slots. We pessimistically assume that they
165 ! can all alias each other.
168 : set-heap-ac ( vreg -- ) heap-ac get set-ac ;
170 : set-new-ac ( vreg -- ) next-ac set-ac ;
172 : kill-constant-set-slot ( slot# vreg -- )
173 [ live-slots get at delete-at ] with each-alias ;
175 : record-constant-set-slot ( slot# vreg -- )
177 dup empty? [ dup last store? [ dup pop* ] when ] unless
178 store new-action swap ?push
181 : kill-computed-set-slot ( ac -- )
182 [ live-slots get at clear-assoc ] each-alias ;
184 : remember-set-slot ( slot#/f vreg -- )
186 [ record-constant-set-slot ]
187 [ kill-constant-set-slot ] 2bi
188 ] [ nip kill-computed-set-slot ] if ;
192 : constant ( vreg -- n/f )
193 #! Return a ##load-immediate value, or f if the vreg was not
194 #! assigned by an ##load-immediate.
195 resolve constants get at ;
197 GENERIC: insn-slot# ( insn -- slot#/f )
198 GENERIC: insn-object ( insn -- vreg )
200 M: ##slot insn-slot# slot>> constant ;
201 M: ##slot-imm insn-slot# slot>> ;
202 M: ##set-slot insn-slot# slot>> constant ;
203 M: ##set-slot-imm insn-slot# slot>> ;
204 M: ##alien-global insn-slot# [ library>> ] [ symbol>> ] bi 2array ;
205 M: ##vm-field-ptr insn-slot# field-name>> ;
207 M: ##slot insn-object obj>> resolve ;
208 M: ##slot-imm insn-object obj>> resolve ;
209 M: ##set-slot insn-object obj>> resolve ;
210 M: ##set-slot-imm insn-object obj>> resolve ;
211 M: ##alien-global insn-object drop \ ##alien-global ;
212 M: ##vm-field-ptr insn-object drop \ ##vm-field-ptr ;
214 : init-alias-analysis ( insns -- insns' )
215 H{ } clone histories set
216 H{ } clone vregs>acs set
217 H{ } clone acs>vregs set
218 H{ } clone live-slots set
219 H{ } clone constants set
220 H{ } clone copies set
225 \ ##vm-field-ptr set-new-ac
226 \ ##alien-global set-new-ac
228 dup local-live-in [ set-heap-ac ] each ;
230 GENERIC: analyze-aliases* ( insn -- insn' )
232 M: insn analyze-aliases*
233 ! If an instruction defines a value with a non-integer
234 ! representation it means that the value will be boxed
235 ! anywhere its used as a tagged pointer. Boxing allocates
236 ! a new value, except boxing instructions haven't been
239 over defs-vreg-rep int-rep eq?
240 [ set-heap-ac ] [ set-new-ac ] if
243 M: ##phi analyze-aliases*
244 dup defs-vreg set-heap-ac ;
246 M: ##load-immediate analyze-aliases*
248 dup [ val>> ] [ dst>> ] bi constants get set-at ;
250 M: ##allocation analyze-aliases*
251 #! A freshly allocated object is distinct from any other
253 dup dst>> set-new-ac ;
255 M: ##read analyze-aliases*
257 dup [ dst>> ] [ insn-slot# ] [ insn-object ] tri
259 2nip any-rep \ ##copy new-insn analyze-aliases* nip
264 : idempotent? ( value slot#/f vreg -- ? )
265 #! Are we storing a value back to the same slot it was read
269 M: ##write analyze-aliases*
271 [ src>> resolve ] [ insn-slot# ] [ insn-object ] tri
272 [ remember-set-slot drop ] [ load-slot ] 3bi ;
274 M: ##copy analyze-aliases*
275 #! The output vreg gets the same alias class as the input
276 #! vreg, since they both contain the same value.
279 : useless-compare? ( insn -- ? )
282 [ [ src1>> ] [ src2>> ] bi [ resolve vreg>ac ] bi@ = not ]
285 M: ##compare analyze-aliases*
287 dup useless-compare? [
288 dst>> \ f type-number \ ##load-immediate new-insn
292 : analyze-aliases ( insns -- insns' )
293 [ insn# set analyze-aliases* ] map-index sift ;
297 : compute-live-stores ( -- )
300 values [ [ store? ] filter [ insn#>> ] map ] map concat
304 GENERIC: eliminate-dead-stores* ( insn -- insn' )
306 : (eliminate-dead-stores) ( insn -- insn' )
308 insn# get live-stores get key? [
313 M: ##set-slot eliminate-dead-stores* (eliminate-dead-stores) ;
315 M: ##set-slot-imm eliminate-dead-stores* (eliminate-dead-stores) ;
317 M: insn eliminate-dead-stores* ;
319 : eliminate-dead-stores ( insns -- insns' )
320 [ insn# set eliminate-dead-stores* ] map-index sift ;
322 : alias-analysis-step ( insns -- insns' )
326 eliminate-dead-stores ;
328 : alias-analysis ( cfg -- cfg' )
329 [ alias-analysis-step ] local-optimization ;