2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[Simplify]{The main module of the simplifier}
7 module Simplify ( simplTopBinds, simplExpr, simplBind ) where
9 #include "HsVersions.h"
12 import CmdLineOpts ( SimplifierSwitch(..) )
13 import ConFold ( completePrim )
14 import CoreUnfold ( Unfolding, SimpleUnfolding, mkFormSummary,
15 exprIsTrivial, whnfOrBottom, inlineUnconditionally,
18 import CostCentre ( isSccCountCostCentre, cmpCostCentre, costsAreSubsumed, useCurrentCostCentre )
20 import CoreUtils ( coreExprType, nonErrorRHSs, maybeErrorApp,
21 unTagBinders, squashableDictishCcExpr
23 import Id ( idType, idMustBeINLINEd, idWantsToBeINLINEd, idMustNotBeINLINEd,
24 addIdArity, getIdArity,
25 getIdDemandInfo, addIdDemandInfo
27 import Name ( isExported, isLocallyDefined )
28 import IdInfo ( willBeDemanded, noDemandInfo, DemandInfo, ArityInfo(..),
29 atLeastArity, unknownArity )
30 import Literal ( isNoRepLit )
31 import Maybes ( maybeToBool )
32 import PrimOp ( primOpOkForSpeculation, PrimOp(..) )
33 import SimplCase ( simplCase, bindLargeRhs )
36 import SimplVar ( completeVar, simplBinder, simplBinders, simplTyBinder, simplTyBinders )
38 import Type ( mkTyVarTy, mkTyVarTys, mkAppTy, applyTy, applyTys,
39 mkFunTys, splitAlgTyConApp_maybe,
40 splitFunTys, splitFunTy_maybe, isUnpointedType
42 import TysPrim ( realWorldStatePrimTy )
43 import Util ( Eager, appEager, returnEager, runEager, mapEager,
44 isSingleton, zipEqual, zipWithEqual, mapAndUnzip
49 The controlling flags, and what they do
50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)
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
70 -ffloat-lets-ok = OK to float lets out of lets if the enclosing
71 let is strict or if the floating will expose
74 -ffloat-primops-ok = OK to float out of lets cases whose scrutinee
75 is a primop that cannot fail [simplifier].
77 -fcode-duplication-ok = allows the previous option to work on cases with
78 multiple branches [simplifier].
80 -flet-to-case = does let-to-case transformation [simplifier].
82 -fcase-of-case = does case of case transformation [simplifier].
84 -fpedantic-bottoms = does not allow:
85 case x of y -> e ===> e[x/y]
86 (which may turn bottom into non-bottom)
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
96 let x = e in ...x... ===> let x = e in ...e...
98 We have two mechanisms for inlining:
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
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.
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.
121 We *never* put a non-HNF unfolding in the UnfoldEnv except in the
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:
130 f = \y -> ...y...y...y...
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@.
137 Because of this, the "unconditional-inline" mechanism above is the
138 only way in which non-HNFs can get inlined.
143 When a variable has an INLINE pragma on it --- which includes wrappers
144 produced by the strictness analyser --- we treat it rather carefully.
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
152 in {-# INLINE y #-} y = f 3
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.)
160 So, we clean out the UnfoldEnv of all SimpleUnfolding inlinings before
161 going into such an RHS.
163 What about imports? They don't really matter much because we only
164 inline relatively small things via imports.
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.
173 %************************************************************************
175 \subsection[Simplify-simplExpr]{The main function: simplExpr}
177 %************************************************************************
179 At the top level things are a little different.
181 * No cloning (not allowed for exported Ids, unnecessary for the others)
182 * Floating is done a bit differently (no case floating; check for leaks; handle letrec)
185 simplTopBinds :: SimplEnv -> [InBinding] -> SmplM [OutBinding]
187 -- Dead code is now discarded by the occurrence analyser,
189 simplTopBinds env binds
190 = mapSmpl (floatBind env True) binds `thenSmpl` \ binds_s ->
191 simpl_top_binds env (concat binds_s)
193 simpl_top_binds env [] = returnSmpl []
195 simpl_top_binds env (NonRec binder@(in_id,occ_info) rhs : binds)
196 = --- No cloning necessary at top level
197 simplBinder env binder `thenSmpl` \ (env1, out_id) ->
198 simplRhsExpr env binder rhs out_id `thenSmpl` \ (rhs',arity) ->
199 completeNonRec env1 binder (out_id `withArity` arity) rhs' `thenSmpl` \ (new_env, binds1') ->
200 simpl_top_binds new_env binds `thenSmpl` \ binds2' ->
201 returnSmpl (binds1' ++ binds2')
203 simpl_top_binds env (Rec pairs : binds)
204 = -- No cloning necessary at top level, but we nevertheless
205 -- add the Ids to the environment. This makes sure that
206 -- info carried on the Id (such as arity info) gets propagated
209 -- This may seem optional, but I found an occasion when it Really matters.
210 -- Consider foo{n} = ...foo...
213 -- where baz* is exported and foo isn't. Then when we do "indirection-shorting"
214 -- in tidyCore, we need the {no-inline} pragma from foo to attached to the final
215 -- thing: baz*{n} = ...baz...
217 -- Sure we could have made the indirection-shorting a bit cleverer, but
218 -- propagating pragma info is a Good Idea anyway.
219 simplBinders env (map fst pairs) `thenSmpl` \ (env1, out_ids) ->
220 simplRecursiveGroup env1 out_ids pairs `thenSmpl` \ (bind', new_env) ->
221 simpl_top_binds new_env binds `thenSmpl` \ binds' ->
222 returnSmpl (Rec bind' : binds')
225 %************************************************************************
227 \subsection[Simplify-simplExpr]{The main function: simplExpr}
229 %************************************************************************
233 simplExpr :: SimplEnv
234 -> InExpr -> [OutArg]
235 -> OutType -- Type of (e args); i.e. type of overall result
239 The expression returned has the same meaning as the input expression
240 applied to the specified arguments.
245 Check if there's a macro-expansion, and if so rattle on. Otherwise do
246 the more sophisticated stuff.
249 simplExpr env (Var v) args result_ty
250 = case (runEager $ lookupId env v) of
251 LitArg lit -- A boring old literal
252 -> ASSERT( null args )
255 VarArg var -- More interesting! An id!
256 -> completeVar env var args result_ty
257 -- Either Id is in the local envt, or it's a global.
258 -- In either case we don't need to apply the type
259 -- environment to it.
266 simplExpr env (Lit l) [] result_ty = returnSmpl (Lit l)
268 simplExpr env (Lit l) _ _ = panic "simplExpr:Lit with argument"
272 Primitive applications are simple.
273 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275 NB: Prim expects an empty argument list! (Because it should be
276 saturated and not higher-order. ADR)
279 simplExpr env (Prim op prim_args) args result_ty
281 mapEager (simplArg env) prim_args `appEager` \ prim_args' ->
282 simpl_op op `appEager` \ op' ->
283 completePrim env op' prim_args'
285 -- PrimOps just need any types in them renamed.
287 simpl_op (CCallOp label is_asm may_gc arg_tys result_ty)
288 = mapEager (simplTy env) arg_tys `appEager` \ arg_tys' ->
289 simplTy env result_ty `appEager` \ result_ty' ->
290 returnEager (CCallOp label is_asm may_gc arg_tys' result_ty')
292 simpl_op other_op = returnEager other_op
295 Constructor applications
296 ~~~~~~~~~~~~~~~~~~~~~~~~
297 Nothing to try here. We only reuse constructors when they appear as the
298 rhs of a let binding (see completeLetBinding).
301 simplExpr env (Con con con_args) args result_ty
302 = ASSERT( null args )
303 mapEager (simplArg env) con_args `appEager` \ con_args' ->
304 returnSmpl (Con con con_args')
308 Applications are easy too:
309 ~~~~~~~~~~~~~~~~~~~~~~~~~~
310 Just stuff 'em in the arg stack
313 simplExpr env (App fun arg) args result_ty
314 = simplArg env arg `appEager` \ arg' ->
315 simplExpr env fun (arg' : args) result_ty
321 First the case when it's applied to an argument.
324 simplExpr env (Lam (TyBinder tyvar) body) (TyArg ty : args) result_ty
325 = tick TyBetaReduction `thenSmpl_`
326 simplExpr (bindTyVar env tyvar ty) body args result_ty
330 simplExpr env tylam@(Lam (TyBinder tyvar) body) [] result_ty
331 = simplTyBinder env tyvar `thenSmpl` \ (new_env, tyvar') ->
333 new_result_ty = applyTy result_ty (mkTyVarTy tyvar')
335 simplExpr new_env body [] new_result_ty `thenSmpl` \ body' ->
336 returnSmpl (Lam (TyBinder tyvar') body')
339 simplExpr env (Lam (TyBinder _) _) (_ : _) result_ty
340 = panic "simplExpr:TyLam with non-TyArg"
348 There's a complication with lambdas that aren't saturated.
353 If we did nothing, x is used inside the \y, so would be marked
354 as dangerous to dup. But in the common case where the abstraction
355 is applied to two arguments this is over-pessimistic.
356 So instead we don't take account of the \y when dealing with x's usage;
357 instead, the simplifier is careful when partially applying lambdas.
360 simplExpr env expr@(Lam (ValBinder binder) body) orig_args result_ty
361 = go 0 env expr orig_args
363 go n env (Lam (ValBinder binder) body) (val_arg : args)
364 | isValArg val_arg -- The lambda has an argument
365 = tick BetaReduction `thenSmpl_`
366 go (n+1) (bindIdToAtom env binder val_arg) body args
368 go n env expr@(Lam (ValBinder binder) body) args
369 -- The lambda is un-saturated, so we must zap the occurrence info
370 -- on the arguments we've already beta-reduced into the body of the lambda
371 = ASSERT( null args ) -- Value lambda must match value argument!
373 new_env = markDangerousOccs env (take n orig_args)
375 simplValLam new_env expr 0 {- Guaranteed applied to at least 0 args! -} result_ty
376 `thenSmpl` \ (expr', arity) ->
379 go n env non_val_lam_expr args -- The lambda had enough arguments
380 = simplExpr env non_val_lam_expr args result_ty
388 simplExpr env (Let bind body) args result_ty
389 = simplBind env bind (\env -> simplExpr env body args result_ty) result_ty
396 simplExpr env expr@(Case scrut alts) args result_ty
397 = simplCase env scrut alts (\env rhs -> simplExpr env rhs args result_ty) result_ty
404 simplExpr env (Coerce coercion ty body) args result_ty
405 = simplCoerce env coercion ty body args result_ty
412 1) Eliminating nested sccs ...
413 We must be careful to maintain the scc counts ...
416 simplExpr env (SCC cc1 (SCC cc2 expr)) args result_ty
417 | not (isSccCountCostCentre cc2) && case cmpCostCentre cc1 cc2 of { EQ -> True; _ -> False }
418 -- eliminate inner scc if no call counts and same cc as outer
419 = simplExpr env (SCC cc1 expr) args result_ty
421 | not (isSccCountCostCentre cc2) && not (isSccCountCostCentre cc1)
422 -- eliminate outer scc if no call counts associated with either ccs
423 = simplExpr env (SCC cc2 expr) args result_ty
426 2) Moving sccs inside lambdas ...
429 simplExpr env (SCC cc (Lam binder@(ValBinder _) body)) args result_ty
430 | not (isSccCountCostCentre cc)
431 -- move scc inside lambda only if no call counts
432 = simplExpr env (Lam binder (SCC cc body)) args result_ty
434 simplExpr env (SCC cc (Lam binder body)) args result_ty
435 -- always ok to move scc inside type/usage lambda
436 = simplExpr env (Lam binder (SCC cc body)) args result_ty
439 3) Eliminating dict sccs ...
442 simplExpr env (SCC cc expr) args result_ty
443 | squashableDictishCcExpr cc expr
444 -- eliminate dict cc if trivial dict expression
445 = simplExpr env expr args result_ty
448 4) Moving arguments inside the body of an scc ...
449 This moves the cost of doing the application inside the scc
450 (which may include the cost of extracting methods etc)
453 simplExpr env (SCC cost_centre body) args result_ty
455 new_env = setEnclosingCC env cost_centre
457 simplExpr new_env body args result_ty `thenSmpl` \ body' ->
458 returnSmpl (SCC cost_centre body')
461 %************************************************************************
463 \subsection{Simplify RHS of a Let/Letrec}
465 %************************************************************************
467 simplRhsExpr does arity-expansion. That is, given:
469 * a right hand side /\ tyvars -> \a1 ... an -> e
470 * the information (stored in BinderInfo) that the function will always
471 be applied to at least k arguments
473 it transforms the rhs to
475 /\tyvars -> \a1 ... an b(n+1) ... bk -> (e b(n+1) ... bk)
477 This is a Very Good Thing!
484 -> OutId -- The new binder (used only for its type)
485 -> SmplM (OutExpr, ArityInfo)
490 simplRhsExpr env binder@(id,occ_info) rhs new_id
491 | maybeToBool (splitAlgTyConApp_maybe rhs_ty)
492 -- Deal with the data type case, in which case the elaborate
493 -- eta-expansion nonsense is really quite a waste of time.
494 = simplExpr rhs_env rhs [] rhs_ty `thenSmpl` \ rhs' ->
495 returnSmpl (rhs', ArityExactly 0)
497 | otherwise -- OK, use the big hammer
498 = -- Deal with the big lambda part
499 simplTyBinders env tyvars `thenSmpl` \ (lam_env, tyvars') ->
501 body_ty = applyTys rhs_ty (mkTyVarTys tyvars')
503 -- Deal with the little lambda part
504 -- Note that we call simplLam even if there are no binders,
505 -- in case it can do arity expansion.
506 simplValLam lam_env body (getBinderInfoArity occ_info) body_ty `thenSmpl` \ (lambda', arity) ->
508 -- Put on the big lambdas, trying to float out any bindings caught inside
509 mkRhsTyLam tyvars' lambda' `thenSmpl` \ rhs' ->
511 returnSmpl (rhs', arity)
513 rhs_ty = idType new_id
514 rhs_env | idWantsToBeINLINEd id -- Don't ever inline in a INLINE thing's rhs
515 = switchOffInlining env1 -- See comments with switchOffInlining
519 -- The top level "enclosing CC" is "SUBSUMED". But the enclosing CC
520 -- for the rhs of top level defs is "OST_CENTRE". Consider
522 -- g = \y -> let v = f y in scc "x" (v ...)
523 -- Here we want to inline "f", since its CC is SUBSUMED, but we don't
524 -- want to inline "v" since its CC is dynamically determined.
526 current_cc = getEnclosingCC env
527 env1 | costsAreSubsumed current_cc = setEnclosingCC env useCurrentCostCentre
530 (tyvars, body) = collectTyBinders rhs
534 ----------------------------------------------------------------
535 An old special case that is now nuked.
537 First a special case for variable right-hand sides
539 It's OK to simplify the RHS, but it's often a waste of time. Often
540 these v = w things persist because v is exported, and w is used
541 elsewhere. So if we're not careful we'll eta expand the rhs, only
542 to eta reduce it in competeNonRec.
544 If we leave the binding unchanged, we will certainly replace v by w at
545 every occurrence of v, which is good enough.
547 In fact, it's *better* to replace v by w than to inline w in v's rhs,
548 even if this is the only occurrence of w. Why? Because w might have
549 IdInfo (such as strictness) that v doesn't.
551 Furthermore, there might be other uses of w; if so, inlining w in
552 v's rhs will duplicate w's rhs, whereas replacing v by w doesn't.
554 HOWEVER, we have to be careful if w is something that *must* be
555 inlined. In particular, its binding may have been dropped. Here's
556 an example that actually happened:
557 let x = let y = e in y
559 The "let y" was floated out, and then (since y occurs once in a
560 definitely inlinable position) the binding was dropped, leaving
561 {y=e} let x = y in f x
562 But now using the reasoning of this little section,
563 y wasn't inlined, because it was a let x=y form.
568 This "optimisation" turned out to be a bad idea. If there's are
569 top-level exported bindings like
574 then y wasn't getting inlined in x's rhs, and we were getting
575 bad code. So I've removed the special case from here, and
576 instead we only try eta reduction and constructor reuse
577 in completeNonRec if the thing is *not* exported.
581 simplRhsExpr env binder@(id,occ_info) (Var v) new_id
582 | maybeToBool maybe_stop_at_var
583 = returnSmpl (Var the_var, getIdArity the_var)
586 = case (runEager $ lookupId env v) of
587 VarArg v' | not (must_unfold v') -> Just v'
590 Just the_var = maybe_stop_at_var
592 must_unfold v' = idMustBeINLINEd v'
593 || case lookupOutIdEnv env v' of
594 Just (_, _, InUnfolding _ _) -> True
598 End of old, nuked, special case.
599 ------------------------------------------------------------------
602 %************************************************************************
604 \subsection{Simplify a lambda abstraction}
606 %************************************************************************
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.
612 simplValLam env expr min_no_of_args expr_ty
613 | not (switchIsSet env SimplDoLambdaEtaExpansion) || -- Bale out if eta expansion off
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
620 -- g = \a b c -> f alpha beta a b c
622 -- The original RHS is "trivial" (exprIsTrivial), because it generates
623 -- no code (renames f to g). But the new RHS isn't.
625 null potential_extra_binder_tys || -- or ain't a function
626 no_of_extra_binders <= 0 -- or no extra binders needed
627 = simplBinders env binders `thenSmpl` \ (new_env, binders') ->
628 simplExpr new_env body [] body_ty `thenSmpl` \ body' ->
629 returnSmpl (mkValLam binders' body', final_arity)
631 | otherwise -- Eta expansion possible
632 = -- A SSERT( no_of_extra_binders <= length potential_extra_binder_tys )
633 (if not ( no_of_extra_binders <= length potential_extra_binder_tys ) then
634 pprTrace "simplValLam" (vcat [ppr expr,
637 int no_of_extra_binders,
638 ppr potential_extra_binder_tys])
641 tick EtaExpansion `thenSmpl_`
642 simplBinders env binders `thenSmpl` \ (new_env, binders') ->
643 newIds extra_binder_tys `thenSmpl` \ extra_binders' ->
644 simplExpr new_env body (map VarArg extra_binders') etad_body_ty `thenSmpl` \ body' ->
646 mkValLam (binders' ++ extra_binders') body',
651 (binders,body) = collectValBinders expr
652 no_of_binders = length binders
653 (arg_tys, res_ty) = splitFunTys expr_ty
654 potential_extra_binder_tys = (if not (no_of_binders <= length arg_tys) then
655 pprTrace "simplValLam" (vcat [ppr expr,
659 drop no_of_binders arg_tys
660 body_ty = mkFunTys potential_extra_binder_tys res_ty
662 -- Note: it's possible that simplValLam will be applied to something
663 -- with a forall type. Eg when being applied to the rhs of
665 -- where wurble has a forall-type, but no big lambdas at the top.
666 -- We could be clever an insert new big lambdas, but we don't bother.
668 etad_body_ty = mkFunTys (drop no_of_extra_binders potential_extra_binder_tys) res_ty
669 extra_binder_tys = take no_of_extra_binders potential_extra_binder_tys
670 final_arity = atLeastArity (no_of_binders + no_of_extra_binders)
672 no_of_extra_binders = -- First, use the info about how many args it's
673 -- always applied to in its scope; but ignore this
674 -- info for thunks. To see why we ignore it for thunks,
675 -- consider let f = lookup env key in (f 1, f 2)
676 -- We'd better not eta expand f just because it is
678 (min_no_of_args - no_of_binders)
680 -- Next, try seeing if there's a lambda hidden inside
682 -- etaExpandCount can reuturn a huge number (like 10000!) if
683 -- it finds that the body is a call to "error"; hence
684 -- the use of "min" here.
686 (etaExpandCount body `min` length potential_extra_binder_tys)
688 -- Finally, see if it's a state transformer, in which
689 -- case we eta-expand on principle! This can waste work,
690 -- but usually doesn't
692 case potential_extra_binder_tys of
693 [ty] | ty == realWorldStatePrimTy -> 1
699 %************************************************************************
701 \subsection[Simplify-coerce]{Coerce expressions}
703 %************************************************************************
706 -- (coerce (case s of p -> r)) args ==> case s of p -> (coerce r) args
707 simplCoerce env coercion ty expr@(Case scrut alts) args result_ty
708 = simplCase env scrut alts (\env rhs -> simplCoerce env coercion ty rhs args result_ty) result_ty
710 -- (coerce (let defns in b)) args ==> let defns' in (coerce b) args
711 simplCoerce env coercion ty (Let bind body) args result_ty
712 = simplBind env bind (\env -> simplCoerce env coercion ty body args result_ty) result_ty
715 simplCoerce env coercion ty expr args result_ty
716 = simplTy env ty `appEager` \ ty' ->
717 simplTy env expr_ty `appEager` \ expr_ty' ->
718 simplExpr env expr [] expr_ty' `thenSmpl` \ expr' ->
719 returnSmpl (mkGenApp (mkCoerce coercion ty' expr') args)
721 expr_ty = coreExprType (unTagBinders expr) -- Rather like simplCase other_scrut
723 -- Try cancellation; we do this "on the way up" because
724 -- I think that's where it'll bite best
725 mkCoerce (CoerceOut con1) ty1 (Coerce (CoerceIn con2) ty2 body) | con1 == con2 = body
726 mkCoerce coercion ty body = Coerce coercion ty body
730 %************************************************************************
732 \subsection[Simplify-bind]{Binding groups}
734 %************************************************************************
737 simplBind :: SimplEnv
739 -> (SimplEnv -> SmplM OutExpr)
743 simplBind env (NonRec binder rhs) body_c body_ty = simplNonRec env binder rhs body_c body_ty
744 simplBind env (Rec pairs) body_c body_ty = simplRec env pairs body_c body_ty
748 %************************************************************************
750 \subsection[Simplify-let]{Let-expressions}
752 %************************************************************************
756 The booleans controlling floating have to be set with a little care.
757 Here's one performance bug I found:
759 let x = let y = let z = case a# +# 1 of {b# -> E1}
764 Now, if E2, E3 aren't HNFs we won't float the y-binding or the z-binding.
765 Before case_floating_ok included float_exposes_hnf, the case expression was floated
766 *one level per simplifier iteration* outwards. So it made th s
769 Floating case from let
770 ~~~~~~~~~~~~~~~~~~~~~~
771 When floating cases out of lets, remember this:
773 let x* = case e of alts
776 where x* is sure to be demanded or e is a cheap operation that cannot
777 fail, e.g. unboxed addition. Here we should be prepared to duplicate
778 <small expr>. A good example:
787 p1 -> foldr c n (build e1)
788 p2 -> foldr c n (build e2)
790 NEW: We use the same machinery that we use for case-of-case to
791 *always* do case floating from let, that is we let bind and abstract
792 the original let body, and let the occurrence analyser later decide
793 whether the new let should be inlined or not. The example above
797 let join_body x' = foldr c n x'
799 p1 -> let x* = build e1
801 p2 -> let x* = build e2
804 note that join_body is a let-no-escape.
805 In this particular example join_body will later be inlined,
806 achieving the same effect.
807 ToDo: check this is OK with andy
810 Let to case: two points
813 Point 1. We defer let-to-case for all data types except single-constructor
814 ones. Suppose we change
820 It can be the case that we find that b ultimately contains ...(case x of ..)....
821 and this is the only occurrence of x. Then if we've done let-to-case
822 we can't inline x, which is a real pain. On the other hand, we lose no
823 transformations by not doing this transformation, because the relevant
824 case-of-X transformations are also implemented by simpl_bind.
826 If x is a single-constructor type, then we go ahead anyway, giving
828 case e of (y,z) -> let x = (y,z) in b
830 because now we can squash case-on-x wherever they occur in b.
832 We do let-to-case on multi-constructor types in the tidy-up phase
833 (tidyCoreExpr) mainly so that the code generator doesn't need to
834 spot the demand-flag.
837 Point 2. It's important to try let-to-case before doing the
838 strict-let-of-case transformation, which happens in the next equation
841 let a*::Int = case v of {p1->e1; p2->e2}
844 (The * means that a is sure to be demanded.)
845 If we do case-floating first we get this:
849 p1-> let a*=e1 in k a
850 p2-> let a*=e2 in k a
852 Now watch what happens if we do let-to-case first:
854 case (case v of {p1->e1; p2->e2}) of
855 Int a# -> let a*=I# a# in b
857 let k = \a# -> let a*=I# a# in b
859 p1 -> case e1 of I# a# -> k a#
860 p1 -> case e2 of I# a# -> k a#
862 The latter is clearly better. (Remember the reboxing let-decl for a
863 is likely to go away, because after all b is strict in a.)
865 We do not do let to case for WHNFs, e.g.
871 as this is less efficient. but we don't mind doing let-to-case for
872 "bottom", as that will allow us to remove more dead code, if anything:
876 case error of x -> ...
880 Notice that let to case occurs only if x is used strictly in its body
885 -- Dead code is now discarded by the occurrence analyser,
887 simplNonRec env binder@(id,occ_info) rhs body_c body_ty
888 | inlineUnconditionally ok_to_dup id occ_info
889 = -- The binder is used in definitely-inline way in the body
890 -- So add it to the environment, drop the binding, and continue
891 body_c (extendEnvGivenInlining env id occ_info rhs)
893 | idWantsToBeINLINEd id
894 = complete_bind env rhs -- Don't mess about with floating or let-to-case on
897 -- Do let-to-case right away for unpointed types
898 -- These shouldn't occur much, but do occur right after desugaring,
899 -- because we havn't done dependency analysis at that point, so
900 -- we can't trivially do let-to-case (because there may be some unboxed
901 -- things bound in letrecs that aren't really recursive).
902 | isUnpointedType rhs_ty && not rhs_is_whnf
903 = simplCase env rhs (PrimAlts [] (BindDefault binder (Var id)))
904 (\env rhs -> complete_bind env rhs) body_ty
906 -- Try let-to-case; see notes below about let-to-case
910 || (not rhs_is_whnf && singleConstructorType rhs_ty)
911 -- Don't do let-to-case if the RHS is a constructor application.
912 -- Even then only do it for single constructor types.
913 -- For other types we defer doing it until the tidy-up phase at
914 -- the end of simplification.
916 = tick Let2Case `thenSmpl_`
917 simplCase env rhs (AlgAlts [] (BindDefault binder (Var id)))
918 (\env rhs -> complete_bind env rhs) body_ty
919 -- OLD COMMENT: [now the new RHS is only "x" so there's less worry]
920 -- NB: it's tidier to call complete_bind not simpl_bind, else
921 -- we nearly end up in a loop. Consider:
923 -- ==> case rhs of (p,q) -> let x=(p,q) in b
924 -- This effectively what the above simplCase call does.
925 -- Now, the inner let is a let-to-case target again! Actually, since
926 -- the RHS is in WHNF it won't happen, but it's a close thing!
932 simpl_bind env (Let bind rhs) | let_floating_ok
933 = tick LetFloatFromLet `thenSmpl_`
934 simplBind env (if will_be_demanded then bind
935 else un_demandify_bind bind)
936 (\env -> simpl_bind env rhs) body_ty
938 -- Try case-from-let; this deals with a strict let of error too
939 simpl_bind env (Case scrut alts) | case_floating_ok scrut
940 = tick CaseFloatFromLet `thenSmpl_`
942 -- First, bind large let-body if necessary
943 if ok_to_dup || isSingleton (nonErrorRHSs alts)
945 simplCase env scrut alts (\env rhs -> simpl_bind env rhs) body_ty
947 bindLargeRhs env [binder] body_ty body_c `thenSmpl` \ (extra_binding, new_body) ->
949 body_c' = \env -> simplExpr env new_body [] body_ty
950 case_c = \env rhs -> simplNonRec env binder rhs body_c' body_ty
952 simplCase env scrut alts case_c body_ty `thenSmpl` \ case_expr ->
953 returnSmpl (Let extra_binding case_expr)
955 -- None of the above; simplify rhs and tidy up
956 simpl_bind env rhs = complete_bind env rhs
958 complete_bind env rhs
959 = simplBinder env binder `thenSmpl` \ (env_w_clone, new_id) ->
960 simplRhsExpr env binder rhs new_id `thenSmpl` \ (rhs',arity) ->
961 completeNonRec env_w_clone binder
962 (new_id `withArity` arity) rhs' `thenSmpl` \ (new_env, binds) ->
963 body_c new_env `thenSmpl` \ body' ->
964 returnSmpl (mkCoLetsAny binds body')
967 -- All this stuff is computed at the start of the simpl_bind loop
968 float_lets = switchIsSet env SimplFloatLetsExposingWHNF
969 float_primops = switchIsSet env SimplOkToFloatPrimOps
970 ok_to_dup = switchIsSet env SimplOkToDupCode
971 always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
972 try_let_to_case = switchIsSet env SimplLetToCase
973 no_float = switchIsSet env SimplNoLetFromStrictLet
975 demand_info = getIdDemandInfo id
976 will_be_demanded = willBeDemanded demand_info
979 form = mkFormSummary rhs
980 rhs_is_bot = case form of
983 rhs_is_whnf = case form of
988 float_exposes_hnf = floatExposesHNF float_lets float_primops ok_to_dup rhs
990 let_floating_ok = (will_be_demanded && not no_float) ||
991 always_float_let_from_let ||
994 case_floating_ok scrut = (will_be_demanded && not no_float) ||
995 (float_exposes_hnf && is_cheap_prim_app scrut && float_primops)
1000 @completeNonRec@ looks at the simplified post-floating RHS of the
1001 let-expression, with a view to turning
1005 where y is just a variable. Now we can eliminate the binding
1006 altogether, and replace x by y throughout.
1008 There are two cases when we can do this:
1010 * When e is a constructor application, and we have
1011 another variable in scope bound to the same
1012 constructor application. [This is just a special
1013 case of common-subexpression elimination.]
1015 * When e can be eta-reduced to a variable. E.g.
1019 HOWEVER, if x is exported, we don't attempt this at all. Why not?
1020 Because then we can't remove the x=y binding, in which case we
1021 have just made things worse, perhaps a lot worse.
1024 completeNonRec env binder new_id new_rhs
1025 = returnSmpl (env', [NonRec b r | (b,r) <- binds])
1027 (env', binds) = completeBind env binder new_id new_rhs
1030 completeBind :: SimplEnv
1031 -> InBinder -> OutId -> OutExpr -- Id and RHS
1032 -> (SimplEnv, [(OutId, OutExpr)]) -- Final envt and binding(s)
1034 completeBind env binder@(_,occ_info) new_id new_rhs
1035 | idMustNotBeINLINEd new_id -- Occurrence analyser says "don't inline"
1038 | atomic_rhs -- If rhs (after eta reduction) is atomic
1039 && not (isExported new_id) -- and binder isn't exported
1040 = -- Drop the binding completely
1042 env1 = notInScope env new_id
1043 env2 = bindIdToAtom env1 binder the_arg
1047 | atomic_rhs -- Rhs is atomic, and new_id is exported
1048 && case eta'd_rhs of { Var v -> isLocallyDefined v && not (isExported v); other -> False }
1049 = -- The local variable v will be eliminated next time round
1050 -- in favour of new_id, so it's a waste to replace all new_id's with v's
1052 -- This case is an optional improvement; saves a simplifier iteration
1053 (env, [(new_id, eta'd_rhs)])
1055 | otherwise -- Non-atomic
1057 env1 = extendEnvGivenBinding env occ_info new_id new_rhs
1062 new_binds = [(new_id, new_rhs)]
1063 atomic_rhs = is_atomic eta'd_rhs
1064 eta'd_rhs = case lookForConstructor env new_rhs of
1066 other -> etaCoreExpr new_rhs
1068 the_arg = case eta'd_rhs of
1073 ----------------------------------------------------------------------------
1074 A digression on constructor CSE
1082 Is it a good idea to replace the rhs @y:ys@ with @x@? This depends a
1083 bit on the compiler technology, but in general I believe not. For
1084 example, here's some code from a real program:
1086 const.Int.max.wrk{-s2516-} =
1087 \ upk.s3297# upk.s3298# ->
1091 a.s3299 = I#! upk.s3297#
1093 case (const.Int._tagCmp.wrk{-s2513-} upk.s3297# upk.s3298#) of {
1094 _LT -> I#! upk.s3298#
1099 The a.s3299 really isn't doing much good. We'd be better off inlining
1100 it. (Actually, let-no-escapery means it isn't as bad as it looks.)
1102 So the current strategy is to inline all known-form constructors, and
1103 only do the reverse (turn a constructor application back into a
1104 variable) when we find a let-expression:
1108 ... (let y = C a1 .. an in ...) ...
1110 where it is always good to ditch the binding for y, and replace y by
1113 ----------------------------------------------------------------------------
1115 ----------------------------------------------------------------------------
1116 A digression on "optimising" coercions
1118 The trouble is that we kept transforming
1126 and counting a couple of ticks for this non-transformation
1128 -- We want to ensure that all let-bound Coerces have
1129 -- atomic bodies, so they can freely be inlined.
1130 completeNonRec env binder new_id (Coerce coercion ty rhs)
1131 | not (is_atomic rhs)
1132 = newId (coreExprType rhs) `thenSmpl` \ inner_id ->
1134 (inner_id, dangerousArgOcc) inner_id rhs `thenSmpl` \ (env1, binds1) ->
1135 -- Dangerous occ because, like constructor args,
1136 -- it can be duplicated easily
1138 atomic_rhs = case runEager $ lookupId env1 inner_id of
1142 completeNonRec env1 binder new_id
1143 (Coerce coercion ty atomic_rhs) `thenSmpl` \ (env2, binds2) ->
1145 returnSmpl (env2, binds1 ++ binds2)
1147 ----------------------------------------------------------------------------
1151 %************************************************************************
1153 \subsection[Simplify-letrec]{Letrec-expressions}
1155 %************************************************************************
1159 Here's the game plan
1161 1. Float any let(rec)s out of the RHSs
1162 2. Clone all the Ids and extend the envt with these clones
1163 3. Simplify one binding at a time, adding each binding to the
1164 environment once it's done.
1166 This relies on the occurrence analyser to
1167 a) break all cycles with an Id marked MustNotBeInlined
1168 b) sort the decls into topological order
1169 The former prevents infinite inlinings, and the latter means
1170 that we get maximum benefit from working top to bottom.
1174 simplRec env pairs body_c body_ty
1175 = -- Do floating, if necessary
1176 floatBind env False (Rec pairs) `thenSmpl` \ [Rec pairs'] ->
1178 binders = map fst pairs'
1180 simplBinders env binders `thenSmpl` \ (env_w_clones, ids') ->
1181 simplRecursiveGroup env_w_clones ids' pairs' `thenSmpl` \ (pairs', new_env) ->
1183 body_c new_env `thenSmpl` \ body' ->
1185 returnSmpl (Let (Rec pairs') body')
1189 -- The env passed to simplRecursiveGroup already has
1190 -- bindings that clone the variables of the group.
1191 simplRecursiveGroup env new_ids []
1192 = returnSmpl ([], env)
1194 simplRecursiveGroup env (new_id : new_ids) ((binder@(id, occ_info), rhs) : pairs)
1195 | inlineUnconditionally ok_to_dup id occ_info
1196 = -- Single occurrence, so drop binding and extend env with the inlining
1197 -- This is a little delicate, because what if the unique occurrence
1198 -- is *before* this binding? This'll never happen, because
1199 -- either it'll be marked "never inline" or else its occurrence will
1200 -- occur after its binding in the group.
1202 -- If these claims aren't right Core Lint will spot an unbound
1203 -- variable. A quick fix is to delete this clause for simplRecursiveGroup
1205 new_env = extendEnvGivenInlining env new_id occ_info rhs
1207 simplRecursiveGroup new_env new_ids pairs
1210 = simplRhsExpr env binder rhs new_id `thenSmpl` \ (new_rhs, arity) ->
1212 new_id' = new_id `withArity` arity
1213 (new_env, new_binds') = completeBind env binder new_id' new_rhs
1215 simplRecursiveGroup new_env new_ids pairs `thenSmpl` \ (new_pairs, final_env) ->
1216 returnSmpl (new_binds' ++ new_pairs, final_env)
1218 ok_to_dup = switchIsSet env SimplOkToDupCode
1224 floatBind :: SimplEnv
1225 -> Bool -- True <=> Top level
1227 -> SmplM [InBinding]
1229 floatBind env top_level bind
1235 = tickN LetFloatFromLet n_extras `thenSmpl_`
1236 -- It's important to increment the tick counts if we
1237 -- do any floating. A situation where this turns out
1238 -- to be important is this:
1239 -- Float in produces:
1240 -- letrec x = let y = Ey in Ex
1242 -- Now floating gives this:
1246 --- We now want to iterate once more in case Ey doesn't
1247 -- mention x, in which case the y binding can be pulled
1248 -- out as an enclosing let(rec), which in turn gives
1249 -- the strictness analyser more chance.
1253 binds' = fltBind bind
1254 n_extras = sum (map no_of_binds binds') - no_of_binds bind
1256 float_lets = switchIsSet env SimplFloatLetsExposingWHNF
1257 always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
1259 -- fltBind guarantees not to return leaky floats
1260 -- and all the binders of the floats have had their demand-info zapped
1261 fltBind (NonRec bndr rhs)
1262 = binds ++ [NonRec bndr rhs']
1264 (binds, rhs') = fltRhs rhs
1269 pairs' = concat [ let
1270 (binds, rhs') = fltRhs rhs
1272 foldr get_pairs [(bndr, rhs')] binds
1273 | (bndr, rhs) <- pairs
1276 get_pairs (NonRec bndr rhs) rest = (bndr,rhs) : rest
1277 get_pairs (Rec pairs) rest = pairs ++ rest
1279 -- fltRhs has same invariant as fltBind
1281 | (always_float_let_from_let ||
1282 floatExposesHNF True False False rhs)
1289 -- fltExpr has same invariant as fltBind
1290 fltExpr (Let bind body)
1291 | not top_level || binds_wont_leak
1292 -- fltExpr guarantees not to return leaky floats
1293 = (binds' ++ body_binds, body')
1295 binds_wont_leak = all leakFreeBind binds'
1296 (body_binds, body') = fltExpr body
1297 binds' = fltBind (un_demandify_bind bind)
1299 fltExpr expr = ([], expr)
1301 -- Crude but effective
1302 no_of_binds (NonRec _ _) = 1
1303 no_of_binds (Rec pairs) = length pairs
1305 leakFreeBind (NonRec bndr rhs) = leakFree bndr rhs
1306 leakFreeBind (Rec pairs) = and [leakFree bndr rhs | (bndr, rhs) <- pairs]
1308 leakFree (id,_) rhs = case getIdArity id of
1309 ArityAtLeast n | n > 0 -> True
1310 ArityExactly n | n > 0 -> True
1311 other -> whnfOrBottom (mkFormSummary rhs)
1315 %************************************************************************
1317 \subsection[Simplify-atoms]{Simplifying atoms}
1319 %************************************************************************
1322 simplArg :: SimplEnv -> InArg -> Eager ans OutArg
1324 simplArg env (LitArg lit) = returnEager (LitArg lit)
1325 simplArg env (TyArg ty) = simplTy env ty `appEager` \ ty' ->
1326 returnEager (TyArg ty')
1327 simplArg env (VarArg id) = lookupId env id
1330 %************************************************************************
1332 \subsection[Simplify-quickies]{Some local help functions}
1334 %************************************************************************
1338 -- un_demandify_bind switches off the willBeDemanded Info field
1339 -- for bindings floated out of a non-demanded let
1340 un_demandify_bind (NonRec binder rhs)
1341 = NonRec (un_demandify_bndr binder) rhs
1342 un_demandify_bind (Rec pairs)
1343 = Rec [(un_demandify_bndr binder, rhs) | (binder,rhs) <- pairs]
1345 un_demandify_bndr (id, occ_info) = (id `addIdDemandInfo` noDemandInfo, occ_info)
1347 is_cheap_prim_app (Prim op _) = primOpOkForSpeculation op
1348 is_cheap_prim_app other = False
1350 computeResultType :: SimplEnv -> InType -> [OutArg] -> OutType
1351 computeResultType env expr_ty orig_args
1352 = simplTy env expr_ty `appEager` \ expr_ty' ->
1355 go ty (TyArg ty_arg : args) = go (mkAppTy ty ty_arg) args
1356 go ty (a:args) | isValArg a = case (splitFunTy_maybe ty) of
1357 Just (_, res_ty) -> go res_ty args
1359 pprPanic "computeResultType" (vcat [
1365 go expr_ty' orig_args
1368 var `withArity` UnknownArity = var
1369 var `withArity` arity = var `addIdArity` arity
1371 is_atomic (Var v) = True
1372 is_atomic (Lit l) = not (isNoRepLit l)
1373 is_atomic other = False