2 % (c) The AQUA Project, Glasgow University, 1993-1995
4 \section[SimplMonad]{The simplifier Monad}
7 #include "HsVersions.h"
11 initSmpl, returnSmpl, thenSmpl, thenSmpl_,
12 mapSmpl, mapAndUnzipSmpl,
15 SimplCount{-abstract-}, TickType(..), tick, tickN,
16 simplCount, detailedSimplCount,
17 zeroSimplCount, showSimplCount, combineSimplCounts,
20 cloneId, cloneIds, cloneTyVarSmpl, newIds, newId,
22 -- and to make the interface self-sufficient...
23 BinderInfo, CoreExpr, Id, PrimOp, TyVar, UniType,
26 IF_ATTACK_PRAGMAS(COMMA splitUniqSupply)
29 IMPORT_Trace -- ToDo: rm (debugging)
34 import AbsUniType ( cloneTyVar )
36 import Id ( mkIdWithNewUniq, mkSysLocal )
39 import SrcLoc ( mkUnknownSrcLoc, SrcLoc )
44 infixr 9 `thenSmpl`, `thenSmpl_`
47 %************************************************************************
49 \subsection[Monad]{Monad plumbing}
51 %************************************************************************
53 For the simplifier monad, we want to {\em thread} a unique supply and a counter.
54 (Command-line switches move around through the explicitly-passed SimplEnv.)
59 -> SimplCount -- things being threaded
60 -> (result, SimplCount)
64 initSmpl :: SplitUniqSupply -- no init count; set to 0
68 initSmpl us m = m us zeroSimplCount
70 #ifdef __GLASGOW_HASKELL__
71 {-# INLINE thenSmpl #-}
72 {-# INLINE thenSmpl_ #-}
73 {-# INLINE returnSmpl #-}
76 returnSmpl :: a -> SmplM a
77 returnSmpl e us sc = (e, sc)
79 thenSmpl :: SmplM a -> (a -> SmplM b) -> SmplM b
80 thenSmpl_ :: SmplM a -> SmplM b -> SmplM b
83 = case splitUniqSupply us of { (s1, s2) ->
84 case (m s1 sc0) of { (m_result, sc1) ->
88 = case splitUniqSupply us of { (s1, s2) ->
89 case (m s1 sc0) of { (_, sc1) ->
92 mapSmpl :: (a -> SmplM b) -> [a] -> SmplM [b]
93 mapAndUnzipSmpl :: (a -> SmplM (b, c)) -> [a] -> SmplM ([b],[c])
95 mapSmpl f [] = returnSmpl []
97 = f x `thenSmpl` \ x' ->
98 mapSmpl f xs `thenSmpl` \ xs' ->
101 mapAndUnzipSmpl f [] = returnSmpl ([],[])
102 mapAndUnzipSmpl f (x:xs)
103 = f x `thenSmpl` \ (r1, r2) ->
104 mapAndUnzipSmpl f xs `thenSmpl` \ (rs1, rs2) ->
105 returnSmpl (r1:rs1, r2:rs2)
109 %************************************************************************
111 \subsection[SimplCount]{Counting up what we've done}
113 %************************************************************************
115 The assoc list isn't particularly costly, because we only use
116 the number of ticks in ``real life.''
118 The right thing to do, if you want that to go fast, is thread
119 a mutable array through @SimplM@.
123 = SimplCount FAST_INT -- number of ticks
124 [(TickType, Int)] -- assoc list of all diff kinds of ticks
127 = UnfoldingDone {-UNUSED: | Unused -}
128 | FoldrBuild | MagicUnfold | ConReused
129 | CaseFloatFromLet | CaseOfCase {-UNUSED: | CaseFloatFromApp -}
130 | LetFloatFromLet | LetFloatFromCase {-UNUSED: | LetFloatFromApp -}
131 | KnownBranch | Let2Case {-UNUSED: | UnboxingLet2Case -}
132 | CaseMerge {-UNUSED: | CaseToLet-} | CaseElim
134 | AtomicRhs -- Rhs of a let-expression was an atom
135 | EtaExpansion {-UNUSED: | ArityExpand-}
136 {-UNUSED: | ConstantFolding-} | CaseOfError {-UNUSED: | InlineRemoved -}
145 deriving (Eq, Ord, Ix)
147 instance Text TickType where
148 showsPrec p UnfoldingDone = showString "UnfoldingDone "
149 --UNUSED: showsPrec p Unused = showString "Unused "
150 showsPrec p FoldrBuild = showString "FoldrBuild "
151 showsPrec p MagicUnfold = showString "MagicUnfold "
152 showsPrec p ConReused = showString "ConReused "
153 showsPrec p CaseFloatFromLet= showString "CaseFloatFromLet "
154 showsPrec p CaseOfCase = showString "CaseOfCase "
155 --UNUSED: showsPrec p CaseFloatFromApp= showString "CaseFloatFromApp "
156 showsPrec p LetFloatFromLet = showString "LetFloatFromLet "
157 showsPrec p LetFloatFromCase= showString "LetFloatFromCase "
158 --UNUSED: showsPrec p LetFloatFromApp = showString "LetFloatFromApp "
159 showsPrec p KnownBranch = showString "KnownBranch "
160 showsPrec p Let2Case = showString "Let2Case "
161 --UNUSED: showsPrec p UnboxingLet2Case= showString "UnboxingLet2Case "
162 showsPrec p CaseMerge = showString "CaseMerge "
163 --UNUSED: showsPrec p CaseToLet = showString "CaseToLet "
164 showsPrec p CaseElim = showString "CaseElim "
165 showsPrec p CaseIdentity = showString "CaseIdentity "
166 showsPrec p AtomicRhs = showString "AtomicRhs "
167 showsPrec p EtaExpansion = showString "EtaExpansion "
168 --UNUSED: showsPrec p ArityExpand = showString "ArityExpand "
169 --UNUSED: showsPrec p ConstantFolding = showString "ConstantFolding "
170 showsPrec p CaseOfError = showString "CaseOfError "
171 --UNUSED: showsPrec p InlineRemoved = showString "InlineRemoved "
172 showsPrec p FoldrConsNil = showString "FoldrConsNil "
173 showsPrec p Foldr_Nil = showString "Foldr_Nil "
174 showsPrec p FoldrFoldr = showString "FoldrFoldr "
175 showsPrec p Foldr_List = showString "Foldr_List "
176 showsPrec p FoldrCons = showString "FoldrCons "
177 showsPrec p FoldrInline = showString "FoldrInline "
178 showsPrec p TyBetaReduction = showString "TyBetaReduction "
179 showsPrec p BetaReduction = showString "BetaReduction "
181 showSimplCount :: SimplCount -> String
183 showSimplCount (SimplCount _ stuff)
187 shw ((t,n):tns) | n /= 0 = show t ++ ('\t' : show n) ++ ('\n' : shw tns)
188 | otherwise = shw tns
190 zeroSimplCount :: SimplCount
194 --UNUSED: (Unused, 0),
198 (CaseFloatFromLet, 0),
200 --UNUSED: (CaseFloatFromApp, 0),
201 (LetFloatFromLet, 0),
202 (LetFloatFromCase, 0),
203 --UNUSED: (LetFloatFromApp, 0),
206 --UNUSED: (UnboxingLet2Case, 0),
208 --UNUSED: (CaseToLet, 0),
213 --UNUSED: (ArityExpand,0),
214 --UNUSED: (ConstantFolding, 0),
216 --UNUSED: (InlineRemoved,0),
226 --= array (con2tag_TickType UnfoldingDone, con2tag_TickType FoldrInline)
227 -- [ i := 0 | i <- indices zeroSimplCount ]
230 Counting-related monad functions:
232 tick :: TickType -> SmplM ()
234 tick tick_type us (SimplCount n stuff)
235 = ((), SimplCount (n _ADD_ ILIT(1))
236 #ifdef OMIT_SIMPL_COUNTS
237 stuff -- don't change anything
243 inc_tick [] = panic "couldn't inc_tick!"
244 inc_tick (x@(ttype, cnt) : xs)
245 = if ttype == tick_type then
253 tickN :: TickType -> Int -> SmplM ()
255 tickN tick_type IBOX(increment) us (SimplCount n stuff)
256 = ((), SimplCount (n _ADD_ increment)
257 #ifdef OMIT_SIMPL_COUNTS
258 stuff -- don't change anything
264 inc_tick [] = panic "couldn't inc_tick!"
265 inc_tick (x@(ttype, cnt) : xs)
266 = if ttype == tick_type then
268 incd = cnt + IBOX(increment)
274 simplCount :: SmplM Int
275 simplCount us sc@(SimplCount n _) = (IBOX(n), sc)
277 detailedSimplCount :: SmplM SimplCount
278 detailedSimplCount us sc = (sc, sc)
280 combineSimplCounts :: SimplCount -> SimplCount -> SimplCount
282 #ifdef OMIT_SIMPL_COUNTS
283 combineSimplCounts (SimplCount n1 stuff1) (SimplCount n2 stuff2)
284 = SimplCount (n1 _ADD_ n2)
285 stuff1 -- just pick one
287 combineSimplCounts (SimplCount n1 stuff1) (SimplCount n2 stuff2)
288 = SimplCount (n1 _ADD_ n2)
289 (zipWith (\ (t1,c1) (t2,c2) -> (t1,c1+c2)) stuff1 stuff2)
293 %************************************************************************
295 \subsection{Monad primitives}
297 %************************************************************************
300 newId :: UniType -> SmplM Id
302 = (mkSysLocal SLIT("s") uniq ty mkUnknownSrcLoc, sc)
306 newIds :: [UniType] -> SmplM [Id]
308 = (zipWith mk_id tys uniqs, sc)
310 uniqs = getSUniques (length tys) us
311 mk_id ty uniq = mkSysLocal SLIT("s") uniq ty mkUnknownSrcLoc
313 cloneTyVarSmpl :: TyVar -> SmplM TyVar
315 cloneTyVarSmpl tyvar us sc
319 new_tyvar = cloneTyVar tyvar uniq
321 cloneId :: SimplEnv -> InBinder -> SmplM OutId
322 cloneId env (id,_) us sc
323 = (mkIdWithNewUniq id_with_new_ty uniq, sc)
325 id_with_new_ty = simplTyInId env id
328 cloneIds :: SimplEnv -> [InBinder] -> SmplM [OutId]
329 cloneIds env binders = mapSmpl (cloneId env) binders