Do not optimise RULE lhs in substRule
[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 doc subst') args
703 , ru_rhs = substExpr (text "foo") subst' rhs }
704 -- Do NOT optimise the RHS (previously we did simplOptExpr here)
705 -- See Note [Substitute lazily]
706 where
707 doc = ptext (sLit "subst-rule") <+> ppr fn_name
708 (subst', bndrs') = substBndrs subst bndrs
709
710 ------------------
711 substVects :: Subst -> [CoreVect] -> [CoreVect]
712 substVects subst = map (substVect subst)
713
714 ------------------
715 substVect :: Subst -> CoreVect -> CoreVect
716 substVect subst (Vect v rhs) = Vect v (simpleOptExprWith subst rhs)
717 substVect _subst vd@(NoVect _) = vd
718 substVect _subst vd@(VectType _ _ _) = vd
719 substVect _subst vd@(VectClass _) = vd
720 substVect _subst vd@(VectInst _) = vd
721
722 ------------------
723 substVarSet :: Subst -> VarSet -> VarSet
724 substVarSet subst fvs
725 = foldVarSet (unionVarSet . subst_fv subst) emptyVarSet fvs
726 where
727 subst_fv subst fv
728 | isId fv = exprFreeVars (lookupIdSubst (text "substVarSet") subst fv)
729 | otherwise = Type.tyVarsOfType (lookupTvSubst subst fv)
730
731 ------------------
732 substTickish :: Subst -> Tickish Id -> Tickish Id
733 substTickish subst (Breakpoint n ids) = Breakpoint n (map do_one ids)
734 where do_one = getIdFromTrivialExpr . lookupIdSubst (text "subst_tickish") subst
735 substTickish _subst other = other
736
737 {- Note [Substitute lazily]
738 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
739 The functions that substitute over IdInfo must be pretty lazy, becuause
740 they are knot-tied by substRecBndrs.
741
742 One case in point was Trac #10627 in which a rule for a function 'f'
743 referred to 'f' (at a differnet type) on the RHS. But instead of just
744 substituting in the rhs of the rule, we were calling simpleOptExpr, which
745 looked at the idInfo for 'f'; result <<loop>>.
746
747 In any case we don't need to optimise the RHS of rules, or unfoldings,
748 because the simplifier will do that.
749
750
751 Note [substTickish]
752 ~~~~~~~~~~~~~~~~~~~~~~
753 A Breakpoint contains a list of Ids. What happens if we ever want to
754 substitute an expression for one of these Ids?
755
756 First, we ensure that we only ever substitute trivial expressions for
757 these Ids, by marking them as NoOccInfo in the occurrence analyser.
758 Then, when substituting for the Id, we unwrap any type applications
759 and abstractions to get back to an Id, with getIdFromTrivialExpr.
760
761 Second, we have to ensure that we never try to substitute a literal
762 for an Id in a breakpoint. We ensure this by never storing an Id with
763 an unlifted type in a Breakpoint - see Coverage.mkTickish.
764 Breakpoints can't handle free variables with unlifted types anyway.
765 -}
766
767 {-
768 Note [Worker inlining]
769 ~~~~~~~~~~~~~~~~~~~~~~
770 A worker can get sustituted away entirely.
771 - it might be trivial
772 - it might simply be very small
773 We do not treat an InlWrapper as an 'occurrence' in the occurrence
774 analyser, so it's possible that the worker is not even in scope any more.
775
776 In all all these cases we simply drop the special case, returning to
777 InlVanilla. The WARN is just so I can see if it happens a lot.
778
779
780 ************************************************************************
781 * *
782 The Very Simple Optimiser
783 * *
784 ************************************************************************
785
786 Note [Optimise coercion boxes aggressively]
787 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788
789 The simple expression optimiser needs to deal with Eq# boxes as follows:
790 1. If the result of optimising the RHS of a non-recursive binding is an
791 Eq# box, that box is substituted rather than turned into a let, just as
792 if it were trivial.
793 let eqv = Eq# co in e ==> e[Eq# co/eqv]
794
795 2. If the result of optimising a case scrutinee is a Eq# box and the case
796 deconstructs it in a trivial way, we evaluate the case then and there.
797 case Eq# co of Eq# cov -> e ==> e[co/cov]
798
799 We do this for two reasons:
800
801 1. Bindings/case scrutinisation of this form is often created by the
802 evidence-binding mechanism and we need them to be inlined to be able
803 desugar RULE LHSes that involve equalities (see e.g. T2291)
804
805 2. The test T4356 fails Lint because it creates a coercion between types
806 of kind (* -> * -> *) and (?? -> ? -> *), which differ. If we do this
807 inlining aggressively we can collapse away the intermediate coercion between
808 these two types and hence pass Lint again. (This is a sort of a hack.)
809
810 In fact, our implementation uses slightly liberalised versions of the second rule
811 rule so that the optimisations are a bit more generally applicable. Precisely:
812 2a. We reduce any situation where we can spot a case-of-known-constructor
813
814 As a result, the only time we should get residual coercion boxes in the code is
815 when the type checker generates something like:
816
817 \eqv -> let eqv' = Eq# (case eqv of Eq# cov -> ... cov ...)
818
819 However, the case of lambda-bound equality evidence is fairly rare, so these two
820 rules should suffice for solving the rule LHS problem for now.
821
822 Annoyingly, we cannot use this modified rule 1a instead of 1:
823
824 1a. If we come across a let-bound constructor application with trivial arguments,
825 add an appropriate unfolding to the let binder. We spot constructor applications
826 by using exprIsConApp_maybe, so this would actually let rule 2a reduce more.
827
828 The reason is that we REALLY NEED coercion boxes to be substituted away. With rule 1a
829 we wouldn't simplify this expression at all:
830
831 let eqv = Eq# co
832 in foo eqv (bar eqv)
833
834 The rule LHS desugarer can't deal with Let at all, so we need to push that box into
835 the use sites.
836 -}
837
838 simpleOptExpr :: CoreExpr -> CoreExpr
839 -- Do simple optimisation on an expression
840 -- The optimisation is very straightforward: just
841 -- inline non-recursive bindings that are used only once,
842 -- or where the RHS is trivial
843 --
844 -- We also inline bindings that bind a Eq# box: see
845 -- See Note [Optimise coercion boxes aggressively].
846 --
847 -- The result is NOT guaranteed occurrence-analysed, because
848 -- in (let x = y in ....) we substitute for x; so y's occ-info
849 -- may change radically
850
851 simpleOptExpr expr
852 = -- pprTrace "simpleOptExpr" (ppr init_subst $$ ppr expr)
853 simpleOptExprWith init_subst expr
854 where
855 init_subst = mkEmptySubst (mkInScopeSet (exprFreeVars expr))
856 -- It's potentially important to make a proper in-scope set
857 -- Consider let x = ..y.. in \y. ...x...
858 -- Then we should remember to clone y before substituting
859 -- for x. It's very unlikely to occur, because we probably
860 -- won't *be* substituting for x if it occurs inside a
861 -- lambda.
862 --
863 -- It's a bit painful to call exprFreeVars, because it makes
864 -- three passes instead of two (occ-anal, and go)
865
866 simpleOptExprWith :: Subst -> InExpr -> OutExpr
867 simpleOptExprWith subst expr = simple_opt_expr subst (occurAnalyseExpr expr)
868
869 ----------------------
870 simpleOptPgm :: DynFlags -> Module
871 -> CoreProgram -> [CoreRule] -> [CoreVect]
872 -> IO (CoreProgram, [CoreRule], [CoreVect])
873 simpleOptPgm dflags this_mod binds rules vects
874 = do { dumpIfSet_dyn dflags Opt_D_dump_occur_anal "Occurrence analysis"
875 (pprCoreBindings occ_anald_binds $$ pprRules rules );
876
877 ; return (reverse binds', substRulesForImportedIds subst' rules, substVects subst' vects) }
878 where
879 occ_anald_binds = occurAnalysePgm this_mod (\_ -> False) {- No rules active -}
880 rules vects emptyVarEnv binds
881 (subst', binds') = foldl do_one (emptySubst, []) occ_anald_binds
882
883 do_one (subst, binds') bind
884 = case simple_opt_bind subst bind of
885 (subst', Nothing) -> (subst', binds')
886 (subst', Just bind') -> (subst', bind':binds')
887
888 ----------------------
889 type InVar = Var
890 type OutVar = Var
891 type InId = Id
892 type OutId = Id
893 type InExpr = CoreExpr
894 type OutExpr = CoreExpr
895
896 -- In these functions the substitution maps InVar -> OutExpr
897
898 ----------------------
899 simple_opt_expr :: Subst -> InExpr -> OutExpr
900 simple_opt_expr subst expr
901 = go expr
902 where
903 in_scope_env = (substInScope subst, simpleUnfoldingFun)
904
905 go (Var v) = lookupIdSubst (text "simpleOptExpr") subst v
906 go (App e1 e2) = simple_app subst e1 [go e2]
907 go (Type ty) = Type (substTy subst ty)
908 go (Coercion co) = Coercion (optCoercion (getCvSubst subst) co)
909 go (Lit lit) = Lit lit
910 go (Tick tickish e) = mkTick (substTickish subst tickish) (go e)
911 go (Cast e co) | isReflCo co' = go e
912 | otherwise = Cast (go e) co'
913 where
914 co' = optCoercion (getCvSubst subst) co
915
916 go (Let bind body) = case simple_opt_bind subst bind of
917 (subst', Nothing) -> simple_opt_expr subst' body
918 (subst', Just bind) -> Let bind (simple_opt_expr subst' body)
919
920 go lam@(Lam {}) = go_lam [] subst lam
921 go (Case e b ty as)
922 -- See Note [Optimise coercion boxes aggressively]
923 | isDeadBinder b
924 , Just (con, _tys, es) <- exprIsConApp_maybe in_scope_env e'
925 , Just (altcon, bs, rhs) <- findAlt (DataAlt con) as
926 = case altcon of
927 DEFAULT -> go rhs
928 _ -> mkLets (catMaybes mb_binds) $ simple_opt_expr subst' rhs
929 where (subst', mb_binds) = mapAccumL simple_opt_out_bind subst
930 (zipEqual "simpleOptExpr" bs es)
931
932 | otherwise
933 = Case e' b' (substTy subst ty)
934 (map (go_alt subst') as)
935 where
936 e' = go e
937 (subst', b') = subst_opt_bndr subst b
938
939 ----------------------
940 go_alt subst (con, bndrs, rhs)
941 = (con, bndrs', simple_opt_expr subst' rhs)
942 where
943 (subst', bndrs') = subst_opt_bndrs subst bndrs
944
945 ----------------------
946 -- go_lam tries eta reduction
947 go_lam bs' subst (Lam b e)
948 = go_lam (b':bs') subst' e
949 where
950 (subst', b') = subst_opt_bndr subst b
951 go_lam bs' subst e
952 | Just etad_e <- tryEtaReduce bs e' = etad_e
953 | otherwise = mkLams bs e'
954 where
955 bs = reverse bs'
956 e' = simple_opt_expr subst e
957
958 ----------------------
959 -- simple_app collects arguments for beta reduction
960 simple_app :: Subst -> InExpr -> [OutExpr] -> CoreExpr
961 simple_app subst (App e1 e2) as
962 = simple_app subst e1 (simple_opt_expr subst e2 : as)
963 simple_app subst (Lam b e) (a:as)
964 = case maybe_substitute subst b a of
965 Just ext_subst -> simple_app ext_subst e as
966 Nothing -> Let (NonRec b2 a) (simple_app subst' e as)
967 where
968 (subst', b') = subst_opt_bndr subst b
969 b2 = add_info subst' b b'
970 simple_app subst (Var v) as
971 | isCompulsoryUnfolding (idUnfolding v)
972 , isAlwaysActive (idInlineActivation v)
973 -- See Note [Unfold compulsory unfoldings in LHSs]
974 = simple_app subst (unfoldingTemplate (idUnfolding v)) as
975 simple_app subst (Tick t e) as
976 -- Okay to do "(Tick t e) x ==> Tick t (e x)"?
977 | t `tickishScopesLike` SoftScope
978 = mkTick t $ simple_app subst e as
979 simple_app subst e as
980 = foldl App (simple_opt_expr subst e) as
981
982 ----------------------
983 simple_opt_bind,simple_opt_bind' :: Subst -> CoreBind -> (Subst, Maybe CoreBind)
984 simple_opt_bind s b -- Can add trace stuff here
985 = simple_opt_bind' s b
986
987 simple_opt_bind' subst (Rec prs)
988 = (subst'', res_bind)
989 where
990 res_bind = Just (Rec (reverse rev_prs'))
991 (subst', bndrs') = subst_opt_bndrs subst (map fst prs)
992 (subst'', rev_prs') = foldl do_pr (subst', []) (prs `zip` bndrs')
993 do_pr (subst, prs) ((b,r), b')
994 = case maybe_substitute subst b r2 of
995 Just subst' -> (subst', prs)
996 Nothing -> (subst, (b2,r2):prs)
997 where
998 b2 = add_info subst b b'
999 r2 = simple_opt_expr subst r
1000
1001 simple_opt_bind' subst (NonRec b r)
1002 = simple_opt_out_bind subst (b, simple_opt_expr subst r)
1003
1004 ----------------------
1005 simple_opt_out_bind :: Subst -> (InVar, OutExpr) -> (Subst, Maybe CoreBind)
1006 simple_opt_out_bind subst (b, r')
1007 | Just ext_subst <- maybe_substitute subst b r'
1008 = (ext_subst, Nothing)
1009 | otherwise
1010 = (subst', Just (NonRec b2 r'))
1011 where
1012 (subst', b') = subst_opt_bndr subst b
1013 b2 = add_info subst' b b'
1014
1015 ----------------------
1016 maybe_substitute :: Subst -> InVar -> OutExpr -> Maybe Subst
1017 -- (maybe_substitute subst in_var out_rhs)
1018 -- either extends subst with (in_var -> out_rhs)
1019 -- or returns Nothing
1020 maybe_substitute subst b r
1021 | Type ty <- r -- let a::* = TYPE ty in <body>
1022 = ASSERT( isTyVar b )
1023 Just (extendTvSubst subst b ty)
1024
1025 | Coercion co <- r
1026 = ASSERT( isCoVar b )
1027 Just (extendCvSubst subst b co)
1028
1029 | isId b -- let x = e in <body>
1030 , not (isCoVar b) -- See Note [Do not inline CoVars unconditionally]
1031 -- in SimplUtils
1032 , safe_to_inline (idOccInfo b)
1033 , isAlwaysActive (idInlineActivation b) -- Note [Inline prag in simplOpt]
1034 , not (isStableUnfolding (idUnfolding b))
1035 , not (isExportedId b)
1036 , not (isUnLiftedType (idType b)) || exprOkForSpeculation r
1037 = Just (extendIdSubst subst b r)
1038
1039 | otherwise
1040 = Nothing
1041 where
1042 -- Unconditionally safe to inline
1043 safe_to_inline :: OccInfo -> Bool
1044 safe_to_inline (IAmALoopBreaker {}) = False
1045 safe_to_inline IAmDead = True
1046 safe_to_inline (OneOcc in_lam one_br _) = (not in_lam && one_br) || trivial
1047 safe_to_inline NoOccInfo = trivial
1048
1049 trivial | exprIsTrivial r = True
1050 | (Var fun, args) <- collectArgs r
1051 , Just dc <- isDataConWorkId_maybe fun
1052 , dc `hasKey` eqBoxDataConKey || dc `hasKey` coercibleDataConKey
1053 , all exprIsTrivial args = True -- See Note [Optimise coercion boxes aggressively]
1054 | otherwise = False
1055
1056 ----------------------
1057 subst_opt_bndr :: Subst -> InVar -> (Subst, OutVar)
1058 subst_opt_bndr subst bndr
1059 | isTyVar bndr = substTyVarBndr subst bndr
1060 | isCoVar bndr = substCoVarBndr subst bndr
1061 | otherwise = subst_opt_id_bndr subst bndr
1062
1063 subst_opt_id_bndr :: Subst -> InId -> (Subst, OutId)
1064 -- Nuke all fragile IdInfo, unfolding, and RULES;
1065 -- it gets added back later by add_info
1066 -- Rather like SimplEnv.substIdBndr
1067 --
1068 -- It's important to zap fragile OccInfo (which CoreSubst.substIdBndr
1069 -- carefully does not do) because simplOptExpr invalidates it
1070
1071 subst_opt_id_bndr subst@(Subst in_scope id_subst tv_subst cv_subst) old_id
1072 = (Subst new_in_scope new_id_subst tv_subst cv_subst, new_id)
1073 where
1074 id1 = uniqAway in_scope old_id
1075 id2 = setIdType id1 (substTy subst (idType old_id))
1076 new_id = zapFragileIdInfo id2 -- Zaps rules, worker-info, unfolding
1077 -- and fragile OccInfo
1078 new_in_scope = in_scope `extendInScopeSet` new_id
1079
1080 -- Extend the substitution if the unique has changed,
1081 -- or there's some useful occurrence information
1082 -- See the notes with substTyVarBndr for the delSubstEnv
1083 new_id_subst | new_id /= old_id
1084 = extendVarEnv id_subst old_id (Var new_id)
1085 | otherwise
1086 = delVarEnv id_subst old_id
1087
1088 ----------------------
1089 subst_opt_bndrs :: Subst -> [InVar] -> (Subst, [OutVar])
1090 subst_opt_bndrs subst bndrs
1091 = mapAccumL subst_opt_bndr subst bndrs
1092
1093 ----------------------
1094 add_info :: Subst -> InVar -> OutVar -> OutVar
1095 add_info subst old_bndr new_bndr
1096 | isTyVar old_bndr = new_bndr
1097 | otherwise = maybeModifyIdInfo mb_new_info new_bndr
1098 where mb_new_info = substIdInfo subst new_bndr (idInfo old_bndr)
1099
1100 simpleUnfoldingFun :: IdUnfoldingFun
1101 simpleUnfoldingFun id
1102 | isAlwaysActive (idInlineActivation id) = idUnfolding id
1103 | otherwise = noUnfolding
1104
1105 {-
1106 Note [Inline prag in simplOpt]
1107 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1108 If there's an INLINE/NOINLINE pragma that restricts the phase in
1109 which the binder can be inlined, we don't inline here; after all,
1110 we don't know what phase we're in. Here's an example
1111
1112 foo :: Int -> Int -> Int
1113 {-# INLINE foo #-}
1114 foo m n = inner m
1115 where
1116 {-# INLINE [1] inner #-}
1117 inner m = m+n
1118
1119 bar :: Int -> Int
1120 bar n = foo n 1
1121
1122 When inlining 'foo' in 'bar' we want the let-binding for 'inner'
1123 to remain visible until Phase 1
1124
1125 Note [Unfold compulsory unfoldings in LHSs]
1126 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1127 When the user writes `RULES map coerce = coerce` as a rule, the rule
1128 will only ever match if simpleOptExpr replaces coerce by its unfolding
1129 on the LHS, because that is the core that the rule matching engine
1130 will find. So do that for everything that has a compulsory
1131 unfolding. Also see Note [Desugaring coerce as cast] in Desugar.
1132
1133 However, we don't want to inline 'seq', which happens to also have a
1134 compulsory unfolding, so we only do this unfolding only for things
1135 that are always-active. See Note [User-defined RULES for seq] in MkId.
1136
1137
1138 ************************************************************************
1139 * *
1140 exprIsConApp_maybe
1141 * *
1142 ************************************************************************
1143
1144 Note [exprIsConApp_maybe]
1145 ~~~~~~~~~~~~~~~~~~~~~~~~~
1146 exprIsConApp_maybe is a very important function. There are two principal
1147 uses:
1148 * case e of { .... }
1149 * cls_op e, where cls_op is a class operation
1150
1151 In both cases you want to know if e is of form (C e1..en) where C is
1152 a data constructor.
1153
1154 However e might not *look* as if
1155
1156
1157 Note [exprIsConApp_maybe on literal strings]
1158 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1159 See #9400.
1160
1161 Conceptually, a string literal "abc" is just ('a':'b':'c':[]), but in Core
1162 they are represented as unpackCString# "abc"# by MkCore.mkStringExprFS, or
1163 unpackCStringUtf8# when the literal contains multi-byte UTF8 characters.
1164
1165 For optimizations we want to be able to treat it as a list, so they can be
1166 decomposed when used in a case-statement. exprIsConApp_maybe detects those
1167 calls to unpackCString# and returns:
1168
1169 Just (':', [Char], ['a', unpackCString# "bc"]).
1170
1171 We need to be careful about UTF8 strings here. ""# contains a ByteString, so
1172 we must parse it back into a FastString to split off the first character.
1173 That way we can treat unpackCString# and unpackCStringUtf8# in the same way.
1174 -}
1175
1176 data ConCont = CC [CoreExpr] Coercion
1177 -- Substitution already applied
1178
1179 -- | Returns @Just (dc, [t1..tk], [x1..xn])@ if the argument expression is
1180 -- a *saturated* constructor application of the form @dc t1..tk x1 .. xn@,
1181 -- where t1..tk are the *universally-qantified* type args of 'dc'
1182 exprIsConApp_maybe :: InScopeEnv -> CoreExpr -> Maybe (DataCon, [Type], [CoreExpr])
1183 exprIsConApp_maybe (in_scope, id_unf) expr
1184 = go (Left in_scope) expr (CC [] (mkReflCo Representational (exprType expr)))
1185 where
1186 go :: Either InScopeSet Subst
1187 -> CoreExpr -> ConCont
1188 -> Maybe (DataCon, [Type], [CoreExpr])
1189 go subst (Tick t expr) cont
1190 | not (tickishIsCode t) = go subst expr cont
1191 go subst (Cast expr co1) (CC [] co2)
1192 = go subst expr (CC [] (subst_co subst co1 `mkTransCo` co2))
1193 go subst (App fun arg) (CC args co)
1194 = go subst fun (CC (subst_arg subst arg : args) co)
1195 go subst (Lam var body) (CC (arg:args) co)
1196 | exprIsTrivial arg -- Don't duplicate stuff!
1197 = go (extend subst var arg) body (CC args co)
1198 go (Right sub) (Var v) cont
1199 = go (Left (substInScope sub))
1200 (lookupIdSubst (text "exprIsConApp" <+> ppr expr) sub v)
1201 cont
1202
1203 go (Left in_scope) (Var fun) cont@(CC args co)
1204
1205 | Just con <- isDataConWorkId_maybe fun
1206 , count isValArg args == idArity fun
1207 = dealWithCoercion co con args
1208
1209 -- Look through dictionary functions; see Note [Unfolding DFuns]
1210 | DFunUnfolding { df_bndrs = bndrs, df_con = con, df_args = dfun_args } <- unfolding
1211 , bndrs `equalLength` args -- See Note [DFun arity check]
1212 , let subst = mkOpenSubst in_scope (bndrs `zip` args)
1213 = dealWithCoercion co con (map (substExpr (text "exprIsConApp1") subst) dfun_args)
1214
1215 -- Look through unfoldings, but only arity-zero one;
1216 -- if arity > 0 we are effectively inlining a function call,
1217 -- and that is the business of callSiteInline.
1218 -- In practice, without this test, most of the "hits" were
1219 -- CPR'd workers getting inlined back into their wrappers,
1220 | idArity fun == 0
1221 , Just rhs <- expandUnfolding_maybe unfolding
1222 , let in_scope' = extendInScopeSetSet in_scope (exprFreeVars rhs)
1223 = go (Left in_scope') rhs cont
1224
1225 | (fun `hasKey` unpackCStringIdKey)
1226 || (fun `hasKey` unpackCStringUtf8IdKey)
1227 , [Lit (MachStr str)] <- args
1228 = dealWithStringLiteral fun str co
1229 where
1230 unfolding = id_unf fun
1231
1232 go _ _ _ = Nothing
1233
1234 ----------------------------
1235 -- Operations on the (Either InScopeSet CoreSubst)
1236 -- The Left case is wildly dominant
1237 subst_co (Left {}) co = co
1238 subst_co (Right s) co = CoreSubst.substCo s co
1239
1240 subst_arg (Left {}) e = e
1241 subst_arg (Right s) e = substExpr (text "exprIsConApp2") s e
1242
1243 extend (Left in_scope) v e = Right (extendSubst (mkEmptySubst in_scope) v e)
1244 extend (Right s) v e = Right (extendSubst s v e)
1245
1246 -- See Note [exprIsConApp_maybe on literal strings]
1247 dealWithStringLiteral :: Var -> BS.ByteString -> Coercion
1248 -> Maybe (DataCon, [Type], [CoreExpr])
1249
1250 -- This is not possible with user-supplied empty literals, MkCore.mkStringExprFS
1251 -- turns those into [] automatically, but just in case something else in GHC
1252 -- generates a string literal directly.
1253 dealWithStringLiteral _ str co
1254 | BS.null str
1255 = dealWithCoercion co nilDataCon [Type charTy]
1256
1257 dealWithStringLiteral fun str co
1258 = let strFS = mkFastStringByteString str
1259
1260 char = mkConApp charDataCon [mkCharLit (headFS strFS)]
1261 charTail = fastStringToByteString (tailFS strFS)
1262
1263 -- In singleton strings, just add [] instead of unpackCstring# ""#.
1264 rest = if BS.null charTail
1265 then mkConApp nilDataCon [Type charTy]
1266 else App (Var fun)
1267 (Lit (MachStr charTail))
1268
1269 in dealWithCoercion co consDataCon [Type charTy, char, rest]
1270
1271 dealWithCoercion :: Coercion -> DataCon -> [CoreExpr]
1272 -> Maybe (DataCon, [Type], [CoreExpr])
1273 dealWithCoercion co dc dc_args
1274 | isReflCo co
1275 , let (univ_ty_args, rest_args) = splitAtList (dataConUnivTyVars dc) dc_args
1276 = Just (dc, stripTypeArgs univ_ty_args, rest_args)
1277
1278 | Pair _from_ty to_ty <- coercionKind co
1279 , Just (to_tc, to_tc_arg_tys) <- splitTyConApp_maybe to_ty
1280 , to_tc == dataConTyCon dc
1281 -- These two tests can fail; we might see
1282 -- (C x y) `cast` (g :: T a ~ S [a]),
1283 -- where S is a type function. In fact, exprIsConApp
1284 -- will probably not be called in such circumstances,
1285 -- but there't nothing wrong with it
1286
1287 = -- Here we do the KPush reduction rule as described in the FC paper
1288 -- The transformation applies iff we have
1289 -- (C e1 ... en) `cast` co
1290 -- where co :: (T t1 .. tn) ~ to_ty
1291 -- The left-hand one must be a T, because exprIsConApp returned True
1292 -- but the right-hand one might not be. (Though it usually will.)
1293 let
1294 tc_arity = tyConArity to_tc
1295 dc_univ_tyvars = dataConUnivTyVars dc
1296 dc_ex_tyvars = dataConExTyVars dc
1297 arg_tys = dataConRepArgTys dc
1298
1299 non_univ_args = dropList dc_univ_tyvars dc_args
1300 (ex_args, val_args) = splitAtList dc_ex_tyvars non_univ_args
1301
1302 -- Make the "theta" from Fig 3 of the paper
1303 gammas = decomposeCo tc_arity co
1304 theta_subst = liftCoSubstWith Representational
1305 (dc_univ_tyvars ++ dc_ex_tyvars)
1306 -- existentials are at role N
1307 (gammas ++ map (mkReflCo Nominal)
1308 (stripTypeArgs ex_args))
1309
1310 -- Cast the value arguments (which include dictionaries)
1311 new_val_args = zipWith cast_arg arg_tys val_args
1312 cast_arg arg_ty arg = mkCast arg (theta_subst arg_ty)
1313
1314 dump_doc = vcat [ppr dc, ppr dc_univ_tyvars, ppr dc_ex_tyvars,
1315 ppr arg_tys, ppr dc_args,
1316 ppr ex_args, ppr val_args, ppr co, ppr _from_ty, ppr to_ty, ppr to_tc ]
1317 in
1318 ASSERT2( eqType _from_ty (mkTyConApp to_tc (stripTypeArgs $ takeList dc_univ_tyvars dc_args))
1319 , dump_doc )
1320 ASSERT2( all isTypeArg ex_args, dump_doc )
1321 ASSERT2( equalLength val_args arg_tys, dump_doc )
1322 Just (dc, to_tc_arg_tys, ex_args ++ new_val_args)
1323
1324 | otherwise
1325 = Nothing
1326
1327 stripTypeArgs :: [CoreExpr] -> [Type]
1328 stripTypeArgs args = ASSERT2( all isTypeArg args, ppr args )
1329 [ty | Type ty <- args]
1330 -- We really do want isTypeArg here, not isTyCoArg!
1331
1332 {-
1333 Note [Unfolding DFuns]
1334 ~~~~~~~~~~~~~~~~~~~~~~
1335 DFuns look like
1336
1337 df :: forall a b. (Eq a, Eq b) -> Eq (a,b)
1338 df a b d_a d_b = MkEqD (a,b) ($c1 a b d_a d_b)
1339 ($c2 a b d_a d_b)
1340
1341 So to split it up we just need to apply the ops $c1, $c2 etc
1342 to the very same args as the dfun. It takes a little more work
1343 to compute the type arguments to the dictionary constructor.
1344
1345 Note [DFun arity check]
1346 ~~~~~~~~~~~~~~~~~~~~~~~
1347 Here we check that the total number of supplied arguments (inclding
1348 type args) matches what the dfun is expecting. This may be *less*
1349 than the ordinary arity of the dfun: see Note [DFun unfoldings] in CoreSyn
1350 -}
1351
1352 exprIsLiteral_maybe :: InScopeEnv -> CoreExpr -> Maybe Literal
1353 -- Same deal as exprIsConApp_maybe, but much simpler
1354 -- Nevertheless we do need to look through unfoldings for
1355 -- Integer literals, which are vigorously hoisted to top level
1356 -- and not subsequently inlined
1357 exprIsLiteral_maybe env@(_, id_unf) e
1358 = case e of
1359 Lit l -> Just l
1360 Tick _ e' -> exprIsLiteral_maybe env e' -- dubious?
1361 Var v | Just rhs <- expandUnfolding_maybe (id_unf v)
1362 -> exprIsLiteral_maybe env rhs
1363 _ -> Nothing
1364
1365 {-
1366 Note [exprIsLambda_maybe]
1367 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1368 exprIsLambda_maybe will, given an expression `e`, try to turn it into the form
1369 `Lam v e'` (returned as `Just (v,e')`). Besides using lambdas, it looks through
1370 casts (using the Push rule), and it unfolds function calls if the unfolding
1371 has a greater arity than arguments are present.
1372
1373 Currently, it is used in Rules.match, and is required to make
1374 "map coerce = coerce" match.
1375 -}
1376
1377 exprIsLambda_maybe :: InScopeEnv -> CoreExpr
1378 -> Maybe (Var, CoreExpr,[Tickish Id])
1379 -- See Note [exprIsLambda_maybe]
1380
1381 -- The simple case: It is a lambda already
1382 exprIsLambda_maybe _ (Lam x e)
1383 = Just (x, e, [])
1384
1385 -- Still straightforward: Ticks that we can float out of the way
1386 exprIsLambda_maybe (in_scope_set, id_unf) (Tick t e)
1387 | tickishFloatable t
1388 , Just (x, e, ts) <- exprIsLambda_maybe (in_scope_set, id_unf) e
1389 = Just (x, e, t:ts)
1390
1391 -- Also possible: A casted lambda. Push the coercion inside
1392 exprIsLambda_maybe (in_scope_set, id_unf) (Cast casted_e co)
1393 | Just (x, e,ts) <- exprIsLambda_maybe (in_scope_set, id_unf) casted_e
1394 -- Only do value lambdas.
1395 -- this implies that x is not in scope in gamma (makes this code simpler)
1396 , not (isTyVar x) && not (isCoVar x)
1397 , ASSERT( not $ x `elemVarSet` tyCoVarsOfCo co) True
1398 , Just (x',e') <- pushCoercionIntoLambda in_scope_set x e co
1399 , let res = Just (x',e',ts)
1400 = --pprTrace "exprIsLambda_maybe:Cast" (vcat [ppr casted_e,ppr co,ppr res)])
1401 res
1402
1403 -- Another attempt: See if we find a partial unfolding
1404 exprIsLambda_maybe (in_scope_set, id_unf) e
1405 | (Var f, as, ts) <- collectArgsTicks tickishFloatable e
1406 , idArity f > length (filter isValArg as)
1407 -- Make sure there is hope to get a lambda
1408 , Just rhs <- expandUnfolding_maybe (id_unf f)
1409 -- Optimize, for beta-reduction
1410 , let e' = simpleOptExprWith (mkEmptySubst in_scope_set) (rhs `mkApps` as)
1411 -- Recurse, because of possible casts
1412 , Just (x', e'', ts') <- exprIsLambda_maybe (in_scope_set, id_unf) e'
1413 , let res = Just (x', e'', ts++ts')
1414 = -- pprTrace "exprIsLambda_maybe:Unfold" (vcat [ppr e, ppr (x',e'')])
1415 res
1416
1417 exprIsLambda_maybe _ _e
1418 = -- pprTrace "exprIsLambda_maybe:Fail" (vcat [ppr _e])
1419 Nothing
1420
1421
1422 pushCoercionIntoLambda
1423 :: InScopeSet -> Var -> CoreExpr -> Coercion -> Maybe (Var, CoreExpr)
1424 pushCoercionIntoLambda in_scope x e co
1425 -- This implements the Push rule from the paper on coercions
1426 -- Compare with simplCast in Simplify
1427 | ASSERT(not (isTyVar x) && not (isCoVar x)) True
1428 , Pair s1s2 t1t2 <- coercionKind co
1429 , Just (_s1,_s2) <- splitFunTy_maybe s1s2
1430 , Just (t1,_t2) <- splitFunTy_maybe t1t2
1431 = let [co1, co2] = decomposeCo 2 co
1432 -- Should we optimize the coercions here?
1433 -- Otherwise they might not match too well
1434 x' = x `setIdType` t1
1435 in_scope' = in_scope `extendInScopeSet` x'
1436 subst = extendIdSubst (mkEmptySubst in_scope')
1437 x
1438 (mkCast (Var x') co1)
1439 in Just (x', subst_expr subst e `mkCast` co2)
1440 | otherwise
1441 = pprTrace "exprIsLambda_maybe: Unexpected lambda in case" (ppr (Lam x e))
1442 Nothing