[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / simplStg / SimplStg.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
5
6 \begin{code}
7 module SimplStg ( stg2stg ) where
8
9 #include "HsVersions.h"
10
11 import StgSyn
12
13 import LambdaLift       ( liftProgram )
14 import CostCentre       ( CostCentre, CostCentreStack )
15 import SCCfinal         ( stgMassageForProfiling )
16 import StgLint          ( lintStgBindings )
17 import StgStats         ( showStgStats )
18 import StgVarInfo       ( setStgVarInfo )
19 import UpdAnal          ( updateAnalyse )
20 import SRT              ( computeSRTs )
21
22 import CmdLineOpts      ( opt_SccGroup,
23                           opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
24                           opt_DoStgLinting,
25                           StgToDo(..)
26                         )
27 import Id               ( Id )
28 import VarEnv
29 import ErrUtils         ( doIfSet )
30 import UniqSupply       ( splitUniqSupply, UniqSupply )
31 import Util             ( panic, assertPanic, trace )
32 import IO               ( hPutStr, stderr )
33 import Outputable
34 \end{code}
35
36 \begin{code}
37 stg2stg :: [StgToDo]            -- spec of what stg-to-stg passes to do
38         -> FAST_STRING          -- module name (profiling only)
39         -> UniqSupply           -- a name supply
40         -> [StgBinding]         -- input...
41         -> IO
42             ([(StgBinding,[Id])],  -- output program...
43              ([CostCentre],        -- local cost-centres that need to be decl'd
44               [CostCentre],        -- "extern" cost-centres
45               [CostCentreStack]))  -- pre-defined "singleton" cost centre stacks
46
47 stg2stg stg_todos module_name us binds
48   = case (splitUniqSupply us)   of { (us4now, us4later) ->
49
50     doIfSet do_verbose_stg2stg
51         (printErrs (text "VERBOSE STG-TO-STG:" $$
52                     text "*** Core2Stg:" $$
53                     vcat (map ppr (setStgVarInfo False binds)))) >>
54
55         -- Do the main business!
56     foldl_mn do_stg_pass (binds, us4now, ([],[],[])) stg_todos
57                 >>= \ (processed_binds, _, cost_centres) ->
58
59         --      Do essential wind-up
60
61         -- Essential wind-up: part (b), do setStgVarInfo. It has to
62         -- happen regardless, because the code generator uses its
63         -- decorations.
64         --
65         -- Why does it have to happen last?  Because earlier passes
66         -- may move things around, which would change the live-var
67         -- info.  Also, setStgVarInfo decides about let-no-escape
68         -- things, which in turn do a better job if arities are
69         -- correct, which is done by satStgRhs.
70         --
71
72     let
73         annotated_binds = setStgVarInfo do_let_no_escapes processed_binds
74         srt_binds       = computeSRTs annotated_binds
75     in
76
77     return (srt_binds, cost_centres)
78    }
79   where
80     do_let_no_escapes  = opt_StgDoLetNoEscapes
81     do_verbose_stg2stg = opt_D_verbose_stg2stg
82
83     grp_name  = case (opt_SccGroup) of
84                   Just xx -> _PK_ xx
85                   Nothing -> module_name -- default: module name
86
87     -------------
88     stg_linter = if opt_DoStgLinting
89                  then lintStgBindings
90                  else ( \ whodunnit binds -> binds )
91
92     -------------------------------------------
93     do_stg_pass (binds, us, ccs) to_do
94       = let
95             (us1, us2) = splitUniqSupply us
96         in
97         case to_do of
98           StgDoStaticArgs ->  panic "STG static argument transformation deleted"
99
100           StgDoUpdateAnalysis ->
101              _scc_ "StgUpdAnal"
102                 -- NB We have to do setStgVarInfo first!  (There's one
103                 -- place free-var info is used) But no let-no-escapes,
104                 -- because update analysis doesn't care.
105              end_pass us2 "UpdAnal" ccs (updateAnalyse (setStgVarInfo False binds))
106
107           D_stg_stats ->
108              trace (showStgStats binds)
109              end_pass us2 "StgStats" ccs binds
110
111           StgDoLambdaLift ->
112              _scc_ "StgLambdaLift"
113                 -- NB We have to do setStgVarInfo first!
114              let
115                 binds3 = liftProgram module_name us1 (setStgVarInfo do_let_no_escapes binds)
116              in
117              end_pass us2 "LambdaLift" ccs binds3
118
119           StgDoMassageForProfiling ->
120              _scc_ "ProfMassage"
121              let
122                  (collected_CCs, binds3)
123                    = stgMassageForProfiling module_name grp_name us1 binds
124              in
125              end_pass us2 "ProfMassage" collected_CCs binds3
126
127     end_pass us2 what ccs binds2
128       = -- report verbosely, if required
129         (if do_verbose_stg2stg then
130             hPutStr stderr (show
131               (text ("*** "++what++":") $$ vcat (map ppr binds2)
132             ))
133          else return ()) >>
134         let
135             linted_binds = stg_linter what binds2
136         in
137         return (linted_binds, us2, ccs)
138             -- return: processed binds
139             --         UniqueSupply for the next guy to use
140             --         cost-centres to be declared/registered (specialised)
141             --         add to description of what's happened (reverse order)
142
143 -- here so it can be inlined...
144 foldl_mn f z []     = return z
145 foldl_mn f z (x:xs) = f z x     >>= \ zz ->
146                      foldl_mn f zz xs
147 \end{code}