2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[SimplCore]{Driver for simplifying @Core@ programs}
7 module SimplCore ( core2core ) where
9 #include "HsVersions.h"
11 import CmdLineOpts ( CoreToDo(..), SimplifierSwitch(..),
12 SwitchResult(..), intSwitchSet,
14 DynFlags, DynFlag(..), dopt
16 import CoreLint ( beginPass, endPass )
18 import CoreFVs ( ruleSomeFreeVars )
19 import HscTypes ( PackageRuleBase, HomeSymbolTable, ModDetails(..) )
20 import CSE ( cseProgram )
21 import Rules ( RuleBase, emptyRuleBase, ruleBaseFVs, ruleBaseIds, extendRuleBaseList, addRuleBaseFVs )
22 import Module ( moduleEnvElts )
24 import PprCore ( pprCoreBindings, pprIdCoreRule )
25 import OccurAnal ( occurAnalyseBinds )
26 import CoreUtils ( etaReduceExpr, coreBindsSize )
27 import Simplify ( simplTopBinds, simplExpr )
28 import SimplUtils ( simplBinders )
30 import ErrUtils ( dumpIfSet, dumpIfSet_dyn )
31 import FloatIn ( floatInwards )
32 import FloatOut ( floatOutwards )
33 import Id ( Id, isDataConWrapId, setIdNoDiscard )
35 import LiberateCase ( liberateCase )
36 import SAT ( doStaticArgs )
37 import Specialise ( specProgram)
38 import UsageSPInf ( doUsageSPInf )
39 import StrictAnal ( saBinds )
40 import WorkWrap ( wwTopBinds )
41 import CprAnalyse ( cprAnalyse )
43 import UniqSupply ( UniqSupply, mkSplitUniqSupply, splitUniqSupply )
44 import IO ( hPutStr, stderr )
47 import List ( partition )
50 %************************************************************************
52 \subsection{The driver for the simplifier}
54 %************************************************************************
58 -> PackageRuleBase -- Rule-base accumulated from imported packages
60 -> [CoreToDo] -- Spec of what core-to-core passes to do
61 -> [CoreBind] -- Binds in
62 -> [IdCoreRule] -- Rules in
63 -> IO ([CoreBind], [IdCoreRule]) -- binds, local orphan rules out
65 core2core dflags pkg_rule_base hst core_todos binds rules
67 us <- mkSplitUniqSupply 's'
68 let (cp_us, ru_us) = splitUniqSupply us
70 -- COMPUTE THE RULE BASE TO USE
71 (rule_base, binds1, orphan_rules)
72 <- prepareRules dflags pkg_rule_base hst ru_us binds rules
76 (stats, processed_binds)
77 <- doCorePasses dflags rule_base (zeroSimplCount dflags) cp_us binds1 core_todos
79 dumpIfSet_dyn dflags Opt_D_dump_simpl_stats
80 "Grand total simplifier statistics"
84 -- We only return local orphan rules, i.e., local rules not attached to an Id
85 -- The bindings cotain more rules, embedded in the Ids
86 return (processed_binds, orphan_rules)
89 doCorePasses :: DynFlags
90 -> RuleBase -- the main rule base
91 -> SimplCount -- simplifier stats
92 -> UniqSupply -- uniques
93 -> [CoreBind] -- local binds in (with rules attached)
94 -> [CoreToDo] -- which passes to do
95 -> IO (SimplCount, [CoreBind]) -- stats, binds, local orphan rules
97 doCorePasses dflags rb stats us binds []
98 = return (stats, binds)
100 doCorePasses dflags rb stats us binds (to_do : to_dos)
102 let (us1, us2) = splitUniqSupply us
104 (stats1, binds1) <- doCorePass dflags rb us1 binds to_do
106 doCorePasses dflags rb (stats `plusSimplCount` stats1) us2 binds1 to_dos
108 doCorePass dfs rb us binds (CoreDoSimplify sw_chkr)
109 = _scc_ "Simplify" simplifyPgm dfs rb sw_chkr us binds
110 doCorePass dfs rb us binds CoreCSE
111 = _scc_ "CommonSubExpr" noStats dfs (cseProgram dfs binds)
112 doCorePass dfs rb us binds CoreLiberateCase
113 = _scc_ "LiberateCase" noStats dfs (liberateCase dfs binds)
114 doCorePass dfs rb us binds CoreDoFloatInwards
115 = _scc_ "FloatInwards" noStats dfs (floatInwards dfs binds)
116 doCorePass dfs rb us binds (CoreDoFloatOutwards f)
117 = _scc_ "FloatOutwards" noStats dfs (floatOutwards dfs f us binds)
118 doCorePass dfs rb us binds CoreDoStaticArgs
119 = _scc_ "StaticArgs" noStats dfs (doStaticArgs us binds)
120 doCorePass dfs rb us binds CoreDoStrictness
121 = _scc_ "Stranal" noStats dfs (saBinds dfs binds)
122 doCorePass dfs rb us binds CoreDoWorkerWrapper
123 = _scc_ "WorkWrap" noStats dfs (wwTopBinds dfs us binds)
124 doCorePass dfs rb us binds CoreDoSpecialising
125 = _scc_ "Specialise" noStats dfs (specProgram dfs us binds)
126 doCorePass dfs rb us binds CoreDoCPResult
127 = _scc_ "CPResult" noStats dfs (cprAnalyse dfs binds)
128 doCorePass dfs rb us binds CoreDoPrintCore
129 = _scc_ "PrintCore" noStats dfs (printCore binds)
130 doCorePass dfs rb us binds CoreDoUSPInf
131 = _scc_ "CoreUsageSPInf" noStats dfs (doUsageSPInf dfs us binds)
132 doCorePass dfs rb us binds CoreDoGlomBinds
133 = noStats dfs (glomBinds dfs binds)
135 printCore binds = do dumpIfSet True "Print Core"
136 (pprCoreBindings binds)
139 -- most passes return no stats and don't change rules
140 noStats dfs thing = do { binds <- thing; return (zeroSimplCount dfs, binds) }
145 %************************************************************************
147 \subsection{Dealing with rules}
149 %************************************************************************
151 -- prepareLocalRuleBase takes the CoreBinds and rules defined in this module.
152 -- It attaches those rules that are for local Ids to their binders, and
153 -- returns the remainder attached to Ids in an IdSet. It also returns
154 -- Ids mentioned on LHS of some rule; these should be blacklisted.
156 -- The rule Ids and LHS Ids are black-listed; that is, they aren't inlined
157 -- so that the opportunity to apply the rule isn't lost too soon
160 prepareRules :: DynFlags -> PackageRuleBase -> HomeSymbolTable
162 -> [CoreBind] -> [IdCoreRule] -- Local bindings and rules
163 -> IO (RuleBase, -- Full rule base
164 [CoreBind], -- Bindings augmented with rules
165 [IdCoreRule]) -- Orphan rules
167 prepareRules dflags pkg_rule_base hst us binds rules
168 = do { let (better_rules,_) = initSmpl dflags sw_chkr us local_ids black_list_all
169 (mapSmpl simplRule rules)
171 ; dumpIfSet_dyn dflags Opt_D_dump_rules "Transformation rules"
172 (vcat (map pprIdCoreRule better_rules))
174 ; let (local_id_rules, orphan_rules) = partition ((`elemVarSet` local_ids) . fst) better_rules
175 (binds1, local_rule_fvs) = addRulesToBinds binds local_id_rules
176 imp_rule_base = foldl add_rules pkg_rule_base (moduleEnvElts hst)
177 rule_base = extendRuleBaseList imp_rule_base orphan_rules
178 final_rule_base = addRuleBaseFVs rule_base local_rule_fvs
179 -- The last step black-lists the free vars of local rules too
181 ; return (final_rule_base, binds1, orphan_rules)
184 sw_chkr any = SwBool False -- A bit bogus
185 black_list_all v = not (isDataConWrapId v)
186 -- This stops all inlining except the
187 -- wrappers for data constructors
189 add_rules rule_base mds = extendRuleBaseList rule_base (md_rules mds)
191 -- Boringly, we need to gather the in-scope set.
192 -- Typically this thunk won't even be forced, but the test in
193 -- simpVar fails if it isn't right, and it might conceiveably matter
194 local_ids = foldr (unionVarSet . mkVarSet . bindersOf) emptyVarSet binds
196 addRulesToBinds :: [CoreBind] -> [(Id,CoreRule)] -> ([CoreBind], IdSet)
197 -- A horrible function
199 -- Attach the rules for each locally-defined Id to that Id.
200 -- - This makes the rules easier to look up
201 -- - It means that transformation rules and specialisations for
202 -- locally defined Ids are handled uniformly
203 -- - It keeps alive things that are referred to only from a rule
204 -- (the occurrence analyser knows about rules attached to Ids)
205 -- - It makes sure that, when we apply a rule, the free vars
206 -- of the RHS are more likely to be in scope
208 -- Both the LHS and RHS Ids are marked 'no-discard'.
209 -- This means that the binding won't be discarded EVEN if the binding
210 -- ends up being trivial (v = w) -- the simplifier would usually just
211 -- substitute w for v throughout, but we don't apply the substitution to
212 -- the rules (maybe we should?), so this substitution would make the rule
215 addRulesToBinds binds local_rules
216 = (map zap_bind binds, rule_lhs_fvs)
218 -- rule_fvs is the set of all variables mentioned in this module's rules
219 rule_fvs = unionVarSets [ ruleSomeFreeVars isId rule | (_,rule) <- local_rules ]
221 rule_base = extendRuleBaseList emptyRuleBase local_rules
222 rule_lhs_fvs = ruleBaseFVs rule_base
223 rule_ids = ruleBaseIds rule_base
225 zap_bind (NonRec b r) = NonRec (zap_bndr b) r
226 zap_bind (Rec prs) = Rec [(zap_bndr b, r) | (b,r) <- prs]
228 zap_bndr bndr = case lookupVarSet rule_ids bndr of
229 Just bndr' -> setIdNoDiscard bndr'
230 Nothing | bndr `elemVarSet` rule_fvs -> setIdNoDiscard bndr
235 We must do some gentle simplification on the template (but not the RHS)
236 of each rule. The case that forced me to add this was the fold/build rule,
237 which without simplification looked like:
238 fold k z (build (/\a. g a)) ==> ...
239 This doesn't match unless you do eta reduction on the build argument.
242 simplRule rule@(id, BuiltinRule _)
244 simplRule rule@(id, Rule name bndrs args rhs)
245 = simplBinders bndrs $ \ bndrs' ->
246 mapSmpl simpl_arg args `thenSmpl` \ args' ->
247 simplExpr rhs `thenSmpl` \ rhs' ->
248 returnSmpl (id, Rule name bndrs' args' rhs')
251 -- I've seen rules in which a LHS like
252 -- augment g (build h)
254 -- augment (\a. g a) (build h)
255 -- So it's a help to eta-reduce the args as we simplify them.
256 -- Otherwise we don't match when given an argument like
258 = simplExpr e `thenSmpl` \ e' ->
259 returnSmpl (etaReduceExpr e')
263 %************************************************************************
265 \subsection{Glomming}
267 %************************************************************************
270 glomBinds :: DynFlags -> [CoreBind] -> IO [CoreBind]
271 -- Glom all binds together in one Rec, in case any
272 -- transformations have introduced any new dependencies
274 -- NB: the global invariant is this:
275 -- *** the top level bindings are never cloned, and are always unique ***
277 -- We sort them into dependency order, but applying transformation rules may
278 -- make something at the top refer to something at the bottom:
282 -- RULE: p (q x) = h x
284 -- Applying this rule makes f refer to h,
285 -- although it doesn't appear to in the source program.
286 -- This pass lets us control where it happens.
288 -- NOTICE that this cannot happen for rules whose head is a locally-defined
289 -- function. It only happens for rules whose head is an imported function
290 -- (p in the example above). So, for example, the rule had been
291 -- RULE: f (p x) = h x
292 -- then the rule for f would be attached to f itself (in its IdInfo)
293 -- by prepareLocalRuleBase and h would be regarded by the occurrency
294 -- analyser as free in f.
296 glomBinds dflags binds
297 = do { beginPass dflags "GlomBinds" ;
298 let { recd_binds = [Rec (flattenBinds binds)] } ;
300 -- Not much point in printing the result...
301 -- just consumes output bandwidth
305 %************************************************************************
307 \subsection{The driver for the simplifier}
309 %************************************************************************
312 simplifyPgm :: DynFlags
314 -> (SimplifierSwitch -> SwitchResult)
316 -> [CoreBind] -- Input
317 -> IO (SimplCount, [CoreBind]) -- New bindings
319 simplifyPgm dflags rule_base
322 beginPass dflags "Simplify";
324 (termination_msg, it_count, counts_out, binds')
325 <- iteration us 1 (zeroSimplCount dflags) binds;
327 dumpIfSet (dopt Opt_D_verbose_core2core dflags
328 && dopt Opt_D_dump_simpl_stats dflags)
329 "Simplifier statistics"
330 (vcat [text termination_msg <+> text "after" <+> ppr it_count <+> text "iterations",
332 pprSimplCount counts_out]);
334 endPass dflags "Simplify"
335 (dopt Opt_D_verbose_core2core dflags
336 && not (dopt Opt_D_dump_simpl_iterations dflags))
339 return (counts_out, binds')
342 max_iterations = getSimplIntSwitch sw_chkr MaxSimplifierIterations
343 black_list_fn = blackListed rule_lhs_fvs (intSwitchSet sw_chkr SimplInlinePhase)
344 imported_rule_ids = ruleBaseIds rule_base
345 rule_lhs_fvs = ruleBaseFVs rule_base
347 iteration us iteration_no counts binds
348 -- Try and force thunks off the binds; significantly reduces
349 -- space usage, especially with -O. JRS, 000620.
350 | let sz = coreBindsSize binds in sz == sz
352 -- Occurrence analysis
353 let { tagged_binds = _scc_ "OccAnal" occurAnalyseBinds binds } ;
355 dumpIfSet_dyn dflags Opt_D_dump_occur_anal "Occurrence analysis"
356 (pprCoreBindings tagged_binds);
359 -- We do this with a *case* not a *let* because lazy pattern
360 -- matching bit us with bad space leak!
361 -- With a let, we ended up with
366 -- case t of {(_,counts') -> if counts'=0 then ...
367 -- So the conditional didn't force counts', because the
368 -- selection got duplicated. Sigh!
369 case initSmpl dflags sw_chkr us1 imported_rule_ids black_list_fn
370 (simplTopBinds tagged_binds)
371 of { (binds', counts') -> do {
372 -- The imported_rule_ids are used by initSmpl to initialise
373 -- the in-scope set. That way, the simplifier will change any
374 -- occurrences of the imported id to the one in the imported_rule_ids
375 -- set, which are decorated with their rules.
377 let { all_counts = counts `plusSimplCount` counts' } ;
379 -- Stop if nothing happened; don't dump output
380 if isZeroSimplCount counts' then
381 return ("Simplifier reached fixed point", iteration_no, all_counts, binds')
384 -- Dump the result of this iteration
385 dumpIfSet_dyn dflags Opt_D_dump_simpl_iterations
386 ("Simplifier iteration " ++ show iteration_no
387 ++ " out of " ++ show max_iterations)
388 (pprSimplCount counts') ;
390 if dopt Opt_D_dump_simpl_iterations dflags then
392 ("Simplifier iteration " ++ show iteration_no ++ " result")
393 (dopt Opt_D_verbose_core2core dflags)
398 -- Stop if we've run out of iterations
399 if iteration_no == max_iterations then
402 if max_iterations > 2 then
403 hPutStr stderr ("NOTE: Simplifier still going after " ++
404 show max_iterations ++
405 " iterations; bailing out.\n")
410 return ("Simplifier baled out", iteration_no, all_counts, binds')
414 else iteration us2 (iteration_no + 1) all_counts binds'
417 (us1, us2) = splitUniqSupply us