3f5626ddb0f290d7fe41047749f80740d3f2ff52
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreTidy.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Tidying up Core}
5
6 \begin{code}
7 module CoreTidy (
8         tidyCorePgm, tidyExpr, 
9         tidyBndr, tidyBndrs
10     ) where
11
12 #include "HsVersions.h"
13
14 import CmdLineOpts      ( opt_D_dump_simpl, opt_D_verbose_core2core, opt_UsageSPOn )
15 import CoreSyn
16 import CoreUnfold       ( noUnfolding )
17 import CoreLint         ( beginPass, endPass )
18 import Rules            ( ProtoCoreRule(..) )
19 import UsageSPInf       ( doUsageSPInf )
20 import VarEnv
21 import VarSet
22 import Var              ( Id, Var )
23 import Id               ( idType, idInfo, idName, 
24                           mkVanillaId, mkId, exportWithOrigOccName,
25                           idStrictness, setIdStrictness,
26                           idDemandInfo, setIdDemandInfo,
27                         ) 
28 import IdInfo           ( specInfo, setSpecInfo, 
29                           inlinePragInfo, setInlinePragInfo, InlinePragInfo(..),
30                           setUnfoldingInfo, setDemandInfo,
31                           workerInfo, setWorkerInfo, WorkerInfo(..)
32                         )
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 )
42 import Outputable
43 \end{code}
44
45
46
47 %************************************************************************
48 %*                                                                      *
49 \subsection{Tidying core}
50 %*                                                                      *
51 %************************************************************************
52
53 Several tasks are done by @tidyCorePgm@
54
55 1.  Make certain top-level bindings into Globals. The point is that 
56     Global things get externally-visible labels at code generation
57     time
58
59
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.]
65
66
67 3. If @opt_UsageSPOn@ then compute usage information (which is
68    needed by Core2Stg).  ** NOTE _scc_ HERE **
69
70 \begin{code}
71 tidyCorePgm :: UniqSupply -> Module -> [CoreBind] -> [ProtoCoreRule]
72             -> IO ([CoreBind], [ProtoCoreRule])
73 tidyCorePgm us module_name binds_in rules
74   = do
75         beginPass "Tidy Core"
76
77         let (tidy_env1, binds_tidy) = mapAccumL (tidyBind (Just module_name)) init_tidy_env binds_in
78             rules_out               = tidyProtoRules tidy_env1 rules
79
80         binds_out <- if opt_UsageSPOn
81                      then _scc_ "CoreUsageSPInf" doUsageSPInf us binds_tidy
82                      else return binds_tidy
83
84         endPass "Tidy Core" (opt_D_dump_simpl || opt_D_verbose_core2core) binds_out
85         return (binds_out, rules_out)
86   where
87         -- We also make sure to avoid any exported binders.  Consider
88         --      f{-u1-} = 1     -- Local decl
89         --      ...
90         --      f{-u2-} = 2     -- Exported decl
91         --
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]
98
99 tidyBind :: Maybe Module                -- (Just m) for top level, Nothing for nested
100          -> TidyEnv
101          -> CoreBind
102          -> (TidyEnv, CoreBind)
103 tidyBind maybe_mod env (NonRec bndr rhs)
104   = let
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
109         -- if we don't!
110     in
111     (env', NonRec bndr' rhs')
112
113 tidyBind maybe_mod env (Rec pairs)
114   = let
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
121
122         (bndrs, rhss)  = unzip pairs
123         (env', bndrs') = mapAccumL (tidy_bndr maybe_mod env') env bndrs
124         rhss'          = map (tidyExpr env') rhss
125   in
126   (env', Rec (zip bndrs' rhss'))
127
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)
132
133 tidyExpr env (Let b e)       = Let b' (tidyExpr env' e)
134                              where
135                                (env', b') = tidyBind Nothing env b
136
137 tidyExpr env (Case e b alts) = Case (tidyExpr env e) b' (map (tidyAlt env') alts)
138                              where
139                                (env', b') = tidyBndr env b
140
141 tidyExpr env (Var v)         = Var (tidyVarOcc env v)
142
143 tidyExpr env (Lam b e)       = Lam b' (tidyExpr env' e)
144                              where
145                                (env', b') = tidyBndr env b
146
147 tidyAlt env (con, vs, rhs)   = (con, vs', tidyExpr env' rhs)
148                              where
149                                (env', vs') = tidyBndrs env vs
150
151 tidyNote env (Coerce t1 t2)  = Coerce (tidyType env t1) (tidyType env t2)
152
153 tidyNote env note            = note
154
155 tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
156                                   Just v' -> v'
157                                   Nothing -> v
158 \end{code}
159
160 \begin{code}
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
163 \end{code}
164
165
166
167 %************************************************************************
168 %*                                                                      *
169 \subsection{Tidying up a binder}
170 %*                                                                      *
171 %************************************************************************
172
173 \begin{code}
174 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
175 tidyBndr env var | isTyVar var = tidyTyVar env var
176                  | otherwise   = tidyId    env var
177
178 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
179 tidyBndrs env vars = mapAccumL tidyBndr env vars
180
181 tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
182 tidyId env@(tidy_env, var_env) id
183   =     -- Non-top-level variables
184     let 
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.
196                         --
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
200
201         var_env'          = extendVarEnv var_env id id'
202     in
203     ((tidy_env', var_env'), id')
204
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
212     let
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'
219     in
220     ((tidy_env', var_env'), id')
221 \end{code}
222
223 \begin{code}
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
228
229 tidyIdInfo env info
230   = info5
231   where
232     rules = specInfo info
233
234     info2 | isEmptyCoreRules rules = info 
235           | otherwise              = info `setSpecInfo` tidyRules env rules
236                 
237     info3 = info2 `setUnfoldingInfo` noUnfolding 
238     info4 = info3 `setDemandInfo`    wwLazy             -- I don't understand why...
239
240     info5 = case workerInfo info of
241                 NoWorker -> info4
242                 HasWorker w a  -> info4 `setWorkerInfo` HasWorker (tidyVarOcc env w) a
243
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
248     ]
249
250 tidyRules :: TidyEnv -> CoreRules -> CoreRules
251 tidyRules env (Rules rules fvs) 
252   = Rules (map (tidyRule env) rules)
253           (foldVarSet tidy_set_elem emptyVarSet fvs)
254   where
255     tidy_set_elem var new_set = extendVarSet new_set (tidyVarOcc env var)
256
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))
261   where
262     (env', vars') = tidyBndrs env vars
263 \end{code}