{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
module GHC.Core.Unfold.Make
( noUnfolding
, mkUnfolding
, mkCoreUnfolding
, mkFinalUnfolding
, mkSimpleUnfolding
, mkWorkerUnfolding
, mkInlineUnfolding
, mkInlineUnfoldingWithArity
, mkInlinableUnfolding
, mkWrapperUnfolding
, mkCompulsoryUnfolding
, mkCompulsoryUnfolding'
, mkDFunUnfolding
, specUnfolding
, certainlyWillInline
)
where
import GHC.Prelude
import GHC.Core
import GHC.Core.Unfold
import GHC.Core.Opt.OccurAnal ( occurAnalyseExpr )
import GHC.Core.Opt.Arity ( manifestArity )
import GHC.Core.DataCon
import GHC.Core.Utils
import GHC.Types.Basic
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Demand ( DmdSig, isDeadEndSig )
import GHC.Utils.Outputable
import GHC.Utils.Misc
import GHC.Utils.Panic
import {-# SOURCE #-} GHC.Core.SimpleOpt
mkFinalUnfolding :: UnfoldingOpts -> UnfoldingSource -> DmdSig -> CoreExpr -> Unfolding
mkFinalUnfolding :: UnfoldingOpts -> UnfoldingSource -> DmdSig -> CoreArg -> Unfolding
mkFinalUnfolding UnfoldingOpts
opts UnfoldingSource
src DmdSig
strict_sig CoreArg
expr
= UnfoldingOpts
-> UnfoldingSource -> Bool -> Bool -> CoreArg -> Unfolding
mkUnfolding UnfoldingOpts
opts UnfoldingSource
src
Bool
True
(DmdSig -> Bool
isDeadEndSig DmdSig
strict_sig)
CoreArg
expr
mkCompulsoryUnfolding :: SimpleOpts -> CoreExpr -> Unfolding
mkCompulsoryUnfolding :: SimpleOpts -> CoreArg -> Unfolding
mkCompulsoryUnfolding SimpleOpts
opts CoreArg
expr = CoreArg -> Unfolding
mkCompulsoryUnfolding' ((() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts CoreArg
expr)
mkCompulsoryUnfolding' :: CoreExpr -> Unfolding
mkCompulsoryUnfolding' :: CoreArg -> Unfolding
mkCompulsoryUnfolding' CoreArg
expr
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineCompulsory Bool
True
CoreArg
expr
(UnfWhen { ug_arity :: ArityInfo
ug_arity = ArityInfo
0
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk, ug_boring_ok :: Bool
ug_boring_ok = Bool
boringCxtOk })
mkSimpleUnfolding :: UnfoldingOpts -> CoreExpr -> Unfolding
mkSimpleUnfolding :: UnfoldingOpts -> CoreArg -> Unfolding
mkSimpleUnfolding !UnfoldingOpts
opts CoreArg
rhs
= UnfoldingOpts
-> UnfoldingSource -> Bool -> Bool -> CoreArg -> Unfolding
mkUnfolding UnfoldingOpts
opts UnfoldingSource
InlineRhs Bool
False Bool
False CoreArg
rhs
mkDFunUnfolding :: [Var] -> DataCon -> [CoreExpr] -> Unfolding
mkDFunUnfolding :: [Var] -> DataCon -> [CoreArg] -> Unfolding
mkDFunUnfolding [Var]
bndrs DataCon
con [CoreArg]
ops
= DFunUnfolding { df_bndrs :: [Var]
df_bndrs = [Var]
bndrs
, df_con :: DataCon
df_con = DataCon
con
, df_args :: [CoreArg]
df_args = (CoreArg -> CoreArg) -> [CoreArg] -> [CoreArg]
forall a b. (a -> b) -> [a] -> [b]
map CoreArg -> CoreArg
occurAnalyseExpr [CoreArg]
ops }
mkWrapperUnfolding :: SimpleOpts -> CoreExpr -> Arity -> Unfolding
mkWrapperUnfolding :: SimpleOpts -> CoreArg -> ArityInfo -> Unfolding
mkWrapperUnfolding SimpleOpts
opts CoreArg
expr ArityInfo
arity
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable Bool
True
((() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts CoreArg
expr)
(UnfWhen { ug_arity :: ArityInfo
ug_arity = ArityInfo
arity
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = Bool
boringCxtNotOk })
mkWorkerUnfolding :: SimpleOpts -> (CoreExpr -> CoreExpr) -> Unfolding -> Unfolding
mkWorkerUnfolding :: SimpleOpts -> (CoreArg -> CoreArg) -> Unfolding -> Unfolding
mkWorkerUnfolding SimpleOpts
opts CoreArg -> CoreArg
work_fn
(CoreUnfolding { uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src, uf_tmpl :: Unfolding -> CoreArg
uf_tmpl = CoreArg
tmpl
, uf_is_top :: Unfolding -> Bool
uf_is_top = Bool
top_lvl })
| UnfoldingSource -> Bool
isStableSource UnfoldingSource
src
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreArg
new_tmpl UnfoldingGuidance
guidance
where
new_tmpl :: CoreArg
new_tmpl = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts (CoreArg -> CoreArg
work_fn CoreArg
tmpl)
guidance :: UnfoldingGuidance
guidance = UnfoldingOpts -> Bool -> CoreArg -> UnfoldingGuidance
calcUnfoldingGuidance (SimpleOpts -> UnfoldingOpts
so_uf_opts SimpleOpts
opts) Bool
False CoreArg
new_tmpl
mkWorkerUnfolding SimpleOpts
_ CoreArg -> CoreArg
_ Unfolding
_ = Unfolding
noUnfolding
mkInlineUnfolding :: SimpleOpts -> CoreExpr -> Unfolding
mkInlineUnfolding :: SimpleOpts -> CoreArg -> Unfolding
mkInlineUnfolding SimpleOpts
opts CoreArg
expr
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable
Bool
True
CoreArg
expr' UnfoldingGuidance
guide
where
expr' :: CoreArg
expr' = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts CoreArg
expr
guide :: UnfoldingGuidance
guide = UnfWhen { ug_arity :: ArityInfo
ug_arity = CoreArg -> ArityInfo
manifestArity CoreArg
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 = CoreArg -> Bool
inlineBoringOk CoreArg
expr'
mkInlineUnfoldingWithArity :: Arity -> SimpleOpts -> CoreExpr -> Unfolding
mkInlineUnfoldingWithArity :: ArityInfo -> SimpleOpts -> CoreArg -> Unfolding
mkInlineUnfoldingWithArity ArityInfo
arity SimpleOpts
opts CoreArg
expr
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
InlineStable
Bool
True
CoreArg
expr' UnfoldingGuidance
guide
where
expr' :: CoreArg
expr' = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts CoreArg
expr
guide :: UnfoldingGuidance
guide = UnfWhen { ug_arity :: ArityInfo
ug_arity = ArityInfo
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 | ArityInfo
arity ArityInfo -> ArityInfo -> Bool
forall a. Eq a => a -> a -> Bool
== ArityInfo
0 = Bool
True
| Bool
otherwise = CoreArg -> Bool
inlineBoringOk CoreArg
expr'
mkInlinableUnfolding :: SimpleOpts -> CoreExpr -> Unfolding
mkInlinableUnfolding :: SimpleOpts -> CoreArg -> Unfolding
mkInlinableUnfolding SimpleOpts
opts CoreArg
expr
= UnfoldingOpts
-> UnfoldingSource -> Bool -> Bool -> CoreArg -> Unfolding
mkUnfolding (SimpleOpts -> UnfoldingOpts
so_uf_opts SimpleOpts
opts) UnfoldingSource
InlineStable Bool
False Bool
False CoreArg
expr'
where
expr' :: CoreArg
expr' = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts CoreArg
expr
specUnfolding :: SimpleOpts
-> [Var] -> (CoreExpr -> CoreExpr)
-> [CoreArg]
-> Unfolding -> Unfolding
specUnfolding :: SimpleOpts
-> [Var]
-> (CoreArg -> CoreArg)
-> [CoreArg]
-> Unfolding
-> Unfolding
specUnfolding SimpleOpts
opts [Var]
spec_bndrs CoreArg -> CoreArg
spec_app [CoreArg]
rule_lhs_args
df :: Unfolding
df@(DFunUnfolding { df_bndrs :: Unfolding -> [Var]
df_bndrs = [Var]
old_bndrs, df_con :: Unfolding -> DataCon
df_con = DataCon
con, df_args :: Unfolding -> [CoreArg]
df_args = [CoreArg]
args })
= Bool -> SDoc -> Unfolding -> Unfolding
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr ([CoreArg]
rule_lhs_args [CoreArg] -> [Var] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [Var]
old_bndrs)
(Unfolding -> SDoc
forall a. Outputable a => a -> SDoc
ppr Unfolding
df SDoc -> SDoc -> SDoc
$$ [CoreArg] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [CoreArg]
rule_lhs_args) (Unfolding -> Unfolding) -> Unfolding -> Unfolding
forall a b. (a -> b) -> a -> b
$
[Var] -> DataCon -> [CoreArg] -> Unfolding
mkDFunUnfolding [Var]
spec_bndrs DataCon
con ((CoreArg -> CoreArg) -> [CoreArg] -> [CoreArg]
forall a b. (a -> b) -> [a] -> [b]
map CoreArg -> CoreArg
spec_arg [CoreArg]
args)
where
spec_arg :: CoreArg -> CoreArg
spec_arg CoreArg
arg = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts (CoreArg -> CoreArg) -> CoreArg -> CoreArg
forall a b. (a -> b) -> a -> b
$
CoreArg -> CoreArg
spec_app ([Var] -> CoreArg -> CoreArg
forall b. [b] -> Expr b -> Expr b
mkLams [Var]
old_bndrs CoreArg
arg)
specUnfolding SimpleOpts
opts [Var]
spec_bndrs CoreArg -> CoreArg
spec_app [CoreArg]
rule_lhs_args
(CoreUnfolding { uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src, uf_tmpl :: Unfolding -> CoreArg
uf_tmpl = CoreArg
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 -> ArityInfo
ug_arity = ArityInfo
old_arity } <- UnfoldingGuidance
old_guidance
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreArg
new_tmpl
(UnfoldingGuidance
old_guidance { ug_arity :: ArityInfo
ug_arity = ArityInfo
old_arity ArityInfo -> ArityInfo -> ArityInfo
forall a. Num a => a -> a -> a
- ArityInfo
arity_decrease })
where
new_tmpl :: CoreArg
new_tmpl = (() :: Constraint) => SimpleOpts -> CoreArg -> CoreArg
SimpleOpts -> CoreArg -> CoreArg
simpleOptExpr SimpleOpts
opts (CoreArg -> CoreArg) -> CoreArg -> CoreArg
forall a b. (a -> b) -> a -> b
$
[Var] -> CoreArg -> CoreArg
forall b. [b] -> Expr b -> Expr b
mkLams [Var]
spec_bndrs (CoreArg -> CoreArg) -> CoreArg -> CoreArg
forall a b. (a -> b) -> a -> b
$
CoreArg -> CoreArg
spec_app CoreArg
tmpl
arity_decrease :: ArityInfo
arity_decrease = (CoreArg -> Bool) -> [CoreArg] -> ArityInfo
forall a. (a -> Bool) -> [a] -> ArityInfo
count CoreArg -> Bool
forall b. Expr b -> Bool
isValArg [CoreArg]
rule_lhs_args ArityInfo -> ArityInfo -> ArityInfo
forall a. Num a => a -> a -> a
- (Var -> Bool) -> [Var] -> ArityInfo
forall a. (a -> Bool) -> [a] -> ArityInfo
count Var -> Bool
isId [Var]
spec_bndrs
specUnfolding SimpleOpts
_ [Var]
_ CoreArg -> CoreArg
_ [CoreArg]
_ Unfolding
_ = Unfolding
noUnfolding
mkUnfolding :: UnfoldingOpts
-> UnfoldingSource
-> Bool
-> Bool
-> CoreExpr
-> Unfolding
mkUnfolding :: UnfoldingOpts
-> UnfoldingSource -> Bool -> Bool -> CoreArg -> Unfolding
mkUnfolding UnfoldingOpts
opts UnfoldingSource
src Bool
top_lvl Bool
is_bottoming CoreArg
expr
= UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreArg
expr UnfoldingGuidance
guidance
where
is_top_bottoming :: Bool
is_top_bottoming = Bool
top_lvl Bool -> Bool -> Bool
&& Bool
is_bottoming
guidance :: UnfoldingGuidance
guidance = UnfoldingOpts -> Bool -> CoreArg -> UnfoldingGuidance
calcUnfoldingGuidance UnfoldingOpts
opts Bool
is_top_bottoming CoreArg
expr
mkCoreUnfolding :: UnfoldingSource -> Bool -> CoreExpr
-> UnfoldingGuidance -> Unfolding
mkCoreUnfolding :: UnfoldingSource
-> Bool -> CoreArg -> UnfoldingGuidance -> Unfolding
mkCoreUnfolding UnfoldingSource
src Bool
top_lvl CoreArg
expr UnfoldingGuidance
guidance
=
let is_value :: Bool
is_value = CoreArg -> Bool
exprIsHNF CoreArg
expr
is_conlike :: Bool
is_conlike = CoreArg -> Bool
exprIsConLike CoreArg
expr
is_work_free :: Bool
is_work_free = CoreArg -> Bool
exprIsWorkFree CoreArg
expr
is_expandable :: Bool
is_expandable = CoreArg -> Bool
exprIsExpandable CoreArg
expr
in
CoreUnfolding { uf_tmpl :: CoreArg
uf_tmpl = Bool
is_value Bool -> CoreArg -> CoreArg
forall a b. a -> b -> b
`seq`
Bool
is_conlike Bool -> CoreArg -> CoreArg
forall a b. a -> b -> b
`seq`
Bool
is_work_free Bool -> CoreArg -> CoreArg
forall a b. a -> b -> b
`seq`
Bool
is_expandable Bool -> CoreArg -> CoreArg
forall a b. a -> b -> b
`seq`
CoreArg -> CoreArg
occurAnalyseExpr CoreArg
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 = Bool
is_value,
uf_is_conlike :: Bool
uf_is_conlike = Bool
is_conlike,
uf_is_work_free :: Bool
uf_is_work_free = Bool
is_work_free,
uf_expandable :: Bool
uf_expandable = Bool
is_expandable,
uf_guidance :: UnfoldingGuidance
uf_guidance = UnfoldingGuidance
guidance }
certainlyWillInline :: UnfoldingOpts -> IdInfo -> CoreExpr -> Maybe Unfolding
certainlyWillInline :: UnfoldingOpts -> IdInfo -> CoreArg -> Maybe Unfolding
certainlyWillInline UnfoldingOpts
opts IdInfo
fn_info CoreArg
rhs'
= case Unfolding
fn_unf of
CoreUnfolding { uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfoldingGuidance
guidance, uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src }
| Bool
noinline -> Maybe Unfolding
forall a. Maybe a
Nothing
| Bool
otherwise
-> case UnfoldingGuidance
guidance of
UnfoldingGuidance
UnfNever -> Maybe Unfolding
forall a. Maybe a
Nothing
UnfWhen {} -> Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just (Unfolding
fn_unf { uf_src :: UnfoldingSource
uf_src = UnfoldingSource
src', uf_tmpl :: CoreArg
uf_tmpl = CoreArg
tmpl' })
UnfIfGoodArgs { ug_size :: UnfoldingGuidance -> ArityInfo
ug_size = ArityInfo
size, ug_args :: UnfoldingGuidance -> [ArityInfo]
ug_args = [ArityInfo]
args }
-> ArityInfo
-> [ArityInfo] -> UnfoldingSource -> CoreArg -> Maybe Unfolding
do_cunf ArityInfo
size [ArityInfo]
args UnfoldingSource
src' CoreArg
tmpl'
where
src' :: UnfoldingSource
src' =
case UnfoldingSource
src of
UnfoldingSource
InlineCompulsory -> UnfoldingSource
InlineCompulsory
UnfoldingSource
_ -> UnfoldingSource
InlineStable
tmpl' :: CoreArg
tmpl' =
case UnfoldingSource
src of
UnfoldingSource
InlineRhs -> CoreArg -> CoreArg
occurAnalyseExpr CoreArg
rhs'
UnfoldingSource
_ -> Unfolding -> CoreArg
uf_tmpl Unfolding
fn_unf
DFunUnfolding {} -> Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just Unfolding
fn_unf
Unfolding
_other_unf -> Maybe Unfolding
forall a. Maybe a
Nothing
where
noinline :: Bool
noinline = InlinePragma -> Bool
isNoInlinePragma (IdInfo -> InlinePragma
inlinePragInfo IdInfo
fn_info)
fn_unf :: Unfolding
fn_unf = IdInfo -> Unfolding
unfoldingInfo IdInfo
fn_info
do_cunf :: ArityInfo
-> [ArityInfo] -> UnfoldingSource -> CoreArg -> Maybe Unfolding
do_cunf ArityInfo
size [ArityInfo]
args UnfoldingSource
src' CoreArg
tmpl'
| IdInfo -> ArityInfo
arityInfo IdInfo
fn_info ArityInfo -> ArityInfo -> Bool
forall a. Ord a => a -> a -> Bool
> ArityInfo
0
, Bool -> Bool
not (DmdSig -> Bool
isDeadEndSig (IdInfo -> DmdSig
dmdSigInfo IdInfo
fn_info))
, let unf_arity :: ArityInfo
unf_arity = [ArityInfo] -> ArityInfo
forall a. [a] -> ArityInfo
forall (t :: * -> *) a. Foldable t => t a -> ArityInfo
length [ArityInfo]
args
, ArityInfo
size ArityInfo -> ArityInfo -> ArityInfo
forall a. Num a => a -> a -> a
- (ArityInfo
10 ArityInfo -> ArityInfo -> ArityInfo
forall a. Num a => a -> a -> a
* (ArityInfo
unf_arity ArityInfo -> ArityInfo -> ArityInfo
forall a. Num a => a -> a -> a
+ ArityInfo
1)) ArityInfo -> ArityInfo -> Bool
forall a. Ord a => a -> a -> Bool
<= UnfoldingOpts -> ArityInfo
unfoldingUseThreshold UnfoldingOpts
opts
= Unfolding -> Maybe Unfolding
forall a. a -> Maybe a
Just (Unfolding
fn_unf { uf_src :: UnfoldingSource
uf_src = UnfoldingSource
src'
, uf_tmpl :: CoreArg
uf_tmpl = CoreArg
tmpl'
, uf_guidance :: UnfoldingGuidance
uf_guidance = UnfWhen { ug_arity :: ArityInfo
ug_arity = ArityInfo
unf_arity
, ug_unsat_ok :: Bool
ug_unsat_ok = Bool
unSaturatedOk
, ug_boring_ok :: Bool
ug_boring_ok = CoreArg -> Bool
inlineBoringOk CoreArg
tmpl' } })
| Bool
otherwise
= Maybe Unfolding
forall a. Maybe a
Nothing