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 )
31 import GlaExts ( trace )
35 simplifyPgm :: [CoreBinding] -- input
36 -> (SimplifierSwitch->SwitchResult)
37 -> SimplCount -- info about how many times
38 -- each transformation has occurred
40 -> ([CoreBinding], -- output
41 Int, -- info about how much happened
42 SimplCount) -- accumulated simpl stats
44 simplifyPgm binds s_sw_chkr simpl_stats us
45 = --case (splitUniqSupply us) of { (s1, s2) ->
46 case (initSmpl us (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
47 (pgm2, it_count, combineSimplCounts simpl_stats simpl_stats2) }
49 simpl_switch_is_on = switchIsOn s_sw_chkr
51 max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
53 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
55 simpl_pgm n iterations pgm
56 = -- find out what top-level binders are used,
57 -- and prepare to unfold all the "simple" bindings
59 tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
62 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
64 -- Quit if we didn't actually do anything; otherwise,
65 -- try again (if suitable flags)
67 simplCount `thenSmpl` \ r ->
68 detailedSimplCount `thenSmpl` \ dr ->
70 show_status = pprTrace "Simplifer run: " (vcat [
71 hcat [ptext SLIT("iteration "),
73 ptext SLIT(" out of "),
74 int max_simpl_iterations],
75 text (showSimplCount dr),
76 if opt_D_dump_simpl_iterations then
77 vcat (map (pprCoreBinding) new_pgm)
83 (if opt_D_verbose_core2core
84 || simpl_switch_is_on ShowSimplifierProgress
88 (let stop_now = r == n {-nothing happened-}
89 || (if iterations >= max_simpl_iterations then
90 (if max_simpl_iterations > 1 {-otherwise too boring-} then
92 ("NOTE: Simplifier still going after " ++
93 show max_simpl_iterations ++
94 " iterations; bailing out.")
101 returnSmpl (new_pgm, iterations, dr)
103 simpl_pgm r (iterations + 1) new_pgm