9346a92be6d282b25d172408e2acd8eb05502a93
[ghc-hetmet.git] / ghc / compiler / main / TidyPgm.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Tidying up Core}
5
6 \begin{code}
7 module TidyPgm( tidyCorePgm, tidyCoreExpr ) where
8
9 #include "HsVersions.h"
10
11 import CmdLineOpts      ( DynFlags, DynFlag(..), opt_OmitInterfacePragmas )
12 import CoreSyn
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 )
19 import VarEnv
20 import VarSet
21 import Var              ( Id, Var )
22 import Id               ( idType, idInfo, idName, idCoreRules, 
23                           isExportedId, mkVanillaGlobal, isLocalId, 
24                           isImplicitId 
25                         ) 
26 import IdInfo           {- loads of stuff -}
27 import NewDemand        ( isBottomingSig, topSig )
28 import BasicTypes       ( isNeverActive )
29 import Name             ( getOccName, nameOccName, mkInternalName, 
30                           localiseName, isExternalName, nameSrcLoc
31                         )
32 import RnEnv            ( lookupOrigNameCache, newExternalName )
33 import NameEnv          ( filterNameEnv )
34 import OccName          ( TidyOccEnv, initTidyOccEnv, tidyOccName )
35 import Type             ( tidyTopType )
36 import Module           ( Module )
37 import HscTypes         ( PersistentCompilerState( pcs_nc ), 
38                           NameCache( nsNames, nsUniqs ),
39                           TypeEnv, extendTypeEnvList, typeEnvIds,
40                           ModGuts(..), ModGuts, TyThing(..)
41                         )
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 )
49 import Outputable
50 \end{code}
51
52
53
54 %************************************************************************
55 %*                                                                      *
56 \subsection{What goes on}
57 %*                                                                      * 
58 %************************************************************************
59
60 [SLPJ: 19 Nov 00]
61
62 The plan is this.  
63
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
68 unit.  These are
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
75 IdEnv Bool
76
77
78 Step 2: Tidy the program
79 ~~~~~~~~~~~~~~~~~~~~~~~~
80 Next we traverse the bindings top to bottom.  For each *top-level*
81 binder
82
83  1. Make it into a GlobalId
84
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.]
88
89     We use the NameCache kept in the PersistentCompilerState as the
90     source of such system-wide uniques.
91
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.
95   
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
100
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).
104   
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
108
109         * its unfolding, if it should have one
110         
111         * its arity, computed from the number of visible lambdas
112
113         * its CAF info, computed from what is free in its RHS
114
115                 
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.
119
120 \begin{code}
121 tidyCorePgm :: DynFlags
122             -> PersistentCompilerState
123             -> CgInfoEnv                -- Information from the back end,
124                                         -- to be splatted into the IdInfo
125             -> ModGuts
126             -> IO (PersistentCompilerState, ModGuts)
127
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"
133
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.)
141
142         -- We also make sure to avoid any exported binders.  Consider
143         --      f{-u1-} = 1     -- Local decl
144         --      ...
145         --      f{-u2-} = 2     -- Exported decl
146         --
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,
154                                                    isExternalName name]
155                 -- In computing our "avoids" list, we must include
156                 --      all implicit Ids
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.
161
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
165
166         ; let tidy_rules = tidyIdRules (occ_env,subst_env) ext_rules
167
168         ; let pcs' = pcs { pcs_nc = orig_ns' }
169
170         ; let final_ids  = [ id 
171                            | bind <- tidy_binds
172                            , id <- bindersOf bind
173                            , isExternalName (idName id)]
174
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)
179
180
181         ; let tidy_type_env = mkFinalTypeEnv env_tc final_ids
182               tidy_dfun_ids = map lookup_dfun_id insts_tc
183
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 }
188
189         ; endPass dflags "Tidy Core" Opt_D_dump_simpl tidy_binds
190         ; dumpIfSet_core dflags Opt_D_dump_simpl
191                 "Tidy Core Rules"
192                 (pprIdRules tidy_rules)
193
194         ; return (pcs', tidy_result)
195         }
196
197 tidyCoreExpr :: CoreExpr -> IO CoreExpr
198 tidyCoreExpr expr = return (tidyExpr emptyTidyEnv expr)
199 \end{code}
200
201
202 %************************************************************************
203 %*                                                                      *
204 \subsection{Write a new interface file}
205 %*                                                                      *
206 %************************************************************************
207
208 \begin{code}
209 mkFinalTypeEnv :: TypeEnv       -- From typechecker
210                -> [Id]          -- Final Ids
211                -> TypeEnv
212
213 mkFinalTypeEnv type_env final_ids
214   = extendTypeEnvList (filterNameEnv keep_it type_env)
215                       (map AnId final_ids)
216   where
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
227         --
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
231
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
236 \end{code}
237
238 \begin{code}
239 findExternalRules :: [CoreBind]
240                   -> [IdCoreRule] -- Orphan rules
241                   -> IdEnv a      -- Ids that are exported, so we need their rules
242                   -> [IdCoreRule]
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 = []
248   | otherwise
249   = filter needed_rule (orphan_rules ++ local_rules)
250   where
251     local_rules  = [ rule
252                    | id <- bindersOfBinds binds,
253                      id `elemVarEnv` ext_ids,
254                      rule <- idCoreRules id
255                    ]
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
261
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
265                 -- importing module)
266
267     internal_id id = isLocalId id && not (id `elemVarEnv` ext_ids)
268 \end{code}
269
270 %************************************************************************
271 %*                                                                      *
272 \subsection{Step 1: finding externals}
273 %*                                                                      * 
274 %************************************************************************
275
276 \begin{code}
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
283   where
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.)
293
294     find (NonRec id rhs) needed
295         | need_id needed id = addExternal (id,rhs) needed
296         | otherwise         = needed
297     find (Rec prs) needed   = find_prs prs needed
298
299         -- For a recursive group we have to look for a fixed point
300     find_prs prs needed 
301         | null needed_prs = needed
302         | otherwise       = find_prs other_prs new_needed
303         where
304           (needed_prs, other_prs) = partition (need_pr needed) prs
305           new_needed = foldr addExternal needed needed_prs
306
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
312
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)
318                  id show_unfold
319   where
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
323
324     new_needed_ids | opt_OmitInterfacePragmas = emptyVarSet
325                    | otherwise                = worker_ids      `unionVarSet`
326                                                 unfold_ids      `unionVarSet`
327                                                 spec_ids
328
329     idinfo         = idInfo id
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
335
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)
341
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
347                   not dont_inline        &&
348                   not loop_breaker       &&
349                   rhs_is_small           &&     -- Small enough
350                   okToUnfoldInHiFile rhs        -- No casms etc
351
352     unfold_ids | show_unfold = exprSomeFreeVars isLocalId rhs
353                | otherwise   = emptyVarSet
354
355     worker_ids = case worker_info of
356                    HasWorker work_id _ -> unitVarSet work_id
357                    otherwise           -> emptyVarSet
358 \end{code}
359
360
361 %************************************************************************
362 %*                                                                      *
363 \subsection{Step 2: top-level tidying}
364 %*                                                                      *
365 %************************************************************************
366
367
368 \begin{code}
369 type TopTidyEnv = (NameCache, TidyOccEnv, VarEnv Var)
370
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
379 --
380 --   * occ_env: The TidyOccEnv, which tells us which local occurrences 
381 --     are 'used'
382 --
383 --   * subst_env: A Var->Var mapping that substitutes the new Var for the old
384 \end{code}
385
386
387 \begin{code}
388 tidyTopBind :: Module
389             -> IdEnv Bool       -- Domain = Ids that should be external
390                                 -- True <=> their unfolding is external too
391             -> CgInfoEnv
392             -> TopTidyEnv -> CoreBind
393             -> (TopTidyEnv, CoreBind)
394
395 tidyTopBind mod ext_ids cg_info_env top_tidy_env (NonRec bndr rhs)
396   = ((orig,occ,subst) , NonRec bndr' rhs')
397   where
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
403
404 tidyTopBind mod ext_ids cg_info_env top_tidy_env (Rec prs)
405   = (final_env, Rec prs')
406   where
407     (final_env@(_,occ,subst), prs') = mapAccumL do_one top_tidy_env prs
408     rec_tidy_env = (occ,subst)
409
410     do_one top_tidy_env (bndr,rhs) 
411         = ((orig,occ,subst), (bndr',rhs'))
412         where
413         ((orig,occ,subst), bndr')
414            = tidyTopBinder mod ext_ids cg_info_env
415                 rec_tidy_env rhs rhs' top_tidy_env bndr
416
417         rhs' = tidyExpr rec_tidy_env rhs
418
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
427
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
435
436         -- The rhs is already tidied
437         
438   = ((orig_env', occ_env', subst_env'), id')
439   where
440     (orig_env', occ_env', name') = tidyTopName mod ns2 occ_env2
441                                                is_external
442                                                (idName id)
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')
447
448     id' = mkVanillaGlobal name' ty' idinfo
449
450     subst_env' = extendVarEnv subst_env2 id id'
451
452     maybe_external = lookupVarEnv ext_ids id
453     is_external    = isJust maybe_external
454
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
461
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
467     -- fix it here.
468     arity = exprArity rhs
469
470
471
472 -- tidyTopIdInfo creates the final IdInfo for top-level
473 -- binders.  There are two delicate pieces:
474 --
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.
478 --
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.
487 --     
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.
490
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
501         --
502         -- This IdInfo will live long-term in the Id => need to make
503         -- conservative assumption about Caf-hood
504
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
511         `setArityInfo`         arity
512         `setAllStrictnessInfo` newStrictnessInfo idinfo
513
514   | otherwise           -- Externally-visible Ids get the whole lot
515   = vanillaIdInfo
516         `setCgInfo`            cg_info
517         `setArityInfo`         arity
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
524
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)
531
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
536
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
540         --
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
543
544   | local  && external = case lookupOrigNameCache ns_names mod occ' 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.
552
553   where
554     global           = isExternalName name
555     local            = not global
556     internal         = not external
557     loc              = nameSrcLoc name
558
559     (occ_env', occ') = tidyOccName occ_env (nameOccName name)
560
561     ns_names         = nsNames ns
562     (us1, us2)       = splitUniqSupply (nsUniqs ns)
563     uniq             = uniqFromSupply us1
564     new_local_name   = mkInternalName uniq occ' loc
565     ns_w_local       = ns { nsUniqs = us2 }
566
567     (ns_w_global, new_external_name) = newExternalName ns mod occ' loc
568
569
570 ------------  Worker  --------------
571 tidyWorker tidy_env (HasWorker work_id wrap_arity) 
572   = HasWorker (tidyVarOcc tidy_env work_id) wrap_arity
573 tidyWorker tidy_env other
574   = NoWorker
575 \end{code}