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