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