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 Id ( mkIdEnv, lookupIdEnv, IdEnv
17 import Maybes ( catMaybes )
18 import OccurAnal ( occurAnalyseBinds )
19 import PprCore ( pprCoreBinding ) -- added SOF
22 import Simplify ( simplTopBinds )
23 import TyVar ( TyVarEnv )
24 import UniqSupply ( thenUs, returnUs, mapUs,
25 splitUniqSupply, UniqSM,
28 import Util ( isIn, isn'tIn, removeDups, trace )
34 simplifyPgm :: [CoreBinding] -- input
35 -> (SimplifierSwitch->SwitchResult)
36 -> SimplCount -- info about how many times
37 -- each transformation has occurred
39 -> ([CoreBinding], -- output
40 Int, -- info about how much happened
41 SimplCount) -- accumulated simpl stats
43 simplifyPgm binds s_sw_chkr simpl_stats us
44 = --case (splitUniqSupply us) of { (s1, s2) ->
45 case (initSmpl us (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
46 (pgm2, it_count, combineSimplCounts simpl_stats simpl_stats2) }
48 simpl_switch_is_on = switchIsOn s_sw_chkr
50 max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
52 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
54 simpl_pgm n iterations pgm
55 = -- find out what top-level binders are used,
56 -- and prepare to unfold all the "simple" bindings
58 tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
61 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
63 -- Quit if we didn't actually do anything; otherwise,
64 -- try again (if suitable flags)
66 simplCount `thenSmpl` \ r ->
67 detailedSimplCount `thenSmpl` \ dr ->
69 show_status = pprTrace "Simplifer run: " (vcat [
70 hcat [ptext SLIT("iteration "),
72 ptext SLIT(" out of "),
73 int max_simpl_iterations],
74 text (showSimplCount dr),
75 if opt_D_dump_simpl_iterations then
76 vcat (map (pprCoreBinding) new_pgm)
82 (if opt_D_verbose_core2core
83 || simpl_switch_is_on ShowSimplifierProgress
87 (let stop_now = r == n {-nothing happened-}
88 || (if iterations >= max_simpl_iterations then
89 (if max_simpl_iterations > 1 {-otherwise too boring-} then
91 ("NOTE: Simplifier still going after " ++
92 show max_simpl_iterations ++
93 " iterations; bailing out.")
100 returnSmpl (new_pgm, iterations, dr)
102 simpl_pgm r (iterations + 1) new_pgm