[project @ 1997-09-26 13:38:58 by simonm]
[ghc-hetmet.git] / ghc / compiler / simplCore / Simplify.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[Simplify]{The main module of the simplifier}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module Simplify ( simplTopBinds, simplExpr, simplBind ) where
10
11 IMPORT_1_3(List(partition))
12
13 IMP_Ubiq(){-uitous-}
14 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
15 IMPORT_DELOOPER(SmplLoop)               -- paranoia checking
16 #endif
17
18 import BinderInfo
19 import CmdLineOpts      ( SimplifierSwitch(..) )
20 import ConFold          ( completePrim )
21 import CoreUnfold       ( Unfolding, SimpleUnfolding, mkFormSummary, 
22                           exprIsTrivial, whnfOrBottom, inlineUnconditionally,
23                           FormSummary(..)
24                         )
25 import CostCentre       ( isSccCountCostCentre, cmpCostCentre, costsAreSubsumed, useCurrentCostCentre )
26 import CoreSyn
27 import CoreUtils        ( coreExprType, nonErrorRHSs, maybeErrorApp,
28                           unTagBinders, squashableDictishCcExpr
29                         )
30 import Id               ( idType, idMustBeINLINEd, idWantsToBeINLINEd, idMustNotBeINLINEd, 
31                           addIdArity, getIdArity,
32                           getIdDemandInfo, addIdDemandInfo,
33                           GenId{-instance NamedThing-}
34                         )
35 import Name             ( isExported )
36 import IdInfo           ( willBeDemanded, noDemandInfo, DemandInfo, ArityInfo(..),
37                           atLeastArity, unknownArity )
38 import Literal          ( isNoRepLit )
39 import Maybes           ( maybeToBool )
40 import PprType          ( GenType{-instance Outputable-}, GenTyVar{- instance Outputable -} )
41 #if __GLASGOW_HASKELL__ <= 30
42 import PprCore          ( GenCoreArg, GenCoreExpr )
43 #endif
44 import TyVar            ( GenTyVar {- instance Eq -} )
45 import Pretty           --( ($$) )
46 import PrimOp           ( primOpOkForSpeculation, PrimOp(..) )
47 import SimplCase        ( simplCase, bindLargeRhs )
48 import SimplEnv
49 import SimplMonad
50 import SimplVar         ( completeVar )
51 import Unique           ( Unique )
52 import SimplUtils
53 import Type             ( mkTyVarTy, mkTyVarTys, mkAppTy, applyTy, mkFunTys, maybeAppDataTyCon,
54                           splitFunTy, splitFunTyExpandingDicts, getFunTy_maybe, eqTy
55                         )
56 import TysWiredIn       ( realWorldStateTy )
57 import Outputable       ( PprStyle(..), Outputable(..) )
58 import Util             ( SYN_IE(Eager), appEager, returnEager, runEager, mapEager,
59                           isSingleton, zipEqual, zipWithEqual, mapAndUnzip, panic, pprPanic, assertPanic, pprTrace )
60 \end{code}
61
62 The controlling flags, and what they do
63 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64
65 passes:
66 ------
67 -fsimplify              = run the simplifier
68 -ffloat-inwards         = runs the float lets inwards pass
69 -ffloat                 = runs the full laziness pass
70                           (ToDo: rename to -ffull-laziness)
71 -fupdate-analysis       = runs update analyser
72 -fstrictness            = runs strictness analyser
73 -fsaturate-apps         = saturates applications (eta expansion)
74
75 options:
76 -------
77 -ffloat-past-lambda     = OK to do full laziness.
78                           (ToDo: remove, as the full laziness pass is
79                                  useless without this flag, therefore
80                                  it is unnecessary. Just -ffull-laziness
81                                  should be kept.)
82
83 -ffloat-lets-ok         = OK to float lets out of lets if the enclosing
84                           let is strict or if the floating will expose
85                           a WHNF [simplifier].
86
87 -ffloat-primops-ok      = OK to float out of lets cases whose scrutinee
88                           is a primop that cannot fail [simplifier].
89
90 -fcode-duplication-ok   = allows the previous option to work on cases with
91                           multiple branches [simplifier].
92
93 -flet-to-case           = does let-to-case transformation [simplifier].
94
95 -fcase-of-case          = does case of case transformation [simplifier].
96
97 -fpedantic-bottoms      = does not allow:
98                              case x of y -> e  ===>  e[x/y]
99                           (which may turn bottom into non-bottom)
100
101
102                         NOTES ON INLINING
103                         ~~~~~~~~~~~~~~~~~
104
105 Inlining is one of the delicate aspects of the simplifier.  By
106 ``inlining'' we mean replacing an occurrence of a variable ``x'' by
107 the RHS of x's definition.  Thus
108
109         let x = e in ...x...    ===>   let x = e in ...e...
110
111 We have two mechanisms for inlining:
112
113 1.  Unconditional.  The occurrence analyser has pinned an (OneOcc
114 FunOcc NoDupDanger NotInsideSCC n) flag on the variable, saying ``it's
115 certainly safe to inline this variable, and to drop its binding''.
116 (...Umm... if n <= 1; if n > 1, it is still safe, provided you are
117 happy to be duplicating code...) When it encounters such a beast, the
118 simplifer binds the variable to its RHS (in the id_env) and continues.
119 It doesn't even look at the RHS at that stage.  It also drops the
120 binding altogether.
121
122 2.  Conditional.  In all other situations, the simplifer simplifies
123 the RHS anyway, and keeps the new binding.  It also binds the new
124 (cloned) variable to a ``suitable'' Unfolding in the UnfoldEnv.
125
126 Here, ``suitable'' might mean NoUnfolding (if the occurrence
127 info is ManyOcc and the RHS is not a manifest HNF, or UnfoldAlways (if
128 the variable has an INLINE pragma on it).  The idea is that anything
129 in the UnfoldEnv is safe to use, but also has an enclosing binding if
130 you decide not to use it.
131
132 Head normal forms
133 ~~~~~~~~~~~~~~~~~
134 We *never* put a non-HNF unfolding in the UnfoldEnv except in the
135 INLINE-pragma case.
136
137 At one time I thought it would be OK to put non-HNF unfoldings in for
138 variables which occur only once [if they got inlined at that
139 occurrence the RHS of the binding would become dead, so no duplication
140 would occur].   But consider:
141 @
142         let x = <expensive>
143             f = \y -> ...y...y...y...
144         in f x
145 @
146 Now, it seems that @x@ appears only once, but even so it is NOT safe
147 to put @x@ in the UnfoldEnv, because @f@ will be inlined, and will
148 duplicate the references to @x@.
149
150 Because of this, the "unconditional-inline" mechanism above is the
151 only way in which non-HNFs can get inlined.
152
153 INLINE pragmas
154 ~~~~~~~~~~~~~~
155
156 When a variable has an INLINE pragma on it --- which includes wrappers
157 produced by the strictness analyser --- we treat it rather carefully.
158
159 For a start, we are careful not to substitute into its RHS, because
160 that might make it BIG, and the user said "inline exactly this", not
161 "inline whatever you get after inlining other stuff inside me".  For
162 example
163
164         let f = BIG
165         in {-# INLINE y #-} y = f 3
166         in ...y...y...
167
168 Here we don't want to substitute BIG for the (single) occurrence of f,
169 because then we'd duplicate BIG when we inline'd y.  (Exception:
170 things in the UnfoldEnv with UnfoldAlways flags, which originated in
171 other INLINE pragmas.)
172
173 So, we clean out the UnfoldEnv of all SimpleUnfolding inlinings before
174 going into such an RHS.
175
176 What about imports?  They don't really matter much because we only
177 inline relatively small things via imports.
178
179 We augment the the UnfoldEnv with UnfoldAlways guidance if there's an
180 INLINE pragma.  We also do this for the RHSs of recursive decls,
181 before looking at the recursive decls. That way we achieve the effect
182 of inlining a wrapper in the body of its worker, in the case of a
183 mutually-recursive worker/wrapper split.
184
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection[Simplify-simplExpr]{The main function: simplExpr}
189 %*                                                                      *
190 %************************************************************************
191
192 At the top level things are a little different.
193
194   * No cloning (not allowed for exported Ids, unnecessary for the others)
195   * Floating is done a bit differently (no case floating; check for leaks; handle letrec)
196
197 \begin{code}
198 simplTopBinds :: SimplEnv -> [InBinding] -> SmplM [OutBinding]
199
200 -- Dead code is now discarded by the occurrence analyser,
201
202 simplTopBinds env binds
203   = mapSmpl (floatBind env True) binds  `thenSmpl` \ binds_s ->
204     simpl_top_binds env (concat binds_s)
205   where
206     simpl_top_binds env [] = returnSmpl []
207
208     simpl_top_binds env (NonRec binder@(in_id,occ_info) rhs : binds)
209       =         --- No cloning necessary at top level
210         simplRhsExpr env binder rhs in_id                               `thenSmpl` \ (rhs',arity) ->
211         completeNonRec env binder (in_id `withArity` arity) rhs'        `thenSmpl` \ (new_env, binds1') ->
212         simpl_top_binds new_env binds                                   `thenSmpl` \ binds2' ->
213         returnSmpl (binds1' ++ binds2')
214
215     simpl_top_binds env (Rec pairs : binds)
216       =         -- No cloning necessary at top level, but we nevertheless
217                 -- add the Ids to the environment.  This makes sure that
218                 -- info carried on the Id (such as arity info) gets propagated
219                 -- to occurrences.
220                 --
221                 -- This may seem optional, but I found an occasion when it Really matters.
222                 -- Consider     foo{n} = ...foo...
223                 --              baz* = foo
224                 --
225                 -- where baz* is exported and foo isn't.  Then when we do "indirection-shorting"
226                 -- in tidyCore, we need the {no-inline} pragma from foo to attached to the final
227                 -- thing:       baz*{n} = ...baz...
228                 --
229                 -- Sure we could have made the indirection-shorting a bit cleverer, but
230                 -- propagating pragma info is a Good Idea anyway.
231         let
232             env1 = extendIdEnvWithClones env binders ids
233         in
234         simplRecursiveGroup env1 ids pairs      `thenSmpl` \ (bind', new_env) ->
235         simpl_top_binds new_env binds           `thenSmpl` \ binds' ->
236         returnSmpl (Rec bind' : binds')
237       where
238         binders = map fst pairs
239         ids     = map fst binders
240 \end{code}
241
242 %************************************************************************
243 %*                                                                      *
244 \subsection[Simplify-simplExpr]{The main function: simplExpr}
245 %*                                                                      *
246 %************************************************************************
247
248
249 \begin{code}
250 simplExpr :: SimplEnv
251           -> InExpr -> [OutArg]
252           -> OutType            -- Type of (e args); i.e. type of overall result
253           -> SmplM OutExpr
254 \end{code}
255
256 The expression returned has the same meaning as the input expression
257 applied to the specified arguments.
258
259
260 Variables
261 ~~~~~~~~~
262 Check if there's a macro-expansion, and if so rattle on.  Otherwise do
263 the more sophisticated stuff.
264
265 \begin{code}
266 simplExpr env (Var v) args result_ty
267   = case (runEager $ lookupId env v) of
268       LitArg lit                -- A boring old literal
269         -> ASSERT( null args )
270            returnSmpl (Lit lit)
271
272       VarArg var        -- More interesting!  An id!
273         -> completeVar env var args result_ty
274                                 -- Either Id is in the local envt, or it's a global.
275                                 -- In either case we don't need to apply the type
276                                 -- environment to it.
277 \end{code}
278
279 Literals
280 ~~~~~~~~
281
282 \begin{code}
283 simplExpr env (Lit l) [] result_ty = returnSmpl (Lit l)
284 #ifdef DEBUG
285 simplExpr env (Lit l) _  _ = panic "simplExpr:Lit with argument"
286 #endif
287 \end{code}
288
289 Primitive applications are simple.
290 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
292 NB: Prim expects an empty argument list! (Because it should be
293 saturated and not higher-order. ADR)
294
295 \begin{code}
296 simplExpr env (Prim op prim_args) args result_ty
297   = ASSERT (null args)
298     mapEager (simplArg env) prim_args   `appEager` \ prim_args' ->
299     simpl_op op                         `appEager` \ op' ->
300     completePrim env op' prim_args'
301   where
302     -- PrimOps just need any types in them renamed.
303
304     simpl_op (CCallOp label is_asm may_gc arg_tys result_ty)
305       = mapEager (simplTy env) arg_tys  `appEager` \ arg_tys' ->
306         simplTy env result_ty           `appEager` \ result_ty' ->
307         returnEager (CCallOp label is_asm may_gc arg_tys' result_ty')
308
309     simpl_op other_op = returnEager other_op
310 \end{code}
311
312 Constructor applications
313 ~~~~~~~~~~~~~~~~~~~~~~~~
314 Nothing to try here.  We only reuse constructors when they appear as the
315 rhs of a let binding (see completeLetBinding).
316
317 \begin{code}
318 simplExpr env (Con con con_args) args result_ty
319   = ASSERT( null args )
320     mapEager (simplArg env) con_args    `appEager` \ con_args' ->
321     returnSmpl (Con con con_args')
322 \end{code}
323
324
325 Applications are easy too:
326 ~~~~~~~~~~~~~~~~~~~~~~~~~~
327 Just stuff 'em in the arg stack
328
329 \begin{code}
330 simplExpr env (App fun arg) args result_ty
331   = simplArg env arg    `appEager` \ arg' ->
332     simplExpr env fun (arg' : args) result_ty
333 \end{code}
334
335 Type lambdas
336 ~~~~~~~~~~~~
337
338 First the case when it's applied to an argument.
339
340 \begin{code}
341 simplExpr env (Lam (TyBinder tyvar) body) (TyArg ty : args) result_ty
342   = -- ASSERT(not (isPrimType ty))
343     tick TyBetaReduction        `thenSmpl_`
344     simplExpr (extendTyEnv env tyvar ty) body args result_ty
345 \end{code}
346
347 \begin{code}
348 simplExpr env tylam@(Lam (TyBinder tyvar) body) [] result_ty
349   = cloneTyVarSmpl tyvar                `thenSmpl` \ tyvar' ->
350     let
351         new_ty  = mkTyVarTy tyvar'
352         new_env = extendTyEnv env tyvar new_ty
353         new_result_ty = applyTy result_ty new_ty
354     in
355     simplExpr new_env body [] new_result_ty             `thenSmpl` \ body' ->
356     returnSmpl (Lam (TyBinder tyvar') body')
357
358 #ifdef DEBUG
359 simplExpr env (Lam (TyBinder _) _) (_ : _) result_ty
360   = panic "simplExpr:TyLam with non-TyArg"
361 #endif
362 \end{code}
363
364
365 Ordinary lambdas
366 ~~~~~~~~~~~~~~~~
367
368 There's a complication with lambdas that aren't saturated.
369 Suppose we have:
370
371         (\x. \y. ...x...)
372
373 If we did nothing, x is used inside the \y, so would be marked
374 as dangerous to dup.  But in the common case where the abstraction
375 is applied to two arguments this is over-pessimistic.
376 So instead we don't take account of the \y when dealing with x's usage;
377 instead, the simplifier is careful when partially applying lambdas.
378
379 \begin{code}
380 simplExpr env expr@(Lam (ValBinder binder) body) orig_args result_ty
381   = go 0 env expr orig_args
382   where
383     go n env (Lam (ValBinder binder) body) (val_arg : args)
384       | isValArg val_arg                -- The lambda has an argument
385       = tick BetaReduction      `thenSmpl_`
386         go (n+1) (extendIdEnvWithAtom env binder val_arg) body args
387
388     go n env expr@(Lam (ValBinder binder) body) args
389         -- The lambda is un-saturated, so we must zap the occurrence info
390         -- on the arguments we've already beta-reduced into the body of the lambda
391       = ASSERT( null args )     -- Value lambda must match value argument!
392         let
393             new_env = markDangerousOccs env (take n orig_args)
394         in
395         simplValLam new_env expr 0 {- Guaranteed applied to at least 0 args! -} result_ty 
396                                 `thenSmpl` \ (expr', arity) ->
397         returnSmpl expr'
398
399     go n env non_val_lam_expr args      -- The lambda had enough arguments
400       = simplExpr env non_val_lam_expr args result_ty
401 \end{code}
402
403
404 Let expressions
405 ~~~~~~~~~~~~~~~
406
407 \begin{code}
408 simplExpr env (Let bind body) args result_ty
409   = simplBind env bind (\env -> simplExpr env body args result_ty) result_ty
410 \end{code}
411
412 Case expressions
413 ~~~~~~~~~~~~~~~~
414
415 \begin{code}
416 simplExpr env expr@(Case scrut alts) args result_ty
417   = simplCase env scrut alts (\env rhs -> simplExpr env rhs args result_ty) result_ty
418 \end{code}
419
420
421 Coercions
422 ~~~~~~~~~
423 \begin{code}
424 simplExpr env (Coerce coercion ty body) args result_ty
425   = simplCoerce env coercion ty body args result_ty
426 \end{code}
427
428
429 Set-cost-centre
430 ~~~~~~~~~~~~~~~
431
432 1) Eliminating nested sccs ...
433 We must be careful to maintain the scc counts ...
434
435 \begin{code}
436 simplExpr env (SCC cc1 (SCC cc2 expr)) args result_ty
437   | not (isSccCountCostCentre cc2) && case cmpCostCentre cc1 cc2 of { EQ_ -> True; _ -> False }
438         -- eliminate inner scc if no call counts and same cc as outer
439   = simplExpr env (SCC cc1 expr) args result_ty
440
441   | not (isSccCountCostCentre cc2) && not (isSccCountCostCentre cc1)
442         -- eliminate outer scc if no call counts associated with either ccs
443   = simplExpr env (SCC cc2 expr) args result_ty
444 \end{code}
445
446 2) Moving sccs inside lambdas ...
447   
448 \begin{code}
449 simplExpr env (SCC cc (Lam binder@(ValBinder _) body)) args result_ty
450   | not (isSccCountCostCentre cc)
451         -- move scc inside lambda only if no call counts
452   = simplExpr env (Lam binder (SCC cc body)) args result_ty
453
454 simplExpr env (SCC cc (Lam binder body)) args result_ty
455         -- always ok to move scc inside type/usage lambda
456   = simplExpr env (Lam binder (SCC cc body)) args result_ty
457 \end{code}
458
459 3) Eliminating dict sccs ...
460
461 \begin{code}
462 simplExpr env (SCC cc expr) args result_ty
463   | squashableDictishCcExpr cc expr
464         -- eliminate dict cc if trivial dict expression
465   = simplExpr env expr args result_ty
466 \end{code}
467
468 4) Moving arguments inside the body of an scc ...
469 This moves the cost of doing the application inside the scc
470 (which may include the cost of extracting methods etc)
471
472 \begin{code}
473 simplExpr env (SCC cost_centre body) args result_ty
474   = let
475         new_env = setEnclosingCC env cost_centre
476     in
477     simplExpr new_env body args result_ty               `thenSmpl` \ body' ->
478     returnSmpl (SCC cost_centre body')
479 \end{code}
480
481 %************************************************************************
482 %*                                                                      *
483 \subsection{Simplify RHS of a Let/Letrec}
484 %*                                                                      *
485 %************************************************************************
486
487 simplRhsExpr does arity-expansion.  That is, given:
488
489         * a right hand side /\ tyvars -> \a1 ... an -> e
490         * the information (stored in BinderInfo) that the function will always
491           be applied to at least k arguments
492
493 it transforms the rhs to
494
495         /\tyvars -> \a1 ... an b(n+1) ... bk -> (e b(n+1) ... bk)
496
497 This is a Very Good Thing!
498
499 \begin{code}
500 simplRhsExpr
501         :: SimplEnv
502         -> InBinder
503         -> InExpr
504         -> OutId                -- The new binder (used only for its type)
505         -> SmplM (OutExpr, ArityInfo)
506 \end{code}
507
508 First a special case for variable right-hand sides
509         v = w
510 It's OK to simplify the RHS, but it's often a waste of time.  Often
511 these v = w things persist because v is exported, and w is used 
512 elsewhere.  So if we're not careful we'll eta expand the rhs, only
513 to eta reduce it in competeNonRec.
514
515 If we leave the binding unchanged, we will certainly replace v by w at 
516 every occurrence of v, which is good enough.  
517
518 In fact, it's *better* to replace v by w than to inline w in v's rhs,
519 even if this is the only occurrence of w.  Why? Because w might have
520 IdInfo (like strictness) that v doesn't.
521 Furthermore, there might be other uses of w; if so, inlining w in 
522 v's rhs will duplicate w's rhs, whereas replacing v by w doesn't.
523
524 HOWEVER, we have to be careful if w is something that *must* be
525 inlined.  In particular, its binding may have been dropped.  Here's
526 an example that actually happened:
527         let x = let y = e in y
528      in f x
529 The "let y" was floated out, and then (since y occurs once in a
530 definitely inlinable position) the binding was dropped, leaving
531         {y=e} let x = y in f x
532 But now using the reasoning of this little section, 
533 y wasn't inlined, because it was a let x=y form.
534
535 \begin{code}
536 simplRhsExpr env binder@(id,occ_info) (Var v) new_id
537  | maybeToBool maybe_stop_at_var
538  = returnSmpl (Var the_var, getIdArity the_var)
539  where
540    maybe_stop_at_var 
541      = case (runEager $ lookupId env v) of
542          VarArg v' | not (must_unfold v') -> Just v'
543          other                            -> Nothing
544
545    Just the_var = maybe_stop_at_var
546
547    must_unfold v' =  idMustBeINLINEd v'
548                   || case lookupOutIdEnv env v' of
549                         Just (_, _, InUnfolding _ _) -> True
550                         other                        -> False
551 \end{code}
552
553 \begin{code}
554 simplRhsExpr env binder@(id,occ_info) rhs new_id
555   | maybeToBool (maybeAppDataTyCon rhs_ty)
556         -- Deal with the data type case, in which case the elaborate
557         -- eta-expansion nonsense is really quite a waste of time.
558   = simplExpr rhs_env rhs [] rhs_ty             `thenSmpl` \ rhs' ->
559     returnSmpl (rhs', ArityExactly 0)
560
561   | otherwise   -- OK, use the big hammer
562   =     -- Deal with the big lambda part
563     ASSERT( null uvars )        -- For now
564
565     mapSmpl cloneTyVarSmpl tyvars                       `thenSmpl` \ tyvars' ->
566     let
567         new_tys  = mkTyVarTys tyvars'
568         body_ty  = foldl applyTy rhs_ty new_tys
569         lam_env  = extendTyEnvList rhs_env (zipEqual "simplRhsExpr" tyvars new_tys)
570     in
571         -- Deal with the little lambda part
572         -- Note that we call simplLam even if there are no binders,
573         -- in case it can do arity expansion.
574     simplValLam lam_env body (getBinderInfoArity occ_info) body_ty      `thenSmpl` \ (lambda', arity) ->
575
576         -- Put on the big lambdas, trying to float out any bindings caught inside
577     mkRhsTyLam tyvars' lambda'                                  `thenSmpl` \ rhs' ->
578
579     returnSmpl (rhs', arity)
580   where
581     rhs_ty  = idType new_id
582     rhs_env | idWantsToBeINLINEd id     -- Don't ever inline in a INLINE thing's rhs
583             = switchOffInlining env1    -- See comments with switchOffInlining
584             | otherwise 
585             = env1
586
587         -- The top level "enclosing CC" is "SUBSUMED".  But the enclosing CC
588         -- for the rhs of top level defs is "OST_CENTRE".  Consider
589         --      f = \x -> e
590         --      g = \y -> let v = f y in scc "x" (v ...)
591         -- Here we want to inline "f", since its CC is SUBSUMED, but we don't
592         -- want to inline "v" since its CC is dynamically determined.
593
594     current_cc = getEnclosingCC env
595     env1 | costsAreSubsumed current_cc = setEnclosingCC env useCurrentCostCentre
596          | otherwise                   = env
597
598     (uvars, tyvars, body) = collectUsageAndTyBinders rhs
599 \end{code}
600
601
602 %************************************************************************
603 %*                                                                      *
604 \subsection{Simplify a lambda abstraction}
605 %*                                                                      *
606 %************************************************************************
607
608 Simplify (\binders -> body) trying eta expansion and reduction, given that
609 the abstraction will always be applied to at least min_no_of_args.
610
611 \begin{code}
612 simplValLam env expr min_no_of_args expr_ty
613   | not (switchIsSet env SimplDoLambdaEtaExpansion) ||  -- Bale out if eta expansion off
614
615     exprIsTrivial expr                              ||  -- or it's a trivial RHS
616         -- No eta expansion for trivial RHSs
617         -- It's rather a Bad Thing to expand
618         --      g = f alpha beta
619         -- to
620         --      g = \a b c -> f alpha beta a b c
621         --
622         -- The original RHS is "trivial" (exprIsTrivial), because it generates
623         -- no code (renames f to g).  But the new RHS isn't.
624
625     null potential_extra_binder_tys                 ||  -- or ain't a function
626     no_of_extra_binders <= 0                            -- or no extra binders needed
627   = cloneIds env binders                `thenSmpl` \ binders' ->
628     let
629         new_env = extendIdEnvWithClones env binders binders'
630     in
631     simplExpr new_env body [] body_ty           `thenSmpl` \ body' ->
632     returnSmpl (mkValLam binders' body', final_arity)
633
634   | otherwise                           -- Eta expansion possible
635   = -- A SSERT( no_of_extra_binders <= length potential_extra_binder_tys )
636     (if not ( no_of_extra_binders <= length potential_extra_binder_tys ) then
637         pprTrace "simplValLam" (vcat [ppr PprDebug expr, 
638                                           ppr PprDebug expr_ty,
639                                           ppr PprDebug binders,
640                                           int no_of_extra_binders,
641                                           ppr PprDebug potential_extra_binder_tys])
642     else \x -> x) $
643
644     tick EtaExpansion                   `thenSmpl_`
645     cloneIds env binders                `thenSmpl` \ binders' ->
646     let
647         new_env = extendIdEnvWithClones env binders binders'
648     in
649     newIds extra_binder_tys                                             `thenSmpl` \ extra_binders' ->
650     simplExpr new_env body (map VarArg extra_binders') etad_body_ty     `thenSmpl` \ body' ->
651     returnSmpl (
652       mkValLam (binders' ++ extra_binders') body',
653       final_arity
654     )
655
656   where
657     (binders,body)             = collectValBinders expr
658     no_of_binders              = length binders
659     (arg_tys, res_ty)          = splitFunTyExpandingDicts expr_ty
660     potential_extra_binder_tys = (if not (no_of_binders <= length arg_tys) then
661                                         pprTrace "simplValLam" (vcat [ppr PprDebug expr, 
662                                                                           ppr PprDebug expr_ty,
663                                                                           ppr PprDebug binders])
664                                   else \x->x) $
665                                  drop no_of_binders arg_tys
666     body_ty                    = mkFunTys potential_extra_binder_tys res_ty
667
668         -- Note: it's possible that simplValLam will be applied to something
669         -- with a forall type.  Eg when being applied to the rhs of
670         --              let x = wurble
671         -- where wurble has a forall-type, but no big lambdas at the top.
672         -- We could be clever an insert new big lambdas, but we don't bother.
673
674     etad_body_ty        = mkFunTys (drop no_of_extra_binders potential_extra_binder_tys) res_ty
675     extra_binder_tys    = take no_of_extra_binders potential_extra_binder_tys
676     final_arity         = atLeastArity (no_of_binders + no_of_extra_binders)
677
678     no_of_extra_binders =       -- First, use the info about how many args it's
679                                 -- always applied to in its scope; but ignore this
680                                 -- info for thunks. To see why we ignore it for thunks,
681                                 -- consider     let f = lookup env key in (f 1, f 2)
682                                 -- We'd better not eta expand f just because it is 
683                                 -- always applied!
684                            (min_no_of_args - no_of_binders)
685
686                                 -- Next, try seeing if there's a lambda hidden inside
687                                 -- something cheap.
688                                 -- etaExpandCount can reuturn a huge number (like 10000!) if
689                                 -- it finds that the body is a call to "error"; hence
690                                 -- the use of "min" here.
691                            `max`
692                            (etaExpandCount body `min` length potential_extra_binder_tys)
693
694                                 -- Finally, see if it's a state transformer, in which
695                                 -- case we eta-expand on principle! This can waste work,
696                                 -- but usually doesn't
697                            `max`
698                            case potential_extra_binder_tys of
699                                 [ty] | ty `eqTy` realWorldStateTy -> 1
700                                 other                             -> 0
701 \end{code}
702
703
704
705 %************************************************************************
706 %*                                                                      *
707 \subsection[Simplify-coerce]{Coerce expressions}
708 %*                                                                      *
709 %************************************************************************
710
711 \begin{code}
712 -- (coerce (case s of p -> r)) args ==> case s of p -> (coerce r) args
713 simplCoerce env coercion ty expr@(Case scrut alts) args result_ty
714   = simplCase env scrut alts (\env rhs -> simplCoerce env coercion ty rhs args result_ty) result_ty
715
716 -- (coerce (let defns in b)) args  ==> let defns' in (coerce b) args
717 simplCoerce env coercion ty (Let bind body) args result_ty
718   = simplBind env bind (\env -> simplCoerce env coercion ty body args result_ty) result_ty
719
720 -- Default case
721 simplCoerce env coercion ty expr args result_ty
722   = simplTy env ty                      `appEager` \ ty' ->
723     simplTy env expr_ty                 `appEager` \ expr_ty' ->
724     simplExpr env expr [] expr_ty'      `thenSmpl` \ expr' ->
725     returnSmpl (mkGenApp (mkCoerce coercion ty' expr') args)
726   where
727     expr_ty = coreExprType (unTagBinders expr)  -- Rather like simplCase other_scrut
728
729         -- Try cancellation; we do this "on the way up" because
730         -- I think that's where it'll bite best
731     mkCoerce (CoerceOut con1) ty1 (Coerce (CoerceIn  con2) ty2 body) | con1 == con2 = body
732     mkCoerce coercion ty  body = Coerce coercion ty body
733 \end{code}
734
735
736 %************************************************************************
737 %*                                                                      *
738 \subsection[Simplify-bind]{Binding groups}
739 %*                                                                      *
740 %************************************************************************
741
742 \begin{code}
743 simplBind :: SimplEnv
744           -> InBinding
745           -> (SimplEnv -> SmplM OutExpr)
746           -> OutType
747           -> SmplM OutExpr
748
749 simplBind env (NonRec binder rhs) body_c body_ty = simplNonRec env binder rhs body_c body_ty
750 simplBind env (Rec pairs)         body_c body_ty = simplRec    env pairs      body_c body_ty
751 \end{code}
752
753
754 %************************************************************************
755 %*                                                                      *
756 \subsection[Simplify-let]{Let-expressions}
757 %*                                                                      *
758 %************************************************************************
759
760 Float switches
761 ~~~~~~~~~~~~~~
762 The booleans controlling floating have to be set with a little care.
763 Here's one performance bug I found:
764
765         let x = let y = let z = case a# +# 1 of {b# -> E1}
766                         in E2
767                 in E3
768         in E4
769
770 Now, if E2, E3 aren't HNFs we won't float the y-binding or the z-binding.
771 Before case_floating_ok included float_exposes_hnf, the case expression was floated
772 *one level per simplifier iteration* outwards.  So it made th s
773
774
775 Floating case from let
776 ~~~~~~~~~~~~~~~~~~~~~~
777 When floating cases out of lets, remember this:
778
779         let x* = case e of alts
780         in <small expr>
781
782 where x* is sure to be demanded or e is a cheap operation that cannot
783 fail, e.g. unboxed addition.  Here we should be prepared to duplicate
784 <small expr>.  A good example:
785
786         let x* = case y of
787                    p1 -> build e1
788                    p2 -> build e2
789         in
790         foldr c n x*
791 ==>
792         case y of
793           p1 -> foldr c n (build e1)
794           p2 -> foldr c n (build e2)
795
796 NEW: We use the same machinery that we use for case-of-case to
797 *always* do case floating from let, that is we let bind and abstract
798 the original let body, and let the occurrence analyser later decide
799 whether the new let should be inlined or not. The example above
800 becomes:
801
802 ==>
803       let join_body x' = foldr c n x'
804         in case y of
805         p1 -> let x* = build e1
806                 in join_body x*
807         p2 -> let x* = build e2
808                 in join_body x*
809
810 note that join_body is a let-no-escape.
811 In this particular example join_body will later be inlined,
812 achieving the same effect.
813 ToDo: check this is OK with andy
814
815
816 Let to case: two points
817 ~~~~~~~~~~~
818
819 Point 1.  We defer let-to-case for all data types except single-constructor
820 ones.  Suppose we change
821
822         let x* = e in b
823 to
824         case e of x -> b
825
826 It can be the case that we find that b ultimately contains ...(case x of ..)....
827 and this is the only occurrence of x.  Then if we've done let-to-case
828 we can't inline x, which is a real pain.  On the other hand, we lose no
829 transformations by not doing this transformation, because the relevant
830 case-of-X transformations are also implemented by simpl_bind.
831
832 If x is a single-constructor type, then we go ahead anyway, giving
833
834         case e of (y,z) -> let x = (y,z) in b
835
836 because now we can squash case-on-x wherever they occur in b.
837
838 We do let-to-case on multi-constructor types in the tidy-up phase
839 (tidyCoreExpr) mainly so that the code generator doesn't need to
840 spot the demand-flag.
841
842
843 Point 2.  It's important to try let-to-case before doing the
844 strict-let-of-case transformation, which happens in the next equation
845 for simpl_bind.
846
847         let a*::Int = case v of {p1->e1; p2->e2}
848         in b
849
850 (The * means that a is sure to be demanded.)
851 If we do case-floating first we get this:
852
853         let k = \a* -> b
854         in case v of
855                 p1-> let a*=e1 in k a
856                 p2-> let a*=e2 in k a
857
858 Now watch what happens if we do let-to-case first:
859
860         case (case v of {p1->e1; p2->e2}) of
861           Int a# -> let a*=I# a# in b
862 ===>
863         let k = \a# -> let a*=I# a# in b
864         in case v of
865                 p1 -> case e1 of I# a# -> k a#
866                 p1 -> case e2 of I# a# -> k a#
867
868 The latter is clearly better.  (Remember the reboxing let-decl for a
869 is likely to go away, because after all b is strict in a.)
870
871 We do not do let to case for WHNFs, e.g.
872
873           let x = a:b in ...
874           =/=>
875           case a:b of x in ...
876
877 as this is less efficient.  but we don't mind doing let-to-case for
878 "bottom", as that will allow us to remove more dead code, if anything:
879
880           let x = error in ...
881           ===>
882           case error  of x -> ...
883           ===>
884           error
885
886 Notice that let to case occurs only if x is used strictly in its body
887 (obviously).
888
889
890 \begin{code}
891 -- Dead code is now discarded by the occurrence analyser,
892
893 simplNonRec env binder@(id,occ_info) rhs body_c body_ty
894   | inlineUnconditionally ok_to_dup id occ_info
895   =     -- The binder is used in definitely-inline way in the body
896         -- So add it to the environment, drop the binding, and continue
897     body_c (extendEnvGivenInlining env id occ_info rhs)
898
899   | idWantsToBeINLINEd id
900   = complete_bind env rhs       -- Don't mess about with floating or let-to-case on
901                                 -- INLINE things
902   | otherwise
903   = simpl_bind env rhs
904   where
905     -- Try let-to-case; see notes below about let-to-case
906     simpl_bind env rhs | try_let_to_case &&
907                          will_be_demanded &&
908                          (rhs_is_bot ||
909                           not rhs_is_whnf &&
910                           singleConstructorType rhs_ty
911                                 -- Only do let-to-case for single constructor types. 
912                                 -- For other types we defer doing it until the tidy-up phase at
913                                 -- the end of simplification.
914                          )
915       = tick Let2Case                           `thenSmpl_`
916         simplCase env rhs (AlgAlts [] (BindDefault binder (Var id)))
917                           (\env rhs -> complete_bind env rhs) body_ty
918                 -- OLD COMMENT:  [now the new RHS is only "x" so there's less worry]
919                 -- NB: it's tidier to call complete_bind not simpl_bind, else
920                 -- we nearly end up in a loop.  Consider:
921                 --      let x = rhs in b
922                 -- ==>  case rhs of (p,q) -> let x=(p,q) in b
923                 -- This effectively what the above simplCase call does.
924                 -- Now, the inner let is a let-to-case target again!  Actually, since
925                 -- the RHS is in WHNF it won't happen, but it's a close thing!
926
927     -- Try let-from-let
928     simpl_bind env (Let bind rhs) | let_floating_ok
929       = tick LetFloatFromLet                    `thenSmpl_`
930         simplBind env (fix_up_demandedness will_be_demanded bind)
931                       (\env -> simpl_bind env rhs) body_ty
932
933     -- Try case-from-let; this deals with a strict let of error too
934     simpl_bind env (Case scrut alts) | case_floating_ok scrut
935       = tick CaseFloatFromLet                           `thenSmpl_`
936
937         -- First, bind large let-body if necessary
938         if ok_to_dup || isSingleton (nonErrorRHSs alts)
939         then
940             simplCase env scrut alts (\env rhs -> simpl_bind env rhs) body_ty
941         else
942             bindLargeRhs env [binder] body_ty body_c    `thenSmpl` \ (extra_binding, new_body) ->
943             let
944                 body_c' = \env -> simplExpr env new_body [] body_ty
945                 case_c  = \env rhs -> simplNonRec env binder rhs body_c' body_ty
946             in
947             simplCase env scrut alts case_c body_ty     `thenSmpl` \ case_expr ->
948             returnSmpl (Let extra_binding case_expr)
949
950     -- None of the above; simplify rhs and tidy up
951     simpl_bind env rhs = complete_bind env rhs
952  
953     complete_bind env rhs
954       = cloneId env binder                      `thenSmpl` \ new_id ->
955         simplRhsExpr env binder rhs new_id      `thenSmpl` \ (rhs',arity) ->
956         completeNonRec env binder 
957                 (new_id `withArity` arity) rhs' `thenSmpl` \ (new_env, binds) ->
958         body_c new_env                          `thenSmpl` \ body' ->
959         returnSmpl (mkCoLetsAny binds body')
960
961
962         -- All this stuff is computed at the start of the simpl_bind loop
963     float_lets                = switchIsSet env SimplFloatLetsExposingWHNF
964     float_primops             = switchIsSet env SimplOkToFloatPrimOps
965     ok_to_dup                 = switchIsSet env SimplOkToDupCode
966     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
967     try_let_to_case           = switchIsSet env SimplLetToCase
968     no_float                  = switchIsSet env SimplNoLetFromStrictLet
969
970     demand_info      = getIdDemandInfo id
971     will_be_demanded = willBeDemanded demand_info
972     rhs_ty           = idType id
973
974     form        = mkFormSummary rhs
975     rhs_is_bot  = case form of
976                         BottomForm -> True
977                         other      -> False
978     rhs_is_whnf = case form of
979                         VarForm -> True
980                         ValueForm -> True
981                         other -> False
982
983     float_exposes_hnf = floatExposesHNF float_lets float_primops ok_to_dup rhs
984
985     let_floating_ok  = (will_be_demanded && not no_float) ||
986                        always_float_let_from_let ||
987                        float_exposes_hnf
988
989     case_floating_ok scrut = (will_be_demanded && not no_float) || 
990                              (float_exposes_hnf && is_cheap_prim_app scrut && float_primops)
991         -- See note below 
992 \end{code}
993
994
995 @completeNonRec@ looks at the simplified post-floating RHS of the
996 let-expression, and decides what to do.  There's one interesting
997 aspect to this, namely constructor reuse.  Consider
998 @
999         f = \x -> case x of
1000                     (y:ys) -> y:ys
1001                     []     -> ...
1002 @
1003 Is it a good idea to replace the rhs @y:ys@ with @x@?  This depends a
1004 bit on the compiler technology, but in general I believe not. For
1005 example, here's some code from a real program:
1006 @
1007 const.Int.max.wrk{-s2516-} =
1008     \ upk.s3297#  upk.s3298# ->
1009         let {
1010           a.s3299 :: Int
1011           _N_ {-# U(P) #-}
1012           a.s3299 = I#! upk.s3297#
1013         } in
1014           case (const.Int._tagCmp.wrk{-s2513-} upk.s3297# upk.s3298#) of {
1015             _LT -> I#! upk.s3298#
1016             _EQ -> a.s3299
1017             _GT -> a.s3299
1018           }
1019 @
1020 The a.s3299 really isn't doing much good.  We'd be better off inlining
1021 it.  (Actually, let-no-escapery means it isn't as bad as it looks.)
1022
1023 So the current strategy is to inline all known-form constructors, and
1024 only do the reverse (turn a constructor application back into a
1025 variable) when we find a let-expression:
1026 @
1027         let x = C a1 .. an
1028         in
1029         ... (let y = C a1 .. an in ...) ...
1030 @
1031 where it is always good to ditch the binding for y, and replace y by
1032 x.  That's just what completeLetBinding does.
1033
1034
1035 \begin{code}
1036 {- FAILED CODE
1037    The trouble is that we keep transforming
1038                 let x = coerce e
1039                     y = coerce x
1040                 in ...
1041    to
1042                 let x' = coerce e
1043                     y' = coerce x'
1044                 in ...
1045    and counting a couple of ticks for this non-transformation
1046
1047         -- We want to ensure that all let-bound Coerces have 
1048         -- atomic bodies, so they can freely be inlined.
1049 completeNonRec env binder new_id (Coerce coercion ty rhs)
1050   | not (is_atomic rhs)
1051   = newId (coreExprType rhs)                            `thenSmpl` \ inner_id ->
1052     completeNonRec env 
1053                    (inner_id, dangerousArgOcc) inner_id rhs `thenSmpl` \ (env1, binds1) ->
1054         -- Dangerous occ because, like constructor args,
1055         -- it can be duplicated easily
1056     let
1057         atomic_rhs = case runEager $ lookupId env1 inner_id of
1058                         LitArg l -> Lit l
1059                         VarArg v -> Var v
1060     in
1061     completeNonRec env1 binder new_id
1062                    (Coerce coercion ty atomic_rhs)      `thenSmpl` \ (env2, binds2) ->
1063
1064     returnSmpl (env2, binds1 ++ binds2)
1065 -}
1066
1067
1068         -- Right hand sides that are constructors
1069         --      let v = C args
1070         --      in
1071         --- ...(let w = C same-args in ...)...
1072         -- Then use v instead of w.      This may save
1073         -- re-constructing an existing constructor.
1074 completeNonRec env binder new_id rhs@(Con con con_args)
1075   | switchIsSet env SimplReuseCon && 
1076     maybeToBool maybe_existing_con &&
1077     not (isExported new_id)             -- Don't bother for exported things
1078                                         -- because we won't be able to drop
1079                                         -- its binding.
1080   = tick ConReused              `thenSmpl_`
1081     returnSmpl (extendIdEnvWithAtom env binder (VarArg it), [NonRec new_id rhs])
1082   where
1083     maybe_existing_con = lookForConstructor env con con_args
1084     Just it            = maybe_existing_con
1085
1086
1087         -- Default case
1088         -- Check for atomic right-hand sides.
1089         -- We used to have a "tick AtomicRhs" in here, but it causes more trouble
1090         -- than it's worth.  For a top-level binding a = b, where a is exported,
1091         -- we can't drop the binding, so we get repeated AtomicRhs ticks
1092 completeNonRec env binder@(id,occ_info) new_id new_rhs
1093  | is_atomic eta'd_rhs          -- If rhs (after eta reduction) is atomic
1094  = returnSmpl (atomic_env , [NonRec new_id eta'd_rhs])
1095
1096  | otherwise                    -- Non atomic rhs (don't eta after all)
1097  = returnSmpl (non_atomic_env , [NonRec new_id new_rhs])
1098  where
1099    atomic_env = extendIdEnvWithAtom env binder the_arg
1100
1101    non_atomic_env = extendEnvGivenBinding (extendIdEnvWithClone env binder new_id)
1102                                           occ_info new_id new_rhs
1103
1104    eta'd_rhs = etaCoreExpr new_rhs
1105    the_arg   = case eta'd_rhs of
1106                   Var v -> VarArg v
1107                   Lit l -> LitArg l
1108 \end{code}
1109
1110 %************************************************************************
1111 %*                                                                      *
1112 \subsection[Simplify-letrec]{Letrec-expressions}
1113 %*                                                                      *
1114 %************************************************************************
1115
1116 Letrec expressions
1117 ~~~~~~~~~~~~~~~~~~
1118 Here's the game plan
1119
1120 1. Float any let(rec)s out of the RHSs
1121 2. Clone all the Ids and extend the envt with these clones
1122 3. Simplify one binding at a time, adding each binding to the
1123    environment once it's done.
1124
1125 This relies on the occurrence analyser to
1126         a) break all cycles with an Id marked MustNotBeInlined
1127         b) sort the decls into topological order
1128 The former prevents infinite inlinings, and the latter means
1129 that we get maximum benefit from working top to bottom.
1130
1131
1132 \begin{code}
1133 simplRec env pairs body_c body_ty
1134   =     -- Do floating, if necessary
1135     floatBind env False (Rec pairs)     `thenSmpl` \ [Rec pairs'] ->
1136     let
1137         binders = map fst pairs'
1138     in
1139     cloneIds env binders                        `thenSmpl` \ ids' ->
1140     let
1141        env_w_clones = extendIdEnvWithClones env binders ids'
1142     in
1143     simplRecursiveGroup env_w_clones ids' pairs'        `thenSmpl` \ (pairs', new_env) ->
1144
1145     body_c new_env                              `thenSmpl` \ body' ->
1146
1147     returnSmpl (Let (Rec pairs') body')
1148 \end{code}
1149
1150 \begin{code}
1151 -- The env passed to simplRecursiveGroup already has 
1152 -- bindings that clone the variables of the group.
1153 simplRecursiveGroup env new_ids []
1154   = returnSmpl ([], env)
1155
1156 simplRecursiveGroup env (new_id : new_ids) ((binder@(id, occ_info), rhs) : pairs)
1157   | inlineUnconditionally ok_to_dup id occ_info
1158   =     -- Single occurrence, so drop binding and extend env with the inlining
1159         -- This is a little delicate, because what if the unique occurrence
1160         -- is *before* this binding?  This'll never happen, because
1161         -- either it'll be marked "never inline" or else its occurrence will
1162         -- occur after its binding in the group.
1163         --
1164         -- If these claims aren't right Core Lint will spot an unbound
1165         -- variable.  A quick fix is to delete this clause for simplRecursiveGroup
1166     let
1167         new_env = extendEnvGivenInlining env new_id occ_info rhs
1168     in
1169     simplRecursiveGroup new_env new_ids pairs
1170
1171   | otherwise
1172   = simplRhsExpr env binder rhs new_id          `thenSmpl` \ (new_rhs, arity) ->
1173     let
1174         new_id' = new_id `withArity` arity
1175     
1176         -- ToDo: this next bit could usefully share code with completeNonRec
1177
1178         new_env 
1179           | idMustNotBeINLINEd new_id           -- Occurrence analyser says "don't inline"
1180           = env
1181
1182           | is_atomic eta'd_rhs                 -- If rhs (after eta reduction) is atomic
1183           = extendIdEnvWithAtom env binder the_arg
1184
1185           | otherwise                           -- Non-atomic
1186           = extendEnvGivenBinding env occ_info new_id new_rhs
1187                                                 -- Don't eta if it doesn't eliminate the binding
1188
1189         eta'd_rhs = etaCoreExpr new_rhs
1190         the_arg   = case eta'd_rhs of
1191                           Var v -> VarArg v
1192                           Lit l -> LitArg l
1193     in
1194     simplRecursiveGroup new_env new_ids pairs   `thenSmpl` \ (new_pairs, final_env) ->
1195     returnSmpl ((new_id', new_rhs) : new_pairs, final_env)   
1196   where
1197     ok_to_dup = switchIsSet env SimplOkToDupCode
1198 \end{code}
1199
1200
1201
1202 \begin{code}
1203 floatBind :: SimplEnv
1204           -> Bool                               -- True <=> Top level
1205           -> InBinding
1206           -> SmplM [InBinding]
1207
1208 floatBind env top_level bind
1209   | not float_lets ||
1210     n_extras == 0
1211   = returnSmpl [bind]
1212
1213   | otherwise      
1214   = tickN LetFloatFromLet n_extras              `thenSmpl_` 
1215                 -- It's important to increment the tick counts if we
1216                 -- do any floating.  A situation where this turns out
1217                 -- to be important is this:
1218                 -- Float in produces:
1219                 --      letrec  x = let y = Ey in Ex
1220                 --      in B
1221                 -- Now floating gives this:
1222                 --      letrec x = Ex
1223                 --             y = Ey
1224                 --      in B
1225                 --- We now want to iterate once more in case Ey doesn't
1226                 -- mention x, in which case the y binding can be pulled
1227                 -- out as an enclosing let(rec), which in turn gives
1228                 -- the strictness analyser more chance.
1229     returnSmpl binds'
1230
1231   where
1232     (binds', _, n_extras) = fltBind bind        
1233
1234     float_lets                = switchIsSet env SimplFloatLetsExposingWHNF
1235     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
1236
1237         -- fltBind guarantees not to return leaky floats
1238         -- and all the binders of the floats have had their demand-info zapped
1239     fltBind (NonRec bndr rhs)
1240       = (binds ++ [NonRec (un_demandify bndr) rhs'], 
1241          leakFree bndr rhs', 
1242          length binds)
1243       where
1244         (binds, rhs') = fltRhs rhs
1245     
1246     fltBind (Rec pairs)
1247       = ([Rec (extras
1248                ++
1249                binders `zip` rhss')],
1250          and (zipWith leakFree binders rhss'),
1251          length extras
1252         )
1253     
1254       where
1255         (binders, rhss)  = unzip pairs
1256         (binds_s, rhss') = mapAndUnzip fltRhs rhss
1257         extras           = concat (map get_pairs (concat binds_s))
1258
1259         get_pairs (NonRec bndr rhs) = [(bndr,rhs)]
1260         get_pairs (Rec pairs)       = pairs
1261     
1262         -- fltRhs has same invariant as fltBind
1263     fltRhs rhs
1264       |  (always_float_let_from_let ||
1265           floatExposesHNF True False False rhs)
1266       = fltExpr rhs
1267     
1268       | otherwise
1269       = ([], rhs)
1270     
1271     
1272         -- fltExpr has same invariant as fltBind
1273     fltExpr (Let bind body)
1274       | not top_level || binds_wont_leak
1275             -- fltExpr guarantees not to return leaky floats
1276       = (binds' ++ body_binds, body')
1277       where
1278         (body_binds, body')          = fltExpr body
1279         (binds', binds_wont_leak, _) = fltBind bind
1280     
1281     fltExpr expr = ([], expr)
1282
1283 -- Crude but effective
1284 leakFree (id,_) rhs = case getIdArity id of
1285                         ArityAtLeast n | n > 0 -> True
1286                         ArityExactly n | n > 0 -> True
1287                         other                  -> whnfOrBottom (mkFormSummary rhs)
1288 \end{code}
1289
1290
1291 %************************************************************************
1292 %*                                                                      *
1293 \subsection[Simplify-atoms]{Simplifying atoms}
1294 %*                                                                      *
1295 %************************************************************************
1296
1297 \begin{code}
1298 simplArg :: SimplEnv -> InArg -> Eager ans OutArg
1299
1300 simplArg env (LitArg lit) = returnEager (LitArg lit)
1301 simplArg env (TyArg  ty)  = simplTy env ty      `appEager` \ ty' -> 
1302                             returnEager (TyArg ty')
1303 simplArg env (VarArg id)  = lookupId env id
1304 \end{code}
1305
1306 %************************************************************************
1307 %*                                                                      *
1308 \subsection[Simplify-quickies]{Some local help functions}
1309 %*                                                                      *
1310 %************************************************************************
1311
1312
1313 \begin{code}
1314 -- fix_up_demandedness switches off the willBeDemanded Info field
1315 -- for bindings floated out of a non-demanded let
1316 fix_up_demandedness True {- Will be demanded -} bind
1317    = bind       -- Simple; no change to demand info needed
1318 fix_up_demandedness False {- May not be demanded -} (NonRec binder rhs)
1319    = NonRec (un_demandify binder) rhs
1320 fix_up_demandedness False {- May not be demanded -} (Rec pairs)
1321    = Rec [(un_demandify binder, rhs) | (binder,rhs) <- pairs]
1322
1323 un_demandify (id, occ_info) = (id `addIdDemandInfo` noDemandInfo, occ_info)
1324
1325 is_cheap_prim_app (Prim op _) = primOpOkForSpeculation op
1326 is_cheap_prim_app other       = False
1327
1328 computeResultType :: SimplEnv -> InType -> [OutArg] -> OutType
1329 computeResultType env expr_ty orig_args
1330   = simplTy env expr_ty         `appEager` \ expr_ty' ->
1331     let
1332         go ty [] = ty
1333         go ty (TyArg ty_arg : args) = go (mkAppTy ty ty_arg) args
1334         go ty (a:args) | isValArg a = case (getFunTy_maybe ty) of
1335                                         Just (_, res_ty) -> go res_ty args
1336                                         Nothing          -> 
1337                                             pprPanic "computeResultType" (vcat [
1338                                                                         ppr PprDebug (a:args),
1339                                                                         ppr PprDebug orig_args,
1340                                                                         ppr PprDebug expr_ty',
1341                                                                         ppr PprDebug ty])
1342     in
1343     go expr_ty' orig_args
1344
1345
1346 var `withArity` UnknownArity = var
1347 var `withArity` arity        = var `addIdArity` arity
1348
1349 is_atomic (Var v) = True
1350 is_atomic (Lit l) = not (isNoRepLit l)
1351 is_atomic other   = False
1352 \end{code}
1353