2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[Simplify]{The main module of the simplifier}
7 #include "HsVersions.h"
9 module Simplify ( simplTopBinds, simplExpr, simplBind ) where
11 IMPORT_1_3(List(partition))
14 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
15 IMPORT_DELOOPER(SmplLoop) -- paranoia checking
19 import CmdLineOpts ( SimplifierSwitch(..) )
20 import ConFold ( completePrim )
21 import CoreUnfold ( Unfolding, SimpleUnfolding, mkFormSummary,
22 exprIsTrivial, whnfOrBottom, inlineUnconditionally,
25 import CostCentre ( isSccCountCostCentre, cmpCostCentre, costsAreSubsumed, useCurrentCostCentre )
27 import CoreUtils ( coreExprType, nonErrorRHSs, maybeErrorApp,
28 unTagBinders, squashableDictishCcExpr
30 import Id ( idType, idMustBeINLINEd, idWantsToBeINLINEd, idMustNotBeINLINEd,
31 addIdArity, getIdArity,
32 getIdDemandInfo, addIdDemandInfo,
33 GenId{-instance NamedThing-}
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 )
44 import TyVar ( GenTyVar {- instance Eq -} )
45 import Pretty --( ($$) )
46 import PrimOp ( primOpOkForSpeculation, PrimOp(..) )
47 import SimplCase ( simplCase, bindLargeRhs )
50 import SimplVar ( completeVar )
51 import Unique ( Unique )
53 import Type ( mkTyVarTy, mkTyVarTys, mkAppTy, applyTy, mkFunTys, maybeAppDataTyCon,
54 splitFunTy, splitFunTyExpandingDicts, getFunTy_maybe, eqTy
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 )
62 The controlling flags, and what they do
63 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)
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
83 -ffloat-lets-ok = OK to float lets out of lets if the enclosing
84 let is strict or if the floating will expose
87 -ffloat-primops-ok = OK to float out of lets cases whose scrutinee
88 is a primop that cannot fail [simplifier].
90 -fcode-duplication-ok = allows the previous option to work on cases with
91 multiple branches [simplifier].
93 -flet-to-case = does let-to-case transformation [simplifier].
95 -fcase-of-case = does case of case transformation [simplifier].
97 -fpedantic-bottoms = does not allow:
98 case x of y -> e ===> e[x/y]
99 (which may turn bottom into non-bottom)
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
109 let x = e in ...x... ===> let x = e in ...e...
111 We have two mechanisms for inlining:
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
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.
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.
134 We *never* put a non-HNF unfolding in the UnfoldEnv except in the
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:
143 f = \y -> ...y...y...y...
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@.
150 Because of this, the "unconditional-inline" mechanism above is the
151 only way in which non-HNFs can get inlined.
156 When a variable has an INLINE pragma on it --- which includes wrappers
157 produced by the strictness analyser --- we treat it rather carefully.
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
165 in {-# INLINE y #-} y = f 3
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.)
173 So, we clean out the UnfoldEnv of all SimpleUnfolding inlinings before
174 going into such an RHS.
176 What about imports? They don't really matter much because we only
177 inline relatively small things via imports.
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.
186 %************************************************************************
188 \subsection[Simplify-simplExpr]{The main function: simplExpr}
190 %************************************************************************
192 At the top level things are a little different.
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)
198 simplTopBinds :: SimplEnv -> [InBinding] -> SmplM [OutBinding]
200 -- Dead code is now discarded by the occurrence analyser,
202 simplTopBinds env binds
203 = mapSmpl (floatBind env True) binds `thenSmpl` \ binds_s ->
204 simpl_top_binds env (concat binds_s)
206 simpl_top_binds env [] = returnSmpl []
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')
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
221 -- This may seem optional, but I found an occasion when it Really matters.
222 -- Consider foo{n} = ...foo...
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...
229 -- Sure we could have made the indirection-shorting a bit cleverer, but
230 -- propagating pragma info is a Good Idea anyway.
232 env1 = extendIdEnvWithClones env binders ids
234 simplRecursiveGroup env1 ids pairs `thenSmpl` \ (bind', new_env) ->
235 simpl_top_binds new_env binds `thenSmpl` \ binds' ->
236 returnSmpl (Rec bind' : binds')
238 binders = map fst pairs
239 ids = map fst binders
242 %************************************************************************
244 \subsection[Simplify-simplExpr]{The main function: simplExpr}
246 %************************************************************************
250 simplExpr :: SimplEnv
251 -> InExpr -> [OutArg]
252 -> OutType -- Type of (e args); i.e. type of overall result
256 The expression returned has the same meaning as the input expression
257 applied to the specified arguments.
262 Check if there's a macro-expansion, and if so rattle on. Otherwise do
263 the more sophisticated stuff.
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 )
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.
283 simplExpr env (Lit l) [] result_ty = returnSmpl (Lit l)
285 simplExpr env (Lit l) _ _ = panic "simplExpr:Lit with argument"
289 Primitive applications are simple.
290 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
292 NB: Prim expects an empty argument list! (Because it should be
293 saturated and not higher-order. ADR)
296 simplExpr env (Prim op prim_args) args result_ty
298 mapEager (simplArg env) prim_args `appEager` \ prim_args' ->
299 simpl_op op `appEager` \ op' ->
300 completePrim env op' prim_args'
302 -- PrimOps just need any types in them renamed.
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')
309 simpl_op other_op = returnEager other_op
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).
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')
325 Applications are easy too:
326 ~~~~~~~~~~~~~~~~~~~~~~~~~~
327 Just stuff 'em in the arg stack
330 simplExpr env (App fun arg) args result_ty
331 = simplArg env arg `appEager` \ arg' ->
332 simplExpr env fun (arg' : args) result_ty
338 First the case when it's applied to an argument.
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
348 simplExpr env tylam@(Lam (TyBinder tyvar) body) [] result_ty
349 = cloneTyVarSmpl tyvar `thenSmpl` \ tyvar' ->
351 new_ty = mkTyVarTy tyvar'
352 new_env = extendTyEnv env tyvar new_ty
353 new_result_ty = applyTy result_ty new_ty
355 simplExpr new_env body [] new_result_ty `thenSmpl` \ body' ->
356 returnSmpl (Lam (TyBinder tyvar') body')
359 simplExpr env (Lam (TyBinder _) _) (_ : _) result_ty
360 = panic "simplExpr:TyLam with non-TyArg"
368 There's a complication with lambdas that aren't saturated.
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.
380 simplExpr env expr@(Lam (ValBinder binder) body) orig_args result_ty
381 = go 0 env expr orig_args
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
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!
393 new_env = markDangerousOccs env (take n orig_args)
395 simplValLam new_env expr 0 {- Guaranteed applied to at least 0 args! -} result_ty
396 `thenSmpl` \ (expr', arity) ->
399 go n env non_val_lam_expr args -- The lambda had enough arguments
400 = simplExpr env non_val_lam_expr args result_ty
408 simplExpr env (Let bind body) args result_ty
409 = simplBind env bind (\env -> simplExpr env body args result_ty) result_ty
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
424 simplExpr env (Coerce coercion ty body) args result_ty
425 = simplCoerce env coercion ty body args result_ty
432 1) Eliminating nested sccs ...
433 We must be careful to maintain the scc counts ...
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
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
446 2) Moving sccs inside lambdas ...
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
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
459 3) Eliminating dict sccs ...
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
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)
473 simplExpr env (SCC cost_centre body) args result_ty
475 new_env = setEnclosingCC env cost_centre
477 simplExpr new_env body args result_ty `thenSmpl` \ body' ->
478 returnSmpl (SCC cost_centre body')
481 %************************************************************************
483 \subsection{Simplify RHS of a Let/Letrec}
485 %************************************************************************
487 simplRhsExpr does arity-expansion. That is, given:
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
493 it transforms the rhs to
495 /\tyvars -> \a1 ... an b(n+1) ... bk -> (e b(n+1) ... bk)
497 This is a Very Good Thing!
504 -> OutId -- The new binder (used only for its type)
505 -> SmplM (OutExpr, ArityInfo)
508 First a special case for variable right-hand sides
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.
515 If we leave the binding unchanged, we will certainly replace v by w at
516 every occurrence of v, which is good enough.
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.
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
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.
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)
541 = case (runEager $ lookupId env v) of
542 VarArg v' | not (must_unfold v') -> Just v'
545 Just the_var = maybe_stop_at_var
547 must_unfold v' = idMustBeINLINEd v'
548 || case lookupOutIdEnv env v' of
549 Just (_, _, InUnfolding _ _) -> True
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)
561 | otherwise -- OK, use the big hammer
562 = -- Deal with the big lambda part
563 ASSERT( null uvars ) -- For now
565 mapSmpl cloneTyVarSmpl tyvars `thenSmpl` \ tyvars' ->
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)
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) ->
576 -- Put on the big lambdas, trying to float out any bindings caught inside
577 mkRhsTyLam tyvars' lambda' `thenSmpl` \ rhs' ->
579 returnSmpl (rhs', arity)
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
587 -- The top level "enclosing CC" is "SUBSUMED". But the enclosing CC
588 -- for the rhs of top level defs is "OST_CENTRE". Consider
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.
594 current_cc = getEnclosingCC env
595 env1 | costsAreSubsumed current_cc = setEnclosingCC env useCurrentCostCentre
598 (uvars, tyvars, body) = collectUsageAndTyBinders rhs
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 = cloneIds env binders `thenSmpl` \ binders' ->
629 new_env = extendIdEnvWithClones env binders binders'
631 simplExpr new_env body [] body_ty `thenSmpl` \ body' ->
632 returnSmpl (mkValLam binders' body', final_arity)
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])
644 tick EtaExpansion `thenSmpl_`
645 cloneIds env binders `thenSmpl` \ binders' ->
647 new_env = extendIdEnvWithClones env binders binders'
649 newIds extra_binder_tys `thenSmpl` \ extra_binders' ->
650 simplExpr new_env body (map VarArg extra_binders') etad_body_ty `thenSmpl` \ body' ->
652 mkValLam (binders' ++ extra_binders') body',
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])
665 drop no_of_binders arg_tys
666 body_ty = mkFunTys potential_extra_binder_tys res_ty
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
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.
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)
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
684 (min_no_of_args - no_of_binders)
686 -- Next, try seeing if there's a lambda hidden inside
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.
692 (etaExpandCount body `min` length potential_extra_binder_tys)
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
698 case potential_extra_binder_tys of
699 [ty] | ty `eqTy` realWorldStateTy -> 1
705 %************************************************************************
707 \subsection[Simplify-coerce]{Coerce expressions}
709 %************************************************************************
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
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
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)
727 expr_ty = coreExprType (unTagBinders expr) -- Rather like simplCase other_scrut
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
736 %************************************************************************
738 \subsection[Simplify-bind]{Binding groups}
740 %************************************************************************
743 simplBind :: SimplEnv
745 -> (SimplEnv -> SmplM OutExpr)
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
754 %************************************************************************
756 \subsection[Simplify-let]{Let-expressions}
758 %************************************************************************
762 The booleans controlling floating have to be set with a little care.
763 Here's one performance bug I found:
765 let x = let y = let z = case a# +# 1 of {b# -> E1}
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
775 Floating case from let
776 ~~~~~~~~~~~~~~~~~~~~~~
777 When floating cases out of lets, remember this:
779 let x* = case e of alts
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:
793 p1 -> foldr c n (build e1)
794 p2 -> foldr c n (build e2)
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
803 let join_body x' = foldr c n x'
805 p1 -> let x* = build e1
807 p2 -> let x* = build e2
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
816 Let to case: two points
819 Point 1. We defer let-to-case for all data types except single-constructor
820 ones. Suppose we change
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.
832 If x is a single-constructor type, then we go ahead anyway, giving
834 case e of (y,z) -> let x = (y,z) in b
836 because now we can squash case-on-x wherever they occur in b.
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.
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
847 let a*::Int = case v of {p1->e1; p2->e2}
850 (The * means that a is sure to be demanded.)
851 If we do case-floating first we get this:
855 p1-> let a*=e1 in k a
856 p2-> let a*=e2 in k a
858 Now watch what happens if we do let-to-case first:
860 case (case v of {p1->e1; p2->e2}) of
861 Int a# -> let a*=I# a# in b
863 let k = \a# -> let a*=I# a# in b
865 p1 -> case e1 of I# a# -> k a#
866 p1 -> case e2 of I# a# -> k a#
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.)
871 We do not do let to case for WHNFs, e.g.
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:
882 case error of x -> ...
886 Notice that let to case occurs only if x is used strictly in its body
891 -- Dead code is now discarded by the occurrence analyser,
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)
899 | idWantsToBeINLINEd id
900 = complete_bind env rhs -- Don't mess about with floating or let-to-case on
905 -- Try let-to-case; see notes below about let-to-case
906 simpl_bind env rhs | try_let_to_case &&
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.
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:
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!
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
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_`
937 -- First, bind large let-body if necessary
938 if ok_to_dup || isSingleton (nonErrorRHSs alts)
940 simplCase env scrut alts (\env rhs -> simpl_bind env rhs) body_ty
942 bindLargeRhs env [binder] body_ty body_c `thenSmpl` \ (extra_binding, new_body) ->
944 body_c' = \env -> simplExpr env new_body [] body_ty
945 case_c = \env rhs -> simplNonRec env binder rhs body_c' body_ty
947 simplCase env scrut alts case_c body_ty `thenSmpl` \ case_expr ->
948 returnSmpl (Let extra_binding case_expr)
950 -- None of the above; simplify rhs and tidy up
951 simpl_bind env rhs = complete_bind env rhs
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')
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
970 demand_info = getIdDemandInfo id
971 will_be_demanded = willBeDemanded demand_info
974 form = mkFormSummary rhs
975 rhs_is_bot = case form of
978 rhs_is_whnf = case form of
983 float_exposes_hnf = floatExposesHNF float_lets float_primops ok_to_dup rhs
985 let_floating_ok = (will_be_demanded && not no_float) ||
986 always_float_let_from_let ||
989 case_floating_ok scrut = (will_be_demanded && not no_float) ||
990 (float_exposes_hnf && is_cheap_prim_app scrut && float_primops)
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
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:
1007 const.Int.max.wrk{-s2516-} =
1008 \ upk.s3297# upk.s3298# ->
1012 a.s3299 = I#! upk.s3297#
1014 case (const.Int._tagCmp.wrk{-s2513-} upk.s3297# upk.s3298#) of {
1015 _LT -> I#! upk.s3298#
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.)
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:
1029 ... (let y = C a1 .. an in ...) ...
1031 where it is always good to ditch the binding for y, and replace y by
1032 x. That's just what completeLetBinding does.
1037 The trouble is that we keep transforming
1045 and counting a couple of ticks for this non-transformation
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 ->
1053 (inner_id, dangerousArgOcc) inner_id rhs `thenSmpl` \ (env1, binds1) ->
1054 -- Dangerous occ because, like constructor args,
1055 -- it can be duplicated easily
1057 atomic_rhs = case runEager $ lookupId env1 inner_id of
1061 completeNonRec env1 binder new_id
1062 (Coerce coercion ty atomic_rhs) `thenSmpl` \ (env2, binds2) ->
1064 returnSmpl (env2, binds1 ++ binds2)
1068 -- Right hand sides that are constructors
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
1080 = tick ConReused `thenSmpl_`
1081 returnSmpl (extendIdEnvWithAtom env binder (VarArg it), [NonRec new_id rhs])
1083 maybe_existing_con = lookForConstructor env con con_args
1084 Just it = maybe_existing_con
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])
1096 | otherwise -- Non atomic rhs (don't eta after all)
1097 = returnSmpl (non_atomic_env , [NonRec new_id new_rhs])
1099 atomic_env = extendIdEnvWithAtom env binder the_arg
1101 non_atomic_env = extendEnvGivenBinding (extendIdEnvWithClone env binder new_id)
1102 occ_info new_id new_rhs
1104 eta'd_rhs = etaCoreExpr new_rhs
1105 the_arg = case eta'd_rhs of
1110 %************************************************************************
1112 \subsection[Simplify-letrec]{Letrec-expressions}
1114 %************************************************************************
1118 Here's the game plan
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.
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.
1133 simplRec env pairs body_c body_ty
1134 = -- Do floating, if necessary
1135 floatBind env False (Rec pairs) `thenSmpl` \ [Rec pairs'] ->
1137 binders = map fst pairs'
1139 cloneIds env binders `thenSmpl` \ ids' ->
1141 env_w_clones = extendIdEnvWithClones env binders ids'
1143 simplRecursiveGroup env_w_clones ids' pairs' `thenSmpl` \ (pairs', new_env) ->
1145 body_c new_env `thenSmpl` \ body' ->
1147 returnSmpl (Let (Rec pairs') body')
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)
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.
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
1167 new_env = extendEnvGivenInlining env new_id occ_info rhs
1169 simplRecursiveGroup new_env new_ids pairs
1172 = simplRhsExpr env binder rhs new_id `thenSmpl` \ (new_rhs, arity) ->
1174 new_id' = new_id `withArity` arity
1176 -- ToDo: this next bit could usefully share code with completeNonRec
1179 | idMustNotBeINLINEd new_id -- Occurrence analyser says "don't inline"
1182 | is_atomic eta'd_rhs -- If rhs (after eta reduction) is atomic
1183 = extendIdEnvWithAtom env binder the_arg
1185 | otherwise -- Non-atomic
1186 = extendEnvGivenBinding env occ_info new_id new_rhs
1187 -- Don't eta if it doesn't eliminate the binding
1189 eta'd_rhs = etaCoreExpr new_rhs
1190 the_arg = case eta'd_rhs of
1194 simplRecursiveGroup new_env new_ids pairs `thenSmpl` \ (new_pairs, final_env) ->
1195 returnSmpl ((new_id', new_rhs) : new_pairs, final_env)
1197 ok_to_dup = switchIsSet env SimplOkToDupCode
1203 floatBind :: SimplEnv
1204 -> Bool -- True <=> Top level
1206 -> SmplM [InBinding]
1208 floatBind env top_level bind
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
1221 -- Now floating gives this:
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.
1232 (binds', _, n_extras) = fltBind bind
1234 float_lets = switchIsSet env SimplFloatLetsExposingWHNF
1235 always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
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'],
1244 (binds, rhs') = fltRhs rhs
1249 binders `zip` rhss')],
1250 and (zipWith leakFree binders rhss'),
1255 (binders, rhss) = unzip pairs
1256 (binds_s, rhss') = mapAndUnzip fltRhs rhss
1257 extras = concat (map get_pairs (concat binds_s))
1259 get_pairs (NonRec bndr rhs) = [(bndr,rhs)]
1260 get_pairs (Rec pairs) = pairs
1262 -- fltRhs has same invariant as fltBind
1264 | (always_float_let_from_let ||
1265 floatExposesHNF True False False rhs)
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')
1278 (body_binds, body') = fltExpr body
1279 (binds', binds_wont_leak, _) = fltBind bind
1281 fltExpr expr = ([], expr)
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)
1291 %************************************************************************
1293 \subsection[Simplify-atoms]{Simplifying atoms}
1295 %************************************************************************
1298 simplArg :: SimplEnv -> InArg -> Eager ans OutArg
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
1306 %************************************************************************
1308 \subsection[Simplify-quickies]{Some local help functions}
1310 %************************************************************************
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]
1323 un_demandify (id, occ_info) = (id `addIdDemandInfo` noDemandInfo, occ_info)
1325 is_cheap_prim_app (Prim op _) = primOpOkForSpeculation op
1326 is_cheap_prim_app other = False
1328 computeResultType :: SimplEnv -> InType -> [OutArg] -> OutType
1329 computeResultType env expr_ty orig_args
1330 = simplTy env expr_ty `appEager` \ expr_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
1337 pprPanic "computeResultType" (vcat [
1338 ppr PprDebug (a:args),
1339 ppr PprDebug orig_args,
1340 ppr PprDebug expr_ty',
1343 go expr_ty' orig_args
1346 var `withArity` UnknownArity = var
1347 var `withArity` arity = var `addIdArity` arity
1349 is_atomic (Var v) = True
1350 is_atomic (Lit l) = not (isNoRepLit l)
1351 is_atomic other = False