Reduce magic for seqId
[ghc.git] / compiler / coreSyn / CoreSubst.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 Utility functions on @Core@ syntax
7 -}
8
9 {-# LANGUAGE CPP #-}
10 module CoreSubst (
11 -- * Main data types
12 Subst(..), -- Implementation exported for supercompiler's Renaming.hs only
13 TvSubstEnv, IdSubstEnv, InScopeSet,
14
15 -- ** Substituting into expressions and related types
16 deShadowBinds, substSpec, substRulesForImportedIds,
17 substTy, substCo, substExpr, substExprSC, substBind, substBindSC,
18 substUnfolding, substUnfoldingSC,
19 lookupIdSubst, lookupTvSubst, lookupCvSubst, substIdOcc,
20 substTickish, substVarSet,
21
22 -- ** Operations on substitutions
23 emptySubst, mkEmptySubst, mkSubst, mkOpenSubst, substInScope, isEmptySubst,
24 extendIdSubst, extendIdSubstList, extendTvSubst, extendTvSubstList,
25 extendCvSubst, extendCvSubstList,
26 extendSubst, extendSubstList, extendSubstWithVar, zapSubstEnv,
27 addInScopeSet, extendInScope, extendInScopeList, extendInScopeIds,
28 isInScope, setInScope,
29 delBndr, delBndrs,
30
31 -- ** Substituting and cloning binders
32 substBndr, substBndrs, substRecBndrs,
33 cloneBndr, cloneBndrs, cloneIdBndr, cloneIdBndrs, cloneRecIdBndrs,
34
35 -- ** Simple expression optimiser
36 simpleOptPgm, simpleOptExpr, simpleOptExprWith,
37 exprIsConApp_maybe, exprIsLiteral_maybe, exprIsLambda_maybe,
38 ) where
39
40 #include "HsVersions.h"
41
42 import CoreSyn
43 import CoreFVs
44 import CoreUtils
45 import Literal ( Literal(MachStr) )
46 import qualified Data.ByteString as BS
47 import OccurAnal( occurAnalyseExpr, occurAnalysePgm )
48
49 import qualified Type
50 import qualified Coercion
51
52 -- We are defining local versions
53 import Type hiding ( substTy, extendTvSubst, extendTvSubstList
54 , isInScope, substTyVarBndr, cloneTyVarBndr )
55 import Coercion hiding ( substTy, substCo, extendTvSubst, substTyVarBndr, substCoVarBndr )
56
57 import TyCon ( tyConArity )
58 import DataCon
59 import PrelNames ( eqBoxDataConKey, coercibleDataConKey, unpackCStringIdKey
60 , unpackCStringUtf8IdKey )
61 import OptCoercion ( optCoercion )
62 import PprCore ( pprCoreBindings, pprRules )
63 import Module ( Module )
64 import VarSet
65 import VarEnv
66 import Id
67 import Name ( Name )
68 import Var
69 import IdInfo
70 import Unique
71 import UniqSupply
72 import Maybes
73 import ErrUtils
74 import DynFlags
75 import BasicTypes ( isAlwaysActive )
76 import Util
77 import Pair
78 import Outputable
79 import PprCore () -- Instances
80 import FastString
81
82 import Data.List
83
84 import TysWiredIn
85
86 {-
87 ************************************************************************
88 * *
89 \subsection{Substitutions}
90 * *
91 ************************************************************************
92 -}
93
94 -- | A substitution environment, containing both 'Id' and 'TyVar' substitutions.
95 --
96 -- Some invariants apply to how you use the substitution:
97 --
98 -- 1. #in_scope_invariant# The in-scope set contains at least those 'Id's and 'TyVar's that will be in scope /after/
99 -- applying the substitution to a term. Precisely, the in-scope set must be a superset of the free vars of the
100 -- substitution range that might possibly clash with locally-bound variables in the thing being substituted in.
101 --
102 -- 2. #apply_once# You may apply the substitution only /once/
103 --
104 -- There are various ways of setting up the in-scope set such that the first of these invariants hold:
105 --
106 -- * Arrange that the in-scope set really is all the things in scope
107 --
108 -- * Arrange that it's the free vars of the range of the substitution
109 --
110 -- * Make it empty, if you know that all the free vars of the substitution are fresh, and hence can't possibly clash
111 data Subst
112 = Subst InScopeSet -- Variables in in scope (both Ids and TyVars) /after/
113 -- applying the substitution
114 IdSubstEnv -- Substitution for Ids
115 TvSubstEnv -- Substitution from TyVars to Types
116 CvSubstEnv -- Substitution from CoVars to Coercions
117
118 -- INVARIANT 1: See #in_scope_invariant#
119 -- This is what lets us deal with name capture properly
120 -- It's a hard invariant to check...
121 --
122 -- INVARIANT 2: The substitution is apply-once; see Note [Apply once] with
123 -- Types.TvSubstEnv
124 --
125 -- INVARIANT 3: See Note [Extending the Subst]
126
127 {-
128 Note [Extending the Subst]
129 ~~~~~~~~~~~~~~~~~~~~~~~~~~
130 For a core Subst, which binds Ids as well, we make a different choice for Ids
131 than we do for TyVars.
132
133 For TyVars, see Note [Extending the TvSubst] with Type.TvSubstEnv
134
135 For Ids, we have a different invariant
136 The IdSubstEnv is extended *only* when the Unique on an Id changes
137 Otherwise, we just extend the InScopeSet
138
139 In consequence:
140
141 * If the TvSubstEnv and IdSubstEnv are both empty, substExpr would be a
142 no-op, so substExprSC ("short cut") does nothing.
143
144 However, substExpr still goes ahead and substitutes. Reason: we may
145 want to replace existing Ids with new ones from the in-scope set, to
146 avoid space leaks.
147
148 * In substIdBndr, we extend the IdSubstEnv only when the unique changes
149
150 * If the CvSubstEnv, TvSubstEnv and IdSubstEnv are all empty,
151 substExpr does nothing (Note that the above rule for substIdBndr
152 maintains this property. If the incoming envts are both empty, then
153 substituting the type and IdInfo can't change anything.)
154
155 * In lookupIdSubst, we *must* look up the Id in the in-scope set, because
156 it may contain non-trivial changes. Example:
157 (/\a. \x:a. ...x...) Int
158 We extend the TvSubstEnv with [a |-> Int]; but x's unique does not change
159 so we only extend the in-scope set. Then we must look up in the in-scope
160 set when we find the occurrence of x.
161
162 * The requirement to look up the Id in the in-scope set means that we
163 must NOT take no-op short cut when the IdSubst is empty.
164 We must still look up every Id in the in-scope set.
165
166 * (However, we don't need to do so for expressions found in the IdSubst
167 itself, whose range is assumed to be correct wrt the in-scope set.)
168
169 Why do we make a different choice for the IdSubstEnv than the
170 TvSubstEnv and CvSubstEnv?
171
172 * For Ids, we change the IdInfo all the time (e.g. deleting the
173 unfolding), and adding it back later, so using the TyVar convention
174 would entail extending the substitution almost all the time
175
176 * The simplifier wants to look up in the in-scope set anyway, in case it
177 can see a better unfolding from an enclosing case expression
178
179 * For TyVars, only coercion variables can possibly change, and they are
180 easy to spot
181 -}
182
183 -- | An environment for substituting for 'Id's
184 type IdSubstEnv = IdEnv CoreExpr
185
186 ----------------------------
187 isEmptySubst :: Subst -> Bool
188 isEmptySubst (Subst _ id_env tv_env cv_env)
189 = isEmptyVarEnv id_env && isEmptyVarEnv tv_env && isEmptyVarEnv cv_env
190
191 emptySubst :: Subst
192 emptySubst = Subst emptyInScopeSet emptyVarEnv emptyVarEnv emptyVarEnv
193
194 mkEmptySubst :: InScopeSet -> Subst
195 mkEmptySubst in_scope = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
196
197 mkSubst :: InScopeSet -> TvSubstEnv -> CvSubstEnv -> IdSubstEnv -> Subst
198 mkSubst in_scope tvs cvs ids = Subst in_scope ids tvs cvs
199
200 -- | Find the in-scope set: see "CoreSubst#in_scope_invariant"
201 substInScope :: Subst -> InScopeSet
202 substInScope (Subst in_scope _ _ _) = in_scope
203
204 -- | Remove all substitutions for 'Id's and 'Var's that might have been built up
205 -- while preserving the in-scope set
206 zapSubstEnv :: Subst -> Subst
207 zapSubstEnv (Subst in_scope _ _ _) = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
208
209 -- | Add a substitution for an 'Id' to the 'Subst': you must ensure that the in-scope set is
210 -- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
211 extendIdSubst :: Subst -> Id -> CoreExpr -> Subst
212 -- ToDo: add an ASSERT that fvs(subst-result) is already in the in-scope set
213 extendIdSubst (Subst in_scope ids tvs cvs) v r = Subst in_scope (extendVarEnv ids v r) tvs cvs
214
215 -- | Adds multiple 'Id' substitutions to the 'Subst': see also 'extendIdSubst'
216 extendIdSubstList :: Subst -> [(Id, CoreExpr)] -> Subst
217 extendIdSubstList (Subst in_scope ids tvs cvs) prs = Subst in_scope (extendVarEnvList ids prs) tvs cvs
218
219 -- | Add a substitution for a 'TyVar' to the 'Subst': you must ensure that the in-scope set is
220 -- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
221 extendTvSubst :: Subst -> TyVar -> Type -> Subst
222 extendTvSubst (Subst in_scope ids tvs cvs) v r = Subst in_scope ids (extendVarEnv tvs v r) cvs
223
224 -- | Adds multiple 'TyVar' substitutions to the 'Subst': see also 'extendTvSubst'
225 extendTvSubstList :: Subst -> [(TyVar,Type)] -> Subst
226 extendTvSubstList (Subst in_scope ids tvs cvs) prs = Subst in_scope ids (extendVarEnvList tvs prs) cvs
227
228 -- | Add a substitution from a 'CoVar' to a 'Coercion' to the 'Subst': you must ensure that the in-scope set is
229 -- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
230 extendCvSubst :: Subst -> CoVar -> Coercion -> Subst
231 extendCvSubst (Subst in_scope ids tvs cvs) v r = Subst in_scope ids tvs (extendVarEnv cvs v r)
232
233 -- | Adds multiple 'CoVar' -> 'Coercion' substitutions to the
234 -- 'Subst': see also 'extendCvSubst'
235 extendCvSubstList :: Subst -> [(CoVar,Coercion)] -> Subst
236 extendCvSubstList (Subst in_scope ids tvs cvs) prs = Subst in_scope ids tvs (extendVarEnvList cvs prs)
237
238 -- | Add a substitution appropriate to the thing being substituted
239 -- (whether an expression, type, or coercion). See also
240 -- 'extendIdSubst', 'extendTvSubst', and 'extendCvSubst'.
241 extendSubst :: Subst -> Var -> CoreArg -> Subst
242 extendSubst subst var arg
243 = case arg of
244 Type ty -> ASSERT( isTyVar var ) extendTvSubst subst var ty
245 Coercion co -> ASSERT( isCoVar var ) extendCvSubst subst var co
246 _ -> ASSERT( isId var ) extendIdSubst subst var arg
247
248 extendSubstWithVar :: Subst -> Var -> Var -> Subst
249 extendSubstWithVar subst v1 v2
250 | isTyVar v1 = ASSERT( isTyVar v2 ) extendTvSubst subst v1 (mkTyVarTy v2)
251 | isCoVar v1 = ASSERT( isCoVar v2 ) extendCvSubst subst v1 (mkCoVarCo v2)
252 | otherwise = ASSERT( isId v2 ) extendIdSubst subst v1 (Var v2)
253
254 -- | Add a substitution as appropriate to each of the terms being
255 -- substituted (whether expressions, types, or coercions). See also
256 -- 'extendSubst'.
257 extendSubstList :: Subst -> [(Var,CoreArg)] -> Subst
258 extendSubstList subst [] = subst
259 extendSubstList subst ((var,rhs):prs) = extendSubstList (extendSubst subst var rhs) prs
260
261 -- | Find the substitution for an 'Id' in the 'Subst'
262 lookupIdSubst :: SDoc -> Subst -> Id -> CoreExpr
263 lookupIdSubst doc (Subst in_scope ids _ _) v
264 | not (isLocalId v) = Var v
265 | Just e <- lookupVarEnv ids v = e
266 | Just v' <- lookupInScope in_scope v = Var v'
267 -- Vital! See Note [Extending the Subst]
268 | otherwise = WARN( True, ptext (sLit "CoreSubst.lookupIdSubst") <+> doc <+> ppr v
269 $$ ppr in_scope)
270 Var v
271
272 -- | Find the substitution for a 'TyVar' in the 'Subst'
273 lookupTvSubst :: Subst -> TyVar -> Type
274 lookupTvSubst (Subst _ _ tvs _) v = ASSERT( isTyVar v) lookupVarEnv tvs v `orElse` Type.mkTyVarTy v
275
276 -- | Find the coercion substitution for a 'CoVar' in the 'Subst'
277 lookupCvSubst :: Subst -> CoVar -> Coercion
278 lookupCvSubst (Subst _ _ _ cvs) v = ASSERT( isCoVar v ) lookupVarEnv cvs v `orElse` mkCoVarCo v
279
280 delBndr :: Subst -> Var -> Subst
281 delBndr (Subst in_scope ids tvs cvs) v
282 | isCoVar v = Subst in_scope ids tvs (delVarEnv cvs v)
283 | isTyVar v = Subst in_scope ids (delVarEnv tvs v) cvs
284 | otherwise = Subst in_scope (delVarEnv ids v) tvs cvs
285
286 delBndrs :: Subst -> [Var] -> Subst
287 delBndrs (Subst in_scope ids tvs cvs) vs
288 = Subst in_scope (delVarEnvList ids vs) (delVarEnvList tvs vs) (delVarEnvList cvs vs)
289 -- Easiest thing is just delete all from all!
290
291 -- | Simultaneously substitute for a bunch of variables
292 -- No left-right shadowing
293 -- ie the substitution for (\x \y. e) a1 a2
294 -- so neither x nor y scope over a1 a2
295 mkOpenSubst :: InScopeSet -> [(Var,CoreArg)] -> Subst
296 mkOpenSubst in_scope pairs = Subst in_scope
297 (mkVarEnv [(id,e) | (id, e) <- pairs, isId id])
298 (mkVarEnv [(tv,ty) | (tv, Type ty) <- pairs])
299 (mkVarEnv [(v,co) | (v, Coercion co) <- pairs])
300
301 ------------------------------
302 isInScope :: Var -> Subst -> Bool
303 isInScope v (Subst in_scope _ _ _) = v `elemInScopeSet` in_scope
304
305 -- | Add the 'Var' to the in-scope set, but do not remove
306 -- any existing substitutions for it
307 addInScopeSet :: Subst -> VarSet -> Subst
308 addInScopeSet (Subst in_scope ids tvs cvs) vs
309 = Subst (in_scope `extendInScopeSetSet` vs) ids tvs cvs
310
311 -- | Add the 'Var' to the in-scope set: as a side effect,
312 -- and remove any existing substitutions for it
313 extendInScope :: Subst -> Var -> Subst
314 extendInScope (Subst in_scope ids tvs cvs) v
315 = Subst (in_scope `extendInScopeSet` v)
316 (ids `delVarEnv` v) (tvs `delVarEnv` v) (cvs `delVarEnv` v)
317
318 -- | Add the 'Var's to the in-scope set: see also 'extendInScope'
319 extendInScopeList :: Subst -> [Var] -> Subst
320 extendInScopeList (Subst in_scope ids tvs cvs) vs
321 = Subst (in_scope `extendInScopeSetList` vs)
322 (ids `delVarEnvList` vs) (tvs `delVarEnvList` vs) (cvs `delVarEnvList` vs)
323
324 -- | Optimized version of 'extendInScopeList' that can be used if you are certain
325 -- all the things being added are 'Id's and hence none are 'TyVar's or 'CoVar's
326 extendInScopeIds :: Subst -> [Id] -> Subst
327 extendInScopeIds (Subst in_scope ids tvs cvs) vs
328 = Subst (in_scope `extendInScopeSetList` vs)
329 (ids `delVarEnvList` vs) tvs cvs
330
331 setInScope :: Subst -> InScopeSet -> Subst
332 setInScope (Subst _ ids tvs cvs) in_scope = Subst in_scope ids tvs cvs
333
334 -- Pretty printing, for debugging only
335
336 instance Outputable Subst where
337 ppr (Subst in_scope ids tvs cvs)
338 = ptext (sLit "<InScope =") <+> braces (fsep (map ppr (varEnvElts (getInScopeVars in_scope))))
339 $$ ptext (sLit " IdSubst =") <+> ppr ids
340 $$ ptext (sLit " TvSubst =") <+> ppr tvs
341 $$ ptext (sLit " CvSubst =") <+> ppr cvs
342 <> char '>'
343
344 {-
345 ************************************************************************
346 * *
347 Substituting expressions
348 * *
349 ************************************************************************
350 -}
351
352 -- | Apply a substitution to an entire 'CoreExpr'. Remember, you may only
353 -- apply the substitution /once/: see "CoreSubst#apply_once"
354 --
355 -- Do *not* attempt to short-cut in the case of an empty substitution!
356 -- See Note [Extending the Subst]
357 substExprSC :: SDoc -> Subst -> CoreExpr -> CoreExpr
358 substExprSC _doc subst orig_expr
359 | isEmptySubst subst = orig_expr
360 | otherwise = -- pprTrace "enter subst-expr" (doc $$ ppr orig_expr) $
361 subst_expr subst orig_expr
362
363 substExpr :: SDoc -> Subst -> CoreExpr -> CoreExpr
364 substExpr _doc subst orig_expr = subst_expr subst orig_expr
365
366 subst_expr :: Subst -> CoreExpr -> CoreExpr
367 subst_expr subst expr
368 = go expr
369 where
370 go (Var v) = lookupIdSubst (text "subst_expr") subst v
371 go (Type ty) = Type (substTy subst ty)
372 go (Coercion co) = Coercion (substCo subst co)
373 go (Lit lit) = Lit lit
374 go (App fun arg) = App (go fun) (go arg)
375 go (Tick tickish e) = mkTick (substTickish subst tickish) (go e)
376 go (Cast e co) = Cast (go e) (substCo subst co)
377 -- Do not optimise even identity coercions
378 -- Reason: substitution applies to the LHS of RULES, and
379 -- if you "optimise" an identity coercion, you may
380 -- lose a binder. We optimise the LHS of rules at
381 -- construction time
382
383 go (Lam bndr body) = Lam bndr' (subst_expr subst' body)
384 where
385 (subst', bndr') = substBndr subst bndr
386
387 go (Let bind body) = Let bind' (subst_expr subst' body)
388 where
389 (subst', bind') = substBind subst bind
390
391 go (Case scrut bndr ty alts) = Case (go scrut) bndr' (substTy subst ty) (map (go_alt subst') alts)
392 where
393 (subst', bndr') = substBndr subst bndr
394
395 go_alt subst (con, bndrs, rhs) = (con, bndrs', subst_expr subst' rhs)
396 where
397 (subst', bndrs') = substBndrs subst bndrs
398
399 -- | Apply a substitution to an entire 'CoreBind', additionally returning an updated 'Subst'
400 -- that should be used by subsequent substitutions.
401 substBind, substBindSC :: Subst -> CoreBind -> (Subst, CoreBind)
402
403 substBindSC subst bind -- Short-cut if the substitution is empty
404 | not (isEmptySubst subst)
405 = substBind subst bind
406 | otherwise
407 = case bind of
408 NonRec bndr rhs -> (subst', NonRec bndr' rhs)
409 where
410 (subst', bndr') = substBndr subst bndr
411 Rec pairs -> (subst', Rec (bndrs' `zip` rhss'))
412 where
413 (bndrs, rhss) = unzip pairs
414 (subst', bndrs') = substRecBndrs subst bndrs
415 rhss' | isEmptySubst subst' = rhss
416 | otherwise = map (subst_expr subst') rhss
417
418 substBind subst (NonRec bndr rhs) = (subst', NonRec bndr' (subst_expr subst rhs))
419 where
420 (subst', bndr') = substBndr subst bndr
421
422 substBind subst (Rec pairs) = (subst', Rec (bndrs' `zip` rhss'))
423 where
424 (bndrs, rhss) = unzip pairs
425 (subst', bndrs') = substRecBndrs subst bndrs
426 rhss' = map (subst_expr subst') rhss
427
428 -- | De-shadowing the program is sometimes a useful pre-pass. It can be done simply
429 -- by running over the bindings with an empty substitution, because substitution
430 -- returns a result that has no-shadowing guaranteed.
431 --
432 -- (Actually, within a single /type/ there might still be shadowing, because
433 -- 'substTy' is a no-op for the empty substitution, but that's probably OK.)
434 --
435 -- [Aug 09] This function is not used in GHC at the moment, but seems so
436 -- short and simple that I'm going to leave it here
437 deShadowBinds :: CoreProgram -> CoreProgram
438 deShadowBinds binds = snd (mapAccumL substBind emptySubst binds)
439
440 {-
441 ************************************************************************
442 * *
443 Substituting binders
444 * *
445 ************************************************************************
446
447 Remember that substBndr and friends are used when doing expression
448 substitution only. Their only business is substitution, so they
449 preserve all IdInfo (suitably substituted). For example, we *want* to
450 preserve occ info in rules.
451 -}
452
453 -- | Substitutes a 'Var' for another one according to the 'Subst' given, returning
454 -- the result and an updated 'Subst' that should be used by subsequent substitutions.
455 -- 'IdInfo' is preserved by this process, although it is substituted into appropriately.
456 substBndr :: Subst -> Var -> (Subst, Var)
457 substBndr subst bndr
458 | isTyVar bndr = substTyVarBndr subst bndr
459 | isCoVar bndr = substCoVarBndr subst bndr
460 | otherwise = substIdBndr (text "var-bndr") subst subst bndr
461
462 -- | Applies 'substBndr' to a number of 'Var's, accumulating a new 'Subst' left-to-right
463 substBndrs :: Subst -> [Var] -> (Subst, [Var])
464 substBndrs subst bndrs = mapAccumL substBndr subst bndrs
465
466 -- | Substitute in a mutually recursive group of 'Id's
467 substRecBndrs :: Subst -> [Id] -> (Subst, [Id])
468 substRecBndrs subst bndrs
469 = (new_subst, new_bndrs)
470 where -- Here's the reason we need to pass rec_subst to subst_id
471 (new_subst, new_bndrs) = mapAccumL (substIdBndr (text "rec-bndr") new_subst) subst bndrs
472
473 substIdBndr :: SDoc
474 -> Subst -- ^ Substitution to use for the IdInfo
475 -> Subst -> Id -- ^ Substitution and Id to transform
476 -> (Subst, Id) -- ^ Transformed pair
477 -- NB: unfolding may be zapped
478
479 substIdBndr _doc rec_subst subst@(Subst in_scope env tvs cvs) old_id
480 = -- pprTrace "substIdBndr" (doc $$ ppr old_id $$ ppr in_scope) $
481 (Subst (in_scope `extendInScopeSet` new_id) new_env tvs cvs, new_id)
482 where
483 id1 = uniqAway in_scope old_id -- id1 is cloned if necessary
484 id2 | no_type_change = id1
485 | otherwise = setIdType id1 (substTy subst old_ty)
486
487 old_ty = idType old_id
488 no_type_change = isEmptyVarEnv tvs ||
489 isEmptyVarSet (Type.tyVarsOfType old_ty)
490
491 -- new_id has the right IdInfo
492 -- The lazy-set is because we're in a loop here, with
493 -- rec_subst, when dealing with a mutually-recursive group
494 new_id = maybeModifyIdInfo mb_new_info id2
495 mb_new_info = substIdInfo rec_subst id2 (idInfo id2)
496 -- NB: unfolding info may be zapped
497
498 -- Extend the substitution if the unique has changed
499 -- See the notes with substTyVarBndr for the delVarEnv
500 new_env | no_change = delVarEnv env old_id
501 | otherwise = extendVarEnv env old_id (Var new_id)
502
503 no_change = id1 == old_id
504 -- See Note [Extending the Subst]
505 -- it's /not/ necessary to check mb_new_info and no_type_change
506
507 {-
508 Now a variant that unconditionally allocates a new unique.
509 It also unconditionally zaps the OccInfo.
510 -}
511
512 -- | Very similar to 'substBndr', but it always allocates a new 'Unique' for
513 -- each variable in its output. It substitutes the IdInfo though.
514 cloneIdBndr :: Subst -> UniqSupply -> Id -> (Subst, Id)
515 cloneIdBndr subst us old_id
516 = clone_id subst subst (old_id, uniqFromSupply us)
517
518 -- | Applies 'cloneIdBndr' to a number of 'Id's, accumulating a final
519 -- substitution from left to right
520 cloneIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
521 cloneIdBndrs subst us ids
522 = mapAccumL (clone_id subst) subst (ids `zip` uniqsFromSupply us)
523
524 cloneBndrs :: Subst -> UniqSupply -> [Var] -> (Subst, [Var])
525 -- Works for all kinds of variables (typically case binders)
526 -- not just Ids
527 cloneBndrs subst us vs
528 = mapAccumL (\subst (v, u) -> cloneBndr subst u v) subst (vs `zip` uniqsFromSupply us)
529
530 cloneBndr :: Subst -> Unique -> Var -> (Subst, Var)
531 cloneBndr subst uniq v
532 | isTyVar v = cloneTyVarBndr subst v uniq
533 | otherwise = clone_id subst subst (v,uniq) -- Works for coercion variables too
534
535 -- | Clone a mutually recursive group of 'Id's
536 cloneRecIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
537 cloneRecIdBndrs subst us ids
538 = (subst', ids')
539 where
540 (subst', ids') = mapAccumL (clone_id subst') subst
541 (ids `zip` uniqsFromSupply us)
542
543 -- Just like substIdBndr, except that it always makes a new unique
544 -- It is given the unique to use
545 clone_id :: Subst -- Substitution for the IdInfo
546 -> Subst -> (Id, Unique) -- Substitution and Id to transform
547 -> (Subst, Id) -- Transformed pair
548
549 clone_id rec_subst subst@(Subst in_scope idvs tvs cvs) (old_id, uniq)
550 = (Subst (in_scope `extendInScopeSet` new_id) new_idvs tvs new_cvs, new_id)
551 where
552 id1 = setVarUnique old_id uniq
553 id2 = substIdType subst id1
554 new_id = maybeModifyIdInfo (substIdInfo rec_subst id2 (idInfo old_id)) id2
555 (new_idvs, new_cvs) | isCoVar old_id = (idvs, extendVarEnv cvs old_id (mkCoVarCo new_id))
556 | otherwise = (extendVarEnv idvs old_id (Var new_id), cvs)
557
558 {-
559 ************************************************************************
560 * *
561 Types and Coercions
562 * *
563 ************************************************************************
564
565 For types and coercions we just call the corresponding functions in
566 Type and Coercion, but we have to repackage the substitution, from a
567 Subst to a TvSubst.
568 -}
569
570 substTyVarBndr :: Subst -> TyVar -> (Subst, TyVar)
571 substTyVarBndr (Subst in_scope id_env tv_env cv_env) tv
572 = case Type.substTyVarBndr (TvSubst in_scope tv_env) tv of
573 (TvSubst in_scope' tv_env', tv')
574 -> (Subst in_scope' id_env tv_env' cv_env, tv')
575
576 cloneTyVarBndr :: Subst -> TyVar -> Unique -> (Subst, TyVar)
577 cloneTyVarBndr (Subst in_scope id_env tv_env cv_env) tv uniq
578 = case Type.cloneTyVarBndr (TvSubst in_scope tv_env) tv uniq of
579 (TvSubst in_scope' tv_env', tv')
580 -> (Subst in_scope' id_env tv_env' cv_env, tv')
581
582 substCoVarBndr :: Subst -> TyVar -> (Subst, TyVar)
583 substCoVarBndr (Subst in_scope id_env tv_env cv_env) cv
584 = case Coercion.substCoVarBndr (CvSubst in_scope tv_env cv_env) cv of
585 (CvSubst in_scope' tv_env' cv_env', cv')
586 -> (Subst in_scope' id_env tv_env' cv_env', cv')
587
588 -- | See 'Type.substTy'
589 substTy :: Subst -> Type -> Type
590 substTy subst ty = Type.substTy (getTvSubst subst) ty
591
592 getTvSubst :: Subst -> TvSubst
593 getTvSubst (Subst in_scope _ tenv _) = TvSubst in_scope tenv
594
595 getCvSubst :: Subst -> CvSubst
596 getCvSubst (Subst in_scope _ tenv cenv) = CvSubst in_scope tenv cenv
597
598 -- | See 'Coercion.substCo'
599 substCo :: Subst -> Coercion -> Coercion
600 substCo subst co = Coercion.substCo (getCvSubst subst) co
601
602 {-
603 ************************************************************************
604 * *
605 \section{IdInfo substitution}
606 * *
607 ************************************************************************
608 -}
609
610 substIdType :: Subst -> Id -> Id
611 substIdType subst@(Subst _ _ tv_env cv_env) id
612 | (isEmptyVarEnv tv_env && isEmptyVarEnv cv_env) || isEmptyVarSet (Type.tyVarsOfType old_ty) = id
613 | otherwise = setIdType id (substTy subst old_ty)
614 -- The tyVarsOfType is cheaper than it looks
615 -- because we cache the free tyvars of the type
616 -- in a Note in the id's type itself
617 where
618 old_ty = idType id
619
620 ------------------
621 -- | Substitute into some 'IdInfo' with regard to the supplied new 'Id'.
622 substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo
623 substIdInfo subst new_id info
624 | nothing_to_do = Nothing
625 | otherwise = Just (info `setSpecInfo` substSpec subst new_id old_rules
626 `setUnfoldingInfo` substUnfolding subst old_unf)
627 where
628 old_rules = specInfo info
629 old_unf = unfoldingInfo info
630 nothing_to_do = isEmptySpecInfo old_rules && isClosedUnfolding old_unf
631
632
633 ------------------
634 -- | Substitutes for the 'Id's within an unfolding
635 substUnfolding, substUnfoldingSC :: Subst -> Unfolding -> Unfolding
636 -- Seq'ing on the returned Unfolding is enough to cause
637 -- all the substitutions to happen completely
638
639 substUnfoldingSC subst unf -- Short-cut version
640 | isEmptySubst subst = unf
641 | otherwise = substUnfolding subst unf
642
643 substUnfolding subst df@(DFunUnfolding { df_bndrs = bndrs, df_args = args })
644 = df { df_bndrs = bndrs', df_args = args' }
645 where
646 (subst',bndrs') = substBndrs subst bndrs
647 args' = map (substExpr (text "subst-unf:dfun") subst') args
648
649 substUnfolding subst unf@(CoreUnfolding { uf_tmpl = tmpl, uf_src = src })
650 -- Retain an InlineRule!
651 | not (isStableSource src) -- Zap an unstable unfolding, to save substitution work
652 = NoUnfolding
653 | otherwise -- But keep a stable one!
654 = seqExpr new_tmpl `seq`
655 unf { uf_tmpl = new_tmpl }
656 where
657 new_tmpl = substExpr (text "subst-unf") subst tmpl
658
659 substUnfolding _ unf = unf -- NoUnfolding, OtherCon
660
661 ------------------
662 substIdOcc :: Subst -> Id -> Id
663 -- These Ids should not be substituted to non-Ids
664 substIdOcc subst v = case lookupIdSubst (text "substIdOcc") subst v of
665 Var v' -> v'
666 other -> pprPanic "substIdOcc" (vcat [ppr v <+> ppr other, ppr subst])
667
668 ------------------
669 -- | Substitutes for the 'Id's within the 'WorkerInfo' given the new function 'Id'
670 substSpec :: Subst -> Id -> SpecInfo -> SpecInfo
671 substSpec subst new_id (SpecInfo rules rhs_fvs)
672 = seqSpecInfo new_spec `seq` new_spec
673 where
674 subst_ru_fn = const (idName new_id)
675 new_spec = SpecInfo (map (substRule subst subst_ru_fn) rules)
676 (substVarSet subst rhs_fvs)
677
678 ------------------
679 substRulesForImportedIds :: Subst -> [CoreRule] -> [CoreRule]
680 substRulesForImportedIds subst rules
681 = map (substRule subst not_needed) rules
682 where
683 not_needed name = pprPanic "substRulesForImportedIds" (ppr name)
684
685 ------------------
686 substRule :: Subst -> (Name -> Name) -> CoreRule -> CoreRule
687
688 -- The subst_ru_fn argument is applied to substitute the ru_fn field
689 -- of the rule:
690 -- - Rules for *imported* Ids never change ru_fn
691 -- - Rules for *local* Ids are in the IdInfo for that Id,
692 -- and the ru_fn field is simply replaced by the new name
693 -- of the Id
694 substRule _ _ rule@(BuiltinRule {}) = rule
695 substRule subst subst_ru_fn rule@(Rule { ru_bndrs = bndrs, ru_args = args
696 , ru_fn = fn_name, ru_rhs = rhs
697 , ru_local = is_local })
698 = rule { ru_bndrs = bndrs',
699 ru_fn = if is_local
700 then subst_ru_fn fn_name
701 else fn_name,
702 ru_args = map (substExpr (text "subst-rule" <+> ppr fn_name) subst') args,
703 ru_rhs = simpleOptExprWith subst' rhs }
704 -- Do simple optimisation on RHS, in case substitution lets
705 -- you improve it. The real simplifier never gets to look at it.
706 where
707 (subst', bndrs') = substBndrs subst bndrs
708
709 ------------------
710 substVects :: Subst -> [CoreVect] -> [CoreVect]
711 substVects subst = map (substVect subst)
712
713 ------------------
714 substVect :: Subst -> CoreVect -> CoreVect
715 substVect subst (Vect v rhs) = Vect v (simpleOptExprWith subst rhs)
716 substVect _subst vd@(NoVect _) = vd
717 substVect _subst vd@(VectType _ _ _) = vd
718 substVect _subst vd@(VectClass _) = vd
719 substVect _subst vd@(VectInst _) = vd
720
721 ------------------
722 substVarSet :: Subst -> VarSet -> VarSet
723 substVarSet subst fvs
724 = foldVarSet (unionVarSet . subst_fv subst) emptyVarSet fvs
725 where
726 subst_fv subst fv
727 | isId fv = exprFreeVars (lookupIdSubst (text "substVarSet") subst fv)
728 | otherwise = Type.tyVarsOfType (lookupTvSubst subst fv)
729
730 ------------------
731 substTickish :: Subst -> Tickish Id -> Tickish Id
732 substTickish subst (Breakpoint n ids) = Breakpoint n (map do_one ids)
733 where do_one = getIdFromTrivialExpr . lookupIdSubst (text "subst_tickish") subst
734 substTickish _subst other = other
735
736 {- Note [substTickish]
737
738 A Breakpoint contains a list of Ids. What happens if we ever want to
739 substitute an expression for one of these Ids?
740
741 First, we ensure that we only ever substitute trivial expressions for
742 these Ids, by marking them as NoOccInfo in the occurrence analyser.
743 Then, when substituting for the Id, we unwrap any type applications
744 and abstractions to get back to an Id, with getIdFromTrivialExpr.
745
746 Second, we have to ensure that we never try to substitute a literal
747 for an Id in a breakpoint. We ensure this by never storing an Id with
748 an unlifted type in a Breakpoint - see Coverage.mkTickish.
749 Breakpoints can't handle free variables with unlifted types anyway.
750 -}
751
752 {-
753 Note [Worker inlining]
754 ~~~~~~~~~~~~~~~~~~~~~~
755 A worker can get sustituted away entirely.
756 - it might be trivial
757 - it might simply be very small
758 We do not treat an InlWrapper as an 'occurrence' in the occurrence
759 analyser, so it's possible that the worker is not even in scope any more.
760
761 In all all these cases we simply drop the special case, returning to
762 InlVanilla. The WARN is just so I can see if it happens a lot.
763
764
765 ************************************************************************
766 * *
767 The Very Simple Optimiser
768 * *
769 ************************************************************************
770
771 Note [Optimise coercion boxes aggressively]
772 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
773
774 The simple expression optimiser needs to deal with Eq# boxes as follows:
775 1. If the result of optimising the RHS of a non-recursive binding is an
776 Eq# box, that box is substituted rather than turned into a let, just as
777 if it were trivial.
778 let eqv = Eq# co in e ==> e[Eq# co/eqv]
779
780 2. If the result of optimising a case scrutinee is a Eq# box and the case
781 deconstructs it in a trivial way, we evaluate the case then and there.
782 case Eq# co of Eq# cov -> e ==> e[co/cov]
783
784 We do this for two reasons:
785
786 1. Bindings/case scrutinisation of this form is often created by the
787 evidence-binding mechanism and we need them to be inlined to be able
788 desugar RULE LHSes that involve equalities (see e.g. T2291)
789
790 2. The test T4356 fails Lint because it creates a coercion between types
791 of kind (* -> * -> *) and (?? -> ? -> *), which differ. If we do this
792 inlining aggressively we can collapse away the intermediate coercion between
793 these two types and hence pass Lint again. (This is a sort of a hack.)
794
795 In fact, our implementation uses slightly liberalised versions of the second rule
796 rule so that the optimisations are a bit more generally applicable. Precisely:
797 2a. We reduce any situation where we can spot a case-of-known-constructor
798
799 As a result, the only time we should get residual coercion boxes in the code is
800 when the type checker generates something like:
801
802 \eqv -> let eqv' = Eq# (case eqv of Eq# cov -> ... cov ...)
803
804 However, the case of lambda-bound equality evidence is fairly rare, so these two
805 rules should suffice for solving the rule LHS problem for now.
806
807 Annoyingly, we cannot use this modified rule 1a instead of 1:
808
809 1a. If we come across a let-bound constructor application with trivial arguments,
810 add an appropriate unfolding to the let binder. We spot constructor applications
811 by using exprIsConApp_maybe, so this would actually let rule 2a reduce more.
812
813 The reason is that we REALLY NEED coercion boxes to be substituted away. With rule 1a
814 we wouldn't simplify this expression at all:
815
816 let eqv = Eq# co
817 in foo eqv (bar eqv)
818
819 The rule LHS desugarer can't deal with Let at all, so we need to push that box into
820 the use sites.
821 -}
822
823 simpleOptExpr :: CoreExpr -> CoreExpr
824 -- Do simple optimisation on an expression
825 -- The optimisation is very straightforward: just
826 -- inline non-recursive bindings that are used only once,
827 -- or where the RHS is trivial
828 --
829 -- We also inline bindings that bind a Eq# box: see
830 -- See Note [Optimise coercion boxes aggressively].
831 --
832 -- The result is NOT guaranteed occurrence-analysed, because
833 -- in (let x = y in ....) we substitute for x; so y's occ-info
834 -- may change radically
835
836 simpleOptExpr expr
837 = -- pprTrace "simpleOptExpr" (ppr init_subst $$ ppr expr)
838 simpleOptExprWith init_subst expr
839 where
840 init_subst = mkEmptySubst (mkInScopeSet (exprFreeVars expr))
841 -- It's potentially important to make a proper in-scope set
842 -- Consider let x = ..y.. in \y. ...x...
843 -- Then we should remember to clone y before substituting
844 -- for x. It's very unlikely to occur, because we probably
845 -- won't *be* substituting for x if it occurs inside a
846 -- lambda.
847 --
848 -- It's a bit painful to call exprFreeVars, because it makes
849 -- three passes instead of two (occ-anal, and go)
850
851 simpleOptExprWith :: Subst -> InExpr -> OutExpr
852 simpleOptExprWith subst expr = simple_opt_expr subst (occurAnalyseExpr expr)
853
854 ----------------------
855 simpleOptPgm :: DynFlags -> Module
856 -> CoreProgram -> [CoreRule] -> [CoreVect]
857 -> IO (CoreProgram, [CoreRule], [CoreVect])
858 simpleOptPgm dflags this_mod binds rules vects
859 = do { dumpIfSet_dyn dflags Opt_D_dump_occur_anal "Occurrence analysis"
860 (pprCoreBindings occ_anald_binds $$ pprRules rules );
861
862 ; return (reverse binds', substRulesForImportedIds subst' rules, substVects subst' vects) }
863 where
864 occ_anald_binds = occurAnalysePgm this_mod (\_ -> False) {- No rules active -}
865 rules vects emptyVarEnv binds
866 (subst', binds') = foldl do_one (emptySubst, []) occ_anald_binds
867
868 do_one (subst, binds') bind
869 = case simple_opt_bind subst bind of
870 (subst', Nothing) -> (subst', binds')
871 (subst', Just bind') -> (subst', bind':binds')
872
873 ----------------------
874 type InVar = Var
875 type OutVar = Var
876 type InId = Id
877 type OutId = Id
878 type InExpr = CoreExpr
879 type OutExpr = CoreExpr
880
881 -- In these functions the substitution maps InVar -> OutExpr
882
883 ----------------------
884 simple_opt_expr :: Subst -> InExpr -> OutExpr
885 simple_opt_expr subst expr
886 = go expr
887 where
888 in_scope_env = (substInScope subst, simpleUnfoldingFun)
889
890 go (Var v) = lookupIdSubst (text "simpleOptExpr") subst v
891 go (App e1 e2) = simple_app subst e1 [go e2]
892 go (Type ty) = Type (substTy subst ty)
893 go (Coercion co) = Coercion (optCoercion (getCvSubst subst) co)
894 go (Lit lit) = Lit lit
895 go (Tick tickish e) = mkTick (substTickish subst tickish) (go e)
896 go (Cast e co) | isReflCo co' = go e
897 | otherwise = Cast (go e) co'
898 where
899 co' = optCoercion (getCvSubst subst) co
900
901 go (Let bind body) = case simple_opt_bind subst bind of
902 (subst', Nothing) -> simple_opt_expr subst' body
903 (subst', Just bind) -> Let bind (simple_opt_expr subst' body)
904
905 go lam@(Lam {}) = go_lam [] subst lam
906 go (Case e b ty as)
907 -- See Note [Optimise coercion boxes aggressively]
908 | isDeadBinder b
909 , Just (con, _tys, es) <- exprIsConApp_maybe in_scope_env e'
910 , Just (altcon, bs, rhs) <- findAlt (DataAlt con) as
911 = case altcon of
912 DEFAULT -> go rhs
913 _ -> mkLets (catMaybes mb_binds) $ simple_opt_expr subst' rhs
914 where (subst', mb_binds) = mapAccumL simple_opt_out_bind subst
915 (zipEqual "simpleOptExpr" bs es)
916
917 | otherwise
918 = Case e' b' (substTy subst ty)
919 (map (go_alt subst') as)
920 where
921 e' = go e
922 (subst', b') = subst_opt_bndr subst b
923
924 ----------------------
925 go_alt subst (con, bndrs, rhs)
926 = (con, bndrs', simple_opt_expr subst' rhs)
927 where
928 (subst', bndrs') = subst_opt_bndrs subst bndrs
929
930 ----------------------
931 -- go_lam tries eta reduction
932 go_lam bs' subst (Lam b e)
933 = go_lam (b':bs') subst' e
934 where
935 (subst', b') = subst_opt_bndr subst b
936 go_lam bs' subst e
937 | Just etad_e <- tryEtaReduce bs e' = etad_e
938 | otherwise = mkLams bs e'
939 where
940 bs = reverse bs'
941 e' = simple_opt_expr subst e
942
943 ----------------------
944 -- simple_app collects arguments for beta reduction
945 simple_app :: Subst -> InExpr -> [OutExpr] -> CoreExpr
946 simple_app subst (App e1 e2) as
947 = simple_app subst e1 (simple_opt_expr subst e2 : as)
948 simple_app subst (Lam b e) (a:as)
949 = case maybe_substitute subst b a of
950 Just ext_subst -> simple_app ext_subst e as
951 Nothing -> Let (NonRec b2 a) (simple_app subst' e as)
952 where
953 (subst', b') = subst_opt_bndr subst b
954 b2 = add_info subst' b b'
955 simple_app subst (Var v) as
956 | isCompulsoryUnfolding (idUnfolding v)
957 , isAlwaysActive (idInlineActivation v)
958 -- See Note [Unfold compulsory unfoldings in LHSs]
959 = simple_app subst (unfoldingTemplate (idUnfolding v)) as
960 simple_app subst (Tick t e) as
961 -- Okay to do "(Tick t e) x ==> Tick t (e x)"?
962 | t `tickishScopesLike` SoftScope
963 = mkTick t $ simple_app subst e as
964 simple_app subst e as
965 = foldl App (simple_opt_expr subst e) as
966
967 ----------------------
968 simple_opt_bind,simple_opt_bind' :: Subst -> CoreBind -> (Subst, Maybe CoreBind)
969 simple_opt_bind s b -- Can add trace stuff here
970 = simple_opt_bind' s b
971
972 simple_opt_bind' subst (Rec prs)
973 = (subst'', res_bind)
974 where
975 res_bind = Just (Rec (reverse rev_prs'))
976 (subst', bndrs') = subst_opt_bndrs subst (map fst prs)
977 (subst'', rev_prs') = foldl do_pr (subst', []) (prs `zip` bndrs')
978 do_pr (subst, prs) ((b,r), b')
979 = case maybe_substitute subst b r2 of
980 Just subst' -> (subst', prs)
981 Nothing -> (subst, (b2,r2):prs)
982 where
983 b2 = add_info subst b b'
984 r2 = simple_opt_expr subst r
985
986 simple_opt_bind' subst (NonRec b r)
987 = simple_opt_out_bind subst (b, simple_opt_expr subst r)
988
989 ----------------------
990 simple_opt_out_bind :: Subst -> (InVar, OutExpr) -> (Subst, Maybe CoreBind)
991 simple_opt_out_bind subst (b, r')
992 | Just ext_subst <- maybe_substitute subst b r'
993 = (ext_subst, Nothing)
994 | otherwise
995 = (subst', Just (NonRec b2 r'))
996 where
997 (subst', b') = subst_opt_bndr subst b
998 b2 = add_info subst' b b'
999
1000 ----------------------
1001 maybe_substitute :: Subst -> InVar -> OutExpr -> Maybe Subst
1002 -- (maybe_substitute subst in_var out_rhs)
1003 -- either extends subst with (in_var -> out_rhs)
1004 -- or returns Nothing
1005 maybe_substitute subst b r
1006 | Type ty <- r -- let a::* = TYPE ty in <body>
1007 = ASSERT( isTyVar b )
1008 Just (extendTvSubst subst b ty)
1009
1010 | Coercion co <- r
1011 = ASSERT( isCoVar b )
1012 Just (extendCvSubst subst b co)
1013
1014 | isId b -- let x = e in <body>
1015 , not (isCoVar b) -- See Note [Do not inline CoVars unconditionally]
1016 -- in SimplUtils
1017 , safe_to_inline (idOccInfo b)
1018 , isAlwaysActive (idInlineActivation b) -- Note [Inline prag in simplOpt]
1019 , not (isStableUnfolding (idUnfolding b))
1020 , not (isExportedId b)
1021 , not (isUnLiftedType (idType b)) || exprOkForSpeculation r
1022 = Just (extendIdSubst subst b r)
1023
1024 | otherwise
1025 = Nothing
1026 where
1027 -- Unconditionally safe to inline
1028 safe_to_inline :: OccInfo -> Bool
1029 safe_to_inline (IAmALoopBreaker {}) = False
1030 safe_to_inline IAmDead = True
1031 safe_to_inline (OneOcc in_lam one_br _) = (not in_lam && one_br) || trivial
1032 safe_to_inline NoOccInfo = trivial
1033
1034 trivial | exprIsTrivial r = True
1035 | (Var fun, args) <- collectArgs r
1036 , Just dc <- isDataConWorkId_maybe fun
1037 , dc `hasKey` eqBoxDataConKey || dc `hasKey` coercibleDataConKey
1038 , all exprIsTrivial args = True -- See Note [Optimise coercion boxes aggressively]
1039 | otherwise = False
1040
1041 ----------------------
1042 subst_opt_bndr :: Subst -> InVar -> (Subst, OutVar)
1043 subst_opt_bndr subst bndr
1044 | isTyVar bndr = substTyVarBndr subst bndr
1045 | isCoVar bndr = substCoVarBndr subst bndr
1046 | otherwise = subst_opt_id_bndr subst bndr
1047
1048 subst_opt_id_bndr :: Subst -> InId -> (Subst, OutId)
1049 -- Nuke all fragile IdInfo, unfolding, and RULES;
1050 -- it gets added back later by add_info
1051 -- Rather like SimplEnv.substIdBndr
1052 --
1053 -- It's important to zap fragile OccInfo (which CoreSubst.substIdBndr
1054 -- carefully does not do) because simplOptExpr invalidates it
1055
1056 subst_opt_id_bndr subst@(Subst in_scope id_subst tv_subst cv_subst) old_id
1057 = (Subst new_in_scope new_id_subst tv_subst cv_subst, new_id)
1058 where
1059 id1 = uniqAway in_scope old_id
1060 id2 = setIdType id1 (substTy subst (idType old_id))
1061 new_id = zapFragileIdInfo id2 -- Zaps rules, worker-info, unfolding
1062 -- and fragile OccInfo
1063 new_in_scope = in_scope `extendInScopeSet` new_id
1064
1065 -- Extend the substitution if the unique has changed,
1066 -- or there's some useful occurrence information
1067 -- See the notes with substTyVarBndr for the delSubstEnv
1068 new_id_subst | new_id /= old_id
1069 = extendVarEnv id_subst old_id (Var new_id)
1070 | otherwise
1071 = delVarEnv id_subst old_id
1072
1073 ----------------------
1074 subst_opt_bndrs :: Subst -> [InVar] -> (Subst, [OutVar])
1075 subst_opt_bndrs subst bndrs
1076 = mapAccumL subst_opt_bndr subst bndrs
1077
1078 ----------------------
1079 add_info :: Subst -> InVar -> OutVar -> OutVar
1080 add_info subst old_bndr new_bndr
1081 | isTyVar old_bndr = new_bndr
1082 | otherwise = maybeModifyIdInfo mb_new_info new_bndr
1083 where mb_new_info = substIdInfo subst new_bndr (idInfo old_bndr)
1084
1085 simpleUnfoldingFun :: IdUnfoldingFun
1086 simpleUnfoldingFun id
1087 | isAlwaysActive (idInlineActivation id) = idUnfolding id
1088 | otherwise = noUnfolding
1089
1090 {-
1091 Note [Inline prag in simplOpt]
1092 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1093 If there's an INLINE/NOINLINE pragma that restricts the phase in
1094 which the binder can be inlined, we don't inline here; after all,
1095 we don't know what phase we're in. Here's an example
1096
1097 foo :: Int -> Int -> Int
1098 {-# INLINE foo #-}
1099 foo m n = inner m
1100 where
1101 {-# INLINE [1] inner #-}
1102 inner m = m+n
1103
1104 bar :: Int -> Int
1105 bar n = foo n 1
1106
1107 When inlining 'foo' in 'bar' we want the let-binding for 'inner'
1108 to remain visible until Phase 1
1109
1110 Note [Unfold compulsory unfoldings in LHSs]
1111 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1112 When the user writes `RULES map coerce = coerce` as a rule, the rule
1113 will only ever match if simpleOptExpr replaces coerce by its unfolding
1114 on the LHS, because that is the core that the rule matching engine
1115 will find. So do that for everything that has a compulsory
1116 unfolding. Also see Note [Desugaring coerce as cast] in Desugar.
1117
1118 However, we don't want to inline 'seq', which happens to also have a
1119 compulsory unfolding, so we only do this unfolding only for things
1120 that are always-active. See Note [User-defined RULES for seq] in MkId.
1121
1122
1123 ************************************************************************
1124 * *
1125 exprIsConApp_maybe
1126 * *
1127 ************************************************************************
1128
1129 Note [exprIsConApp_maybe]
1130 ~~~~~~~~~~~~~~~~~~~~~~~~~
1131 exprIsConApp_maybe is a very important function. There are two principal
1132 uses:
1133 * case e of { .... }
1134 * cls_op e, where cls_op is a class operation
1135
1136 In both cases you want to know if e is of form (C e1..en) where C is
1137 a data constructor.
1138
1139 However e might not *look* as if
1140
1141
1142 Note [exprIsConApp_maybe on literal strings]
1143 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1144 See #9400.
1145
1146 Conceptually, a string literal "abc" is just ('a':'b':'c':[]), but in Core
1147 they are represented as unpackCString# "abc"# by MkCore.mkStringExprFS, or
1148 unpackCStringUtf8# when the literal contains multi-byte UTF8 characters.
1149
1150 For optimizations we want to be able to treat it as a list, so they can be
1151 decomposed when used in a case-statement. exprIsConApp_maybe detects those
1152 calls to unpackCString# and returns:
1153
1154 Just (':', [Char], ['a', unpackCString# "bc"]).
1155
1156 We need to be careful about UTF8 strings here. ""# contains a ByteString, so
1157 we must parse it back into a FastString to split off the first character.
1158 That way we can treat unpackCString# and unpackCStringUtf8# in the same way.
1159 -}
1160
1161 data ConCont = CC [CoreExpr] Coercion
1162 -- Substitution already applied
1163
1164 -- | Returns @Just (dc, [t1..tk], [x1..xn])@ if the argument expression is
1165 -- a *saturated* constructor application of the form @dc t1..tk x1 .. xn@,
1166 -- where t1..tk are the *universally-qantified* type args of 'dc'
1167 exprIsConApp_maybe :: InScopeEnv -> CoreExpr -> Maybe (DataCon, [Type], [CoreExpr])
1168 exprIsConApp_maybe (in_scope, id_unf) expr
1169 = go (Left in_scope) expr (CC [] (mkReflCo Representational (exprType expr)))
1170 where
1171 go :: Either InScopeSet Subst
1172 -> CoreExpr -> ConCont
1173 -> Maybe (DataCon, [Type], [CoreExpr])
1174 go subst (Tick t expr) cont
1175 | not (tickishIsCode t) = go subst expr cont
1176 go subst (Cast expr co1) (CC [] co2)
1177 = go subst expr (CC [] (subst_co subst co1 `mkTransCo` co2))
1178 go subst (App fun arg) (CC args co)
1179 = go subst fun (CC (subst_arg subst arg : args) co)
1180 go subst (Lam var body) (CC (arg:args) co)
1181 | exprIsTrivial arg -- Don't duplicate stuff!
1182 = go (extend subst var arg) body (CC args co)
1183 go (Right sub) (Var v) cont
1184 = go (Left (substInScope sub))
1185 (lookupIdSubst (text "exprIsConApp" <+> ppr expr) sub v)
1186 cont
1187
1188 go (Left in_scope) (Var fun) cont@(CC args co)
1189
1190 | Just con <- isDataConWorkId_maybe fun
1191 , count isValArg args == idArity fun
1192 = dealWithCoercion co con args
1193
1194 -- Look through dictionary functions; see Note [Unfolding DFuns]
1195 | DFunUnfolding { df_bndrs = bndrs, df_con = con, df_args = dfun_args } <- unfolding
1196 , bndrs `equalLength` args -- See Note [DFun arity check]
1197 , let subst = mkOpenSubst in_scope (bndrs `zip` args)
1198 = dealWithCoercion co con (map (substExpr (text "exprIsConApp1") subst) dfun_args)
1199
1200 -- Look through unfoldings, but only arity-zero one;
1201 -- if arity > 0 we are effectively inlining a function call,
1202 -- and that is the business of callSiteInline.
1203 -- In practice, without this test, most of the "hits" were
1204 -- CPR'd workers getting inlined back into their wrappers,
1205 | idArity fun == 0
1206 , Just rhs <- expandUnfolding_maybe unfolding
1207 , let in_scope' = extendInScopeSetSet in_scope (exprFreeVars rhs)
1208 = go (Left in_scope') rhs cont
1209
1210 | (fun `hasKey` unpackCStringIdKey)
1211 || (fun `hasKey` unpackCStringUtf8IdKey)
1212 , [Lit (MachStr str)] <- args
1213 = dealWithStringLiteral fun str co
1214 where
1215 unfolding = id_unf fun
1216
1217 go _ _ _ = Nothing
1218
1219 ----------------------------
1220 -- Operations on the (Either InScopeSet CoreSubst)
1221 -- The Left case is wildly dominant
1222 subst_co (Left {}) co = co
1223 subst_co (Right s) co = CoreSubst.substCo s co
1224
1225 subst_arg (Left {}) e = e
1226 subst_arg (Right s) e = substExpr (text "exprIsConApp2") s e
1227
1228 extend (Left in_scope) v e = Right (extendSubst (mkEmptySubst in_scope) v e)
1229 extend (Right s) v e = Right (extendSubst s v e)
1230
1231 -- See Note [exprIsConApp_maybe on literal strings]
1232 dealWithStringLiteral :: Var -> BS.ByteString -> Coercion
1233 -> Maybe (DataCon, [Type], [CoreExpr])
1234
1235 -- This is not possible with user-supplied empty literals, MkCore.mkStringExprFS
1236 -- turns those into [] automatically, but just in case something else in GHC
1237 -- generates a string literal directly.
1238 dealWithStringLiteral _ str co
1239 | BS.null str
1240 = dealWithCoercion co nilDataCon [Type charTy]
1241
1242 dealWithStringLiteral fun str co
1243 = let strFS = mkFastStringByteString str
1244
1245 char = mkConApp charDataCon [mkCharLit (headFS strFS)]
1246 charTail = fastStringToByteString (tailFS strFS)
1247
1248 -- In singleton strings, just add [] instead of unpackCstring# ""#.
1249 rest = if BS.null charTail
1250 then mkConApp nilDataCon [Type charTy]
1251 else App (Var fun)
1252 (Lit (MachStr charTail))
1253
1254 in dealWithCoercion co consDataCon [Type charTy, char, rest]
1255
1256 dealWithCoercion :: Coercion -> DataCon -> [CoreExpr]
1257 -> Maybe (DataCon, [Type], [CoreExpr])
1258 dealWithCoercion co dc dc_args
1259 | isReflCo co
1260 , let (univ_ty_args, rest_args) = splitAtList (dataConUnivTyVars dc) dc_args
1261 = Just (dc, stripTypeArgs univ_ty_args, rest_args)
1262
1263 | Pair _from_ty to_ty <- coercionKind co
1264 , Just (to_tc, to_tc_arg_tys) <- splitTyConApp_maybe to_ty
1265 , to_tc == dataConTyCon dc
1266 -- These two tests can fail; we might see
1267 -- (C x y) `cast` (g :: T a ~ S [a]),
1268 -- where S is a type function. In fact, exprIsConApp
1269 -- will probably not be called in such circumstances,
1270 -- but there't nothing wrong with it
1271
1272 = -- Here we do the KPush reduction rule as described in the FC paper
1273 -- The transformation applies iff we have
1274 -- (C e1 ... en) `cast` co
1275 -- where co :: (T t1 .. tn) ~ to_ty
1276 -- The left-hand one must be a T, because exprIsConApp returned True
1277 -- but the right-hand one might not be. (Though it usually will.)
1278 let
1279 tc_arity = tyConArity to_tc
1280 dc_univ_tyvars = dataConUnivTyVars dc
1281 dc_ex_tyvars = dataConExTyVars dc
1282 arg_tys = dataConRepArgTys dc
1283
1284 non_univ_args = dropList dc_univ_tyvars dc_args
1285 (ex_args, val_args) = splitAtList dc_ex_tyvars non_univ_args
1286
1287 -- Make the "theta" from Fig 3 of the paper
1288 gammas = decomposeCo tc_arity co
1289 theta_subst = liftCoSubstWith Representational
1290 (dc_univ_tyvars ++ dc_ex_tyvars)
1291 -- existentials are at role N
1292 (gammas ++ map (mkReflCo Nominal)
1293 (stripTypeArgs ex_args))
1294
1295 -- Cast the value arguments (which include dictionaries)
1296 new_val_args = zipWith cast_arg arg_tys val_args
1297 cast_arg arg_ty arg = mkCast arg (theta_subst arg_ty)
1298
1299 dump_doc = vcat [ppr dc, ppr dc_univ_tyvars, ppr dc_ex_tyvars,
1300 ppr arg_tys, ppr dc_args,
1301 ppr ex_args, ppr val_args, ppr co, ppr _from_ty, ppr to_ty, ppr to_tc ]
1302 in
1303 ASSERT2( eqType _from_ty (mkTyConApp to_tc (stripTypeArgs $ takeList dc_univ_tyvars dc_args))
1304 , dump_doc )
1305 ASSERT2( all isTypeArg ex_args, dump_doc )
1306 ASSERT2( equalLength val_args arg_tys, dump_doc )
1307 Just (dc, to_tc_arg_tys, ex_args ++ new_val_args)
1308
1309 | otherwise
1310 = Nothing
1311
1312 stripTypeArgs :: [CoreExpr] -> [Type]
1313 stripTypeArgs args = ASSERT2( all isTypeArg args, ppr args )
1314 [ty | Type ty <- args]
1315 -- We really do want isTypeArg here, not isTyCoArg!
1316
1317 {-
1318 Note [Unfolding DFuns]
1319 ~~~~~~~~~~~~~~~~~~~~~~
1320 DFuns look like
1321
1322 df :: forall a b. (Eq a, Eq b) -> Eq (a,b)
1323 df a b d_a d_b = MkEqD (a,b) ($c1 a b d_a d_b)
1324 ($c2 a b d_a d_b)
1325
1326 So to split it up we just need to apply the ops $c1, $c2 etc
1327 to the very same args as the dfun. It takes a little more work
1328 to compute the type arguments to the dictionary constructor.
1329
1330 Note [DFun arity check]
1331 ~~~~~~~~~~~~~~~~~~~~~~~
1332 Here we check that the total number of supplied arguments (inclding
1333 type args) matches what the dfun is expecting. This may be *less*
1334 than the ordinary arity of the dfun: see Note [DFun unfoldings] in CoreSyn
1335 -}
1336
1337 exprIsLiteral_maybe :: InScopeEnv -> CoreExpr -> Maybe Literal
1338 -- Same deal as exprIsConApp_maybe, but much simpler
1339 -- Nevertheless we do need to look through unfoldings for
1340 -- Integer literals, which are vigorously hoisted to top level
1341 -- and not subsequently inlined
1342 exprIsLiteral_maybe env@(_, id_unf) e
1343 = case e of
1344 Lit l -> Just l
1345 Tick _ e' -> exprIsLiteral_maybe env e' -- dubious?
1346 Var v | Just rhs <- expandUnfolding_maybe (id_unf v)
1347 -> exprIsLiteral_maybe env rhs
1348 _ -> Nothing
1349
1350 {-
1351 Note [exprIsLambda_maybe]
1352 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1353 exprIsLambda_maybe will, given an expression `e`, try to turn it into the form
1354 `Lam v e'` (returned as `Just (v,e')`). Besides using lambdas, it looks through
1355 casts (using the Push rule), and it unfolds function calls if the unfolding
1356 has a greater arity than arguments are present.
1357
1358 Currently, it is used in Rules.match, and is required to make
1359 "map coerce = coerce" match.
1360 -}
1361
1362 exprIsLambda_maybe :: InScopeEnv -> CoreExpr
1363 -> Maybe (Var, CoreExpr,[Tickish Id])
1364 -- See Note [exprIsLambda_maybe]
1365
1366 -- The simple case: It is a lambda already
1367 exprIsLambda_maybe _ (Lam x e)
1368 = Just (x, e, [])
1369
1370 -- Still straightforward: Ticks that we can float out of the way
1371 exprIsLambda_maybe (in_scope_set, id_unf) (Tick t e)
1372 | tickishFloatable t
1373 , Just (x, e, ts) <- exprIsLambda_maybe (in_scope_set, id_unf) e
1374 = Just (x, e, t:ts)
1375
1376 -- Also possible: A casted lambda. Push the coercion inside
1377 exprIsLambda_maybe (in_scope_set, id_unf) (Cast casted_e co)
1378 | Just (x, e,ts) <- exprIsLambda_maybe (in_scope_set, id_unf) casted_e
1379 -- Only do value lambdas.
1380 -- this implies that x is not in scope in gamma (makes this code simpler)
1381 , not (isTyVar x) && not (isCoVar x)
1382 , ASSERT( not $ x `elemVarSet` tyCoVarsOfCo co) True
1383 , Just (x',e') <- pushCoercionIntoLambda in_scope_set x e co
1384 , let res = Just (x',e',ts)
1385 = --pprTrace "exprIsLambda_maybe:Cast" (vcat [ppr casted_e,ppr co,ppr res)])
1386 res
1387
1388 -- Another attempt: See if we find a partial unfolding
1389 exprIsLambda_maybe (in_scope_set, id_unf) e
1390 | (Var f, as, ts) <- collectArgsTicks tickishFloatable e
1391 , idArity f > length (filter isValArg as)
1392 -- Make sure there is hope to get a lambda
1393 , Just rhs <- expandUnfolding_maybe (id_unf f)
1394 -- Optimize, for beta-reduction
1395 , let e' = simpleOptExprWith (mkEmptySubst in_scope_set) (rhs `mkApps` as)
1396 -- Recurse, because of possible casts
1397 , Just (x', e'', ts') <- exprIsLambda_maybe (in_scope_set, id_unf) e'
1398 , let res = Just (x', e'', ts++ts')
1399 = -- pprTrace "exprIsLambda_maybe:Unfold" (vcat [ppr e, ppr (x',e'')])
1400 res
1401
1402 exprIsLambda_maybe _ _e
1403 = -- pprTrace "exprIsLambda_maybe:Fail" (vcat [ppr _e])
1404 Nothing
1405
1406
1407 pushCoercionIntoLambda
1408 :: InScopeSet -> Var -> CoreExpr -> Coercion -> Maybe (Var, CoreExpr)
1409 pushCoercionIntoLambda in_scope x e co
1410 -- This implements the Push rule from the paper on coercions
1411 -- Compare with simplCast in Simplify
1412 | ASSERT(not (isTyVar x) && not (isCoVar x)) True
1413 , Pair s1s2 t1t2 <- coercionKind co
1414 , Just (_s1,_s2) <- splitFunTy_maybe s1s2
1415 , Just (t1,_t2) <- splitFunTy_maybe t1t2
1416 = let [co1, co2] = decomposeCo 2 co
1417 -- Should we optimize the coercions here?
1418 -- Otherwise they might not match too well
1419 x' = x `setIdType` t1
1420 in_scope' = in_scope `extendInScopeSet` x'
1421 subst = extendIdSubst (mkEmptySubst in_scope')
1422 x
1423 (mkCast (Var x') co1)
1424 in Just (x', subst_expr subst e `mkCast` co2)
1425 | otherwise
1426 = pprTrace "exprIsLambda_maybe: Unexpected lambda in case" (ppr (Lam x e))
1427 Nothing