2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[CoreUnfold]{Core-syntax unfoldings}
6 Unfoldings (which can travel across module boundaries) are in Core
7 syntax (namely @CoreExpr@s).
9 The type @Unfolding@ sits ``above'' simply-Core-expressions
10 unfoldings, capturing ``higher-level'' things we know about a binding,
11 usually things that the simplifier found out (e.g., ``it's a
12 literal''). In the corner of a @CoreUnfolding@ unfolding, you will
13 find, unsurprisingly, a Core expression.
17 Unfolding, UnfoldingGuidance, -- Abstract types
19 noUnfolding, mkTopUnfolding, mkUnfolding, mkCompulsoryUnfolding, seqUnfolding,
20 mkOtherCon, otherCons,
21 unfoldingTemplate, maybeUnfoldingTemplate,
22 isEvaldUnfolding, isValueUnfolding, isCheapUnfolding, isCompulsoryUnfolding,
23 hasUnfolding, hasSomeUnfolding, neverUnfold,
25 couldBeSmallEnoughToInline,
32 #include "HsVersions.h"
34 import CmdLineOpts ( opt_UF_CreationThreshold,
36 opt_UF_FunAppDiscount,
38 opt_UF_DearOp, opt_UnfoldCasms,
39 DynFlags, DynFlag(..), dopt
42 import PprCore ( pprCoreExpr )
43 import OccurAnal ( occurAnalyseGlobalExpr )
44 import CoreUtils ( exprIsValue, exprIsCheap, exprIsTrivial )
45 import Id ( Id, idType, isId,
47 isFCallId_maybe, globalIdDetails
49 import DataCon ( isUnboxedTupleCon )
50 import Literal ( isLitLitLit, litSize )
51 import PrimOp ( primOpIsDupable, primOpOutOfLine )
52 import ForeignCall ( okToExposeFCall )
53 import IdInfo ( OccInfo(..), GlobalIdDetails(..) )
54 import Type ( isUnLiftedType )
55 import PrelNames ( hasKey, buildIdKey, augmentIdKey )
60 #if __GLASGOW_HASKELL__ >= 404
61 import GlaExts ( fromInt, Int# )
66 %************************************************************************
68 \subsection{Making unfoldings}
70 %************************************************************************
73 mkTopUnfolding expr = mkUnfolding True {- Top level -} expr
75 mkUnfolding top_lvl expr
76 = CoreUnfolding (occurAnalyseGlobalExpr expr)
83 -- OK to inline inside a lambda
85 (calcUnfoldingGuidance opt_UF_CreationThreshold expr)
86 -- Sometimes during simplification, there's a large let-bound thing
87 -- which has been substituted, and so is now dead; so 'expr' contains
88 -- two copies of the thing while the occurrence-analysed expression doesn't
89 -- Nevertheless, we don't occ-analyse before computing the size because the
90 -- size computation bales out after a while, whereas occurrence analysis does not.
92 -- This can occasionally mean that the guidance is very pessimistic;
93 -- it gets fixed up next round
95 mkCompulsoryUnfolding expr -- Used for things that absolutely must be unfolded
96 = CompulsoryUnfolding (occurAnalyseGlobalExpr expr)
100 %************************************************************************
102 \subsection{The UnfoldingGuidance type}
104 %************************************************************************
107 instance Outputable UnfoldingGuidance where
108 ppr UnfoldNever = ptext SLIT("NEVER")
109 ppr (UnfoldIfGoodArgs v cs size discount)
110 = hsep [ ptext SLIT("IF_ARGS"), int v,
111 brackets (hsep (map int cs)),
118 calcUnfoldingGuidance
119 :: Int -- bomb out if size gets bigger than this
120 -> CoreExpr -- expression to look at
122 calcUnfoldingGuidance bOMB_OUT_SIZE expr
123 = case collect_val_bndrs expr of { (inline, val_binders, body) ->
125 n_val_binders = length val_binders
127 max_inline_size = n_val_binders+2
128 -- The idea is that if there is an INLINE pragma (inline is True)
129 -- and there's a big body, we give a size of n_val_binders+2. This
130 -- This is just enough to fail the no-size-increase test in callSiteInline,
131 -- so that INLINE things don't get inlined into entirely boring contexts,
135 case (sizeExpr (iUnbox bOMB_OUT_SIZE) val_binders body) of
138 | not inline -> UnfoldNever
139 -- A big function with an INLINE pragma must
140 -- have an UnfoldIfGoodArgs guidance
141 | inline -> UnfoldIfGoodArgs n_val_binders
142 (map (const 0) val_binders)
145 SizeIs size cased_args scrut_discount
148 (map discount_for val_binders)
150 (iBox scrut_discount)
152 boxed_size = iBox size
154 final_size | inline = boxed_size `min` max_inline_size
155 | otherwise = boxed_size
157 -- Sometimes an INLINE thing is smaller than n_val_binders+2.
158 -- A particular case in point is a constructor, which has size 1.
159 -- We want to inline this regardless, hence the `min`
161 discount_for b = foldlBag (\acc (b',n) -> if b==b' then acc+n else acc)
165 collect_val_bndrs e = go False [] e
166 -- We need to be a bit careful about how we collect the
167 -- value binders. In ptic, if we see
168 -- __inline_me (\x y -> e)
169 -- We want to say "2 value binders". Why? So that
170 -- we take account of information given for the arguments
172 go inline rev_vbs (Note InlineMe e) = go True rev_vbs e
173 go inline rev_vbs (Lam b e) | isId b = go inline (b:rev_vbs) e
174 | otherwise = go inline rev_vbs e
175 go inline rev_vbs e = (inline, reverse rev_vbs, e)
179 sizeExpr :: Int# -- Bomb out if it gets bigger than this
180 -> [Id] -- Arguments; we're interested in which of these
185 sizeExpr bOMB_OUT_SIZE top_args expr
188 size_up (Type t) = sizeZero -- Types cost nothing
189 size_up (Var v) = sizeOne
191 size_up (Note InlineMe body) = sizeOne -- Inline notes make it look very small
192 -- This can be important. If you have an instance decl like this:
193 -- instance Foo a => Foo [a] where
194 -- {-# INLINE op1, op2 #-}
197 -- then we'll get a dfun which is a pair of two INLINE lambdas
199 size_up (Note _ body) = size_up body -- Other notes cost nothing
201 size_up (App fun (Type t)) = size_up fun
202 size_up (App fun arg) = size_up_app fun [arg]
204 size_up (Lit lit) = sizeN (litSize lit)
206 size_up (Lam b e) | isId b = lamScrutDiscount (size_up e `addSizeN` 1)
207 | otherwise = size_up e
209 size_up (Let (NonRec binder rhs) body)
210 = nukeScrutDiscount (size_up rhs) `addSize`
211 size_up body `addSizeN`
212 (if isUnLiftedType (idType binder) then 0 else 1)
213 -- For the allocation
214 -- If the binder has an unlifted type there is no allocation
216 size_up (Let (Rec pairs) body)
217 = nukeScrutDiscount rhs_size `addSize`
218 size_up body `addSizeN`
219 length pairs -- For the allocation
221 rhs_size = foldr (addSize . size_up . snd) sizeZero pairs
223 size_up (Case (Var v) _ alts)
224 | v `elem` top_args -- We are scrutinising an argument variable
226 {- I'm nuking this special case; BUT see the comment with case alternatives.
228 (a) It's too eager. We don't want to inline a wrapper into a
229 context with no benefit.
230 E.g. \ x. f (x+x) no point in inlining (+) here!
232 (b) It's ineffective. Once g's wrapper is inlined, its case-expressions
233 aren't scrutinising arguments any more
237 [alt] -> size_up_alt alt `addSize` SizeIs 0# (unitBag (v, 1)) 0#
238 -- We want to make wrapper-style evaluation look cheap, so that
239 -- when we inline a wrapper it doesn't make call site (much) bigger
240 -- Otherwise we get nasty phase ordering stuff:
243 -- If we inline g's wrapper, f looks big, and doesn't get inlined
244 -- into h; if we inline f first, while it looks small, then g's
245 -- wrapper will get inlined later anyway. To avoid this nasty
246 -- ordering difference, we make (case a of (x,y) -> ...),
247 -- *where a is one of the arguments* look free.
251 alts_size (foldr addSize sizeOne alt_sizes) -- The 1 is for the scrutinee
252 (foldr1 maxSize alt_sizes)
254 -- Good to inline if an arg is scrutinised, because
255 -- that may eliminate allocation in the caller
256 -- And it eliminates the case itself
259 alt_sizes = map size_up_alt alts
261 -- alts_size tries to compute a good discount for
262 -- the case when we are scrutinising an argument variable
263 alts_size (SizeIs tot tot_disc tot_scrut) -- Size of all alternatives
264 (SizeIs max max_disc max_scrut) -- Size of biggest alternative
265 = SizeIs tot (unitBag (v, iBox (_ILIT 1 +# tot -# max)) `unionBags` max_disc) max_scrut
266 -- If the variable is known, we produce a discount that
267 -- will take us back to 'max', the size of rh largest alternative
268 -- The 1+ is a little discount for reduced allocation in the caller
269 alts_size tot_size _ = tot_size
272 size_up (Case e _ alts) = nukeScrutDiscount (size_up e) `addSize`
273 foldr (addSize . size_up_alt) sizeZero alts
274 -- We don't charge for the case itself
275 -- It's a strict thing, and the price of the call
276 -- is paid by scrut. Also consider
277 -- case f x of DEFAULT -> e
278 -- This is just ';'! Don't charge for it.
281 size_up_app (App fun arg) args
282 | isTypeArg arg = size_up_app fun args
283 | otherwise = size_up_app fun (arg:args)
284 size_up_app fun args = foldr (addSize . nukeScrutDiscount . size_up)
285 (size_up_fun fun args)
288 -- A function application with at least one value argument
289 -- so if the function is an argument give it an arg-discount
291 -- Also behave specially if the function is a build
293 -- Also if the function is a constant Id (constr or primop)
294 -- compute discounts specially
295 size_up_fun (Var fun) args
296 | fun `hasKey` buildIdKey = buildSize
297 | fun `hasKey` augmentIdKey = augmentSize
299 = case globalIdDetails fun of
300 DataConId dc -> conSizeN dc (valArgCount args)
302 FCallId fc -> sizeN opt_UF_DearOp
303 PrimOpId op -> primOpSize op (valArgCount args)
304 -- foldr addSize (primOpSize op) (map arg_discount args)
305 -- At one time I tried giving an arg-discount if a primop
306 -- is applied to one of the function's arguments, but it's
307 -- not good. At the moment, any unlifted-type arg gets a
308 -- 'True' for 'yes I'm evald', so we collect the discount even
309 -- if we know nothing about it. And just having it in a primop
310 -- doesn't help at all if we don't know something more.
312 other -> fun_discount fun `addSizeN`
313 (1 + length (filter (not . exprIsTrivial) args))
314 -- The 1+ is for the function itself
315 -- Add 1 for each non-trivial arg;
316 -- the allocation cost, as in let(rec)
317 -- Slight hack here: for constructors the args are almost always
318 -- trivial; and for primops they are almost always prim typed
319 -- We should really only count for non-prim-typed args in the
320 -- general case, but that seems too much like hard work
322 size_up_fun other args = size_up other
325 size_up_alt (con, bndrs, rhs) = size_up rhs
326 -- Don't charge for args, so that wrappers look cheap
327 -- (See comments about wrappers with Case)
330 -- We want to record if we're case'ing, or applying, an argument
331 fun_discount v | v `elem` top_args = SizeIs 0# (unitBag (v, opt_UF_FunAppDiscount)) 0#
332 fun_discount other = sizeZero
335 -- These addSize things have to be here because
336 -- I don't want to give them bOMB_OUT_SIZE as an argument
338 addSizeN TooBig _ = TooBig
339 addSizeN (SizeIs n xs d) m = mkSizeIs bOMB_OUT_SIZE (n +# iUnbox m) xs d
341 addSize TooBig _ = TooBig
342 addSize _ TooBig = TooBig
343 addSize (SizeIs n1 xs d1) (SizeIs n2 ys d2)
344 = mkSizeIs bOMB_OUT_SIZE (n1 +# n2) (xs `unionBags` ys) (d1 +# d2)
347 Code for manipulating sizes
350 data ExprSize = TooBig
351 | SizeIs FastInt -- Size found
352 (Bag (Id,Int)) -- Arguments cased herein, and discount for each such
353 FastInt -- Size to subtract if result is scrutinised
354 -- by a case expression
356 -- subtract the discount before deciding whether to bale out. eg. we
357 -- want to inline a large constructor application into a selector:
358 -- tup = (a_1, ..., a_99)
359 -- x = case tup of ...
361 mkSizeIs max n xs d | (n -# d) ># max = TooBig
362 | otherwise = SizeIs n xs d
364 maxSize TooBig _ = TooBig
365 maxSize _ TooBig = TooBig
366 maxSize s1@(SizeIs n1 _ _) s2@(SizeIs n2 _ _) | n1 ># n2 = s1
369 sizeZero = SizeIs (_ILIT 0) emptyBag (_ILIT 0)
370 sizeOne = SizeIs (_ILIT 1) emptyBag (_ILIT 0)
371 sizeN n = SizeIs (iUnbox n) emptyBag (_ILIT 0)
373 | isUnboxedTupleCon dc = SizeIs (_ILIT 0) emptyBag (iUnbox n +# _ILIT 1)
374 | otherwise = SizeIs (_ILIT 1) emptyBag (iUnbox n +# _ILIT 1)
375 -- Treat constructors as size 1; we are keen to expose them
376 -- (and we charge separately for their args). We can't treat
377 -- them as size zero, else we find that (iBox x) has size 1,
378 -- which is the same as a lone variable; and hence 'v' will
379 -- always be replaced by (iBox x), where v is bound to iBox x.
381 -- However, unboxed tuples count as size zero
382 -- I found occasions where we had
383 -- f x y z = case op# x y z of { s -> (# s, () #) }
384 -- and f wasn't getting inlined
387 | not (primOpIsDupable op) = sizeN opt_UF_DearOp
388 | not (primOpOutOfLine op) = sizeN (2 - n_args)
389 -- Be very keen to inline simple primops.
390 -- We give a discount of 1 for each arg so that (op# x y z) costs 2.
391 -- We can't make it cost 1, else we'll inline let v = (op# x y z)
392 -- at every use of v, which is excessive.
394 -- A good example is:
395 -- let x = +# p q in C {x}
396 -- Even though x get's an occurrence of 'many', its RHS looks cheap,
397 -- and there's a good chance it'll get inlined back into C's RHS. Urgh!
398 | otherwise = sizeOne
400 buildSize = SizeIs (-2#) emptyBag 4#
401 -- We really want to inline applications of build
402 -- build t (\cn -> e) should cost only the cost of e (because build will be inlined later)
403 -- Indeed, we should add a result_discount becuause build is
404 -- very like a constructor. We don't bother to check that the
405 -- build is saturated (it usually is). The "-2" discounts for the \c n,
406 -- The "4" is rather arbitrary.
408 augmentSize = SizeIs (-2#) emptyBag 4#
409 -- Ditto (augment t (\cn -> e) ys) should cost only the cost of
410 -- e plus ys. The -2 accounts for the \cn
412 nukeScrutDiscount (SizeIs n vs d) = SizeIs n vs 0#
413 nukeScrutDiscount TooBig = TooBig
415 -- When we return a lambda, give a discount if it's used (applied)
416 lamScrutDiscount (SizeIs n vs d) = case opt_UF_FunAppDiscount of { d -> SizeIs n vs (iUnbox d) }
417 lamScrutDiscount TooBig = TooBig
421 %************************************************************************
423 \subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
425 %************************************************************************
427 We have very limited information about an unfolding expression: (1)~so
428 many type arguments and so many value arguments expected---for our
429 purposes here, we assume we've got those. (2)~A ``size'' or ``cost,''
430 a single integer. (3)~An ``argument info'' vector. For this, what we
431 have at the moment is a Boolean per argument position that says, ``I
432 will look with great favour on an explicit constructor in this
433 position.'' (4)~The ``discount'' to subtract if the expression
434 is being scrutinised.
436 Assuming we have enough type- and value arguments (if not, we give up
437 immediately), then we see if the ``discounted size'' is below some
438 (semi-arbitrary) threshold. It works like this: for every argument
439 position where we're looking for a constructor AND WE HAVE ONE in our
440 hands, we get a (again, semi-arbitrary) discount [proportion to the
441 number of constructors in the type being scrutinized].
443 If we're in the context of a scrutinee ( \tr{(case <expr > of A .. -> ...;.. )})
444 and the expression in question will evaluate to a constructor, we use
445 the computed discount size *for the result only* rather than
446 computing the argument discounts. Since we know the result of
447 the expression is going to be taken apart, discounting its size
448 is more accurate (see @sizeExpr@ above for how this discount size
451 We use this one to avoid exporting inlinings that we ``couldn't possibly
452 use'' on the other side. Can be overridden w/ flaggery.
453 Just the same as smallEnoughToInline, except that it has no actual arguments.
456 couldBeSmallEnoughToInline :: Int -> CoreExpr -> Bool
457 couldBeSmallEnoughToInline threshold rhs = case calcUnfoldingGuidance threshold rhs of
461 certainlyWillInline :: Unfolding -> Bool
462 -- Sees if the unfolding is pretty certain to inline
463 certainlyWillInline (CoreUnfolding _ _ _ is_cheap (UnfoldIfGoodArgs n_vals _ size _))
464 = is_cheap && size - (n_vals +1) <= opt_UF_UseThreshold
465 certainlyWillInline other
469 @okToUnfoldInHifile@ is used when emitting unfolding info into an interface
470 file to determine whether an unfolding candidate really should be unfolded.
471 The predicate is needed to prevent @_casm_@s (+ lit-lits) from being emitted
472 into interface files.
474 The reason for inlining expressions containing _casm_s into interface files
475 is that these fragments of C are likely to mention functions/#defines that
476 will be out-of-scope when inlined into another module. This is not an
477 unfixable problem for the user (just need to -#include the approp. header
478 file), but turning it off seems to the simplest thing to do.
481 okToUnfoldInHiFile :: CoreExpr -> Bool
482 okToUnfoldInHiFile e = opt_UnfoldCasms || go e
484 -- Race over an expression looking for CCalls..
485 go (Var v) = case isFCallId_maybe v of
486 Just fcall -> okToExposeFCall fcall
488 go (Lit lit) = not (isLitLitLit lit)
489 go (App fun arg) = go fun && go arg
490 go (Lam _ body) = go body
491 go (Let binds body) = and (map go (body :rhssOfBind binds))
492 go (Case scrut bndr alts) = and (map go (scrut:rhssOfAlts alts)) &&
493 not (any isLitLitLit [ lit | (LitAlt lit, _, _) <- alts ])
494 go (Note _ body) = go body
499 %************************************************************************
501 \subsection{callSiteInline}
503 %************************************************************************
505 This is the key function. It decides whether to inline a variable at a call site
507 callSiteInline is used at call sites, so it is a bit more generous.
508 It's a very important function that embodies lots of heuristics.
509 A non-WHNF can be inlined if it doesn't occur inside a lambda,
510 and occurs exactly once or
511 occurs once in each branch of a case and is small
513 If the thing is in WHNF, there's no danger of duplicating work,
514 so we can inline if it occurs once, or is small
516 NOTE: we don't want to inline top-level functions that always diverge.
517 It just makes the code bigger. Tt turns out that the convenient way to prevent
518 them inlining is to give them a NOINLINE pragma, which we do in
519 StrictAnal.addStrictnessInfoToTopId
522 callSiteInline :: DynFlags
523 -> Bool -- True <=> the Id can be inlined
524 -> Bool -- 'inline' note at call site
527 -> [Bool] -- One for each value arg; True if it is interesting
528 -> Bool -- True <=> continuation is interesting
529 -> Maybe CoreExpr -- Unfolding, if any
532 callSiteInline dflags active_inline inline_call occ id arg_infos interesting_cont
533 = case idUnfolding id of {
534 NoUnfolding -> Nothing ;
535 OtherCon cs -> Nothing ;
537 CompulsoryUnfolding unf_template -> Just unf_template ;
538 -- CompulsoryUnfolding => there is no top-level binding
539 -- for these things, so we must inline it.
540 -- Only a couple of primop-like things have
541 -- compulsory unfoldings (see MkId.lhs).
542 -- We don't allow them to be inactive
544 CoreUnfolding unf_template is_top is_value is_cheap guidance ->
547 result | yes_or_no = Just unf_template
548 | otherwise = Nothing
550 n_val_args = length arg_infos
553 | not active_inline = False
554 | otherwise = case occ of
555 IAmDead -> pprTrace "callSiteInline: dead" (ppr id) False
556 IAmALoopBreaker -> False
557 OneOcc in_lam one_br -> (not in_lam || is_cheap) && consider_safe in_lam True one_br
558 NoOccInfo -> is_cheap && consider_safe True False False
560 consider_safe in_lam once once_in_one_branch
561 -- consider_safe decides whether it's a good idea to inline something,
562 -- given that there's no work-duplication issue (the caller checks that).
563 -- once_in_one_branch = True means there's a unique textual occurrence
567 -- Be very keen to inline something if this is its unique occurrence:
569 -- a) Inlining gives a good chance of eliminating the original
570 -- binding (and hence the allocation) for the thing.
571 -- (Provided it's not a top level binding, in which case the
572 -- allocation costs nothing.)
574 -- b) Inlining a function that is called only once exposes the
575 -- body function to the call site.
577 -- The only time we hold back is when substituting inside a lambda;
578 -- then if the context is totally uninteresting (not applied, not scrutinised)
579 -- there is no point in substituting because it might just increase allocation,
580 -- by allocating the function itself many times
581 -- Note [Jan 2002]: this comment looks out of date. The actual code
582 -- doesn't inline *ever* in an uninteresting context. Why not? I
583 -- think it's just because we don't want to inline top-level constants
584 -- into uninteresting contexts, lest we (for example) re-nest top-level
587 -- Note: there used to be a '&& not top_level' in the guard above,
588 -- but that stopped us inlining top-level functions used only once,
590 = WARN( not is_top && not in_lam, ppr id )
591 -- If (not in_lam) && one_br then PreInlineUnconditionally
592 -- should have caught it, shouldn't it? Unless it's a top
594 not (null arg_infos) || interesting_cont
598 UnfoldNever -> False ;
599 UnfoldIfGoodArgs n_vals_wanted arg_discounts size res_discount
601 | enough_args && size <= (n_vals_wanted + 1)
602 -- Inline unconditionally if there no size increase
603 -- Size of call is n_vals_wanted (+1 for the function)
607 -> some_benefit && small_enough
610 some_benefit = or arg_infos || really_interesting_cont ||
611 (not is_top && (once || (n_vals_wanted > 0 && enough_args)))
612 -- If it occurs more than once, there must be something interesting
613 -- about some argument, or the result context, to make it worth inlining
615 -- If a function has a nested defn we also record some-benefit,
616 -- on the grounds that we are often able to eliminate the binding,
617 -- and hence the allocation, for the function altogether; this is good
618 -- for join points. But this only makes sense for *functions*;
619 -- inlining a constructor doesn't help allocation unless the result is
620 -- scrutinised. UNLESS the constructor occurs just once, albeit possibly
621 -- in multiple case branches. Then inlining it doesn't increase allocation,
622 -- but it does increase the chance that the constructor won't be allocated at all
623 -- in the branches that don't use it.
625 enough_args = n_val_args >= n_vals_wanted
626 really_interesting_cont | n_val_args < n_vals_wanted = False -- Too few args
627 | n_val_args == n_vals_wanted = interesting_cont
628 | otherwise = True -- Extra args
629 -- really_interesting_cont tells if the result of the
630 -- call is in an interesting context.
632 small_enough = (size - discount) <= opt_UF_UseThreshold
633 discount = computeDiscount n_vals_wanted arg_discounts res_discount
634 arg_infos really_interesting_cont
637 if dopt Opt_D_dump_inlinings dflags then
638 pprTrace "Considering inlining"
639 (ppr id <+> vcat [text "active:" <+> ppr active_inline,
640 text "occ info:" <+> ppr occ,
641 text "arg infos" <+> ppr arg_infos,
642 text "interesting continuation" <+> ppr interesting_cont,
643 text "is value:" <+> ppr is_value,
644 text "is cheap:" <+> ppr is_cheap,
645 text "guidance" <+> ppr guidance,
646 text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO",
648 text "Unfolding =" <+> pprCoreExpr unf_template
655 computeDiscount :: Int -> [Int] -> Int -> [Bool] -> Bool -> Int
656 computeDiscount n_vals_wanted arg_discounts res_discount arg_infos result_used
657 -- We multiple the raw discounts (args_discount and result_discount)
658 -- ty opt_UnfoldingKeenessFactor because the former have to do with
659 -- *size* whereas the discounts imply that there's some extra
660 -- *efficiency* to be gained (e.g. beta reductions, case reductions)
663 -- we also discount 1 for each argument passed, because these will
664 -- reduce with the lambdas in the function (we count 1 for a lambda
666 = 1 + -- Discount of 1 because the result replaces the call
667 -- so we count 1 for the function itself
668 length (take n_vals_wanted arg_infos) +
669 -- Discount of 1 for each arg supplied, because the
670 -- result replaces the call
671 round (opt_UF_KeenessFactor *
672 fromInt (arg_discount + result_discount))
674 arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)
676 mk_arg_discount discount is_evald | is_evald = discount
679 -- Don't give a result discount unless there are enough args
680 result_discount | result_used = res_discount -- Over-applied, or case scrut