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

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