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 ( CollectedCCs )
14 import SCCfinal ( stgMassageForProfiling )
15 import StgLint ( lintStgBindings )
16 import StgStats ( showStgStats )
17 import SRT ( computeSRTs )
19 import Packages ( HomeModules )
20 import DynFlags ( DynFlags(..), DynFlag(..), dopt, StgToDo(..),
23 import Module ( Module )
24 import ErrUtils ( doIfSet_dyn, dumpIfSet_dyn, showPass )
25 import UniqSupply ( mkSplitUniqSupply, splitUniqSupply )
30 stg2stg :: DynFlags -- includes spec of what stg-to-stg passes to do
32 -> Module -- module name (profiling only)
33 -> [StgBinding] -- input...
34 -> IO ( [(StgBinding,[(Id,[Id])])] -- output program...
35 , CollectedCCs) -- cost centre information (declared and used)
37 stg2stg dflags pkg_deps module_name binds
38 = do { showPass dflags "Stg2Stg"
39 ; us <- mkSplitUniqSupply 'g'
41 ; doIfSet_dyn dflags Opt_D_verbose_stg2stg
42 (printDump (text "VERBOSE STG-TO-STG:"))
44 ; (binds', us', ccs) <- end_pass us "Stg2Stg" ([],[],[]) binds
46 -- Do the main business!
47 ; (processed_binds, _, cost_centres)
48 <- foldl_mn do_stg_pass (binds', us', ccs) (getStgToDo dflags)
50 ; let srt_binds = computeSRTs processed_binds
52 ; dumpIfSet_dyn dflags Opt_D_dump_stg "STG syntax:"
53 (pprStgBindingsWithSRTs srt_binds)
55 ; return (srt_binds, cost_centres)
59 stg_linter = if dopt Opt_DoStgLinting dflags
61 else ( \ whodunnit binds -> binds )
63 -------------------------------------------
64 do_stg_pass (binds, us, ccs) to_do
66 (us1, us2) = splitUniqSupply us
70 trace (showStgStats binds)
71 end_pass us2 "StgStats" ccs binds
73 StgDoMassageForProfiling ->
74 {-# SCC "ProfMassage" #-}
76 (collected_CCs, binds3)
77 = stgMassageForProfiling pkg_deps module_name us1 binds
79 end_pass us2 "ProfMassage" collected_CCs binds3
81 end_pass us2 what ccs binds2
82 = do -- report verbosely, if required
83 dumpIfSet_dyn dflags Opt_D_verbose_stg2stg what
84 (vcat (map ppr binds2))
85 let linted_binds = stg_linter what binds2
86 return (linted_binds, us2, ccs)
87 -- return: processed binds
88 -- UniqueSupply for the next guy to use
89 -- cost-centres to be declared/registered (specialised)
90 -- add to description of what's happened (reverse order)
92 -- here so it can be inlined...
93 foldl_mn f z [] = return z
94 foldl_mn f z (x:xs) = f z x >>= \ zz ->