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, IdOrTyVar )
23 import Id ( idType, idInfo, idName,
24 mkVanillaId, mkId, exportWithOrigOccName,
25 getIdStrictness, setIdStrictness,
26 getIdDemandInfo, setIdDemandInfo,
28 import IdInfo ( specInfo, setSpecInfo,
29 inlinePragInfo, setInlinePragInfo, InlinePragInfo(..),
30 setUnfoldingInfo, setDemandInfo,
31 workerInfo, setWorkerInfo
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
108 (env', NonRec bndr' rhs')
110 tidyBind maybe_mod env (Rec pairs)
112 -- We use env' when tidying the rhss
113 -- When tidying the binder itself we may tidy it's
114 -- specialisations; if any of these mention other binders
115 -- in the group we should really feed env' to them too;
116 -- but that seems (a) unlikely and (b) a bit tiresome.
117 -- So I left it out for now
119 (bndrs, rhss) = unzip pairs
120 (env', bndrs') = mapAccumL (tidy_bndr maybe_mod env') env bndrs
121 rhss' = map (tidyExpr env') rhss
123 (env', Rec (zip bndrs' rhss'))
125 tidyExpr env (Type ty) = Type (tidyType env ty)
126 tidyExpr env (Con con args) = Con con (map (tidyExpr env) args)
127 tidyExpr env (App f a) = App (tidyExpr env f) (tidyExpr env a)
128 tidyExpr env (Note n e) = Note (tidyNote env n) (tidyExpr env e)
130 tidyExpr env (Let b e) = Let b' (tidyExpr env' e)
132 (env', b') = tidyBind Nothing env b
134 tidyExpr env (Case e b alts) = Case (tidyExpr env e) b' (map (tidyAlt env') alts)
136 (env', b') = tidyBndr env b
138 tidyExpr env (Var v) = Var (tidyVarOcc env v)
140 tidyExpr env (Lam b e) = Lam b' (tidyExpr env' e)
142 (env', b') = tidyBndr env b
144 tidyAlt env (con, vs, rhs) = (con, vs', tidyExpr env' rhs)
146 (env', vs') = tidyBndrs env vs
148 tidyNote env (Coerce t1 t2) = Coerce (tidyType env t1) (tidyType env t2)
150 tidyNote env note = note
152 tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
158 tidy_bndr (Just mod) env_idinfo env var = tidyTopId mod env env_idinfo var
159 tidy_bndr Nothing env_idinfo env var = tidyBndr env var
164 %************************************************************************
166 \subsection{Tidying up a binder}
168 %************************************************************************
171 tidyBndr :: TidyEnv -> IdOrTyVar -> (TidyEnv, IdOrTyVar)
172 tidyBndr env var | isTyVar var = tidyTyVar env var
173 | otherwise = tidyId env var
175 tidyBndrs :: TidyEnv -> [IdOrTyVar] -> (TidyEnv, [IdOrTyVar])
176 tidyBndrs env vars = mapAccumL tidyBndr env vars
178 tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
179 tidyId env@(tidy_env, var_env) id
180 = -- Non-top-level variables
182 -- Give the Id a fresh print-name, *and* rename its type
183 -- The SrcLoc isn't important now, though we could extract it from the Id
184 name' = mkLocalName (getUnique id) occ' noSrcLoc
185 (tidy_env', occ') = tidyOccName tidy_env (getOccName id)
186 ty' = tidyType env (idType id)
187 id' = mkVanillaId name' ty'
188 `setIdStrictness` getIdStrictness id
189 `setIdDemandInfo` getIdDemandInfo id
190 -- NB: This throws away the IdInfo of the Id, which we
191 -- no longer need. That means we don't need to
192 -- run over it with env, nor renumber it.
194 -- The exception is strictness and demand info, which
195 -- is used to decide whether to use let or case for
196 -- function arguments and let bindings
198 var_env' = extendVarEnv var_env id id'
200 ((tidy_env', var_env'), id')
202 tidyTopId :: Module -> TidyEnv -> TidyEnv -> Id -> (TidyEnv, Id)
203 -- The second env is the one to use for the IdInfo
204 -- It's necessary because when we are dealing with a recursive
205 -- group, a variable late in the group might be mentioned
206 -- in the IdInfo of one early in the group
207 tidyTopId mod env@(tidy_env, var_env) env_idinfo id
208 = -- Top level variables
210 (tidy_env', name') | exportWithOrigOccName id = (tidy_env, idName id)
211 | otherwise = tidyTopName mod tidy_env (idName id)
212 ty' = tidyTopType (idType id)
213 idinfo' = tidyIdInfo env_idinfo (idInfo id)
214 id' = mkId name' ty' idinfo'
215 var_env' = extendVarEnv var_env id id'
217 ((tidy_env', var_env'), id')
221 -- tidyIdInfo does these things:
222 -- a) tidy the specialisation info and worker info (if any)
223 -- b) zap the unfolding and demand info
224 -- The latter two are to avoid space leaks
229 rules = specInfo info
231 info2 | isEmptyCoreRules rules = info
232 | otherwise = info `setSpecInfo` tidyRules env rules
234 info3 = info2 `setUnfoldingInfo` noUnfolding
235 info4 = info3 `setDemandInfo` wwLazy -- I don't understand why...
237 info5 = case workerInfo info of
239 Just w -> info4 `setWorkerInfo` Just (tidyVarOcc env w)
241 tidyProtoRules :: TidyEnv -> [ProtoCoreRule] -> [ProtoCoreRule]
242 tidyProtoRules env rules
243 = [ ProtoCoreRule is_local (tidyVarOcc env fn) (tidyRule env rule)
244 | ProtoCoreRule is_local fn rule <- rules
247 tidyRules :: TidyEnv -> CoreRules -> CoreRules
248 tidyRules env (Rules rules fvs)
249 = Rules (map (tidyRule env) rules)
250 (foldVarSet tidy_set_elem emptyVarSet fvs)
252 tidy_set_elem var new_set = extendVarSet new_set (tidyVarOcc env var)
254 tidyRule :: TidyEnv -> CoreRule -> CoreRule
255 tidyRule env rule@(BuiltinRule _) = rule
256 tidyRule env (Rule name vars tpl_args rhs)
257 = (Rule name vars' (map (tidyExpr env') tpl_args) (tidyExpr env' rhs))
259 (env', vars') = tidyBndrs env vars