2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
7 module SimplStg ( stg2stg ) where
9 #include "HsVersions.h"
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 SRT ( computeSRTs )
21 import CmdLineOpts ( DynFlags, DynFlag(..), dopt,
22 opt_StgDoLetNoEscapes,
26 import Module ( Module, moduleString )
27 import ErrUtils ( doIfSet_dyn, dumpIfSet_dyn )
28 import UniqSupply ( splitUniqSupply, UniqSupply )
29 import IO ( hPutStr, stdout )
35 -> [StgToDo] -- spec of what stg-to-stg passes to do
36 -> Module -- module name (profiling only)
37 -> UniqSupply -- a name supply
38 -> [StgBinding] -- input...
40 ([(StgBinding,[Id])], -- output program...
41 ([CostCentre], -- local cost-centres that need to be decl'd
42 [CostCentre], -- "extern" cost-centres
43 [CostCentreStack])) -- pre-defined "singleton" cost centre stacks
45 stg2stg dflags stg_todos module_name us binds
46 = case (splitUniqSupply us) of { (us4now, us4later) ->
48 doIfSet_dyn dflags Opt_D_verbose_stg2stg (printErrs (text "VERBOSE STG-TO-STG:")) >>
50 end_pass us4now "Core2Stg" ([],[],[]) binds
51 >>= \ (binds', us, ccs) ->
53 -- Do the main business!
54 foldl_mn do_stg_pass (binds', us, ccs) stg_todos
55 >>= \ (processed_binds, _, cost_centres) ->
57 -- Do essential wind-up
59 -- Essential wind-up: part (b), do setStgVarInfo. It has to
60 -- happen regardless, because the code generator uses its
63 -- Why does it have to happen last? Because earlier passes
64 -- may move things around, which would change the live-var
65 -- info. Also, setStgVarInfo decides about let-no-escape
66 -- things, which in turn do a better job if arities are
67 -- correct, which is done by satStgRhs.
71 annotated_binds = setStgVarInfo opt_StgDoLetNoEscapes processed_binds
72 srt_binds = computeSRTs annotated_binds
75 dumpIfSet_dyn dflags Opt_D_dump_stg "STG syntax:"
76 (pprStgBindingsWithSRTs srt_binds) >>
78 return (srt_binds, cost_centres)
82 stg_linter = if dopt Opt_DoStgLinting dflags
84 else ( \ whodunnit binds -> binds )
86 -------------------------------------------
87 do_stg_pass (binds, us, ccs) to_do
89 (us1, us2) = splitUniqSupply us
92 StgDoStaticArgs -> panic "STG static argument transformation deleted"
95 trace (showStgStats binds)
96 end_pass us2 "StgStats" ccs binds
100 -- NB We have to do setStgVarInfo first!
102 binds3 = liftProgram module_name us1 (setStgVarInfo opt_StgDoLetNoEscapes binds)
104 end_pass us2 "LambdaLift" ccs binds3
106 StgDoMassageForProfiling ->
109 (collected_CCs, binds3)
110 = stgMassageForProfiling module_name us1 binds
112 end_pass us2 "ProfMassage" collected_CCs binds3
114 end_pass us2 what ccs binds2
115 = -- report verbosely, if required
116 (if dopt Opt_D_verbose_stg2stg dflags then
117 hPutStr stdout (showSDoc
118 (text ("*** "++what++":") $$ vcat (map ppr binds2)
122 linted_binds = stg_linter what binds2
124 return (linted_binds, us2, ccs)
125 -- return: processed binds
126 -- UniqueSupply for the next guy to use
127 -- cost-centres to be declared/registered (specialised)
128 -- add to description of what's happened (reverse order)
130 -- here so it can be inlined...
131 foldl_mn f z [] = return z
132 foldl_mn f z (x:xs) = f z x >>= \ zz ->