2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[SatStgRhs]{Saturates RHSs when they are partial applications}
8 Subject: arg satis check
9 Date: Wed, 29 Apr 92 13:33:58 +0100
10 From: Simon L Peyton Jones <simonpj>
14 Another transformation to consider. We'd like to avoid
15 argument-satisfaction checks wherever possible. So, whenever we have an
16 STG binding application
18 f = vs \ xs -> g e1 ... en
20 where xs has one or more elements
22 where g is a known function with arity m+n,
26 f = vs \ xs++{x1...xm} -> g e1 ... en x1 .. xm
28 Now g has enough args. One arg-satisfaction check disappears;
29 the one for the closure incorporates the one for g.
31 You might like to consider variants, applying the transformation more
32 widely. I concluded that this was the only instance which made
33 sense, but I could be wrong.
38 The algorithm proceeds as follows:
41 Gather the arity information of the functions defined in this module
42 (as @getIdArity@ only knows about the arity of @ImportedIds@).
45 for every definition of the form
47 v = /\ts -> \vs -> f args
49 we try to match the arity of \tr{f} with the number of arguments.
50 If they do not match we insert extra lambdas to make that application
54 This is done for local definitions as well.
57 #include "HsVersions.h"
59 module SatStgRhs ( satStgRhs ) where
63 import Type ( splitTypeWithDictsAsArgs, Class,
64 TyVarTemplate, TauType(..)
67 import Id ( mkSysLocal, idType, getIdArity, addIdArity )
68 import IdInfo -- SIGH: ( arityMaybe, ArityInfo, OptIdInfo(..) )
69 import SrcLoc ( mkUnknownSrcLoc, SrcLoc )
77 type ExprArityInfo = Maybe Int -- Just n => This expression has a guaranteed
79 -- Nothing => Don't know how many args it needs
81 type Id_w_Arity = Id -- An Id with correct arity info pinned on it
82 type SatEnv = IdEnv Id_w_Arity -- Binds only local, let(rec)-bound things
87 \item adds extra args where necessary;
88 \item pins the correct arity on everything.
91 %************************************************************************
93 \subsection{Top-level list of bindings (a ``program'')}
95 %************************************************************************
98 satStgRhs :: [StgBinding] -> UniqSM [StgBinding]
100 satStgRhs p = satProgram nullIdEnv p
102 satProgram :: SatEnv -> [StgBinding] -> UniqSM [StgBinding]
103 satProgram env [] = returnUs []
105 satProgram env (bind:binds)
106 = satBinding True{-toplevel-} env bind `thenUs` \ (env2, bind2) ->
107 satProgram env2 binds `thenUs` \ binds2 ->
108 returnUs (bind2 : binds2)
111 %************************************************************************
113 \subsection{Bindings}
115 %************************************************************************
118 satBinding :: Bool -- True <=> top-level
121 -> UniqSM (SatEnv, StgBinding)
123 satBinding top env (StgNonRec b rhs)
124 = satRhs top env (b, rhs) `thenUs` \ (b2, rhs2) ->
126 env2 = addOneToIdEnv env b b2
128 returnUs (env2, StgNonRec b2 rhs2)
130 satBinding top env (StgRec pairs)
131 = -- Do it once to get the arities right...
132 mapUs (satRhs top env) pairs `thenUs` \ pairs2 ->
134 env2 = growIdEnvList env (map fst pairs `zip` map fst pairs2)
136 -- Do it again to *use* those arities:
137 mapUs (satRhs top env2) pairs `thenUs` \ pairs3 ->
139 returnUs (env2, StgRec pairs3)
141 satRhs :: Bool -> SatEnv -> (Id, StgRhs) -> UniqSM (Id_w_Arity, StgRhs)
143 satRhs top env (b, StgRhsCon cc con args) -- Nothing much to do here
145 b2 = b `addIdArity` 0 -- bound to a saturated constructor; hence zero.
147 returnUs (b2, StgRhsCon cc con (lookupArgs env args))
149 satRhs top env (b, StgRhsClosure cc bi fv u args body)
150 = satExpr env body `thenUs` \ (arity_info, body2) ->
152 num_args = length args
156 returnUs (num_args, StgRhsClosure cc bi fv u args body2)
159 ASSERT(needed_args >= 1)
161 let -- the arity we're aiming for is: what we already have ("args")
162 -- plus the ones requested in "arity_info"
163 new_arity = num_args + needed_args
165 -- get type info for this function:
166 (_,all_arg_tys,_) = splitTypeWithDictsAsArgs (idType b)
168 -- now, we already have "args"; we drop that many types
169 args_we_dont_have_tys = drop num_args all_arg_tys
171 -- finally, we take some of those (up to maybe all of them),
172 -- depending on how many "needed_args"
173 args_to_add_tys = take needed_args args_we_dont_have_tys
175 -- make up names for them
176 mapUs newName args_to_add_tys `thenUs` \ nns ->
178 -- and do the business
180 body3 = saturate body2 (map StgVarArg nns)
182 new_cc -- if we're adding args, we'd better not
183 -- keep calling something a CAF! (what about DICTs? ToDo: WDP 95/02)
184 = if not (isCafCC cc)
186 else if top then subsumedCosts else useCurrentCostCentre
188 returnUs (new_arity, StgRhsClosure new_cc bi fv ReEntrant (args++nns) body3)
190 `thenUs` \ (arity, rhs2) ->
192 b2 = b `addIdArity` arity
197 %************************************************************************
199 \subsection{Expressions}
201 %************************************************************************
204 satExpr :: SatEnv -> StgExpr -> UniqSM (ExprArityInfo, StgExpr)
206 satExpr env app@(StgApp (StgLitArg lit) [] lvs) = returnUs (Nothing, app)
208 satExpr env app@(StgApp (StgVarArg f) as lvs)
209 = returnUs (arity_to_return, StgApp (StgVarArg f2) as2 lvs)
211 as2 = lookupArgs env as
213 arity_to_return = case arityMaybe (getIdArity f2) of
216 Just f_arity -> if remaining_arity > 0
217 then Just remaining_arity
220 remaining_arity = f_arity - length as
222 satExpr env app@(StgCon con as lvs)
223 = returnUs (Nothing, StgCon con (lookupArgs env as) lvs)
225 satExpr env app@(StgPrim op as lvs)
226 = returnUs (Nothing, StgPrim op (lookupArgs env as) lvs)
228 satExpr env (StgSCC ty l e)
229 = satExpr env e `thenUs` \ (_, e2) ->
230 returnUs (Nothing, StgSCC ty l e2)
232 {- OMITTED: Let-no-escapery should come *after* saturation
234 satExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
235 = satBinding binds `thenUs` \ (binds2, c) ->
236 satExpr body `thenUs` \ (_, body2, c2) ->
237 returnUs (Nothing, StgLetNoEscape lvs_whole lvs_rhss binds2 body2, c + c2)
240 satExpr env (StgLet binds body)
241 = satBinding False{-not top-level-} env binds `thenUs` \ (env2, binds2) ->
242 satExpr env2 body `thenUs` \ (_, body2) ->
243 returnUs (Nothing, StgLet binds2 body2)
245 satExpr env (StgCase expr lve lva uniq alts)
246 = satExpr env expr `thenUs` \ (_, expr2) ->
247 sat_alts alts `thenUs` \ alts2 ->
248 returnUs (Nothing, StgCase expr2 lve lva uniq alts2)
250 sat_alts (StgAlgAlts ty alts def)
251 = mapUs sat_alg_alt alts `thenUs` \ alts2 ->
252 sat_deflt def `thenUs` \ def2 ->
253 returnUs (StgAlgAlts ty alts2 def2)
255 sat_alg_alt (id, bs, use_mask, e)
256 = satExpr env e `thenUs` \ (_, e2) ->
257 returnUs (id, bs, use_mask, e2)
259 sat_alts (StgPrimAlts ty alts def)
260 = mapUs sat_prim_alt alts `thenUs` \ alts2 ->
261 sat_deflt def `thenUs` \ def2 ->
262 returnUs (StgPrimAlts ty alts2 def2)
265 = satExpr env e `thenUs` \ (_, e2) ->
268 sat_deflt StgNoDefault
269 = returnUs StgNoDefault
271 sat_deflt (StgBindDefault b u expr)
272 = satExpr env expr `thenUs` \ (_,expr2) ->
273 returnUs (StgBindDefault b u expr2)
276 %************************************************************************
278 \subsection{Utility functions}
280 %************************************************************************
283 saturate :: StgExpr -> [StgArg] -> StgExpr
285 saturate (StgApp f as lvs) ids = StgApp f (as ++ ids) lvs
286 saturate other _ = panic "SatStgRhs: saturate"
290 lookupArgs :: SatEnv -> [StgArg] -> [StgArg]
291 lookupArgs env args = map do args
293 do (StgVarArg v) = StgVarArg (lookupVar env v)
294 do a@(StgLitArg lit) = a
296 lookupVar :: SatEnv -> Id -> Id
297 lookupVar env v = case lookupIdEnv env v of
301 newName :: Type -> UniqSM Id
303 = getUnique `thenUs` \ uniq ->
304 returnUs (mkSysLocal SLIT("sat") uniq ut mkUnknownSrcLoc)