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 184 - (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 :     (* fexp: (intset * lexp) -> (int * intset * lexp)
55 :     * The three subparts returned are:
56 :     * - the size of lexp
57 :     * - the set of freevariables of lexp (plus the ones passed as arguments
58 :     * which are assumed to be the freevars of the continuation of lexp)
59 :     * - a new lexp with FIXes rewritten.
60 :     *)
61 :     fun fexp (fv,lexp) = let
62 :    
63 :     fun addv (s,F.VAR lv) = S.add(lv, s)
64 :     | addv (s,_) = s
65 :     fun addvs (s,vs) = foldl (fn (v,s) => addv(s, v)) s vs
66 :     fun rmvs (s,lvs) = foldl S.rmv s lvs
67 :    
68 :     (* Looks for free vars in the primop descriptor.
69 :     * This is normally unnecessary since these are special vars anyway *)
70 :     fun fpo (fv,(NONE:F.dict option,po,lty,tycs)) = fv
71 :     | fpo (fv,(SOME{default,table},po,lty,tycs)) =
72 :     addvs(addv(fv, F.VAR default), map (F.VAR o #2) table)
73 :    
74 :     (* Looks for free vars in the primop descriptor.
75 :     * This is normally unnecessary since these are exception vars anyway *)
76 :     fun fdcon (fv,(s,Access.EXN(Access.LVAR lv),lty)) = addv(fv, F.VAR lv)
77 :     | fdcon (fv,_) = fv
78 :    
79 : monnier 162 (* recognize the curried essence of a function.
80 :     * - hd:bool identifies the head of the (potentially) curried function
81 :     * - r:bool indicates whether the head was recursive
82 :     * - na:int gives the number of args still allowed *)
83 :     fun curry (hd,r,na) (le as (F.FIX([(fk,f,args,body)], F.RET[F.VAR lv]))) =
84 : monnier 184 if lv = f andalso #inline fk = F.IH_SAFE then
85 :     let val fisrec = isSome(#isrec fk)
86 :     val na = na - length args
87 :     in if na >= 0 andalso (hd orelse r orelse not fisrec) then
88 :     (* recursive functions are only accepted for uncurrying
89 :     * if they are the head of the function or if the head
90 :     * is already recursive *)
91 :     let val (funs,body) =
92 :     curry (false, r orelse fisrec, na) body
93 :     in ((fk,f,args)::funs,body)
94 : monnier 122 end
95 : monnier 184 else ([], le)
96 :     end
97 : monnier 122 else
98 :     (* this "never" occurs, but dead-code removal is not bullet-proof *)
99 : monnier 164 ([], le)
100 : monnier 122 | curry first le = ([], le)
101 :    
102 :     (* do the actual uncurrying *)
103 :     fun uncurry (args as (fk,f,fargs)::_::_,body) =
104 : monnier 160 let val f' = cplv f (* the new fun name *)
105 : monnier 122
106 : monnier 184 (* find the rtys of the uncurried function *)
107 :     fun getrtypes (({isrec=SOME(rtys,_),...}:F.fkind,_,_),_) = SOME rtys
108 :     | getrtypes ((_,_,_),rtys) =
109 :     Option.map (fn [lty] => #2(LT.ltd_fkfun lty)
110 :     | _ => bug "strange isrec") rtys
111 : monnier 122
112 :     (* create the new fkinds *)
113 : monnier 184 val (nfk,nfk') = OU.fk_wrap(fk, foldl getrtypes NONE args)
114 : monnier 122
115 :     (* funarg renaming *)
116 : monnier 160 fun newargs fargs = map (fn (a,t) => (cplv a,t)) fargs
117 : monnier 122
118 :     (* create (curried) wrappers to be inlined *)
119 :     fun recurry ([],args) = F.APP(F.VAR f', map (F.VAR o #1) args)
120 : monnier 184 | recurry (({inline,isrec,known,cconv},f,fargs)::rest,args) =
121 :     let val fk = {inline=F.IH_ALWAYS, isrec=NONE,
122 :     known=known, cconv=cconv}
123 : monnier 122 val nfargs = newargs fargs
124 : monnier 160 val g = cplv f'
125 : monnier 122 in F.FIX([(fk, g, nfargs, recurry(rest, args @ nfargs))],
126 :     F.RET[F.VAR g])
127 :     end
128 :    
129 :     (* build the new f fundec *)
130 :     val nfargs = newargs fargs
131 :     val nf = (nfk, f, nfargs, recurry(tl args, nfargs))
132 :    
133 :     (* make up the body of the uncurried function (creating
134 :     * dummy wrappers for the intermediate functions that are now
135 :     * useless).
136 :     * Intermediate functions that were not marked as recursive
137 :     * cannot appear in the body, so we don't need to build them.
138 :     * Note that we can't just rely on dead-code elimination to remove
139 :     * them because we may not be able to create them correctly with
140 :     * the limited type information gleaned in this phase. *)
141 :     fun uncurry' ([],args) = body
142 :     | uncurry' ((fk,f,fargs)::rest,args) =
143 :     let val le = uncurry'(rest, args @ fargs)
144 :     in case fk
145 : monnier 184 of {isrec=SOME _,cconv,known,inline} =>
146 : monnier 122 let val nfargs = newargs fargs
147 : monnier 184 val fk = {isrec=NONE, inline=F.IH_ALWAYS,
148 :     known=known, cconv=cconv}
149 : monnier 122 in F.FIX([(fk, f, nfargs,
150 :     recurry(rest, args @ nfargs))],
151 :     le)
152 :     end
153 :     | _ => le
154 :     end
155 :    
156 :     (* the new f' fundec *)
157 :     val nfbody' = uncurry'(tl args, fargs)
158 :     val nf' = (nfk', f', foldr (op @) [] (map #3 args), nfbody')
159 :    
160 :     in (nf, nf')
161 :     end
162 :     | uncurry (_,body) = bug "uncurrying a non-curried function"
163 :    
164 :     in case lexp
165 :     of F.RET vs => (0, addvs(fv, vs), lexp)
166 :     | F.LET (lvs,le1,le2) =>
167 :     let val (s2,fv,le2) = fexp(fv, le2)
168 :     val (s1,fv,le1) = fexp(rmvs(fv, lvs), le1)
169 :     in (s1 + s2, fv, F.LET(lvs, le1, le2))
170 :     end
171 :     | F.FIX (fdecs,le) =>
172 :     let val funs = S.make(map #2 fdecs) (* set of funs defined by the FIX *)
173 :    
174 :     (* process the main lexp and make it into a dummy function.
175 :     * The computation of the freevars is a little sloppy since `fv'
176 : monnier 160 * includes freevars of the continuation, but the uniqueness
177 : monnier 122 * of varnames ensures that S.inter(fv, funs) gives the correct
178 :     * result nonetheless. *)
179 :     val (s,fv,le) = fexp(fv, le)
180 :     val lename = LambdaVar.mkLvar()
181 :     val m = M.singleton(lename, (S.members(S.inter(fv, funs)), 0,
182 : monnier 184 {inline=F.IH_SAFE, isrec=NONE,
183 :     known=true,cconv=F.CC_FCT},
184 :     [], le))
185 : monnier 122
186 :     (* process each fun *)
187 :     fun ffun (fdec as (fk,f,args,body):F.fundec,(s,fv,funs,m)) =
188 : monnier 162 case curry (true,false,!maxargs) (F.FIX([fdec], F.RET[F.VAR f]))
189 : monnier 122 of (args as _::_::_,body) => (* curried function *)
190 :     let val ((fk,f,fargs,fbody),(fk',f',fargs',fbody')) =
191 :     uncurry(args,body)
192 :     (* add the wrapper function *)
193 :     val nm = M.add(m, f, ([f'], 1, fk, fargs, fbody))
194 :     (* now, retry ffun with the uncurried function *)
195 :     in ffun((fk', f', fargs', fbody'),
196 :     (s+1, fv, S.add(f', funs), nm))
197 :     end
198 :     | _ => (* non-curried function *)
199 :     let val (fs,ffv,body) = fexp(S.empty, body)
200 :     val ffv = rmvs(ffv, map #1 args) (* fun's freevars *)
201 :     val ifv = S.inter(ffv, funs) (* set of rec funs ref'ed *)
202 :     in
203 :     (fs + s, S.union(ffv, fv), funs,
204 :     M.add(m, f, (S.members ifv, fs, fk, args, body)))
205 :     end
206 :    
207 :     (* process the functions, collecting them in map m *)
208 :     val (s,fv,funs,m) = foldl ffun (s, fv, funs, m) fdecs
209 :    
210 :     (* find strongly connected components *)
211 :     val top = SCC.topOrder{root=lename, follow= #1 o (M.lookup m)}
212 :    
213 :     (* turns them back into flint code *)
214 :     fun sccconvert (SCC.SIMPLE f,le) =
215 :     (* a simple function. Fix the fk accordingly *)
216 : monnier 184 let val (_,s,{isrec,cconv,known,inline},args,body) = M.lookup m f
217 : monnier 122 val fk =
218 : monnier 184 (* small functions inlining heuristic *)
219 :     let val inline' =
220 :     if inline = F.IH_SAFE andalso
221 :     s < !CTRL.inlineThreshold then
222 :     F.IH_ALWAYS
223 :     else inline
224 :     in {isrec=NONE, inline=inline',
225 :     known=known, cconv=cconv}
226 :     end
227 : monnier 122 in F.FIX([(fk, f, args, body)], le)
228 :     end
229 :     | sccconvert (SCC.RECURSIVE fs,le) =
230 :     let fun scfun f =
231 : monnier 184 let val (_,s,fk as {isrec,cconv,known,inline},args,le) =
232 :     M.lookup m f
233 :     val fk' =
234 :     (* let's check for unroll opportunities.
235 :     * This heuristic is pretty bad since it doesn't
236 :     * take the number of rec-calls into account *)
237 :     case (isrec,inline)
238 :     of (SOME(_,(F.LK_LOOP|F.LK_WHILE)),F.IH_SAFE) =>
239 :     if s < !CTRL.unrollThreshold then
240 :     {inline=F.IH_UNROLL, isrec=isrec,
241 :     cconv=cconv, known=known}
242 :     else fk
243 :     | _ => fk
244 :     in (fk, f, args, le) end
245 : monnier 122 in F.FIX(map scfun fs, le)
246 :     end
247 :     in
248 :     case top
249 :     of (SCC.SIMPLE f)::sccs =>
250 :     (assert(f = lename);
251 :     (s, S.diff(fv, funs), foldl sccconvert le sccs))
252 :     | (SCC.RECURSIVE _)::_ => bug "recursive main body in SCC ?!?!?"
253 :     | [] => bug "SCC going crazy"
254 :     end
255 :     | F.APP (f,args) =>
256 :     (* the cost of a function call depends on the number of args
257 :     * and the size of the continuation (number of free vars).
258 :     * We could also ask Collect whether f is known *)
259 :     (3 + (length args) + (S.size fv), addvs(fv, f::args), lexp)
260 :     | F.TFN ((f,args,body),le) =>
261 :     let val (se,fve,le) = fexp(fv, le)
262 :     val (sb,fvb,body) = fexp(S.empty, body)
263 :     in (sb + se, S.union(S.rmv(f, fve), fvb), F.TFN((f, args, body), le))
264 :     end
265 :     | F.TAPP (f,args) =>
266 :     (* The cost of TAPP is kinda hard to estimate. It can be very cheap,
267 :     * and just return a function, or it might do all kinds of wrapping
268 :     * but we have almost no information on which to base our choice.
269 :     * We opted for cheap here, to try to inline them more (they might
270 :     * become cheaper once inlined) *)
271 :     (3, addv(fv, f), lexp)
272 :     | F.SWITCH (v,ac,arms,def) =>
273 :     let fun farm (dcon as F.DATAcon(dc,_,lv),le) =
274 :     (* the binding might end up costly, but we count it as 1 *)
275 :     let val (s,fv,le) = fexp(fv,le)
276 :     in (1+s, fdcon(S.rmv(lv, fv),dc), (dcon, le))
277 :     end
278 :     | farm (dc,le) =
279 :     let val (s,fv,le) = fexp(fv, le) in (s, fv, (dc, le)) end
280 :     val (s,fv,arms) =
281 :     foldl (fn ((s1,fv1,arm),(s2,fv2,arms)) =>
282 :     (s1+s2, S.union(fv1, fv2), arm::arms))
283 :     (0, fv, []) (map farm arms)
284 :     in case def
285 :     of NONE => (s, fv, F.SWITCH(v, ac, arms, NONE))
286 :     | SOME le => let val (sd,fvd,le) = fexp(fv,le)
287 :     in (s+sd, S.union(fv, fvd), F.SWITCH(v, ac, arms, SOME le))
288 :     end
289 :     end
290 :     | F.CON (dc,tycs,v,lv,le) =>
291 :     let val (s,fv,le) = fexp(fv, le)
292 :     in (2+s, fdcon(addv(S.rmv(lv, fv), v),dc), F.CON(dc, tycs, v, lv, le))
293 :     end
294 :     | F.RECORD (rk,vs,lv,le) =>
295 :     let val (s,fv,le) = fexp(fv, le)
296 :     in ((length vs)+s, addvs(S.rmv(lv, fv), vs), F.RECORD(rk, vs, lv, le))
297 :     end
298 :     | F.SELECT (v,i,lv,le) =>
299 :     let val (s,fv,le) = fexp(fv, le)
300 :     in (1+s, addv(S.rmv(lv, fv), v), F.SELECT(v,i,lv,le))
301 :     end
302 :     | F.RAISE (v,ltys) => (3, addv(fv, v), lexp)
303 :     | F.HANDLE (le,v) =>
304 :     let val (s,fv,le) = fexp(fv, le)
305 :     in (2+s, addv(fv, v), F.HANDLE(le,v))
306 :     end
307 :     | F.BRANCH (po,vs,le1,le2) =>
308 :     let val (s1,fv1,le1) = fexp(fv,le1)
309 :     val (s2,fv2,le2) = fexp(fv,le2)
310 :     in (1+s1+s2, fpo(addvs(S.union(fv1, fv2), vs), po),
311 :     F.BRANCH(po, vs, le1, le2))
312 :     end
313 :     | F.PRIMOP (po,vs,lv,le) =>
314 :     let val (s,fv,le) = fexp(fv, le)
315 :     in (1+s, fpo(addvs(S.rmv(lv, fv), vs),po), F.PRIMOP(po,vs,lv,le))
316 :     end
317 :     end
318 :    
319 :     fun fixfix ((fk,f,args,body):F.prog) =
320 :     let val (s,fv,nbody) = fexp(S.empty, body)
321 :     val fv = S.diff(fv, S.make(map #1 args))
322 :     in
323 :     (* PPFlint.printLexp(F.RET(map F.VAR (S.members fv))); *)
324 :     assert(S.isEmpty(fv));
325 :     (fk, f, args, nbody)
326 :     end
327 :    
328 :     end
329 :     end

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