2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{Tidying up Core}
12 #include "HsVersions.h"
14 import CmdLineOpts ( opt_D_dump_simpl, opt_D_verbose_core2core, opt_UsageSPOn )
16 import CoreUnfold ( noUnfolding )
17 import CoreLint ( beginPass, endPass )
18 import Rules ( ProtoCoreRule(..) )
21 import Var ( Id, IdOrTyVar )
22 import Id ( idType, idInfo, idName,
23 mkVanillaId, mkId, isUserExportedId,
24 getIdStrictness, setIdStrictness,
25 getIdDemandInfo, setIdDemandInfo,
27 import IdInfo ( specInfo, setSpecInfo,
28 inlinePragInfo, setInlinePragInfo, InlinePragInfo(..),
29 setUnfoldingInfo, setDemandInfo
31 import Demand ( wwLazy )
32 import Name ( getOccName, tidyTopName, mkLocalName, isLocallyDefined )
33 import OccName ( initTidyOccEnv, tidyOccName )
34 import Type ( tidyTopType, tidyType, tidyTypes, tidyTyVar, tidyTyVars )
35 import Class ( Class, classSelIds )
36 import Module ( Module )
37 import UniqSupply ( UniqSupply )
38 import Unique ( Uniquable(..) )
39 import SrcLoc ( noSrcLoc )
40 import Util ( mapAccumL )
43 doUsageSPInf = panic "doUsageSpInf"
48 %************************************************************************
50 \subsection{Tidying core}
52 %************************************************************************
54 Several tasks are done by @tidyCorePgm@
56 1. Make certain top-level bindings into Globals. The point is that
57 Global things get externally-visible labels at code generation
61 2. Give all binders a nice print-name. Their uniques aren't changed;
62 rather we give them lexically unique occ-names, so that we can
63 safely print the OccNae only in the interface file. [Bad idea to
64 change the uniques, because the code generator makes global labels
65 from the uniques for local thunks etc.]
68 3. If @opt_UsageSPOn@ then compute usage information (which is
69 needed by Core2Stg). ** NOTE _scc_ HERE **
72 tidyCorePgm :: UniqSupply -> Module -> [CoreBind] -> [ProtoCoreRule]
73 -> IO ([CoreBind], [ProtoCoreRule])
74 tidyCorePgm us module_name binds_in rules
78 let (tidy_env1, binds_tidy) = mapAccumL (tidyBind (Just module_name)) init_tidy_env binds_in
79 rules_out = tidyProtoRules tidy_env1 rules
81 binds_out <- if opt_UsageSPOn
82 then _scc_ "CoreUsageSPInf" doUsageSPInf us binds_tidy
83 else return binds_tidy
85 endPass "Tidy Core" (opt_D_dump_simpl || opt_D_verbose_core2core) binds_out
86 return (binds_out, rules_out)
88 -- We also make sure to avoid any exported binders. Consider
89 -- f{-u1-} = 1 -- Local decl
91 -- f{-u2-} = 2 -- Exported decl
93 -- The second exported decl must 'get' the name 'f', so we
94 -- have to put 'f' in the avoids list before we get to the first
95 -- decl. tidyTopId then does a no-op on exported binders.
96 init_tidy_env = (initTidyOccEnv avoids, emptyVarEnv)
97 avoids = [getOccName bndr | bndr <- bindersOfBinds binds_in,
98 isUserExportedId bndr]
100 tidyBind :: Maybe Module -- (Just m) for top level, Nothing for nested
103 -> (TidyEnv, CoreBind)
104 tidyBind maybe_mod env (NonRec bndr rhs)
106 (env', bndr') = tidy_bndr maybe_mod env bndr
107 rhs' = tidyExpr env rhs
109 (env', NonRec bndr' rhs')
111 tidyBind maybe_mod env (Rec pairs)
113 -- We use env' when tidying the rhss
114 -- When tidying the binder itself we may tidy it's
115 -- specialisations; if any of these mention other binders
116 -- in the group we should really feed env' to them too;
117 -- but that seems (a) unlikely and (b) a bit tiresome.
118 -- So I left it out for now
120 (bndrs, rhss) = unzip pairs
121 (env', bndrs') = mapAccumL (tidy_bndr maybe_mod) env bndrs
122 rhss' = map (tidyExpr env') rhss
124 (env', Rec (zip bndrs' rhss'))
126 tidyExpr env (Type ty) = Type (tidyType env ty)
127 tidyExpr env (Con con args) = Con con (map (tidyExpr env) args)
128 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
129 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
131 tidyExpr env (Let b e) = Let b' (tidyExpr env' e)
133 (env', b') = tidyBind Nothing env b
135 tidyExpr env (Case e b alts) = Case (tidyExpr env e) b' (map (tidyAlt env') alts)
137 (env', b') = tidyBndr env b
139 tidyExpr env (Var v) = Var (tidyVarOcc env v)
141 tidyExpr env (Lam b e) = Lam b' (tidyExpr env' e)
143 (env', b') = tidyBndr env b
145 tidyAlt env (con, vs, rhs) = (con, vs', tidyExpr env' rhs)
147 (env', vs') = tidyBndrs env vs
149 tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
151 tidyNote env note = note
153 tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
159 tidy_bndr (Just mod) env id = tidyTopId mod env id
160 tidy_bndr Nothing env var = tidyBndr env var
165 %************************************************************************
167 \subsection{Tidying up a binder}
169 %************************************************************************
172 tidyBndr :: TidyEnv -> IdOrTyVar -> (TidyEnv, IdOrTyVar)
173 tidyBndr env var | isTyVar var = tidyTyVar env var
174 | otherwise = tidyId env var
176 tidyBndrs :: TidyEnv -> [IdOrTyVar] -> (TidyEnv, [IdOrTyVar])
177 tidyBndrs env vars = mapAccumL tidyBndr env vars
179 tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
180 tidyId env@(tidy_env, var_env) id
181 = -- Non-top-level variables
183 -- Give the Id a fresh print-name, *and* rename its type
184 -- The SrcLoc isn't important now, though we could extract it from the Id
185 name' = mkLocalName (getUnique id) occ' noSrcLoc
186 (tidy_env', occ') = tidyOccName tidy_env (getOccName id)
187 ty' = tidyType env (idType id)
188 id' = mkVanillaId name' ty'
189 `setIdStrictness` getIdStrictness id
190 `setIdDemandInfo` getIdDemandInfo id
191 -- NB: This throws away the IdInfo of the Id, which we
192 -- no longer need. That means we don't need to
193 -- run over it with env, nor renumber it.
195 -- The exception is strictness and demand info, which
196 -- is used to decide whether to use let or case for
197 -- function arguments and let bindings
199 var_env' = extendVarEnv var_env id id'
201 ((tidy_env', var_env'), id')
203 tidyTopId :: Module -> TidyEnv -> Id -> (TidyEnv, Id)
204 tidyTopId mod env@(tidy_env, var_env) id
205 = -- Top level variables
207 (tidy_env', name') | isUserExportedId id = (tidy_env, idName id)
208 | otherwise = tidyTopName mod tidy_env (idName id)
209 ty' = tidyTopType (idType id)
210 idinfo' = tidyIdInfo env (idInfo id)
211 id' = mkId name' ty' idinfo'
212 var_env' = extendVarEnv var_env id id'
214 ((tidy_env', var_env'), id')
218 -- tidyIdInfo does these things:
219 -- a) tidy the specialisation info (if any)
220 -- b) zap a complicated ICanSafelyBeINLINEd pragma,
221 -- c) zap the unfolding
222 -- The latter two are to avoid space leaks
227 rules = specInfo info
229 info1 | isEmptyCoreRules rules = info
230 | otherwise = info `setSpecInfo` tidyRules env rules
232 info2 = case inlinePragInfo info of
233 ICanSafelyBeINLINEd _ _ -> info1 `setInlinePragInfo` NoInlinePragInfo
236 info3 = info2 `setUnfoldingInfo` noUnfolding
237 info4 = info3 `setDemandInfo` wwLazy -- I don't understand why...
239 tidyProtoRules :: TidyEnv -> [ProtoCoreRule] -> [ProtoCoreRule]
240 tidyProtoRules env rules
241 = [ ProtoCoreRule is_local (tidyVarOcc env fn) (tidyRule env rule)
242 | ProtoCoreRule is_local fn rule <- rules
245 tidyRules :: TidyEnv -> CoreRules -> CoreRules
246 tidyRules env (Rules rules fvs)
247 = Rules (map (tidyRule env) rules)
248 (foldVarSet tidy_set_elem emptyVarSet fvs)
250 tidy_set_elem var new_set = extendVarSet new_set (tidyVarOcc env var)
252 tidyRule :: TidyEnv -> CoreRule -> CoreRule
253 tidyRule env (Rule name vars tpl_args rhs)
254 = (Rule name vars' (map (tidyExpr env') tpl_args) (tidyExpr env' rhs))
256 (env', vars') = tidyBndrs env vars