Robustify the treatement of DFunUnfolding
[ghc-hetmet.git] / compiler / coreSyn / CoreUnfold.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1994-1998
4 %
5
6 Core-syntax unfoldings
7
8 Unfoldings (which can travel across module boundaries) are in Core
9 syntax (namely @CoreExpr@s).
10
11 The type @Unfolding@ sits ``above'' simply-Core-expressions
12 unfoldings, capturing ``higher-level'' things we know about a binding,
13 usually things that the simplifier found out (e.g., ``it's a
14 literal'').  In the corner of a @CoreUnfolding@ unfolding, you will
15 find, unsurprisingly, a Core expression.
16
17 \begin{code}
18 module CoreUnfold (
19         Unfolding, UnfoldingGuidance,   -- Abstract types
20
21         noUnfolding, mkImplicitUnfolding, 
22         mkTopUnfolding, mkUnfolding, mkCoreUnfolding,
23         mkInlineRule, mkWwInlineRule,
24         mkCompulsoryUnfolding, mkDFunUnfolding,
25
26         interestingArg, ArgSummary(..),
27
28         couldBeSmallEnoughToInline, 
29         certainlyWillInline, smallEnoughToInline,
30
31         callSiteInline, CallCtxt(..), 
32
33         exprIsConApp_maybe
34
35     ) where
36
37 #include "HsVersions.h"
38
39 import StaticFlags
40 import DynFlags
41 import CoreSyn
42 import PprCore          ()      -- Instances
43 import TcType           ( tcSplitSigmaTy, tcSplitDFunHead )
44 import OccurAnal
45 import CoreSubst hiding( substTy )
46 import CoreFVs         ( exprFreeVars )
47 import CoreArity       ( manifestArity )
48 import CoreUtils
49 import Id
50 import DataCon
51 import TyCon
52 import Literal
53 import PrimOp
54 import IdInfo
55 import BasicTypes       ( Arity )
56 import TcType           ( tcSplitDFunTy )
57 import Type 
58 import Coercion
59 import PrelNames
60 import VarEnv           ( mkInScopeSet )
61 import Bag
62 import Util
63 import FastTypes
64 import FastString
65 import Outputable
66
67 \end{code}
68
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection{Making unfoldings}
73 %*                                                                      *
74 %************************************************************************
75
76 \begin{code}
77 mkTopUnfolding :: Bool -> CoreExpr -> Unfolding
78 mkTopUnfolding is_bottoming expr 
79   = mkUnfolding True {- Top level -} is_bottoming expr
80
81 mkImplicitUnfolding :: CoreExpr -> Unfolding
82 -- For implicit Ids, do a tiny bit of optimising first
83 mkImplicitUnfolding expr = mkTopUnfolding False (simpleOptExpr expr) 
84
85 -- Note [Top-level flag on inline rules]
86 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 -- Slight hack: note that mk_inline_rules conservatively sets the
88 -- top-level flag to True.  It gets set more accurately by the simplifier
89 -- Simplify.simplUnfolding.
90
91 mkUnfolding :: Bool -> Bool -> CoreExpr -> Unfolding
92 mkUnfolding top_lvl is_bottoming expr
93   = CoreUnfolding { uf_tmpl       = occurAnalyseExpr expr,
94                     uf_src        = InlineRhs,
95                     uf_arity      = arity,
96                     uf_is_top     = top_lvl,
97                     uf_is_value   = exprIsHNF        expr,
98                     uf_is_conlike = exprIsConLike    expr,
99                     uf_expandable = exprIsExpandable expr,
100                     uf_is_cheap   = is_cheap,
101                     uf_guidance   = guidance }
102   where
103     is_cheap = exprIsCheap expr
104     (arity, guidance) = calcUnfoldingGuidance is_cheap (top_lvl && is_bottoming) 
105                                               opt_UF_CreationThreshold expr
106         -- Sometimes during simplification, there's a large let-bound thing     
107         -- which has been substituted, and so is now dead; so 'expr' contains
108         -- two copies of the thing while the occurrence-analysed expression doesn't
109         -- Nevertheless, we *don't* occ-analyse before computing the size because the
110         -- size computation bales out after a while, whereas occurrence analysis does not.
111         --
112         -- This can occasionally mean that the guidance is very pessimistic;
113         -- it gets fixed up next round.  And it should be rare, because large
114         -- let-bound things that are dead are usually caught by preInlineUnconditionally
115
116 mkCoreUnfolding :: Bool -> UnfoldingSource -> CoreExpr
117                 -> Arity -> UnfoldingGuidance -> Unfolding
118 -- Occurrence-analyses the expression before capturing it
119 mkCoreUnfolding top_lvl src expr arity guidance 
120   = CoreUnfolding { uf_tmpl       = occurAnalyseExpr expr,
121                     uf_src        = src,
122                     uf_arity      = arity,
123                     uf_is_top     = top_lvl,
124                     uf_is_value   = exprIsHNF        expr,
125                     uf_is_conlike = exprIsConLike    expr,
126                     uf_is_cheap   = exprIsCheap      expr,
127                     uf_expandable = exprIsExpandable expr,
128                     uf_guidance   = guidance }
129
130 mkDFunUnfolding :: Type -> [CoreExpr] -> Unfolding
131 mkDFunUnfolding dfun_ty ops 
132   = DFunUnfolding dfun_nargs data_con ops
133   where
134     (tvs, theta, head_ty) = tcSplitSigmaTy dfun_ty
135           -- NB: tcSplitSigmaTy: do not look through a newtype
136           --     when the dictionary type is a newtype
137     (cls, _)   = tcSplitDFunHead head_ty
138     dfun_nargs = length tvs + length theta
139     data_con   = classDataCon cls
140
141 mkWwInlineRule :: Id -> CoreExpr -> Arity -> Unfolding
142 mkWwInlineRule id expr arity
143   = mkCoreUnfolding True (InlineWrapper id) 
144                    (simpleOptExpr expr) arity
145                    (UnfWhen unSaturatedOk boringCxtNotOk)
146
147 mkCompulsoryUnfolding :: CoreExpr -> Unfolding
148 mkCompulsoryUnfolding expr         -- Used for things that absolutely must be unfolded
149   = mkCoreUnfolding True InlineCompulsory
150                     expr 0    -- Arity of unfolding doesn't matter
151                     (UnfWhen unSaturatedOk boringCxtOk)
152
153 mkInlineRule :: CoreExpr -> Maybe Arity -> Unfolding
154 mkInlineRule expr mb_arity 
155   = mkCoreUnfolding True InlineRule      -- Note [Top-level flag on inline rules]
156                     expr' arity 
157                     (UnfWhen unsat_ok boring_ok)
158   where
159     expr' = simpleOptExpr expr
160     (unsat_ok, arity) = case mb_arity of
161                           Nothing -> (unSaturatedOk, manifestArity expr')
162                           Just ar -> (needSaturated, ar)
163               
164     boring_ok = case calcUnfoldingGuidance True    -- Treat as cheap
165                                            False   -- But not bottoming
166                                            (arity+1) expr' of
167                   (_, UnfWhen _ boring_ok) -> boring_ok
168                   _other                   -> boringCxtNotOk
169      -- See Note [INLINE for small functions]
170 \end{code}
171
172
173 %************************************************************************
174 %*                                                                      *
175 \subsection{The UnfoldingGuidance type}
176 %*                                                                      *
177 %************************************************************************
178
179 \begin{code}
180 calcUnfoldingGuidance
181         :: Bool         -- True <=> the rhs is cheap, or we want to treat it
182                         --          as cheap (INLINE things)     
183         -> Bool         -- True <=> this is a top-level unfolding for a
184                         --          diverging function; don't inline this
185         -> Int          -- Bomb out if size gets bigger than this
186         -> CoreExpr     -- Expression to look at
187         -> (Arity, UnfoldingGuidance)
188 calcUnfoldingGuidance expr_is_cheap top_bot bOMB_OUT_SIZE expr
189   = case collectBinders expr of { (bndrs, body) ->
190     let
191         val_bndrs   = filter isId bndrs
192         n_val_bndrs = length val_bndrs
193
194         guidance 
195           = case (sizeExpr (iUnbox bOMB_OUT_SIZE) val_bndrs body) of
196               TooBig -> UnfNever
197               SizeIs size cased_bndrs scrut_discount
198                 | uncondInline n_val_bndrs (iBox size)
199                 , expr_is_cheap
200                 -> UnfWhen unSaturatedOk boringCxtOk   -- Note [INLINE for small functions]
201                 | top_bot  -- See Note [Do not inline top-level bottoming functions]
202                 -> UnfNever
203
204                 | otherwise
205                 -> UnfIfGoodArgs { ug_args  = map (discount cased_bndrs) val_bndrs
206                                  , ug_size  = iBox size
207                                  , ug_res   = iBox scrut_discount }
208
209         discount cbs bndr
210            = foldlBag (\acc (b',n) -> if bndr==b' then acc+n else acc) 
211                       0 cbs
212     in
213     (n_val_bndrs, guidance) }
214 \end{code}
215
216 Note [Computing the size of an expression]
217 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218 The basic idea of sizeExpr is obvious enough: count nodes.  But getting the
219 heuristics right has taken a long time.  Here's the basic strategy:
220
221     * Variables, literals: 0
222       (Exception for string literals, see litSize.)
223
224     * Function applications (f e1 .. en): 1 + #value args
225
226     * Constructor applications: 1, regardless of #args
227
228     * Let(rec): 1 + size of components
229
230     * Note, cast: 0
231
232 Examples
233
234   Size  Term
235   --------------
236     0     42#
237     0     x
238     0     True
239     2     f x
240     1     Just x
241     4     f (g x)
242
243 Notice that 'x' counts 0, while (f x) counts 2.  That's deliberate: there's
244 a function call to account for.  Notice also that constructor applications 
245 are very cheap, because exposing them to a caller is so valuable.
246
247
248 Note [Do not inline top-level bottoming functions]
249 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250 The FloatOut pass has gone to some trouble to float out calls to 'error' 
251 and similar friends.  See Note [Bottoming floats] in SetLevels.
252 Do not re-inline them!  But we *do* still inline if they are very small
253 (the uncondInline stuff).
254
255
256 Note [INLINE for small functions]
257 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
258 Consider        {-# INLINE f #-}
259                 f x = Just x
260                 g y = f y
261 Then f's RHS is no larger than its LHS, so we should inline it into
262 even the most boring context.  In general, f the function is
263 sufficiently small that its body is as small as the call itself, the
264 inline unconditionally, regardless of how boring the context is.
265
266 Things to note:
267
268  * We inline *unconditionally* if inlined thing is smaller (using sizeExpr)
269    than the thing it's replacing.  Notice that
270       (f x) --> (g 3)             -- YES, unconditionally
271       (f x) --> x : []            -- YES, *even though* there are two
272                                   --      arguments to the cons
273       x     --> g 3               -- NO
274       x     --> Just v            -- NO
275
276   It's very important not to unconditionally replace a variable by
277   a non-atomic term.
278
279 * We do this even if the thing isn't saturated, else we end up with the
280   silly situation that
281      f x y = x
282      ...map (f 3)...
283   doesn't inline.  Even in a boring context, inlining without being
284   saturated will give a lambda instead of a PAP, and will be more
285   efficient at runtime.
286
287 * However, when the function's arity > 0, we do insist that it 
288   has at least one value argument at the call site.  Otherwise we find this:
289        f = /\a \x:a. x
290        d = /\b. MkD (f b)
291   If we inline f here we get
292        d = /\b. MkD (\x:b. x)
293   and then prepareRhs floats out the argument, abstracting the type
294   variables, so we end up with the original again!
295
296
297 \begin{code}
298 uncondInline :: Arity -> Int -> Bool
299 -- Inline unconditionally if there no size increase
300 -- Size of call is arity (+1 for the function)
301 -- See Note [INLINE for small functions]
302 uncondInline arity size 
303   | arity == 0 = size == 0
304   | otherwise  = size <= arity + 1
305 \end{code}
306
307
308 \begin{code}
309 sizeExpr :: FastInt         -- Bomb out if it gets bigger than this
310          -> [Id]            -- Arguments; we're interested in which of these
311                             -- get case'd
312          -> CoreExpr
313          -> ExprSize
314
315 -- Note [Computing the size of an expression]
316
317 sizeExpr bOMB_OUT_SIZE top_args expr
318   = size_up expr
319   where
320     size_up (Cast e _) = size_up e
321     size_up (Note _ e) = size_up e
322     size_up (Type _)   = sizeZero           -- Types cost nothing
323     size_up (Lit lit)  = sizeN (litSize lit)
324     size_up (Var f)    = size_up_call f []  -- Make sure we get constructor
325                                             -- discounts even on nullary constructors
326
327     size_up (App fun (Type _)) = size_up fun
328     size_up (App fun arg)      = size_up arg  `addSizeNSD`
329                                  size_up_app fun [arg]
330
331     size_up (Lam b e) | isId b    = lamScrutDiscount (size_up e `addSizeN` 1)
332                       | otherwise = size_up e
333
334     size_up (Let (NonRec binder rhs) body)
335       = size_up rhs             `addSizeNSD`
336         size_up body            `addSizeN`
337         (if isUnLiftedType (idType binder) then 0 else 1)
338                 -- For the allocation
339                 -- If the binder has an unlifted type there is no allocation
340
341     size_up (Let (Rec pairs) body)
342       = foldr (addSizeNSD . size_up . snd) 
343               (size_up body `addSizeN` length pairs)    -- (length pairs) for the allocation
344               pairs
345
346     size_up (Case (Var v) _ _ alts) 
347         | v `elem` top_args             -- We are scrutinising an argument variable
348         = alts_size (foldr1 addAltSize alt_sizes)
349                     (foldr1 maxSize alt_sizes)
350                 -- Good to inline if an arg is scrutinised, because
351                 -- that may eliminate allocation in the caller
352                 -- And it eliminates the case itself
353         where
354           alt_sizes = map size_up_alt alts
355
356                 -- alts_size tries to compute a good discount for
357                 -- the case when we are scrutinising an argument variable
358           alts_size (SizeIs tot tot_disc tot_scrut)  -- Size of all alternatives
359                     (SizeIs max _        _)          -- Size of biggest alternative
360                 = SizeIs tot (unitBag (v, iBox (_ILIT(2) +# tot -# max)) `unionBags` tot_disc) tot_scrut
361                         -- If the variable is known, we produce a discount that
362                         -- will take us back to 'max', the size of the largest alternative
363                         -- The 1+ is a little discount for reduced allocation in the caller
364                         --
365                         -- Notice though, that we return tot_disc, the total discount from 
366                         -- all branches.  I think that's right.
367
368           alts_size tot_size _ = tot_size
369
370     size_up (Case e _ _ alts) = size_up e  `addSizeNSD` 
371                                 foldr (addAltSize . size_up_alt) sizeZero alts
372                 -- We don't charge for the case itself
373                 -- It's a strict thing, and the price of the call
374                 -- is paid by scrut.  Also consider
375                 --      case f x of DEFAULT -> e
376                 -- This is just ';'!  Don't charge for it.
377                 --
378                 -- Moreover, we charge one per alternative.
379
380     ------------ 
381     -- size_up_app is used when there's ONE OR MORE value args
382     size_up_app (App fun arg) args 
383         | isTypeArg arg            = size_up_app fun args
384         | otherwise                = size_up arg  `addSizeNSD`
385                                      size_up_app fun (arg:args)
386     size_up_app (Var fun)     args = size_up_call fun args
387     size_up_app other         args = size_up other `addSizeN` length args
388
389     ------------ 
390     size_up_call :: Id -> [CoreExpr] -> ExprSize
391     size_up_call fun val_args
392        = case idDetails fun of
393            FCallId _        -> sizeN opt_UF_DearOp
394            DataConWorkId dc -> conSize    dc (length val_args)
395            PrimOpId op      -> primOpSize op (length val_args)
396            ClassOpId _      -> classOpSize top_args val_args
397            _                -> funSize top_args fun (length val_args)
398
399     ------------ 
400     size_up_alt (_con, _bndrs, rhs) = size_up rhs `addSizeN` 1
401         -- Don't charge for args, so that wrappers look cheap
402         -- (See comments about wrappers with Case)
403         --
404         -- IMPORATANT: *do* charge 1 for the alternative, else we 
405         -- find that giant case nests are treated as practically free
406         -- A good example is Foreign.C.Error.errrnoToIOError
407
408     ------------
409         -- These addSize things have to be here because
410         -- I don't want to give them bOMB_OUT_SIZE as an argument
411     addSizeN TooBig          _  = TooBig
412     addSizeN (SizeIs n xs d) m  = mkSizeIs bOMB_OUT_SIZE (n +# iUnbox m) xs d
413     
414         -- addAltSize is used to add the sizes of case alternatives
415     addAltSize TooBig            _      = TooBig
416     addAltSize _                 TooBig = TooBig
417     addAltSize (SizeIs n1 xs d1) (SizeIs n2 ys d2) 
418         = mkSizeIs bOMB_OUT_SIZE (n1 +# n2) 
419                                  (xs `unionBags` ys) 
420                                  (d1 +# d2)   -- Note [addAltSize result discounts]
421
422         -- This variant ignores the result discount from its LEFT argument
423         -- It's used when the second argument isn't part of the result
424     addSizeNSD TooBig            _      = TooBig
425     addSizeNSD _                 TooBig = TooBig
426     addSizeNSD (SizeIs n1 xs _) (SizeIs n2 ys d2) 
427         = mkSizeIs bOMB_OUT_SIZE (n1 +# n2) 
428                                  (xs `unionBags` ys) 
429                                  d2  -- Ignore d1
430 \end{code}
431
432 \begin{code}
433 -- | Finds a nominal size of a string literal.
434 litSize :: Literal -> Int
435 -- Used by CoreUnfold.sizeExpr
436 litSize (MachStr str) = 1 + ((lengthFS str + 3) `div` 4)
437         -- If size could be 0 then @f "x"@ might be too small
438         -- [Sept03: make literal strings a bit bigger to avoid fruitless 
439         --  duplication of little strings]
440 litSize _other = 0    -- Must match size of nullary constructors
441                       -- Key point: if  x |-> 4, then x must inline unconditionally
442                       --            (eg via case binding)
443
444 classOpSize :: [Id] -> [CoreExpr] -> ExprSize
445 -- See Note [Conlike is interesting]
446 classOpSize _ [] 
447   = sizeZero
448 classOpSize top_args (arg1 : other_args)
449   = SizeIs (iUnbox size) arg_discount (_ILIT(0))
450   where
451     size = 2 + length other_args
452     -- If the class op is scrutinising a lambda bound dictionary then
453     -- give it a discount, to encourage the inlining of this function
454     -- The actual discount is rather arbitrarily chosen
455     arg_discount = case arg1 of
456                      Var dict | dict `elem` top_args 
457                               -> unitBag (dict, opt_UF_DictDiscount)
458                      _other   -> emptyBag
459                      
460 funSize :: [Id] -> Id -> Int -> ExprSize
461 -- Size for functions that are not constructors or primops
462 -- Note [Function applications]
463 funSize top_args fun n_val_args
464   | fun `hasKey` buildIdKey   = buildSize
465   | fun `hasKey` augmentIdKey = augmentSize
466   | otherwise = SizeIs (iUnbox size) arg_discount (iUnbox res_discount)
467   where
468     some_val_args = n_val_args > 0
469
470     arg_discount | some_val_args && fun `elem` top_args
471                  = unitBag (fun, opt_UF_FunAppDiscount)
472                  | otherwise = emptyBag
473         -- If the function is an argument and is applied
474         -- to some values, give it an arg-discount
475
476     res_discount | idArity fun > n_val_args = opt_UF_FunAppDiscount
477                  | otherwise                = 0
478         -- If the function is partially applied, show a result discount
479
480     size | some_val_args = 1 + n_val_args
481          | otherwise     = 0
482         -- The 1+ is for the function itself
483         -- Add 1 for each non-trivial arg;
484         -- the allocation cost, as in let(rec)
485   
486
487 conSize :: DataCon -> Int -> ExprSize
488 conSize dc n_val_args
489   | n_val_args == 0 = SizeIs (_ILIT(0)) emptyBag (_ILIT(1))     -- Like variables
490
491 -- See Note [Constructor size]
492   | isUnboxedTupleCon dc = SizeIs (_ILIT(0)) emptyBag (iUnbox n_val_args +# _ILIT(1))
493
494 -- See Note [Unboxed tuple result discount]
495 --  | isUnboxedTupleCon dc = SizeIs (_ILIT(0)) emptyBag (_ILIT(0))
496
497 -- See Note [Constructor size]
498   | otherwise = SizeIs (_ILIT(1)) emptyBag (iUnbox n_val_args +# _ILIT(1))
499 \end{code}
500
501 Note [Constructor size]
502 ~~~~~~~~~~~~~~~~~~~~~~~
503 Treat a constructors application as size 1, regardless of how many
504 arguments it has; we are keen to expose them (and we charge separately
505 for their args).  We can't treat them as size zero, else we find that
506 (Just x) has size 0, which is the same as a lone variable; and hence
507 'v' will always be replaced by (Just x), where v is bound to Just x.
508
509 However, unboxed tuples count as size zero. I found occasions where we had 
510         f x y z = case op# x y z of { s -> (# s, () #) }
511 and f wasn't getting inlined.
512
513 Note [Unboxed tuple result discount]
514 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
515 I tried giving unboxed tuples a *result discount* of zero (see the
516 commented-out line).  Why?  When returned as a result they do not
517 allocate, so maybe we don't want to charge so much for them If you
518 have a non-zero discount here, we find that workers often get inlined
519 back into wrappers, because it look like
520     f x = case $wf x of (# a,b #) -> (a,b)
521 and we are keener because of the case.  However while this change
522 shrank binary sizes by 0.5% it also made spectral/boyer allocate 5%
523 more. All other changes were very small. So it's not a big deal but I
524 didn't adopt the idea.
525
526 \begin{code}
527 primOpSize :: PrimOp -> Int -> ExprSize
528 primOpSize op n_val_args
529  | not (primOpIsDupable op) = sizeN opt_UF_DearOp
530  | not (primOpOutOfLine op) = sizeN 1
531         -- Be very keen to inline simple primops.
532         -- We give a discount of 1 for each arg so that (op# x y z) costs 2.
533         -- We can't make it cost 1, else we'll inline let v = (op# x y z) 
534         -- at every use of v, which is excessive.
535         --
536         -- A good example is:
537         --      let x = +# p q in C {x}
538         -- Even though x get's an occurrence of 'many', its RHS looks cheap,
539         -- and there's a good chance it'll get inlined back into C's RHS. Urgh!
540
541  | otherwise = sizeN n_val_args
542
543
544 buildSize :: ExprSize
545 buildSize = SizeIs (_ILIT(0)) emptyBag (_ILIT(4))
546         -- We really want to inline applications of build
547         -- build t (\cn -> e) should cost only the cost of e (because build will be inlined later)
548         -- Indeed, we should add a result_discount becuause build is 
549         -- very like a constructor.  We don't bother to check that the
550         -- build is saturated (it usually is).  The "-2" discounts for the \c n, 
551         -- The "4" is rather arbitrary.
552
553 augmentSize :: ExprSize
554 augmentSize = SizeIs (_ILIT(0)) emptyBag (_ILIT(4))
555         -- Ditto (augment t (\cn -> e) ys) should cost only the cost of
556         -- e plus ys. The -2 accounts for the \cn 
557
558 -- When we return a lambda, give a discount if it's used (applied)
559 lamScrutDiscount :: ExprSize -> ExprSize
560 lamScrutDiscount (SizeIs n vs _) = SizeIs n vs (iUnbox opt_UF_FunAppDiscount)
561 lamScrutDiscount TooBig          = TooBig
562 \end{code}
563
564 Note [addAltSize result discounts]
565 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
566 When adding the size of alternatives, we *add* the result discounts
567 too, rather than take the *maximum*.  For a multi-branch case, this
568 gives a discount for each branch that returns a constructor, making us
569 keener to inline.  I did try using 'max' instead, but it makes nofib 
570 'rewrite' and 'puzzle' allocate significantly more, and didn't make
571 binary sizes shrink significantly either.
572
573 Note [Discounts and thresholds]
574 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
575 Constants for discounts and thesholds are defined in main/StaticFlags,
576 all of form opt_UF_xxxx.   They are:
577
578 opt_UF_CreationThreshold (45)
579      At a definition site, if the unfolding is bigger than this, we
580      may discard it altogether
581
582 opt_UF_UseThreshold (6)
583      At a call site, if the unfolding, less discounts, is smaller than
584      this, then it's small enough inline
585
586 opt_UF_KeennessFactor (1.5)
587      Factor by which the discounts are multiplied before 
588      subtracting from size
589
590 opt_UF_DictDiscount (1)
591      The discount for each occurrence of a dictionary argument
592      as an argument of a class method.  Should be pretty small
593      else big functions may get inlined
594
595 opt_UF_FunAppDiscount (6)
596      Discount for a function argument that is applied.  Quite
597      large, because if we inline we avoid the higher-order call.
598
599 opt_UF_DearOp (4)
600      The size of a foreign call or not-dupable PrimOp
601
602
603 Note [Function applications]
604 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
605 In a function application (f a b)
606
607   - If 'f' is an argument to the function being analysed, 
608     and there's at least one value arg, record a FunAppDiscount for f
609
610   - If the application if a PAP (arity > 2 in this example)
611     record a *result* discount (because inlining
612     with "extra" args in the call may mean that we now 
613     get a saturated application)
614
615 Code for manipulating sizes
616
617 \begin{code}
618 data ExprSize = TooBig
619               | SizeIs FastInt          -- Size found
620                        (Bag (Id,Int))   -- Arguments cased herein, and discount for each such
621                        FastInt          -- Size to subtract if result is scrutinised 
622                                         -- by a case expression
623
624 instance Outputable ExprSize where
625   ppr TooBig         = ptext (sLit "TooBig")
626   ppr (SizeIs a _ c) = brackets (int (iBox a) <+> int (iBox c))
627
628 -- subtract the discount before deciding whether to bale out. eg. we
629 -- want to inline a large constructor application into a selector:
630 --      tup = (a_1, ..., a_99)
631 --      x = case tup of ...
632 --
633 mkSizeIs :: FastInt -> FastInt -> Bag (Id, Int) -> FastInt -> ExprSize
634 mkSizeIs max n xs d | (n -# d) ># max = TooBig
635                     | otherwise       = SizeIs n xs d
636  
637 maxSize :: ExprSize -> ExprSize -> ExprSize
638 maxSize TooBig         _                                  = TooBig
639 maxSize _              TooBig                             = TooBig
640 maxSize s1@(SizeIs n1 _ _) s2@(SizeIs n2 _ _) | n1 ># n2  = s1
641                                               | otherwise = s2
642
643 sizeZero :: ExprSize
644 sizeN :: Int -> ExprSize
645
646 sizeZero = SizeIs (_ILIT(0))  emptyBag (_ILIT(0))
647 sizeN n  = SizeIs (iUnbox n) emptyBag (_ILIT(0))
648 \end{code}
649
650
651 %************************************************************************
652 %*                                                                      *
653 \subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
654 %*                                                                      *
655 %************************************************************************
656
657 We use 'couldBeSmallEnoughToInline' to avoid exporting inlinings that
658 we ``couldn't possibly use'' on the other side.  Can be overridden w/
659 flaggery.  Just the same as smallEnoughToInline, except that it has no
660 actual arguments.
661
662 \begin{code}
663 couldBeSmallEnoughToInline :: Int -> CoreExpr -> Bool
664 couldBeSmallEnoughToInline threshold rhs 
665   = case sizeExpr (iUnbox threshold) [] body of
666        TooBig -> False
667        _      -> True
668   where
669     (_, body) = collectBinders rhs
670
671 ----------------
672 smallEnoughToInline :: Unfolding -> Bool
673 smallEnoughToInline (CoreUnfolding {uf_guidance = UnfIfGoodArgs {ug_size = size}})
674   = size <= opt_UF_UseThreshold
675 smallEnoughToInline _
676   = False
677
678 ----------------
679 certainlyWillInline :: Unfolding -> Bool
680   -- Sees if the unfolding is pretty certain to inline  
681 certainlyWillInline (CoreUnfolding { uf_is_cheap = is_cheap, uf_arity = n_vals, uf_guidance = guidance })
682   = case guidance of
683       UnfNever      -> False
684       UnfWhen {}    -> True
685       UnfIfGoodArgs { ug_size = size} 
686                     -> is_cheap && size - (n_vals +1) <= opt_UF_UseThreshold
687
688 certainlyWillInline _
689   = False
690 \end{code}
691
692 %************************************************************************
693 %*                                                                      *
694 \subsection{callSiteInline}
695 %*                                                                      *
696 %************************************************************************
697
698 This is the key function.  It decides whether to inline a variable at a call site
699
700 callSiteInline is used at call sites, so it is a bit more generous.
701 It's a very important function that embodies lots of heuristics.
702 A non-WHNF can be inlined if it doesn't occur inside a lambda,
703 and occurs exactly once or 
704     occurs once in each branch of a case and is small
705
706 If the thing is in WHNF, there's no danger of duplicating work, 
707 so we can inline if it occurs once, or is small
708
709 NOTE: we don't want to inline top-level functions that always diverge.
710 It just makes the code bigger.  Tt turns out that the convenient way to prevent
711 them inlining is to give them a NOINLINE pragma, which we do in 
712 StrictAnal.addStrictnessInfoToTopId
713
714 \begin{code}
715 callSiteInline :: DynFlags
716                -> Id                    -- The Id
717                -> Unfolding             -- Its unfolding (if active)
718                -> Bool                  -- True if there are are no arguments at all (incl type args)
719                -> [ArgSummary]          -- One for each value arg; True if it is interesting
720                -> CallCtxt              -- True <=> continuation is interesting
721                -> Maybe CoreExpr        -- Unfolding, if any
722
723
724 instance Outputable ArgSummary where
725   ppr TrivArg    = ptext (sLit "TrivArg")
726   ppr NonTrivArg = ptext (sLit "NonTrivArg")
727   ppr ValueArg   = ptext (sLit "ValueArg")
728
729 data CallCtxt = BoringCtxt
730
731               | ArgCtxt         -- We are somewhere in the argument of a function
732                         Bool    -- True  <=> we're somewhere in the RHS of function with rules
733                                 -- False <=> we *are* the argument of a function with non-zero
734                                 --           arg discount
735                                 --        OR 
736                                 --           we *are* the RHS of a let  Note [RHS of lets]
737                                 -- In both cases, be a little keener to inline
738
739               | ValAppCtxt      -- We're applied to at least one value arg
740                                 -- This arises when we have ((f x |> co) y)
741                                 -- Then the (f x) has argument 'x' but in a ValAppCtxt
742
743               | CaseCtxt        -- We're the scrutinee of a case
744                                 -- that decomposes its scrutinee
745
746 instance Outputable CallCtxt where
747   ppr BoringCtxt      = ptext (sLit "BoringCtxt")
748   ppr (ArgCtxt rules) = ptext (sLit "ArgCtxt") <+> ppr rules
749   ppr CaseCtxt        = ptext (sLit "CaseCtxt")
750   ppr ValAppCtxt      = ptext (sLit "ValAppCtxt")
751
752 callSiteInline dflags id unfolding lone_variable arg_infos cont_info
753   = case unfolding of {
754         NoUnfolding      -> Nothing ;
755         OtherCon _       -> Nothing ;
756         DFunUnfolding {} -> Nothing ;   -- Never unfold a DFun
757         CoreUnfolding { uf_tmpl = unf_template, uf_is_top = is_top, 
758                         uf_is_cheap = is_cheap, uf_arity = uf_arity, uf_guidance = guidance } ->
759                         -- uf_arity will typically be equal to (idArity id), 
760                         -- but may be less for InlineRules
761     let
762         n_val_args = length arg_infos
763         saturated  = n_val_args >= uf_arity
764
765         result | yes_or_no = Just unf_template
766                | otherwise = Nothing
767
768         interesting_args = any nonTriv arg_infos 
769                 -- NB: (any nonTriv arg_infos) looks at the
770                 -- over-saturated args too which is "wrong"; 
771                 -- but if over-saturated we inline anyway.
772
773                -- some_benefit is used when the RHS is small enough
774                -- and the call has enough (or too many) value
775                -- arguments (ie n_val_args >= arity). But there must
776                -- be *something* interesting about some argument, or the
777                -- result context, to make it worth inlining
778         some_benefit 
779            | not saturated = interesting_args   -- Under-saturated
780                                                 -- Note [Unsaturated applications]
781            | n_val_args > uf_arity = True       -- Over-saturated
782            | otherwise = interesting_args       -- Saturated
783                       || interesting_saturated_call 
784
785         interesting_saturated_call 
786           = case cont_info of
787               BoringCtxt -> not is_top && uf_arity > 0        -- Note [Nested functions]
788               CaseCtxt   -> not (lone_variable && is_cheap)   -- Note [Lone variables]
789               ArgCtxt {} -> uf_arity > 0                      -- Note [Inlining in ArgCtxt]
790               ValAppCtxt -> True                              -- Note [Cast then apply]
791
792         (yes_or_no, extra_doc)
793           = case guidance of
794               UnfNever -> (False, empty)
795
796               UnfWhen unsat_ok boring_ok 
797                  -> (enough_args && (boring_ok || some_benefit), empty )
798                  where      -- See Note [INLINE for small functions]
799                    enough_args = saturated || (unsat_ok && n_val_args > 0)
800
801               UnfIfGoodArgs { ug_args = arg_discounts, ug_res = res_discount, ug_size = size }
802                  -> ( is_cheap && some_benefit && small_enough
803                     , (text "discounted size =" <+> int discounted_size) )
804                  where
805                    discounted_size = size - discount
806                    small_enough = discounted_size <= opt_UF_UseThreshold
807                    discount = computeDiscount uf_arity arg_discounts 
808                                               res_discount arg_infos cont_info
809                 
810     in    
811     if (dopt Opt_D_dump_inlinings dflags && dopt Opt_D_verbose_core2core dflags) then
812         pprTrace ("Considering inlining: " ++ showSDoc (ppr id))
813                  (vcat [text "arg infos" <+> ppr arg_infos,
814                         text "uf arity" <+> ppr uf_arity,
815                         text "interesting continuation" <+> ppr cont_info,
816                         text "some_benefit" <+> ppr some_benefit,
817                         text "is cheap:" <+> ppr is_cheap,
818                         text "guidance" <+> ppr guidance,
819                         extra_doc,
820                         text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO"])
821                   result
822     else
823     result
824     }
825 \end{code}
826
827 Note [RHS of lets]
828 ~~~~~~~~~~~~~~~~~~
829 Be a tiny bit keener to inline in the RHS of a let, because that might
830 lead to good thing later
831      f y = (y,y,y)
832      g y = let x = f y in ...(case x of (a,b,c) -> ...) ...
833 We'd inline 'f' if the call was in a case context, and it kind-of-is,
834 only we can't see it.  So we treat the RHS of a let as not-totally-boring.
835     
836 Note [Unsaturated applications]
837 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
838 When a call is not saturated, we *still* inline if one of the
839 arguments has interesting structure.  That's sometimes very important.
840 A good example is the Ord instance for Bool in Base:
841
842  Rec {
843     $fOrdBool =GHC.Classes.D:Ord
844                  @ Bool
845                  ...
846                  $cmin_ajX
847
848     $cmin_ajX [Occ=LoopBreaker] :: Bool -> Bool -> Bool
849     $cmin_ajX = GHC.Classes.$dmmin @ Bool $fOrdBool
850   }
851
852 But the defn of GHC.Classes.$dmmin is:
853
854   $dmmin :: forall a. GHC.Classes.Ord a => a -> a -> a
855     {- Arity: 3, HasNoCafRefs, Strictness: SLL,
856        Unfolding: (\ @ a $dOrd :: GHC.Classes.Ord a x :: a y :: a ->
857                    case @ a GHC.Classes.<= @ a $dOrd x y of wild {
858                      GHC.Bool.False -> y GHC.Bool.True -> x }) -}
859
860 We *really* want to inline $dmmin, even though it has arity 3, in
861 order to unravel the recursion.
862
863
864 Note [Things to watch]
865 ~~~~~~~~~~~~~~~~~~~~~~
866 *   { y = I# 3; x = y `cast` co; ...case (x `cast` co) of ... }
867     Assume x is exported, so not inlined unconditionally.
868     Then we want x to inline unconditionally; no reason for it 
869     not to, and doing so avoids an indirection.
870
871 *   { x = I# 3; ....f x.... }
872     Make sure that x does not inline unconditionally!  
873     Lest we get extra allocation.
874
875 Note [Inlining an InlineRule]
876 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
877 An InlineRules is used for
878   (a) programmer INLINE pragmas
879   (b) inlinings from worker/wrapper
880
881 For (a) the RHS may be large, and our contract is that we *only* inline
882 when the function is applied to all the arguments on the LHS of the
883 source-code defn.  (The uf_arity in the rule.)
884
885 However for worker/wrapper it may be worth inlining even if the 
886 arity is not satisfied (as we do in the CoreUnfolding case) so we don't
887 require saturation.
888
889
890 Note [Nested functions]
891 ~~~~~~~~~~~~~~~~~~~~~~~
892 If a function has a nested defn we also record some-benefit, on the
893 grounds that we are often able to eliminate the binding, and hence the
894 allocation, for the function altogether; this is good for join points.
895 But this only makes sense for *functions*; inlining a constructor
896 doesn't help allocation unless the result is scrutinised.  UNLESS the
897 constructor occurs just once, albeit possibly in multiple case
898 branches.  Then inlining it doesn't increase allocation, but it does
899 increase the chance that the constructor won't be allocated at all in
900 the branches that don't use it.
901
902 Note [Cast then apply]
903 ~~~~~~~~~~~~~~~~~~~~~~
904 Consider
905    myIndex = __inline_me ( (/\a. <blah>) |> co )
906    co :: (forall a. a -> a) ~ (forall a. T a)
907      ... /\a.\x. case ((myIndex a) |> sym co) x of { ... } ...
908
909 We need to inline myIndex to unravel this; but the actual call (myIndex a) has
910 no value arguments.  The ValAppCtxt gives it enough incentive to inline.
911
912 Note [Inlining in ArgCtxt]
913 ~~~~~~~~~~~~~~~~~~~~~~~~~~
914 The condition (arity > 0) here is very important, because otherwise
915 we end up inlining top-level stuff into useless places; eg
916    x = I# 3#
917    f = \y.  g x
918 This can make a very big difference: it adds 16% to nofib 'integer' allocs,
919 and 20% to 'power'.
920
921 At one stage I replaced this condition by 'True' (leading to the above 
922 slow-down).  The motivation was test eyeball/inline1.hs; but that seems
923 to work ok now.
924
925 NOTE: arguably, we should inline in ArgCtxt only if the result of the
926 call is at least CONLIKE.  At least for the cases where we use ArgCtxt
927 for the RHS of a 'let', we only profit from the inlining if we get a 
928 CONLIKE thing (modulo lets).
929
930 Note [Lone variables]   See also Note [Interaction of exprIsCheap and lone variables]
931 ~~~~~~~~~~~~~~~~~~~~~   which appears below
932 The "lone-variable" case is important.  I spent ages messing about
933 with unsatisfactory varaints, but this is nice.  The idea is that if a
934 variable appears all alone
935
936         as an arg of lazy fn, or rhs    BoringCtxt
937         as scrutinee of a case          CaseCtxt
938         as arg of a fn                  ArgCtxt
939 AND
940         it is bound to a cheap expression
941
942 then we should not inline it (unless there is some other reason,
943 e.g. is is the sole occurrence).  That is what is happening at 
944 the use of 'lone_variable' in 'interesting_saturated_call'.
945
946 Why?  At least in the case-scrutinee situation, turning
947         let x = (a,b) in case x of y -> ...
948 into
949         let x = (a,b) in case (a,b) of y -> ...
950 and thence to 
951         let x = (a,b) in let y = (a,b) in ...
952 is bad if the binding for x will remain.
953
954 Another example: I discovered that strings
955 were getting inlined straight back into applications of 'error'
956 because the latter is strict.
957         s = "foo"
958         f = \x -> ...(error s)...
959
960 Fundamentally such contexts should not encourage inlining because the
961 context can ``see'' the unfolding of the variable (e.g. case or a
962 RULE) so there's no gain.  If the thing is bound to a value.
963
964 However, watch out:
965
966  * Consider this:
967         foo = _inline_ (\n. [n])
968         bar = _inline_ (foo 20)
969         baz = \n. case bar of { (m:_) -> m + n }
970    Here we really want to inline 'bar' so that we can inline 'foo'
971    and the whole thing unravels as it should obviously do.  This is 
972    important: in the NDP project, 'bar' generates a closure data
973    structure rather than a list. 
974
975    So the non-inlining of lone_variables should only apply if the
976    unfolding is regarded as cheap; because that is when exprIsConApp_maybe
977    looks through the unfolding.  Hence the "&& is_cheap" in the
978    InlineRule branch.
979
980  * Even a type application or coercion isn't a lone variable.
981    Consider
982         case $fMonadST @ RealWorld of { :DMonad a b c -> c }
983    We had better inline that sucker!  The case won't see through it.
984
985    For now, I'm treating treating a variable applied to types 
986    in a *lazy* context "lone". The motivating example was
987         f = /\a. \x. BIG
988         g = /\a. \y.  h (f a)
989    There's no advantage in inlining f here, and perhaps
990    a significant disadvantage.  Hence some_val_args in the Stop case
991
992 Note [Interaction of exprIsCheap and lone variables]
993 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
994 The lone-variable test says "don't inline if a case expression
995 scrutines a lone variable whose unfolding is cheap".  It's very 
996 important that, under these circumstances, exprIsConApp_maybe
997 can spot a constructor application. So, for example, we don't
998 consider
999         let x = e in (x,x)
1000 to be cheap, and that's good because exprIsConApp_maybe doesn't
1001 think that expression is a constructor application.
1002
1003 I used to test is_value rather than is_cheap, which was utterly
1004 wrong, because the above expression responds True to exprIsHNF.
1005
1006 This kind of thing can occur if you have
1007
1008         {-# INLINE foo #-}
1009         foo = let x = e in (x,x)
1010
1011 which Roman did.
1012
1013 \begin{code}
1014 computeDiscount :: Int -> [Int] -> Int -> [ArgSummary] -> CallCtxt -> Int
1015 computeDiscount n_vals_wanted arg_discounts res_discount arg_infos cont_info
1016         -- We multiple the raw discounts (args_discount and result_discount)
1017         -- ty opt_UnfoldingKeenessFactor because the former have to do with
1018         --  *size* whereas the discounts imply that there's some extra 
1019         --  *efficiency* to be gained (e.g. beta reductions, case reductions) 
1020         -- by inlining.
1021
1022   = 1           -- Discount of 1 because the result replaces the call
1023                 -- so we count 1 for the function itself
1024
1025     + length (take n_vals_wanted arg_infos)
1026                -- Discount of (un-scaled) 1 for each arg supplied, 
1027                -- because the result replaces the call
1028
1029     + round (opt_UF_KeenessFactor * 
1030              fromIntegral (arg_discount + res_discount'))
1031   where
1032     arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)
1033
1034     mk_arg_discount _        TrivArg    = 0 
1035     mk_arg_discount _        NonTrivArg = 1   
1036     mk_arg_discount discount ValueArg   = discount 
1037
1038     res_discount' = case cont_info of
1039                         BoringCtxt  -> 0
1040                         CaseCtxt    -> res_discount
1041                         _other      -> 4 `min` res_discount
1042                 -- res_discount can be very large when a function returns
1043                 -- constructors; but we only want to invoke that large discount
1044                 -- when there's a case continuation.
1045                 -- Otherwise we, rather arbitrarily, threshold it.  Yuk.
1046                 -- But we want to aovid inlining large functions that return 
1047                 -- constructors into contexts that are simply "interesting"
1048 \end{code}
1049
1050 %************************************************************************
1051 %*                                                                      *
1052         Interesting arguments
1053 %*                                                                      *
1054 %************************************************************************
1055
1056 Note [Interesting arguments]
1057 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1058 An argument is interesting if it deserves a discount for unfoldings
1059 with a discount in that argument position.  The idea is to avoid
1060 unfolding a function that is applied only to variables that have no
1061 unfolding (i.e. they are probably lambda bound): f x y z There is
1062 little point in inlining f here.
1063
1064 Generally, *values* (like (C a b) and (\x.e)) deserve discounts.  But
1065 we must look through lets, eg (let x = e in C a b), because the let will
1066 float, exposing the value, if we inline.  That makes it different to
1067 exprIsHNF.
1068
1069 Before 2009 we said it was interesting if the argument had *any* structure
1070 at all; i.e. (hasSomeUnfolding v).  But does too much inlining; see Trac #3016.
1071
1072 But we don't regard (f x y) as interesting, unless f is unsaturated.
1073 If it's saturated and f hasn't inlined, then it's probably not going
1074 to now!
1075
1076 Note [Conlike is interesting]
1077 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1078 Consider
1079         f d = ...((*) d x y)...
1080         ... f (df d')...
1081 where df is con-like. Then we'd really like to inline 'f' so that the
1082 rule for (*) (df d) can fire.  To do this 
1083   a) we give a discount for being an argument of a class-op (eg (*) d)
1084   b) we say that a con-like argument (eg (df d)) is interesting
1085
1086 \begin{code}
1087 data ArgSummary = TrivArg       -- Nothing interesting
1088                 | NonTrivArg    -- Arg has structure
1089                 | ValueArg      -- Arg is a con-app or PAP
1090                                 -- ..or con-like. Note [Conlike is interesting]
1091
1092 interestingArg :: CoreExpr -> ArgSummary
1093 -- See Note [Interesting arguments]
1094 interestingArg e = go e 0
1095   where
1096     -- n is # value args to which the expression is applied
1097     go (Lit {}) _          = ValueArg
1098     go (Var v)  n
1099        | isConLikeId v     = ValueArg   -- Experimenting with 'conlike' rather that
1100                                         --    data constructors here
1101        | idArity v > n     = ValueArg   -- Catches (eg) primops with arity but no unfolding
1102        | n > 0             = NonTrivArg -- Saturated or unknown call
1103        | conlike_unfolding = ValueArg   -- n==0; look for an interesting unfolding
1104                                         -- See Note [Conlike is interesting]
1105        | otherwise         = TrivArg    -- n==0, no useful unfolding
1106        where
1107          conlike_unfolding = isConLikeUnfolding (idUnfolding v)
1108
1109     go (Type _)          _ = TrivArg
1110     go (App fn (Type _)) n = go fn n    
1111     go (App fn _)        n = go fn (n+1)
1112     go (Note _ a)        n = go a n
1113     go (Cast e _)        n = go e n
1114     go (Lam v e)         n 
1115        | isTyVar v         = go e n
1116        | n>0               = go e (n-1)
1117        | otherwise         = ValueArg
1118     go (Let _ e)         n = case go e n of { ValueArg -> ValueArg; _ -> NonTrivArg }
1119     go (Case {})         _ = NonTrivArg
1120
1121 nonTriv ::  ArgSummary -> Bool
1122 nonTriv TrivArg = False
1123 nonTriv _       = True
1124 \end{code}
1125
1126 %************************************************************************
1127 %*                                                                      *
1128          exprIsConApp_maybe
1129 %*                                                                      *
1130 %************************************************************************
1131
1132 Note [exprIsConApp_maybe]
1133 ~~~~~~~~~~~~~~~~~~~~~~~~~
1134 exprIsConApp_maybe is a very important function.  There are two principal
1135 uses:
1136   * case e of { .... }
1137   * cls_op e, where cls_op is a class operation
1138
1139 In both cases you want to know if e is of form (C e1..en) where C is
1140 a data constructor.
1141
1142 However e might not *look* as if 
1143
1144 \begin{code}
1145 -- | Returns @Just (dc, [t1..tk], [x1..xn])@ if the argument expression is 
1146 -- a *saturated* constructor application of the form @dc t1..tk x1 .. xn@,
1147 -- where t1..tk are the *universally-qantified* type args of 'dc'
1148 exprIsConApp_maybe :: IdUnfoldingFun -> CoreExpr -> Maybe (DataCon, [Type], [CoreExpr])
1149
1150 exprIsConApp_maybe id_unf (Note _ expr)
1151   = exprIsConApp_maybe id_unf expr
1152         -- We ignore all notes.  For example,
1153         --      case _scc_ "foo" (C a b) of
1154         --                      C a b -> e
1155         -- should be optimised away, but it will be only if we look
1156         -- through the SCC note.
1157
1158 exprIsConApp_maybe id_unf (Cast expr co)
1159   =     -- Here we do the KPush reduction rule as described in the FC paper
1160         -- The transformation applies iff we have
1161         --      (C e1 ... en) `cast` co
1162         -- where co :: (T t1 .. tn) ~ to_ty
1163         -- The left-hand one must be a T, because exprIsConApp returned True
1164         -- but the right-hand one might not be.  (Though it usually will.)
1165
1166     case exprIsConApp_maybe id_unf expr of {
1167         Nothing                          -> Nothing ;
1168         Just (dc, _dc_univ_args, dc_args) -> 
1169
1170     let (_from_ty, to_ty) = coercionKind co
1171         dc_tc = dataConTyCon dc
1172     in
1173     case splitTyConApp_maybe to_ty of {
1174         Nothing -> Nothing ;
1175         Just (to_tc, to_tc_arg_tys) 
1176                 | dc_tc /= to_tc -> Nothing
1177                 -- These two Nothing cases are possible; we might see 
1178                 --      (C x y) `cast` (g :: T a ~ S [a]),
1179                 -- where S is a type function.  In fact, exprIsConApp
1180                 -- will probably not be called in such circumstances,
1181                 -- but there't nothing wrong with it 
1182
1183                 | otherwise  ->
1184     let
1185         tc_arity       = tyConArity dc_tc
1186         dc_univ_tyvars = dataConUnivTyVars dc
1187         dc_ex_tyvars   = dataConExTyVars dc
1188         arg_tys        = dataConRepArgTys dc
1189
1190         dc_eqs :: [(Type,Type)]   -- All equalities from the DataCon
1191         dc_eqs = [(mkTyVarTy tv, ty)   | (tv,ty) <- dataConEqSpec dc] ++
1192                  [getEqPredTys eq_pred | eq_pred <- dataConEqTheta dc]
1193
1194         (ex_args, rest1)    = splitAtList dc_ex_tyvars dc_args
1195         (co_args, val_args) = splitAtList dc_eqs rest1
1196
1197         -- Make the "theta" from Fig 3 of the paper
1198         gammas = decomposeCo tc_arity co
1199         theta  = zipOpenTvSubst (dc_univ_tyvars ++ dc_ex_tyvars)
1200                                 (gammas         ++ stripTypeArgs ex_args)
1201
1202           -- Cast the existential coercion arguments
1203         cast_co (ty1, ty2) (Type co) 
1204           = Type $ mkSymCoercion (substTy theta ty1)
1205                    `mkTransCoercion` co
1206                    `mkTransCoercion` (substTy theta ty2)
1207         cast_co _ other_arg = pprPanic "cast_co" (ppr other_arg)
1208         new_co_args = zipWith cast_co dc_eqs co_args
1209   
1210           -- Cast the value arguments (which include dictionaries)
1211         new_val_args = zipWith cast_arg arg_tys val_args
1212         cast_arg arg_ty arg = mkCoerce (substTy theta arg_ty) arg
1213     in
1214 #ifdef DEBUG
1215     let dump_doc = vcat [ppr dc,      ppr dc_univ_tyvars, ppr dc_ex_tyvars,
1216                          ppr arg_tys, ppr dc_args,        ppr _dc_univ_args,
1217                          ppr ex_args, ppr val_args]
1218     in
1219     ASSERT2( coreEqType _from_ty (mkTyConApp dc_tc _dc_univ_args), dump_doc )
1220     ASSERT2( all isTypeArg (ex_args ++ co_args), dump_doc )
1221     ASSERT2( equalLength val_args arg_tys, dump_doc )
1222 #endif
1223
1224     Just (dc, to_tc_arg_tys, ex_args ++ new_co_args ++ new_val_args)
1225     }}
1226
1227 exprIsConApp_maybe id_unf expr 
1228   = analyse expr [] 
1229   where
1230     analyse (App fun arg) args = analyse fun (arg:args)
1231     analyse fun@(Lam {})  args = beta fun [] args 
1232
1233     analyse (Var fun) args
1234         | Just con <- isDataConWorkId_maybe fun
1235         , count isValArg args == idArity fun
1236         , let (univ_ty_args, rest_args) = splitAtList (dataConUnivTyVars con) args
1237         = Just (con, stripTypeArgs univ_ty_args, rest_args)
1238
1239         -- Look through dictionary functions; see Note [Unfolding DFuns]
1240         | DFunUnfolding dfun_nargs con ops <- unfolding
1241         , let sat = length args == dfun_nargs    -- See Note [DFun arity check]
1242           in if sat then True else 
1243              pprTrace "Unsaturated dfun" (ppr fun <+> int dfun_nargs $$ ppr args) False   
1244         , let (dfun_tvs, _cls, dfun_res_tys) = tcSplitDFunTy (idType fun)
1245               subst = zipOpenTvSubst dfun_tvs (stripTypeArgs (takeList dfun_tvs args))
1246         = Just (con, substTys subst dfun_res_tys, 
1247                      [mkApps op args | op <- ops])
1248
1249         -- Look through unfoldings, but only cheap ones, because
1250         -- we are effectively duplicating the unfolding
1251         | Just rhs <- expandUnfolding_maybe unfolding
1252         = -- pprTrace "expanding" (ppr fun $$ ppr rhs) $
1253           analyse rhs args
1254         where
1255           unfolding = id_unf fun
1256
1257     analyse _ _ = Nothing
1258
1259     -----------
1260     beta (Lam v body) pairs (arg : args) 
1261         | isTypeArg arg
1262         = beta body ((v,arg):pairs) args 
1263
1264     beta (Lam {}) _ _    -- Un-saturated, or not a type lambda
1265         = Nothing
1266
1267     beta fun pairs args
1268         = case analyse (substExpr (text "subst-expr-is-con-app") subst fun) args of
1269             Nothing  -> -- pprTrace "Bale out! exprIsConApp_maybe" doc $
1270                         Nothing
1271             Just ans -> -- pprTrace "Woo-hoo! exprIsConApp_maybe" doc $
1272                         Just ans
1273         where
1274           subst = mkOpenSubst (mkInScopeSet (exprFreeVars fun)) pairs
1275           -- doc = vcat [ppr fun, ppr expr, ppr pairs, ppr args]
1276
1277
1278 stripTypeArgs :: [CoreExpr] -> [Type]
1279 stripTypeArgs args = ASSERT2( all isTypeArg args, ppr args )
1280                      [ty | Type ty <- args]
1281 \end{code}
1282
1283 Note [Unfolding DFuns]
1284 ~~~~~~~~~~~~~~~~~~~~~~
1285 DFuns look like
1286
1287   df :: forall a b. (Eq a, Eq b) -> Eq (a,b)
1288   df a b d_a d_b = MkEqD (a,b) ($c1 a b d_a d_b)
1289                                ($c2 a b d_a d_b)
1290
1291 So to split it up we just need to apply the ops $c1, $c2 etc
1292 to the very same args as the dfun.  It takes a little more work
1293 to compute the type arguments to the dictionary constructor.
1294
1295 Note [DFun arity check]
1296 ~~~~~~~~~~~~~~~~~~~~~~~
1297 Here we check that the total number of supplied arguments (inclding 
1298 type args) matches what the dfun is expecting.  This may be *less*
1299 than the ordinary arity of the dfun: see Note [DFun unfoldings] in CoreSyn