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 IdInfo ( arityMaybe )
73 import SrcLoc ( mkUnknownSrcLoc )
74 import Type ( splitSigmaTy, splitForAllTy, splitFunTyExpandingDicts )
75 import UniqSupply ( returnUs, thenUs, mapUs, getUnique, SYN_IE(UniqSM) )
76 import Util ( panic, assertPanic )
80 type ExprArityInfo = Maybe Int -- Just n => This expression has a guaranteed
82 -- Nothing => Don't know how many args it needs
84 type Id_w_Arity = Id -- An Id with correct arity info pinned on it
85 type SatEnv = IdEnv Id_w_Arity -- Binds only local, let(rec)-bound things
90 \item adds extra args where necessary;
91 \item pins the correct arity on everything.
94 %************************************************************************
96 \subsection{Top-level list of bindings (a ``program'')}
98 %************************************************************************
101 satStgRhs :: [StgBinding] -> UniqSM [StgBinding]
103 satStgRhs p = satProgram nullIdEnv p
105 satProgram :: SatEnv -> [StgBinding] -> UniqSM [StgBinding]
106 satProgram env [] = returnUs []
108 satProgram env (bind:binds)
109 = satBinding True{-toplevel-} env bind `thenUs` \ (env2, bind2) ->
110 satProgram env2 binds `thenUs` \ binds2 ->
111 returnUs (bind2 : binds2)
114 %************************************************************************
116 \subsection{Bindings}
118 %************************************************************************
121 satBinding :: Bool -- True <=> top-level
124 -> UniqSM (SatEnv, StgBinding)
126 satBinding top env (StgNonRec b rhs)
127 = satRhs top env (b, rhs) `thenUs` \ (b2, rhs2) ->
129 env2 = addOneToIdEnv env b b2
131 returnUs (env2, StgNonRec b2 rhs2)
133 satBinding top env (StgRec pairs)
134 = -- Do it once to get the arities right...
135 mapUs (satRhs top env) pairs `thenUs` \ pairs2 ->
137 env2 = growIdEnvList env (map fst pairs `zip` map fst pairs2)
139 -- Do it again to *use* those arities:
140 mapUs (satRhs top env2) pairs `thenUs` \ pairs3 ->
142 returnUs (env2, StgRec pairs3)
144 satRhs :: Bool -> SatEnv -> (Id, StgRhs) -> UniqSM (Id_w_Arity, StgRhs)
146 satRhs top env (b, StgRhsCon cc con args) -- Nothing much to do here
148 b2 = b `addIdArity` 0 -- bound to a saturated constructor; hence zero.
150 returnUs (b2, StgRhsCon cc con (lookupArgs env args))
152 satRhs top env (b, StgRhsClosure cc bi fv u args body)
153 = satExpr env body `thenUs` \ (arity_info, body2) ->
155 num_args = length args
159 returnUs (num_args, StgRhsClosure cc bi fv u args body2)
162 ASSERT(needed_args >= 1)
164 let -- the arity we're aiming for is: what we already have ("args")
165 -- plus the ones requested in "arity_info"
166 new_arity = num_args + needed_args
168 -- get type info for this function:
169 (_, rho_ty) = splitForAllTy (idType b)
170 (all_arg_tys, _) = splitFunTyExpandingDicts rho_ty
172 -- now, we already have "args"; we drop that many types
173 args_we_dont_have_tys = drop num_args all_arg_tys
175 -- finally, we take some of those (up to maybe all of them),
176 -- depending on how many "needed_args"
177 args_to_add_tys = take needed_args args_we_dont_have_tys
179 -- make up names for them
180 mapUs newName args_to_add_tys `thenUs` \ nns ->
182 -- and do the business
184 body3 = saturate body2 (map StgVarArg nns)
186 new_cc -- if we're adding args, we'd better not
187 -- keep calling something a CAF! (what about DICTs? ToDo: WDP 95/02)
188 = if not (isCafCC cc)
190 else if top then subsumedCosts else useCurrentCostCentre
192 returnUs (new_arity, StgRhsClosure new_cc bi fv ReEntrant (args++nns) body3)
194 `thenUs` \ (arity, rhs2) ->
196 b2 = b `addIdArity` arity
201 %************************************************************************
203 \subsection{Expressions}
205 %************************************************************************
208 satExpr :: SatEnv -> StgExpr -> UniqSM (ExprArityInfo, StgExpr)
210 satExpr env app@(StgApp (StgLitArg lit) [] lvs) = returnUs (Nothing, app)
212 satExpr env app@(StgApp (StgVarArg f) as lvs)
213 = returnUs (arity_to_return, StgApp (StgVarArg f2) as2 lvs)
215 as2 = lookupArgs env as
217 arity_to_return = case arityMaybe (getIdArity f2) of
220 Just f_arity -> if remaining_arity > 0
221 then Just remaining_arity
224 remaining_arity = f_arity - length as
226 satExpr env app@(StgCon con as lvs)
227 = returnUs (Nothing, StgCon con (lookupArgs env as) lvs)
229 satExpr env app@(StgPrim op as lvs)
230 = returnUs (Nothing, StgPrim op (lookupArgs env as) lvs)
232 satExpr env (StgSCC ty l e)
233 = satExpr env e `thenUs` \ (_, e2) ->
234 returnUs (Nothing, StgSCC ty l e2)
236 {- OMITTED: Let-no-escapery should come *after* saturation
238 satExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
239 = satBinding binds `thenUs` \ (binds2, c) ->
240 satExpr body `thenUs` \ (_, body2, c2) ->
241 returnUs (Nothing, StgLetNoEscape lvs_whole lvs_rhss binds2 body2, c + c2)
244 satExpr env (StgLet binds body)
245 = satBinding False{-not top-level-} env binds `thenUs` \ (env2, binds2) ->
246 satExpr env2 body `thenUs` \ (_, body2) ->
247 returnUs (Nothing, StgLet binds2 body2)
249 satExpr env (StgCase expr lve lva uniq alts)
250 = satExpr env expr `thenUs` \ (_, expr2) ->
251 sat_alts alts `thenUs` \ alts2 ->
252 returnUs (Nothing, StgCase expr2 lve lva uniq alts2)
254 sat_alts (StgAlgAlts ty alts def)
255 = mapUs sat_alg_alt alts `thenUs` \ alts2 ->
256 sat_deflt def `thenUs` \ def2 ->
257 returnUs (StgAlgAlts ty alts2 def2)
259 sat_alg_alt (id, bs, use_mask, e)
260 = satExpr env e `thenUs` \ (_, e2) ->
261 returnUs (id, bs, use_mask, e2)
263 sat_alts (StgPrimAlts ty alts def)
264 = mapUs sat_prim_alt alts `thenUs` \ alts2 ->
265 sat_deflt def `thenUs` \ def2 ->
266 returnUs (StgPrimAlts ty alts2 def2)
269 = satExpr env e `thenUs` \ (_, e2) ->
272 sat_deflt StgNoDefault
273 = returnUs StgNoDefault
275 sat_deflt (StgBindDefault b u expr)
276 = satExpr env expr `thenUs` \ (_,expr2) ->
277 returnUs (StgBindDefault b u expr2)
280 %************************************************************************
282 \subsection{Utility functions}
284 %************************************************************************
287 saturate :: StgExpr -> [StgArg] -> StgExpr
289 saturate (StgApp f as lvs) ids = StgApp f (as ++ ids) lvs
290 saturate other _ = panic "SatStgRhs: saturate"
294 lookupArgs :: SatEnv -> [StgArg] -> [StgArg]
295 lookupArgs env args = map doo args
297 doo (StgVarArg v) = StgVarArg (lookupVar env v)
298 doo a@(StgLitArg lit) = a
300 lookupVar :: SatEnv -> Id -> Id
301 lookupVar env v = case lookupIdEnv env v of
305 newName :: Type -> UniqSM Id
307 = getUnique `thenUs` \ uniq ->
308 returnUs (mkSysLocal SLIT("sat") uniq ut mkUnknownSrcLoc)