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