2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplPgm]{Interface to the simplifier}
7 #include "HsVersions.h"
9 module SimplPgm ( simplifyPgm ) where
13 import CmdLineOpts ( opt_D_verbose_core2core,
14 switchIsOn, intSwitchSet, SimplifierSwitch(..)
17 import CoreUtils ( substCoreExpr )
18 import Id ( externallyVisibleId,
19 mkIdEnv, lookupIdEnv, SYN_IE(IdEnv),
20 GenId{-instance Ord3-}
22 import Maybes ( catMaybes )
23 import OccurAnal ( occurAnalyseBinds )
24 import Pretty ( ppAboves, ppBesides, ppInt, ppChar, ppStr )
27 import Simplify ( simplTopBinds )
28 import TyVar ( nullTyVarEnv, SYN_IE(TyVarEnv) )
29 import UniqSupply ( thenUs, returnUs, mapUs, splitUniqSupply, SYN_IE(UniqSM) )
30 import Util ( isIn, isn'tIn, removeDups, pprTrace )
34 simplifyPgm :: [CoreBinding] -- input
35 -> (SimplifierSwitch->SwitchResult)
36 -> SimplCount -- info about how many times
37 -- each transformation has occurred
39 -> ([CoreBinding], -- output
40 Int, -- info about how much happened
41 SimplCount) -- accumulated simpl stats
43 simplifyPgm binds s_sw_chkr simpl_stats us
44 = case (splitUniqSupply us) of { (s1, s2) ->
45 case (initSmpl s1 (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
46 case (tidy_top pgm2 s2) of { pgm3 ->
47 (pgm3, it_count, combineSimplCounts simpl_stats simpl_stats2) }}}
49 simpl_switch_is_on = switchIsOn s_sw_chkr
51 occur_anal = occurAnalyseBinds
54 = case (intSwitchSet s_sw_chkr MaxSimplifierIterations) of
55 Nothing -> 1 -- default
58 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
60 simpl_pgm n iterations pgm
61 = -- find out what top-level binders are used,
62 -- and prepare to unfold all the "simple" bindings
64 tagged_pgm = occur_anal pgm simpl_switch_is_on
67 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
69 -- Quit if we didn't actually do anything; otherwise,
70 -- try again (if suitable flags)
72 simplCount `thenSmpl` \ r ->
73 detailedSimplCount `thenSmpl` \ dr ->
75 show_status = pprTrace "NewSimpl: " (ppAboves [
76 ppBesides [ppInt iterations, ppChar '/', ppInt max_simpl_iterations],
77 ppStr (showSimplCount dr)
78 --DEBUG: , ppAboves (map (pprCoreBinding PprDebug) new_pgm)
82 (if opt_D_verbose_core2core
83 || simpl_switch_is_on ShowSimplifierProgress
87 (let stop_now = r == n {-nothing happened-}
88 || (if iterations > max_simpl_iterations then
89 (if max_simpl_iterations > 1 {-otherwise too boring-} then
91 ("NOTE: Simplifier still going after "++show max_simpl_iterations++" iterations; bailing out.")
98 returnSmpl (new_pgm, iterations, dr)
100 simpl_pgm r (iterations + 1) new_pgm
104 In @tidy_top@, we look for things at the top-level of the form...
108 x_exported = x_local -- or perhaps...
110 x_exported = /\ tyvars -> x_local tyvars -- where this is eta-reducible
112 In cases we find like this, we go {\em backwards} and replace
113 \tr{x_local} with \tr{x_exported}. This save a gratuitous jump
114 (from \tr{x_exported} to \tr{x_local}), and makes strictness
115 information propagate better.
117 If more than one exported thing is equal to a local thing (i.e., the
118 local thing really is shared), then obviously we give up.
120 Strategy: first collect the info; then make a \tr{Id -> Id} mapping.
121 Then blast the whole program (LHSs as well as RHSs) with it.
124 type BlastEnv = IdEnv Id -- domain is local Ids; range is exported Ids
126 not_elem = isn'tIn "undup"
128 tidy_top :: [CoreBinding] -> UniqSM [CoreBinding]
131 = if null blast_alist then
132 returnUs binds_in -- no joy there
134 mapUs blast binds_in `thenUs` \ binds_maybe ->
135 returnUs (catMaybes binds_maybe)
137 blast_alist = undup (foldl find_cand [] binds_in)
138 blast_id_env = mkIdEnv blast_alist
139 blast_val_env= mkIdEnv [ (l, Var e) | (l,e) <- blast_alist ]
140 blast_all_exps = map snd blast_alist
143 find_cand blast_list (Rec _) = blast_list -- recursively paranoid, as usual
145 find_cand blast_list (NonRec binder rhs)
146 = if not (externallyVisibleId binder) then
149 case rhs_equiv_to_local_var rhs of
150 Nothing -> blast_list
151 Just local -> (local, binder) : blast_list -- tag it on
153 ------------------------------------------
154 -- if an Id appears >1 time in the domain,
155 -- *all* occurrences must be expunged.
156 undup :: [(Id, Id)] -> [(Id, Id)]
160 (singles, dups) = removeDups compare blast_list
161 list_of_dups = concat dups
163 [ s | s <- singles, s `not_elem` list_of_dups ]
165 compare (x,_) (y,_) = x `cmp` y
167 ------------------------------------------
168 rhs_equiv_to_local_var (Var x)
169 = if externallyVisibleId x then Nothing else Just x
171 rhs_equiv_to_local_var expr = Nothing
173 ------------------------------------------
174 -- "blast" does the substitution:
175 -- returns Nothing if a binding goes away
176 -- returns "Just b" to give back a fixed-up binding
178 blast :: CoreBinding -> UniqSM (Maybe CoreBinding)
181 = mapUs blast_pr pairs `thenUs` \ blasted_pairs ->
182 returnUs (Just (Rec blasted_pairs))
184 blast_pr (binder, rhs)
185 = substCoreExpr blast_val_env nullTyVarEnv rhs `thenUs` \ new_rhs ->
187 case (lookupIdEnv blast_id_env binder) of
188 Just exportee -> (exportee, new_rhs)
189 Nothing -> (binder, new_rhs)
192 blast (NonRec binder rhs)
193 = if binder `is_elem` blast_all_exps then
194 returnUs Nothing -- this binding dies!
196 substCoreExpr blast_val_env nullTyVarEnv rhs `thenUs` \ new_rhs ->
198 case (lookupIdEnv blast_id_env binder) of
199 Just exportee -> NonRec exportee new_rhs
200 Nothing -> NonRec binder new_rhs
203 is_elem = isIn "blast"