{-# LANGUAGE CPP #-}
module GHC.Core.Opt.Simplify.Utils (
mkLam, mkCase, prepareAlts, tryEtaExpandRhs,
preInlineUnconditionally, postInlineUnconditionally,
activeUnfolding, activeRule,
getUnfoldingInRuleMatch,
simplEnvForGHCi, updModeForStableUnfoldings, updModeForRules,
SimplCont(..), DupFlag(..), StaticEnv,
isSimplified, contIsStop,
contIsDupable, contResultType, contHoleType,
contIsTrivial, contArgs,
countArgs,
mkBoringStop, mkRhsStop, mkLazyArgStop, contIsRhsOrArg,
interestingCallContext,
ArgInfo(..), ArgSpec(..), mkArgInfo,
addValArgTo, addCastTo, addTyArgTo,
argInfoExpr, argInfoAppArgs, pushSimplifiedArgs,
abstractFloats,
isExitJoinId
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Core.Opt.Simplify.Env
import GHC.Core.Opt.Monad ( SimplMode(..), Tick(..) )
import GHC.Driver.Session
import GHC.Core
import qualified GHC.Core.Subst
import GHC.Core.Ppr
import GHC.Core.TyCo.Ppr ( pprParendType )
import GHC.Core.FVs
import GHC.Core.Utils
import GHC.Core.Opt.Arity
import GHC.Core.Unfold
import GHC.Types.Name
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Var
import GHC.Types.Demand
import GHC.Types.Var.Set
import GHC.Types.Basic
import GHC.Core.Opt.Simplify.Monad
import GHC.Core.Type hiding( substTy )
import GHC.Core.Coercion hiding( substCo )
import GHC.Core.DataCon ( dataConWorkId, isNullaryRepDataCon )
import GHC.Utils.Misc
import GHC.Data.OrdList ( isNilOL )
import GHC.Utils.Monad
import GHC.Utils.Outputable
import GHC.Core.Opt.ConstantFold
import GHC.Data.FastString ( fsLit )
import Control.Monad ( when )
import Data.List ( sortBy )
data SimplCont
= Stop
OutType
CallCtxt
| CastIt
OutCoercion
SimplCont
| ApplyToVal
{ SimplCont -> DupFlag
sc_dup :: DupFlag
, SimplCont -> OutExpr
sc_arg :: InExpr
, SimplCont -> StaticEnv
sc_env :: StaticEnv
, SimplCont -> SimplCont
sc_cont :: SimplCont }
| ApplyToTy
{ SimplCont -> OutType
sc_arg_ty :: OutType
, SimplCont -> OutType
sc_hole_ty :: OutType
, sc_cont :: SimplCont }
| Select
{ sc_dup :: DupFlag
, SimplCont -> Id
sc_bndr :: InId
, SimplCont -> [InAlt]
sc_alts :: [InAlt]
, sc_env :: StaticEnv
, sc_cont :: SimplCont }
| StrictBind
{ sc_dup :: DupFlag
, sc_bndr :: InId
, SimplCont -> [Id]
sc_bndrs :: [InBndr]
, SimplCont -> OutExpr
sc_body :: InExpr
, sc_env :: StaticEnv
, sc_cont :: SimplCont }
| StrictArg
{ sc_dup :: DupFlag
, SimplCont -> ArgInfo
sc_fun :: ArgInfo
, SimplCont -> CallCtxt
sc_cci :: CallCtxt
, sc_cont :: SimplCont }
| TickIt
(Tickish Id)
SimplCont
type StaticEnv = SimplEnv
data DupFlag = NoDup
| Simplified
| OkToDup
isSimplified :: DupFlag -> Bool
isSimplified :: DupFlag -> Bool
isSimplified DupFlag
NoDup = Bool
False
isSimplified DupFlag
_ = Bool
True
perhapsSubstTy :: DupFlag -> StaticEnv -> Type -> Type
perhapsSubstTy :: DupFlag -> StaticEnv -> OutType -> OutType
perhapsSubstTy DupFlag
dup StaticEnv
env OutType
ty
| DupFlag -> Bool
isSimplified DupFlag
dup = OutType
ty
| Bool
otherwise = StaticEnv -> OutType -> OutType
substTy StaticEnv
env OutType
ty
instance Outputable DupFlag where
ppr :: DupFlag -> SDoc
ppr DupFlag
OkToDup = String -> SDoc
text String
"ok"
ppr DupFlag
NoDup = String -> SDoc
text String
"nodup"
ppr DupFlag
Simplified = String -> SDoc
text String
"simpl"
instance Outputable SimplCont where
ppr :: SimplCont -> SDoc
ppr (Stop OutType
ty CallCtxt
interesting) = String -> SDoc
text String
"Stop" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
brackets (CallCtxt -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable CallCtxt
ppr CallCtxt
interesting) SDoc -> SDoc -> SDoc
<+> OutType -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable OutType
ppr OutType
ty
ppr (CastIt OutCoercion
co SimplCont
cont ) = (String -> SDoc
text String
"CastIt" SDoc -> SDoc -> SDoc
<+> OutCoercion -> SDoc
pprOptCo OutCoercion
co) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (TickIt Tickish Id
t SimplCont
cont) = (String -> SDoc
text String
"TickIt" SDoc -> SDoc -> SDoc
<+> Tickish Id -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall id. Outputable id => Outputable (Tickish id)
External instance of the constraint type Outputable Id
ppr Tickish Id
t) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (ApplyToTy { sc_arg_ty :: SimplCont -> OutType
sc_arg_ty = OutType
ty, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont })
= (String -> SDoc
text String
"ApplyToTy" SDoc -> SDoc -> SDoc
<+> OutType -> SDoc
pprParendType OutType
ty) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (ApplyToVal { sc_arg :: SimplCont -> OutExpr
sc_arg = OutExpr
arg, sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
dup, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont })
= (String -> SDoc
text String
"ApplyToVal" SDoc -> SDoc -> SDoc
<+> DupFlag -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable DupFlag
ppr DupFlag
dup SDoc -> SDoc -> SDoc
<+> OutExpr -> SDoc
forall b. OutputableBndr b => Expr b -> SDoc
External instance of the constraint type OutputableBndr Id
pprParendExpr OutExpr
arg)
SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (StrictBind { sc_bndr :: SimplCont -> Id
sc_bndr = Id
b, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont })
= (String -> SDoc
text String
"StrictBind" SDoc -> SDoc -> SDoc
<+> Id -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable Id
ppr Id
b) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (StrictArg { sc_fun :: SimplCont -> ArgInfo
sc_fun = ArgInfo
ai, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont })
= (String -> SDoc
text String
"StrictArg" SDoc -> SDoc -> SDoc
<+> Id -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable Id
ppr (ArgInfo -> Id
ai_fun ArgInfo
ai)) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
ppr (Select { sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
dup, sc_bndr :: SimplCont -> Id
sc_bndr = Id
bndr, sc_alts :: SimplCont -> [InAlt]
sc_alts = [InAlt]
alts, sc_env :: SimplCont -> StaticEnv
sc_env = StaticEnv
se, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont })
= (String -> SDoc
text String
"Select" SDoc -> SDoc -> SDoc
<+> DupFlag -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable DupFlag
ppr DupFlag
dup SDoc -> SDoc -> SDoc
<+> Id -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable Id
ppr Id
bndr) SDoc -> SDoc -> SDoc
$$
SDoc -> SDoc
whenPprDebug (Arity -> SDoc -> SDoc
nest Arity
2 (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat [TvSubstEnv -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall a. Outputable a => Outputable (UniqFM a)
External instance of the constraint type Outputable OutType
ppr (StaticEnv -> TvSubstEnv
seTvSubst StaticEnv
se), [InAlt] -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall a. Outputable a => Outputable [a]
External instance of the constraint type forall a b c.
(Outputable a, Outputable b, Outputable c) =>
Outputable (a, b, c)
External instance of the constraint type Outputable AltCon
External instance of the constraint type forall a. Outputable a => Outputable [a]
External instance of the constraint type Outputable Id
External instance of the constraint type forall b. OutputableBndr b => Outputable (Expr b)
External instance of the constraint type OutputableBndr Id
ppr [InAlt]
alts]) SDoc -> SDoc -> SDoc
$$ SimplCont -> SDoc
forall a. Outputable a => a -> SDoc
Instance of class: Outputable of the constraint type Outputable SimplCont
ppr SimplCont
cont
data ArgInfo
= ArgInfo {
ArgInfo -> Id
ai_fun :: OutId,
ArgInfo -> [ArgSpec]
ai_args :: [ArgSpec],
ArgInfo -> OutType
ai_type :: OutType,
ArgInfo -> FunRules
ai_rules :: FunRules,
ArgInfo -> Bool
ai_encl :: Bool,
ArgInfo -> [Bool]
ai_strs :: [Bool],
ArgInfo -> [Arity]
ai_discs :: [Int]
}
data ArgSpec
= ValArg OutExpr
| TyArg { ArgSpec -> OutType
as_arg_ty :: OutType
, ArgSpec -> OutType
as_hole_ty :: OutType }
| CastBy OutCoercion
instance Outputable ArgSpec where
ppr :: ArgSpec -> SDoc
ppr (ValArg OutExpr
e) = String -> SDoc
text String
"ValArg" SDoc -> SDoc -> SDoc
<+> OutExpr -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall b. OutputableBndr b => Outputable (Expr b)
External instance of the constraint type OutputableBndr Id
ppr OutExpr
e
ppr (TyArg { as_arg_ty :: ArgSpec -> OutType
as_arg_ty = OutType
ty }) = String -> SDoc
text String
"TyArg" SDoc -> SDoc -> SDoc
<+> OutType -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable OutType
ppr OutType
ty
ppr (CastBy OutCoercion
c) = String -> SDoc
text String
"CastBy" SDoc -> SDoc -> SDoc
<+> OutCoercion -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type Outputable OutCoercion
ppr OutCoercion
c
addValArgTo :: ArgInfo -> OutExpr -> ArgInfo
addValArgTo :: ArgInfo -> OutExpr -> ArgInfo
addValArgTo ArgInfo
ai OutExpr
arg = ArgInfo
ai { ai_args :: [ArgSpec]
ai_args = OutExpr -> ArgSpec
ValArg OutExpr
arg ArgSpec -> [ArgSpec] -> [ArgSpec]
forall a. a -> [a] -> [a]
: ArgInfo -> [ArgSpec]
ai_args ArgInfo
ai
, ai_type :: OutType
ai_type = OutType -> OutExpr -> OutType
applyTypeToArg (ArgInfo -> OutType
ai_type ArgInfo
ai) OutExpr
arg
, ai_rules :: FunRules
ai_rules = FunRules -> FunRules
decRules (ArgInfo -> FunRules
ai_rules ArgInfo
ai) }
addTyArgTo :: ArgInfo -> OutType -> ArgInfo
addTyArgTo :: ArgInfo -> OutType -> ArgInfo
addTyArgTo ArgInfo
ai OutType
arg_ty = ArgInfo
ai { ai_args :: [ArgSpec]
ai_args = ArgSpec
arg_spec ArgSpec -> [ArgSpec] -> [ArgSpec]
forall a. a -> [a] -> [a]
: ArgInfo -> [ArgSpec]
ai_args ArgInfo
ai
, ai_type :: OutType
ai_type = HasDebugCallStack => OutType -> OutType -> OutType
OutType -> OutType -> OutType
External instance of the constraint type HasDebugCallStack
piResultTy OutType
poly_fun_ty OutType
arg_ty
, ai_rules :: FunRules
ai_rules = FunRules -> FunRules
decRules (ArgInfo -> FunRules
ai_rules ArgInfo
ai) }
where
poly_fun_ty :: OutType
poly_fun_ty = ArgInfo -> OutType
ai_type ArgInfo
ai
arg_spec :: ArgSpec
arg_spec = TyArg :: OutType -> OutType -> ArgSpec
TyArg { as_arg_ty :: OutType
as_arg_ty = OutType
arg_ty, as_hole_ty :: OutType
as_hole_ty = OutType
poly_fun_ty }
addCastTo :: ArgInfo -> OutCoercion -> ArgInfo
addCastTo :: ArgInfo -> OutCoercion -> ArgInfo
addCastTo ArgInfo
ai OutCoercion
co = ArgInfo
ai { ai_args :: [ArgSpec]
ai_args = OutCoercion -> ArgSpec
CastBy OutCoercion
co ArgSpec -> [ArgSpec] -> [ArgSpec]
forall a. a -> [a] -> [a]
: ArgInfo -> [ArgSpec]
ai_args ArgInfo
ai
, ai_type :: OutType
ai_type = OutCoercion -> OutType
coercionRKind OutCoercion
co }
argInfoAppArgs :: [ArgSpec] -> [OutExpr]
argInfoAppArgs :: [ArgSpec] -> [OutExpr]
argInfoAppArgs [] = []
argInfoAppArgs (CastBy {} : [ArgSpec]
_) = []
argInfoAppArgs (ValArg OutExpr
e : [ArgSpec]
as) = OutExpr
e OutExpr -> [OutExpr] -> [OutExpr]
forall a. a -> [a] -> [a]
: [ArgSpec] -> [OutExpr]
argInfoAppArgs [ArgSpec]
as
argInfoAppArgs (TyArg { as_arg_ty :: ArgSpec -> OutType
as_arg_ty = OutType
ty } : [ArgSpec]
as) = OutType -> OutExpr
forall b. OutType -> Expr b
Type OutType
ty OutExpr -> [OutExpr] -> [OutExpr]
forall a. a -> [a] -> [a]
: [ArgSpec] -> [OutExpr]
argInfoAppArgs [ArgSpec]
as
pushSimplifiedArgs :: SimplEnv -> [ArgSpec] -> SimplCont -> SimplCont
pushSimplifiedArgs :: StaticEnv -> [ArgSpec] -> SimplCont -> SimplCont
pushSimplifiedArgs StaticEnv
_env [] SimplCont
k = SimplCont
k
pushSimplifiedArgs StaticEnv
env (ArgSpec
arg : [ArgSpec]
args) SimplCont
k
= case ArgSpec
arg of
TyArg { as_arg_ty :: ArgSpec -> OutType
as_arg_ty = OutType
arg_ty, as_hole_ty :: ArgSpec -> OutType
as_hole_ty = OutType
hole_ty }
-> ApplyToTy :: OutType -> OutType -> SimplCont -> SimplCont
ApplyToTy { sc_arg_ty :: OutType
sc_arg_ty = OutType
arg_ty, sc_hole_ty :: OutType
sc_hole_ty = OutType
hole_ty, sc_cont :: SimplCont
sc_cont = SimplCont
rest }
ValArg OutExpr
e -> ApplyToVal :: DupFlag -> OutExpr -> StaticEnv -> SimplCont -> SimplCont
ApplyToVal { sc_arg :: OutExpr
sc_arg = OutExpr
e, sc_env :: StaticEnv
sc_env = StaticEnv
env, sc_dup :: DupFlag
sc_dup = DupFlag
Simplified, sc_cont :: SimplCont
sc_cont = SimplCont
rest }
CastBy OutCoercion
c -> OutCoercion -> SimplCont -> SimplCont
CastIt OutCoercion
c SimplCont
rest
where
rest :: SimplCont
rest = StaticEnv -> [ArgSpec] -> SimplCont -> SimplCont
pushSimplifiedArgs StaticEnv
env [ArgSpec]
args SimplCont
k
argInfoExpr :: OutId -> [ArgSpec] -> OutExpr
argInfoExpr :: Id -> [ArgSpec] -> OutExpr
argInfoExpr Id
fun [ArgSpec]
rev_args
= [ArgSpec] -> OutExpr
go [ArgSpec]
rev_args
where
go :: [ArgSpec] -> OutExpr
go [] = Id -> OutExpr
forall b. Id -> Expr b
Var Id
fun
go (ValArg OutExpr
a : [ArgSpec]
as) = [ArgSpec] -> OutExpr
go [ArgSpec]
as OutExpr -> OutExpr -> OutExpr
forall b. Expr b -> Expr b -> Expr b
`App` OutExpr
a
go (TyArg { as_arg_ty :: ArgSpec -> OutType
as_arg_ty = OutType
ty } : [ArgSpec]
as) = [ArgSpec] -> OutExpr
go [ArgSpec]
as OutExpr -> OutExpr -> OutExpr
forall b. Expr b -> Expr b -> Expr b
`App` OutType -> OutExpr
forall b. OutType -> Expr b
Type OutType
ty
go (CastBy OutCoercion
co : [ArgSpec]
as) = OutExpr -> OutCoercion -> OutExpr
mkCast ([ArgSpec] -> OutExpr
go [ArgSpec]
as) OutCoercion
co
type FunRules = Maybe (Int, [CoreRule])
decRules :: FunRules -> FunRules
decRules :: FunRules -> FunRules
decRules (Just (Arity
n, [CoreRule]
rules)) = (Arity, [CoreRule]) -> FunRules
forall a. a -> Maybe a
Just (Arity
nArity -> Arity -> Arity
forall a. Num a => a -> a -> a
External instance of the constraint type Num Arity
-Arity
1, [CoreRule]
rules)
decRules FunRules
Nothing = FunRules
forall a. Maybe a
Nothing
mkFunRules :: [CoreRule] -> FunRules
mkFunRules :: [CoreRule] -> FunRules
mkFunRules [] = FunRules
forall a. Maybe a
Nothing
mkFunRules [CoreRule]
rs = (Arity, [CoreRule]) -> FunRules
forall a. a -> Maybe a
Just (Arity
n_required, [CoreRule]
rs)
where
n_required :: Arity
n_required = [Arity] -> Arity
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
External instance of the constraint type Ord Arity
External instance of the constraint type Foldable []
maximum ((CoreRule -> Arity) -> [CoreRule] -> [Arity]
forall a b. (a -> b) -> [a] -> [b]
map CoreRule -> Arity
ruleArity [CoreRule]
rs)
mkBoringStop :: OutType -> SimplCont
mkBoringStop :: OutType -> SimplCont
mkBoringStop OutType
ty = OutType -> CallCtxt -> SimplCont
Stop OutType
ty CallCtxt
BoringCtxt
mkRhsStop :: OutType -> SimplCont
mkRhsStop :: OutType -> SimplCont
mkRhsStop OutType
ty = OutType -> CallCtxt -> SimplCont
Stop OutType
ty CallCtxt
RhsCtxt
mkLazyArgStop :: OutType -> CallCtxt -> SimplCont
mkLazyArgStop :: OutType -> CallCtxt -> SimplCont
mkLazyArgStop OutType
ty CallCtxt
cci = OutType -> CallCtxt -> SimplCont
Stop OutType
ty CallCtxt
cci
contIsRhsOrArg :: SimplCont -> Bool
contIsRhsOrArg :: SimplCont -> Bool
contIsRhsOrArg (Stop {}) = Bool
True
contIsRhsOrArg (StrictBind {}) = Bool
True
contIsRhsOrArg (StrictArg {}) = Bool
True
contIsRhsOrArg SimplCont
_ = Bool
False
contIsRhs :: SimplCont -> Bool
contIsRhs :: SimplCont -> Bool
contIsRhs (Stop OutType
_ CallCtxt
RhsCtxt) = Bool
True
contIsRhs SimplCont
_ = Bool
False
contIsStop :: SimplCont -> Bool
contIsStop :: SimplCont -> Bool
contIsStop (Stop {}) = Bool
True
contIsStop SimplCont
_ = Bool
False
contIsDupable :: SimplCont -> Bool
contIsDupable :: SimplCont -> Bool
contIsDupable (Stop {}) = Bool
True
contIsDupable (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> Bool
contIsDupable SimplCont
k
contIsDupable (ApplyToVal { sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
OkToDup }) = Bool
True
contIsDupable (Select { sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
OkToDup }) = Bool
True
contIsDupable (StrictArg { sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
OkToDup }) = Bool
True
contIsDupable (CastIt OutCoercion
_ SimplCont
k) = SimplCont -> Bool
contIsDupable SimplCont
k
contIsDupable SimplCont
_ = Bool
False
contIsTrivial :: SimplCont -> Bool
contIsTrivial :: SimplCont -> Bool
contIsTrivial (Stop {}) = Bool
True
contIsTrivial (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> Bool
contIsTrivial SimplCont
k
contIsTrivial (ApplyToVal { sc_arg :: SimplCont -> OutExpr
sc_arg = Coercion OutCoercion
_, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> Bool
contIsTrivial SimplCont
k
contIsTrivial (CastIt OutCoercion
_ SimplCont
k) = SimplCont -> Bool
contIsTrivial SimplCont
k
contIsTrivial SimplCont
_ = Bool
False
contResultType :: SimplCont -> OutType
contResultType :: SimplCont -> OutType
contResultType (Stop OutType
ty CallCtxt
_) = OutType
ty
contResultType (CastIt OutCoercion
_ SimplCont
k) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (StrictBind { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (StrictArg { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (Select { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (ApplyToVal { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> OutType
contResultType SimplCont
k
contResultType (TickIt Tickish Id
_ SimplCont
k) = SimplCont -> OutType
contResultType SimplCont
k
contHoleType :: SimplCont -> OutType
contHoleType :: SimplCont -> OutType
contHoleType (Stop OutType
ty CallCtxt
_) = OutType
ty
contHoleType (TickIt Tickish Id
_ SimplCont
k) = SimplCont -> OutType
contHoleType SimplCont
k
contHoleType (CastIt OutCoercion
co SimplCont
_) = OutCoercion -> OutType
coercionLKind OutCoercion
co
contHoleType (StrictBind { sc_bndr :: SimplCont -> Id
sc_bndr = Id
b, sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
dup, sc_env :: SimplCont -> StaticEnv
sc_env = StaticEnv
se })
= DupFlag -> StaticEnv -> OutType -> OutType
perhapsSubstTy DupFlag
dup StaticEnv
se (Id -> OutType
idType Id
b)
contHoleType (StrictArg { sc_fun :: SimplCont -> ArgInfo
sc_fun = ArgInfo
ai }) = OutType -> OutType
funArgTy (ArgInfo -> OutType
ai_type ArgInfo
ai)
contHoleType (ApplyToTy { sc_hole_ty :: SimplCont -> OutType
sc_hole_ty = OutType
ty }) = OutType
ty
contHoleType (ApplyToVal { sc_arg :: SimplCont -> OutExpr
sc_arg = OutExpr
e, sc_env :: SimplCont -> StaticEnv
sc_env = StaticEnv
se, sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
dup, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k })
= OutType -> OutType -> OutType
mkVisFunTy (DupFlag -> StaticEnv -> OutType -> OutType
perhapsSubstTy DupFlag
dup StaticEnv
se (OutExpr -> OutType
exprType OutExpr
e))
(SimplCont -> OutType
contHoleType SimplCont
k)
contHoleType (Select { sc_dup :: SimplCont -> DupFlag
sc_dup = DupFlag
d, sc_bndr :: SimplCont -> Id
sc_bndr = Id
b, sc_env :: SimplCont -> StaticEnv
sc_env = StaticEnv
se })
= DupFlag -> StaticEnv -> OutType -> OutType
perhapsSubstTy DupFlag
d StaticEnv
se (Id -> OutType
idType Id
b)
countArgs :: SimplCont -> Int
countArgs :: SimplCont -> Arity
countArgs (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont }) = Arity
1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
External instance of the constraint type Num Arity
+ SimplCont -> Arity
countArgs SimplCont
cont
countArgs (ApplyToVal { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
cont }) = Arity
1 Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
External instance of the constraint type Num Arity
+ SimplCont -> Arity
countArgs SimplCont
cont
countArgs SimplCont
_ = Arity
0
contArgs :: SimplCont -> (Bool, [ArgSummary], SimplCont)
contArgs :: SimplCont -> (Bool, [ArgSummary], SimplCont)
contArgs SimplCont
cont
| SimplCont -> Bool
lone SimplCont
cont = (Bool
True, [], SimplCont
cont)
| Bool
otherwise = [ArgSummary] -> SimplCont -> (Bool, [ArgSummary], SimplCont)
go [] SimplCont
cont
where
lone :: SimplCont -> Bool
lone (ApplyToTy {}) = Bool
False
lone (ApplyToVal {}) = Bool
False
lone (CastIt {}) = Bool
False
lone SimplCont
_ = Bool
True
go :: [ArgSummary] -> SimplCont -> (Bool, [ArgSummary], SimplCont)
go [ArgSummary]
args (ApplyToVal { sc_arg :: SimplCont -> OutExpr
sc_arg = OutExpr
arg, sc_env :: SimplCont -> StaticEnv
sc_env = StaticEnv
se, sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k })
= [ArgSummary] -> SimplCont -> (Bool, [ArgSummary], SimplCont)
go (OutExpr -> StaticEnv -> ArgSummary
is_interesting OutExpr
arg StaticEnv
se ArgSummary -> [ArgSummary] -> [ArgSummary]
forall a. a -> [a] -> [a]
: [ArgSummary]
args) SimplCont
k
go [ArgSummary]
args (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = [ArgSummary] -> SimplCont -> (Bool, [ArgSummary], SimplCont)
go [ArgSummary]
args SimplCont
k
go [ArgSummary]
args (CastIt OutCoercion
_ SimplCont
k) = [ArgSummary] -> SimplCont -> (Bool, [ArgSummary], SimplCont)
go [ArgSummary]
args SimplCont
k
go [ArgSummary]
args SimplCont
k = (Bool
False, [ArgSummary] -> [ArgSummary]
forall a. [a] -> [a]
reverse [ArgSummary]
args, SimplCont
k)
is_interesting :: OutExpr -> StaticEnv -> ArgSummary
is_interesting OutExpr
arg StaticEnv
se = StaticEnv -> OutExpr -> ArgSummary
interestingArg StaticEnv
se OutExpr
arg
mkArgInfo :: SimplEnv
-> Id
-> [CoreRule]
-> Int
-> SimplCont
-> ArgInfo
mkArgInfo :: StaticEnv -> Id -> [CoreRule] -> Arity -> SimplCont -> ArgInfo
mkArgInfo StaticEnv
env Id
fun [CoreRule]
rules Arity
n_val_args SimplCont
call_cont
| Arity
n_val_args Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
< Id -> Arity
idArity Id
fun
= ArgInfo :: Id
-> [ArgSpec]
-> OutType
-> FunRules
-> Bool
-> [Bool]
-> [Arity]
-> ArgInfo
ArgInfo { ai_fun :: Id
ai_fun = Id
fun, ai_args :: [ArgSpec]
ai_args = [], ai_type :: OutType
ai_type = OutType
fun_ty
, ai_rules :: FunRules
ai_rules = FunRules
fun_rules
, ai_encl :: Bool
ai_encl = Bool
False
, ai_strs :: [Bool]
ai_strs = [Bool]
vanilla_stricts
, ai_discs :: [Arity]
ai_discs = [Arity]
vanilla_discounts }
| Bool
otherwise
= ArgInfo :: Id
-> [ArgSpec]
-> OutType
-> FunRules
-> Bool
-> [Bool]
-> [Arity]
-> ArgInfo
ArgInfo { ai_fun :: Id
ai_fun = Id
fun, ai_args :: [ArgSpec]
ai_args = [], ai_type :: OutType
ai_type = OutType
fun_ty
, ai_rules :: FunRules
ai_rules = FunRules
fun_rules
, ai_encl :: Bool
ai_encl = [CoreRule] -> SimplCont -> Bool
interestingArgContext [CoreRule]
rules SimplCont
call_cont
, ai_strs :: [Bool]
ai_strs = [Bool]
arg_stricts
, ai_discs :: [Arity]
ai_discs = [Arity]
arg_discounts }
where
fun_ty :: OutType
fun_ty = Id -> OutType
idType Id
fun
fun_rules :: FunRules
fun_rules = [CoreRule] -> FunRules
mkFunRules [CoreRule]
rules
vanilla_discounts, arg_discounts :: [Int]
vanilla_discounts :: [Arity]
vanilla_discounts = Arity -> [Arity]
forall a. a -> [a]
repeat Arity
0
arg_discounts :: [Arity]
arg_discounts = case Id -> Unfolding
idUnfolding Id
fun of
CoreUnfolding {uf_guidance :: Unfolding -> UnfoldingGuidance
uf_guidance = UnfIfGoodArgs {ug_args :: UnfoldingGuidance -> [Arity]
ug_args = [Arity]
discounts}}
-> [Arity]
discounts [Arity] -> [Arity] -> [Arity]
forall a. [a] -> [a] -> [a]
++ [Arity]
vanilla_discounts
Unfolding
_ -> [Arity]
vanilla_discounts
vanilla_stricts, arg_stricts :: [Bool]
vanilla_stricts :: [Bool]
vanilla_stricts = Bool -> [Bool]
forall a. a -> [a]
repeat Bool
False
arg_stricts :: [Bool]
arg_stricts
| Bool -> Bool
not (SimplMode -> Bool
sm_inline (StaticEnv -> SimplMode
seMode StaticEnv
env))
= [Bool]
vanilla_stricts
| Bool
otherwise
= OutType -> [Bool] -> [Bool]
add_type_str OutType
fun_ty ([Bool] -> [Bool]) -> [Bool] -> [Bool]
forall a b. (a -> b) -> a -> b
$
case StrictSig -> ([Demand], Divergence)
splitStrictSig (Id -> StrictSig
idStrictness Id
fun) of
([Demand]
demands, Divergence
result_info)
| Bool -> Bool
not ([Demand]
demands [Demand] -> Arity -> Bool
forall a. [a] -> Arity -> Bool
`lengthExceeds` Arity
n_val_args)
->
if Divergence -> Bool
isDeadEndDiv Divergence
result_info then
(Demand -> Bool) -> [Demand] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isStrictDmd [Demand]
demands
else
(Demand -> Bool) -> [Demand] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isStrictDmd [Demand]
demands [Bool] -> [Bool] -> [Bool]
forall a. [a] -> [a] -> [a]
++ [Bool]
vanilla_stricts
| Bool
otherwise
-> WARN( True, text "More demands than arity" <+> ppr fun <+> ppr (idArity fun)
<+> ppr n_val_args <+> ppr demands )
[Bool]
vanilla_stricts
add_type_str :: Type -> [Bool] -> [Bool]
add_type_str :: OutType -> [Bool] -> [Bool]
add_type_str OutType
_ [] = []
add_type_str OutType
fun_ty all_strs :: [Bool]
all_strs@(Bool
str:[Bool]
strs)
| Just (OutType
arg_ty, OutType
fun_ty') <- OutType -> Maybe (OutType, OutType)
splitFunTy_maybe OutType
fun_ty
= (Bool
str Bool -> Bool -> Bool
|| Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Eq a => Eq (Maybe a)
External instance of the constraint type Eq Bool
== HasDebugCallStack => OutType -> Maybe Bool
OutType -> Maybe Bool
External instance of the constraint type HasDebugCallStack
isLiftedType_maybe OutType
arg_ty)
Bool -> [Bool] -> [Bool]
forall a. a -> [a] -> [a]
: OutType -> [Bool] -> [Bool]
add_type_str OutType
fun_ty' [Bool]
strs
| Just (Id
_, OutType
fun_ty') <- OutType -> Maybe (Id, OutType)
splitForAllTy_maybe OutType
fun_ty
= OutType -> [Bool] -> [Bool]
add_type_str OutType
fun_ty' [Bool]
all_strs
| Bool
otherwise
= [Bool]
all_strs
interestingCallContext :: SimplEnv -> SimplCont -> CallCtxt
interestingCallContext :: StaticEnv -> SimplCont -> CallCtxt
interestingCallContext StaticEnv
env SimplCont
cont
= SimplCont -> CallCtxt
interesting SimplCont
cont
where
interesting :: SimplCont -> CallCtxt
interesting (Select {})
| SimplMode -> Bool
sm_case_case (StaticEnv -> SimplMode
getMode StaticEnv
env) = CallCtxt
CaseCtxt
| Bool
otherwise = CallCtxt
BoringCtxt
interesting (ApplyToVal {}) = CallCtxt
ValAppCtxt
interesting (StrictArg { sc_cci :: SimplCont -> CallCtxt
sc_cci = CallCtxt
cci }) = CallCtxt
cci
interesting (StrictBind {}) = CallCtxt
BoringCtxt
interesting (Stop OutType
_ CallCtxt
cci) = CallCtxt
cci
interesting (TickIt Tickish Id
_ SimplCont
k) = SimplCont -> CallCtxt
interesting SimplCont
k
interesting (ApplyToTy { sc_cont :: SimplCont -> SimplCont
sc_cont = SimplCont
k }) = SimplCont -> CallCtxt
interesting SimplCont
k
interesting (CastIt OutCoercion
_ SimplCont
k) = SimplCont -> CallCtxt
interesting SimplCont
k
interestingArgContext :: [CoreRule] -> SimplCont -> Bool
interestingArgContext :: [CoreRule] -> SimplCont -> Bool
interestingArgContext [CoreRule]
rules SimplCont
call_cont
= [CoreRule] -> Bool
forall a. [a] -> Bool
notNull [CoreRule]
rules Bool -> Bool -> Bool
|| Bool
enclosing_fn_has_rules
where
enclosing_fn_has_rules :: Bool
enclosing_fn_has_rules = SimplCont -> Bool
go SimplCont
call_cont
go :: SimplCont -> Bool
go (Select {}) = Bool
False
go (ApplyToVal {}) = Bool
False
go (ApplyToTy {}) = Bool
False
go (StrictArg { sc_cci :: SimplCont -> CallCtxt
sc_cci = CallCtxt
cci }) = CallCtxt -> Bool
interesting CallCtxt
cci
go (StrictBind {}) = Bool
False
go (CastIt OutCoercion
_ SimplCont
c) = SimplCont -> Bool
go SimplCont
c
go (Stop OutType
_ CallCtxt
cci) = CallCtxt -> Bool
interesting CallCtxt
cci
go (TickIt Tickish Id
_ SimplCont
c) = SimplCont -> Bool
go SimplCont
c
interesting :: CallCtxt -> Bool
interesting CallCtxt
RuleArgCtxt = Bool
True
interesting CallCtxt
_ = Bool
False
interestingArg :: SimplEnv -> CoreExpr -> ArgSummary
interestingArg :: StaticEnv -> OutExpr -> ArgSummary
interestingArg StaticEnv
env OutExpr
e = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
0 OutExpr
e
where
go :: StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
n (Var Id
v)
= case StaticEnv -> Id -> SimplSR
substId StaticEnv
env Id
v of
DoneId Id
v' -> Arity -> Id -> ArgSummary
go_var Arity
n Id
v'
DoneEx OutExpr
e Maybe Arity
_ -> StaticEnv -> Arity -> OutExpr -> ArgSummary
go (StaticEnv -> StaticEnv
zapSubstEnv StaticEnv
env) Arity
n OutExpr
e
ContEx TvSubstEnv
tvs CvSubstEnv
cvs SimplIdSubst
ids OutExpr
e -> StaticEnv -> Arity -> OutExpr -> ArgSummary
go (StaticEnv -> TvSubstEnv -> CvSubstEnv -> SimplIdSubst -> StaticEnv
setSubstEnv StaticEnv
env TvSubstEnv
tvs CvSubstEnv
cvs SimplIdSubst
ids) Arity
n OutExpr
e
go StaticEnv
_ Arity
_ (Lit {}) = ArgSummary
ValueArg
go StaticEnv
_ Arity
_ (Type OutType
_) = ArgSummary
TrivArg
go StaticEnv
_ Arity
_ (Coercion OutCoercion
_) = ArgSummary
TrivArg
go StaticEnv
env Arity
n (App OutExpr
fn (Type OutType
_)) = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
n OutExpr
fn
go StaticEnv
env Arity
n (App OutExpr
fn OutExpr
_) = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env (Arity
nArity -> Arity -> Arity
forall a. Num a => a -> a -> a
External instance of the constraint type Num Arity
+Arity
1) OutExpr
fn
go StaticEnv
env Arity
n (Tick Tickish Id
_ OutExpr
a) = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
n OutExpr
a
go StaticEnv
env Arity
n (Cast OutExpr
e OutCoercion
_) = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
n OutExpr
e
go StaticEnv
env Arity
n (Lam Id
v OutExpr
e)
| Id -> Bool
isTyVar Id
v = StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env Arity
n OutExpr
e
| Arity
nArity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
>Arity
0 = ArgSummary
NonTrivArg
| Bool
otherwise = ArgSummary
ValueArg
go StaticEnv
_ Arity
_ (Case {}) = ArgSummary
NonTrivArg
go StaticEnv
env Arity
n (Let Bind Id
b OutExpr
e) = case StaticEnv -> Arity -> OutExpr -> ArgSummary
go StaticEnv
env' Arity
n OutExpr
e of
ArgSummary
ValueArg -> ArgSummary
ValueArg
ArgSummary
_ -> ArgSummary
NonTrivArg
where
env' :: StaticEnv
env' = StaticEnv
env StaticEnv -> [Id] -> StaticEnv
`addNewInScopeIds` Bind Id -> [Id]
forall b. Bind b -> [b]
bindersOf Bind Id
b
go_var :: Arity -> Id -> ArgSummary
go_var Arity
n Id
v
| Id -> Bool
isConLikeId Id
v = ArgSummary
ValueArg
| Id -> Arity
idArity Id
v Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
> Arity
n = ArgSummary
ValueArg
| Arity
n Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
> Arity
0 = ArgSummary
NonTrivArg
| Bool
conlike_unfolding = ArgSummary
ValueArg
| Bool
otherwise = ArgSummary
TrivArg
where
conlike_unfolding :: Bool
conlike_unfolding = Unfolding -> Bool
isConLikeUnfolding (Id -> Unfolding
idUnfolding Id
v)
simplEnvForGHCi :: DynFlags -> SimplEnv
simplEnvForGHCi :: DynFlags -> StaticEnv
simplEnvForGHCi DynFlags
dflags
= SimplMode -> StaticEnv
mkSimplEnv (SimplMode -> StaticEnv) -> SimplMode -> StaticEnv
forall a b. (a -> b) -> a -> b
$ SimplMode :: [String]
-> CompilerPhase
-> DynFlags
-> Bool
-> Bool
-> Bool
-> Bool
-> SimplMode
SimplMode { sm_names :: [String]
sm_names = [String
"GHCi"]
, sm_phase :: CompilerPhase
sm_phase = CompilerPhase
InitialPhase
, sm_dflags :: DynFlags
sm_dflags = DynFlags
dflags
, sm_rules :: Bool
sm_rules = Bool
rules_on
, sm_inline :: Bool
sm_inline = Bool
False
, sm_eta_expand :: Bool
sm_eta_expand = Bool
eta_expand_on
, sm_case_case :: Bool
sm_case_case = Bool
True }
where
rules_on :: Bool
rules_on = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_EnableRewriteRules DynFlags
dflags
eta_expand_on :: Bool
eta_expand_on = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_DoLambdaEtaExpansion DynFlags
dflags
updModeForStableUnfoldings :: Activation -> SimplMode -> SimplMode
updModeForStableUnfoldings :: Activation -> SimplMode -> SimplMode
updModeForStableUnfoldings Activation
inline_rule_act SimplMode
current_mode
= SimplMode
current_mode { sm_phase :: CompilerPhase
sm_phase = Activation -> CompilerPhase
phaseFromActivation Activation
inline_rule_act
, sm_inline :: Bool
sm_inline = Bool
True
, sm_eta_expand :: Bool
sm_eta_expand = Bool
False }
where
phaseFromActivation :: Activation -> CompilerPhase
phaseFromActivation (ActiveAfter SourceText
_ Arity
n) = Arity -> CompilerPhase
Phase Arity
n
phaseFromActivation Activation
_ = CompilerPhase
InitialPhase
updModeForRules :: SimplMode -> SimplMode
updModeForRules :: SimplMode -> SimplMode
updModeForRules SimplMode
current_mode
= SimplMode
current_mode { sm_phase :: CompilerPhase
sm_phase = CompilerPhase
InitialPhase
, sm_inline :: Bool
sm_inline = Bool
False
, sm_rules :: Bool
sm_rules = Bool
False
, sm_eta_expand :: Bool
sm_eta_expand = Bool
False }
activeUnfolding :: SimplMode -> Id -> Bool
activeUnfolding :: SimplMode -> Id -> Bool
activeUnfolding SimplMode
mode Id
id
| Unfolding -> Bool
isCompulsoryUnfolding (Id -> Unfolding
realIdUnfolding Id
id)
= Bool
True
| Bool
otherwise
= CompilerPhase -> Activation -> Bool
isActive (SimplMode -> CompilerPhase
sm_phase SimplMode
mode) (Id -> Activation
idInlineActivation Id
id)
Bool -> Bool -> Bool
&& SimplMode -> Bool
sm_inline SimplMode
mode
getUnfoldingInRuleMatch :: SimplEnv -> InScopeEnv
getUnfoldingInRuleMatch :: StaticEnv -> InScopeEnv
getUnfoldingInRuleMatch StaticEnv
env
= (InScopeSet
in_scope, Id -> Unfolding
id_unf)
where
in_scope :: InScopeSet
in_scope = StaticEnv -> InScopeSet
seInScope StaticEnv
env
mode :: SimplMode
mode = StaticEnv -> SimplMode
getMode StaticEnv
env
id_unf :: Id -> Unfolding
id_unf Id
id | Id -> Bool
unf_is_active Id
id = Id -> Unfolding
idUnfolding Id
id
| Bool
otherwise = Unfolding
NoUnfolding
unf_is_active :: Id -> Bool
unf_is_active Id
id
| Bool -> Bool
not (SimplMode -> Bool
sm_rules SimplMode
mode) =
Unfolding -> Bool
isStableUnfolding (Id -> Unfolding
realIdUnfolding Id
id)
| Bool
otherwise = CompilerPhase -> Activation -> Bool
isActive (SimplMode -> CompilerPhase
sm_phase SimplMode
mode) (Id -> Activation
idInlineActivation Id
id)
activeRule :: SimplMode -> Activation -> Bool
activeRule :: SimplMode -> Activation -> Bool
activeRule SimplMode
mode
| Bool -> Bool
not (SimplMode -> Bool
sm_rules SimplMode
mode) = \Activation
_ -> Bool
False
| Bool
otherwise = CompilerPhase -> Activation -> Bool
isActive (SimplMode -> CompilerPhase
sm_phase SimplMode
mode)
preInlineUnconditionally
:: SimplEnv -> TopLevelFlag -> InId
-> InExpr -> StaticEnv
-> Maybe SimplEnv
preInlineUnconditionally :: StaticEnv
-> TopLevelFlag -> Id -> OutExpr -> StaticEnv -> Maybe StaticEnv
preInlineUnconditionally StaticEnv
env TopLevelFlag
top_lvl Id
bndr OutExpr
rhs StaticEnv
rhs_env
| Bool -> Bool
not Bool
pre_inline_unconditionally = Maybe StaticEnv
forall a. Maybe a
Nothing
| Bool -> Bool
not Bool
active = Maybe StaticEnv
forall a. Maybe a
Nothing
| TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl Bool -> Bool -> Bool
&& Id -> Bool
isDeadEndId Id
bndr = Maybe StaticEnv
forall a. Maybe a
Nothing
| Id -> Bool
isCoVar Id
bndr = Maybe StaticEnv
forall a. Maybe a
Nothing
| Id -> Bool
isExitJoinId Id
bndr = Maybe StaticEnv
forall a. Maybe a
Nothing
| Bool -> Bool
not (OccInfo -> Bool
one_occ (Id -> OccInfo
idOccInfo Id
bndr)) = Maybe StaticEnv
forall a. Maybe a
Nothing
| Bool -> Bool
not (Unfolding -> Bool
isStableUnfolding Unfolding
unf) = StaticEnv -> Maybe StaticEnv
forall a. a -> Maybe a
Just (OutExpr -> StaticEnv
extend_subst_with OutExpr
rhs)
| InlinePragma -> Bool
isInlinablePragma InlinePragma
inline_prag
, Just OutExpr
inl <- Unfolding -> Maybe OutExpr
maybeUnfoldingTemplate Unfolding
unf = StaticEnv -> Maybe StaticEnv
forall a. a -> Maybe a
Just (OutExpr -> StaticEnv
extend_subst_with OutExpr
inl)
| Bool
otherwise = Maybe StaticEnv
forall a. Maybe a
Nothing
where
unf :: Unfolding
unf = Id -> Unfolding
idUnfolding Id
bndr
extend_subst_with :: OutExpr -> StaticEnv
extend_subst_with OutExpr
inl_rhs = StaticEnv -> Id -> SimplSR -> StaticEnv
extendIdSubst StaticEnv
env Id
bndr (StaticEnv -> OutExpr -> SimplSR
mkContEx StaticEnv
rhs_env OutExpr
inl_rhs)
one_occ :: OccInfo -> Bool
one_occ OccInfo
IAmDead = Bool
True
one_occ OneOcc{ occ_one_br :: OccInfo -> OneBranch
occ_one_br = OneBranch
InOneBranch
, occ_in_lam :: OccInfo -> InsideLam
occ_in_lam = InsideLam
NotInsideLam } = TopLevelFlag -> Bool
isNotTopLevel TopLevelFlag
top_lvl Bool -> Bool -> Bool
|| Bool
early_phase
one_occ OneOcc{ occ_one_br :: OccInfo -> OneBranch
occ_one_br = OneBranch
InOneBranch
, occ_in_lam :: OccInfo -> InsideLam
occ_in_lam = InsideLam
IsInsideLam
, occ_int_cxt :: OccInfo -> InterestingCxt
occ_int_cxt = InterestingCxt
IsInteresting } = OutExpr -> Bool
canInlineInLam OutExpr
rhs
one_occ OccInfo
_ = Bool
False
pre_inline_unconditionally :: Bool
pre_inline_unconditionally = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SimplPreInlining (StaticEnv -> DynFlags
seDynFlags StaticEnv
env)
mode :: SimplMode
mode = StaticEnv -> SimplMode
getMode StaticEnv
env
active :: Bool
active = CompilerPhase -> Activation -> Bool
isActive (SimplMode -> CompilerPhase
sm_phase SimplMode
mode) (InlinePragma -> Activation
inlinePragmaActivation InlinePragma
inline_prag)
inline_prag :: InlinePragma
inline_prag = Id -> InlinePragma
idInlinePragma Id
bndr
canInlineInLam :: OutExpr -> Bool
canInlineInLam (Lit Literal
_) = Bool
True
canInlineInLam (Lam Id
b OutExpr
e) = Id -> Bool
isRuntimeVar Id
b Bool -> Bool -> Bool
|| OutExpr -> Bool
canInlineInLam OutExpr
e
canInlineInLam (Tick Tickish Id
t OutExpr
e) = Bool -> Bool
not (Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishIsCode Tickish Id
t) Bool -> Bool -> Bool
&& OutExpr -> Bool
canInlineInLam OutExpr
e
canInlineInLam OutExpr
_ = Bool
False
early_phase :: Bool
early_phase = case SimplMode -> CompilerPhase
sm_phase SimplMode
mode of
Phase Arity
0 -> Bool
False
CompilerPhase
_ -> Bool
True
postInlineUnconditionally
:: SimplEnv -> TopLevelFlag
-> OutId
-> OccInfo
-> OutExpr
-> Bool
postInlineUnconditionally :: StaticEnv -> TopLevelFlag -> Id -> OccInfo -> OutExpr -> Bool
postInlineUnconditionally StaticEnv
env TopLevelFlag
top_lvl Id
bndr OccInfo
occ_info OutExpr
rhs
| Bool -> Bool
not Bool
active = Bool
False
| OccInfo -> Bool
isWeakLoopBreaker OccInfo
occ_info = Bool
False
| Unfolding -> Bool
isStableUnfolding Unfolding
unfolding = Bool
False
| TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl = Bool
False
| OutExpr -> Bool
exprIsTrivial OutExpr
rhs = Bool
True
| Bool
otherwise
= case OccInfo
occ_info of
OneOcc { occ_in_lam :: OccInfo -> InsideLam
occ_in_lam = InsideLam
in_lam, occ_int_cxt :: OccInfo -> InterestingCxt
occ_int_cxt = InterestingCxt
int_cxt }
-> DynFlags -> Unfolding -> Bool
smallEnoughToInline DynFlags
dflags Unfolding
unfolding
Bool -> Bool -> Bool
&& (InsideLam
in_lam InsideLam -> InsideLam -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq InsideLam
== InsideLam
NotInsideLam Bool -> Bool -> Bool
||
(Unfolding -> Bool
isCheapUnfolding Unfolding
unfolding Bool -> Bool -> Bool
&& InterestingCxt
int_cxt InterestingCxt -> InterestingCxt -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq InterestingCxt
== InterestingCxt
IsInteresting))
OccInfo
IAmDead -> Bool
True
OccInfo
_ -> Bool
False
where
unfolding :: Unfolding
unfolding = Id -> Unfolding
idUnfolding Id
bndr
dflags :: DynFlags
dflags = StaticEnv -> DynFlags
seDynFlags StaticEnv
env
active :: Bool
active = CompilerPhase -> Activation -> Bool
isActive (SimplMode -> CompilerPhase
sm_phase (StaticEnv -> SimplMode
getMode StaticEnv
env)) (Id -> Activation
idInlineActivation Id
bndr)
mkLam :: SimplEnv -> [OutBndr] -> OutExpr -> SimplCont -> SimplM OutExpr
mkLam :: StaticEnv -> [Id] -> OutExpr -> SimplCont -> SimplM OutExpr
mkLam StaticEnv
_env [] OutExpr
body SimplCont
_cont
= OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return OutExpr
body
mkLam StaticEnv
env [Id]
bndrs OutExpr
body SimplCont
cont
= do { DynFlags
dflags <- SimplM DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
External instance of the constraint type HasDynFlags SimplM
getDynFlags
; DynFlags -> [Id] -> OutExpr -> SimplM OutExpr
mkLam' DynFlags
dflags [Id]
bndrs OutExpr
body }
where
mkLam' :: DynFlags -> [OutBndr] -> OutExpr -> SimplM OutExpr
mkLam' :: DynFlags -> [Id] -> OutExpr -> SimplM OutExpr
mkLam' DynFlags
dflags [Id]
bndrs (Cast OutExpr
body OutCoercion
co)
| Bool -> Bool
not ((Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
any Id -> Bool
bad [Id]
bndrs)
= do { OutExpr
lam <- DynFlags -> [Id] -> OutExpr -> SimplM OutExpr
mkLam' DynFlags
dflags [Id]
bndrs OutExpr
body
; OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (OutExpr -> OutCoercion -> OutExpr
mkCast OutExpr
lam (Role -> [Id] -> OutCoercion -> OutCoercion
mkPiCos Role
Representational [Id]
bndrs OutCoercion
co)) }
where
co_vars :: TyCoVarSet
co_vars = OutCoercion -> TyCoVarSet
tyCoVarsOfCo OutCoercion
co
bad :: Id -> Bool
bad Id
bndr = Id -> Bool
isCoVar Id
bndr Bool -> Bool -> Bool
&& Id
bndr Id -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
co_vars
mkLam' DynFlags
dflags [Id]
bndrs body :: OutExpr
body@(Lam {})
= DynFlags -> [Id] -> OutExpr -> SimplM OutExpr
mkLam' DynFlags
dflags ([Id]
bndrs [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
bndrs1) OutExpr
body1
where
([Id]
bndrs1, OutExpr
body1) = OutExpr -> ([Id], OutExpr)
forall b. Expr b -> ([b], Expr b)
collectBinders OutExpr
body
mkLam' DynFlags
dflags [Id]
bndrs (Tick Tickish Id
t OutExpr
expr)
| Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable Tickish Id
t
= Tickish Id -> OutExpr -> OutExpr
mkTick Tickish Id
t (OutExpr -> OutExpr) -> SimplM OutExpr -> SimplM OutExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
External instance of the constraint type Functor SimplM
<$> DynFlags -> [Id] -> OutExpr -> SimplM OutExpr
mkLam' DynFlags
dflags [Id]
bndrs OutExpr
expr
mkLam' DynFlags
dflags [Id]
bndrs OutExpr
body
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_DoEtaReduction DynFlags
dflags
, Just OutExpr
etad_lam <- [Id] -> OutExpr -> Maybe OutExpr
tryEtaReduce [Id]
bndrs OutExpr
body
= do { Tick -> SimplM ()
tick (Id -> Tick
EtaReduction ([Id] -> Id
forall a. [a] -> a
head [Id]
bndrs))
; OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return OutExpr
etad_lam }
| Bool -> Bool
not (SimplCont -> Bool
contIsRhs SimplCont
cont)
, SimplMode -> Bool
sm_eta_expand (StaticEnv -> SimplMode
getMode StaticEnv
env)
, (Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
any Id -> Bool
isRuntimeVar [Id]
bndrs
, let body_arity :: Arity
body_arity = DynFlags -> OutExpr -> Arity
exprEtaExpandArity DynFlags
dflags OutExpr
body
, Arity
body_arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
> Arity
0
= do { Tick -> SimplM ()
tick (Id -> Tick
EtaExpansion ([Id] -> Id
forall a. [a] -> a
head [Id]
bndrs))
; let res :: OutExpr
res = [Id] -> OutExpr -> OutExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
bndrs (Arity -> OutExpr -> OutExpr
etaExpand Arity
body_arity OutExpr
body)
; String -> SDoc -> SimplM ()
traceSmpl String
"eta expand" ([SDoc] -> SDoc
vcat [String -> SDoc
text String
"before" SDoc -> SDoc -> SDoc
<+> OutExpr -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall b. OutputableBndr b => Outputable (Expr b)
External instance of the constraint type OutputableBndr Id
ppr ([Id] -> OutExpr -> OutExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
bndrs OutExpr
body)
, String -> SDoc
text String
"after" SDoc -> SDoc -> SDoc
<+> OutExpr -> SDoc
forall a. Outputable a => a -> SDoc
External instance of the constraint type forall b. OutputableBndr b => Outputable (Expr b)
External instance of the constraint type OutputableBndr Id
ppr OutExpr
res])
; OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return OutExpr
res }
| Bool
otherwise
= OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ([Id] -> OutExpr -> OutExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
bndrs OutExpr
body)
tryEtaExpandRhs :: SimplMode -> OutId -> OutExpr
-> SimplM (Arity, Bool, OutExpr)
tryEtaExpandRhs :: SimplMode -> Id -> OutExpr -> SimplM (Arity, Bool, OutExpr)
tryEtaExpandRhs SimplMode
mode Id
bndr OutExpr
rhs
| Just Arity
join_arity <- Id -> Maybe Arity
isJoinId_maybe Id
bndr
= do { let ([Id]
join_bndrs, OutExpr
join_body) = Arity -> OutExpr -> ([Id], OutExpr)
forall b. Arity -> Expr b -> ([b], Expr b)
collectNBinders Arity
join_arity OutExpr
rhs
; (Arity, Bool, OutExpr) -> SimplM (Arity, Bool, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ((Id -> Bool) -> [Id] -> Arity
forall a. (a -> Bool) -> [a] -> Arity
count Id -> Bool
isId [Id]
join_bndrs, OutExpr -> Bool
exprIsDeadEnd OutExpr
join_body, OutExpr
rhs) }
| Bool
otherwise
= do { (Arity
new_arity, Bool
is_bot, OutExpr
new_rhs) <- SimplM (Arity, Bool, OutExpr)
try_expand
; WARN( new_arity < old_id_arity,
(text "Arity decrease:" <+> (ppr bndr <+> ppr old_id_arity
<+> ppr old_arity <+> ppr new_arity) $$ ppr new_rhs) )
(Arity, Bool, OutExpr) -> SimplM (Arity, Bool, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Arity
new_arity, Bool
is_bot, OutExpr
new_rhs) }
where
try_expand :: SimplM (Arity, Bool, OutExpr)
try_expand
| OutExpr -> Bool
exprIsTrivial OutExpr
rhs
= (Arity, Bool, OutExpr) -> SimplM (Arity, Bool, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (OutExpr -> Arity
exprArity OutExpr
rhs, Bool
False, OutExpr
rhs)
| SimplMode -> Bool
sm_eta_expand SimplMode
mode
, Arity
new_arity Arity -> Arity -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Arity
> Arity
old_arity
= do { Tick -> SimplM ()
tick (Id -> Tick
EtaExpansion Id
bndr)
; (Arity, Bool, OutExpr) -> SimplM (Arity, Bool, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Arity
new_arity, Bool
is_bot, Arity -> OutExpr -> OutExpr
etaExpand Arity
new_arity OutExpr
rhs) }
| Bool
otherwise
= (Arity, Bool, OutExpr) -> SimplM (Arity, Bool, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Arity
old_arity, Bool
is_bot Bool -> Bool -> Bool
&& Arity
new_arity Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Arity
== Arity
old_arity, OutExpr
rhs)
dflags :: DynFlags
dflags = SimplMode -> DynFlags
sm_dflags SimplMode
mode
old_arity :: Arity
old_arity = OutExpr -> Arity
exprArity OutExpr
rhs
old_id_arity :: Arity
old_id_arity = Id -> Arity
idArity Id
bndr
(Arity
new_arity1, Bool
is_bot) = DynFlags -> Id -> OutExpr -> Arity -> (Arity, Bool)
findRhsArity DynFlags
dflags Id
bndr OutExpr
rhs Arity
old_arity
new_arity2 :: Arity
new_arity2 = Id -> Arity
idCallArity Id
bndr
new_arity :: Arity
new_arity = Arity -> Arity -> Arity
forall a. Ord a => a -> a -> a
External instance of the constraint type Ord Arity
max Arity
new_arity1 Arity
new_arity2
abstractFloats :: DynFlags -> TopLevelFlag -> [OutTyVar] -> SimplFloats
-> OutExpr -> SimplM ([OutBind], OutExpr)
abstractFloats :: DynFlags
-> TopLevelFlag
-> [Id]
-> SimplFloats
-> OutExpr
-> SimplM ([Bind Id], OutExpr)
abstractFloats DynFlags
dflags TopLevelFlag
top_lvl [Id]
main_tvs SimplFloats
floats OutExpr
body
= ASSERT( notNull body_floats )
ASSERT( isNilOL (sfJoinFloats floats) )
do { (Subst
subst, [Bind Id]
float_binds) <- (Subst -> Bind Id -> SimplM (Subst, Bind Id))
-> Subst -> [Bind Id] -> SimplM (Subst, [Bind Id])
forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
External instance of the constraint type Monad SimplM
mapAccumLM Subst -> Bind Id -> SimplM (Subst, Bind Id)
abstract Subst
empty_subst [Bind Id]
body_floats
; ([Bind Id], OutExpr) -> SimplM ([Bind Id], OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ([Bind Id]
float_binds, SDoc -> Subst -> OutExpr -> OutExpr
GHC.Core.Subst.substExpr (String -> SDoc
text String
"abstract_floats1") Subst
subst OutExpr
body) }
where
is_top_lvl :: Bool
is_top_lvl = TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl
main_tv_set :: TyCoVarSet
main_tv_set = [Id] -> TyCoVarSet
mkVarSet [Id]
main_tvs
body_floats :: [Bind Id]
body_floats = LetFloats -> [Bind Id]
letFloatBinds (SimplFloats -> LetFloats
sfLetFloats SimplFloats
floats)
empty_subst :: Subst
empty_subst = InScopeSet -> Subst
GHC.Core.Subst.mkEmptySubst (SimplFloats -> InScopeSet
sfInScope SimplFloats
floats)
abstract :: GHC.Core.Subst.Subst -> OutBind -> SimplM (GHC.Core.Subst.Subst, OutBind)
abstract :: Subst -> Bind Id -> SimplM (Subst, Bind Id)
abstract Subst
subst (NonRec Id
id OutExpr
rhs)
= do { (Id
poly_id1, OutExpr
poly_app) <- [Id] -> Id -> SimplM (Id, OutExpr)
mk_poly1 [Id]
tvs_here Id
id
; let (Id
poly_id2, OutExpr
poly_rhs) = Id -> [Id] -> OutExpr -> (Id, OutExpr)
mk_poly2 Id
poly_id1 [Id]
tvs_here OutExpr
rhs'
subst' :: Subst
subst' = Subst -> Id -> OutExpr -> Subst
GHC.Core.Subst.extendIdSubst Subst
subst Id
id OutExpr
poly_app
; (Subst, Bind Id) -> SimplM (Subst, Bind Id)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Subst
subst', Id -> OutExpr -> Bind Id
forall b. b -> Expr b -> Bind b
NonRec Id
poly_id2 OutExpr
poly_rhs) }
where
rhs' :: OutExpr
rhs' = SDoc -> Subst -> OutExpr -> OutExpr
GHC.Core.Subst.substExpr (String -> SDoc
text String
"abstract_floats2") Subst
subst OutExpr
rhs
tvs_here :: [Id]
tvs_here = [Id] -> [Id]
scopedSort ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$
(Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filter (Id -> TyCoVarSet -> Bool
`elemVarSet` TyCoVarSet
main_tv_set) ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$
[Id] -> [Id]
closeOverKindsList ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$
(Id -> Bool) -> OutExpr -> [Id]
exprSomeFreeVarsList Id -> Bool
isTyVar OutExpr
rhs'
abstract Subst
subst (Rec [(Id, OutExpr)]
prs)
= do { ([Id]
poly_ids, [OutExpr]
poly_apps) <- (Id -> SimplM (Id, OutExpr)) -> [Id] -> SimplM ([Id], [OutExpr])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
External instance of the constraint type Applicative SimplM
mapAndUnzipM ([Id] -> Id -> SimplM (Id, OutExpr)
mk_poly1 [Id]
tvs_here) [Id]
ids
; let subst' :: Subst
subst' = Subst -> [(Id, OutExpr)] -> Subst
GHC.Core.Subst.extendSubstList Subst
subst ([Id]
ids [Id] -> [OutExpr] -> [(Id, OutExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [OutExpr]
poly_apps)
poly_pairs :: [(Id, OutExpr)]
poly_pairs = [ Id -> [Id] -> OutExpr -> (Id, OutExpr)
mk_poly2 Id
poly_id [Id]
tvs_here OutExpr
rhs'
| (Id
poly_id, OutExpr
rhs) <- [Id]
poly_ids [Id] -> [OutExpr] -> [(Id, OutExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [OutExpr]
rhss
, let rhs' :: OutExpr
rhs' = SDoc -> Subst -> OutExpr -> OutExpr
GHC.Core.Subst.substExpr (String -> SDoc
text String
"abstract_floats")
Subst
subst' OutExpr
rhs ]
; (Subst, Bind Id) -> SimplM (Subst, Bind Id)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Subst
subst', [(Id, OutExpr)] -> Bind Id
forall b. [(b, Expr b)] -> Bind b
Rec [(Id, OutExpr)]
poly_pairs) }
where
([Id]
ids,[OutExpr]
rhss) = [(Id, OutExpr)] -> ([Id], [OutExpr])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, OutExpr)]
prs
tvs_here :: [Id]
tvs_here = [Id] -> [Id]
scopedSort [Id]
main_tvs
mk_poly1 :: [TyVar] -> Id -> SimplM (Id, CoreExpr)
mk_poly1 :: [Id] -> Id -> SimplM (Id, OutExpr)
mk_poly1 [Id]
tvs_here Id
var
= do { Unique
uniq <- SimplM Unique
forall (m :: * -> *). MonadUnique m => m Unique
External instance of the constraint type MonadUnique SimplM
getUniqueM
; let poly_name :: Name
poly_name = Name -> Unique -> Name
setNameUnique (Id -> Name
idName Id
var) Unique
uniq
poly_ty :: OutType
poly_ty = [Id] -> OutType -> OutType
mkInfForAllTys [Id]
tvs_here (Id -> OutType
idType Id
var)
poly_id :: Id
poly_id = Id -> [Id] -> Id -> Id
transferPolyIdInfo Id
var [Id]
tvs_here (Id -> Id) -> Id -> Id
forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => Name -> OutType -> Id
Name -> OutType -> Id
External instance of the constraint type HasDebugCallStack
mkLocalId Name
poly_name OutType
poly_ty
; (Id, OutExpr) -> SimplM (Id, OutExpr)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (Id
poly_id, OutExpr -> [OutType] -> OutExpr
forall b. Expr b -> [OutType] -> Expr b
mkTyApps (Id -> OutExpr
forall b. Id -> Expr b
Var Id
poly_id) ([Id] -> [OutType]
mkTyVarTys [Id]
tvs_here)) }
mk_poly2 :: Id -> [TyVar] -> CoreExpr -> (Id, CoreExpr)
mk_poly2 :: Id -> [Id] -> OutExpr -> (Id, OutExpr)
mk_poly2 Id
poly_id [Id]
tvs_here OutExpr
rhs
= (Id
poly_id Id -> Unfolding -> Id
`setIdUnfolding` Unfolding
unf, OutExpr
poly_rhs)
where
poly_rhs :: OutExpr
poly_rhs = [Id] -> OutExpr -> OutExpr
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
tvs_here OutExpr
rhs
unf :: Unfolding
unf = DynFlags -> UnfoldingSource -> Bool -> Bool -> OutExpr -> Unfolding
mkUnfolding DynFlags
dflags UnfoldingSource
InlineRhs Bool
is_top_lvl Bool
False OutExpr
poly_rhs
prepareAlts :: OutExpr -> OutId -> [InAlt] -> SimplM ([AltCon], [InAlt])
prepareAlts :: OutExpr -> Id -> [InAlt] -> SimplM ([AltCon], [InAlt])
prepareAlts OutExpr
scrut Id
case_bndr' [InAlt]
alts
| Just (TyCon
tc, [OutType]
tys) <- HasDebugCallStack => OutType -> Maybe (TyCon, [OutType])
OutType -> Maybe (TyCon, [OutType])
External instance of the constraint type HasDebugCallStack
splitTyConApp_maybe (Id -> OutType
varType Id
case_bndr')
= do { [Unique]
us <- SimplM [Unique]
forall (m :: * -> *). MonadUnique m => m [Unique]
External instance of the constraint type MonadUnique SimplM
getUniquesM
; let ([AltCon]
idcs1, [InAlt]
alts1) = TyCon -> [OutType] -> [AltCon] -> [InAlt] -> ([AltCon], [InAlt])
forall a.
TyCon
-> [OutType]
-> [AltCon]
-> [(AltCon, [Id], a)]
-> ([AltCon], [(AltCon, [Id], a)])
filterAlts TyCon
tc [OutType]
tys [AltCon]
imposs_cons [InAlt]
alts
(Bool
yes2, [InAlt]
alts2) = [Unique]
-> TyCon -> [OutType] -> [AltCon] -> [InAlt] -> (Bool, [InAlt])
refineDefaultAlt [Unique]
us TyCon
tc [OutType]
tys [AltCon]
idcs1 [InAlt]
alts1
(Bool
yes3, [AltCon]
idcs3, [InAlt]
alts3) = [AltCon] -> [InAlt] -> (Bool, [AltCon], [InAlt])
combineIdenticalAlts [AltCon]
idcs1 [InAlt]
alts2
; Bool -> SimplM () -> SimplM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
External instance of the constraint type Applicative SimplM
when Bool
yes2 (SimplM () -> SimplM ()) -> SimplM () -> SimplM ()
forall a b. (a -> b) -> a -> b
$ Tick -> SimplM ()
tick (Id -> Tick
FillInCaseDefault Id
case_bndr')
; Bool -> SimplM () -> SimplM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
External instance of the constraint type Applicative SimplM
when Bool
yes3 (SimplM () -> SimplM ()) -> SimplM () -> SimplM ()
forall a b. (a -> b) -> a -> b
$ Tick -> SimplM ()
tick (Id -> Tick
AltMerge Id
case_bndr')
; ([AltCon], [InAlt]) -> SimplM ([AltCon], [InAlt])
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ([AltCon]
idcs3, [InAlt]
alts3) }
| Bool
otherwise
= ([AltCon], [InAlt]) -> SimplM ([AltCon], [InAlt])
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ([], [InAlt]
alts)
where
imposs_cons :: [AltCon]
imposs_cons = case OutExpr
scrut of
Var Id
v -> Unfolding -> [AltCon]
otherCons (Id -> Unfolding
idUnfolding Id
v)
OutExpr
_ -> []
mkCase, mkCase1, mkCase2, mkCase3
:: DynFlags
-> OutExpr -> OutId
-> OutType -> [OutAlt]
-> SimplM OutExpr
mkCase :: DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase DynFlags
dflags OutExpr
scrut Id
outer_bndr OutType
alts_ty ((AltCon
DEFAULT, [Id]
_, OutExpr
deflt_rhs) : [InAlt]
outer_alts)
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_CaseMerge DynFlags
dflags
, ([Tickish Id]
ticks, Case (Var Id
inner_scrut_var) Id
inner_bndr OutType
_ [InAlt]
inner_alts)
<- (Tickish Id -> Bool) -> OutExpr -> ([Tickish Id], OutExpr)
forall b. (Tickish Id -> Bool) -> Expr b -> ([Tickish Id], Expr b)
stripTicksTop Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable OutExpr
deflt_rhs
, Id
inner_scrut_var Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Id
== Id
outer_bndr
= do { Tick -> SimplM ()
tick (Id -> Tick
CaseMerge Id
outer_bndr)
; let wrap_alt :: (a, t Id, OutExpr) -> (a, t Id, OutExpr)
wrap_alt (a
con, t Id
args, OutExpr
rhs) = ASSERT( outer_bndr `notElem` args )
(a
con, t Id
args, OutExpr -> OutExpr
wrap_rhs OutExpr
rhs)
wrap_rhs :: OutExpr -> OutExpr
wrap_rhs OutExpr
rhs = Bind Id -> OutExpr -> OutExpr
forall b. Bind b -> Expr b -> Expr b
Let (Id -> OutExpr -> Bind Id
forall b. b -> Expr b -> Bind b
NonRec Id
inner_bndr (Id -> OutExpr
forall b. Id -> Expr b
Var Id
outer_bndr)) OutExpr
rhs
wrapped_alts :: [InAlt]
wrapped_alts | Id -> Bool
isDeadBinder Id
inner_bndr = [InAlt]
inner_alts
| Bool
otherwise = (InAlt -> InAlt) -> [InAlt] -> [InAlt]
forall a b. (a -> b) -> [a] -> [b]
map InAlt -> InAlt
forall {t :: * -> *} {a}.
Foldable t =>
(a, t Id, OutExpr) -> (a, t Id, OutExpr)
External instance of the constraint type Foldable []
wrap_alt [InAlt]
inner_alts
merged_alts :: [InAlt]
merged_alts = [InAlt] -> [InAlt] -> [InAlt]
forall a b.
[(AltCon, a, b)] -> [(AltCon, a, b)] -> [(AltCon, a, b)]
mergeAlts [InAlt]
outer_alts [InAlt]
wrapped_alts
; (OutExpr -> OutExpr) -> SimplM OutExpr -> SimplM OutExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
External instance of the constraint type Functor SimplM
fmap ([Tickish Id] -> OutExpr -> OutExpr
mkTicks [Tickish Id]
ticks) (SimplM OutExpr -> SimplM OutExpr)
-> SimplM OutExpr -> SimplM OutExpr
forall a b. (a -> b) -> a -> b
$
DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase1 DynFlags
dflags OutExpr
scrut Id
outer_bndr OutType
alts_ty [InAlt]
merged_alts
}
mkCase DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts = DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase1 DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts
mkCase1 :: DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase1 DynFlags
_dflags OutExpr
scrut Id
case_bndr OutType
_ alts :: [InAlt]
alts@((AltCon
_,[Id]
_,OutExpr
rhs1) : [InAlt]
_)
| (InAlt -> Bool) -> [InAlt] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all InAlt -> Bool
forall {b}. (AltCon, [Id], Expr b) -> Bool
identity_alt [InAlt]
alts
= do { Tick -> SimplM ()
tick (Id -> Tick
CaseIdentity Id
case_bndr)
; OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return ([Tickish Id] -> OutExpr -> OutExpr
mkTicks [Tickish Id]
ticks (OutExpr -> OutExpr) -> OutExpr -> OutExpr
forall a b. (a -> b) -> a -> b
$ OutExpr -> OutExpr -> OutExpr
forall {b} {b}. Expr b -> Expr b -> Expr b
re_cast OutExpr
scrut OutExpr
rhs1) }
where
ticks :: [Tickish Id]
ticks = (InAlt -> [Tickish Id]) -> [InAlt] -> [Tickish Id]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
External instance of the constraint type Foldable []
concatMap ((Tickish Id -> Bool) -> OutExpr -> [Tickish Id]
forall b. (Tickish Id -> Bool) -> Expr b -> [Tickish Id]
stripTicksT Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable (OutExpr -> [Tickish Id])
-> (InAlt -> OutExpr) -> InAlt -> [Tickish Id]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InAlt -> OutExpr
forall a b c. (a, b, c) -> c
thdOf3) ([InAlt] -> [InAlt]
forall a. [a] -> [a]
tail [InAlt]
alts)
identity_alt :: (AltCon, [Id], Expr b) -> Bool
identity_alt (AltCon
con, [Id]
args, Expr b
rhs) = Expr b -> AltCon -> [Id] -> Bool
forall {b}. Expr b -> AltCon -> [Id] -> Bool
check_eq Expr b
rhs AltCon
con [Id]
args
check_eq :: Expr b -> AltCon -> [Id] -> Bool
check_eq (Cast Expr b
rhs OutCoercion
co) AltCon
con [Id]
args
= Bool -> Bool
not ((Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
any (Id -> TyCoVarSet -> Bool
`elemVarSet` OutCoercion -> TyCoVarSet
tyCoVarsOfCo OutCoercion
co) [Id]
args) Bool -> Bool -> Bool
&& Expr b -> AltCon -> [Id] -> Bool
check_eq Expr b
rhs AltCon
con [Id]
args
check_eq (Tick Tickish Id
t Expr b
e) AltCon
alt [Id]
args
= Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable Tickish Id
t Bool -> Bool -> Bool
&& Expr b -> AltCon -> [Id] -> Bool
check_eq Expr b
e AltCon
alt [Id]
args
check_eq (Lit Literal
lit) (LitAlt Literal
lit') [Id]
_ = Literal
lit Literal -> Literal -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Literal
== Literal
lit'
check_eq (Var Id
v) AltCon
_ [Id]
_ | Id
v Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Id
== Id
case_bndr = Bool
True
check_eq (Var Id
v) (DataAlt DataCon
con) [Id]
args
| [OutType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
External instance of the constraint type Foldable []
null [OutType]
arg_tys, [Id] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
External instance of the constraint type Foldable []
null [Id]
args = Id
v Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Id
== DataCon -> Id
dataConWorkId DataCon
con
check_eq Expr b
rhs (DataAlt DataCon
con) [Id]
args = (Tickish Id -> Bool) -> Expr b -> Expr b -> Bool
forall b. (Tickish Id -> Bool) -> Expr b -> Expr b -> Bool
cheapEqExpr' Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable Expr b
rhs (Expr b -> Bool) -> Expr b -> Bool
forall a b. (a -> b) -> a -> b
$
DataCon -> [OutType] -> [Id] -> Expr b
forall b. DataCon -> [OutType] -> [Id] -> Expr b
mkConApp2 DataCon
con [OutType]
arg_tys [Id]
args
check_eq Expr b
_ AltCon
_ [Id]
_ = Bool
False
arg_tys :: [OutType]
arg_tys = OutType -> [OutType]
tyConAppArgs (Id -> OutType
idType Id
case_bndr)
re_cast :: Expr b -> Expr b -> Expr b
re_cast Expr b
scrut (Cast Expr b
rhs OutCoercion
co) = Expr b -> OutCoercion -> Expr b
forall b. Expr b -> OutCoercion -> Expr b
Cast (Expr b -> Expr b -> Expr b
re_cast Expr b
scrut Expr b
rhs) OutCoercion
co
re_cast Expr b
scrut Expr b
_ = Expr b
scrut
mkCase1 DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts = DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase2 DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts
mkCase2 :: DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase2 DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts
|
case [InAlt]
alts of
[(AltCon
DEFAULT,[Id]
_,OutExpr
_)] -> Bool
False
[InAlt]
_ -> Bool
True
, GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_CaseFolding DynFlags
dflags
, Just (OutExpr
scrut', AltCon -> Maybe AltCon
tx_con, Id -> OutExpr
mk_orig) <- Platform
-> OutExpr
-> Maybe (OutExpr, AltCon -> Maybe AltCon, Id -> OutExpr)
caseRules (DynFlags -> Platform
targetPlatform DynFlags
dflags) OutExpr
scrut
= do { Id
bndr' <- FastString -> OutType -> SimplM Id
newId (String -> FastString
fsLit String
"lwild") (OutExpr -> OutType
exprType OutExpr
scrut')
; [InAlt]
alts' <- (InAlt -> SimplM (Maybe InAlt)) -> [InAlt] -> SimplM [InAlt]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
External instance of the constraint type Applicative SimplM
mapMaybeM ((AltCon -> Maybe AltCon)
-> (Id -> OutExpr) -> Id -> InAlt -> SimplM (Maybe InAlt)
tx_alt AltCon -> Maybe AltCon
tx_con Id -> OutExpr
mk_orig Id
bndr') [InAlt]
alts
; DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase3 DynFlags
dflags OutExpr
scrut' Id
bndr' OutType
alts_ty ([InAlt] -> SimplM OutExpr) -> [InAlt] -> SimplM OutExpr
forall a b. (a -> b) -> a -> b
$
[InAlt] -> [InAlt]
add_default ([InAlt] -> [InAlt]
re_sort [InAlt]
alts')
}
| Bool
otherwise
= DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase3 DynFlags
dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts
where
tx_alt :: (AltCon -> Maybe AltCon) -> (Id -> CoreExpr) -> Id
-> CoreAlt -> SimplM (Maybe CoreAlt)
tx_alt :: (AltCon -> Maybe AltCon)
-> (Id -> OutExpr) -> Id -> InAlt -> SimplM (Maybe InAlt)
tx_alt AltCon -> Maybe AltCon
tx_con Id -> OutExpr
mk_orig Id
new_bndr (AltCon
con, [Id]
bs, OutExpr
rhs)
= case AltCon -> Maybe AltCon
tx_con AltCon
con of
Maybe AltCon
Nothing -> Maybe InAlt -> SimplM (Maybe InAlt)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return Maybe InAlt
forall a. Maybe a
Nothing
Just AltCon
con' -> do { [Id]
bs' <- Id -> AltCon -> SimplM [Id]
forall {m :: * -> *}. MonadUnique m => Id -> AltCon -> m [Id]
External instance of the constraint type MonadUnique SimplM
mk_new_bndrs Id
new_bndr AltCon
con'
; Maybe InAlt -> SimplM (Maybe InAlt)
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (InAlt -> Maybe InAlt
forall a. a -> Maybe a
Just (AltCon
con', [Id]
bs', OutExpr
rhs')) }
where
rhs' :: OutExpr
rhs' | Id -> Bool
isDeadBinder Id
bndr = OutExpr
rhs
| Bool
otherwise = Id -> OutExpr -> OutExpr -> OutExpr
bindNonRec Id
bndr OutExpr
orig_val OutExpr
rhs
orig_val :: OutExpr
orig_val = case AltCon
con of
AltCon
DEFAULT -> Id -> OutExpr
mk_orig Id
new_bndr
LitAlt Literal
l -> Literal -> OutExpr
forall b. Literal -> Expr b
Lit Literal
l
DataAlt DataCon
dc -> DataCon -> [OutType] -> [Id] -> OutExpr
forall b. DataCon -> [OutType] -> [Id] -> Expr b
mkConApp2 DataCon
dc (OutType -> [OutType]
tyConAppArgs (Id -> OutType
idType Id
bndr)) [Id]
bs
mk_new_bndrs :: Id -> AltCon -> m [Id]
mk_new_bndrs Id
new_bndr (DataAlt DataCon
dc)
| Bool -> Bool
not (DataCon -> Bool
isNullaryRepDataCon DataCon
dc)
=
do { [Unique]
us <- m [Unique]
forall (m :: * -> *). MonadUnique m => m [Unique]
Evidence bound by a type signature of the constraint type MonadUnique m
getUniquesM
; let ([Id]
ex_tvs, [Id]
arg_ids) = [Unique] -> DataCon -> [OutType] -> ([Id], [Id])
dataConRepInstPat [Unique]
us DataCon
dc
(OutType -> [OutType]
tyConAppArgs (Id -> OutType
idType Id
new_bndr))
; [Id] -> m [Id]
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type forall (m :: * -> *). MonadUnique m => Monad m
Evidence bound by a type signature of the constraint type MonadUnique m
return ([Id]
ex_tvs [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
arg_ids) }
mk_new_bndrs Id
_ AltCon
_ = [Id] -> m [Id]
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type forall (m :: * -> *). MonadUnique m => Monad m
Evidence bound by a type signature of the constraint type MonadUnique m
return []
re_sort :: [CoreAlt] -> [CoreAlt]
re_sort :: [InAlt] -> [InAlt]
re_sort [InAlt]
alts = (InAlt -> InAlt -> Ordering) -> [InAlt] -> [InAlt]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy InAlt -> InAlt -> Ordering
forall a b. (AltCon, a, b) -> (AltCon, a, b) -> Ordering
cmpAlt [InAlt]
alts
add_default :: [CoreAlt] -> [CoreAlt]
add_default :: [InAlt] -> [InAlt]
add_default ((LitAlt {}, [Id]
bs, OutExpr
rhs) : [InAlt]
alts) = (AltCon
DEFAULT, [Id]
bs, OutExpr
rhs) InAlt -> [InAlt] -> [InAlt]
forall a. a -> [a] -> [a]
: [InAlt]
alts
add_default [InAlt]
alts = [InAlt]
alts
mkCase3 :: DynFlags -> OutExpr -> Id -> OutType -> [InAlt] -> SimplM OutExpr
mkCase3 DynFlags
_dflags OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts
= OutExpr -> SimplM OutExpr
forall (m :: * -> *) a. Monad m => a -> m a
External instance of the constraint type Monad SimplM
return (OutExpr -> Id -> OutType -> [InAlt] -> OutExpr
forall b. Expr b -> b -> OutType -> [Alt b] -> Expr b
Case OutExpr
scrut Id
bndr OutType
alts_ty [InAlt]
alts)
isExitJoinId :: Var -> Bool
isExitJoinId :: Id -> Bool
isExitJoinId Id
id
= Id -> Bool
isJoinId Id
id
Bool -> Bool -> Bool
&& OccInfo -> Bool
isOneOcc (Id -> OccInfo
idOccInfo Id
id)
Bool -> Bool -> Bool
&& OccInfo -> InsideLam
occ_in_lam (Id -> OccInfo
idOccInfo Id
id) InsideLam -> InsideLam -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq InsideLam
== InsideLam
IsInsideLam