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(..)
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 ( ppAboves, ppBesides, ppInt, ppChar, ppStr, ppPStr,
27 import PprStyle ( PprStyle(..) ) -- added SOF
28 import PprCore ( pprCoreBinding ) -- added SOF
31 import Simplify ( simplTopBinds )
32 import TyVar ( nullTyVarEnv, SYN_IE(TyVarEnv) )
33 import UniqSupply ( thenUs, returnUs, mapUs, splitUniqSupply, SYN_IE(UniqSM) )
34 import Util ( isIn, isn'tIn, removeDups, pprTrace )
38 simplifyPgm :: [CoreBinding] -- input
39 -> (SimplifierSwitch->SwitchResult)
40 -> SimplCount -- info about how many times
41 -- each transformation has occurred
43 -> ([CoreBinding], -- output
44 Int, -- info about how much happened
45 SimplCount) -- accumulated simpl stats
47 simplifyPgm binds s_sw_chkr simpl_stats us
48 = --case (splitUniqSupply us) of { (s1, s2) ->
49 case (initSmpl us (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
50 (pgm2, it_count, combineSimplCounts simpl_stats simpl_stats2) }
52 simpl_switch_is_on = switchIsOn s_sw_chkr
54 max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
56 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
58 simpl_pgm n iterations pgm
59 = -- find out what top-level binders are used,
60 -- and prepare to unfold all the "simple" bindings
62 tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
65 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
67 -- Quit if we didn't actually do anything; otherwise,
68 -- try again (if suitable flags)
70 simplCount `thenSmpl` \ r ->
71 detailedSimplCount `thenSmpl` \ dr ->
73 show_status = pprTrace "Simplifer run: " (ppAboves [
74 ppBesides [ppPStr SLIT("iteration "), ppInt iterations, ppPStr SLIT(" out of "), ppInt max_simpl_iterations],
75 ppStr (showSimplCount dr),
76 if opt_D_dump_simpl_iterations then
77 ppAboves (map (pprCoreBinding PprDebug) 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