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 )
40 import Outputable ( Outputable(..) )
42 infixr 9 `thenSmpl`, `thenSmpl_`
45 %************************************************************************
47 \subsection{Monad plumbing}
49 %************************************************************************
51 For the simplifier monad, we want to {\em thread} a unique supply and a counter.
52 (Command-line switches move around through the explicitly-passed SimplEnv.)
57 -> SimplCount -- things being threaded
58 -> (result, SimplCount)
62 initSmpl :: UniqSupply -- no init count; set to 0
66 initSmpl us m = m us zeroSimplCount
68 {-# INLINE thenSmpl #-}
69 {-# INLINE thenSmpl_ #-}
70 {-# INLINE returnSmpl #-}
72 returnSmpl :: a -> SmplM a
73 returnSmpl e us sc = (e, sc)
75 thenSmpl :: SmplM a -> (a -> SmplM b) -> SmplM b
76 thenSmpl_ :: SmplM a -> SmplM b -> SmplM b
79 = case splitUniqSupply us of { (s1, s2) ->
80 case (m s1 sc0) of { (m_result, sc1) ->
84 = case splitUniqSupply us of { (s1, s2) ->
85 case (m s1 sc0) of { (_, sc1) ->
88 mapSmpl :: (a -> SmplM b) -> [a] -> SmplM [b]
89 mapAndUnzipSmpl :: (a -> SmplM (b, c)) -> [a] -> SmplM ([b],[c])
91 mapSmpl f [] = returnSmpl []
93 = f x `thenSmpl` \ x' ->
94 mapSmpl f xs `thenSmpl` \ xs' ->
97 mapAndUnzipSmpl f [] = returnSmpl ([],[])
98 mapAndUnzipSmpl f (x:xs)
99 = f x `thenSmpl` \ (r1, r2) ->
100 mapAndUnzipSmpl f xs `thenSmpl` \ (rs1, rs2) ->
101 returnSmpl (r1:rs1, r2:rs2)
105 %************************************************************************
107 \subsection{Counting up what we've done}
109 %************************************************************************
111 The assoc list isn't particularly costly, because we only use
112 the number of ticks in ``real life.''
114 The right thing to do, if you want that to go fast, is thread
115 a mutable array through @SimplM@.
119 = SimplCount FAST_INT -- number of ticks
120 [(TickType, Int)] -- assoc list of all diff kinds of ticks
123 type UnfoldingHistory = (Int, -- N
124 [(Id,Int)], -- Last N unfoldings
125 [(Id,Int)]) -- The MaxUnfoldHistory unfoldings before that
128 = UnfoldingDone | MagicUnfold | ConReused
129 | CaseFloatFromLet | CaseOfCase
130 | LetFloatFromLet | LetFloatFromCase
131 | KnownBranch | Let2Case
132 | CaseMerge | CaseElim
134 | AtomicRhs -- Rhs of a let-expression was an atom
140 {- BEGIN F/B ENTRIES -}
142 | FoldrBuild -- foldr f z (build g) ==>
143 | FoldrAugment -- foldr f z (augment g z) ==>
144 | Foldr_Nil -- foldr f z [] ==>
145 | Foldr_List -- foldr f z (x:...) ==>
147 | FoldlBuild -- foldl f z (build g) ==>
148 | FoldlAugment -- foldl f z (augment g z) ==>
149 | Foldl_Nil -- foldl f z [] ==>
150 | Foldl_List -- foldl f z (x:...) ==>
152 | Foldr_Cons_Nil -- foldr (:) [] => id
153 | Foldr_Cons -- foldr (:) => flip (++)
155 | Str_FoldrStr -- foldr f z "hello" => unpackFoldrPS__ f z "hello"
156 | Str_UnpackCons -- unpackFoldrPS# (:) z "hello" => unpackAppendPS__ z "hello"
157 | Str_UnpackNil -- unpackAppendPS__ [] "hello" => "hello"
158 {- END F/B ENTRIES -}
159 deriving (Eq, Ord, Ix)
161 instance Text TickType where
162 showsPrec p UnfoldingDone = showString "UnfoldingDone "
163 showsPrec p MagicUnfold = showString "MagicUnfold "
164 showsPrec p ConReused = showString "ConReused "
165 showsPrec p CaseFloatFromLet= showString "CaseFloatFromLet "
166 showsPrec p CaseOfCase = showString "CaseOfCase "
167 showsPrec p LetFloatFromLet = showString "LetFloatFromLet "
168 showsPrec p LetFloatFromCase= showString "LetFloatFromCase "
169 showsPrec p KnownBranch = showString "KnownBranch "
170 showsPrec p Let2Case = showString "Let2Case "
171 showsPrec p CaseMerge = showString "CaseMerge "
172 showsPrec p CaseElim = showString "CaseElim "
173 showsPrec p CaseIdentity = showString "CaseIdentity "
174 showsPrec p AtomicRhs = showString "AtomicRhs "
175 showsPrec p EtaExpansion = showString "EtaExpansion "
176 showsPrec p CaseOfError = showString "CaseOfError "
177 showsPrec p TyBetaReduction = showString "TyBetaReduction "
178 showsPrec p BetaReduction = showString "BetaReduction "
179 showsPrec p SpecialisationDone
180 = showString "Specialisation "
182 -- Foldr/Build Stuff:
183 showsPrec p FoldrBuild = showString "FoldrBuild "
184 showsPrec p FoldrAugment = showString "FoldrAugment "
185 showsPrec p Foldr_Nil = showString "Foldr_Nil "
186 showsPrec p Foldr_List = showString "Foldr_List "
188 showsPrec p FoldlBuild = showString "FoldlBuild "
189 showsPrec p FoldlAugment = showString "FoldlAugment "
190 showsPrec p Foldl_Nil = showString "Foldl_Nil "
191 showsPrec p Foldl_List = showString "Foldl_List "
193 showsPrec p Foldr_Cons_Nil = showString "Foldr_Cons_Nil "
194 showsPrec p Foldr_Cons = showString "Foldr_Cons "
196 showsPrec p Str_FoldrStr = showString "Str_FoldrStr "
197 showsPrec p Str_UnpackCons = showString "Str_UnpackCons "
198 showsPrec p Str_UnpackNil = showString "Str_UnpackNil "
200 showSimplCount :: SimplCount -> String
202 showSimplCount (SimplCount _ stuff (_, unf1, unf2))
203 = shw stuff ++ "\nMost recent unfoldings: " ++ show (ppr PprDebug (reverse unf2 ++ reverse unf1))
206 shw ((t,n):tns) | n /= 0 = show t ++ ('\t' : show n) ++ ('\n' : shw tns)
207 | otherwise = shw tns
209 -- ToDo: move to Outputable
210 instance Outputable Int where
213 zeroSimplCount :: SimplCount
215 = SimplCount ILIT(0) stuff (0, [], [])
218 [ (UnfoldingDone, 0),
221 (CaseFloatFromLet, 0),
223 (LetFloatFromLet, 0),
224 (LetFloatFromCase, 0),
235 (SpecialisationDone,0),
236 -- Foldr/Build Stuff:
252 --= array (con2tag_TickType UnfoldingDone, con2tag_TickType FoldrInline)
253 -- [ i := 0 | i <- indices zeroSimplCount ]
256 Counting-related monad functions:
258 tick :: TickType -> SmplM ()
260 tick tick_type us (SimplCount n stuff unf)
261 = -- pprTrace "Tick: " (text (show tick_type)) $
262 #ifdef OMIT_SIMPL_COUNTS
263 ((), SimplCount (n _ADD_ ILIT(1) stuff unf)) stuff -- don't change anything
266 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff unf)
268 new_stuff = inc_tick tick_type ILIT(1) stuff
271 maxUnfoldHistory :: Int
272 maxUnfoldHistory = 20
274 tickUnfold :: Id -> SmplM ()
275 tickUnfold id us (SimplCount n stuff (n_unf, unf1, unf2))
276 = -- pprTrace "Unfolding: " (ppr PprDebug id) $
279 ((), SimplCount (n _ADD_ ILIT(1)) new_stuff new_unf)
281 new_stuff = inc_tick UnfoldingDone ILIT(1) stuff
283 new_unf | n_unf >= maxUnfoldHistory = (1, [unf_item], unf1)
284 | otherwise = (n_unf+1, unf_item:unf1, unf2)
286 unf_item = (id, IBOX(n))
289 -- force list to avoid getting a chain of @inc_tick@ applications
290 -- building up on the heap. (Only true when not dumping stats).
294 seqTriple (_,_,_) y = y
296 tickN :: TickType -> Int -> SmplM ()
298 tickN tick_type 0 us counts
300 tickN tick_type IBOX(increment) us (SimplCount n stuff unf)
301 = -- pprTrace "Tick: " (text (show tick_type)) $
302 #ifdef OMIT_SIMPL_COUNTS
303 ((), SimplCount (n _ADD_ increment) stuff) -- don't change anything
306 ((), SimplCount (n _ADD_ increment) new_stuff unf)
308 new_stuff = inc_tick tick_type increment stuff
311 inc_tick tick_type n [] = panic "couldn't inc_tick!"
313 inc_tick tick_type n (x@(ttype, I# cnt#) : xs)
314 | ttype == tick_type = case cnt# +# n of
315 incd -> (ttype,IBOX(incd)) : xs
317 | otherwise = case inc_tick tick_type n xs of { [] -> [x]; ls -> x:ls }
320 simplCount :: SmplM Int
321 simplCount us sc@(SimplCount n _ _) = (IBOX(n), sc)
323 detailedSimplCount :: SmplM SimplCount
324 detailedSimplCount us sc = (sc, sc)
326 combineSimplCounts :: SimplCount -> SimplCount -> SimplCount
328 #ifdef OMIT_SIMPL_COUNTS
329 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
330 = SimplCount (n1 _ADD_ n2)
331 stuff2 -- just pick one
334 combineSimplCounts (SimplCount n1 stuff1 unf1) (SimplCount n2 stuff2 unf2)
336 SimplCount (n1 _ADD_ n2) new_stuff unf2 -- Just pick the second for unfold history
338 new_stuff = zipWithEqual "combineSimplCounts" (\ (t1,c1) (t2,c2) -> (t1,c1+c2)) stuff1 stuff2
342 %************************************************************************
344 \subsection{Monad primitives}
346 %************************************************************************
349 newId :: Type -> SmplM Id
351 = (mkSysLocal SLIT("s") uniq ty noSrcLoc, sc)
355 newIds :: [Type] -> SmplM [Id]
357 = (zipWithEqual "newIds" mk_id tys uniqs, sc)
359 uniqs = getUniques (length tys) us
360 mk_id ty uniq = mkSysLocal SLIT("s") uniq ty noSrcLoc
362 cloneTyVarSmpl :: TyVar -> SmplM TyVar
364 cloneTyVarSmpl tyvar us sc
368 new_tyvar = cloneTyVar tyvar uniq
370 cloneId :: SimplEnv -> InBinder -> SmplM OutId
371 cloneId env (id,_) us sc
372 = simplTyInId env id `appEager` \ id_with_new_ty ->
373 (mkIdWithNewUniq id_with_new_ty uniq, sc)
377 cloneIds :: SimplEnv -> [InBinder] -> SmplM [OutId]
378 cloneIds env binders = mapSmpl (cloneId env) binders