2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 Utility functions on @Core@ syntax
9 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 -- | Commonly useful utilites for manipulating the Core language
18 -- * Constructing expressions
19 mkInlineMe, mkSCC, mkCoerce, mkCoerceI,
20 bindNonRec, needsCaseBinding,
21 mkAltExpr, mkPiType, mkPiTypes,
23 -- * Taking expressions apart
24 findDefault, findAlt, isDefaultAlt, mergeAlts, trimConArgs,
26 -- * Properties of expressions
27 exprType, coreAltType, coreAltsType,
28 exprIsDupable, exprIsTrivial, exprIsCheap, exprIsExpandable,
29 exprIsHNF,exprOkForSpeculation, exprIsBig,
30 exprIsConApp_maybe, exprIsBottom,
33 -- * Expression and bindings size
34 coreBindsSize, exprSize,
42 -- * Manipulating data constructors and types
43 applyTypeToArgs, applyTypeToArg,
44 dataConOrigInstPat, dataConRepInstPat, dataConRepFSInstPat
47 #include "HsVersions.h"
78 import GHC.Exts -- For `xori`
82 %************************************************************************
84 \subsection{Find the type of a Core atom/expression}
86 %************************************************************************
89 exprType :: CoreExpr -> Type
90 -- ^ Recover the type of a well-typed Core expression. Fails when
91 -- applied to the actual 'CoreSyn.Type' expression as it cannot
92 -- really be said to have a type
93 exprType (Var var) = idType var
94 exprType (Lit lit) = literalType lit
95 exprType (Let _ body) = exprType body
96 exprType (Case _ _ ty _) = ty
97 exprType (Cast _ co) = snd (coercionKind co)
98 exprType (Note _ e) = exprType e
99 exprType (Lam binder expr) = mkPiType binder (exprType expr)
101 = case collectArgs e of
102 (fun, args) -> applyTypeToArgs e (exprType fun) args
104 exprType other = pprTrace "exprType" (pprCoreExpr other) alphaTy
106 coreAltType :: CoreAlt -> Type
107 -- ^ Returns the type of the alternatives right hand side
108 coreAltType (_,_,rhs) = exprType rhs
110 coreAltsType :: [CoreAlt] -> Type
111 -- ^ Returns the type of the first alternative, which should be the same as for all alternatives
112 coreAltsType (alt:_) = coreAltType alt
113 coreAltsType [] = panic "corAltsType"
117 mkPiType :: Var -> Type -> Type
118 -- ^ Makes a @(->)@ type or a forall type, depending
119 -- on whether it is given a type variable or a term variable.
120 mkPiTypes :: [Var] -> Type -> Type
121 -- ^ 'mkPiType' for multiple type or value arguments
124 | isId v = mkFunTy (idType v) ty
125 | otherwise = mkForAllTy v ty
127 mkPiTypes vs ty = foldr mkPiType ty vs
131 applyTypeToArg :: Type -> CoreExpr -> Type
132 -- ^ Determines the type resulting from applying an expression to a function with the given type
133 applyTypeToArg fun_ty (Type arg_ty) = applyTy fun_ty arg_ty
134 applyTypeToArg fun_ty _ = funResultTy fun_ty
136 applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> Type
137 -- ^ A more efficient version of 'applyTypeToArg' when we have several arguments.
138 -- The first argument is just for debugging, and gives some context
139 applyTypeToArgs _ op_ty [] = op_ty
141 applyTypeToArgs e op_ty (Type ty : args)
142 = -- Accumulate type arguments so we can instantiate all at once
145 go rev_tys (Type ty : args) = go (ty:rev_tys) args
146 go rev_tys rest_args = applyTypeToArgs e op_ty' rest_args
148 op_ty' = applyTysD msg op_ty (reverse rev_tys)
149 msg = ptext (sLit "applyTypeToArgs") <+>
152 applyTypeToArgs e op_ty (_ : args)
153 = case (splitFunTy_maybe op_ty) of
154 Just (_, res_ty) -> applyTypeToArgs e res_ty args
155 Nothing -> pprPanic "applyTypeToArgs" (panic_msg e op_ty)
157 panic_msg :: CoreExpr -> Type -> SDoc
158 panic_msg e op_ty = pprCoreExpr e $$ ppr op_ty
161 %************************************************************************
163 \subsection{Attaching notes}
165 %************************************************************************
167 mkNote removes redundant coercions, and SCCs where possible
171 mkNote :: Note -> CoreExpr -> CoreExpr
172 mkNote (SCC cc) expr = mkSCC cc expr
173 mkNote InlineMe expr = mkInlineMe expr
174 mkNote note expr = Note note expr
178 Drop trivial InlineMe's. This is somewhat important, because if we have an unfolding
179 that looks like (Note InlineMe (Var v)), the InlineMe doesn't go away because it may
180 not be *applied* to anything.
182 We don't use exprIsTrivial here, though, because we sometimes generate worker/wrapper
185 f = inline_me (coerce t fw)
186 As usual, the inline_me prevents the worker from getting inlined back into the wrapper.
187 We want the split, so that the coerces can cancel at the call site.
189 However, we can get left with tiresome type applications. Notably, consider
190 f = /\ a -> let t = e in (t, w)
191 Then lifting the let out of the big lambda gives
193 f = /\ a -> let t = inline_me (t' a) in (t, w)
194 The inline_me is to stop the simplifier inlining t' right back
195 into t's RHS. In the next phase we'll substitute for t (since
196 its rhs is trivial) and *then* we could get rid of the inline_me.
197 But it hardly seems worth it, so I don't bother.
200 -- | Wraps the given expression in an inlining hint unless the expression
201 -- is trivial in some sense, so that doing so would usually hurt us
202 mkInlineMe :: CoreExpr -> CoreExpr
203 mkInlineMe (Var v) = Var v
204 mkInlineMe e = Note InlineMe e
208 -- | Wrap the given expression in the coercion, dropping identity coercions and coalescing nested coercions
209 mkCoerceI :: CoercionI -> CoreExpr -> CoreExpr
211 mkCoerceI (ACo co) e = mkCoerce co e
213 -- | Wrap the given expression in the coercion safely, coalescing nested coercions
214 mkCoerce :: Coercion -> CoreExpr -> CoreExpr
215 mkCoerce co (Cast expr co2)
216 = ASSERT(let { (from_ty, _to_ty) = coercionKind co;
217 (_from_ty2, to_ty2) = coercionKind co2} in
218 from_ty `coreEqType` to_ty2 )
219 mkCoerce (mkTransCoercion co2 co) expr
222 = let (from_ty, _to_ty) = coercionKind co in
223 -- if to_ty `coreEqType` from_ty
226 ASSERT2(from_ty `coreEqType` (exprType expr), text "Trying to coerce" <+> text "(" <> ppr expr $$ text "::" <+> ppr (exprType expr) <> text ")" $$ ppr co $$ ppr (coercionKindPredTy co))
231 -- | Wraps the given expression in the cost centre unless
232 -- in a way that maximises their utility to the user
233 mkSCC :: CostCentre -> Expr b -> Expr b
234 -- Note: Nested SCC's *are* preserved for the benefit of
235 -- cost centre stack profiling
236 mkSCC _ (Lit lit) = Lit lit
237 mkSCC cc (Lam x e) = Lam x (mkSCC cc e) -- Move _scc_ inside lambda
238 mkSCC cc (Note (SCC cc') e) = Note (SCC cc) (Note (SCC cc') e)
239 mkSCC cc (Note n e) = Note n (mkSCC cc e) -- Move _scc_ inside notes
240 mkSCC cc (Cast e co) = Cast (mkSCC cc e) co -- Move _scc_ inside cast
241 mkSCC cc expr = Note (SCC cc) expr
245 %************************************************************************
247 \subsection{Other expression construction}
249 %************************************************************************
252 bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
253 -- ^ @bindNonRec x r b@ produces either:
259 -- > case r of x { _DEFAULT_ -> b }
261 -- depending on whether we have to use a @case@ or @let@
262 -- binding for the expression (see 'needsCaseBinding').
263 -- It's used by the desugarer to avoid building bindings
264 -- that give Core Lint a heart attack, although actually
265 -- the simplifier deals with them perfectly well. See
266 -- also 'MkCore.mkCoreLet'
267 bindNonRec bndr rhs body
268 | needsCaseBinding (idType bndr) rhs = Case rhs bndr (exprType body) [(DEFAULT, [], body)]
269 | otherwise = Let (NonRec bndr rhs) body
271 -- | Tests whether we have to use a @case@ rather than @let@ binding for this expression
272 -- as per the invariants of 'CoreExpr': see "CoreSyn#let_app_invariant"
273 needsCaseBinding :: Type -> CoreExpr -> Bool
274 needsCaseBinding ty rhs = isUnLiftedType ty && not (exprOkForSpeculation rhs)
275 -- Make a case expression instead of a let
276 -- These can arise either from the desugarer,
277 -- or from beta reductions: (\x.e) (x +# y)
281 mkAltExpr :: AltCon -- ^ Case alternative constructor
282 -> [CoreBndr] -- ^ Things bound by the pattern match
283 -> [Type] -- ^ The type arguments to the case alternative
285 -- ^ This guy constructs the value that the scrutinee must have
286 -- given that you are in one particular branch of a case
287 mkAltExpr (DataAlt con) args inst_tys
288 = mkConApp con (map Type inst_tys ++ varsToCoreExprs args)
289 mkAltExpr (LitAlt lit) [] []
291 mkAltExpr (LitAlt _) _ _ = panic "mkAltExpr LitAlt"
292 mkAltExpr DEFAULT _ _ = panic "mkAltExpr DEFAULT"
296 %************************************************************************
298 \subsection{Taking expressions apart}
300 %************************************************************************
302 The default alternative must be first, if it exists at all.
303 This makes it easy to find, though it makes matching marginally harder.
306 -- | Extract the default case alternative
307 findDefault :: [CoreAlt] -> ([CoreAlt], Maybe CoreExpr)
308 findDefault ((DEFAULT,args,rhs) : alts) = ASSERT( null args ) (alts, Just rhs)
309 findDefault alts = (alts, Nothing)
311 -- | Find the case alternative corresponding to a particular
312 -- constructor: panics if no such constructor exists
313 findAlt :: AltCon -> [CoreAlt] -> CoreAlt
316 (deflt@(DEFAULT,_,_):alts) -> go alts deflt
317 _ -> go alts panic_deflt
319 panic_deflt = pprPanic "Missing alternative" (ppr con $$ vcat (map ppr alts))
322 go (alt@(con1,_,_) : alts) deflt
323 = case con `cmpAltCon` con1 of
324 LT -> deflt -- Missed it already; the alts are in increasing order
326 GT -> ASSERT( not (con1 == DEFAULT) ) go alts deflt
328 isDefaultAlt :: CoreAlt -> Bool
329 isDefaultAlt (DEFAULT, _, _) = True
330 isDefaultAlt _ = False
332 ---------------------------------
333 mergeAlts :: [CoreAlt] -> [CoreAlt] -> [CoreAlt]
334 -- ^ Merge alternatives preserving order; alternatives in
335 -- the first argument shadow ones in the second
336 mergeAlts [] as2 = as2
337 mergeAlts as1 [] = as1
338 mergeAlts (a1:as1) (a2:as2)
339 = case a1 `cmpAlt` a2 of
340 LT -> a1 : mergeAlts as1 (a2:as2)
341 EQ -> a1 : mergeAlts as1 as2 -- Discard a2
342 GT -> a2 : mergeAlts (a1:as1) as2
345 ---------------------------------
346 trimConArgs :: AltCon -> [CoreArg] -> [CoreArg]
349 -- > case (C a b x y) of
352 -- We want to drop the leading type argument of the scrutinee
353 -- leaving the arguments to match agains the pattern
355 trimConArgs DEFAULT args = ASSERT( null args ) []
356 trimConArgs (LitAlt _) args = ASSERT( null args ) []
357 trimConArgs (DataAlt dc) args = dropList (dataConUnivTyVars dc) args
361 %************************************************************************
363 \subsection{Figuring out things about expressions}
365 %************************************************************************
367 @exprIsTrivial@ is true of expressions we are unconditionally happy to
368 duplicate; simple variables and constants, and type
369 applications. Note that primop Ids aren't considered
372 There used to be a gruesome test for (hasNoBinding v) in the
374 exprIsTrivial (Var v) | hasNoBinding v = idArity v == 0
375 The idea here is that a constructor worker, like \$wJust, is
376 really short for (\x -> \$wJust x), becuase \$wJust has no binding.
377 So it should be treated like a lambda. Ditto unsaturated primops.
378 But now constructor workers are not "have-no-binding" Ids. And
379 completely un-applied primops and foreign-call Ids are sufficiently
380 rare that I plan to allow them to be duplicated and put up with
383 SCC notes. We do not treat (_scc_ "foo" x) as trivial, because
384 a) it really generates code, (and a heap object when it's
385 a function arg) to capture the cost centre
386 b) see the note [SCC-and-exprIsTrivial] in Simplify.simplLazyBind
389 exprIsTrivial :: CoreExpr -> Bool
390 exprIsTrivial (Var _) = True -- See notes above
391 exprIsTrivial (Type _) = True
392 exprIsTrivial (Lit lit) = litIsTrivial lit
393 exprIsTrivial (App e arg) = not (isRuntimeArg arg) && exprIsTrivial e
394 exprIsTrivial (Note (SCC _) _) = False -- See notes above
395 exprIsTrivial (Note _ e) = exprIsTrivial e
396 exprIsTrivial (Cast e _) = exprIsTrivial e
397 exprIsTrivial (Lam b body) = not (isRuntimeVar b) && exprIsTrivial body
398 exprIsTrivial _ = False
402 @exprIsDupable@ is true of expressions that can be duplicated at a modest
403 cost in code size. This will only happen in different case
404 branches, so there's no issue about duplicating work.
406 That is, exprIsDupable returns True of (f x) even if
407 f is very very expensive to call.
409 Its only purpose is to avoid fruitless let-binding
410 and then inlining of case join points
414 exprIsDupable :: CoreExpr -> Bool
415 exprIsDupable (Type _) = True
416 exprIsDupable (Var _) = True
417 exprIsDupable (Lit lit) = litIsDupable lit
418 exprIsDupable (Note InlineMe _) = True
419 exprIsDupable (Note _ e) = exprIsDupable e
420 exprIsDupable (Cast e _) = exprIsDupable e
425 go (App f a) n_args = n_args < dupAppSize
431 dupAppSize = 4 -- Size of application we are prepared to duplicate
434 @exprIsCheap@ looks at a Core expression and returns \tr{True} if
435 it is obviously in weak head normal form, or is cheap to get to WHNF.
436 [Note that that's not the same as exprIsDupable; an expression might be
437 big, and hence not dupable, but still cheap.]
439 By ``cheap'' we mean a computation we're willing to:
440 push inside a lambda, or
441 inline at more than one place
442 That might mean it gets evaluated more than once, instead of being
443 shared. The main examples of things which aren't WHNF but are
448 (where e, and all the ei are cheap)
451 (where e and b are cheap)
454 (where op is a cheap primitive operator)
457 (because we are happy to substitute it inside a lambda)
459 Notice that a variable is considered 'cheap': we can push it inside a lambda,
460 because sharing will make sure it is only evaluated once.
463 exprIsCheap' :: (Id -> Bool) -> CoreExpr -> Bool
464 exprIsCheap' _ (Lit _) = True
465 exprIsCheap' _ (Type _) = True
466 exprIsCheap' _ (Var _) = True
467 exprIsCheap' _ (Note InlineMe _) = True
468 exprIsCheap' is_conlike (Note _ e) = exprIsCheap' is_conlike e
469 exprIsCheap' is_conlike (Cast e _) = exprIsCheap' is_conlike e
470 exprIsCheap' is_conlike (Lam x e) = isRuntimeVar x
471 || exprIsCheap' is_conlike e
472 exprIsCheap' is_conlike (Case e _ _ alts) = exprIsCheap' is_conlike e &&
473 and [exprIsCheap' is_conlike rhs | (_,_,rhs) <- alts]
474 -- Experimentally, treat (case x of ...) as cheap
475 -- (and case __coerce x etc.)
476 -- This improves arities of overloaded functions where
477 -- there is only dictionary selection (no construction) involved
478 exprIsCheap' is_conlike (Let (NonRec x _) e)
479 | isUnLiftedType (idType x) = exprIsCheap' is_conlike e
481 -- strict lets always have cheap right hand sides,
482 -- and do no allocation.
484 exprIsCheap' is_conlike other_expr -- Applications and variables
487 -- Accumulate value arguments, then decide
488 go (App f a) val_args | isRuntimeArg a = go f (a:val_args)
489 | otherwise = go f val_args
491 go (Var _) [] = True -- Just a type application of a variable
492 -- (f t1 t2 t3) counts as WHNF
494 = case idDetails f of
495 RecSelId {} -> go_sel args
496 ClassOpId _ -> go_sel args
497 PrimOpId op -> go_primop op args
499 _ | is_conlike f -> go_pap args
500 | length args < idArity f -> go_pap args
503 -- Application of a function which
504 -- always gives bottom; we treat this as cheap
505 -- because it certainly doesn't need to be shared!
510 go_pap args = all exprIsTrivial args
511 -- For constructor applications and primops, check that all
512 -- the args are trivial. We don't want to treat as cheap, say,
514 -- We'll put up with one constructor application, but not dozens
517 go_primop op args = primOpIsCheap op && all (exprIsCheap' is_conlike) args
518 -- In principle we should worry about primops
519 -- that return a type variable, since the result
520 -- might be applied to something, but I'm not going
521 -- to bother to check the number of args
524 go_sel [arg] = exprIsCheap' is_conlike arg -- I'm experimenting with making record selection
525 go_sel _ = False -- look cheap, so we will substitute it inside a
526 -- lambda. Particularly for dictionary field selection.
527 -- BUT: Take care with (sel d x)! The (sel d) might be cheap, but
528 -- there's no guarantee that (sel d x) will be too. Hence (n_val_args == 1)
530 exprIsCheap :: CoreExpr -> Bool
531 exprIsCheap = exprIsCheap' isDataConWorkId
533 exprIsExpandable :: CoreExpr -> Bool
534 exprIsExpandable = exprIsCheap' isConLikeId
538 -- | 'exprOkForSpeculation' returns True of an expression that is:
540 -- * Safe to evaluate even if normal order eval might not
541 -- evaluate the expression at all, or
543 -- * Safe /not/ to evaluate even if normal order would do so
545 -- Precisely, it returns @True@ iff:
547 -- * The expression guarantees to terminate,
551 -- * without raising an exception,
553 -- * without causing a side effect (e.g. writing a mutable variable)
555 -- Note that if @exprIsHNF e@, then @exprOkForSpecuation e@.
556 -- As an example of the considerations in this test, consider:
558 -- > let x = case y# +# 1# of { r# -> I# r# }
561 -- being translated to:
563 -- > case y# +# 1# of { r# ->
568 -- We can only do this if the @y + 1@ is ok for speculation: it has no
569 -- side effects, and can't diverge or raise an exception.
570 exprOkForSpeculation :: CoreExpr -> Bool
571 exprOkForSpeculation (Lit _) = True
572 exprOkForSpeculation (Type _) = True
573 -- Tick boxes are *not* suitable for speculation
574 exprOkForSpeculation (Var v) = isUnLiftedType (idType v)
575 && not (isTickBoxOp v)
576 exprOkForSpeculation (Note _ e) = exprOkForSpeculation e
577 exprOkForSpeculation (Cast e _) = exprOkForSpeculation e
578 exprOkForSpeculation other_expr
579 = case collectArgs other_expr of
580 (Var f, args) -> spec_ok (idDetails f) args
584 spec_ok (DataConWorkId _) _
585 = True -- The strictness of the constructor has already
586 -- been expressed by its "wrapper", so we don't need
587 -- to take the arguments into account
589 spec_ok (PrimOpId op) args
590 | isDivOp op, -- Special case for dividing operations that fail
591 [arg1, Lit lit] <- args -- only if the divisor is zero
592 = not (isZeroLit lit) && exprOkForSpeculation arg1
593 -- Often there is a literal divisor, and this
594 -- can get rid of a thunk in an inner looop
597 = primOpOkForSpeculation op &&
598 all exprOkForSpeculation args
599 -- A bit conservative: we don't really need
600 -- to care about lazy arguments, but this is easy
604 -- | True of dyadic operators that can fail only if the second arg is zero!
605 isDivOp :: PrimOp -> Bool
606 -- This function probably belongs in PrimOp, or even in
607 -- an automagically generated file.. but it's such a
608 -- special case I thought I'd leave it here for now.
609 isDivOp IntQuotOp = True
610 isDivOp IntRemOp = True
611 isDivOp WordQuotOp = True
612 isDivOp WordRemOp = True
613 isDivOp IntegerQuotRemOp = True
614 isDivOp IntegerDivModOp = True
615 isDivOp FloatDivOp = True
616 isDivOp DoubleDivOp = True
621 -- | True of expressions that are guaranteed to diverge upon execution
622 exprIsBottom :: CoreExpr -> Bool
623 exprIsBottom e = go 0 e
625 -- n is the number of args
626 go n (Note _ e) = go n e
627 go n (Cast e _) = go n e
628 go n (Let _ e) = go n e
629 go _ (Case e _ _ _) = go 0 e -- Just check the scrut
630 go n (App e _) = go (n+1) e
631 go n (Var v) = idAppIsBottom v n
633 go _ (Lam _ _) = False
634 go _ (Type _) = False
636 idAppIsBottom :: Id -> Int -> Bool
637 idAppIsBottom id n_val_args = appIsBottom (idNewStrictness id) n_val_args
642 -- | This returns true for expressions that are certainly /already/
643 -- evaluated to /head/ normal form. This is used to decide whether it's ok
646 -- > case x of _ -> e
652 -- and to decide whether it's safe to discard a 'seq'.
653 -- So, it does /not/ treat variables as evaluated, unless they say they are.
654 -- However, it /does/ treat partial applications and constructor applications
655 -- as values, even if their arguments are non-trivial, provided the argument
656 -- type is lifted. For example, both of these are values:
658 -- > (:) (f x) (map f xs)
659 -- > map (...redex...)
661 -- Because 'seq' on such things completes immediately.
663 -- For unlifted argument types, we have to be careful:
667 -- Suppose @f x@ diverges; then @C (f x)@ is not a value. However this can't
668 -- happen: see "CoreSyn#let_app_invariant". This invariant states that arguments of
669 -- unboxed type must be ok-for-speculation (or trivial).
670 exprIsHNF :: CoreExpr -> Bool -- True => Value-lambda, constructor, PAP
671 exprIsHNF (Var v) -- NB: There are no value args at this point
672 = isDataConWorkId v -- Catches nullary constructors,
673 -- so that [] and () are values, for example
674 || idArity v > 0 -- Catches (e.g.) primops that don't have unfoldings
675 || isEvaldUnfolding (idUnfolding v)
676 -- Check the thing's unfolding; it might be bound to a value
677 -- A worry: what if an Id's unfolding is just itself:
678 -- then we could get an infinite loop...
680 exprIsHNF (Lit _) = True
681 exprIsHNF (Type _) = True -- Types are honorary Values;
682 -- we don't mind copying them
683 exprIsHNF (Lam b e) = isRuntimeVar b || exprIsHNF e
684 exprIsHNF (Note _ e) = exprIsHNF e
685 exprIsHNF (Cast e _) = exprIsHNF e
686 exprIsHNF (App e (Type _)) = exprIsHNF e
687 exprIsHNF (App e a) = app_is_value e [a]
690 -- There is at least one value argument
691 app_is_value :: CoreExpr -> [CoreArg] -> Bool
692 app_is_value (Var fun) args
693 = idArity fun > valArgCount args -- Under-applied function
694 || isDataConWorkId fun -- or data constructor
695 app_is_value (Note _ f) as = app_is_value f as
696 app_is_value (Cast f _) as = app_is_value f as
697 app_is_value (App f a) as = app_is_value f (a:as)
698 app_is_value _ _ = False
701 These InstPat functions go here to avoid circularity between DataCon and Id
704 dataConRepInstPat, dataConOrigInstPat :: [Unique] -> DataCon -> [Type] -> ([TyVar], [CoVar], [Id])
705 dataConRepFSInstPat :: [FastString] -> [Unique] -> DataCon -> [Type] -> ([TyVar], [CoVar], [Id])
707 dataConRepInstPat = dataConInstPat dataConRepArgTys (repeat ((fsLit "ipv")))
708 dataConRepFSInstPat = dataConInstPat dataConRepArgTys
709 dataConOrigInstPat = dataConInstPat dc_arg_tys (repeat ((fsLit "ipv")))
711 dc_arg_tys dc = map mkPredTy (dataConEqTheta dc) ++ map mkPredTy (dataConDictTheta dc) ++ dataConOrigArgTys dc
712 -- Remember to include the existential dictionaries
714 dataConInstPat :: (DataCon -> [Type]) -- function used to find arg tys
715 -> [FastString] -- A long enough list of FSs to use for names
716 -> [Unique] -- An equally long list of uniques, at least one for each binder
718 -> [Type] -- Types to instantiate the universally quantified tyvars
719 -> ([TyVar], [CoVar], [Id]) -- Return instantiated variables
720 -- dataConInstPat arg_fun fss us con inst_tys returns a triple
721 -- (ex_tvs, co_tvs, arg_ids),
723 -- ex_tvs are intended to be used as binders for existential type args
725 -- co_tvs are intended to be used as binders for coercion args and the kinds
726 -- of these vars have been instantiated by the inst_tys and the ex_tys
727 -- The co_tvs include both GADT equalities (dcEqSpec) and
728 -- programmer-specified equalities (dcEqTheta)
730 -- arg_ids are indended to be used as binders for value arguments,
731 -- and their types have been instantiated with inst_tys and ex_tys
732 -- The arg_ids include both dicts (dcDictTheta) and
733 -- programmer-specified arguments (after rep-ing) (deRepArgTys)
736 -- The following constructor T1
739 -- T1 :: forall b. Int -> b -> T(a,b)
742 -- has representation type
743 -- forall a. forall a1. forall b. (a ~ (a1,b)) =>
746 -- dataConInstPat fss us T1 (a1',b') will return
748 -- ([a1'', b''], [c :: (a1', b')~(a1'', b'')], [x :: Int, y :: b''])
750 -- where the double-primed variables are created with the FastStrings and
751 -- Uniques given as fss and us
752 dataConInstPat arg_fun fss uniqs con inst_tys
753 = (ex_bndrs, co_bndrs, arg_ids)
755 univ_tvs = dataConUnivTyVars con
756 ex_tvs = dataConExTyVars con
757 arg_tys = arg_fun con
758 eq_spec = dataConEqSpec con
759 eq_theta = dataConEqTheta con
760 eq_preds = eqSpecPreds eq_spec ++ eq_theta
763 n_co = length eq_preds
765 -- split the Uniques and FastStrings
766 (ex_uniqs, uniqs') = splitAt n_ex uniqs
767 (co_uniqs, id_uniqs) = splitAt n_co uniqs'
769 (ex_fss, fss') = splitAt n_ex fss
770 (co_fss, id_fss) = splitAt n_co fss'
772 -- Make existential type variables
773 ex_bndrs = zipWith3 mk_ex_var ex_uniqs ex_fss ex_tvs
774 mk_ex_var uniq fs var = mkTyVar new_name kind
776 new_name = mkSysTvName uniq fs
779 -- Make the instantiating substitution
780 subst = zipOpenTvSubst (univ_tvs ++ ex_tvs) (inst_tys ++ map mkTyVarTy ex_bndrs)
782 -- Make new coercion vars, instantiating kind
783 co_bndrs = zipWith3 mk_co_var co_uniqs co_fss eq_preds
784 mk_co_var uniq fs eq_pred = mkCoVar new_name co_kind
786 new_name = mkSysTvName uniq fs
787 co_kind = substTy subst (mkPredTy eq_pred)
789 -- make value vars, instantiating types
790 mk_id_var uniq fs ty = mkUserLocal (mkVarOccFS fs) uniq (substTy subst ty) noSrcSpan
791 arg_ids = zipWith3 mk_id_var id_uniqs id_fss arg_tys
793 -- | Returns @Just (dc, [x1..xn])@ if the argument expression is
794 -- a constructor application of the form @dc x1 .. xn@
795 exprIsConApp_maybe :: CoreExpr -> Maybe (DataCon, [CoreExpr])
796 exprIsConApp_maybe (Cast expr co)
797 = -- Here we do the KPush reduction rule as described in the FC paper
798 case exprIsConApp_maybe expr of {
800 Just (dc, dc_args) ->
802 -- The transformation applies iff we have
803 -- (C e1 ... en) `cast` co
804 -- where co :: (T t1 .. tn) ~ (T s1 ..sn)
805 -- That is, with a T at the top of both sides
806 -- The left-hand one must be a T, because exprIsConApp returned True
807 -- but the right-hand one might not be. (Though it usually will.)
809 let (from_ty, to_ty) = coercionKind co
810 (from_tc, from_tc_arg_tys) = splitTyConApp from_ty
811 -- The inner one must be a TyConApp
813 case splitTyConApp_maybe to_ty of {
815 Just (to_tc, to_tc_arg_tys)
816 | from_tc /= to_tc -> Nothing
817 -- These two Nothing cases are possible; we might see
818 -- (C x y) `cast` (g :: T a ~ S [a]),
819 -- where S is a type function. In fact, exprIsConApp
820 -- will probably not be called in such circumstances,
821 -- but there't nothing wrong with it
825 tc_arity = tyConArity from_tc
827 (univ_args, rest1) = splitAt tc_arity dc_args
828 (ex_args, rest2) = splitAt n_ex_tvs rest1
829 (co_args_spec, rest3) = splitAt n_cos_spec rest2
830 (co_args_theta, val_args) = splitAt n_cos_theta rest3
832 arg_tys = dataConRepArgTys dc
833 dc_univ_tyvars = dataConUnivTyVars dc
834 dc_ex_tyvars = dataConExTyVars dc
835 dc_eq_spec = dataConEqSpec dc
836 dc_eq_theta = dataConEqTheta dc
837 dc_tyvars = dc_univ_tyvars ++ dc_ex_tyvars
838 n_ex_tvs = length dc_ex_tyvars
839 n_cos_spec = length dc_eq_spec
840 n_cos_theta = length dc_eq_theta
842 -- Make the "theta" from Fig 3 of the paper
843 gammas = decomposeCo tc_arity co
844 new_tys = gammas ++ map (\ (Type t) -> t) ex_args
845 theta = zipOpenTvSubst dc_tyvars new_tys
847 -- First we cast the existential coercion arguments
848 cast_co_spec (tv, ty) co
849 = cast_co_theta (mkEqPred (mkTyVarTy tv, ty)) co
850 cast_co_theta eqPred (Type co)
851 | (ty1, ty2) <- getEqPredTys eqPred
852 = Type $ mkSymCoercion (substTy theta ty1)
854 `mkTransCoercion` (substTy theta ty2)
855 new_co_args = zipWith cast_co_spec dc_eq_spec co_args_spec ++
856 zipWith cast_co_theta dc_eq_theta co_args_theta
858 -- ...and now value arguments
859 new_val_args = zipWith cast_arg arg_tys val_args
860 cast_arg arg_ty arg = mkCoerce (substTy theta arg_ty) arg
863 ASSERT( length univ_args == tc_arity )
864 ASSERT( from_tc == dataConTyCon dc )
865 ASSERT( and (zipWith coreEqType [t | Type t <- univ_args] from_tc_arg_tys) )
866 ASSERT( all isTypeArg (univ_args ++ ex_args) )
867 ASSERT2( equalLength val_args arg_tys, ppr dc $$ ppr dc_tyvars $$ ppr dc_ex_tyvars $$ ppr arg_tys $$ ppr dc_args $$ ppr univ_args $$ ppr ex_args $$ ppr val_args $$ ppr arg_tys )
869 Just (dc, map Type to_tc_arg_tys ++ ex_args ++ new_co_args ++ new_val_args)
873 -- We do not want to tell the world that we have a
874 -- Cons, to *stop* Case of Known Cons, which removes
876 exprIsConApp_maybe (Note (TickBox {}) expr)
878 exprIsConApp_maybe (Note (BinaryTickBox {}) expr)
882 exprIsConApp_maybe (Note _ expr)
883 = exprIsConApp_maybe expr
884 -- We ignore InlineMe notes in case we have
885 -- x = __inline_me__ (a,b)
886 -- All part of making sure that INLINE pragmas never hurt
887 -- Marcin tripped on this one when making dictionaries more inlinable
889 -- In fact, we ignore all notes. For example,
890 -- case _scc_ "foo" (C a b) of
892 -- should be optimised away, but it will be only if we look
893 -- through the SCC note.
895 exprIsConApp_maybe expr = analyse (collectArgs expr)
897 analyse (Var fun, args)
898 | Just con <- isDataConWorkId_maybe fun,
899 args `lengthAtLeast` dataConRepArity con
900 -- Might be > because the arity excludes type args
903 -- Look through unfoldings, but only cheap ones, because
904 -- we are effectively duplicating the unfolding
905 analyse (Var fun, [])
906 | let unf = idUnfolding fun,
907 isExpandableUnfolding unf
908 = exprIsConApp_maybe (unfoldingTemplate unf)
915 %************************************************************************
917 \subsection{Equality}
919 %************************************************************************
922 -- | A cheap equality test which bales out fast!
923 -- If it returns @True@ the arguments are definitely equal,
924 -- otherwise, they may or may not be equal.
926 -- See also 'exprIsBig'
927 cheapEqExpr :: Expr b -> Expr b -> Bool
929 cheapEqExpr (Var v1) (Var v2) = v1==v2
930 cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
931 cheapEqExpr (Type t1) (Type t2) = t1 `coreEqType` t2
933 cheapEqExpr (App f1 a1) (App f2 a2)
934 = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
936 cheapEqExpr (Cast e1 t1) (Cast e2 t2)
937 = e1 `cheapEqExpr` e2 && t1 `coreEqCoercion` t2
939 cheapEqExpr _ _ = False
941 exprIsBig :: Expr b -> Bool
942 -- ^ Returns @True@ of expressions that are too big to be compared by 'cheapEqExpr'
943 exprIsBig (Lit _) = False
944 exprIsBig (Var _) = False
945 exprIsBig (Type _) = False
946 exprIsBig (App f a) = exprIsBig f || exprIsBig a
947 exprIsBig (Cast e _) = exprIsBig e -- Hopefully coercions are not too big!
953 %************************************************************************
955 \subsection{The size of an expression}
957 %************************************************************************
960 coreBindsSize :: [CoreBind] -> Int
961 coreBindsSize bs = foldr ((+) . bindSize) 0 bs
963 exprSize :: CoreExpr -> Int
964 -- ^ A measure of the size of the expressions, strictly greater than 0
965 -- It also forces the expression pretty drastically as a side effect
966 exprSize (Var v) = v `seq` 1
967 exprSize (Lit lit) = lit `seq` 1
968 exprSize (App f a) = exprSize f + exprSize a
969 exprSize (Lam b e) = varSize b + exprSize e
970 exprSize (Let b e) = bindSize b + exprSize e
971 exprSize (Case e b t as) = seqType t `seq` exprSize e + varSize b + 1 + foldr ((+) . altSize) 0 as
972 exprSize (Cast e co) = (seqType co `seq` 1) + exprSize e
973 exprSize (Note n e) = noteSize n + exprSize e
974 exprSize (Type t) = seqType t `seq` 1
976 noteSize :: Note -> Int
977 noteSize (SCC cc) = cc `seq` 1
978 noteSize InlineMe = 1
979 noteSize (CoreNote s) = s `seq` 1 -- hdaume: core annotations
981 varSize :: Var -> Int
982 varSize b | isTyVar b = 1
983 | otherwise = seqType (idType b) `seq`
984 megaSeqIdInfo (idInfo b) `seq`
987 varsSize :: [Var] -> Int
988 varsSize = sum . map varSize
990 bindSize :: CoreBind -> Int
991 bindSize (NonRec b e) = varSize b + exprSize e
992 bindSize (Rec prs) = foldr ((+) . pairSize) 0 prs
994 pairSize :: (Var, CoreExpr) -> Int
995 pairSize (b,e) = varSize b + exprSize e
997 altSize :: CoreAlt -> Int
998 altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
1002 %************************************************************************
1004 \subsection{Hashing}
1006 %************************************************************************
1009 hashExpr :: CoreExpr -> Int
1010 -- ^ Two expressions that hash to the same @Int@ may be equal (but may not be)
1011 -- Two expressions that hash to the different Ints are definitely unequal.
1013 -- The emphasis is on a crude, fast hash, rather than on high precision.
1015 -- But unequal here means \"not identical\"; two alpha-equivalent
1016 -- expressions may hash to the different Ints.
1018 -- We must be careful that @\\x.x@ and @\\y.y@ map to the same hash code,
1019 -- (at least if we want the above invariant to be true).
1021 hashExpr e = fromIntegral (hash_expr (1,emptyVarEnv) e .&. 0x7fffffff)
1022 -- UniqFM doesn't like negative Ints
1024 type HashEnv = (Int, VarEnv Int) -- Hash code for bound variables
1026 hash_expr :: HashEnv -> CoreExpr -> Word32
1027 -- Word32, because we're expecting overflows here, and overflowing
1028 -- signed types just isn't cool. In C it's even undefined.
1029 hash_expr env (Note _ e) = hash_expr env e
1030 hash_expr env (Cast e _) = hash_expr env e
1031 hash_expr env (Var v) = hashVar env v
1032 hash_expr _ (Lit lit) = fromIntegral (hashLiteral lit)
1033 hash_expr env (App f e) = hash_expr env f * fast_hash_expr env e
1034 hash_expr env (Let (NonRec b r) e) = hash_expr (extend_env env b) e * fast_hash_expr env r
1035 hash_expr env (Let (Rec ((b,_):_)) e) = hash_expr (extend_env env b) e
1036 hash_expr env (Case e _ _ _) = hash_expr env e
1037 hash_expr env (Lam b e) = hash_expr (extend_env env b) e
1038 hash_expr _ (Type _) = WARN(True, text "hash_expr: type") 1
1039 -- Shouldn't happen. Better to use WARN than trace, because trace
1040 -- prevents the CPR optimisation kicking in for hash_expr.
1042 fast_hash_expr :: HashEnv -> CoreExpr -> Word32
1043 fast_hash_expr env (Var v) = hashVar env v
1044 fast_hash_expr env (Type t) = fast_hash_type env t
1045 fast_hash_expr _ (Lit lit) = fromIntegral (hashLiteral lit)
1046 fast_hash_expr env (Cast e _) = fast_hash_expr env e
1047 fast_hash_expr env (Note _ e) = fast_hash_expr env e
1048 fast_hash_expr env (App _ a) = fast_hash_expr env a -- A bit idiosyncratic ('a' not 'f')!
1049 fast_hash_expr _ _ = 1
1051 fast_hash_type :: HashEnv -> Type -> Word32
1052 fast_hash_type env ty
1053 | Just tv <- getTyVar_maybe ty = hashVar env tv
1054 | Just (tc,tys) <- splitTyConApp_maybe ty = let hash_tc = fromIntegral (hashName (tyConName tc))
1055 in foldr (\t n -> fast_hash_type env t + n) hash_tc tys
1058 extend_env :: HashEnv -> Var -> (Int, VarEnv Int)
1059 extend_env (n,env) b = (n+1, extendVarEnv env b n)
1061 hashVar :: HashEnv -> Var -> Word32
1063 = fromIntegral (lookupVarEnv env v `orElse` hashName (idName v))
1066 %************************************************************************
1068 \subsection{Determining non-updatable right-hand-sides}
1070 %************************************************************************
1072 Top-level constructor applications can usually be allocated
1073 statically, but they can't if the constructor, or any of the
1074 arguments, come from another DLL (because we can't refer to static
1075 labels in other DLLs).
1077 If this happens we simply make the RHS into an updatable thunk,
1078 and 'execute' it rather than allocating it statically.
1081 -- | This function is called only on *top-level* right-hand sides.
1082 -- Returns @True@ if the RHS can be allocated statically in the output,
1083 -- with no thunks involved at all.
1084 rhsIsStatic :: PackageId -> CoreExpr -> Bool
1085 -- It's called (i) in TidyPgm.hasCafRefs to decide if the rhs is, or
1086 -- refers to, CAFs; (ii) in CoreToStg to decide whether to put an
1087 -- update flag on it and (iii) in DsExpr to decide how to expand
1090 -- The basic idea is that rhsIsStatic returns True only if the RHS is
1091 -- (a) a value lambda
1092 -- (b) a saturated constructor application with static args
1094 -- BUT watch out for
1095 -- (i) Any cross-DLL references kill static-ness completely
1096 -- because they must be 'executed' not statically allocated
1097 -- ("DLL" here really only refers to Windows DLLs, on other platforms,
1098 -- this is not necessary)
1100 -- (ii) We treat partial applications as redexes, because in fact we
1101 -- make a thunk for them that runs and builds a PAP
1102 -- at run-time. The only appliations that are treated as
1103 -- static are *saturated* applications of constructors.
1105 -- We used to try to be clever with nested structures like this:
1106 -- ys = (:) w ((:) w [])
1107 -- on the grounds that CorePrep will flatten ANF-ise it later.
1108 -- But supporting this special case made the function much more
1109 -- complicated, because the special case only applies if there are no
1110 -- enclosing type lambdas:
1111 -- ys = /\ a -> Foo (Baz ([] a))
1112 -- Here the nested (Baz []) won't float out to top level in CorePrep.
1114 -- But in fact, even without -O, nested structures at top level are
1115 -- flattened by the simplifier, so we don't need to be super-clever here.
1119 -- f = \x::Int. x+7 TRUE
1120 -- p = (True,False) TRUE
1122 -- d = (fst p, False) FALSE because there's a redex inside
1123 -- (this particular one doesn't happen but...)
1125 -- h = D# (1.0## /## 2.0##) FALSE (redex again)
1126 -- n = /\a. Nil a TRUE
1128 -- t = /\a. (:) (case w a of ...) (Nil a) FALSE (redex)
1131 -- This is a bit like CoreUtils.exprIsHNF, with the following differences:
1132 -- a) scc "foo" (\x -> ...) is updatable (so we catch the right SCC)
1134 -- b) (C x xs), where C is a contructors is updatable if the application is
1137 -- c) don't look through unfolding of f in (f x).
1139 rhsIsStatic _this_pkg rhs = is_static False rhs
1141 is_static :: Bool -- True <=> in a constructor argument; must be atomic
1144 is_static False (Lam b e) = isRuntimeVar b || is_static False e
1146 is_static _ (Note (SCC _) _) = False
1147 is_static in_arg (Note _ e) = is_static in_arg e
1148 is_static in_arg (Cast e _) = is_static in_arg e
1150 is_static _ (Lit lit)
1152 MachLabel _ _ _ -> False
1154 -- A MachLabel (foreign import "&foo") in an argument
1155 -- prevents a constructor application from being static. The
1156 -- reason is that it might give rise to unresolvable symbols
1157 -- in the object file: under Linux, references to "weak"
1158 -- symbols from the data segment give rise to "unresolvable
1159 -- relocation" errors at link time This might be due to a bug
1160 -- in the linker, but we'll work around it here anyway.
1163 is_static in_arg other_expr = go other_expr 0
1165 go (Var f) n_val_args
1166 #if mingw32_TARGET_OS
1167 | not (isDllName _this_pkg (idName f))
1169 = saturated_data_con f n_val_args
1170 || (in_arg && n_val_args == 0)
1171 -- A naked un-applied variable is *not* deemed a static RHS
1173 -- Reason: better to update so that the indirection gets shorted
1174 -- out, and the true value will be seen
1175 -- NB: if you change this, you'll break the invariant that THUNK_STATICs
1176 -- are always updatable. If you do so, make sure that non-updatable
1177 -- ones have enough space for their static link field!
1179 go (App f a) n_val_args
1180 | isTypeArg a = go f n_val_args
1181 | not in_arg && is_static True a = go f (n_val_args + 1)
1182 -- The (not in_arg) checks that we aren't in a constructor argument;
1183 -- if we are, we don't allow (value) applications of any sort
1185 -- NB. In case you wonder, args are sometimes not atomic. eg.
1186 -- x = D# (1.0## /## 2.0##)
1187 -- can't float because /## can fail.
1189 go (Note (SCC _) _) _ = False
1190 go (Note _ f) n_val_args = go f n_val_args
1191 go (Cast e _) n_val_args = go e n_val_args
1195 saturated_data_con f n_val_args
1196 = case isDataConWorkId_maybe f of
1197 Just dc -> n_val_args == dataConRepArity dc