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

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