2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreUtils]{Utility functions on @Core@ syntax}
9 mkNote, mkInlineMe, mkSCC, mkCoerce,
10 bindNonRec, mkIfThenElse, mkAltExpr,
13 -- Properties of expressions
14 exprType, coreAltsType,
15 exprIsBottom, exprIsDupable, exprIsTrivial, exprIsCheap,
16 exprIsValue,exprOkForSpeculation, exprIsBig,
18 idAppIsBottom, idAppIsCheap,
20 -- Expr transformation
21 etaReduceExpr, exprEtaExpandArity,
30 cheapEqExpr, eqExpr, applyTypeToArgs
33 #include "HsVersions.h"
36 import GlaExts -- For `xori`
39 import CoreFVs ( exprFreeVars )
40 import PprCore ( pprCoreExpr )
41 import Var ( Var, isId, isTyVar )
44 import Name ( hashName )
45 import Literal ( hashLiteral, literalType, litIsDupable )
46 import DataCon ( DataCon, dataConRepArity )
47 import PrimOp ( primOpOkForSpeculation, primOpIsCheap,
49 import Id ( Id, idType, idFlavour, idStrictness, idLBVarInfo,
50 mkWildId, idArity, idName, idUnfolding, idInfo,
51 isDataConId_maybe, isPrimOpId_maybe
53 import IdInfo ( LBVarInfo(..),
56 import Demand ( appIsBottom )
57 import Type ( Type, mkFunTy, mkForAllTy,
58 splitFunTy_maybe, tyVarsOfType, tyVarsOfTypes,
59 applyTys, isUnLiftedType, seqType,
62 import TysWiredIn ( boolTy, trueDataCon, falseDataCon )
63 import CostCentre ( CostCentre )
64 import Maybes ( maybeToBool )
66 import TysPrim ( alphaTy ) -- Debugging only
70 %************************************************************************
72 \subsection{Find the type of a Core atom/expression}
74 %************************************************************************
77 exprType :: CoreExpr -> Type
79 exprType (Var var) = idType var
80 exprType (Lit lit) = literalType lit
81 exprType (Let _ body) = exprType body
82 exprType (Case _ _ alts) = coreAltsType alts
83 exprType (Note (Coerce ty _) e) = ty -- **! should take usage from e
84 exprType (Note other_note e) = exprType e
85 exprType (Lam binder expr) = mkPiType binder (exprType expr)
87 = case collectArgs e of
88 (fun, args) -> applyTypeToArgs e (exprType fun) args
90 exprType other = pprTrace "exprType" (pprCoreExpr other) alphaTy
92 coreAltsType :: [CoreAlt] -> Type
93 coreAltsType ((_,_,rhs) : _) = exprType rhs
96 @mkPiType@ makes a (->) type or a forall type, depending on whether
97 it is given a type variable or a term variable. We cleverly use the
98 lbvarinfo field to figure out the right annotation for the arrove in
99 case of a term variable.
102 mkPiType :: Var -> Type -> Type -- The more polymorphic version doesn't work...
103 mkPiType v ty | isId v = (case idLBVarInfo v of
104 LBVarInfo u -> mkUTy u
106 mkFunTy (idType v) ty
107 | isTyVar v = mkForAllTy v ty
111 -- The first argument is just for debugging
112 applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> Type
113 applyTypeToArgs e op_ty [] = op_ty
115 applyTypeToArgs e op_ty (Type ty : args)
116 = -- Accumulate type arguments so we can instantiate all at once
117 applyTypeToArgs e (applyTys op_ty tys) rest_args
119 (tys, rest_args) = go [ty] args
120 go tys (Type ty : args) = go (ty:tys) args
121 go tys rest_args = (reverse tys, rest_args)
123 applyTypeToArgs e op_ty (other_arg : args)
124 = case (splitFunTy_maybe op_ty) of
125 Just (_, res_ty) -> applyTypeToArgs e res_ty args
126 Nothing -> pprPanic "applyTypeToArgs" (pprCoreExpr e)
131 %************************************************************************
133 \subsection{Attaching notes}
135 %************************************************************************
137 mkNote removes redundant coercions, and SCCs where possible
140 mkNote :: Note -> CoreExpr -> CoreExpr
141 mkNote (Coerce to_ty from_ty) expr = mkCoerce to_ty from_ty expr
142 mkNote (SCC cc) expr = mkSCC cc expr
143 mkNote InlineMe expr = mkInlineMe expr
144 mkNote note expr = Note note expr
146 -- Slide InlineCall in around the function
147 -- No longer necessary I think (SLPJ Apr 99)
148 -- mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
149 -- mkNote InlineCall (Var v) = Note InlineCall (Var v)
150 -- mkNote InlineCall expr = expr
153 Drop trivial InlineMe's. This is somewhat important, because if we have an unfolding
154 that looks like (Note InlineMe (Var v)), the InlineMe doesn't go away because it may
155 not be *applied* to anything.
158 mkInlineMe e | exprIsTrivial e = e
159 | otherwise = Note InlineMe e
165 mkCoerce :: Type -> Type -> CoreExpr -> CoreExpr
167 mkCoerce to_ty from_ty (Note (Coerce to_ty2 from_ty2) expr)
168 = ASSERT( from_ty == to_ty2 )
169 mkCoerce to_ty from_ty2 expr
171 mkCoerce to_ty from_ty expr
172 | to_ty == from_ty = expr
173 | otherwise = ASSERT( from_ty == exprType expr )
174 Note (Coerce to_ty from_ty) expr
178 mkSCC :: CostCentre -> Expr b -> Expr b
179 -- Note: Nested SCC's *are* preserved for the benefit of
180 -- cost centre stack profiling (Durham)
182 mkSCC cc (Lit lit) = Lit lit
183 mkSCC cc (Lam x e) = Lam x (mkSCC cc e) -- Move _scc_ inside lambda
184 mkSCC cc expr = Note (SCC cc) expr
188 %************************************************************************
190 \subsection{Other expression construction}
192 %************************************************************************
195 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
196 -- (bindNonRec x r b) produces either
199 -- case r of x { _DEFAULT_ -> b }
201 -- depending on whether x is unlifted or not
202 -- It's used by the desugarer to avoid building bindings
203 -- that give Core Lint a heart attack. Actually the simplifier
204 -- deals with them perfectly well.
205 bindNonRec bndr rhs body
206 | isUnLiftedType (idType bndr) = Case rhs bndr [(DEFAULT,[],body)]
207 | otherwise = Let (NonRec bndr rhs) body
211 mkAltExpr :: AltCon -> [CoreBndr] -> [Type] -> CoreExpr
212 -- This guy constructs the value that the scrutinee must have
213 -- when you are in one particular branch of a case
214 mkAltExpr (DataAlt con) args inst_tys
215 = mkConApp con (map Type inst_tys ++ map varToCoreExpr args)
216 mkAltExpr (LitAlt lit) [] []
219 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
220 mkIfThenElse guard then_expr else_expr
221 = Case guard (mkWildId boolTy)
222 [ (DataAlt trueDataCon, [], then_expr),
223 (DataAlt falseDataCon, [], else_expr) ]
226 %************************************************************************
228 \subsection{Figuring out things about expressions}
230 %************************************************************************
232 @exprIsTrivial@ is true of expressions we are unconditionally happy to
233 duplicate; simple variables and constants, and type
234 applications. Note that primop Ids aren't considered
237 @exprIsBottom@ is true of expressions that are guaranteed to diverge
241 exprIsTrivial (Var v)
242 | Just op <- isPrimOpId_maybe v = primOpIsDupable op
244 exprIsTrivial (Type _) = True
245 exprIsTrivial (Lit lit) = True
246 exprIsTrivial (App e arg) = isTypeArg arg && exprIsTrivial e
247 exprIsTrivial (Note _ e) = exprIsTrivial e
248 exprIsTrivial (Lam b body) | isTyVar b = exprIsTrivial body
249 exprIsTrivial other = False
253 @exprIsDupable@ is true of expressions that can be duplicated at a modest
254 cost in code size. This will only happen in different case
255 branches, so there's no issue about duplicating work.
257 That is, exprIsDupable returns True of (f x) even if
258 f is very very expensive to call.
260 Its only purpose is to avoid fruitless let-binding
261 and then inlining of case join points
265 exprIsDupable (Type _) = True
266 exprIsDupable (Var v) = True
267 exprIsDupable (Lit lit) = litIsDupable lit
268 exprIsDupable (Note _ e) = exprIsDupable e
272 go (Var v) n_args = True
273 go (App f a) n_args = n_args < dupAppSize
276 go other n_args = False
279 dupAppSize = 4 -- Size of application we are prepared to duplicate
282 @exprIsCheap@ looks at a Core expression and returns \tr{True} if
283 it is obviously in weak head normal form, or is cheap to get to WHNF.
284 [Note that that's not the same as exprIsDupable; an expression might be
285 big, and hence not dupable, but still cheap.]
287 By ``cheap'' we mean a computation we're willing to:
288 push inside a lambda, or
289 inline at more than one place
290 That might mean it gets evaluated more than once, instead of being
291 shared. The main examples of things which aren't WHNF but are
296 (where e, and all the ei are cheap)
299 (where e and b are cheap)
302 (where op is a cheap primitive operator)
305 (because we are happy to substitute it inside a lambda)
307 Notice that a variable is considered 'cheap': we can push it inside a lambda,
308 because sharing will make sure it is only evaluated once.
311 exprIsCheap :: CoreExpr -> Bool
312 exprIsCheap (Lit lit) = True
313 exprIsCheap (Type _) = True
314 exprIsCheap (Var _) = True
315 exprIsCheap (Note _ e) = exprIsCheap e
316 exprIsCheap (Lam x e) = if isId x then True else exprIsCheap e
317 exprIsCheap (Case e _ alts) = exprIsCheap e &&
318 and [exprIsCheap rhs | (_,_,rhs) <- alts]
319 -- Experimentally, treat (case x of ...) as cheap
320 -- (and case __coerce x etc.)
321 -- This improves arities of overloaded functions where
322 -- there is only dictionary selection (no construction) involved
323 exprIsCheap (Let (NonRec x _) e)
324 | isUnLiftedType (idType x) = exprIsCheap e
326 -- strict lets always have cheap right hand sides, and
329 exprIsCheap other_expr
330 = go other_expr 0 True
332 go (Var f) n_args args_cheap
333 = (idAppIsCheap f n_args && args_cheap)
334 -- A constructor, cheap primop, or partial application
336 || idAppIsBottom f n_args
337 -- Application of a function which
338 -- always gives bottom; we treat this as cheap
339 -- because it certainly doesn't need to be shared!
341 go (App f a) n_args args_cheap
342 | isTypeArg a = go f n_args args_cheap
343 | otherwise = go f (n_args + 1) (exprIsCheap a && args_cheap)
345 go other n_args args_cheap = False
347 idAppIsCheap :: Id -> Int -> Bool
348 idAppIsCheap id n_val_args
349 | n_val_args == 0 = True -- Just a type application of
350 -- a variable (f t1 t2 t3)
352 | otherwise = case idFlavour id of
354 RecordSelId _ -> True -- I'm experimenting with making record selection
355 -- look cheap, so we will substitute it inside a
356 -- lambda. Particularly for dictionary field selection
358 PrimOpId op -> primOpIsCheap op -- In principle we should worry about primops
359 -- that return a type variable, since the result
360 -- might be applied to something, but I'm not going
361 -- to bother to check the number of args
362 other -> n_val_args < idArity id
365 exprOkForSpeculation returns True of an expression that it is
367 * safe to evaluate even if normal order eval might not
368 evaluate the expression at all, or
370 * safe *not* to evaluate even if normal order would do so
374 the expression guarantees to terminate,
376 without raising an exception,
377 without causing a side effect (e.g. writing a mutable variable)
380 let x = case y# +# 1# of { r# -> I# r# }
383 case y# +# 1# of { r# ->
388 We can only do this if the (y+1) is ok for speculation: it has no
389 side effects, and can't diverge or raise an exception.
392 exprOkForSpeculation :: CoreExpr -> Bool
393 exprOkForSpeculation (Lit _) = True
394 exprOkForSpeculation (Var v) = isUnLiftedType (idType v)
395 exprOkForSpeculation (Note _ e) = exprOkForSpeculation e
396 exprOkForSpeculation other_expr
397 = go other_expr 0 True
399 go (Var f) n_args args_ok
400 = case idFlavour f of
401 DataConId _ -> True -- The strictness of the constructor has already
402 -- been expressed by its "wrapper", so we don't need
403 -- to take the arguments into account
405 PrimOpId op -> primOpOkForSpeculation op && args_ok
406 -- A bit conservative: we don't really need
407 -- to care about lazy arguments, but this is easy
411 go (App f a) n_args args_ok
412 | isTypeArg a = go f n_args args_ok
413 | otherwise = go f (n_args + 1) (exprOkForSpeculation a && args_ok)
415 go other n_args args_ok = False
420 exprIsBottom :: CoreExpr -> Bool -- True => definitely bottom
421 exprIsBottom e = go 0 e
423 -- n is the number of args
424 go n (Note _ e) = go n e
425 go n (Let _ e) = go n e
426 go n (Case e _ _) = go 0 e -- Just check the scrut
427 go n (App e _) = go (n+1) e
428 go n (Var v) = idAppIsBottom v n
430 go n (Lam _ _) = False
432 idAppIsBottom :: Id -> Int -> Bool
433 idAppIsBottom id n_val_args = appIsBottom (idStrictness id) n_val_args
436 @exprIsValue@ returns true for expressions that are certainly *already*
437 evaluated to WHNF. This is used to decide wether it's ok to change
438 case x of _ -> e ===> e
440 and to decide whether it's safe to discard a `seq`
442 So, it does *not* treat variables as evaluated, unless they say they are
445 exprIsValue :: CoreExpr -> Bool -- True => Value-lambda, constructor, PAP
446 exprIsValue (Type ty) = True -- Types are honorary Values; we don't mind
448 exprIsValue (Lit l) = True
449 exprIsValue (Lam b e) = isId b || exprIsValue e
450 exprIsValue (Note _ e) = exprIsValue e
451 exprIsValue other_expr
454 go (Var f) n_args = idAppIsValue f n_args
457 | isTypeArg a = go f n_args
458 | otherwise = go f (n_args + 1)
460 go (Note _ f) n_args = go f n_args
462 go other n_args = False
464 idAppIsValue :: Id -> Int -> Bool
465 idAppIsValue id n_val_args
466 = case idFlavour id of
468 PrimOpId _ -> n_val_args < idArity id
469 other | n_val_args == 0 -> isEvaldUnfolding (idUnfolding id)
470 | otherwise -> n_val_args < idArity id
471 -- A worry: what if an Id's unfolding is just itself:
472 -- then we could get an infinite loop...
476 exprIsConApp_maybe :: CoreExpr -> Maybe (DataCon, [CoreExpr])
477 exprIsConApp_maybe expr
478 = analyse (collectArgs expr)
480 analyse (Var fun, args)
481 | maybeToBool maybe_con_app = maybe_con_app
483 maybe_con_app = case isDataConId_maybe fun of
484 Just con | length args >= dataConRepArity con
485 -- Might be > because the arity excludes type args
489 analyse (Var fun, [])
490 = case maybeUnfoldingTemplate (idUnfolding fun) of
492 Just unf -> exprIsConApp_maybe unf
494 analyse other = Nothing
498 %************************************************************************
500 \subsection{Eta reduction and expansion}
502 %************************************************************************
504 @etaReduceExpr@ trys an eta reduction at the top level of a Core Expr.
506 e.g. \ x y -> f x y ===> f
508 But we only do this if it gets rid of a whole lambda, not part.
509 The idea is that lambdas are often quite helpful: they indicate
510 head normal forms, so we don't want to chuck them away lightly.
513 etaReduceExpr :: CoreExpr -> CoreExpr
514 -- ToDo: we should really check that we don't turn a non-bottom
515 -- lambda into a bottom variable. Sigh
517 etaReduceExpr expr@(Lam bndr body)
518 = check (reverse binders) body
520 (binders, body) = collectBinders expr
523 | not (any (`elemVarSet` body_fvs) binders)
526 body_fvs = exprFreeVars body
528 check (b : bs) (App fun arg)
529 | (varToCoreExpr b `cheapEqExpr` arg)
532 check _ _ = expr -- Bale out
534 etaReduceExpr expr = expr -- The common case
539 exprEtaExpandArity :: CoreExpr -> Int -- The number of args the thing can be applied to
540 -- without doing much work
541 -- This is used when eta expanding
542 -- e ==> \xy -> e x y
544 -- It returns 1 (or more) to:
545 -- case x of p -> \s -> ...
546 -- because for I/O ish things we really want to get that \s to the top.
547 -- We are prepared to evaluate x each time round the loop in order to get that
548 -- Hence "generous" arity
551 = go e `max` 0 -- Never go -ve!
553 go (Var v) = idArity v
554 go (App f (Type _)) = go f
555 go (App f a) | exprIsCheap a = go f - 1
556 go (Lam x e) | isId x = go e + 1
558 go (Note n e) | ok_note n = go e
559 go (Case scrut _ alts)
560 | exprIsCheap scrut = min_zero [go rhs | (_,_,rhs) <- alts]
562 | all exprIsCheap (rhssOfBind b) = go e
566 ok_note (Coerce _ _) = True
567 ok_note InlineCall = True
568 ok_note other = False
569 -- Notice that we do not look through __inline_me__
570 -- This one is a bit more surprising, but consider
571 -- f = _inline_me (\x -> e)
572 -- We DO NOT want to eta expand this to
573 -- f = \x -> (_inline_me (\x -> e)) x
574 -- because the _inline_me gets dropped now it is applied,
579 min_zero :: [Int] -> Int -- Find the minimum, but zero is the smallest
580 min_zero (x:xs) = go x xs
582 go 0 xs = 0 -- Nothing beats zero
584 go min (x:xs) | x < min = go x xs
585 | otherwise = go min xs
590 %************************************************************************
592 \subsection{Equality}
594 %************************************************************************
596 @cheapEqExpr@ is a cheap equality test which bales out fast!
597 True => definitely equal
598 False => may or may not be equal
601 cheapEqExpr :: Expr b -> Expr b -> Bool
603 cheapEqExpr (Var v1) (Var v2) = v1==v2
604 cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
605 cheapEqExpr (Type t1) (Type t2) = t1 == t2
607 cheapEqExpr (App f1 a1) (App f2 a2)
608 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
610 cheapEqExpr _ _ = False
612 exprIsBig :: Expr b -> Bool
613 -- Returns True of expressions that are too big to be compared by cheapEqExpr
614 exprIsBig (Lit _) = False
615 exprIsBig (Var v) = False
616 exprIsBig (Type t) = False
617 exprIsBig (App f a) = exprIsBig f || exprIsBig a
618 exprIsBig other = True
623 eqExpr :: CoreExpr -> CoreExpr -> Bool
624 -- Works ok at more general type, but only needed at CoreExpr
626 = eq emptyVarEnv e1 e2
628 -- The "env" maps variables in e1 to variables in ty2
629 -- So when comparing lambdas etc,
630 -- we in effect substitute v2 for v1 in e1 before continuing
631 eq env (Var v1) (Var v2) = case lookupVarEnv env v1 of
632 Just v1' -> v1' == v2
635 eq env (Lit lit1) (Lit lit2) = lit1 == lit2
636 eq env (App f1 a1) (App f2 a2) = eq env f1 f2 && eq env a1 a2
637 eq env (Lam v1 e1) (Lam v2 e2) = eq (extendVarEnv env v1 v2) e1 e2
638 eq env (Let (NonRec v1 r1) e1)
639 (Let (NonRec v2 r2) e2) = eq env r1 r2 && eq (extendVarEnv env v1 v2) e1 e2
640 eq env (Let (Rec ps1) e1)
641 (Let (Rec ps2) e2) = length ps1 == length ps2 &&
642 and (zipWith eq_rhs ps1 ps2) &&
645 env' = extendVarEnvList env [(v1,v2) | ((v1,_),(v2,_)) <- zip ps1 ps2]
646 eq_rhs (_,r1) (_,r2) = eq env' r1 r2
647 eq env (Case e1 v1 a1)
648 (Case e2 v2 a2) = eq env e1 e2 &&
649 length a1 == length a2 &&
650 and (zipWith (eq_alt env') a1 a2)
652 env' = extendVarEnv env v1 v2
654 eq env (Note n1 e1) (Note n2 e2) = eq_note env n1 n2 && eq env e1 e2
655 eq env (Type t1) (Type t2) = t1 == t2
658 eq_list env [] [] = True
659 eq_list env (e1:es1) (e2:es2) = eq env e1 e2 && eq_list env es1 es2
660 eq_list env es1 es2 = False
662 eq_alt env (c1,vs1,r1) (c2,vs2,r2) = c1==c2 &&
663 eq (extendVarEnvList env (vs1 `zip` vs2)) r1 r2
665 eq_note env (SCC cc1) (SCC cc2) = cc1 == cc2
666 eq_note env (Coerce t1 f1) (Coerce t2 f2) = t1==t2 && f1==f2
667 eq_note env InlineCall InlineCall = True
668 eq_note env other1 other2 = False
672 %************************************************************************
674 \subsection{The size of an expression}
676 %************************************************************************
679 coreBindsSize :: [CoreBind] -> Int
680 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
682 exprSize :: CoreExpr -> Int
683 -- A measure of the size of the expressions
684 -- It also forces the expression pretty drastically as a side effect
685 exprSize (Var v) = varSize v
686 exprSize (Lit lit) = lit `seq` 1
687 exprSize (App f a) = exprSize f + exprSize a
688 exprSize (Lam b e) = varSize b + exprSize e
689 exprSize (Let b e) = bindSize b + exprSize e
690 exprSize (Case e b as) = exprSize e + varSize b + foldr ((+) . altSize) 0 as
691 exprSize (Note n e) = noteSize n + exprSize e
692 exprSize (Type t) = seqType t `seq` 1
694 noteSize (SCC cc) = cc `seq` 1
695 noteSize (Coerce t1 t2) = seqType t1 `seq` seqType t2 `seq` 1
696 noteSize InlineCall = 1
697 noteSize InlineMe = 1
699 varSize :: Var -> Int
700 varSize b | isTyVar b = 1
701 | otherwise = seqType (idType b) `seq`
702 megaSeqIdInfo (idInfo b) `seq`
705 varsSize = foldr ((+) . varSize) 0
707 bindSize (NonRec b e) = varSize b + exprSize e
708 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
710 pairSize (b,e) = varSize b + exprSize e
712 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
716 %************************************************************************
720 %************************************************************************
723 hashExpr :: CoreExpr -> Int
724 hashExpr e | hash < 0 = 77 -- Just in case we hit -maxInt
727 hash = abs (hash_expr e) -- Negative numbers kill UniqFM
729 hash_expr (Note _ e) = hash_expr e
730 hash_expr (Let (NonRec b r) e) = hashId b
731 hash_expr (Let (Rec ((b,r):_)) e) = hashId b
732 hash_expr (Case _ b _) = hashId b
733 hash_expr (App f e) = hash_expr f * fast_hash_expr e
734 hash_expr (Var v) = hashId v
735 hash_expr (Lit lit) = hashLiteral lit
736 hash_expr (Lam b _) = hashId b
737 hash_expr (Type t) = trace "hash_expr: type" 1 -- Shouldn't happen
739 fast_hash_expr (Var v) = hashId v
740 fast_hash_expr (Lit lit) = hashLiteral lit
741 fast_hash_expr (App f (Type _)) = fast_hash_expr f
742 fast_hash_expr (App f a) = fast_hash_expr a
743 fast_hash_expr (Lam b _) = hashId b
744 fast_hash_expr other = 1
747 hashId id = hashName (idName id)