2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplMonad]{The simplifier Monad}
7 #include "HsVersions.h"
11 initSmpl, returnSmpl, thenSmpl, thenSmpl_,
12 mapSmpl, mapAndUnzipSmpl,
15 SimplCount{-abstract-}, TickType(..), tick, tickN, tickUnfold,
16 simplCount, detailedSimplCount,
17 zeroSimplCount, showSimplCount, combineSimplCounts,
20 cloneId, cloneIds, cloneTyVarSmpl, newIds, newId
26 IMPORT_DELOOPER(SmplLoop) -- well, cheating sort of
28 import Id ( GenId, mkSysLocal, mkIdWithNewUniq, SYN_IE(Id) )
29 import CoreUnfold ( SimpleUnfolding )
31 import SrcLoc ( noSrcLoc )
32 import TyVar ( cloneTyVar, SYN_IE(TyVar) )
33 import Type ( SYN_IE(Type) )
34 import UniqSupply ( getUnique, getUniques, splitUniqSupply,
37 import Util ( zipWithEqual, panic, SYN_IE(Eager), appEager, pprTrace )
39 import Outputable ( PprStyle(..), Outputable(..) )
41 infixr 9 `thenSmpl`, `thenSmpl_`
44 %************************************************************************
46 \subsection{Monad plumbing}
48 %************************************************************************
50 For the simplifier monad, we want to {\em thread} a unique supply and a counter.
51 (Command-line switches move around through the explicitly-passed SimplEnv.)
56 -> SimplCount -- things being threaded
57 -> (result, SimplCount)
61 initSmpl :: UniqSupply -- no init count; set to 0
65 initSmpl us m = m us zeroSimplCount
67 {-# INLINE thenSmpl #-}
68 {-# INLINE thenSmpl_ #-}
69 {-# INLINE returnSmpl #-}
71 returnSmpl :: a -> SmplM a
72 returnSmpl e us sc = (e, sc)
74 thenSmpl :: SmplM a -> (a -> SmplM b) -> SmplM b
75 thenSmpl_ :: SmplM a -> SmplM b -> SmplM b
78 = case splitUniqSupply us of { (s1, s2) ->
79 case (m s1 sc0) of { (m_result, sc1) ->
83 = case splitUniqSupply us of { (s1, s2) ->
84 case (m s1 sc0) of { (_, sc1) ->
87 mapSmpl :: (a -> SmplM b) -> [a] -> SmplM [b]
88 mapAndUnzipSmpl :: (a -> SmplM (b, c)) -> [a] -> SmplM ([b],[c])
90 mapSmpl f [] = returnSmpl []
92 = f x `thenSmpl` \ x' ->
93 mapSmpl f xs `thenSmpl` \ xs' ->
96 mapAndUnzipSmpl f [] = returnSmpl ([],[])
97 mapAndUnzipSmpl f (x:xs)
98 = f x `thenSmpl` \ (r1, r2) ->
99 mapAndUnzipSmpl f xs `thenSmpl` \ (rs1, rs2) ->
100 returnSmpl (r1:rs1, r2:rs2)
104 %************************************************************************
106 \subsection{Counting up what we've done}
108 %************************************************************************
110 The assoc list isn't particularly costly, because we only use
111 the number of ticks in ``real life.''
113 The right thing to do, if you want that to go fast, is thread
114 a mutable array through @SimplM@.
118 = SimplCount FAST_INT -- number of ticks
119 [(TickType, Int)] -- assoc list of all diff kinds of ticks
122 type UnfoldingHistory = (Int, -- N
123 [(Id,Int)], -- Last N unfoldings
124 [(Id,Int)]) -- The MaxUnfoldHistory unfoldings before that
127 = UnfoldingDone | MagicUnfold | ConReused
128 | CaseFloatFromLet | CaseOfCase
129 | LetFloatFromLet | LetFloatFromCase
130 | KnownBranch | Let2Case
131 | CaseMerge | CaseElim
133 | AtomicRhs -- Rhs of a let-expression was an atom
139 {- BEGIN F/B ENTRIES -}
141 | FoldrBuild -- foldr f z (build g) ==>
142 | FoldrAugment -- foldr f z (augment g z) ==>
143 | Foldr_Nil -- foldr f z [] ==>
144 | Foldr_List -- foldr f z (x:...) ==>
146 | FoldlBuild -- foldl f z (build g) ==>
147 | FoldlAugment -- foldl f z (augment g z) ==>
148 | Foldl_Nil -- foldl f z [] ==>
149 | Foldl_List -- foldl f z (x:...) ==>
151 | Foldr_Cons_Nil -- foldr (:) [] => id
152 | Foldr_Cons -- foldr (:) => flip (++)
154 | Str_FoldrStr -- foldr f z "hello" => unpackFoldrPS__ f z "hello"
155 | Str_UnpackCons -- unpackFoldrPS# (:) z "hello" => unpackAppendPS__ z "hello"
156 | Str_UnpackNil -- unpackAppendPS__ [] "hello" => "hello"
157 {- END F/B ENTRIES -}
158 deriving (Eq, Ord, Ix)
160 instance Text TickType where
161 showsPrec p UnfoldingDone = showString "UnfoldingDone "
162 showsPrec p MagicUnfold = showString "MagicUnfold "
163 showsPrec p ConReused = showString "ConReused "
164 showsPrec p CaseFloatFromLet= showString "CaseFloatFromLet "
165 showsPrec p CaseOfCase = showString "CaseOfCase "
166 showsPrec p LetFloatFromLet = showString "LetFloatFromLet "
167 showsPrec p LetFloatFromCase= showString "LetFloatFromCase "
168 showsPrec p KnownBranch = showString "KnownBranch "
169 showsPrec p Let2Case = showString "Let2Case "
170 showsPrec p CaseMerge = showString "CaseMerge "
171 showsPrec p CaseElim = showString "CaseElim "
172 showsPrec p CaseIdentity = showString "CaseIdentity "
173 showsPrec p AtomicRhs = showString "AtomicRhs "
174 showsPrec p EtaExpansion = showString "EtaExpansion "
175 showsPrec p CaseOfError = showString "CaseOfError "
176 showsPrec p TyBetaReduction = showString "TyBetaReduction "
177 showsPrec p BetaReduction = showString "BetaReduction "
178 showsPrec p SpecialisationDone
179 = showString "Specialisation "
181 -- Foldr/Build Stuff:
182 showsPrec p FoldrBuild = showString "FoldrBuild "
183 showsPrec p FoldrAugment = showString "FoldrAugment "
184 showsPrec p Foldr_Nil = showString "Foldr_Nil "
185 showsPrec p Foldr_List = showString "Foldr_List "
187 showsPrec p FoldlBuild = showString "FoldlBuild "
188 showsPrec p FoldlAugment = showString "FoldlAugment "
189 showsPrec p Foldl_Nil = showString "Foldl_Nil "
190 showsPrec p Foldl_List = showString "Foldl_List "
192 showsPrec p Foldr_Cons_Nil = showString "Foldr_Cons_Nil "
193 showsPrec p Foldr_Cons = showString "Foldr_Cons "
195 showsPrec p Str_FoldrStr = showString "Str_FoldrStr "
196 showsPrec p Str_UnpackCons = showString "Str_UnpackCons "
197 showsPrec p Str_UnpackNil = showString "Str_UnpackNil "
199 showSimplCount :: SimplCount -> String
201 showSimplCount (SimplCount _ stuff (_, unf1, unf2))
202 = shw stuff ++ "\nMost recent unfoldings: " ++ show (ppr PprDebug (reverse unf2 ++ reverse unf1))
205 shw ((t,n):tns) | n /= 0 = show t ++ ('\t' : show n) ++ ('\n' : shw tns)
206 | otherwise = shw tns
208 -- ToDo: move to Outputable
209 instance Outputable Int where
212 zeroSimplCount :: SimplCount
214 = SimplCount ILIT(0) stuff (0, [], [])
217 [ (UnfoldingDone, 0),
220 (CaseFloatFromLet, 0),
222 (LetFloatFromLet, 0),
223 (LetFloatFromCase, 0),
234 (SpecialisationDone,0),
235 -- Foldr/Build Stuff:
251 --= array (con2tag_TickType UnfoldingDone, con2tag_TickType FoldrInline)
252 -- [ i := 0 | i <- indices zeroSimplCount ]
255 Counting-related monad functions:
257 tick :: TickType -> SmplM ()
259 tick tick_type us (SimplCount n stuff unf)
260 = -- pprTrace "Tick: " (text (show tick_type)) $
261 #ifdef OMIT_SIMPL_COUNTS
262 ((), SimplCount (n _ADD_ ILIT(1) stuff unf)) stuff -- don't change anything
265 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff unf)
267 new_stuff = inc_tick tick_type ILIT(1) stuff
270 maxUnfoldHistory :: Int
271 maxUnfoldHistory = 20
273 tickUnfold :: Id -> SmplM ()
274 tickUnfold id us (SimplCount n stuff (n_unf, unf1, unf2))
275 = -- pprTrace "Unfolding: " (ppr PprDebug id) $
278 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff new_unf)
280 new_stuff = inc_tick UnfoldingDone ILIT(1) stuff
282 new_unf | n_unf >= maxUnfoldHistory = (1, [unf_item], unf1)
283 | otherwise = (n_unf+1, unf_item:unf1, unf2)
285 unf_item = (id, IBOX(n))
288 -- force list to avoid getting a chain of @inc_tick@ applications
289 -- building up on the heap. (Only true when not dumping stats).
293 seqTriple (_,_,_) y = y
295 tickN :: TickType -> Int -> SmplM ()
297 tickN tick_type 0 us counts
299 tickN tick_type IBOX(increment) us (SimplCount n stuff unf)
300 = -- pprTrace "Tick: " (text (show tick_type)) $
301 #ifdef OMIT_SIMPL_COUNTS
302 ((), SimplCount (n _ADD_ increment) stuff) -- don't change anything
305 ((), SimplCount (n _ADD_ increment) new_stuff unf)
307 new_stuff = inc_tick tick_type increment stuff
310 inc_tick tick_type n [] = panic "couldn't inc_tick!"
312 inc_tick tick_type n (x@(ttype, I# cnt#) : xs)
313 | ttype == tick_type = case cnt# +# n of
314 incd -> (ttype,IBOX(incd)) : xs
316 | otherwise = case inc_tick tick_type n xs of { [] -> [x]; ls -> x:ls }
319 simplCount :: SmplM Int
320 simplCount us sc@(SimplCount n _ _) = (IBOX(n), sc)
322 detailedSimplCount :: SmplM SimplCount
323 detailedSimplCount us sc = (sc, sc)
325 combineSimplCounts :: SimplCount -> SimplCount -> SimplCount
327 #ifdef OMIT_SIMPL_COUNTS
328 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
329 = SimplCount (n1 _ADD_ n2)
330 stuff2 -- just pick one
333 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
335 SimplCount (n1 _ADD_ n2) new_stuff unf2 -- Just pick the second for unfold history
337 new_stuff = zipWithEqual "combineSimplCounts" (\ (t1,c1) (t2,c2) -> (t1,c1+c2)) stuff1 stuff2
341 %************************************************************************
343 \subsection{Monad primitives}
345 %************************************************************************
348 newId :: Type -> SmplM Id
350 = (mkSysLocal SLIT("s") uniq ty noSrcLoc, sc)
354 newIds :: [Type] -> SmplM [Id]
356 = (zipWithEqual "newIds" mk_id tys uniqs, sc)
358 uniqs = getUniques (length tys) us
359 mk_id ty uniq = mkSysLocal SLIT("s") uniq ty noSrcLoc
361 cloneTyVarSmpl :: TyVar -> SmplM TyVar
363 cloneTyVarSmpl tyvar us sc
367 new_tyvar = cloneTyVar tyvar uniq
369 cloneId :: SimplEnv -> InBinder -> SmplM OutId
370 cloneId env (id,_) us sc
371 = simplTyInId env id `appEager` \ id_with_new_ty ->
372 (mkIdWithNewUniq id_with_new_ty uniq, sc)
376 cloneIds :: SimplEnv -> [InBinder] -> SmplM [OutId]
377 cloneIds env binders = mapSmpl (cloneId env) binders