- arity = exprArity rhs
-
- strictness_info = getIdStrictness fn_id
- StrictnessInfo arg_demands result_bot = strictness_info
- has_strictness = case strictness_info of
- StrictnessInfo _ _ -> True
- other -> False
-
- do_strict_ww = has_strictness && worthSplitting wrap_dmds result_bot
-
- -- NB: There maybe be more items in arg_demands than arity, because
- -- the strictness info is semantic and looks through InlineMe and Scc Notes,
- -- whereas arity does not
- demands_for_visible_args = take arity arg_demands
- remaining_arg_demands = drop arity arg_demands
-
- wrap_dmds | has_strictness = setUnpackStrategy demands_for_visible_args
- | otherwise = take arity (repeat wwLazy)
-
- wrapper_strictness | has_strictness = mkStrictnessInfo (wrap_dmds ++ remaining_arg_demands, result_bot)
- | otherwise = noStrictnessInfo
-
- -------------------------------------------------------------
- cpr_info = getIdCprInfo fn_id
- has_cpr_info = case cpr_info of
- CPRInfo _ -> True
- other -> False
-
- do_cpr_ww = has_cpr_info
-
- -------------------------------------------------------------
- do_coerce_ww = check_for_coerce arity fun_ty
-
--- See if there's a Coerce before we run out of arity;
--- if so, it's worth trying a w/w split. Reason: we find
--- functions like f = coerce (\s -> e)
--- and g = \x -> coerce (\s -> e)
--- and they may have no useful strictness or cpr info, but if we
--- do the w/w thing we get rid of the coerces.
-
-check_for_coerce arity ty
- = length arg_tys <= arity && isNewType res_ty
- -- Don't look further than arity args,
- -- but if there are arity or fewer, see if there's
- -- a newtype in the corner
+
+ arity = arityInfo fn_info -- The arity is set by the simplifier using exprEtaExpandArity
+ -- So it may be more than the number of top-level-visible lambdas
+
+ work_res_info | isBotRes res_info = BotRes -- Cpr stuff done by wrapper
+ | otherwise = TopRes
+
+ one_shots = get_one_shots rhs
+
+-- If the original function has one-shot arguments, it is important to
+-- make the wrapper and worker have corresponding one-shot arguments too.
+-- Otherwise we spuriously float stuff out of case-expression join points,
+-- which is very annoying.
+get_one_shots (Lam b e)
+ | isId b = isOneShotLambda b : get_one_shots e
+ | otherwise = get_one_shots e
+get_one_shots (Note _ e) = get_one_shots e
+get_one_shots other = noOneShotInfo
+\end{code}
+
+Thunk splitting
+~~~~~~~~~~~~~~~
+Suppose x is used strictly (never mind whether it has the CPR
+property).
+
+ let
+ x* = x-rhs
+ in body
+
+splitThunk transforms like this:
+
+ let
+ x* = case x-rhs of { I# a -> I# a }
+ in body
+
+Now simplifier will transform to
+
+ case x-rhs of
+ I# a -> let x* = I# b
+ in body
+
+which is what we want. Now suppose x-rhs is itself a case:
+
+ x-rhs = case e of { T -> I# a; F -> I# b }
+
+The join point will abstract over a, rather than over (which is
+what would have happened before) which is fine.
+
+Notice that x certainly has the CPR property now!
+
+In fact, splitThunk uses the function argument w/w splitting
+function, so that if x's demand is deeper (say U(U(L,L),L))
+then the splitting will go deeper too.
+
+\begin{code}
+-- splitThunk converts the *non-recursive* binding
+-- x = e
+-- into
+-- x = let x = e
+-- in case x of
+-- I# y -> let x = I# y in x }
+-- See comments above. Is it not beautifully short?
+
+splitThunk fn_id rhs
+ = mkWWstr [fn_id] `thenUs` \ (_, wrap_fn, work_fn) ->
+ returnUs [ (fn_id, Let (NonRec fn_id rhs) (wrap_fn (work_fn (Var fn_id)))) ]
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Functions over Demands}
+%* *
+%************************************************************************
+
+\begin{code}
+worthSplittingFun :: [Demand] -> DmdResult -> Bool
+ -- True <=> the wrapper would not be an identity function
+worthSplittingFun ds res
+ = any worth_it ds || returnsCPR res
+ -- worthSplitting returns False for an empty list of demands,
+ -- and hence do_strict_ww is False if arity is zero and there is no CPR
+
+ -- We used not to split if the result is bottom.
+ -- [Justification: there's no efficiency to be gained.]
+ -- But it's sometimes bad not to make a wrapper. Consider
+ -- fw = \x# -> let x = I# x# in case e of
+ -- p1 -> error_fn x
+ -- p2 -> error_fn x
+ -- p3 -> the real stuff
+ -- The re-boxing code won't go away unless error_fn gets a wrapper too.
+ -- [We don't do reboxing now, but in general it's better to pass
+ -- an unboxed thing to f, and have it reboxed in the error cases....]