2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
4 \section[WorkWrap]{Worker/wrapper-generating back-end of strictness analyser}
7 #include "HsVersions.h"
9 module WorkWrap ( workersAndWrappers ) where
14 import CoreUnfold ( Unfolding(..), UnfoldingGuidance(..) )
15 IMPORT_DELOOPER(IdLoop) -- ToDo:rm when iWantToBeINLINEd goes
17 import CoreUtils ( coreExprType )
18 import Id ( idWantsToBeINLINEd, getIdStrictness, mkWorkerId,
19 getIdInfo, replaceIdInfo, GenId
21 import IdInfo ( noIdInfo, addInfo_UF, indicatesWorker,
22 mkStrictnessInfo, StrictnessInfo(..)
25 import UniqSupply ( returnUs, thenUs, mapUs, getUnique, SYN_IE(UniqSM) )
28 iWantToBeINLINEd :: UnfoldingGuidance -> Unfolding
29 iWantToBeINLINEd x = NoUnfolding --ToDo:panic "WorkWrap.iWantToBeINLINEd (ToDo)"
32 We take Core bindings whose binders have their strictness attached (by
33 the front-end of the strictness analyser), and we return some
34 ``plain'' bindings which have been worker/wrapper-ified, meaning:
37 Functions have been split into workers and wrappers where appropriate;
39 Binders' @IdInfos@ have been updated to reflect the existence
40 of these workers/wrappers (this is where we get STRICTNESS pragma
41 info for exported values).
45 workersAndWrappers :: [CoreBinding] -> UniqSM [CoreBinding]
47 workersAndWrappers top_binds
48 = mapUs (wwBind True{-top-level-}) top_binds `thenUs` \ top_binds2 ->
50 top_binds3 = map make_top_binding top_binds2
52 returnUs (concat top_binds3)
54 make_top_binding :: WwBinding -> [CoreBinding]
56 make_top_binding (WwLet binds) = binds
59 %************************************************************************
61 \subsection[wwBind-wwExpr]{@wwBind@ and @wwExpr@}
63 %************************************************************************
65 @wwBind@ works on a binding, trying each \tr{(binder, expr)} pair in
66 turn. Non-recursive case first, then recursive...
69 wwBind :: Bool -- True <=> top-level binding
71 -> UniqSM WwBinding -- returns a WwBinding intermediate form;
72 -- the caller will convert to Expr/Binding,
75 wwBind top_level (NonRec binder rhs)
76 = wwExpr rhs `thenUs` \ new_rhs ->
77 tryWW binder new_rhs `thenUs` \ new_pairs ->
78 returnUs (WwLet [NonRec b e | (b,e) <- new_pairs])
79 -- Generated bindings must be non-recursive
80 -- because the original binding was.
82 ------------------------------
84 wwBind top_level (Rec pairs)
85 = mapUs do_one pairs `thenUs` \ new_pairs ->
86 returnUs (WwLet [Rec (concat new_pairs)])
88 do_one (binder, rhs) = wwExpr rhs `thenUs` \ new_rhs ->
92 @wwExpr@ basically just walks the tree, looking for appropriate
93 annotations that can be used. Remember it is @wwBind@ that does the
94 matching by looking for strict arguments of the correct type.
95 @wwExpr@ is a version that just returns the ``Plain'' Tree.
99 wwExpr :: CoreExpr -> UniqSM CoreExpr
101 wwExpr e@(Var _) = returnUs e
102 wwExpr e@(Lit _) = returnUs e
103 wwExpr e@(Con _ _) = returnUs e
104 wwExpr e@(Prim _ _) = returnUs e
106 wwExpr (Lam binder expr)
107 = wwExpr expr `thenUs` \ new_expr ->
108 returnUs (Lam binder new_expr)
111 = wwExpr f `thenUs` \ new_f ->
112 returnUs (App new_f a)
115 = wwExpr expr `thenUs` \ new_expr ->
116 returnUs (SCC cc new_expr)
118 wwExpr (Coerce c ty expr)
119 = wwExpr expr `thenUs` \ new_expr ->
120 returnUs (Coerce c ty new_expr)
122 wwExpr (Let bind expr)
123 = wwBind False{-not top-level-} bind `thenUs` \ intermediate_bind ->
124 wwExpr expr `thenUs` \ new_expr ->
125 returnUs (mash_ww_bind intermediate_bind new_expr)
127 mash_ww_bind (WwLet binds) body = mkCoLetsNoUnboxed binds body
128 mash_ww_bind (WwCase case_fn) body = case_fn body
130 wwExpr (Case expr alts)
131 = wwExpr expr `thenUs` \ new_expr ->
132 ww_alts alts `thenUs` \ new_alts ->
133 returnUs (Case new_expr new_alts)
135 ww_alts (AlgAlts alts deflt)
136 = mapUs ww_alg_alt alts `thenUs` \ new_alts ->
137 ww_deflt deflt `thenUs` \ new_deflt ->
138 returnUs (AlgAlts new_alts new_deflt)
140 ww_alts (PrimAlts alts deflt)
141 = mapUs ww_prim_alt alts `thenUs` \ new_alts ->
142 ww_deflt deflt `thenUs` \ new_deflt ->
143 returnUs (PrimAlts new_alts new_deflt)
145 ww_alg_alt (con, binders, rhs)
146 = wwExpr rhs `thenUs` \ new_rhs ->
147 returnUs (con, binders, new_rhs)
149 ww_prim_alt (lit, rhs)
150 = wwExpr rhs `thenUs` \ new_rhs ->
151 returnUs (lit, new_rhs)
156 ww_deflt (BindDefault binder rhs)
157 = wwExpr rhs `thenUs` \ new_rhs ->
158 returnUs (BindDefault binder new_rhs)
161 %************************************************************************
163 \subsection[tryWW]{@tryWW@: attempt a worker/wrapper pair}
165 %************************************************************************
167 @tryWW@ just accumulates arguments, converts strictness info from the
168 front-end into the proper form, then calls @mkWwBodies@ to do
171 We have to BE CAREFUL that we don't worker-wrapperize an Id that has
172 already been w-w'd! (You can end up with several liked-named Ids
173 bouncing around at the same time---absolute mischief.) So the
174 criterion we use is: if an Id already has an unfolding (for whatever
175 reason), then we don't w-w it.
177 The only reason this is monadised is for the unique supply.
180 tryWW :: Id -- the fn binder
181 -> CoreExpr -- the bound rhs; its innards
183 -> UniqSM [(Id, CoreExpr)] -- either *one* or *two* pairs;
184 -- if one, then no worker (only
185 -- the orig "wrapper" lives on);
186 -- if two, then a worker and a
189 | idWantsToBeINLINEd fn_id
190 -- No point in worker/wrappering something that is going to be
191 -- INLINEd wholesale anyway. If the strictness analyser is run
192 -- twice, this test also prevents wrappers (which are INLINEd)
193 -- from being re-done.
197 = case (getIdStrictness fn_id) of
199 NoStrictnessInfo -> do_nothing
200 BottomGuaranteed -> do_nothing
201 StrictnessInfo [] _ -> do_nothing -- V weird (but possible?)
203 StrictnessInfo args_info _ ->
204 if not (indicatesWorker args_info) then
208 -- OK, it looks as if a worker is worth a try
210 (uvars, tyvars, args, body) = collectBinders rhs
211 body_ty = coreExprType body
213 mkWwBodies body_ty tyvars args args_info `thenUs` \ result ->
216 Nothing -> -- Very peculiar. This can only happen if we hit an
217 -- abstract type, which we shouldn't have since we've
218 -- constructed the args_info in this module!
220 -- False. We might hit the all-args-absent-and-the-
221 -- body-is-unboxed case. A Nothing is legit. (WDP 94/10)
224 Just (wrapper_w_hole, worker_w_hole, worker_strictness, worker_ty_w_hole) ->
226 -- Terrific! It worked!
227 getUnique `thenUs` \ worker_uniq ->
229 worker_ty = worker_ty_w_hole body_ty
231 worker_id = mkWorkerId worker_uniq fn_id worker_ty
232 (noIdInfo `addInfo` worker_strictness)
234 wrapper_rhs = wrapper_w_hole worker_id
235 worker_rhs = worker_w_hole body
237 revised_strictness_info
238 = -- We know the basic strictness info already, but
239 -- we need to slam in the exact identity of the
241 mkStrictnessInfo args_info (Just worker_id)
243 wrapper_id = fn_id `replaceIdInfo`
244 (getIdInfo fn_id `addInfo`
245 revised_strictness_info `addInfo_UF`
246 iWantToBeINLINEd UnfoldAlways)
247 -- NB! the "iWantToBeINLINEd" part adds an INLINE pragma to
248 -- the wrapper, which is of course what we want.
250 returnUs [ (worker_id, worker_rhs), -- worker comes first
251 (wrapper_id, wrapper_rhs) ] -- because wrapper mentions it
253 do_nothing = returnUs [ (fn_id, rhs) ]