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 773 - (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 : blume 773 | sizeOf(C.RCC(_,_,_,_,k),hp) = sizeOf(k,hp)
103 : monnier 409
104 :     and sizeOfs([],hp) = hp
105 :     | sizeOfs(k::ks,hp) = Int.max(sizeOf(k,hp),sizeOfs(ks,hp))
106 :    
107 : blume 733 val locMap = IntHashTable.mkTable(37,NotFound) (* lvar -> loc *)
108 :     val look = IntHashTable.lookup locMap
109 :     val bind = IntHashTable.insert locMap
110 : monnier 409
111 :     val newMem = Cells.newCell Cells.MEM
112 :    
113 : leunga 585 val _ = PT.reset newMem
114 : monnier 409
115 : leunga 585 fun newRef _ = ref(PT.SCELL(newMem(),ref []))
116 :    
117 :     val exnptr = PT.newSRef() (* exception handler *)
118 :     val varptr = PT.newSRef() (* var ptr *)
119 :    
120 : monnier 409 fun lookup x =
121 :     look x handle _ =>
122 : leunga 585 let val r = newRef() in bind(x,r); r end
123 : monnier 409
124 : leunga 585
125 :     fun defineFunction(fk, f, args, _, cexp) =
126 :     let val xs =
127 :     map (fn x => let val r = newRef() in bind(x,r); r end) args
128 :     in bind(f, PT.mkLambda xs) end
129 :    
130 :     val off0 = C.OFFp 0
131 :    
132 : monnier 409 fun process(fk, f, args, _, cexp) =
133 : leunga 585 let (* create a table of allocation offset locations *)
134 : monnier 409 val table = Array.tabulate(sizeOf(cexp, 0) div 4, newRef)
135 :    
136 :     fun select(i,C.VAR v,x) = bind(x,PT.pi(lookup v,i))
137 :     | select(i,_,x) = ()
138 :    
139 :     fun offset(i,C.VAR v,x) = bind(x,PT.offset(lookup v,i))
140 :     | offset(i,_,x) = ()
141 :    
142 :     fun value (C.VAR v) = lookup v
143 :     | value _ = newRef()
144 :    
145 :     fun apply(C.VAR f,args) = PT.app(lookup f,map value args)
146 :     | apply _ = ()
147 :    
148 : leunga 585 fun getPath(v,C.OFFp 0) = value v
149 :     | getPath(v,C.OFFp n) = PT.offset(value v, n)
150 :     | getPath(v,C.SELp(n,path)) = PT.pi(getPath(v,path),n)
151 : monnier 409
152 : leunga 585 fun getPaths([],hp) = []
153 :     | getPaths((v,path)::vs,hp) =
154 :     let val r = Array.sub(table,hp)
155 :     val r' = getPath(v,path)
156 :     in PT.unify(r,r'); r::getPaths(vs,hp+1) end
157 : monnier 409
158 : leunga 585 fun getF64Paths([],hp) = []
159 :     | getF64Paths((v,path)::vs,hp) =
160 :     let val r1 = Array.sub(table,hp)
161 :     val r2 = Array.sub(table,hp+1)
162 :     val r' = getPath(v,path)
163 :     in PT.unify(r1,r'); PT.unify(r2,r');
164 :     r'::getF64Paths(vs,hp+2)
165 :     end
166 : monnier 409
167 : leunga 585 (* How to make a record *)
168 :     fun mkRec(f,getPaths,x,vs,hp) =
169 :     let val i = Word.toInt(Word.>>(Word.fromInt hp,0w2))
170 :     val r = f(SOME(Array.sub(table,i)),getPaths(vs,i+1))
171 :     in bind(x,r) end
172 :     fun mkFRecord(x,vs,hp) = mkRec(PT.mkRecord,getF64Paths,x,vs,hp)
173 :     fun mkVector(x,vs,hp) = mkRec(PT.mkRecord,getPaths,x,vs,hp)
174 :     fun mkNormalRecord(x,vs,hp) = mkRec(PT.mkRecord,getPaths,x,vs,hp)
175 :    
176 :     fun mkRecord(C.RK_FBLOCK,x,vs,hp) = mkFRecord(x,vs,hp)
177 :     | mkRecord(C.RK_FCONT,x,vs,hp) = mkFRecord(x,vs,hp)
178 :     | mkRecord(C.RK_VECTOR,x,vs,hp) = mkVector(x,vs,hp)
179 :     | mkRecord(_,x,vs,hp) = mkNormalRecord(x,vs,hp)
180 :    
181 : leunga 590 fun makeTop(m) = (PT.unify(m, top); top)
182 :    
183 : leunga 585 (* CPS Pure Primitives *)
184 : leunga 590 fun arrayptr v = PT.pi(value v, 0)
185 : leunga 585
186 :     fun mkspecial(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
187 :     fun fwrap(x,v,hp) = mkFRecord(x,[(v,off0)],hp)
188 :     fun i32wrap(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
189 :     fun makeref(x,v,hp) = mkNormalRecord(x,[(v,off0)],hp)
190 :     fun newarray0(x,hp) =
191 :     bind(x,PT.mkRecord(NONE,[PT.mkRecord(NONE,[])]))
192 :    
193 : leunga 590 fun objlength(x,v) = bind(x, PT.pi(value v, ~1))
194 :     fun length(x,v) = bind(x, PT.pi(value v, 1))
195 :     fun arraysub(x,a,i) = makeTop(PT.weakSubscript(arrayptr a))
196 : leunga 585 fun subscriptv(x,a,i) = arraysub(x,a,i)
197 :     fun subscript(x,a,i) = arraysub(x,a,i)
198 :     fun pure_numsubscript(x,a,i) = arraysub(x,a,i)
199 : leunga 590 fun gettag(x,v) = bind(x,PT.pi(value v, ~1))
200 : leunga 585 fun numsubscript8(x,a,i) = arraysub(x,a,i)
201 :     fun numsubscriptf64(x,a,i) = arraysub(x,a,i)
202 : leunga 590 fun getcon(x,v) = bind(x, PT.pi(value v,0))
203 :     fun getexn(x,v) = bind(x, PT.pi(value v,0))
204 : leunga 585 fun recsubscript(x,a,i) = arraysub(x,a,i)
205 :     fun raw64subscript(x,a,i) = arraysub(x,a,i)
206 :    
207 :     (* CPS Looker Primitives *)
208 : leunga 590 fun deref(x,v) = makeTop(PT.strongSubscript(value v, 0))
209 : leunga 585 fun gethdlr x = bind(x, PT.strongSubscript(exnptr, 0))
210 :     fun getvar x = bind(x, PT.strongSubscript(varptr, 0))
211 :    
212 :     (* CPS Setter Primitives *)
213 : leunga 590 fun supdate(a,x) = PT.strongUpdate(value a, 0, makeTop(value x))
214 :     fun wupdate(a,x) = PT.weakUpdate(value a, makeTop(value x))
215 : leunga 585
216 : leunga 590 fun arrayupdate(a,i,x) = PT.weakUpdate(arrayptr a,value x)
217 :    
218 : leunga 585 fun assign(a,x) = supdate(a,x)
219 :     fun unboxedassign(a,x) = supdate(a,x)
220 : leunga 590 fun update(a,i,x) = arrayupdate(a,i,x)
221 :     fun boxedupdate(a,i,x) = arrayupdate(a,i,x)
222 :     fun unboxedupdate(a,i,x) = arrayupdate(a,i,x)
223 :     fun numupdate(a,i,x) = arrayupdate(a,i,x)
224 :     fun numupdateF64(a,i,x) = arrayupdate(a,i,x)
225 : leunga 585 fun sethdlr x = PT.strongUpdate(exnptr, 0, value x)
226 :     fun setvar x = PT.strongUpdate(varptr, 0, value x)
227 :    
228 : blume 772 (* I don't know whether the following makes any sense...
229 :     * Basically, I want to ignore this aliasing analysis
230 :     * as far as raw access is concerned. (The invariant is
231 :     * that raw access NEVER occurs to any memory location
232 :     * that ML "knows" about. -- Blume (2000/1/1) *)
233 :     fun rawstore (a, x) = ()
234 :     fun rawload (a, x) = top
235 :    
236 : monnier 409 fun infer(C.RECORD(rk,vs,x,k),hp) =
237 : leunga 585 (mkRecord(rk,x,vs,hp); infer(k,allocRecord(rk,vs,hp)))
238 : monnier 409 | infer(C.SELECT(i,v,x,cty,k),hp) = (select(i,v,x); infer(k,hp))
239 :     | infer(C.OFFSET(i,v,x,k),hp) = (offset(i,v,x); infer(k,hp))
240 :     | infer(C.APP(f,vs),hp) = apply(f,vs)
241 :     | infer(C.FIX _,hp) = error "infer: FIX"
242 :     | infer(C.SWITCH(v,x,ks),hp) = infers(ks,hp)
243 :     | infer(C.BRANCH(p,_,x,k1,k2),hp) = (infer(k1,hp); infer(k2,hp))
244 : leunga 585
245 : monnier 409 (*
246 :     * These things are misnamed! There is nothing pure about them!
247 :     *)
248 : leunga 585 | infer(C.PURE(P.objlength, [v], x, _, k), hp) =
249 :     (objlength(x, v); infer(k, hp))
250 :     | infer(C.PURE(P.length, [v], x, _, k), hp) =
251 :     (length(x, v); infer(k, hp))
252 :     | infer(C.PURE(P.subscriptv,[a,i],x,_,k),hp) =
253 :     (subscriptv(x, a, i); infer(k, hp))
254 :     | infer(C.PURE(P.pure_numsubscript{kind=P.INT 8},[a,i],x,_,k),hp) =
255 :     (pure_numsubscript(x, a, i); infer(k, hp))
256 :     | infer(C.PURE(P.gettag, [v], x, _, k), hp) =
257 :     (gettag(x, v); infer(k, hp))
258 : monnier 409 | infer(C.PURE(P.mkspecial,[i,v],x,cty,k),hp) =
259 : leunga 585 (mkspecial(x,v,hp); infer(k,hp+8))
260 : monnier 409 | infer(C.PURE(P.makeref,[v],x,cty,k),hp) =
261 : leunga 585 (makeref(x,v,hp); infer(k,hp+8))
262 :     | infer(C.PURE(P.fwrap,[v],x,cty,k),hp) =
263 :     (fwrap(x,v,hp); infer(k,frecordSize(1,hp)))
264 :     | infer(C.PURE(P.i32wrap,[v],x,cty,k),hp) =
265 :     (i32wrap(x,v,hp); infer(k,hp+8))
266 :     | infer(C.PURE(P.getcon,[v],x,_,k), hp) =
267 :     (getcon(x, v); infer(k, hp))
268 :     | infer(C.PURE(P.getexn,[v],x,_,k), hp) =
269 :     (getexn(x, v); infer(k, hp))
270 :     | infer(C.PURE(P.recsubscript,[a,i],x,_,k), hp) =
271 :     (recsubscript(x,a,i); infer(k, hp))
272 :     | infer(C.PURE(P.raw64subscript,[a,i],x,_,k), hp) =
273 :     (raw64subscript(x,a,i); infer(k, hp))
274 : monnier 409 | infer(C.PURE(P.newarray0,_,x,cty,k),hp) =
275 : leunga 585 (newarray0(x,hp); infer(k,hp+array0Size))
276 : monnier 409 | infer(C.PURE(p,vs,x,cty,k),hp) = infer(k,hp)
277 : leunga 585
278 : monnier 409 | infer(C.ARITH(a,vs,x,cty,k),hp) = infer(k,hp)
279 :    
280 : leunga 585 (* Lookers *)
281 :     | infer(C.LOOKER(P.!,[v],x,_,k),hp) = (deref(x,v); infer(k,hp))
282 :     | infer(C.LOOKER(P.gethdlr,[],x,_,k),hp) = (gethdlr x; infer(k,hp))
283 :     | infer(C.LOOKER(P.subscript,[a,i],x,_,k),hp) =
284 :     (subscript(x,a,i); infer(k,hp))
285 :     | infer(C.LOOKER(P.numsubscript{kind=P.INT 8},[a,i],x,_,k),hp) =
286 :     (numsubscript8(x,a,i); infer(k,hp))
287 :     | infer(C.LOOKER(P.numsubscript{kind=P.FLOAT 64},[a,i],x,_,k),hp) =
288 :     (numsubscriptf64(x,a,i); infer(k,hp))
289 :    
290 :     | infer(C.LOOKER(P.getvar,[],x,_,k),hp) = (getvar x; infer(k,hp))
291 :    
292 :     | infer(C.LOOKER(P.deflvar,[],x,cty,k),hp) = infer(k,hp) (* nop! *)
293 : blume 772 | infer (C.LOOKER (P.rawload _, [a], x, _, k), hp) =
294 :     (rawload (x, a); infer(k,hp))
295 :    
296 : leunga 585 (* Setters *)
297 :     | infer(C.SETTER(P.assign, [a,v], k),hp) =
298 :     (assign(a,v); infer(k,hp+storeListSize))
299 :     | infer(C.SETTER(P.unboxedassign, [a,v], k),hp) =
300 :     (unboxedassign(a,v); infer(k,hp))
301 :     | infer(C.SETTER(P.update, [a,i,v], k),hp) =
302 :     (update(a,i,v); infer(k,hp+storeListSize))
303 : leunga 590 | infer(C.SETTER(P.boxedupdate, [a,i,v], k), hp) =
304 : leunga 585 (boxedupdate(a,i,v); infer(k,hp+storeListSize))
305 : leunga 590 | infer(C.SETTER(P.unboxedupdate, [a,i,v], k), hp) =
306 :     (unboxedupdate(a,i,v); infer(k,hp))
307 : leunga 585 | infer(C.SETTER(P.numupdate{kind=P.INT _}, [a,i,v], k),hp) =
308 :     (numupdate(a,i,v); infer(k,hp))
309 :     | infer(C.SETTER(P.numupdate{kind=P.FLOAT 64}, [a,i,v], k),hp) =
310 :     (numupdateF64(a,i,v); infer(k,hp))
311 :    
312 :     | infer(C.SETTER(P.sethdlr, [x], k), hp) = (sethdlr x; infer(k,hp))
313 :     | infer(C.SETTER(P.setvar, [x], k), hp) = (setvar x; infer(k,hp))
314 : blume 772 | infer (C.SETTER (P.rawstore _, [a, x], k), hp) =
315 :     (rawstore (a, x); infer (k, hp))
316 : leunga 585
317 :     (* Apparently these are nops (see MLRiscGen.sml) *)
318 :     | infer(C.SETTER(P.uselvar, [x], k), hp) = infer(k, hp)
319 :     | infer(C.SETTER(P.acclink, _, k), hp) = infer(k, hp)
320 :     | infer(C.SETTER(P.setmark, _, k), hp) = infer(k, hp)
321 :     | infer(C.SETTER(P.free, [x], k), hp) = infer(k, hp)
322 :    
323 :     | infer(C.SETTER(P.setpseudo, _, k), hp) =
324 :     (print "setpseudo not implemented\n"; infer(k, hp))
325 :     | infer(e, hp) =
326 :     (PPCps.prcps e; print "\n"; error "infer")
327 :    
328 : monnier 409 and infers([],hp) = ()
329 :     | infers(k::ks,hp) = (infer(k,hp); infers(ks,hp))
330 :     in infer(cexp, 0)
331 :     end
332 :    
333 :     in if !Control.CG.memDisambiguate then
334 : leunga 585 (CPSRegions.reset();
335 :     app defineFunction cpsFunctions;
336 :     app process cpsFunctions;
337 : monnier 409 fn r => look r handle _ => top
338 :     )
339 :     else
340 :     (fn _ => top)
341 : leunga 585 end
342 : monnier 409 end

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