2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplPgm]{Interface to the simplifier}
7 #include "HsVersions.h"
9 module SimplPgm ( simplifyPgm ) where
13 import CmdLineOpts ( opt_D_verbose_core2core, opt_D_dump_simpl_iterations,
14 switchIsOn, SimplifierSwitch(..), SYN_IE(SwitchResult)
17 import CoreUnfold ( SimpleUnfolding )
18 import CoreUtils ( substCoreExpr )
19 import Id ( mkIdEnv, lookupIdEnv, SYN_IE(IdEnv),
20 GenId{-instance Ord3-}
22 import Maybes ( catMaybes )
23 import OccurAnal ( occurAnalyseBinds )
24 import Pretty ( Doc, vcat, hcat, int, char, text, ptext, empty )
25 import PprStyle ( PprStyle(..) ) -- added SOF
26 import PprCore ( pprCoreBinding ) -- added SOF
29 import Simplify ( simplTopBinds )
30 import TyVar ( nullTyVarEnv, SYN_IE(TyVarEnv) )
31 import UniqSupply ( thenUs, returnUs, mapUs,
32 splitUniqSupply, SYN_IE(UniqSM),
35 import Util ( isIn, isn'tIn, removeDups, pprTrace )
39 simplifyPgm :: [CoreBinding] -- input
40 -> (SimplifierSwitch->SwitchResult)
41 -> SimplCount -- info about how many times
42 -- each transformation has occurred
44 -> ([CoreBinding], -- output
45 Int, -- info about how much happened
46 SimplCount) -- accumulated simpl stats
48 simplifyPgm binds s_sw_chkr simpl_stats us
49 = --case (splitUniqSupply us) of { (s1, s2) ->
50 case (initSmpl us (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
51 (pgm2, it_count, combineSimplCounts simpl_stats simpl_stats2) }
53 simpl_switch_is_on = switchIsOn s_sw_chkr
55 max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
57 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
59 simpl_pgm n iterations pgm
60 = -- find out what top-level binders are used,
61 -- and prepare to unfold all the "simple" bindings
63 tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
66 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
68 -- Quit if we didn't actually do anything; otherwise,
69 -- try again (if suitable flags)
71 simplCount `thenSmpl` \ r ->
72 detailedSimplCount `thenSmpl` \ dr ->
74 show_status = pprTrace "Simplifer run: " (vcat [
75 hcat [ptext SLIT("iteration "),
77 ptext SLIT(" out of "),
78 int max_simpl_iterations],
79 text (showSimplCount dr),
80 if opt_D_dump_simpl_iterations then
81 vcat (map (pprCoreBinding PprDebug) new_pgm)
87 (if opt_D_verbose_core2core
88 || simpl_switch_is_on ShowSimplifierProgress
92 (let stop_now = r == n {-nothing happened-}
93 || (if iterations >= max_simpl_iterations then
94 (if max_simpl_iterations > 1 {-otherwise too boring-} then
96 ("NOTE: Simplifier still going after " ++
97 show max_simpl_iterations ++
98 " iterations; bailing out.")
105 returnSmpl (new_pgm, iterations, dr)
107 simpl_pgm r (iterations + 1) new_pgm