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, getWorkerIdAndCons ) where
14 import CoreUnfold ( Unfolding, certainlySmallEnoughToInline, calcUnfoldingGuidance )
15 import CmdLineOpts ( opt_UnfoldingCreationThreshold )
17 import CoreUtils ( coreExprType )
18 import Id ( getInlinePragma, getIdStrictness, mkWorkerId,
19 addIdStrictness, addInlinePragma,
20 SYN_IE(IdSet), emptyIdSet, addOneToIdSet,
23 import IdInfo ( noIdInfo, addUnfoldInfo,
24 mkStrictnessInfo, addStrictnessInfo, StrictnessInfo(..)
27 import UniqSupply ( returnUs, thenUs, mapUs, getUnique, SYN_IE(UniqSM) )
30 import Outputable ( ppr, PprStyle(..) )
31 import Util ( pprPanic )
34 We take Core bindings whose binders have their strictness attached (by
35 the front-end of the strictness analyser), and we return some
36 ``plain'' bindings which have been worker/wrapper-ified, meaning:
39 Functions have been split into workers and wrappers where appropriate;
41 Binders' @IdInfos@ have been updated to reflect the existence
42 of these workers/wrappers (this is where we get STRICTNESS pragma
43 info for exported values).
47 workersAndWrappers :: [CoreBinding] -> UniqSM [CoreBinding]
49 workersAndWrappers top_binds
50 = mapUs (wwBind True{-top-level-}) top_binds `thenUs` \ top_binds2 ->
52 top_binds3 = map make_top_binding top_binds2
54 returnUs (concat top_binds3)
56 make_top_binding :: WwBinding -> [CoreBinding]
58 make_top_binding (WwLet binds) = binds
61 %************************************************************************
63 \subsection[wwBind-wwExpr]{@wwBind@ and @wwExpr@}
65 %************************************************************************
67 @wwBind@ works on a binding, trying each \tr{(binder, expr)} pair in
68 turn. Non-recursive case first, then recursive...
71 wwBind :: Bool -- True <=> top-level binding
73 -> UniqSM WwBinding -- returns a WwBinding intermediate form;
74 -- the caller will convert to Expr/Binding,
77 wwBind top_level (NonRec binder rhs)
78 = wwExpr rhs `thenUs` \ new_rhs ->
79 tryWW binder new_rhs `thenUs` \ new_pairs ->
80 returnUs (WwLet [NonRec b e | (b,e) <- new_pairs])
81 -- Generated bindings must be non-recursive
82 -- because the original binding was.
84 ------------------------------
86 wwBind top_level (Rec pairs)
87 = mapUs do_one pairs `thenUs` \ new_pairs ->
88 returnUs (WwLet [Rec (concat new_pairs)])
90 do_one (binder, rhs) = wwExpr rhs `thenUs` \ new_rhs ->
94 @wwExpr@ basically just walks the tree, looking for appropriate
95 annotations that can be used. Remember it is @wwBind@ that does the
96 matching by looking for strict arguments of the correct type.
97 @wwExpr@ is a version that just returns the ``Plain'' Tree.
101 wwExpr :: CoreExpr -> UniqSM CoreExpr
103 wwExpr e@(Var _) = returnUs e
104 wwExpr e@(Lit _) = returnUs e
105 wwExpr e@(Con _ _) = returnUs e
106 wwExpr e@(Prim _ _) = returnUs e
108 wwExpr (Lam binder expr)
109 = wwExpr expr `thenUs` \ new_expr ->
110 returnUs (Lam binder new_expr)
113 = wwExpr f `thenUs` \ new_f ->
114 returnUs (App new_f a)
117 = wwExpr expr `thenUs` \ new_expr ->
118 returnUs (SCC cc new_expr)
120 wwExpr (Coerce c ty expr)
121 = wwExpr expr `thenUs` \ new_expr ->
122 returnUs (Coerce c ty new_expr)
124 wwExpr (Let bind expr)
125 = wwBind False{-not top-level-} bind `thenUs` \ intermediate_bind ->
126 wwExpr expr `thenUs` \ new_expr ->
127 returnUs (mash_ww_bind intermediate_bind new_expr)
129 mash_ww_bind (WwLet binds) body = mkCoLetsNoUnboxed binds body
130 mash_ww_bind (WwCase case_fn) body = case_fn body
132 wwExpr (Case expr alts)
133 = wwExpr expr `thenUs` \ new_expr ->
134 ww_alts alts `thenUs` \ new_alts ->
135 returnUs (Case new_expr new_alts)
137 ww_alts (AlgAlts alts deflt)
138 = mapUs ww_alg_alt alts `thenUs` \ new_alts ->
139 ww_deflt deflt `thenUs` \ new_deflt ->
140 returnUs (AlgAlts new_alts new_deflt)
142 ww_alts (PrimAlts alts deflt)
143 = mapUs ww_prim_alt alts `thenUs` \ new_alts ->
144 ww_deflt deflt `thenUs` \ new_deflt ->
145 returnUs (PrimAlts new_alts new_deflt)
147 ww_alg_alt (con, binders, rhs)
148 = wwExpr rhs `thenUs` \ new_rhs ->
149 returnUs (con, binders, new_rhs)
151 ww_prim_alt (lit, rhs)
152 = wwExpr rhs `thenUs` \ new_rhs ->
153 returnUs (lit, new_rhs)
158 ww_deflt (BindDefault binder rhs)
159 = wwExpr rhs `thenUs` \ new_rhs ->
160 returnUs (BindDefault binder new_rhs)
163 %************************************************************************
165 \subsection[tryWW]{@tryWW@: attempt a worker/wrapper pair}
167 %************************************************************************
169 @tryWW@ just accumulates arguments, converts strictness info from the
170 front-end into the proper form, then calls @mkWwBodies@ to do
173 We have to BE CAREFUL that we don't worker-wrapperize an Id that has
174 already been w-w'd! (You can end up with several liked-named Ids
175 bouncing around at the same time---absolute mischief.) So the
176 criterion we use is: if an Id already has an unfolding (for whatever
177 reason), then we don't w-w it.
179 The only reason this is monadised is for the unique supply.
182 tryWW :: Id -- The fn binder
183 -> CoreExpr -- The bound rhs; its innards
185 -> UniqSM [(Id, CoreExpr)] -- either *one* or *two* pairs;
186 -- if one, then no worker (only
187 -- the orig "wrapper" lives on);
188 -- if two, then a worker and a
191 | (certainlySmallEnoughToInline $
192 calcUnfoldingGuidance (getInlinePragma fn_id)
193 opt_UnfoldingCreationThreshold
196 -- No point in worker/wrappering something that is going to be
197 -- INLINEd wholesale anyway. If the strictness analyser is run
198 -- twice, this test also prevents wrappers (which are INLINEd)
199 -- from being re-done.
201 || not has_strictness_info
202 || not (worthSplitting revised_wrap_args_info)
203 = returnUs [ (fn_id, rhs) ]
205 | otherwise -- Do w/w split
207 (uvars, tyvars, wrap_args, body) = collectBinders rhs
209 mkWwBodies tyvars wrap_args
211 revised_wrap_args_info `thenUs` \ (wrap_fn, work_fn, work_demands) ->
212 getUnique `thenUs` \ work_uniq ->
214 work_rhs = work_fn body
215 work_id = mkWorkerId work_uniq fn_id (coreExprType work_rhs) work_info
216 work_info = noIdInfo `addStrictnessInfo` mkStrictnessInfo work_demands False
218 wrap_rhs = wrap_fn work_id
219 wrap_id = addInlinePragma (fn_id `addIdStrictness`
220 mkStrictnessInfo revised_wrap_args_info True)
221 -- Add info to the wrapper:
222 -- (a) we want to inline it everywhere
223 -- (b) we want to pin on its revised stricteness info
224 -- (c) we pin on its worker id and the list of constructors mentioned in the wrapper
226 returnUs ([(work_id, work_rhs), (wrap_id, wrap_rhs)])
227 -- Worker first, because wrapper mentions it
229 strictness_info = getIdStrictness fn_id
230 has_strictness_info = case strictness_info of
231 StrictnessInfo _ _ -> True
234 wrap_args_info = case strictness_info of
235 StrictnessInfo args_info _ -> args_info
236 revised_wrap_args_info = setUnpackStrategy wrap_args_info
238 -- This rather crude function looks at a wrapper function, and
239 -- snaffles out (a) the worker Id and (b) constructors needed to
241 -- These are needed when we write an interface file.
242 getWorkerIdAndCons wrap_id wrapper_fn
245 go (Lam _ body) = go body
246 go (Case _ (AlgAlts [(con,_,rhs)] _)) = let (wrap_id, cons) = go rhs
247 in (wrap_id, cons `addOneToIdSet` con)
248 go (Let (NonRec _ (Coerce (CoerceOut con) _ _)) body)
249 = let (wrap_id, cons) = go body
250 in (wrap_id, cons `addOneToIdSet` con)
251 go other = (get_work_id other, emptyIdSet)
253 get_work_id (App fn _) = get_work_id fn
254 get_work_id (Var work_id) = work_id
255 get_work_id other = pprPanic "getWorkerIdAndCons" (ppr PprDebug wrap_id)