Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/compiler/CodeGen/cpscompile/memAliasing.sml
ViewVC logotype

Annotation of /sml/trunk/compiler/CodeGen/cpscompile/memAliasing.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 733 - (view) (download)
Original Path: sml/trunk/src/compiler/CodeGen/cpscompile/memAliasing.sml

1 : monnier 409 (*
2 :     * Perform memory aliasing analysis.
3 :     *
4 :     * The old memory disambiguation module discards aliasing information
5 :     * across CPS function boundaries, which made it not very useful for the
6 :     * optimizations I have in mind.
7 :     *
8 :     * This is an alternative module that (hopefully) does the right thing.
9 :     * The algorithm is inspired by Steensgaard's work on flow insensitive
10 :     * points-to analysis, but has been hacked to deal with target level issues.
11 :     *
12 :     * Some target level issues
13 :     * ------------------------
14 :     * In the source level two CPS allocations cannot be aliased by definition.
15 :     * However, when allocations are translated into target code, they become
16 :     * stores to fixed offsets from the heap pointer. Two allocation stores
17 :     * that may write to the same offset are aliased. Allocation stores that are
18 :     * in disjoint program paths may be assigned the same heap allocation offset.
19 :     * We have to mark these as aliased since we want to allow speculative writes
20 :     * to the allocation space.
21 :     *
22 :     * Representing heap offsets
23 :     * -------------------------
24 :     *
25 :     *
26 :     * Language
27 :     * --------
28 :     * e ::= x <- v.i; k /* select */
29 :     * | x <- v+i; k /* offset */
30 :     * | x <- [v1,...vn]^hp; k /* record allocation at heap pointer hp */
31 :     * | x <- !v; k /* dereference */
32 :     * | v1 := v2; k /* update */
33 :     * | f(v1,...,vn) /* tail call */
34 :     *
35 :     * Since the analysis is flow insensitive, the branch constructs are
36 :     * irrelevant.
37 :     *
38 :     * -- Allen
39 :     *)
40 :    
41 :     signature MEM_ALIASING =
42 :     sig
43 : leunga 585 val analyze : CPS.function list -> (CPS.lvar -> CPSRegions.region)
44 : monnier 409 end
45 :    
46 :     functor MemAliasing(Cells : CELLS) : MEM_ALIASING =
47 :     struct
48 :     structure C = CPS
49 :     structure P = CPS.P
50 :     structure PT = PointsTo
51 :    
52 :     fun error msg = MLRiscErrorMsg.error("MemAliasing",msg)
53 :    
54 :     (*
55 :     * The following functions advances the heap pointer.
56 :     * These functions are highly dependent on the runtime system and
57 :     * how data structures are represented.
58 :     * IMPORTANT: we are assuming that the new array representation is used.
59 :     *)
60 : leunga 585 fun recordSize(n,hp) = n * 4 + 4 + hp
61 :     fun frecordSize(n,hp) =
62 : monnier 409 let val hp = if Word.andb(Word.fromInt hp,0w4) <> 0w0 then hp+8 else hp+4
63 :     in 8*n + hp end
64 : leunga 585 fun vectorSize(n,hp) = n * 4 + 16 + hp
65 : monnier 409
66 : leunga 585 fun allocRecord(C.RK_FBLOCK,vs,hp) = frecordSize(length vs,hp)
67 :     | allocRecord(C.RK_FCONT,vs,hp) = frecordSize(length vs,hp)
68 :     | allocRecord(C.RK_VECTOR,vs,hp) = vectorSize(length vs,hp)
69 :     | allocRecord(_,vs,hp) = recordSize(length vs,hp)
70 : monnier 409
71 :     val storeListSize = 8
72 :     val array0Size = 20
73 :    
74 :     exception NotFound
75 :    
76 : leunga 590 val top = CPSRegions.memory
77 :    
78 : monnier 409 (*
79 :     * Analyze a set of CPS functions
80 :     *)
81 :     fun analyze(cpsFunctions) =
82 :     let fun sizeOf(C.RECORD(rk,vs,x,k),hp) = sizeOf(k,allocRecord(rk,vs,hp))
83 :     | sizeOf(C.SELECT(off,v,x,cty,k),hp) = sizeOf(k,hp)
84 :     | sizeOf(C.OFFSET(off,v,x,k),hp) = sizeOf(k,hp)
85 :     | sizeOf(C.APP(f,vs),hp) = hp
86 :     | sizeOf(C.FIX _,hp) = error "sizeOf: FIX"
87 :     | sizeOf(C.SWITCH(v,x,ks),hp) = sizeOfs(ks,hp)
88 :     | sizeOf(C.BRANCH(p,_,x,k1,k2),hp) =
89 :     Int.max(sizeOf(k1,hp),sizeOf(k2,hp))
90 : leunga 585 | sizeOf(C.SETTER(P.assign,vs,k),hp) = sizeOf(k,hp+storeListSize)
91 : monnier 409 | sizeOf(C.SETTER(P.update,vs,k),hp) = sizeOf(k,hp+storeListSize)
92 :     | sizeOf(C.SETTER(P.boxedupdate,vs,k),hp) = sizeOf(k,hp+storeListSize)
93 :     | sizeOf(C.SETTER(_,vs,k),hp) = sizeOf(k,hp)
94 : leunga 585 | sizeOf(C.PURE(P.fwrap,vs,x,cty,k),hp) = sizeOf(k,frecordSize(1,hp))
95 : monnier 409 | sizeOf(C.PURE(P.mkspecial,vs,x,cty,k),hp) = sizeOf(k,hp+8)
96 :     | sizeOf(C.PURE(P.makeref,vs,x,cty,k),hp) = sizeOf(k,hp+8)
97 :     | sizeOf(C.PURE(P.i32wrap,vs,x,cty,k),hp) = sizeOf(k,hp+8)
98 :     | sizeOf(C.PURE(P.newarray0,vs,x,cty,k),hp) = sizeOf(k,hp+array0Size)
99 :     | sizeOf(C.PURE(p,vs,x,cty,k),hp) = sizeOf(k,hp)
100 :     | sizeOf(C.ARITH(a,vs,x,cty,k),hp) = sizeOf(k,hp)
101 :     | sizeOf(C.LOOKER(lk,vs,x,cty,k),hp) = sizeOf(k,hp)
102 :    
103 :     and sizeOfs([],hp) = hp
104 :     | sizeOfs(k::ks,hp) = Int.max(sizeOf(k,hp),sizeOfs(ks,hp))
105 :    
106 : blume 733 val locMap = IntHashTable.mkTable(37,NotFound) (* lvar -> loc *)
107 :     val look = IntHashTable.lookup locMap
108 :     val bind = IntHashTable.insert locMap
109 : monnier 409
110 :     val newMem = Cells.newCell Cells.MEM
111 :    
112 : leunga 585 val _ = PT.reset newMem
113 : monnier 409
114 : leunga 585 fun newRef _ = ref(PT.SCELL(newMem(),ref []))
115 :    
116 :     val exnptr = PT.newSRef() (* exception handler *)
117 :     val varptr = PT.newSRef() (* var ptr *)
118 :    
119 : monnier 409 fun lookup x =
120 :     look x handle _ =>
121 : leunga 585 let val r = newRef() in bind(x,r); r end
122 : monnier 409
123 : leunga 585
124 :     fun defineFunction(fk, f, args, _, cexp) =
125 :     let val xs =
126 :     map (fn x => let val r = newRef() in bind(x,r); r end) args
127 :     in bind(f, PT.mkLambda xs) end
128 :    
129 :     val off0 = C.OFFp 0
130 :    
131 : monnier 409 fun process(fk, f, args, _, cexp) =
132 : leunga 585 let (* create a table of allocation offset locations *)
133 : monnier 409 val table = Array.tabulate(sizeOf(cexp, 0) div 4, newRef)
134 :    
135 :     fun select(i,C.VAR v,x) = bind(x,PT.pi(lookup v,i))
136 :     | select(i,_,x) = ()
137 :    
138 :     fun offset(i,C.VAR v,x) = bind(x,PT.offset(lookup v,i))
139 :     | offset(i,_,x) = ()
140 :    
141 :     fun value (C.VAR v) = lookup v
142 :     | value _ = newRef()
143 :    
144 :     fun apply(C.VAR f,args) = PT.app(lookup f,map value args)
145 :     | apply _ = ()
146 :    
147 : leunga 585 fun getPath(v,C.OFFp 0) = value v
148 :     | getPath(v,C.OFFp n) = PT.offset(value v, n)
149 :     | getPath(v,C.SELp(n,path)) = PT.pi(getPath(v,path),n)
150 : monnier 409
151 : leunga 585 fun getPaths([],hp) = []
152 :     | getPaths((v,path)::vs,hp) =
153 :     let val r = Array.sub(table,hp)
154 :     val r' = getPath(v,path)
155 :     in PT.unify(r,r'); r::getPaths(vs,hp+1) end
156 : monnier 409
157 : leunga 585 fun getF64Paths([],hp) = []
158 :     | getF64Paths((v,path)::vs,hp) =
159 :     let val r1 = Array.sub(table,hp)
160 :     val r2 = Array.sub(table,hp+1)
161 :     val r' = getPath(v,path)
162 :     in PT.unify(r1,r'); PT.unify(r2,r');
163 :     r'::getF64Paths(vs,hp+2)
164 :     end
165 : monnier 409
166 : leunga 585 (* How to make a record *)
167 :     fun mkRec(f,getPaths,x,vs,hp) =
168 :     let val i = Word.toInt(Word.>>(Word.fromInt hp,0w2))
169 :     val r = f(SOME(Array.sub(table,i)),getPaths(vs,i+1))
170 :     in bind(x,r) end
171 :     fun mkFRecord(x,vs,hp) = mkRec(PT.mkRecord,getF64Paths,x,vs,hp)
172 :     fun mkVector(x,vs,hp) = mkRec(PT.mkRecord,getPaths,x,vs,hp)
173 :     fun mkNormalRecord(x,vs,hp) = mkRec(PT.mkRecord,getPaths,x,vs,hp)
174 :    
175 :     fun mkRecord(C.RK_FBLOCK,x,vs,hp) = mkFRecord(x,vs,hp)
176 :     | mkRecord(C.RK_FCONT,x,vs,hp) = mkFRecord(x,vs,hp)
177 :     | mkRecord(C.RK_VECTOR,x,vs,hp) = mkVector(x,vs,hp)
178 :     | mkRecord(_,x,vs,hp) = mkNormalRecord(x,vs,hp)
179 :    
180 : leunga 590 fun makeTop(m) = (PT.unify(m, top); top)
181 :    
182 : leunga 585 (* CPS Pure Primitives *)
183 : leunga 590 fun arrayptr v = PT.pi(value v, 0)
184 : leunga 585
185 :     fun mkspecial(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
186 :     fun fwrap(x,v,hp) = mkFRecord(x,[(v,off0)],hp)
187 :     fun i32wrap(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
188 :     fun makeref(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
189 :     fun newarray0(x,hp) =
190 :     bind(x,PT.mkRecord(NONE,[PT.mkRecord(NONE,[])]))
191 :    
192 : leunga 590 fun objlength(x,v) = bind(x, PT.pi(value v, ~1))
193 :     fun length(x,v) = bind(x, PT.pi(value v, 1))
194 :     fun arraysub(x,a,i) = makeTop(PT.weakSubscript(arrayptr a))
195 : leunga 585 fun subscriptv(x,a,i) = arraysub(x,a,i)
196 :     fun subscript(x,a,i) = arraysub(x,a,i)
197 :     fun pure_numsubscript(x,a,i) = arraysub(x,a,i)
198 : leunga 590 fun gettag(x,v) = bind(x,PT.pi(value v, ~1))
199 : leunga 585 fun numsubscript8(x,a,i) = arraysub(x,a,i)
200 :     fun numsubscriptf64(x,a,i) = arraysub(x,a,i)
201 : leunga 590 fun getcon(x,v) = bind(x, PT.pi(value v,0))
202 :     fun getexn(x,v) = bind(x, PT.pi(value v,0))
203 : leunga 585 fun recsubscript(x,a,i) = arraysub(x,a,i)
204 :     fun raw64subscript(x,a,i) = arraysub(x,a,i)
205 :    
206 :     (* CPS Looker Primitives *)
207 : leunga 590 fun deref(x,v) = makeTop(PT.strongSubscript(value v, 0))
208 : leunga 585 fun gethdlr x = bind(x, PT.strongSubscript(exnptr, 0))
209 :     fun getvar x = bind(x, PT.strongSubscript(varptr, 0))
210 :    
211 :     (* CPS Setter Primitives *)
212 : leunga 590 fun supdate(a,x) = PT.strongUpdate(value a, 0, makeTop(value x))
213 :     fun wupdate(a,x) = PT.weakUpdate(value a, makeTop(value x))
214 : leunga 585
215 : leunga 590 fun arrayupdate(a,i,x) = PT.weakUpdate(arrayptr a,value x)
216 :    
217 : leunga 585 fun assign(a,x) = supdate(a,x)
218 :     fun unboxedassign(a,x) = supdate(a,x)
219 : leunga 590 fun update(a,i,x) = arrayupdate(a,i,x)
220 :     fun boxedupdate(a,i,x) = arrayupdate(a,i,x)
221 :     fun unboxedupdate(a,i,x) = arrayupdate(a,i,x)
222 :     fun numupdate(a,i,x) = arrayupdate(a,i,x)
223 :     fun numupdateF64(a,i,x) = arrayupdate(a,i,x)
224 : leunga 585 fun sethdlr x = PT.strongUpdate(exnptr, 0, value x)
225 :     fun setvar x = PT.strongUpdate(varptr, 0, value x)
226 :    
227 : monnier 409 fun infer(C.RECORD(rk,vs,x,k),hp) =
228 : leunga 585 (mkRecord(rk,x,vs,hp); infer(k,allocRecord(rk,vs,hp)))
229 : monnier 409 | infer(C.SELECT(i,v,x,cty,k),hp) = (select(i,v,x); infer(k,hp))
230 :     | infer(C.OFFSET(i,v,x,k),hp) = (offset(i,v,x); infer(k,hp))
231 :     | infer(C.APP(f,vs),hp) = apply(f,vs)
232 :     | infer(C.FIX _,hp) = error "infer: FIX"
233 :     | infer(C.SWITCH(v,x,ks),hp) = infers(ks,hp)
234 :     | infer(C.BRANCH(p,_,x,k1,k2),hp) = (infer(k1,hp); infer(k2,hp))
235 : leunga 585
236 : monnier 409 (*
237 :     * These things are misnamed! There is nothing pure about them!
238 :     *)
239 : leunga 585 | infer(C.PURE(P.objlength, [v], x, _, k), hp) =
240 :     (objlength(x, v); infer(k, hp))
241 :     | infer(C.PURE(P.length, [v], x, _, k), hp) =
242 :     (length(x, v); infer(k, hp))
243 :     | infer(C.PURE(P.subscriptv,[a,i],x,_,k),hp) =
244 :     (subscriptv(x, a, i); infer(k, hp))
245 :     | infer(C.PURE(P.pure_numsubscript{kind=P.INT 8},[a,i],x,_,k),hp) =
246 :     (pure_numsubscript(x, a, i); infer(k, hp))
247 :     | infer(C.PURE(P.gettag, [v], x, _, k), hp) =
248 :     (gettag(x, v); infer(k, hp))
249 : monnier 409 | infer(C.PURE(P.mkspecial,[i,v],x,cty,k),hp) =
250 : leunga 585 (mkspecial(x,v,hp); infer(k,hp+8))
251 : monnier 409 | infer(C.PURE(P.makeref,[v],x,cty,k),hp) =
252 : leunga 585 (makeref(x,v,hp); infer(k,hp+8))
253 :     | infer(C.PURE(P.fwrap,[v],x,cty,k),hp) =
254 :     (fwrap(x,v,hp); infer(k,frecordSize(1,hp)))
255 :     | infer(C.PURE(P.i32wrap,[v],x,cty,k),hp) =
256 :     (i32wrap(x,v,hp); infer(k,hp+8))
257 :     | infer(C.PURE(P.getcon,[v],x,_,k), hp) =
258 :     (getcon(x, v); infer(k, hp))
259 :     | infer(C.PURE(P.getexn,[v],x,_,k), hp) =
260 :     (getexn(x, v); infer(k, hp))
261 :     | infer(C.PURE(P.recsubscript,[a,i],x,_,k), hp) =
262 :     (recsubscript(x,a,i); infer(k, hp))
263 :     | infer(C.PURE(P.raw64subscript,[a,i],x,_,k), hp) =
264 :     (raw64subscript(x,a,i); infer(k, hp))
265 : monnier 409 | infer(C.PURE(P.newarray0,_,x,cty,k),hp) =
266 : leunga 585 (newarray0(x,hp); infer(k,hp+array0Size))
267 : monnier 409 | infer(C.PURE(p,vs,x,cty,k),hp) = infer(k,hp)
268 : leunga 585
269 : monnier 409 | infer(C.ARITH(a,vs,x,cty,k),hp) = infer(k,hp)
270 :    
271 : leunga 585 (* Lookers *)
272 :     | infer(C.LOOKER(P.!,[v],x,_,k),hp) = (deref(x,v); infer(k,hp))
273 :     | infer(C.LOOKER(P.gethdlr,[],x,_,k),hp) = (gethdlr x; infer(k,hp))
274 :     | infer(C.LOOKER(P.subscript,[a,i],x,_,k),hp) =
275 :     (subscript(x,a,i); infer(k,hp))
276 :     | infer(C.LOOKER(P.numsubscript{kind=P.INT 8},[a,i],x,_,k),hp) =
277 :     (numsubscript8(x,a,i); infer(k,hp))
278 :     | infer(C.LOOKER(P.numsubscript{kind=P.FLOAT 64},[a,i],x,_,k),hp) =
279 :     (numsubscriptf64(x,a,i); infer(k,hp))
280 :    
281 :     | infer(C.LOOKER(P.getvar,[],x,_,k),hp) = (getvar x; infer(k,hp))
282 :    
283 :     | infer(C.LOOKER(P.deflvar,[],x,cty,k),hp) = infer(k,hp) (* nop! *)
284 :    
285 :     (* Setters *)
286 :     | infer(C.SETTER(P.assign, [a,v], k),hp) =
287 :     (assign(a,v); infer(k,hp+storeListSize))
288 :     | infer(C.SETTER(P.unboxedassign, [a,v], k),hp) =
289 :     (unboxedassign(a,v); infer(k,hp))
290 :     | infer(C.SETTER(P.update, [a,i,v], k),hp) =
291 :     (update(a,i,v); infer(k,hp+storeListSize))
292 : leunga 590 | infer(C.SETTER(P.boxedupdate, [a,i,v], k), hp) =
293 : leunga 585 (boxedupdate(a,i,v); infer(k,hp+storeListSize))
294 : leunga 590 | infer(C.SETTER(P.unboxedupdate, [a,i,v], k), hp) =
295 :     (unboxedupdate(a,i,v); infer(k,hp))
296 : leunga 585 | infer(C.SETTER(P.numupdate{kind=P.INT _}, [a,i,v], k),hp) =
297 :     (numupdate(a,i,v); infer(k,hp))
298 :     | infer(C.SETTER(P.numupdate{kind=P.FLOAT 64}, [a,i,v], k),hp) =
299 :     (numupdateF64(a,i,v); infer(k,hp))
300 :    
301 :     | infer(C.SETTER(P.sethdlr, [x], k), hp) = (sethdlr x; infer(k,hp))
302 :     | infer(C.SETTER(P.setvar, [x], k), hp) = (setvar x; infer(k,hp))
303 :    
304 :     (* Apparently these are nops (see MLRiscGen.sml) *)
305 :     | infer(C.SETTER(P.uselvar, [x], k), hp) = infer(k, hp)
306 :     | infer(C.SETTER(P.acclink, _, k), hp) = infer(k, hp)
307 :     | infer(C.SETTER(P.setmark, _, k), hp) = infer(k, hp)
308 :     | infer(C.SETTER(P.free, [x], k), hp) = infer(k, hp)
309 :    
310 :     | infer(C.SETTER(P.setpseudo, _, k), hp) =
311 :     (print "setpseudo not implemented\n"; infer(k, hp))
312 :     | infer(e, hp) =
313 :     (PPCps.prcps e; print "\n"; error "infer")
314 :    
315 : monnier 409 and infers([],hp) = ()
316 :     | infers(k::ks,hp) = (infer(k,hp); infers(ks,hp))
317 :     in infer(cexp, 0)
318 :     end
319 :    
320 :     in if !Control.CG.memDisambiguate then
321 : leunga 585 (CPSRegions.reset();
322 :     app defineFunction cpsFunctions;
323 :     app process cpsFunctions;
324 : monnier 409 fn r => look r handle _ => top
325 :     )
326 :     else
327 :     (fn _ => top)
328 : leunga 585 end
329 : monnier 409 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0