2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplPgm]{Interface to the simplifier}
7 module SimplPgm ( simplifyPgm ) where
9 #include "HsVersions.h"
11 import CmdLineOpts ( opt_D_verbose_core2core, opt_D_dump_simpl_iterations,
12 switchIsOn, SimplifierSwitch(..), SwitchResult
15 import CoreUnfold ( SimpleUnfolding )
16 import CoreUtils ( substCoreExpr )
17 import Id ( mkIdEnv, lookupIdEnv, IdEnv
19 import Maybes ( catMaybes )
20 import OccurAnal ( occurAnalyseBinds )
21 import PprCore ( pprCoreBinding ) -- added SOF
24 import Simplify ( simplTopBinds )
25 import TyVar ( TyVarEnv )
26 import UniqSupply ( thenUs, returnUs, mapUs,
27 splitUniqSupply, UniqSM,
30 import Util ( isIn, isn'tIn, removeDups )
33 import GlaExts ( trace )
37 simplifyPgm :: [CoreBinding] -- input
38 -> (SimplifierSwitch->SwitchResult)
39 -> SimplCount -- info about how many times
40 -- each transformation has occurred
42 -> ([CoreBinding], -- output
43 Int, -- info about how much happened
44 SimplCount) -- accumulated simpl stats
46 simplifyPgm binds s_sw_chkr simpl_stats us
47 = --case (splitUniqSupply us) of { (s1, s2) ->
48 case (initSmpl us (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
49 (pgm2, it_count, combineSimplCounts simpl_stats simpl_stats2) }
51 simpl_switch_is_on = switchIsOn s_sw_chkr
53 max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
55 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
57 simpl_pgm n iterations pgm
58 = -- find out what top-level binders are used,
59 -- and prepare to unfold all the "simple" bindings
61 tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
64 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
66 -- Quit if we didn't actually do anything; otherwise,
67 -- try again (if suitable flags)
69 simplCount `thenSmpl` \ r ->
70 detailedSimplCount `thenSmpl` \ dr ->
72 show_status = pprTrace "Simplifer run: " (vcat [
73 hcat [ptext SLIT("iteration "),
75 ptext SLIT(" out of "),
76 int max_simpl_iterations],
77 text (showSimplCount dr),
78 if opt_D_dump_simpl_iterations then
79 vcat (map (pprCoreBinding) new_pgm)
85 (if opt_D_verbose_core2core
86 || simpl_switch_is_on ShowSimplifierProgress
90 (let stop_now = r == n {-nothing happened-}
91 || (if iterations >= max_simpl_iterations then
92 (if max_simpl_iterations > 1 {-otherwise too boring-} then
94 ("NOTE: Simplifier still going after " ++
95 show max_simpl_iterations ++
96 " iterations; bailing out.")
103 returnSmpl (new_pgm, iterations, dr)
105 simpl_pgm r (iterations + 1) new_pgm