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, IdEnv(..),
20 GenId{-instance Ord3-}
22 import Maybes ( catMaybes )
23 import Name ( isExported )
24 import OccurAnal ( occurAnalyseBinds )
25 import Pretty ( ppAboves, ppBesides, ppInt, ppChar, ppStr )
28 import Simplify ( simplTopBinds )
29 import TyVar ( nullTyVarEnv, TyVarEnv(..) )
30 import UniqSupply ( thenUs, returnUs, mapUs, splitUniqSupply, UniqSM(..) )
31 import Util ( isIn, isn'tIn, removeDups, pprTrace )
35 simplifyPgm :: [CoreBinding] -- input
36 -> (SimplifierSwitch->SwitchResult)
37 -> SimplCount -- info about how many times
38 -- each transformation has occurred
40 -> ([CoreBinding], -- output
41 Int, -- info about how much happened
42 SimplCount) -- accumulated simpl stats
44 simplifyPgm binds s_sw_chkr simpl_stats us
45 = case (splitUniqSupply us) of { (s1, s2) ->
46 case (initSmpl s1 (simpl_pgm 0 1 binds)) of { ((pgm2, it_count, simpl_stats2), _) ->
47 case (tidy_top pgm2 s2) of { pgm3 ->
48 (pgm3, it_count, combineSimplCounts simpl_stats simpl_stats2) }}}
50 simpl_switch_is_on = switchIsOn s_sw_chkr
52 occur_anal = occurAnalyseBinds
55 = case (intSwitchSet s_sw_chkr MaxSimplifierIterations) of
56 Nothing -> 1 -- default
59 simpl_pgm :: Int -> Int -> [CoreBinding] -> SmplM ([CoreBinding], Int, SimplCount)
61 simpl_pgm n iterations pgm
62 = -- find out what top-level binders are used,
63 -- and prepare to unfold all the "simple" bindings
65 tagged_pgm = occur_anal pgm simpl_switch_is_on
68 simplTopBinds (nullSimplEnv s_sw_chkr) tagged_pgm `thenSmpl` \ new_pgm ->
70 -- Quit if we didn't actually do anything; otherwise,
71 -- try again (if suitable flags)
73 simplCount `thenSmpl` \ r ->
74 detailedSimplCount `thenSmpl` \ dr ->
76 show_status = pprTrace "NewSimpl: " (ppAboves [
77 ppBesides [ppInt iterations, ppChar '/', ppInt max_simpl_iterations],
78 ppStr (showSimplCount dr)
79 --DEBUG: , ppAboves (map (pprCoreBinding PprDebug) new_pgm)
83 (if opt_D_verbose_core2core
84 || simpl_switch_is_on ShowSimplifierProgress
88 (let stop_now = r == n {-nothing happened-}
89 || (if iterations > max_simpl_iterations then
90 (if max_simpl_iterations > 1 {-otherwise too boring-} then
92 ("NOTE: Simplifier still going after "++show max_simpl_iterations++" iterations; bailing out.")
99 returnSmpl (new_pgm, iterations, dr)
101 simpl_pgm r (iterations + 1) new_pgm
105 In @tidy_top@, we look for things at the top-level of the form...
109 x_exported = x_local -- or perhaps...
111 x_exported = /\ tyvars -> x_local tyvars -- where this is eta-reducible
113 In cases we find like this, we go {\em backwards} and replace
114 \tr{x_local} with \tr{x_exported}. This save a gratuitous jump
115 (from \tr{x_exported} to \tr{x_local}), and makes strictness
116 information propagate better.
118 If more than one exported thing is equal to a local thing (i.e., the
119 local thing really is shared), then obviously we give up.
121 Strategy: first collect the info; then make a \tr{Id -> Id} mapping.
122 Then blast the whole program (LHSs as well as RHSs) with it.
125 type BlastEnv = IdEnv Id -- domain is local Ids; range is exported Ids
127 not_elem = isn'tIn "undup"
129 tidy_top :: [CoreBinding] -> UniqSM [CoreBinding]
132 = if null blast_alist then
133 returnUs binds_in -- no joy there
135 mapUs blast binds_in `thenUs` \ binds_maybe ->
136 returnUs (catMaybes binds_maybe)
138 blast_alist = undup (foldl find_cand [] binds_in)
139 blast_id_env = mkIdEnv blast_alist
140 blast_val_env= mkIdEnv [ (l, Var e) | (l,e) <- blast_alist ]
141 blast_all_exps = map snd blast_alist
144 find_cand blast_list (Rec _) = blast_list -- recursively paranoid, as usual
146 find_cand blast_list (NonRec binder rhs)
147 = if not (isExported binder) then
150 case rhs_equiv_to_local_var rhs of
151 Nothing -> blast_list
152 Just local -> (local, binder) : blast_list -- tag it on
154 ------------------------------------------
155 -- if an Id appears >1 time in the domain,
156 -- *all* occurrences must be expunged.
157 undup :: [(Id, Id)] -> [(Id, Id)]
161 (singles, dups) = removeDups compare blast_list
162 list_of_dups = concat dups
164 [ s | s <- singles, s `not_elem` list_of_dups ]
166 compare (x,_) (y,_) = x `cmp` y
168 ------------------------------------------
169 rhs_equiv_to_local_var (Var x)
170 = if externallyVisibleId x then Nothing else Just x
172 rhs_equiv_to_local_var expr = Nothing
174 ------------------------------------------
175 -- "blast" does the substitution:
176 -- returns Nothing if a binding goes away
177 -- returns "Just b" to give back a fixed-up binding
179 blast :: CoreBinding -> UniqSM (Maybe CoreBinding)
182 = mapUs blast_pr pairs `thenUs` \ blasted_pairs ->
183 returnUs (Just (Rec blasted_pairs))
185 blast_pr (binder, rhs)
186 = substCoreExpr blast_val_env nullTyVarEnv rhs `thenUs` \ new_rhs ->
188 case (lookupIdEnv blast_id_env binder) of
189 Just exportee -> (exportee, new_rhs)
190 Nothing -> (binder, new_rhs)
193 blast (NonRec binder rhs)
194 = if binder `is_elem` blast_all_exps then
195 returnUs Nothing -- this binding dies!
197 substCoreExpr blast_val_env nullTyVarEnv rhs `thenUs` \ new_rhs ->
199 case (lookupIdEnv blast_id_env binder) of
200 Just exportee -> NonRec exportee new_rhs
201 Nothing -> NonRec binder new_rhs
204 is_elem = isIn "blast"