2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[SatStgRhs]{Saturates RHSs when they are partial applications}
6 96/03: This is actually an essential module, as it sets arity info
7 for the code generator.
10 Subject: arg satis check
11 Date: Wed, 29 Apr 92 13:33:58 +0100
12 From: Simon L Peyton Jones <simonpj>
16 Another transformation to consider. We'd like to avoid
17 argument-satisfaction checks wherever possible. So, whenever we have an
18 STG binding application
20 f = vs \ xs -> g e1 ... en
22 where xs has one or more elements
24 where g is a known function with arity m+n,
28 f = vs \ xs++{x1...xm} -> g e1 ... en x1 .. xm
30 Now g has enough args. One arg-satisfaction check disappears;
31 the one for the closure incorporates the one for g.
33 You might like to consider variants, applying the transformation more
34 widely. I concluded that this was the only instance which made
35 sense, but I could be wrong.
40 The algorithm proceeds as follows:
43 Gather the arity information of the functions defined in this module
44 (as @getIdArity@ only knows about the arity of @ImportedIds@).
47 for every definition of the form
49 v = /\ts -> \vs -> f args
51 we try to match the arity of \tr{f} with the number of arguments.
52 If they do not match we insert extra lambdas to make that application
56 This is done for local definitions as well.
59 #include "HsVersions.h"
61 module SatStgRhs ( satStgRhs ) where
67 import CostCentre ( isCafCC, subsumedCosts, useCurrentCostCentre )
68 import Id ( idType, getIdArity, addIdArity, mkSysLocal,
69 nullIdEnv, addOneToIdEnv, growIdEnvList,
70 lookupIdEnv, SYN_IE(IdEnv)
72 import SrcLoc ( noSrcLoc )
73 import Type ( splitSigmaTy, splitForAllTy, splitFunTyExpandingDicts )
74 import UniqSupply ( returnUs, thenUs, mapUs, getUnique, SYN_IE(UniqSM) )
75 import Util ( panic, assertPanic )
79 type ExprArityInfo = Maybe Int -- Just n => This expression has a guaranteed
81 -- Nothing => Don't know how many args it needs
83 type Id_w_Arity = Id -- An Id with correct arity info pinned on it
84 type SatEnv = IdEnv Id_w_Arity -- Binds only local, let(rec)-bound things
89 \item adds extra args where necessary;
90 \item pins the correct arity on everything.
93 %************************************************************************
95 \subsection{Top-level list of bindings (a ``program'')}
97 %************************************************************************
100 satStgRhs :: [StgBinding] -> UniqSM [StgBinding]
101 satStgRhs = panic "satStgRhs"
103 {- NUKED FOR NOW SLPJ Dec 96
106 satStgRhs p = satProgram nullIdEnv p
108 satProgram :: SatEnv -> [StgBinding] -> UniqSM [StgBinding]
109 satProgram env [] = returnUs []
111 satProgram env (bind:binds)
112 = satBinding True{-toplevel-} env bind `thenUs` \ (env2, bind2) ->
113 satProgram env2 binds `thenUs` \ binds2 ->
114 returnUs (bind2 : binds2)
117 %************************************************************************
119 \subsection{Bindings}
121 %************************************************************************
124 satBinding :: Bool -- True <=> top-level
127 -> UniqSM (SatEnv, StgBinding)
129 satBinding top env (StgNonRec b rhs)
130 = satRhs top env (b, rhs) `thenUs` \ (b2, rhs2) ->
132 env2 = addOneToIdEnv env b b2
134 returnUs (env2, StgNonRec b2 rhs2)
136 satBinding top env (StgRec pairs)
137 = -- Do it once to get the arities right...
138 mapUs (satRhs top env) pairs `thenUs` \ pairs2 ->
140 env2 = growIdEnvList env (map fst pairs `zip` map fst pairs2)
142 -- Do it again to *use* those arities:
143 mapUs (satRhs top env2) pairs `thenUs` \ pairs3 ->
145 returnUs (env2, StgRec pairs3)
147 satRhs :: Bool -> SatEnv -> (Id, StgRhs) -> UniqSM (Id_w_Arity, StgRhs)
149 satRhs top env (b, StgRhsCon cc con args) -- Nothing much to do here
151 b2 = b `addIdArity` 0 -- bound to a saturated constructor; hence zero.
153 returnUs (b2, StgRhsCon cc con (lookupArgs env args))
155 satRhs top env (b, StgRhsClosure cc bi fv u args body)
156 = satExpr env body `thenUs` \ (arity_info, body2) ->
158 num_args = length args
162 returnUs (num_args, StgRhsClosure cc bi fv u args body2)
165 ASSERT(needed_args >= 1)
167 let -- the arity we're aiming for is: what we already have ("args")
168 -- plus the ones requested in "arity_info"
169 new_arity = num_args + needed_args
171 -- get type info for this function:
172 (_, rho_ty) = splitForAllTy (idType b)
173 (all_arg_tys, _) = splitFunTyExpandingDicts rho_ty
175 -- now, we already have "args"; we drop that many types
176 args_we_dont_have_tys = drop num_args all_arg_tys
178 -- finally, we take some of those (up to maybe all of them),
179 -- depending on how many "needed_args"
180 args_to_add_tys = take needed_args args_we_dont_have_tys
182 -- make up names for them
183 mapUs newName args_to_add_tys `thenUs` \ nns ->
185 -- and do the business
187 body3 = saturate body2 (map StgVarArg nns)
189 new_cc -- if we're adding args, we'd better not
190 -- keep calling something a CAF! (what about DICTs? ToDo: WDP 95/02)
191 = if not (isCafCC cc)
193 else if top then subsumedCosts else useCurrentCostCentre
195 returnUs (new_arity, StgRhsClosure new_cc bi fv ReEntrant (args++nns) body3)
197 `thenUs` \ (arity, rhs2) ->
199 b2 = b `addIdArity` arity
204 %************************************************************************
206 \subsection{Expressions}
208 %************************************************************************
211 satExpr :: SatEnv -> StgExpr -> UniqSM (ExprArityInfo, StgExpr)
213 satExpr env app@(StgApp (StgLitArg lit) [] lvs) = returnUs (Nothing, app)
215 satExpr env app@(StgApp (StgVarArg f) as lvs)
216 = returnUs (arity_to_return, StgApp (StgVarArg f2) as2 lvs)
218 as2 = lookupArgs env as
220 arity_to_return = case arityMaybe (getIdArity f2) of
223 Just f_arity -> if remaining_arity > 0
224 then Just remaining_arity
227 remaining_arity = f_arity - length as
229 satExpr env app@(StgCon con as lvs)
230 = returnUs (Nothing, StgCon con (lookupArgs env as) lvs)
232 satExpr env app@(StgPrim op as lvs)
233 = returnUs (Nothing, StgPrim op (lookupArgs env as) lvs)
235 satExpr env (StgSCC ty l e)
236 = satExpr env e `thenUs` \ (_, e2) ->
237 returnUs (Nothing, StgSCC ty l e2)
239 {- OMITTED: Let-no-escapery should come *after* saturation
241 satExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
242 = satBinding binds `thenUs` \ (binds2, c) ->
243 satExpr body `thenUs` \ (_, body2, c2) ->
244 returnUs (Nothing, StgLetNoEscape lvs_whole lvs_rhss binds2 body2, c + c2)
247 satExpr env (StgLet binds body)
248 = satBinding False{-not top-level-} env binds `thenUs` \ (env2, binds2) ->
249 satExpr env2 body `thenUs` \ (_, body2) ->
250 returnUs (Nothing, StgLet binds2 body2)
252 satExpr env (StgCase expr lve lva uniq alts)
253 = satExpr env expr `thenUs` \ (_, expr2) ->
254 sat_alts alts `thenUs` \ alts2 ->
255 returnUs (Nothing, StgCase expr2 lve lva uniq alts2)
257 sat_alts (StgAlgAlts ty alts def)
258 = mapUs sat_alg_alt alts `thenUs` \ alts2 ->
259 sat_deflt def `thenUs` \ def2 ->
260 returnUs (StgAlgAlts ty alts2 def2)
262 sat_alg_alt (id, bs, use_mask, e)
263 = satExpr env e `thenUs` \ (_, e2) ->
264 returnUs (id, bs, use_mask, e2)
266 sat_alts (StgPrimAlts ty alts def)
267 = mapUs sat_prim_alt alts `thenUs` \ alts2 ->
268 sat_deflt def `thenUs` \ def2 ->
269 returnUs (StgPrimAlts ty alts2 def2)
272 = satExpr env e `thenUs` \ (_, e2) ->
275 sat_deflt StgNoDefault
276 = returnUs StgNoDefault
278 sat_deflt (StgBindDefault b u expr)
279 = satExpr env expr `thenUs` \ (_,expr2) ->
280 returnUs (StgBindDefault b u expr2)
283 %************************************************************************
285 \subsection{Utility functions}
287 %************************************************************************
290 saturate :: StgExpr -> [StgArg] -> StgExpr
292 saturate (StgApp f as lvs) ids = StgApp f (as ++ ids) lvs
293 saturate other _ = panic "SatStgRhs: saturate"
297 lookupArgs :: SatEnv -> [StgArg] -> [StgArg]
298 lookupArgs env args = map doo args
300 doo (StgVarArg v) = StgVarArg (lookupVar env v)
301 doo a@(StgLitArg lit) = a
303 lookupVar :: SatEnv -> Id -> Id
304 lookupVar env v = case lookupIdEnv env v of
308 newName :: Type -> UniqSM Id
310 = getUnique `thenUs` \ uniq ->
311 returnUs (mkSysLocal SLIT("sat") uniq ut noSrcLoc)