1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math namespaces assocs hashtables sequences
4 accessors vectors combinators sets compiler.vops compiler.cfg ;
7 ! Alias analysis -- must be run after compiler.cfg.stack.
9 ! We try to eliminate redundant slot and stack
10 ! traffic using some simple heuristics.
12 ! All heap-allocated objects which are loaded from the stack, or
13 ! other object slots are pessimistically assumed to belong to
14 ! the same alias class.
16 ! Freshly-allocated objects get their own alias class.
18 ! The data and retain stack pointer registers are treated
19 ! uniformly, and each one gets its own alias class.
21 ! Simple pseudo-C example showing load elimination:
23 ! int *x, *y, z: inputs
24 ! int a, b, c, d, e: locals
26 ! Before alias analysis:
36 ! After alias analysis:
39 ! b = a /* ELIMINATED */
42 ! d = x[2] /* if x=y, d=z, if x!=y, d=b; NOT ELIMINATED */
43 ! e = z /* ELIMINATED */
44 ! f = c /* ELIMINATED */
46 ! Simple pseudo-C example showing store elimination:
48 ! Before alias analysis:
57 ! After alias analysis:
59 ! x[0] = a /* dead if n = 0, live otherwise; NOT ELIMINATED */
62 ! /* x[1] = d */ /* ELIMINATED */
66 ! Map vregs -> alias classes
69 : check [ "BUG: static type error detected" throw ] unless* ; inline
71 : vreg>ac ( vreg -- ac )
72 #! Only vregs produced by %%allot, %peek and %%slot can
73 #! ever be used as valid inputs to %%slot and %%set-slot,
74 #! so we assert this fact by not giving alias classes to
76 vregs>acs get at check ;
78 ! Map alias classes -> sequence of vregs
81 : ac>vregs ( ac -- vregs ) acs>vregs get at ;
83 : aliases ( vreg -- vregs )
84 #! All vregs which may contain the same value as vreg.
87 : each-alias ( vreg quot -- )
88 [ aliases ] dip each ; inline
90 ! Map vregs -> slot# -> vreg
93 ! Current instruction number
96 ! Load/store history, for dead store elimination
100 : new-action ( class -- action )
101 insn# get swap boa ; inline
103 ! Maps vreg -> slot# -> sequence of loads/stores
106 : history ( vreg -- history ) histories get at ;
108 : set-ac ( vreg ac -- )
109 #! Set alias class of newly-seen vreg.
111 [ drop H{ } clone swap histories get set-at ]
112 [ drop H{ } clone swap live-slots get set-at ]
113 [ swap vregs>acs get set-at ]
114 [ acs>vregs get push-at ]
117 : live-slot ( slot#/f vreg -- vreg' )
118 #! If the slot number is unknown, we never reuse a previous
120 over [ live-slots get at at ] [ 2drop f ] if ;
122 : load-constant-slot ( value slot# vreg -- )
123 live-slots get at check set-at ;
125 : load-slot ( value slot#/f vreg -- )
126 over [ load-constant-slot ] [ 3drop ] if ;
128 : record-constant-slot ( slot# vreg -- )
129 #! A load can potentially read every store of this slot#
130 #! in that alias class.
132 history [ load new-action swap ?push ] change-at
135 : record-computed-slot ( vreg -- )
136 #! Computed load is like a load of every slot touched so far
138 history values [ load new-action swap push ] each
141 : remember-slot ( value slot#/f vreg -- )
143 [ [ record-constant-slot ] [ load-constant-slot ] 2bi ]
144 [ 2nip record-computed-slot ] if ;
149 ac-counter [ dup 1+ ] change ;
151 ! Alias class for objects which are loaded from the data stack
152 ! or other object slots. We pessimistically assume that they
153 ! can all alias each other.
156 : set-heap-ac ( vreg -- ) heap-ac get set-ac ;
158 : set-new-ac ( vreg -- ) next-ac set-ac ;
160 : kill-constant-set-slot ( slot# vreg -- )
161 [ live-slots get at delete-at ] with each-alias ;
163 : record-constant-set-slot ( slot# vreg -- )
165 dup empty? [ dup peek store? [ dup pop* ] when ] unless
166 store new-action swap ?push
169 : kill-computed-set-slot ( ac -- )
170 [ live-slots get at clear-assoc ] each-alias ;
172 : remember-set-slot ( slot#/f vreg -- )
174 [ record-constant-set-slot ]
175 [ kill-constant-set-slot ] 2bi
176 ] [ nip kill-computed-set-slot ] if ;
180 : resolve ( vreg -- vreg )
181 dup copies get at swap or ;
185 : constant ( vreg -- n/f )
186 #! Return an %iconst value, or f if the vreg was not
187 #! assigned by an %iconst.
188 resolve constants get at ;
190 ! We treat slot accessors and stack traffic alike
191 GENERIC: insn-slot# ( insn -- slot#/f )
192 GENERIC: insn-object ( insn -- vreg )
194 M: %peek insn-slot# n>> ;
195 M: %replace insn-slot# n>> ;
196 M: %%slot insn-slot# slot>> constant ;
197 M: %%set-slot insn-slot# slot>> constant ;
199 M: %peek insn-object stack>> ;
200 M: %replace insn-object stack>> ;
201 M: %%slot insn-object obj>> resolve ;
202 M: %%set-slot insn-object obj>> resolve ;
204 : init-alias-analysis ( -- )
205 H{ } clone histories set
206 H{ } clone vregs>acs set
207 H{ } clone acs>vregs set
208 H{ } clone live-slots set
209 H{ } clone constants set
210 H{ } clone copies set
216 %retain next-ac set-ac ;
218 GENERIC: analyze-aliases ( insn -- insn' )
220 M: %iconst analyze-aliases
221 dup [ value>> ] [ out>> ] bi constants get set-at ;
223 M: %%allot analyze-aliases
224 #! A freshly allocated object is distinct from any other
226 dup out>> set-new-ac ;
228 M: read-op analyze-aliases
229 dup out>> set-heap-ac
230 dup [ out>> ] [ insn-slot# ] [ insn-object ] tri
232 2nip %copy boa analyze-aliases nip
237 : idempotent? ( value slot#/f vreg -- ? )
238 #! Are we storing a value back to the same slot it was read
242 M: write-op analyze-aliases
244 [ in>> resolve ] [ insn-slot# ] [ insn-object ] tri
248 [ remember-set-slot drop ] [ load-slot ] 3bi
251 M: %copy analyze-aliases
252 #! The output vreg gets the same alias class as the input
253 #! vreg, since they both contain the same value.
254 dup [ in>> resolve ] [ out>> ] bi copies get set-at ;
256 M: vop analyze-aliases ;
260 : compute-live-stores ( -- )
263 values [ [ store? ] filter [ insn#>> ] map ] map concat
267 GENERIC: eliminate-dead-store ( insn -- insn' )
269 : (eliminate-dead-store) ( insn -- insn' )
271 insn# get live-stores get key? [
276 M: %replace eliminate-dead-store
277 #! Writes to above the top of the stack can be pruned also.
278 #! This is sound since any such writes are not observable
279 #! after the basic block, and any reads of those locations
280 #! will have been converted to copies by analyze-slot,
281 #! and the final stack height of the basic block is set at
282 #! the beginning by compiler.cfg.stack.
283 dup n>> 0 < [ drop nop ] [ (eliminate-dead-store) ] if ;
285 M: %%set-slot eliminate-dead-store (eliminate-dead-store) ;
287 M: vop eliminate-dead-store ;
289 : alias-analysis ( insns -- insns' )
291 [ insn# set analyze-aliases ] map-index
293 [ insn# set eliminate-dead-store ] map-index ;