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

Annotation of /sml/branches/SMLNJ/src/compiler/FLINT/opt/fixfix.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 130 - (view) (download)

1 : monnier 122 (* copyright 1998 YALE FLINT PROJECT *)
2 :    
3 :     (* This module does various FIX-related transformations:
4 :     * - FIXes are split into their strongly-connected components
5 :     * - small non-recursive functions are marked inlinable
6 :     *)
7 :    
8 :     signature FIXFIX =
9 :     sig
10 :     val fixfix : FLINT.prog -> FLINT.prog
11 :     end
12 :    
13 :     (* It could later be extended to also do:
14 :     * - curried functrions are uncurried
15 :     * - hoisting of inner functions out of their englobing function
16 :     * so that the outer function becomes smaller, giving more opportunity
17 :     * for inlining.
18 :     *)
19 :    
20 :     structure FixFix :> FIXFIX =
21 :     struct
22 :    
23 :     local
24 :     structure F = FLINT
25 :     structure S = IntSetF
26 :     structure M = IntmapF
27 :     structure PP = PPFlint
28 :     structure LV = LambdaVar
29 :     structure LK = LtyKernel
30 :     structure LT = LtyExtern
31 :     in
32 :    
33 :     val say = Control.Print.say
34 :     fun bug msg = ErrorMsg.impossible ("FixFix: "^msg)
35 :     fun buglexp (msg,le) = (say "\n"; PP.printLexp le; say " "; bug msg)
36 :     fun bugval (msg,v) = (say "\n"; PP.printSval v; say " "; bug msg)
37 :     fun assert p = if p then () else bug ("assertion failed")
38 :    
39 :     structure SccNode = struct
40 :     type node = LambdaVar.lvar
41 :     val eq = (op =)
42 :     val lt = (op <)
43 :     end
44 :     structure SCC = SCCUtilFun (structure Node = SccNode)
45 :    
46 :     (* fexp: (intset * lexp) -> (int * intset * lexp)
47 :     * The three subparts returned are:
48 :     * - the size of lexp
49 :     * - the set of freevariables of lexp (plus the ones passed as arguments
50 :     * which are assumed to be the freevars of the continuation of lexp)
51 :     * - a new lexp with FIXes rewritten.
52 :     *)
53 :     fun fexp (fv,lexp) = let
54 :    
55 :     fun addv (s,F.VAR lv) = S.add(lv, s)
56 :     | addv (s,_) = s
57 :     fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
58 :     fun rmvs (s,lvs) = foldl S.rmv s lvs
59 :    
60 :     (* Looks for free vars in the primop descriptor.
61 :     * This is normally unnecessary since these are special vars anyway *)
62 :     fun fpo (fv,(NONE:F.dict option,po,lty,tycs)) = fv
63 :     | fpo (fv,(SOME{default,table},po,lty,tycs)) =
64 :     addvs(addv(fv, F.VAR default), map (F.VAR o #2) table)
65 :    
66 :     (* Looks for free vars in the primop descriptor.
67 :     * This is normally unnecessary since these are exception vars anyway *)
68 :     fun fdcon (fv,(s,Access.EXN(Access.LVAR lv),lty)) = addv(fv, F.VAR lv)
69 :     | fdcon (fv,_) = fv
70 :    
71 :     (* recognize the curried essence of a function. *)
72 :     fun curry (head,r) (le as (F.FIX([(fk,f,args,body)], F.RET[F.VAR lv]))) =
73 :     if lv = f then
74 :     case fk
75 :     of F.FK_FCT => ([], le) (* don't bother *)
76 :     | F.FK_FUN {inline=true,...} => ([], le) (* don't bother *)
77 :     | F.FK_FUN fk' =>
78 :     let val fisrec = isSome(#isrec fk')
79 :     in if head orelse r orelse not fisrec then
80 :     (* recursive functions are only accepted for uncurrying
81 :     * if they are the head of the function or if the head
82 :     * is already recursive *)
83 :     let val (funs,body) = curry (false, r orelse fisrec) body
84 :     in ((fk,f,args)::funs,body)
85 :     end
86 :     else ([], le)
87 :     end
88 :     else
89 :     (* this "never" occurs, but dead-code removal is not bullet-proof *)
90 :     ([(fk,f,args)], body)
91 :     | curry first le = ([], le)
92 :    
93 :     (* do the actual uncurrying *)
94 :     fun uncurry (args as (fk,f,fargs)::_::_,body) =
95 :     let val f' = LV.mkLvar() (* the new fun name *)
96 :    
97 :     fun getrtypes ([],rtys) = (NONE, rtys)
98 :     | getrtypes ((fk,f,fargs:(F.lvar * F.lty) list)::rest,rtys) =
99 :     case fk
100 :     of F.FK_FUN{isrec=SOME rtys,...} =>
101 :     let val fty = LT.ltc_fkfun(fk, map #2 fargs, rtys)
102 :     val (_,rtys) = getrtypes(rest, SOME rtys)
103 :     in (SOME fty, rtys)
104 :     end
105 :     | _ =>
106 :     let val rtys = Option.map (fn [lty] => #2(LT.ltd_fkfun lty)
107 :     | _ => bug "strange isrec") rtys
108 :     val (fty,rtys) = getrtypes(rest,rtys)
109 :     val fty = Option.map
110 :     (fn lty =>
111 :     LT.ltc_fkfun(fk, map #2 fargs, [lty]))
112 :     fty
113 :     in (fty,rtys)
114 :     end
115 :    
116 :     (* create the new fkinds *)
117 :     val (fty,rtys') = getrtypes(args, NONE)
118 :     val (nfk,nfk') =
119 :     case fk
120 :     of F.FK_FCT => (F.FK_FCT, F.FK_FCT)
121 :     | F.FK_FUN {isrec,known,fixed,inline} =>
122 :     let val fixed' =
123 :     case fixed
124 :     of LK.FF_VAR(f1,f2) => LK.FF_VAR(true, f2)
125 :     | LK.FF_FIXED => LK.FF_FIXED
126 :     (* val rtys = Option.map (fn lty => #2(LT.ltd_fkfun lty)) *)
127 :     (* fty *)
128 :     in (F.FK_FUN{isrec=isrec, known=known,
129 :     fixed=fixed, inline=true},
130 :     F.FK_FUN{isrec=rtys', known=true,
131 :     fixed=fixed', inline=inline})
132 :     end
133 :    
134 :     (* funarg renaming *)
135 :     fun newargs fargs = map (fn (_,t) => (LV.mkLvar(),t)) fargs
136 :    
137 :     (* create (curried) wrappers to be inlined *)
138 :     fun recurry ([],args) = F.APP(F.VAR f', map (F.VAR o #1) args)
139 :     | recurry ((fk,f,fargs)::rest,args) =
140 :     let val fk = case fk
141 :     of F.FK_FCT => fk
142 :     | F.FK_FUN{isrec,fixed,known,inline} =>
143 :     F.FK_FUN{isrec=NONE, fixed=fixed,
144 :     known=known, inline=true}
145 :     val nfargs = newargs fargs
146 :     val g = LV.mkLvar()
147 :     in F.FIX([(fk, g, nfargs, recurry(rest, args @ nfargs))],
148 :     F.RET[F.VAR g])
149 :     end
150 :    
151 :     (* build the new f fundec *)
152 :     val nfargs = newargs fargs
153 :     val nf = (nfk, f, nfargs, recurry(tl args, nfargs))
154 :    
155 :     (* make up the body of the uncurried function (creating
156 :     * dummy wrappers for the intermediate functions that are now
157 :     * useless).
158 :     * Intermediate functions that were not marked as recursive
159 :     * cannot appear in the body, so we don't need to build them.
160 :     * Note that we can't just rely on dead-code elimination to remove
161 :     * them because we may not be able to create them correctly with
162 :     * the limited type information gleaned in this phase. *)
163 :     fun uncurry' ([],args) = body
164 :     | uncurry' ((fk,f,fargs)::rest,args) =
165 :     let val le = uncurry'(rest, args @ fargs)
166 :     in case fk
167 :     of F.FK_FUN{isrec=SOME _, ...} =>
168 :     let val nfargs = newargs fargs
169 :     val fk = case fk
170 :     of F.FK_FCT => fk
171 :     | F.FK_FUN{isrec,fixed,known,inline} =>
172 :     F.FK_FUN{isrec=NONE, fixed=fixed,
173 :     known=known, inline=true}
174 :     in F.FIX([(fk, f, nfargs,
175 :     recurry(rest, args @ nfargs))],
176 :     le)
177 :     end
178 :     | _ => le
179 :     end
180 :    
181 :     (* the new f' fundec *)
182 :     val nfbody' = uncurry'(tl args, fargs)
183 :     val nf' = (nfk', f', foldr (op @) [] (map #3 args), nfbody')
184 :    
185 :     in (nf, nf')
186 :     end
187 :     | uncurry (_,body) = bug "uncurrying a non-curried function"
188 :    
189 :     in case lexp
190 :     of F.RET vs => (0, addvs(fv, vs), lexp)
191 :     | F.LET (lvs,le1,le2) =>
192 :     let val (s2,fv,le2) = fexp(fv, le2)
193 :     val (s1,fv,le1) = fexp(rmvs(fv, lvs), le1)
194 :     in (s1 + s2, fv, F.LET(lvs, le1, le2))
195 :     end
196 :     | F.FIX (fdecs,le) =>
197 :     let val funs = S.make(map #2 fdecs) (* set of funs defined by the FIX *)
198 :    
199 :     (* process the main lexp and make it into a dummy function.
200 :     * The computation of the freevars is a little sloppy since `fv'
201 :     * includes freevars of the continuation, but the unicity
202 :     * of varnames ensures that S.inter(fv, funs) gives the correct
203 :     * result nonetheless. *)
204 :     val (s,fv,le) = fexp(fv, le)
205 :     val lename = LambdaVar.mkLvar()
206 :     val m = M.singleton(lename, (S.members(S.inter(fv, funs)), 0,
207 :     F.FK_FCT, [], le))
208 :    
209 :     (* process each fun *)
210 :     fun ffun (fdec as (fk,f,args,body):F.fundec,(s,fv,funs,m)) =
211 :     case curry (true,false) (F.FIX([fdec], F.RET[F.VAR f]))
212 :     of (args as _::_::_,body) => (* curried function *)
213 :     let val ((fk,f,fargs,fbody),(fk',f',fargs',fbody')) =
214 :     uncurry(args,body)
215 :     (* add the wrapper function *)
216 :     val nm = M.add(m, f, ([f'], 1, fk, fargs, fbody))
217 :     (* now, retry ffun with the uncurried function *)
218 :     in ffun((fk', f', fargs', fbody'),
219 :     (s+1, fv, S.add(f', funs), nm))
220 :     end
221 :     | _ => (* non-curried function *)
222 :     let val (fs,ffv,body) = fexp(S.empty, body)
223 :     val ffv = rmvs(ffv, map #1 args) (* fun's freevars *)
224 :     val ifv = S.inter(ffv, funs) (* set of rec funs ref'ed *)
225 :     in
226 :     (fs + s, S.union(ffv, fv), funs,
227 :     M.add(m, f, (S.members ifv, fs, fk, args, body)))
228 :     end
229 :    
230 :     (* process the functions, collecting them in map m *)
231 :     val (s,fv,funs,m) = foldl ffun (s, fv, funs, m) fdecs
232 :    
233 :     (* find strongly connected components *)
234 :     val top = SCC.topOrder{root=lename, follow= #1 o (M.lookup m)}
235 :    
236 :     (* turns them back into flint code *)
237 :     fun sccconvert (SCC.SIMPLE f,le) =
238 :     (* a simple function. Fix the fk accordingly *)
239 :     let val (_,s,fk,args,body) = M.lookup m f
240 :     val fk =
241 :     case fk
242 :     of F.FK_FCT => F.FK_FCT
243 :     | F.FK_FUN {isrec,fixed,known,inline} =>
244 :     (* small functions inlining heuristic *)
245 :     let val small = s < !Control.FLINT.inlineThreshold
246 :     in F.FK_FUN{isrec=NONE, fixed=fixed,
247 :     known=known, inline=inline orelse small}
248 :     end
249 :     in F.FIX([(fk, f, args, body)], le)
250 :     end
251 :     | sccconvert (SCC.RECURSIVE fs,le) =
252 :     let fun scfun f =
253 :     let val (_,_,fk,args,body) = M.lookup m f
254 :     in (fk, f, args, body) end
255 :     in F.FIX(map scfun fs, le)
256 :     end
257 :     in
258 :     case top
259 :     of (SCC.SIMPLE f)::sccs =>
260 :     (assert(f = lename);
261 :     (s, S.diff(fv, funs), foldl sccconvert le sccs))
262 :     | (SCC.RECURSIVE _)::_ => bug "recursive main body in SCC ?!?!?"
263 :     | [] => bug "SCC going crazy"
264 :     end
265 :     | F.APP (f,args) =>
266 :     (* the cost of a function call depends on the number of args
267 :     * and the size of the continuation (number of free vars).
268 :     * We could also ask Collect whether f is known *)
269 :     (3 + (length args) + (S.size fv), addvs(fv, f::args), lexp)
270 :     | F.TFN ((f,args,body),le) =>
271 :     let val (se,fve,le) = fexp(fv, le)
272 :     val (sb,fvb,body) = fexp(S.empty, body)
273 :     in (sb + se, S.union(S.rmv(f, fve), fvb), F.TFN((f, args, body), le))
274 :     end
275 :     | F.TAPP (f,args) =>
276 :     (* The cost of TAPP is kinda hard to estimate. It can be very cheap,
277 :     * and just return a function, or it might do all kinds of wrapping
278 :     * but we have almost no information on which to base our choice.
279 :     * We opted for cheap here, to try to inline them more (they might
280 :     * become cheaper once inlined) *)
281 :     (3, addv(fv, f), lexp)
282 :     | F.SWITCH (v,ac,arms,def) =>
283 :     let fun farm (dcon as F.DATAcon(dc,_,lv),le) =
284 :     (* the binding might end up costly, but we count it as 1 *)
285 :     let val (s,fv,le) = fexp(fv,le)
286 :     in (1+s, fdcon(S.rmv(lv, fv),dc), (dcon, le))
287 :     end
288 :     | farm (dc,le) =
289 :     let val (s,fv,le) = fexp(fv, le) in (s, fv, (dc, le)) end
290 :     val (s,fv,arms) =
291 :     foldl (fn ((s1,fv1,arm),(s2,fv2,arms)) =>
292 :     (s1+s2, S.union(fv1, fv2), arm::arms))
293 :     (0, fv, []) (map farm arms)
294 :     in case def
295 :     of NONE => (s, fv, F.SWITCH(v, ac, arms, NONE))
296 :     | SOME le => let val (sd,fvd,le) = fexp(fv,le)
297 :     in (s+sd, S.union(fv, fvd), F.SWITCH(v, ac, arms, SOME le))
298 :     end
299 :     end
300 :     | F.CON (dc,tycs,v,lv,le) =>
301 :     let val (s,fv,le) = fexp(fv, le)
302 :     in (2+s, fdcon(addv(S.rmv(lv, fv), v),dc), F.CON(dc, tycs, v, lv, le))
303 :     end
304 :     | F.RECORD (rk,vs,lv,le) =>
305 :     let val (s,fv,le) = fexp(fv, le)
306 :     in ((length vs)+s, addvs(S.rmv(lv, fv), vs), F.RECORD(rk, vs, lv, le))
307 :     end
308 :     | F.SELECT (v,i,lv,le) =>
309 :     let val (s,fv,le) = fexp(fv, le)
310 :     in (1+s, addv(S.rmv(lv, fv), v), F.SELECT(v,i,lv,le))
311 :     end
312 :     | F.RAISE (v,ltys) => (3, addv(fv, v), lexp)
313 :     | F.HANDLE (le,v) =>
314 :     let val (s,fv,le) = fexp(fv, le)
315 :     in (2+s, addv(fv, v), F.HANDLE(le,v))
316 :     end
317 :     | F.BRANCH (po,vs,le1,le2) =>
318 :     let val (s1,fv1,le1) = fexp(fv,le1)
319 :     val (s2,fv2,le2) = fexp(fv,le2)
320 :     in (1+s1+s2, fpo(addvs(S.union(fv1, fv2), vs), po),
321 :     F.BRANCH(po, vs, le1, le2))
322 :     end
323 :     | F.PRIMOP (po,vs,lv,le) =>
324 :     let val (s,fv,le) = fexp(fv, le)
325 :     in (1+s, fpo(addvs(S.rmv(lv, fv), vs),po), F.PRIMOP(po,vs,lv,le))
326 :     end
327 :     end
328 :    
329 :     fun fixfix ((fk,f,args,body):F.prog) =
330 :     let val (s,fv,nbody) = fexp(S.empty, body)
331 :     val fv = S.diff(fv, S.make(map #1 args))
332 :     in
333 :     (* PPFlint.printLexp(F.RET(map F.VAR (S.members fv))); *)
334 :     assert(S.isEmpty(fv));
335 :     (fk, f, args, nbody)
336 :     end
337 :    
338 :     end
339 :     end

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