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 IN: compiler.cfg.alias-analysis
17 ! We try to eliminate redundant slot operations using some simple heuristics.
19 ! All heap-allocated objects which are loaded from the stack, or
20 ! other object slots are pessimistically assumed to belong to
21 ! the same alias class.
23 ! Freshly-allocated objects get their own alias class.
25 ! Simple pseudo-C example showing load elimination:
27 ! int *x, *y, z: inputs
28 ! int a, b, c, d, e: locals
30 ! Before alias analysis:
40 ! After alias analysis:
43 ! b = a /* ELIMINATED */
46 ! d = x[2] /* if x=y, d=z, if x!=y, d=b; NOT ELIMINATED */
47 ! e = z /* ELIMINATED */
48 ! f = c /* ELIMINATED */
50 ! Simple pseudo-C example showing store elimination:
52 ! Before alias analysis:
61 ! After alias analysis:
63 ! x[0] = a /* dead if n = 0, live otherwise; NOT ELIMINATED */
66 ! /* x[1] = d */ /* ELIMINATED */
70 ! Map vregs -> alias classes
73 ERROR: vreg-ac-not-set vreg ;
75 : vreg>ac ( vreg -- ac )
76 #! Only vregs produced by ##allot, ##peek and ##slot can
77 #! ever be used as valid inputs to ##slot and ##set-slot,
78 #! so we assert this fact by not giving alias classes to
80 vregs>acs get ?at [ vreg-ac-not-set ] unless ;
82 ! Map alias classes -> sequence of vregs
85 : ac>vregs ( ac -- vregs ) acs>vregs get at ;
87 GENERIC: aliases ( vreg -- vregs )
90 #! All vregs which may contain the same value as vreg.
96 : each-alias ( vreg quot -- )
97 [ aliases ] dip each ; inline
99 ! Map vregs -> slot# -> vreg
102 ! Current instruction number
105 ! Load/store history, for dead store elimination
109 : new-action ( class -- action )
110 insn# get swap boa ; inline
112 ! Maps vreg -> slot# -> sequence of loads/stores
115 : history ( vreg -- history ) histories get at ;
117 : set-ac ( vreg ac -- )
118 #! Set alias class of newly-seen vreg.
120 [ drop H{ } clone swap histories get set-at ]
121 [ drop H{ } clone swap live-slots get set-at ]
122 [ swap vregs>acs get set-at ]
123 [ acs>vregs get push-at ]
126 : live-slot ( slot#/f vreg -- vreg' )
127 #! If the slot number is unknown, we never reuse a previous
129 over [ live-slots get at at ] [ 2drop f ] if ;
131 ERROR: vreg-has-no-slots vreg ;
133 : load-constant-slot ( value slot# vreg -- )
134 live-slots get ?at [ vreg-has-no-slots ] unless set-at ;
136 : load-slot ( value slot#/f vreg -- )
137 over [ load-constant-slot ] [ 3drop ] if ;
139 : record-constant-slot ( slot# vreg -- )
140 #! A load can potentially read every store of this slot#
141 #! in that alias class.
143 history [ load new-action swap ?push ] change-at
146 : record-computed-slot ( vreg -- )
147 #! Computed load is like a load of every slot touched so far
149 history values [ load new-action swap push ] each
152 : remember-slot ( value slot#/f vreg -- )
154 [ [ record-constant-slot ] [ load-constant-slot ] 2bi ]
155 [ 2nip record-computed-slot ] if ;
160 ac-counter [ dup 1 + ] change ;
162 ! Alias class for objects which are loaded from the data stack
163 ! or other object slots. We pessimistically assume that they
164 ! can all alias each other.
167 : set-heap-ac ( vreg -- ) heap-ac get set-ac ;
169 : set-new-ac ( vreg -- ) next-ac set-ac ;
171 : kill-constant-set-slot ( slot# vreg -- )
172 [ live-slots get at delete-at ] with each-alias ;
174 : record-constant-set-slot ( slot# vreg -- )
176 dup empty? [ dup last store? [ dup pop* ] when ] unless
177 store new-action swap ?push
180 : kill-computed-set-slot ( ac -- )
181 [ live-slots get at clear-assoc ] each-alias ;
183 : remember-set-slot ( slot#/f vreg -- )
185 [ record-constant-set-slot ]
186 [ kill-constant-set-slot ] 2bi
187 ] [ nip kill-computed-set-slot ] if ;
191 : constant ( vreg -- n/f )
192 #! Return a ##load-immediate value, or f if the vreg was not
193 #! assigned by an ##load-immediate.
194 resolve constants get at ;
196 GENERIC: insn-slot# ( insn -- slot#/f )
197 GENERIC: insn-object ( insn -- vreg )
199 M: ##slot insn-slot# slot>> constant ;
200 M: ##slot-imm insn-slot# slot>> ;
201 M: ##set-slot insn-slot# slot>> constant ;
202 M: ##set-slot-imm insn-slot# slot>> ;
203 M: ##alien-global insn-slot# [ library>> ] [ symbol>> ] bi 2array ;
204 M: ##vm-field-ptr insn-slot# field-name>> ;
206 M: ##slot insn-object obj>> resolve ;
207 M: ##slot-imm insn-object obj>> resolve ;
208 M: ##set-slot insn-object obj>> resolve ;
209 M: ##set-slot-imm insn-object obj>> resolve ;
210 M: ##alien-global insn-object drop \ ##alien-global ;
211 M: ##vm-field-ptr insn-object drop \ ##vm-field-ptr ;
213 : init-alias-analysis ( insns -- insns' )
214 H{ } clone histories set
215 H{ } clone vregs>acs set
216 H{ } clone acs>vregs set
217 H{ } clone live-slots set
218 H{ } clone constants set
219 H{ } clone copies set
224 \ ##vm-field-ptr set-new-ac
225 \ ##alien-global set-new-ac
227 dup local-live-in [ set-heap-ac ] each ;
229 GENERIC: analyze-aliases* ( insn -- insn' )
231 M: insn analyze-aliases*
232 ! If an instruction defines a value with a non-integer
233 ! representation it means that the value will be boxed
234 ! anywhere its used as a tagged pointer. Boxing allocates
235 ! a new value, except boxing instructions haven't been
238 over defs-vreg-rep int-rep eq?
239 [ set-heap-ac ] [ set-new-ac ] if
242 M: ##phi analyze-aliases*
243 dup defs-vreg set-heap-ac ;
245 M: ##load-immediate analyze-aliases*
247 dup [ val>> ] [ dst>> ] bi constants get set-at ;
249 M: ##allocation analyze-aliases*
250 #! A freshly allocated object is distinct from any other
252 dup dst>> set-new-ac ;
254 M: ##read analyze-aliases*
256 dup [ dst>> ] [ insn-slot# ] [ insn-object ] tri
258 2nip any-rep \ ##copy new-insn analyze-aliases* nip
263 : idempotent? ( value slot#/f vreg -- ? )
264 #! Are we storing a value back to the same slot it was read
268 M: ##write analyze-aliases*
270 [ src>> resolve ] [ insn-slot# ] [ insn-object ] tri
271 [ remember-set-slot drop ] [ load-slot ] 3bi ;
273 M: ##copy analyze-aliases*
274 #! The output vreg gets the same alias class as the input
275 #! vreg, since they both contain the same value.
278 : useless-compare? ( insn -- ? )
281 [ [ src1>> ] [ src2>> ] bi [ resolve vreg>ac ] bi@ = not ]
284 M: ##compare analyze-aliases*
286 dup useless-compare? [
287 dst>> \ f tag-number \ ##load-immediate new-insn
291 : analyze-aliases ( insns -- insns' )
292 [ insn# set analyze-aliases* ] map-index sift ;
296 : compute-live-stores ( -- )
299 values [ [ store? ] filter [ insn#>> ] map ] map concat
303 GENERIC: eliminate-dead-stores* ( insn -- insn' )
305 : (eliminate-dead-stores) ( insn -- insn' )
307 insn# get live-stores get key? [
312 M: ##set-slot eliminate-dead-stores* (eliminate-dead-stores) ;
314 M: ##set-slot-imm eliminate-dead-stores* (eliminate-dead-stores) ;
316 M: insn eliminate-dead-stores* ;
318 : eliminate-dead-stores ( insns -- insns' )
319 [ insn# set eliminate-dead-stores* ] map-index sift ;
321 : alias-analysis-step ( insns -- insns' )
325 eliminate-dead-stores ;
327 : alias-analysis ( cfg -- cfg' )
328 [ alias-analysis-step ] local-optimization ;