2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[SimplUtils]{The simplifier utilities}
8 simplBinder, simplBinders, simplIds,
9 tryRhsTyLam, tryEtaExpansion,
12 -- The continuation type
13 SimplCont(..), DupFlag(..), contIsDupable, contResultType,
14 countValArgs, countArgs, mkRhsStop, mkStop,
15 getContArgs, interestingCallContext, interestingArg, isStrictType, discardInline
19 #include "HsVersions.h"
21 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..),
22 opt_SimplDoLambdaEtaExpansion, opt_SimplCaseMerge, opt_DictsStrict,
26 import CoreUtils ( exprIsTrivial, cheapEqExpr, exprType, exprIsCheap,
27 etaExpand, exprEtaExpandArity, bindNonRec, mkCoerce,
30 import Subst ( InScopeSet, mkSubst, substBndrs, substBndr, substIds, substExpr )
31 import Id ( idType, idName,
32 idUnfolding, idStrictness,
35 import IdInfo ( StrictnessInfo(..) )
36 import Maybes ( maybeToBool, catMaybes )
37 import Name ( setNameUnique )
38 import Demand ( isStrict )
40 import Type ( Type, mkForAllTys, seqType, repType,
41 splitTyConApp_maybe, tyConAppArgs, mkTyVarTys,
42 isDictTy, isDataType, isUnLiftedType,
45 import TyCon ( tyConDataConsIfAvailable )
46 import DataCon ( dataConRepArity )
47 import VarEnv ( SubstEnv )
48 import Util ( lengthExceeds )
53 %************************************************************************
55 \subsection{The continuation data type}
57 %************************************************************************
60 data SimplCont -- Strict contexts
61 = Stop OutType -- Type of the result
62 Bool -- True => This is the RHS of a thunk whose type suggests
63 -- that update-in-place would be possible
64 -- (This makes the inliner a little keener.)
66 | CoerceIt OutType -- The To-type, simplified
69 | InlinePlease -- This continuation makes a function very
70 SimplCont -- keen to inline itelf
73 InExpr SubstEnv -- The argument, as yet unsimplified,
74 SimplCont -- and its subst-env
77 InId [InAlt] SubstEnv -- The case binder, alts, and subst-env
80 | ArgOf DupFlag -- An arbitrary strict context: the argument
81 -- of a strict function, or a primitive-arg fn
83 OutType -- cont_ty: the type of the expression being sought by the context
84 -- f (error "foo") ==> coerce t (error "foo")
86 -- We need to know the type t, to which to coerce.
87 (OutExpr -> SimplM OutExprStuff) -- What to do with the result
88 -- The result expression in the OutExprStuff has type cont_ty
90 instance Outputable SimplCont where
91 ppr (Stop _ _) = ptext SLIT("Stop")
92 ppr (ApplyTo dup arg se cont) = (ptext SLIT("ApplyTo") <+> ppr dup <+> ppr arg) $$ ppr cont
93 ppr (ArgOf dup _ _) = ptext SLIT("ArgOf...") <+> ppr dup
94 ppr (Select dup bndr alts se cont) = (ptext SLIT("Select") <+> ppr dup <+> ppr bndr) $$
95 (nest 4 (ppr alts)) $$ ppr cont
96 ppr (CoerceIt ty cont) = (ptext SLIT("CoerceIt") <+> ppr ty) $$ ppr cont
97 ppr (InlinePlease cont) = ptext SLIT("InlinePlease") $$ ppr cont
99 data DupFlag = OkToDup | NoDup
101 instance Outputable DupFlag where
102 ppr OkToDup = ptext SLIT("ok")
103 ppr NoDup = ptext SLIT("nodup")
107 mkRhsStop, mkStop :: OutType -> SimplCont
108 mkStop ty = Stop ty False
109 mkRhsStop ty = Stop ty (canUpdateInPlace ty)
113 contIsDupable :: SimplCont -> Bool
114 contIsDupable (Stop _ _) = True
115 contIsDupable (ApplyTo OkToDup _ _ _) = True
116 contIsDupable (ArgOf OkToDup _ _) = True
117 contIsDupable (Select OkToDup _ _ _ _) = True
118 contIsDupable (CoerceIt _ cont) = contIsDupable cont
119 contIsDupable (InlinePlease cont) = contIsDupable cont
120 contIsDupable other = False
123 discardInline :: SimplCont -> SimplCont
124 discardInline (InlinePlease cont) = cont
125 discardInline (ApplyTo d e s cont) = ApplyTo d e s (discardInline cont)
126 discardInline cont = cont
129 discardableCont :: SimplCont -> Bool
130 discardableCont (Stop _ _) = False
131 discardableCont (CoerceIt _ cont) = discardableCont cont
132 discardableCont (InlinePlease cont) = discardableCont cont
133 discardableCont other = True
135 discardCont :: SimplCont -- A continuation, expecting
136 -> SimplCont -- Replace the continuation with a suitable coerce
137 discardCont cont = case cont of
139 other -> CoerceIt to_ty (mkStop to_ty)
141 to_ty = contResultType cont
144 contResultType :: SimplCont -> OutType
145 contResultType (Stop to_ty _) = to_ty
146 contResultType (ArgOf _ to_ty _) = to_ty
147 contResultType (ApplyTo _ _ _ cont) = contResultType cont
148 contResultType (CoerceIt _ cont) = contResultType cont
149 contResultType (InlinePlease cont) = contResultType cont
150 contResultType (Select _ _ _ _ cont) = contResultType cont
153 countValArgs :: SimplCont -> Int
154 countValArgs (ApplyTo _ (Type ty) se cont) = countValArgs cont
155 countValArgs (ApplyTo _ val_arg se cont) = 1 + countValArgs cont
156 countValArgs other = 0
158 countArgs :: SimplCont -> Int
159 countArgs (ApplyTo _ arg se cont) = 1 + countArgs cont
165 getContArgs :: OutId -> SimplCont
166 -> SimplM ([(InExpr, SubstEnv, Bool)], -- Arguments; the Bool is true for strict args
167 SimplCont, -- Remaining continuation
168 Bool) -- Whether we came across an InlineCall
169 -- getContArgs id k = (args, k', inl)
170 -- args are the leading ApplyTo items in k
171 -- (i.e. outermost comes first)
172 -- augmented with demand info from the functionn
173 getContArgs fun orig_cont
174 = getSwitchChecker `thenSmpl` \ chkr ->
176 -- Ignore strictness info if the no-case-of-case
177 -- flag is on. Strictness changes evaluation order
178 -- and that can change full laziness
179 stricts | switchIsOn chkr NoCaseOfCase = vanilla_stricts
180 | otherwise = computed_stricts
182 go [] stricts False orig_cont
184 ----------------------------
187 go acc ss inl (ApplyTo _ arg@(Type _) se cont)
188 = go ((arg,se,False) : acc) ss inl cont
189 -- NB: don't bother to instantiate the function type
192 go acc (s:ss) inl (ApplyTo _ arg se cont)
193 = go ((arg,se,s) : acc) ss inl cont
195 -- An Inline continuation
196 go acc ss inl (InlinePlease cont)
197 = go acc ss True cont
199 -- We're run out of arguments, or else we've run out of demands
200 -- The latter only happens if the result is guaranteed bottom
201 -- This is the case for
202 -- * case (error "hello") of { ... }
203 -- * (error "Hello") arg
204 -- * f (error "Hello") where f is strict
207 | null ss && discardableCont cont = tick BottomFound `thenSmpl_`
208 returnSmpl (reverse acc, discardCont cont, inl)
209 | otherwise = returnSmpl (reverse acc, cont, inl)
211 ----------------------------
212 vanilla_stricts, computed_stricts :: [Bool]
213 vanilla_stricts = repeat False
214 computed_stricts = zipWith (||) fun_stricts arg_stricts
216 ----------------------------
217 (val_arg_tys, _) = splitRepFunTys (idType fun)
218 arg_stricts = map isStrictType val_arg_tys ++ repeat False
219 -- These argument types are used as a cheap and cheerful way to find
220 -- unboxed arguments, which must be strict. But it's an InType
221 -- and so there might be a type variable where we expect a function
222 -- type (the substitution hasn't happened yet). And we don't bother
223 -- doing the type applications for a polymorphic function.
224 -- Hence the split*Rep*FunTys
226 ----------------------------
227 -- If fun_stricts is finite, it means the function returns bottom
228 -- after that number of value args have been consumed
229 -- Otherwise it's infinite, extended with False
231 = case idStrictness fun of
232 StrictnessInfo demands result_bot
233 | not (demands `lengthExceeds` countValArgs orig_cont)
234 -> -- Enough args, use the strictness given.
235 -- For bottoming functions we used to pretend that the arg
236 -- is lazy, so that we don't treat the arg as an
237 -- interesting context. This avoids substituting
238 -- top-level bindings for (say) strings into
239 -- calls to error. But now we are more careful about
240 -- inlining lone variables, so its ok (see SimplUtils.analyseCont)
242 map isStrict demands -- Finite => result is bottom
244 map isStrict demands ++ vanilla_stricts
246 other -> vanilla_stricts -- Not enough args, or no strictness
250 isStrictType :: Type -> Bool
251 -- isStrictType computes whether an argument (or let RHS) should
252 -- be computed strictly or lazily, based only on its type
254 | isUnLiftedType ty = True
255 | opt_DictsStrict && isDictTy ty && isDataType ty = True
257 -- Return true only for dictionary types where the dictionary
258 -- has more than one component (else we risk poking on the component
259 -- of a newtype dictionary)
262 interestingArg :: InScopeSet -> InExpr -> SubstEnv -> Bool
263 -- An argument is interesting if it has *some* structure
264 -- We are here trying to avoid unfolding a function that
265 -- is applied only to variables that have no unfolding
266 -- (i.e. they are probably lambda bound): f x y z
267 -- There is little point in inlining f here.
268 interestingArg in_scope arg subst
269 = analyse (substExpr (mkSubst in_scope subst) arg)
270 -- 'analyse' only looks at the top part of the result
271 -- and substExpr is lazy, so this isn't nearly as brutal
274 analyse (Var v) = hasSomeUnfolding (idUnfolding v)
275 -- Was: isValueUnfolding (idUnfolding v')
276 -- But that seems over-pessimistic
277 analyse (Type _) = False
278 analyse (App fn (Type _)) = analyse fn
279 analyse (Note _ a) = analyse a
281 -- Consider let x = 3 in f x
282 -- The substitution will contain (x -> ContEx 3), and we want to
283 -- to say that x is an interesting argument.
284 -- But consider also (\x. f x y) y
285 -- The substitution will contain (x -> ContEx y), and we want to say
286 -- that x is not interesting (assuming y has no unfolding)
289 Comment about interestingCallContext
290 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291 We want to avoid inlining an expression where there can't possibly be
292 any gain, such as in an argument position. Hence, if the continuation
293 is interesting (eg. a case scrutinee, application etc.) then we
294 inline, otherwise we don't.
296 Previously some_benefit used to return True only if the variable was
297 applied to some value arguments. This didn't work:
299 let x = _coerce_ (T Int) Int (I# 3) in
300 case _coerce_ Int (T Int) x of
303 we want to inline x, but can't see that it's a constructor in a case
304 scrutinee position, and some_benefit is False.
308 dMonadST = _/\_ t -> :Monad (g1 _@_ t, g2 _@_ t, g3 _@_ t)
310 .... case dMonadST _@_ x0 of (a,b,c) -> ....
312 we'd really like to inline dMonadST here, but we *don't* want to
313 inline if the case expression is just
315 case x of y { DEFAULT -> ... }
317 since we can just eliminate this case instead (x is in WHNF). Similar
318 applies when x is bound to a lambda expression. Hence
319 contIsInteresting looks for case expressions with just a single
323 interestingCallContext :: Bool -- False <=> no args at all
324 -> Bool -- False <=> no value args
326 -- The "lone-variable" case is important. I spent ages
327 -- messing about with unsatisfactory varaints, but this is nice.
328 -- The idea is that if a variable appear all alone
329 -- as an arg of lazy fn, or rhs Stop
330 -- as scrutinee of a case Select
331 -- as arg of a strict fn ArgOf
332 -- then we should not inline it (unless there is some other reason,
333 -- e.g. is is the sole occurrence). We achieve this by making
334 -- interestingCallContext return False for a lone variable.
336 -- Why? At least in the case-scrutinee situation, turning
337 -- let x = (a,b) in case x of y -> ...
339 -- let x = (a,b) in case (a,b) of y -> ...
341 -- let x = (a,b) in let y = (a,b) in ...
342 -- is bad if the binding for x will remain.
344 -- Another example: I discovered that strings
345 -- were getting inlined straight back into applications of 'error'
346 -- because the latter is strict.
348 -- f = \x -> ...(error s)...
350 -- Fundamentally such contexts should not ecourage inlining becuase
351 -- the context can ``see'' the unfolding of the variable (e.g. case or a RULE)
352 -- so there's no gain.
354 -- However, even a type application or coercion isn't a lone variable.
356 -- case $fMonadST @ RealWorld of { :DMonad a b c -> c }
357 -- We had better inline that sucker! The case won't see through it.
359 -- For now, I'm treating treating a variable applied to types
360 -- in a *lazy* context "lone". The motivating example was
362 -- g = /\a. \y. h (f a)
363 -- There's no advantage in inlining f here, and perhaps
364 -- a significant disadvantage. Hence some_val_args in the Stop case
366 interestingCallContext some_args some_val_args cont
369 interesting (InlinePlease _) = True
370 interesting (Select _ _ _ _ _) = some_args
371 interesting (ApplyTo _ _ _ _) = some_args -- Can happen if we have (coerce t (f x)) y
372 interesting (ArgOf _ _ _) = some_val_args
373 interesting (Stop ty upd_in_place) = some_val_args && upd_in_place
374 interesting (CoerceIt _ cont) = interesting cont
375 -- If this call is the arg of a strict function, the context
376 -- is a bit interesting. If we inline here, we may get useful
377 -- evaluation information to avoid repeated evals: e.g.
379 -- Here the contIsInteresting makes the '*' keener to inline,
380 -- which in turn exposes a constructor which makes the '+' inline.
381 -- Assuming that +,* aren't small enough to inline regardless.
383 -- It's also very important to inline in a strict context for things
386 -- Here, the context of (f x) is strict, and if f's unfolding is
387 -- a build it's *great* to inline it here. So we must ensure that
388 -- the context for (f x) is not totally uninteresting.
392 canUpdateInPlace :: Type -> Bool
393 -- Consider let x = <wurble> in ...
394 -- If <wurble> returns an explicit constructor, we might be able
395 -- to do update in place. So we treat even a thunk RHS context
396 -- as interesting if update in place is possible. We approximate
397 -- this by seeing if the type has a single constructor with a
398 -- small arity. But arity zero isn't good -- we share the single copy
399 -- for that case, so no point in sharing.
401 -- Note the repType: we want to look through newtypes for this purpose
404 | not opt_UF_UpdateInPlace = False
406 = case splitTyConApp_maybe (repType ty) of {
410 case tyConDataConsIfAvailable tycon of
411 [dc] -> arity == 1 || arity == 2
413 arity = dataConRepArity dc
420 %************************************************************************
422 \section{Dealing with a single binder}
424 %************************************************************************
427 simplBinders :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
428 simplBinders bndrs thing_inside
429 = getSubst `thenSmpl` \ subst ->
431 (subst', bndrs') = substBndrs subst bndrs
433 seqBndrs bndrs' `seq`
434 setSubst subst' (thing_inside bndrs')
436 simplBinder :: InBinder -> (OutBinder -> SimplM a) -> SimplM a
437 simplBinder bndr thing_inside
438 = getSubst `thenSmpl` \ subst ->
440 (subst', bndr') = substBndr subst bndr
443 setSubst subst' (thing_inside bndr')
446 -- Same semantics as simplBinders, but a little less
447 -- plumbing and hence a little more efficient.
448 -- Maybe not worth the candle?
449 simplIds :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
450 simplIds ids thing_inside
451 = getSubst `thenSmpl` \ subst ->
453 (subst', bndrs') = substIds subst ids
455 seqBndrs bndrs' `seq`
456 setSubst subst' (thing_inside bndrs')
459 seqBndrs (b:bs) = seqBndr b `seq` seqBndrs bs
461 seqBndr b | isTyVar b = b `seq` ()
462 | otherwise = seqType (idType b) `seq`
468 %************************************************************************
470 \subsection{Local tyvar-lifting}
472 %************************************************************************
474 mkRhsTyLam tries this transformation, when the big lambda appears as
475 the RHS of a let(rec) binding:
477 /\abc -> let(rec) x = e in b
479 let(rec) x' = /\abc -> let x = x' a b c in e
481 /\abc -> let x = x' a b c in b
483 This is good because it can turn things like:
485 let f = /\a -> letrec g = ... g ... in g
487 letrec g' = /\a -> ... g' a ...
491 which is better. In effect, it means that big lambdas don't impede
494 This optimisation is CRUCIAL in eliminating the junk introduced by
495 desugaring mutually recursive definitions. Don't eliminate it lightly!
497 So far as the implementation is concerned:
499 Invariant: go F e = /\tvs -> F e
503 = Let x' = /\tvs -> F e
507 G = F . Let x = x' tvs
509 go F (Letrec xi=ei in b)
510 = Letrec {xi' = /\tvs -> G ei}
514 G = F . Let {xi = xi' tvs}
516 [May 1999] If we do this transformation *regardless* then we can
517 end up with some pretty silly stuff. For example,
520 st = /\ s -> let { x1=r1 ; x2=r2 } in ...
525 st = /\s -> ...[y1 s/x1, y2 s/x2]
528 Unless the "..." is a WHNF there is really no point in doing this.
529 Indeed it can make things worse. Suppose x1 is used strictly,
532 x1* = case f y of { (a,b) -> e }
534 If we abstract this wrt the tyvar we then can't do the case inline
535 as we would normally do.
539 tryRhsTyLam :: OutExpr -> SimplM ([OutBind], OutExpr)
541 tryRhsTyLam rhs -- Only does something if there's a let
542 | null tyvars || not (worth_it body) -- inside a type lambda,
543 = returnSmpl ([], rhs) -- and a WHNF inside that
546 = go (\x -> x) body `thenSmpl` \ (binds, body') ->
547 returnSmpl (binds, mkLams tyvars body')
550 (tyvars, body) = collectTyBinders rhs
552 worth_it e@(Let _ _) = whnf_in_middle e
555 whnf_in_middle (Let (NonRec x rhs) e) | isUnLiftedType (idType x) = False
556 whnf_in_middle (Let _ e) = whnf_in_middle e
557 whnf_in_middle e = exprIsCheap e
559 go fn (Let bind@(NonRec var rhs) body)
561 = go (fn . Let bind) body
563 go fn (Let (NonRec var rhs) body)
564 = mk_poly tyvars_here var `thenSmpl` \ (var', rhs') ->
565 go (fn . Let (mk_silly_bind var rhs')) body `thenSmpl` \ (binds, body') ->
566 returnSmpl (NonRec var' (mkLams tyvars_here (fn rhs)) : binds, body')
570 -- main_tyvar_set = mkVarSet tyvars
571 -- var_ty = idType var
572 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfType var_ty)
573 -- tyvars_here was an attempt to reduce the number of tyvars
574 -- wrt which the new binding is abstracted. But the naive
575 -- approach of abstract wrt the tyvars free in the Id's type
577 -- /\ a b -> let t :: (a,b) = (e1, e2)
580 -- Here, b isn't free in x's type, but we must nevertheless
581 -- abstract wrt b as well, because t's type mentions b.
582 -- Since t is floated too, we'd end up with the bogus:
583 -- poly_t = /\ a b -> (e1, e2)
584 -- poly_x = /\ a -> fst (poly_t a *b*)
585 -- So for now we adopt the even more naive approach of
586 -- abstracting wrt *all* the tyvars. We'll see if that
587 -- gives rise to problems. SLPJ June 98
589 go fn (Let (Rec prs) body)
590 = mapAndUnzipSmpl (mk_poly tyvars_here) vars `thenSmpl` \ (vars', rhss') ->
592 gn body = fn (foldr Let body (zipWith mk_silly_bind vars rhss'))
593 new_bind = Rec (vars' `zip` [mkLams tyvars_here (gn rhs) | rhs <- rhss])
595 go gn body `thenSmpl` \ (binds, body') ->
596 returnSmpl (new_bind : binds, body')
598 (vars,rhss) = unzip prs
600 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfTypes var_tys)
601 -- var_tys = map idType vars
602 -- See notes with tyvars_here above
604 go fn body = returnSmpl ([], fn body)
606 mk_poly tyvars_here var
607 = getUniqueSmpl `thenSmpl` \ uniq ->
609 poly_name = setNameUnique (idName var) uniq -- Keep same name
610 poly_ty = mkForAllTys tyvars_here (idType var) -- But new type of course
611 poly_id = mkVanillaId poly_name poly_ty
613 -- In the olden days, it was crucial to copy the occInfo of the original var,
614 -- because we were looking at occurrence-analysed but as yet unsimplified code!
615 -- In particular, we mustn't lose the loop breakers. BUT NOW we are looking
616 -- at already simplified code, so it doesn't matter
618 -- It's even right to retain single-occurrence or dead-var info:
619 -- Suppose we started with /\a -> let x = E in B
620 -- where x occurs once in B. Then we transform to:
621 -- let x' = /\a -> E in /\a -> let x* = x' a in B
622 -- where x* has an INLINE prag on it. Now, once x* is inlined,
623 -- the occurrences of x' will be just the occurrences originally
626 returnSmpl (poly_id, mkTyApps (Var poly_id) (mkTyVarTys tyvars_here))
628 mk_silly_bind var rhs = NonRec var (Note InlineMe rhs)
629 -- Suppose we start with:
631 -- x = /\ a -> let g = G in E
633 -- Then we'll float to get
635 -- x = let poly_g = /\ a -> G
636 -- in /\ a -> let g = poly_g a in E
638 -- But now the occurrence analyser will see just one occurrence
639 -- of poly_g, not inside a lambda, so the simplifier will
640 -- PreInlineUnconditionally poly_g back into g! Badk to square 1!
641 -- (I used to think that the "don't inline lone occurrences" stuff
642 -- would stop this happening, but since it's the *only* occurrence,
643 -- PreInlineUnconditionally kicks in first!)
645 -- Solution: put an INLINE note on g's RHS, so that poly_g seems
646 -- to appear many times. (NB: mkInlineMe eliminates
647 -- such notes on trivial RHSs, so do it manually.)
651 %************************************************************************
653 \subsection{Eta expansion}
655 %************************************************************************
657 Try eta expansion for RHSs
660 Case 1 f = \x1..xn -> N ==> f = \x1..xn y1..ym -> N y1..ym
663 Case 2 f = N E1..En ==> z1=E1
666 f = \y1..ym -> N z1..zn y1..ym
668 where (in both cases)
670 * The xi can include type variables
672 * The yi are all value variables
674 * N is a NORMAL FORM (i.e. no redexes anywhere)
675 wanting a suitable number of extra args.
677 * the Ei must not have unlifted type
679 There is no point in looking for a combination of the two, because
680 that would leave use with some lets sandwiched between lambdas; that's
681 what the final test in the first equation is for.
684 tryEtaExpansion :: OutExpr -> OutType -> SimplM ([OutBind], OutExpr)
685 tryEtaExpansion rhs rhs_ty
686 | not opt_SimplDoLambdaEtaExpansion -- Not if switched off
687 || exprIsTrivial rhs -- Not if RHS is trivial
688 || final_arity == 0 -- Not if arity is zero
689 = returnSmpl ([], rhs)
691 | n_val_args == 0 && not arity_is_manifest
692 = -- Some lambdas but not enough: case 1
693 getUniqSupplySmpl `thenSmpl` \ us ->
694 returnSmpl ([], etaExpand final_arity us rhs rhs_ty)
696 | n_val_args > 0 && not (any cant_bind arg_infos)
697 = -- Partial application: case 2
698 mapAndUnzipSmpl bind_z_arg arg_infos `thenSmpl` \ (maybe_z_binds, z_args) ->
699 getUniqSupplySmpl `thenSmpl` \ us ->
700 returnSmpl (catMaybes maybe_z_binds,
701 etaExpand final_arity us (mkApps fun z_args) rhs_ty)
704 = returnSmpl ([], rhs)
706 (fun, args) = collectArgs rhs
707 n_val_args = valArgCount args
708 (fun_arity, arity_is_manifest) = exprEtaExpandArity fun
709 final_arity = 0 `max` (fun_arity - n_val_args)
710 arg_infos = [(arg, exprType arg, exprIsTrivial arg) | arg <- args]
711 cant_bind (_, ty, triv) = not triv && isUnLiftedType ty
713 bind_z_arg (arg, arg_ty, trivial_arg)
714 | trivial_arg = returnSmpl (Nothing, arg)
715 | otherwise = newId SLIT("z") arg_ty $ \ z ->
716 returnSmpl (Just (NonRec z arg), Var z)
720 %************************************************************************
722 \subsection{Case absorption and identity-case elimination}
724 %************************************************************************
727 mkCase :: OutExpr -> OutId -> [OutAlt] -> SimplM OutExpr
730 @mkCase@ tries the following transformation (if possible):
732 case e of b { ==> case e of b {
733 p1 -> rhs1 p1 -> rhs1
735 pm -> rhsm pm -> rhsm
736 _ -> case b of b' { pn -> rhsn[b/b'] {or (alg) let b=b' in rhsn}
737 {or (prim) case b of b' { _ -> rhsn}}
740 po -> rhso _ -> rhsd[b/b'] {or let b'=b in rhsd}
744 which merges two cases in one case when -- the default alternative of
745 the outer case scrutises the same variable as the outer case This
746 transformation is called Case Merging. It avoids that the same
747 variable is scrutinised multiple times.
750 mkCase scrut outer_bndr outer_alts
752 && maybeToBool maybe_case_in_default
754 = tick (CaseMerge outer_bndr) `thenSmpl_`
755 returnSmpl (Case scrut outer_bndr new_alts)
756 -- Warning: don't call mkCase recursively!
757 -- Firstly, there's no point, because inner alts have already had
758 -- mkCase applied to them, so they won't have a case in their default
759 -- Secondly, if you do, you get an infinite loop, because the bindNonRec
760 -- in munge_rhs puts a case into the DEFAULT branch!
762 new_alts = outer_alts_without_deflt ++ munged_inner_alts
763 maybe_case_in_default = case findDefault outer_alts of
764 (outer_alts_without_default,
765 Just (Case (Var scrut_var) inner_bndr inner_alts))
767 | outer_bndr == scrut_var
768 -> Just (outer_alts_without_default, inner_bndr, inner_alts)
771 Just (outer_alts_without_deflt, inner_bndr, inner_alts) = maybe_case_in_default
773 -- Eliminate any inner alts which are shadowed by the outer ones
774 outer_cons = [con | (con,_,_) <- outer_alts_without_deflt]
776 munged_inner_alts = [ (con, args, munge_rhs rhs)
777 | (con, args, rhs) <- inner_alts,
778 not (con `elem` outer_cons) -- Eliminate shadowed inner alts
780 munge_rhs rhs = bindNonRec inner_bndr (Var outer_bndr) rhs
783 Now the identity-case transformation:
792 mkCase scrut case_bndr alts
793 | all identity_alt alts
794 = tick (CaseIdentity case_bndr) `thenSmpl_`
795 returnSmpl (re_note scrut)
797 identity_alt (con, args, rhs) = de_note rhs `cheapEqExpr` identity_rhs con args
799 identity_rhs (DataAlt con) args = mkConApp con (arg_tys ++ map varToCoreExpr args)
800 identity_rhs (LitAlt lit) _ = Lit lit
801 identity_rhs DEFAULT _ = Var case_bndr
803 arg_tys = map Type (tyConAppArgs (idType case_bndr))
806 -- case coerce T e of x { _ -> coerce T' x }
807 -- And we definitely want to eliminate this case!
808 -- So we throw away notes from the RHS, and reconstruct
809 -- (at least an approximation) at the other end
810 de_note (Note _ e) = de_note e
813 -- re_note wraps a coerce if it might be necessary
814 re_note scrut = case head alts of
815 (_,_,rhs1@(Note _ _)) -> mkCoerce (exprType rhs1) (idType case_bndr) scrut
822 mkCase other_scrut case_bndr other_alts
823 = returnSmpl (Case other_scrut case_bndr other_alts)