2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[CoreUnfold]{Core-syntax unfoldings}
6 Unfoldings (which can travel across module boundaries) are in Core
7 syntax (namely @CoreExpr@s).
9 The type @Unfolding@ sits ``above'' simply-Core-expressions
10 unfoldings, capturing ``higher-level'' things we know about a binding,
11 usually things that the simplifier found out (e.g., ``it's a
12 literal''). In the corner of a @CoreUnfolding@ unfolding, you will
13 find, unsurprisingly, a Core expression.
17 Unfolding(..), UnfoldingGuidance, -- types
19 noUnfolding, mkUnfolding, getUnfoldingTemplate,
20 isEvaldUnfolding, hasUnfolding,
22 couldBeSmallEnoughToInline,
23 certainlySmallEnoughToInline,
26 calcUnfoldingGuidance,
28 callSiteInline, blackListed
31 #include "HsVersions.h"
33 import CmdLineOpts ( opt_UF_CreationThreshold,
35 opt_UF_ScrutConDiscount,
36 opt_UF_FunAppDiscount,
37 opt_UF_PrimArgDiscount,
39 opt_UF_CheapOp, opt_UF_DearOp, opt_UF_NoRepLit,
40 opt_UnfoldCasms, opt_PprStyle_Debug,
44 import PprCore ( pprCoreExpr )
45 import CoreUtils ( whnfOrBottom )
46 import OccurAnal ( occurAnalyseGlobalExpr )
48 import CoreUtils ( coreExprType, exprIsTrivial, mkFormSummary,
50 import Id ( Id, idType, idUnique, isId,
51 getIdSpecialisation, getInlinePragma, getIdUnfolding
54 import Const ( Con(..), isLitLitLit, isWHNFCon )
55 import PrimOp ( PrimOp(..), primOpIsDupable )
56 import IdInfo ( ArityInfo(..), InlinePragInfo(..), OccInfo(..) )
57 import TyCon ( tyConFamilySize )
58 import Type ( splitAlgTyConApp_maybe, splitFunTy_maybe )
59 import Const ( isNoRepLit )
60 import Unique ( Unique, buildIdKey, augmentIdKey, runSTRepIdKey )
61 import Maybes ( maybeToBool )
63 import Util ( isIn, lengthExceeds )
67 %************************************************************************
69 \subsection{@Unfolding@ and @UnfoldingGuidance@ types}
71 %************************************************************************
77 | OtherCon [Con] -- It ain't one of these
78 -- (OtherCon xs) also indicates that something has been evaluated
79 -- and hence there's no point in re-evaluating it.
80 -- OtherCon [] is used even for non-data-type values
81 -- to indicated evaluated-ness. Notably:
82 -- data C = C !(Int -> Int)
83 -- case x of { C f -> ... }
84 -- Here, f gets an OtherCon [] unfolding.
86 | CoreUnfolding -- An unfolding with redundant cached information
87 FormSummary -- Tells whether the template is a WHNF or bottom
88 UnfoldingGuidance -- Tells about the *size* of the template.
89 CoreExpr -- Template; binder-info is correct
93 noUnfolding = NoUnfolding
97 -- strictness mangling (depends on there being no CSE)
98 ufg = calcUnfoldingGuidance opt_UF_CreationThreshold expr
99 occ = occurAnalyseGlobalExpr expr
101 CoreUnfolding (mkFormSummary expr) ufg occ
103 getUnfoldingTemplate :: Unfolding -> CoreExpr
104 getUnfoldingTemplate (CoreUnfolding _ _ expr) = expr
105 getUnfoldingTemplate other = panic "getUnfoldingTemplate"
107 isEvaldUnfolding :: Unfolding -> Bool
108 isEvaldUnfolding (OtherCon _) = True
109 isEvaldUnfolding (CoreUnfolding ValueForm _ expr) = True
110 isEvaldUnfolding other = False
112 hasUnfolding :: Unfolding -> Bool
113 hasUnfolding NoUnfolding = False
114 hasUnfolding other = True
116 data UnfoldingGuidance
118 | UnfoldAlways -- There is no "original" definition,
119 -- so you'd better unfold. Or: something
120 -- so cheap to unfold (e.g., 1#) that
121 -- you should do it absolutely always.
123 | UnfoldIfGoodArgs Int -- and "n" value args
125 [Int] -- Discount if the argument is evaluated.
126 -- (i.e., a simplification will definitely
127 -- be possible). One elt of the list per *value* arg.
129 Int -- The "size" of the unfolding; to be elaborated
132 Int -- Scrutinee discount: the discount to substract if the thing is in
133 -- a context (case (thing args) of ...),
134 -- (where there are the right number of arguments.)
138 instance Outputable UnfoldingGuidance where
139 ppr UnfoldAlways = ptext SLIT("ALWAYS")
140 ppr UnfoldNever = ptext SLIT("NEVER")
141 ppr (UnfoldIfGoodArgs v cs size discount)
142 = hsep [ptext SLIT("IF_ARGS"), int v,
143 if null cs -- always print *something*
145 else hcat (map (text . show) cs),
151 %************************************************************************
153 \subsection[calcUnfoldingGuidance]{Calculate ``unfolding guidance'' for an expression}
155 %************************************************************************
158 calcUnfoldingGuidance
159 :: Int -- bomb out if size gets bigger than this
160 -> CoreExpr -- expression to look at
162 calcUnfoldingGuidance bOMB_OUT_SIZE expr
163 | exprIsTrivial expr -- Often trivial expressions are never bound
164 -- to an expression, but it can happen. For
165 -- example, the Id for a nullary constructor has
166 -- a trivial expression as its unfolding, and
167 -- we want to make sure that we always unfold it.
171 = case collectBinders expr of { (binders, body) ->
173 val_binders = filter isId binders
175 case (sizeExpr bOMB_OUT_SIZE val_binders body) of
177 TooBig -> UnfoldNever
179 SizeIs size cased_args scrut_discount
182 (map discount_for val_binders)
188 | is_fun_ty = num_cases * opt_UF_FunAppDiscount
189 | is_data_ty = num_cases * tyConFamilySize tycon * opt_UF_ScrutConDiscount
190 | otherwise = num_cases * opt_UF_PrimArgDiscount
192 num_cases = foldlBag (\n b' -> if b==b' then n+1 else n) 0 cased_args
193 -- Count occurrences of b in cased_args
195 is_fun_ty = maybeToBool (splitFunTy_maybe arg_ty)
196 (is_data_ty, tycon) = case (splitAlgTyConApp_maybe (idType b)) of
197 Nothing -> (False, panic "discount")
198 Just (tc,_,_) -> (True, tc)
203 sizeExpr :: Int -- Bomb out if it gets bigger than this
204 -> [Id] -- Arguments; we're interested in which of these
209 sizeExpr (I# bOMB_OUT_SIZE) args expr
212 size_up (Type t) = sizeZero -- Types cost nothing
213 size_up (Var v) = sizeOne
215 size_up (Note InlineMe _) = sizeTwo -- The idea is that this is one more
216 -- than the size of the "call" (i.e. 1)
217 -- We want to reply "no" to noSizeIncrease
218 -- for a bare reference (i.e. applied to no args)
219 -- to an INLINE thing
221 size_up (Note _ body) = size_up body -- Notes cost nothing
223 size_up (App fun (Type t)) = size_up fun
224 size_up (App fun arg) = size_up_app fun `addSize` size_up arg
226 size_up (Con con args) = foldr (addSize . size_up)
227 (size_up_con con args)
230 size_up (Lam b e) | isId b = size_up e `addSizeN` 1
231 | otherwise = size_up e
233 size_up (Let (NonRec binder rhs) body)
234 = nukeScrutDiscount (size_up rhs) `addSize`
235 size_up body `addSizeN`
236 1 -- For the allocation
238 size_up (Let (Rec pairs) body)
239 = nukeScrutDiscount rhs_size `addSize`
240 size_up body `addSizeN`
241 length pairs -- For the allocation
243 rhs_size = foldr (addSize . size_up . snd) sizeZero pairs
245 size_up (Case scrut _ alts)
246 = nukeScrutDiscount (size_up scrut) `addSize`
247 arg_discount scrut `addSize`
248 foldr (addSize . size_up_alt) sizeZero alts `addSizeN`
249 case (splitAlgTyConApp_maybe (coreExprType scrut)) of
251 Just (tc,_,_) -> tyConFamilySize tc
254 -- A function application with at least one value argument
255 -- so if the function is an argument give it an arg-discount
256 size_up_app (App fun arg) = size_up_app fun `addSize` size_up arg
257 size_up_app fun = arg_discount fun `addSize` size_up fun
260 size_up_alt (con, bndrs, rhs) = size_up rhs
261 -- Don't charge for args, so that wrappers look cheap
264 size_up_con (Literal lit) args | isNoRepLit lit = sizeN opt_UF_NoRepLit
265 | otherwise = sizeOne
267 size_up_con (DataCon dc) args = conSizeN (valArgCount args)
269 size_up_con (PrimOp op) args = foldr addSize (sizeN op_cost) (map arg_discount args)
270 -- Give an arg-discount if a primop is applies to
271 -- one of the function's arguments
273 op_cost | primOpIsDupable op = opt_UF_CheapOp
274 | otherwise = opt_UF_DearOp
277 -- We want to record if we're case'ing, or applying, an argument
278 arg_discount (Var v) | v `is_elem` args = scrutArg v
279 arg_discount other = sizeZero
281 is_elem :: Id -> [Id] -> Bool
282 is_elem = isIn "size_up_scrut"
285 -- These addSize things have to be here because
286 -- I don't want to give them bOMB_OUT_SIZE as an argument
288 addSizeN TooBig _ = TooBig
289 addSizeN (SizeIs n xs d) (I# m)
290 | n_tot -# d <# bOMB_OUT_SIZE = SizeIs n_tot xs d
295 addSize TooBig _ = TooBig
296 addSize _ TooBig = TooBig
297 addSize (SizeIs n1 xs d1) (SizeIs n2 ys d2)
298 | (n_tot -# d_tot) <# bOMB_OUT_SIZE = SizeIs n_tot xys d_tot
303 xys = xs `unionBags` ys
306 Code for manipulating sizes
310 data ExprSize = TooBig
311 | SizeIs Int# -- Size found
312 (Bag Id) -- Arguments cased herein
313 Int# -- Size to subtract if result is scrutinised
314 -- by a case expression
316 sizeZero = SizeIs 0# emptyBag 0#
317 sizeOne = SizeIs 1# emptyBag 0#
318 sizeTwo = SizeIs 2# emptyBag 0#
319 sizeN (I# n) = SizeIs n emptyBag 0#
320 conSizeN (I# n) = SizeIs 1# emptyBag (n +# 1#)
321 -- Treat constructors as size 1, that unfoldAlways responsds 'False'
322 -- when asked about 'x' when x is bound to (C 3#).
323 -- This avoids gratuitous 'ticks' when x itself appears as an
324 -- atomic constructor argument.
326 scrutArg v = SizeIs 0# (unitBag v) 0#
328 nukeScrutDiscount (SizeIs n vs d) = SizeIs n vs 0#
329 nukeScrutDiscount TooBig = TooBig
333 %************************************************************************
335 \subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
337 %************************************************************************
339 We have very limited information about an unfolding expression: (1)~so
340 many type arguments and so many value arguments expected---for our
341 purposes here, we assume we've got those. (2)~A ``size'' or ``cost,''
342 a single integer. (3)~An ``argument info'' vector. For this, what we
343 have at the moment is a Boolean per argument position that says, ``I
344 will look with great favour on an explicit constructor in this
345 position.'' (4)~The ``discount'' to subtract if the expression
346 is being scrutinised.
348 Assuming we have enough type- and value arguments (if not, we give up
349 immediately), then we see if the ``discounted size'' is below some
350 (semi-arbitrary) threshold. It works like this: for every argument
351 position where we're looking for a constructor AND WE HAVE ONE in our
352 hands, we get a (again, semi-arbitrary) discount [proportion to the
353 number of constructors in the type being scrutinized].
355 If we're in the context of a scrutinee ( \tr{(case <expr > of A .. -> ...;.. )})
356 and the expression in question will evaluate to a constructor, we use
357 the computed discount size *for the result only* rather than
358 computing the argument discounts. Since we know the result of
359 the expression is going to be taken apart, discounting its size
360 is more accurate (see @sizeExpr@ above for how this discount size
363 We use this one to avoid exporting inlinings that we ``couldn't possibly
364 use'' on the other side. Can be overridden w/ flaggery.
365 Just the same as smallEnoughToInline, except that it has no actual arguments.
368 couldBeSmallEnoughToInline :: UnfoldingGuidance -> Bool
369 couldBeSmallEnoughToInline UnfoldNever = False
370 couldBeSmallEnoughToInline other = True
372 certainlySmallEnoughToInline :: UnfoldingGuidance -> Bool
373 certainlySmallEnoughToInline UnfoldNever = False
374 certainlySmallEnoughToInline UnfoldAlways = True
375 certainlySmallEnoughToInline (UnfoldIfGoodArgs _ _ size _) = size <= opt_UF_UseThreshold
378 @okToUnfoldInHifile@ is used when emitting unfolding info into an interface
379 file to determine whether an unfolding candidate really should be unfolded.
380 The predicate is needed to prevent @_casm_@s (+ lit-lits) from being emitted
381 into interface files.
383 The reason for inlining expressions containing _casm_s into interface files
384 is that these fragments of C are likely to mention functions/#defines that
385 will be out-of-scope when inlined into another module. This is not an
386 unfixable problem for the user (just need to -#include the approp. header
387 file), but turning it off seems to the simplest thing to do.
390 okToUnfoldInHiFile :: CoreExpr -> Bool
391 okToUnfoldInHiFile e = opt_UnfoldCasms || go e
393 -- Race over an expression looking for CCalls..
395 go (Con (Literal lit) _) = not (isLitLitLit lit)
396 go (Con (PrimOp op) args) = okToUnfoldPrimOp op && all go args
397 go (Con con args) = True -- con args are always atomic
398 go (App fun arg) = go fun && go arg
399 go (Lam _ body) = go body
400 go (Let binds body) = and (map go (body :rhssOfBind binds))
401 go (Case scrut bndr alts) = and (map go (scrut:rhssOfAlts alts))
402 go (Note _ body) = go body
405 -- ok to unfold a PrimOp as long as it's not a _casm_
406 okToUnfoldPrimOp (CCallOp _ is_casm _ _) = not is_casm
407 okToUnfoldPrimOp _ = True
411 %************************************************************************
413 \subsection{callSiteInline}
415 %************************************************************************
417 This is the key function. It decides whether to inline a variable at a call site
419 callSiteInline is used at call sites, so it is a bit more generous.
420 It's a very important function that embodies lots of heuristics.
421 A non-WHNF can be inlined if it doesn't occur inside a lambda,
422 and occurs exactly once or
423 occurs once in each branch of a case and is small
425 If the thing is in WHNF, there's no danger of duplicating work,
426 so we can inline if it occurs once, or is small
429 callSiteInline :: Bool -- True <=> the Id is black listed
430 -> Bool -- 'inline' note at call site
432 -> [CoreExpr] -- Arguments
433 -> Bool -- True <=> continuation is interesting
434 -> Maybe CoreExpr -- Unfolding, if any
437 callSiteInline black_listed inline_call id args interesting_cont
438 = case getIdUnfolding id of {
439 NoUnfolding -> Nothing ;
440 OtherCon _ -> Nothing ;
441 CoreUnfolding form guidance unf_template ->
444 result | yes_or_no = Just unf_template
445 | otherwise = Nothing
447 inline_prag = getInlinePragma id
448 arg_infos = map interestingArg val_args
449 val_args = filter isValArg args
450 whnf = whnfOrBottom form
454 IAmDead -> pprTrace "callSiteInline: dead" (ppr id) False
455 IMustNotBeINLINEd -> False
456 IAmALoopBreaker -> False
457 IMustBeINLINEd -> True -- Overrides absolutely everything, including the black list
458 ICanSafelyBeINLINEd in_lam one_br -> consider in_lam one_br
459 NoInlinePragInfo -> consider InsideLam False
461 consider in_lam one_branch
462 | black_listed = False
464 | one_branch -- Be very keen to inline something if this is its unique occurrence; that
465 -- gives a good chance of eliminating the original binding for the thing.
466 -- The only time we hold back is when substituting inside a lambda;
467 -- then if the context is totally uninteresting (not applied, not scrutinised)
468 -- there is no point in substituting because it might just increase allocation.
471 InsideLam -> whnf && (not (null args) || interesting_cont)
473 | otherwise -- Occurs (textually) more than once, so look at its size
477 UnfoldIfGoodArgs n_vals_wanted arg_discounts size res_discount
478 | enough_args && size <= (n_vals_wanted + 1)
480 -- Size of call is n_vals_wanted (+1 for the function)
485 | not (or arg_infos || really_interesting_cont)
486 -- If it occurs more than once, there must be something interesting
487 -- about some argument, or the result, to make it worth inlining
492 NotInsideLam -> small_enough
493 InsideLam -> whnf && small_enough
496 n_args = length arg_infos
497 enough_args = n_args >= n_vals_wanted
498 really_interesting_cont | n_args < n_vals_wanted = False -- Too few args
499 | n_args == n_vals_wanted = interesting_cont
500 | otherwise = True -- Extra args
501 -- This rather elaborate defn for really_interesting_cont is important
502 -- Consider an I# = INLINE (\x -> I# {x})
503 -- The unfolding guidance deems it to have size 2, and no arguments.
504 -- So in an application (I# y) we must take the extra arg 'y' as
505 -- evidene of an interesting context!
507 small_enough = (size - discount) <= opt_UF_UseThreshold
508 discount = computeDiscount n_vals_wanted arg_discounts res_discount
509 arg_infos really_interesting_cont
514 if opt_D_dump_inlinings then
515 pprTrace "Considering inlining"
516 (ppr id <+> vcat [text "black listed" <+> ppr black_listed,
517 text "inline prag:" <+> ppr inline_prag,
518 text "arg infos" <+> ppr arg_infos,
519 text "interesting continuation" <+> ppr interesting_cont,
520 text "whnf" <+> ppr whnf,
521 text "guidance" <+> ppr guidance,
522 text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO",
524 text "Unfolding =" <+> pprCoreExpr unf_template
532 -- An argument is interesting if it has *some* structure
533 -- We are here trying to avoid unfolding a function that
534 -- is applied only to variables that have no unfolding
535 -- (i.e. they are probably lambda bound): f x y z
536 -- There is little point in inlining f here.
537 interestingArg (Type _) = False
538 interestingArg (App fn (Type _)) = interestingArg fn
539 interestingArg (Var v) = hasUnfolding (getIdUnfolding v)
540 interestingArg other = True
543 computeDiscount :: Int -> [Int] -> Int -> [Bool] -> Bool -> Int
544 computeDiscount n_vals_wanted arg_discounts res_discount arg_infos result_used
545 -- We multiple the raw discounts (args_discount and result_discount)
546 -- ty opt_UnfoldingKeenessFactor because the former have to do with
547 -- *size* whereas the discounts imply that there's some extra
548 -- *efficiency* to be gained (e.g. beta reductions, case reductions)
551 -- we also discount 1 for each argument passed, because these will
552 -- reduce with the lambdas in the function (we count 1 for a lambda
554 = length (take n_vals_wanted arg_infos) +
555 -- Discount of 1 for each arg supplied, because the
556 -- result replaces the call
557 round (opt_UF_KeenessFactor *
558 fromInt (arg_discount + result_discount))
560 arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)
562 mk_arg_discount discount is_evald | is_evald = discount
565 -- Don't give a result discount unless there are enough args
566 result_discount | result_used = res_discount -- Over-applied, or case scrut
571 %************************************************************************
573 \subsection{Black-listing}
575 %************************************************************************
577 Inlining is controlled by the "Inline phase" number, which is set
578 by the per-simplification-pass '-finline-phase' flag.
580 For optimisation we use phase 1,2 and nothing (i.e. no -finline-phase flag)
581 in that order. The meanings of these are determined by the @blackListed@ function
585 blackListed :: IdSet -- Used in transformation rules
586 -> Maybe Int -- Inline phase
587 -> Id -> Bool -- True <=> blacklisted
589 -- The blackListed function sees whether a variable should *not* be
590 -- inlined because of the inline phase we are in. This is the sole
591 -- place that the inline phase number is looked at.
593 -- Phase 0: used for 'no inlinings please'
594 blackListed rule_vars (Just 0)
597 -- Phase 1: don't inline any rule-y things or things with specialisations
598 blackListed rule_vars (Just 1)
599 = \v -> let v_uniq = idUnique v
600 in v `elemVarSet` rule_vars
601 || not (isEmptyCoreRules (getIdSpecialisation v))
602 || v_uniq == runSTRepIdKey
604 -- Phase 2: allow build/augment to inline, and specialisations
605 blackListed rule_vars (Just 2)
606 = \v -> let v_uniq = idUnique v
607 in (v `elemVarSet` rule_vars && not (v_uniq == buildIdKey ||
608 v_uniq == augmentIdKey))
609 || v_uniq == runSTRepIdKey
611 -- Otherwise just go for it
612 blackListed rule_vars phase
617 SLPJ 95/04: Why @runST@ must be inlined very late:
621 (a, s') = newArray# 100 [] s
622 (_, s'') = fill_in_array_or_something a x s'
626 If we inline @runST@, we'll get:
629 (a, s') = newArray# 100 [] realWorld#{-NB-}
630 (_, s'') = fill_in_array_or_something a x s'
634 And now the @newArray#@ binding can be floated to become a CAF, which
635 is totally and utterly wrong:
638 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
641 let (_, s'') = fill_in_array_or_something a x s' in
644 All calls to @f@ will share a {\em single} array!
646 Yet we do want to inline runST sometime, so we can avoid
647 needless code. Solution: black list it until the last moment.