e365817dfcd7ffff7188c891abec3ab11a3dbe11
[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 )
29 import Outputable 
30
31 import GlaExts          ( trace )
32 \end{code}
33
34 \begin{code}
35 simplifyPgm :: [CoreBinding]    -- input
36             -> (SimplifierSwitch->SwitchResult)
37             -> SimplCount       -- info about how many times
38                                 -- each transformation has occurred
39             -> UniqSupply
40             -> ([CoreBinding],  -- output
41                  Int,           -- info about how much happened
42                  SimplCount)    -- accumulated simpl stats
43
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) }
48   where
49     simpl_switch_is_on  = switchIsOn s_sw_chkr
50
51     max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
52
53     simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
54
55     simpl_pgm n iterations pgm
56       = -- find out what top-level binders are used,
57         -- and prepare to unfold all the "simple" bindings
58         let
59             tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
60         in
61               -- do the business
62         simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
63
64               -- Quit if we didn't actually do anything; otherwise,
65               -- try again (if suitable flags)
66
67         simplCount                              `thenSmpl` \ r ->
68         detailedSimplCount                      `thenSmpl` \ dr ->
69         let
70             show_status = pprTrace "Simplifer run: " (vcat [
71                 hcat [ptext SLIT("iteration "), 
72                            int iterations, 
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)
78                 else
79                         empty
80                 ])
81         in
82
83         (if opt_D_verbose_core2core
84          || simpl_switch_is_on  ShowSimplifierProgress
85          then show_status
86          else id)
87
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
91                                 trace
92                                 ("NOTE: Simplifier still going after " ++ 
93                                   show max_simpl_iterations ++ 
94                                   " iterations; bailing out.")
95                              else id)
96                             True
97                          else
98                             False)
99         in
100         if stop_now then
101             returnSmpl (new_pgm, iterations, dr)
102         else
103             simpl_pgm r (iterations + 1) new_pgm
104         )
105 \end{code}
106