[project @ 2003-10-10 12:42:30 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / SimplCore.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[SimplCore]{Driver for simplifying @Core@ programs}
5
6 \begin{code}
7 module SimplCore ( core2core, simplifyExpr ) where
8
9 #include "HsVersions.h"
10
11 import CmdLineOpts      ( CoreToDo(..), SimplifierSwitch(..),
12                           SimplifierMode(..), DynFlags, DynFlag(..), dopt,
13                           dopt_CoreToDo, buildCoreToDo
14                         )
15 import CoreSyn
16 import CoreFVs          ( ruleRhsFreeVars )
17 import TcIface          ( loadImportedRules )
18 import HscTypes         ( HscEnv(..), GhciMode(..),
19                           ModGuts(..), ModGuts, Avails, availsToNameSet, 
20                           ModDetails(..),
21                           HomeModInfo(..), ExternalPackageState(..), hscEPS
22                         )
23 import CSE              ( cseProgram )
24 import Rules            ( RuleBase, emptyRuleBase, ruleBaseIds, 
25                           extendRuleBaseList, pprRuleBase, 
26                           ruleCheckProgram )
27 import Module           ( moduleEnvElts )
28 import Name             ( Name, isExternalName )
29 import NameSet          ( elemNameSet )
30 import PprCore          ( pprCoreBindings, pprCoreExpr )
31 import OccurAnal        ( occurAnalyseBinds, occurAnalyseGlobalExpr )
32 import CoreUtils        ( coreBindsSize )
33 import Simplify         ( simplTopBinds, simplExpr )
34 import SimplUtils       ( simplBinders )
35 import SimplMonad
36 import ErrUtils         ( dumpIfSet, dumpIfSet_dyn, showPass )
37 import CoreLint         ( endPass )
38 import FloatIn          ( floatInwards )
39 import FloatOut         ( floatOutwards )
40 import Id               ( idName, setIdLocalExported )
41 import VarSet
42 import LiberateCase     ( liberateCase )
43 import SAT              ( doStaticArgs )
44 import Specialise       ( specProgram)
45 import SpecConstr       ( specConstrProgram)
46 import DmdAnal          ( dmdAnalPgm )
47 import WorkWrap         ( wwTopBinds )
48 #ifdef OLD_STRICTNESS
49 import StrictAnal       ( saBinds )
50 import CprAnalyse       ( cprAnalyse )
51 #endif
52
53 import UniqSupply       ( UniqSupply, mkSplitUniqSupply, splitUniqSupply )
54 import IO               ( hPutStr, stderr )
55 import Outputable
56
57 import Maybes           ( orElse )
58 import List             ( partition )
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{The driver for the simplifier}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 core2core :: HscEnv
69           -> ModGuts
70           -> IO ModGuts
71
72 core2core hsc_env 
73           mod_impl@(ModGuts { mg_exports = exports, 
74                               mg_binds = binds_in })
75   = do
76         let dflags        = hsc_dflags hsc_env
77             ghci_mode     = hsc_mode hsc_env
78             core_todos
79                 | Just todo <- dopt_CoreToDo dflags  =  todo
80                 | otherwise                          =  buildCoreToDo dflags
81
82         us <-  mkSplitUniqSupply 's'
83         let (cp_us, ru_us) = splitUniqSupply us
84
85                 -- COMPUTE THE RULE BASE TO USE
86         (rule_base, local_rule_ids, orphan_rules)
87                 <- prepareRules hsc_env mod_impl ru_us
88
89                 -- PREPARE THE BINDINGS
90         let binds1 = updateBinders ghci_mode local_rule_ids 
91                                    orphan_rules exports binds_in
92
93                 -- DO THE BUSINESS
94         (stats, processed_binds)
95                 <- doCorePasses dflags rule_base (zeroSimplCount dflags) cp_us binds1 core_todos
96
97         dumpIfSet_dyn dflags Opt_D_dump_simpl_stats
98                   "Grand total simplifier statistics"
99                   (pprSimplCount stats)
100
101         -- Return results
102         -- We only return local orphan rules, i.e., local rules not attached to an Id
103         -- The bindings cotain more rules, embedded in the Ids
104         return (mod_impl { mg_binds = processed_binds, mg_rules = orphan_rules})
105
106
107 simplifyExpr :: DynFlags -- includes spec of what core-to-core passes to do
108              -> CoreExpr
109              -> IO CoreExpr
110 -- simplifyExpr is called by the driver to simplify an
111 -- expression typed in at the interactive prompt
112 simplifyExpr dflags expr
113   = do  {
114         ; showPass dflags "Simplify"
115
116         ; us <-  mkSplitUniqSupply 's'
117
118         ; let env              = emptySimplEnv SimplGently [] emptyVarSet
119               (expr', _counts) = initSmpl dflags us (simplExprGently env expr)
120
121         ; dumpIfSet_dyn dflags Opt_D_dump_simpl "Simplified expression"
122                         (pprCoreExpr expr')
123
124         ; return expr'
125         }
126
127
128 doCorePasses :: DynFlags
129              -> RuleBase        -- the main rule base
130              -> SimplCount      -- simplifier stats
131              -> UniqSupply      -- uniques
132              -> [CoreBind]      -- local binds in (with rules attached)
133              -> [CoreToDo]      -- which passes to do
134              -> IO (SimplCount, [CoreBind])  -- stats, binds, local orphan rules
135
136 doCorePasses dflags rb stats us binds []
137   = return (stats, binds)
138
139 doCorePasses dflags rb stats us binds (to_do : to_dos) 
140   = do
141         let (us1, us2) = splitUniqSupply us
142
143         (stats1, binds1) <- doCorePass dflags rb us1 binds to_do
144
145         doCorePasses dflags rb (stats `plusSimplCount` stats1) us2 binds1 to_dos
146
147 doCorePass dfs rb us binds (CoreDoSimplify mode switches) 
148    = _scc_ "Simplify"      simplifyPgm dfs rb mode switches us binds
149 doCorePass dfs rb us binds CoreCSE                      
150    = _scc_ "CommonSubExpr" noStats dfs (cseProgram dfs binds)
151 doCorePass dfs rb us binds CoreLiberateCase             
152    = _scc_ "LiberateCase"  noStats dfs (liberateCase dfs binds)
153 doCorePass dfs rb us binds CoreDoFloatInwards       
154    = _scc_ "FloatInwards"  noStats dfs (floatInwards dfs binds)
155 doCorePass dfs rb us binds (CoreDoFloatOutwards f)  
156    = _scc_ "FloatOutwards" noStats dfs (floatOutwards dfs f us binds)
157 doCorePass dfs rb us binds CoreDoStaticArgs             
158    = _scc_ "StaticArgs"    noStats dfs (doStaticArgs us binds)
159 doCorePass dfs rb us binds CoreDoStrictness             
160    = _scc_ "Stranal"       noStats dfs (dmdAnalPgm dfs binds)
161 doCorePass dfs rb us binds CoreDoWorkerWrapper      
162    = _scc_ "WorkWrap"      noStats dfs (wwTopBinds dfs us binds)
163 doCorePass dfs rb us binds CoreDoSpecialising       
164    = _scc_ "Specialise"    noStats dfs (specProgram dfs us binds)
165 doCorePass dfs rb us binds CoreDoSpecConstr
166    = _scc_ "SpecConstr"    noStats dfs (specConstrProgram dfs us binds)
167 #ifdef OLD_STRICTNESS
168 doCorePass dfs rb us binds CoreDoOldStrictness
169    = _scc_ "OldStrictness"      noStats dfs (doOldStrictness dfs binds)
170 #endif
171 doCorePass dfs rb us binds CoreDoPrintCore              
172    = _scc_ "PrintCore"     noStats dfs (printCore binds)
173 doCorePass dfs rb us binds CoreDoGlomBinds              
174    = noStats dfs (glomBinds dfs binds)
175 doCorePass dfs rb us binds (CoreDoRuleCheck phase pat)
176    = noStats dfs (ruleCheck dfs phase pat binds)
177 doCorePass dfs rb us binds CoreDoNothing
178    = noStats dfs (return binds)
179
180 #ifdef OLD_STRICTNESS
181 doOldStrictness dfs binds 
182   = do binds1 <- saBinds dfs binds
183        binds2 <- cprAnalyse dfs binds1
184        return binds2
185 #endif
186
187 printCore binds = do dumpIfSet True "Print Core"
188                                (pprCoreBindings binds)
189                      return binds
190
191 ruleCheck dflags phase pat binds = do showPass dflags "RuleCheck"
192                                       printDump (ruleCheckProgram phase pat binds)
193                                       return binds
194
195 -- most passes return no stats and don't change rules
196 noStats dfs thing = do { binds <- thing; return (zeroSimplCount dfs, binds) }
197
198 \end{code}
199
200
201
202 %************************************************************************
203 %*                                                                      *
204 \subsection{Dealing with rules}
205 %*                                                                      *
206 %************************************************************************
207
208 -- prepareLocalRuleBase takes the CoreBinds and rules defined in this module.
209 -- It attaches those rules that are for local Ids to their binders, and
210 -- returns the remainder attached to Ids in an IdSet.  It also returns
211 -- Ids mentioned on LHS of some rule; these should be blacklisted.
212
213 -- The rule Ids and LHS Ids are black-listed; that is, they aren't inlined
214 -- so that the opportunity to apply the rule isn't lost too soon
215
216 \begin{code}
217 prepareRules :: HscEnv 
218              -> ModGuts
219              -> UniqSupply
220              -> IO (RuleBase,           -- Full rule base
221                     IdSet,              -- Local rule Ids
222                     [IdCoreRule])       -- Orphan rules defined in this module
223
224 prepareRules hsc_env@(HscEnv { hsc_dflags = dflags, hsc_HPT = hpt })
225              (ModGuts { mg_binds = binds, mg_rules = local_rules,
226                         mg_deps = deps })
227              us 
228   = do  { pkg_rule_base <- loadImportedRules hsc_env deps
229
230         ; let env              = emptySimplEnv SimplGently [] local_ids 
231               (better_rules,_) = initSmpl dflags us (mapSmpl (simplRule env) local_rules)
232
233         ; let (local_rules, orphan_rules) = partition ((`elemVarSet` local_ids) . fst) better_rules
234                 -- We use (`elemVarSet` local_ids) rather than isLocalId because
235                 -- isLocalId isn't true of class methods.
236                 -- If we miss any rules for Ids defined here, then we end up
237                 -- giving the local decl a new Unique (because the in-scope-set is the
238                 -- same as the rule-id set), and now the binding for the class method 
239                 -- doesn't have the same Unique as the one in the Class and the tc-env
240                 --      Example:        class Foo a where
241                 --                        op :: a -> a
242                 --                      {-# RULES "op" op x = x #-}
243               local_rule_base = extendRuleBaseList emptyRuleBase local_rules
244               local_rule_ids  = ruleBaseIds local_rule_base     -- Local Ids with rules attached
245
246               imp_rule_base   = foldl add_rules pkg_rule_base (moduleEnvElts hpt)
247               final_rule_base = extendRuleBaseList imp_rule_base orphan_rules
248
249         ; dumpIfSet_dyn dflags Opt_D_dump_rules "Transformation rules"
250                 (vcat [text "Local rules", pprRuleBase local_rule_base,
251                        text "",
252                        text "Imported rules", pprRuleBase final_rule_base])
253
254         ; return (final_rule_base, local_rule_ids, orphan_rules)
255     }
256   where
257     add_rules rule_base mod_info = extendRuleBaseList rule_base (md_rules (hm_details mod_info))
258
259         -- Boringly, we need to gather the in-scope set.
260     local_ids = foldr (unionVarSet . mkVarSet . bindersOf) emptyVarSet binds
261
262
263 updateBinders :: GhciMode
264               -> IdSet                  -- Locally defined ids with their Rules attached
265               -> [IdCoreRule]           -- Orphan rules
266               -> Avails                 -- What is exported
267               -> [CoreBind] -> [CoreBind]
268         -- A horrible function
269
270 -- Update the binders of top-level bindings as follows
271 --      a) Attach the rules for each locally-defined Id to that Id.
272 --      b) Set the no-discard flag if either the Id is exported,
273 --         or it's mentioned in the RHS of a rule
274 --
275 -- You might wonder why exported Ids aren't already marked as such;
276 -- it's just because the type checker is rather busy already and
277 -- I didn't want to pass in yet another mapping.
278 -- 
279 -- Reason for (a)
280 --      - It makes the rules easier to look up
281 --      - It means that transformation rules and specialisations for
282 --        locally defined Ids are handled uniformly
283 --      - It keeps alive things that are referred to only from a rule
284 --        (the occurrence analyser knows about rules attached to Ids)
285 --      - It makes sure that, when we apply a rule, the free vars
286 --        of the RHS are more likely to be in scope
287 --
288 -- Reason for (b)
289 --     It means that the binding won't be discarded EVEN if the binding
290 --     ends up being trivial (v = w) -- the simplifier would usually just 
291 --     substitute w for v throughout, but we don't apply the substitution to
292 --     the rules (maybe we should?), so this substitution would make the rule
293 --     bogus.
294
295 updateBinders ghci_mode rule_ids orphan_rules exports binds
296   = map update_bndrs binds
297   where
298     update_bndrs (NonRec b r) = NonRec (update_bndr b) r
299     update_bndrs (Rec prs)    = Rec [(update_bndr b, r) | (b,r) <- prs]
300
301     update_bndr bndr 
302         | dont_discard bndr = setIdLocalExported bndr_with_rules
303         | otherwise         = bndr_with_rules
304         where
305           bndr_with_rules = lookupVarSet rule_ids bndr `orElse` bndr
306
307     orph_rhs_fvs = unionVarSets (map (ruleRhsFreeVars . snd) orphan_rules)
308         -- An orphan rule must keep alive the free vars 
309         -- of its right-hand side.  
310         -- Non-orphan rules are attached to the Id (bndr_with_rules above)
311         -- and that keeps the rhs free vars alive
312
313     dont_discard bndr =  is_exported (idName bndr)
314                       || bndr `elemVarSet` orph_rhs_fvs 
315
316         -- In interactive mode, we don't want to discard any top-level
317         -- entities at all (eg. do not inline them away during
318         -- simplification), and retain them all in the TypeEnv so they are
319         -- available from the command line.
320         --
321         -- isExternalName separates the user-defined top-level names from those
322         -- introduced by the type checker.
323     is_exported :: Name -> Bool
324     is_exported | ghci_mode == Interactive = isExternalName
325                 | otherwise                = (`elemNameSet` export_fvs)
326
327     export_fvs = availsToNameSet exports
328 \end{code}
329
330
331 We must do some gentle simplification on the template (but not the RHS)
332 of each rule.  The case that forced me to add this was the fold/build rule,
333 which without simplification looked like:
334         fold k z (build (/\a. g a))  ==>  ...
335 This doesn't match unless you do eta reduction on the build argument.
336
337 \begin{code}
338 simplRule env rule@(id, BuiltinRule _ _)
339   = returnSmpl rule
340 simplRule env rule@(id, Rule act name bndrs args rhs)
341   = simplBinders env bndrs              `thenSmpl` \ (env, bndrs') -> 
342     mapSmpl (simplExprGently env) args  `thenSmpl` \ args' ->
343     simplExprGently env rhs             `thenSmpl` \ rhs' ->
344     returnSmpl (id, Rule act name bndrs' args' rhs')
345
346 -- It's important that simplExprGently does eta reduction.
347 -- For example, in a rule like:
348 --      augment g (build h) 
349 -- we do not want to get
350 --      augment (\a. g a) (build h)
351 -- otherwise we don't match when given an argument like
352 --      (\a. h a a)
353 --
354 -- The simplifier does indeed do eta reduction (it's in
355 -- Simplify.completeLam) but only if -O is on.
356 \end{code}
357
358 \begin{code}
359 simplExprGently :: SimplEnv -> CoreExpr -> SimplM CoreExpr
360 -- Simplifies an expression 
361 --      does occurrence analysis, then simplification
362 --      and repeats (twice currently) because one pass
363 --      alone leaves tons of crud.
364 -- Used (a) for user expressions typed in at the interactive prompt
365 --      (b) the LHS and RHS of a RULE
366 --
367 -- The name 'Gently' suggests that the SimplifierMode is SimplGently,
368 -- and in fact that is so.... but the 'Gently' in simplExprGently doesn't
369 -- enforce that; it just simplifies the expression twice
370
371 simplExprGently env expr
372   = simplExpr env (occurAnalyseGlobalExpr expr)         `thenSmpl` \ expr1 ->
373     simplExpr env (occurAnalyseGlobalExpr expr1)
374 \end{code}
375
376
377 %************************************************************************
378 %*                                                                      *
379 \subsection{Glomming}
380 %*                                                                      *
381 %************************************************************************
382
383 \begin{code}
384 glomBinds :: DynFlags -> [CoreBind] -> IO [CoreBind]
385 -- Glom all binds together in one Rec, in case any
386 -- transformations have introduced any new dependencies
387 --
388 -- NB: the global invariant is this:
389 --      *** the top level bindings are never cloned, and are always unique ***
390 --
391 -- We sort them into dependency order, but applying transformation rules may
392 -- make something at the top refer to something at the bottom:
393 --      f = \x -> p (q x)
394 --      h = \y -> 3
395 --      
396 --      RULE:  p (q x) = h x
397 --
398 -- Applying this rule makes f refer to h, 
399 -- although it doesn't appear to in the source program.  
400 -- This pass lets us control where it happens.
401 --
402 -- NOTICE that this cannot happen for rules whose head is a locally-defined
403 -- function.  It only happens for rules whose head is an imported function
404 -- (p in the example above).  So, for example, the rule had been
405 --      RULE: f (p x) = h x
406 -- then the rule for f would be attached to f itself (in its IdInfo) 
407 -- by prepareLocalRuleBase and h would be regarded by the occurrency 
408 -- analyser as free in f.
409
410 glomBinds dflags binds
411   = do { showPass dflags "GlomBinds" ;
412          let { recd_binds = [Rec (flattenBinds binds)] } ;
413          return recd_binds }
414         -- Not much point in printing the result... 
415         -- just consumes output bandwidth
416 \end{code}
417
418
419 %************************************************************************
420 %*                                                                      *
421 \subsection{The driver for the simplifier}
422 %*                                                                      *
423 %************************************************************************
424
425 \begin{code}
426 simplifyPgm :: DynFlags 
427             -> RuleBase
428             -> SimplifierMode
429             -> [SimplifierSwitch]
430             -> UniqSupply
431             -> [CoreBind]                   -- Input
432             -> IO (SimplCount, [CoreBind])  -- New bindings
433
434 simplifyPgm dflags rule_base
435             mode switches us binds
436   = do {
437         showPass dflags "Simplify";
438
439         (termination_msg, it_count, counts_out, binds') 
440            <- iteration us 1 (zeroSimplCount dflags) binds;
441
442         dumpIfSet (dopt Opt_D_verbose_core2core dflags 
443                    && dopt Opt_D_dump_simpl_stats dflags)
444                   "Simplifier statistics"
445                   (vcat [text termination_msg <+> text "after" <+> ppr it_count <+> text "iterations",
446                          text "",
447                          pprSimplCount counts_out]);
448
449         endPass dflags "Simplify" Opt_D_verbose_core2core binds';
450
451         return (counts_out, binds')
452     }
453   where
454     phase_info        = case mode of
455                           SimplGently  -> "gentle"
456                           SimplPhase n -> show n
457
458     imported_rule_ids = ruleBaseIds rule_base
459     simpl_env         = emptySimplEnv mode switches imported_rule_ids
460     sw_chkr           = getSwitchChecker simpl_env
461     max_iterations    = intSwitchSet sw_chkr MaxSimplifierIterations `orElse` 2
462  
463     iteration us iteration_no counts binds
464         -- iteration_no is the number of the iteration we are
465         -- about to begin, with '1' for the first
466       | iteration_no > max_iterations   -- Stop if we've run out of iterations
467       = do {
468 #ifdef DEBUG
469             if  max_iterations > 2 then
470                 hPutStr stderr ("NOTE: Simplifier still going after " ++ 
471                                 show max_iterations ++ 
472                                 " iterations; bailing out.\n")
473             else 
474                 return ();
475 #endif
476                 -- Subtract 1 from iteration_no to get the
477                 -- number of iterations we actually completed
478             return ("Simplifier baled out", iteration_no - 1, counts, binds)
479         }
480
481       -- Try and force thunks off the binds; significantly reduces
482       -- space usage, especially with -O.  JRS, 000620.
483       | let sz = coreBindsSize binds in sz == sz
484       = do {
485                 -- Occurrence analysis
486            let { tagged_binds = _scc_ "OccAnal" occurAnalyseBinds binds } ;
487
488            dumpIfSet_dyn dflags Opt_D_dump_occur_anal "Occurrence analysis"
489                      (pprCoreBindings tagged_binds);
490
491                 -- SIMPLIFY
492                 -- We do this with a *case* not a *let* because lazy pattern
493                 -- matching bit us with bad space leak!
494                 -- With a let, we ended up with
495                 --   let
496                 --      t = initSmpl ...
497                 --      counts' = snd t
498                 --   in
499                 --      case t of {(_,counts') -> if counts'=0 then ... }
500                 -- So the conditional didn't force counts', because the
501                 -- selection got duplicated.  Sigh!
502            case initSmpl dflags us1 (simplTopBinds simpl_env tagged_binds) of {
503                 (binds', counts') -> do {
504                         -- The imported_rule_ids are used by initSmpl to initialise
505                         -- the in-scope set.  That way, the simplifier will change any
506                         -- occurrences of the imported id to the one in the imported_rule_ids
507                         -- set, which are decorated with their rules.
508
509            let { all_counts = counts `plusSimplCount` counts' ;
510                  herald     = "Simplifier phase " ++ phase_info ++ 
511                               ", iteration " ++ show iteration_no ++
512                               " out of " ++ show max_iterations
513                 } ;
514
515                 -- Stop if nothing happened; don't dump output
516            if isZeroSimplCount counts' then
517                 return ("Simplifier reached fixed point", iteration_no, all_counts, binds')
518            else do {
519
520                 -- Dump the result of this iteration
521            dumpIfSet_dyn dflags Opt_D_dump_simpl_iterations herald
522                          (pprSimplCount counts') ;
523
524            endPass dflags herald Opt_D_dump_simpl_iterations binds' ;
525
526                 -- Loop
527            iteration us2 (iteration_no + 1) all_counts binds'
528         }  } } }
529       where
530           (us1, us2) = splitUniqSupply us
531 \end{code}