774ee6e0ec18c934d23a35041aba03d2d77a8340
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreUnfold.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[CoreUnfold]{Core-syntax unfoldings}
5
6 Unfoldings (which can travel across module boundaries) are in Core
7 syntax (namely @CoreExpr@s).
8
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.
14
15 \begin{code}
16 module CoreUnfold (
17         Unfolding, UnfoldingGuidance,   -- Abstract types
18
19         noUnfolding, mkTopUnfolding, mkUnfolding, mkCompulsoryUnfolding, seqUnfolding,
20         evaldUnfolding, mkOtherCon, otherCons,
21         unfoldingTemplate, maybeUnfoldingTemplate,
22         isEvaldUnfolding, isValueUnfolding, isCheapUnfolding, isCompulsoryUnfolding,
23         hasUnfolding, hasSomeUnfolding, neverUnfold,
24
25         couldBeSmallEnoughToInline, 
26         certainlyWillInline, 
27
28         callSiteInline
29     ) where
30
31 #include "HsVersions.h"
32
33 import StaticFlags      ( opt_UF_CreationThreshold, opt_UF_UseThreshold,
34                           opt_UF_FunAppDiscount, opt_UF_KeenessFactor,
35                           opt_UF_DearOp,
36                         )
37 import DynFlags         ( DynFlags, DynFlag(..), dopt )
38 import CoreSyn
39 import PprCore          ( pprCoreExpr )
40 import OccurAnal        ( occurAnalyseExpr )
41 import CoreUtils        ( exprIsValue, exprIsCheap, exprIsTrivial )
42 import Id               ( Id, idType, isId,
43                           idUnfolding, globalIdDetails
44                         )
45 import DataCon          ( isUnboxedTupleCon )
46 import Literal          ( litSize )
47 import PrimOp           ( primOpIsDupable, primOpOutOfLine )
48 import IdInfo           ( OccInfo(..), GlobalIdDetails(..) )
49 import Type             ( isUnLiftedType )
50 import PrelNames        ( hasKey, buildIdKey, augmentIdKey )
51 import Bag
52 import FastTypes
53 import Outputable
54 import Util
55
56 #if __GLASGOW_HASKELL__ >= 404
57 import GLAEXTS          ( Int# )
58 #endif
59 \end{code}
60
61
62 %************************************************************************
63 %*                                                                      *
64 \subsection{Making unfoldings}
65 %*                                                                      *
66 %************************************************************************
67
68 \begin{code}
69 mkTopUnfolding expr = mkUnfolding True {- Top level -} expr
70
71 mkUnfolding top_lvl expr
72   = CoreUnfolding (occurAnalyseExpr expr)
73                   top_lvl
74
75                   (exprIsValue expr)
76                         -- Already evaluated
77
78                   (exprIsCheap expr)
79                         -- OK to inline inside a lambda
80
81                   (calcUnfoldingGuidance opt_UF_CreationThreshold expr)
82         -- Sometimes during simplification, there's a large let-bound thing     
83         -- which has been substituted, and so is now dead; so 'expr' contains
84         -- two copies of the thing while the occurrence-analysed expression doesn't
85         -- Nevertheless, we don't occ-analyse before computing the size because the
86         -- size computation bales out after a while, whereas occurrence analysis does not.
87         --
88         -- This can occasionally mean that the guidance is very pessimistic;
89         -- it gets fixed up next round
90
91 mkCompulsoryUnfolding expr      -- Used for things that absolutely must be unfolded
92   = CompulsoryUnfolding (occurAnalyseExpr expr)
93 \end{code}
94
95
96 %************************************************************************
97 %*                                                                      *
98 \subsection{The UnfoldingGuidance type}
99 %*                                                                      *
100 %************************************************************************
101
102 \begin{code}
103 instance Outputable UnfoldingGuidance where
104     ppr UnfoldNever     = ptext SLIT("NEVER")
105     ppr (UnfoldIfGoodArgs v cs size discount)
106       = hsep [ ptext SLIT("IF_ARGS"), int v,
107                brackets (hsep (map int cs)),
108                int size,
109                int discount ]
110 \end{code}
111
112
113 \begin{code}
114 calcUnfoldingGuidance
115         :: Int                  -- bomb out if size gets bigger than this
116         -> CoreExpr             -- expression to look at
117         -> UnfoldingGuidance
118 calcUnfoldingGuidance bOMB_OUT_SIZE expr
119   = case collect_val_bndrs expr of { (inline, val_binders, body) ->
120     let
121         n_val_binders = length val_binders
122
123         max_inline_size = n_val_binders+2
124         -- The idea is that if there is an INLINE pragma (inline is True)
125         -- and there's a big body, we give a size of n_val_binders+2.  This
126         -- This is just enough to fail the no-size-increase test in callSiteInline,
127         --   so that INLINE things don't get inlined into entirely boring contexts,
128         --   but no more.
129
130     in
131     case (sizeExpr (iUnbox bOMB_OUT_SIZE) val_binders body) of
132
133       TooBig 
134         | not inline -> UnfoldNever
135                 -- A big function with an INLINE pragma must
136                 -- have an UnfoldIfGoodArgs guidance
137         | otherwise  -> UnfoldIfGoodArgs n_val_binders
138                                          (map (const 0) val_binders)
139                                          max_inline_size 0
140
141       SizeIs size cased_args scrut_discount
142         -> UnfoldIfGoodArgs
143                         n_val_binders
144                         (map discount_for val_binders)
145                         final_size
146                         (iBox scrut_discount)
147         where        
148             boxed_size    = iBox size
149
150             final_size | inline     = boxed_size `min` max_inline_size
151                        | otherwise  = boxed_size
152
153                 -- Sometimes an INLINE thing is smaller than n_val_binders+2.
154                 -- A particular case in point is a constructor, which has size 1.
155                 -- We want to inline this regardless, hence the `min`
156
157             discount_for b = foldlBag (\acc (b',n) -> if b==b' then acc+n else acc) 
158                                       0 cased_args
159         }
160   where
161     collect_val_bndrs e = go False [] e
162         -- We need to be a bit careful about how we collect the
163         -- value binders.  In ptic, if we see 
164         --      __inline_me (\x y -> e)
165         -- We want to say "2 value binders".  Why?  So that 
166         -- we take account of information given for the arguments
167
168     go inline rev_vbs (Note InlineMe e)     = go True   rev_vbs     e
169     go inline rev_vbs (Lam b e) | isId b    = go inline (b:rev_vbs) e
170                                 | otherwise = go inline rev_vbs     e
171     go inline rev_vbs e                     = (inline, reverse rev_vbs, e)
172 \end{code}
173
174 \begin{code}
175 sizeExpr :: Int#            -- Bomb out if it gets bigger than this
176          -> [Id]            -- Arguments; we're interested in which of these
177                             -- get case'd
178          -> CoreExpr
179          -> ExprSize
180
181 sizeExpr bOMB_OUT_SIZE top_args expr
182   = size_up expr
183   where
184     size_up (Type t)          = sizeZero        -- Types cost nothing
185     size_up (Var v)           = sizeOne
186
187     size_up (Note InlineMe body) = sizeOne      -- Inline notes make it look very small
188         -- This can be important.  If you have an instance decl like this:
189         --      instance Foo a => Foo [a] where
190         --         {-# INLINE op1, op2 #-}
191         --         op1 = ...
192         --         op2 = ...
193         -- then we'll get a dfun which is a pair of two INLINE lambdas
194
195     size_up (Note _        body) = size_up body -- Other notes cost nothing
196
197     size_up (App fun (Type t)) = size_up fun
198     size_up (App fun arg)      = size_up_app fun [arg]
199
200     size_up (Lit lit)          = sizeN (litSize lit)
201
202     size_up (Lam b e) | isId b    = lamScrutDiscount (size_up e `addSizeN` 1)
203                       | otherwise = size_up e
204
205     size_up (Let (NonRec binder rhs) body)
206       = nukeScrutDiscount (size_up rhs)         `addSize`
207         size_up body                            `addSizeN`
208         (if isUnLiftedType (idType binder) then 0 else 1)
209                 -- For the allocation
210                 -- If the binder has an unlifted type there is no allocation
211
212     size_up (Let (Rec pairs) body)
213       = nukeScrutDiscount rhs_size              `addSize`
214         size_up body                            `addSizeN`
215         length pairs            -- For the allocation
216       where
217         rhs_size = foldr (addSize . size_up . snd) sizeZero pairs
218
219     size_up (Case (Var v) _ _ alts) 
220         | v `elem` top_args             -- We are scrutinising an argument variable
221         = 
222 {-      I'm nuking this special case; BUT see the comment with case alternatives.
223
224         (a) It's too eager.  We don't want to inline a wrapper into a
225             context with no benefit.  
226             E.g.  \ x. f (x+x)          no point in inlining (+) here!
227
228         (b) It's ineffective. Once g's wrapper is inlined, its case-expressions 
229             aren't scrutinising arguments any more
230
231             case alts of
232
233                 [alt] -> size_up_alt alt `addSize` SizeIs 0# (unitBag (v, 1)) 0#
234                 -- We want to make wrapper-style evaluation look cheap, so that
235                 -- when we inline a wrapper it doesn't make call site (much) bigger
236                 -- Otherwise we get nasty phase ordering stuff: 
237                 --      f x = g x x
238                 --      h y = ...(f e)...
239                 -- If we inline g's wrapper, f looks big, and doesn't get inlined
240                 -- into h; if we inline f first, while it looks small, then g's 
241                 -- wrapper will get inlined later anyway.  To avoid this nasty
242                 -- ordering difference, we make (case a of (x,y) -> ...), 
243                 --  *where a is one of the arguments* look free.
244
245                 other -> 
246 -}
247                          alts_size (foldr addSize sizeOne alt_sizes)    -- The 1 is for the scrutinee
248                                    (foldr1 maxSize alt_sizes)
249
250                 -- Good to inline if an arg is scrutinised, because
251                 -- that may eliminate allocation in the caller
252                 -- And it eliminates the case itself
253
254         where
255           alt_sizes = map size_up_alt alts
256
257                 -- alts_size tries to compute a good discount for
258                 -- the case when we are scrutinising an argument variable
259           alts_size (SizeIs tot tot_disc tot_scrut)             -- Size of all alternatives
260                     (SizeIs max max_disc max_scrut)             -- Size of biggest alternative
261                 = SizeIs tot (unitBag (v, iBox (_ILIT 1 +# tot -# max)) `unionBags` max_disc) max_scrut
262                         -- If the variable is known, we produce a discount that
263                         -- will take us back to 'max', the size of rh largest alternative
264                         -- The 1+ is a little discount for reduced allocation in the caller
265           alts_size tot_size _ = tot_size
266
267 -- gaw 2004
268     size_up (Case e _ _ alts) = nukeScrutDiscount (size_up e) `addSize` 
269                                  foldr (addSize . size_up_alt) sizeZero alts
270                 -- We don't charge for the case itself
271                 -- It's a strict thing, and the price of the call
272                 -- is paid by scrut.  Also consider
273                 --      case f x of DEFAULT -> e
274                 -- This is just ';'!  Don't charge for it.
275
276     ------------ 
277     size_up_app (App fun arg) args   
278         | isTypeArg arg              = size_up_app fun args
279         | otherwise                  = size_up_app fun (arg:args)
280     size_up_app fun           args   = foldr (addSize . nukeScrutDiscount . size_up) 
281                                              (size_up_fun fun args)
282                                              args
283
284         -- A function application with at least one value argument
285         -- so if the function is an argument give it an arg-discount
286         --
287         -- Also behave specially if the function is a build
288         --
289         -- Also if the function is a constant Id (constr or primop)
290         -- compute discounts specially
291     size_up_fun (Var fun) args
292       | fun `hasKey` buildIdKey   = buildSize
293       | fun `hasKey` augmentIdKey = augmentSize
294       | otherwise 
295       = case globalIdDetails fun of
296           DataConWorkId dc -> conSizeN dc (valArgCount args)
297
298           FCallId fc   -> sizeN opt_UF_DearOp
299           PrimOpId op  -> primOpSize op (valArgCount args)
300                           -- foldr addSize (primOpSize op) (map arg_discount args)
301                           -- At one time I tried giving an arg-discount if a primop 
302                           -- is applied to one of the function's arguments, but it's
303                           -- not good.  At the moment, any unlifted-type arg gets a
304                           -- 'True' for 'yes I'm evald', so we collect the discount even
305                           -- if we know nothing about it.  And just having it in a primop
306                           -- doesn't help at all if we don't know something more.
307
308           other        -> fun_discount fun `addSizeN` 
309                           (1 + length (filter (not . exprIsTrivial) args))
310                                 -- The 1+ is for the function itself
311                                 -- Add 1 for each non-trivial arg;
312                                 -- the allocation cost, as in let(rec)
313                                 -- Slight hack here: for constructors the args are almost always
314                                 --      trivial; and for primops they are almost always prim typed
315                                 --      We should really only count for non-prim-typed args in the
316                                 --      general case, but that seems too much like hard work
317
318     size_up_fun other args = size_up other
319
320     ------------ 
321     size_up_alt (con, bndrs, rhs) = size_up rhs
322         -- Don't charge for args, so that wrappers look cheap
323         -- (See comments about wrappers with Case)
324
325     ------------
326         -- We want to record if we're case'ing, or applying, an argument
327     fun_discount v | v `elem` top_args = SizeIs 0# (unitBag (v, opt_UF_FunAppDiscount)) 0#
328     fun_discount other                 = sizeZero
329
330     ------------
331         -- These addSize things have to be here because
332         -- I don't want to give them bOMB_OUT_SIZE as an argument
333
334     addSizeN TooBig          _  = TooBig
335     addSizeN (SizeIs n xs d) m  = mkSizeIs bOMB_OUT_SIZE (n +# iUnbox m) xs d
336     
337     addSize TooBig            _                 = TooBig
338     addSize _                 TooBig            = TooBig
339     addSize (SizeIs n1 xs d1) (SizeIs n2 ys d2) 
340         = mkSizeIs bOMB_OUT_SIZE (n1 +# n2) (xs `unionBags` ys) (d1 +# d2)
341 \end{code}
342
343 Code for manipulating sizes
344
345 \begin{code}
346 data ExprSize = TooBig
347               | SizeIs FastInt          -- Size found
348                        (Bag (Id,Int))   -- Arguments cased herein, and discount for each such
349                        FastInt          -- Size to subtract if result is scrutinised 
350                                         -- by a case expression
351
352 -- subtract the discount before deciding whether to bale out. eg. we
353 -- want to inline a large constructor application into a selector:
354 --      tup = (a_1, ..., a_99)
355 --      x = case tup of ...
356 --
357 mkSizeIs max n xs d | (n -# d) ># max = TooBig
358                     | otherwise       = SizeIs n xs d
359  
360 maxSize TooBig         _                                  = TooBig
361 maxSize _              TooBig                             = TooBig
362 maxSize s1@(SizeIs n1 _ _) s2@(SizeIs n2 _ _) | n1 ># n2  = s1
363                                               | otherwise = s2
364
365 sizeZero        = SizeIs (_ILIT 0)  emptyBag (_ILIT 0)
366 sizeOne         = SizeIs (_ILIT 1)  emptyBag (_ILIT 0)
367 sizeN n         = SizeIs (iUnbox n) emptyBag (_ILIT 0)
368 conSizeN dc n   
369   | isUnboxedTupleCon dc = SizeIs (_ILIT 0) emptyBag (iUnbox n +# _ILIT 1)
370   | otherwise            = SizeIs (_ILIT 1) emptyBag (iUnbox n +# _ILIT 1)
371         -- Treat constructors as size 1; we are keen to expose them
372         -- (and we charge separately for their args).  We can't treat
373         -- them as size zero, else we find that (iBox x) has size 1,
374         -- which is the same as a lone variable; and hence 'v' will 
375         -- always be replaced by (iBox x), where v is bound to iBox x.
376         --
377         -- However, unboxed tuples count as size zero
378         -- I found occasions where we had 
379         --      f x y z = case op# x y z of { s -> (# s, () #) }
380         -- and f wasn't getting inlined
381
382 primOpSize op n_args
383  | not (primOpIsDupable op) = sizeN opt_UF_DearOp
384  | not (primOpOutOfLine op) = sizeN (2 - n_args)
385         -- Be very keen to inline simple primops.
386         -- We give a discount of 1 for each arg so that (op# x y z) costs 2.
387         -- We can't make it cost 1, else we'll inline let v = (op# x y z) 
388         -- at every use of v, which is excessive.
389         --
390         -- A good example is:
391         --      let x = +# p q in C {x}
392         -- Even though x get's an occurrence of 'many', its RHS looks cheap,
393         -- and there's a good chance it'll get inlined back into C's RHS. Urgh!
394  | otherwise                = sizeOne
395
396 buildSize = SizeIs (-2#) emptyBag 4#
397         -- We really want to inline applications of build
398         -- build t (\cn -> e) should cost only the cost of e (because build will be inlined later)
399         -- Indeed, we should add a result_discount becuause build is 
400         -- very like a constructor.  We don't bother to check that the
401         -- build is saturated (it usually is).  The "-2" discounts for the \c n, 
402         -- The "4" is rather arbitrary.
403
404 augmentSize = SizeIs (-2#) emptyBag 4#
405         -- Ditto (augment t (\cn -> e) ys) should cost only the cost of
406         -- e plus ys. The -2 accounts for the \cn 
407                                                 
408 nukeScrutDiscount (SizeIs n vs d) = SizeIs n vs 0#
409 nukeScrutDiscount TooBig          = TooBig
410
411 -- When we return a lambda, give a discount if it's used (applied)
412 lamScrutDiscount  (SizeIs n vs d) = case opt_UF_FunAppDiscount of { d -> SizeIs n vs (iUnbox d) }
413 lamScrutDiscount TooBig           = TooBig
414 \end{code}
415
416
417 %************************************************************************
418 %*                                                                      *
419 \subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
420 %*                                                                      *
421 %************************************************************************
422
423 We have very limited information about an unfolding expression: (1)~so
424 many type arguments and so many value arguments expected---for our
425 purposes here, we assume we've got those.  (2)~A ``size'' or ``cost,''
426 a single integer.  (3)~An ``argument info'' vector.  For this, what we
427 have at the moment is a Boolean per argument position that says, ``I
428 will look with great favour on an explicit constructor in this
429 position.'' (4)~The ``discount'' to subtract if the expression
430 is being scrutinised. 
431
432 Assuming we have enough type- and value arguments (if not, we give up
433 immediately), then we see if the ``discounted size'' is below some
434 (semi-arbitrary) threshold.  It works like this: for every argument
435 position where we're looking for a constructor AND WE HAVE ONE in our
436 hands, we get a (again, semi-arbitrary) discount [proportion to the
437 number of constructors in the type being scrutinized].
438
439 If we're in the context of a scrutinee ( \tr{(case <expr > of A .. -> ...;.. )})
440 and the expression in question will evaluate to a constructor, we use
441 the computed discount size *for the result only* rather than
442 computing the argument discounts. Since we know the result of
443 the expression is going to be taken apart, discounting its size
444 is more accurate (see @sizeExpr@ above for how this discount size
445 is computed).
446
447 We use this one to avoid exporting inlinings that we ``couldn't possibly
448 use'' on the other side.  Can be overridden w/ flaggery.
449 Just the same as smallEnoughToInline, except that it has no actual arguments.
450
451 \begin{code}
452 couldBeSmallEnoughToInline :: Int -> CoreExpr -> Bool
453 couldBeSmallEnoughToInline threshold rhs = case calcUnfoldingGuidance threshold rhs of
454                                                 UnfoldNever -> False
455                                                 other       -> True
456
457 certainlyWillInline :: Unfolding -> Bool
458   -- Sees if the unfolding is pretty certain to inline  
459 certainlyWillInline (CoreUnfolding _ _ _ is_cheap (UnfoldIfGoodArgs n_vals _ size _))
460   = is_cheap && size - (n_vals +1) <= opt_UF_UseThreshold
461 certainlyWillInline other
462   = False
463 \end{code}
464
465 %************************************************************************
466 %*                                                                      *
467 \subsection{callSiteInline}
468 %*                                                                      *
469 %************************************************************************
470
471 This is the key function.  It decides whether to inline a variable at a call site
472
473 callSiteInline is used at call sites, so it is a bit more generous.
474 It's a very important function that embodies lots of heuristics.
475 A non-WHNF can be inlined if it doesn't occur inside a lambda,
476 and occurs exactly once or 
477     occurs once in each branch of a case and is small
478
479 If the thing is in WHNF, there's no danger of duplicating work, 
480 so we can inline if it occurs once, or is small
481
482 NOTE: we don't want to inline top-level functions that always diverge.
483 It just makes the code bigger.  Tt turns out that the convenient way to prevent
484 them inlining is to give them a NOINLINE pragma, which we do in 
485 StrictAnal.addStrictnessInfoToTopId
486
487 \begin{code}
488 callSiteInline :: DynFlags
489                -> Bool                  -- True <=> the Id can be inlined
490                -> Bool                  -- 'inline' note at call site
491                -> OccInfo
492                -> Id                    -- The Id
493                -> [Bool]                -- One for each value arg; True if it is interesting
494                -> Bool                  -- True <=> continuation is interesting
495                -> Maybe CoreExpr        -- Unfolding, if any
496
497
498 callSiteInline dflags active_inline inline_call occ id arg_infos interesting_cont
499   = case idUnfolding id of {
500         NoUnfolding -> Nothing ;
501         OtherCon cs -> Nothing ;
502
503         CompulsoryUnfolding unf_template -> Just unf_template ;
504                 -- CompulsoryUnfolding => there is no top-level binding
505                 -- for these things, so we must inline it.
506                 -- Only a couple of primop-like things have 
507                 -- compulsory unfoldings (see MkId.lhs).
508                 -- We don't allow them to be inactive
509
510         CoreUnfolding unf_template is_top is_value is_cheap guidance ->
511
512     let
513         result | yes_or_no = Just unf_template
514                | otherwise = Nothing
515
516         n_val_args  = length arg_infos
517
518         yes_or_no 
519           | not active_inline = False
520           | otherwise = case occ of
521                                 IAmDead              -> pprTrace "callSiteInline: dead" (ppr id) False
522                                 IAmALoopBreaker      -> False
523
524                                 -- Occurs once in one branch.  These are deal with by
525                                 -- preInlineUnconditionally, so we ignore them here:
526                                 OneOcc _ True _      -> False
527
528                                 OneOcc in_lam False _ -> (not in_lam || is_cheap) && consider_safe True
529                                 other                -> is_cheap && consider_safe False
530
531         consider_safe once
532                 -- consider_safe decides whether it's a good idea to
533                 -- inline something, given that there's no
534                 -- work-duplication issue (the caller checks that).
535           | inline_call  = True
536
537           | otherwise
538           = case guidance of
539               UnfoldNever  -> False
540               UnfoldIfGoodArgs n_vals_wanted arg_discounts size res_discount
541
542                   | enough_args && size <= (n_vals_wanted + 1)
543                         -- Inline unconditionally if there no size increase
544                         -- Size of call is n_vals_wanted (+1 for the function)
545                   -> True
546
547                   | otherwise
548                   -> some_benefit && small_enough
549
550                   where
551                     some_benefit = or arg_infos || really_interesting_cont || 
552                                    (not is_top && (once || (n_vals_wanted > 0 && enough_args)))
553                 -- If it occurs more than once, there must be
554                 -- something interesting about some argument, or the
555                 -- result context, to make it worth inlining
556                 --
557                 -- If a function has a nested defn we also record
558                 -- some-benefit, on the grounds that we are often able
559                 -- to eliminate the binding, and hence the allocation,
560                 -- for the function altogether; this is good for join
561                 -- points.  But this only makes sense for *functions*;
562                 -- inlining a constructor doesn't help allocation
563                 -- unless the result is scrutinised.  UNLESS the
564                 -- constructor occurs just once, albeit possibly in
565                 -- multiple case branches.  Then inlining it doesn't
566                 -- increase allocation, but it does increase the
567                 -- chance that the constructor won't be allocated at
568                 -- all in the branches that don't use it.
569
570                     enough_args           = n_val_args >= n_vals_wanted
571                     really_interesting_cont | n_val_args <  n_vals_wanted = False       -- Too few args
572                                             | n_val_args == n_vals_wanted = interesting_cont
573                                             | otherwise                   = True        -- Extra args
574                         -- really_interesting_cont tells if the result of the
575                         -- call is in an interesting context.
576
577                     small_enough = (size - discount) <= opt_UF_UseThreshold
578                     discount     = computeDiscount n_vals_wanted arg_discounts res_discount 
579                                                  arg_infos really_interesting_cont
580                 
581     in    
582     if dopt Opt_D_dump_inlinings dflags then
583         pprTrace "Considering inlining"
584                  (ppr id <+> vcat [text "active:" <+> ppr active_inline,
585                                    text "occ info:" <+> ppr occ,
586                                    text "arg infos" <+> ppr arg_infos,
587                                    text "interesting continuation" <+> ppr interesting_cont,
588                                    text "is value:" <+> ppr is_value,
589                                    text "is cheap:" <+> ppr is_cheap,
590                                    text "guidance" <+> ppr guidance,
591                                    text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO"])
592                   result
593     else
594     result
595     }
596
597 computeDiscount :: Int -> [Int] -> Int -> [Bool] -> Bool -> Int
598 computeDiscount n_vals_wanted arg_discounts res_discount arg_infos result_used
599         -- We multiple the raw discounts (args_discount and result_discount)
600         -- ty opt_UnfoldingKeenessFactor because the former have to do with
601         --  *size* whereas the discounts imply that there's some extra 
602         --  *efficiency* to be gained (e.g. beta reductions, case reductions) 
603         -- by inlining.
604
605         -- we also discount 1 for each argument passed, because these will
606         -- reduce with the lambdas in the function (we count 1 for a lambda
607         -- in size_up).
608   = 1 +                 -- Discount of 1 because the result replaces the call
609                         -- so we count 1 for the function itself
610     length (take n_vals_wanted arg_infos) +
611                         -- Discount of 1 for each arg supplied, because the 
612                         -- result replaces the call
613     round (opt_UF_KeenessFactor * 
614            fromIntegral (arg_discount + result_discount))
615   where
616     arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)
617
618     mk_arg_discount discount is_evald | is_evald  = discount
619                                       | otherwise = 0
620
621         -- Don't give a result discount unless there are enough args
622     result_discount | result_used = res_discount        -- Over-applied, or case scrut
623                     | otherwise   = 0
624 \end{code}