CoreUnfold.lhs 30.1 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3 4
% (c) The AQUA Project, Glasgow University, 1994-1998
%
Simon Marlow's avatar
Simon Marlow committed
5 6

Core-syntax unfoldings
7 8 9 10 11 12 13 14 15 16 17 18

Unfoldings (which can travel across module boundaries) are in Core
syntax (namely @CoreExpr@s).

The type @Unfolding@ sits ``above'' simply-Core-expressions
unfoldings, capturing ``higher-level'' things we know about a binding,
usually things that the simplifier found out (e.g., ``it's a
literal'').  In the corner of a @CoreUnfolding@ unfolding, you will
find, unsurprisingly, a Core expression.

\begin{code}
module CoreUnfold (
19
	Unfolding, UnfoldingGuidance,	-- Abstract types
20

21 22
	noUnfolding, mkTopUnfolding, mkImplicitUnfolding, mkUnfolding, 
	mkCompulsoryUnfolding, seqUnfolding,
23
	evaldUnfolding, mkOtherCon, otherCons,
24
	unfoldingTemplate, maybeUnfoldingTemplate,
25
	isEvaldUnfolding, isValueUnfolding, isCheapUnfolding, isCompulsoryUnfolding,
26
	hasUnfolding, hasSomeUnfolding, neverUnfold,
27 28

	couldBeSmallEnoughToInline, 
29
	certainlyWillInline, smallEnoughToInline,
30

31
	callSiteInline, CallCtxt(..)
32

33 34
    ) where

Simon Marlow's avatar
Simon Marlow committed
35 36
import StaticFlags
import DynFlags
37
import CoreSyn
38
import PprCore		()	-- Instances
Simon Marlow's avatar
Simon Marlow committed
39
import OccurAnal
40 41
import CoreSubst 	( Subst, emptySubst, substTy, extendIdSubst, extendTvSubst
			, lookupIdSubst, substBndr, substBndrs, substRecBndrs )
Simon Marlow's avatar
Simon Marlow committed
42 43 44 45 46 47
import CoreUtils
import Id
import DataCon
import Literal
import PrimOp
import IdInfo
48
import Type hiding( substTy, extendTvSubst )
Simon Marlow's avatar
Simon Marlow committed
49
import PrelNames
50
import Bag
51
import FastTypes
52
import FastString
53
import Outputable
54

55 56
\end{code}

57

58 59
%************************************************************************
%*									*
60
\subsection{Making unfoldings}
61 62 63 64
%*									*
%************************************************************************

\begin{code}
twanvl's avatar
twanvl committed
65
mkTopUnfolding :: CoreExpr -> Unfolding
66
mkTopUnfolding expr = mkUnfolding True {- Top level -} expr
67

68 69 70 71 72 73 74 75 76
mkImplicitUnfolding :: CoreExpr -> Unfolding
-- For implicit Ids, do a tiny bit of optimising first
mkImplicitUnfolding expr 
  = CoreUnfolding (simpleOptExpr emptySubst expr)
		  True
		  (exprIsHNF expr)
		  (exprIsCheap expr)
		  (calcUnfoldingGuidance opt_UF_CreationThreshold expr)

twanvl's avatar
twanvl committed
77
mkUnfolding :: Bool -> CoreExpr -> Unfolding
78
mkUnfolding top_lvl expr
79
  = CoreUnfolding (occurAnalyseExpr expr)
80
		  top_lvl
81

82
		  (exprIsHNF expr)
83 84 85 86 87
			-- Already evaluated

		  (exprIsCheap expr)
			-- OK to inline inside a lambda

88
		  (calcUnfoldingGuidance opt_UF_CreationThreshold expr)
89 90 91 92 93 94 95 96
	-- Sometimes during simplification, there's a large let-bound thing	
	-- which has been substituted, and so is now dead; so 'expr' contains
	-- two copies of the thing while the occurrence-analysed expression doesn't
	-- Nevertheless, we don't occ-analyse before computing the size because the
	-- size computation bales out after a while, whereas occurrence analysis does not.
	--
	-- This can occasionally mean that the guidance is very pessimistic;
	-- it gets fixed up next round
97

98
instance Outputable Unfolding where
Ian Lynagh's avatar
Ian Lynagh committed
99 100 101
  ppr NoUnfolding = ptext (sLit "No unfolding")
  ppr (OtherCon cs) = ptext (sLit "OtherCon") <+> ppr cs
  ppr (CompulsoryUnfolding e) = ptext (sLit "Compulsory") <+> ppr e
102
  ppr (CoreUnfolding e top hnf cheap g) 
Ian Lynagh's avatar
Ian Lynagh committed
103
	= ptext (sLit "Unf") <+> sep [ppr top <+> ppr hnf <+> ppr cheap <+> ppr g, 
104 105
				     ppr e]

twanvl's avatar
twanvl committed
106
mkCompulsoryUnfolding :: CoreExpr -> Unfolding
107
mkCompulsoryUnfolding expr	-- Used for things that absolutely must be unfolded
108
  = CompulsoryUnfolding (occurAnalyseExpr expr)
109
\end{code}
110

111

112 113 114 115 116
%************************************************************************
%*									*
\subsection{The UnfoldingGuidance type}
%*									*
%************************************************************************
117 118 119

\begin{code}
instance Outputable UnfoldingGuidance where
Ian Lynagh's avatar
Ian Lynagh committed
120
    ppr UnfoldNever	= ptext (sLit "NEVER")
121
    ppr (UnfoldIfGoodArgs v cs size discount)
Ian Lynagh's avatar
Ian Lynagh committed
122
      = hsep [ ptext (sLit "IF_ARGS"), int v,
123
	       brackets (hsep (map int cs)),
124 125 126 127 128 129 130 131 132 133
	       int size,
	       int discount ]
\end{code}


\begin{code}
calcUnfoldingGuidance
	:: Int		    	-- bomb out if size gets bigger than this
	-> CoreExpr    		-- expression to look at
	-> UnfoldingGuidance
134
calcUnfoldingGuidance bOMB_OUT_SIZE expr
135
  = case collect_val_bndrs expr of { (inline, val_binders, body) ->
136 137
    let
	n_val_binders = length val_binders
138

139
	max_inline_size = n_val_binders+2
140 141 142 143 144 145
	-- The idea is that if there is an INLINE pragma (inline is True)
	-- and there's a big body, we give a size of n_val_binders+2.  This
	-- This is just enough to fail the no-size-increase test in callSiteInline,
	--   so that INLINE things don't get inlined into entirely boring contexts,
	--   but no more.

146
    in
147
    case (sizeExpr (iUnbox bOMB_OUT_SIZE) val_binders body) of
148

149 150 151 152
      TooBig 
	| not inline -> UnfoldNever
		-- A big function with an INLINE pragma must
		-- have an UnfoldIfGoodArgs guidance
153
	| otherwise  -> UnfoldIfGoodArgs n_val_binders
154
					 (map (const 0) val_binders)
155
					 max_inline_size 0
156 157 158

      SizeIs size cased_args scrut_discount
	-> UnfoldIfGoodArgs
159
			n_val_binders
160
			(map discount_for val_binders)
161
			final_size
162
			(iBox scrut_discount)
163
	where        
164
	    boxed_size    = iBox size
165

166
	    final_size | inline     = boxed_size `min` max_inline_size
167
		       | otherwise  = boxed_size
168 169

		-- Sometimes an INLINE thing is smaller than n_val_binders+2.
170 171 172
		-- A particular case in point is a constructor, which has size 1.
		-- We want to inline this regardless, hence the `min`

173 174
	    discount_for b = foldlBag (\acc (b',n) -> if b==b' then acc+n else acc) 
				      0 cased_args
175
	}
176 177 178 179 180 181 182 183
  where
    collect_val_bndrs e = go False [] e
	-- We need to be a bit careful about how we collect the
	-- value binders.  In ptic, if we see 
	--	__inline_me (\x y -> e)
	-- We want to say "2 value binders".  Why?  So that 
	-- we take account of information given for the arguments

twanvl's avatar
twanvl committed
184
    go _      rev_vbs (Note InlineMe e)     = go True   rev_vbs     e
185 186 187
    go inline rev_vbs (Lam b e) | isId b    = go inline (b:rev_vbs) e
				| otherwise = go inline rev_vbs     e
    go inline rev_vbs e			    = (inline, reverse rev_vbs, e)
188 189 190
\end{code}

\begin{code}
191
sizeExpr :: FastInt 	    -- Bomb out if it gets bigger than this
192 193 194 195 196
	 -> [Id]	    -- Arguments; we're interested in which of these
			    -- get case'd
	 -> CoreExpr
	 -> ExprSize

197
sizeExpr bOMB_OUT_SIZE top_args expr
198 199
  = size_up expr
  where
twanvl's avatar
twanvl committed
200 201
    size_up (Type _)           = sizeZero        -- Types cost nothing
    size_up (Var _)            = sizeOne
202

twanvl's avatar
twanvl committed
203
    size_up (Note InlineMe _)  = sizeOne         -- Inline notes make it look very small
204 205 206 207 208 209 210
	-- This can be important.  If you have an instance decl like this:
	-- 	instance Foo a => Foo [a] where
	--	   {-# INLINE op1, op2 #-}
	--	   op1 = ...
	--	   op2 = ...
	-- then we'll get a dfun which is a pair of two INLINE lambdas

twanvl's avatar
twanvl committed
211
    size_up (Note _      body) = size_up body  -- Other notes cost nothing
212
    
twanvl's avatar
twanvl committed
213
    size_up (Cast e _)         = size_up e
214

twanvl's avatar
twanvl committed
215
    size_up (App fun (Type _)) = size_up fun
216
    size_up (App fun arg)      = size_up_app fun [arg]
217

218
    size_up (Lit lit) 	       = sizeN (litSize lit)
219

220
    size_up (Lam b e) | isId b    = lamScrutDiscount (size_up e `addSizeN` 1)
221 222 223 224 225
		      | otherwise = size_up e

    size_up (Let (NonRec binder rhs) body)
      = nukeScrutDiscount (size_up rhs)		`addSize`
	size_up body				`addSizeN`
226 227 228
	(if isUnLiftedType (idType binder) then 0 else 1)
		-- For the allocation
		-- If the binder has an unlifted type there is no allocation
229 230 231 232 233 234 235 236

    size_up (Let (Rec pairs) body)
      = nukeScrutDiscount rhs_size		`addSize`
	size_up body				`addSizeN`
	length pairs		-- For the allocation
      where
	rhs_size = foldr (addSize . size_up . snd) sizeZero pairs

237
    size_up (Case (Var v) _ _ alts) 
238
	| v `elem` top_args		-- We are scrutinising an argument variable
239 240 241 242 243
	= 
{-	I'm nuking this special case; BUT see the comment with case alternatives.

	(a) It's too eager.  We don't want to inline a wrapper into a
	    context with no benefit.  
244
	    E.g.  \ x. f (x+x)   	no point in inlining (+) here!
245 246 247 248 249 250

	(b) It's ineffective. Once g's wrapper is inlined, its case-expressions 
	    aren't scrutinising arguments any more

	    case alts of

251
		[alt] -> size_up_alt alt `addSize` SizeIs (_ILIT(0)) (unitBag (v, 1)) (_ILIT(0))
252 253 254 255 256 257 258 259 260
		-- We want to make wrapper-style evaluation look cheap, so that
		-- when we inline a wrapper it doesn't make call site (much) bigger
		-- Otherwise we get nasty phase ordering stuff: 
		--	f x = g x x
		--	h y = ...(f e)...
		-- If we inline g's wrapper, f looks big, and doesn't get inlined
		-- into h; if we inline f first, while it looks small, then g's 
		-- wrapper will get inlined later anyway.  To avoid this nasty
		-- ordering difference, we make (case a of (x,y) -> ...), 
261
		--  *where a is one of the arguments* look free.
262

263 264 265
		other -> 
-}
			 alts_size (foldr addSize sizeOne alt_sizes)	-- The 1 is for the scrutinee
266 267
				   (foldr1 maxSize alt_sizes)

268 269 270
		-- Good to inline if an arg is scrutinised, because
		-- that may eliminate allocation in the caller
		-- And it eliminates the case itself
271

272 273 274
	where
	  alt_sizes = map size_up_alt alts

275 276
		-- alts_size tries to compute a good discount for
		-- the case when we are scrutinising an argument variable
twanvl's avatar
twanvl committed
277 278
	  alts_size (SizeIs tot _tot_disc _tot_scrut)           -- Size of all alternatives
		    (SizeIs max  max_disc  max_scrut)           -- Size of biggest alternative
279
	 	= SizeIs tot (unitBag (v, iBox (_ILIT(1) +# tot -# max)) `unionBags` max_disc) max_scrut
280 281 282 283 284
			-- If the variable is known, we produce a discount that
			-- will take us back to 'max', the size of rh largest alternative
			-- The 1+ is a little discount for reduced allocation in the caller
	  alts_size tot_size _ = tot_size

285 286
    size_up (Case e _ _ alts) = nukeScrutDiscount (size_up e) `addSize` 
			         foldr (addSize . size_up_alt) sizeZero alts
287 288 289 290 291
	  	-- We don't charge for the case itself
		-- It's a strict thing, and the price of the call
		-- is paid by scrut.  Also consider
		--	case f x of DEFAULT -> e
		-- This is just ';'!  Don't charge for it.
292 293

    ------------ 
294 295 296
    size_up_app (App fun arg) args   
	| isTypeArg arg		     = size_up_app fun args
	| otherwise		     = size_up_app fun (arg:args)
297
    size_up_app fun 	      args   = foldr (addSize . nukeScrutDiscount . size_up) 
298
					     (size_up_fun fun args)
299
					     args
300 301 302

	-- A function application with at least one value argument
	-- so if the function is an argument give it an arg-discount
303
	--
304
	-- Also behave specially if the function is a build
305
	--
306
	-- Also if the function is a constant Id (constr or primop)
307 308
	-- compute discounts specially
    size_up_fun (Var fun) args
309 310
      | fun `hasKey` buildIdKey   = buildSize
      | fun `hasKey` augmentIdKey = augmentSize
311
      | otherwise 
312
      = case globalIdDetails fun of
313
	  DataConWorkId dc -> conSizeN dc (valArgCount args)
314

twanvl's avatar
twanvl committed
315
	  FCallId _    -> sizeN opt_UF_DearOp
316 317 318 319 320 321 322 323 324
	  PrimOpId op  -> primOpSize op (valArgCount args)
			  -- foldr addSize (primOpSize op) (map arg_discount args)
			  -- At one time I tried giving an arg-discount if a primop 
			  -- is applied to one of the function's arguments, but it's
			  -- not good.  At the moment, any unlifted-type arg gets a
			  -- 'True' for 'yes I'm evald', so we collect the discount even
			  -- if we know nothing about it.  And just having it in a primop
			  -- doesn't help at all if we don't know something more.

twanvl's avatar
twanvl committed
325
	  _            -> fun_discount fun `addSizeN`
326 327 328 329 330 331 332 333
			  (1 + length (filter (not . exprIsTrivial) args))
				-- The 1+ is for the function itself
				-- Add 1 for each non-trivial arg;
				-- the allocation cost, as in let(rec)
				-- Slight hack here: for constructors the args are almost always
				--	trivial; and for primops they are almost always prim typed
				-- 	We should really only count for non-prim-typed args in the
				--	general case, but that seems too much like hard work
334

twanvl's avatar
twanvl committed
335
    size_up_fun other _ = size_up other
336 337

    ------------ 
twanvl's avatar
twanvl committed
338
    size_up_alt (_con, _bndrs, rhs) = size_up rhs
339 340
 	-- Don't charge for args, so that wrappers look cheap
	-- (See comments about wrappers with Case)
341 342 343

    ------------
	-- We want to record if we're case'ing, or applying, an argument
344
    fun_discount v | v `elem` top_args = SizeIs (_ILIT(0)) (unitBag (v, opt_UF_FunAppDiscount)) (_ILIT(0))
twanvl's avatar
twanvl committed
345
    fun_discount _                     = sizeZero
346 347 348 349 350

    ------------
	-- These addSize things have to be here because
	-- I don't want to give them bOMB_OUT_SIZE as an argument

351 352
    addSizeN TooBig          _  = TooBig
    addSizeN (SizeIs n xs d) m 	= mkSizeIs bOMB_OUT_SIZE (n +# iUnbox m) xs d
353
    
354 355 356 357
    addSize TooBig	      _			= TooBig
    addSize _		      TooBig		= TooBig
    addSize (SizeIs n1 xs d1) (SizeIs n2 ys d2) 
	= mkSizeIs bOMB_OUT_SIZE (n1 +# n2) (xs `unionBags` ys) (d1 +# d2)
358 359 360 361 362 363
\end{code}

Code for manipulating sizes

\begin{code}
data ExprSize = TooBig
364
	      | SizeIs FastInt		-- Size found
365
		       (Bag (Id,Int))	-- Arguments cased herein, and discount for each such
366
		       FastInt		-- Size to subtract if result is scrutinised 
367 368
					-- by a case expression

369 370 371 372 373
-- subtract the discount before deciding whether to bale out. eg. we
-- want to inline a large constructor application into a selector:
--  	tup = (a_1, ..., a_99)
--  	x = case tup of ...
--
twanvl's avatar
twanvl committed
374
mkSizeIs :: FastInt -> FastInt -> Bag (Id, Int) -> FastInt -> ExprSize
375 376 377
mkSizeIs max n xs d | (n -# d) ># max = TooBig
		    | otherwise	      = SizeIs n xs d
 
twanvl's avatar
twanvl committed
378
maxSize :: ExprSize -> ExprSize -> ExprSize
379 380 381 382
maxSize TooBig         _ 				  = TooBig
maxSize _              TooBig				  = TooBig
maxSize s1@(SizeIs n1 _ _) s2@(SizeIs n2 _ _) | n1 ># n2  = s1
					      | otherwise = s2
383

twanvl's avatar
twanvl committed
384 385 386 387
sizeZero, sizeOne :: ExprSize
sizeN :: Int -> ExprSize
conSizeN :: DataCon ->Int -> ExprSize

388 389 390
sizeZero     	= SizeIs (_ILIT(0))  emptyBag (_ILIT(0))
sizeOne      	= SizeIs (_ILIT(1))  emptyBag (_ILIT(0))
sizeN n 	= SizeIs (iUnbox n) emptyBag (_ILIT(0))
391
conSizeN dc n   
392 393
  | isUnboxedTupleCon dc = SizeIs (_ILIT(0)) emptyBag (iUnbox n +# _ILIT(1))
  | otherwise		 = SizeIs (_ILIT(1)) emptyBag (iUnbox n +# _ILIT(1))
394 395
	-- Treat constructors as size 1; we are keen to expose them
	-- (and we charge separately for their args).  We can't treat
396
	-- them as size zero, else we find that (iBox x) has size 1,
397
	-- which is the same as a lone variable; and hence 'v' will 
398
	-- always be replaced by (iBox x), where v is bound to iBox x.
399 400 401 402 403
	--
	-- However, unboxed tuples count as size zero
	-- I found occasions where we had 
	--	f x y z = case op# x y z of { s -> (# s, () #) }
	-- and f wasn't getting inlined
404

twanvl's avatar
twanvl committed
405
primOpSize :: PrimOp -> Int -> ExprSize
406 407
primOpSize op n_args
 | not (primOpIsDupable op) = sizeN opt_UF_DearOp
408
 | not (primOpOutOfLine op) = sizeN (2 - n_args)
409
	-- Be very keen to inline simple primops.
410 411 412 413 414 415 416 417
	-- We give a discount of 1 for each arg so that (op# x y z) costs 2.
	-- We can't make it cost 1, else we'll inline let v = (op# x y z) 
	-- at every use of v, which is excessive.
	--
	-- A good example is:
	--	let x = +# p q in C {x}
	-- Even though x get's an occurrence of 'many', its RHS looks cheap,
	-- and there's a good chance it'll get inlined back into C's RHS. Urgh!
418
 | otherwise	      	    = sizeOne
419

twanvl's avatar
twanvl committed
420
buildSize :: ExprSize
421
buildSize = SizeIs (_ILIT(-2)) emptyBag (_ILIT(4))
422 423 424 425
	-- We really want to inline applications of build
	-- build t (\cn -> e) should cost only the cost of e (because build will be inlined later)
	-- Indeed, we should add a result_discount becuause build is 
	-- very like a constructor.  We don't bother to check that the
426
	-- build is saturated (it usually is).  The "-2" discounts for the \c n, 
427
	-- The "4" is rather arbitrary.
428

twanvl's avatar
twanvl committed
429
augmentSize :: ExprSize
430
augmentSize = SizeIs (_ILIT(-2)) emptyBag (_ILIT(4))
431 432
	-- Ditto (augment t (\cn -> e) ys) should cost only the cost of
	-- e plus ys. The -2 accounts for the \cn 
twanvl's avatar
twanvl committed
433 434 435 436

nukeScrutDiscount :: ExprSize -> ExprSize
nukeScrutDiscount (SizeIs n vs _) = SizeIs n vs (_ILIT(0))
nukeScrutDiscount TooBig          = TooBig
437 438

-- When we return a lambda, give a discount if it's used (applied)
twanvl's avatar
twanvl committed
439 440 441
lamScrutDiscount :: ExprSize -> ExprSize
lamScrutDiscount (SizeIs n vs _) = case opt_UF_FunAppDiscount of { d -> SizeIs n vs (iUnbox d) }
lamScrutDiscount TooBig          = TooBig
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
\end{code}


%************************************************************************
%*									*
\subsection[considerUnfolding]{Given all the info, do (not) do the unfolding}
%*									*
%************************************************************************

We have very limited information about an unfolding expression: (1)~so
many type arguments and so many value arguments expected---for our
purposes here, we assume we've got those.  (2)~A ``size'' or ``cost,''
a single integer.  (3)~An ``argument info'' vector.  For this, what we
have at the moment is a Boolean per argument position that says, ``I
will look with great favour on an explicit constructor in this
position.'' (4)~The ``discount'' to subtract if the expression
is being scrutinised. 

Assuming we have enough type- and value arguments (if not, we give up
immediately), then we see if the ``discounted size'' is below some
(semi-arbitrary) threshold.  It works like this: for every argument
position where we're looking for a constructor AND WE HAVE ONE in our
hands, we get a (again, semi-arbitrary) discount [proportion to the
number of constructors in the type being scrutinized].

If we're in the context of a scrutinee ( \tr{(case <expr > of A .. -> ...;.. )})
and the expression in question will evaluate to a constructor, we use
the computed discount size *for the result only* rather than
computing the argument discounts. Since we know the result of
the expression is going to be taken apart, discounting its size
is more accurate (see @sizeExpr@ above for how this discount size
is computed).

We use this one to avoid exporting inlinings that we ``couldn't possibly
use'' on the other side.  Can be overridden w/ flaggery.
Just the same as smallEnoughToInline, except that it has no actual arguments.

\begin{code}
480
couldBeSmallEnoughToInline :: Int -> CoreExpr -> Bool
481
couldBeSmallEnoughToInline threshold rhs = case calcUnfoldingGuidance threshold rhs of
twanvl's avatar
twanvl committed
482 483
                                                UnfoldNever -> False
                                                _           -> True
484

485 486 487 488
certainlyWillInline :: Unfolding -> Bool
  -- Sees if the unfolding is pretty certain to inline	
certainlyWillInline (CoreUnfolding _ _ _ is_cheap (UnfoldIfGoodArgs n_vals _ size _))
  = is_cheap && size - (n_vals +1) <= opt_UF_UseThreshold
twanvl's avatar
twanvl committed
489
certainlyWillInline _
490
  = False
491 492 493 494

smallEnoughToInline :: Unfolding -> Bool
smallEnoughToInline (CoreUnfolding _ _ _ _ (UnfoldIfGoodArgs _ _ size _))
  = size <= opt_UF_UseThreshold
twanvl's avatar
twanvl committed
495
smallEnoughToInline _
496
  = False
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
\end{code}

%************************************************************************
%*									*
\subsection{callSiteInline}
%*									*
%************************************************************************

This is the key function.  It decides whether to inline a variable at a call site

callSiteInline is used at call sites, so it is a bit more generous.
It's a very important function that embodies lots of heuristics.
A non-WHNF can be inlined if it doesn't occur inside a lambda,
and occurs exactly once or 
    occurs once in each branch of a case and is small

If the thing is in WHNF, there's no danger of duplicating work, 
so we can inline if it occurs once, or is small

516
NOTE: we don't want to inline top-level functions that always diverge.
517
It just makes the code bigger.  Tt turns out that the convenient way to prevent
518 519 520
them inlining is to give them a NOINLINE pragma, which we do in 
StrictAnal.addStrictnessInfoToTopId

521
\begin{code}
522
callSiteInline :: DynFlags
523
	       -> Bool			-- True <=> the Id can be inlined
524
	       -> Id			-- The Id
525
	       -> Bool			-- True if there are are no arguments at all (incl type args)
526
	       -> [Bool]		-- One for each value arg; True if it is interesting
527
	       -> CallCtxt		-- True <=> continuation is interesting
528 529 530
	       -> Maybe CoreExpr	-- Unfolding, if any


531
data CallCtxt = BoringCtxt
532

533 534 535 536 537 538 539 540 541 542
	      | ArgCtxt Bool	-- We're somewhere in the RHS of function with rules
				--	=> be keener to inline
			Int	-- We *are* the argument of a function with this arg discount
				--	=> be keener to inline
		-- INVARIANT: ArgCtxt False 0 ==> BoringCtxt

	      | CaseCtxt	-- We're the scrutinee of a case
				-- that decomposes its scrutinee

instance Outputable CallCtxt where
Ian Lynagh's avatar
Ian Lynagh committed
543 544 545
  ppr BoringCtxt    = ptext (sLit "BoringCtxt")
  ppr (ArgCtxt _ _) = ptext (sLit "ArgCtxt")
  ppr CaseCtxt 	    = ptext (sLit "CaseCtxt")
546 547

callSiteInline dflags active_inline id lone_variable arg_infos cont_info
548
  = case idUnfolding id of {
549
	NoUnfolding -> Nothing ;
twanvl's avatar
twanvl committed
550
	OtherCon _  -> Nothing ;
551 552 553 554 555 556

	CompulsoryUnfolding unf_template -> Just unf_template ;
		-- CompulsoryUnfolding => there is no top-level binding
		-- for these things, so we must inline it.
		-- Only a couple of primop-like things have 
		-- compulsory unfoldings (see MkId.lhs).
557
		-- We don't allow them to be inactive
558

559
	CoreUnfolding unf_template is_top is_value is_cheap guidance ->
560 561 562 563 564

    let
	result | yes_or_no = Just unf_template
	       | otherwise = Nothing

565
	n_val_args  = length arg_infos
566

567
 	yes_or_no = active_inline && is_cheap && consider_safe
568
		-- We consider even the once-in-one-branch
569 570 571 572 573 574
		-- occurrences, because they won't all have been
		-- caught by preInlineUnconditionally.  In particular,
		-- if the occurrence is once inside a lambda, and the
		-- rhs is cheap but not a manifest lambda, then
		-- pre-inline will not have inlined it for fear of
		-- invalidating the occurrence info in the rhs.
575

576
	consider_safe
577 578 579
		-- consider_safe decides whether it's a good idea to
		-- inline something, given that there's no
		-- work-duplication issue (the caller checks that).
580
	  = case guidance of
581
	      UnfoldNever  -> False
582
	      UnfoldIfGoodArgs n_vals_wanted arg_discounts size res_discount
583
		  | enough_args && size <= (n_vals_wanted + 1)
584
			-- Inline unconditionally if there no size increase
585
			-- Size of call is n_vals_wanted (+1 for the function)
586 587 588
		  -> True

	  	  | otherwise
589
		  -> some_benefit && small_enough && inline_enough_args
590

591
		  where
592
		    enough_args	= n_val_args >= n_vals_wanted
593 594 595
                    inline_enough_args =
                      not (dopt Opt_InlineIfEnoughArgs dflags) || enough_args

596 597 598 599 600 601 602 603 604 605

		    some_benefit = or arg_infos || really_interesting_cont
				-- There must be something interesting
				-- about some argument, or the result
				-- context, to make it worth inlining

		    really_interesting_cont 
			| n_val_args <  n_vals_wanted = False	-- Too few args
		    	| n_val_args == n_vals_wanted = interesting_saturated_call
		    	| otherwise		      = True	-- Extra args
606 607 608
		    	-- really_interesting_cont tells if the result of the
		    	-- call is in an interesting context.

609 610
		    interesting_saturated_call 
			= case cont_info of
611 612
			    BoringCtxt -> not is_top && n_vals_wanted > 0	-- Note [Nested functions] 
			    CaseCtxt   -> not lone_variable || not is_value	-- Note [Lone variables]
613 614
			    ArgCtxt {} -> n_vals_wanted > 0 
				-- See Note [Inlining in ArgCtxt]
615

616
		    small_enough = (size - discount) <= opt_UF_UseThreshold
617 618 619
		    discount = computeDiscount n_vals_wanted arg_discounts 
					       res_discount' arg_infos
		    res_discount' = case cont_info of
620 621 622
					BoringCtxt  -> 0
					CaseCtxt    -> res_discount
					ArgCtxt _ _ -> 4 `min` res_discount
623 624 625 626 627 628
			-- res_discount can be very large when a function returns
			-- construtors; but we only want to invoke that large discount
			-- when there's a case continuation.
			-- Otherwise we, rather arbitrarily, threshold it.  Yuk.
			-- But we want to aovid inlining large functions that return 
			-- constructors into contexts that are simply "interesting"
629
		
630
    in    
631
    if dopt Opt_D_dump_inlinings dflags then
632
	pprTrace "Considering inlining"
633
		 (ppr id <+> vcat [text "active:" <+> ppr active_inline,
634
			  	   text "arg infos" <+> ppr arg_infos,
635
				   text "interesting continuation" <+> ppr cont_info,
636
				   text "is value:" <+> ppr is_value,
637
				   text "is cheap:" <+> ppr is_cheap,
638
				   text "guidance" <+> ppr guidance,
639
				   text "ANSWER =" <+> if yes_or_no then text "YES" else text "NO"])
640 641 642 643
		  result
    else
    result
    }
644 645 646 647 648 649 650 651 652 653 654 655 656 657
\end{code}

Note [Nested functions]
~~~~~~~~~~~~~~~~~~~~~~~
If a function has a nested defn we also record some-benefit, on the
grounds that we are often able to eliminate the binding, and hence the
allocation, for the function altogether; this is good for join points.
But this only makes sense for *functions*; inlining a constructor
doesn't help allocation unless the result is scrutinised.  UNLESS the
constructor occurs just once, albeit possibly in multiple case
branches.  Then inlining it doesn't increase allocation, but it does
increase the chance that the constructor won't be allocated at all in
the branches that don't use it.

658 659 660 661 662 663 664 665 666 667 668 669 670
Note [Inlining in ArgCtxt]
~~~~~~~~~~~~~~~~~~~~~~~~~~
The condition (n_vals_wanted > 0) here is very important, because otherwise
we end up inlining top-level stuff into useless places; eg
   x = I# 3#
   f = \y.  g x
This can make a very big difference: it adds 16% to nofib 'integer' allocs,
and 20% to 'power'.

At one stage I replaced this condition by 'True' (leading to the above 
slow-down).  The motivation was test eyeball/inline1.hs; but that seems
to work ok now.

671
Note [Lone variables]
672
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
The "lone-variable" case is important.  I spent ages messing about
with unsatisfactory varaints, but this is nice.  The idea is that if a
variable appears all alone
	as an arg of lazy fn, or rhs	Stop
	as scrutinee of a case		Select
	as arg of a strict fn		ArgOf
AND
	it is bound to a value
then we should not inline it (unless there is some other reason,
e.g. is is the sole occurrence).  That is what is happening at 
the use of 'lone_variable' in 'interesting_saturated_call'.

Why?  At least in the case-scrutinee situation, turning
	let x = (a,b) in case x of y -> ...
into
	let x = (a,b) in case (a,b) of y -> ...
and thence to 
	let x = (a,b) in let y = (a,b) in ...
is bad if the binding for x will remain.

Another example: I discovered that strings
were getting inlined straight back into applications of 'error'
because the latter is strict.
	s = "foo"
	f = \x -> ...(error s)...

Fundamentally such contexts should not encourage inlining because the
context can ``see'' the unfolding of the variable (e.g. case or a
RULE) so there's no gain.  If the thing is bound to a value.

However, watch out:

 * Consider this:
	foo = _inline_ (\n. [n])
	bar = _inline_ (foo 20)
	baz = \n. case bar of { (m:_) -> m + n }
   Here we really want to inline 'bar' so that we can inline 'foo'
   and the whole thing unravels as it should obviously do.  This is 
   important: in the NDP project, 'bar' generates a closure data
   structure rather than a list. 

 * Even a type application or coercion isn't a lone variable.
   Consider
	case $fMonadST @ RealWorld of { :DMonad a b c -> c }
   We had better inline that sucker!  The case won't see through it.

   For now, I'm treating treating a variable applied to types 
   in a *lazy* context "lone". The motivating example was
	f = /\a. \x. BIG
	g = /\a. \y.  h (f a)
   There's no advantage in inlining f here, and perhaps
   a significant disadvantage.  Hence some_val_args in the Stop case
725

726 727 728
\begin{code}
computeDiscount :: Int -> [Int] -> Int -> [Bool] -> Int
computeDiscount n_vals_wanted arg_discounts result_discount arg_infos
729 730
 	-- We multiple the raw discounts (args_discount and result_discount)
	-- ty opt_UnfoldingKeenessFactor because the former have to do with
731 732
	--  *size* whereas the discounts imply that there's some extra 
	--  *efficiency* to be gained (e.g. beta reductions, case reductions) 
733 734 735 736 737
	-- by inlining.

	-- we also discount 1 for each argument passed, because these will
	-- reduce with the lambdas in the function (we count 1 for a lambda
 	-- in size_up).
738 739 740
  = 1 +			-- Discount of 1 because the result replaces the call
			-- so we count 1 for the function itself
    length (take n_vals_wanted arg_infos) +
741 742 743
			-- Discount of 1 for each arg supplied, because the 
			-- result replaces the call
    round (opt_UF_KeenessFactor * 
744
	   fromIntegral (arg_discount + result_discount))
745 746 747 748 749 750
  where
    arg_discount = sum (zipWith mk_arg_discount arg_discounts arg_infos)

    mk_arg_discount discount is_evald | is_evald  = discount
				      | otherwise = 0
\end{code}
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

%************************************************************************
%*									*
	The Very Simple Optimiser
%*									*
%************************************************************************


\begin{code}
simpleOptExpr :: Subst -> CoreExpr -> CoreExpr
-- Return an occur-analysed and slightly optimised expression
-- The optimisation is very straightforward: just
-- inline non-recursive bindings that are used only once, 
-- or wheere the RHS is trivial

simpleOptExpr subst expr
  = go subst (occurAnalyseExpr expr)
  where
    go subst (Var v)          = lookupIdSubst subst v
    go subst (App e1 e2)      = App (go subst e1) (go subst e2)
    go subst (Type ty)        = Type (substTy subst ty)
    go _     (Lit lit)        = Lit lit
    go subst (Note note e)    = Note note (go subst e)
    go subst (Cast e co)      = Cast (go subst e) (substTy subst co)
    go subst (Let bind body)  = go_bind subst bind body
    go subst (Lam bndr body)  = Lam bndr' (go subst' body)
		              where
			        (subst', bndr') = substBndr subst bndr

    go subst (Case e b ty as) = Case (go subst e) b' 
				     (substTy subst ty)
				     (map (go_alt subst') as)
			      where
			  	 (subst', b') = substBndr subst b


    ----------------------
    go_alt subst (con, bndrs, rhs) = (con, bndrs', go subst' rhs)
				 where
				   (subst', bndrs') = substBndrs subst bndrs

    ----------------------
    go_bind subst (Rec prs) body = Let (Rec (bndrs' `zip` rhss'))
				       (go subst' body)
			    where
			      (bndrs, rhss)    = unzip prs
			      (subst', bndrs') = substRecBndrs subst bndrs
			      rhss'	       = map (go subst') rhss

    go_bind subst (NonRec b r) body = go_nonrec subst b (go subst r) body

    ----------------------
    go_nonrec subst b (Type ty') body
      | isTyVar b = go (extendTvSubst subst b ty') body
805
	-- let a::* = TYPE ty in <body>
806
    go_nonrec subst b r' body
807
      | isId b	-- let x = e in <body>
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
      , exprIsTrivial r' || safe_to_inline (idOccInfo b)
      = go (extendIdSubst subst b r') body
    go_nonrec subst b r' body
      = Let (NonRec b' r') (go subst' body)
      where
	(subst', b') = substBndr subst b

    ----------------------
	-- Unconditionally safe to inline
    safe_to_inline :: OccInfo -> Bool
    safe_to_inline IAmDead                  = True
    safe_to_inline (OneOcc in_lam one_br _) = not in_lam && one_br
    safe_to_inline (IAmALoopBreaker {})     = False
    safe_to_inline NoOccInfo                = False
\end{code}