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_SccGroup,
23 opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
24 opt_DoStgLinting, opt_D_dump_stg,
28 import Module ( Module, moduleString )
30 import ErrUtils ( doIfSet, dumpIfSet )
31 import UniqSupply ( splitUniqSupply, UniqSupply )
32 import IO ( hPutStr, stderr )
37 stg2stg :: [StgToDo] -- spec of what stg-to-stg passes to do
38 -> Module -- module name (profiling only)
39 -> UniqSupply -- a name supply
40 -> [StgBinding] -- input...
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
47 stg2stg stg_todos module_name us binds
48 = case (splitUniqSupply us) of { (us4now, us4later) ->
50 doIfSet opt_D_verbose_stg2stg (printErrs (text "VERBOSE STG-TO-STG:")) >>
52 end_pass us4now "Core2Stg" ([],[],[]) binds
53 >>= \ (binds', us, ccs) ->
55 -- Do the main business!
56 foldl_mn do_stg_pass (binds', us, ccs) stg_todos
57 >>= \ (processed_binds, _, cost_centres) ->
59 -- Do essential wind-up
61 -- Essential wind-up: part (b), do setStgVarInfo. It has to
62 -- happen regardless, because the code generator uses its
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.
73 annotated_binds = setStgVarInfo opt_StgDoLetNoEscapes processed_binds
74 srt_binds = computeSRTs annotated_binds
77 dumpIfSet opt_D_dump_stg "STG syntax:"
78 (pprStgBindingsWithSRTs srt_binds) >>
80 return (srt_binds, cost_centres)
84 grp_name = case (opt_SccGroup) of
86 Nothing -> _PK_ (moduleString module_name) -- default: module name
89 stg_linter = if opt_DoStgLinting
91 else ( \ whodunnit binds -> binds )
93 -------------------------------------------
94 do_stg_pass (binds, us, ccs) to_do
96 (us1, us2) = splitUniqSupply us
99 StgDoStaticArgs -> panic "STG static argument transformation deleted"
101 StgDoUpdateAnalysis ->
103 -- NB We have to do setStgVarInfo first! (There's one
104 -- place free-var info is used) But no let-no-escapes,
105 -- because update analysis doesn't care.
106 end_pass us2 "UpdAnal" ccs (updateAnalyse (setStgVarInfo False binds))
109 trace (showStgStats binds)
110 end_pass us2 "StgStats" ccs binds
113 _scc_ "StgLambdaLift"
114 -- NB We have to do setStgVarInfo first!
116 binds3 = liftProgram module_name us1 (setStgVarInfo opt_StgDoLetNoEscapes binds)
118 end_pass us2 "LambdaLift" ccs binds3
120 StgDoMassageForProfiling ->
123 (collected_CCs, binds3)
124 = stgMassageForProfiling module_name grp_name us1 binds
126 end_pass us2 "ProfMassage" collected_CCs binds3
128 end_pass us2 what ccs binds2
129 = -- report verbosely, if required
130 (if opt_D_verbose_stg2stg then
131 hPutStr stderr (showSDoc
132 (text ("*** "++what++":") $$ vcat (map ppr binds2)
136 linted_binds = stg_linter what binds2
138 return (linted_binds, us2, ccs)
139 -- return: processed binds
140 -- UniqueSupply for the next guy to use
141 -- cost-centres to be declared/registered (specialised)
142 -- add to description of what's happened (reverse order)
144 -- here so it can be inlined...
145 foldl_mn f z [] = return z
146 foldl_mn f z (x:xs) = f z x >>= \ zz ->