2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreUtils]{Utility functions on @Core@ syntax}
8 exprType, coreAltsType,
11 mkNote, mkInlineMe, mkSCC, mkCoerce,
12 bindNonRec, mkIfThenElse, mkAltExpr,
14 exprIsBottom, exprIsDupable, exprIsTrivial, exprIsCheap,
15 exprIsValue,exprOkForSpeculation, exprIsBig,
16 exprArity, exprIsConApp_maybe,
18 idAppIsBottom, idAppIsCheap,
20 etaReduceExpr, exprEtaExpandArity,
29 cheapEqExpr, eqExpr, applyTypeToArgs
32 #include "HsVersions.h"
35 import GlaExts -- For `xori`
38 import CoreFVs ( exprFreeVars )
39 import PprCore ( pprCoreExpr )
40 import Var ( Var, isId, isTyVar )
43 import Name ( isLocallyDefined, hashName )
44 import Literal ( Literal, hashLiteral, literalType )
45 import DataCon ( DataCon, dataConRepArity )
46 import PrimOp ( primOpOkForSpeculation, primOpIsCheap )
47 import Id ( Id, idType, idFlavour, idStrictness, idLBVarInfo, mkWildId,
48 idArity, idName, idUnfolding, idInfo, isDataConId_maybe
51 import IdInfo ( arityLowerBound, InlinePragInfo(..),
55 import Demand ( appIsBottom )
56 import Type ( Type, mkFunTy, mkForAllTy,
57 splitFunTy_maybe, tyVarsOfType, tyVarsOfTypes,
58 isNotUsgTy, mkUsgTy, unUsgTy, UsageAnn(..),
59 applyTys, isUnLiftedType, seqType
61 import TysWiredIn ( boolTy, stringTy, trueDataCon, falseDataCon )
62 import CostCentre ( CostCentre )
63 import Unique ( buildIdKey, augmentIdKey )
64 import Util ( zipWithEqual, mapAccumL )
65 import Maybes ( maybeToBool )
67 import TysPrim ( alphaTy ) -- Debugging only
71 %************************************************************************
73 \subsection{Find the type of a Core atom/expression}
75 %************************************************************************
78 exprType :: CoreExpr -> Type
80 exprType (Var var) = idType var
81 exprType (Lit lit) = literalType lit
82 exprType (Let _ body) = exprType body
83 exprType (Case _ _ alts) = coreAltsType alts
84 exprType (Note (Coerce ty _) e) = ty -- **! should take usage from e
85 exprType (Note (TermUsg u) e) = mkUsgTy u (unUsgTy (exprType e))
86 exprType (Note other_note e) = exprType e
87 exprType (Lam binder expr)
88 | isId binder = (case idLBVarInfo binder of
89 IsOneShotLambda -> mkUsgTy UsOnce
91 idType binder `mkFunTy` exprType expr
92 | isTyVar binder = mkForAllTy binder (exprType expr)
95 = case collectArgs e of
96 (fun, args) -> applyTypeToArgs e (exprType fun) args
98 exprType other = pprTrace "exprType" (pprCoreExpr other) alphaTy
100 coreAltsType :: [CoreAlt] -> Type
101 coreAltsType ((_,_,rhs) : _) = exprType rhs
105 -- The first argument is just for debugging
106 applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> Type
107 applyTypeToArgs e op_ty [] = op_ty
109 applyTypeToArgs e op_ty (Type ty : args)
110 = -- Accumulate type arguments so we can instantiate all at once
111 ASSERT2( all isNotUsgTy tys,
112 ppr e <+> text "of" <+> ppr op_ty <+> text "to" <+>
113 ppr (Type ty : args) <+> text "i.e." <+> ppr tys )
114 applyTypeToArgs e (applyTys op_ty tys) rest_args
116 (tys, rest_args) = go [ty] args
117 go tys (Type ty : args) = go (ty:tys) args
118 go tys rest_args = (reverse tys, rest_args)
120 applyTypeToArgs e op_ty (other_arg : args)
121 = case (splitFunTy_maybe op_ty) of
122 Just (_, res_ty) -> applyTypeToArgs e res_ty args
123 Nothing -> pprPanic "applyTypeToArgs" (pprCoreExpr e)
128 %************************************************************************
130 \subsection{Attaching notes}
132 %************************************************************************
134 mkNote removes redundant coercions, and SCCs where possible
137 mkNote :: Note -> CoreExpr -> CoreExpr
138 mkNote (Coerce to_ty from_ty) expr = mkCoerce to_ty from_ty expr
139 mkNote (SCC cc) expr = mkSCC cc expr
140 mkNote InlineMe expr = mkInlineMe expr
141 mkNote note expr = Note note expr
143 -- Slide InlineCall in around the function
144 -- No longer necessary I think (SLPJ Apr 99)
145 -- mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
146 -- mkNote InlineCall (Var v) = Note InlineCall (Var v)
147 -- mkNote InlineCall expr = expr
150 Drop trivial InlineMe's. This is somewhat important, because if we have an unfolding
151 that looks like (Note InlineMe (Var v)), the InlineMe doesn't go away because it may
152 not be *applied* to anything.
155 mkInlineMe e | exprIsTrivial e = e
156 | otherwise = Note InlineMe e
162 mkCoerce :: Type -> Type -> CoreExpr -> CoreExpr
164 mkCoerce to_ty from_ty (Note (Coerce to_ty2 from_ty2) expr)
165 = ASSERT( from_ty == to_ty2 )
166 mkCoerce to_ty from_ty2 expr
168 mkCoerce to_ty from_ty expr
169 | to_ty == from_ty = expr
170 | otherwise = ASSERT( from_ty == exprType expr )
171 Note (Coerce to_ty from_ty) expr
175 mkSCC :: CostCentre -> Expr b -> Expr b
176 -- Note: Nested SCC's *are* preserved for the benefit of
177 -- cost centre stack profiling (Durham)
179 mkSCC cc (Lit lit) = Lit lit
180 mkSCC cc (Lam x e) = Lam x (mkSCC cc e) -- Move _scc_ inside lambda
181 mkSCC cc expr = Note (SCC cc) expr
185 %************************************************************************
187 \subsection{Other expression construction}
189 %************************************************************************
192 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
193 -- (bindNonRec x r b) produces either
196 -- case r of x { _DEFAULT_ -> b }
198 -- depending on whether x is unlifted or not
199 -- It's used by the desugarer to avoid building bindings
200 -- that give Core Lint a heart attack. Actually the simplifier
201 -- deals with them perfectly well.
202 bindNonRec bndr rhs body
203 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
204 | otherwise = Let (NonRec bndr rhs) body
208 mkAltExpr :: AltCon -> [CoreBndr] -> [Type] -> CoreExpr
209 -- This guy constructs the value that the scrutinee must have
210 -- when you are in one particular branch of a case
211 mkAltExpr (DataAlt con) args inst_tys
212 = mkConApp con (map Type inst_tys ++ map varToCoreExpr args)
213 mkAltExpr (LitAlt lit) [] []
216 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
217 mkIfThenElse guard then_expr else_expr
218 = Case guard (mkWildId boolTy)
219 [ (DataAlt trueDataCon, [], then_expr),
220 (DataAlt falseDataCon, [], else_expr) ]
223 %************************************************************************
225 \subsection{Figuring out things about expressions}
227 %************************************************************************
229 @exprIsTrivial@ is true of expressions we are unconditionally
230 happy to duplicate; simple variables and constants,
231 and type applications.
233 @exprIsBottom@ is true of expressions that are guaranteed to diverge
237 exprIsTrivial (Type _) = True
238 exprIsTrivial (Lit lit) = True
239 exprIsTrivial (Var v) = True
240 exprIsTrivial (App e arg) = isTypeArg arg && exprIsTrivial e
241 exprIsTrivial (Note _ e) = exprIsTrivial e
242 exprIsTrivial (Lam b body) | isTyVar b = exprIsTrivial body
243 exprIsTrivial other = False
247 @exprIsDupable@ is true of expressions that can be duplicated at a modest
248 cost in code size. This will only happen in different case
249 branches, so there's no issue about duplicating work.
251 That is, exprIsDupable returns True of (f x) even if
252 f is very very expensive to call.
254 Its only purpose is to avoid fruitless let-binding
255 and then inlining of case join points
259 exprIsDupable (Type _) = True
260 exprIsDupable (Var v) = True
261 exprIsDupable (Lit lit) = True
262 exprIsDupable (Note _ e) = exprIsDupable e
266 go (Var v) n_args = True
267 go (App f a) n_args = n_args < dupAppSize
270 go other n_args = False
273 dupAppSize = 4 -- Size of application we are prepared to duplicate
276 @exprIsCheap@ looks at a Core expression and returns \tr{True} if
277 it is obviously in weak head normal form, or is cheap to get to WHNF.
278 [Note that that's not the same as exprIsDupable; an expression might be
279 big, and hence not dupable, but still cheap.]
281 By ``cheap'' we mean a computation we're willing to:
282 push inside a lambda, or
283 inline at more than one place
284 That might mean it gets evaluated more than once, instead of being
285 shared. The main examples of things which aren't WHNF but are
291 where e, and all the ei are cheap; and
296 where e and b are cheap; and
300 where op is a cheap primitive operator
304 Notice that a variable is considered 'cheap': we can push it inside a lambda,
305 because sharing will make sure it is only evaluated once.
308 exprIsCheap :: CoreExpr -> Bool
309 exprIsCheap (Lit lit) = True
310 exprIsCheap (Type _) = True
311 exprIsCheap (Var _) = True
312 exprIsCheap (Note _ e) = exprIsCheap e
313 exprIsCheap (Lam x e) = if isId x then True else exprIsCheap e
314 exprIsCheap (Case (Var v) _ alts) = and [exprIsCheap rhs | (_,_,rhs) <- alts]
315 -- Experimentally, treat (case x of ...) as cheap
316 -- This improves arities of overloaded functions where
317 -- there is only dictionary selection (no construction) involved
318 exprIsCheap other_expr
319 = go other_expr 0 True
321 go (Var f) n_args args_cheap
322 = (idAppIsCheap f n_args && args_cheap)
323 -- A constructor, cheap primop, or partial application
325 || idAppIsBottom f n_args
326 -- Application of a function which
327 -- always gives bottom; we treat this as
328 -- a WHNF, because it certainly doesn't
329 -- need to be shared!
331 go (App f a) n_args args_cheap
332 | isTypeArg a = go f n_args args_cheap
333 | otherwise = go f (n_args + 1) (exprIsCheap a && args_cheap)
335 go other n_args args_cheap = False
337 idAppIsCheap :: Id -> Int -> Bool
338 idAppIsCheap id n_val_args
339 | n_val_args == 0 = True -- Just a type application of
340 -- a variable (f t1 t2 t3)
342 | otherwise = case idFlavour id of
344 RecordSelId _ -> True -- I'm experimenting with making record selection
345 -- look cheap, so we will substitute it inside a
346 -- lambda. Particularly for dictionary field selection
348 PrimOpId op -> primOpIsCheap op -- In principle we should worry about primops
349 -- that return a type variable, since the result
350 -- might be applied to something, but I'm not going
351 -- to bother to check the number of args
352 other -> n_val_args < idArity id
355 exprOkForSpeculation returns True of an expression that it is
357 * safe to evaluate even if normal order eval might not
358 evaluate the expression at all, or
360 * safe *not* to evaluate even if normal order would do so
364 the expression guarantees to terminate,
366 without raising an exception,
367 without causing a side effect (e.g. writing a mutable variable)
370 let x = case y# +# 1# of { r# -> I# r# }
373 case y# +# 1# of { r# ->
378 We can only do this if the (y+1) is ok for speculation: it has no
379 side effects, and can't diverge or raise an exception.
382 exprOkForSpeculation :: CoreExpr -> Bool
383 exprOkForSpeculation (Lit _) = True
384 exprOkForSpeculation (Var v) = isUnLiftedType (idType v)
385 exprOkForSpeculation (Note _ e) = exprOkForSpeculation e
386 exprOkForSpeculation other_expr
387 = go other_expr 0 True
389 go (Var f) n_args args_ok
390 = case idFlavour f of
391 DataConId _ -> True -- The strictness of the constructor has already
392 -- been expressed by its "wrapper", so we don't need
393 -- to take the arguments into account
395 PrimOpId op -> primOpOkForSpeculation op && args_ok
396 -- A bit conservative: we don't really need
397 -- to care about lazy arguments, but this is easy
401 go (App f a) n_args args_ok
402 | isTypeArg a = go f n_args args_ok
403 | otherwise = go f (n_args + 1) (exprOkForSpeculation a && args_ok)
405 go other n_args args_ok = False
410 exprIsBottom :: CoreExpr -> Bool -- True => definitely bottom
411 exprIsBottom e = go 0 e
413 -- n is the number of args
414 go n (Note _ e) = go n e
415 go n (Let _ e) = go n e
416 go n (Case e _ _) = go 0 e -- Just check the scrut
417 go n (App e _) = go (n+1) e
418 go n (Var v) = idAppIsBottom v n
420 go n (Lam _ _) = False
422 idAppIsBottom :: Id -> Int -> Bool
423 idAppIsBottom id n_val_args = appIsBottom (idStrictness id) n_val_args
426 @exprIsValue@ returns true for expressions that are certainly *already*
427 evaluated to WHNF. This is used to decide wether it's ok to change
428 case x of _ -> e ===> e
430 and to decide whether it's safe to discard a `seq`
432 So, it does *not* treat variables as evaluated, unless they say they are
435 exprIsValue :: CoreExpr -> Bool -- True => Value-lambda, constructor, PAP
436 exprIsValue (Type ty) = True -- Types are honorary Values; we don't mind
438 exprIsValue (Lit l) = True
439 exprIsValue (Lam b e) = isId b || exprIsValue e
440 exprIsValue (Note _ e) = exprIsValue e
441 exprIsValue other_expr
444 go (Var f) n_args = idAppIsValue f n_args
447 | isTypeArg a = go f n_args
448 | otherwise = go f (n_args + 1)
450 go (Note _ f) n_args = go f n_args
452 go other n_args = False
454 idAppIsValue :: Id -> Int -> Bool
455 idAppIsValue id n_val_args
456 = case idFlavour id of
458 PrimOpId _ -> n_val_args < idArity id
459 other | n_val_args == 0 -> isEvaldUnfolding (idUnfolding id)
460 | otherwise -> n_val_args < idArity id
461 -- A worry: what if an Id's unfolding is just itself:
462 -- then we could get an infinite loop...
466 exprArity :: CoreExpr -> Int -- How many value lambdas are at the top
467 exprArity (Lam b e) | isTyVar b = exprArity e
468 | otherwise = 1 + exprArity e
470 exprArity (Note note e) | ok_note note = exprArity e
472 ok_note (Coerce _ _) = True
473 -- We *do* look through coerces when getting arities.
474 -- Reason: arities are to do with *representation* and
476 ok_note InlineMe = True
477 ok_note InlineCall = True
478 ok_note other = False
479 -- SCC and TermUsg might be over-conservative?
485 exprIsConApp_maybe :: CoreExpr -> Maybe (DataCon, [CoreExpr])
486 exprIsConApp_maybe expr
487 = analyse (collectArgs expr)
489 analyse (Var fun, args)
490 | maybeToBool maybe_con_app = maybe_con_app
492 maybe_con_app = case isDataConId_maybe fun of
493 Just con | length args >= dataConRepArity con
494 -- Might be > because the arity excludes type args
498 analyse (Var fun, [])
499 = case maybeUnfoldingTemplate (idUnfolding fun) of
501 Just unf -> exprIsConApp_maybe unf
503 analyse other = Nothing
507 %************************************************************************
509 \subsection{Eta reduction and expansion}
511 %************************************************************************
513 @etaReduceExpr@ trys an eta reduction at the top level of a Core Expr.
515 e.g. \ x y -> f x y ===> f
517 But we only do this if it gets rid of a whole lambda, not part.
518 The idea is that lambdas are often quite helpful: they indicate
519 head normal forms, so we don't want to chuck them away lightly.
522 etaReduceExpr :: CoreExpr -> CoreExpr
523 -- ToDo: we should really check that we don't turn a non-bottom
524 -- lambda into a bottom variable. Sigh
526 etaReduceExpr expr@(Lam bndr body)
527 = check (reverse binders) body
529 (binders, body) = collectBinders expr
532 | not (any (`elemVarSet` body_fvs) binders)
535 body_fvs = exprFreeVars body
537 check (b : bs) (App fun arg)
538 | (varToCoreExpr b `cheapEqExpr` arg)
541 check _ _ = expr -- Bale out
543 etaReduceExpr expr = expr -- The common case
548 exprEtaExpandArity :: CoreExpr -> Int -- The number of args the thing can be applied to
549 -- without doing much work
550 -- This is used when eta expanding
551 -- e ==> \xy -> e x y
553 -- It returns 1 (or more) to:
554 -- case x of p -> \s -> ...
555 -- because for I/O ish things we really want to get that \s to the top.
556 -- We are prepared to evaluate x each time round the loop in order to get that
557 -- Hence "generous" arity
562 go (Var v) = idArity v
563 go (App f (Type _)) = go f
564 go (App f a) | exprIsCheap a = (go f - 1) `max` 0 -- Never go -ve!
565 go (Lam x e) | isId x = go e + 1
567 go (Note n e) | ok_note n = go e
568 go (Case scrut _ alts)
569 | exprIsCheap scrut = min_zero [go rhs | (_,_,rhs) <- alts]
571 | all exprIsCheap (rhssOfBind b) = go e
575 ok_note (Coerce _ _) = True
576 ok_note InlineCall = True
577 ok_note other = False
578 -- Notice that we do not look through __inline_me__
579 -- This one is a bit more surprising, but consider
580 -- f = _inline_me (\x -> e)
581 -- We DO NOT want to eta expand this to
582 -- f = \x -> (_inline_me (\x -> e)) x
583 -- because the _inline_me gets dropped now it is applied,
588 min_zero :: [Int] -> Int -- Find the minimum, but zero is the smallest
589 min_zero (x:xs) = go x xs
591 go 0 xs = 0 -- Nothing beats zero
593 go min (x:xs) | x < min = go x xs
594 | otherwise = go min xs
599 %************************************************************************
601 \subsection{Equality}
603 %************************************************************************
605 @cheapEqExpr@ is a cheap equality test which bales out fast!
606 True => definitely equal
607 False => may or may not be equal
610 cheapEqExpr :: Expr b -> Expr b -> Bool
612 cheapEqExpr (Var v1) (Var v2) = v1==v2
613 cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
614 cheapEqExpr (Type t1) (Type t2) = t1 == t2
616 cheapEqExpr (App f1 a1) (App f2 a2)
617 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
619 cheapEqExpr _ _ = False
621 exprIsBig :: Expr b -> Bool
622 -- Returns True of expressions that are too big to be compared by cheapEqExpr
623 exprIsBig (Lit _) = False
624 exprIsBig (Var v) = False
625 exprIsBig (Type t) = False
626 exprIsBig (App f a) = exprIsBig f || exprIsBig a
627 exprIsBig other = True
632 eqExpr :: CoreExpr -> CoreExpr -> Bool
633 -- Works ok at more general type, but only needed at CoreExpr
635 = eq emptyVarEnv e1 e2
637 -- The "env" maps variables in e1 to variables in ty2
638 -- So when comparing lambdas etc,
639 -- we in effect substitute v2 for v1 in e1 before continuing
640 eq env (Var v1) (Var v2) = case lookupVarEnv env v1 of
641 Just v1' -> v1' == v2
644 eq env (Lit lit1) (Lit lit2) = lit1 == lit2
645 eq env (App f1 a1) (App f2 a2) = eq env f1 f2 && eq env a1 a2
646 eq env (Lam v1 e1) (Lam v2 e2) = eq (extendVarEnv env v1 v2) e1 e2
647 eq env (Let (NonRec v1 r1) e1)
648 (Let (NonRec v2 r2) e2) = eq env r1 r2 && eq (extendVarEnv env v1 v2) e1 e2
649 eq env (Let (Rec ps1) e1)
650 (Let (Rec ps2) e2) = length ps1 == length ps2 &&
651 and (zipWith eq_rhs ps1 ps2) &&
654 env' = extendVarEnvList env [(v1,v2) | ((v1,_),(v2,_)) <- zip ps1 ps2]
655 eq_rhs (_,r1) (_,r2) = eq env' r1 r2
656 eq env (Case e1 v1 a1)
657 (Case e2 v2 a2) = eq env e1 e2 &&
658 length a1 == length a2 &&
659 and (zipWith (eq_alt env') a1 a2)
661 env' = extendVarEnv env v1 v2
663 eq env (Note n1 e1) (Note n2 e2) = eq_note env n1 n2 && eq env e1 e2
664 eq env (Type t1) (Type t2) = t1 == t2
667 eq_list env [] [] = True
668 eq_list env (e1:es1) (e2:es2) = eq env e1 e2 && eq_list env es1 es2
669 eq_list env es1 es2 = False
671 eq_alt env (c1,vs1,r1) (c2,vs2,r2) = c1==c2 &&
672 eq (extendVarEnvList env (vs1 `zip` vs2)) r1 r2
674 eq_note env (SCC cc1) (SCC cc2) = cc1 == cc2
675 eq_note env (Coerce t1 f1) (Coerce t2 f2) = t1==t2 && f1==f2
676 eq_note env InlineCall InlineCall = True
677 eq_note env other1 other2 = False
681 %************************************************************************
683 \subsection{The size of an expression}
685 %************************************************************************
688 coreBindsSize :: [CoreBind] -> Int
689 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
691 exprSize :: CoreExpr -> Int
692 -- A measure of the size of the expressions
693 -- It also forces the expression pretty drastically as a side effect
694 exprSize (Var v) = varSize v
695 exprSize (Lit lit) = 1
696 exprSize (App f a) = exprSize f + exprSize a
697 exprSize (Lam b e) = varSize b + exprSize e
698 exprSize (Let b e) = bindSize b + exprSize e
699 exprSize (Case e b as) = exprSize e + varSize b + foldr ((+) . altSize) 0 as
700 exprSize (Note n e) = exprSize e
701 exprSize (Type t) = seqType t `seq`
704 exprsSize = foldr ((+) . exprSize) 0
706 varSize :: Var -> Int
707 varSize b | isTyVar b = 1
708 | otherwise = seqType (idType b) `seq`
709 megaSeqIdInfo (idInfo b) `seq`
712 varsSize = foldr ((+) . varSize) 0
714 bindSize (NonRec b e) = varSize b + exprSize e
715 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
717 pairSize (b,e) = varSize b + exprSize e
719 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
723 %************************************************************************
727 %************************************************************************
730 hashExpr :: CoreExpr -> Int
731 hashExpr e | hash < 0 = 77 -- Just in case we hit -maxInt
734 hash = abs (hash_expr e) -- Negative numbers kill UniqFM
736 hash_expr (Note _ e) = hash_expr e
737 hash_expr (Let (NonRec b r) e) = hashId b
738 hash_expr (Let (Rec ((b,r):_)) e) = hashId b
739 hash_expr (Case _ b _) = hashId b
740 hash_expr (App f e) = hash_expr f * fast_hash_expr e
741 hash_expr (Var v) = hashId v
742 hash_expr (Lit lit) = hashLiteral lit
743 hash_expr (Lam b _) = hashId b
744 hash_expr (Type t) = trace "hash_expr: type" 1 -- Shouldn't happen
746 fast_hash_expr (Var v) = hashId v
747 fast_hash_expr (Lit lit) = hashLiteral lit
748 fast_hash_expr (App f (Type _)) = fast_hash_expr f
749 fast_hash_expr (App f a) = fast_hash_expr a
750 fast_hash_expr (Lam b _) = hashId b
751 fast_hash_expr other = 1
754 hashId id = hashName (idName id)