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/src/compiler/FLINT/opt/fixfix.sml
ViewVC logotype

Annotation of /sml/trunk/src/compiler/FLINT/opt/fixfix.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 255 - (view) (download)

1 : monnier 122 (* copyright 1998 YALE FLINT PROJECT *)
2 : monnier 160 (* monnier@cs.yale.edu *)
3 : monnier 122
4 :     (* This module does various FIX-related transformations:
5 :     * - FIXes are split into their strongly-connected components
6 :     * - small non-recursive functions are marked inlinable
7 : monnier 160 * - curried functions are uncurried
8 : monnier 122 *)
9 :    
10 :     signature FIXFIX =
11 :     sig
12 :     val fixfix : FLINT.prog -> FLINT.prog
13 :     end
14 :    
15 : monnier 160 (* Maybe later:
16 : monnier 122 * - hoisting of inner functions out of their englobing function
17 :     * so that the outer function becomes smaller, giving more opportunity
18 :     * for inlining.
19 : monnier 160 * - eta expand escaping functions
20 :     * - loop-preheader introduction
21 : monnier 122 *)
22 :    
23 :     structure FixFix :> FIXFIX =
24 :     struct
25 :    
26 :     local
27 :     structure F = FLINT
28 :     structure S = IntSetF
29 :     structure M = IntmapF
30 :     structure PP = PPFlint
31 :     structure LT = LtyExtern
32 : monnier 220 structure LK = LtyKernel
33 : monnier 163 structure OU = OptUtils
34 : monnier 220 structure CTRL = FLINT_Control
35 : monnier 122 in
36 :    
37 : monnier 220 val say = Control_Print.say
38 : monnier 122 fun bug msg = ErrorMsg.impossible ("FixFix: "^msg)
39 :     fun buglexp (msg,le) = (say "\n"; PP.printLexp le; say " "; bug msg)
40 :     fun bugval (msg,v) = (say "\n"; PP.printSval v; say " "; bug msg)
41 :     fun assert p = if p then () else bug ("assertion failed")
42 :    
43 : monnier 160 val cplv = LambdaVar.dupLvar
44 :    
45 : monnier 162 (* to limit the amount of uncurrying *)
46 : monnier 184 val maxargs = CTRL.maxargs
47 : monnier 162
48 : monnier 122 structure SccNode = struct
49 :     type node = LambdaVar.lvar
50 :     val eq = (op =)
51 :     val lt = (op <)
52 :     end
53 :     structure SCC = SCCUtilFun (structure Node = SccNode)
54 :    
55 : monnier 213 datatype info = Fun of int ref
56 :     | Arg of int * (int * int) ref
57 :    
58 : monnier 202 (* fexp: int ref intmapf -> lexp) -> (int * intset * lexp)
59 :     * The intmap contains refs to counters. The meaning of the counters
60 :     * is slightly overloaded:
61 :     * - if the counter is negative, it means the lvar
62 :     * is a locally known function and the counter's absolute value denotes
63 :     * the number of calls (off by one to make sure it's always negative).
64 :     * - else, it indicates that the lvar is a
65 :     * function argument and the absolute value is a (fuzzily defined) measure
66 :     * of the reduction in code size/speed that would result from knowing
67 :     * its value (might be used to decide whether or not duplicating code is
68 :     * desirable at a specific call site).
69 : monnier 122 * The three subparts returned are:
70 :     * - the size of lexp
71 :     * - the set of freevariables of lexp (plus the ones passed as arguments
72 :     * which are assumed to be the freevars of the continuation of lexp)
73 :     * - a new lexp with FIXes rewritten.
74 :     *)
75 : monnier 213 fun fexp mf depth lexp = let
76 : monnier 122
77 : monnier 213 val loop = fexp mf depth
78 : monnier 202
79 :     fun lookup (F.VAR lv) = M.lookup mf lv
80 :     | lookup _ = raise M.IntmapF
81 :    
82 : monnier 122 fun addv (s,F.VAR lv) = S.add(lv, s)
83 :     | addv (s,_) = s
84 :     fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
85 :     fun rmvs (s,lvs) = foldl S.rmv s lvs
86 :    
87 :     (* Looks for free vars in the primop descriptor.
88 :     * This is normally unnecessary since these are special vars anyway *)
89 :     fun fpo (fv,(NONE:F.dict option,po,lty,tycs)) = fv
90 :     | fpo (fv,(SOME{default,table},po,lty,tycs)) =
91 :     addvs(addv(fv, F.VAR default), map (F.VAR o #2) table)
92 :    
93 :     (* Looks for free vars in the primop descriptor.
94 :     * This is normally unnecessary since these are exception vars anyway *)
95 :     fun fdcon (fv,(s,Access.EXN(Access.LVAR lv),lty)) = addv(fv, F.VAR lv)
96 :     | fdcon (fv,_) = fv
97 :    
98 : monnier 162 (* recognize the curried essence of a function.
99 : monnier 220 * - hd:fkind option identifies the head of the curried function
100 : monnier 162 * - na:int gives the number of args still allowed *)
101 : monnier 220 fun curry (hd,na)
102 :     (le as (F.FIX([(fk as {inline=F.IH_SAFE,...},f,args,body)],
103 :     F.RET[F.VAR lv]))) =
104 :     if lv = f andalso na >= length args then
105 :     case (hd,fk)
106 :     of ((* (SOME{isrec=NONE,...},{isrec=SOME _,...}) | *)
107 :     (SOME{cconv=F.CC_FCT,...},{cconv=F.CC_FUN _,...}) |
108 :     (SOME{cconv=F.CC_FUN _,...},{cconv=F.CC_FCT,...})) =>
109 :     ([], le)
110 :     (* | ((NONE,_) |
111 :     (SOME{isrec=SOME _,...},_) |
112 :     (SOME{isrec=NONE,...},{isrec=NONE,...})) => *)
113 :     (* recursive functions are only accepted for uncurrying
114 :     * if they are the head of the function or if the head
115 :     * is already recursive *)
116 :     | _ =>
117 :     let val (funs,body) = curry (SOME fk, na - (length args)) body
118 : monnier 184 in ((fk,f,args)::funs,body)
119 : monnier 122 end
120 :     else
121 :     (* this "never" occurs, but dead-code removal is not bullet-proof *)
122 : monnier 164 ([], le)
123 : monnier 220 | curry _ le = ([], le)
124 : monnier 122
125 : monnier 220 exception Uncurryable
126 :    
127 : monnier 122 (* do the actual uncurrying *)
128 :     fun uncurry (args as (fk,f,fargs)::_::_,body) =
129 : monnier 160 let val f' = cplv f (* the new fun name *)
130 : monnier 122
131 : monnier 184 (* find the rtys of the uncurried function *)
132 :     fun getrtypes (({isrec=SOME(rtys,_),...}:F.fkind,_,_),_) = SOME rtys
133 :     | getrtypes ((_,_,_),rtys) =
134 :     Option.map (fn [lty] => #2(LT.ltd_fkfun lty)
135 :     | _ => bug "strange isrec") rtys
136 : monnier 122
137 :     (* create the new fkinds *)
138 : monnier 220 val ncconv = case #cconv(#1(List.last args)) of
139 :     F.CC_FUN(LK.FF_VAR(_,raw)) => F.CC_FUN(LK.FF_VAR(true, raw))
140 :     | cconv => cconv
141 : monnier 184 val (nfk,nfk') = OU.fk_wrap(fk, foldl getrtypes NONE args)
142 : monnier 220 val nfk' = {inline= #inline nfk', isrec= #isrec nfk',
143 :     known= #known nfk', cconv= ncconv}
144 : monnier 122
145 :     (* funarg renaming *)
146 : monnier 160 fun newargs fargs = map (fn (a,t) => (cplv a,t)) fargs
147 : monnier 122
148 :     (* create (curried) wrappers to be inlined *)
149 :     fun recurry ([],args) = F.APP(F.VAR f', map (F.VAR o #1) args)
150 : monnier 184 | recurry (({inline,isrec,known,cconv},f,fargs)::rest,args) =
151 :     let val fk = {inline=F.IH_ALWAYS, isrec=NONE,
152 :     known=known, cconv=cconv}
153 : monnier 122 val nfargs = newargs fargs
154 : monnier 160 val g = cplv f'
155 : monnier 122 in F.FIX([(fk, g, nfargs, recurry(rest, args @ nfargs))],
156 :     F.RET[F.VAR g])
157 :     end
158 :    
159 :     (* build the new f fundec *)
160 :     val nfargs = newargs fargs
161 :     val nf = (nfk, f, nfargs, recurry(tl args, nfargs))
162 :    
163 :     (* make up the body of the uncurried function (creating
164 :     * dummy wrappers for the intermediate functions that are now
165 :     * useless).
166 :     * Intermediate functions that were not marked as recursive
167 :     * cannot appear in the body, so we don't need to build them.
168 :     * Note that we can't just rely on dead-code elimination to remove
169 :     * them because we may not be able to create them correctly with
170 :     * the limited type information gleaned in this phase. *)
171 :     fun uncurry' ([],args) = body
172 :     | uncurry' ((fk,f,fargs)::rest,args) =
173 :     let val le = uncurry'(rest, args @ fargs)
174 :     in case fk
175 : monnier 184 of {isrec=SOME _,cconv,known,inline} =>
176 : monnier 122 let val nfargs = newargs fargs
177 : monnier 184 val fk = {isrec=NONE, inline=F.IH_ALWAYS,
178 :     known=known, cconv=cconv}
179 : monnier 122 in F.FIX([(fk, f, nfargs,
180 :     recurry(rest, args @ nfargs))],
181 :     le)
182 :     end
183 :     | _ => le
184 :     end
185 :    
186 :     (* the new f' fundec *)
187 :     val nfbody' = uncurry'(tl args, fargs)
188 :     val nf' = (nfk', f', foldr (op @) [] (map #3 args), nfbody')
189 :    
190 :     in (nf, nf')
191 :     end
192 :     | uncurry (_,body) = bug "uncurrying a non-curried function"
193 :    
194 :     in case lexp
195 : monnier 202 of F.RET vs => (0, addvs(S.empty, vs), lexp)
196 :     | F.LET (lvs,body,le) =>
197 :     let val (s2,fvl,nle) = loop le
198 :     val (s1,fvb,nbody) = loop body
199 :     in (s1 + s2, S.union(rmvs(fvl, lvs), fvb), F.LET(lvs, nbody, nle))
200 : monnier 122 end
201 :     | F.FIX (fdecs,le) =>
202 :     let val funs = S.make(map #2 fdecs) (* set of funs defined by the FIX *)
203 :    
204 : monnier 202 (* create call-counters for each fun and add them to fm *)
205 :     val (fs,mf) = foldl (fn ((fk,f,args,body),(fs,mf)) =>
206 : monnier 213 let val c = ref 0
207 :     in ((fk, f, args, body, c)::fs,
208 :     M.add(mf, f, Fun c))
209 : monnier 202 end)
210 :     ([],mf)
211 :     fdecs
212 : monnier 122
213 :     (* process each fun *)
214 : monnier 202 fun ffun (fdec as (fk as {isrec,...}:F.fkind,f,args,body,cf),
215 : monnier 199 (s,fv,funs,m)) =
216 : monnier 220 case curry (NONE,!maxargs)
217 : monnier 202 (F.FIX([(fk,f,args,body)], F.RET[F.VAR f]))
218 : monnier 122 of (args as _::_::_,body) => (* curried function *)
219 :     let val ((fk,f,fargs,fbody),(fk',f',fargs',fbody')) =
220 :     uncurry(args,body)
221 :     (* add the wrapper function *)
222 : monnier 213 val cs = map (fn _ => ref(0,0)) fargs
223 : monnier 202 val nm = M.add(m, f, ([f'], 1, fk, fargs, fbody, cf, cs))
224 : monnier 122 (* now, retry ffun with the uncurried function *)
225 : monnier 202 in ffun((fk', f', fargs', fbody', ref 1),
226 : monnier 122 (s+1, fv, S.add(f', funs), nm))
227 :     end
228 :     | _ => (* non-curried function *)
229 : monnier 213 let val newdepth =
230 :     case isrec
231 :     of SOME(_,(F.LK_TAIL | F.LK_LOOP)) => depth + 1
232 :     | _ => depth
233 :     val (mf,cs) = foldr (fn ((v,t),(m,cs)) =>
234 :     let val c = ref(0, 0)
235 :     in (M.add(m, v, Arg(newdepth, c)),
236 :     c::cs) end)
237 : monnier 202 (mf,[]) args
238 : monnier 213 val (fs,ffv,body) = fexp mf newdepth body
239 : monnier 122 val ffv = rmvs(ffv, map #1 args) (* fun's freevars *)
240 :     val ifv = S.inter(ffv, funs) (* set of rec funs ref'ed *)
241 :     in
242 :     (fs + s, S.union(ffv, fv), funs,
243 : monnier 213 M.add(m,f,(S.members ifv, fs, fk, args, body, cf, cs)))
244 : monnier 122 end
245 :    
246 : monnier 202 (* process the main lexp and make it into a dummy function.
247 :     * The computation of the freevars is a little sloppy since `fv'
248 :     * includes freevars of the continuation, but the uniqueness
249 :     * of varnames ensures that S.inter(fv, funs) gives the correct
250 :     * result nonetheless. *)
251 : monnier 213 val (s,fv,le) = fexp mf depth le
252 : monnier 202 val lename = LambdaVar.mkLvar()
253 :     val m = M.singleton(lename, (S.members(S.inter(fv, funs)), 0,
254 :     {inline=F.IH_SAFE, isrec=NONE,
255 :     known=true,cconv=F.CC_FCT},
256 :     [], le, ref 0, []))
257 :    
258 : monnier 122 (* process the functions, collecting them in map m *)
259 : monnier 202 val (s,fv,funs,m) = foldl ffun (s, fv, funs, m) fs
260 : monnier 122
261 :     (* find strongly connected components *)
262 :     val top = SCC.topOrder{root=lename, follow= #1 o (M.lookup m)}
263 :    
264 :     (* turns them back into flint code *)
265 : monnier 202 fun sccSimple f (_,s,{isrec,cconv,known,inline},args,body,cf,cs) =
266 :     let (* small functions inlining heuristic *)
267 :     val ilthreshold = !CTRL.inlineThreshold + (length args)
268 :     val ilh =
269 :     if inline = F.IH_ALWAYS then inline
270 : monnier 213 (* else if s < ilthreshold then F.IH_ALWAYS *)
271 :     else let val cs = map (fn ref(sp,ti) => sp + ti div 2) cs
272 : monnier 202 val s' = foldl (op+) 0 cs
273 :     in if s < 2*s' + ilthreshold
274 : monnier 216 then ((*; say((Collect.LVarString f)^
275 :     " {"^(Int.toString(!cf))^
276 :     "} = F.IH_MAYBE "^
277 :     (Int.toString (s-ilthreshold))^
278 :     (foldl (fn (i,s) => s^" "^
279 :     (Int.toString i))
280 :     "" cs)^"\n"); *)
281 : monnier 202 F.IH_MAYBE (s-ilthreshold, cs))
282 :     else inline
283 : monnier 184 end
284 : monnier 202 val fk = {isrec=NONE, inline=ilh, known=known, cconv=cconv}
285 :     in (fk, f, args, body)
286 : monnier 122 end
287 : monnier 202 fun sccRec f (_,s,fk as {isrec,cconv,known,inline},args,body,cf,cs) =
288 :     let val fk' =
289 :     (* let's check for unroll opportunities.
290 :     * This heuristic is pretty bad since it doesn't
291 :     * take the number of rec-calls into account *)
292 :     case (isrec,inline)
293 :     of (SOME(_,(F.LK_LOOP|F.LK_TAIL)),F.IH_SAFE) =>
294 :     if s < !CTRL.unrollThreshold then
295 :     {inline=F.IH_UNROLL, isrec=isrec,
296 :     cconv=cconv, known=known}
297 :     else fk
298 :     | _ => fk
299 :     in (fk, f, args, body)
300 :     end
301 :     fun sccconvert (SCC.SIMPLE f,le) =
302 :     F.FIX([sccSimple f (M.lookup m f)], le)
303 : monnier 122 | sccconvert (SCC.RECURSIVE fs,le) =
304 : monnier 202 F.FIX(map (fn f => sccRec f (M.lookup m f)) fs, le)
305 : monnier 122 in
306 :     case top
307 :     of (SCC.SIMPLE f)::sccs =>
308 :     (assert(f = lename);
309 :     (s, S.diff(fv, funs), foldl sccconvert le sccs))
310 :     | (SCC.RECURSIVE _)::_ => bug "recursive main body in SCC ?!?!?"
311 :     | [] => bug "SCC going crazy"
312 :     end
313 : monnier 202 | F.APP (F.VAR f,args) =>
314 :     (* For known functions, increase the counter and
315 :     * make the call a bit cheaper. *)
316 :     let val scall =
317 : monnier 213 (case M.lookup mf f
318 :     of Fun(fc as ref c) => (fc := c + 1; 1)
319 :     | Arg(d, ac as ref (sp,ti)) =>
320 :     (ac := (4 + sp, OU.pow2(depth - d) * 30 + ti); 5))
321 :     handle M.IntmapF => 5
322 :     in
323 : monnier 202 (scall + (length args), addvs(S.singleton f, args), lexp)
324 :     end
325 : monnier 220 | F.TFN ((tfk,f,args,body),le) =>
326 : monnier 202 let val (se,fve,le) = loop le
327 :     val (sb,fvb,body) = loop body
328 : monnier 220 in (sb + se, S.union(S.rmv(f, fve), fvb),
329 :     F.TFN((tfk, f, args, body), le))
330 : monnier 122 end
331 : monnier 202 | F.TAPP (F.VAR f,args) =>
332 : monnier 122 (* The cost of TAPP is kinda hard to estimate. It can be very cheap,
333 :     * and just return a function, or it might do all kinds of wrapping
334 :     * but we have almost no information on which to base our choice.
335 :     * We opted for cheap here, to try to inline them more (they might
336 :     * become cheaper once inlined) *)
337 : monnier 202 (3, S.singleton f, lexp)
338 : monnier 122 | F.SWITCH (v,ac,arms,def) =>
339 :     let fun farm (dcon as F.DATAcon(dc,_,lv),le) =
340 :     (* the binding might end up costly, but we count it as 1 *)
341 : monnier 202 let val (s,fv,le) = loop le
342 : monnier 122 in (1+s, fdcon(S.rmv(lv, fv),dc), (dcon, le))
343 :     end
344 :     | farm (dc,le) =
345 : monnier 202 let val (s,fv,le) = loop le in (s, fv, (dc, le)) end
346 : monnier 213 val narms = length arms
347 : monnier 202 val (s,smax,fv,arms) =
348 :     foldl (fn ((s1,fv1,arm),(s2,smax,fv2,arms)) =>
349 :     (s1+s2, Int.max(s1,smax), S.union(fv1, fv2), arm::arms))
350 : monnier 213 (narms, 0, S.empty, []) (map farm arms)
351 :     in (case lookup v
352 :     of Arg(d,ac as ref(sp,ti)) =>
353 :     ac := (sp + s - smax + narms, OU.pow2(depth - d) * 2 + ti)
354 :     | _ => ()) handle M.IntmapF => ();
355 : monnier 202 case def
356 : monnier 213 of NONE => (s, fv, F.SWITCH(v, ac, arms, NONE))
357 :     | SOME le => let val (sd,fvd,le) = loop le
358 :     in (s+sd, S.union(fv, fvd), F.SWITCH(v, ac, arms, SOME le))
359 :     end
360 : monnier 122 end
361 :     | F.CON (dc,tycs,v,lv,le) =>
362 : monnier 202 let val (s,fv,le) = loop le
363 : monnier 122 in (2+s, fdcon(addv(S.rmv(lv, fv), v),dc), F.CON(dc, tycs, v, lv, le))
364 :     end
365 :     | F.RECORD (rk,vs,lv,le) =>
366 : monnier 202 let val (s,fv,le) = loop le
367 : monnier 122 in ((length vs)+s, addvs(S.rmv(lv, fv), vs), F.RECORD(rk, vs, lv, le))
368 :     end
369 :     | F.SELECT (v,i,lv,le) =>
370 : monnier 202 let val (s,fv,le) = loop le
371 : monnier 213 in (case lookup v
372 :     of Arg(d,ac as ref(sp,ti)) =>
373 :     ac := (sp + 1, OU.pow2(depth - d) + ti)
374 :     | _ => ()) handle M.IntmapF=>();
375 : monnier 202 (1+s, addv(S.rmv(lv, fv), v), F.SELECT(v,i,lv,le))
376 : monnier 122 end
377 : monnier 255 | F.RAISE (F.VAR v,ltys) =>
378 :     (* artificially high size estimate to discourage inlining *)
379 :     (15, S.singleton v, lexp)
380 : monnier 122 | F.HANDLE (le,v) =>
381 : monnier 202 let val (s,fv,le) = loop le
382 : monnier 122 in (2+s, addv(fv, v), F.HANDLE(le,v))
383 :     end
384 :     | F.BRANCH (po,vs,le1,le2) =>
385 : monnier 202 let val (s1,fv1,le1) = loop le1
386 :     val (s2,fv2,le2) = loop le2
387 : monnier 122 in (1+s1+s2, fpo(addvs(S.union(fv1, fv2), vs), po),
388 :     F.BRANCH(po, vs, le1, le2))
389 :     end
390 :     | F.PRIMOP (po,vs,lv,le) =>
391 : monnier 202 let val (s,fv,le) = loop le
392 : monnier 122 in (1+s, fpo(addvs(S.rmv(lv, fv), vs),po), F.PRIMOP(po,vs,lv,le))
393 :     end
394 : monnier 202
395 :     | F.APP _ => bug "bogus F.APP"
396 :     | F.TAPP _ => bug "bogus F.TAPP"
397 :     | F.RAISE _ => bug "bogus F.RAISE"
398 : monnier 122 end
399 :    
400 :     fun fixfix ((fk,f,args,body):F.prog) =
401 : monnier 213 let val (s,fv,nbody) = fexp M.empty 0 body
402 : monnier 122 val fv = S.diff(fv, S.make(map #1 args))
403 :     in
404 :     (* PPFlint.printLexp(F.RET(map F.VAR (S.members fv))); *)
405 :     assert(S.isEmpty(fv));
406 :     (fk, f, args, nbody)
407 :     end
408 :    
409 :     end
410 :     end

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