X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2FcoreSyn%2FCoreTidy.lhs;h=377bfd8c84cf9dabee556fb58bda0076922ded08;hp=6f13740b9c4c9f64cf4cb0850d3e829f1870336f;hb=febf1ced754a3996ac1a5877dcded87828560d1c;hpb=a5168e30f331c6fe912cca4f53be8544ce6800d5 diff --git a/compiler/coreSyn/CoreTidy.lhs b/compiler/coreSyn/CoreTidy.lhs index 6f13740..377bfd8 100644 --- a/compiler/coreSyn/CoreTidy.lhs +++ b/compiler/coreSyn/CoreTidy.lhs @@ -1,39 +1,34 @@ % +% (c) The University of Glasgow 2006 % (c) The AQUA Project, Glasgow University, 1996-1998 % +This module contains "tidying" code for *nested* expressions, bindings, rules. +The code for *top-level* bindings is in TidyPgm. + \begin{code} module CoreTidy ( - tidyExpr, tidyVarOcc, tidyRule, tidyRules + tidyExpr, tidyVarOcc, tidyRule, tidyRules, tidyUnfolding ) where #include "HsVersions.h" import CoreSyn -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 CoreArity +import Id +import IdInfo +import TcType( tidyType, tidyCo, tidyTyVarBndr ) +import Var import VarEnv -import UniqFM ( lookupUFM ) -import Name ( Name, getOccName ) -import OccName ( tidyOccName ) -import SrcLoc ( noSrcLoc ) -import Maybes ( orElse ) +import UniqFM +import Name hiding (tidyNameOcc) +import SrcLoc +import Maybes +import Data.List import Outputable -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 expressions, rules} @@ -46,22 +41,26 @@ tidyBind :: TidyEnv -> (TidyEnv, CoreBind) tidyBind env (NonRec bndr rhs) - = tidyLetBndr env (bndr,rhs) =: \ (env', bndr') -> + = tidyLetBndr env env (bndr,rhs) =: \ (env', bndr') -> (env', NonRec bndr' (tidyExpr env' rhs)) tidyBind env (Rec prs) - = mapAccumL tidyLetBndr env prs =: \ (env', bndrs') -> + = let + (env', bndrs') = mapAccumL (tidyLetBndr env') env prs + in 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 (Var v) = Var (tidyVarOcc env v) +tidyExpr env (Type ty) = Type (tidyType env ty) +tidyExpr env (Coercion co) = Coercion (tidyCo env co) +tidyExpr _ (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 (Cast e co) = Cast (tidyExpr env e) (tidyCo env co) tidyExpr env (Let b e) = tidyBind env b =: \ (env', b') -> @@ -77,54 +76,25 @@ tidyExpr env (Lam b e) 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 env1 con tvs' scrut_ty - in - 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) +tidyAlt :: CoreBndr -> TidyEnv -> CoreAlt -> CoreAlt +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 +tidyNote :: TidyEnv -> Note -> Note +tidyNote _ note = note ------------ Rules -------------- tidyRules :: TidyEnv -> [CoreRule] -> [CoreRule] -tidyRules env [] = [] +tidyRules _ [] = [] tidyRules env (rule : rules) = tidyRule env rule =: \ rule -> tidyRules env rules =: \ rules -> (rule : rules) tidyRule :: TidyEnv -> CoreRule -> CoreRule -tidyRule env rule@(BuiltinRule {}) = rule +tidyRule _ 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) -> @@ -148,7 +118,7 @@ tidyNameOcc :: TidyEnv -> Name -> Name -- 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 + Just v -> idName v tidyVarOcc :: TidyEnv -> Var -> Var tidyVarOcc (_, var_env) v = lookupVarEnv var_env v `orElse` v @@ -162,12 +132,17 @@ tidyBndr env var tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var]) tidyBndrs env vars = mapAccumL tidyBndr env vars -tidyLetBndr :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var) +tidyLetBndr :: TidyEnv -- Knot-tied version for unfoldings + -> TidyEnv -- The one to extend + -> (Id, CoreExpr) -> (TidyEnv, Var) -- Used for local (non-top-level) let(rec)s -tidyLetBndr env (id,rhs) - = ((tidy_env,new_var_env), final_id) +tidyLetBndr rec_tidy_env env (id,rhs) + = ((tidy_occ_env,new_var_env), final_id) where - ((tidy_env,var_env), new_id) = tidyIdBndr env id + ((tidy_occ_env,var_env), new_id) = tidyIdBndr env id + new_var_env = extendVarEnv var_env id final_id + -- Override the env we get back from tidyId with the + -- new IdInfo so it gets propagated to the usage sites. -- We need to keep around any interesting strictness and -- demand info because later on we may need to use it when @@ -181,22 +156,26 @@ tidyLetBndr env (id,rhs) -- CorePrep to turn the let into a case. -- -- Similarly arity info for eta expansion in CorePrep - -- + -- + -- Set inline-prag info so that we preseve it across + -- separate compilation boundaries final_id = new_id `setIdInfo` new_info idinfo = idInfo id - new_info = vanillaIdInfo + new_info = idInfo new_id `setArityInfo` exprArity rhs - `setAllStrictnessInfo` newStrictnessInfo idinfo - `setNewDemandInfo` newDemandInfo idinfo + `setStrictnessInfo` strictnessInfo idinfo + `setDemandInfo` demandInfo idinfo + `setInlinePragInfo` inlinePragInfo idinfo + `setUnfoldingInfo` new_unf - -- 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 + new_unf | isStableUnfolding unf = tidyUnfolding rec_tidy_env unf (panic "tidy_unf") + | otherwise = noUnfolding + unf = unfoldingInfo idinfo -- 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 + = -- 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 @@ -204,18 +183,55 @@ tidyIdBndr env@(tidy_env, var_env) 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' = mkUserLocal occ' (idUnique id) ty' noSrcLoc - `setIdInfo` vanillaIdInfo - var_env' = extendVarEnv var_env id id' + ty' = tidyType env (idType id) + name' = mkInternalName (idUnique id) occ' noSrcSpan + id' = mkLocalIdWithInfo name' ty' new_info + var_env' = extendVarEnv var_env id id' + + -- Note [Tidy IdInfo] + new_info = vanillaIdInfo `setOccInfo` occInfo old_info + old_info = idInfo id in - ((tidy_env', var_env'), id') + ((tidy_env', var_env'), id') } + +------------ Unfolding -------------- +tidyUnfolding :: TidyEnv -> Unfolding -> Unfolding -> Unfolding +tidyUnfolding tidy_env (DFunUnfolding ar con ids) _ + = DFunUnfolding ar con (map (fmap (tidyExpr tidy_env)) ids) +tidyUnfolding tidy_env + unf@(CoreUnfolding { uf_tmpl = unf_rhs, uf_src = src }) + unf_from_rhs + | isStableSource src + = unf { uf_tmpl = tidyExpr tidy_env unf_rhs, -- Preserves OccInfo + uf_src = tidySrc tidy_env src } + | otherwise + = unf_from_rhs +tidyUnfolding _ unf _ = unf -- NoUnfolding or OtherCon + +tidySrc :: TidyEnv -> UnfoldingSource -> UnfoldingSource +tidySrc tidy_env (InlineWrapper w) = InlineWrapper (tidyVarOcc tidy_env w) +tidySrc _ inl_info = inl_info \end{code} +Note [Tidy IdInfo] +~~~~~~~~~~~~~~~~~~ +All nested Ids now have the same IdInfo, namely vanillaIdInfo, which +should save some space; except that we preserve occurrence info for +two reasons: + + (a) To make printing tidy core nicer + + (b) Because we tidy RULES and InlineRules, which may then propagate + via --make into the compilation of the next module, and we want + the benefit of that occurrence analysis when we use the rule or + or inline the function. In particular, it's vital not to lose + loop-breaker info, else we get an infinite inlining loop + +Note that tidyLetBndr puts more IdInfo back. + + \begin{code} +(=:) :: a -> (a -> b) -> b m =: k = m `seq` k m \end{code}