8a8ff80c511ded9310a4846fa434e70db1f42150
[ghc-hetmet.git] / ghc / compiler / stranal / WorkWrap.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
3 %
4 \section[WorkWrap]{Worker/wrapper-generating back-end of strictness analyser}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module WorkWrap ( workersAndWrappers ) where
10
11 IMP_Ubiq(){-uitous-}
12
13 import CoreSyn
14 import CoreUnfold       ( Unfolding(..), UnfoldingGuidance(..) )
15 IMPORT_DELOOPER(IdLoop)  -- ToDo:rm when iWantToBeINLINEd goes
16
17 import CoreUtils        ( coreExprType )
18 import Id               ( idWantsToBeINLINEd, getIdStrictness, mkWorkerId,
19                           getIdInfo, replaceIdInfo, GenId
20                         )
21 import IdInfo           ( noIdInfo, addInfo_UF, indicatesWorker,
22                           mkStrictnessInfo, StrictnessInfo(..)
23                         )
24 import SaLib
25 import UniqSupply       ( returnUs, thenUs, mapUs, getUnique, SYN_IE(UniqSM) )
26 import WwLib
27
28 iWantToBeINLINEd :: UnfoldingGuidance -> Unfolding
29 iWantToBeINLINEd x = NoUnfolding --ToDo:panic "WorkWrap.iWantToBeINLINEd (ToDo)"
30 \end{code}
31
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:
35 \begin{enumerate}
36 \item
37 Functions have been split into workers and wrappers where appropriate;
38 \item
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).
42 \end{enumerate}
43
44 \begin{code}
45 workersAndWrappers :: [CoreBinding] -> UniqSM [CoreBinding]
46
47 workersAndWrappers top_binds
48   = mapUs (wwBind True{-top-level-}) top_binds `thenUs` \ top_binds2 ->
49     let
50         top_binds3 = map make_top_binding top_binds2
51     in
52     returnUs (concat top_binds3)
53   where
54     make_top_binding :: WwBinding -> [CoreBinding]
55
56     make_top_binding (WwLet binds) = binds
57 \end{code}
58
59 %************************************************************************
60 %*                                                                      *
61 \subsection[wwBind-wwExpr]{@wwBind@ and @wwExpr@}
62 %*                                                                      *
63 %************************************************************************
64
65 @wwBind@ works on a binding, trying each \tr{(binder, expr)} pair in
66 turn.  Non-recursive case first, then recursive...
67
68 \begin{code}
69 wwBind  :: Bool                 -- True <=> top-level binding
70         -> CoreBinding
71         -> UniqSM WwBinding     -- returns a WwBinding intermediate form;
72                                 -- the caller will convert to Expr/Binding,
73                                 -- as appropriate.
74
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.
81
82 ------------------------------
83
84 wwBind top_level (Rec pairs)
85   = mapUs do_one pairs          `thenUs` \ new_pairs ->
86     returnUs (WwLet [Rec (concat new_pairs)])
87   where
88     do_one (binder, rhs) = wwExpr rhs   `thenUs` \ new_rhs ->
89                            tryWW binder new_rhs
90 \end{code}
91
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.
96 ???????????????? ToDo
97
98 \begin{code}
99 wwExpr :: CoreExpr -> UniqSM CoreExpr
100
101 wwExpr e@(Var _)    = returnUs e
102 wwExpr e@(Lit _)    = returnUs e
103 wwExpr e@(Con  _ _) = returnUs e
104 wwExpr e@(Prim _ _) = returnUs e
105
106 wwExpr (Lam binder expr)
107   = wwExpr expr                 `thenUs` \ new_expr ->
108     returnUs (Lam binder new_expr)
109
110 wwExpr (App f a)
111   = wwExpr f                    `thenUs` \ new_f ->
112     returnUs (App new_f a)
113
114 wwExpr (SCC cc expr)
115   = wwExpr expr                 `thenUs` \ new_expr ->
116     returnUs (SCC cc new_expr)
117
118 wwExpr (Coerce c ty expr)
119   = wwExpr expr                 `thenUs` \ new_expr ->
120     returnUs (Coerce c ty new_expr)
121
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)
126   where
127     mash_ww_bind (WwLet  binds)   body = mkCoLetsNoUnboxed binds body
128     mash_ww_bind (WwCase case_fn) body = case_fn body
129
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)
134   where
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)
139
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)
144
145     ww_alg_alt (con, binders, rhs)
146       = wwExpr rhs                      `thenUs` \ new_rhs ->
147         returnUs (con, binders, new_rhs)
148
149     ww_prim_alt (lit, rhs)
150       = wwExpr rhs                      `thenUs` \ new_rhs ->
151         returnUs (lit, new_rhs)
152
153     ww_deflt NoDefault
154       = returnUs NoDefault
155
156     ww_deflt (BindDefault binder rhs)
157       = wwExpr rhs                      `thenUs` \ new_rhs ->
158         returnUs (BindDefault binder new_rhs)
159 \end{code}
160
161 %************************************************************************
162 %*                                                                      *
163 \subsection[tryWW]{@tryWW@: attempt a worker/wrapper pair}
164 %*                                                                      *
165 %************************************************************************
166
167 @tryWW@ just accumulates arguments, converts strictness info from the
168 front-end into the proper form, then calls @mkWwBodies@ to do
169 the business.
170
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.
176
177 The only reason this is monadised is for the unique supply.
178
179 \begin{code}
180 tryWW   :: Id                           -- the fn binder
181         -> CoreExpr             -- the bound rhs; its innards
182                                         --   are already ww'd
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
187                                         -- wrapper.
188 tryWW fn_id rhs
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.
194   = do_nothing
195
196   | otherwise
197   = case (getIdStrictness fn_id) of
198
199       NoStrictnessInfo    -> do_nothing
200       BottomGuaranteed    -> do_nothing
201       StrictnessInfo [] _ -> do_nothing -- V weird (but possible?)
202
203       StrictnessInfo args_info _ ->
204         if not (indicatesWorker args_info) then
205             do_nothing
206         else
207
208         -- OK, it looks as if a worker is worth a try
209         let
210              (uvars, tyvars, args, body) = collectBinders rhs
211              body_ty                     = coreExprType body
212         in
213         mkWwBodies body_ty tyvars args args_info `thenUs` \ result ->
214         case result of
215
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!
219
220                         -- False. We might hit the all-args-absent-and-the-
221                         -- body-is-unboxed case.  A Nothing is legit. (WDP 94/10)
222                         do_nothing
223
224           Just (wrapper_w_hole, worker_w_hole, worker_strictness, worker_ty_w_hole) ->
225
226                 -- Terrific!  It worked!
227             getUnique           `thenUs` \ worker_uniq ->
228             let
229                 worker_ty   = worker_ty_w_hole body_ty
230
231                 worker_id   = mkWorkerId worker_uniq fn_id worker_ty
232                                 (noIdInfo `addInfo` worker_strictness)
233
234                 wrapper_rhs = wrapper_w_hole worker_id
235                 worker_rhs  = worker_w_hole body
236
237                 revised_strictness_info
238                   = -- We know the basic strictness info already, but
239                     -- we need to slam in the exact identity of the
240                     -- worker Id:
241                     mkStrictnessInfo args_info (Just worker_id)
242
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.
249             in
250             returnUs [ (worker_id,  worker_rhs),   -- worker comes first
251                        (wrapper_id, wrapper_rhs) ] -- because wrapper mentions it
252   where
253     do_nothing = returnUs [ (fn_id, rhs) ]
254 \end{code}