%
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
+% (c) The AQUA Project, Glasgow University, 1996-1998
%
-\section{Tidying up Core}
\begin{code}
module CoreTidy (
- tidyCorePgm, tidyExpr,
- tidyBndr, tidyBndrs
+ tidyExpr, tidyVarOcc, tidyRule, tidyRules
) where
#include "HsVersions.h"
-import CmdLineOpts ( opt_D_dump_simpl, opt_D_verbose_core2core, opt_UsageSPOn )
import CoreSyn
-import CoreUnfold ( noUnfolding )
-import CoreLint ( beginPass, endPass )
-import Rules ( ProtoCoreRule(..) )
+import CoreUtils ( exprArity )
+import Unify ( coreRefineTys )
+import DataCon ( DataCon, isVanillaDataCon )
+import Id ( Id, mkUserLocal, idInfo, setIdInfo, idUnique,
+ idType, setIdType )
+import IdInfo ( setArityInfo, vanillaIdInfo,
+ newStrictnessInfo, setAllStrictnessInfo,
+ newDemandInfo, setNewDemandInfo )
+import Type ( Type, tidyType, tidyTyVarBndr, substTy, mkOpenTvSubst )
+import Var ( Var, TyVar, varName )
import VarEnv
-import VarSet
-import Var ( Id, IdOrTyVar )
-import Id ( idType, idInfo, idName,
- mkVanillaId, mkId, isUserExportedId,
- getIdStrictness, setIdStrictness,
- getIdDemandInfo, setIdDemandInfo,
- )
-import IdInfo ( specInfo, setSpecInfo,
- inlinePragInfo, setInlinePragInfo, InlinePragInfo(..),
- setUnfoldingInfo, setDemandInfo
- )
-import Demand ( wwLazy )
-import Name ( getOccName, tidyTopName, mkLocalName, isLocallyDefined )
-import OccName ( initTidyOccEnv, tidyOccName )
-import Type ( tidyTopType, tidyType, tidyTypes, tidyTyVar, tidyTyVars )
-import Class ( Class, classSelIds )
-import Module ( Module )
-import UniqSupply ( UniqSupply )
-import Unique ( Uniquable(..) )
+import UniqFM ( lookupUFM )
+import Name ( Name, getOccName )
+import OccName ( tidyOccName )
import SrcLoc ( noSrcLoc )
-import Util ( mapAccumL )
+import Maybes ( orElse )
import Outputable
-
-doUsageSPInf = panic "doUsageSpInf"
+import Util ( mapAccumL )
\end{code}
+This module contains "tidying" code for *nested* expressions, bindings, rules.
+The code for *top-level* bindings is in TidyPgm.
%************************************************************************
%* *
-\subsection{Tidying core}
+\subsection{Tidying expressions, rules}
%* *
%************************************************************************
-Several tasks are done by @tidyCorePgm@
-
-1. Make certain top-level bindings into Globals. The point is that
- Global things get externally-visible labels at code generation
- time
-
-
-2. Give all binders a nice print-name. Their uniques aren't changed;
- rather we give them lexically unique occ-names, so that we can
- safely print the OccNae only in the interface file. [Bad idea to
- change the uniques, because the code generator makes global labels
- from the uniques for local thunks etc.]
-
-
-3. If @opt_UsageSPOn@ then compute usage information (which is
- needed by Core2Stg). ** NOTE _scc_ HERE **
-
\begin{code}
-tidyCorePgm :: UniqSupply -> Module -> [CoreBind] -> [ProtoCoreRule]
- -> IO ([CoreBind], [ProtoCoreRule])
-tidyCorePgm us module_name binds_in rules
- = do
- beginPass "Tidy Core"
-
- let (tidy_env1, binds_tidy) = mapAccumL (tidyBind (Just module_name)) init_tidy_env binds_in
- rules_out = tidyProtoRules tidy_env1 rules
-
- binds_out <- if opt_UsageSPOn
- then _scc_ "CoreUsageSPInf" doUsageSPInf us binds_tidy
- else return binds_tidy
-
- endPass "Tidy Core" (opt_D_dump_simpl || opt_D_verbose_core2core) binds_out
- return (binds_out, rules_out)
- where
- -- We also make sure to avoid any exported binders. Consider
- -- f{-u1-} = 1 -- Local decl
- -- ...
- -- f{-u2-} = 2 -- Exported decl
- --
- -- The second exported decl must 'get' the name 'f', so we
- -- have to put 'f' in the avoids list before we get to the first
- -- decl. tidyTopId then does a no-op on exported binders.
- init_tidy_env = (initTidyOccEnv avoids, emptyVarEnv)
- avoids = [getOccName bndr | bndr <- bindersOfBinds binds_in,
- isUserExportedId bndr]
-
-tidyBind :: Maybe Module -- (Just m) for top level, Nothing for nested
- -> TidyEnv
+tidyBind :: TidyEnv
-> CoreBind
- -> (TidyEnv, CoreBind)
-tidyBind maybe_mod env (NonRec bndr rhs)
- = let
- (env', bndr') = tidy_bndr maybe_mod env bndr
- rhs' = tidyExpr env rhs
+ -> (TidyEnv, CoreBind)
+
+tidyBind env (NonRec bndr rhs)
+ = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') ->
+ (env', NonRec bndr' (tidyExpr env' rhs))
+
+tidyBind env (Rec prs)
+ = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') ->
+ map (tidyExpr env') (map snd prs) =: \ rhss' ->
+ (env', Rec (zip bndrs' rhss'))
+
+
+------------ Expressions --------------
+tidyExpr :: TidyEnv -> CoreExpr -> CoreExpr
+tidyExpr env (Var v) = Var (tidyVarOcc env v)
+tidyExpr env (Type ty) = Type (tidyType env ty)
+tidyExpr env (Lit lit) = Lit lit
+tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
+tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
+
+tidyExpr env (Let b e)
+ = tidyBind env b =: \ (env', b') ->
+ Let b' (tidyExpr env' e)
+
+tidyExpr env (Case e b ty alts)
+ = tidyBndr env b =: \ (env', b) ->
+ Case (tidyExpr env e) b (tidyType env ty)
+ (map (tidyAlt b env') alts)
+
+tidyExpr env (Lam b e)
+ = tidyBndr env b =: \ (env', b) ->
+ Lam b (tidyExpr env' e)
+
+------------ Case alternatives --------------
+tidyAlt case_bndr env (DataAlt con, vs, rhs)
+ | not (isVanillaDataCon con) -- GADT case
+ = tidyBndrs env tvs =: \ (env1, tvs') ->
+ let
+ env2 = refineTidyEnv env con tvs' scrut_ty
in
- (env', NonRec bndr' rhs')
-
-tidyBind maybe_mod env (Rec pairs)
- = let
- -- We use env' when tidying the rhss
- -- When tidying the binder itself we may tidy it's
- -- specialisations; if any of these mention other binders
- -- in the group we should really feed env' to them too;
- -- but that seems (a) unlikely and (b) a bit tiresome.
- -- So I left it out for now
-
- (bndrs, rhss) = unzip pairs
- (env', bndrs') = mapAccumL (tidy_bndr maybe_mod) env bndrs
- rhss' = map (tidyExpr env') rhss
- in
- (env', Rec (zip bndrs' rhss'))
-
-tidyExpr env (Type ty) = Type (tidyType env ty)
-tidyExpr env (Con con args) = Con con (map (tidyExpr env) args)
-tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
-tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
-
-tidyExpr env (Let b e) = Let b' (tidyExpr env' e)
- where
- (env', b') = tidyBind Nothing env b
-
-tidyExpr env (Case e b alts) = Case (tidyExpr env e) b' (map (tidyAlt env') alts)
- where
- (env', b') = tidyBndr env b
-
-tidyExpr env (Var v) = Var (tidyVarOcc env v)
-
-tidyExpr env (Lam b e) = Lam b' (tidyExpr env' e)
- where
- (env', b') = tidyBndr env b
-
-tidyAlt env (con, vs, rhs) = (con, vs', tidyExpr env' rhs)
- where
- (env', vs') = tidyBndrs env vs
+ tidyBndrs env2 ids =: \ (env3, ids') ->
+ (DataAlt con, tvs' ++ ids', tidyExpr env3 rhs)
+ where
+ (tvs, ids) = span isTyVar vs
+ scrut_ty = idType case_bndr
+
+tidyAlt case_bndr env (con, vs, rhs)
+ = tidyBndrs env vs =: \ (env', vs) ->
+ (con, vs, tidyExpr env' rhs)
+
+refineTidyEnv :: TidyEnv -> DataCon -> [TyVar] -> Type -> TidyEnv
+-- Refine the TidyEnv in the light of the type refinement from coreRefineTys
+refineTidyEnv tidy_env@(occ_env, var_env) con tvs scrut_ty
+ = case coreRefineTys con tvs scrut_ty of
+ Nothing -> tidy_env
+ Just (tv_subst, all_bound_here)
+ | all_bound_here -- Local type refinement only
+ -> tidy_env
+ | otherwise -- Apply the refining subst to the tidy env
+ -- This ensures that occurences have the most refined type
+ -- And that means that exprType will work right everywhere
+ -> (occ_env, mapVarEnv (refine subst) var_env)
+ where
+ subst = mkOpenTvSubst tv_subst
+ where
+ refine subst var | isId var = setIdType var (substTy subst (idType var))
+ | otherwise = var
+------------ Notes --------------
tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
-
tidyNote env note = note
-tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
- Just v' -> v'
- Nothing -> v
-\end{code}
+------------ Rules --------------
+tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule]
+tidyRules env [] = []
+tidyRules env (rule : rules)
+ = tidyRule env rule =: \ rule ->
+ tidyRules env rules =: \ rules ->
+ (rule : rules)
-\begin{code}
-tidy_bndr (Just mod) env id = tidyTopId mod env id
-tidy_bndr Nothing env var = tidyBndr env var
+tidyRule :: TidyEnv -> CoreRule -> CoreRule
+tidyRule env rule@(BuiltinRule {}) = rule
+tidyRule env rule@(Rule { ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs,
+ ru_fn = fn, ru_rough = mb_ns })
+ = tidyBndrs env bndrs =: \ (env', bndrs) ->
+ map (tidyExpr env') args =: \ args ->
+ rule { ru_bndrs = bndrs, ru_args = args,
+ ru_rhs = tidyExpr env' rhs,
+ ru_fn = tidyNameOcc env fn,
+ ru_rough = map (fmap (tidyNameOcc env')) mb_ns }
\end{code}
-
%************************************************************************
%* *
-\subsection{Tidying up a binder}
+\subsection{Tidying non-top-level binders}
%* *
%************************************************************************
\begin{code}
-tidyBndr :: TidyEnv -> IdOrTyVar -> (TidyEnv, IdOrTyVar)
-tidyBndr env var | isTyVar var = tidyTyVar env var
- | otherwise = tidyId env var
-
-tidyBndrs :: TidyEnv -> [IdOrTyVar] -> (TidyEnv, [IdOrTyVar])
+tidyNameOcc :: TidyEnv -> Name -> Name
+-- In rules and instances, we have Names, and we must tidy them too
+-- Fortunately, we can lookup in the VarEnv with a name
+tidyNameOcc (_, var_env) n = case lookupUFM var_env n of
+ Nothing -> n
+ Just v -> varName v
+
+tidyVarOcc :: TidyEnv -> Var -> Var
+tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v
+
+-- tidyBndr is used for lambda and case binders
+tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
+tidyBndr env var
+ | isTyVar var = tidyTyVarBndr env var
+ | otherwise = tidyIdBndr env var
+
+tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
tidyBndrs env vars = mapAccumL tidyBndr env vars
-tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
-tidyId env@(tidy_env, var_env) id
- = -- Non-top-level variables
+tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
+-- Used for local (non-top-level) let(rec)s
+tidyLetBndr env (id,rhs)
+ = ((tidy_env,new_var_env), final_id)
+ where
+ ((tidy_env,var_env), new_id) = tidyIdBndr env id
+
+ -- We need to keep around any interesting strictness and
+ -- demand info because later on we may need to use it when
+ -- converting to A-normal form.
+ -- eg.
+ -- f (g x), where f is strict in its argument, will be converted
+ -- into case (g x) of z -> f z by CorePrep, but only if f still
+ -- has its strictness info.
+ --
+ -- Similarly for the demand info - on a let binder, this tells
+ -- CorePrep to turn the let into a case.
+ --
+ -- Similarly arity info for eta expansion in CorePrep
+ --
+ final_id = new_id `setIdInfo` new_info
+ idinfo = idInfo id
+ new_info = vanillaIdInfo
+ `setArityInfo` exprArity rhs
+ `setAllStrictnessInfo` newStrictnessInfo idinfo
+ `setNewDemandInfo` newDemandInfo idinfo
+
+ -- Override the env we get back from tidyId with the new IdInfo
+ -- so it gets propagated to the usage sites.
+ new_var_env = extendVarEnv var_env id final_id
+
+-- Non-top-level variables
+tidyIdBndr :: TidyEnv -> Id -> (TidyEnv, Id)
+tidyIdBndr env@(tidy_env, var_env) id
+ = -- do this pattern match strictly, otherwise we end up holding on to
+ -- stuff in the OccName.
+ case tidyOccName tidy_env (getOccName id) of { (tidy_env', occ') ->
let
-- Give the Id a fresh print-name, *and* rename its type
- -- The SrcLoc isn't important now, though we could extract it from the Id
- name' = mkLocalName (getUnique id) occ' noSrcLoc
- (tidy_env', occ') = tidyOccName tidy_env (getOccName id)
+ -- The SrcLoc isn't important now,
+ -- though we could extract it from the Id
+ --
+ -- All nested Ids now have the same IdInfo, namely vanillaIdInfo,
+ -- which should save some space.
+ -- But note that tidyLetBndr puts some of it back.
ty' = tidyType env (idType id)
- id' = mkVanillaId name' ty'
- `setIdStrictness` getIdStrictness id
- `setIdDemandInfo` getIdDemandInfo id
- -- NB: This throws away the IdInfo of the Id, which we
- -- no longer need. That means we don't need to
- -- run over it with env, nor renumber it.
- --
- -- The exception is strictness and demand info, which
- -- is used to decide whether to use let or case for
- -- function arguments and let bindings
-
+ id' = mkUserLocal occ' (idUnique id) ty' noSrcLoc
+ `setIdInfo` vanillaIdInfo
var_env' = extendVarEnv var_env id id'
in
- ((tidy_env', var_env'), id')
-
-tidyTopId :: Module -> TidyEnv -> Id -> (TidyEnv, Id)
-tidyTopId mod env@(tidy_env, var_env) id
- = -- Top level variables
- let
- (tidy_env', name') | isUserExportedId id = (tidy_env, idName id)
- | otherwise = tidyTopName mod tidy_env (idName id)
- ty' = tidyTopType (idType id)
- idinfo' = tidyIdInfo env (idInfo id)
- id' = mkId name' ty' idinfo'
- var_env' = extendVarEnv var_env id id'
- in
- ((tidy_env', var_env'), id')
+ ((tidy_env', var_env'), id')
+ }
\end{code}
\begin{code}
--- tidyIdInfo does these things:
--- a) tidy the specialisation info (if any)
--- b) zap a complicated ICanSafelyBeINLINEd pragma,
--- c) zap the unfolding
--- The latter two are to avoid space leaks
-
-tidyIdInfo env info
- = info4
- where
- rules = specInfo info
-
- info1 | isEmptyCoreRules rules = info
- | otherwise = info `setSpecInfo` tidyRules env rules
-
- info2 = case inlinePragInfo info of
- ICanSafelyBeINLINEd _ _ -> info1 `setInlinePragInfo` NoInlinePragInfo
- other -> info1
-
- info3 = info2 `setUnfoldingInfo` noUnfolding
- info4 = info3 `setDemandInfo` wwLazy -- I don't understand why...
-
-tidyProtoRules :: TidyEnv -> [ProtoCoreRule] -> [ProtoCoreRule]
-tidyProtoRules env rules
- = [ ProtoCoreRule is_local (tidyVarOcc env fn) (tidyRule env rule)
- | ProtoCoreRule is_local fn rule <- rules
- ]
-
-tidyRules :: TidyEnv -> CoreRules -> CoreRules
-tidyRules env (Rules rules fvs)
- = Rules (map (tidyRule env) rules)
- (foldVarSet tidy_set_elem emptyVarSet fvs)
- where
- tidy_set_elem var new_set = extendVarSet new_set (tidyVarOcc env var)
-
-tidyRule :: TidyEnv -> CoreRule -> CoreRule
-tidyRule env (Rule name vars tpl_args rhs)
- = (Rule name vars' (map (tidyExpr env') tpl_args) (tidyExpr env' rhs))
- where
- (env', vars') = tidyBndrs env vars
+m =: k = m `seq` k m
\end{code}