2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
7 module SimplStg ( stg2stg ) where
9 #include "HsVersions.h"
13 import LambdaLift ( liftProgram )
14 import Name ( isLocallyDefined )
15 import UniqSet ( UniqSet(..), mapUniqSet )
16 import CostCentre ( CostCentre )
17 import SCCfinal ( stgMassageForProfiling )
18 import StgLint ( lintStgBindings )
19 import StgStats ( showStgStats )
20 import StgVarInfo ( setStgVarInfo )
21 import UpdAnal ( updateAnalyse )
23 import CmdLineOpts ( opt_SccGroup, --Not used:opt_EnsureSplittableC,
24 opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
28 import Id ( nullIdEnv, lookupIdEnv, addOneToIdEnv,
29 growIdEnvList, isNullIdEnv, IdEnv,
30 GenId{-instance Eq/Outputable -}, Id
32 import Maybes ( maybeToBool )
33 import PprType ( GenType{-instance Outputable-} )
34 import ErrUtils ( doIfSet )
35 import UniqSupply ( splitUniqSupply, UniqSupply )
36 import Util ( mapAccumL, panic, assertPanic )
37 import IO ( hPutStr, stderr )
39 import GlaExts ( trace )
43 stg2stg :: [StgToDo] -- spec of what stg-to-stg passes to do
44 -> FAST_STRING -- module name (profiling only)
45 -> UniqSupply -- a name supply
46 -> [StgBinding] -- input...
48 ([StgBinding], -- output program...
49 ([CostCentre], -- local cost-centres that need to be decl'd
50 [CostCentre])) -- "extern" cost-centres
52 stg2stg stg_todos module_name us binds
53 = case (splitUniqSupply us) of { (us4now, us4later) ->
55 doIfSet do_verbose_stg2stg
56 (printErrs (text "VERBOSE STG-TO-STG:" $$
57 text "*** Core2Stg:" $$
58 vcat (map ppr (setStgVarInfo False binds)))) >>
60 -- Do the main business!
61 foldl_mn do_stg_pass (binds, us4now, ([],[])) stg_todos
62 >>= \ (processed_binds, _, cost_centres) ->
64 -- Do essential wind-up
66 {- Nuked for now SLPJ Dec 96
68 -- Essential wind-up: part (a), saturate RHSs
69 -- This must occur *after* elimIndirections, because elimIndirections
70 -- can change things' arities. Consider:
72 -- x_global = \a -> x_local a
73 -- Then elimIndirections will change the program to
75 -- and lo and behold x_global's arity has changed!
76 case (satStgRhs processed_binds us4later) of { saturated_binds ->
79 -- Essential wind-up: part (b), do setStgVarInfo. It has to
80 -- happen regardless, because the code generator uses its
83 -- Why does it have to happen last? Because earlier passes
84 -- may move things around, which would change the live-var
85 -- info. Also, setStgVarInfo decides about let-no-escape
86 -- things, which in turn do a better job if arities are
87 -- correct, which is done by satStgRhs.
90 return (setStgVarInfo do_let_no_escapes processed_binds, cost_centres)
93 do_let_no_escapes = opt_StgDoLetNoEscapes
94 do_verbose_stg2stg = opt_D_verbose_stg2stg
97 (do_unlocalising, unlocal_tag)
98 = case opt_EnsureSplittableC of
99 Just tag -> (True, _PK_ tag)
100 Nothing -> (False, panic "tag")
102 grp_name = case (opt_SccGroup) of
104 Nothing -> module_name -- default: module name
107 stg_linter = if False --LATER: opt_DoStgLinting (ToDo)
109 else ( \ whodunnit binds -> binds )
111 -------------------------------------------
112 do_stg_pass (binds, us, ccs) to_do
114 (us1, us2) = splitUniqSupply us
117 StgDoStaticArgs -> panic "STG static argument transformation deleted"
119 StgDoUpdateAnalysis ->
120 ASSERT(null (fst ccs) && null (snd ccs))
122 -- NB We have to do setStgVarInfo first! (There's one
123 -- place free-var info is used) But no let-no-escapes,
124 -- because update analysis doesn't care.
125 end_pass us2 "UpdAnal" ccs (updateAnalyse (setStgVarInfo False binds))
128 trace (showStgStats binds)
129 end_pass us2 "StgStats" ccs binds
132 _scc_ "StgLambdaLift"
133 -- NB We have to do setStgVarInfo first!
135 binds3 = liftProgram module_name us1 (setStgVarInfo do_let_no_escapes binds)
137 end_pass us2 "LambdaLift" ccs binds3
139 StgDoMassageForProfiling ->
142 (collected_CCs, binds3)
143 = stgMassageForProfiling module_name grp_name us1 binds
145 end_pass us2 "ProfMassage" collected_CCs binds3
147 end_pass us2 what ccs binds2
148 = -- report verbosely, if required
149 (if do_verbose_stg2stg then
150 hPutStr stderr (showSDoc
151 (text ("*** "++what++":") $$ vcat (map ppr binds2)
155 linted_binds = stg_linter what binds2
157 return (linted_binds, us2, ccs)
158 -- return: processed binds
159 -- UniqueSupply for the next guy to use
160 -- cost-centres to be declared/registered (specialised)
161 -- add to description of what's happened (reverse order)
163 -- here so it can be inlined...
164 foldl_mn f z [] = return z
165 foldl_mn f z (x:xs) = f z x >>= \ zz ->