2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
4 \section[WorkWrap]{Worker/wrapper-generating back-end of strictness analyser}
7 module WorkWrap ( workersAndWrappers, getWorkerIdAndCons ) where
9 #include "HsVersions.h"
12 import CoreUnfold ( Unfolding, certainlySmallEnoughToInline, calcUnfoldingGuidance )
13 import CmdLineOpts ( opt_UnfoldingCreationThreshold )
15 import CoreUtils ( coreExprType )
16 import MkId ( mkWorkerId )
17 import Id ( getInlinePragma, getIdStrictness,
18 addIdStrictness, addInlinePragma,
19 IdSet, emptyIdSet, addOneToIdSet,
22 import IdInfo ( noIdInfo, mkStrictnessInfo, setStrictnessInfo, StrictnessInfo(..) )
24 import UniqSupply ( returnUs, thenUs, mapUs, getUnique, UniqSM )
29 We take Core bindings whose binders have their strictness attached (by
30 the front-end of the strictness analyser), and we return some
31 ``plain'' bindings which have been worker/wrapper-ified, meaning:
34 Functions have been split into workers and wrappers where appropriate;
36 Binders' @IdInfos@ have been updated to reflect the existence
37 of these workers/wrappers (this is where we get STRICTNESS pragma
38 info for exported values).
42 workersAndWrappers :: [CoreBinding] -> UniqSM [CoreBinding]
44 workersAndWrappers top_binds
45 = mapUs (wwBind True{-top-level-}) top_binds `thenUs` \ top_binds2 ->
47 top_binds3 = map make_top_binding top_binds2
49 returnUs (concat top_binds3)
51 make_top_binding :: WwBinding -> [CoreBinding]
53 make_top_binding (WwLet binds) = binds
56 %************************************************************************
58 \subsection[wwBind-wwExpr]{@wwBind@ and @wwExpr@}
60 %************************************************************************
62 @wwBind@ works on a binding, trying each \tr{(binder, expr)} pair in
63 turn. Non-recursive case first, then recursive...
66 wwBind :: Bool -- True <=> top-level binding
68 -> UniqSM WwBinding -- returns a WwBinding intermediate form;
69 -- the caller will convert to Expr/Binding,
72 wwBind top_level (NonRec binder rhs)
73 = wwExpr rhs `thenUs` \ new_rhs ->
74 tryWW binder new_rhs `thenUs` \ new_pairs ->
75 returnUs (WwLet [NonRec b e | (b,e) <- new_pairs])
76 -- Generated bindings must be non-recursive
77 -- because the original binding was.
79 ------------------------------
81 wwBind top_level (Rec pairs)
82 = mapUs do_one pairs `thenUs` \ new_pairs ->
83 returnUs (WwLet [Rec (concat new_pairs)])
85 do_one (binder, rhs) = wwExpr rhs `thenUs` \ new_rhs ->
89 @wwExpr@ basically just walks the tree, looking for appropriate
90 annotations that can be used. Remember it is @wwBind@ that does the
91 matching by looking for strict arguments of the correct type.
92 @wwExpr@ is a version that just returns the ``Plain'' Tree.
96 wwExpr :: CoreExpr -> UniqSM CoreExpr
98 wwExpr e@(Var _) = returnUs e
99 wwExpr e@(Lit _) = returnUs e
100 wwExpr e@(Con _ _) = returnUs e
101 wwExpr e@(Prim _ _) = returnUs e
103 wwExpr (Lam binder expr)
104 = wwExpr expr `thenUs` \ new_expr ->
105 returnUs (Lam binder new_expr)
108 = wwExpr f `thenUs` \ new_f ->
109 returnUs (App new_f a)
111 wwExpr (Note note expr)
112 = wwExpr expr `thenUs` \ new_expr ->
113 returnUs (Note note new_expr)
115 wwExpr (Let bind expr)
116 = wwBind False{-not top-level-} bind `thenUs` \ intermediate_bind ->
117 wwExpr expr `thenUs` \ new_expr ->
118 returnUs (mash_ww_bind intermediate_bind new_expr)
120 mash_ww_bind (WwLet binds) body = mkCoLetsNoUnboxed binds body
121 mash_ww_bind (WwCase case_fn) body = case_fn body
123 wwExpr (Case expr alts)
124 = wwExpr expr `thenUs` \ new_expr ->
125 ww_alts alts `thenUs` \ new_alts ->
126 returnUs (Case new_expr new_alts)
128 ww_alts (AlgAlts alts deflt)
129 = mapUs ww_alg_alt alts `thenUs` \ new_alts ->
130 ww_deflt deflt `thenUs` \ new_deflt ->
131 returnUs (AlgAlts new_alts new_deflt)
133 ww_alts (PrimAlts alts deflt)
134 = mapUs ww_prim_alt alts `thenUs` \ new_alts ->
135 ww_deflt deflt `thenUs` \ new_deflt ->
136 returnUs (PrimAlts new_alts new_deflt)
138 ww_alg_alt (con, binders, rhs)
139 = wwExpr rhs `thenUs` \ new_rhs ->
140 returnUs (con, binders, new_rhs)
142 ww_prim_alt (lit, rhs)
143 = wwExpr rhs `thenUs` \ new_rhs ->
144 returnUs (lit, new_rhs)
149 ww_deflt (BindDefault binder rhs)
150 = wwExpr rhs `thenUs` \ new_rhs ->
151 returnUs (BindDefault binder new_rhs)
154 %************************************************************************
156 \subsection[tryWW]{@tryWW@: attempt a worker/wrapper pair}
158 %************************************************************************
160 @tryWW@ just accumulates arguments, converts strictness info from the
161 front-end into the proper form, then calls @mkWwBodies@ to do
164 We have to BE CAREFUL that we don't worker-wrapperize an Id that has
165 already been w-w'd! (You can end up with several liked-named Ids
166 bouncing around at the same time---absolute mischief.) So the
167 criterion we use is: if an Id already has an unfolding (for whatever
168 reason), then we don't w-w it.
170 The only reason this is monadised is for the unique supply.
173 tryWW :: Id -- The fn binder
174 -> CoreExpr -- The bound rhs; its innards
176 -> UniqSM [(Id, CoreExpr)] -- either *one* or *two* pairs;
177 -- if one, then no worker (only
178 -- the orig "wrapper" lives on);
179 -- if two, then a worker and a
182 | (certainlySmallEnoughToInline fn_id $
183 calcUnfoldingGuidance opt_UnfoldingCreationThreshold rhs
185 -- No point in worker/wrappering something that is going to be
186 -- INLINEd wholesale anyway. If the strictness analyser is run
187 -- twice, this test also prevents wrappers (which are INLINEd)
188 -- from being re-done.
190 || not has_strictness_info
191 || not (worthSplitting revised_wrap_args_info)
192 = returnUs [ (fn_id, rhs) ]
194 | otherwise -- Do w/w split
196 (tyvars, wrap_args, body) = collectBinders rhs
198 mkWwBodies tyvars wrap_args
200 revised_wrap_args_info `thenUs` \ (wrap_fn, work_fn, work_demands) ->
201 getUnique `thenUs` \ work_uniq ->
203 work_rhs = work_fn body
204 work_id = mkWorkerId work_uniq fn_id (coreExprType work_rhs) work_info
205 work_info = mkStrictnessInfo work_demands False `setStrictnessInfo` noIdInfo
207 wrap_rhs = wrap_fn work_id
208 wrap_id = addInlinePragma (fn_id `addIdStrictness`
209 mkStrictnessInfo revised_wrap_args_info True)
210 -- Add info to the wrapper:
211 -- (a) we want to inline it everywhere
212 -- (b) we want to pin on its revised stricteness info
213 -- (c) we pin on its worker id and the list of constructors mentioned in the wrapper
215 returnUs ([(work_id, work_rhs), (wrap_id, wrap_rhs)])
216 -- Worker first, because wrapper mentions it
218 strictness_info = getIdStrictness fn_id
219 has_strictness_info = case strictness_info of
220 StrictnessInfo _ _ -> True
223 wrap_args_info = case strictness_info of
224 StrictnessInfo args_info _ -> args_info
225 revised_wrap_args_info = setUnpackStrategy wrap_args_info
227 -- This rather (nay! extremely!) crude function looks at a wrapper function, and
228 -- snaffles out (a) the worker Id and (b) constructors needed to
230 -- These are needed when we write an interface file.
231 getWorkerIdAndCons wrap_id wrapper_fn
234 go (Lam _ body) = go body
235 go (Case _ (AlgAlts [(con,_,rhs)] _)) = let (wrap_id, cons) = go rhs
236 in (wrap_id, cons `addOneToIdSet` con)
238 -- Coercions don't mention the construtor now,
239 -- so I don't think we need this
240 go (Let (NonRec _ (Coerce (CoerceOut con) _ _)) body)
241 = let (wrap_id, cons) = go body
242 in (wrap_id, cons `addOneToIdSet` con)
244 go other = (get_work_id other, emptyIdSet)
246 get_work_id (App fn _) = get_work_id fn
247 get_work_id (Var work_id) = work_id
248 get_work_id other = pprPanic "getWorkerIdAndCons" (ppr wrap_id)