2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CoreUtils]{Utility functions on @Core@ syntax}
9 mkNote, mkInlineMe, mkSCC, mkCoerce, mkCoerce2,
10 bindNonRec, needsCaseBinding,
11 mkIfThenElse, mkAltExpr, mkPiType, mkPiTypes,
13 -- Taking expressions apart
14 findDefault, findAlt, hasDefault,
16 -- Properties of expressions
17 exprType, coreAltsType,
18 exprIsBottom, exprIsDupable, exprIsTrivial, exprIsCheap,
19 exprIsValue,exprOkForSpeculation, exprIsBig,
20 exprIsConApp_maybe, exprIsAtom,
21 idAppIsBottom, idAppIsCheap,
24 -- Arity and eta expansion
25 manifestArity, exprArity,
26 exprEtaExpandArity, etaExpand,
35 cheapEqExpr, eqExpr, applyTypeToArgs, applyTypeToArg,
38 hasCafRefs, rhsIsNonUpd,
40 -- Cross-DLL references
44 #include "HsVersions.h"
47 import GLAEXTS -- For `xori`
50 import PprCore ( pprCoreExpr )
51 import Var ( Var, isId, isTyVar )
53 import Name ( hashName, isDllName )
54 import Literal ( hashLiteral, literalType, litIsDupable,
55 litIsTrivial, isZeroLit, isLitLitLit )
56 import DataCon ( DataCon, dataConRepArity, dataConArgTys,
57 isExistentialDataCon, dataConTyCon, dataConName )
58 import PrimOp ( PrimOp(..), primOpOkForSpeculation, primOpIsCheap )
59 import Id ( Id, idType, globalIdDetails, idNewStrictness,
60 mkWildId, idArity, idName, idUnfolding, idInfo,
61 isOneShotLambda, isDataConWorkId_maybe, mkSysLocal,
62 isDataConWorkId, isBottomingId, idCafInfo
64 import IdInfo ( GlobalIdDetails(..), megaSeqIdInfo,
65 CafInfo(..), mayHaveCafRefs )
66 import NewDemand ( appIsBottom )
67 import Type ( Type, mkFunTy, mkForAllTy, splitFunTy_maybe,
69 applyTys, isUnLiftedType, seqType, mkTyVarTy,
70 splitForAllTy_maybe, isForAllTy, splitNewType_maybe,
71 splitTyConApp_maybe, eqType, funResultTy, applyTy,
74 import TyCon ( tyConArity )
75 import TysWiredIn ( boolTy, trueDataCon, falseDataCon )
76 import CostCentre ( CostCentre )
77 import BasicTypes ( Arity )
78 import Unique ( Unique )
80 import TysPrim ( alphaTy ) -- Debugging only
81 import Util ( equalLength, lengthAtLeast )
82 import TysPrim ( statePrimTyCon )
83 import FastTypes hiding ( fastOr )
87 %************************************************************************
89 \subsection{Find the type of a Core atom/expression}
91 %************************************************************************
94 exprType :: CoreExpr -> Type
96 exprType (Var var) = idType var
97 exprType (Lit lit) = literalType lit
98 exprType (Let _ body) = exprType body
99 exprType (Case _ _ alts) = coreAltsType alts
100 exprType (Note (Coerce ty _) e) = ty -- **! should take usage from e
101 exprType (Note other_note e) = exprType e
102 exprType (Lam binder expr) = mkPiType binder (exprType expr)
104 = case collectArgs e of
105 (fun, args) -> applyTypeToArgs e (exprType fun) args
107 exprType other = pprTrace "exprType" (pprCoreExpr other) alphaTy
109 coreAltsType :: [CoreAlt] -> Type
110 coreAltsType ((_,_,rhs) : _) = exprType rhs
113 @mkPiType@ makes a (->) type or a forall type, depending on whether
114 it is given a type variable or a term variable. We cleverly use the
115 lbvarinfo field to figure out the right annotation for the arrove in
116 case of a term variable.
119 mkPiType :: Var -> Type -> Type -- The more polymorphic version
120 mkPiTypes :: [Var] -> Type -> Type -- doesn't work...
122 mkPiTypes vs ty = foldr mkPiType ty vs
125 | isId v = mkFunTy (idType v) ty
126 | otherwise = mkForAllTy v ty
130 applyTypeToArg :: Type -> CoreExpr -> Type
131 applyTypeToArg fun_ty (Type arg_ty) = applyTy fun_ty arg_ty
132 applyTypeToArg fun_ty other_arg = funResultTy fun_ty
134 applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> Type
135 -- A more efficient version of applyTypeToArg
136 -- when we have several args
137 -- The first argument is just for debugging
138 applyTypeToArgs e op_ty [] = op_ty
140 applyTypeToArgs e op_ty (Type ty : args)
141 = -- Accumulate type arguments so we can instantiate all at once
144 go rev_tys (Type ty : args) = go (ty:rev_tys) args
145 go rev_tys rest_args = applyTypeToArgs e op_ty' rest_args
147 op_ty' = applyTys op_ty (reverse rev_tys)
149 applyTypeToArgs e op_ty (other_arg : args)
150 = case (splitFunTy_maybe op_ty) of
151 Just (_, res_ty) -> applyTypeToArgs e res_ty args
152 Nothing -> pprPanic "applyTypeToArgs" (pprCoreExpr e)
157 %************************************************************************
159 \subsection{Attaching notes}
161 %************************************************************************
163 mkNote removes redundant coercions, and SCCs where possible
166 mkNote :: Note -> CoreExpr -> CoreExpr
167 mkNote (Coerce to_ty from_ty) expr = mkCoerce2 to_ty from_ty expr
168 mkNote (SCC cc) expr = mkSCC cc expr
169 mkNote InlineMe expr = mkInlineMe expr
170 mkNote note expr = Note note expr
172 -- Slide InlineCall in around the function
173 -- No longer necessary I think (SLPJ Apr 99)
174 -- mkNote InlineCall (App f a) = App (mkNote InlineCall f) a
175 -- mkNote InlineCall (Var v) = Note InlineCall (Var v)
176 -- mkNote InlineCall expr = expr
179 Drop trivial InlineMe's. This is somewhat important, because if we have an unfolding
180 that looks like (Note InlineMe (Var v)), the InlineMe doesn't go away because it may
181 not be *applied* to anything.
183 We don't use exprIsTrivial here, though, because we sometimes generate worker/wrapper
186 f = inline_me (coerce t fw)
187 As usual, the inline_me prevents the worker from getting inlined back into the wrapper.
188 We want the split, so that the coerces can cancel at the call site.
190 However, we can get left with tiresome type applications. Notably, consider
191 f = /\ a -> let t = e in (t, w)
192 Then lifting the let out of the big lambda gives
194 f = /\ a -> let t = inline_me (t' a) in (t, w)
195 The inline_me is to stop the simplifier inlining t' right back
196 into t's RHS. In the next phase we'll substitute for t (since
197 its rhs is trivial) and *then* we could get rid of the inline_me.
198 But it hardly seems worth it, so I don't bother.
201 mkInlineMe (Var v) = Var v
202 mkInlineMe e = Note InlineMe e
208 mkCoerce :: Type -> CoreExpr -> CoreExpr
209 mkCoerce to_ty expr = mkCoerce2 to_ty (exprType expr) expr
211 mkCoerce2 :: Type -> Type -> CoreExpr -> CoreExpr
212 mkCoerce2 to_ty from_ty (Note (Coerce to_ty2 from_ty2) expr)
213 = ASSERT( from_ty `eqType` to_ty2 )
214 mkCoerce2 to_ty from_ty2 expr
216 mkCoerce2 to_ty from_ty expr
217 | to_ty `eqType` from_ty = expr
218 | otherwise = ASSERT( from_ty `eqType` exprType expr )
219 Note (Coerce to_ty from_ty) expr
223 mkSCC :: CostCentre -> Expr b -> Expr b
224 -- Note: Nested SCC's *are* preserved for the benefit of
225 -- cost centre stack profiling
226 mkSCC cc (Lit lit) = Lit lit
227 mkSCC cc (Lam x e) = Lam x (mkSCC cc e) -- Move _scc_ inside lambda
228 mkSCC cc (Note (SCC cc') e) = Note (SCC cc) (Note (SCC cc') e)
229 mkSCC cc (Note n e) = Note n (mkSCC cc e) -- Move _scc_ inside notes
230 mkSCC cc expr = Note (SCC cc) expr
234 %************************************************************************
236 \subsection{Other expression construction}
238 %************************************************************************
241 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
242 -- (bindNonRec x r b) produces either
245 -- case r of x { _DEFAULT_ -> b }
247 -- depending on whether x is unlifted or not
248 -- It's used by the desugarer to avoid building bindings
249 -- that give Core Lint a heart attack. Actually the simplifier
250 -- deals with them perfectly well.
251 bindNonRec bndr rhs body
252 | needsCaseBinding (idType bndr) rhs = Case rhs bndr [(DEFAULT,[],body)]
253 | otherwise = Let (NonRec bndr rhs) body
255 needsCaseBinding ty rhs = isUnLiftedType ty && not (exprOkForSpeculation rhs)
256 -- Make a case expression instead of a let
257 -- These can arise either from the desugarer,
258 -- or from beta reductions: (\x.e) (x +# y)
262 mkAltExpr :: AltCon -> [CoreBndr] -> [Type] -> CoreExpr
263 -- This guy constructs the value that the scrutinee must have
264 -- when you are in one particular branch of a case
265 mkAltExpr (DataAlt con) args inst_tys
266 = mkConApp con (map Type inst_tys ++ map varToCoreExpr args)
267 mkAltExpr (LitAlt lit) [] []
270 mkIfThenElse :: CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
271 mkIfThenElse guard then_expr else_expr
272 = Case guard (mkWildId boolTy)
273 [ (DataAlt trueDataCon, [], then_expr),
274 (DataAlt falseDataCon, [], else_expr) ]
278 %************************************************************************
280 \subsection{Taking expressions apart}
282 %************************************************************************
284 The default alternative must be first, if it exists at all.
285 This makes it easy to find, though it makes matching marginally harder.
288 hasDefault :: [CoreAlt] -> Bool
289 hasDefault ((DEFAULT,_,_) : alts) = True
292 findDefault :: [CoreAlt] -> ([CoreAlt], Maybe CoreExpr)
293 findDefault ((DEFAULT,args,rhs) : alts) = ASSERT( null args ) (alts, Just rhs)
294 findDefault alts = (alts, Nothing)
296 findAlt :: AltCon -> [CoreAlt] -> CoreAlt
299 (deflt@(DEFAULT,_,_):alts) -> go alts deflt
300 other -> go alts panic_deflt
303 panic_deflt = pprPanic "Missing alternative" (ppr con $$ vcat (map ppr alts))
306 go (alt@(con1,_,_) : alts) deflt | con == con1 = alt
307 | otherwise = ASSERT( not (con1 == DEFAULT) )
312 %************************************************************************
314 \subsection{Figuring out things about expressions}
316 %************************************************************************
318 @exprIsTrivial@ is true of expressions we are unconditionally happy to
319 duplicate; simple variables and constants, and type
320 applications. Note that primop Ids aren't considered
323 @exprIsBottom@ is true of expressions that are guaranteed to diverge
326 There used to be a gruesome test for (hasNoBinding v) in the
328 exprIsTrivial (Var v) | hasNoBinding v = idArity v == 0
329 The idea here is that a constructor worker, like $wJust, is
330 really short for (\x -> $wJust x), becuase $wJust has no binding.
331 So it should be treated like a lambda. Ditto unsaturated primops.
332 But now constructor workers are not "have-no-binding" Ids. And
333 completely un-applied primops and foreign-call Ids are sufficiently
334 rare that I plan to allow them to be duplicated and put up with
338 exprIsTrivial (Var v) = True -- See notes above
339 exprIsTrivial (Type _) = True
340 exprIsTrivial (Lit lit) = litIsTrivial lit
341 exprIsTrivial (App e arg) = not (isRuntimeArg arg) && exprIsTrivial e
342 exprIsTrivial (Note _ e) = exprIsTrivial e
343 exprIsTrivial (Lam b body) = not (isRuntimeVar b) && exprIsTrivial body
344 exprIsTrivial other = False
346 exprIsAtom :: CoreExpr -> Bool
347 -- Used to decide whether to let-binding an STG argument
348 -- when compiling to ILX => type applications are not allowed
349 exprIsAtom (Var v) = True -- primOpIsDupable?
350 exprIsAtom (Lit lit) = True
351 exprIsAtom (Type ty) = True
352 exprIsAtom (Note (SCC _) e) = False
353 exprIsAtom (Note _ e) = exprIsAtom e
354 exprIsAtom other = False
358 @exprIsDupable@ is true of expressions that can be duplicated at a modest
359 cost in code size. This will only happen in different case
360 branches, so there's no issue about duplicating work.
362 That is, exprIsDupable returns True of (f x) even if
363 f is very very expensive to call.
365 Its only purpose is to avoid fruitless let-binding
366 and then inlining of case join points
370 exprIsDupable (Type _) = True
371 exprIsDupable (Var v) = True
372 exprIsDupable (Lit lit) = litIsDupable lit
373 exprIsDupable (Note InlineMe e) = True
374 exprIsDupable (Note _ e) = exprIsDupable e
378 go (Var v) n_args = True
379 go (App f a) n_args = n_args < dupAppSize
382 go other n_args = False
385 dupAppSize = 4 -- Size of application we are prepared to duplicate
388 @exprIsCheap@ looks at a Core expression and returns \tr{True} if
389 it is obviously in weak head normal form, or is cheap to get to WHNF.
390 [Note that that's not the same as exprIsDupable; an expression might be
391 big, and hence not dupable, but still cheap.]
393 By ``cheap'' we mean a computation we're willing to:
394 push inside a lambda, or
395 inline at more than one place
396 That might mean it gets evaluated more than once, instead of being
397 shared. The main examples of things which aren't WHNF but are
402 (where e, and all the ei are cheap)
405 (where e and b are cheap)
408 (where op is a cheap primitive operator)
411 (because we are happy to substitute it inside a lambda)
413 Notice that a variable is considered 'cheap': we can push it inside a lambda,
414 because sharing will make sure it is only evaluated once.
417 exprIsCheap :: CoreExpr -> Bool
418 exprIsCheap (Lit lit) = True
419 exprIsCheap (Type _) = True
420 exprIsCheap (Var _) = True
421 exprIsCheap (Note InlineMe e) = True
422 exprIsCheap (Note _ e) = exprIsCheap e
423 exprIsCheap (Lam x e) = isRuntimeVar x || exprIsCheap e
424 exprIsCheap (Case e _ alts) = exprIsCheap e &&
425 and [exprIsCheap rhs | (_,_,rhs) <- alts]
426 -- Experimentally, treat (case x of ...) as cheap
427 -- (and case __coerce x etc.)
428 -- This improves arities of overloaded functions where
429 -- there is only dictionary selection (no construction) involved
430 exprIsCheap (Let (NonRec x _) e)
431 | isUnLiftedType (idType x) = exprIsCheap e
433 -- strict lets always have cheap right hand sides, and
436 exprIsCheap other_expr
437 = go other_expr 0 True
439 go (Var f) n_args args_cheap
440 = (idAppIsCheap f n_args && args_cheap)
441 -- A constructor, cheap primop, or partial application
443 || idAppIsBottom f n_args
444 -- Application of a function which
445 -- always gives bottom; we treat this as cheap
446 -- because it certainly doesn't need to be shared!
448 go (App f a) n_args args_cheap
449 | not (isRuntimeArg a) = go f n_args args_cheap
450 | otherwise = go f (n_args + 1) (exprIsCheap a && args_cheap)
452 go other n_args args_cheap = False
454 idAppIsCheap :: Id -> Int -> Bool
455 idAppIsCheap id n_val_args
456 | n_val_args == 0 = True -- Just a type application of
457 -- a variable (f t1 t2 t3)
459 | otherwise = case globalIdDetails id of
460 DataConWorkId _ -> True
461 RecordSelId _ -> True -- I'm experimenting with making record selection
462 ClassOpId _ -> True -- look cheap, so we will substitute it inside a
463 -- lambda. Particularly for dictionary field selection
465 PrimOpId op -> primOpIsCheap op -- In principle we should worry about primops
466 -- that return a type variable, since the result
467 -- might be applied to something, but I'm not going
468 -- to bother to check the number of args
469 other -> n_val_args < idArity id
472 exprOkForSpeculation returns True of an expression that it is
474 * safe to evaluate even if normal order eval might not
475 evaluate the expression at all, or
477 * safe *not* to evaluate even if normal order would do so
481 the expression guarantees to terminate,
483 without raising an exception,
484 without causing a side effect (e.g. writing a mutable variable)
487 let x = case y# +# 1# of { r# -> I# r# }
490 case y# +# 1# of { r# ->
495 We can only do this if the (y+1) is ok for speculation: it has no
496 side effects, and can't diverge or raise an exception.
499 exprOkForSpeculation :: CoreExpr -> Bool
500 exprOkForSpeculation (Lit _) = True
501 exprOkForSpeculation (Type _) = True
502 exprOkForSpeculation (Var v) = isUnLiftedType (idType v)
503 exprOkForSpeculation (Note _ e) = exprOkForSpeculation e
504 exprOkForSpeculation other_expr
505 = case collectArgs other_expr of
506 (Var f, args) -> spec_ok (globalIdDetails f) args
510 spec_ok (DataConWorkId _) args
511 = True -- The strictness of the constructor has already
512 -- been expressed by its "wrapper", so we don't need
513 -- to take the arguments into account
515 spec_ok (PrimOpId op) args
516 | isDivOp op, -- Special case for dividing operations that fail
517 [arg1, Lit lit] <- args -- only if the divisor is zero
518 = not (isZeroLit lit) && exprOkForSpeculation arg1
519 -- Often there is a literal divisor, and this
520 -- can get rid of a thunk in an inner looop
523 = primOpOkForSpeculation op &&
524 all exprOkForSpeculation args
525 -- A bit conservative: we don't really need
526 -- to care about lazy arguments, but this is easy
528 spec_ok other args = False
530 isDivOp :: PrimOp -> Bool
531 -- True of dyadic operators that can fail
532 -- only if the second arg is zero
533 -- This function probably belongs in PrimOp, or even in
534 -- an automagically generated file.. but it's such a
535 -- special case I thought I'd leave it here for now.
536 isDivOp IntQuotOp = True
537 isDivOp IntRemOp = True
538 isDivOp WordQuotOp = True
539 isDivOp WordRemOp = True
540 isDivOp IntegerQuotRemOp = True
541 isDivOp IntegerDivModOp = True
542 isDivOp FloatDivOp = True
543 isDivOp DoubleDivOp = True
544 isDivOp other = False
549 exprIsBottom :: CoreExpr -> Bool -- True => definitely bottom
550 exprIsBottom e = go 0 e
552 -- n is the number of args
553 go n (Note _ e) = go n e
554 go n (Let _ e) = go n e
555 go n (Case e _ _) = go 0 e -- Just check the scrut
556 go n (App e _) = go (n+1) e
557 go n (Var v) = idAppIsBottom v n
559 go n (Lam _ _) = False
561 idAppIsBottom :: Id -> Int -> Bool
562 idAppIsBottom id n_val_args = appIsBottom (idNewStrictness id) n_val_args
565 @exprIsValue@ returns true for expressions that are certainly *already*
566 evaluated to *head* normal form. This is used to decide whether it's ok
569 case x of _ -> e ===> e
571 and to decide whether it's safe to discard a `seq`
573 So, it does *not* treat variables as evaluated, unless they say they are.
575 But it *does* treat partial applications and constructor applications
576 as values, even if their arguments are non-trivial, provided the argument
578 e.g. (:) (f x) (map f xs) is a value
579 map (...redex...) is a value
580 Because `seq` on such things completes immediately
582 For unlifted argument types, we have to be careful:
584 Suppose (f x) diverges; then C (f x) is not a value. True, but
585 this form is illegal (see the invariants in CoreSyn). Args of unboxed
586 type must be ok-for-speculation (or trivial).
589 exprIsValue :: CoreExpr -> Bool -- True => Value-lambda, constructor, PAP
590 exprIsValue (Var v) -- NB: There are no value args at this point
591 = isDataConWorkId v -- Catches nullary constructors,
592 -- so that [] and () are values, for example
593 || idArity v > 0 -- Catches (e.g.) primops that don't have unfoldings
594 || isEvaldUnfolding (idUnfolding v)
595 -- Check the thing's unfolding; it might be bound to a value
596 -- A worry: what if an Id's unfolding is just itself:
597 -- then we could get an infinite loop...
599 exprIsValue (Lit l) = True
600 exprIsValue (Type ty) = True -- Types are honorary Values;
601 -- we don't mind copying them
602 exprIsValue (Lam b e) = isRuntimeVar b || exprIsValue e
603 exprIsValue (Note _ e) = exprIsValue e
604 exprIsValue (App e (Type _)) = exprIsValue e
605 exprIsValue (App e a) = app_is_value e [a]
606 exprIsValue other = False
608 -- There is at least one value argument
609 app_is_value (Var fun) args
610 | isDataConWorkId fun -- Constructor apps are values
611 || idArity fun > valArgCount args -- Under-applied function
612 = check_args (idType fun) args
613 app_is_value (App f a) as = app_is_value f (a:as)
614 app_is_value other as = False
616 -- 'check_args' checks that unlifted-type args
617 -- are in fact guaranteed non-divergent
618 check_args fun_ty [] = True
619 check_args fun_ty (Type _ : args) = case splitForAllTy_maybe fun_ty of
620 Just (_, ty) -> check_args ty args
621 check_args fun_ty (arg : args)
622 | isUnLiftedType arg_ty = exprOkForSpeculation arg
623 | otherwise = check_args res_ty args
625 (arg_ty, res_ty) = splitFunTy fun_ty
629 exprIsConApp_maybe :: CoreExpr -> Maybe (DataCon, [CoreExpr])
630 exprIsConApp_maybe (Note (Coerce to_ty from_ty) expr)
631 = -- Maybe this is over the top, but here we try to turn
632 -- coerce (S,T) ( x, y )
634 -- ( coerce S x, coerce T y )
635 -- This happens in anger in PrelArrExts which has a coerce
636 -- case coerce memcpy a b of
638 -- where the memcpy is in the IO monad, but the call is in
640 case exprIsConApp_maybe expr of {
644 case splitTyConApp_maybe to_ty of {
646 Just (tc, tc_arg_tys) | tc /= dataConTyCon dc -> Nothing
647 | isExistentialDataCon dc -> Nothing
649 -- Type constructor must match
650 -- We knock out existentials to keep matters simple(r)
652 arity = tyConArity tc
653 val_args = drop arity args
654 to_arg_tys = dataConArgTys dc tc_arg_tys
655 mk_coerce ty arg = mkCoerce ty arg
656 new_val_args = zipWith mk_coerce to_arg_tys val_args
658 ASSERT( all isTypeArg (take arity args) )
659 ASSERT( equalLength val_args to_arg_tys )
660 Just (dc, map Type tc_arg_tys ++ new_val_args)
663 exprIsConApp_maybe (Note _ expr)
664 = exprIsConApp_maybe expr
665 -- We ignore InlineMe notes in case we have
666 -- x = __inline_me__ (a,b)
667 -- All part of making sure that INLINE pragmas never hurt
668 -- Marcin tripped on this one when making dictionaries more inlinable
670 -- In fact, we ignore all notes. For example,
671 -- case _scc_ "foo" (C a b) of
673 -- should be optimised away, but it will be only if we look
674 -- through the SCC note.
676 exprIsConApp_maybe expr = analyse (collectArgs expr)
678 analyse (Var fun, args)
679 | Just con <- isDataConWorkId_maybe fun,
680 args `lengthAtLeast` dataConRepArity con
681 -- Might be > because the arity excludes type args
684 -- Look through unfoldings, but only cheap ones, because
685 -- we are effectively duplicating the unfolding
686 analyse (Var fun, [])
687 | let unf = idUnfolding fun,
689 = exprIsConApp_maybe (unfoldingTemplate unf)
691 analyse other = Nothing
696 %************************************************************************
698 \subsection{Eta reduction and expansion}
700 %************************************************************************
703 exprEtaExpandArity :: CoreExpr -> Arity
704 -- The Int is number of value args the thing can be
705 -- applied to without doing much work
707 -- This is used when eta expanding
708 -- e ==> \xy -> e x y
710 -- It returns 1 (or more) to:
711 -- case x of p -> \s -> ...
712 -- because for I/O ish things we really want to get that \s to the top.
713 -- We are prepared to evaluate x each time round the loop in order to get that
715 -- It's all a bit more subtle than it looks. Consider one-shot lambdas
716 -- let x = expensive in \y z -> E
717 -- We want this to have arity 2 if the \y-abstraction is a 1-shot lambda
718 -- Hence the ArityType returned by arityType
720 -- NB: this is particularly important/useful for IO state
721 -- transformers, where we often get
722 -- let x = E in \ s -> ...
723 -- and the \s is a real-world state token abstraction. Such
724 -- abstractions are almost invariably 1-shot, so we want to
725 -- pull the \s out, past the let x=E.
726 -- The hack is in Id.isOneShotLambda
729 -- f = \x -> error "foo"
730 -- Here, arity 1 is fine. But if it is
731 -- f = \x -> case e of
732 -- True -> error "foo"
733 -- False -> \y -> x+y
734 -- then we want to get arity 2.
735 -- Hence the ABot/ATop in ArityType
738 exprEtaExpandArity e = arityDepth (arityType e)
740 -- A limited sort of function type
741 data ArityType = AFun Bool ArityType -- True <=> one-shot
742 | ATop -- Know nothing
745 arityDepth :: ArityType -> Arity
746 arityDepth (AFun _ ty) = 1 + arityDepth ty
749 andArityType ABot at2 = at2
750 andArityType ATop at2 = ATop
751 andArityType (AFun t1 at1) (AFun t2 at2) = AFun (t1 && t2) (andArityType at1 at2)
752 andArityType at1 at2 = andArityType at2 at1
754 arityType :: CoreExpr -> ArityType
755 -- (go1 e) = [b1,..,bn]
756 -- means expression can be rewritten \x_b1 -> ... \x_bn -> body
757 -- where bi is True <=> the lambda is one-shot
759 arityType (Note n e) = arityType e
760 -- Not needed any more: etaExpand is cleverer
761 -- | ok_note n = arityType e
762 -- | otherwise = ATop
767 mk :: Arity -> ArityType
768 mk 0 | isBottomingId v = ABot
770 mk n = AFun False (mk (n-1))
772 -- When the type of the Id encodes one-shot-ness,
773 -- use the idinfo here
775 -- Lambdas; increase arity
776 arityType (Lam x e) | isId x = AFun (isOneShotLambda x || isStateHack x) (arityType e)
777 | otherwise = arityType e
779 -- Applications; decrease arity
780 arityType (App f (Type _)) = arityType f
781 arityType (App f a) = case arityType f of
782 AFun one_shot xs | exprIsCheap a -> xs
785 -- Case/Let; keep arity if either the expression is cheap
786 -- or it's a 1-shot lambda
787 arityType (Case scrut _ alts) = case foldr1 andArityType [arityType rhs | (_,_,rhs) <- alts] of
788 xs@(AFun one_shot _) | one_shot -> xs
789 xs | exprIsCheap scrut -> xs
792 arityType (Let b e) = case arityType e of
793 xs@(AFun one_shot _) | one_shot -> xs
794 xs | all exprIsCheap (rhssOfBind b) -> xs
797 arityType other = ATop
799 isStateHack id = case splitTyConApp_maybe (idType id) of
800 Just (tycon,_) | tycon == statePrimTyCon -> True
803 -- The last clause is a gross hack. It claims that
804 -- every function over realWorldStatePrimTy is a one-shot
805 -- function. This is pretty true in practice, and makes a big
806 -- difference. For example, consider
807 -- a `thenST` \ r -> ...E...
808 -- The early full laziness pass, if it doesn't know that r is one-shot
809 -- will pull out E (let's say it doesn't mention r) to give
810 -- let lvl = E in a `thenST` \ r -> ...lvl...
811 -- When `thenST` gets inlined, we end up with
812 -- let lvl = E in \s -> case a s of (r, s') -> ...lvl...
813 -- and we don't re-inline E.
815 -- It would be better to spot that r was one-shot to start with, but
816 -- I don't want to rely on that.
818 -- Another good example is in fill_in in PrelPack.lhs. We should be able to
819 -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
821 {- NOT NEEDED ANY MORE: etaExpand is cleverer
822 ok_note InlineMe = False
824 -- Notice that we do not look through __inline_me__
825 -- This may seem surprising, but consider
826 -- f = _inline_me (\x -> e)
827 -- We DO NOT want to eta expand this to
828 -- f = \x -> (_inline_me (\x -> e)) x
829 -- because the _inline_me gets dropped now it is applied,
838 etaExpand :: Arity -- Result should have this number of value args
840 -> CoreExpr -> Type -- Expression and its type
842 -- (etaExpand n us e ty) returns an expression with
843 -- the same meaning as 'e', but with arity 'n'.
845 -- Given e' = etaExpand n us e ty
847 -- ty = exprType e = exprType e'
849 -- Note that SCCs are not treated specially. If we have
850 -- etaExpand 2 (\x -> scc "foo" e)
851 -- = (\xy -> (scc "foo" e) y)
852 -- So the costs of evaluating 'e' (not 'e y') are attributed to "foo"
854 etaExpand n us expr ty
855 | manifestArity expr >= n = expr -- The no-op case
856 | otherwise = eta_expand n us expr ty
859 -- manifestArity sees how many leading value lambdas there are
860 manifestArity :: CoreExpr -> Arity
861 manifestArity (Lam v e) | isId v = 1 + manifestArity e
862 | otherwise = manifestArity e
863 manifestArity (Note _ e) = manifestArity e
866 -- etaExpand deals with for-alls. For example:
868 -- where E :: forall a. a -> a
870 -- (/\b. \y::a -> E b y)
872 -- It deals with coerces too, though they are now rare
873 -- so perhaps the extra code isn't worth it
875 eta_expand n us expr ty
877 -- The ILX code generator requires eta expansion for type arguments
878 -- too, but alas the 'n' doesn't tell us how many of them there
879 -- may be. So we eagerly eta expand any big lambdas, and just
880 -- cross our fingers about possible loss of sharing in the ILX case.
881 -- The Right Thing is probably to make 'arity' include
882 -- type variables throughout the compiler. (ToDo.)
884 -- Saturated, so nothing to do
887 -- Short cut for the case where there already
888 -- is a lambda; no point in gratuitously adding more
889 eta_expand n us (Lam v body) ty
891 = Lam v (eta_expand n us body (applyTy ty (mkTyVarTy v)))
894 = Lam v (eta_expand (n-1) us body (funResultTy ty))
896 -- We used to have a special case that stepped inside Coerces here,
897 -- thus: eta_expand n us (Note note@(Coerce _ ty) e) _
898 -- = Note note (eta_expand n us e ty)
899 -- BUT this led to an infinite loop
900 -- Example: newtype T = MkT (Int -> Int)
901 -- eta_expand 1 (coerce (Int->Int) e)
902 -- --> coerce (Int->Int) (eta_expand 1 T e)
904 -- --> coerce (Int->Int) (coerce T
905 -- (\x::Int -> eta_expand 1 (coerce (Int->Int) e)))
906 -- by the splitNewType_maybe case below
909 eta_expand n us expr ty
910 = case splitForAllTy_maybe ty of {
911 Just (tv,ty') -> Lam tv (eta_expand n us (App expr (Type (mkTyVarTy tv))) ty')
915 case splitFunTy_maybe ty of {
916 Just (arg_ty, res_ty) -> Lam arg1 (eta_expand (n-1) us2 (App expr (Var arg1)) res_ty)
918 arg1 = mkSysLocal FSLIT("eta") uniq arg_ty
924 -- newtype T = MkT (Int -> Int)
925 -- Consider eta-expanding this
928 -- coerce T (\x::Int -> (coerce (Int->Int) e) x)
930 case splitNewType_maybe ty of {
931 Just ty' -> mkCoerce2 ty ty' (eta_expand n us (mkCoerce2 ty' ty expr) ty') ;
932 Nothing -> pprTrace "Bad eta expand" (ppr expr $$ ppr ty) expr
936 exprArity is a cheap-and-cheerful version of exprEtaExpandArity.
937 It tells how many things the expression can be applied to before doing
938 any work. It doesn't look inside cases, lets, etc. The idea is that
939 exprEtaExpandArity will do the hard work, leaving something that's easy
940 for exprArity to grapple with. In particular, Simplify uses exprArity to
941 compute the ArityInfo for the Id.
943 Originally I thought that it was enough just to look for top-level lambdas, but
944 it isn't. I've seen this
946 foo = PrelBase.timesInt
948 We want foo to get arity 2 even though the eta-expander will leave it
949 unchanged, in the expectation that it'll be inlined. But occasionally it
950 isn't, because foo is blacklisted (used in a rule).
952 Similarly, see the ok_note check in exprEtaExpandArity. So
953 f = __inline_me (\x -> e)
954 won't be eta-expanded.
956 And in any case it seems more robust to have exprArity be a bit more intelligent.
957 But note that (\x y z -> f x y z)
958 should have arity 3, regardless of f's arity.
961 exprArity :: CoreExpr -> Arity
964 go (Var v) = idArity v
965 go (Lam x e) | isId x = go e + 1
968 go (App e (Type t)) = go e
969 go (App f a) | exprIsCheap a = (go f - 1) `max` 0
970 -- NB: exprIsCheap a!
971 -- f (fac x) does not have arity 2,
972 -- even if f has arity 3!
973 -- NB: `max 0`! (\x y -> f x) has arity 2, even if f is
974 -- unknown, hence arity 0
978 %************************************************************************
980 \subsection{Equality}
982 %************************************************************************
984 @cheapEqExpr@ is a cheap equality test which bales out fast!
985 True => definitely equal
986 False => may or may not be equal
989 cheapEqExpr :: Expr b -> Expr b -> Bool
991 cheapEqExpr (Var v1) (Var v2) = v1==v2
992 cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
993 cheapEqExpr (Type t1) (Type t2) = t1 `eqType` t2
995 cheapEqExpr (App f1 a1) (App f2 a2)
996 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
998 cheapEqExpr _ _ = False
1000 exprIsBig :: Expr b -> Bool
1001 -- Returns True of expressions that are too big to be compared by cheapEqExpr
1002 exprIsBig (Lit _) = False
1003 exprIsBig (Var v) = False
1004 exprIsBig (Type t) = False
1005 exprIsBig (App f a) = exprIsBig f || exprIsBig a
1006 exprIsBig other = True
1011 eqExpr :: CoreExpr -> CoreExpr -> Bool
1012 -- Works ok at more general type, but only needed at CoreExpr
1013 -- Used in rule matching, so when we find a type we use
1014 -- eqTcType, which doesn't look through newtypes
1015 -- [And it doesn't risk falling into a black hole either.]
1017 = eq emptyVarEnv e1 e2
1019 -- The "env" maps variables in e1 to variables in ty2
1020 -- So when comparing lambdas etc,
1021 -- we in effect substitute v2 for v1 in e1 before continuing
1022 eq env (Var v1) (Var v2) = case lookupVarEnv env v1 of
1023 Just v1' -> v1' == v2
1026 eq env (Lit lit1) (Lit lit2) = lit1 == lit2
1027 eq env (App f1 a1) (App f2 a2) = eq env f1 f2 && eq env a1 a2
1028 eq env (Lam v1 e1) (Lam v2 e2) = eq (extendVarEnv env v1 v2) e1 e2
1029 eq env (Let (NonRec v1 r1) e1)
1030 (Let (NonRec v2 r2) e2) = eq env r1 r2 && eq (extendVarEnv env v1 v2) e1 e2
1031 eq env (Let (Rec ps1) e1)
1032 (Let (Rec ps2) e2) = equalLength ps1 ps2 &&
1033 and (zipWith eq_rhs ps1 ps2) &&
1036 env' = extendVarEnvList env [(v1,v2) | ((v1,_),(v2,_)) <- zip ps1 ps2]
1037 eq_rhs (_,r1) (_,r2) = eq env' r1 r2
1038 eq env (Case e1 v1 a1)
1039 (Case e2 v2 a2) = eq env e1 e2 &&
1040 equalLength a1 a2 &&
1041 and (zipWith (eq_alt env') a1 a2)
1043 env' = extendVarEnv env v1 v2
1045 eq env (Note n1 e1) (Note n2 e2) = eq_note env n1 n2 && eq env e1 e2
1046 eq env (Type t1) (Type t2) = t1 `eqType` t2
1047 eq env e1 e2 = False
1049 eq_list env [] [] = True
1050 eq_list env (e1:es1) (e2:es2) = eq env e1 e2 && eq_list env es1 es2
1051 eq_list env es1 es2 = False
1053 eq_alt env (c1,vs1,r1) (c2,vs2,r2) = c1==c2 &&
1054 eq (extendVarEnvList env (vs1 `zip` vs2)) r1 r2
1056 eq_note env (SCC cc1) (SCC cc2) = cc1 == cc2
1057 eq_note env (Coerce t1 f1) (Coerce t2 f2) = t1 `eqType` t2 && f1 `eqType` f2
1058 eq_note env InlineCall InlineCall = True
1059 eq_note env (CoreNote s1) (CoreNote s2) = s1 == s2
1060 eq_note env other1 other2 = False
1064 %************************************************************************
1066 \subsection{The size of an expression}
1068 %************************************************************************
1071 coreBindsSize :: [CoreBind] -> Int
1072 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
1074 exprSize :: CoreExpr -> Int
1075 -- A measure of the size of the expressions
1076 -- It also forces the expression pretty drastically as a side effect
1077 exprSize (Var v) = v `seq` 1
1078 exprSize (Lit lit) = lit `seq` 1
1079 exprSize (App f a) = exprSize f + exprSize a
1080 exprSize (Lam b e) = varSize b + exprSize e
1081 exprSize (Let b e) = bindSize b + exprSize e
1082 exprSize (Case e b as) = exprSize e + varSize b + foldr ((+) . altSize) 0 as
1083 exprSize (Note n e) = noteSize n + exprSize e
1084 exprSize (Type t) = seqType t `seq` 1
1086 noteSize (SCC cc) = cc `seq` 1
1087 noteSize (Coerce t1 t2) = seqType t1 `seq` seqType t2 `seq` 1
1088 noteSize InlineCall = 1
1089 noteSize InlineMe = 1
1090 noteSize (CoreNote s) = s `seq` 1 -- hdaume: core annotations
1092 varSize :: Var -> Int
1093 varSize b | isTyVar b = 1
1094 | otherwise = seqType (idType b) `seq`
1095 megaSeqIdInfo (idInfo b) `seq`
1098 varsSize = foldr ((+) . varSize) 0
1100 bindSize (NonRec b e) = varSize b + exprSize e
1101 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
1103 pairSize (b,e) = varSize b + exprSize e
1105 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
1109 %************************************************************************
1111 \subsection{Hashing}
1113 %************************************************************************
1116 hashExpr :: CoreExpr -> Int
1117 hashExpr e | hash < 0 = 77 -- Just in case we hit -maxInt
1120 hash = abs (hash_expr e) -- Negative numbers kill UniqFM
1122 hash_expr (Note _ e) = hash_expr e
1123 hash_expr (Let (NonRec b r) e) = hashId b
1124 hash_expr (Let (Rec ((b,r):_)) e) = hashId b
1125 hash_expr (Case _ b _) = hashId b
1126 hash_expr (App f e) = hash_expr f * fast_hash_expr e
1127 hash_expr (Var v) = hashId v
1128 hash_expr (Lit lit) = hashLiteral lit
1129 hash_expr (Lam b _) = hashId b
1130 hash_expr (Type t) = trace "hash_expr: type" 1 -- Shouldn't happen
1132 fast_hash_expr (Var v) = hashId v
1133 fast_hash_expr (Lit lit) = hashLiteral lit
1134 fast_hash_expr (App f (Type _)) = fast_hash_expr f
1135 fast_hash_expr (App f a) = fast_hash_expr a
1136 fast_hash_expr (Lam b _) = hashId b
1137 fast_hash_expr other = 1
1140 hashId id = hashName (idName id)
1143 %************************************************************************
1145 \subsection{Cross-DLL references}
1147 %************************************************************************
1149 Top-level constructor applications can usually be allocated
1150 statically, but they can't if
1151 a) the constructor, or any of the arguments, come from another DLL
1152 b) any of the arguments are LitLits
1153 (because we can't refer to static labels in other DLLs).
1155 If this happens we simply make the RHS into an updatable thunk,
1156 and 'exectute' it rather than allocating it statically.
1158 We also catch lit-lit arguments here, because those cannot be used in
1159 static constructors either. (litlits are deprecated, so I'm not going
1160 to bother cleaning up this infelicity --SDM).
1163 isCrossDllConApp :: DataCon -> [CoreExpr] -> Bool
1164 isCrossDllConApp con args =
1165 isDllName (dataConName con) || any isCrossDllArg args
1167 isCrossDllArg :: CoreExpr -> Bool
1168 -- True if somewhere in the expression there's a cross-DLL reference
1169 isCrossDllArg (Type _) = False
1170 isCrossDllArg (Var v) = isDllName (idName v)
1171 isCrossDllArg (Note _ e) = isCrossDllArg e
1172 isCrossDllArg (Lit lit) = isLitLitLit lit
1173 isCrossDllArg (App e1 e2) = isCrossDllArg e1 || isCrossDllArg e2
1174 -- must be a type app
1175 isCrossDllArg (Lam v e) = isCrossDllArg e
1176 -- must be a type lam
1179 %************************************************************************
1181 \subsection{Figuring out CafInfo for an expression}
1183 %************************************************************************
1185 hasCafRefs decides whether a top-level closure can point into the dynamic heap.
1186 We mark such things as `MayHaveCafRefs' because this information is
1187 used to decide whether a particular closure needs to be referenced
1190 There are two reasons for setting MayHaveCafRefs:
1191 a) The RHS is a CAF: a top-level updatable thunk.
1192 b) The RHS refers to something that MayHaveCafRefs
1194 Possible improvement: In an effort to keep the number of CAFs (and
1195 hence the size of the SRTs) down, we could also look at the expression and
1196 decide whether it requires a small bounded amount of heap, so we can ignore
1197 it as a CAF. In these cases however, we would need to use an additional
1198 CAF list to keep track of non-collectable CAFs.
1201 hasCafRefs :: (Var -> Bool) -> Arity -> CoreExpr -> CafInfo
1202 hasCafRefs p arity expr
1203 | is_caf || mentions_cafs = MayHaveCafRefs
1204 | otherwise = NoCafRefs
1206 mentions_cafs = isFastTrue (cafRefs p expr)
1207 is_caf = not (arity > 0 || rhsIsNonUpd expr)
1208 -- NB. we pass in the arity of the expression, which is expected
1209 -- to be calculated by exprArity. This is because exprArity
1210 -- knows how much eta expansion is going to be done by
1211 -- CorePrep later on, and we don't want to duplicate that
1212 -- knowledge in rhsIsNonUpd below.
1215 | isId id && p id = fastBool (mayHaveCafRefs (idCafInfo id))
1216 | otherwise = fastBool False
1218 cafRefs p (Lit l) = fastBool False
1219 cafRefs p (App f a) = fastOr (cafRefs p f) (cafRefs p) a
1220 cafRefs p (Lam x e) = cafRefs p e
1221 cafRefs p (Let b e) = fastOr (cafRefss p (rhssOfBind b)) (cafRefs p) e
1222 cafRefs p (Case e bndr alts) = fastOr (cafRefs p e) (cafRefss p) (rhssOfAlts alts)
1223 cafRefs p (Note n e) = cafRefs p e
1224 cafRefs p (Type t) = fastBool False
1226 cafRefss p [] = fastBool False
1227 cafRefss p (e:es) = fastOr (cafRefs p e) (cafRefss p) es
1229 -- hack for lazy-or over FastBool.
1230 fastOr a f x = fastBool (isFastTrue a || isFastTrue (f x))
1233 rhsIsNonUpd :: CoreExpr -> Bool
1234 -- True => Value-lambda, saturated constructor
1235 -- This is a bit like CoreUtils.exprIsValue, with the following differences:
1236 -- a) scc "foo" (\x -> ...) is updatable (so we catch the right SCC)
1238 -- b) (C x xs), where C is a contructors is updatable if the application is
1241 -- c) don't look through unfolding of f in (f x).
1243 -- When opt_RuntimeTypes is on, we keep type lambdas and treat
1244 -- them as making the RHS re-entrant (non-updatable).
1246 rhsIsNonUpd (Lam b e) = isRuntimeVar b || rhsIsNonUpd e
1247 rhsIsNonUpd (Note (SCC _) e) = False
1248 rhsIsNonUpd (Note _ e) = rhsIsNonUpd e
1249 rhsIsNonUpd other_expr
1250 = go other_expr 0 []
1252 go (Var f) n_args args = idAppIsNonUpd f n_args args
1254 go (App f a) n_args args
1255 | isTypeArg a = go f n_args args
1256 | otherwise = go f (n_args + 1) (a:args)
1258 go (Note (SCC _) f) n_args args = False
1259 go (Note _ f) n_args args = go f n_args args
1261 go other n_args args = False
1263 idAppIsNonUpd :: Id -> Int -> [CoreExpr] -> Bool
1264 idAppIsNonUpd id n_val_args args
1265 -- saturated constructors are not updatable
1266 | Just con <- isDataConWorkId_maybe id,
1267 n_val_args == dataConRepArity con,
1268 not (isCrossDllConApp con args),
1271 -- NB. args sometimes not atomic. eg.
1272 -- x = D# (1.0## /## 2.0##)
1273 -- can't float because /## can fail.
1276 -- Historical note: we used to make partial applications
1277 -- non-updatable, so they behaved just like PAPs, but this
1278 -- doesn't work too well with eval/apply so it is disabled