{-# LANGUAGE CPP #-}
module CoreUnfold (
Unfolding, UnfoldingGuidance,
noUnfolding, mkImplicitUnfolding,
mkUnfolding, mkCoreUnfolding,
mkTopUnfolding, mkSimpleUnfolding, mkWorkerUnfolding,
mkInlineUnfolding, mkInlineUnfoldingWithArity,
mkInlinableUnfolding, mkWwInlineRule,
mkCompulsoryUnfolding, mkDFunUnfolding,
specUnfolding,
ArgSummary(..),
couldBeSmallEnoughToInline, inlineBoringOk,
certainlyWillInline, smallEnoughToInline,
callSiteInline, CallCtxt(..),
exprIsConApp_maybe, exprIsLiteral_maybe
) where
#include "HsVersions.h"
import GhcPrelude
import DynFlags
import CoreSyn
import PprCore ()
import OccurAnal ( occurAnalyseExpr )
import CoreOpt
import CoreArity ( manifestArity )
import CoreUtils
import Id
import Demand ( isBottomingSig )
import DataCon
import Literal
import PrimOp
import IdInfo
import BasicTypes ( Arity, InlineSpec(..), inlinePragmaSpec )
import Type
import PrelNames
import TysPrim ( realWorldStatePrimTy )
import Bag
import Util
import Outputable
import ForeignCall
import Name
import qualified Data.ByteString as BS
import Data.List
mkTopUnfolding :: DynFlags -> Bool -> CoreExpr -> Unfolding
mkTopUnfolding :: DynFlags -> Bool -> CoreExpr -> Unfolding
mkTopUnfolding dflags :: DynFlags
dflags is_bottoming :: Bool
is_bottoming rhs :: CoreExpr
rhs
= DynFlags
-> UnfoldingSource -> Bool -> Bool -> CoreExpr -> Unfolding
mkUnfolding DynFlags
dflags UnfoldingSource
InlineRhs Bool
True Bool
is_bottoming CoreExpr
rhs
mkImplicitUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkImplicitUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkImplicitUnfolding dflags :: DynFlags
dflags expr :: CoreExpr
expr
= DynFlags -> Bool -> CoreExpr -> Unfolding
mkTopUnfolding DynFlags
dflags Bool
False (DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags CoreExpr
expr)
mkSimpleUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkSimpleUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkSimpleUnfolding dflags :: DynFlags
dflags rhs :: CoreExpr
rhs
= DynFlags
-> UnfoldingSource -> Bool -> Bool -> CoreExpr -> Unfolding
mkUnfolding DynFlags
dflags UnfoldingSource
InlineRhs Bool
False Bool
False CoreExpr
rhs
mkDFunUnfolding :: [Var] -> DataCon -> [CoreExpr] -> Unfolding
mkDFunUnfolding :: [Var] -> DataCon -> [CoreExpr] -> Unfolding
mkDFunUnfolding bndrs :: [Var]
bndrs con :: DataCon
con ops :: [CoreExpr]
ops
= DFunUnfolding :: [Var] -> DataCon -> [CoreExpr] -> Unfolding
DFunUnfolding { df_bndrs :: [Var]
df_bndrs = [Var]
bndrs
, df_con :: DataCon
df_con = DataCon
con
, df_args :: [CoreExpr]
df_args = (CoreExpr -> CoreExpr) -> [CoreExpr] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map CoreExpr -> CoreExpr
occurAnalyseExpr [CoreExpr]
ops }
mkWwInlineRule :: DynFlags -> CoreExpr -> Arity -> Unfolding
mkWwInlineRule :: DynFlags -> CoreExpr -> Arity -> Unfolding
mkWwInlineRule dflags :: DynFlags
dflags expr :: CoreExpr
expr arity :: Arity
arity
= UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable Bool
True
(DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags CoreExpr
expr)
(UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = Arity
arity, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boringCxtNotOk })
mkCompulsoryUnfolding :: CoreExpr -> Unfolding
mkCompulsoryUnfolding :: CoreExpr -> Unfolding
mkCompulsoryUnfolding expr :: CoreExpr
expr
= UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineCompulsory Bool
True
(DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
unsafeGlobalDynFlags CoreExpr
expr)
(UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = 0
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk, ug_boring_ok :: Bool
ug_boring_ok = Bool
boringCxtOk })
mkWorkerUnfolding :: DynFlags -> (CoreExpr -> CoreExpr) -> Unfolding -> Unfolding
mkWorkerUnfolding :: DynFlags -> (CoreExpr -> CoreExpr) -> Unfolding -> Unfolding
mkWorkerUnfolding dflags :: DynFlags
dflags work_fn :: CoreExpr -> CoreExpr
work_fn
(CoreUnfolding { uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src, uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
tmpl
, uf_is_top :: Unfolding -> Bool
uf_is_top = Bool
top_lvl })
| UnfoldingSource -> Bool
isStableSource UnfoldingSource
src
= UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreExpr
new_tmpl UnfoldingGuidance
guidance
where
new_tmpl :: CoreExpr
new_tmpl = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags (CoreExpr -> CoreExpr
work_fn CoreExpr
tmpl)
guidance :: UnfoldingGuidance
guidance = DynFlags -> Bool -> CoreExpr -> UnfoldingGuidance
calcUnfoldingGuidance DynFlags
dflags Bool
False CoreExpr
new_tmpl
mkWorkerUnfolding _ _ _ = Unfolding
noUnfolding
mkInlineUnfolding :: CoreExpr -> Unfolding
mkInlineUnfolding :: CoreExpr -> Unfolding
mkInlineUnfolding expr :: CoreExpr
expr
= UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable
Bool
True
CoreExpr
expr' UnfoldingGuidance
guide
where
expr' :: CoreExpr
expr' = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
unsafeGlobalDynFlags CoreExpr
expr
guide :: UnfoldingGuidance
guide = UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = CoreExpr -> Arity
manifestArity CoreExpr
expr'
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boring_ok }
boring_ok :: Bool
boring_ok = CoreExpr -> Bool
inlineBoringOk CoreExpr
expr'
mkInlineUnfoldingWithArity :: Arity -> CoreExpr -> Unfolding
mkInlineUnfoldingWithArity :: Arity -> CoreExpr -> Unfolding
mkInlineUnfoldingWithArity arity :: Arity
arity expr :: CoreExpr
expr
= UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable
Bool
True
CoreExpr
expr' UnfoldingGuidance
guide
where
expr' :: CoreExpr
expr' = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
unsafeGlobalDynFlags CoreExpr
expr
guide :: UnfoldingGuidance
guide = UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = Arity
arity
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
needSaturated
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boring_ok }
boring_ok :: Bool
boring_ok | Arity
arity Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = Bool
True
| Bool
otherwise = CoreExpr -> Bool
inlineBoringOk CoreExpr
expr'
mkInlinableUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkInlinableUnfolding :: DynFlags -> CoreExpr -> Unfolding
mkInlinableUnfolding dflags :: DynFlags
dflags expr :: CoreExpr
expr
= DynFlags
-> UnfoldingSource -> Bool -> Bool -> CoreExpr -> Unfolding
mkUnfolding DynFlags
dflags UnfoldingSource
InlineStable Bool
False Bool
False CoreExpr
expr'
where
expr' :: CoreExpr
expr' = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags CoreExpr
expr
specUnfolding :: DynFlags -> [Var] -> (CoreExpr -> CoreExpr) -> Arity
-> Unfolding -> Unfolding
specUnfolding :: DynFlags
-> [Var]
-> (CoreExpr -> CoreExpr)
-> Arity
-> Unfolding
-> Unfolding
specUnfolding dflags :: DynFlags
dflags spec_bndrs :: [Var]
spec_bndrs spec_app :: CoreExpr -> CoreExpr
spec_app arity_decrease :: Arity
arity_decrease
df :: Unfolding
df@(DFunUnfolding { df_bndrs :: Unfolding -> [Var]
df_bndrs = [Var]
old_bndrs, df_con :: Unfolding -> DataCon
df_con = DataCon
con, df_args :: Unfolding -> [CoreExpr]
df_args = [CoreExpr]
args })
= ASSERT2( arity_decrease == count isId old_bndrs - count isId spec_bndrs, ppr df )
[Var] -> DataCon -> [CoreExpr] -> Unfolding
mkDFunUnfolding [Var]
spec_bndrs DataCon
con ((CoreExpr -> CoreExpr) -> [CoreExpr] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map CoreExpr -> CoreExpr
spec_arg [CoreExpr]
args)
where
spec_arg :: CoreExpr -> CoreExpr
spec_arg arg :: CoreExpr
arg = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags (CoreExpr -> CoreExpr
spec_app ([Var] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Var]
old_bndrs CoreExpr
arg))
specUnfolding dflags :: DynFlags
dflags spec_bndrs :: [Var]
spec_bndrs spec_app :: CoreExpr -> CoreExpr
spec_app arity_decrease :: Arity
arity_decrease
(CoreUnfolding { uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src, uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
tmpl
, uf_is_top :: Unfolding -> Bool
uf_is_top = Bool
top_lvl
, uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfoldingGuidance
old_guidance })
| UnfoldingSource -> Bool
isStableSource UnfoldingSource
src
, UnfWhen { ug_arity :: UnfoldingGuidance -> Arity
ug_arity = Arity
old_arity
, ug_unsat_ok :: UnfoldingGuidance -> Bool
ug_unsat_ok = Bool
unsat_ok
, ug_boring_ok :: UnfoldingGuidance -> Bool
ug_boring_ok = Bool
boring_ok } <- UnfoldingGuidance
old_guidance
= let guidance :: UnfoldingGuidance
guidance = UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = Arity
old_arity Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
arity_decrease
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unsat_ok
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boring_ok }
new_tmpl :: CoreExpr
new_tmpl = DynFlags -> CoreExpr -> CoreExpr
simpleOptExpr DynFlags
dflags ([Var] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Var]
spec_bndrs (CoreExpr -> CoreExpr
spec_app CoreExpr
tmpl))
in UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreExpr
new_tmpl UnfoldingGuidance
guidance
specUnfolding _ _ _ _ _ = Unfolding
noUnfolding
mkCoreUnfolding :: UnfoldingSource -> Bool -> CoreExpr
-> UnfoldingGuidance -> Unfolding
mkCoreUnfolding :: UnfoldingSource
-> Bool -> CoreExpr -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding src :: UnfoldingSource
src top_lvl :: Bool
top_lvl expr :: CoreExpr
expr guidance :: UnfoldingGuidance
guidance
= CoreUnfolding :: CoreExpr
-> UnfoldingSource
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> UnfoldingGuidance
-> Unfolding
CoreUnfolding { uf_tmpl :: CoreExpr
uf_tmpl = CoreExpr -> CoreExpr
occurAnalyseExpr CoreExpr
expr,
uf_src :: UnfoldingSource
uf_src = UnfoldingSource
src,
uf_is_top :: Bool
uf_is_top = Bool
top_lvl,
uf_is_value :: Bool
uf_is_value = CoreExpr -> Bool
exprIsHNF CoreExpr
expr,
uf_is_conlike :: Bool
uf_is_conlike = CoreExpr -> Bool
exprIsConLike CoreExpr
expr,
uf_is_work_free :: Bool
uf_is_work_free = CoreExpr -> Bool
exprIsWorkFree CoreExpr
expr,
uf_expandable :: Bool
uf_expandable = CoreExpr -> Bool
exprIsExpandable CoreExpr
expr,
uf_guidance :: UnfoldingGuidance
uf_guidance = UnfoldingGuidance
guidance }
mkUnfolding :: DynFlags -> UnfoldingSource
-> Bool
-> Bool
-> CoreExpr
-> Unfolding
mkUnfolding :: DynFlags
-> UnfoldingSource -> Bool -> Bool -> CoreExpr -> Unfolding
mkUnfolding dflags :: DynFlags
dflags src :: UnfoldingSource
src is_top_lvl :: Bool
is_top_lvl is_bottoming :: Bool
is_bottoming expr :: CoreExpr
expr
= CoreUnfolding :: CoreExpr
-> UnfoldingSource
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> UnfoldingGuidance
-> Unfolding
CoreUnfolding { uf_tmpl :: CoreExpr
uf_tmpl = CoreExpr -> CoreExpr
occurAnalyseExpr CoreExpr
expr,
uf_src :: UnfoldingSource
uf_src = UnfoldingSource
src,
uf_is_top :: Bool
uf_is_top = Bool
is_top_lvl,
uf_is_value :: Bool
uf_is_value = CoreExpr -> Bool
exprIsHNF CoreExpr
expr,
uf_is_conlike :: Bool
uf_is_conlike = CoreExpr -> Bool
exprIsConLike CoreExpr
expr,
uf_expandable :: Bool
uf_expandable = CoreExpr -> Bool
exprIsExpandable CoreExpr
expr,
uf_is_work_free :: Bool
uf_is_work_free = CoreExpr -> Bool
exprIsWorkFree CoreExpr
expr,
uf_guidance :: UnfoldingGuidance
uf_guidance = UnfoldingGuidance
guidance }
where
is_top_bottoming :: Bool
is_top_bottoming = Bool
is_top_lvl Bool -> Bool -> Bool
&& Bool
is_bottoming
guidance :: UnfoldingGuidance
guidance = DynFlags -> Bool -> CoreExpr -> UnfoldingGuidance
calcUnfoldingGuidance DynFlags
dflags Bool
is_top_bottoming CoreExpr
expr
inlineBoringOk :: CoreExpr -> Bool
inlineBoringOk :: CoreExpr -> Bool
inlineBoringOk e :: CoreExpr
e
= Arity -> CoreExpr -> Bool
go 0 CoreExpr
e
where
go :: Int -> CoreExpr -> Bool
go :: Arity -> CoreExpr -> Bool
go credit :: Arity
credit (Lam x :: Var
x e :: CoreExpr
e) | Var -> Bool
isId Var
x = Arity -> CoreExpr -> Bool
go (Arity
creditArity -> Arity -> Arity
forall a. Num a => a -> a -> a
+1) CoreExpr
e
| Bool
otherwise = Arity -> CoreExpr -> Bool
go Arity
credit CoreExpr
e
go credit :: Arity
credit (App f :: CoreExpr
f (Type {})) = Arity -> CoreExpr -> Bool
go Arity
credit CoreExpr
f
go credit :: Arity
credit (App f :: CoreExpr
f a :: CoreExpr
a) | Arity
credit Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0
, CoreExpr -> Bool
exprIsTrivial CoreExpr
a = Arity -> CoreExpr -> Bool
go (Arity
creditArity -> Arity -> Arity
forall a. Num a => a -> a -> a
-1) CoreExpr
f
go credit :: Arity
credit (Tick _ e :: CoreExpr
e) = Arity -> CoreExpr -> Bool
go Arity
credit CoreExpr
e
go credit :: Arity
credit (Cast e :: CoreExpr
e _) = Arity -> CoreExpr -> Bool
go Arity
credit CoreExpr
e
go _ (Var {}) = Bool
boringCxtOk
go _ _ = Bool
boringCxtNotOk
calcUnfoldingGuidance
:: DynFlags
-> Bool
-> CoreExpr
-> UnfoldingGuidance
calcUnfoldingGuidance :: DynFlags -> Bool -> CoreExpr -> UnfoldingGuidance
calcUnfoldingGuidance dflags :: DynFlags
dflags is_top_bottoming :: Bool
is_top_bottoming (Tick t :: Tickish Var
t expr :: CoreExpr
expr)
| Bool -> Bool
not (Tickish Var -> Bool
forall id. Tickish id -> Bool
tickishIsCode Tickish Var
t)
= DynFlags -> Bool -> CoreExpr -> UnfoldingGuidance
calcUnfoldingGuidance DynFlags
dflags Bool
is_top_bottoming CoreExpr
expr
calcUnfoldingGuidance dflags :: DynFlags
dflags is_top_bottoming :: Bool
is_top_bottoming expr :: CoreExpr
expr
= case DynFlags -> Arity -> [Var] -> CoreExpr -> ExprSize
sizeExpr DynFlags
dflags Arity
bOMB_OUT_SIZE [Var]
val_bndrs CoreExpr
body of
TooBig -> UnfoldingGuidance
UnfNever
SizeIs size :: Arity
size cased_bndrs :: Bag (Var, Arity)
cased_bndrs scrut_discount :: Arity
scrut_discount
| CoreExpr -> Arity -> Arity -> Bool
uncondInline CoreExpr
expr Arity
n_val_bndrs Arity
size
-> UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boringCxtOk
, ug_arity :: Arity
ug_arity = Arity
n_val_bndrs }
| Bool
is_top_bottoming
-> UnfoldingGuidance
UnfNever
| Bool
otherwise
-> UnfIfGoodArgs :: [Arity] -> Arity -> Arity -> UnfoldingGuidance
UnfIfGoodArgs { ug_args :: [Arity]
ug_args = (Var -> Arity) -> [Var] -> [Arity]
forall a b. (a -> b) -> [a] -> [b]
map (Bag (Var, Arity) -> Var -> Arity
mk_discount Bag (Var, Arity)
cased_bndrs) [Var]
val_bndrs
, ug_size :: Arity
ug_size = Arity
size
, ug_res :: Arity
ug_res = Arity
scrut_discount }
where
(bndrs :: [Var]
bndrs, body :: CoreExpr
body) = CoreExpr -> ([Var], CoreExpr)
forall b. Expr b -> ([b], Expr b)
collectBinders CoreExpr
expr
bOMB_OUT_SIZE :: Arity
bOMB_OUT_SIZE = DynFlags -> Arity
ufCreationThreshold DynFlags
dflags
val_bndrs :: [Var]
val_bndrs = (Var -> Bool) -> [Var] -> [Var]
forall a. (a -> Bool) -> [a] -> [a]
filter Var -> Bool
isId [Var]
bndrs
n_val_bndrs :: Arity
n_val_bndrs = [Var] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Var]
val_bndrs
mk_discount :: Bag (Id,Int) -> Id -> Int
mk_discount :: Bag (Var, Arity) -> Var -> Arity
mk_discount cbs :: Bag (Var, Arity)
cbs bndr :: Var
bndr = (Arity -> (Var, Arity) -> Arity)
-> Arity -> Bag (Var, Arity) -> Arity
forall r a. (r -> a -> r) -> r -> Bag a -> r
foldlBag Arity -> (Var, Arity) -> Arity
combine 0 Bag (Var, Arity)
cbs
where
combine :: Arity -> (Var, Arity) -> Arity
combine acc :: Arity
acc (bndr' :: Var
bndr', disc :: Arity
disc)
| Var
bndr Var -> Var -> Bool
forall a. Eq a => a -> a -> Bool
== Var
bndr' = Arity
acc Arity -> Arity -> Arity
`plus_disc` Arity
disc
| Bool
otherwise = Arity
acc
plus_disc :: Int -> Int -> Int
plus_disc :: Arity -> Arity -> Arity
plus_disc | Type -> Bool
isFunTy (Var -> Type
idType Var
bndr) = Arity -> Arity -> Arity
forall a. Ord a => a -> a -> a
max
| Bool
otherwise = Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
(+)
uncondInline :: CoreExpr -> Arity -> Int -> Bool
uncondInline :: CoreExpr -> Arity -> Arity -> Bool
uncondInline rhs :: CoreExpr
rhs arity :: Arity
arity size :: Arity
size
| Arity
arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = Arity
size Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
<= 10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (Arity
arity Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 1)
| Bool
otherwise = CoreExpr -> Bool
exprIsTrivial CoreExpr
rhs
sizeExpr :: DynFlags
-> Int
-> [Id]
-> CoreExpr
-> ExprSize
sizeExpr :: DynFlags -> Arity -> [Var] -> CoreExpr -> ExprSize
sizeExpr dflags :: DynFlags
dflags bOMB_OUT_SIZE :: Arity
bOMB_OUT_SIZE top_args :: [Var]
top_args expr :: CoreExpr
expr
= CoreExpr -> ExprSize
size_up CoreExpr
expr
where
size_up :: CoreExpr -> ExprSize
size_up (Cast e :: CoreExpr
e _) = CoreExpr -> ExprSize
size_up CoreExpr
e
size_up (Tick _ e :: CoreExpr
e) = CoreExpr -> ExprSize
size_up CoreExpr
e
size_up (Type _) = ExprSize
sizeZero
size_up (Coercion _) = ExprSize
sizeZero
size_up (Lit lit :: Literal
lit) = Arity -> ExprSize
sizeN (Literal -> Arity
litSize Literal
lit)
size_up (Var f :: Var
f) | Var -> Bool
isRealWorldId Var
f = ExprSize
sizeZero
| Bool
otherwise = Var -> [CoreExpr] -> Arity -> ExprSize
size_up_call Var
f [] 0
size_up (App fun :: CoreExpr
fun arg :: CoreExpr
arg)
| CoreExpr -> Bool
forall b. Expr b -> Bool
isTyCoArg CoreExpr
arg = CoreExpr -> ExprSize
size_up CoreExpr
fun
| Bool
otherwise = CoreExpr -> ExprSize
size_up CoreExpr
arg ExprSize -> ExprSize -> ExprSize
`addSizeNSD`
CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
fun [CoreExpr
arg] (if CoreExpr -> Bool
forall b. Expr b -> Bool
isRealWorldExpr CoreExpr
arg then 1 else 0)
size_up (Lam b :: Var
b e :: CoreExpr
e)
| Var -> Bool
isId Var
b Bool -> Bool -> Bool
&& Bool -> Bool
not (Var -> Bool
isRealWorldId Var
b) = DynFlags -> ExprSize -> ExprSize
lamScrutDiscount DynFlags
dflags (CoreExpr -> ExprSize
size_up CoreExpr
e ExprSize -> Arity -> ExprSize
`addSizeN` 10)
| Bool
otherwise = CoreExpr -> ExprSize
size_up CoreExpr
e
size_up (Let (NonRec binder :: Var
binder rhs :: CoreExpr
rhs) body :: CoreExpr
body)
= (Var, CoreExpr) -> ExprSize
size_up_rhs (Var
binder, CoreExpr
rhs) ExprSize -> ExprSize -> ExprSize
`addSizeNSD`
CoreExpr -> ExprSize
size_up CoreExpr
body ExprSize -> Arity -> ExprSize
`addSizeN`
Var -> Arity
forall p. Num p => Var -> p
size_up_alloc Var
binder
size_up (Let (Rec pairs :: [(Var, CoreExpr)]
pairs) body :: CoreExpr
body)
= ((Var, CoreExpr) -> ExprSize -> ExprSize)
-> ExprSize -> [(Var, CoreExpr)] -> ExprSize
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (ExprSize -> ExprSize -> ExprSize
addSizeNSD (ExprSize -> ExprSize -> ExprSize)
-> ((Var, CoreExpr) -> ExprSize)
-> (Var, CoreExpr)
-> ExprSize
-> ExprSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var, CoreExpr) -> ExprSize
size_up_rhs)
(CoreExpr -> ExprSize
size_up CoreExpr
body ExprSize -> Arity -> ExprSize
`addSizeN` [Arity] -> Arity
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((Var, CoreExpr) -> Arity) -> [(Var, CoreExpr)] -> [Arity]
forall a b. (a -> b) -> [a] -> [b]
map (Var -> Arity
forall p. Num p => Var -> p
size_up_alloc (Var -> Arity)
-> ((Var, CoreExpr) -> Var) -> (Var, CoreExpr) -> Arity
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst) [(Var, CoreExpr)]
pairs))
[(Var, CoreExpr)]
pairs
size_up (Case e :: CoreExpr
e _ _ alts :: [Alt Var]
alts)
| [Alt Var] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Alt Var]
alts
= CoreExpr -> ExprSize
size_up CoreExpr
e
size_up (Case e :: CoreExpr
e _ _ alts :: [Alt Var]
alts)
| Just v :: Var
v <- CoreExpr -> Maybe Var
forall b. Expr b -> Maybe Var
is_top_arg CoreExpr
e
= let
alt_sizes :: [ExprSize]
alt_sizes = (Alt Var -> ExprSize) -> [Alt Var] -> [ExprSize]
forall a b. (a -> b) -> [a] -> [b]
map Alt Var -> ExprSize
size_up_alt [Alt Var]
alts
alts_size :: ExprSize -> ExprSize -> ExprSize
alts_size (SizeIs tot :: Arity
tot tot_disc :: Bag (Var, Arity)
tot_disc tot_scrut :: Arity
tot_scrut)
(SizeIs max :: Arity
max _ _)
= Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
tot ((Var, Arity) -> Bag (Var, Arity)
forall a. a -> Bag a
unitBag (Var
v, 20 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
tot Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
max)
Bag (Var, Arity) -> Bag (Var, Arity) -> Bag (Var, Arity)
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag (Var, Arity)
tot_disc) Arity
tot_scrut
alts_size tot_size :: ExprSize
tot_size _ = ExprSize
tot_size
in
ExprSize -> ExprSize -> ExprSize
alts_size ((ExprSize -> ExprSize -> ExprSize) -> [ExprSize] -> ExprSize
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 ExprSize -> ExprSize -> ExprSize
addAltSize [ExprSize]
alt_sizes)
((ExprSize -> ExprSize -> ExprSize) -> [ExprSize] -> ExprSize
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 ExprSize -> ExprSize -> ExprSize
maxSize [ExprSize]
alt_sizes)
where
is_top_arg :: Expr b -> Maybe Var
is_top_arg (Var v :: Var
v) | Var
v Var -> [Var] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Var]
top_args = Var -> Maybe Var
forall a. a -> Maybe a
Just Var
v
is_top_arg (Cast e :: Expr b
e _) = Expr b -> Maybe Var
is_top_arg Expr b
e
is_top_arg _ = Maybe Var
forall a. Maybe a
Nothing
size_up (Case e :: CoreExpr
e _ _ alts :: [Alt Var]
alts) = CoreExpr -> ExprSize
size_up CoreExpr
e ExprSize -> ExprSize -> ExprSize
`addSizeNSD`
(Alt Var -> ExprSize -> ExprSize)
-> ExprSize -> [Alt Var] -> ExprSize
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (ExprSize -> ExprSize -> ExprSize
addAltSize (ExprSize -> ExprSize -> ExprSize)
-> (Alt Var -> ExprSize) -> Alt Var -> ExprSize -> ExprSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Alt Var -> ExprSize
size_up_alt) ExprSize
case_size [Alt Var]
alts
where
case_size :: ExprSize
case_size
| CoreExpr -> Bool
forall b. Expr b -> Bool
is_inline_scrut CoreExpr
e, [Alt Var] -> Arity -> Bool
forall a. [a] -> Arity -> Bool
lengthAtMost [Alt Var]
alts 1 = Arity -> ExprSize
sizeN (-10)
| Bool
otherwise = ExprSize
sizeZero
is_inline_scrut :: Expr b -> Bool
is_inline_scrut (Var v :: Var
v) = HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Var -> Type
idType Var
v)
is_inline_scrut scrut :: Expr b
scrut
| (Var f :: Var
f, _) <- Expr b -> (Expr b, [Expr b])
forall b. Expr b -> (Expr b, [Expr b])
collectArgs Expr b
scrut
= case Var -> IdDetails
idDetails Var
f of
FCallId fc :: ForeignCall
fc -> Bool -> Bool
not (ForeignCall -> Bool
isSafeForeignCall ForeignCall
fc)
PrimOpId op :: PrimOp
op -> Bool -> Bool
not (PrimOp -> Bool
primOpOutOfLine PrimOp
op)
_other :: IdDetails
_other -> Bool
False
| Bool
otherwise
= Bool
False
size_up_rhs :: (Var, CoreExpr) -> ExprSize
size_up_rhs (bndr :: Var
bndr, rhs :: CoreExpr
rhs)
| Just join_arity :: Arity
join_arity <- Var -> Maybe Arity
isJoinId_maybe Var
bndr
, (_bndrs :: [Var]
_bndrs, body :: CoreExpr
body) <- Arity -> CoreExpr -> ([Var], CoreExpr)
forall b. Arity -> Expr b -> ([b], Expr b)
collectNBinders Arity
join_arity CoreExpr
rhs
= CoreExpr -> ExprSize
size_up CoreExpr
body
| Bool
otherwise
= CoreExpr -> ExprSize
size_up CoreExpr
rhs
size_up_app :: CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app (App fun :: CoreExpr
fun arg :: CoreExpr
arg) args :: [CoreExpr]
args voids :: Arity
voids
| CoreExpr -> Bool
forall b. Expr b -> Bool
isTyCoArg CoreExpr
arg = CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
fun [CoreExpr]
args Arity
voids
| CoreExpr -> Bool
forall b. Expr b -> Bool
isRealWorldExpr CoreExpr
arg = CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
fun (CoreExpr
argCoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
:[CoreExpr]
args) (Arity
voids Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 1)
| Bool
otherwise = CoreExpr -> ExprSize
size_up CoreExpr
arg ExprSize -> ExprSize -> ExprSize
`addSizeNSD`
CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
fun (CoreExpr
argCoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
:[CoreExpr]
args) Arity
voids
size_up_app (Var fun :: Var
fun) args :: [CoreExpr]
args voids :: Arity
voids = Var -> [CoreExpr] -> Arity -> ExprSize
size_up_call Var
fun [CoreExpr]
args Arity
voids
size_up_app (Tick _ expr :: CoreExpr
expr) args :: [CoreExpr]
args voids :: Arity
voids = CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
expr [CoreExpr]
args Arity
voids
size_up_app (Cast expr :: CoreExpr
expr _) args :: [CoreExpr]
args voids :: Arity
voids = CoreExpr -> [CoreExpr] -> Arity -> ExprSize
size_up_app CoreExpr
expr [CoreExpr]
args Arity
voids
size_up_app other :: CoreExpr
other args :: [CoreExpr]
args voids :: Arity
voids = CoreExpr -> ExprSize
size_up CoreExpr
other ExprSize -> Arity -> ExprSize
`addSizeN`
Arity -> Arity -> Arity
callSize ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
args) Arity
voids
size_up_call :: Id -> [CoreExpr] -> Int -> ExprSize
size_up_call :: Var -> [CoreExpr] -> Arity -> ExprSize
size_up_call fun :: Var
fun val_args :: [CoreExpr]
val_args voids :: Arity
voids
= case Var -> IdDetails
idDetails Var
fun of
FCallId _ -> Arity -> ExprSize
sizeN (Arity -> Arity -> Arity
callSize ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
val_args) Arity
voids)
DataConWorkId dc :: DataCon
dc -> DataCon -> Arity -> ExprSize
conSize DataCon
dc ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
val_args)
PrimOpId op :: PrimOp
op -> PrimOp -> Arity -> ExprSize
primOpSize PrimOp
op ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
val_args)
ClassOpId _ -> DynFlags -> [Var] -> [CoreExpr] -> ExprSize
classOpSize DynFlags
dflags [Var]
top_args [CoreExpr]
val_args
_ -> DynFlags -> [Var] -> Var -> Arity -> Arity -> ExprSize
funSize DynFlags
dflags [Var]
top_args Var
fun ([CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
val_args) Arity
voids
size_up_alt :: Alt Var -> ExprSize
size_up_alt (_con :: AltCon
_con, _bndrs :: [Var]
_bndrs, rhs :: CoreExpr
rhs) = CoreExpr -> ExprSize
size_up CoreExpr
rhs ExprSize -> Arity -> ExprSize
`addSizeN` 10
size_up_alloc :: Var -> p
size_up_alloc bndr :: Var
bndr
| Var -> Bool
isTyVar Var
bndr
Bool -> Bool -> Bool
|| Var -> Bool
isJoinId Var
bndr
Bool -> Bool -> Bool
|| HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Var -> Type
idType Var
bndr)
= 0
| Bool
otherwise
= 10
addSizeN :: ExprSize -> Arity -> ExprSize
addSizeN TooBig _ = ExprSize
TooBig
addSizeN (SizeIs n :: Arity
n xs :: Bag (Var, Arity)
xs d :: Arity
d) m :: Arity
m = Arity -> Arity -> Bag (Var, Arity) -> Arity -> ExprSize
mkSizeIs Arity
bOMB_OUT_SIZE (Arity
n Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
m) Bag (Var, Arity)
xs Arity
d
addAltSize :: ExprSize -> ExprSize -> ExprSize
addAltSize TooBig _ = ExprSize
TooBig
addAltSize _ TooBig = ExprSize
TooBig
addAltSize (SizeIs n1 :: Arity
n1 xs :: Bag (Var, Arity)
xs d1 :: Arity
d1) (SizeIs n2 :: Arity
n2 ys :: Bag (Var, Arity)
ys d2 :: Arity
d2)
= Arity -> Arity -> Bag (Var, Arity) -> Arity -> ExprSize
mkSizeIs Arity
bOMB_OUT_SIZE (Arity
n1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n2)
(Bag (Var, Arity)
xs Bag (Var, Arity) -> Bag (Var, Arity) -> Bag (Var, Arity)
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag (Var, Arity)
ys)
(Arity
d1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
d2)
addSizeNSD :: ExprSize -> ExprSize -> ExprSize
addSizeNSD TooBig _ = ExprSize
TooBig
addSizeNSD _ TooBig = ExprSize
TooBig
addSizeNSD (SizeIs n1 :: Arity
n1 xs :: Bag (Var, Arity)
xs _) (SizeIs n2 :: Arity
n2 ys :: Bag (Var, Arity)
ys d2 :: Arity
d2)
= Arity -> Arity -> Bag (Var, Arity) -> Arity -> ExprSize
mkSizeIs Arity
bOMB_OUT_SIZE (Arity
n1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n2)
(Bag (Var, Arity)
xs Bag (Var, Arity) -> Bag (Var, Arity) -> Bag (Var, Arity)
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag (Var, Arity)
ys)
Arity
d2
isRealWorldId :: Var -> Bool
isRealWorldId id :: Var
id = Var -> Type
idType Var
id Type -> Type -> Bool
`eqType` Type
realWorldStatePrimTy
isRealWorldExpr :: Expr b -> Bool
isRealWorldExpr (Var id :: Var
id) = Var -> Bool
isRealWorldId Var
id
isRealWorldExpr (Tick _ e :: Expr b
e) = Expr b -> Bool
isRealWorldExpr Expr b
e
isRealWorldExpr _ = Bool
False
litSize :: Literal -> Int
litSize :: Literal -> Arity
litSize (LitNumber LitNumInteger _ _) = 100
litSize (LitNumber LitNumNatural _ _) = 100
litSize (LitString str :: ByteString
str) = 10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* ((ByteString -> Arity
BS.length ByteString
str Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 3) Arity -> Arity -> Arity
forall a. Integral a => a -> a -> a
`div` 4)
litSize _other :: Literal
_other = 0
classOpSize :: DynFlags -> [Id] -> [CoreExpr] -> ExprSize
classOpSize :: DynFlags -> [Var] -> [CoreExpr] -> ExprSize
classOpSize _ _ []
= ExprSize
sizeZero
classOpSize dflags :: DynFlags
dflags top_args :: [Var]
top_args (arg1 :: CoreExpr
arg1 : other_args :: [CoreExpr]
other_args)
= Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
size Bag (Var, Arity)
arg_discount 0
where
size :: Arity
size = 20 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ (10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* [CoreExpr] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [CoreExpr]
other_args)
arg_discount :: Bag (Var, Arity)
arg_discount = case CoreExpr
arg1 of
Var dict :: Var
dict | Var
dict Var -> [Var] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Var]
top_args
-> (Var, Arity) -> Bag (Var, Arity)
forall a. a -> Bag a
unitBag (Var
dict, DynFlags -> Arity
ufDictDiscount DynFlags
dflags)
_other :: CoreExpr
_other -> Bag (Var, Arity)
forall a. Bag a
emptyBag
callSize
:: Int
-> Int
-> Int
callSize :: Arity -> Arity -> Arity
callSize n_val_args :: Arity
n_val_args voids :: Arity
voids = 10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n_val_args Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
voids)
jumpSize
:: Int
-> Int
-> Int
jumpSize :: Arity -> Arity -> Arity
jumpSize n_val_args :: Arity
n_val_args voids :: Arity
voids = 2 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n_val_args Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
voids)
funSize :: DynFlags -> [Id] -> Id -> Int -> Int -> ExprSize
funSize :: DynFlags -> [Var] -> Var -> Arity -> Arity -> ExprSize
funSize dflags :: DynFlags
dflags top_args :: [Var]
top_args fun :: Var
fun n_val_args :: Arity
n_val_args voids :: Arity
voids
| Var
fun Var -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
buildIdKey = ExprSize
buildSize
| Var
fun Var -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
augmentIdKey = ExprSize
augmentSize
| Bool
otherwise = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
size Bag (Var, Arity)
arg_discount Arity
res_discount
where
some_val_args :: Bool
some_val_args = Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0
is_join :: Bool
is_join = Var -> Bool
isJoinId Var
fun
size :: Arity
size | Bool
is_join = Arity -> Arity -> Arity
jumpSize Arity
n_val_args Arity
voids
| Bool -> Bool
not Bool
some_val_args = 0
| Bool
otherwise = Arity -> Arity -> Arity
callSize Arity
n_val_args Arity
voids
arg_discount :: Bag (Var, Arity)
arg_discount | Bool
some_val_args Bool -> Bool -> Bool
&& Var
fun Var -> [Var] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Var]
top_args
= (Var, Arity) -> Bag (Var, Arity)
forall a. a -> Bag a
unitBag (Var
fun, DynFlags -> Arity
ufFunAppDiscount DynFlags
dflags)
| Bool
otherwise = Bag (Var, Arity)
forall a. Bag a
emptyBag
res_discount :: Arity
res_discount | Var -> Arity
idArity Var
fun Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> Arity
n_val_args = DynFlags -> Arity
ufFunAppDiscount DynFlags
dflags
| Bool
otherwise = 0
conSize :: DataCon -> Int -> ExprSize
conSize :: DataCon -> Arity -> ExprSize
conSize dc :: DataCon
dc n_val_args :: Arity
n_val_args
| Arity
n_val_args Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 0 Bag (Var, Arity)
forall a. Bag a
emptyBag 10
| DataCon -> Bool
isUnboxedTupleCon DataCon
dc = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 0 Bag (Var, Arity)
forall a. Bag a
emptyBag (10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n_val_args))
| Bool
otherwise = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 10 Bag (Var, Arity)
forall a. Bag a
emptyBag (10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n_val_args))
primOpSize :: PrimOp -> Int -> ExprSize
primOpSize :: PrimOp -> Arity -> ExprSize
primOpSize op :: PrimOp
op n_val_args :: Arity
n_val_args
= if PrimOp -> Bool
primOpOutOfLine PrimOp
op
then Arity -> ExprSize
sizeN (Arity
op_size Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
n_val_args)
else Arity -> ExprSize
sizeN Arity
op_size
where
op_size :: Arity
op_size = PrimOp -> Arity
primOpCodeSize PrimOp
op
buildSize :: ExprSize
buildSize :: ExprSize
buildSize = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 0 Bag (Var, Arity)
forall a. Bag a
emptyBag 40
augmentSize :: ExprSize
augmentSize :: ExprSize
augmentSize = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 0 Bag (Var, Arity)
forall a. Bag a
emptyBag 40
lamScrutDiscount :: DynFlags -> ExprSize -> ExprSize
lamScrutDiscount :: DynFlags -> ExprSize -> ExprSize
lamScrutDiscount dflags :: DynFlags
dflags (SizeIs n :: Arity
n vs :: Bag (Var, Arity)
vs _) = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
n Bag (Var, Arity)
vs (DynFlags -> Arity
ufFunAppDiscount DynFlags
dflags)
lamScrutDiscount _ TooBig = ExprSize
TooBig
data ExprSize
= TooBig
| SizeIs { ExprSize -> Arity
_es_size_is :: {-# UNPACK #-} !Int
, ExprSize -> Bag (Var, Arity)
_es_args :: !(Bag (Id,Int))
, ExprSize -> Arity
_es_discount :: {-# UNPACK #-} !Int
}
instance Outputable ExprSize where
ppr :: ExprSize -> SDoc
ppr TooBig = String -> SDoc
text "TooBig"
ppr (SizeIs a :: Arity
a _ c :: Arity
c) = SDoc -> SDoc
brackets (Arity -> SDoc
int Arity
a SDoc -> SDoc -> SDoc
<+> Arity -> SDoc
int Arity
c)
mkSizeIs :: Int -> Int -> Bag (Id, Int) -> Int -> ExprSize
mkSizeIs :: Arity -> Arity -> Bag (Var, Arity) -> Arity -> ExprSize
mkSizeIs max :: Arity
max n :: Arity
n xs :: Bag (Var, Arity)
xs d :: Arity
d | (Arity
n Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
d) Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> Arity
max = ExprSize
TooBig
| Bool
otherwise = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
n Bag (Var, Arity)
xs Arity
d
maxSize :: ExprSize -> ExprSize -> ExprSize
maxSize :: ExprSize -> ExprSize -> ExprSize
maxSize TooBig _ = ExprSize
TooBig
maxSize _ TooBig = ExprSize
TooBig
maxSize s1 :: ExprSize
s1@(SizeIs n1 :: Arity
n1 _ _) s2 :: ExprSize
s2@(SizeIs n2 :: Arity
n2 _ _) | Arity
n1 Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> Arity
n2 = ExprSize
s1
| Bool
otherwise = ExprSize
s2
sizeZero :: ExprSize
sizeN :: Int -> ExprSize
sizeZero :: ExprSize
sizeZero = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs 0 Bag (Var, Arity)
forall a. Bag a
emptyBag 0
sizeN :: Arity -> ExprSize
sizeN n :: Arity
n = Arity -> Bag (Var, Arity) -> Arity -> ExprSize
SizeIs Arity
n Bag (Var, Arity)
forall a. Bag a
emptyBag 0
couldBeSmallEnoughToInline :: DynFlags -> Int -> CoreExpr -> Bool
couldBeSmallEnoughToInline :: DynFlags -> Arity -> CoreExpr -> Bool
couldBeSmallEnoughToInline dflags :: DynFlags
dflags threshold :: Arity
threshold rhs :: CoreExpr
rhs
= case DynFlags -> Arity -> [Var] -> CoreExpr -> ExprSize
sizeExpr DynFlags
dflags Arity
threshold [] CoreExpr
body of
TooBig -> Bool
False
_ -> Bool
True
where
(_, body :: CoreExpr
body) = CoreExpr -> ([Var], CoreExpr)
forall b. Expr b -> ([b], Expr b)
collectBinders CoreExpr
rhs
smallEnoughToInline :: DynFlags -> Unfolding -> Bool
smallEnoughToInline :: DynFlags -> Unfolding -> Bool
smallEnoughToInline dflags :: DynFlags
dflags (CoreUnfolding {uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfIfGoodArgs {ug_size :: UnfoldingGuidance -> Arity
ug_size = Arity
size}})
= Arity
size Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
<= DynFlags -> Arity
ufUseThreshold DynFlags
dflags
smallEnoughToInline _ _
= Bool
False
certainlyWillInline :: DynFlags -> IdInfo -> Maybe Unfolding
certainlyWillInline :: DynFlags -> IdInfo -> Maybe Unfolding
certainlyWillInline dflags :: DynFlags
dflags fn_info :: IdInfo
fn_info
= case IdInfo -> Unfolding
unfoldingInfo IdInfo
fn_info of
CoreUnfolding { uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
e, uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfoldingGuidance
g }
| Bool
loop_breaker -> Maybe Unfolding
forall a. Maybe a
Nothing
| Bool
otherwise -> CoreExpr -> UnfoldingGuidance -> Maybe Unfolding
do_cunf CoreExpr
e UnfoldingGuidance
g
DFunUnfolding {} -> Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just Unfolding
fn_unf
_other_unf :: Unfolding
_other_unf -> Maybe Unfolding
forall a. Maybe a
Nothing
where
loop_breaker :: Bool
loop_breaker = OccInfo -> Bool
isStrongLoopBreaker (IdInfo -> OccInfo
occInfo IdInfo
fn_info)
fn_unf :: Unfolding
fn_unf = IdInfo -> Unfolding
unfoldingInfo IdInfo
fn_info
do_cunf :: CoreExpr -> UnfoldingGuidance -> Maybe Unfolding
do_cunf :: CoreExpr -> UnfoldingGuidance -> Maybe Unfolding
do_cunf _ UnfNever = Maybe Unfolding
forall a. Maybe a
Nothing
do_cunf _ (UnfWhen {}) = Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just (Unfolding
fn_unf { uf_src :: UnfoldingSource
uf_src = UnfoldingSource
InlineStable })
do_cunf expr :: CoreExpr
expr (UnfIfGoodArgs { ug_size :: UnfoldingGuidance -> Arity
ug_size = Arity
size, ug_args :: UnfoldingGuidance -> [Arity]
ug_args = [Arity]
args })
| Bool -> Bool
not ([Arity] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Arity]
args)
, case InlinePragma -> InlineSpec
inlinePragmaSpec (IdInfo -> InlinePragma
inlinePragInfo IdInfo
fn_info) of
NoInline -> Bool
False
_ -> Bool
True
, Bool -> Bool
not (StrictSig -> Bool
isBottomingSig (IdInfo -> StrictSig
strictnessInfo IdInfo
fn_info))
, let arity :: Arity
arity = [Arity] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Arity]
args
, Arity
size Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- (10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* (Arity
arity Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 1)) Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
<= DynFlags -> Arity
ufUseThreshold DynFlags
dflags
= Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just (Unfolding
fn_unf { uf_src :: UnfoldingSource
uf_src = UnfoldingSource
InlineStable
, uf_guidance :: UnfoldingGuidance
uf_guidance = UnfWhen :: Arity -> Bool -> Bool -> UnfoldingGuidance
UnfWhen { ug_arity :: Arity
ug_arity = Arity
arity
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = CoreExpr -> Bool
inlineBoringOk CoreExpr
expr } })
| Bool
otherwise
= Maybe Unfolding
forall a. Maybe a
Nothing
callSiteInline :: DynFlags
-> Id
-> Bool
-> Bool
-> [ArgSummary]
-> CallCtxt
-> Maybe CoreExpr
data ArgSummary = TrivArg
| NonTrivArg
| ValueArg
instance Outputable ArgSummary where
ppr :: ArgSummary -> SDoc
ppr TrivArg = String -> SDoc
text "TrivArg"
ppr NonTrivArg = String -> SDoc
text "NonTrivArg"
ppr ValueArg = String -> SDoc
text "ValueArg"
nonTriv :: ArgSummary -> Bool
nonTriv :: ArgSummary -> Bool
nonTriv TrivArg = Bool
False
nonTriv _ = Bool
True
data CallCtxt
= BoringCtxt
| RhsCtxt
| DiscArgCtxt
| RuleArgCtxt
| ValAppCtxt
| CaseCtxt
instance Outputable CallCtxt where
ppr :: CallCtxt -> SDoc
ppr CaseCtxt = String -> SDoc
text "CaseCtxt"
ppr ValAppCtxt = String -> SDoc
text "ValAppCtxt"
ppr BoringCtxt = String -> SDoc
text "BoringCtxt"
ppr RhsCtxt = String -> SDoc
text "RhsCtxt"
ppr DiscArgCtxt = String -> SDoc
text "DiscArgCtxt"
ppr RuleArgCtxt = String -> SDoc
text "RuleArgCtxt"
callSiteInline :: DynFlags
-> Var
-> Bool
-> Bool
-> [ArgSummary]
-> CallCtxt
-> Maybe CoreExpr
callSiteInline dflags :: DynFlags
dflags id :: Var
id active_unfolding :: Bool
active_unfolding lone_variable :: Bool
lone_variable arg_infos :: [ArgSummary]
arg_infos cont_info :: CallCtxt
cont_info
= case Var -> Unfolding
idUnfolding Var
id of
CoreUnfolding { uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
unf_template
, uf_is_work_free :: Unfolding -> Bool
uf_is_work_free = Bool
is_wf
, uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfoldingGuidance
guidance, uf_expandable :: Unfolding -> Bool
uf_expandable = Bool
is_exp }
| Bool
active_unfolding -> DynFlags
-> Var
-> Bool
-> [ArgSummary]
-> CallCtxt
-> CoreExpr
-> Bool
-> Bool
-> UnfoldingGuidance
-> Maybe CoreExpr
tryUnfolding DynFlags
dflags Var
id Bool
lone_variable
[ArgSummary]
arg_infos CallCtxt
cont_info CoreExpr
unf_template
Bool
is_wf Bool
is_exp UnfoldingGuidance
guidance
| Bool
otherwise -> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id "Inactive unfolding:" (Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
id) Maybe CoreExpr
forall a. Maybe a
Nothing
NoUnfolding -> Maybe CoreExpr
forall a. Maybe a
Nothing
BootUnfolding -> Maybe CoreExpr
forall a. Maybe a
Nothing
OtherCon {} -> Maybe CoreExpr
forall a. Maybe a
Nothing
DFunUnfolding {} -> Maybe CoreExpr
forall a. Maybe a
Nothing
traceInline :: DynFlags -> Id -> String -> SDoc -> a -> a
traceInline :: DynFlags -> Var -> String -> SDoc -> a -> a
traceInline dflags :: DynFlags
dflags inline_id :: Var
inline_id str :: String
str doc :: SDoc
doc result :: a
result
| Just prefix :: String
prefix <- DynFlags -> Maybe String
inlineCheck DynFlags
dflags
= if String
prefix String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` OccName -> String
occNameString (Var -> OccName
forall a. NamedThing a => a -> OccName
getOccName Var
inline_id)
then String -> SDoc -> a -> a
forall a. String -> SDoc -> a -> a
pprTrace String
str SDoc
doc a
result
else a
result
| DumpFlag -> DynFlags -> Bool
dopt DumpFlag
Opt_D_dump_inlinings DynFlags
dflags Bool -> Bool -> Bool
&& DumpFlag -> DynFlags -> Bool
dopt DumpFlag
Opt_D_verbose_core2core DynFlags
dflags
= String -> SDoc -> a -> a
forall a. String -> SDoc -> a -> a
pprTrace String
str SDoc
doc a
result
| Bool
otherwise
= a
result
tryUnfolding :: DynFlags -> Id -> Bool -> [ArgSummary] -> CallCtxt
-> CoreExpr -> Bool -> Bool -> UnfoldingGuidance
-> Maybe CoreExpr
tryUnfolding :: DynFlags
-> Var
-> Bool
-> [ArgSummary]
-> CallCtxt
-> CoreExpr
-> Bool
-> Bool
-> UnfoldingGuidance
-> Maybe CoreExpr
tryUnfolding dflags :: DynFlags
dflags id :: Var
id lone_variable :: Bool
lone_variable
arg_infos :: [ArgSummary]
arg_infos cont_info :: CallCtxt
cont_info unf_template :: CoreExpr
unf_template
is_wf :: Bool
is_wf is_exp :: Bool
is_exp guidance :: UnfoldingGuidance
guidance
= case UnfoldingGuidance
guidance of
UnfNever -> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (String -> SDoc
text "UnfNever") Maybe CoreExpr
forall a. Maybe a
Nothing
UnfWhen { ug_arity :: UnfoldingGuidance -> Arity
ug_arity = Arity
uf_arity, ug_unsat_ok :: UnfoldingGuidance -> Bool
ug_unsat_ok = Bool
unsat_ok, ug_boring_ok :: UnfoldingGuidance -> Bool
ug_boring_ok = Bool
boring_ok }
| Bool
enough_args Bool -> Bool -> Bool
&& (Bool
boring_ok Bool -> Bool -> Bool
|| Bool
some_benefit Bool -> Bool -> Bool
|| DynFlags -> Bool
ufVeryAggressive DynFlags
dflags)
-> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (Bool -> SDoc -> Bool -> SDoc
forall a. Outputable a => a -> SDoc -> Bool -> SDoc
mk_doc Bool
some_benefit SDoc
empty Bool
True) (CoreExpr -> Maybe CoreExpr
forall a. a -> Maybe a
Just CoreExpr
unf_template)
| Bool
otherwise
-> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (Bool -> SDoc -> Bool -> SDoc
forall a. Outputable a => a -> SDoc -> Bool -> SDoc
mk_doc Bool
some_benefit SDoc
empty Bool
False) Maybe CoreExpr
forall a. Maybe a
Nothing
where
some_benefit :: Bool
some_benefit = Arity -> Bool
calc_some_benefit Arity
uf_arity
enough_args :: Bool
enough_args = (Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
>= Arity
uf_arity) Bool -> Bool -> Bool
|| (Bool
unsat_ok Bool -> Bool -> Bool
&& Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0)
UnfIfGoodArgs { ug_args :: UnfoldingGuidance -> [Arity]
ug_args = [Arity]
arg_discounts, ug_res :: UnfoldingGuidance -> Arity
ug_res = Arity
res_discount, ug_size :: UnfoldingGuidance -> Arity
ug_size = Arity
size }
| DynFlags -> Bool
ufVeryAggressive DynFlags
dflags
-> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (Bool -> SDoc -> Bool -> SDoc
forall a. Outputable a => a -> SDoc -> Bool -> SDoc
mk_doc Bool
some_benefit SDoc
extra_doc Bool
True) (CoreExpr -> Maybe CoreExpr
forall a. a -> Maybe a
Just CoreExpr
unf_template)
| Bool
is_wf Bool -> Bool -> Bool
&& Bool
some_benefit Bool -> Bool -> Bool
&& Bool
small_enough
-> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (Bool -> SDoc -> Bool -> SDoc
forall a. Outputable a => a -> SDoc -> Bool -> SDoc
mk_doc Bool
some_benefit SDoc
extra_doc Bool
True) (CoreExpr -> Maybe CoreExpr
forall a. a -> Maybe a
Just CoreExpr
unf_template)
| Bool
otherwise
-> DynFlags
-> Var -> String -> SDoc -> Maybe CoreExpr -> Maybe CoreExpr
forall a. DynFlags -> Var -> String -> SDoc -> a -> a
traceInline DynFlags
dflags Var
id String
str (Bool -> SDoc -> Bool -> SDoc
forall a. Outputable a => a -> SDoc -> Bool -> SDoc
mk_doc Bool
some_benefit SDoc
extra_doc Bool
False) Maybe CoreExpr
forall a. Maybe a
Nothing
where
some_benefit :: Bool
some_benefit = Arity -> Bool
calc_some_benefit ([Arity] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Arity]
arg_discounts)
extra_doc :: SDoc
extra_doc = String -> SDoc
text "discounted size =" SDoc -> SDoc -> SDoc
<+> Arity -> SDoc
int Arity
discounted_size
discounted_size :: Arity
discounted_size = Arity
size Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
discount
small_enough :: Bool
small_enough = Arity
discounted_size Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
<= DynFlags -> Arity
ufUseThreshold DynFlags
dflags
discount :: Arity
discount = DynFlags -> [Arity] -> Arity -> [ArgSummary] -> CallCtxt -> Arity
computeDiscount DynFlags
dflags [Arity]
arg_discounts
Arity
res_discount [ArgSummary]
arg_infos CallCtxt
cont_info
where
mk_doc :: a -> SDoc -> Bool -> SDoc
mk_doc some_benefit :: a
some_benefit extra_doc :: SDoc
extra_doc yes_or_no :: Bool
yes_or_no
= [SDoc] -> SDoc
vcat [ String -> SDoc
text "arg infos" SDoc -> SDoc -> SDoc
<+> [ArgSummary] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [ArgSummary]
arg_infos
, String -> SDoc
text "interesting continuation" SDoc -> SDoc -> SDoc
<+> CallCtxt -> SDoc
forall a. Outputable a => a -> SDoc
ppr CallCtxt
cont_info
, String -> SDoc
text "some_benefit" SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
some_benefit
, String -> SDoc
text "is exp:" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
is_exp
, String -> SDoc
text "is work-free:" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
is_wf
, String -> SDoc
text "guidance" SDoc -> SDoc -> SDoc
<+> UnfoldingGuidance -> SDoc
forall a. Outputable a => a -> SDoc
ppr UnfoldingGuidance
guidance
, SDoc
extra_doc
, String -> SDoc
text "ANSWER =" SDoc -> SDoc -> SDoc
<+> if Bool
yes_or_no then String -> SDoc
text "YES" else String -> SDoc
text "NO"]
str :: String
str = "Considering inlining: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ DynFlags -> SDoc -> String
showSDocDump DynFlags
dflags (Var -> SDoc
forall a. Outputable a => a -> SDoc
ppr Var
id)
n_val_args :: Arity
n_val_args = [ArgSummary] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [ArgSummary]
arg_infos
calc_some_benefit :: Arity -> Bool
calc_some_benefit :: Arity -> Bool
calc_some_benefit uf_arity :: Arity
uf_arity
| Bool -> Bool
not Bool
saturated = Bool
interesting_args
| Bool
otherwise = Bool
interesting_args
Bool -> Bool -> Bool
|| Bool
interesting_call
where
saturated :: Bool
saturated = Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
>= Arity
uf_arity
over_saturated :: Bool
over_saturated = Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> Arity
uf_arity
interesting_args :: Bool
interesting_args = (ArgSummary -> Bool) -> [ArgSummary] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ArgSummary -> Bool
nonTriv [ArgSummary]
arg_infos
interesting_call :: Bool
interesting_call
| Bool
over_saturated
= Bool
True
| Bool
otherwise
= case CallCtxt
cont_info of
CaseCtxt -> Bool -> Bool
not (Bool
lone_variable Bool -> Bool -> Bool
&& Bool
is_exp)
ValAppCtxt -> Bool
True
RuleArgCtxt -> Arity
uf_arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0
DiscArgCtxt -> Arity
uf_arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0
RhsCtxt -> Arity
uf_arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
> 0
_other :: CallCtxt
_other -> Bool
False
computeDiscount :: DynFlags -> [Int] -> Int -> [ArgSummary] -> CallCtxt
-> Int
computeDiscount :: DynFlags -> [Arity] -> Arity -> [ArgSummary] -> CallCtxt -> Arity
computeDiscount dflags :: DynFlags
dflags arg_discounts :: [Arity]
arg_discounts res_discount :: Arity
res_discount arg_infos :: [ArgSummary]
arg_infos cont_info :: CallCtxt
cont_info
= 10
Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ 10 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
* [Arity] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [Arity]
actual_arg_discounts
Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Float -> Arity
forall a b. (RealFrac a, Integral b) => a -> b
round (DynFlags -> Float
ufKeenessFactor DynFlags
dflags Float -> Float -> Float
forall a. Num a => a -> a -> a
*
Arity -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Arity
total_arg_discount Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
+ Arity
res_discount'))
where
actual_arg_discounts :: [Arity]
actual_arg_discounts = (Arity -> ArgSummary -> Arity)
-> [Arity] -> [ArgSummary] -> [Arity]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Arity -> ArgSummary -> Arity
forall p. Num p => p -> ArgSummary -> p
mk_arg_discount [Arity]
arg_discounts [ArgSummary]
arg_infos
total_arg_discount :: Arity
total_arg_discount = [Arity] -> Arity
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Arity]
actual_arg_discounts
mk_arg_discount :: p -> ArgSummary -> p
mk_arg_discount _ TrivArg = 0
mk_arg_discount _ NonTrivArg = 10
mk_arg_discount discount :: p
discount ValueArg = p
discount
res_discount' :: Arity
res_discount'
| Ordering
LT <- [Arity]
arg_discounts [Arity] -> [ArgSummary] -> Ordering
forall a b. [a] -> [b] -> Ordering
`compareLength` [ArgSummary]
arg_infos
= Arity
res_discount
| Bool
otherwise
= case CallCtxt
cont_info of
BoringCtxt -> 0
CaseCtxt -> Arity
res_discount
ValAppCtxt -> Arity
res_discount
_ -> 40 Arity -> Arity -> Arity
forall a. Ord a => a -> a -> a
`min` Arity
res_discount