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,
21 -- Expr transformation
22 etaReduce, exprEtaExpandArity,
32 cheapEqExpr, eqExpr, applyTypeToArgs
35 #include "HsVersions.h"
38 import GlaExts -- For `xori`
41 import CoreFVs ( exprFreeVars )
42 import PprCore ( pprCoreExpr )
43 import Var ( Var, isId, isTyVar )
46 import Name ( hashName )
47 import Literal ( hashLiteral, literalType, litIsDupable )
48 import DataCon ( DataCon, dataConRepArity )
49 import PrimOp ( primOpOkForSpeculation, primOpIsCheap,
51 import Id ( Id, idType, idFlavour, idStrictness, idLBVarInfo,
52 mkWildId, idArity, idName, idUnfolding, idInfo,
53 isDataConId_maybe, isPrimOpId_maybe
55 import IdInfo ( LBVarInfo(..),
58 import Demand ( appIsBottom )
59 import Type ( Type, mkFunTy, mkForAllTy, splitFunTy_maybe,
60 applyTys, isUnLiftedType, seqType, mkUTy
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
497 The arity of an expression (in the code-generator sense, i.e. the
498 number of lambdas at the beginning).
501 exprArity :: CoreExpr -> Int
503 | isTyVar x = exprArity e
504 | otherwise = 1 + exprArity e
506 -- Ignore coercions. Top level sccs are removed by the final
507 -- profiling pass, so we ignore those too.
512 %************************************************************************
514 \subsection{Eta reduction and expansion}
516 %************************************************************************
518 @etaReduce@ trys an eta reduction at the top level of a Core Expr.
520 e.g. \ x y -> f x y ===> f
522 But we only do this if it gets rid of a whole lambda, not part.
523 The idea is that lambdas are often quite helpful: they indicate
524 head normal forms, so we don't want to chuck them away lightly.
527 etaReduce :: CoreExpr -> CoreExpr
528 -- ToDo: we should really check that we don't turn a non-bottom
529 -- lambda into a bottom variable. Sigh
531 etaReduce expr@(Lam bndr body)
532 = check (reverse binders) body
534 (binders, body) = collectBinders expr
537 | not (any (`elemVarSet` body_fvs) binders)
540 body_fvs = exprFreeVars body
542 check (b : bs) (App fun arg)
543 | (varToCoreExpr b `cheapEqExpr` arg)
546 check _ _ = expr -- Bale out
548 etaReduce expr = expr -- The common case
553 exprEtaExpandArity :: CoreExpr -> Int -- The number of args the thing can be applied to
554 -- without doing much work
555 -- This is used when eta expanding
556 -- e ==> \xy -> e x y
558 -- It returns 1 (or more) to:
559 -- case x of p -> \s -> ...
560 -- because for I/O ish things we really want to get that \s to the top.
561 -- We are prepared to evaluate x each time round the loop in order to get that
562 -- Hence "generous" arity
565 = go e `max` 0 -- Never go -ve!
567 go (Var v) = idArity v
568 go (App f (Type _)) = go f
569 go (App f a) | exprIsCheap a = go f - 1
570 go (Lam x e) | isId x = go e + 1
572 go (Note n e) | ok_note n = go e
573 go (Case scrut _ alts)
574 | exprIsCheap scrut = min_zero [go rhs | (_,_,rhs) <- alts]
576 | all exprIsCheap (rhssOfBind b) = go e
580 ok_note (Coerce _ _) = True
581 ok_note InlineCall = True
582 ok_note other = False
583 -- Notice that we do not look through __inline_me__
584 -- This one is a bit more surprising, but consider
585 -- f = _inline_me (\x -> e)
586 -- We DO NOT want to eta expand this to
587 -- f = \x -> (_inline_me (\x -> e)) x
588 -- because the _inline_me gets dropped now it is applied,
593 min_zero :: [Int] -> Int -- Find the minimum, but zero is the smallest
594 min_zero (x:xs) = go x xs
596 go 0 xs = 0 -- Nothing beats zero
598 go min (x:xs) | x < min = go x xs
599 | otherwise = go min xs
605 etaExpand :: Int -- Add this number of value args
607 -> CoreExpr -> Type -- Expression and its type
610 -- Given e' = etaExpand n us e ty
612 -- ty = exprType e = exprType e'
614 -- etaExpand deals with for-alls and coerces. For example:
616 -- where E :: forall a. T
617 -- newtype T = MkT (A -> B)
620 -- (/\b. coerce T (\y::A -> (coerce (A->B) (E b) y)
622 -- (case x of { I# x -> /\ a -> coerce T E)
624 etaExpand n us expr ty
625 | n == 0 -- Saturated, so nothing to do
628 | otherwise -- An unsaturated constructor or primop; eta expand it
629 = case splitForAllTy_maybe ty of {
630 Just (tv,ty') -> Lam tv (etaExpand n us (App expr (Type (mkTyVarTy tv))) ty')
634 case splitFunTy_maybe ty of {
635 Just (arg_ty, res_ty) -> Lam arg' (etaExpand (n-1) us2 (App expr (Var arg')) res_ty)
637 arg' = mkSysLocal SLIT("eta") uniq arg_ty
638 (us1, us2) = splitUnqiSupply us
639 uniq = uniqFromSupply us1
643 case splitNewType_maybe ty of {
644 Just ty' -> mkCoerce ty ty' (etaExpand n us (mkCoerce ty' ty expr) ty')
646 Nothing -> pprTrace "Bad eta expand" (ppr expr $$ ppr ty) expr
651 %************************************************************************
653 \subsection{Equality}
655 %************************************************************************
657 @cheapEqExpr@ is a cheap equality test which bales out fast!
658 True => definitely equal
659 False => may or may not be equal
662 cheapEqExpr :: Expr b -> Expr b -> Bool
664 cheapEqExpr (Var v1) (Var v2) = v1==v2
665 cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
666 cheapEqExpr (Type t1) (Type t2) = t1 == t2
668 cheapEqExpr (App f1 a1) (App f2 a2)
669 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
671 cheapEqExpr _ _ = False
673 exprIsBig :: Expr b -> Bool
674 -- Returns True of expressions that are too big to be compared by cheapEqExpr
675 exprIsBig (Lit _) = False
676 exprIsBig (Var v) = False
677 exprIsBig (Type t) = False
678 exprIsBig (App f a) = exprIsBig f || exprIsBig a
679 exprIsBig other = True
684 eqExpr :: CoreExpr -> CoreExpr -> Bool
685 -- Works ok at more general type, but only needed at CoreExpr
687 = eq emptyVarEnv e1 e2
689 -- The "env" maps variables in e1 to variables in ty2
690 -- So when comparing lambdas etc,
691 -- we in effect substitute v2 for v1 in e1 before continuing
692 eq env (Var v1) (Var v2) = case lookupVarEnv env v1 of
693 Just v1' -> v1' == v2
696 eq env (Lit lit1) (Lit lit2) = lit1 == lit2
697 eq env (App f1 a1) (App f2 a2) = eq env f1 f2 && eq env a1 a2
698 eq env (Lam v1 e1) (Lam v2 e2) = eq (extendVarEnv env v1 v2) e1 e2
699 eq env (Let (NonRec v1 r1) e1)
700 (Let (NonRec v2 r2) e2) = eq env r1 r2 && eq (extendVarEnv env v1 v2) e1 e2
701 eq env (Let (Rec ps1) e1)
702 (Let (Rec ps2) e2) = length ps1 == length ps2 &&
703 and (zipWith eq_rhs ps1 ps2) &&
706 env' = extendVarEnvList env [(v1,v2) | ((v1,_),(v2,_)) <- zip ps1 ps2]
707 eq_rhs (_,r1) (_,r2) = eq env' r1 r2
708 eq env (Case e1 v1 a1)
709 (Case e2 v2 a2) = eq env e1 e2 &&
710 length a1 == length a2 &&
711 and (zipWith (eq_alt env') a1 a2)
713 env' = extendVarEnv env v1 v2
715 eq env (Note n1 e1) (Note n2 e2) = eq_note env n1 n2 && eq env e1 e2
716 eq env (Type t1) (Type t2) = t1 == t2
719 eq_list env [] [] = True
720 eq_list env (e1:es1) (e2:es2) = eq env e1 e2 && eq_list env es1 es2
721 eq_list env es1 es2 = False
723 eq_alt env (c1,vs1,r1) (c2,vs2,r2) = c1==c2 &&
724 eq (extendVarEnvList env (vs1 `zip` vs2)) r1 r2
726 eq_note env (SCC cc1) (SCC cc2) = cc1 == cc2
727 eq_note env (Coerce t1 f1) (Coerce t2 f2) = t1==t2 && f1==f2
728 eq_note env InlineCall InlineCall = True
729 eq_note env other1 other2 = False
733 %************************************************************************
735 \subsection{The size of an expression}
737 %************************************************************************
740 coreBindsSize :: [CoreBind] -> Int
741 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
743 exprSize :: CoreExpr -> Int
744 -- A measure of the size of the expressions
745 -- It also forces the expression pretty drastically as a side effect
746 exprSize (Var v) = varSize v
747 exprSize (Lit lit) = lit `seq` 1
748 exprSize (App f a) = exprSize f + exprSize a
749 exprSize (Lam b e) = varSize b + exprSize e
750 exprSize (Let b e) = bindSize b + exprSize e
751 exprSize (Case e b as) = exprSize e + varSize b + foldr ((+) . altSize) 0 as
752 exprSize (Note n e) = noteSize n + exprSize e
753 exprSize (Type t) = seqType t `seq` 1
755 noteSize (SCC cc) = cc `seq` 1
756 noteSize (Coerce t1 t2) = seqType t1 `seq` seqType t2 `seq` 1
757 noteSize InlineCall = 1
758 noteSize InlineMe = 1
760 varSize :: Var -> Int
761 varSize b | isTyVar b = 1
762 | otherwise = seqType (idType b) `seq`
763 megaSeqIdInfo (idInfo b) `seq`
766 varsSize = foldr ((+) . varSize) 0
768 bindSize (NonRec b e) = varSize b + exprSize e
769 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
771 pairSize (b,e) = varSize b + exprSize e
773 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
777 %************************************************************************
781 %************************************************************************
784 hashExpr :: CoreExpr -> Int
785 hashExpr e | hash < 0 = 77 -- Just in case we hit -maxInt
788 hash = abs (hash_expr e) -- Negative numbers kill UniqFM
790 hash_expr (Note _ e) = hash_expr e
791 hash_expr (Let (NonRec b r) e) = hashId b
792 hash_expr (Let (Rec ((b,r):_)) e) = hashId b
793 hash_expr (Case _ b _) = hashId b
794 hash_expr (App f e) = hash_expr f * fast_hash_expr e
795 hash_expr (Var v) = hashId v
796 hash_expr (Lit lit) = hashLiteral lit
797 hash_expr (Lam b _) = hashId b
798 hash_expr (Type t) = trace "hash_expr: type" 1 -- Shouldn't happen
800 fast_hash_expr (Var v) = hashId v
801 fast_hash_expr (Lit lit) = hashLiteral lit
802 fast_hash_expr (App f (Type _)) = fast_hash_expr f
803 fast_hash_expr (App f a) = fast_hash_expr a
804 fast_hash_expr (Lam b _) = hashId b
805 fast_hash_expr other = 1
808 hashId id = hashName (idName id)