{-# LANGUAGE CPP #-}
module GHC.Core.Opt.DmdAnal ( dmdAnalProgram ) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Driver.Session
import GHC.Core.Opt.WorkWrap.Utils
import GHC.Types.Demand
import GHC.Core
import GHC.Core.Multiplicity ( scaledThing )
import GHC.Core.Seq ( seqBinds )
import GHC.Utils.Outputable
import GHC.Types.Var.Env
import GHC.Types.Var.Set
import GHC.Types.Basic
import Data.List ( mapAccumL )
import GHC.Core.DataCon
import GHC.Types.ForeignCall ( isSafeForeignCall )
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Core.Utils
import GHC.Core.TyCon
import GHC.Core.Type
import GHC.Core.FVs ( exprFreeIds, ruleRhsFreeIds )
import GHC.Core.Coercion ( Coercion, coVarsOfCo )
import GHC.Core.FamInstEnv
import GHC.Utils.Misc
import GHC.Data.Maybe ( isJust )
import GHC.Builtin.PrimOps
import GHC.Builtin.Types.Prim ( realWorldStatePrimTy )
import GHC.Utils.Error ( dumpIfSet_dyn, DumpFormat (..) )
import GHC.Types.Unique.Set
dmdAnalProgram :: DynFlags -> FamInstEnvs -> CoreProgram -> IO CoreProgram
dmdAnalProgram :: DynFlags -> FamInstEnvs -> CoreProgram -> IO CoreProgram
dmdAnalProgram DynFlags
dflags FamInstEnvs
fam_envs CoreProgram
binds = do
let env :: AnalEnv
env = DynFlags -> FamInstEnvs -> AnalEnv
emptyAnalEnv DynFlags
dflags FamInstEnvs
fam_envs
let binds_plus_dmds :: CoreProgram
binds_plus_dmds = (AnalEnv, CoreProgram) -> CoreProgram
forall a b. (a, b) -> b
snd ((AnalEnv, CoreProgram) -> CoreProgram)
-> (AnalEnv, CoreProgram) -> CoreProgram
forall a b. (a -> b) -> a -> b
$ (AnalEnv -> CoreBind -> (AnalEnv, CoreBind))
-> AnalEnv -> CoreProgram -> (AnalEnv, CoreProgram)
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL AnalEnv -> CoreBind -> (AnalEnv, CoreBind)
dmdAnalTopBind AnalEnv
env CoreProgram
binds
DynFlags -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
dumpIfSet_dyn DynFlags
dflags DumpFlag
Opt_D_dump_str_signatures String
"Strictness signatures" DumpFormat
FormatText (SDoc -> IO ()) -> SDoc -> IO ()
forall a b. (a -> b) -> a -> b
$
(IdInfo -> SDoc) -> CoreProgram -> SDoc
dumpIdInfoOfProgram (StrictSig -> SDoc
pprIfaceStrictSig (StrictSig -> SDoc) -> (IdInfo -> StrictSig) -> IdInfo -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IdInfo -> StrictSig
strictnessInfo) CoreProgram
binds_plus_dmds
CoreProgram -> ()
seqBinds CoreProgram
binds_plus_dmds () -> IO CoreProgram -> IO CoreProgram
`seq` CoreProgram -> IO CoreProgram
forall (m :: * -> *) a. Monad m => a -> m a
return CoreProgram
binds_plus_dmds
dmdAnalTopBind :: AnalEnv
-> CoreBind
-> (AnalEnv, CoreBind)
dmdAnalTopBind :: AnalEnv -> CoreBind -> (AnalEnv, CoreBind)
dmdAnalTopBind AnalEnv
env (NonRec Var
id CoreExpr
rhs)
= ( TopLevelFlag -> AnalEnv -> Var -> StrictSig -> AnalEnv
extendAnalEnv TopLevelFlag
TopLevel AnalEnv
env Var
id StrictSig
sig
, Var -> CoreExpr -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec (Var -> StrictSig -> Var
setIdStrictness Var
id StrictSig
sig) CoreExpr
rhs')
where
( DmdEnv
_, StrictSig
sig, CoreExpr
rhs') = Maybe [Var]
-> AnalEnv
-> CleanDemand
-> Var
-> CoreExpr
-> (DmdEnv, StrictSig, CoreExpr)
dmdAnalRhsLetDown Maybe [Var]
forall a. Maybe a
Nothing AnalEnv
env CleanDemand
cleanEvalDmd Var
id CoreExpr
rhs
dmdAnalTopBind AnalEnv
env (Rec [(Var, CoreExpr)]
pairs)
= (AnalEnv
env', [(Var, CoreExpr)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Var, CoreExpr)]
pairs')
where
(AnalEnv
env', DmdEnv
_, [(Var, CoreExpr)]
pairs') = TopLevelFlag
-> AnalEnv
-> CleanDemand
-> [(Var, CoreExpr)]
-> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
dmdFix TopLevelFlag
TopLevel AnalEnv
env CleanDemand
cleanEvalDmd [(Var, CoreExpr)]
pairs
dmdTransformThunkDmd :: CoreExpr -> Demand -> Demand
dmdTransformThunkDmd :: CoreExpr -> Demand -> Demand
dmdTransformThunkDmd CoreExpr
e
| CoreExpr -> Bool
exprIsTrivial CoreExpr
e = Demand -> Demand
forall a. a -> a
id
| Bool
otherwise = Demand -> Demand
forall s u. JointDmd s (Use u) -> JointDmd s (Use u)
oneifyDmd
dmdAnalStar :: AnalEnv
-> Demand
-> CoreExpr
-> (BothDmdArg, CoreExpr)
dmdAnalStar :: AnalEnv -> Demand -> CoreExpr -> (BothDmdArg, CoreExpr)
dmdAnalStar AnalEnv
env Demand
dmd CoreExpr
e
| (DmdShell
dmd_shell, CleanDemand
cd) <- Demand -> (DmdShell, CleanDemand)
toCleanDmd Demand
dmd
, (DmdType
dmd_ty, CoreExpr
e') <- AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
cd CoreExpr
e
= ASSERT2( not (isUnliftedType (exprType e)) || exprOkForSpeculation e, ppr e )
(DmdShell -> DmdType -> BothDmdArg
postProcessDmdType DmdShell
dmd_shell DmdType
dmd_ty, CoreExpr
e')
dmdAnal, dmdAnal' :: AnalEnv
-> CleanDemand
-> CoreExpr -> (DmdType, CoreExpr)
dmdAnal :: AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
d CoreExpr
e =
AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal' AnalEnv
env CleanDemand
d CoreExpr
e
dmdAnal' :: AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal' AnalEnv
_ CleanDemand
_ (Lit Literal
lit) = (DmdType
nopDmdType, Literal -> CoreExpr
forall b. Literal -> Expr b
Lit Literal
lit)
dmdAnal' AnalEnv
_ CleanDemand
_ (Type Type
ty) = (DmdType
nopDmdType, Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty)
dmdAnal' AnalEnv
_ CleanDemand
_ (Coercion Coercion
co)
= (DmdEnv -> DmdType
unitDmdType (Coercion -> DmdEnv
coercionDmdEnv Coercion
co), Coercion -> CoreExpr
forall b. Coercion -> Expr b
Coercion Coercion
co)
dmdAnal' AnalEnv
env CleanDemand
dmd (Var Var
var)
= (AnalEnv -> Var -> CleanDemand -> DmdType
dmdTransform AnalEnv
env Var
var CleanDemand
dmd, Var -> CoreExpr
forall b. Var -> Expr b
Var Var
var)
dmdAnal' AnalEnv
env CleanDemand
dmd (Cast CoreExpr
e Coercion
co)
= (DmdType
dmd_ty DmdType -> BothDmdArg -> DmdType
`bothDmdType` DmdEnv -> BothDmdArg
mkBothDmdArg (Coercion -> DmdEnv
coercionDmdEnv Coercion
co), CoreExpr -> Coercion -> CoreExpr
forall b. Expr b -> Coercion -> Expr b
Cast CoreExpr
e' Coercion
co)
where
(DmdType
dmd_ty, CoreExpr
e') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
e
dmdAnal' AnalEnv
env CleanDemand
dmd (Tick Tickish Var
t CoreExpr
e)
= (DmdType
dmd_ty, Tickish Var -> CoreExpr -> CoreExpr
forall b. Tickish Var -> Expr b -> Expr b
Tick Tickish Var
t CoreExpr
e')
where
(DmdType
dmd_ty, CoreExpr
e') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
e
dmdAnal' AnalEnv
env CleanDemand
dmd (App CoreExpr
fun (Type Type
ty))
= (DmdType
fun_ty, CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun' (Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty))
where
(DmdType
fun_ty, CoreExpr
fun') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
fun
dmdAnal' AnalEnv
env CleanDemand
dmd (App CoreExpr
fun CoreExpr
arg)
=
let
call_dmd :: CleanDemand
call_dmd = CleanDemand -> CleanDemand
mkCallDmd CleanDemand
dmd
(DmdType
fun_ty, CoreExpr
fun') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
call_dmd CoreExpr
fun
(Demand
arg_dmd, DmdType
res_ty) = DmdType -> (Demand, DmdType)
splitDmdTy DmdType
fun_ty
(BothDmdArg
arg_ty, CoreExpr
arg') = AnalEnv -> Demand -> CoreExpr -> (BothDmdArg, CoreExpr)
dmdAnalStar AnalEnv
env (CoreExpr -> Demand -> Demand
dmdTransformThunkDmd CoreExpr
arg Demand
arg_dmd) CoreExpr
arg
in
(DmdType
res_ty DmdType -> BothDmdArg -> DmdType
`bothDmdType` BothDmdArg
arg_ty, CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App CoreExpr
fun' CoreExpr
arg')
dmdAnal' AnalEnv
env CleanDemand
dmd (Lam Var
var CoreExpr
body)
| Var -> Bool
isTyVar Var
var
= let
(DmdType
body_ty, CoreExpr
body') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
body
in
(DmdType
body_ty, Var -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Var
var CoreExpr
body')
| Bool
otherwise
= let (CleanDemand
body_dmd, DmdShell
defer_and_use) = CleanDemand -> (CleanDemand, DmdShell)
peelCallDmd CleanDemand
dmd
(DmdType
body_ty, CoreExpr
body') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
body_dmd CoreExpr
body
(DmdType
lam_ty, Var
var') = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Var)
annotateLamIdBndr AnalEnv
env Bool
notArgOfDfun DmdType
body_ty Var
var
in
(DmdShell -> DmdType -> DmdType
postProcessUnsat DmdShell
defer_and_use DmdType
lam_ty, Var -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam Var
var' CoreExpr
body')
dmdAnal' AnalEnv
env CleanDemand
dmd (Case CoreExpr
scrut Var
case_bndr Type
ty [(DataAlt DataCon
dc, [Var]
bndrs, CoreExpr
rhs)])
| let tycon :: TyCon
tycon = DataCon -> TyCon
dataConTyCon DataCon
dc
, Maybe DataCon -> Bool
forall a. Maybe a -> Bool
isJust (TyCon -> Maybe DataCon
isDataProductTyCon_maybe TyCon
tycon)
= let
(DmdType
rhs_ty, CoreExpr
rhs') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
rhs
(DmdType
alt_ty1, [Demand]
dmds) = AnalEnv -> DmdType -> [Var] -> (DmdType, [Demand])
findBndrsDmds AnalEnv
env DmdType
rhs_ty [Var]
bndrs
(DmdType
alt_ty2, Demand
case_bndr_dmd) = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
False DmdType
alt_ty1 Var
case_bndr
id_dmds :: [Demand]
id_dmds = Demand -> [Demand] -> [Demand]
addCaseBndrDmd Demand
case_bndr_dmd [Demand]
dmds
fam_envs :: FamInstEnvs
fam_envs = AnalEnv -> FamInstEnvs
ae_fam_envs AnalEnv
env
alt_ty3 :: DmdType
alt_ty3
| FamInstEnvs -> CoreExpr -> Bool
exprMayThrowPreciseException FamInstEnvs
fam_envs CoreExpr
scrut
= DmdType -> DmdType
deferAfterPreciseException DmdType
alt_ty2
| Bool
otherwise
= DmdType
alt_ty2
scrut_dmd :: CleanDemand
scrut_dmd = [Demand] -> CleanDemand
mkProdDmd [Demand]
id_dmds
(DmdType
scrut_ty, CoreExpr
scrut') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
scrut_dmd CoreExpr
scrut
res_ty :: DmdType
res_ty = DmdType
alt_ty3 DmdType -> BothDmdArg -> DmdType
`bothDmdType` DmdType -> BothDmdArg
toBothDmdArg DmdType
scrut_ty
case_bndr' :: Var
case_bndr' = Var -> Demand -> Var
setIdDemandInfo Var
case_bndr Demand
case_bndr_dmd
bndrs' :: [Var]
bndrs' = [Var] -> [Demand] -> [Var]
setBndrsDemandInfo [Var]
bndrs [Demand]
id_dmds
in
(DmdType
res_ty, CoreExpr -> Var -> Type -> [Alt Var] -> CoreExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CoreExpr
scrut' Var
case_bndr' Type
ty [(DataCon -> AltCon
DataAlt DataCon
dc, [Var]
bndrs', CoreExpr
rhs')])
dmdAnal' AnalEnv
env CleanDemand
dmd (Case CoreExpr
scrut Var
case_bndr Type
ty [Alt Var]
alts)
= let
([DmdType]
alt_tys, [Alt Var]
alts') = (Alt Var -> (DmdType, Alt Var))
-> [Alt Var] -> ([DmdType], [Alt Var])
forall a b c. (a -> (b, c)) -> [a] -> ([b], [c])
mapAndUnzip (AnalEnv -> CleanDemand -> Var -> Alt Var -> (DmdType, Alt Var)
dmdAnalAlt AnalEnv
env CleanDemand
dmd Var
case_bndr) [Alt Var]
alts
(DmdType
scrut_ty, CoreExpr
scrut') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
cleanEvalDmd CoreExpr
scrut
(DmdType
alt_ty, Var
case_bndr') = AnalEnv -> DmdType -> Var -> (DmdType, Var)
annotateBndr AnalEnv
env ((DmdType -> DmdType -> DmdType) -> DmdType -> [DmdType] -> DmdType
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr DmdType -> DmdType -> DmdType
lubDmdType DmdType
botDmdType [DmdType]
alt_tys) Var
case_bndr
fam_envs :: FamInstEnvs
fam_envs = AnalEnv -> FamInstEnvs
ae_fam_envs AnalEnv
env
alt_ty2 :: DmdType
alt_ty2
| FamInstEnvs -> CoreExpr -> Bool
exprMayThrowPreciseException FamInstEnvs
fam_envs CoreExpr
scrut
= DmdType -> DmdType
deferAfterPreciseException DmdType
alt_ty
| Bool
otherwise
= DmdType
alt_ty
res_ty :: DmdType
res_ty = DmdType
alt_ty2 DmdType -> BothDmdArg -> DmdType
`bothDmdType` DmdType -> BothDmdArg
toBothDmdArg DmdType
scrut_ty
in
(DmdType
res_ty, CoreExpr -> Var -> Type -> [Alt Var] -> CoreExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CoreExpr
scrut' Var
case_bndr' Type
ty [Alt Var]
alts')
dmdAnal' AnalEnv
env CleanDemand
dmd (Let (NonRec Var
id CoreExpr
rhs) CoreExpr
body)
| Var -> Bool
useLetUp Var
id
= (DmdType
final_ty, CoreBind -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let (Var -> CoreExpr -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Var
id' CoreExpr
rhs') CoreExpr
body')
where
(DmdType
body_ty, CoreExpr
body') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
body
(DmdType
body_ty', Demand
id_dmd) = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
notArgOfDfun DmdType
body_ty Var
id
id' :: Var
id' = Var -> Demand -> Var
setIdDemandInfo Var
id Demand
id_dmd
(BothDmdArg
rhs_ty, CoreExpr
rhs') = AnalEnv -> Demand -> CoreExpr -> (BothDmdArg, CoreExpr)
dmdAnalStar AnalEnv
env (CoreExpr -> Demand -> Demand
dmdTransformThunkDmd CoreExpr
rhs Demand
id_dmd) CoreExpr
rhs
final_ty :: DmdType
final_ty = DmdType
body_ty' DmdType -> BothDmdArg -> DmdType
`bothDmdType` BothDmdArg
rhs_ty
dmdAnal' AnalEnv
env CleanDemand
dmd (Let (NonRec Var
id CoreExpr
rhs) CoreExpr
body)
= (DmdType
body_ty2, CoreBind -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let (Var -> CoreExpr -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Var
id2 CoreExpr
rhs') CoreExpr
body')
where
(DmdEnv
lazy_fv, StrictSig
sig, CoreExpr
rhs') = Maybe [Var]
-> AnalEnv
-> CleanDemand
-> Var
-> CoreExpr
-> (DmdEnv, StrictSig, CoreExpr)
dmdAnalRhsLetDown Maybe [Var]
forall a. Maybe a
Nothing AnalEnv
env CleanDemand
dmd Var
id CoreExpr
rhs
id1 :: Var
id1 = Var -> StrictSig -> Var
setIdStrictness Var
id StrictSig
sig
env1 :: AnalEnv
env1 = TopLevelFlag -> AnalEnv -> Var -> StrictSig -> AnalEnv
extendAnalEnv TopLevelFlag
NotTopLevel AnalEnv
env Var
id StrictSig
sig
(DmdType
body_ty, CoreExpr
body') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env1 CleanDemand
dmd CoreExpr
body
(DmdType
body_ty1, Var
id2) = AnalEnv -> DmdType -> Var -> (DmdType, Var)
annotateBndr AnalEnv
env DmdType
body_ty Var
id1
body_ty2 :: DmdType
body_ty2 = DmdType -> DmdEnv -> DmdType
addLazyFVs DmdType
body_ty1 DmdEnv
lazy_fv
dmdAnal' AnalEnv
env CleanDemand
dmd (Let (Rec [(Var, CoreExpr)]
pairs) CoreExpr
body)
= let
(AnalEnv
env', DmdEnv
lazy_fv, [(Var, CoreExpr)]
pairs') = TopLevelFlag
-> AnalEnv
-> CleanDemand
-> [(Var, CoreExpr)]
-> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
dmdFix TopLevelFlag
NotTopLevel AnalEnv
env CleanDemand
dmd [(Var, CoreExpr)]
pairs
(DmdType
body_ty, CoreExpr
body') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env' CleanDemand
dmd CoreExpr
body
body_ty1 :: DmdType
body_ty1 = DmdType -> [Var] -> DmdType
deleteFVs DmdType
body_ty (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
pairs)
body_ty2 :: DmdType
body_ty2 = DmdType -> DmdEnv -> DmdType
addLazyFVs DmdType
body_ty1 DmdEnv
lazy_fv
in
DmdType
body_ty2 DmdType -> (DmdType, CoreExpr) -> (DmdType, CoreExpr)
`seq`
(DmdType
body_ty2, CoreBind -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let ([(Var, CoreExpr)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Var, CoreExpr)]
pairs') CoreExpr
body')
deleteFVs :: DmdType -> [Var] -> DmdType
deleteFVs :: DmdType -> [Var] -> DmdType
deleteFVs (DmdType DmdEnv
fvs [Demand]
dmds Divergence
res) [Var]
bndrs
= DmdEnv -> [Demand] -> Divergence -> DmdType
DmdType (DmdEnv -> [Var] -> DmdEnv
forall a. VarEnv a -> [Var] -> VarEnv a
delVarEnvList DmdEnv
fvs [Var]
bndrs) [Demand]
dmds Divergence
res
exprMayThrowPreciseException :: FamInstEnvs -> CoreExpr -> Bool
exprMayThrowPreciseException :: FamInstEnvs -> CoreExpr -> Bool
exprMayThrowPreciseException FamInstEnvs
envs CoreExpr
e
| Bool -> Bool
not (FamInstEnvs -> Type -> Bool
forcesRealWorld FamInstEnvs
envs (CoreExpr -> Type
exprType CoreExpr
e))
= Bool
False
| (Var Var
f, [CoreExpr]
_) <- CoreExpr -> (CoreExpr, [CoreExpr])
forall b. Expr b -> (Expr b, [Expr b])
collectArgs CoreExpr
e
, Just PrimOp
op <- Var -> Maybe PrimOp
isPrimOpId_maybe Var
f
, PrimOp
op PrimOp -> PrimOp -> Bool
forall a. Eq a => a -> a -> Bool
/= PrimOp
RaiseIOOp
= Bool
False
| (Var Var
f, [CoreExpr]
_) <- CoreExpr -> (CoreExpr, [CoreExpr])
forall b. Expr b -> (Expr b, [Expr b])
collectArgs CoreExpr
e
, Just ForeignCall
fcall <- Var -> Maybe ForeignCall
isFCallId_maybe Var
f
, Bool -> Bool
not (ForeignCall -> Bool
isSafeForeignCall ForeignCall
fcall)
= Bool
False
| Bool
otherwise
= Bool
True
forcesRealWorld :: FamInstEnvs -> Type -> Bool
forcesRealWorld :: FamInstEnvs -> Type -> Bool
forcesRealWorld FamInstEnvs
fam_envs Type
ty
| Type
ty Type -> Type -> Bool
`eqType` Type
realWorldStatePrimTy
= Bool
True
| Just DataConAppContext{ dcac_dc :: DataConAppContext -> DataCon
dcac_dc = DataCon
dc, dcac_arg_tys :: DataConAppContext -> [(Scaled Type, StrictnessMark)]
dcac_arg_tys = [(Scaled Type, StrictnessMark)]
field_tys }
<- FamInstEnvs -> Type -> Maybe DataConAppContext
deepSplitProductType_maybe FamInstEnvs
fam_envs Type
ty
, DataCon -> Bool
isUnboxedTupleCon DataCon
dc
= ((Scaled Type, StrictnessMark) -> Bool)
-> [(Scaled Type, StrictnessMark)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(Scaled Type
ty,StrictnessMark
_) -> Scaled Type -> Type
forall a. Scaled a -> a
scaledThing Scaled Type
ty Type -> Type -> Bool
`eqType` Type
realWorldStatePrimTy) [(Scaled Type, StrictnessMark)]
field_tys
| Bool
otherwise
= Bool
False
dmdAnalAlt :: AnalEnv -> CleanDemand -> Id -> Alt Var -> (DmdType, Alt Var)
dmdAnalAlt :: AnalEnv -> CleanDemand -> Var -> Alt Var -> (DmdType, Alt Var)
dmdAnalAlt AnalEnv
env CleanDemand
dmd Var
case_bndr (AltCon
con,[Var]
bndrs,CoreExpr
rhs)
| [Var] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Var]
bndrs
, (DmdType
rhs_ty, CoreExpr
rhs') <- AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
rhs
= (DmdType
rhs_ty, (AltCon
con, [], CoreExpr
rhs'))
| Bool
otherwise
, (DmdType
rhs_ty, CoreExpr
rhs') <- AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
dmd CoreExpr
rhs
, (DmdType
alt_ty, [Demand]
dmds) <- AnalEnv -> DmdType -> [Var] -> (DmdType, [Demand])
findBndrsDmds AnalEnv
env DmdType
rhs_ty [Var]
bndrs
, let case_bndr_dmd :: Demand
case_bndr_dmd = DmdType -> Var -> Demand
findIdDemand DmdType
alt_ty Var
case_bndr
id_dmds :: [Demand]
id_dmds = Demand -> [Demand] -> [Demand]
addCaseBndrDmd Demand
case_bndr_dmd [Demand]
dmds
= (DmdType
alt_ty, (AltCon
con, [Var] -> [Demand] -> [Var]
setBndrsDemandInfo [Var]
bndrs [Demand]
id_dmds, CoreExpr
rhs'))
dmdTransform :: AnalEnv
-> Id
-> CleanDemand
-> DmdType
dmdTransform :: AnalEnv -> Var -> CleanDemand -> DmdType
dmdTransform AnalEnv
env Var
var CleanDemand
dmd
| Var -> Bool
isDataConWorkId Var
var
= Arity -> CleanDemand -> DmdType
dmdTransformDataConSig (Var -> Arity
idArity Var
var) CleanDemand
dmd
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_DmdTxDictSel (AnalEnv -> DynFlags
ae_dflags AnalEnv
env),
Just Class
_ <- Var -> Maybe Class
isClassOpId_maybe Var
var
= StrictSig -> CleanDemand -> DmdType
dmdTransformDictSelSig (Var -> StrictSig
idStrictness Var
var) CleanDemand
dmd
| Var -> Bool
isGlobalId Var
var
, let res :: DmdType
res = StrictSig -> CleanDemand -> DmdType
dmdTransformSig (Var -> StrictSig
idStrictness Var
var) CleanDemand
dmd
=
DmdType
res
| Just (StrictSig
sig, TopLevelFlag
top_lvl) <- AnalEnv -> Var -> Maybe (StrictSig, TopLevelFlag)
lookupSigEnv AnalEnv
env Var
var
, let fn_ty :: DmdType
fn_ty = StrictSig -> CleanDemand -> DmdType
dmdTransformSig StrictSig
sig CleanDemand
dmd
=
if TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl
then DmdType
fn_ty
else DmdType -> Var -> Demand -> DmdType
addVarDmd DmdType
fn_ty Var
var (CleanDemand -> Demand
mkOnceUsedDmd CleanDemand
dmd)
| Bool
otherwise
=
DmdEnv -> DmdType
unitDmdType (Var -> Demand -> DmdEnv
forall a. Var -> a -> VarEnv a
unitVarEnv Var
var (CleanDemand -> Demand
mkOnceUsedDmd CleanDemand
dmd))
dmdAnalRhsLetDown
:: Maybe [Id]
-> AnalEnv -> CleanDemand
-> Id -> CoreExpr
-> (DmdEnv, StrictSig, CoreExpr)
dmdAnalRhsLetDown :: Maybe [Var]
-> AnalEnv
-> CleanDemand
-> Var
-> CoreExpr
-> (DmdEnv, StrictSig, CoreExpr)
dmdAnalRhsLetDown Maybe [Var]
rec_flag AnalEnv
env CleanDemand
let_dmd Var
id CoreExpr
rhs
= (DmdEnv
lazy_fv, StrictSig
sig, CoreExpr
rhs')
where
rhs_arity :: Arity
rhs_arity = Var -> Arity
idArity Var
id
rhs_dmd :: CleanDemand
rhs_dmd
| Var -> Bool
isJoinId Var
id
= Arity -> CleanDemand -> CleanDemand
mkCallDmds Arity
rhs_arity CleanDemand
let_dmd
| Bool
otherwise
= AnalEnv -> Arity -> CoreExpr -> CleanDemand
mkRhsDmd AnalEnv
env Arity
rhs_arity CoreExpr
rhs
(DmdType
rhs_dmd_ty, CoreExpr
rhs') = AnalEnv -> CleanDemand -> CoreExpr -> (DmdType, CoreExpr)
dmdAnal AnalEnv
env CleanDemand
rhs_dmd CoreExpr
rhs
DmdType DmdEnv
rhs_fv [Demand]
rhs_dmds Divergence
rhs_div = DmdType
rhs_dmd_ty
sig :: StrictSig
sig = Arity -> DmdType -> StrictSig
mkStrictSigForArity Arity
rhs_arity (DmdEnv -> [Demand] -> Divergence -> DmdType
DmdType DmdEnv
sig_fv [Demand]
rhs_dmds Divergence
rhs_div)
rhs_fv1 :: DmdEnv
rhs_fv1 = case Maybe [Var]
rec_flag of
Just [Var]
bs -> DmdEnv -> DmdEnv
reuseEnv (DmdEnv -> [Var] -> DmdEnv
forall a. VarEnv a -> [Var] -> VarEnv a
delVarEnvList DmdEnv
rhs_fv [Var]
bs)
Maybe [Var]
Nothing -> DmdEnv
rhs_fv
rhs_fv2 :: DmdEnv
rhs_fv2 = DmdEnv
rhs_fv1 DmdEnv -> IdSet -> DmdEnv
`keepAliveDmdEnv` IdSet
extra_fvs
(DmdEnv
lazy_fv, DmdEnv
sig_fv) = Bool -> DmdEnv -> (DmdEnv, DmdEnv)
splitFVs Bool
is_thunk DmdEnv
rhs_fv2
is_thunk :: Bool
is_thunk = Bool -> Bool
not (CoreExpr -> Bool
exprIsHNF CoreExpr
rhs) Bool -> Bool -> Bool
&& Bool -> Bool
not (Var -> Bool
isJoinId Var
id)
extra_fvs :: IdSet
extra_fvs = (CoreRule -> IdSet -> IdSet) -> IdSet -> [CoreRule] -> IdSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (IdSet -> IdSet -> IdSet
unionVarSet (IdSet -> IdSet -> IdSet)
-> (CoreRule -> IdSet) -> CoreRule -> IdSet -> IdSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreRule -> IdSet
ruleRhsFreeIds) IdSet
unf_fvs ([CoreRule] -> IdSet) -> [CoreRule] -> IdSet
forall a b. (a -> b) -> a -> b
$
Var -> [CoreRule]
idCoreRules Var
id
unf :: Unfolding
unf = Var -> Unfolding
realIdUnfolding Var
id
unf_fvs :: IdSet
unf_fvs | Unfolding -> Bool
isStableUnfolding Unfolding
unf
, Just CoreExpr
unf_body <- Unfolding -> Maybe CoreExpr
maybeUnfoldingTemplate Unfolding
unf
= CoreExpr -> IdSet
exprFreeIds CoreExpr
unf_body
| Bool
otherwise = IdSet
emptyVarSet
mkRhsDmd :: AnalEnv -> Arity -> CoreExpr -> CleanDemand
mkRhsDmd :: AnalEnv -> Arity -> CoreExpr -> CleanDemand
mkRhsDmd AnalEnv
_env Arity
rhs_arity CoreExpr
_rhs = Arity -> CleanDemand -> CleanDemand
mkCallDmds Arity
rhs_arity CleanDemand
cleanEvalDmd
useLetUp :: Var -> Bool
useLetUp :: Var -> Bool
useLetUp Var
f = Var -> Arity
idArity Var
f Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== Arity
0 Bool -> Bool -> Bool
&& Bool -> Bool
not (Var -> Bool
isJoinId Var
f)
dmdFix :: TopLevelFlag
-> AnalEnv
-> CleanDemand
-> [(Id,CoreExpr)]
-> (AnalEnv, DmdEnv, [(Id,CoreExpr)])
dmdFix :: TopLevelFlag
-> AnalEnv
-> CleanDemand
-> [(Var, CoreExpr)]
-> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
dmdFix TopLevelFlag
top_lvl AnalEnv
env CleanDemand
let_dmd [(Var, CoreExpr)]
orig_pairs
= Arity -> [(Var, CoreExpr)] -> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
loop Arity
1 [(Var, CoreExpr)]
initial_pairs
where
bndrs :: [Var]
bndrs = ((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
orig_pairs
initial_pairs :: [(Var, CoreExpr)]
initial_pairs | AnalEnv -> Bool
ae_virgin AnalEnv
env = [(Var -> StrictSig -> Var
setIdStrictness Var
id StrictSig
botSig, CoreExpr
rhs) | (Var
id, CoreExpr
rhs) <- [(Var, CoreExpr)]
orig_pairs ]
| Bool
otherwise = [(Var, CoreExpr)]
orig_pairs
abort :: (AnalEnv, DmdEnv, [(Id,CoreExpr)])
abort :: (AnalEnv, DmdEnv, [(Var, CoreExpr)])
abort = (AnalEnv
env, DmdEnv
lazy_fv', [(Var, CoreExpr)]
zapped_pairs)
where (DmdEnv
lazy_fv, [(Var, CoreExpr)]
pairs') = Bool -> [(Var, CoreExpr)] -> (DmdEnv, [(Var, CoreExpr)])
step Bool
True ([(Var, CoreExpr)] -> [(Var, CoreExpr)]
zapIdStrictness [(Var, CoreExpr)]
orig_pairs)
non_lazy_fvs :: DmdEnv
non_lazy_fvs = [DmdEnv] -> DmdEnv
forall a. [VarEnv a] -> VarEnv a
plusVarEnvList ([DmdEnv] -> DmdEnv) -> [DmdEnv] -> DmdEnv
forall a b. (a -> b) -> a -> b
$ ((Var, CoreExpr) -> DmdEnv) -> [(Var, CoreExpr)] -> [DmdEnv]
forall a b. (a -> b) -> [a] -> [b]
map (StrictSig -> DmdEnv
strictSigDmdEnv (StrictSig -> DmdEnv)
-> ((Var, CoreExpr) -> StrictSig) -> (Var, CoreExpr) -> DmdEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> StrictSig
idStrictness (Var -> StrictSig)
-> ((Var, CoreExpr) -> Var) -> (Var, CoreExpr) -> StrictSig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst) [(Var, CoreExpr)]
pairs'
lazy_fv' :: DmdEnv
lazy_fv' = DmdEnv
lazy_fv DmdEnv -> DmdEnv -> DmdEnv
forall a. VarEnv a -> VarEnv a -> VarEnv a
`plusVarEnv` (Demand -> Demand) -> DmdEnv -> DmdEnv
forall a b. (a -> b) -> VarEnv a -> VarEnv b
mapVarEnv (Demand -> Demand -> Demand
forall a b. a -> b -> a
const Demand
topDmd) DmdEnv
non_lazy_fvs
zapped_pairs :: [(Var, CoreExpr)]
zapped_pairs = [(Var, CoreExpr)] -> [(Var, CoreExpr)]
zapIdStrictness [(Var, CoreExpr)]
pairs'
loop :: Int -> [(Id,CoreExpr)] -> (AnalEnv, DmdEnv, [(Id,CoreExpr)])
loop :: Arity -> [(Var, CoreExpr)] -> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
loop Arity
n [(Var, CoreExpr)]
pairs =
Arity -> [(Var, CoreExpr)] -> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
loop' Arity
n [(Var, CoreExpr)]
pairs
loop' :: Arity -> [(Var, CoreExpr)] -> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
loop' Arity
n [(Var, CoreExpr)]
pairs
| Bool
found_fixpoint = (AnalEnv
final_anal_env, DmdEnv
lazy_fv, [(Var, CoreExpr)]
pairs')
| Arity
n Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== Arity
10 = (AnalEnv, DmdEnv, [(Var, CoreExpr)])
abort
| Bool
otherwise = Arity -> [(Var, CoreExpr)] -> (AnalEnv, DmdEnv, [(Var, CoreExpr)])
loop (Arity
nArity -> Arity -> Arity
forall a. Num a => a -> a -> a
+Arity
1) [(Var, CoreExpr)]
pairs'
where
found_fixpoint :: Bool
found_fixpoint = ((Var, CoreExpr) -> StrictSig) -> [(Var, CoreExpr)] -> [StrictSig]
forall a b. (a -> b) -> [a] -> [b]
map (Var -> StrictSig
idStrictness (Var -> StrictSig)
-> ((Var, CoreExpr) -> Var) -> (Var, CoreExpr) -> StrictSig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst) [(Var, CoreExpr)]
pairs' [StrictSig] -> [StrictSig] -> Bool
forall a. Eq a => a -> a -> Bool
== ((Var, CoreExpr) -> StrictSig) -> [(Var, CoreExpr)] -> [StrictSig]
forall a b. (a -> b) -> [a] -> [b]
map (Var -> StrictSig
idStrictness (Var -> StrictSig)
-> ((Var, CoreExpr) -> Var) -> (Var, CoreExpr) -> StrictSig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst) [(Var, CoreExpr)]
pairs
first_round :: Bool
first_round = Arity
n Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== Arity
1
(DmdEnv
lazy_fv, [(Var, CoreExpr)]
pairs') = Bool -> [(Var, CoreExpr)] -> (DmdEnv, [(Var, CoreExpr)])
step Bool
first_round [(Var, CoreExpr)]
pairs
final_anal_env :: AnalEnv
final_anal_env = TopLevelFlag -> AnalEnv -> [Var] -> AnalEnv
extendAnalEnvs TopLevelFlag
top_lvl AnalEnv
env (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
pairs')
step :: Bool -> [(Id, CoreExpr)] -> (DmdEnv, [(Id, CoreExpr)])
step :: Bool -> [(Var, CoreExpr)] -> (DmdEnv, [(Var, CoreExpr)])
step Bool
first_round [(Var, CoreExpr)]
pairs = (DmdEnv
lazy_fv, [(Var, CoreExpr)]
pairs')
where
start_env :: AnalEnv
start_env | Bool
first_round = AnalEnv
env
| Bool
otherwise = AnalEnv -> AnalEnv
nonVirgin AnalEnv
env
start :: (AnalEnv, DmdEnv)
start = (TopLevelFlag -> AnalEnv -> [Var] -> AnalEnv
extendAnalEnvs TopLevelFlag
top_lvl AnalEnv
start_env (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
pairs), DmdEnv
emptyDmdEnv)
((AnalEnv
_,DmdEnv
lazy_fv), [(Var, CoreExpr)]
pairs') = ((AnalEnv, DmdEnv)
-> (Var, CoreExpr) -> ((AnalEnv, DmdEnv), (Var, CoreExpr)))
-> (AnalEnv, DmdEnv)
-> [(Var, CoreExpr)]
-> ((AnalEnv, DmdEnv), [(Var, CoreExpr)])
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL (AnalEnv, DmdEnv)
-> (Var, CoreExpr) -> ((AnalEnv, DmdEnv), (Var, CoreExpr))
my_downRhs (AnalEnv, DmdEnv)
start [(Var, CoreExpr)]
pairs
my_downRhs :: (AnalEnv, DmdEnv)
-> (Var, CoreExpr) -> ((AnalEnv, DmdEnv), (Var, CoreExpr))
my_downRhs (AnalEnv
env, DmdEnv
lazy_fv) (Var
id,CoreExpr
rhs)
= ((AnalEnv
env', DmdEnv
lazy_fv'), (Var
id', CoreExpr
rhs'))
where
(DmdEnv
lazy_fv1, StrictSig
sig, CoreExpr
rhs') = Maybe [Var]
-> AnalEnv
-> CleanDemand
-> Var
-> CoreExpr
-> (DmdEnv, StrictSig, CoreExpr)
dmdAnalRhsLetDown ([Var] -> Maybe [Var]
forall a. a -> Maybe a
Just [Var]
bndrs) AnalEnv
env CleanDemand
let_dmd Var
id CoreExpr
rhs
lazy_fv' :: DmdEnv
lazy_fv' = (Demand -> Demand -> Demand) -> DmdEnv -> DmdEnv -> DmdEnv
forall a. (a -> a -> a) -> VarEnv a -> VarEnv a -> VarEnv a
plusVarEnv_C Demand -> Demand -> Demand
bothDmd DmdEnv
lazy_fv DmdEnv
lazy_fv1
env' :: AnalEnv
env' = TopLevelFlag -> AnalEnv -> Var -> StrictSig -> AnalEnv
extendAnalEnv TopLevelFlag
top_lvl AnalEnv
env Var
id StrictSig
sig
id' :: Var
id' = Var -> StrictSig -> Var
setIdStrictness Var
id StrictSig
sig
zapIdStrictness :: [(Id, CoreExpr)] -> [(Id, CoreExpr)]
zapIdStrictness :: [(Var, CoreExpr)] -> [(Var, CoreExpr)]
zapIdStrictness [(Var, CoreExpr)]
pairs = [(Var -> StrictSig -> Var
setIdStrictness Var
id StrictSig
nopSig, CoreExpr
rhs) | (Var
id, CoreExpr
rhs) <- [(Var, CoreExpr)]
pairs ]
unitDmdType :: DmdEnv -> DmdType
unitDmdType :: DmdEnv -> DmdType
unitDmdType DmdEnv
dmd_env = DmdEnv -> [Demand] -> Divergence -> DmdType
DmdType DmdEnv
dmd_env [] Divergence
topDiv
coercionDmdEnv :: Coercion -> DmdEnv
coercionDmdEnv :: Coercion -> DmdEnv
coercionDmdEnv Coercion
co = (Var -> Demand) -> VarEnv Var -> DmdEnv
forall a b. (a -> b) -> VarEnv a -> VarEnv b
mapVarEnv (Demand -> Var -> Demand
forall a b. a -> b -> a
const Demand
topDmd) (IdSet -> VarEnv Var
forall a. UniqSet a -> UniqFM a a
getUniqSet (IdSet -> VarEnv Var) -> IdSet -> VarEnv Var
forall a b. (a -> b) -> a -> b
$ Coercion -> IdSet
coVarsOfCo Coercion
co)
addVarDmd :: DmdType -> Var -> Demand -> DmdType
addVarDmd :: DmdType -> Var -> Demand -> DmdType
addVarDmd (DmdType DmdEnv
fv [Demand]
ds Divergence
res) Var
var Demand
dmd
= DmdEnv -> [Demand] -> Divergence -> DmdType
DmdType ((Demand -> Demand -> Demand) -> DmdEnv -> Var -> Demand -> DmdEnv
forall a. (a -> a -> a) -> VarEnv a -> Var -> a -> VarEnv a
extendVarEnv_C Demand -> Demand -> Demand
bothDmd DmdEnv
fv Var
var Demand
dmd) [Demand]
ds Divergence
res
addLazyFVs :: DmdType -> DmdEnv -> DmdType
addLazyFVs :: DmdType -> DmdEnv -> DmdType
addLazyFVs DmdType
dmd_ty DmdEnv
lazy_fvs
= DmdType
dmd_ty DmdType -> BothDmdArg -> DmdType
`bothDmdType` DmdEnv -> BothDmdArg
mkBothDmdArg DmdEnv
lazy_fvs
setBndrsDemandInfo :: [Var] -> [Demand] -> [Var]
setBndrsDemandInfo :: [Var] -> [Demand] -> [Var]
setBndrsDemandInfo (Var
b:[Var]
bs) [Demand]
ds
| Var -> Bool
isTyVar Var
b = Var
b Var -> [Var] -> [Var]
forall a. a -> [a] -> [a]
: [Var] -> [Demand] -> [Var]
setBndrsDemandInfo [Var]
bs [Demand]
ds
setBndrsDemandInfo (Var
b:[Var]
bs) (Demand
d:[Demand]
ds) =
let !new_info :: Var
new_info = Var -> Demand -> Var
setIdDemandInfo Var
b Demand
d
!vars :: [Var]
vars = [Var] -> [Demand] -> [Var]
setBndrsDemandInfo [Var]
bs [Demand]
ds
in Var
new_info Var -> [Var] -> [Var]
forall a. a -> [a] -> [a]
: [Var]
vars
setBndrsDemandInfo [] [Demand]
ds = ASSERT( null ds ) []
setBndrsDemandInfo [Var]
bs [Demand]
_ = String -> SDoc -> [Var]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"setBndrsDemandInfo" ([Var] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Var]
bs)
annotateBndr :: AnalEnv -> DmdType -> Var -> (DmdType, Var)
annotateBndr :: AnalEnv -> DmdType -> Var -> (DmdType, Var)
annotateBndr AnalEnv
env DmdType
dmd_ty Var
var
| Var -> Bool
isId Var
var = (DmdType
dmd_ty', Var -> Demand -> Var
setIdDemandInfo Var
var Demand
dmd)
| Bool
otherwise = (DmdType
dmd_ty, Var
var)
where
(DmdType
dmd_ty', Demand
dmd) = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
False DmdType
dmd_ty Var
var
annotateLamIdBndr :: AnalEnv
-> DFunFlag
-> DmdType
-> Id
-> (DmdType,
Id)
annotateLamIdBndr :: AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Var)
annotateLamIdBndr AnalEnv
env Bool
arg_of_dfun DmdType
dmd_ty Var
id
= ASSERT( isId id )
(DmdType
final_ty, Var -> Demand -> Var
setIdDemandInfo Var
id Demand
dmd)
where
final_ty :: DmdType
final_ty = case Unfolding -> Maybe CoreExpr
maybeUnfoldingTemplate (Var -> Unfolding
idUnfolding Var
id) of
Maybe CoreExpr
Nothing -> DmdType
main_ty
Just CoreExpr
unf -> DmdType
main_ty DmdType -> BothDmdArg -> DmdType
`bothDmdType` BothDmdArg
unf_ty
where
(BothDmdArg
unf_ty, CoreExpr
_) = AnalEnv -> Demand -> CoreExpr -> (BothDmdArg, CoreExpr)
dmdAnalStar AnalEnv
env Demand
dmd CoreExpr
unf
main_ty :: DmdType
main_ty = Demand -> DmdType -> DmdType
addDemand Demand
dmd DmdType
dmd_ty'
(DmdType
dmd_ty', Demand
dmd) = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
arg_of_dfun DmdType
dmd_ty Var
id
type DFunFlag = Bool
notArgOfDfun :: DFunFlag
notArgOfDfun :: Bool
notArgOfDfun = Bool
False
data AnalEnv
= AE { AnalEnv -> DynFlags
ae_dflags :: DynFlags
, AnalEnv -> SigEnv
ae_sigs :: SigEnv
, AnalEnv -> Bool
ae_virgin :: Bool
, AnalEnv -> FamInstEnvs
ae_fam_envs :: FamInstEnvs
}
type SigEnv = VarEnv (StrictSig, TopLevelFlag)
instance Outputable AnalEnv where
ppr :: AnalEnv -> SDoc
ppr (AE { ae_sigs :: AnalEnv -> SigEnv
ae_sigs = SigEnv
env, ae_virgin :: AnalEnv -> Bool
ae_virgin = Bool
virgin })
= String -> SDoc
text String
"AE" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
braces ([SDoc] -> SDoc
vcat
[ String -> SDoc
text String
"ae_virgin =" SDoc -> SDoc -> SDoc
<+> Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
virgin
, String -> SDoc
text String
"ae_sigs =" SDoc -> SDoc -> SDoc
<+> SigEnv -> SDoc
forall a. Outputable a => a -> SDoc
ppr SigEnv
env ])
emptyAnalEnv :: DynFlags -> FamInstEnvs -> AnalEnv
emptyAnalEnv :: DynFlags -> FamInstEnvs -> AnalEnv
emptyAnalEnv DynFlags
dflags FamInstEnvs
fam_envs
= AE :: DynFlags -> SigEnv -> Bool -> FamInstEnvs -> AnalEnv
AE { ae_dflags :: DynFlags
ae_dflags = DynFlags
dflags
, ae_sigs :: SigEnv
ae_sigs = SigEnv
emptySigEnv
, ae_virgin :: Bool
ae_virgin = Bool
True
, ae_fam_envs :: FamInstEnvs
ae_fam_envs = FamInstEnvs
fam_envs
}
emptySigEnv :: SigEnv
emptySigEnv :: SigEnv
emptySigEnv = SigEnv
forall a. VarEnv a
emptyVarEnv
extendAnalEnvs :: TopLevelFlag -> AnalEnv -> [Id] -> AnalEnv
extendAnalEnvs :: TopLevelFlag -> AnalEnv -> [Var] -> AnalEnv
extendAnalEnvs TopLevelFlag
top_lvl AnalEnv
env [Var]
vars
= AnalEnv
env { ae_sigs :: SigEnv
ae_sigs = TopLevelFlag -> SigEnv -> [Var] -> SigEnv
extendSigEnvs TopLevelFlag
top_lvl (AnalEnv -> SigEnv
ae_sigs AnalEnv
env) [Var]
vars }
extendSigEnvs :: TopLevelFlag -> SigEnv -> [Id] -> SigEnv
extendSigEnvs :: TopLevelFlag -> SigEnv -> [Var] -> SigEnv
extendSigEnvs TopLevelFlag
top_lvl SigEnv
sigs [Var]
vars
= SigEnv -> [(Var, (StrictSig, TopLevelFlag))] -> SigEnv
forall a. VarEnv a -> [(Var, a)] -> VarEnv a
extendVarEnvList SigEnv
sigs [ (Var
var, (Var -> StrictSig
idStrictness Var
var, TopLevelFlag
top_lvl)) | Var
var <- [Var]
vars]
extendAnalEnv :: TopLevelFlag -> AnalEnv -> Id -> StrictSig -> AnalEnv
extendAnalEnv :: TopLevelFlag -> AnalEnv -> Var -> StrictSig -> AnalEnv
extendAnalEnv TopLevelFlag
top_lvl AnalEnv
env Var
var StrictSig
sig
= AnalEnv
env { ae_sigs :: SigEnv
ae_sigs = TopLevelFlag -> SigEnv -> Var -> StrictSig -> SigEnv
extendSigEnv TopLevelFlag
top_lvl (AnalEnv -> SigEnv
ae_sigs AnalEnv
env) Var
var StrictSig
sig }
extendSigEnv :: TopLevelFlag -> SigEnv -> Id -> StrictSig -> SigEnv
extendSigEnv :: TopLevelFlag -> SigEnv -> Var -> StrictSig -> SigEnv
extendSigEnv TopLevelFlag
top_lvl SigEnv
sigs Var
var StrictSig
sig = SigEnv -> Var -> (StrictSig, TopLevelFlag) -> SigEnv
forall a. VarEnv a -> Var -> a -> VarEnv a
extendVarEnv SigEnv
sigs Var
var (StrictSig
sig, TopLevelFlag
top_lvl)
lookupSigEnv :: AnalEnv -> Id -> Maybe (StrictSig, TopLevelFlag)
lookupSigEnv :: AnalEnv -> Var -> Maybe (StrictSig, TopLevelFlag)
lookupSigEnv AnalEnv
env Var
id = SigEnv -> Var -> Maybe (StrictSig, TopLevelFlag)
forall a. VarEnv a -> Var -> Maybe a
lookupVarEnv (AnalEnv -> SigEnv
ae_sigs AnalEnv
env) Var
id
nonVirgin :: AnalEnv -> AnalEnv
nonVirgin :: AnalEnv -> AnalEnv
nonVirgin AnalEnv
env = AnalEnv
env { ae_virgin :: Bool
ae_virgin = Bool
False }
findBndrsDmds :: AnalEnv -> DmdType -> [Var] -> (DmdType, [Demand])
findBndrsDmds :: AnalEnv -> DmdType -> [Var] -> (DmdType, [Demand])
findBndrsDmds AnalEnv
env DmdType
dmd_ty [Var]
bndrs
= DmdType -> [Var] -> (DmdType, [Demand])
go DmdType
dmd_ty [Var]
bndrs
where
go :: DmdType -> [Var] -> (DmdType, [Demand])
go DmdType
dmd_ty [] = (DmdType
dmd_ty, [])
go DmdType
dmd_ty (Var
b:[Var]
bs)
| Var -> Bool
isId Var
b = let (DmdType
dmd_ty1, [Demand]
dmds) = DmdType -> [Var] -> (DmdType, [Demand])
go DmdType
dmd_ty [Var]
bs
(DmdType
dmd_ty2, Demand
dmd) = AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
False DmdType
dmd_ty1 Var
b
in (DmdType
dmd_ty2, Demand
dmd Demand -> [Demand] -> [Demand]
forall a. a -> [a] -> [a]
: [Demand]
dmds)
| Bool
otherwise = DmdType -> [Var] -> (DmdType, [Demand])
go DmdType
dmd_ty [Var]
bs
findBndrDmd :: AnalEnv -> Bool -> DmdType -> Id -> (DmdType, Demand)
findBndrDmd :: AnalEnv -> Bool -> DmdType -> Var -> (DmdType, Demand)
findBndrDmd AnalEnv
env Bool
arg_of_dfun DmdType
dmd_ty Var
id
= (DmdType
dmd_ty', Demand
dmd')
where
dmd' :: Demand
dmd' = Demand -> Demand
strictify (Demand -> Demand) -> Demand -> Demand
forall a b. (a -> b) -> a -> b
$
Demand -> TypeShape -> Demand
trimToType Demand
starting_dmd (FamInstEnvs -> Type -> TypeShape
findTypeShape FamInstEnvs
fam_envs Type
id_ty)
(DmdType
dmd_ty', Demand
starting_dmd) = DmdType -> Var -> (DmdType, Demand)
peelFV DmdType
dmd_ty Var
id
id_ty :: Type
id_ty = Var -> Type
idType Var
id
strictify :: Demand -> Demand
strictify Demand
dmd
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_DictsStrict (AnalEnv -> DynFlags
ae_dflags AnalEnv
env)
, Bool -> Bool
not Bool
arg_of_dfun
= Type -> Demand -> Demand
strictifyDictDmd Type
id_ty Demand
dmd
| Bool
otherwise
= Demand
dmd
fam_envs :: FamInstEnvs
fam_envs = AnalEnv -> FamInstEnvs
ae_fam_envs AnalEnv
env