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