2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 Taken quite directly from the Peyton Jones/Lester paper.
8 -- Cheap and cheerful variant...
11 -- Complicated and expensive variant for float-out
13 freeVarsOf, freeTyVarsOf,
14 CoreExprWithFVs, -- For the above functions
16 FVInfo(..), LeakInfo(..)
19 #include "HsVersions.h"
21 import AnnCoreSyn -- output
24 import CoreUtils ( idSpecVars )
25 import Id ( idType, getIdArity, isBottomingId,
26 emptyIdSet, unitIdSet, mkIdSet, unionIdSets,
27 elementOfIdSet, minusIdSet, unionManyIdSets,
30 import IdInfo ( ArityInfo(..) )
31 import PrimOp ( PrimOp(..) )
32 import Type ( tyVarsOfType, Type )
33 import TyVar ( emptyTyVarSet, unitTyVarSet, minusTyVarSet,
37 import BasicTypes ( Unused )
38 import UniqSet ( unionUniqSets, addOneToUniqSet )
39 import Util ( panic, assertPanic )
43 %************************************************************************
45 \section[freevars-everywhere]{Attaching free variables to every sub-expression
47 %************************************************************************
49 The free variable pass annotates every node in the expression with its
50 NON-GLOBAL free variables and type variables.
52 The ``free type variables'' are defined to be those which are mentioned
53 in type applications, {\em not} ones which lie buried in the types of Ids.
55 *** ALAS: we *do* need to collect tyvars from lambda-bound ids. ***
56 I've half-convinced myself we don't for case- and letrec bound ids
57 but I might be wrong. (SLPJ, date unknown)
60 type CoreExprWithFVs = AnnCoreExpr Id Id Unused FVInfo
62 type TyVarCands = TyVarSet -- for when we carry around lists of
63 type IdCands = IdSet -- "candidate" TyVars/Ids.
64 noTyVarCands = emptyTyVarSet
65 noIdCands = emptyIdSet
68 = FVInfo IdSet -- Free ids
69 TyVarSet -- Free tyvars
72 noFreeIds = emptyIdSet
73 noFreeTyVars = emptyTyVarSet
74 noFreeAnything = (noFreeIds, noFreeTyVars)
75 aFreeId i = unitIdSet i
76 aFreeTyVar t = unitTyVarSet t
77 is_among = elementOfIdSet
78 munge_id_ty i = tyVarsOfType (idType i)
79 combine = unionUniqSets -- used both for {Id,TyVar}Sets
82 combineFVInfo (FVInfo fvs1 tfvs1 leak1) (FVInfo fvs2 tfvs2 leak2)
83 = FVInfo (fvs1 `combine` fvs2)
84 (tfvs1 `combine` tfvs2)
85 (leak1 `orLeak` leak2)
88 Leak-free-ness is based only on the value, not the type. In
89 particular, nested collections of constructors are guaranteed leak
90 free. Function applications are not, except for PAPs.
92 Applications of error gets (LeakFree bigArity) -- a hack!
97 | LeakFree Int -- Leak free, and guarantees to absorb this # of
98 -- args before becoming leaky.
100 lEAK_FREE_0 = LeakFree 0
101 lEAK_FREE_BIG = LeakFree bigArity
103 bigArity = 1000::Int -- NB: arbitrary
105 orLeak :: LeakInfo -> LeakInfo -> LeakInfo
106 orLeak MightLeak _ = MightLeak
107 orLeak _ MightLeak = MightLeak
108 orLeak (LeakFree n) (LeakFree m) = LeakFree (n `min` m)
111 Main public interface:
113 freeVars :: CoreExpr -> CoreExprWithFVs
115 freeVars expr = fvExpr noIdCands noTyVarCands expr
119 %************************************************************************
121 \subsection{Free variables (and types)}
123 %************************************************************************
125 We do the free-variable stuff by passing around ``candidates lists''
126 of @Ids@ and @TyVars@ that may be considered free. This is useful,
127 e.g., to avoid considering top-level binders as free variables---don't
128 put them on the candidates list.
132 fvExpr :: IdCands -- In-scope Ids
133 -> TyVarCands -- In-scope tyvars
137 fvExpr id_cands tyvar_cands (Var v)
138 = (FVInfo fvs noFreeTyVars leakiness, AnnVar v)
141 ToDo: insert motivating example for why we *need*
142 to include the idSpecVars in the FV list.
144 fvs = fvs_v `unionIdSets` mkIdSet (idSpecVars v)
147 | v `is_among` id_cands = aFreeId v
148 | otherwise = noFreeIds
151 | isBottomingId v = lEAK_FREE_BIG -- Hack
152 | otherwise = case getIdArity v of
153 UnknownArity -> lEAK_FREE_0
154 ArityAtLeast arity -> LeakFree arity
155 ArityExactly arity -> LeakFree arity
157 fvExpr id_cands tyvar_cands (Lit k)
158 = (FVInfo noFreeIds noFreeTyVars lEAK_FREE_0, AnnLit k)
160 fvExpr id_cands tyvar_cands (Con c args)
161 = (FVInfo args_fvs tfvs lEAK_FREE_0, AnnCon c args)
163 (args_fvs, tfvs) = freeArgs id_cands tyvar_cands args
165 fvExpr id_cands tyvar_cands (Prim op args)
166 = (FVInfo args_fvs tfvs lEAK_FREE_0, AnnPrim op args)
168 (args_fvs, tfvs) = freeArgs id_cands tyvar_cands args_to_use{-NB-}
171 CCallOp _ _ _ _ res_ty -> TyArg res_ty : args
174 -- this Lam stuff could probably be improved by rewriting (WDP 96/03)
176 fvExpr id_cands tyvar_cands (Lam b@(ValBinder binder) body)
177 = (FVInfo (freeVarsOf body2 `minusIdSet` unitIdSet binder)
178 (freeTyVarsOf body2 `combine` munge_id_ty binder)
182 -- We need to collect free tyvars from the binders
183 body2 = fvExpr (unitIdSet binder `combine` id_cands) tyvar_cands body
185 leakiness = case leakinessOf body2 of
186 MightLeak -> LeakFree 1
187 LeakFree n -> LeakFree (n + 1)
189 fvExpr id_cands tyvar_cands (Lam b@(TyBinder tyvar) body)
190 = (FVInfo (freeVarsOf body2)
191 (freeTyVarsOf body2 `minusTyVarSet` aFreeTyVar tyvar)
195 body2 = fvExpr id_cands (aFreeTyVar tyvar `combine` tyvar_cands) body
197 -- ditto on rewriting this App stuff (WDP 96/03)
199 fvExpr id_cands tyvar_cands (App fun arg)
200 = (FVInfo (freeVarsOf fun2 `combine` fvs_arg)
201 (freeTyVarsOf fun2 `combine` tfvs_arg)
205 fun2 = fvExpr id_cands tyvar_cands fun
206 fun2_leakiness = leakinessOf fun2
208 (fvs_arg, tfvs_arg) = freeArgs id_cands tyvar_cands [arg]
210 leakiness = if (notValArg arg) then
213 case fun2_leakiness of
214 LeakFree n | n>1 -> LeakFree (n-1) -- Note > not >=
217 fvExpr id_cands tyvar_cands (Case expr alts)
218 = (combineFVInfo expr_fvinfo alts_fvinfo,
221 expr2@(expr_fvinfo,_) = fvExpr id_cands tyvar_cands expr
222 (alts_fvinfo, alts') = annotate_alts alts
224 annotate_alts (AlgAlts alts deflt)
225 = (fvinfo, AnnAlgAlts alts' deflt')
227 (alts_fvinfo_s, alts') = unzip (map ann_boxed_alt alts)
228 (deflt_fvinfo, deflt') = annotate_default deflt
229 fvinfo = foldr combineFVInfo deflt_fvinfo alts_fvinfo_s
231 ann_boxed_alt (con, params, rhs)
232 = (FVInfo (freeVarsOf rhs' `minusIdSet` mkIdSet params)
233 (freeTyVarsOf rhs' `combine` param_ftvs)
237 rhs' = fvExpr (mkIdSet params `combine` id_cands) tyvar_cands rhs
238 param_ftvs = foldr (combine . munge_id_ty) noFreeTyVars params
239 -- We need to collect free tyvars from the binders
241 annotate_alts (PrimAlts alts deflt)
242 = (fvinfo, AnnPrimAlts alts' deflt')
244 (alts_fvinfo_s, alts') = unzip (map ann_unboxed_alt alts)
245 (deflt_fvinfo, deflt') = annotate_default deflt
246 fvinfo = foldr combineFVInfo deflt_fvinfo alts_fvinfo_s
248 ann_unboxed_alt (lit, rhs) = (rhs_info, (lit, rhs'))
250 rhs'@(rhs_info,_) = fvExpr id_cands tyvar_cands rhs
252 annotate_default NoDefault = (FVInfo noFreeIds noFreeTyVars lEAK_FREE_BIG,
255 annotate_default (BindDefault binder rhs)
256 = (FVInfo (freeVarsOf rhs' `minusIdSet` aFreeId binder)
257 (freeTyVarsOf rhs' `combine` binder_ftvs)
259 AnnBindDefault binder rhs')
261 rhs' = fvExpr (aFreeId binder `combine` id_cands) tyvar_cands rhs
262 binder_ftvs = munge_id_ty binder
263 -- We need to collect free tyvars from the binder
265 -- Don't forget to notice that the idSpecVars of the binder
266 -- are free in the whole expression; albeit not in the RHS or body
268 fvExpr id_cands tyvar_cands (Let (NonRec binder rhs) body)
269 = (FVInfo (freeVarsOf rhs' `combine` body_fvs `combine` mkIdSet (idSpecVars binder))
270 (freeTyVarsOf rhs' `combine` freeTyVarsOf body2 `combine` binder_ftvs)
271 (leakinessOf rhs' `orLeak` leakinessOf body2),
272 AnnLet (AnnNonRec binder rhs') body2)
274 rhs' = fvRhs id_cands tyvar_cands (binder, rhs)
275 body2 = fvExpr (aFreeId binder `combine` id_cands) tyvar_cands body
276 body_fvs = freeVarsOf body2 `minusIdSet` aFreeId binder
277 binder_ftvs = munge_id_ty binder
278 -- We need to collect free tyvars from the binder
280 fvExpr id_cands tyvar_cands (Let (Rec binds) body)
281 = (FVInfo (binds_fvs `combine` body_fvs)
282 (rhss_tfvs `combine` freeTyVarsOf body2 `combine` binders_ftvs)
283 (leakiness_of_rhss `orLeak` leakinessOf body2),
284 AnnLet (AnnRec (binders `zip` rhss')) body2)
286 (binders, rhss) = unzip binds
287 new_id_cands = binders_set `combine` id_cands
288 binders_set = mkIdSet binders
289 rhss' = map (fvRhs new_id_cands tyvar_cands) binds
291 FVInfo rhss_fvs rhss_tfvs leakiness_of_rhss
292 = foldr1 combineFVInfo [info | (info,_) <- rhss']
294 -- Don't forget to notice that the idSpecVars of the binder
295 -- are free in the whole expression; albeit not in the RHS or body
296 binds_fvs = (foldr (unionIdSets . mkIdSet . idSpecVars) rhss_fvs binders)
300 body2 = fvExpr new_id_cands tyvar_cands body
301 body_fvs = freeVarsOf body2 `minusIdSet` binders_set
302 binders_ftvs = foldr (combine . munge_id_ty) noFreeTyVars binders
303 -- We need to collect free tyvars from the binders
305 fvExpr id_cands tyvar_cands (Note (Coerce to_ty from_ty) expr)
306 = (FVInfo (freeVarsOf expr2)
307 (freeTyVarsOf expr2 `combine` tfvs1 `combine` tfvs2)
309 AnnNote (Coerce to_ty from_ty) expr2)
311 expr2 = fvExpr id_cands tyvar_cands expr
312 tfvs1 = freeTy tyvar_cands from_ty
313 tfvs2 = freeTy tyvar_cands to_ty
315 fvExpr id_cands tyvar_cands (Note other_note expr)
316 = (fvinfo, AnnNote other_note expr2)
318 expr2@(fvinfo,_) = fvExpr id_cands tyvar_cands expr
320 fvRhs id_cands tyvar_cands (bndr,rhs)
321 = fvExpr id_cands tyvar_cands rhs
325 freeArgs :: IdCands -> TyVarCands
329 freeArgs icands tcands [] = noFreeAnything
330 freeArgs icands tcands (arg:args)
331 -- this code is written this funny way only for "efficiency" purposes
333 free_first_arg@(arg_fvs, tfvs) = free_arg arg
338 case (freeArgs icands tcands args) of { (irest, trest) ->
339 (arg_fvs `combine` irest, tfvs `combine` trest) }
341 free_arg (LitArg _) = noFreeAnything
342 free_arg (TyArg ty) = (noFreeIds, freeTy tcands ty)
344 | v `is_among` icands = (aFreeId v, noFreeTyVars)
345 | otherwise = noFreeAnything
348 freeTy :: TyVarCands -> Type -> TyVarSet
350 freeTy cands ty = tyVarsOfType ty `intersectTyVarSets` cands
352 freeVarsOf :: CoreExprWithFVs -> IdSet
353 freeVarsOf (FVInfo free_vars _ _, _) = free_vars
355 freeTyVarsOf :: CoreExprWithFVs -> TyVarSet
356 freeTyVarsOf (FVInfo _ free_tyvars _, _) = free_tyvars
358 leakinessOf :: CoreExprWithFVs -> LeakInfo
359 leakinessOf (FVInfo _ _ leakiness, _) = leakiness
363 %************************************************************************
365 \section{Finding the free variables of an expression}
367 %************************************************************************
369 This function simply finds the free variables of an expression.
372 type InterestingIdFun
373 = Id -- The Id being looked at
374 -> Bool -- True <=> interesting
376 exprFreeVars :: InterestingIdFun -> CoreExpr -> IdSet
377 exprFreeVars fv_cand e = expr_fvs fv_cand emptyIdSet e
382 expr_fvs :: InterestingIdFun -- "Interesting id" predicate
383 -> IdSet -- In scope ids
387 expr_fvs fv_cand in_scope (Var v) = id_fvs fv_cand in_scope v
388 expr_fvs fv_cand in_scope (Lit lit) = noFreeIds
389 expr_fvs fv_cand in_scope (Con con args) = args_fvs fv_cand in_scope args
390 expr_fvs fv_cand in_scope (Prim op args) = args_fvs fv_cand in_scope args
391 expr_fvs fv_cand in_scope (Note _ expr) = expr_fvs fv_cand in_scope expr
392 expr_fvs fv_cand in_scope (App fun arg) = expr_fvs fv_cand in_scope fun `combine`
393 arg_fvs fv_cand in_scope arg
396 expr_fvs fv_cand in_scope (Lam (ValBinder b) body)
397 = (expr_fvs fv_cand (in_scope `add` b) body)
398 expr_fvs fv_cand in_scope (Lam (TyBinder b) body)
399 = expr_fvs fv_cand in_scope body
401 expr_fvs fv_cand in_scope (Case scrut alts)
402 = expr_fvs fv_cand in_scope scrut `combine` alts_fvs
406 AlgAlts alg_alts deflt -> unionManyIdSets (deflt_fvs : alt_fvs)
408 alt_fvs = map do_alg_alt alg_alts
409 deflt_fvs = do_deflt deflt
411 PrimAlts prim_alts deflt -> unionManyIdSets (deflt_fvs : alt_fvs)
413 alt_fvs = map do_prim_alt prim_alts
414 deflt_fvs = do_deflt deflt
416 do_alg_alt :: (Id, [Id], CoreExpr) -> IdSet
417 do_alg_alt (con, args, rhs) = expr_fvs fv_cand new_in_scope rhs
419 new_in_scope = in_scope `combine` mkIdSet args
421 do_prim_alt (lit, rhs) = expr_fvs fv_cand in_scope rhs
423 do_deflt NoDefault = noFreeIds
424 do_deflt (BindDefault b rhs) = expr_fvs fv_cand (in_scope `add` b) rhs
426 expr_fvs fv_cand in_scope (Let (NonRec b r) body)
427 = expr_fvs fv_cand in_scope r `combine`
428 expr_fvs fv_cand (in_scope `add` b) body
430 expr_fvs fv_cand in_scope (Let (Rec pairs) body)
431 = foldr (combine . expr_fvs fv_cand in_scope' . snd) noFreeIds pairs `combine`
432 expr_fvs fv_cand in_scope' body
434 in_scope' = in_scope `combine` mkIdSet (map fst pairs)
439 --------------------------------------
440 arg_fvs fv_cand in_scope (VarArg v) = id_fvs fv_cand in_scope v
441 arg_fvs fv_cand in_scope other_arg = noFreeIds
443 --------------------------------------
444 args_fvs fv_cand in_scope args = foldr (combine . arg_fvs fv_cand in_scope) noFreeIds args
447 --------------------------------------
448 id_fvs fv_cand in_scope v
449 | v `elementOfIdSet` in_scope = noFreeIds
450 | fv_cand v = aFreeId v
451 | otherwise = noFreeIds