2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplMonad]{The simplifier Monad}
9 initSmpl, returnSmpl, thenSmpl, thenSmpl_,
10 mapSmpl, mapAndUnzipSmpl,
13 SimplCount{-abstract-}, TickType(..), tick, tickN, tickUnfold,
14 simplCount, detailedSimplCount,
15 zeroSimplCount, showSimplCount, combineSimplCounts,
18 cloneId, cloneIds, cloneTyVarSmpl, newIds, newId
21 #include "HsVersions.h"
23 -- import {-# SOURCE #-} Simplify
24 -- import {-# SOURCE #-} MagicUFs
26 import Id ( GenId, mkSysLocal, mkIdWithNewUniq, Id )
27 import CoreUnfold ( SimpleUnfolding )
29 import SrcLoc ( noSrcLoc )
30 import TyVar ( cloneTyVar, TyVar )
32 import UniqSupply ( getUnique, getUniques, splitUniqSupply,
35 import Util ( zipWithEqual, Eager, appEager )
39 infixr 9 `thenSmpl`, `thenSmpl_`
42 %************************************************************************
44 \subsection{Monad plumbing}
46 %************************************************************************
48 For the simplifier monad, we want to {\em thread} a unique supply and a counter.
49 (Command-line switches move around through the explicitly-passed SimplEnv.)
54 -> SimplCount -- things being threaded
55 -> (result, SimplCount)
59 initSmpl :: UniqSupply -- no init count; set to 0
63 initSmpl us m = m us zeroSimplCount
65 {-# INLINE thenSmpl #-}
66 {-# INLINE thenSmpl_ #-}
67 {-# INLINE returnSmpl #-}
69 returnSmpl :: a -> SmplM a
70 returnSmpl e us sc = (e, sc)
72 thenSmpl :: SmplM a -> (a -> SmplM b) -> SmplM b
73 thenSmpl_ :: SmplM a -> SmplM b -> SmplM b
76 = case splitUniqSupply us of { (s1, s2) ->
77 case (m s1 sc0) of { (m_result, sc1) ->
81 = case splitUniqSupply us of { (s1, s2) ->
82 case (m s1 sc0) of { (_, sc1) ->
85 mapSmpl :: (a -> SmplM b) -> [a] -> SmplM [b]
86 mapAndUnzipSmpl :: (a -> SmplM (b, c)) -> [a] -> SmplM ([b],[c])
88 mapSmpl f [] = returnSmpl []
90 = f x `thenSmpl` \ x' ->
91 mapSmpl f xs `thenSmpl` \ xs' ->
94 mapAndUnzipSmpl f [] = returnSmpl ([],[])
95 mapAndUnzipSmpl f (x:xs)
96 = f x `thenSmpl` \ (r1, r2) ->
97 mapAndUnzipSmpl f xs `thenSmpl` \ (rs1, rs2) ->
98 returnSmpl (r1:rs1, r2:rs2)
102 %************************************************************************
104 \subsection{Counting up what we've done}
106 %************************************************************************
108 The assoc list isn't particularly costly, because we only use
109 the number of ticks in ``real life.''
111 The right thing to do, if you want that to go fast, is thread
112 a mutable array through @SimplM@.
116 = SimplCount FAST_INT -- number of ticks
117 [(TickType, Int)] -- assoc list of all diff kinds of ticks
120 type UnfoldingHistory = (Int, -- N
121 [(Id,Int)], -- Last N unfoldings
122 [(Id,Int)]) -- The MaxUnfoldHistory unfoldings before that
125 = UnfoldingDone | MagicUnfold | ConReused
126 | CaseFloatFromLet | CaseOfCase
127 | LetFloatFromLet | LetFloatFromCase
128 | KnownBranch | Let2Case
129 | CaseMerge | CaseElim
131 | AtomicRhs -- Rhs of a let-expression was an atom
137 {- BEGIN F/B ENTRIES -}
139 | FoldrBuild -- foldr f z (build g) ==>
140 | FoldrAugment -- foldr f z (augment g z) ==>
141 | Foldr_Nil -- foldr f z [] ==>
142 | Foldr_List -- foldr f z (x:...) ==>
144 | FoldlBuild -- foldl f z (build g) ==>
145 | FoldlAugment -- foldl f z (augment g z) ==>
146 | Foldl_Nil -- foldl f z [] ==>
147 | Foldl_List -- foldl f z (x:...) ==>
149 | Foldr_Cons_Nil -- foldr (:) [] => id
150 | Foldr_Cons -- foldr (:) => flip (++)
152 | Str_FoldrStr -- foldr f z "hello" => unpackFoldrPS__ f z "hello"
153 | Str_UnpackCons -- unpackFoldrPS# (:) z "hello" => unpackAppendPS__ z "hello"
154 | Str_UnpackNil -- unpackAppendPS__ [] "hello" => "hello"
155 {- END F/B ENTRIES -}
156 deriving (Eq, Ord, Ix)
158 instance Text TickType where
159 showsPrec p UnfoldingDone = showString "UnfoldingDone "
160 showsPrec p MagicUnfold = showString "MagicUnfold "
161 showsPrec p ConReused = showString "ConReused "
162 showsPrec p CaseFloatFromLet= showString "CaseFloatFromLet "
163 showsPrec p CaseOfCase = showString "CaseOfCase "
164 showsPrec p LetFloatFromLet = showString "LetFloatFromLet "
165 showsPrec p LetFloatFromCase= showString "LetFloatFromCase "
166 showsPrec p KnownBranch = showString "KnownBranch "
167 showsPrec p Let2Case = showString "Let2Case "
168 showsPrec p CaseMerge = showString "CaseMerge "
169 showsPrec p CaseElim = showString "CaseElim "
170 showsPrec p CaseIdentity = showString "CaseIdentity "
171 showsPrec p AtomicRhs = showString "AtomicRhs "
172 showsPrec p EtaExpansion = showString "EtaExpansion "
173 showsPrec p CaseOfError = showString "CaseOfError "
174 showsPrec p TyBetaReduction = showString "TyBetaReduction "
175 showsPrec p BetaReduction = showString "BetaReduction "
176 showsPrec p SpecialisationDone
177 = showString "Specialisation "
179 -- Foldr/Build Stuff:
180 showsPrec p FoldrBuild = showString "FoldrBuild "
181 showsPrec p FoldrAugment = showString "FoldrAugment "
182 showsPrec p Foldr_Nil = showString "Foldr_Nil "
183 showsPrec p Foldr_List = showString "Foldr_List "
185 showsPrec p FoldlBuild = showString "FoldlBuild "
186 showsPrec p FoldlAugment = showString "FoldlAugment "
187 showsPrec p Foldl_Nil = showString "Foldl_Nil "
188 showsPrec p Foldl_List = showString "Foldl_List "
190 showsPrec p Foldr_Cons_Nil = showString "Foldr_Cons_Nil "
191 showsPrec p Foldr_Cons = showString "Foldr_Cons "
193 showsPrec p Str_FoldrStr = showString "Str_FoldrStr "
194 showsPrec p Str_UnpackCons = showString "Str_UnpackCons "
195 showsPrec p Str_UnpackNil = showString "Str_UnpackNil "
197 showSimplCount :: SimplCount -> String
199 showSimplCount (SimplCount _ stuff (_, unf1, unf2))
200 = shw stuff ++ "\nMost recent unfoldings: " ++ showSDoc (ppr (reverse unf2 ++ reverse unf1))
203 shw ((t,n):tns) | n /= 0 = show t ++ ('\t' : show n) ++ ('\n' : shw tns)
204 | otherwise = shw tns
206 zeroSimplCount :: SimplCount
208 = SimplCount ILIT(0) stuff (0, [], [])
211 [ (UnfoldingDone, 0),
214 (CaseFloatFromLet, 0),
216 (LetFloatFromLet, 0),
217 (LetFloatFromCase, 0),
228 (SpecialisationDone,0),
229 -- Foldr/Build Stuff:
245 --= array (con2tag_TickType UnfoldingDone, con2tag_TickType FoldrInline)
246 -- [ i := 0 | i <- indices zeroSimplCount ]
249 Counting-related monad functions:
251 tick :: TickType -> SmplM ()
253 tick tick_type us (SimplCount n stuff unf)
254 = -- pprTrace "Tick: " (text (show tick_type)) $
255 #ifdef OMIT_SIMPL_COUNTS
256 ((), SimplCount (n _ADD_ ILIT(1) stuff unf)) stuff -- don't change anything
259 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff unf)
261 new_stuff = inc_tick tick_type ILIT(1) stuff
264 maxUnfoldHistory :: Int
265 maxUnfoldHistory = 20
267 tickUnfold :: Id -> SmplM ()
268 tickUnfold id us (SimplCount n stuff (n_unf, unf1, unf2))
269 = -- pprTrace "Unfolding: " (ppr id) $
272 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff new_unf)
274 new_stuff = inc_tick UnfoldingDone ILIT(1) stuff
276 new_unf | n_unf >= maxUnfoldHistory = (1, [unf_item], unf1)
277 | otherwise = (n_unf+1, unf_item:unf1, unf2)
279 unf_item = (id, IBOX(n))
282 -- force list to avoid getting a chain of @inc_tick@ applications
283 -- building up on the heap. (Only true when not dumping stats).
287 seqTriple (_,_,_) y = y
289 tickN :: TickType -> Int -> SmplM ()
291 tickN tick_type 0 us counts
293 tickN tick_type IBOX(increment) us (SimplCount n stuff unf)
294 = -- pprTrace "Tick: " (text (show tick_type)) $
295 #ifdef OMIT_SIMPL_COUNTS
296 ((), SimplCount (n _ADD_ increment) stuff) -- don't change anything
299 ((), SimplCount (n _ADD_ increment) new_stuff unf)
301 new_stuff = inc_tick tick_type increment stuff
304 inc_tick tick_type n [] = panic "couldn't inc_tick!"
306 inc_tick tick_type n (x@(ttype, I# cnt#) : xs)
307 | ttype == tick_type = case cnt# +# n of
308 incd -> (ttype,IBOX(incd)) : xs
310 | otherwise = case inc_tick tick_type n xs of { [] -> [x]; ls -> x:ls }
313 simplCount :: SmplM Int
314 simplCount us sc@(SimplCount n _ _) = (IBOX(n), sc)
316 detailedSimplCount :: SmplM SimplCount
317 detailedSimplCount us sc = (sc, sc)
319 combineSimplCounts :: SimplCount -> SimplCount -> SimplCount
321 #ifdef OMIT_SIMPL_COUNTS
322 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
323 = SimplCount (n1 _ADD_ n2)
324 stuff2 -- just pick one
327 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
329 SimplCount (n1 _ADD_ n2) new_stuff unf2 -- Just pick the second for unfold history
331 new_stuff = zipWithEqual "combineSimplCounts" (\ (t1,c1) (t2,c2) -> (t1,c1+c2)) stuff1 stuff2
335 %************************************************************************
337 \subsection{Monad primitives}
339 %************************************************************************
342 newId :: Type -> SmplM Id
344 = (mkSysLocal SLIT("s") uniq ty noSrcLoc, sc)
348 newIds :: [Type] -> SmplM [Id]
350 = (zipWithEqual "newIds" mk_id tys uniqs, sc)
352 uniqs = getUniques (length tys) us
353 mk_id ty uniq = mkSysLocal SLIT("s") uniq ty noSrcLoc
355 cloneTyVarSmpl :: TyVar -> SmplM TyVar
357 cloneTyVarSmpl tyvar us sc
361 new_tyvar = cloneTyVar tyvar uniq
363 cloneId :: SimplEnv -> InBinder -> SmplM OutId
364 cloneId env (id,_) us sc
365 = simplTyInId env id `appEager` \ id_with_new_ty ->
366 (mkIdWithNewUniq id_with_new_ty uniq, sc)
370 cloneIds :: SimplEnv -> [InBinder] -> SmplM [OutId]
371 cloneIds env binders = mapSmpl (cloneId env) binders