2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
4 \section[SimplStg]{Driver for simplifying @STG@ programs}
7 #include "HsVersions.h"
9 module SimplStg ( stg2stg ) where
12 IMPORT_1_3(IO(hPutStr,stderr))
16 import LambdaLift ( liftProgram )
17 import Name ( isLocallyDefined )
18 import UniqSet ( UniqSet(..), mapUniqSet )
19 import CostCentre ( CostCentre )
20 import SCCfinal ( stgMassageForProfiling )
21 import StgLint ( lintStgBindings )
22 import StgStats ( showStgStats )
23 import StgVarInfo ( setStgVarInfo )
24 import UpdAnal ( updateAnalyse )
26 import CmdLineOpts ( opt_SccGroup, --Not used:opt_EnsureSplittableC,
27 opt_StgDoLetNoEscapes, opt_D_verbose_stg2stg,
31 import Id ( nullIdEnv, lookupIdEnv, addOneToIdEnv,
32 growIdEnvList, isNullIdEnv, SYN_IE(IdEnv),
33 GenId{-instance Eq/Outputable -}, SYN_IE(Id)
35 import Maybes ( maybeToBool )
36 import PprType ( GenType{-instance Outputable-} )
37 import Outputable ( PprStyle, Outputable(..) )
38 import Pretty ( Doc, ($$), vcat, text, ptext )
39 import UniqSupply ( splitUniqSupply, UniqSupply )
40 import Util ( mapAccumL, panic, assertPanic )
44 stg2stg :: [StgToDo] -- spec of what stg-to-stg passes to do
45 -> FAST_STRING -- module name (profiling only)
46 -> PprStyle -- printing style (for debugging only)
47 -> UniqSupply -- a name supply
48 -> [StgBinding] -- input...
50 ([StgBinding], -- output program...
51 ([CostCentre], -- local cost-centres that need to be decl'd
52 [CostCentre])) -- "extern" cost-centres
54 stg2stg stg_todos module_name ppr_style us binds
55 = case (splitUniqSupply us) of { (us4now, us4later) ->
57 (if do_verbose_stg2stg then
58 hPutStr stderr "VERBOSE STG-TO-STG:\n" >>
60 (($$) (ptext SLIT("*** Core2Stg:"))
61 (vcat (map (ppr ppr_style) (setStgVarInfo False binds)))
65 -- Do the main business!
66 foldl_mn do_stg_pass (binds, us4now, ([],[])) stg_todos
67 >>= \ (processed_binds, _, cost_centres) ->
69 -- Do essential wind-up
71 {- Nuked for now SLPJ Dec 96
73 -- Essential wind-up: part (a), saturate RHSs
74 -- This must occur *after* elimIndirections, because elimIndirections
75 -- can change things' arities. Consider:
77 -- x_global = \a -> x_local a
78 -- Then elimIndirections will change the program to
80 -- and lo and behold x_global's arity has changed!
81 case (satStgRhs processed_binds us4later) of { saturated_binds ->
84 -- Essential wind-up: part (b), do setStgVarInfo. It has to
85 -- happen regardless, because the code generator uses its
88 -- Why does it have to happen last? Because earlier passes
89 -- may move things around, which would change the live-var
90 -- info. Also, setStgVarInfo decides about let-no-escape
91 -- things, which in turn do a better job if arities are
92 -- correct, which is done by satStgRhs.
95 return (setStgVarInfo do_let_no_escapes processed_binds, cost_centres)
98 do_let_no_escapes = opt_StgDoLetNoEscapes
99 do_verbose_stg2stg = opt_D_verbose_stg2stg
102 (do_unlocalising, unlocal_tag)
103 = case opt_EnsureSplittableC of
104 Just tag -> (True, _PK_ tag)
105 Nothing -> (False, panic "tag")
107 grp_name = case (opt_SccGroup) of
109 Nothing -> module_name -- default: module name
112 stg_linter = if False --LATER: opt_DoStgLinting (ToDo)
113 then lintStgBindings ppr_style
114 else ( \ whodunnit binds -> binds )
116 -------------------------------------------
117 do_stg_pass (binds, us, ccs) to_do
119 (us1, us2) = splitUniqSupply us
122 StgDoStaticArgs -> panic "STG static argument transformation deleted"
124 StgDoUpdateAnalysis ->
125 ASSERT(null (fst ccs) && null (snd ccs))
127 -- NB We have to do setStgVarInfo first! (There's one
128 -- place free-var info is used) But no let-no-escapes,
129 -- because update analysis doesn't care.
130 end_pass us2 "UpdAnal" ccs (updateAnalyse (setStgVarInfo False binds))
133 trace (showStgStats binds)
134 end_pass us2 "StgStats" ccs binds
137 _scc_ "StgLambdaLift"
138 -- NB We have to do setStgVarInfo first!
140 binds3 = liftProgram module_name us1 (setStgVarInfo do_let_no_escapes binds)
142 end_pass us2 "LambdaLift" ccs binds3
144 StgDoMassageForProfiling ->
147 (collected_CCs, binds3)
148 = stgMassageForProfiling module_name grp_name us1 binds
150 end_pass us2 "ProfMassage" collected_CCs binds3
152 end_pass us2 what ccs binds2
153 = -- report verbosely, if required
154 (if do_verbose_stg2stg then
156 (($$) (text ("*** "++what++":"))
157 (vcat (map (ppr ppr_style) binds2))
161 linted_binds = stg_linter what binds2
163 return (linted_binds, us2, ccs)
164 -- return: processed binds
165 -- UniqueSupply for the next guy to use
166 -- cost-centres to be declared/registered (specialised)
167 -- add to description of what's happened (reverse order)
169 -- here so it can be inlined...
170 foldl_mn f z [] = return z
171 foldl_mn f z (x:xs) = f z x >>= \ zz ->