[project @ 1996-07-25 20:43:49 by partain]
[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 IMP_Ubiq(){-uitous-}
12 IMPORT_DELOOPER(SmplLoop)               -- paranoia checking
13 IMPORT_1_3(List(partition))
14
15 import BinderInfo
16 import CmdLineOpts      ( SimplifierSwitch(..) )
17 import ConFold          ( completePrim )
18 import CoreUnfold       ( Unfolding, SimpleUnfolding, mkFormSummary, FormSummary(..) )
19 import CostCentre       ( isSccCountCostCentre, cmpCostCentre )
20 import CoreSyn
21 import CoreUtils        ( coreExprType, nonErrorRHSs, maybeErrorApp,
22                           unTagBinders, squashableDictishCcExpr
23                         )
24 import Id               ( idType, idWantsToBeINLINEd,
25                           externallyVisibleId,
26                           getIdDemandInfo, addIdDemandInfo,
27                           GenId{-instance NamedThing-}
28                         )
29 import IdInfo           ( willBeDemanded, DemandInfo )
30 import Literal          ( isNoRepLit )
31 import Maybes           ( maybeToBool )
32 --import Name           ( isExported )
33 import PprStyle         ( PprStyle(..) )
34 import PprType          ( GenType{-instance Outputable-} )
35 import Pretty           ( ppAbove )
36 import PrimOp           ( primOpOkForSpeculation, PrimOp(..) )
37 import SimplCase        ( simplCase, bindLargeRhs )
38 import SimplEnv
39 import SimplMonad
40 import SimplVar         ( completeVar )
41 import SimplUtils
42 import Type             ( mkTyVarTy, mkTyVarTys, mkAppTy,
43                           splitFunTy, getFunTy_maybe, eqTy
44                         )
45 import TysWiredIn       ( realWorldStateTy )
46 import Util             ( isSingleton, zipEqual, panic, pprPanic, assertPanic )
47 \end{code}
48
49 The controlling flags, and what they do
50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52 passes:
53 ------
54 -fsimplify              = run the simplifier
55 -ffloat-inwards         = runs the float lets inwards pass
56 -ffloat                 = runs the full laziness pass
57                           (ToDo: rename to -ffull-laziness)
58 -fupdate-analysis       = runs update analyser
59 -fstrictness            = runs strictness analyser
60 -fsaturate-apps         = saturates applications (eta expansion)
61
62 options:
63 -------
64 -ffloat-past-lambda     = OK to do full laziness.
65                           (ToDo: remove, as the full laziness pass is
66                                  useless without this flag, therefore
67                                  it is unnecessary. Just -ffull-laziness
68                                  should be kept.)
69
70 -ffloat-lets-ok         = OK to float lets out of lets if the enclosing
71                           let is strict or if the floating will expose
72                           a WHNF [simplifier].
73
74 -ffloat-primops-ok      = OK to float out of lets cases whose scrutinee
75                           is a primop that cannot fail [simplifier].
76
77 -fcode-duplication-ok   = allows the previous option to work on cases with
78                           multiple branches [simplifier].
79
80 -flet-to-case           = does let-to-case transformation [simplifier].
81
82 -fcase-of-case          = does case of case transformation [simplifier].
83
84 -fpedantic-bottoms      = does not allow:
85                              case x of y -> e  ===>  e[x/y]
86                           (which may turn bottom into non-bottom)
87
88
89                         NOTES ON INLINING
90                         ~~~~~~~~~~~~~~~~~
91
92 Inlining is one of the delicate aspects of the simplifier.  By
93 ``inlining'' we mean replacing an occurrence of a variable ``x'' by
94 the RHS of x's definition.  Thus
95
96         let x = e in ...x...    ===>   let x = e in ...e...
97
98 We have two mechanisms for inlining:
99
100 1.  Unconditional.  The occurrence analyser has pinned an (OneOcc
101 FunOcc NoDupDanger NotInsideSCC n) flag on the variable, saying ``it's
102 certainly safe to inline this variable, and to drop its binding''.
103 (...Umm... if n <= 1; if n > 1, it is still safe, provided you are
104 happy to be duplicating code...) When it encounters such a beast, the
105 simplifer binds the variable to its RHS (in the id_env) and continues.
106 It doesn't even look at the RHS at that stage.  It also drops the
107 binding altogether.
108
109 2.  Conditional.  In all other situations, the simplifer simplifies
110 the RHS anyway, and keeps the new binding.  It also binds the new
111 (cloned) variable to a ``suitable'' Unfolding in the UnfoldEnv.
112
113 Here, ``suitable'' might mean NoUnfolding (if the occurrence
114 info is ManyOcc and the RHS is not a manifest HNF, or UnfoldAlways (if
115 the variable has an INLINE pragma on it).  The idea is that anything
116 in the UnfoldEnv is safe to use, but also has an enclosing binding if
117 you decide not to use it.
118
119 Head normal forms
120 ~~~~~~~~~~~~~~~~~
121 We *never* put a non-HNF unfolding in the UnfoldEnv except in the
122 INLINE-pragma case.
123
124 At one time I thought it would be OK to put non-HNF unfoldings in for
125 variables which occur only once [if they got inlined at that
126 occurrence the RHS of the binding would become dead, so no duplication
127 would occur].   But consider:
128 @
129         let x = <expensive>
130             f = \y -> ...y...y...y...
131         in f x
132 @
133 Now, it seems that @x@ appears only once, but even so it is NOT safe
134 to put @x@ in the UnfoldEnv, because @f@ will be inlined, and will
135 duplicate the references to @x@.
136
137 Because of this, the "unconditional-inline" mechanism above is the
138 only way in which non-HNFs can get inlined.
139
140 INLINE pragmas
141 ~~~~~~~~~~~~~~
142
143 When a variable has an INLINE pragma on it --- which includes wrappers
144 produced by the strictness analyser --- we treat it rather carefully.
145
146 For a start, we are careful not to substitute into its RHS, because
147 that might make it BIG, and the user said "inline exactly this", not
148 "inline whatever you get after inlining other stuff inside me".  For
149 example
150
151         let f = BIG
152         in {-# INLINE y #-} y = f 3
153         in ...y...y...
154
155 Here we don't want to substitute BIG for the (single) occurrence of f,
156 because then we'd duplicate BIG when we inline'd y.  (Exception:
157 things in the UnfoldEnv with UnfoldAlways flags, which originated in
158 other INLINE pragmas.)
159
160 So, we clean out the UnfoldEnv of all SimpleUnfolding inlinings before
161 going into such an RHS.
162
163 What about imports?  They don't really matter much because we only
164 inline relatively small things via imports.
165
166 We augment the the UnfoldEnv with UnfoldAlways guidance if there's an
167 INLINE pragma.  We also do this for the RHSs of recursive decls,
168 before looking at the recursive decls. That way we achieve the effect
169 of inlining a wrapper in the body of its worker, in the case of a
170 mutually-recursive worker/wrapper split.
171
172
173 %************************************************************************
174 %*                                                                      *
175 \subsection[Simplify-simplExpr]{The main function: simplExpr}
176 %*                                                                      *
177 %************************************************************************
178
179 At the top level things are a little different.
180
181   * No cloning (not allowed for exported Ids, unnecessary for the others)
182
183   * No floating.   Case floating is obviously out.  Let floating is
184         theoretically OK, but dangerous because of space leaks.
185         The long-distance let-floater lifts these lets.
186
187 \begin{code}
188 simplTopBinds :: SimplEnv -> [InBinding] -> SmplM [OutBinding]
189
190 simplTopBinds env [] = returnSmpl []
191
192 -- Dead code is now discarded by the occurrence analyser,
193
194 simplTopBinds env (NonRec binder@(in_id,occ_info) rhs : binds)
195   =     -- No cloning necessary at top level
196         -- Process the binding
197     simplRhsExpr env binder rhs                 `thenSmpl` \ rhs' ->
198     completeNonRec env binder in_id rhs'        `thenSmpl` \ (new_env, binds1') ->
199
200         -- Process the other bindings
201     simplTopBinds new_env binds `thenSmpl` \ binds2' ->
202
203         -- Glue together and return ...
204     returnSmpl (binds1' ++ binds2')
205
206 simplTopBinds env (Rec pairs : binds)
207   = simplRecursiveGroup env ids pairs   `thenSmpl` \ (bind', new_env) ->
208
209         -- Process the other bindings
210     simplTopBinds new_env binds         `thenSmpl` \ binds' ->
211
212         -- Glue together and return
213     returnSmpl (bind' : binds')
214   where
215     ids = [id | (binder@(id,_), rhs) <- pairs]  -- No cloning necessary at top level
216 \end{code}
217
218 %************************************************************************
219 %*                                                                      *
220 \subsection[Simplify-simplExpr]{The main function: simplExpr}
221 %*                                                                      *
222 %************************************************************************
223
224
225 \begin{code}
226 simplExpr :: SimplEnv
227           -> InExpr -> [OutArg]
228           -> SmplM OutExpr
229 \end{code}
230
231 The expression returned has the same meaning as the input expression
232 applied to the specified arguments.
233
234
235 Variables
236 ~~~~~~~~~
237 Check if there's a macro-expansion, and if so rattle on.  Otherwise do
238 the more sophisticated stuff.
239
240 \begin{code}
241 simplExpr env (Var v) args
242   = case (lookupId env v) of
243       LitArg lit                -- A boring old literal
244         -> ASSERT( null args )
245            returnSmpl (Lit lit)
246
247       VarArg var        -- More interesting!  An id!
248         -> completeVar env var args
249                                 -- Either Id is in the local envt, or it's a global.
250                                 -- In either case we don't need to apply the type
251                                 -- environment to it.
252 \end{code}
253
254 Literals
255 ~~~~~~~~
256
257 \begin{code}
258 simplExpr env (Lit l) [] = returnSmpl (Lit l)
259 #ifdef DEBUG
260 simplExpr env (Lit l) _  = panic "simplExpr:Lit with argument"
261 #endif
262 \end{code}
263
264 Primitive applications are simple.
265 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266
267 NB: Prim expects an empty argument list! (Because it should be
268 saturated and not higher-order. ADR)
269
270 \begin{code}
271 simplExpr env (Prim op prim_args) args
272   = ASSERT (null args)
273     let
274         prim_args' = [simplArg env prim_arg | prim_arg <- prim_args]
275         op'        = simpl_op op
276     in
277     completePrim env op' prim_args'
278   where
279     -- PrimOps just need any types in them renamed.
280
281     simpl_op (CCallOp label is_asm may_gc arg_tys result_ty)
282       = let
283             arg_tys'    = map (simplTy env) arg_tys
284             result_ty'  = simplTy env result_ty
285         in
286         CCallOp label is_asm may_gc arg_tys' result_ty'
287
288     simpl_op other_op = other_op
289 \end{code}
290
291 Constructor applications
292 ~~~~~~~~~~~~~~~~~~~~~~~~
293 Nothing to try here.  We only reuse constructors when they appear as the
294 rhs of a let binding (see completeLetBinding).
295
296 \begin{code}
297 simplExpr env (Con con con_args) args
298   = ASSERT( null args )
299     returnSmpl (Con con [simplArg env con_arg | con_arg <- con_args])
300 \end{code}
301
302
303 Applications are easy too:
304 ~~~~~~~~~~~~~~~~~~~~~~~~~~
305 Just stuff 'em in the arg stack
306
307 \begin{code}
308 simplExpr env (App fun arg) args
309   = simplExpr env fun (simplArg env arg : args)
310 \end{code}
311
312 Type lambdas
313 ~~~~~~~~~~~~
314
315 We only eta-reduce a type lambda if all type arguments in the body can
316 be eta-reduced. This requires us to collect up all tyvar parameters so
317 we can pass them all to @mkTyLamTryingEta@.
318
319 \begin{code}
320 simplExpr env (Lam (TyBinder tyvar) body) (TyArg ty : args)
321   = -- ASSERT(not (isPrimType ty))
322     tick TyBetaReduction        `thenSmpl_`
323     simplExpr (extendTyEnv env tyvar ty) body args
324
325 simplExpr env tylam@(Lam (TyBinder tyvar) body) []
326   = do_tylambdas env [] tylam
327   where
328     do_tylambdas env tyvars' (Lam (TyBinder tyvar) body)
329       =   -- Clone the type variable
330         cloneTyVarSmpl tyvar            `thenSmpl` \ tyvar' ->
331         let
332             new_env = extendTyEnv env tyvar (mkTyVarTy tyvar')
333         in
334         do_tylambdas new_env (tyvar':tyvars') body
335
336     do_tylambdas env tyvars' body
337       = simplExpr env body []           `thenSmpl` \ body' ->
338         returnSmpl (
339            (if switchIsSet env SimplDoEtaReduction
340            then mkTyLamTryingEta
341            else mkTyLam) (reverse tyvars')  body'
342         )
343
344 #ifdef DEBUG
345 simplExpr env (Lam (TyBinder _) _) (_ : _)
346   = panic "simplExpr:TyLam with non-TyArg"
347 #endif
348 \end{code}
349
350
351 Ordinary lambdas
352 ~~~~~~~~~~~~~~~~
353
354 There's a complication with lambdas that aren't saturated.
355 Suppose we have:
356
357         (\x. \y. ...x...)
358
359 If we did nothing, x is used inside the \y, so would be marked
360 as dangerous to dup.  But in the common case where the abstraction
361 is applied to two arguments this is over-pessimistic.
362 So instead we don't take account of the \y when dealing with x's usage;
363 instead, the simplifier is careful when partially applying lambdas.
364
365 \begin{code}
366 simplExpr env expr@(Lam (ValBinder binder) body) orig_args
367   = go 0 env expr orig_args
368   where
369     go n env (Lam (ValBinder binder) body) (val_arg : args)
370       | isValArg val_arg                -- The lambda has an argument
371       = tick BetaReduction      `thenSmpl_`
372         go (n+1) (extendIdEnvWithAtom env binder val_arg) body args
373
374     go n env expr@(Lam (ValBinder binder) body) args
375         -- The lambda is un-saturated, so we must zap the occurrence info
376         -- on the arguments we've already beta-reduced into the body of the lambda
377       = ASSERT( null args )     -- Value lambda must match value argument!
378         let
379             new_env = markDangerousOccs env (take n orig_args)
380         in
381         simplValLam new_env expr 0 {- Guaranteed applied to at least 0 args! -}
382
383     go n env non_val_lam_expr args      -- The lambda had enough arguments
384       = simplExpr env non_val_lam_expr args
385 \end{code}
386
387
388 Let expressions
389 ~~~~~~~~~~~~~~~
390
391 \begin{code}
392 simplExpr env (Let bind body) args
393   = simplBind env bind (\env -> simplExpr env body args)
394                        (computeResultType env body args)
395 \end{code}
396
397 Case expressions
398 ~~~~~~~~~~~~~~~~
399
400 \begin{code}
401 simplExpr env expr@(Case scrut alts) args
402   = simplCase env scrut alts (\env rhs -> simplExpr env rhs args)
403                              (computeResultType env expr args)
404 \end{code}
405
406
407 Coercions
408 ~~~~~~~~~
409 \begin{code}
410 simplExpr env (Coerce coercion ty body) args
411   = simplCoerce env coercion ty body args 
412 \end{code}
413
414
415 Set-cost-centre
416 ~~~~~~~~~~~~~~~
417
418 1) Eliminating nested sccs ...
419 We must be careful to maintain the scc counts ...
420
421 \begin{code}
422 simplExpr env (SCC cc1 (SCC cc2 expr)) args
423   | not (isSccCountCostCentre cc2) && case cmpCostCentre cc1 cc2 of { EQ_ -> True; _ -> False }
424         -- eliminate inner scc if no call counts and same cc as outer
425   = simplExpr env (SCC cc1 expr) args
426
427   | not (isSccCountCostCentre cc2) && not (isSccCountCostCentre cc1)
428         -- eliminate outer scc if no call counts associated with either ccs
429   = simplExpr env (SCC cc2 expr) args
430 \end{code}
431
432 2) Moving sccs inside lambdas ...
433   
434 \begin{code}
435 simplExpr env (SCC cc (Lam binder@(ValBinder _) body)) args
436   | not (isSccCountCostCentre cc)
437         -- move scc inside lambda only if no call counts
438   = simplExpr env (Lam binder (SCC cc body)) args
439
440 simplExpr env (SCC cc (Lam binder body)) args
441         -- always ok to move scc inside type/usage lambda
442   = simplExpr env (Lam binder (SCC cc body)) args
443 \end{code}
444
445 3) Eliminating dict sccs ...
446
447 \begin{code}
448 simplExpr env (SCC cc expr) args
449   | squashableDictishCcExpr cc expr
450         -- eliminate dict cc if trivial dict expression
451   = simplExpr env expr args
452 \end{code}
453
454 4) Moving arguments inside the body of an scc ...
455 This moves the cost of doing the application inside the scc
456 (which may include the cost of extracting methods etc)
457
458 \begin{code}
459 simplExpr env (SCC cost_centre body) args
460   = let
461         new_env = setEnclosingCC env cost_centre
462     in
463     simplExpr new_env body args         `thenSmpl` \ body' ->
464     returnSmpl (SCC cost_centre body')
465 \end{code}
466
467 %************************************************************************
468 %*                                                                      *
469 \subsection{Simplify RHS of a Let/Letrec}
470 %*                                                                      *
471 %************************************************************************
472
473 simplRhsExpr does arity-expansion.  That is, given:
474
475         * a right hand side /\ tyvars -> \a1 ... an -> e
476         * the information (stored in BinderInfo) that the function will always
477           be applied to at least k arguments
478
479 it transforms the rhs to
480
481         /\tyvars -> \a1 ... an b(n+1) ... bk -> (e b(n+1) ... bk)
482
483 This is a Very Good Thing!
484
485 \begin{code}
486 simplRhsExpr
487         :: SimplEnv
488         -> InBinder
489         -> InExpr
490         -> SmplM OutExpr
491
492 simplRhsExpr env binder@(id,occ_info) rhs
493   | dont_eta_expand rhs
494   = simplExpr rhs_env rhs []
495
496   | otherwise   -- Have a go at eta expansion
497   =     -- Deal with the big lambda part
498     ASSERT( null uvars )        -- For now
499
500     mapSmpl cloneTyVarSmpl tyvars                       `thenSmpl` \ tyvars' ->
501     let
502         lam_env  = extendTyEnvList rhs_env (zipEqual "simplRhsExpr" tyvars (mkTyVarTys tyvars'))
503     in
504         -- Deal with the little lambda part
505         -- Note that we call simplLam even if there are no binders,
506         -- in case it can do arity expansion.
507     simplValLam lam_env body (getBinderInfoArity occ_info)      `thenSmpl` \ lambda' ->
508
509         -- Put it back together
510     returnSmpl (
511        (if switchIsSet env SimplDoEtaReduction
512        then mkTyLamTryingEta
513        else mkTyLam) tyvars' lambda'
514     )
515   where
516
517     rhs_env | not (switchIsSet env IgnoreINLINEPragma) &&
518               idWantsToBeINLINEd id
519             = switchOffInlining env
520             | otherwise 
521             = env
522
523         -- Switch off all inlining in the RHS of things that have an INLINE pragma.
524         -- They are going to be inlined wherever they are used, and then all the
525         -- inlining will take effect.  Meanwhile, there isn't
526         -- much point in doing anything to the as-yet-un-INLINEd rhs.
527         -- It's very important to switch off inlining!  Consider:
528         --
529         -- let f = \pq -> BIG
530         -- in
531         -- let g = \y -> f y y
532         --     {-# INLINE g #-}
533         -- in ...g...g...g...g...g...
534         --
535         -- Now, if that's the ONLY occurrence of f, it will be inlined inside g,
536         -- and thence copied multiple times when g is inlined.
537
538         -- Andy disagrees! Example:
539         --      all xs = foldr (&&) True xs
540         --      any p = all . map p  {-# INLINE any #-}
541         --
542         -- Problem: any won't get deforested, and so if it's exported and
543         -- the importer doesn't use the inlining, (eg passes it as an arg)
544         -- then we won't get deforestation at all.
545         -- We havn't solved this problem yet!
546
547     (uvars, tyvars, body) = collectUsageAndTyBinders rhs
548
549         -- dont_eta_expand prevents eta expansion in silly situations.
550         -- For example, consider the defn
551         --      x = y
552         -- It would be silly to eta expand the "y", because it would just
553         -- get eta-reduced back to y.  Furthermore, if this was a top level defn,
554         -- and x was exported, then the defn won't be eliminated, so this
555         -- silly expand/reduce cycle will happen every time, which makes the
556         -- simplifier loop!.
557         -- The solution is to not even try eta expansion unless the rhs looks
558         -- non-trivial.
559     dont_eta_expand (Lit _)     = True
560     dont_eta_expand (Var _)     = True
561     dont_eta_expand (Con _ _)   = True
562     dont_eta_expand (App f a)
563       | notValArg    a          = dont_eta_expand f
564     dont_eta_expand (Lam x b)
565       | notValBinder x          = dont_eta_expand b
566     dont_eta_expand _           = False
567 \end{code}
568
569
570 %************************************************************************
571 %*                                                                      *
572 \subsection{Simplify a lambda abstraction}
573 %*                                                                      *
574 %************************************************************************
575
576 Simplify (\binders -> body) trying eta expansion and reduction, given that
577 the abstraction will always be applied to at least min_no_of_args.
578
579 \begin{code}
580 simplValLam env expr min_no_of_args
581   | not (switchIsSet env SimplDoLambdaEtaExpansion) ||  -- Bale out if eta expansion off
582     null binders                                    ||  -- or it's a thunk
583     null potential_extra_binder_tys                 ||  -- or ain't a function
584     no_of_extra_binders <= 0                            -- or no extra binders needed
585   = cloneIds env binders                `thenSmpl` \ binders' ->
586     let
587         new_env = extendIdEnvWithClones env binders binders'
588     in
589     simplExpr new_env body []           `thenSmpl` \ body' ->
590     returnSmpl (
591       (if switchIsSet new_env SimplDoEtaReduction
592        then mkValLamTryingEta
593        else mkValLam) binders' body'
594     )
595
596   | otherwise                           -- Eta expansion possible
597   = tick EtaExpansion                   `thenSmpl_`
598     cloneIds env binders                `thenSmpl` \ binders' ->
599     let
600         new_env = extendIdEnvWithClones env binders binders'
601     in
602     newIds extra_binder_tys                             `thenSmpl` \ extra_binders' ->
603     simplExpr new_env body (map VarArg extra_binders')  `thenSmpl` \ body' ->
604     returnSmpl (
605       (if switchIsSet new_env SimplDoEtaReduction
606        then mkValLamTryingEta
607        else mkValLam) (binders' ++ extra_binders') body'
608     )
609
610   where
611     (binders,body) = collectValBinders expr
612     (potential_extra_binder_tys, res_ty)
613         = splitFunTy (simplTy env (coreExprType (unTagBinders body)))
614         -- Note: it's possible that simplValLam will be applied to something
615         -- with a forall type.  Eg when being applied to the rhs of
616         --              let x = wurble
617         -- where wurble has a forall-type, but no big lambdas at the top.
618         -- We could be clever an insert new big lambdas, but we don't bother.
619
620     extra_binder_tys = take no_of_extra_binders potential_extra_binder_tys
621
622     no_of_extra_binders =       -- First, use the info about how many args it's
623                                 -- always applied to in its scope
624                            (min_no_of_args - length binders)
625
626                                 -- Next, try seeing if there's a lambda hidden inside
627                                 -- something cheap
628                            `max`
629                            etaExpandCount body
630
631                                 -- Finally, see if it's a state transformer, in which
632                                 -- case we eta-expand on principle! This can waste work,
633                                 -- but usually doesn't
634                            `max`
635                            case potential_extra_binder_tys of
636                                 [ty] | ty `eqTy` realWorldStateTy -> 1
637                                 other                             -> 0
638
639 \end{code}
640
641
642
643 %************************************************************************
644 %*                                                                      *
645 \subsection[Simplify-coerce]{Coerce expressions}
646 %*                                                                      *
647 %************************************************************************
648
649 \begin{code}
650 -- (coerce (case s of p -> r)) args ==> case s of p -> (coerce r) args
651 simplCoerce env coercion ty expr@(Case scrut alts) args
652   = simplCase env scrut alts (\env rhs -> simplCoerce env coercion ty rhs args)
653                              (computeResultType env expr args)
654
655 -- (coerce (let defns in b)) args  ==> let defns' in (coerce b) args
656 simplCoerce env coercion ty (Let bind body) args
657   = simplBind env bind (\env -> simplCoerce env coercion ty body args)
658                        (computeResultType env body args)
659
660 -- Default case
661 simplCoerce env coercion ty expr args
662   = simplExpr env expr []       `thenSmpl` \ expr' ->
663     returnSmpl (mkGenApp (mkCoerce coercion (simplTy env ty) expr') args)
664   where
665
666         -- Try cancellation; we do this "on the way up" because
667         -- I think that's where it'll bite best
668     mkCoerce (CoerceOut con1) ty1 (Coerce (CoerceIn  con2) ty2 body) | con1 == con2 = body
669     mkCoerce coercion ty  body = Coerce coercion ty body
670 \end{code}
671
672
673 %************************************************************************
674 %*                                                                      *
675 \subsection[Simplify-let]{Let-expressions}
676 %*                                                                      *
677 %************************************************************************
678
679 \begin{code}
680 simplBind :: SimplEnv
681           -> InBinding
682           -> (SimplEnv -> SmplM OutExpr)
683           -> OutType
684           -> SmplM OutExpr
685 \end{code}
686
687 When floating cases out of lets, remember this:
688
689         let x* = case e of alts
690         in <small expr>
691
692 where x* is sure to be demanded or e is a cheap operation that cannot
693 fail, e.g. unboxed addition.  Here we should be prepared to duplicate
694 <small expr>.  A good example:
695
696         let x* = case y of
697                    p1 -> build e1
698                    p2 -> build e2
699         in
700         foldr c n x*
701 ==>
702         case y of
703           p1 -> foldr c n (build e1)
704           p2 -> foldr c n (build e2)
705
706 NEW: We use the same machinery that we use for case-of-case to
707 *always* do case floating from let, that is we let bind and abstract
708 the original let body, and let the occurrence analyser later decide
709 whether the new let should be inlined or not. The example above
710 becomes:
711
712 ==>
713       let join_body x' = foldr c n x'
714         in case y of
715         p1 -> let x* = build e1
716                 in join_body x*
717         p2 -> let x* = build e2
718                 in join_body x*
719
720 note that join_body is a let-no-escape.
721 In this particular example join_body will later be inlined,
722 achieving the same effect.
723 ToDo: check this is OK with andy
724
725
726
727 \begin{code}
728 -- Dead code is now discarded by the occurrence analyser,
729
730 simplBind env (NonRec binder@(id,occ_info) rhs) body_c body_ty
731   = simpl_bind env rhs
732   where
733     -- Try let-to-case; see notes below about let-to-case
734     simpl_bind env rhs | will_be_demanded &&
735                          try_let_to_case &&
736                          type_ok_for_let_to_case rhs_ty &&
737                          not rhs_is_whnf        -- note: WHNF, but not bottom,  (comment below)
738       = tick Let2Case                           `thenSmpl_`
739         mkIdentityAlts rhs_ty                   `thenSmpl` \ id_alts ->
740         simplCase env rhs id_alts (\env rhs -> complete_bind env rhs) body_ty
741                 -- NB: it's tidier to call complete_bind not simpl_bind, else
742                 -- we nearly end up in a loop.  Consider:
743                 --      let x = rhs in b
744                 -- ==>  case rhs of (p,q) -> let x=(p,q) in b
745                 -- This effectively what the above simplCase call does.
746                 -- Now, the inner let is a let-to-case target again!  Actually, since
747                 -- the RHS is in WHNF it won't happen, but it's a close thing!
748
749     -- Try let-from-let
750     simpl_bind env (Let bind rhs) | let_floating_ok
751       = tick LetFloatFromLet                    `thenSmpl_`
752         simplBind env (fix_up_demandedness will_be_demanded bind)
753                       (\env -> simpl_bind env rhs) body_ty
754
755     -- Try case-from-let; this deals with a strict let of error too
756     simpl_bind env (Case scrut alts) | will_be_demanded || 
757                                        (float_primops && is_cheap_prim_app scrut)
758       = tick CaseFloatFromLet                           `thenSmpl_`
759
760         -- First, bind large let-body if necessary
761         if ok_to_dup || isSingleton (nonErrorRHSs alts)
762         then
763             simplCase env scrut alts (\env rhs -> simpl_bind env rhs) body_ty
764         else
765             bindLargeRhs env [binder] body_ty body_c    `thenSmpl` \ (extra_binding, new_body) ->
766             let
767                 body_c' = \env -> simplExpr env new_body []
768                 case_c  = \env rhs -> simplBind env (NonRec binder rhs) body_c' body_ty
769             in
770             simplCase env scrut alts case_c body_ty     `thenSmpl` \ case_expr ->
771             returnSmpl (Let extra_binding case_expr)
772
773     -- None of the above; simplify rhs and tidy up
774     simpl_bind env rhs = complete_bind env rhs
775  
776     complete_bind env rhs
777       = simplRhsExpr env binder rhs             `thenSmpl` \ rhs' ->
778         cloneId env binder                      `thenSmpl` \ new_id ->
779         completeNonRec env binder new_id rhs'   `thenSmpl` \ (new_env, binds) ->
780         body_c new_env                          `thenSmpl` \ body' ->
781         returnSmpl (mkCoLetsAny binds body')
782
783
784         -- All this stuff is computed at the start of the simpl_bind loop
785     float_lets                = switchIsSet env SimplFloatLetsExposingWHNF
786     float_primops             = switchIsSet env SimplOkToFloatPrimOps
787     ok_to_dup                 = switchIsSet env SimplOkToDupCode
788     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
789     try_let_to_case           = switchIsSet env SimplLetToCase
790     no_float                  = switchIsSet env SimplNoLetFromStrictLet
791
792     will_be_demanded = willBeDemanded (getIdDemandInfo id)
793     rhs_ty           = idType id
794
795     rhs_is_whnf = case mkFormSummary rhs of
796                         VarForm -> True
797                         ValueForm -> True
798                         other -> False
799
800     let_floating_ok  = (will_be_demanded && not no_float) ||
801                        always_float_let_from_let ||
802                        floatExposesHNF float_lets float_primops ok_to_dup rhs
803 \end{code}
804
805 Let to case
806 ~~~~~~~~~~~
807 It's important to try let-to-case before floating. Consider
808
809         let a*::Int = case v of {p1->e1; p2->e2}
810         in b
811
812 (The * means that a is sure to be demanded.)
813 If we do case-floating first we get this:
814
815         let k = \a* -> b
816         in case v of
817                 p1-> let a*=e1 in k a
818                 p2-> let a*=e2 in k a
819
820 Now watch what happens if we do let-to-case first:
821
822         case (case v of {p1->e1; p2->e2}) of
823           Int a# -> let a*=I# a# in b
824 ===>
825         let k = \a# -> let a*=I# a# in b
826         in case v of
827                 p1 -> case e1 of I# a# -> k a#
828                 p1 -> case e1 of I# a# -> k a#
829
830 The latter is clearly better.  (Remember the reboxing let-decl for a
831 is likely to go away, because after all b is strict in a.)
832
833 We do not do let to case for WHNFs, e.g.
834
835           let x = a:b in ...
836           =/=>
837           case a:b of x in ...
838
839 as this is less efficient.  but we don't mind doing let-to-case for
840 "bottom", as that will allow us to remove more dead code, if anything:
841
842           let x = error in ...
843           ===>
844           case error  of x -> ...
845           ===>
846           error
847
848 Notice that let to case occurs only if x is used strictly in its body
849 (obviously).
850
851
852 Letrec expressions
853 ~~~~~~~~~~~~~~~~~~
854
855 Simplify each RHS, float any let(recs) from the RHSs (if let-floating is
856 on and it'll expose a HNF), and bang the whole resulting mess together
857 into a huge letrec.
858
859 1. Any "macros" should be expanded.  The main application of this
860 macro-expansion is:
861
862         letrec
863                 f = ....g...
864                 g = ....f...
865         in
866         ....f...
867
868 Here we would like the single call to g to be inlined.
869
870 We can spot this easily, because g will be tagged as having just one
871 occurrence.  The "inlineUnconditionally" predicate is just what we want.
872
873 A worry: could this lead to non-termination?  For example:
874
875         letrec
876                 f = ...g...
877                 g = ...f...
878                 h = ...h...
879         in
880         ..h..
881
882 Here, f and g call each other (just once) and neither is used elsewhere.
883 But it's OK:
884
885 * the occurrence analyser will drop any (sub)-group that isn't used at
886   all.
887
888 * If the group is used outside itself (ie in the "in" part), then there
889   can't be a cyle.
890
891 ** IMPORTANT: check that NewOccAnal has the property that a group of
892    bindings like the above has f&g dropped.! ***
893
894
895 2. We'd also like to pull out any top-level let(rec)s from the
896 rhs of the defns:
897
898         letrec
899                 f = let h = ... in \x -> ....h...f...h...
900         in
901         ...f...
902 ====>
903         letrec
904                 h = ...
905                 f = \x -> ....h...f...h...
906         in
907         ...f...
908
909 But floating cases is less easy?  (Don't for now; ToDo?)
910
911
912 3.  We'd like to arrange that the RHSs "know" about members of the
913 group that are bound to constructors.  For example:
914
915     let rec
916        d.Eq      = (==,/=)
917        f a b c d = case d.Eq of (h,_) -> let x = (a,b); y = (c,d) in not (h x y)
918        /= a b    = unpack tuple a, unpack tuple b, call f
919     in d.Eq
920
921 here, by knowing about d.Eq in f's rhs, one could get rid of
922 the case (and break out the recursion completely).
923 [This occurred with more aggressive inlining threshold (4),
924 nofib/spectral/knights]
925
926 How to do it?
927         1: we simplify constructor rhss first.
928         2: we record the "known constructors" in the environment
929         3: we simplify the other rhss, with the knowledge about the constructors
930
931
932
933 \begin{code}
934 simplBind env (Rec pairs) body_c body_ty
935   =     -- Do floating, if necessary
936     let
937         floated_pairs | do_floating = float_pairs pairs
938                       | otherwise   = pairs
939
940         ticks         | do_floating = length floated_pairs - length pairs
941                       | otherwise   = 0
942
943         binders       = map fst floated_pairs
944     in
945     tickN LetFloatFromLet ticks         `thenSmpl_` 
946                 -- It's important to increment the tick counts if we
947                 -- do any floating.  A situation where this turns out
948                 -- to be important is this:
949                 -- Float in produces:
950                 --      letrec  x = let y = Ey in Ex
951                 --      in B
952                 -- Now floating gives this:
953                 --      letrec x = Ex
954                 --             y = Ey
955                 --      in B
956                 --- We now want to iterate once more in case Ey doesn't
957                 -- mention x, in which case the y binding can be pulled
958                 -- out as an enclosing let(rec), which in turn gives
959                 -- the strictness analyser more chance.
960
961     cloneIds env binders                        `thenSmpl` \ ids' ->
962     let
963        env_w_clones = extendIdEnvWithClones env binders ids'
964     in
965     simplRecursiveGroup env_w_clones ids' floated_pairs `thenSmpl` \ (binding, new_env) ->
966
967     body_c new_env                              `thenSmpl` \ body' ->
968
969     returnSmpl (Let binding body')
970
971   where
972     ------------ Floating stuff -------------------
973
974     float_lets                = switchIsSet env SimplFloatLetsExposingWHNF
975     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
976     do_floating               = float_lets || always_float_let_from_let
977
978     float_pairs pairs = concat (map float_pair pairs)
979
980     float_pair (binder, rhs)
981         | always_float_let_from_let ||
982           floatExposesHNF True False False rhs
983         = (binder,rhs') : pairs'
984
985         | otherwise
986         = [(binder,rhs)]
987         where
988           (pairs', rhs') = do_float rhs
989
990         -- Float just pulls out any top-level let(rec) bindings
991     do_float :: InExpr -> ([(InBinder,InExpr)], InExpr)
992     do_float (Let (Rec pairs) body)     = (float_pairs pairs    ++ pairs', body')
993                                             where
994                                               (pairs', body') = do_float body
995     do_float (Let (NonRec id rhs) body) = (float_pair (id,rhs) ++ pairs', body')
996                                             where
997                                               (pairs', body') = do_float body
998     do_float other                          = ([], other)
999
1000 simplRecursiveGroup env new_ids pairs 
1001   =     -- Add unfoldings to the new_ids corresponding to their RHS
1002     let
1003        binders         = map fst pairs
1004        occs            = map snd binders
1005        new_ids_w_pairs = zipEqual "simplRecGp" new_ids pairs
1006        rhs_env         = foldl extendEnvForRecBinding 
1007                                env new_ids_w_pairs
1008     in
1009
1010     mapSmpl (\(binder,rhs) -> simplRhsExpr rhs_env binder rhs) pairs    `thenSmpl` \ new_rhss ->
1011
1012     let
1013        new_pairs        = zipEqual "simplRecGp" new_ids new_rhss
1014        occs_w_new_pairs = zipEqual "simplRecGp" occs new_pairs
1015        new_env          = foldl add_binding env occs_w_new_pairs
1016
1017        add_binding env (occ_info,(new_id,new_rhs)) 
1018           = extendEnvGivenBinding env occ_info new_id new_rhs
1019     in
1020     returnSmpl (Rec new_pairs, new_env)
1021 \end{code}
1022
1023
1024 @completeLet@ looks at the simplified post-floating RHS of the
1025 let-expression, and decides what to do.  There's one interesting
1026 aspect to this, namely constructor reuse.  Consider
1027 @
1028         f = \x -> case x of
1029                     (y:ys) -> y:ys
1030                     []     -> ...
1031 @
1032 Is it a good idea to replace the rhs @y:ys@ with @x@?  This depends a
1033 bit on the compiler technology, but in general I believe not. For
1034 example, here's some code from a real program:
1035 @
1036 const.Int.max.wrk{-s2516-} =
1037     \ upk.s3297#  upk.s3298# ->
1038         let {
1039           a.s3299 :: Int
1040           _N_ {-# U(P) #-}
1041           a.s3299 = I#! upk.s3297#
1042         } in
1043           case (const.Int._tagCmp.wrk{-s2513-} upk.s3297# upk.s3298#) of {
1044             _LT -> I#! upk.s3298#
1045             _EQ -> a.s3299
1046             _GT -> a.s3299
1047           }
1048 @
1049 The a.s3299 really isn't doing much good.  We'd be better off inlining
1050 it.  (Actually, let-no-escapery means it isn't as bad as it looks.)
1051
1052 So the current strategy is to inline all known-form constructors, and
1053 only do the reverse (turn a constructor application back into a
1054 variable) when we find a let-expression:
1055 @
1056         let x = C a1 .. an
1057         in
1058         ... (let y = C a1 .. an in ...) ...
1059 @
1060 where it is always good to ditch the binding for y, and replace y by
1061 x.  That's just what completeLetBinding does.
1062
1063
1064 \begin{code}
1065         -- We want to ensure that all let-bound Coerces have 
1066         -- atomic bodies, so they can freely be inlined.
1067 completeNonRec env binder new_id (Coerce coercion ty rhs)
1068   | not (is_atomic rhs)
1069   = newId (coreExprType rhs)                            `thenSmpl` \ inner_id ->
1070     completeNonRec env 
1071                    (inner_id, dangerousArgOcc) inner_id rhs `thenSmpl` \ (env1, binds1) ->
1072         -- Dangerous occ because, like constructor args,
1073         -- it can be duplicated easily
1074     let
1075         atomic_rhs = case lookupId env1 inner_id of
1076                         LitArg l -> Lit l
1077                         VarArg v -> Var v
1078     in
1079     completeNonRec env1 binder new_id
1080                    (Coerce coercion ty atomic_rhs)      `thenSmpl` \ (env2, binds2) ->
1081
1082     returnSmpl (env2, binds1 ++ binds2)
1083   where
1084     is_atomic (Var v) = True
1085     is_atomic (Lit l) = not (isNoRepLit l)
1086     is_atomic other   = False
1087         
1088         -- 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 new_id rhs@(Var v)
1093   = returnSmpl (extendIdEnvWithAtom env binder (VarArg v), [NonRec new_id rhs])
1094
1095 completeNonRec env binder new_id rhs@(Lit lit)
1096   | not (isNoRepLit lit)
1097   = returnSmpl (extendIdEnvWithAtom env binder (LitArg lit), [NonRec new_id rhs])
1098
1099         -- Right hand sides that are constructors
1100         --      let v = C args
1101         --      in
1102         --- ...(let w = C same-args in ...)...
1103         -- Then use v instead of w.      This may save
1104         -- re-constructing an existing constructor.
1105 completeNonRec env binder new_id rhs@(Con con con_args)
1106   | switchIsSet env SimplReuseCon && 
1107     maybeToBool maybe_existing_con &&
1108     not (externallyVisibleId new_id)            -- Don't bother for exported things
1109                                                 -- because we won't be able to drop
1110                                                 -- its binding.
1111   = tick ConReused              `thenSmpl_`
1112     returnSmpl (extendIdEnvWithAtom env binder (VarArg it), [NonRec new_id rhs])
1113   where
1114     maybe_existing_con = lookForConstructor env con con_args
1115     Just it            = maybe_existing_con
1116
1117         -- Default case
1118 completeNonRec env binder@(id,occ_info) new_id rhs
1119  = returnSmpl (new_env, [NonRec new_id rhs])
1120  where
1121    env1    = extendIdEnvWithClone env binder new_id
1122    new_env = extendEnvGivenBinding env1 occ_info new_id rhs
1123 \end{code}
1124
1125 %************************************************************************
1126 %*                                                                      *
1127 \subsection[Simplify-atoms]{Simplifying atoms}
1128 %*                                                                      *
1129 %************************************************************************
1130
1131 \begin{code}
1132 simplArg :: SimplEnv -> InArg -> OutArg
1133
1134 simplArg env (LitArg lit) = LitArg lit
1135 simplArg env (TyArg  ty)  = TyArg  (simplTy env ty)
1136 simplArg env (VarArg id)  = lookupId env id
1137 \end{code}
1138
1139 %************************************************************************
1140 %*                                                                      *
1141 \subsection[Simplify-quickies]{Some local help functions}
1142 %*                                                                      *
1143 %************************************************************************
1144
1145
1146 \begin{code}
1147 -- fix_up_demandedness switches off the willBeDemanded Info field
1148 -- for bindings floated out of a non-demanded let
1149 fix_up_demandedness True {- Will be demanded -} bind
1150    = bind       -- Simple; no change to demand info needed
1151 fix_up_demandedness False {- May not be demanded -} (NonRec binder rhs)
1152    = NonRec (un_demandify binder) rhs
1153 fix_up_demandedness False {- May not be demanded -} (Rec pairs)
1154    = Rec [(un_demandify binder, rhs) | (binder,rhs) <- pairs]
1155
1156 un_demandify (id, occ_info) = (id `addIdDemandInfo` noInfo, occ_info)
1157
1158 is_cheap_prim_app (Prim op _) = primOpOkForSpeculation op
1159 is_cheap_prim_app other       = False
1160
1161 computeResultType :: SimplEnv -> InExpr -> [OutArg] -> OutType
1162 computeResultType env expr args
1163   = go expr_ty' args
1164   where
1165     expr_ty  = coreExprType (unTagBinders expr)
1166     expr_ty' = simplTy env expr_ty
1167
1168     go ty [] = ty
1169     go ty (TyArg ty_arg : args) = go (mkAppTy ty ty_arg) args
1170     go ty (a:args) | isValArg a = case (getFunTy_maybe ty) of
1171                                     Just (_, res_ty) -> go res_ty args
1172                                     Nothing          -> panic "computeResultType"
1173 \end{code}
1174