CoreSubst.hs 57.7 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3 4
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

Simon Marlow's avatar
Simon Marlow committed
5 6

Utility functions on @Core@ syntax
Austin Seipp's avatar
Austin Seipp committed
7
-}
8

9
{-# LANGUAGE CPP #-}
10
module CoreSubst (
11 12 13
        -- * Main data types
        Subst(..), -- Implementation exported for supercompiler's Renaming.hs only
        TvSubstEnv, IdSubstEnv, InScopeSet,
14

batterseapower's avatar
batterseapower committed
15
        -- ** Substituting into expressions and related types
16 17
        deShadowBinds, substSpec, substRulesForImportedIds,
        substTy, substCo, substExpr, substExprSC, substBind, substBindSC,
18
        substUnfolding, substUnfoldingSC,
19
        lookupIdSubst, lookupTCvSubst, substIdOcc,
20
        substTickish, substDVarSet,
21

batterseapower's avatar
batterseapower committed
22
        -- ** Operations on substitutions
23
        emptySubst, mkEmptySubst, mkSubst, mkOpenSubst, substInScope, isEmptySubst,
24
        extendIdSubst, extendIdSubstList, extendTCvSubst, extendTvSubstList,
25
        extendSubst, extendSubstList, extendSubstWithVar, zapSubstEnv,
26 27 28
        addInScopeSet, extendInScope, extendInScopeList, extendInScopeIds,
        isInScope, setInScope,
        delBndr, delBndrs,
29

30 31 32
        -- ** Substituting and cloning binders
        substBndr, substBndrs, substRecBndrs,
        cloneBndr, cloneBndrs, cloneIdBndr, cloneIdBndrs, cloneRecIdBndrs,
33

34
        -- ** Simple expression optimiser
35
        simpleOptPgm, simpleOptExpr, simpleOptExprWith,
36
        exprIsConApp_maybe, exprIsLiteral_maybe, exprIsLambda_maybe,
37 38 39 40
    ) where

#include "HsVersions.h"

Simon Marlow's avatar
Simon Marlow committed
41 42
import CoreSyn
import CoreFVs
43
import CoreSeq
Simon Marlow's avatar
Simon Marlow committed
44
import CoreUtils
45 46
import Literal  ( Literal(MachStr) )
import qualified Data.ByteString as BS
47
import OccurAnal( occurAnalyseExpr, occurAnalysePgm )
48

Simon Marlow's avatar
Simon Marlow committed
49
import qualified Type
50 51
import qualified Coercion

52
        -- We are defining local versions
53
import Type     hiding ( substTy, extendTvSubst, extendCvSubst, extendTvSubstList
54
                       , isInScope, substTyVarBndr, cloneTyVarBndr )
55
import Coercion hiding ( substCo, substCoVarBndr )
56

57 58
import TyCon       ( tyConArity )
import DataCon
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
59
import PrelNames
60
import OptCoercion ( optCoercion )
61
import PprCore     ( pprCoreBindings, pprRules )
62
import Module      ( Module )
63 64
import VarSet
import VarEnv
Simon Marlow's avatar
Simon Marlow committed
65
import Id
66
import Name     ( Name )
67
import Var
Simon Marlow's avatar
Simon Marlow committed
68 69 70
import IdInfo
import UniqSupply
import Maybes
71
import ErrUtils
72
import DynFlags
73
import BasicTypes ( isAlwaysActive )
74 75
import Util
import Pair
76
import Outputable
77
import PprCore          ()              -- Instances
78
import FastString
79 80

import Data.List
81 82

import TysWiredIn
83

Austin Seipp's avatar
Austin Seipp committed
84 85 86
{-
************************************************************************
*                                                                      *
87
\subsection{Substitutions}
Austin Seipp's avatar
Austin Seipp committed
88 89 90
*                                                                      *
************************************************************************
-}
91

92 93
-- | A substitution environment, containing 'Id', 'TyVar', and 'CoVar'
-- substitutions.
batterseapower's avatar
batterseapower committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
--
-- Some invariants apply to how you use the substitution:
--
-- 1. #in_scope_invariant# The in-scope set contains at least those 'Id's and 'TyVar's that will be in scope /after/
-- applying the substitution to a term. Precisely, the in-scope set must be a superset of the free vars of the
-- substitution range that might possibly clash with locally-bound variables in the thing being substituted in.
--
-- 2. #apply_once# You may apply the substitution only /once/
--
-- There are various ways of setting up the in-scope set such that the first of these invariants hold:
--
-- * Arrange that the in-scope set really is all the things in scope
--
-- * Arrange that it's the free vars of the range of the substitution
--
-- * Make it empty, if you know that all the free vars of the substitution are fresh, and hence can't possibly clash
110
data Subst
Thomas Schilling's avatar
Thomas Schilling committed
111 112 113
  = Subst InScopeSet  -- Variables in in scope (both Ids and TyVars) /after/
                      -- applying the substitution
          IdSubstEnv  -- Substitution for Ids
114
          TvSubstEnv  -- Substitution from TyVars to Types
115
          CvSubstEnv  -- Substitution from CoVars to Coercions
116

117 118 119 120 121 122 123 124
        -- INVARIANT 1: See #in_scope_invariant#
        -- This is what lets us deal with name capture properly
        -- It's a hard invariant to check...
        --
        -- INVARIANT 2: The substitution is apply-once; see Note [Apply once] with
        --              Types.TvSubstEnv
        --
        -- INVARIANT 3: See Note [Extending the Subst]
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
125

Austin Seipp's avatar
Austin Seipp committed
126
{-
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
127 128 129
Note [Extending the Subst]
~~~~~~~~~~~~~~~~~~~~~~~~~~
For a core Subst, which binds Ids as well, we make a different choice for Ids
130
than we do for TyVars.
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
131

132
For TyVars, see Note [Extending the TCvSubst] with Type.TvSubstEnv
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
133 134

For Ids, we have a different invariant
135 136
        The IdSubstEnv is extended *only* when the Unique on an Id changes
        Otherwise, we just extend the InScopeSet
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
137 138 139

In consequence:

140
* If all subst envs are empty, substExpr would be a
141 142 143 144 145
  no-op, so substExprSC ("short cut") does nothing.

  However, substExpr still goes ahead and substitutes.  Reason: we may
  want to replace existing Ids with new ones from the in-scope set, to
  avoid space leaks.
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
146

147
* In substIdBndr, we extend the IdSubstEnv only when the unique changes
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
148

149 150 151 152 153
* If the CvSubstEnv, TvSubstEnv and IdSubstEnv are all empty,
  substExpr does nothing (Note that the above rule for substIdBndr
  maintains this property.  If the incoming envts are both empty, then
  substituting the type and IdInfo can't change anything.)

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
154 155
* In lookupIdSubst, we *must* look up the Id in the in-scope set, because
  it may contain non-trivial changes.  Example:
156
        (/\a. \x:a. ...x...) Int
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
157 158 159 160
  We extend the TvSubstEnv with [a |-> Int]; but x's unique does not change
  so we only extend the in-scope set.  Then we must look up in the in-scope
  set when we find the occurrence of x.

161
* The requirement to look up the Id in the in-scope set means that we
162
  must NOT take no-op short cut when the IdSubst is empty.
163 164 165 166 167
  We must still look up every Id in the in-scope set.

* (However, we don't need to do so for expressions found in the IdSubst
  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
TvSubstEnv and CvSubstEnv?
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
170 171 172 173 174

* For Ids, we change the IdInfo all the time (e.g. deleting the
  unfolding), and adding it back later, so using the TyVar convention
  would entail extending the substitution almost all the time

175
* The simplifier wants to look up in the in-scope set anyway, in case it
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
176 177
  can see a better unfolding from an enclosing case expression

178
* For TyVars, only coercion variables can possibly change, and they are
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
179
  easy to spot
Austin Seipp's avatar
Austin Seipp committed
180
-}
181

batterseapower's avatar
batterseapower committed
182
-- | An environment for substituting for 'Id's
183 184 185 186
type IdSubstEnv = IdEnv CoreExpr

----------------------------
isEmptySubst :: Subst -> Bool
187
isEmptySubst (Subst _ id_env tv_env cv_env)
188
  = isEmptyVarEnv id_env && isEmptyVarEnv tv_env && isEmptyVarEnv cv_env
189 190

emptySubst :: Subst
191
emptySubst = Subst emptyInScopeSet emptyVarEnv emptyVarEnv emptyVarEnv
192 193

mkEmptySubst :: InScopeSet -> Subst
194
mkEmptySubst in_scope = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
195

196 197
mkSubst :: InScopeSet -> TvSubstEnv -> CvSubstEnv -> IdSubstEnv -> Subst
mkSubst in_scope tvs cvs ids = Subst in_scope ids tvs cvs
198

batterseapower's avatar
batterseapower committed
199
-- | Find the in-scope set: see "CoreSubst#in_scope_invariant"
200
substInScope :: Subst -> InScopeSet
201
substInScope (Subst in_scope _ _ _) = in_scope
202

batterseapower's avatar
batterseapower committed
203 204
-- | Remove all substitutions for 'Id's and 'Var's that might have been built up
-- while preserving the in-scope set
205
zapSubstEnv :: Subst -> Subst
206
zapSubstEnv (Subst in_scope _ _ _) = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
207

batterseapower's avatar
batterseapower committed
208 209
-- | Add a substitution for an 'Id' to the 'Subst': you must ensure that the in-scope set is
-- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
210
extendIdSubst :: Subst -> Id -> CoreExpr -> Subst
batterseapower's avatar
batterseapower committed
211
-- ToDo: add an ASSERT that fvs(subst-result) is already in the in-scope set
212
extendIdSubst (Subst in_scope ids tvs cvs) v r = Subst in_scope (extendVarEnv ids v r) tvs cvs
213

batterseapower's avatar
batterseapower committed
214
-- | Adds multiple 'Id' substitutions to the 'Subst': see also 'extendIdSubst'
215
extendIdSubstList :: Subst -> [(Id, CoreExpr)] -> Subst
216
extendIdSubstList (Subst in_scope ids tvs cvs) prs = Subst in_scope (extendVarEnvList ids prs) tvs cvs
217

218 219 220 221 222 223 224
-- | Add a substitution for a 'TyVar' to the 'Subst'
-- The 'TyVar' *must* be a real TyVar, and not a CoVar
-- You must ensure that the in-scope set is such that
-- the "CoreSubst#in_scope_invariant" is true after extending
-- the substitution like this.
extendTvSubst :: Subst -> TyVar -> Type -> Subst
extendTvSubst (Subst in_scope ids tvs cvs) tv ty
225 226 227
  = ASSERT( isTyVar tv )
    Subst in_scope ids (extendVarEnv tvs tv ty) cvs

228 229 230
-- | Adds multiple 'TyVar' substitutions to the 'Subst': see also 'extendTvSubst'
extendTvSubstList :: Subst -> [(TyVar,Type)] -> Subst
extendTvSubstList subst vrs
231
  = foldl' extend subst vrs
232 233
  where
    extend subst (v, r) = extendTvSubst subst v r
234

235
-- | Add a substitution from a 'CoVar' to a 'Coercion' to the 'Subst': you must ensure that the in-scope set is
236
-- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
237
extendCvSubst :: Subst -> CoVar -> Coercion -> Subst
238 239 240
extendCvSubst (Subst in_scope ids tvs cvs) v r
  = ASSERT( isCoVar v )
    Subst in_scope ids tvs (extendVarEnv cvs v r)
241 242 243

-- | Add a substitution appropriate to the thing being substituted
--   (whether an expression, type, or coercion). See also
244
--   'extendIdSubst', 'extendTvSubst', 'extendCvSubst'
245 246 247
extendSubst :: Subst -> Var -> CoreArg -> Subst
extendSubst subst var arg
  = case arg of
248
      Type ty     -> ASSERT( isTyVar var ) extendTvSubst subst var ty
249 250 251 252 253
      Coercion co -> ASSERT( isCoVar var ) extendCvSubst subst var co
      _           -> ASSERT( isId    var ) extendIdSubst subst var arg

extendSubstWithVar :: Subst -> Var -> Var -> Subst
extendSubstWithVar subst v1 v2
254
  | isTyVar v1 = ASSERT( isTyVar v2 ) extendTvSubst subst v1 (mkTyVarTy v2)
255 256 257 258 259 260
  | isCoVar v1 = ASSERT( isCoVar v2 ) extendCvSubst subst v1 (mkCoVarCo v2)
  | otherwise  = ASSERT( isId    v2 ) extendIdSubst subst v1 (Var v2)

-- | Add a substitution as appropriate to each of the terms being
--   substituted (whether expressions, types, or coercions). See also
--   'extendSubst'.
batterseapower's avatar
batterseapower committed
261
extendSubstList :: Subst -> [(Var,CoreArg)] -> Subst
262
extendSubstList subst []              = subst
batterseapower's avatar
batterseapower committed
263 264 265
extendSubstList subst ((var,rhs):prs) = extendSubstList (extendSubst subst var rhs) prs

-- | Find the substitution for an 'Id' in the 'Subst'
266
lookupIdSubst :: SDoc -> Subst -> Id -> CoreExpr
267
lookupIdSubst doc (Subst in_scope ids _ _) v
268
  | not (isLocalId v) = Var v
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
269 270
  | Just e  <- lookupVarEnv ids       v = e
  | Just v' <- lookupInScope in_scope v = Var v'
271
        -- Vital! See Note [Extending the Subst]
272
  | otherwise = WARN( True, text "CoreSubst.lookupIdSubst" <+> doc <+> ppr v
273 274
                            $$ ppr in_scope)
                Var v
275

batterseapower's avatar
batterseapower committed
276
-- | Find the substitution for a 'TyVar' in the 'Subst'
277 278 279 280 281 282
lookupTCvSubst :: Subst -> TyVar -> Type
lookupTCvSubst (Subst _ _ tvs cvs) v
  | isTyVar v
  = lookupVarEnv tvs v `orElse` Type.mkTyVarTy v
  | otherwise
  = mkCoercionTy $ lookupVarEnv cvs v `orElse` mkCoVarCo v
283

284
delBndr :: Subst -> Var -> Subst
285 286 287 288
delBndr (Subst in_scope ids tvs cvs) v
  | isCoVar v = Subst in_scope ids tvs (delVarEnv cvs v)
  | isTyVar v = Subst in_scope ids (delVarEnv tvs v) cvs
  | otherwise = Subst in_scope (delVarEnv ids v) tvs cvs
289 290

delBndrs :: Subst -> [Var] -> Subst
291 292
delBndrs (Subst in_scope ids tvs cvs) vs
  = Subst in_scope (delVarEnvList ids vs) (delVarEnvList tvs vs) (delVarEnvList cvs vs)
293
      -- Easiest thing is just delete all from all!
294

295 296 297 298
-- | Simultaneously substitute for a bunch of variables
--   No left-right shadowing
--   ie the substitution for   (\x \y. e) a1 a2
--      so neither x nor y scope over a1 a2
299 300
mkOpenSubst :: InScopeSet -> [(Var,CoreArg)] -> Subst
mkOpenSubst in_scope pairs = Subst in_scope
301 302
                                   (mkVarEnv [(id,e)  | (id, e) <- pairs, isId id])
                                   (mkVarEnv [(tv,ty) | (tv, Type ty) <- pairs])
303
                                   (mkVarEnv [(v,co)  | (v, Coercion co) <- pairs])
304

305 306
------------------------------
isInScope :: Var -> Subst -> Bool
307
isInScope v (Subst in_scope _ _ _) = v `elemInScopeSet` in_scope
308

309 310 311
-- | Add the 'Var' to the in-scope set, but do not remove
-- any existing substitutions for it
addInScopeSet :: Subst -> VarSet -> Subst
312 313
addInScopeSet (Subst in_scope ids tvs cvs) vs
  = Subst (in_scope `extendInScopeSetSet` vs) ids tvs cvs
314 315 316

-- | Add the 'Var' to the in-scope set: as a side effect,
-- and remove any existing substitutions for it
317
extendInScope :: Subst -> Var -> Subst
318
extendInScope (Subst in_scope ids tvs cvs) v
319 320
  = Subst (in_scope `extendInScopeSet` v)
          (ids `delVarEnv` v) (tvs `delVarEnv` v) (cvs `delVarEnv` v)
321

batterseapower's avatar
batterseapower committed
322
-- | Add the 'Var's to the in-scope set: see also 'extendInScope'
323
extendInScopeList :: Subst -> [Var] -> Subst
324
extendInScopeList (Subst in_scope ids tvs cvs) vs
325 326
  = Subst (in_scope `extendInScopeSetList` vs)
          (ids `delVarEnvList` vs) (tvs `delVarEnvList` vs) (cvs `delVarEnvList` vs)
327

328
-- | Optimized version of 'extendInScopeList' that can be used if you are certain
329
-- all the things being added are 'Id's and hence none are 'TyVar's or 'CoVar's
330
extendInScopeIds :: Subst -> [Id] -> Subst
331 332 333
extendInScopeIds (Subst in_scope ids tvs cvs) vs
  = Subst (in_scope `extendInScopeSetList` vs)
          (ids `delVarEnvList` vs) tvs cvs
334 335

setInScope :: Subst -> InScopeSet -> Subst
336
setInScope (Subst _ ids tvs cvs) in_scope = Subst in_scope ids tvs cvs
337

Austin Seipp's avatar
Austin Seipp committed
338
-- Pretty printing, for debugging only
339 340

instance Outputable Subst where
341
  ppr (Subst in_scope ids tvs cvs)
342 343 344 345
        =  text "<InScope =" <+> braces (fsep (map ppr (varEnvElts (getInScopeVars in_scope))))
        $$ text " IdSubst   =" <+> ppr ids
        $$ text " TvSubst   =" <+> ppr tvs
        $$ text " CvSubst   =" <+> ppr cvs
346
         <> char '>'
347

Austin Seipp's avatar
Austin Seipp committed
348 349 350
{-
************************************************************************
*                                                                      *
351
        Substituting expressions
Austin Seipp's avatar
Austin Seipp committed
352 353 354
*                                                                      *
************************************************************************
-}
355

356
-- | Apply a substitution to an entire 'CoreExpr'. Remember, you may only
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
357
-- apply the substitution /once/: see "CoreSubst#apply_once"
358 359 360
--
-- Do *not* attempt to short-cut in the case of an empty substitution!
-- See Note [Extending the Subst]
361
substExprSC :: SDoc -> Subst -> CoreExpr -> CoreExpr
362
substExprSC doc subst orig_expr
363 364
  | isEmptySubst subst = orig_expr
  | otherwise          = -- pprTrace "enter subst-expr" (doc $$ ppr orig_expr) $
365
                         subst_expr doc subst orig_expr
366 367

substExpr :: SDoc -> Subst -> CoreExpr -> CoreExpr
368
substExpr doc subst orig_expr = subst_expr doc subst orig_expr
369

370 371
subst_expr :: SDoc -> Subst -> CoreExpr -> CoreExpr
subst_expr doc subst expr
372 373
  = go expr
  where
374
    go (Var v)         = lookupIdSubst (doc $$ text "subst_expr") subst v
375
    go (Type ty)       = Type (substTy subst ty)
376
    go (Coercion co)   = Coercion (substCo subst co)
377 378
    go (Lit lit)       = Lit lit
    go (App fun arg)   = App (go fun) (go arg)
Peter Wortmann's avatar
Peter Wortmann committed
379
    go (Tick tickish e) = mkTick (substTickish subst tickish) (go e)
380
    go (Cast e co)     = Cast (go e) (substCo subst co)
381 382 383 384 385
       -- Do not optimise even identity coercions
       -- Reason: substitution applies to the LHS of RULES, and
       --         if you "optimise" an identity coercion, you may
       --         lose a binder. We optimise the LHS of rules at
       --         construction time
386

387
    go (Lam bndr body) = Lam bndr' (subst_expr doc subst' body)
388 389
                       where
                         (subst', bndr') = substBndr subst bndr
390

391
    go (Let bind body) = Let bind' (subst_expr doc subst' body)
392 393
                       where
                         (subst', bind') = substBind subst bind
394 395

    go (Case scrut bndr ty alts) = Case (go scrut) bndr' (substTy subst ty) (map (go_alt subst') alts)
396 397
                                 where
                                 (subst', bndr') = substBndr subst bndr
398

399
    go_alt subst (con, bndrs, rhs) = (con, bndrs', subst_expr doc subst' rhs)
400 401
                                 where
                                   (subst', bndrs') = substBndrs subst bndrs
402

403 404
-- | Apply a substitution to an entire 'CoreBind', additionally returning an updated 'Subst'
-- that should be used by subsequent substitutions.
405 406
substBind, substBindSC :: Subst -> CoreBind -> (Subst, CoreBind)

407
substBindSC subst bind    -- Short-cut if the substitution is empty
408 409 410 411 412 413
  | not (isEmptySubst subst)
  = substBind subst bind
  | otherwise
  = case bind of
       NonRec bndr rhs -> (subst', NonRec bndr' rhs)
          where
414
            (subst', bndr') = substBndr subst bndr
415 416 417
       Rec pairs -> (subst', Rec (bndrs' `zip` rhss'))
          where
            (bndrs, rhss)    = unzip pairs
418
            (subst', bndrs') = substRecBndrs subst bndrs
419 420 421 422
            rhss' | isEmptySubst subst'
                  = rhss
                  | otherwise
                  = map (subst_expr (text "substBindSC") subst') rhss
423

424 425 426 427
substBind subst (NonRec bndr rhs)
  = (subst', NonRec bndr' (subst_expr (text "substBind") subst rhs))
  where
    (subst', bndr') = substBndr subst bndr
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
428

429 430 431 432 433 434
substBind subst (Rec pairs)
   = (subst', Rec (bndrs' `zip` rhss'))
   where
       (bndrs, rhss)    = unzip pairs
       (subst', bndrs') = substRecBndrs subst bndrs
       rhss' = map (subst_expr (text "substBind") subst') rhss
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
435

batterseapower's avatar
batterseapower committed
436
-- | De-shadowing the program is sometimes a useful pre-pass. It can be done simply
Gabor Greif's avatar
typos  
Gabor Greif committed
437
-- by running over the bindings with an empty substitution, because substitution
batterseapower's avatar
batterseapower committed
438 439
-- returns a result that has no-shadowing guaranteed.
--
440
-- (Actually, within a single /type/ there might still be shadowing, because
batterseapower's avatar
batterseapower committed
441
-- 'substTy' is a no-op for the empty substitution, but that's probably OK.)
442
--
443
-- [Aug 09] This function is not used in GHC at the moment, but seems so
444
--          short and simple that I'm going to leave it here
445
deShadowBinds :: CoreProgram -> CoreProgram
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
446
deShadowBinds binds = snd (mapAccumL substBind emptySubst binds)
447

Austin Seipp's avatar
Austin Seipp committed
448 449 450
{-
************************************************************************
*                                                                      *
451
        Substituting binders
Austin Seipp's avatar
Austin Seipp committed
452 453
*                                                                      *
************************************************************************
454 455 456 457 458

Remember that substBndr and friends are used when doing expression
substitution only.  Their only business is substitution, so they
preserve all IdInfo (suitably substituted).  For example, we *want* to
preserve occ info in rules.
Austin Seipp's avatar
Austin Seipp committed
459
-}
460

batterseapower's avatar
batterseapower committed
461
-- | Substitutes a 'Var' for another one according to the 'Subst' given, returning
462
-- the result and an updated 'Subst' that should be used by subsequent substitutions.
batterseapower's avatar
batterseapower committed
463
-- 'IdInfo' is preserved by this process, although it is substituted into appropriately.
464 465
substBndr :: Subst -> Var -> (Subst, Var)
substBndr subst bndr
466 467 468
  | isTyVar bndr  = substTyVarBndr subst bndr
  | isCoVar bndr  = substCoVarBndr subst bndr
  | otherwise     = substIdBndr (text "var-bndr") subst subst bndr
469

batterseapower's avatar
batterseapower committed
470
-- | Applies 'substBndr' to a number of 'Var's, accumulating a new 'Subst' left-to-right
471 472 473
substBndrs :: Subst -> [Var] -> (Subst, [Var])
substBndrs subst bndrs = mapAccumL substBndr subst bndrs

batterseapower's avatar
batterseapower committed
474
-- | Substitute in a mutually recursive group of 'Id's
475
substRecBndrs :: Subst -> [Id] -> (Subst, [Id])
476
substRecBndrs subst bndrs
477
  = (new_subst, new_bndrs)
478
  where         -- Here's the reason we need to pass rec_subst to subst_id
479
    (new_subst, new_bndrs) = mapAccumL (substIdBndr (text "rec-bndr") new_subst) subst bndrs
480

481 482 483 484 485
substIdBndr :: SDoc
            -> Subst            -- ^ Substitution to use for the IdInfo
            -> Subst -> Id      -- ^ Substitution and Id to transform
            -> (Subst, Id)      -- ^ Transformed pair
                                -- NB: unfolding may be zapped
486

487
substIdBndr _doc rec_subst subst@(Subst in_scope env tvs cvs) old_id
488
  = -- pprTrace "substIdBndr" (doc $$ ppr old_id $$ ppr in_scope) $
489
    (Subst (in_scope `extendInScopeSet` new_id) new_env tvs cvs, new_id)
490
  where
491
    id1 = uniqAway in_scope old_id      -- id1 is cloned if necessary
492
    id2 | no_type_change = id1
493
        | otherwise      = setIdType id1 (substTy subst old_ty)
494 495

    old_ty = idType old_id
496 497
    no_type_change = (isEmptyVarEnv tvs && isEmptyVarEnv cvs) ||
                     isEmptyVarSet (tyCoVarsOfType old_ty)
498

499 500 501
        -- new_id has the right IdInfo
        -- The lazy-set is because we're in a loop here, with
        -- rec_subst, when dealing with a mutually-recursive group
502
    new_id = maybeModifyIdInfo mb_new_info id2
503
    mb_new_info = substIdInfo rec_subst id2 (idInfo id2)
504
        -- NB: unfolding info may be zapped
505

506 507
        -- Extend the substitution if the unique has changed
        -- See the notes with substTyVarBndr for the delVarEnv
508
    new_env | no_change = delVarEnv env old_id
509
            | otherwise = extendVarEnv env old_id (Var new_id)
510

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
511
    no_change = id1 == old_id
512 513
        -- See Note [Extending the Subst]
        -- it's /not/ necessary to check mb_new_info and no_type_change
514

Austin Seipp's avatar
Austin Seipp committed
515
{-
516 517
Now a variant that unconditionally allocates a new unique.
It also unconditionally zaps the OccInfo.
Austin Seipp's avatar
Austin Seipp committed
518
-}
519

batterseapower's avatar
batterseapower committed
520
-- | Very similar to 'substBndr', but it always allocates a new 'Unique' for
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
521
-- each variable in its output.  It substitutes the IdInfo though.
522 523 524 525
cloneIdBndr :: Subst -> UniqSupply -> Id -> (Subst, Id)
cloneIdBndr subst us old_id
  = clone_id subst subst (old_id, uniqFromSupply us)

batterseapower's avatar
batterseapower committed
526 527
-- | Applies 'cloneIdBndr' to a number of 'Id's, accumulating a final
-- substitution from left to right
528 529 530 531
cloneIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneIdBndrs subst us ids
  = mapAccumL (clone_id subst) subst (ids `zip` uniqsFromSupply us)

532 533 534 535
cloneBndrs :: Subst -> UniqSupply -> [Var] -> (Subst, [Var])
-- Works for all kinds of variables (typically case binders)
-- not just Ids
cloneBndrs subst us vs
536 537 538 539
  = mapAccumL (\subst (v, u) -> cloneBndr subst u v) subst (vs `zip` uniqsFromSupply us)

cloneBndr :: Subst -> Unique -> Var -> (Subst, Var)
cloneBndr subst uniq v
Simon Peyton Jones's avatar
Simon Peyton Jones committed
540 541
  | isTyVar v = cloneTyVarBndr subst v uniq
  | otherwise = clone_id subst subst (v,uniq)  -- Works for coercion variables too
542

batterseapower's avatar
batterseapower committed
543
-- | Clone a mutually recursive group of 'Id's
544 545 546 547 548
cloneRecIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneRecIdBndrs subst us ids
  = (subst', ids')
  where
    (subst', ids') = mapAccumL (clone_id subst') subst
549
                               (ids `zip` uniqsFromSupply us)
550 551 552

-- Just like substIdBndr, except that it always makes a new unique
-- It is given the unique to use
553 554 555
clone_id    :: Subst                    -- Substitution for the IdInfo
            -> Subst -> (Id, Unique)    -- Substitution and Id to transform
            -> (Subst, Id)              -- Transformed pair
556

557 558
clone_id rec_subst subst@(Subst in_scope idvs tvs cvs) (old_id, uniq)
  = (Subst (in_scope `extendInScopeSet` new_id) new_idvs tvs new_cvs, new_id)
559
  where
560
    id1     = setVarUnique old_id uniq
561
    id2     = substIdType subst id1
562
    new_id  = maybeModifyIdInfo (substIdInfo rec_subst id2 (idInfo old_id)) id2
563 564
    (new_idvs, new_cvs) | isCoVar old_id = (idvs, extendVarEnv cvs old_id (mkCoVarCo new_id))
                        | otherwise      = (extendVarEnv idvs old_id (Var new_id), cvs)
565

Austin Seipp's avatar
Austin Seipp committed
566 567 568
{-
************************************************************************
*                                                                      *
569
                Types and Coercions
Austin Seipp's avatar
Austin Seipp committed
570 571
*                                                                      *
************************************************************************
572

573 574
For types and coercions we just call the corresponding functions in
Type and Coercion, but we have to repackage the substitution, from a
575
Subst to a TCvSubst.
Austin Seipp's avatar
Austin Seipp committed
576
-}
577 578

substTyVarBndr :: Subst -> TyVar -> (Subst, TyVar)
579
substTyVarBndr (Subst in_scope id_env tv_env cv_env) tv
580 581 582
  = case Type.substTyVarBndr (TCvSubst in_scope tv_env cv_env) tv of
        (TCvSubst in_scope' tv_env' cv_env', tv')
           -> (Subst in_scope' id_env tv_env' cv_env', tv')
583

584 585
cloneTyVarBndr :: Subst -> TyVar -> Unique -> (Subst, TyVar)
cloneTyVarBndr (Subst in_scope id_env tv_env cv_env) tv uniq
586 587 588
  = case Type.cloneTyVarBndr (TCvSubst in_scope tv_env cv_env) tv uniq of
        (TCvSubst in_scope' tv_env' cv_env', tv')
           -> (Subst in_scope' id_env tv_env' cv_env', tv')
589

590 591
substCoVarBndr :: Subst -> TyVar -> (Subst, TyVar)
substCoVarBndr (Subst in_scope id_env tv_env cv_env) cv
592 593
  = case Coercion.substCoVarBndr (TCvSubst in_scope tv_env cv_env) cv of
        (TCvSubst in_scope' tv_env' cv_env', cv')
594
           -> (Subst in_scope' id_env tv_env' cv_env', cv')
595

batterseapower's avatar
batterseapower committed
596
-- | See 'Type.substTy'
597
substTy :: Subst -> Type -> Type
598
substTy subst ty = Type.substTyUnchecked (getTCvSubst subst) ty
599

600 601
getTCvSubst :: Subst -> TCvSubst
getTCvSubst (Subst in_scope _ tenv cenv) = TCvSubst in_scope tenv cenv
602 603 604

-- | See 'Coercion.substCo'
substCo :: Subst -> Coercion -> Coercion
605
substCo subst co = Coercion.substCo (getTCvSubst subst) co
606

Austin Seipp's avatar
Austin Seipp committed
607 608 609
{-
************************************************************************
*                                                                      *
610
\section{IdInfo substitution}
Austin Seipp's avatar
Austin Seipp committed
611 612 613
*                                                                      *
************************************************************************
-}
614 615

substIdType :: Subst -> Id -> Id
616
substIdType subst@(Subst _ _ tv_env cv_env) id
617
  | (isEmptyVarEnv tv_env && isEmptyVarEnv cv_env) || isEmptyVarSet (tyCoVarsOfType old_ty) = id
618
  | otherwise   = setIdType id (substTy subst old_ty)
619
                -- The tyCoVarsOfType is cheaper than it looks
620 621
                -- because we cache the free tyvars of the type
                -- in a Note in the id's type itself
622 623 624 625
  where
    old_ty = idType id

------------------
batterseapower's avatar
batterseapower committed
626 627
-- | Substitute into some 'IdInfo' with regard to the supplied new 'Id'.
substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo
628
substIdInfo subst new_id info
629
  | nothing_to_do = Nothing
630
  | otherwise     = Just (info `setRuleInfo`      substSpec subst new_id old_rules
631
                               `setUnfoldingInfo` substUnfolding subst old_unf)
632
  where
633
    old_rules     = ruleInfo info
634
    old_unf       = unfoldingInfo info
635
    nothing_to_do = isEmptyRuleInfo old_rules && isClosedUnfolding old_unf
636

637 638

------------------
639
-- | Substitutes for the 'Id's within an unfolding
640
substUnfolding, substUnfoldingSC :: Subst -> Unfolding -> Unfolding
641 642
        -- Seq'ing on the returned Unfolding is enough to cause
        -- all the substitutions to happen completely
643

644
substUnfoldingSC subst unf       -- Short-cut version
645 646 647
  | isEmptySubst subst = unf
  | otherwise          = substUnfolding subst unf

648 649
substUnfolding subst df@(DFunUnfolding { df_bndrs = bndrs, df_args = args })
  = df { df_bndrs = bndrs', df_args = args' }
650
  where
651 652
    (subst',bndrs') = substBndrs subst bndrs
    args'           = map (substExpr (text "subst-unf:dfun") subst') args
653

654
substUnfolding subst unf@(CoreUnfolding { uf_tmpl = tmpl, uf_src = src })
655
        -- Retain an InlineRule!
656
  | not (isStableSource src)  -- Zap an unstable unfolding, to save substitution work
657
  = NoUnfolding
658
  | otherwise                 -- But keep a stable one!
659 660
  = seqExpr new_tmpl `seq`
    unf { uf_tmpl = new_tmpl }
661
  where
662
    new_tmpl = substExpr (text "subst-unf") subst tmpl
663

664
substUnfolding _ unf = unf      -- NoUnfolding, OtherCon
665 666 667 668

------------------
substIdOcc :: Subst -> Id -> Id
-- These Ids should not be substituted to non-Ids
669
substIdOcc subst v = case lookupIdSubst (text "substIdOcc") subst v of
670 671
                        Var v' -> v'
                        other  -> pprPanic "substIdOcc" (vcat [ppr v <+> ppr other, ppr subst])
672 673

------------------
batterseapower's avatar
batterseapower committed
674
-- | Substitutes for the 'Id's within the 'WorkerInfo' given the new function 'Id'
675 676 677
substSpec :: Subst -> Id -> RuleInfo -> RuleInfo
substSpec subst new_id (RuleInfo rules rhs_fvs)
  = seqRuleInfo new_spec `seq` new_spec
678
  where
679
    subst_ru_fn = const (idName new_id)
680
    new_spec = RuleInfo (map (substRule subst subst_ru_fn) rules)
681
                        (substDVarSet subst rhs_fvs)
682 683 684

------------------
substRulesForImportedIds :: Subst -> [CoreRule] -> [CoreRule]
685
substRulesForImportedIds subst rules
686 687 688
  = map (substRule subst not_needed) rules
  where
    not_needed name = pprPanic "substRulesForImportedIds" (ppr name)
689 690 691 692 693 694 695 696

------------------
substRule :: Subst -> (Name -> Name) -> CoreRule -> CoreRule

-- The subst_ru_fn argument is applied to substitute the ru_fn field
-- of the rule:
--    - Rules for *imported* Ids never change ru_fn
--    - Rules for *local* Ids are in the IdInfo for that Id,
697
--      and the ru_fn field is simply replaced by the new name
698
--      of the Id
699 700
substRule _ _ rule@(BuiltinRule {}) = rule
substRule subst subst_ru_fn rule@(Rule { ru_bndrs = bndrs, ru_args = args
701 702
                                       , ru_fn = fn_name, ru_rhs = rhs
                                       , ru_local = is_local })
703 704
  = rule { ru_bndrs = bndrs'
         , ru_fn    = if is_local
705
                        then subst_ru_fn fn_name
706 707 708 709 710
                        else fn_name
         , ru_args  = map (substExpr doc subst') args
         , ru_rhs   = substExpr (text "foo") subst' rhs }
           -- Do NOT optimise the RHS (previously we did simplOptExpr here)
           -- See Note [Substitute lazily]
711
  where
712
    doc = text "subst-rule" <+> ppr fn_name
713
    (subst', bndrs') = substBndrs subst bndrs
714

715 716 717 718 719 720
------------------
substVects :: Subst -> [CoreVect] -> [CoreVect]
substVects subst = map (substVect subst)

------------------
substVect :: Subst -> CoreVect -> CoreVect
721 722 723 724 725
substVect subst  (Vect v rhs)        = Vect v (simpleOptExprWith subst rhs)
substVect _subst vd@(NoVect _)       = vd
substVect _subst vd@(VectType _ _ _) = vd
substVect _subst vd@(VectClass _)    = vd
substVect _subst vd@(VectInst _)     = vd
726

727
------------------
728 729 730
substDVarSet :: Subst -> DVarSet -> DVarSet
substDVarSet subst fvs
  = mkDVarSet $ fst $ foldr (subst_fv subst) ([], emptyVarSet) $ dVarSetElems fvs
731
  where
732 733
  subst_fv subst fv acc
     | isId fv = expr_fvs (lookupIdSubst (text "substDVarSet") subst fv) isLocalVar emptyVarSet $! acc
734
     | otherwise = tyCoVarsOfTypeAcc (lookupTCvSubst subst fv) (const True) emptyVarSet $! acc
735 736 737

------------------
substTickish :: Subst -> Tickish Id -> Tickish Id
738 739 740 741
substTickish subst (Breakpoint n ids)
   = Breakpoint n (map do_one ids)
 where
    do_one = getIdFromTrivialExpr . lookupIdSubst (text "subst_tickish") subst
742 743
substTickish _subst other = other

744 745 746 747
{- Note [Substitute lazily]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The functions that substitute over IdInfo must be pretty lazy, becuause
they are knot-tied by substRecBndrs.
748

749 750 751 752