f27289ec0e92901d72b931252ec1e7c23f4aa760
[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, -- types
18
19         noUnfolding, mkUnfolding, 
20         mkOtherCon, otherCons,
21         unfoldingTemplate, maybeUnfoldingTemplate,
22         isEvaldUnfolding, isCheapUnfolding,
23         hasUnfolding, hasSomeUnfolding,
24
25         couldBeSmallEnoughToInline, 
26         certainlySmallEnoughToInline, 
27         okToUnfoldInHiFile,
28
29         calcUnfoldingGuidance,
30
31         callSiteInline, blackListed
32     ) where
33
34 #include "HsVersions.h"
35
36 import CmdLineOpts      ( opt_UF_CreationThreshold,
37                           opt_UF_UseThreshold,
38                           opt_UF_ScrutConDiscount,
39                           opt_UF_FunAppDiscount,
40                           opt_UF_PrimArgDiscount,
41                           opt_UF_KeenessFactor,
42                           opt_UF_CheapOp, opt_UF_DearOp, opt_UF_NoRepLit,
43                           opt_UnfoldCasms, opt_PprStyle_Debug,
44                           opt_D_dump_inlinings
45                         )
46 import CoreSyn
47 import PprCore          ( pprCoreExpr )
48 import OccurAnal        ( occurAnalyseGlobalExpr )
49 import BinderInfo       ( )
50 import CoreUtils        ( coreExprType, exprIsTrivial, exprIsValue, exprIsCheap )
51 import Id               ( Id, idType, idUnique, isId, 
52                           getIdSpecialisation, getInlinePragma, getIdUnfolding
53                         )
54 import VarSet
55 import Name             ( isLocallyDefined )
56 import Const            ( Con(..), isLitLitLit, isWHNFCon )
57 import PrimOp           ( PrimOp(..), primOpIsDupable )
58 import IdInfo           ( ArityInfo(..), InlinePragInfo(..), OccInfo(..) )
59 import TyCon            ( tyConFamilySize )
60 import Type             ( splitAlgTyConApp_maybe, splitFunTy_maybe, isUnLiftedType )
61 import Const            ( isNoRepLit )
62 import Unique           ( Unique, buildIdKey, augmentIdKey, runSTRepIdKey )
63 import Maybes           ( maybeToBool )
64 import Bag
65 import Util             ( isIn, lengthExceeds )
66 import Outputable
67 \end{code}
68
69 %************************************************************************
70 %*                                                                      *
71 \subsection{@Unfolding@ and @UnfoldingGuidance@ types}
72 %*                                                                      *
73 %************************************************************************
74
75 \begin{code}
76 data Unfolding
77   = NoUnfolding
78
79   | OtherCon [Con]              -- It ain't one of these
80                                 -- (OtherCon xs) also indicates that something has been evaluated
81                                 -- and hence there's no point in re-evaluating it.
82                                 -- OtherCon [] is used even for non-data-type values
83                                 -- to indicated evaluated-ness.  Notably:
84                                 --      data C = C !(Int -> Int)
85                                 --      case x of { C f -> ... }
86                                 -- Here, f gets an OtherCon [] unfolding.
87
88   | CoreUnfolding                       -- An unfolding with redundant cached information
89                 CoreExpr                -- Template; binder-info is correct
90                 Bool                    -- exprIsCheap template (cached); it won't duplicate (much) work 
91                                         --      if you inline this in more than one place
92                 Bool                    -- exprIsValue template (cached); it is ok to discard a `seq` on
93                                         --      this variable
94                 UnfoldingGuidance       -- Tells about the *size* of the template.
95 \end{code}
96
97 \begin{code}
98 noUnfolding = NoUnfolding
99 mkOtherCon  = OtherCon
100
101 mkUnfolding expr
102   = CoreUnfolding (occurAnalyseGlobalExpr expr)
103                   (exprIsCheap expr)
104                   (exprIsValue expr)
105                   (calcUnfoldingGuidance opt_UF_CreationThreshold expr)
106
107 unfoldingTemplate :: Unfolding -> CoreExpr
108 unfoldingTemplate (CoreUnfolding expr _ _ _) = expr
109 unfoldingTemplate other = panic "getUnfoldingTemplate"
110
111 maybeUnfoldingTemplate :: Unfolding -> Maybe CoreExpr
112 maybeUnfoldingTemplate (CoreUnfolding expr _ _ _) = Just expr
113 maybeUnfoldingTemplate other                      = Nothing
114
115 otherCons (OtherCon cons) = cons
116 otherCons other           = []
117
118 isEvaldUnfolding :: Unfolding -> Bool
119 isEvaldUnfolding (OtherCon _)                   = True
120 isEvaldUnfolding (CoreUnfolding _ _ is_evald _) = is_evald
121 isEvaldUnfolding other                          = False
122
123 isCheapUnfolding :: Unfolding -> Bool
124 isCheapUnfolding (CoreUnfolding _ is_cheap _ _) = is_cheap
125 isCheapUnfolding other                          = False
126
127 hasUnfolding :: Unfolding -> Bool
128 hasUnfolding (CoreUnfolding _ _ _ _) = True
129 hasUnfolding other                   = False
130
131 hasSomeUnfolding :: Unfolding -> Bool
132 hasSomeUnfolding NoUnfolding = False
133 hasSomeUnfolding other       = True
134
135 data UnfoldingGuidance
136   = UnfoldNever
137   | UnfoldAlways                -- There is no "original" definition,
138                                 -- so you'd better unfold.  Or: something
139                                 -- so cheap to unfold (e.g., 1#) that
140                                 -- you should do it absolutely always.
141
142   | UnfoldIfGoodArgs    Int     -- and "n" value args
143
144                         [Int]   -- Discount if the argument is evaluated.
145                                 -- (i.e., a simplification will definitely
146                                 -- be possible).  One elt of the list per *value* arg.
147
148                         Int     -- The "size" of the unfolding; to be elaborated
149                                 -- later. ToDo
150
151                         Int     -- Scrutinee discount: the discount to substract if the thing is in
152                                 -- a context (case (thing args) of ...),
153                                 -- (where there are the right number of arguments.)
154 \end{code}
155
156 \begin{code}
157 instance Outputable UnfoldingGuidance where
158     ppr UnfoldAlways    = ptext SLIT("ALWAYS")
159     ppr UnfoldNever     = ptext SLIT("NEVER")
160     ppr (UnfoldIfGoodArgs v cs size discount)
161       = hsep [ptext SLIT("IF_ARGS"), int v,
162                if null cs       -- always print *something*
163                 then char 'X'
164                 else hcat (map (text . show) cs),
165                int size,
166                int discount ]
167 \end{code}
168
169
170 %************************************************************************
171 %*                                                                      *
172 \subsection[calcUnfoldingGuidance]{Calculate ``unfolding guidance'' for an expression}
173 %*                                                                      *
174 %************************************************************************
175
176 \begin{code}
177 calcUnfoldingGuidance
178         :: Int                  -- bomb out if size gets bigger than this
179         -> CoreExpr             -- expression to look at
180         -> UnfoldingGuidance
181 calcUnfoldingGuidance bOMB_OUT_SIZE expr
182   | exprIsTrivial expr          -- Often trivial expressions are never bound
183                                 -- to an expression, but it can happen.  For
184                                 -- example, the Id for a nullary constructor has
185                                 -- a trivial expression as its unfolding, and
186                                 -- we want to make sure that we always unfold it.
187   = UnfoldAlways
188  
189   | otherwise
190   = case collectBinders expr of { (binders, body) ->
191     let
192         val_binders = filter isId binders
193     in
194     case (sizeExpr bOMB_OUT_SIZE val_binders body) of
195
196       TooBig -> UnfoldNever
197
198       SizeIs size cased_args scrut_discount
199         -> UnfoldIfGoodArgs
200                         (length val_binders)
201                         (map discount_for val_binders)
202                         (I# size)
203                         (I# scrut_discount)
204         where        
205             discount_for b 
206                 | num_cases == 0 = 0
207                 | is_fun_ty      = num_cases * opt_UF_FunAppDiscount
208                 | is_data_ty     = num_cases * tyConFamilySize tycon * opt_UF_ScrutConDiscount
209                 | otherwise      = num_cases * opt_UF_PrimArgDiscount
210                 where
211                   num_cases           = foldlBag (\n b' -> if b==b' then n+1 else n) 0 cased_args
212                                         -- Count occurrences of b in cased_args
213                   arg_ty              = idType b
214                   is_fun_ty           = maybeToBool (splitFunTy_maybe arg_ty)
215                   (is_data_ty, tycon) = case (splitAlgTyConApp_maybe (idType b)) of
216                                           Nothing       -> (False, panic "discount")
217                                           Just (tc,_,_) -> (True,  tc)
218         }
219 \end{code}
220
221 \begin{code}
222 sizeExpr :: Int             -- Bomb out if it gets bigger than this
223          -> [Id]            -- Arguments; we're interested in which of these
224                             -- get case'd
225          -> CoreExpr
226          -> ExprSize
227
228 sizeExpr (I# bOMB_OUT_SIZE) args expr
229   = size_up expr
230   where
231     size_up (Type t)          = sizeZero        -- Types cost nothing
232     size_up (Var v)           = sizeOne
233
234     size_up (Note InlineMe _) = sizeTwo         -- The idea is that this is one more
235                                                 -- than the size of the "call" (i.e. 1)
236                                                 -- We want to reply "no" to noSizeIncrease
237                                                 -- for a bare reference (i.e. applied to no args) 
238                                                 -- to an INLINE thing
239
240     size_up (Note _ body)     = size_up body    -- Notes cost nothing
241
242     size_up (App fun (Type t))  = size_up fun
243     size_up (App fun arg)       = size_up_app fun [arg]
244
245     size_up (Con con args) = foldr (addSize . size_up) 
246                                    (size_up_con con args)
247                                    args
248
249     size_up (Lam b e) | isId b    = size_up e `addSizeN` 1
250                       | otherwise = size_up e
251
252     size_up (Let (NonRec binder rhs) body)
253       = nukeScrutDiscount (size_up rhs)         `addSize`
254         size_up body                            `addSizeN`
255         (if isUnLiftedType (idType binder) then 0 else 1)
256                 -- For the allocation
257                 -- If the binder has an unlifted type there is no allocation
258
259     size_up (Let (Rec pairs) body)
260       = nukeScrutDiscount rhs_size              `addSize`
261         size_up body                            `addSizeN`
262         length pairs            -- For the allocation
263       where
264         rhs_size = foldr (addSize . size_up . snd) sizeZero pairs
265
266     size_up (Case scrut _ alts)
267       = nukeScrutDiscount (size_up scrut)               `addSize`
268         arg_discount scrut                              `addSize`
269         foldr (addSize . size_up_alt) sizeZero alts     
270
271 -- Just charge for the alts that exist, not the ones that might exist
272 --      `addSizeN`
273 --      case (splitAlgTyConApp_maybe (coreExprType scrut)) of
274 --              Nothing       -> 1
275 --              Just (tc,_,_) -> tyConFamilySize tc
276
277     ------------ 
278     size_up_app (App fun arg) args   = size_up_app fun (arg:args)
279     size_up_app fun           args   = foldr (addSize . size_up) (fun_discount fun) args
280
281         -- A function application with at least one value argument
282         -- so if the function is an argument give it an arg-discount
283         -- Also behave specially if the function is a build
284     fun_discount (Var fun) | idUnique fun == buildIdKey   = buildSize
285                            | idUnique fun == augmentIdKey = augmentSize
286                            | fun `is_elem` args         = scrutArg fun
287     fun_discount other                                  = sizeZero
288
289     ------------ 
290     size_up_alt (con, bndrs, rhs) = size_up rhs
291             -- Don't charge for args, so that wrappers look cheap
292
293     ------------
294     size_up_con (Literal lit) args | isNoRepLit lit = sizeN opt_UF_NoRepLit
295                                    | otherwise      = sizeOne
296
297     size_up_con (DataCon dc) args = conSizeN (valArgCount args)
298                              
299     size_up_con (PrimOp op) args = foldr addSize (sizeN op_cost) (map arg_discount args)
300                 -- Give an arg-discount if a primop is applies to
301                 -- one of the function's arguments
302       where
303         op_cost | primOpIsDupable op = opt_UF_CheapOp
304                 | otherwise          = opt_UF_DearOp
305
306         -- We want to record if we're case'ing, or applying, an argument
307     arg_discount (Var v) | v `is_elem` args = scrutArg v
308     arg_discount other                      = sizeZero
309
310     ------------
311     is_elem :: Id -> [Id] -> Bool
312     is_elem = isIn "size_up_scrut"
313
314     ------------
315         -- These addSize things have to be here because
316         -- I don't want to give them bOMB_OUT_SIZE as an argument
317
318     addSizeN TooBig          _ = TooBig
319     addSizeN (SizeIs n xs d) (I# m)
320       | n_tot -# d <# bOMB_OUT_SIZE = SizeIs n_tot xs d
321       | otherwise                   = TooBig
322       where
323         n_tot = n +# m
324     
325     addSize TooBig _ = TooBig
326     addSize _ TooBig = TooBig
327     addSize (SizeIs n1 xs d1) (SizeIs n2 ys d2)
328       | (n_tot -# d_tot) <# bOMB_OUT_SIZE = SizeIs n_tot xys d_tot
329       | otherwise                         = TooBig
330       where
331         n_tot = n1 +# n2
332         d_tot = d1 +# d2
333         xys   = xs `unionBags` ys
334 \end{code}
335
336 Code for manipulating sizes
337
338 \begin{code}
339
340 data ExprSize = TooBig
341               | SizeIs Int#     -- Size found
342                        (Bag Id) -- Arguments cased herein
343                        Int#     -- Size to subtract if result is scrutinised 
344                                 -- by a case expression
345
346 sizeZero        = SizeIs 0# emptyBag 0#
347 sizeOne         = SizeIs 1# emptyBag 0#
348 sizeTwo         = SizeIs 2# emptyBag 0#
349 sizeN (I# n)    = SizeIs n  emptyBag 0#
350 conSizeN (I# n) = SizeIs 1# emptyBag (n +# 1#)
351         -- Treat constructors as size 1, that unfoldAlways responsds 'False'
352         -- when asked about 'x' when x is bound to (C 3#).
353         -- This avoids gratuitous 'ticks' when x itself appears as an
354         -- atomic constructor argument.
355
356 buildSize = SizeIs (-2#) emptyBag 4#
357         -- We really want to inline applications of build
358         -- build t (\cn -> e) should cost only the cost of e (because build will be inlined later)
359         -- Indeed, we should add a result_discount becuause build is 
360         -- very like a constructor.  We don't bother to check that the
361         -- build is saturated (it usually is).  The "-2" discounts for the \c n, 
362         -- The "4" is rather arbitrary.
363
364 augmentSize = SizeIs (-2#) emptyBag 4#
365         -- Ditto (augment t (\cn -> e) ys) should cost only the cost of
366         -- e plus ys. The -2 accounts for the \cn 
367                                                 
368 scrutArg v      = SizeIs 0# (unitBag v) 0#
369
370 nukeScrutDiscount (SizeIs n vs d) = SizeIs n vs 0#
371 nukeScrutDiscount TooBig          = TooBig
372 \end{code}
373
374
375 %************************************************************************
376 %*                                                                      *
377 \subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
378 %*                                                                      *
379 %************************************************************************
380
381 We have very limited information about an unfolding expression: (1)~so
382 many type arguments and so many value arguments expected---for our
383 purposes here, we assume we've got those.  (2)~A ``size'' or ``cost,''
384 a single integer.  (3)~An ``argument info'' vector.  For this, what we
385 have at the moment is a Boolean per argument position that says, ``I
386 will look with great favour on an explicit constructor in this
387 position.'' (4)~The ``discount'' to subtract if the expression
388 is being scrutinised. 
389
390 Assuming we have enough type- and value arguments (if not, we give up
391 immediately), then we see if the ``discounted size'' is below some
392 (semi-arbitrary) threshold.  It works like this: for every argument
393 position where we're looking for a constructor AND WE HAVE ONE in our
394 hands, we get a (again, semi-arbitrary) discount [proportion to the
395 number of constructors in the type being scrutinized].
396
397 If we're in the context of a scrutinee ( \tr{(case <expr > of A .. -> ...;.. )})
398 and the expression in question will evaluate to a constructor, we use
399 the computed discount size *for the result only* rather than
400 computing the argument discounts. Since we know the result of
401 the expression is going to be taken apart, discounting its size
402 is more accurate (see @sizeExpr@ above for how this discount size
403 is computed).
404
405 We use this one to avoid exporting inlinings that we ``couldn't possibly
406 use'' on the other side.  Can be overridden w/ flaggery.
407 Just the same as smallEnoughToInline, except that it has no actual arguments.
408
409 \begin{code}
410 couldBeSmallEnoughToInline :: UnfoldingGuidance -> Bool
411 couldBeSmallEnoughToInline UnfoldNever = False
412 couldBeSmallEnoughToInline other       = True
413
414 certainlySmallEnoughToInline :: UnfoldingGuidance -> Bool
415 certainlySmallEnoughToInline UnfoldNever                   = False
416 certainlySmallEnoughToInline UnfoldAlways                  = True
417 certainlySmallEnoughToInline (UnfoldIfGoodArgs _ _ size _) = size <= opt_UF_UseThreshold
418 \end{code}
419
420 @okToUnfoldInHifile@ is used when emitting unfolding info into an interface
421 file to determine whether an unfolding candidate really should be unfolded.
422 The predicate is needed to prevent @_casm_@s (+ lit-lits) from being emitted
423 into interface files. 
424
425 The reason for inlining expressions containing _casm_s into interface files
426 is that these fragments of C are likely to mention functions/#defines that
427 will be out-of-scope when inlined into another module. This is not an
428 unfixable problem for the user (just need to -#include the approp. header
429 file), but turning it off seems to the simplest thing to do.
430
431 \begin{code}
432 okToUnfoldInHiFile :: CoreExpr -> Bool
433 okToUnfoldInHiFile e = opt_UnfoldCasms || go e
434  where
435     -- Race over an expression looking for CCalls..
436     go (Var _)                = True
437     go (Con (Literal lit) _)  = not (isLitLitLit lit)
438     go (Con (PrimOp op) args) = okToUnfoldPrimOp op && all go args
439     go (Con con args)         = True -- con args are always atomic
440     go (App fun arg)          = go fun && go arg
441     go (Lam _ body)           = go body
442     go (Let binds body)       = and (map go (body :rhssOfBind binds))
443     go (Case scrut bndr alts) = and (map go (scrut:rhssOfAlts alts))
444     go (Note _ body)          = go body
445     go (Type _)               = True
446
447     -- ok to unfold a PrimOp as long as it's not a _casm_
448     okToUnfoldPrimOp (CCallOp _ is_casm _ _) = not is_casm
449     okToUnfoldPrimOp _                       = True
450 \end{code}
451
452
453 %************************************************************************
454 %*                                                                      *
455 \subsection{callSiteInline}
456 %*                                                                      *
457 %************************************************************************
458
459 This is the key function.  It decides whether to inline a variable at a call site
460
461 callSiteInline is used at call sites, so it is a bit more generous.
462 It's a very important function that embodies lots of heuristics.
463 A non-WHNF can be inlined if it doesn't occur inside a lambda,
464 and occurs exactly once or 
465     occurs once in each branch of a case and is small
466
467 If the thing is in WHNF, there's no danger of duplicating work, 
468 so we can inline if it occurs once, or is small
469
470 \begin{code}
471 callSiteInline :: Bool                  -- True <=> the Id is black listed
472                -> Bool                  -- 'inline' note at call site
473                -> Id                    -- The Id
474                -> [Bool]                -- One for each value arg; True if it is interesting
475                -> Bool                  -- True <=> continuation is interesting
476                -> Maybe CoreExpr        -- Unfolding, if any
477
478
479 callSiteInline black_listed inline_call id arg_infos interesting_cont
480   = case getIdUnfolding id of {
481         NoUnfolding -> Nothing ;
482         OtherCon _  -> Nothing ;
483         CoreUnfolding unf_template is_cheap _ guidance ->
484
485     let
486         result | yes_or_no = Just unf_template
487                | otherwise = Nothing
488
489         inline_prag = getInlinePragma id
490         n_val_args  = length arg_infos
491
492         yes_or_no =
493             case inline_prag of
494                 IAmDead           -> pprTrace "callSiteInline: dead" (ppr id) False
495                 IMustNotBeINLINEd -> False
496                 IAmALoopBreaker   -> False
497                 IMustBeINLINEd    -> True       -- Overrides absolutely everything, including the black list
498                 ICanSafelyBeINLINEd in_lam one_br -> consider in_lam    True  one_br
499                 NoInlinePragInfo                  -> consider InsideLam False False
500
501         consider in_lam once once_in_one_branch
502           | black_listed = False
503           | inline_call  = True
504           | once_in_one_branch  -- Be very keen to inline something if this is its unique occurrence; that
505                                 -- gives a good chance of eliminating the original binding for the thing.
506                                 -- The only time we hold back is when substituting inside a lambda;
507                                 -- then if the context is totally uninteresting (not applied, not scrutinised)
508                                 -- there is no point in substituting because it might just increase allocation.
509           = WARN( case in_lam of { NotInsideLam -> True; other -> False },
510                   text "callSiteInline:oneOcc" <+> ppr id )
511                 -- If it has one occurrence, not inside a lambda, PreInlineUnconditionally
512                 -- should have zapped it already
513             is_cheap && (not (null arg_infos) || interesting_cont)
514
515           | otherwise   -- Occurs (textually) more than once, so look at its size
516           = case guidance of
517               UnfoldAlways -> True
518               UnfoldNever  -> False
519               UnfoldIfGoodArgs n_vals_wanted arg_discounts size res_discount
520                 | enough_args && size <= (n_vals_wanted + 1)
521                         -- No size increase
522                         -- Size of call is n_vals_wanted (+1 for the function)
523                 -> case in_lam of
524                         NotInsideLam -> True
525                         InsideLam    -> is_cheap
526
527                 | not (or arg_infos || really_interesting_cont || once)
528                         -- If it occurs more than once, there must be something interesting 
529                         -- about some argument, or the result, to make it worth inlining
530                         -- We also drop this case if the thing occurs once, although perhaps in 
531                         -- several branches.  In this case we are keener about inlining in the hope
532                         -- that we'll be able to drop the allocation for the function altogether.
533                 -> False
534   
535                 | otherwise
536                 -> case in_lam of
537                         NotInsideLam -> small_enough
538                         InsideLam    -> is_cheap && small_enough
539
540                 where
541                   enough_args             = n_val_args >= n_vals_wanted
542                   really_interesting_cont | n_val_args <  n_vals_wanted = False -- Too few args
543                                           | n_val_args == n_vals_wanted = interesting_cont
544                                           | otherwise                   = True  -- Extra args
545                         -- This rather elaborate defn for really_interesting_cont is important
546                         -- Consider an I# = INLINE (\x -> I# {x})
547                         -- The unfolding guidance deems it to have size 2, and no arguments.
548                         -- So in an application (I# y) we must take the extra arg 'y' as
549                         -- evidence of an interesting context!
550                         
551                   small_enough = (size - discount) <= opt_UF_UseThreshold
552                   discount     = computeDiscount n_vals_wanted arg_discounts res_discount 
553                                                  arg_infos really_interesting_cont
554
555                                 
556     in    
557 #ifdef DEBUG
558     if opt_D_dump_inlinings then
559         pprTrace "Considering inlining"
560                  (ppr id <+> vcat [text "black listed" <+> ppr black_listed,
561                                    text "inline prag:" <+> ppr inline_prag,
562                                    text "arg infos" <+> ppr arg_infos,
563                                    text "interesting continuation" <+> ppr interesting_cont,
564                                    text "is cheap" <+> ppr is_cheap,
565                                    text "guidance" <+> ppr guidance,
566                                    text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO",
567                                    if yes_or_no then
568                                         text "Unfolding =" <+> pprCoreExpr unf_template
569                                    else empty])
570                   result
571     else
572 #endif
573     result
574     }
575
576 computeDiscount :: Int -> [Int] -> Int -> [Bool] -> Bool -> Int
577 computeDiscount n_vals_wanted arg_discounts res_discount arg_infos result_used
578         -- We multiple the raw discounts (args_discount and result_discount)
579         -- ty opt_UnfoldingKeenessFactor because the former have to do with
580         -- *size* whereas the discounts imply that there's some extra 
581         -- *efficiency* to be gained (e.g. beta reductions, case reductions) 
582         -- by inlining.
583
584         -- we also discount 1 for each argument passed, because these will
585         -- reduce with the lambdas in the function (we count 1 for a lambda
586         -- in size_up).
587   = length (take n_vals_wanted arg_infos) +
588                         -- Discount of 1 for each arg supplied, because the 
589                         -- result replaces the call
590     round (opt_UF_KeenessFactor * 
591            fromInt (arg_discount + result_discount))
592   where
593     arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)
594
595     mk_arg_discount discount is_evald | is_evald  = discount
596                                       | otherwise = 0
597
598         -- Don't give a result discount unless there are enough args
599     result_discount | result_used = res_discount        -- Over-applied, or case scrut
600                     | otherwise   = 0
601 \end{code}
602
603
604 %************************************************************************
605 %*                                                                      *
606 \subsection{Black-listing}
607 %*                                                                      *
608 %************************************************************************
609
610 Inlining is controlled by the "Inline phase" number, which is set
611 by the per-simplification-pass '-finline-phase' flag.
612
613 For optimisation we use phase 1,2 and nothing (i.e. no -finline-phase flag)
614 in that order.  The meanings of these are determined by the @blackListed@ function
615 here.
616
617 \begin{code}
618 blackListed :: IdSet            -- Used in transformation rules
619             -> Maybe Int        -- Inline phase
620             -> Id -> Bool       -- True <=> blacklisted
621         
622 -- The blackListed function sees whether a variable should *not* be 
623 -- inlined because of the inline phase we are in.  This is the sole
624 -- place that the inline phase number is looked at.
625
626 -- Phase 0: used for 'no imported inlinings please'
627 -- This prevents wrappers getting inlined which in turn is bad for full laziness
628 blackListed rule_vars (Just 0)
629   = \v -> not (isLocallyDefined v)
630
631 -- Phase 1: don't inline any rule-y things or things with specialisations
632 blackListed rule_vars (Just 1)
633   = \v -> let v_uniq = idUnique v
634           in v `elemVarSet` rule_vars
635           || not (isEmptyCoreRules (getIdSpecialisation v))
636           || v_uniq == runSTRepIdKey
637
638 -- Phase 2: allow build/augment to inline, and specialisations
639 blackListed rule_vars (Just 2)
640   = \v -> let v_uniq = idUnique v
641           in (v `elemVarSet` rule_vars && not (v_uniq == buildIdKey || 
642                                                v_uniq == augmentIdKey))
643           || v_uniq == runSTRepIdKey
644
645 -- Otherwise just go for it
646 blackListed rule_vars phase
647   = \v -> False
648 \end{code}
649
650
651 SLPJ 95/04: Why @runST@ must be inlined very late:
652 \begin{verbatim}
653 f x =
654   runST ( \ s -> let
655                     (a, s')  = newArray# 100 [] s
656                     (_, s'') = fill_in_array_or_something a x s'
657                   in
658                   freezeArray# a s'' )
659 \end{verbatim}
660 If we inline @runST@, we'll get:
661 \begin{verbatim}
662 f x = let
663         (a, s')  = newArray# 100 [] realWorld#{-NB-}
664         (_, s'') = fill_in_array_or_something a x s'
665       in
666       freezeArray# a s''
667 \end{verbatim}
668 And now the @newArray#@ binding can be floated to become a CAF, which
669 is totally and utterly wrong:
670 \begin{verbatim}
671 f = let
672     (a, s')  = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
673     in
674     \ x ->
675         let (_, s'') = fill_in_array_or_something a x s' in
676         freezeArray# a s''
677 \end{verbatim}
678 All calls to @f@ will share a {\em single} array!  
679
680 Yet we do want to inline runST sometime, so we can avoid
681 needless code.  Solution: black list it until the last moment.
682