[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / compiler / simplCore / SimplPgm.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[SimplPgm]{Interface to the simplifier}
5
6 \begin{code}
7 module SimplPgm ( simplifyPgm ) where
8
9 #include "HsVersions.h"
10
11 import CmdLineOpts      ( opt_D_verbose_core2core, opt_D_dump_simpl_iterations,
12                           switchIsOn, SimplifierSwitch(..), SwitchResult
13                         )
14 import CoreSyn
15 import Id               ( mkIdEnv, lookupIdEnv, IdEnv
16                         )
17 import Maybes           ( catMaybes )
18 import OccurAnal        ( occurAnalyseBinds )
19 import PprCore          ( pprCoreBinding ) -- added SOF
20 import SimplEnv
21 import SimplMonad
22 import Simplify         ( simplTopBinds )
23 import TyVar            ( TyVarEnv )
24 import UniqSupply       ( thenUs, returnUs, mapUs, 
25                           splitUniqSupply, UniqSM,
26                           UniqSupply
27                          )
28 import Util             ( isIn, isn'tIn, removeDups, trace )
29 import Outputable 
30
31 \end{code}
32
33 \begin{code}
34 simplifyPgm :: [CoreBinding]    -- input
35             -> (SimplifierSwitch->SwitchResult)
36             -> SimplCount       -- info about how many times
37                                 -- each transformation has occurred
38             -> UniqSupply
39             -> ([CoreBinding],  -- output
40                  Int,           -- info about how much happened
41                  SimplCount)    -- accumulated simpl stats
42
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) }
47   where
48     simpl_switch_is_on  = switchIsOn s_sw_chkr
49
50     max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
51
52     simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
53
54     simpl_pgm n iterations pgm
55       = -- find out what top-level binders are used,
56         -- and prepare to unfold all the "simple" bindings
57         let
58             tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
59         in
60               -- do the business
61         simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
62
63               -- Quit if we didn't actually do anything; otherwise,
64               -- try again (if suitable flags)
65
66         simplCount                              `thenSmpl` \ r ->
67         detailedSimplCount                      `thenSmpl` \ dr ->
68         let
69             show_status = pprTrace "Simplifer run: " (vcat [
70                 hcat [ptext SLIT("iteration "), 
71                            int iterations, 
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)
77                 else
78                         empty
79                 ])
80         in
81
82         (if opt_D_verbose_core2core
83          || simpl_switch_is_on  ShowSimplifierProgress
84          then show_status
85          else id)
86
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
90                                 trace
91                                 ("NOTE: Simplifier still going after " ++ 
92                                   show max_simpl_iterations ++ 
93                                   " iterations; bailing out.")
94                              else id)
95                             True
96                          else
97                             False)
98         in
99         if stop_now then
100             returnSmpl (new_pgm, iterations, dr)
101         else
102             simpl_pgm r (iterations + 1) new_pgm
103         )
104 \end{code}
105