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 UpdAnal ( updateAnalyse )
20 import SRT ( computeSRTs )
22 import CmdLineOpts ( opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
23 opt_DoStgLinting, opt_D_dump_stg,
27 import Module ( Module, moduleString )
29 import ErrUtils ( doIfSet, dumpIfSet )
30 import UniqSupply ( splitUniqSupply, UniqSupply )
31 import IO ( hPutStr, stderr )
36 stg2stg :: [StgToDo] -- spec of what stg-to-stg passes to do
37 -> Module -- module name (profiling only)
38 -> UniqSupply -- a name supply
39 -> [StgBinding] -- input...
41 ([(StgBinding,[Id])], -- output program...
42 ([CostCentre], -- local cost-centres that need to be decl'd
43 [CostCentre], -- "extern" cost-centres
44 [CostCentreStack])) -- pre-defined "singleton" cost centre stacks
46 stg2stg stg_todos module_name us binds
47 = case (splitUniqSupply us) of { (us4now, us4later) ->
49 doIfSet opt_D_verbose_stg2stg (printErrs (text "VERBOSE STG-TO-STG:")) >>
51 end_pass us4now "Core2Stg" ([],[],[]) binds
52 >>= \ (binds', us, ccs) ->
54 -- Do the main business!
55 foldl_mn do_stg_pass (binds', us, ccs) stg_todos
56 >>= \ (processed_binds, _, cost_centres) ->
58 -- Do essential wind-up
60 -- Essential wind-up: part (b), do setStgVarInfo. It has to
61 -- happen regardless, because the code generator uses its
64 -- Why does it have to happen last? Because earlier passes
65 -- may move things around, which would change the live-var
66 -- info. Also, setStgVarInfo decides about let-no-escape
67 -- things, which in turn do a better job if arities are
68 -- correct, which is done by satStgRhs.
72 annotated_binds = setStgVarInfo opt_StgDoLetNoEscapes processed_binds
73 srt_binds = computeSRTs annotated_binds
76 dumpIfSet opt_D_dump_stg "STG syntax:"
77 (pprStgBindingsWithSRTs srt_binds) >>
79 return (srt_binds, cost_centres)
83 stg_linter = if opt_DoStgLinting
85 else ( \ whodunnit binds -> binds )
87 -------------------------------------------
88 do_stg_pass (binds, us, ccs) to_do
90 (us1, us2) = splitUniqSupply us
93 StgDoStaticArgs -> panic "STG static argument transformation deleted"
95 StgDoUpdateAnalysis ->
97 -- NB We have to do setStgVarInfo first! (There's one
98 -- place free-var info is used) But no let-no-escapes,
99 -- because update analysis doesn't care.
100 end_pass us2 "UpdAnal" ccs (updateAnalyse (setStgVarInfo False binds))
103 trace (showStgStats binds)
104 end_pass us2 "StgStats" ccs binds
107 _scc_ "StgLambdaLift"
108 -- NB We have to do setStgVarInfo first!
110 binds3 = liftProgram module_name us1 (setStgVarInfo opt_StgDoLetNoEscapes binds)
112 end_pass us2 "LambdaLift" ccs binds3
114 StgDoMassageForProfiling ->
117 (collected_CCs, binds3)
118 = stgMassageForProfiling module_name us1 binds
120 end_pass us2 "ProfMassage" collected_CCs binds3
122 end_pass us2 what ccs binds2
123 = -- report verbosely, if required
124 (if opt_D_verbose_stg2stg then
125 hPutStr stderr (showSDoc
126 (text ("*** "++what++":") $$ vcat (map ppr binds2)
130 linted_binds = stg_linter what binds2
132 return (linted_binds, us2, ccs)
133 -- return: processed binds
134 -- UniqueSupply for the next guy to use
135 -- cost-centres to be declared/registered (specialised)
136 -- add to description of what's happened (reverse order)
138 -- here so it can be inlined...
139 foldl_mn f z [] = return z
140 foldl_mn f z (x:xs) = f z x >>= \ zz ->