X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FcoreSyn%2FCoreTidy.lhs;h=44437747eb1328894bf3798a4acffe7f4b7472c8;hb=e030a747f89512121e582d302986ff6732ebb0c6;hp=892cb26ba7e6c5e7008bbea1732a94434e5c8e7e;hpb=4161ba13916463f8e67259498eacf22744160e1f;p=ghc-hetmet.git diff --git a/ghc/compiler/coreSyn/CoreTidy.lhs b/ghc/compiler/coreSyn/CoreTidy.lhs index 892cb26..4443774 100644 --- a/ghc/compiler/coreSyn/CoreTidy.lhs +++ b/ghc/compiler/coreSyn/CoreTidy.lhs @@ -1,266 +1,185 @@ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 +% (c) The AQUA Project, Glasgow University, 1996-1998 % -\section{Tidying up Core} +%************************************************************************ +%* * +\section[PprCore]{Printing of Core syntax, including for interfaces} +%* * +%************************************************************************ \begin{code} module CoreTidy ( - tidyCorePgm, tidyExpr, - tidyBndr, tidyBndrs + tidyBind, tidyExpr, + tidyBndr, tidyBndrs, tidyVarOcc, + tidyIdRules, pprTidyIdRules ) where #include "HsVersions.h" -import CmdLineOpts ( DynFlags, DynFlag(..), opt_UsageSPOn, dopt ) import CoreSyn -import CoreUnfold ( noUnfolding ) -import CoreLint ( showPass, endPass ) -import UsageSPInf ( doUsageSPInf ) +import CoreUtils ( exprArity ) +import PprCore ( pprIdRules ) +import Id ( Id, mkUserLocal, idInfo, setIdInfo, idUnique, idType, idCoreRules ) +import IdInfo ( vanillaIdInfo, setArityInfo, + newStrictnessInfo, setAllStrictnessInfo, + newDemandInfo, setNewDemandInfo ) +import Type ( tidyType, tidyTyVarBndr ) +import Var ( Var ) import VarEnv -import VarSet -import Var ( Id, Var ) -import Id ( idType, idInfo, idName, isExportedId, - mkVanillaId, mkId, exportWithOrigOccName, - idStrictness, setIdStrictness, - idDemandInfo, setIdDemandInfo, - ) -import IdInfo ( specInfo, setSpecInfo, - setUnfoldingInfo, setDemandInfo, - workerInfo, setWorkerInfo, WorkerInfo(..) - ) -import Demand ( wwLazy ) -import Name ( getOccName, tidyTopName, mkLocalName ) -import OccName ( initTidyOccEnv, tidyOccName ) -import Type ( tidyTopType, tidyType, tidyTyVar ) -import Module ( Module ) -import UniqSupply ( mkSplitUniqSupply ) -import Unique ( Uniquable(..) ) -import ErrUtils ( showPass ) +import Name ( getOccName ) +import OccName ( tidyOccName ) import SrcLoc ( noSrcLoc ) +import Maybes ( orElse ) +import Outputable import Util ( mapAccumL ) \end{code} - %************************************************************************ %* * -\subsection{Tidying core} +\subsection{Tidying expressions, rules} %* * %************************************************************************ -Several tasks are done by @tidyCorePgm@ +\begin{code} +tidyBind :: TidyEnv + -> CoreBind + -> (TidyEnv, CoreBind) -1. If @opt_UsageSPOn@ then compute usage information (which is - needed by Core2Stg). ** NOTE _scc_ HERE ** - Do this first, because it may introduce new binders. +tidyBind env (NonRec bndr rhs) + = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') -> + (env', NonRec bndr' (tidyExpr env' rhs)) -2. Make certain top-level bindings into Globals. The point is that - Global things get externally-visible labels at code generation - time +tidyBind env (Rec prs) + = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') -> + map (tidyExpr env') (map snd prs) =: \ rhss' -> + (env', Rec (zip bndrs' rhss')) -3. 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.] +------------ Expressions -------------- +tidyCoreExpr :: CoreExpr -> IO CoreExpr +tidyCoreExpr expr = return (tidyExpr emptyTidyEnv expr) -\begin{code} -tidyCorePgm :: DynFlags -> Module - -> [CoreBind] -> [IdCoreRule] - -> IO ([CoreBind], [IdCoreRule]) -tidyCorePgm dflags module_name binds_in orphans_in - = do - us <- mkSplitUniqSupply 'u' +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) - showPass dflags "Tidy Core" +tidyExpr env (Let b e) + = tidyBind env b =: \ (env', b') -> + Let b' (tidyExpr env' e) - binds_in1 <- if opt_UsageSPOn - then _scc_ "CoreUsageSPInf" - doUsageSPInf dflags us binds_in - else return binds_in +tidyExpr env (Case e b alts) + = tidyBndr env b =: \ (env', b) -> + Case (tidyExpr env e) b (map (tidyAlt env') alts) - let (tidy_env1, binds_out) = mapAccumL (tidyBind (Just module_name)) - init_tidy_env binds_in1 - orphans_out = tidyIdRules tidy_env1 orphans_in +tidyExpr env (Lam b e) + = tidyBndr env b =: \ (env', b) -> + Lam b (tidyExpr env' e) - endPass dflags "Tidy Core" (dopt Opt_D_dump_simpl dflags || - dopt Opt_D_verbose_core2core dflags) - binds_out - - return (binds_out, orphans_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, - exportWithOrigOccName bndr] - -tidyBind :: Maybe Module -- (Just m) for top level, Nothing for nested - -> TidyEnv - -> CoreBind - -> (TidyEnv, CoreBind) -tidyBind maybe_mod env (NonRec bndr rhs) - = let - (env', bndr') = tidy_bndr maybe_mod env' env bndr - rhs' = tidyExpr env' rhs - -- We use env' when tidying the RHS even though it's not - -- strictly necessary; it makes the code pretty hard to read - -- if we don't! - 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') env bndrs - rhss' = map (tidyExpr env') rhss - in - (env', Rec (zip bndrs' rhss')) - -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) = 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 +------------ Case alternatives -------------- +tidyAlt env (con, vs, rhs) + = tidyBndrs env vs =: \ (env', vs) -> + (con, vs, tidyExpr env' rhs) +------------ 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} -\begin{code} -tidy_bndr (Just mod) env_idinfo env var = tidyTopId mod env env_idinfo var -tidy_bndr Nothing env_idinfo env var = tidyBndr env var -\end{code} +------------ Rules -------------- +tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule] +tidyIdRules env [] = [] +tidyIdRules env ((fn,rule) : rules) + = tidyRule env rule =: \ rule -> + tidyIdRules env rules =: \ rules -> + ((tidyVarOcc env fn, rule) : rules) +tidyRule :: TidyEnv -> CoreRule -> CoreRule +tidyRule env rule@(BuiltinRule _ _) = rule +tidyRule env (Rule name act vars tpl_args rhs) + = tidyBndrs env vars =: \ (env', vars) -> + map (tidyExpr env') tpl_args =: \ tpl_args -> + (Rule name act vars tpl_args (tidyExpr env' rhs)) + +pprTidyIdRules :: Id -> SDoc +pprTidyIdRules id = pprIdRules (tidyIdRules emptyTidyEnv (idCoreRules id)) +\end{code} %************************************************************************ %* * -\subsection{Tidying up a binder} +\subsection{Tidying non-top-level binders} %* * %************************************************************************ \begin{code} +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 = tidyTyVar env var - | otherwise = tidyId env 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 none, + -- which should save some space. + -- But note that tidyLetBndr puts some of it back. ty' = tidyType env (idType id) - id' = mkVanillaId name' ty' - `setIdStrictness` idStrictness id - `setIdDemandInfo` idDemandInfo 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 var_env' = extendVarEnv var_env id id' in - ((tidy_env', var_env'), id') - -tidyTopId :: Module -> TidyEnv -> TidyEnv -> Id -> (TidyEnv, Id) - -- The second env is the one to use for the IdInfo - -- It's necessary because when we are dealing with a recursive - -- group, a variable late in the group might be mentioned - -- in the IdInfo of one early in the group -tidyTopId mod env@(tidy_env, var_env) env_idinfo id - = -- Top level variables - let - (tidy_env', name') = tidyTopName mod tidy_env (isExportedId id) (idName id) - ty' = tidyTopType (idType id) - idinfo' = tidyIdInfo env_idinfo (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 and worker info (if any) --- b) zap the unfolding and demand info --- The latter two are to avoid space leaks - -tidyIdInfo env info - = info5 - where - rules = specInfo info - - info2 | isEmptyCoreRules rules = info - | otherwise = info `setSpecInfo` tidyRules env rules - - info3 = info2 `setUnfoldingInfo` noUnfolding - info4 = info3 `setDemandInfo` wwLazy +m =: k = m `seq` k m +\end{code} - info5 = case workerInfo info of - NoWorker -> info4 - HasWorker w a -> info4 `setWorkerInfo` HasWorker (tidyVarOcc env w) a -tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule] -tidyIdRules env rules - = [ (tidyVarOcc env fn, tidyRule env rule) | (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@(BuiltinRule _) = rule -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 -\end{code}