2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplMonad]{The simplifier Monad}
9 initSmpl, returnSmpl, thenSmpl, thenSmpl_,
10 mapSmpl, mapAndUnzipSmpl, mapAccumLSmpl,
13 getUniqueSmpl, getUniquesSmpl,
16 SimplCount{-abstract-}, TickType(..), tick, tickN, tickUnfold,
17 simplCount, detailedSimplCount,
18 zeroSimplCount, showSimplCount, combineSimplCounts
21 #include "HsVersions.h"
23 import Id ( GenId, mkSysLocal, mkIdWithNewUniq, Id )
25 import SrcLoc ( noSrcLoc )
26 import TyVar ( TyVar )
28 import UniqSupply ( getUnique, getUniques, splitUniqSupply,
31 import Unique ( Unique )
32 import Util ( zipWithEqual, Eager, appEager )
36 infixr 9 `thenSmpl`, `thenSmpl_`
39 %************************************************************************
41 \subsection{Monad plumbing}
43 %************************************************************************
45 For the simplifier monad, we want to {\em thread} a unique supply and a counter.
46 (Command-line switches move around through the explicitly-passed SimplEnv.)
51 -> SimplCount -- things being threaded
52 -> (result, SimplCount)
56 initSmpl :: UniqSupply -- no init count; set to 0
60 initSmpl us m = m us zeroSimplCount
62 {-# INLINE thenSmpl #-}
63 {-# INLINE thenSmpl_ #-}
64 {-# INLINE returnSmpl #-}
66 returnSmpl :: a -> SmplM a
67 returnSmpl e us sc = (e, sc)
69 thenSmpl :: SmplM a -> (a -> SmplM b) -> SmplM b
70 thenSmpl_ :: SmplM a -> SmplM b -> SmplM b
73 = case splitUniqSupply us of { (s1, s2) ->
74 case (m s1 sc0) of { (m_result, sc1) ->
78 = case splitUniqSupply us of { (s1, s2) ->
79 case (m s1 sc0) of { (_, sc1) ->
82 mapSmpl :: (a -> SmplM b) -> [a] -> SmplM [b]
83 mapAndUnzipSmpl :: (a -> SmplM (b, c)) -> [a] -> SmplM ([b],[c])
85 mapSmpl f [] = returnSmpl []
87 = f x `thenSmpl` \ x' ->
88 mapSmpl f xs `thenSmpl` \ xs' ->
91 mapAndUnzipSmpl f [] = returnSmpl ([],[])
92 mapAndUnzipSmpl f (x:xs)
93 = f x `thenSmpl` \ (r1, r2) ->
94 mapAndUnzipSmpl f xs `thenSmpl` \ (rs1, rs2) ->
95 returnSmpl (r1:rs1, r2:rs2)
97 mapAccumLSmpl f acc [] = returnSmpl (acc, [])
98 mapAccumLSmpl f acc (x:xs) = f acc x `thenSmpl` \ (acc', x') ->
99 mapAccumLSmpl f acc' xs `thenSmpl` \ (acc'', xs') ->
100 returnSmpl (acc'', x':xs')
102 getUniqueSmpl :: SmplM Unique
103 getUniqueSmpl us sc = (getUnique us, sc)
105 getUniquesSmpl :: Int -> SmplM [Unique]
106 getUniquesSmpl n us sc = (getUniques n us, sc)
110 %************************************************************************
112 \subsection{Counting up what we've done}
114 %************************************************************************
116 The assoc list isn't particularly costly, because we only use
117 the number of ticks in ``real life.''
119 The right thing to do, if you want that to go fast, is thread
120 a mutable array through @SimplM@.
124 = SimplCount FAST_INT -- number of ticks
125 [(TickType, Int)] -- assoc list of all diff kinds of ticks
128 type UnfoldingHistory = (Int, -- N
129 [(Id,Int)], -- Last N unfoldings
130 [(Id,Int)]) -- The MaxUnfoldHistory unfoldings before that
133 = UnfoldingDone | MagicUnfold | ConReused
134 | CaseFloatFromLet | CaseOfCase
135 | LetFloatFromLet | LetFloatFromCase
136 | KnownBranch | Let2Case
137 | CaseMerge | CaseElim
139 | AtomicRhs -- Rhs of a let-expression was an atom
145 {- BEGIN F/B ENTRIES -}
147 | FoldrBuild -- foldr f z (build g) ==>
148 | FoldrAugment -- foldr f z (augment g z) ==>
149 | Foldr_Nil -- foldr f z [] ==>
150 | Foldr_List -- foldr f z (x:...) ==>
152 | FoldlBuild -- foldl f z (build g) ==>
153 | FoldlAugment -- foldl f z (augment g z) ==>
154 | Foldl_Nil -- foldl f z [] ==>
155 | Foldl_List -- foldl f z (x:...) ==>
157 | Foldr_Cons_Nil -- foldr (:) [] => id
158 | Foldr_Cons -- foldr (:) => flip (++)
160 | Str_FoldrStr -- foldr f z "hello" => unpackFoldrPS__ f z "hello"
161 | Str_UnpackCons -- unpackFoldrPS# (:) z "hello" => unpackAppendPS__ z "hello"
162 | Str_UnpackNil -- unpackAppendPS__ [] "hello" => "hello"
163 {- END F/B ENTRIES -}
164 deriving (Eq, Ord, Ix)
166 instance Text TickType where
167 showsPrec p UnfoldingDone = showString "UnfoldingDone "
168 showsPrec p MagicUnfold = showString "MagicUnfold "
169 showsPrec p ConReused = showString "ConReused "
170 showsPrec p CaseFloatFromLet= showString "CaseFloatFromLet "
171 showsPrec p CaseOfCase = showString "CaseOfCase "
172 showsPrec p LetFloatFromLet = showString "LetFloatFromLet "
173 showsPrec p LetFloatFromCase= showString "LetFloatFromCase "
174 showsPrec p KnownBranch = showString "KnownBranch "
175 showsPrec p Let2Case = showString "Let2Case "
176 showsPrec p CaseMerge = showString "CaseMerge "
177 showsPrec p CaseElim = showString "CaseElim "
178 showsPrec p CaseIdentity = showString "CaseIdentity "
179 showsPrec p AtomicRhs = showString "AtomicRhs "
180 showsPrec p EtaExpansion = showString "EtaExpansion "
181 showsPrec p CaseOfError = showString "CaseOfError "
182 showsPrec p TyBetaReduction = showString "TyBetaReduction "
183 showsPrec p BetaReduction = showString "BetaReduction "
184 showsPrec p SpecialisationDone
185 = showString "Specialisation "
187 -- Foldr/Build Stuff:
188 showsPrec p FoldrBuild = showString "FoldrBuild "
189 showsPrec p FoldrAugment = showString "FoldrAugment "
190 showsPrec p Foldr_Nil = showString "Foldr_Nil "
191 showsPrec p Foldr_List = showString "Foldr_List "
193 showsPrec p FoldlBuild = showString "FoldlBuild "
194 showsPrec p FoldlAugment = showString "FoldlAugment "
195 showsPrec p Foldl_Nil = showString "Foldl_Nil "
196 showsPrec p Foldl_List = showString "Foldl_List "
198 showsPrec p Foldr_Cons_Nil = showString "Foldr_Cons_Nil "
199 showsPrec p Foldr_Cons = showString "Foldr_Cons "
201 showsPrec p Str_FoldrStr = showString "Str_FoldrStr "
202 showsPrec p Str_UnpackCons = showString "Str_UnpackCons "
203 showsPrec p Str_UnpackNil = showString "Str_UnpackNil "
205 showSimplCount :: SimplCount -> String
207 showSimplCount (SimplCount _ stuff (_, unf1, unf2))
208 = shw stuff ++ "\nMost recent unfoldings: " ++ showSDoc (ppr (reverse unf2 ++ reverse unf1))
211 shw ((t,n):tns) | n /= 0 = show t ++ ('\t' : show n) ++ ('\n' : shw tns)
212 | otherwise = shw tns
214 zeroSimplCount :: SimplCount
216 = SimplCount ILIT(0) stuff (0, [], [])
219 [ (UnfoldingDone, 0),
222 (CaseFloatFromLet, 0),
224 (LetFloatFromLet, 0),
225 (LetFloatFromCase, 0),
236 (SpecialisationDone,0),
237 -- Foldr/Build Stuff:
253 --= array (con2tag_TickType UnfoldingDone, con2tag_TickType FoldrInline)
254 -- [ i := 0 | i <- indices zeroSimplCount ]
257 Counting-related monad functions:
259 tick :: TickType -> SmplM ()
261 tick tick_type us (SimplCount n stuff unf)
262 = -- pprTrace "Tick: " (text (show tick_type)) $
263 #ifdef OMIT_SIMPL_COUNTS
264 ((), SimplCount (n _ADD_ ILIT(1) stuff unf)) stuff -- don't change anything
267 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff unf)
269 new_stuff = inc_tick tick_type ILIT(1) stuff
272 maxUnfoldHistory :: Int
273 maxUnfoldHistory = 20
275 tickUnfold :: Id -> SmplM ()
276 tickUnfold id us (SimplCount n stuff (n_unf, unf1, unf2))
277 = -- pprTrace "Unfolding: " (ppr id) $
280 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff new_unf)
282 new_stuff = inc_tick UnfoldingDone ILIT(1) stuff
284 new_unf | n_unf >= maxUnfoldHistory = (1, [unf_item], unf1)
285 | otherwise = (n_unf+1, unf_item:unf1, unf2)
287 unf_item = (id, IBOX(n))
290 -- force list to avoid getting a chain of @inc_tick@ applications
291 -- building up on the heap. (Only true when not dumping stats).
295 seqTriple (_,_,_) y = y
297 tickN :: TickType -> Int -> SmplM ()
299 tickN tick_type 0 us counts
301 tickN tick_type IBOX(increment) us (SimplCount n stuff unf)
302 = -- pprTrace "Tick: " (text (show tick_type)) $
303 #ifdef OMIT_SIMPL_COUNTS
304 ((), SimplCount (n _ADD_ increment) stuff) -- don't change anything
307 ((), SimplCount (n _ADD_ increment) new_stuff unf)
309 new_stuff = inc_tick tick_type increment stuff
312 inc_tick tick_type n [] = panic "couldn't inc_tick!"
314 inc_tick tick_type n (x@(ttype, I# cnt#) : xs)
315 | ttype == tick_type = case cnt# +# n of
316 incd -> (ttype,IBOX(incd)) : xs
318 | otherwise = case inc_tick tick_type n xs of { [] -> [x]; ls -> x:ls }
321 simplCount :: SmplM Int
322 simplCount us sc@(SimplCount n _ _) = (IBOX(n), sc)
324 detailedSimplCount :: SmplM SimplCount
325 detailedSimplCount us sc = (sc, sc)
327 combineSimplCounts :: SimplCount -> SimplCount -> SimplCount
329 #ifdef OMIT_SIMPL_COUNTS
330 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
331 = SimplCount (n1 _ADD_ n2)
332 stuff2 -- just pick one
335 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
337 SimplCount (n1 _ADD_ n2) new_stuff unf2 -- Just pick the second for unfold history
339 new_stuff = zipWithEqual "combineSimplCounts" (\ (t1,c1) (t2,c2) -> (t1,c1+c2)) stuff1 stuff2