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/fcontract.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (view) (download)

1 : monnier 121 (* copyright 1998 YALE FLINT PROJECT *)
2 : monnier 159 (* monnier@cs.yale.edu *)
3 : monnier 121
4 :     signature FCONTRACT =
5 :     sig
6 :    
7 :     (* needs Collect to be setup properly *)
8 :     val contract : FLINT.fundec -> FLINT.fundec
9 :    
10 :     end
11 :    
12 :     (* All kinds of beta-reductions. In order to do as much work per pass as
13 :     * possible, the usage counts of each variable (maintained by the Collect
14 :     * module) is kept as much uptodate as possible. For instance as soon as a
15 :     * variable becomes dead, all the variables that were referenced have their
16 :     * usage counts decremented correspondingly. This means that we have to
17 :     * be careful to make sure that a dead variable will indeed not appear
18 :     * in the output lexp since it might else reference other dead variables *)
19 :    
20 : monnier 159 (* things that fcontract does:
21 :     * - several things not mentioned
22 :     * - elimination of Con(Decon x)
23 :     * - update counts when selecting a SWITCH alternative
24 : monnier 162 * - contracting RECORD(R.1,R.2) => R (only if the type is easily available)
25 : monnier 184 * - dropping of dead arguments
26 :     * - elimination of constant arguments
27 : monnier 159 *)
28 :    
29 : monnier 121 (* things that lcontract.sml does that fcontract doesn't do (yet):
30 : monnier 159 * - inline across DeBruijn depths (will be solved by named-tvar)
31 : monnier 121 * - elimination of let [dead-vs] = pure in body
32 :     *)
33 :    
34 :     (* things that cpsopt/eta.sml did that fcontract doesn't do:
35 : monnier 159 * - let f vs = select(v,i,g,g vs)
36 : monnier 121 *)
37 :    
38 :     (* things that cpsopt/contract.sml did that fcontract doesn't do:
39 : monnier 159 * - IF-idiom (I still don't know what it is)
40 : monnier 121 * - unifying branches
41 :     * - Handler operations
42 :     * - primops expressions
43 :     * - branch expressions
44 :     *)
45 :    
46 :     (* things that could also be added:
47 : monnier 184 * - elimination of dead vars in let
48 : monnier 121 *)
49 :    
50 :     (* things that would require some type info:
51 :     * - dropping foo in LET vs = RAISE v IN foo
52 :     *)
53 :    
54 :     (* eta-reduction is tricky:
55 :     * - recognition of eta-redexes and introduction of the corresponding
56 :     * substitution in the table has to be done at the very beginning of
57 :     * the processing of the FIX
58 :     * - eta-reduction can turn a known function into an escaping function
59 :     * - fun f (g,v2,v3) = g(g,v2,v3) looks tremendously like an eta-redex
60 :     *)
61 :    
62 :     (* order of contraction is important:
63 :     * - the body of a FIX is contracted before the functions because the
64 :     * functions might end up being inlined in the body in which case they
65 :     * could be contracted twice.
66 :     *)
67 :    
68 :     (* When creating substitution f->g (as happens with eta redexes or with
69 :     * code like `LET [f] = RET[g]'), we need to make sure that the usage cout
70 :     * of f gets properly transfered to g. One way to do that is to make the
71 :     * transfer incremental: each time we apply the substitution, we decrement
72 :     * f's count and increment g's count. But this can be tricky since the
73 :     * elimination of the eta-redex (or the trivial binding) eliminates one of the
74 : monnier 159 * references to g and if this is the only one, we might trigger the killing
75 : monnier 121 * of g even though its count would be later incremented. Similarly, inlining
76 :     * of g would be dangerous as long as some references to f exist.
77 :     * So instead we do the transfer once and for all when we see the eta-redex,
78 :     * which frees us from those two problems but forces us to make sure that
79 :     * every existing reference to f will be substituted with g.
80 :     * Also, the transfer of counts from f to g is not quite straightforward
81 :     * since some of the references to f might be from inside g and without doing
82 :     * the transfer incrementally, we can't easily know which of the usage counts
83 :     * of f should be transfered to the internal counts of g and which to the
84 :     * external counts.
85 :     *)
86 :    
87 : monnier 159 (* Preventing infinite inlining:
88 :     * - inlining a function in its own body amounts to unrolling which has
89 :     * to be controlled (you only want to unroll some number of times).
90 :     * It's currently simply not allowed.
91 :     * - inlining a recursive function outside of tis body amounts to `peeling'
92 :     * one iteration. Here also, since the inlined body will have yet another
93 :     * call, the inlining risks non-termination. It's hence also
94 :     * not allowed.
95 :     * - inlining a mutually recursive function is just a more general form
96 :     * of the problem above although it can be safe and desirable in some cases.
97 :     * To be safe, you simply need that one of the functions forming the
98 :     * mutual-recursion loop cannot be inlined (to break the loop). This cannot
99 :     * be trivially checked. So we (foolishly?) trust the `inline' bit in
100 :     * those cases. This is mostly used to inline wrappers inside the
101 :     * function they wrap.
102 :     * - even if one only allows inlining of funtions showing no sign of
103 :     * recursion, we can be bitten by a program creating its own Y combinator:
104 :     * datatype dt = F of dt -> int -> int
105 :     * let fun f (F g) x = g (F g) x in f (F f) end
106 :     * To solve this problem, `cexp' has an `ifs' parameter containing the set
107 :     * of funtions that we are inlining in order to detect (and break) cycles.
108 :     * - funnily enough, if we allow inlining recursive functions the cycle
109 :     * detection will ensure that the unrolling (or peeling) will only be done
110 :     * once. In the future, maybe.
111 :     *)
112 :    
113 : monnier 184 (* Dropping useless arguments.
114 :     * Arguments whose value is constant (i.e. the function is known and each
115 :     * call site provides the same value for that argument (or the argument
116 :     * itself in the case of recursive calls) can be safely removed and replaced
117 :     * inside the body by a simple let binding. The only problem is that the
118 :     * constant argument might be out of scope at the function definition site.
119 :     * It is obviously always possible to move the function to bring the argument
120 :     * in scope, but since we don't do any code motion here, we're stuck.
121 :     * If it wasn't for this little problem, we could do the cst-arg removal in
122 :     * collect (we don't gain anything from doing it here).
123 :     * The removal of dead arguments (args not used in the body) on the other
124 :     * hand can quite well be done in collect, the only problem being that it
125 :     * is convenient to do it after the cst-arg removal so that we can rely
126 :     * on deadarg to do the actual removal of the cst-arg.
127 :     *)
128 :    
129 : monnier 121 (* Simple inlining (inlining called-once functions, which doesn't require
130 :     * alpha-renaming) seems inoffensive enough but is not always desirable.
131 : monnier 159 * The typical example is wrapper functions introduced by eta-expand: they
132 :     * usually (until inlined) contain the only call to the main function,
133 : monnier 121 * but inlining the main function in the wrapper defeats the purpose of the
134 :     * wrapper.
135 :     * cpsopt dealt with this problem by adding a `NO_INLINE_INTO' hint to the
136 : monnier 159 * wrapper function. In this file, the idea is the following:
137 :     * If you have a function declaration like `let f x = body in exp', first
138 :     * contract `exp' and only contract `body' afterwards. This ensures that
139 :     * the eta-wrapper gets a chance to be inlined before it is (potentially)
140 :     * eta-reduced away. Interesting details:
141 : monnier 121 * - all functions (even the ones that would have a `NO_INLINE_INTO') are
142 :     * contracted, because the "aggressive usage count maintenance" makes any
143 :     * alternative painful (the collect phase has already assumed that dead code
144 :     * will be eliminated, which means that fcontract should at the very least
145 : monnier 159 * do the dead-code elimination, so you can only avoid fcontracting a
146 :     * a function if you can be sure that the body doesn't contain any dead-code,
147 :     * which is generally not known).
148 : monnier 121 * - once a function is fcontracted it is marked as non-inlinable since
149 : monnier 159 * fcontraction might have changed its shape considerably (via inlining).
150 :     * This means that in the case of
151 :     * let fwrap x = body1 and f y = body2 in exp
152 :     * if fwrap is fcontracted before f, then fwrap cannot be inlined in f.
153 :     * To minimize the impact of this problem, we make sure that we fcontract
154 :     * inlinable functions only after fcontracting other mutually recursive
155 :     * functions.
156 : monnier 121 * - at the very end of the optimization phase, cpsopt had a special pass
157 :     * that ignored the `NO_INLINE_INTO' hint (since at this stage, inlining
158 :     * into it doesn't have any undesirable side effects any more). The present
159 :     * code doesn't need such a thing. On another hand, the cpsopt approach
160 :     * had the advantage of keeping the `inline' bit from one contract phase to
161 : monnier 159 * the next. If this ends up being important, one could add a global
162 : monnier 121 * "noinline" flag that could be set to true whenever fcontracting an
163 : monnier 159 * inlinable function (this would ensure that fcontracting such an inlinable
164 :     * function can only reduce its size, which would allow keeping the `inline'
165 :     * bit set after fcontracting).
166 : monnier 121 *)
167 :    
168 :     structure FContract :> FCONTRACT =
169 :     struct
170 :     local
171 :     structure F = FLINT
172 :     structure M = IntmapF
173 : monnier 159 structure S = IntSetF
174 : monnier 121 structure C = Collect
175 : monnier 184 structure O = Option
176 : monnier 121 structure DI = DebIndex
177 :     structure PP = PPFlint
178 : monnier 159 structure FU = FlintUtil
179 :     structure LT = LtyExtern
180 : monnier 163 structure OU = OptUtils
181 : monnier 159 structure CTRL = Control.FLINT
182 : monnier 121 in
183 :    
184 :     val say = Control.Print.say
185 :     fun bug msg = ErrorMsg.impossible ("FContract: "^msg)
186 :     fun buglexp (msg,le) = (say "\n"; PP.printLexp le; bug msg)
187 :     fun bugval (msg,v) = (say "\n"; PP.printSval v; bug msg)
188 :    
189 :     (* fun sayexn e = app say (map (fn s => s^" <- ") (SMLofNJ.exnHistory e)) *)
190 :    
191 :     fun ASSERT (true,_) = ()
192 :     | ASSERT (FALSE,msg) = bug ("assertion "^msg^" failed")
193 :    
194 : monnier 159 val cplv = LambdaVar.dupLvar
195 : monnier 121
196 :     datatype sval
197 :     = Val of F.value (* F.value should never be F.VAR lv *)
198 :     | Fun of F.lvar * F.lexp * (F.lvar * F.lty) list * F.fkind * DI.depth
199 :     | TFun of F.lvar * F.lexp * (F.tvar * F.tkind) list * DI.depth
200 :     | Record of F.lvar * F.value list
201 : monnier 159 | Con of F.lvar * F.value * F.dcon * F.tyc list
202 :     | Decon of F.lvar * F.value * F.dcon * F.tyc list
203 : monnier 121 | Select of F.lvar * F.value * int
204 :     | Var of F.lvar * F.lty option (* cop out case *)
205 :    
206 : monnier 159 fun sval2lty (Var(_,x)) = x
207 :     | sval2lty (Decon(_,_,(_,_,lty),tycs)) =
208 :     SOME(hd(#2 (LT.ltd_arrow (hd(LT.lt_inst(lty, tycs))))))
209 :     | sval2lty _ = NONE
210 : monnier 121
211 : monnier 159 fun tycs_eq ([],[]) = true
212 :     | tycs_eq (tyc1::tycs1,tyc2::tycs2) =
213 :     LT.tc_eqv(tyc1,tyc2) andalso tycs_eq(tycs1,tycs2)
214 :     | tycs_eq _ = false
215 : monnier 121
216 : monnier 185 fun contract (fdec as (_,f,_,_)) = let
217 :    
218 :     val inlineWitness = ref false
219 :    
220 : monnier 159 (* cfg: is used for deBruijn renumbering when inlining at different depths
221 :     * ifs (inlined functions): records which functions we're currently inlining
222 :     * in order to detect loops
223 :     * m: is a map lvars to their defining expressions (svals) *)
224 : monnier 184 fun cexp (cfg as (d,od)) ifs m le cont = let
225 : monnier 159
226 :     val loop = cexp cfg ifs
227 :    
228 : monnier 186 fun used lv = (C.usenb(C.get lv) > 0)
229 :     handle x =>
230 :     (say("while in FContract.used "^(C.LVarString lv)^"\n");
231 :     raise x)
232 : monnier 121
233 :     fun impurePO po = true (* if a PrimOP is pure or not *)
234 :    
235 :     fun eqConV (F.INTcon i1, F.INT i2) = i1 = i2
236 :     | eqConV (F.INT32con i1, F.INT32 i2) = i1 = i2
237 :     | eqConV (F.WORDcon i1, F.WORD i2) = i1 = i2
238 :     | eqConV (F.WORD32con i1, F.WORD32 i2) = i1 = i2
239 :     | eqConV (F.REALcon r1, F.REAL r2) = r1 = r2
240 :     | eqConV (F.STRINGcon s1, F.STRING s2) = s1 = s2
241 :     | eqConV (con,v) = bugval("unexpected comparison with val", v)
242 :    
243 :     fun lookup m lv = (M.lookup m lv)
244 :     (* handle e as M.IntmapF =>
245 :     (say "\nlooking up unbound ";
246 :     say (!PP.LVarString lv);
247 :     raise e) *)
248 :    
249 :     fun sval2val sv =
250 :     case sv
251 : monnier 159 of (Fun{1=lv,...} | TFun{1=lv,...} | Record{1=lv,...} | Decon{1=lv,...}
252 : monnier 121 | Con{1=lv,...} | Select{1=lv,...} | Var{1=lv,...}) => F.VAR lv
253 :     | Val v => v
254 :    
255 : monnier 163 fun val2sval m (F.VAR ov) =
256 : monnier 186 ((lookup m ov) handle x => ((* PP.printSval(F.VAR ov); *) raise x))
257 : monnier 121 | val2sval m v = Val v
258 :    
259 :     fun bugsv (msg,sv) = bugval(msg, sval2val sv)
260 :    
261 :     fun subst m ov = sval2val (lookup m ov)
262 :     val substval = sval2val o (val2sval m)
263 :     fun substvar lv =
264 :     case substval(F.VAR lv)
265 :     of F.VAR lv => lv
266 :     | v => bugval ("unexpected val", v)
267 :    
268 :     (* called when a variable becomes dead.
269 :     * it simply adjusts the use-counts *)
270 :     fun undertake m lv =
271 :     let val undertake = undertake m
272 :     in case lookup m lv
273 : monnier 186 of Var {1=nlv,...} => ()
274 : monnier 121 | Val v => ()
275 :     | Fun (lv,le,args,_,_) =>
276 : monnier 187 C.unuselexp undertake
277 :     (F.LET(map #1 args,
278 :     F.RET (map (fn _ => F.INT 0) args),
279 :     le))
280 :     | TFun{1=lv,2=le,...} =>
281 :     C.unuselexp undertake le
282 :     | (Select {2=v,...} | Con {2=v,...}) => unuseval m v
283 :     | Record {2=vs,...} => app (unuseval m) vs
284 : monnier 159 (* decon's are implicit so we can't get rid of them *)
285 :     | Decon _ => ()
286 : monnier 121 end
287 :     handle M.IntmapF =>
288 : monnier 186 (say("Unable to undertake "^(C.LVarString lv)^"\n"))
289 : monnier 121 | x =>
290 : monnier 186 (say("while undertaking "^(C.LVarString lv)^"\n"); raise x)
291 : monnier 121
292 : monnier 187 and unuseval m (F.VAR lv) =
293 :     if (C.unuse false (C.get lv)) then undertake m lv else ()
294 :     | unuseval f _ = ()
295 :     fun unusecall m lv =
296 :     if (C.unuse true (C.get lv)) then undertake m lv else ()
297 :    
298 :    
299 : monnier 121 fun addbind (m,lv,sv) = M.add(m, lv, sv)
300 :    
301 : monnier 164 (* substitute a value sv for a variable lv and unuse value v. *)
302 : monnier 121 fun substitute (m, lv1, sv, v) =
303 :     (case sval2val sv of F.VAR lv2 => C.transfer(lv1,lv2) | v2 => ();
304 : monnier 187 unuseval m v;
305 : monnier 121 addbind(m, lv1, sv)) handle x =>
306 : monnier 186 (say ("while substituting "^
307 : monnier 164 (C.LVarString lv1)^
308 :     " -> ");
309 : monnier 121 PP.printSval (sval2val sv);
310 :     raise x)
311 :    
312 :     (* common code for primops *)
313 :     fun cpo (SOME{default,table},po,lty,tycs) =
314 :     (SOME{default=substvar default,
315 :     table=map (fn (tycs,lv) => (tycs, substvar lv)) table},
316 :     po,lty,tycs)
317 :     | cpo po = po
318 :    
319 :     fun cdcon (s,Access.EXN(Access.LVAR lv),lty) =
320 :     (s, Access.EXN(Access.LVAR(substvar lv)), lty)
321 :     | cdcon dc = dc
322 :    
323 : monnier 184 fun zip ([],[]) = []
324 :     | zip (x::xs,y::ys) = (x,y)::(zip(xs,ys))
325 :     | zip _ = bug "bad zip"
326 : monnier 163
327 : monnier 159 (* F.APP inlining (if any)
328 :     * `ifs' is the set of function we are currently inlining
329 :     * `f' is the function, `vs' its arguments.
330 :     * return either (NONE, ifs) if inlining cannot be done or
331 :     * (SOME lexp, nifs) where `lexp' is the expansion of APP(f,vs) and
332 :     * `nifs' is the new set of functions we are currently inlining.
333 :     *)
334 :     fun inline ifs (f,vs) =
335 : monnier 121 case ((val2sval m f) handle x => raise x)
336 : monnier 184 of Fun(g,body,args,{inline,...},od) =>
337 : monnier 164 (ASSERT(used g, "used "^(C.LVarString g));
338 : monnier 184 if d <> od then (NONE, ifs)
339 : monnier 186 else if ((C.usenb(C.get g))handle x => raise x) = 1 andalso not(S.member ifs g) then
340 : monnier 121
341 : monnier 184 (* simple inlining: we should copy the body and then
342 :     * kill the function, but instead we just move the body
343 :     * and kill only the function name. This inlining strategy
344 :     * looks inoffensive enough, but still requires some care:
345 :     * see comments at the begining of this file and in cfun *)
346 : monnier 185 (inlineWitness := true;
347 : monnier 187 ignore(C.unuse true (C.get g));
348 : monnier 184 ASSERT(not (used g), "killed");
349 :     (SOME(F.LET(map #1 args, F.RET vs, body), od), ifs))
350 : monnier 121
351 :     (* aggressive inlining (but hopefully safe). We allow
352 :     * inlining for mutually recursive functions (isrec)
353 :     * despite the potential risk. The reason is that it can
354 :     * happen that a wrapper (that should be inlined) has to be made
355 :     * mutually recursive with its main function. On another hand,
356 :     * self recursion (C.recursive) is too dangerous to be inlined
357 : monnier 184 * except for loop unrolling *)
358 :     else if (inline = F.IH_ALWAYS andalso not(S.member ifs g)) orelse
359 :     (inline = F.IH_UNROLL andalso (S.member ifs g)) then
360 : monnier 163 let val nle =
361 : monnier 164 C.copylexp M.empty (F.LET(map #1 args, F.RET vs, body))
362 : monnier 184 in
363 : monnier 185 inlineWitness := true;
364 : monnier 184 (* say ("\nInlining "^(C.LVarString g)); *)
365 : monnier 187 (app (unuseval m) vs) handle x => raise x;
366 :     unusecall m g;
367 : monnier 184 (SOME(nle, od),
368 :     (* gross hack: to prevent further unrolling,
369 :     * I pretend that the rest is not inside the body *)
370 :     if inline = F.IH_UNROLL then S.rmv(g, ifs) else S.add(g, ifs))
371 : monnier 121 end
372 : monnier 159 else (NONE, ifs))
373 :     | sv => (NONE, ifs)
374 : monnier 121 in
375 :     case le
376 : monnier 184 of F.RET vs => cont(m, F.RET(map substval vs) handle x => raise x)
377 : monnier 121
378 :     | F.LET (lvs,le,body) =>
379 : monnier 184 let fun clet () =
380 :     loop m le
381 :     (fn (m,F.RET vs) =>
382 :     let fun simplesubst ((lv,v),m) =
383 :     let val sv = (val2sval m v) handle x => raise x
384 :     in substitute(m, lv, sv, sval2val sv)
385 :     end
386 :     val nm = (foldl simplesubst m (zip(lvs, vs)))
387 :     in loop nm body cont
388 :     end
389 :     | (m,nle) =>
390 :     let val nm = (foldl (fn (lv,m) =>
391 :     addbind(m, lv, Var(lv, NONE)))
392 :     m lvs)
393 :     in case loop nm body cont
394 :     of F.RET vs => if vs = (map F.VAR lvs) then nle
395 :     else F.LET(lvs, nle, F.RET vs)
396 :     | nbody => F.LET(lvs, nle, nbody)
397 :     end)
398 : monnier 121 in case le
399 : monnier 184 of F.BRANCH (po,vs,le1,le2) =>
400 :     (* this is a hack originally meant to cleanup the BRANCH mess
401 :     * introduced in flintnm (where each branch returns just true or
402 :     * false which is generally only used as input to a SWITCH).
403 :     * The present code does slightly more than clean up this case *)
404 : monnier 121 let fun known (F.RECORD(_,_,_,le)) = known le
405 :     | known (F.CON(_,_,_,v,F.RET[F.VAR v'])) = (v = v')
406 :     | known (F.RET[F.VAR v]) = false
407 :     | known (F.RET[_]) = true
408 :     | known _ = false
409 : monnier 184 fun cassoc (lv,v,body,wrap) =
410 : monnier 186 if lv = v andalso ((C.usenb(C.get lv)) handle x=> raise x) = 1 andalso
411 : monnier 121 known le1 andalso known le2 then
412 :     (* here I should also check that le1 != le2 *)
413 :     let val nle1 = F.LET([lv], le1, body)
414 : monnier 159 val nlv = cplv lv
415 : monnier 164 val _ = C.new NONE nlv
416 :     val body2 = C.copylexp (M.add(M.empty, lv, nlv))
417 :     body
418 : monnier 121 val nle2 = F.LET([nlv], le2, body2)
419 : monnier 164 in
420 : monnier 184 loop m (wrap(F.BRANCH(po, vs, nle1, nle2))) cont
421 : monnier 121 end
422 :     else
423 :     clet()
424 :     in case (lvs,body)
425 :     of ([lv],le as F.SWITCH(F.VAR v,_,_,NONE)) =>
426 : monnier 184 cassoc(lv, v, le, OU.id)
427 : monnier 121 | ([lv],F.LET(lvs,le as F.SWITCH(F.VAR v,_,_,NONE),rest)) =>
428 : monnier 184 cassoc(lv, v, le, fn le => F.LET(lvs,le,rest))
429 : monnier 121 | _ => clet()
430 :     end
431 : monnier 184 | _ => clet()
432 : monnier 121 end
433 : monnier 184
434 : monnier 121 | F.FIX (fs,le) =>
435 : monnier 164 let (* register dump bindings *)
436 :     val m = foldl (fn (fdec as (_,f,_,_),m) =>
437 :     addbind(m, f, Var(f,NONE)))
438 :     m fs
439 :    
440 :     (* The actual function contraction *)
441 :     fun cfun (m,[]:F.fundec list,acc) = acc
442 : monnier 184 | cfun (m,fdec as ({inline,cconv,known,isrec},f,args,body)::fs,acc) =
443 : monnier 121 if used f then
444 : monnier 164 let (* val _ = say ("\nEntering "^(C.LVarString f)) *)
445 : monnier 185 val oldWitness =
446 :     (!inlineWitness before inlineWitness := false)
447 : monnier 164 (* make up the bindings for args inside the body *)
448 : monnier 121 fun addnobind ((lv,lty),m) =
449 :     addbind(m, lv, Var(lv, SOME lty))
450 :     val nm = foldl addnobind m args
451 :     (* contract the body and create the resulting fundec *)
452 : monnier 184 val nbody = cexp cfg (S.add(f, ifs)) nm body #2
453 : monnier 185 (* if inlining took place, the body might be completely
454 :     * changed (read: bigger), so we have to reset the
455 :     * `inline' bit *)
456 : monnier 184 val nfk = {isrec=isrec, cconv=cconv,
457 : monnier 186 known=known orelse not(C.escaping(C.get f))handle x => raise x,
458 : monnier 185 inline=if !inlineWitness
459 :     then F.IH_SAFE
460 :     else (inline before
461 :     inlineWitness := oldWitness)}
462 : monnier 121 (* update the binding in the map. This step is not
463 :     * not just a mere optimization but is necessary
464 :     * because if we don't do it and the function
465 :     * gets inlined afterwards, the counts will reflect the
466 :     * new contracted code while we'll be working on the
467 :     * the old uncontracted code *)
468 :     val nm = addbind(m, f, Fun(f, nbody, args, nfk, od))
469 :     in cfun(nm, fs, (nfk, f, args, nbody)::acc)
470 : monnier 164 (* before say ("\nExiting "^(C.LVarString f)) *)
471 : monnier 121 end
472 :     else cfun(m, fs, acc)
473 :    
474 :     (* check for eta redex *)
475 : monnier 186 fun ceta (fdec as (fk,f,args,F.APP(g,vs)):F.fundec,(m,fs,hs)) =
476 : monnier 121 if vs = (map (F.VAR o #1) args) andalso
477 :     (* don't forget to check that g is not one of the args
478 :     * and not f itself either *)
479 :     (List.find (fn v => v = g) (F.VAR f::vs)) = NONE
480 :     then
481 :     let val svg = val2sval m g
482 :     val g = case sval2val svg
483 :     of F.VAR g => g
484 :     | v => bugval("not a variable", v)
485 :     (* NOTE: we don't want to turn a known function into an
486 :     * escaping one. It's dangerous for optimisations based
487 :     * on known functions (elimination of dead args, f.ex)
488 :     * and could generate cases where call>use in collect *)
489 : monnier 186 in if not (((C.escaping(C.get f))handle x => raise x) andalso not (C.escaping(C.get g))handle x => raise x)
490 : monnier 121 then let
491 :     (* if an earlier function h has been eta-reduced
492 :     * to f, we have to be careful to update its
493 :     * binding to not refer to f any more since f
494 :     * will disappear *)
495 :     val nm = foldl (fn (h,m) =>
496 :     if sval2val(lookup m h) = F.VAR f
497 :     then addbind(m, h, svg) else m)
498 :     m hs
499 : monnier 164 in
500 :     (* I could almost reuse `substitute' but the
501 :     * unuse in substitute assumes the val is escaping *)
502 :     C.transfer(f, g);
503 : monnier 187 unusecall m g;
504 : monnier 186 (addbind(m, f, svg), fs, f::hs)
505 : monnier 121 end
506 : monnier 163 (* the default case could ensure the inline *)
507 : monnier 186 else (m, fdec::fs, hs)
508 : monnier 121 end
509 : monnier 186 else (m, fdec::fs, hs)
510 :     | ceta (fdec,(m,fs,hs)) = (m,fdec::fs,hs)
511 : monnier 121
512 : monnier 164 (* drop constant arguments if possible *)
513 : monnier 184 fun cstargs (f as ({inline=F.IH_ALWAYS,...},_,_,_):F.fundec) = f
514 :     | cstargs (f as (fk,g,args,body):F.fundec) =
515 : monnier 186 let val actuals = (C.actuals (C.get g)) handle x => raise x
516 :     val cst =
517 : monnier 184 ListPair.map
518 :     (fn (NONE,_) => false
519 : monnier 186 | (SOME v,(a,_)) =>
520 :     ((case substval v
521 :     of F.VAR lv =>
522 :     if used a andalso used lv then
523 :     (C.use NONE (C.get lv); true)
524 :     else false
525 :     | _ => false)
526 :     handle M.IntmapF => false))
527 :     (actuals, args)
528 : monnier 184 (* if all args are used, there's nothing we can do *)
529 :     in if List.all not cst then f else
530 :     let fun newarg lv =
531 :     let val nlv = cplv lv in C.new NONE nlv; nlv end
532 :     fun filter xs = OU.filter(cst, xs)
533 :     (* construct the new arg list *)
534 :     val nargs = ListPair.map
535 :     (fn ((a,t),true) => (newarg a,t)
536 :     | ((a,t),false) => (a,t))
537 :     (args, cst)
538 :     (* construct the new body *)
539 :     val nbody =
540 :     F.LET(map #1 (filter args),
541 : monnier 186 F.RET(map O.valOf (filter actuals)),
542 : monnier 184 body)
543 :     in (fk, g, nargs, nbody)
544 : monnier 164 end
545 : monnier 184 end
546 : monnier 164
547 : monnier 184 (* add wrapper for various purposes *)
548 :     fun wrap (f as ({inline=F.IH_ALWAYS,...},_,_,_):F.fundec,fs) = f::fs
549 :     | wrap (f as (fk as {isrec,...},g,args,body):F.fundec,fs) =
550 :     let fun dropargs filter =
551 :     let val (nfk,nfk') = OU.fk_wrap(fk, O.map #1 isrec)
552 : monnier 164 val args' = filter args
553 : monnier 163 val ng = cplv g
554 :     val nargs = map (fn (v,t) => (cplv v, t)) args
555 : monnier 164 val nargs' = map #1 (filter nargs)
556 :     val appargs = (map F.VAR nargs')
557 : monnier 184 val nf = (nfk, g, nargs, F.APP(F.VAR ng, appargs))
558 : monnier 164 val nf' = (nfk', ng, args', body)
559 : monnier 186
560 :     val ngi = C.new (SOME(map #1 args')) ng
561 :     val nargsi = map ((C.new NONE) o #1) nargs
562 : monnier 184 in
563 : monnier 186 C.use (SOME appargs) ngi;
564 :     app (C.use NONE) nargsi;
565 : monnier 184 nf'::nf::fs
566 : monnier 163 end
567 : monnier 184 val used = map (used o #1) args
568 :     in
569 :     (* if some args are not used, let's drop them *)
570 :     if not (List.all OU.id used) then
571 :     dropargs (fn xs => OU.filter(used, xs))
572 : monnier 163
573 : monnier 184 (* eta-split: add a wrapper for escaping uses *)
574 : monnier 186 else
575 :     let val gi = C.get g
576 :     in if ((C.escaping gi)handle x => raise x) andalso ((C.called gi)handle x => raise x) then
577 :     (* like dropargs but keeping all args *)
578 :     dropargs OU.id
579 : monnier 121
580 : monnier 186 else f::fs
581 :     end
582 : monnier 184 end
583 : monnier 163
584 : monnier 186 (* junk unused funs *)
585 :     val fs = List.filter (used o #2) fs
586 :    
587 : monnier 184 (* redirect cst args to their source value *)
588 :     val fs = map cstargs fs
589 :    
590 :     (* add various wrappers *)
591 :     val fs = foldl wrap [] fs
592 :    
593 : monnier 121 (* register the new bindings (uncontracted for now) *)
594 :     val nm = foldl (fn (fdec as (fk,f,args,body),m) =>
595 :     addbind(m, f, Fun(f, body, args, fk, od)))
596 :     m fs
597 :     (* check for eta redexes *)
598 : monnier 186 val (nm,fs,_) = foldl ceta (nm,[],[]) fs
599 : monnier 121
600 :     (* move the inlinable functions to the end of the list *)
601 :     val (f1s,f2s) =
602 : monnier 184 List.partition (fn ({inline=F.IH_ALWAYS,...},_,_,_) => true
603 : monnier 121 | _ => false) fs
604 :     val fs = f2s @ f1s
605 :    
606 :     (* contract the main body *)
607 : monnier 184 val nle = loop nm le cont
608 : monnier 121 (* contract the functions *)
609 :     val fs = cfun(nm, fs, [])
610 :     (* junk newly unused funs *)
611 :     val fs = List.filter (used o #2) fs
612 :     in
613 : monnier 163 case fs
614 :     of [] => nle
615 : monnier 184 | [f1 as ({isrec=NONE,...},_,_,_),f2] =>
616 : monnier 186 (* gross hack: `wrap' might have added a second
617 : monnier 163 * non-recursive function. we need to split them into
618 : monnier 184 * 2 FIXes. This is _very_ ad-hoc *)
619 : monnier 163 F.FIX([f2], F.FIX([f1], nle))
620 :     | _ => F.FIX(fs, nle)
621 : monnier 121 end
622 :    
623 :     | F.APP (f,vs) =>
624 :     let val nvs = ((map substval vs) handle x => raise x)
625 : monnier 159 in case inline ifs (f, nvs)
626 : monnier 184 of (SOME(le,od),nifs) => cexp (d,od) ifs m le cont
627 :     | (NONE,_) => cont(m,F.APP((substval f) handle x => raise x, nvs))
628 : monnier 121 end
629 :    
630 :     | F.TFN ((f,args,body),le) =>
631 : monnier 186 if used f then
632 :     let val nbody = cexp (DI.next d, DI.next od) ifs m body #2
633 :     val nm = addbind(m, f, TFun(f, nbody, args, od))
634 :     val nle = loop nm le cont
635 :     in
636 :     if used f then F.TFN((f, args, nbody), nle) else nle
637 :     end
638 :     else loop m le cont
639 : monnier 121
640 : monnier 184 | F.TAPP(f,tycs) =>
641 :     cont(m, F.TAPP((substval f) handle x => raise x, tycs))
642 : monnier 121
643 :     | F.SWITCH (v,ac,arms,def) =>
644 :     (case ((val2sval m v) handle x => raise x)
645 : monnier 185 of sv as Con (lvc,v,dc1,tycs1) =>
646 : monnier 187 let fun killle le = C.unuselexp (undertake m) le
647 : monnier 159 fun kill lv le =
648 : monnier 187 C.unuselexp (undertake (addbind(m,lv,Var(lv,NONE)))) le
649 : monnier 159 fun killarm (F.DATAcon(_,_,lv),le) = kill lv le
650 :     | killarm _ = buglexp("bad arm in switch(con)", le)
651 :    
652 :     fun carm ((F.DATAcon(dc2,tycs2,lv),le)::tl) =
653 : monnier 185 (* sometimes lty1 <> lty2 :-( so this doesn't work:
654 :     * FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) *)
655 :     if #2 dc1 = #2 (cdcon dc2) then
656 : monnier 159 (map killarm tl; (* kill the rest *)
657 : monnier 185 O.map killle def; (* and the default case *)
658 : monnier 184 loop (substitute(m, lv, val2sval m v, F.VAR lvc))
659 :     le cont)
660 : monnier 159 else
661 :     (* kill this arm and continue with the rest *)
662 :     (kill lv le; carm tl)
663 : monnier 185 | carm [] = loop m (O.valOf def) cont
664 : monnier 121 | carm _ = buglexp("unexpected arm in switch(con,...)", le)
665 :     in carm arms
666 :     end
667 :    
668 : monnier 185 | sv as Val v =>
669 : monnier 187 let fun kill le = C.unuselexp (undertake m) le
670 : monnier 159 fun carm ((con,le)::tl) =
671 :     if eqConV(con, v) then
672 : monnier 184 (map (kill o #2) tl;
673 : monnier 185 O.map kill def;
674 : monnier 184 loop m le cont)
675 : monnier 159 else (kill le; carm tl)
676 : monnier 185 | carm [] = loop m (O.valOf def) cont
677 : monnier 121 in carm arms
678 :     end
679 : monnier 185
680 :     | sv as (Var{1=lvc,...} | Select{1=lvc,...} | Decon{1=lvc, ...}
681 :     | (* will probably never happen *) Record{1=lvc,...}) =>
682 :     (case (arms,def)
683 :     of ([(F.DATAcon(dc,tycs,lv),le)],NONE) =>
684 :     (* this is a mere DECON, so we can push the let binding
685 :     * (hidden in cont) inside and maybe even drop the DECON *)
686 :     let val ndc = cdcon dc
687 :     val nm = addbind(m, lv, Decon(lv, F.VAR lvc, ndc, tycs))
688 :     (* see below *)
689 :     val nm = addbind(nm, lvc, Con(lvc, F.VAR lv, ndc, tycs))
690 :     val nle = loop nm le cont
691 :     val nv = sval2val sv
692 :     in
693 :     if used lv then
694 :     F.SWITCH(nv,ac,[(F.DATAcon(ndc,tycs,lv),nle)],NONE)
695 : monnier 187 else (unuseval m nv; nle)
696 : monnier 185 end
697 :     | (([(_,le)],NONE) | ([],SOME le)) =>
698 :     (* This should never happen, but we can optimize it away *)
699 : monnier 187 (unuseval m (sval2val sv); loop m le cont)
700 : monnier 185 | _ =>
701 :     let fun carm (F.DATAcon(dc,tycs,lv),le) =
702 :     let val ndc = cdcon dc
703 :     val nm = addbind(m, lv,
704 :     Decon(lv, F.VAR lvc, ndc, tycs))
705 :     (* we can rebind lv to a more precise value
706 :     * !!BEWARE!! This rebinding is misleading:
707 :     * - it gives the impression that `lvc' is built
708 :     * from`lv' although the reverse is true:
709 :     * if `lvc' is undertaken, `lv's count should
710 :     * *not* be updated!
711 :     * Luckily, `lvc' will not become dead while
712 :     * rebound to Con(lv) because it's used by the
713 :     * SWITCH. All in all, it works fine, but it's
714 :     * not as straightforward as it seems.
715 :     * - it seems to be a good idea, but it can hide
716 :     * other opt-opportunities since it hides the
717 :     * previous binding. *)
718 :     val nm = addbind(nm, lvc,
719 :     Con(lvc, F.VAR lv, ndc, tycs))
720 :     in (F.DATAcon(ndc, tycs, lv), loop nm le #2)
721 :     end
722 :     | carm (con,le) = (con, loop m le #2)
723 :     val narms = map carm arms
724 :     val ndef = Option.map (fn le => loop m le #2) def
725 :     in cont(m, F.SWITCH(sval2val sv, ac, narms, ndef))
726 :     end)
727 :    
728 : monnier 121 | sv as (Fun _ | TFun _) =>
729 :     bugval("unexpected switch arg", sval2val sv))
730 :    
731 : monnier 159 | F.CON (dc1,tycs1,v,lv,le) =>
732 : monnier 186 if used lv then
733 :     let val ndc = cdcon dc1
734 :     fun ccon sv =
735 :     let val nv = sval2val sv
736 :     val nm = addbind(m, lv, Con(lv, nv, ndc, tycs1))
737 :     val nle = loop nm le cont
738 :     in if used lv then F.CON(ndc, tycs1, nv, lv, nle) else nle
739 :     end
740 :     in case ((val2sval m v) handle x => raise x)
741 :     of sv as (Decon (lvd,vc,dc2,tycs2)) =>
742 :     if FU.dcon_eq(dc1, dc2) andalso tycs_eq(tycs1,tycs2) then
743 :     let val sv = (val2sval m vc) handle x => raise x
744 :     in loop (substitute(m, lv, sv, F.VAR lvd)) le cont
745 :     end
746 :     else ccon sv
747 :     | sv => ccon sv
748 :     end
749 :     else loop m le cont
750 : monnier 121
751 :     | F.RECORD (rk,vs,lv,le) =>
752 : monnier 164 (* g: check whether the record already exists *)
753 : monnier 186 if used lv then
754 :     let fun g (n,Select(_,v1,i)::ss) =
755 :     if n = i then
756 :     (case ss
757 :     of Select(_,v2,_)::_ =>
758 :     if v1 = v2 then g(n+1, ss) else NONE
759 :     | [] =>
760 :     (case sval2lty (val2sval m v1)
761 :     of SOME lty =>
762 :     let val ltd = case rk
763 :     of F.RK_STRUCT => LT.ltd_str
764 :     | F.RK_TUPLE _ => LT.ltd_tuple
765 :     | _ => buglexp("bogus rk",le)
766 :     in if length(ltd lty) = n+1
767 :     then SOME v1 else NONE
768 :     end
769 :     | _ => NONE) (* sad case *)
770 :     | _ => NONE)
771 :     else NONE
772 :     | g _ = NONE
773 :     val svs = ((map (val2sval m) vs) handle x => raise x)
774 :     in case g (0,svs)
775 :     of SOME v =>
776 :     let val sv = (val2sval m v) handle x => raise x
777 :     in loop (substitute(m, lv, sv, F.INT 0)) le cont
778 : monnier 187 before app (unuseval m) vs
779 : monnier 186 end
780 :     | _ =>
781 :     let val nvs = map sval2val svs
782 :     val nm = addbind(m, lv, Record(lv, nvs))
783 :     val nle = loop nm le cont
784 :     in if used lv then F.RECORD(rk, nvs, lv, nle) else nle
785 :     end
786 :     end
787 :     else loop m le cont
788 : monnier 121
789 :     | F.SELECT (v,i,lv,le) =>
790 : monnier 186 if used lv then
791 :     (case ((val2sval m v) handle x => raise x)
792 :     of Record (lvr,vs) =>
793 :     let val sv = (val2sval m (List.nth(vs, i))) handle x => raise x
794 :     in loop (substitute(m, lv, sv, F.VAR lvr)) le cont
795 :     end
796 :     | sv =>
797 :     let val nv = sval2val sv
798 :     val nm = addbind (m, lv, Select(lv, nv, i))
799 :     val nle = loop nm le cont
800 :     in if used lv then F.SELECT(nv, i, lv, nle) else nle
801 :     end)
802 :     else loop m le cont
803 : monnier 121
804 : monnier 184 | F.RAISE (v,ltys) =>
805 :     cont(m, F.RAISE((substval v) handle x => raise x, ltys))
806 : monnier 121
807 : monnier 184 | F.HANDLE (le,v) =>
808 :     cont(m, F.HANDLE(loop m le #2, (substval v) handle x => raise x))
809 : monnier 121
810 :     | F.BRANCH (po,vs,le1,le2) =>
811 :     let val nvs = ((map substval vs) handle x => raise x)
812 :     val npo = cpo po
813 : monnier 184 val nle1 = loop m le1 #2
814 :     val nle2 = loop m le2 #2
815 :     in cont(m, F.BRANCH(npo, nvs, nle1, nle2))
816 : monnier 121 end
817 :    
818 :     | F.PRIMOP (po,vs,lv,le) =>
819 :     let val impure = impurePO po
820 : monnier 186 in if impure orelse used lv then
821 :     let val nvs = ((map substval vs) handle x => raise x)
822 :     val npo = cpo po
823 :     val nm = addbind(m, lv, Var(lv,NONE))
824 :     val nle = loop nm le cont
825 :     in
826 :     if impure orelse used lv
827 :     then F.PRIMOP(npo, nvs, lv, nle)
828 :     else nle
829 :     end
830 :     else loop m le cont
831 : monnier 121 end
832 :     end
833 :    
834 : monnier 185 in
835 :     (* C.collect fdec; *)
836 :     case cexp (DI.top,DI.top) S.empty
837 :     M.empty (F.FIX([fdec], F.RET[F.VAR f])) #2
838 :     of F.FIX([fdec], F.RET[F.VAR f]) => fdec
839 :     | fdec => bug "invalid return fundec"
840 :     end
841 : monnier 121
842 :     end
843 :     end

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