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(..) )
19 import UsageSPInf ( doUsageSPInf )
22 import Var ( Id, Var )
23 import Id ( idType, idInfo, idName,
24 mkVanillaId, mkId, exportWithOrigOccName,
25 idStrictness, setIdStrictness,
26 idDemandInfo, setIdDemandInfo,
28 import IdInfo ( specInfo, setSpecInfo,
29 inlinePragInfo, setInlinePragInfo, InlinePragInfo(..),
30 setUnfoldingInfo, setDemandInfo,
31 workerInfo, setWorkerInfo, WorkerInfo(..)
33 import Demand ( wwLazy )
34 import Name ( getOccName, tidyTopName, mkLocalName, isLocallyDefined )
35 import OccName ( initTidyOccEnv, tidyOccName )
36 import Type ( tidyTopType, tidyType, tidyTypes, tidyTyVar, tidyTyVars )
37 import Module ( Module )
38 import UniqSupply ( UniqSupply )
39 import Unique ( Uniquable(..) )
40 import SrcLoc ( noSrcLoc )
41 import Util ( mapAccumL )
47 %************************************************************************
49 \subsection{Tidying core}
51 %************************************************************************
53 Several tasks are done by @tidyCorePgm@
55 1. Make certain top-level bindings into Globals. The point is that
56 Global things get externally-visible labels at code generation
60 2. Give all binders a nice print-name. Their uniques aren't changed;
61 rather we give them lexically unique occ-names, so that we can
62 safely print the OccNae only in the interface file. [Bad idea to
63 change the uniques, because the code generator makes global labels
64 from the uniques for local thunks etc.]
67 3. If @opt_UsageSPOn@ then compute usage information (which is
68 needed by Core2Stg). ** NOTE _scc_ HERE **
71 tidyCorePgm :: UniqSupply -> Module -> [CoreBind] -> [ProtoCoreRule]
72 -> IO ([CoreBind], [ProtoCoreRule])
73 tidyCorePgm us module_name binds_in rules
77 let (tidy_env1, binds_tidy) = mapAccumL (tidyBind (Just module_name)) init_tidy_env binds_in
78 rules_out = tidyProtoRules tidy_env1 rules
80 binds_out <- if opt_UsageSPOn
81 then _scc_ "CoreUsageSPInf" doUsageSPInf us binds_tidy
82 else return binds_tidy
84 endPass "Tidy Core" (opt_D_dump_simpl || opt_D_verbose_core2core) binds_out
85 return (binds_out, rules_out)
87 -- We also make sure to avoid any exported binders. Consider
88 -- f{-u1-} = 1 -- Local decl
90 -- f{-u2-} = 2 -- Exported decl
92 -- The second exported decl must 'get' the name 'f', so we
93 -- have to put 'f' in the avoids list before we get to the first
94 -- decl. tidyTopId then does a no-op on exported binders.
95 init_tidy_env = (initTidyOccEnv avoids, emptyVarEnv)
96 avoids = [getOccName bndr | bndr <- bindersOfBinds binds_in,
97 exportWithOrigOccName bndr]
99 tidyBind :: Maybe Module -- (Just m) for top level, Nothing for nested
102 -> (TidyEnv, CoreBind)
103 tidyBind maybe_mod env (NonRec bndr rhs)
105 (env', bndr') = tidy_bndr maybe_mod env' env bndr
106 rhs' = tidyExpr env' rhs
107 -- We use env' when tidying the RHS even though it's not
108 -- strictly necessary; it makes the code pretty hard to read
111 (env', NonRec bndr' rhs')
113 tidyBind maybe_mod env (Rec pairs)
115 -- We use env' when tidying the rhss
116 -- When tidying the binder itself we may tidy it's
117 -- specialisations; if any of these mention other binders
118 -- in the group we should really feed env' to them too;
119 -- but that seems (a) unlikely and (b) a bit tiresome.
120 -- So I left it out for now
122 (bndrs, rhss) = unzip pairs
123 (env', bndrs') = mapAccumL (tidy_bndr maybe_mod env') env bndrs
124 rhss' = map (tidyExpr env') rhss
126 (env', Rec (zip bndrs' rhss'))
128 tidyExpr env (Type ty) = Type (tidyType env ty)
129 tidyExpr env (Lit lit) = Lit lit
130 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
131 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
133 tidyExpr env (Let b e) = Let b' (tidyExpr env' e)
135 (env', b') = tidyBind Nothing env b
137 tidyExpr env (Case e b alts) = Case (tidyExpr env e) b' (map (tidyAlt env') alts)
139 (env', b') = tidyBndr env b
141 tidyExpr env (Var v) = Var (tidyVarOcc env v)
143 tidyExpr env (Lam b e) = Lam b' (tidyExpr env' e)
145 (env', b') = tidyBndr env b
147 tidyAlt env (con, vs, rhs) = (con, vs', tidyExpr env' rhs)
149 (env', vs') = tidyBndrs env vs
151 tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
153 tidyNote env note = note
155 tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
161 tidy_bndr (Just mod) env_idinfo env var = tidyTopId mod env env_idinfo var
162 tidy_bndr Nothing env_idinfo env var = tidyBndr env var
167 %************************************************************************
169 \subsection{Tidying up a binder}
171 %************************************************************************
174 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
175 tidyBndr env var | isTyVar var = tidyTyVar env var
176 | otherwise = tidyId env var
178 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
179 tidyBndrs env vars = mapAccumL tidyBndr env vars
181 tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
182 tidyId env@(tidy_env, var_env) id
183 = -- Non-top-level variables
185 -- Give the Id a fresh print-name, *and* rename its type
186 -- The SrcLoc isn't important now, though we could extract it from the Id
187 name' = mkLocalName (getUnique id) occ' noSrcLoc
188 (tidy_env', occ') = tidyOccName tidy_env (getOccName id)
189 ty' = tidyType env (idType id)
190 id' = mkVanillaId name' ty'
191 `setIdStrictness` idStrictness id
192 `setIdDemandInfo` idDemandInfo id
193 -- NB: This throws away the IdInfo of the Id, which we
194 -- no longer need. That means we don't need to
195 -- run over it with env, nor renumber it.
197 -- The exception is strictness and demand info, which
198 -- is used to decide whether to use let or case for
199 -- function arguments and let bindings
201 var_env' = extendVarEnv var_env id id'
203 ((tidy_env', var_env'), id')
205 tidyTopId :: Module -> TidyEnv -> TidyEnv -> Id -> (TidyEnv, Id)
206 -- The second env is the one to use for the IdInfo
207 -- It's necessary because when we are dealing with a recursive
208 -- group, a variable late in the group might be mentioned
209 -- in the IdInfo of one early in the group
210 tidyTopId mod env@(tidy_env, var_env) env_idinfo id
211 = -- Top level variables
213 (tidy_env', name') | exportWithOrigOccName id = (tidy_env, idName id)
214 | otherwise = tidyTopName mod tidy_env (idName id)
215 ty' = tidyTopType (idType id)
216 idinfo' = tidyIdInfo env_idinfo (idInfo id)
217 id' = mkId name' ty' idinfo'
218 var_env' = extendVarEnv var_env id id'
220 ((tidy_env', var_env'), id')
224 -- tidyIdInfo does these things:
225 -- a) tidy the specialisation info and worker info (if any)
226 -- b) zap the unfolding and demand info
227 -- The latter two are to avoid space leaks
232 rules = specInfo info
234 info2 | isEmptyCoreRules rules = info
235 | otherwise = info `setSpecInfo` tidyRules env rules
237 info3 = info2 `setUnfoldingInfo` noUnfolding
238 info4 = info3 `setDemandInfo` wwLazy -- I don't understand why...
240 info5 = case workerInfo info of
242 HasWorker w a -> info4 `setWorkerInfo` HasWorker (tidyVarOcc env w) a
244 tidyProtoRules :: TidyEnv -> [ProtoCoreRule] -> [ProtoCoreRule]
245 tidyProtoRules env rules
246 = [ ProtoCoreRule is_local (tidyVarOcc env fn) (tidyRule env rule)
247 | ProtoCoreRule is_local fn rule <- rules
250 tidyRules :: TidyEnv -> CoreRules -> CoreRules
251 tidyRules env (Rules rules fvs)
252 = Rules (map (tidyRule env) rules)
253 (foldVarSet tidy_set_elem emptyVarSet fvs)
255 tidy_set_elem var new_set = extendVarSet new_set (tidyVarOcc env var)
257 tidyRule :: TidyEnv -> CoreRule -> CoreRule
258 tidyRule env rule@(BuiltinRule _) = rule
259 tidyRule env (Rule name vars tpl_args rhs)
260 = (Rule name vars' (map (tidyExpr env') tpl_args) (tidyExpr env' rhs))
262 (env', vars') = tidyBndrs env vars