2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreUtils]{Utility functions on @Core@ syntax}
8 IdSubst, SubstCoreExpr(..),
10 coreExprType, coreAltsType, exprFreeVars, exprSomeFreeVars,
12 exprIsBottom, exprIsDupable, exprIsTrivial, exprIsWHNF, exprIsCheap,
13 FormSummary(..), mkFormSummary, whnfOrBottom,
16 substExpr, substId, substIds,
17 idSpecVars, idFreeVars
20 #include "HsVersions.h"
22 import {-# SOURCE #-} CoreUnfold ( noUnfolding, hasUnfolding )
25 import PprCore () -- Instances only
26 import Var ( IdOrTyVar, isId, isTyVar )
29 import Name ( isLocallyDefined )
30 import Const ( Con(..), isWHNFCon, conIsTrivial, conIsCheap )
31 import Id ( Id, idType, setIdType, idUnique, idAppIsBottom,
32 getIdArity, idFreeTyVars,
33 getIdSpecialisation, setIdSpecialisation,
34 getInlinePragma, setInlinePragma,
35 getIdUnfolding, setIdUnfolding, idInfo
37 import IdInfo ( arityLowerBound, InlinePragInfo(..), lbvarInfo, LBVarInfo(..) )
38 import SpecEnv ( emptySpecEnv, specEnvToList, isEmptySpecEnv )
39 import CostCentre ( CostCentre )
40 import Const ( Con, conType )
41 import Type ( Type, TyVarSubst, mkFunTy, mkForAllTy,
42 splitFunTy_maybe, applyTys, tyVarsOfType, tyVarsOfTypes,
43 isNotUsgTy, mkUsgTy, unUsgTy, UsageAnn(..),
44 fullSubstTy, substTyVar )
45 import Unique ( buildIdKey, augmentIdKey )
46 import Util ( zipWithEqual, mapAccumL )
48 import TysPrim ( alphaTy ) -- Debugging only
52 %************************************************************************
54 \subsection{Substitutions}
56 %************************************************************************
59 type IdSubst = IdEnv SubstCoreExpr -- Maps Ids to SubstCoreExpr
62 = Done CoreExpr -- No more substitution needed
63 | SubstMe CoreExpr TyVarSubst IdSubst -- A suspended substitution
66 %************************************************************************
68 \subsection{Find the type of a Core atom/expression}
70 %************************************************************************
73 coreExprType :: CoreExpr -> Type
75 coreExprType (Var var) = idType var
76 coreExprType (Let _ body) = coreExprType body
77 coreExprType (Case _ _ alts) = coreAltsType alts
78 coreExprType (Note (Coerce ty _) e) = ty
79 coreExprType (Note (TermUsg u) e) = mkUsgTy u (unUsgTy (coreExprType e))
80 coreExprType (Note other_note e) = coreExprType e
81 coreExprType e@(Con con args) = applyTypeToArgs e (conType con) args
83 coreExprType (Lam binder expr)
84 | isId binder = (case (lbvarInfo . idInfo) binder of
85 IsOneShotLambda -> mkUsgTy UsOnce
87 idType binder `mkFunTy` coreExprType expr
88 | isTyVar binder = mkForAllTy binder (coreExprType expr)
90 coreExprType e@(App _ _)
91 = case collectArgs e of
92 (fun, args) -> applyTypeToArgs e (coreExprType fun) args
94 coreExprType other = pprTrace "coreExprType" (ppr other) alphaTy
96 coreAltsType :: [CoreAlt] -> Type
97 coreAltsType ((_,_,rhs) : _) = coreExprType rhs
101 -- The "e" argument is just for debugging
103 applyTypeToArgs e op_ty [] = op_ty
105 applyTypeToArgs e op_ty (Type ty : args)
106 = -- Accumulate type arguments so we can instantiate all at once
107 ASSERT2( all isNotUsgTy tys, ppr e <+> text "of" <+> ppr op_ty <+> text "to" <+> ppr (Type ty : args) <+> text "i.e." <+> ppr tys )
108 applyTypeToArgs e (applyTys op_ty tys) rest_args
110 (tys, rest_args) = go [ty] args
111 go tys (Type ty : args) = go (ty:tys) args
112 go tys rest_args = (reverse tys, rest_args)
114 applyTypeToArgs e op_ty (other_arg : args)
115 = case (splitFunTy_maybe op_ty) of
116 Just (_, res_ty) -> applyTypeToArgs e res_ty args
117 Nothing -> pprPanic "applyTypeToArgs" (ppr e)
121 %************************************************************************
123 \subsection{Figuring out things about expressions}
125 %************************************************************************
129 = VarForm -- Expression is a variable (or scc var, etc)
130 | ValueForm -- Expression is a value: i.e. a value-lambda,constructor, or literal
131 | BottomForm -- Expression is guaranteed to be bottom. We're more gung
132 -- ho about inlining such things, because it can't waste work
133 | OtherForm -- Anything else
135 instance Outputable FormSummary where
136 ppr VarForm = ptext SLIT("Var")
137 ppr ValueForm = ptext SLIT("Value")
138 ppr BottomForm = ptext SLIT("Bot")
139 ppr OtherForm = ptext SLIT("Other")
141 whnfOrBottom :: FormSummary -> Bool
142 whnfOrBottom VarForm = True
143 whnfOrBottom ValueForm = True
144 whnfOrBottom BottomForm = True
145 whnfOrBottom OtherForm = False
149 mkFormSummary :: CoreExpr -> FormSummary
151 = go (0::Int) expr -- The "n" is the number of *value* arguments so far
153 go n (Con con _) | isWHNFCon con = ValueForm
154 | otherwise = OtherForm
156 go n (Note _ e) = go n e
158 go n (Let (NonRec b r) e) | exprIsTrivial r = go n e -- let f = f' alpha in (f,g)
159 -- should be treated as a value
160 go n (Let _ e) = OtherForm
161 go n (Case _ _ _) = OtherForm
163 go 0 (Lam x e) | isId x = ValueForm -- NB: \x.bottom /= bottom!
165 go n (Lam x e) | isId x = go (n-1) e -- Applied lambda
168 go n (App fun (Type _)) = go n fun -- Ignore type args
169 go n (App fun arg) = go (n+1) fun
171 go n (Var f) | idAppIsBottom f n = BottomForm
172 go 0 (Var f) = VarForm
173 go n (Var f) | n < arityLowerBound (getIdArity f) = ValueForm
174 | otherwise = OtherForm
177 @exprIsTrivial@ is true of expressions we are unconditionally
178 happy to duplicate; simple variables and constants,
179 and type applications.
181 @exprIsDupable@ is true of expressions that can be duplicated at a modest
182 cost in space, but without duplicating any work.
185 @exprIsBottom@ is true of expressions that are guaranteed to diverge
189 exprIsTrivial (Type _) = True
190 exprIsTrivial (Var v) = True
191 exprIsTrivial (App e arg) = isTypeArg arg && exprIsTrivial e
192 exprIsTrivial (Note _ e) = exprIsTrivial e
193 exprIsTrivial (Con con args) = conIsTrivial con && all isTypeArg args
194 exprIsTrivial (Lam b body) | isTyVar b = exprIsTrivial body
195 exprIsTrivial other = False
200 exprIsDupable (Type _) = True
201 exprIsDupable (Con con args) = conIsCheap con &&
202 all exprIsDupable args &&
203 valArgCount args <= dupAppSize
205 exprIsDupable (Note _ e) = exprIsDupable e
206 exprIsDupable expr = case collectArgs expr of
207 (Var v, args) -> n_val_args == 0 ||
208 (n_val_args < fun_arity &&
209 all exprIsDupable args &&
210 n_val_args <= dupAppSize)
212 n_val_args = valArgCount args
213 fun_arity = arityLowerBound (getIdArity v)
218 dupAppSize = 4 -- Size of application we are prepared to duplicate
221 @exprIsCheap@ looks at a Core expression and returns \tr{True} if
222 it is obviously in weak head normal form, or is cheap to get to WHNF.
223 [Note that that's not the same as exprIsDupable; an expression might be
224 big, and hence not dupable, but still cheap.]
225 By ``cheap'' we mean a computation we're willing to push inside a lambda
226 in order to bring a couple of lambdas together. That might mean it gets
227 evaluated more than once, instead of being shared. The main examples of things
228 which aren't WHNF but are ``cheap'' are:
233 where e, and all the ei are cheap; and
238 where e and b are cheap; and
242 where op is a cheap primitive operator
245 exprIsCheap :: CoreExpr -> Bool
246 exprIsCheap (Type _) = True
247 exprIsCheap (Var _) = True
248 exprIsCheap (Con con args) = conIsCheap con && all exprIsCheap args
249 exprIsCheap (Note _ e) = exprIsCheap e
250 exprIsCheap (Lam x e) = if isId x then True else exprIsCheap e
251 exprIsCheap (Let bind body) = all exprIsCheap (rhssOfBind bind) && exprIsCheap body
252 exprIsCheap (Case scrut _ alts) = exprIsCheap scrut &&
253 all (\(_,_,rhs) -> exprIsCheap rhs) alts
255 exprIsCheap other_expr -- look for manifest partial application
256 = case collectArgs other_expr of
258 (Var f, args) | idAppIsBottom f (length args)
259 -> True -- Application of a function which
260 -- always gives bottom; we treat this as
261 -- a WHNF, because it certainly doesn't
262 -- need to be shared!
266 num_val_args = valArgCount args
268 num_val_args == 0 || -- Just a type application of
269 -- a variable (f t1 t2 t3)
271 num_val_args < arityLowerBound (getIdArity f)
278 exprIsBottom :: CoreExpr -> Bool -- True => definitely bottom
279 exprIsBottom e = go 0 e
281 -- n is the number of args
282 go n (Note _ e) = go n e
283 go n (Let _ e) = go n e
284 go n (Case e _ _) = go 0 e -- Just check the scrut
285 go n (App e _) = go (n+1) e
286 go n (Var v) = idAppIsBottom v n
287 go n (Con _ _) = False
288 go n (Lam _ _) = False
291 exprIsWHNF reports True for head normal forms. Note that does not necessarily
292 mean *normal* forms; constructors might have non-trivial argument expressions, for
293 example. We use a let binding for WHNFs, rather than a case binding, even if it's
294 used strictly. We try to expose WHNFs by floating lets out of the RHS of lets.
296 We treat applications of buildId and augmentId as honorary WHNFs, because we
297 want them to get exposed
300 exprIsWHNF :: CoreExpr -> Bool -- True => Variable, value-lambda, constructor, PAP
301 exprIsWHNF (Type ty) = True -- Types are honorary WHNFs; we don't mind
303 exprIsWHNF (Var v) = True
304 exprIsWHNF (Lam b e) = isId b || exprIsWHNF e
305 exprIsWHNF (Note _ e) = exprIsWHNF e
306 exprIsWHNF (Let _ e) = False
307 exprIsWHNF (Case _ _ _) = False
308 exprIsWHNF (Con con _) = isWHNFCon con
309 exprIsWHNF e@(App _ _) = case collectArgs e of
310 (Var v, args) -> n_val_args == 0 ||
311 fun_arity > n_val_args ||
312 v_uniq == buildIdKey ||
313 v_uniq == augmentIdKey
315 n_val_args = valArgCount args
316 fun_arity = arityLowerBound (getIdArity v)
322 @cheapEqExpr@ is a cheap equality test which bales out fast!
323 True => definitely equal
324 False => may or may not be equal
327 cheapEqExpr :: Expr b -> Expr b -> Bool
329 cheapEqExpr (Var v1) (Var v2) = v1==v2
330 cheapEqExpr (Con con1 args1) (Con con2 args2)
332 and (zipWithEqual "cheapEqExpr" cheapEqExpr args1 args2)
334 cheapEqExpr (App f1 a1) (App f2 a2)
335 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
337 cheapEqExpr (Type t1) (Type t2) = t1 == t2
339 cheapEqExpr _ _ = False
343 %************************************************************************
345 \section{Finding the free variables of an expression}
347 %************************************************************************
349 This function simply finds the free variables of an expression.
350 So far as type variables are concerned, it only finds tyvars that are
352 * free in type arguments,
353 * free in the type of a binder,
355 but not those that are free in the type of variable occurrence.
358 exprFreeVars :: CoreExpr -> IdOrTyVarSet -- Find all locally-defined free Ids or tyvars
359 exprFreeVars = exprSomeFreeVars isLocallyDefined
361 exprSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
364 exprSomeFreeVars fv_cand e = expr_fvs e fv_cand emptyVarSet
366 type InterestingVarFun = IdOrTyVar -> Bool -- True <=> interesting
371 type FV = InterestingVarFun
372 -> IdOrTyVarSet -- In scope
373 -> IdOrTyVarSet -- Free vars
375 union :: FV -> FV -> FV
376 union fv1 fv2 fv_cand in_scope = fv1 fv_cand in_scope `unionVarSet` fv2 fv_cand in_scope
379 noVars fv_cand in_scope = emptyVarSet
381 oneVar :: IdOrTyVar -> FV
382 oneVar var fv_cand in_scope
383 | keep_it fv_cand in_scope var = unitVarSet var
384 | otherwise = emptyVarSet
386 someVars :: IdOrTyVarSet -> FV
387 someVars vars fv_cand in_scope
388 = filterVarSet (keep_it fv_cand in_scope) vars
390 keep_it fv_cand in_scope var
391 | var `elemVarSet` in_scope = False
396 addBndr :: CoreBndr -> FV -> FV
397 addBndr bndr fv fv_cand in_scope
398 | isId bndr = inside_fvs `unionVarSet` someVars (idFreeVars bndr) fv_cand in_scope
399 | otherwise = inside_fvs
401 inside_fvs = fv fv_cand (in_scope `extendVarSet` bndr)
403 addBndrs :: [CoreBndr] -> FV -> FV
404 addBndrs bndrs fv = foldr addBndr fv bndrs
409 expr_fvs :: CoreExpr -> FV
411 expr_fvs (Type ty) = someVars (tyVarsOfType ty)
412 expr_fvs (Var var) = oneVar var
413 expr_fvs (Con con args) = foldr (union . expr_fvs) noVars args
414 expr_fvs (Note _ expr) = expr_fvs expr
415 expr_fvs (App fun arg) = expr_fvs fun `union` expr_fvs arg
416 expr_fvs (Lam bndr body) = addBndr bndr (expr_fvs body)
418 expr_fvs (Case scrut bndr alts)
419 = expr_fvs scrut `union` addBndr bndr (foldr (union. alt_fvs) noVars alts)
421 alt_fvs (con, bndrs, rhs) = addBndrs bndrs (expr_fvs rhs)
423 expr_fvs (Let (NonRec bndr rhs) body)
424 = expr_fvs rhs `union` addBndr bndr (expr_fvs body)
426 expr_fvs (Let (Rec pairs) body)
427 = addBndrs bndrs (foldr (union . expr_fvs) (expr_fvs body) rhss)
429 (bndrs,rhss) = unzip pairs
433 Given an Id, idSpecVars returns all its specialisations.
434 We extract these from its SpecEnv.
435 This is used by the occurrence analyser and free-var finder;
436 we regard an Id's specialisations as free in the Id's definition.
439 idSpecVars :: Id -> IdOrTyVarSet
441 = foldr (unionVarSet . spec_item_fvs)
443 (specEnvToList (getIdSpecialisation id))
445 spec_item_fvs (tyvars, tys, rhs) = foldl delVarSet
446 (tyVarsOfTypes tys `unionVarSet` exprFreeVars rhs)
449 idFreeVars :: Id -> IdOrTyVarSet
450 idFreeVars id = idSpecVars id `unionVarSet` idFreeTyVars id
454 %************************************************************************
456 \section{Substitution}
458 %************************************************************************
460 This expression substituter deals correctly with name capture, much
463 BUT NOTE that substExpr silently discards the
466 IdInfo attached to any binders in the expression. It's quite
467 tricky to do them 'right' in the case of mutually recursive bindings,
468 and so far has proved unnecessary.
471 substExpr :: TyVarSubst -> IdSubst -- Substitution
472 -> IdOrTyVarSet -- Superset of in-scope
476 substExpr te ve in_scope expr = subst_expr (te, ve, in_scope) expr
478 subst_expr env@(te, ve, in_scope) expr
481 go (Var v) = case lookupVarEnv ve v of
485 Just (SubstMe e' te' ve')
486 -> subst_expr (te', ve', in_scope) e'
488 Nothing -> case lookupVarSet in_scope v of
491 -- NB: we look up in the in_scope set because the variable
492 -- there may have more info. In particular, when substExpr
493 -- is called from the simplifier, the type inside the *occurrences*
494 -- of a variable may not be right; we should replace it with the
495 -- binder, from the in_scope set.
497 go (Type ty) = Type (go_ty ty)
498 go (Con con args) = Con con (map go args)
499 go (App fun arg) = App (go fun) (go arg)
500 go (Note note e) = Note (go_note note) (go e)
502 go (Lam bndr body) = Lam bndr' (subst_expr env' body)
504 (env', bndr') = go_bndr env bndr
506 go (Let (NonRec bndr rhs) body) = Let (NonRec bndr' (go rhs)) (subst_expr env' body)
508 (env', bndr') = go_bndr env bndr
510 go (Let (Rec pairs) body) = Let (Rec pairs') (subst_expr env' body)
512 (ve', in_scope', _, bndrs')
513 = substIds clone_fn te ve in_scope undefined (map fst pairs)
514 env' = (te, ve', in_scope')
515 pairs' = bndrs' `zip` rhss'
516 rhss' = map (subst_expr env' . snd) pairs
518 go (Case scrut bndr alts) = Case (go scrut) bndr' (map (go_alt env') alts)
520 (env', bndr') = go_bndr env bndr
522 go_alt env (con, bndrs, rhs) = (con, bndrs', subst_expr env' rhs)
524 (env', bndrs') = mapAccumL go_bndr env bndrs
526 go_note (Coerce ty1 ty2) = Coerce (go_ty ty1) (go_ty ty2)
529 go_ty ty = fullSubstTy te in_scope ty
531 go_bndr (te, ve, in_scope) bndr
533 = case substTyVar te in_scope bndr of
534 (te', in_scope', bndr') -> ((te', ve, in_scope'), bndr')
537 = case substId clone_fn te ve in_scope undefined bndr of
538 (ve', in_scope', _, bndr') -> ((te, ve', in_scope'), bndr')
541 clone_fn in_scope _ bndr
542 | bndr `elemVarSet` in_scope = Just (uniqAway in_scope bndr, undefined)
543 | otherwise = Nothing
547 Substituting in binders is a rather tricky part of the whole compiler.
550 substIds :: (IdOrTyVarSet -> us -> Id -> Maybe (us, Id)) -- Cloner
551 -> TyVarSubst -> IdSubst -> IdOrTyVarSet -- Usual stuff
552 -> us -- Unique supply
554 -> (IdSubst, IdOrTyVarSet, -- New id_subst, in_scope
555 us, -- New unique supply
558 substIds clone_fn ty_subst id_subst in_scope us []
559 = (id_subst, in_scope, us, [])
561 substIds clone_fn ty_subst id_subst in_scope us (id:ids)
562 = case (substId clone_fn ty_subst id_subst in_scope us id) of {
563 (id_subst', in_scope', us', id') ->
565 case (substIds clone_fn ty_subst id_subst' in_scope' us' ids) of {
566 (id_subst'', in_scope'', us'', ids') ->
568 (id_subst'', in_scope'', us'', id':ids')
572 substId :: (IdOrTyVarSet -> us -> Id -> Maybe (us, Id)) -- Cloner
573 -> TyVarSubst -> IdSubst -> IdOrTyVarSet -- Usual stuff
574 -> us -- Unique supply
576 -> (IdSubst, IdOrTyVarSet, -- New id_subst, in_scope
577 us, -- New unique supply
580 -- Returns an Id with empty unfolding and spec-env.
581 -- It's up to the caller to sort these out.
584 ty_subst id_subst in_scope
587 -- No need to clone, but we *must* zap any current substitution
588 -- for the variable. For example:
589 -- (\x.e) with id_subst = [x |-> e']
590 -- Here we must simply zap the substitution for x
591 = (delVarEnv id_subst id, extendVarSet in_scope id, us, id)
594 = (extendVarEnv id_subst id (Done (Var new_id)),
595 extendVarSet in_scope new_id,
600 old_id_will_do = old1 && old2 && old3 && {-old4 && -}not cloned
602 -- id1 has its type zapped
603 (id1,old1) | isEmptyVarEnv ty_subst
604 || isEmptyVarSet (tyVarsOfType id_ty) = (id, True)
605 | otherwise = (setIdType id ty', False)
607 ty' = fullSubstTy ty_subst in_scope id_ty
609 -- id2 has its SpecEnv zapped
610 -- It's filled in later by Simplify.simplPrags
611 (id2,old2) | isEmptySpecEnv spec_env = (id1, True)
612 | otherwise = (setIdSpecialisation id1 emptySpecEnv, False)
613 spec_env = getIdSpecialisation id
615 -- id3 has its Unfolding zapped
616 -- This is very important; occasionally a let-bound binder is used
617 -- as a binder in some lambda, in which case its unfolding is utterly
618 -- bogus. Also the unfolding uses old binders so if we left it we'd
619 -- have to substitute it. Much better simply to give the Id a new
620 -- unfolding each time, which is what the simplifier does.
621 (id3,old3) | hasUnfolding (getIdUnfolding id) = (id2 `setIdUnfolding` noUnfolding, False)
622 | otherwise = (id2, True)
624 -- new_id is cloned if necessary
625 (new_us, new_id, cloned) = case clone_fn in_scope us id3 of
626 Nothing -> (us, id3, False)
627 Just (us', id') -> (us', id', True)
629 -- new_id_bndr has its Inline info neutered. We must forget about whether it
630 -- was marked safe-to-inline, because that isn't necessarily true in
631 -- the simplified expression. We do this for the *binder* which will
632 -- be used at the binding site, but we *dont* do it for new_id, which
633 -- is put into the in_scope env. Why not? Because the in_scope env
634 -- carries down the occurrence information to usage sites!
636 -- Net result: post-simplification, occurrences may have over-optimistic
637 -- occurrence info, but binders won't.
638 {- (new_id_bndr, old4)
639 = case getInlinePragma id of
640 ICanSafelyBeINLINEd _ _ -> (setInlinePragma new_id NoInlinePragInfo, False)
641 other -> (new_id, True)