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 CostCentre ( CostCentre, CostCentreStack )
14 import SCCfinal ( stgMassageForProfiling )
15 import StgLint ( lintStgBindings )
16 import StgStats ( showStgStats )
17 import SRT ( computeSRTs )
19 import CmdLineOpts ( DynFlags, DynFlag(..), dopt,
20 opt_StgDoLetNoEscapes,
21 StgToDo(..), dopt_StgToDo
24 import Module ( Module )
25 import ErrUtils ( doIfSet_dyn, dumpIfSet_dyn, showPass )
26 import UniqSupply ( mkSplitUniqSupply, splitUniqSupply, UniqSupply )
27 import IO ( hPutStr, stdout )
32 stg2stg :: DynFlags -- includes spec of what stg-to-stg passes to do
33 -> Module -- module name (profiling only)
34 -> [StgBinding] -- input...
36 ([(StgBinding,[Id])], -- output program...
37 ([CostCentre], -- local cost-centres that need to be decl'd
38 [CostCentre], -- "extern" cost-centres
39 [CostCentreStack])) -- pre-defined "singleton" cost centre stacks
41 stg2stg dflags module_name binds
42 = do { showPass dflags "Stg2Stg"
43 ; us <- mkSplitUniqSupply 'g'
45 ; doIfSet_dyn dflags Opt_D_verbose_stg2stg
46 (printDump (text "VERBOSE STG-TO-STG:"))
48 ; (binds', us', ccs) <- end_pass us "Stg2Stg" ([],[],[]) binds
50 -- Do the main business!
51 ; (processed_binds, _, cost_centres)
52 <- foldl_mn do_stg_pass (binds', us', ccs)
55 ; let srt_binds = computeSRTs processed_binds
57 ; dumpIfSet_dyn dflags Opt_D_dump_stg "STG syntax:"
58 (pprStgBindingsWithSRTs srt_binds)
60 ; return (srt_binds, cost_centres)
64 stg_linter = if dopt Opt_DoStgLinting dflags
66 else ( \ whodunnit binds -> binds )
68 -------------------------------------------
69 do_stg_pass (binds, us, ccs) to_do
71 (us1, us2) = splitUniqSupply us
75 trace (showStgStats binds)
76 end_pass us2 "StgStats" ccs binds
78 StgDoMassageForProfiling ->
81 (collected_CCs, binds3)
82 = stgMassageForProfiling module_name us1 binds
84 end_pass us2 "ProfMassage" collected_CCs binds3
86 end_pass us2 what ccs binds2
87 = do -- report verbosely, if required
88 dumpIfSet_dyn dflags Opt_D_verbose_stg2stg what
89 (vcat (map ppr binds2))
90 let linted_binds = stg_linter what binds2
91 return (linted_binds, us2, ccs)
92 -- return: processed binds
93 -- UniqueSupply for the next guy to use
94 -- cost-centres to be declared/registered (specialised)
95 -- add to description of what's happened (reverse order)
97 -- here so it can be inlined...
98 foldl_mn f z [] = return z
99 foldl_mn f z (x:xs) = f z x >>= \ zz ->