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