[project @ 1997-03-14 07:52:06 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 #include "HsVersions.h"
8
9 module SimplPgm ( simplifyPgm ) where
10
11 IMP_Ubiq(){-uitous-}
12
13 import CmdLineOpts      ( opt_D_verbose_core2core, opt_D_dump_simpl_iterations,
14                           switchIsOn, SimplifierSwitch(..)
15                         )
16 import CoreSyn
17 import CoreUnfold       ( SimpleUnfolding )
18 import CoreUtils        ( substCoreExpr )
19 import Id               ( mkIdEnv, lookupIdEnv, SYN_IE(IdEnv),
20                           GenId{-instance Ord3-}
21                         )
22 import Maybes           ( catMaybes )
23 import OccurAnal        ( occurAnalyseBinds )
24 import Pretty           ( ppAboves, ppBesides, ppInt, ppChar, ppStr, ppPStr,
25                           ppNil
26                         )
27 import PprStyle         ( PprStyle(..) )   -- added SOF
28 import PprCore          ( pprCoreBinding ) -- added SOF
29 import SimplEnv
30 import SimplMonad
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 )
35 \end{code}
36
37 \begin{code}
38 simplifyPgm :: [CoreBinding]    -- input
39             -> (SimplifierSwitch->SwitchResult)
40             -> SimplCount       -- info about how many times
41                                 -- each transformation has occurred
42             -> UniqSupply
43             -> ([CoreBinding],  -- output
44                  Int,           -- info about how much happened
45                  SimplCount)    -- accumulated simpl stats
46
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) }
51   where
52     simpl_switch_is_on  = switchIsOn s_sw_chkr
53
54     max_simpl_iterations = getSimplIntSwitch s_sw_chkr MaxSimplifierIterations
55
56     simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
57
58     simpl_pgm n iterations pgm
59       = -- find out what top-level binders are used,
60         -- and prepare to unfold all the "simple" bindings
61         let
62             tagged_pgm = _scc_ "OccAnal" occurAnalyseBinds pgm simpl_switch_is_on
63         in
64               -- do the business
65         simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
66
67               -- Quit if we didn't actually do anything; otherwise,
68               -- try again (if suitable flags)
69
70         simplCount                              `thenSmpl` \ r ->
71         detailedSimplCount                      `thenSmpl` \ dr ->
72         let
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)
78                 else
79                         ppNil
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