[project @ 2001-08-28 10:06:29 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / SimplUtils.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[SimplUtils]{The simplifier utilities}
5
6 \begin{code}
7 module SimplUtils (
8         simplBinder, simplBinders, simplRecIds, simplLetId,
9         tryRhsTyLam, tryEtaExpansion,
10         mkCase,
11
12         -- The continuation type
13         SimplCont(..), DupFlag(..), contIsDupable, contResultType,
14         countValArgs, countArgs, mkRhsStop, mkStop,
15         getContArgs, interestingCallContext, interestingArg, isStrictType, discardInline
16
17     ) where
18
19 #include "HsVersions.h"
20
21 import CmdLineOpts      ( switchIsOn, SimplifierSwitch(..),
22                           opt_SimplDoLambdaEtaExpansion, opt_SimplCaseMerge, 
23                           opt_UF_UpdateInPlace
24                         )
25 import CoreSyn
26 import CoreUtils        ( exprIsTrivial, cheapEqExpr, exprType, exprIsCheap, 
27                           etaExpand, exprEtaExpandArity, bindNonRec, mkCoerce,
28                           findDefault
29                         )
30 import Subst            ( InScopeSet, mkSubst, substExpr )
31 import qualified Subst  ( simplBndrs, simplBndr, simplLetId )
32 import Id               ( idType, idName, 
33                           idUnfolding, idNewStrictness,
34                           mkLocalId, idInfo
35                         )
36 import IdInfo           ( StrictnessInfo(..) )
37 import Maybes           ( maybeToBool, catMaybes )
38 import Name             ( setNameUnique )
39 import NewDemand        ( isStrictDmd, isBotRes, splitStrictSig )
40 import SimplMonad
41 import Type             ( Type, mkForAllTys, seqType, 
42                           splitTyConApp_maybe, tyConAppArgs, mkTyVarTys,
43                           isUnLiftedType, isStrictType,
44                           splitRepFunTys
45                         )
46 import TyCon            ( tyConDataConsIfAvailable )
47 import DataCon          ( dataConRepArity )
48 import VarEnv           ( SubstEnv )
49 import Util             ( lengthExceeds, mapAccumL )
50 import Outputable
51 \end{code}
52
53
54 %************************************************************************
55 %*                                                                      *
56 \subsection{The continuation data type}
57 %*                                                                      *
58 %************************************************************************
59
60 \begin{code}
61 data SimplCont          -- Strict contexts
62   = Stop     OutType            -- Type of the result
63              Bool               -- True => This is the RHS of a thunk whose type suggests
64                                 --         that update-in-place would be possible
65                                 --         (This makes the inliner a little keener.)
66
67   | CoerceIt OutType                    -- The To-type, simplified
68              SimplCont
69
70   | InlinePlease                        -- This continuation makes a function very
71              SimplCont                  -- keen to inline itelf
72
73   | ApplyTo  DupFlag 
74              InExpr SubstEnv            -- The argument, as yet unsimplified, 
75              SimplCont                  -- and its subst-env
76
77   | Select   DupFlag 
78              InId [InAlt] SubstEnv      -- The case binder, alts, and subst-env
79              SimplCont
80
81   | ArgOf    DupFlag            -- An arbitrary strict context: the argument 
82                                 --      of a strict function, or a primitive-arg fn
83                                 --      or a PrimOp
84              OutType            -- cont_ty: the type of the expression being sought by the context
85                                 --      f (error "foo") ==> coerce t (error "foo")
86                                 -- when f is strict
87                                 -- We need to know the type t, to which to coerce.
88              (OutExpr -> SimplM OutExprStuff)   -- What to do with the result
89                                 -- The result expression in the OutExprStuff has type cont_ty
90
91 instance Outputable SimplCont where
92   ppr (Stop _ _)                     = ptext SLIT("Stop")
93   ppr (ApplyTo dup arg se cont)      = (ptext SLIT("ApplyTo") <+> ppr dup <+> ppr arg) $$ ppr cont
94   ppr (ArgOf   dup _ _)              = ptext SLIT("ArgOf...") <+> ppr dup
95   ppr (Select dup bndr alts se cont) = (ptext SLIT("Select") <+> ppr dup <+> ppr bndr) $$ 
96                                        (nest 4 (ppr alts)) $$ ppr cont
97   ppr (CoerceIt ty cont)             = (ptext SLIT("CoerceIt") <+> ppr ty) $$ ppr cont
98   ppr (InlinePlease cont)            = ptext SLIT("InlinePlease") $$ ppr cont
99
100 data DupFlag = OkToDup | NoDup
101
102 instance Outputable DupFlag where
103   ppr OkToDup = ptext SLIT("ok")
104   ppr NoDup   = ptext SLIT("nodup")
105
106
107 -------------------
108 mkRhsStop, mkStop :: OutType -> SimplCont
109 mkStop    ty = Stop ty False
110 mkRhsStop ty = Stop ty (canUpdateInPlace ty)
111
112
113 -------------------
114 contIsDupable :: SimplCont -> Bool
115 contIsDupable (Stop _ _)                 = True
116 contIsDupable (ApplyTo  OkToDup _ _ _)   = True
117 contIsDupable (ArgOf    OkToDup _ _)     = True
118 contIsDupable (Select   OkToDup _ _ _ _) = True
119 contIsDupable (CoerceIt _ cont)          = contIsDupable cont
120 contIsDupable (InlinePlease cont)        = contIsDupable cont
121 contIsDupable other                      = False
122
123 -------------------
124 discardInline :: SimplCont -> SimplCont
125 discardInline (InlinePlease cont)  = cont
126 discardInline (ApplyTo d e s cont) = ApplyTo d e s (discardInline cont)
127 discardInline cont                 = cont
128
129 -------------------
130 discardableCont :: SimplCont -> Bool
131 discardableCont (Stop _ _)          = False
132 discardableCont (CoerceIt _ cont)   = discardableCont cont
133 discardableCont (InlinePlease cont) = discardableCont cont
134 discardableCont other               = True
135
136 discardCont :: SimplCont        -- A continuation, expecting
137             -> SimplCont        -- Replace the continuation with a suitable coerce
138 discardCont cont = case cont of
139                      Stop to_ty _ -> cont
140                      other        -> CoerceIt to_ty (mkStop to_ty)
141                  where
142                    to_ty = contResultType cont
143
144 -------------------
145 contResultType :: SimplCont -> OutType
146 contResultType (Stop to_ty _)        = to_ty
147 contResultType (ArgOf _ to_ty _)     = to_ty
148 contResultType (ApplyTo _ _ _ cont)  = contResultType cont
149 contResultType (CoerceIt _ cont)     = contResultType cont
150 contResultType (InlinePlease cont)   = contResultType cont
151 contResultType (Select _ _ _ _ cont) = contResultType cont
152
153 -------------------
154 countValArgs :: SimplCont -> Int
155 countValArgs (ApplyTo _ (Type ty) se cont) = countValArgs cont
156 countValArgs (ApplyTo _ val_arg   se cont) = 1 + countValArgs cont
157 countValArgs other                         = 0
158
159 countArgs :: SimplCont -> Int
160 countArgs (ApplyTo _ arg se cont) = 1 + countArgs cont
161 countArgs other                   = 0
162 \end{code}
163
164
165 \begin{code}
166 getContArgs :: OutId -> SimplCont 
167             -> SimplM ([(InExpr, SubstEnv, Bool)],      -- Arguments; the Bool is true for strict args
168                         SimplCont,                      -- Remaining continuation
169                         Bool)                           -- Whether we came across an InlineCall
170 -- getContArgs id k = (args, k', inl)
171 --      args are the leading ApplyTo items in k
172 --      (i.e. outermost comes first)
173 --      augmented with demand info from the functionn
174 getContArgs fun orig_cont
175   = getSwitchChecker    `thenSmpl` \ chkr ->
176     let
177                 -- Ignore strictness info if the no-case-of-case
178                 -- flag is on.  Strictness changes evaluation order
179                 -- and that can change full laziness
180         stricts | switchIsOn chkr NoCaseOfCase = vanilla_stricts
181                 | otherwise                    = computed_stricts
182     in
183     go [] stricts False orig_cont
184   where
185     ----------------------------
186
187         -- Type argument
188     go acc ss inl (ApplyTo _ arg@(Type _) se cont)
189         = go ((arg,se,False) : acc) ss inl cont
190                 -- NB: don't bother to instantiate the function type
191
192         -- Value argument
193     go acc (s:ss) inl (ApplyTo _ arg se cont)
194         = go ((arg,se,s) : acc) ss inl cont
195
196         -- An Inline continuation
197     go acc ss inl (InlinePlease cont)
198         = go acc ss True cont
199
200         -- We're run out of arguments, or else we've run out of demands
201         -- The latter only happens if the result is guaranteed bottom
202         -- This is the case for
203         --      * case (error "hello") of { ... }
204         --      * (error "Hello") arg
205         --      * f (error "Hello") where f is strict
206         --      etc
207     go acc ss inl cont 
208         | null ss && discardableCont cont = tick BottomFound    `thenSmpl_`
209                                             returnSmpl (reverse acc, discardCont cont, inl)
210         | otherwise                       = returnSmpl (reverse acc, cont,             inl)
211
212     ----------------------------
213     vanilla_stricts, computed_stricts :: [Bool]
214     vanilla_stricts  = repeat False
215     computed_stricts = zipWith (||) fun_stricts arg_stricts
216
217     ----------------------------
218     (val_arg_tys, _) = splitRepFunTys (idType fun)
219     arg_stricts      = map isStrictType val_arg_tys ++ repeat False
220         -- These argument types are used as a cheap and cheerful way to find
221         -- unboxed arguments, which must be strict.  But it's an InType
222         -- and so there might be a type variable where we expect a function
223         -- type (the substitution hasn't happened yet).  And we don't bother
224         -- doing the type applications for a polymorphic function.
225         -- Hence the split*Rep*FunTys
226
227     ----------------------------
228         -- If fun_stricts is finite, it means the function returns bottom
229         -- after that number of value args have been consumed
230         -- Otherwise it's infinite, extended with False
231     fun_stricts
232       = case splitStrictSig (idNewStrictness fun) of
233           (demands, result_info)
234                 | not (demands `lengthExceeds` countValArgs orig_cont)
235                 ->      -- Enough args, use the strictness given.
236                         -- For bottoming functions we used to pretend that the arg
237                         -- is lazy, so that we don't treat the arg as an
238                         -- interesting context.  This avoids substituting
239                         -- top-level bindings for (say) strings into 
240                         -- calls to error.  But now we are more careful about
241                         -- inlining lone variables, so its ok (see SimplUtils.analyseCont)
242                    if isBotRes result_info then
243                         map isStrictDmd demands         -- Finite => result is bottom
244                    else
245                         map isStrictDmd demands ++ vanilla_stricts
246
247           other -> vanilla_stricts      -- Not enough args, or no strictness
248
249 -------------------
250 interestingArg :: InScopeSet -> InExpr -> SubstEnv -> Bool
251         -- An argument is interesting if it has *some* structure
252         -- We are here trying to avoid unfolding a function that
253         -- is applied only to variables that have no unfolding
254         -- (i.e. they are probably lambda bound): f x y z
255         -- There is little point in inlining f here.
256 interestingArg in_scope arg subst
257   = analyse (substExpr (mkSubst in_scope subst) arg)
258         -- 'analyse' only looks at the top part of the result
259         -- and substExpr is lazy, so this isn't nearly as brutal
260         -- as it looks.
261   where
262     analyse (Var v)           = hasSomeUnfolding (idUnfolding v)
263                                 -- Was: isValueUnfolding (idUnfolding v')
264                                 -- But that seems over-pessimistic
265     analyse (Type _)          = False
266     analyse (App fn (Type _)) = analyse fn
267     analyse (Note _ a)        = analyse a
268     analyse other             = True
269         -- Consider     let x = 3 in f x
270         -- The substitution will contain (x -> ContEx 3), and we want to
271         -- to say that x is an interesting argument.
272         -- But consider also (\x. f x y) y
273         -- The substitution will contain (x -> ContEx y), and we want to say
274         -- that x is not interesting (assuming y has no unfolding)
275 \end{code}
276
277 Comment about interestingCallContext
278 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279 We want to avoid inlining an expression where there can't possibly be
280 any gain, such as in an argument position.  Hence, if the continuation
281 is interesting (eg. a case scrutinee, application etc.) then we
282 inline, otherwise we don't.  
283
284 Previously some_benefit used to return True only if the variable was
285 applied to some value arguments.  This didn't work:
286
287         let x = _coerce_ (T Int) Int (I# 3) in
288         case _coerce_ Int (T Int) x of
289                 I# y -> ....
290
291 we want to inline x, but can't see that it's a constructor in a case
292 scrutinee position, and some_benefit is False.
293
294 Another example:
295
296 dMonadST = _/\_ t -> :Monad (g1 _@_ t, g2 _@_ t, g3 _@_ t)
297
298 ....  case dMonadST _@_ x0 of (a,b,c) -> ....
299
300 we'd really like to inline dMonadST here, but we *don't* want to
301 inline if the case expression is just
302
303         case x of y { DEFAULT -> ... }
304
305 since we can just eliminate this case instead (x is in WHNF).  Similar
306 applies when x is bound to a lambda expression.  Hence
307 contIsInteresting looks for case expressions with just a single
308 default case.
309
310 \begin{code}
311 interestingCallContext :: Bool          -- False <=> no args at all
312                        -> Bool          -- False <=> no value args
313                        -> SimplCont -> Bool
314         -- The "lone-variable" case is important.  I spent ages
315         -- messing about with unsatisfactory varaints, but this is nice.
316         -- The idea is that if a variable appear all alone
317         --      as an arg of lazy fn, or rhs    Stop
318         --      as scrutinee of a case          Select
319         --      as arg of a strict fn           ArgOf
320         -- then we should not inline it (unless there is some other reason,
321         -- e.g. is is the sole occurrence).  We achieve this by making
322         -- interestingCallContext return False for a lone variable.
323         --
324         -- Why?  At least in the case-scrutinee situation, turning
325         --      let x = (a,b) in case x of y -> ...
326         -- into
327         --      let x = (a,b) in case (a,b) of y -> ...
328         -- and thence to 
329         --      let x = (a,b) in let y = (a,b) in ...
330         -- is bad if the binding for x will remain.
331         --
332         -- Another example: I discovered that strings
333         -- were getting inlined straight back into applications of 'error'
334         -- because the latter is strict.
335         --      s = "foo"
336         --      f = \x -> ...(error s)...
337
338         -- Fundamentally such contexts should not ecourage inlining becuase
339         -- the context can ``see'' the unfolding of the variable (e.g. case or a RULE)
340         -- so there's no gain.
341         --
342         -- However, even a type application or coercion isn't a lone variable.
343         -- Consider
344         --      case $fMonadST @ RealWorld of { :DMonad a b c -> c }
345         -- We had better inline that sucker!  The case won't see through it.
346         --
347         -- For now, I'm treating treating a variable applied to types 
348         -- in a *lazy* context "lone". The motivating example was
349         --      f = /\a. \x. BIG
350         --      g = /\a. \y.  h (f a)
351         -- There's no advantage in inlining f here, and perhaps
352         -- a significant disadvantage.  Hence some_val_args in the Stop case
353
354 interestingCallContext some_args some_val_args cont
355   = interesting cont
356   where
357     interesting (InlinePlease _)       = True
358     interesting (Select _ _ _ _ _)     = some_args
359     interesting (ApplyTo _ _ _ _)      = True   -- Can happen if we have (coerce t (f x)) y
360                                                 -- Perhaps True is a bit over-keen, but I've
361                                                 -- seen (coerce f) x, where f has an INLINE prag,
362                                                 -- So we have to give some motivaiton for inlining it
363     interesting (ArgOf _ _ _)          = some_val_args
364     interesting (Stop ty upd_in_place) = some_val_args && upd_in_place
365     interesting (CoerceIt _ cont)      = interesting cont
366         -- If this call is the arg of a strict function, the context
367         -- is a bit interesting.  If we inline here, we may get useful
368         -- evaluation information to avoid repeated evals: e.g.
369         --      x + (y * z)
370         -- Here the contIsInteresting makes the '*' keener to inline,
371         -- which in turn exposes a constructor which makes the '+' inline.
372         -- Assuming that +,* aren't small enough to inline regardless.
373         --
374         -- It's also very important to inline in a strict context for things
375         -- like
376         --              foldr k z (f x)
377         -- Here, the context of (f x) is strict, and if f's unfolding is
378         -- a build it's *great* to inline it here.  So we must ensure that
379         -- the context for (f x) is not totally uninteresting.
380
381
382 -------------------
383 canUpdateInPlace :: Type -> Bool
384 -- Consider   let x = <wurble> in ...
385 -- If <wurble> returns an explicit constructor, we might be able
386 -- to do update in place.  So we treat even a thunk RHS context
387 -- as interesting if update in place is possible.  We approximate
388 -- this by seeing if the type has a single constructor with a
389 -- small arity.  But arity zero isn't good -- we share the single copy
390 -- for that case, so no point in sharing.
391
392 canUpdateInPlace ty 
393   | not opt_UF_UpdateInPlace = False
394   | otherwise
395   = case splitTyConApp_maybe ty of 
396         Nothing         -> False 
397         Just (tycon, _) -> case tyConDataConsIfAvailable tycon of
398                                 [dc]  -> arity == 1 || arity == 2
399                                       where
400                                          arity = dataConRepArity dc
401                                 other -> False
402 \end{code}
403
404
405
406 %************************************************************************
407 %*                                                                      *
408 \section{Dealing with a single binder}
409 %*                                                                      *
410 %************************************************************************
411
412 \begin{code}
413 simplBinders :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
414 simplBinders bndrs thing_inside
415   = getSubst            `thenSmpl` \ subst ->
416     let
417         (subst', bndrs') = Subst.simplBndrs subst bndrs
418     in
419     seqBndrs bndrs'     `seq`
420     setSubst subst' (thing_inside bndrs')
421
422 simplBinder :: InBinder -> (OutBinder -> SimplM a) -> SimplM a
423 simplBinder bndr thing_inside
424   = getSubst            `thenSmpl` \ subst ->
425     let
426         (subst', bndr') = Subst.simplBndr subst bndr
427     in
428     seqBndr bndr'       `seq`
429     setSubst subst' (thing_inside bndr')
430
431
432 simplRecIds :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
433 simplRecIds ids thing_inside
434   = getSubst            `thenSmpl` \ subst ->
435     let
436         (subst', ids') = mapAccumL Subst.simplLetId subst ids
437     in
438     seqBndrs ids'       `seq`
439     setSubst subst' (thing_inside ids')
440
441 simplLetId :: InBinder -> (OutBinder -> SimplM a) -> SimplM a
442 simplLetId id thing_inside
443   = getSubst            `thenSmpl` \ subst ->
444     let
445         (subst', id') = Subst.simplLetId subst id
446     in
447     seqBndr id' `seq`
448     setSubst subst' (thing_inside id')
449
450 seqBndrs [] = ()
451 seqBndrs (b:bs) = seqBndr b `seq` seqBndrs bs
452
453 seqBndr b | isTyVar b = b `seq` ()
454           | otherwise = seqType (idType b)      `seq`
455                         idInfo b                `seq`
456                         ()
457 \end{code}
458
459
460 %************************************************************************
461 %*                                                                      *
462 \subsection{Local tyvar-lifting}
463 %*                                                                      *
464 %************************************************************************
465
466 mkRhsTyLam tries this transformation, when the big lambda appears as
467 the RHS of a let(rec) binding:
468
469         /\abc -> let(rec) x = e in b
470    ==>
471         let(rec) x' = /\abc -> let x = x' a b c in e
472         in 
473         /\abc -> let x = x' a b c in b
474
475 This is good because it can turn things like:
476
477         let f = /\a -> letrec g = ... g ... in g
478 into
479         letrec g' = /\a -> ... g' a ...
480         in
481         let f = /\ a -> g' a
482
483 which is better.  In effect, it means that big lambdas don't impede
484 let-floating.
485
486 This optimisation is CRUCIAL in eliminating the junk introduced by
487 desugaring mutually recursive definitions.  Don't eliminate it lightly!
488
489 So far as the implementation is concerned:
490
491         Invariant: go F e = /\tvs -> F e
492         
493         Equalities:
494                 go F (Let x=e in b)
495                 = Let x' = /\tvs -> F e 
496                   in 
497                   go G b
498                 where
499                     G = F . Let x = x' tvs
500         
501                 go F (Letrec xi=ei in b)
502                 = Letrec {xi' = /\tvs -> G ei} 
503                   in
504                   go G b
505                 where
506                   G = F . Let {xi = xi' tvs}
507
508 [May 1999]  If we do this transformation *regardless* then we can
509 end up with some pretty silly stuff.  For example, 
510
511         let 
512             st = /\ s -> let { x1=r1 ; x2=r2 } in ...
513         in ..
514 becomes
515         let y1 = /\s -> r1
516             y2 = /\s -> r2
517             st = /\s -> ...[y1 s/x1, y2 s/x2]
518         in ..
519
520 Unless the "..." is a WHNF there is really no point in doing this.
521 Indeed it can make things worse.  Suppose x1 is used strictly,
522 and is of the form
523
524         x1* = case f y of { (a,b) -> e }
525
526 If we abstract this wrt the tyvar we then can't do the case inline
527 as we would normally do.
528
529
530 \begin{code}
531 tryRhsTyLam :: OutExpr -> SimplM ([OutBind], OutExpr)
532
533 tryRhsTyLam rhs                         -- Only does something if there's a let
534   | null tyvars || not (worth_it body)  -- inside a type lambda, 
535   = returnSmpl ([], rhs)                -- and a WHNF inside that
536
537   | otherwise
538   = go (\x -> x) body           `thenSmpl` \ (binds, body') ->
539     returnSmpl (binds,  mkLams tyvars body')
540
541   where
542     (tyvars, body) = collectTyBinders rhs
543
544     worth_it e@(Let _ _) = whnf_in_middle e
545     worth_it e           = False
546
547     whnf_in_middle (Let (NonRec x rhs) e) | isUnLiftedType (idType x) = False
548     whnf_in_middle (Let _ e) = whnf_in_middle e
549     whnf_in_middle e         = exprIsCheap e
550
551     go fn (Let bind@(NonRec var rhs) body)
552       | exprIsTrivial rhs
553       = go (fn . Let bind) body
554
555     go fn (Let (NonRec var rhs) body)
556       = mk_poly tyvars_here var                         `thenSmpl` \ (var', rhs') ->
557         go (fn . Let (mk_silly_bind var rhs')) body     `thenSmpl` \ (binds, body') ->
558         returnSmpl (NonRec var' (mkLams tyvars_here (fn rhs)) : binds, body')
559
560       where
561         tyvars_here = tyvars
562                 --      main_tyvar_set = mkVarSet tyvars
563                 --      var_ty = idType var
564                 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfType var_ty)
565                 -- tyvars_here was an attempt to reduce the number of tyvars
566                 -- wrt which the new binding is abstracted.  But the naive
567                 -- approach of abstract wrt the tyvars free in the Id's type
568                 -- fails. Consider:
569                 --      /\ a b -> let t :: (a,b) = (e1, e2)
570                 --                    x :: a     = fst t
571                 --                in ...
572                 -- Here, b isn't free in x's type, but we must nevertheless
573                 -- abstract wrt b as well, because t's type mentions b.
574                 -- Since t is floated too, we'd end up with the bogus:
575                 --      poly_t = /\ a b -> (e1, e2)
576                 --      poly_x = /\ a   -> fst (poly_t a *b*)
577                 -- So for now we adopt the even more naive approach of
578                 -- abstracting wrt *all* the tyvars.  We'll see if that
579                 -- gives rise to problems.   SLPJ June 98
580
581     go fn (Let (Rec prs) body)
582        = mapAndUnzipSmpl (mk_poly tyvars_here) vars     `thenSmpl` \ (vars', rhss') ->
583          let
584             gn body  = fn (foldr Let body (zipWith mk_silly_bind vars rhss'))
585             new_bind = Rec (vars' `zip` [mkLams tyvars_here (gn rhs) | rhs <- rhss])
586          in
587          go gn body                             `thenSmpl` \ (binds, body') -> 
588          returnSmpl (new_bind : binds, body')
589        where
590          (vars,rhss) = unzip prs
591          tyvars_here = tyvars
592                 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfTypes var_tys)
593                 --       var_tys     = map idType vars
594                 -- See notes with tyvars_here above
595
596     go fn body = returnSmpl ([], fn body)
597
598     mk_poly tyvars_here var
599       = getUniqueSmpl           `thenSmpl` \ uniq ->
600         let
601             poly_name = setNameUnique (idName var) uniq         -- Keep same name
602             poly_ty   = mkForAllTys tyvars_here (idType var)    -- But new type of course
603             poly_id   = mkLocalId poly_name poly_ty 
604
605                 -- In the olden days, it was crucial to copy the occInfo of the original var, 
606                 -- because we were looking at occurrence-analysed but as yet unsimplified code!
607                 -- In particular, we mustn't lose the loop breakers.  BUT NOW we are looking
608                 -- at already simplified code, so it doesn't matter
609                 -- 
610                 -- It's even right to retain single-occurrence or dead-var info:
611                 -- Suppose we started with  /\a -> let x = E in B
612                 -- where x occurs once in B. Then we transform to:
613                 --      let x' = /\a -> E in /\a -> let x* = x' a in B
614                 -- where x* has an INLINE prag on it.  Now, once x* is inlined,
615                 -- the occurrences of x' will be just the occurrences originally
616                 -- pinned on x.
617         in
618         returnSmpl (poly_id, mkTyApps (Var poly_id) (mkTyVarTys tyvars_here))
619
620     mk_silly_bind var rhs = NonRec var (Note InlineMe rhs)
621                 -- Suppose we start with:
622                 --
623                 --      x = /\ a -> let g = G in E
624                 --
625                 -- Then we'll float to get
626                 --
627                 --      x = let poly_g = /\ a -> G
628                 --          in /\ a -> let g = poly_g a in E
629                 --
630                 -- But now the occurrence analyser will see just one occurrence
631                 -- of poly_g, not inside a lambda, so the simplifier will
632                 -- PreInlineUnconditionally poly_g back into g!  Badk to square 1!
633                 -- (I used to think that the "don't inline lone occurrences" stuff
634                 --  would stop this happening, but since it's the *only* occurrence,
635                 --  PreInlineUnconditionally kicks in first!)
636                 --
637                 -- Solution: put an INLINE note on g's RHS, so that poly_g seems
638                 --           to appear many times.  (NB: mkInlineMe eliminates
639                 --           such notes on trivial RHSs, so do it manually.)
640 \end{code}
641
642
643 %************************************************************************
644 %*                                                                      *
645 \subsection{Eta expansion}
646 %*                                                                      *
647 %************************************************************************
648
649         Try eta expansion for RHSs
650
651 We go for:
652    Case 1    f = \x1..xn -> N  ==>   f = \x1..xn y1..ym -> N y1..ym
653                  (n >= 0)
654      OR         
655    Case 2    f = N E1..En      ==>   z1=E1
656                  (n > 0)                 .. 
657                                      zn=En
658                                      f = \y1..ym -> N z1..zn y1..ym
659
660 where (in both cases) 
661
662         * The xi can include type variables
663
664         * The yi are all value variables
665
666         * N is a NORMAL FORM (i.e. no redexes anywhere)
667           wanting a suitable number of extra args.
668
669         * the Ei must not have unlifted type
670
671 There is no point in looking for a combination of the two, because
672 that would leave use with some lets sandwiched between lambdas; that's
673 what the final test in the first equation is for.
674
675 In Case 1, we may have to sandwich some coerces between the lambdas
676 to make the types work.   exprEtaExpandArity looks through coerces
677 when computing arity; and etaExpand adds the coerces as necessary when
678 actually computing the expansion.
679
680 \begin{code}
681 tryEtaExpansion :: OutExpr -> OutType -> SimplM ([OutBind], OutExpr)
682 tryEtaExpansion rhs rhs_ty
683   |  not opt_SimplDoLambdaEtaExpansion          -- Not if switched off
684   || exprIsTrivial rhs                          -- Not if RHS is trivial
685   || final_arity == 0                           -- Not if arity is zero
686   = returnSmpl ([], rhs)
687
688   | n_val_args == 0 && not arity_is_manifest
689   =     -- Some lambdas but not enough: case 1
690     getUniqSupplySmpl                           `thenSmpl` \ us ->
691     returnSmpl ([], etaExpand final_arity us rhs rhs_ty)
692
693   | n_val_args > 0 && not (any cant_bind arg_infos)
694   =     -- Partial application: case 2
695     mapAndUnzipSmpl bind_z_arg arg_infos        `thenSmpl` \ (maybe_z_binds, z_args) ->
696     getUniqSupplySmpl                           `thenSmpl` \ us ->
697     returnSmpl (catMaybes maybe_z_binds, 
698                 etaExpand final_arity us (mkApps fun z_args) rhs_ty)
699
700   | otherwise
701   = returnSmpl ([], rhs)
702   where
703     (fun, args)                    = collectArgs rhs
704     n_val_args                     = valArgCount args
705     (fun_arity, arity_is_manifest) = exprEtaExpandArity fun
706     final_arity                    = 0 `max` (fun_arity - n_val_args)
707     arg_infos                      = [(arg, exprType arg, exprIsTrivial arg) | arg <- args]
708     cant_bind (_, ty, triv)        = not triv && isUnLiftedType ty
709
710     bind_z_arg (arg, arg_ty, trivial_arg) 
711         | trivial_arg = returnSmpl (Nothing, arg)
712         | otherwise   = newId SLIT("z") arg_ty  $ \ z ->
713                         returnSmpl (Just (NonRec z arg), Var z)
714 \end{code}
715
716
717 %************************************************************************
718 %*                                                                      *
719 \subsection{Case absorption and identity-case elimination}
720 %*                                                                      *
721 %************************************************************************
722
723 \begin{code}
724 mkCase :: OutExpr -> OutId -> [OutAlt] -> SimplM OutExpr
725 \end{code}
726
727 @mkCase@ tries the following transformation (if possible):
728
729 case e of b {             ==>   case e of b {
730   p1 -> rhs1                      p1 -> rhs1
731   ...                             ...
732   pm -> rhsm                      pm -> rhsm
733   _  -> case b of b' {            pn -> rhsn[b/b'] {or (alg)  let b=b' in rhsn}
734                                                    {or (prim) case b of b' { _ -> rhsn}}
735               pn -> rhsn          ...
736               ...                 po -> rhso[b/b']
737               po -> rhso          _  -> rhsd[b/b'] {or let b'=b in rhsd}
738               _  -> rhsd
739 }
740
741 which merges two cases in one case when -- the default alternative of
742 the outer case scrutises the same variable as the outer case This
743 transformation is called Case Merging.  It avoids that the same
744 variable is scrutinised multiple times.
745
746 \begin{code}
747 mkCase scrut outer_bndr outer_alts
748   |  opt_SimplCaseMerge
749   && maybeToBool maybe_case_in_default
750      
751   = tick (CaseMerge outer_bndr)         `thenSmpl_`
752     returnSmpl (Case scrut outer_bndr new_alts)
753         -- Warning: don't call mkCase recursively!
754         -- Firstly, there's no point, because inner alts have already had
755         -- mkCase applied to them, so they won't have a case in their default
756         -- Secondly, if you do, you get an infinite loop, because the bindNonRec
757         -- in munge_rhs puts a case into the DEFAULT branch!
758   where
759     new_alts = add_default maybe_inner_default
760                            (outer_alts_without_deflt ++ inner_con_alts)
761
762     maybe_case_in_default = case findDefault outer_alts of
763                                 (outer_alts_without_default,
764                                  Just (Case (Var scrut_var) inner_bndr inner_alts))
765                                    | outer_bndr == scrut_var
766                                    -> Just (outer_alts_without_default, inner_bndr, inner_alts)
767                                 other -> Nothing
768
769     Just (outer_alts_without_deflt, inner_bndr, inner_alts) = maybe_case_in_default
770
771                 --  Eliminate any inner alts which are shadowed by the outer ones
772     outer_cons = [con | (con,_,_) <- outer_alts_without_deflt]
773
774     munged_inner_alts = [ (con, args, munge_rhs rhs) 
775                         | (con, args, rhs) <- inner_alts, 
776                            not (con `elem` outer_cons)  -- Eliminate shadowed inner alts
777                         ]
778     munge_rhs rhs = bindNonRec inner_bndr (Var outer_bndr) rhs
779
780     (inner_con_alts, maybe_inner_default) = findDefault munged_inner_alts
781
782     add_default (Just rhs) alts = (DEFAULT,[],rhs) : alts
783     add_default Nothing    alts = alts
784 \end{code}
785
786 Now the identity-case transformation:
787
788         case e of               ===> e
789                 True  -> True;
790                 False -> False
791
792 and similar friends.
793
794 \begin{code}
795 mkCase scrut case_bndr alts
796   | all identity_alt alts
797   = tick (CaseIdentity case_bndr)               `thenSmpl_`
798     returnSmpl (re_note scrut)
799   where
800     identity_alt (con, args, rhs) = de_note rhs `cheapEqExpr` identity_rhs con args
801
802     identity_rhs (DataAlt con) args = mkConApp con (arg_tys ++ map varToCoreExpr args)
803     identity_rhs (LitAlt lit)  _    = Lit lit
804     identity_rhs DEFAULT       _    = Var case_bndr
805
806     arg_tys = map Type (tyConAppArgs (idType case_bndr))
807
808         -- We've seen this:
809         --      case coerce T e of x { _ -> coerce T' x }
810         -- And we definitely want to eliminate this case!
811         -- So we throw away notes from the RHS, and reconstruct
812         -- (at least an approximation) at the other end
813     de_note (Note _ e) = de_note e
814     de_note e          = e
815
816         -- re_note wraps a coerce if it might be necessary
817     re_note scrut = case head alts of
818                         (_,_,rhs1@(Note _ _)) -> mkCoerce (exprType rhs1) (idType case_bndr) scrut
819                         other                 -> scrut
820 \end{code}
821
822 The catch-all case.  We do a final transformation that I've
823 occasionally seen making a big difference:
824
825         case e of               =====>     case e of
826           C _ -> f x                         D v -> ....v....
827           D v -> ....v....                   DEFAULT -> f x
828           DEFAULT -> f x
829
830 The point is that we merge common RHSs, at least for the DEFAULT case.
831 [One could do something more elaborate but I've never seen it needed.]
832 The case where this came up was like this (lib/std/PrelCError.lhs):
833
834         x | p `is` 1 -> e1
835           | p `is` 2 -> e2
836         ...etc...
837
838 where @is@ was something like
839         
840         p `is` n = p /= (-1) && p == n
841
842 This gave rise to a horrible sequence of cases
843
844         case p of
845           (-1) -> $j p
846           1    -> e1
847           DEFAULT -> $j p
848
849 and similarly in cascade for all the join points!
850           
851 \begin{code}
852 mkCase other_scrut case_bndr other_alts
853   = returnSmpl (Case other_scrut case_bndr (mergeDefault other_alts))
854
855 mergeDefault (deflt_alt@(DEFAULT,_,deflt_rhs) : con_alts)
856   = deflt_alt : [alt | alt@(con,_,rhs) <- con_alts, not (rhs `cheapEqExpr` deflt_rhs)]
857         -- NB: we can neglect the binders because we won't get equality if the
858         -- binders are mentioned in rhs (no shadowing)
859 mergeDefault other_alts
860   = other_alts
861 \end{code}