2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[SimplUtils]{The simplifier utilities}
8 simplBinder, simplBinders, simplIds,
12 mkCase, findAlt, findDefault
15 #include "HsVersions.h"
18 import CmdLineOpts ( opt_DoEtaReduction, switchIsOn, SimplifierSwitch(..) )
20 import CoreUtils ( exprIsCheap, exprIsTrivial, exprFreeVars, cheapEqExpr,
24 import Id ( Id, idType, isBottomingId, getIdArity, isId, idName,
25 getInlinePragma, setInlinePragma,
28 import IdInfo ( arityLowerBound, InlinePragInfo(..) )
29 import Demand ( isStrict )
30 import Maybes ( maybeToBool )
31 import Const ( Con(..) )
32 import Name ( isLocalName )
34 import Type ( Type, tyVarsOfType, tyVarsOfTypes, mkForAllTys, mkTyVarTys,
35 splitTyConApp_maybe, mkTyVarTy, substTyVar
37 import Var ( setVarUnique )
39 import UniqSupply ( splitUniqSupply, uniqFromSupply )
40 import Util ( zipWithEqual, mapAccumL )
45 %************************************************************************
47 \section{Dealing with a single binder}
49 %************************************************************************
51 When we hit a binder we may need to
52 (a) apply the the type envt (if non-empty) to its type
53 (b) apply the type envt and id envt to its SpecEnv (if it has one)
54 (c) give it a new unique to avoid name clashes
57 simplBinders :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
58 simplBinders bndrs thing_inside
59 = getSwitchChecker `thenSmpl` \ sw_chkr ->
60 getSimplBinderStuff `thenSmpl` \ stuff ->
62 must_clone = switchIsOn sw_chkr SimplPleaseClone
63 (stuff', bndrs') = mapAccumL (subst_binder must_clone) stuff bndrs
65 setSimplBinderStuff stuff' $
68 simplBinder :: InBinder -> (OutBinder -> SimplM a) -> SimplM a
69 simplBinder bndr thing_inside
70 = getSwitchChecker `thenSmpl` \ sw_chkr ->
71 getSimplBinderStuff `thenSmpl` \ stuff ->
73 must_clone = switchIsOn sw_chkr SimplPleaseClone
74 (stuff', bndr') = subst_binder must_clone stuff bndr
76 setSimplBinderStuff stuff' $
79 -- Same semantics as simplBinders, but a little less
80 -- plumbing and hence a little more efficient.
81 -- Maybe not worth the candle?
82 simplIds :: [InBinder] -> ([OutBinder] -> SimplM a) -> SimplM a
83 simplIds ids thing_inside
84 = getSwitchChecker `thenSmpl` \ sw_chkr ->
85 getSimplBinderStuff `thenSmpl` \ (ty_subst, id_subst, in_scope, us) ->
87 must_clone = switchIsOn sw_chkr SimplPleaseClone
88 (id_subst', in_scope', us', ids') = substIds (simpl_clone_fn must_clone)
89 ty_subst id_subst in_scope us ids
91 setSimplBinderStuff (ty_subst, id_subst', in_scope', us') $
94 subst_binder must_clone (ty_subst, id_subst, in_scope, us) bndr
96 = case substTyVar ty_subst in_scope bndr of
97 (ty_subst', in_scope', bndr') -> ((ty_subst', id_subst, in_scope', us), bndr')
100 = case substId (simpl_clone_fn must_clone) ty_subst id_subst in_scope us bndr of
101 (id_subst', in_scope', us', bndr')
102 -> ((ty_subst, id_subst', in_scope', us'), bndr')
104 simpl_clone_fn must_clone in_scope us id
105 | (must_clone && isLocalName (idName id))
106 || id `elemVarSet` in_scope
107 = case splitUniqSupply us of
108 (us1, us2) -> Just (us1, setVarUnique id (uniqFromSupply us2))
115 %************************************************************************
117 \subsection{Local tyvar-lifting}
119 %************************************************************************
121 mkRhsTyLam tries this transformation, when the big lambda appears as
122 the RHS of a let(rec) binding:
124 /\abc -> let(rec) x = e in b
126 let(rec) x' = /\abc -> let x = x' a b c in e
128 /\abc -> let x = x' a b c in b
130 This is good because it can turn things like:
132 let f = /\a -> letrec g = ... g ... in g
134 letrec g' = /\a -> ... g' a ...
138 which is better. In effect, it means that big lambdas don't impede
141 This optimisation is CRUCIAL in eliminating the junk introduced by
142 desugaring mutually recursive definitions. Don't eliminate it lightly!
144 So far as the implemtation is concerned:
146 Invariant: go F e = /\tvs -> F e
150 = Let x' = /\tvs -> F e
154 G = F . Let x = x' tvs
156 go F (Letrec xi=ei in b)
157 = Letrec {xi' = /\tvs -> G ei}
161 G = F . Let {xi = xi' tvs}
165 | isTyVar b = case collectTyBinders e of
166 (bs,body) -> mkRhsTyLam_help (b:bs) body
168 mkRhsTyLam other_expr -- No-op if not a type lambda
169 = returnSmpl other_expr
172 mkRhsTyLam_help tyvars body
175 main_tyvar_set = mkVarSet tyvars
177 go fn (Let bind@(NonRec var rhs) body) | exprIsTrivial rhs
178 = go (fn . Let bind) body
180 go fn (Let bind@(NonRec var rhs) body)
181 = mk_poly tyvars_here var `thenSmpl` \ (var', rhs') ->
182 go (fn . Let (mk_silly_bind var rhs')) body `thenSmpl` \ body' ->
183 returnSmpl (Let (NonRec var' (mkLams tyvars_here (fn rhs))) body')
186 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfType var_ty)
187 -- tyvars_here was an attempt to reduce the number of tyvars
188 -- wrt which the new binding is abstracted. But the naive
189 -- approach of abstract wrt the tyvars free in the Id's type
191 -- /\ a b -> let t :: (a,b) = (e1, e2)
194 -- Here, b isn't free in a's type, but we must nevertheless
195 -- abstract wrt b as well, because t's type mentions b.
196 -- Since t is floated too, we'd end up with the bogus:
197 -- poly_t = /\ a b -> (e1, e2)
198 -- poly_x = /\ a -> fst (poly_t a *b*)
199 -- So for now we adopt the even more naive approach of
200 -- abstracting wrt *all* the tyvars. We'll see if that
201 -- gives rise to problems. SLPJ June 98
205 go fn (Let (Rec prs) body)
206 = mapAndUnzipSmpl (mk_poly tyvars_here) vars `thenSmpl` \ (vars', rhss') ->
208 gn body = fn $ foldr Let body (zipWith mk_silly_bind vars rhss')
210 go gn body `thenSmpl` \ body' ->
211 returnSmpl (Let (Rec (vars' `zip` [mkLams tyvars_here (gn rhs) | rhs <- rhss])) body')
213 (vars,rhss) = unzip prs
215 -- varSetElems (main_tyvar_set `intersectVarSet` tyVarsOfTypes var_tys)
216 -- See notes with tyvars_here above
218 var_tys = map idType vars
220 go fn body = returnSmpl (mkLams tyvars (fn body))
222 mk_poly tyvars_here var
223 = newId (mkForAllTys tyvars_here (idType var)) $ \ poly_id ->
225 -- It's crucial to copy the inline-prag of the original var, because
226 -- we're looking at occurrence-analysed but as yet unsimplified code!
227 -- In particular, we mustn't lose the loop breakers.
229 -- *However* we don't want to retain a single-occurrence or dead-var info
230 -- because we're adding a load of "silly bindings" of the form
231 -- var _U_ = poly_var t1 t2
232 -- with a must-inline pragma on the silly binding to prevent the
233 -- poly-var from being inlined right back in. Since poly_var now
234 -- occurs inside an INLINE binding, it should be given a ManyOcc,
235 -- else it may get inlined unconditionally
236 poly_inline_prag = case getInlinePragma var of
237 ICanSafelyBeINLINEd _ _ -> NoInlinePragInfo
238 IAmDead -> NoInlinePragInfo
239 var_inline_prag -> var_inline_prag
241 poly_id' = setInlinePragma poly_id poly_inline_prag
243 returnSmpl (poly_id', mkTyApps (Var poly_id') (mkTyVarTys tyvars_here))
245 mk_silly_bind var rhs = NonRec (setInlinePragma var IWantToBeINLINEd) rhs
246 -- The addInlinePragma is really important! If we don't say
247 -- INLINE on these silly little bindings then look what happens!
248 -- Suppose we start with:
250 -- x = let g = /\a -> \x -> f x x
252 -- /\ b -> let g* = g b in E
254 -- Then: * the binding for g gets floated out
255 -- * but then it gets inlined into the rhs of g*
256 -- * then the binding for g* is floated out of the /\b
257 -- * so we're back to square one
258 -- The silly binding for g* must be INLINE, so that no inlining
259 -- will happen in its RHS.
260 -- PS: Jun 98: actually this isn't important any more;
261 -- inlineUnconditionally will catch the type applicn
262 -- and inline it unconditionally, without ever trying
263 -- to simplify the RHS
267 %************************************************************************
269 \subsection{Eta reduction}
271 %************************************************************************
273 @etaCoreExpr@ trys an eta reduction at the top level of a Core Expr.
275 e.g. \ x y -> f x y ===> f
278 a) Before constructing an Unfolding, to
279 try to make the unfolding smaller;
280 b) In tidyCoreExpr, which is done just before converting to STG.
282 But we only do this if
283 i) It gets rid of a whole lambda, not part.
284 The idea is that lambdas are often quite helpful: they indicate
285 head normal forms, so we don't want to chuck them away lightly.
287 ii) It exposes a simple variable or a type application; in short
288 it exposes a "trivial" expression. (exprIsTrivial)
291 etaCoreExpr :: CoreExpr -> CoreExpr
292 -- ToDo: we should really check that we don't turn a non-bottom
293 -- lambda into a bottom variable. Sigh
295 etaCoreExpr expr@(Lam bndr body)
297 = check (reverse binders) body
299 (binders, body) = collectBinders expr
302 | exprIsTrivial body && not (any (`elemVarSet` body_fvs) binders)
305 body_fvs = exprFreeVars body
307 check (b : bs) (App fun arg)
308 | (varToCoreExpr b `cheapEqExpr` arg)
309 && not (is_strict_binder b)
312 check _ _ = expr -- Bale out
314 -- We don't want to eta-abstract (\x -> f x) if x carries a "strict"
315 -- demand info. That demand info conveys useful information to the
316 -- call site, via the let-to-case transform, so we don't want to discard it.
317 is_strict_binder b = isId b && isStrict (getIdDemandInfo b)
319 etaCoreExpr expr = expr -- The common case
323 %************************************************************************
325 \subsection{Eta expansion}
327 %************************************************************************
329 @etaExpandCount@ takes an expression, E, and returns an integer n,
332 E ===> (\x1::t1 x1::t2 ... xn::tn -> E x1 x2 ... xn)
334 is a safe transformation. In particular, the transformation should
335 not cause work to be duplicated, unless it is ``cheap'' (see
336 @manifestlyCheap@ below).
338 @etaExpandCount@ errs on the conservative side. It is always safe to
341 An application of @error@ is special, because it can absorb as many
342 arguments as you care to give it. For this special case we return
343 100, to represent "infinity", which is a bit of a hack.
346 etaExpandCount :: CoreExpr
347 -> Int -- Number of extra args you can safely abstract
349 etaExpandCount (Lam b body)
351 = 1 + etaExpandCount body
353 etaExpandCount (Let bind body)
354 | all exprIsCheap (rhssOfBind bind)
355 = etaExpandCount body
357 etaExpandCount (Case scrut _ alts)
359 = minimum [etaExpandCount rhs | (_,_,rhs) <- alts]
361 etaExpandCount fun@(Var _) = eta_fun fun
363 etaExpandCount (App fun (Type ty))
365 etaExpandCount (App fun arg)
366 | exprIsCheap arg = case etaExpandCount fun of
368 n -> n-1 -- Knock off one
370 etaExpandCount other = 0 -- Give up
373 -- Scc (pessimistic; ToDo),
374 -- Let with non-whnf rhs(s),
375 -- Case with non-whnf scrutinee
377 -----------------------------
378 eta_fun :: CoreExpr -- The function
379 -> Int -- How many args it can safely be applied to
381 eta_fun (App fun (Type ty)) = eta_fun fun
384 | isBottomingId v -- Bottoming ids have "infinite arity"
385 = 10000 -- Blargh. Infinite enough!
387 eta_fun (Var v) = arityLowerBound (getIdArity v)
389 eta_fun other = 0 -- Give up
393 %************************************************************************
395 \subsection{Case absorption and identity-case elimination}
397 %************************************************************************
400 mkCase :: SwitchChecker -> OutExpr -> OutId -> [OutAlt] -> SimplM OutExpr
403 @mkCase@ tries the following transformation (if possible):
405 case e of b { ==> case e of b {
406 p1 -> rhs1 p1 -> rhs1
408 pm -> rhsm pm -> rhsm
409 _ -> case b of b' { pn -> rhsn[b/b'] {or (alg) let b=b' in rhsn}
410 {or (prim) case b of b' { _ -> rhsn}}
413 po -> rhso _ -> rhsd[b/b'] {or let b'=b in rhsd}
417 which merges two cases in one case when -- the default alternative of
418 the outer case scrutises the same variable as the outer case This
419 transformation is called Case Merging. It avoids that the same
420 variable is scrutinised multiple times.
423 mkCase sw_chkr scrut outer_bndr outer_alts
424 | switchIsOn sw_chkr SimplCaseMerge
425 && maybeToBool maybe_case_in_default
427 = tick CaseMerge `thenSmpl_`
428 returnSmpl (Case scrut outer_bndr new_alts)
429 -- Warning: don't call mkCase recursively!
430 -- Firstly, there's no point, because inner alts have already had
431 -- mkCase applied to them, so they won't have a case in their default
432 -- Secondly, if you do, you get an infinite loop, because the bindNonRec
433 -- in munge_rhs puts a case into the DEFAULT branch!
435 new_alts = outer_alts_without_deflt ++ munged_inner_alts
436 maybe_case_in_default = case findDefault outer_alts of
437 (outer_alts_without_default,
438 Just (Case (Var scrut_var) inner_bndr inner_alts))
440 | outer_bndr == scrut_var
441 -> Just (outer_alts_without_default, inner_bndr, inner_alts)
444 Just (outer_alts_without_deflt, inner_bndr, inner_alts) = maybe_case_in_default
446 -- Eliminate any inner alts which are shadowed by the outer ones
447 outer_cons = [con | (con,_,_) <- outer_alts_without_deflt]
449 munged_inner_alts = [ (con, args, munge_rhs rhs)
450 | (con, args, rhs) <- inner_alts,
451 not (con `elem` outer_cons) -- Eliminate shadowed inner alts
453 munge_rhs rhs = bindNonRec inner_bndr (Var outer_bndr) rhs
456 Now the identity-case transformation:
465 mkCase sw_chkr scrut case_bndr alts
466 | all identity_alt alts
467 = tick CaseIdentity `thenSmpl_`
470 identity_alt (DEFAULT, [], Var v) = v == case_bndr
471 identity_alt (con, args, Con con' args') = con == con' &&
472 and (zipWithEqual "mkCase"
474 (map Type arg_tys ++ map varToCoreExpr args)
476 identity_alt other = False
478 arg_tys = case splitTyConApp_maybe (idType case_bndr) of
479 Just (tycon, arg_tys) -> arg_tys
485 mkCase sw_chkr other_scrut case_bndr other_alts
486 = returnSmpl (Case other_scrut case_bndr other_alts)
491 findDefault :: [CoreAlt] -> ([CoreAlt], Maybe CoreExpr)
492 findDefault [] = ([], Nothing)
493 findDefault ((DEFAULT,args,rhs) : alts) = ASSERT( null alts && null args )
495 findDefault (alt : alts) = case findDefault alts of
496 (alts', deflt) -> (alt : alts', deflt)
498 findAlt :: Con -> [CoreAlt] -> CoreAlt
502 go [] = pprPanic "Missing alternative" (ppr con $$ vcat (map ppr alts))
503 go (alt : alts) | matches alt = alt
504 | otherwise = go alts
506 matches (DEFAULT, _, _) = True
507 matches (con1, _, _) = con == con1