2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{Tidying up Core}
7 module TidyPgm( tidyCorePgm, tidyCoreExpr ) where
9 #include "HsVersions.h"
11 import CmdLineOpts ( DynFlags, DynFlag(..), opt_OmitInterfacePragmas )
13 import CoreUnfold ( noUnfolding, mkTopUnfolding, okToUnfoldInHiFile )
14 import CoreFVs ( ruleLhsFreeIds, ruleRhsFreeVars, exprSomeFreeVars )
15 import CoreTidy ( tidyExpr, tidyVarOcc, tidyIdRules )
16 import PprCore ( pprIdRules )
17 import CoreLint ( showPass, endPass )
18 import CoreUtils ( exprArity )
21 import Var ( Id, Var )
22 import Id ( idType, idInfo, idName, idCoreRules,
23 isExportedId, mkVanillaGlobal, isLocalId,
26 import IdInfo {- loads of stuff -}
27 import NewDemand ( isBottomingSig, topSig )
28 import BasicTypes ( isNeverActive )
29 import Name ( getOccName, nameOccName, mkInternalName, mkExternalName,
30 localiseName, isExternalName, nameSrcLoc
32 import NameEnv ( filterNameEnv )
33 import OccName ( TidyOccEnv, initTidyOccEnv, tidyOccName )
34 import Type ( tidyTopType )
35 import Module ( Module, moduleName )
36 import HscTypes ( PersistentCompilerState( pcs_nc ),
37 NameCache( nsNames, nsUniqs ),
38 TypeEnv, extendTypeEnvList, typeEnvIds,
39 ModGuts(..), ModGuts, TyThing(..)
41 import FiniteMap ( lookupFM, addToFM )
42 import Maybes ( orElse )
43 import ErrUtils ( showPass, dumpIfSet_core )
44 import UniqFM ( mapUFM )
45 import UniqSupply ( splitUniqSupply, uniqFromSupply )
46 import List ( partition )
47 import Util ( mapAccumL )
48 import Maybe ( isJust )
54 %************************************************************************
56 \subsection{What goes on}
58 %************************************************************************
64 Step 1: Figure out external Ids
65 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 First we figure out which Ids are "external" Ids. An
67 "external" Id is one that is visible from outside the compilation
69 a) the user exported ones
70 b) ones mentioned in the unfoldings, workers,
71 or rules of externally-visible ones
72 This exercise takes a sweep of the bindings bottom to top. Actually,
73 in Step 2 we're also going to need to know which Ids should be
74 exported with their unfoldings, so we produce not an IdSet but an
78 Step 2: Tidy the program
79 ~~~~~~~~~~~~~~~~~~~~~~~~
80 Next we traverse the bindings top to bottom. For each *top-level*
83 1. Make it into a GlobalId
85 2. Give it a system-wide Unique.
86 [Even non-exported things need system-wide Uniques because the
87 byte-code generator builds a single Name->BCO symbol table.]
89 We use the NameCache kept in the PersistentCompilerState as the
90 source of such system-wide uniques.
92 For external Ids, use the original-name cache in the NameCache
93 to ensure that the unique assigned is the same as the Id had
94 in any previous compilation run.
96 3. If it's an external Id, make it have a global Name, otherwise
97 make it have a local Name.
98 This is used by the code generator to decide whether
99 to make the label externally visible
101 4. Give external Ids a "tidy" occurrence name. This means
102 we can print them in interface files without confusing
103 "x" (unique 5) with "x" (unique 10).
105 5. Give it its UTTERLY FINAL IdInfo; in ptic,
106 * Its IdDetails becomes VanillaGlobal, reflecting the fact that
107 from now on we regard it as a global, not local, Id
109 * its unfolding, if it should have one
111 * its arity, computed from the number of visible lambdas
113 * its CAF info, computed from what is free in its RHS
116 Finally, substitute these new top-level binders consistently
117 throughout, including in unfoldings. We also tidy binders in
118 RHSs, so that they print nicely in interfaces.
121 tidyCorePgm :: DynFlags
122 -> PersistentCompilerState
123 -> CgInfoEnv -- Information from the back end,
124 -- to be splatted into the IdInfo
126 -> IO (PersistentCompilerState, ModGuts)
128 tidyCorePgm dflags pcs cg_info_env
129 mod_impl@(ModGuts { mg_module = mod,
130 mg_types = env_tc, mg_insts = insts_tc,
131 mg_binds = binds_in, mg_rules = orphans_in })
132 = do { showPass dflags "Tidy Core"
134 ; let ext_ids = findExternalSet binds_in orphans_in
135 ; let ext_rules = findExternalRules binds_in orphans_in ext_ids
136 -- findExternalRules filters ext_rules to avoid binders that
137 -- aren't externally visible; but the externally-visible binders
138 -- are computed (by findExternalSet) assuming that all orphan
139 -- rules are exported. So in fact we may export more than we
140 -- need. (It's a sort of mutual recursion.)
142 -- We also make sure to avoid any exported binders. Consider
143 -- f{-u1-} = 1 -- Local decl
145 -- f{-u2-} = 2 -- Exported decl
147 -- The second exported decl must 'get' the name 'f', so we
148 -- have to put 'f' in the avoids list before we get to the first
149 -- decl. tidyTopId then does a no-op on exported binders.
150 ; let orig_ns = pcs_nc pcs
151 init_tidy_env = (orig_ns, initTidyOccEnv avoids, emptyVarEnv)
152 avoids = [getOccName name | bndr <- typeEnvIds env_tc,
153 let name = idName bndr,
155 -- In computing our "avoids" list, we must include
157 -- all things with global names (assigned once and for
158 -- all by the renamer)
159 -- since their names are "taken".
160 -- The type environment is a convenient source of such things.
162 ; let ((orig_ns', occ_env, subst_env), tidy_binds)
163 = mapAccumL (tidyTopBind mod ext_ids cg_info_env)
164 init_tidy_env binds_in
166 ; let tidy_rules = tidyIdRules (occ_env,subst_env) ext_rules
168 ; let pcs' = pcs { pcs_nc = orig_ns' }
170 ; let final_ids = [ id
172 , id <- bindersOf bind
173 , isExternalName (idName id)]
175 -- Dfuns are local Ids that might have
176 -- changed their unique during tidying
177 ; let lookup_dfun_id id = lookupVarEnv subst_env id `orElse`
178 pprPanic "lookup_dfun_id" (ppr id)
181 ; let tidy_type_env = mkFinalTypeEnv env_tc final_ids
182 tidy_dfun_ids = map lookup_dfun_id insts_tc
184 ; let tidy_result = mod_impl { mg_types = tidy_type_env,
185 mg_rules = tidy_rules,
186 mg_insts = tidy_dfun_ids,
187 mg_binds = tidy_binds }
189 ; endPass dflags "Tidy Core" Opt_D_dump_simpl tidy_binds
190 ; dumpIfSet_core dflags Opt_D_dump_simpl
192 (pprIdRules tidy_rules)
194 ; return (pcs', tidy_result)
197 tidyCoreExpr :: CoreExpr -> IO CoreExpr
198 tidyCoreExpr expr = return (tidyExpr emptyTidyEnv expr)
202 %************************************************************************
204 \subsection{Write a new interface file}
206 %************************************************************************
209 mkFinalTypeEnv :: TypeEnv -- From typechecker
213 mkFinalTypeEnv type_env final_ids
214 = extendTypeEnvList (filterNameEnv keep_it type_env)
217 -- The competed type environment is gotten from
218 -- a) keeping the types and classes
219 -- b) removing all Ids,
220 -- c) adding Ids with correct IdInfo, including unfoldings,
221 -- gotten from the bindings
222 -- From (c) we keep only those Ids with Global names;
223 -- the CoreTidy pass makes sure these are all and only
224 -- the externally-accessible ones
225 -- This truncates the type environment to include only the
226 -- exported Ids and things needed from them, which saves space
228 -- However, we do keep things like constructors, which should not appear
229 -- in interface files, because they are needed by importing modules when
230 -- using the compilation manager
232 -- We keep implicit Ids, because they won't appear
233 -- in the bindings from which final_ids are derived!
234 keep_it (AnId id) = isImplicitId id -- Remove all Ids except implicit ones
235 keep_it other = True -- Keep all TyCons and Classes
239 findExternalRules :: [CoreBind]
240 -> [IdCoreRule] -- Orphan rules
241 -> IdEnv a -- Ids that are exported, so we need their rules
243 -- The complete rules are gotten by combining
244 -- a) the orphan rules
245 -- b) rules embedded in the top-level Ids
246 findExternalRules binds orphan_rules ext_ids
247 | opt_OmitInterfacePragmas = []
249 = filter needed_rule (orphan_rules ++ local_rules)
252 | id <- bindersOfBinds binds,
253 id `elemVarEnv` ext_ids,
254 rule <- idCoreRules id
256 needed_rule (id, rule)
257 = not (isBuiltinRule rule)
258 -- We can't print builtin rules in interface files
259 -- Since they are built in, an importing module
260 -- will have access to them anyway
262 && not (any internal_id (varSetElems (ruleLhsFreeIds rule)))
263 -- Don't export a rule whose LHS mentions an Id that
264 -- is completely internal (i.e. not visible to an
267 internal_id id = isLocalId id && not (id `elemVarEnv` ext_ids)
270 %************************************************************************
272 \subsection{Step 1: finding externals}
274 %************************************************************************
277 findExternalSet :: [CoreBind] -> [IdCoreRule]
278 -> IdEnv Bool -- In domain => external
279 -- Range = True <=> show unfolding
280 -- Step 1 from the notes above
281 findExternalSet binds orphan_rules
282 = foldr find init_needed binds
284 orphan_rule_ids :: IdSet
285 orphan_rule_ids = unionVarSets [ ruleRhsFreeVars rule
286 | (_, rule) <- orphan_rules]
287 init_needed :: IdEnv Bool
288 init_needed = mapUFM (\_ -> False) orphan_rule_ids
289 -- The mapUFM is a bit cheesy. It is a cheap way
290 -- to turn the set of orphan_rule_ids, which we use to initialise
291 -- the sweep, into a mapping saying 'don't expose unfolding'
292 -- (When we come to the binding site we may change our mind, of course.)
294 find (NonRec id rhs) needed
295 | need_id needed id = addExternal (id,rhs) needed
297 find (Rec prs) needed = find_prs prs needed
299 -- For a recursive group we have to look for a fixed point
301 | null needed_prs = needed
302 | otherwise = find_prs other_prs new_needed
304 (needed_prs, other_prs) = partition (need_pr needed) prs
305 new_needed = foldr addExternal needed needed_prs
307 -- The 'needed' set contains the Ids that are needed by earlier
308 -- interface file emissions. If the Id isn't in this set, and isn't
309 -- exported, there's no need to emit anything
310 need_id needed_set id = id `elemVarEnv` needed_set || isExportedId id
311 need_pr needed_set (id,rhs) = need_id needed_set id
313 addExternal :: (Id,CoreExpr) -> IdEnv Bool -> IdEnv Bool
314 -- The Id is needed; extend the needed set
315 -- with it and its dependents (free vars etc)
316 addExternal (id,rhs) needed
317 = extendVarEnv (foldVarSet add_occ needed new_needed_ids)
320 add_occ id needed = extendVarEnv needed id False
321 -- "False" because we don't know we need the Id's unfolding
322 -- We'll override it later when we find the binding site
324 new_needed_ids | opt_OmitInterfacePragmas = emptyVarSet
325 | otherwise = worker_ids `unionVarSet`
326 unfold_ids `unionVarSet`
330 dont_inline = isNeverActive (inlinePragInfo idinfo)
331 loop_breaker = isLoopBreaker (occInfo idinfo)
332 bottoming_fn = isBottomingSig (newStrictnessInfo idinfo `orElse` topSig)
333 spec_ids = rulesRhsFreeVars (specInfo idinfo)
334 worker_info = workerInfo idinfo
336 -- Stuff to do with the Id's unfolding
337 -- The simplifier has put an up-to-date unfolding
338 -- in the IdInfo, but the RHS will do just as well
339 unfolding = unfoldingInfo idinfo
340 rhs_is_small = not (neverUnfold unfolding)
342 -- We leave the unfolding there even if there is a worker
343 -- In GHCI the unfolding is used by importers
344 -- When writing an interface file, we omit the unfolding
345 -- if there is a worker
346 show_unfold = not bottoming_fn && -- Not necessary
349 rhs_is_small && -- Small enough
350 okToUnfoldInHiFile rhs -- No casms etc
352 unfold_ids | show_unfold = exprSomeFreeVars isLocalId rhs
353 | otherwise = emptyVarSet
355 worker_ids = case worker_info of
356 HasWorker work_id _ -> unitVarSet work_id
357 otherwise -> emptyVarSet
361 %************************************************************************
363 \subsection{Step 2: top-level tidying}
365 %************************************************************************
369 type TopTidyEnv = (NameCache, TidyOccEnv, VarEnv Var)
371 -- TopTidyEnv: when tidying we need to know
372 -- * ns: The NameCache, containing a unique supply and any pre-ordained Names.
373 -- These may have arisen because the
374 -- renamer read in an interface file mentioning M.$wf, say,
375 -- and assigned it unique r77. If, on this compilation, we've
376 -- invented an Id whose name is $wf (but with a different unique)
377 -- we want to rename it to have unique r77, so that we can do easy
378 -- comparisons with stuff from the interface file
380 -- * occ_env: The TidyOccEnv, which tells us which local occurrences
383 -- * subst_env: A Var->Var mapping that substitutes the new Var for the old
388 tidyTopBind :: Module
389 -> IdEnv Bool -- Domain = Ids that should be external
390 -- True <=> their unfolding is external too
392 -> TopTidyEnv -> CoreBind
393 -> (TopTidyEnv, CoreBind)
395 tidyTopBind mod ext_ids cg_info_env top_tidy_env (NonRec bndr rhs)
396 = ((orig,occ,subst) , NonRec bndr' rhs')
398 ((orig,occ,subst), bndr')
399 = tidyTopBinder mod ext_ids cg_info_env
400 rec_tidy_env rhs rhs' top_tidy_env bndr
401 rec_tidy_env = (occ,subst)
402 rhs' = tidyExpr rec_tidy_env rhs
404 tidyTopBind mod ext_ids cg_info_env top_tidy_env (Rec prs)
405 = (final_env, Rec prs')
407 (final_env@(_,occ,subst), prs') = mapAccumL do_one top_tidy_env prs
408 rec_tidy_env = (occ,subst)
410 do_one top_tidy_env (bndr,rhs)
411 = ((orig,occ,subst), (bndr',rhs'))
413 ((orig,occ,subst), bndr')
414 = tidyTopBinder mod ext_ids cg_info_env
415 rec_tidy_env rhs rhs' top_tidy_env bndr
417 rhs' = tidyExpr rec_tidy_env rhs
419 tidyTopBinder :: Module -> IdEnv Bool -> CgInfoEnv
420 -> TidyEnv -- The TidyEnv is used to tidy the IdInfo
421 -> CoreExpr -- RHS *before* tidying
422 -> CoreExpr -- RHS *after* tidying
423 -- The TidyEnv and the after-tidying RHS are
424 -- both are knot-tied: don't look at them!
425 -> TopTidyEnv -> Id -> (TopTidyEnv, Id)
426 -- NB: tidyTopBinder doesn't affect the unique supply
428 tidyTopBinder mod ext_ids cg_info_env rec_tidy_env rhs tidy_rhs
429 env@(ns2, occ_env2, subst_env2) id
430 -- This function is the heart of Step 2
431 -- The rec_tidy_env is the one to use for the IdInfo
432 -- It's necessary because when we are dealing with a recursive
433 -- group, a variable late in the group might be mentioned
434 -- in the IdInfo of one early in the group
436 -- The rhs is already tidied
438 = ((orig_env', occ_env', subst_env'), id')
440 (orig_env', occ_env', name') = tidyTopName mod ns2 occ_env2
443 ty' = tidyTopType (idType id)
444 idinfo = tidyTopIdInfo rec_tidy_env is_external
445 (idInfo id) unfold_info arity
446 (lookupCgInfo cg_info_env name')
448 id' = mkVanillaGlobal name' ty' idinfo
450 subst_env' = extendVarEnv subst_env2 id id'
452 maybe_external = lookupVarEnv ext_ids id
453 is_external = isJust maybe_external
455 -- Expose an unfolding if ext_ids tells us to
456 -- Remember that ext_ids maps an Id to a Bool:
457 -- True to show the unfolding, False to hide it
458 show_unfold = maybe_external `orElse` False
459 unfold_info | show_unfold = mkTopUnfolding tidy_rhs
460 | otherwise = noUnfolding
462 -- Usually the Id will have an accurate arity on it, because
463 -- the simplifier has just run, but not always.
464 -- One case I found was when the last thing the simplifier
465 -- did was to let-bind a non-atomic argument and then float
466 -- it to the top level. So it seems more robust just to
468 arity = exprArity rhs
472 -- tidyTopIdInfo creates the final IdInfo for top-level
473 -- binders. There are two delicate pieces:
475 -- * Arity. After CoreTidy, this arity must not change any more.
476 -- Indeed, CorePrep must eta expand where necessary to make
477 -- the manifest arity equal to the claimed arity.
479 -- * CAF info, which comes from the CoreToStg pass via a knot.
480 -- The CAF info will not be looked at by the downstream stuff:
481 -- it *generates* it, and knot-ties it back. It will only be
482 -- looked at by (a) MkIface when generating an interface file
483 -- (b) In GHCi, importing modules
484 -- Nevertheless, we add the info here so that it propagates to all
485 -- occurrences of the binders in RHSs, and hence to occurrences in
486 -- unfoldings, which are inside Ids imported by GHCi. Ditto RULES.
488 -- An alterative would be to do a second pass over the unfoldings
489 -- of Ids, and rules, right at the top, but that would be a pain.
491 tidyTopIdInfo tidy_env is_external idinfo unfold_info arity cg_info
492 | opt_OmitInterfacePragmas -- If the interface file has no pragma info
493 = hasCafIdInfo -- then discard all info right here
494 -- This is not so important for *this* module, but it's
495 -- vital for ghc --make:
496 -- subsequent compilations must not see (e.g.) the arity if
497 -- the interface file does not contain arity
498 -- If they do, they'll exploit the arity; then the arity might
499 -- change, but the iface file doesn't change => recompilation
500 -- does not happen => disaster
502 -- This IdInfo will live long-term in the Id => need to make
503 -- conservative assumption about Caf-hood
505 | not is_external -- For internal Ids (not externally visible)
506 = vanillaIdInfo -- we only need enough info for code generation
507 -- Arity and strictness info are enough;
508 -- c.f. CoreTidy.tidyLetBndr
509 -- Use vanillaIdInfo (whose CafInfo is a panic) because we
510 -- should not need the CafInfo
512 `setAllStrictnessInfo` newStrictnessInfo idinfo
514 | otherwise -- Externally-visible Ids get the whole lot
518 `setAllStrictnessInfo` newStrictnessInfo idinfo
519 `setInlinePragInfo` inlinePragInfo idinfo
520 `setUnfoldingInfo` unfold_info
521 `setWorkerInfo` tidyWorker tidy_env (workerInfo idinfo)
522 -- NB: we throw away the Rules
523 -- They have already been extracted by findExternalRules
525 -- This is where we set names to local/global based on whether they really are
526 -- externally visible (see comment at the top of this module). If the name
527 -- was previously local, we have to give it a unique occurrence name if
528 -- we intend to externalise it.
529 tidyTopName mod ns occ_env external name
530 | global && internal = (ns, occ_env, localiseName name)
532 | global && external = (ns, occ_env, name)
533 -- Global names are assumed to have been allocated by the renamer,
534 -- so they already have the "right" unique
535 -- And it's a system-wide unique too
537 | local && internal = (ns_w_local, occ_env', new_local_name)
538 -- Even local, internal names must get a unique occurrence, because
539 -- if we do -split-objs we externalise the name later, in the code generator
541 -- Similarly, we must make sure it has a system-wide Unique, because
542 -- the byte-code generator builds a system-wide Name->BCO symbol table
544 | local && external = case lookupFM ns_names key of
545 Just orig -> (ns, occ_env', orig)
546 Nothing -> (ns_w_global, occ_env', new_external_name)
547 -- If we want to externalise a currently-local name, check
548 -- whether we have already assigned a unique for it.
549 -- If so, use it; if not, extend the table (ns_w_global).
550 -- This is needed when *re*-compiling a module in GHCi; we want to
551 -- use the same name for externally-visible things as we did before.
554 global = isExternalName name
556 internal = not external
558 (occ_env', occ') = tidyOccName occ_env (nameOccName name)
559 key = (moduleName mod, occ')
560 ns_names = nsNames ns
561 ns_uniqs = nsUniqs ns
562 (us1, us2) = splitUniqSupply ns_uniqs
563 uniq = uniqFromSupply us1
564 loc = nameSrcLoc name
566 new_local_name = mkInternalName uniq occ' loc
567 new_external_name = mkExternalName uniq mod occ' loc
569 ns_w_local = ns { nsUniqs = us2 }
570 ns_w_global = ns { nsUniqs = us2, nsNames = addToFM ns_names key new_external_name }
573 ------------ Worker --------------
574 tidyWorker tidy_env (HasWorker work_id wrap_arity)
575 = HasWorker (tidyVarOcc tidy_env work_id) wrap_arity
576 tidyWorker tidy_env other