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