+ 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
+
+ size_up (Case (Var v) _ _ alts)
+ | v `elem` top_args -- We are scrutinising an argument variable
+ =
+{- 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.
+ E.g. \ x. f (x+x) no point in inlining (+) here!
+
+ (b) It's ineffective. Once g's wrapper is inlined, its case-expressions
+ aren't scrutinising arguments any more
+
+ case alts of
+
+ [alt] -> size_up_alt alt `addSize` SizeIs 0# (unitBag (v, 1)) 0#
+ -- 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) -> ...),
+ -- *where a is one of the arguments* look free.
+
+ other ->
+-}
+ alts_size (foldr addSize sizeOne alt_sizes) -- The 1 is for the scrutinee
+ (foldr1 maxSize alt_sizes)
+
+ -- Good to inline if an arg is scrutinised, because
+ -- that may eliminate allocation in the caller
+ -- And it eliminates the case itself
+
+ where
+ alt_sizes = map size_up_alt alts
+
+ -- alts_size tries to compute a good discount for
+ -- the case when we are scrutinising an argument variable
+ alts_size (SizeIs tot tot_disc tot_scrut) -- Size of all alternatives
+ (SizeIs max max_disc max_scrut) -- Size of biggest alternative
+ = SizeIs tot (unitBag (v, iBox (_ILIT 1 +# tot -# max)) `unionBags` max_disc) max_scrut
+ -- 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
+
+-- gaw 2004
+ size_up (Case e _ _ alts) = nukeScrutDiscount (size_up e) `addSize`
+ foldr (addSize . size_up_alt) sizeZero alts
+ -- 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.
+
+ ------------
+ size_up_app (App fun arg) args
+ | isTypeArg arg = size_up_app fun args
+ | otherwise = size_up_app fun (arg:args)
+ size_up_app fun args = foldr (addSize . nukeScrutDiscount . size_up)
+ (size_up_fun fun args)
+ args
+
+ -- A function application with at least one value argument
+ -- so if the function is an argument give it an arg-discount
+ --
+ -- Also behave specially if the function is a build
+ --
+ -- Also if the function is a constant Id (constr or primop)
+ -- compute discounts specially
+ size_up_fun (Var fun) args
+ | fun `hasKey` buildIdKey = buildSize
+ | fun `hasKey` augmentIdKey = augmentSize
+ | otherwise
+ = case globalIdDetails fun of
+ DataConWorkId dc -> conSizeN dc (valArgCount args)
+
+ FCallId fc -> sizeN opt_UF_DearOp
+ 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.
+
+ other -> fun_discount fun `addSizeN`
+ (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
+
+ size_up_fun other args = size_up other
+
+ ------------
+ size_up_alt (con, bndrs, rhs) = size_up rhs
+ -- Don't charge for args, so that wrappers look cheap
+ -- (See comments about wrappers with Case)
+
+ ------------
+ -- We want to record if we're case'ing, or applying, an argument
+ fun_discount v | v `elem` top_args = SizeIs 0# (unitBag (v, opt_UF_FunAppDiscount)) 0#
+ fun_discount other = sizeZero