{-# LANGUAGE CPP, MultiWayIf #-}
module SetLevels (
setLevels,
Level(..), LevelType(..), tOP_LEVEL, isJoinCeilLvl, asJoinCeilLvl,
LevelledBind, LevelledExpr, LevelledBndr,
FloatSpec(..), floatSpecLevel,
incMinorLvl, ltMajLvl, ltLvl, isTopLvl
) where
#include "HsVersions.h"
import GhcPrelude
import CoreSyn
import CoreMonad ( FloatOutSwitches(..) )
import CoreUtils ( exprType, exprIsHNF
, exprOkForSpeculation
, exprIsTopLevelBindable
, isExprLevPoly
, collectMakeStaticArgs
)
import CoreArity ( exprBotStrictness_maybe )
import CoreFVs
import CoreSubst
import MkCore ( sortQuantVars )
import Id
import IdInfo
import Var
import VarSet
import UniqSet ( nonDetFoldUniqSet )
import UniqDSet ( getUniqDSet )
import VarEnv
import Literal ( litIsTrivial )
import Demand ( StrictSig, Demand, isStrictDmd, splitStrictSig, increaseStrictSigArity )
import Name ( getOccName, mkSystemVarName )
import OccName ( occNameString )
import Type ( Type, mkLamTypes, splitTyConApp_maybe, tyCoVarsOfType
, isUnliftedType, closeOverKindsDSet )
import BasicTypes ( Arity, RecFlag(..), isRec )
import DataCon ( dataConOrigResTy )
import TysWiredIn
import UniqSupply
import Util
import Outputable
import FastString
import UniqDFM
import FV
import Data.Maybe
import MonadUtils ( mapAccumLM )
type LevelledExpr = TaggedExpr FloatSpec
type LevelledBind = TaggedBind FloatSpec
type LevelledBndr = TaggedBndr FloatSpec
data Level = Level Int
Int
LevelType
data LevelType = BndrLvl | JoinCeilLvl deriving (LevelType -> LevelType -> Bool
(LevelType -> LevelType -> Bool)
-> (LevelType -> LevelType -> Bool) -> Eq LevelType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LevelType -> LevelType -> Bool
$c/= :: LevelType -> LevelType -> Bool
== :: LevelType -> LevelType -> Bool
$c== :: LevelType -> LevelType -> Bool
Eq)
data FloatSpec
= FloatMe Level
| StayPut Level
floatSpecLevel :: FloatSpec -> Level
floatSpecLevel :: FloatSpec -> Level
floatSpecLevel (FloatMe l :: Level
l) = Level
l
floatSpecLevel (StayPut l :: Level
l) = Level
l
instance Outputable FloatSpec where
ppr :: FloatSpec -> SDoc
ppr (FloatMe l :: Level
l) = Char -> SDoc
char 'F' SDoc -> SDoc -> SDoc
<> Level -> SDoc
forall a. Outputable a => a -> SDoc
ppr Level
l
ppr (StayPut l :: Level
l) = Level -> SDoc
forall a. Outputable a => a -> SDoc
ppr Level
l
tOP_LEVEL :: Level
tOP_LEVEL :: Level
tOP_LEVEL = Int -> Int -> LevelType -> Level
Level 0 0 LevelType
BndrLvl
incMajorLvl :: Level -> Level
incMajorLvl :: Level -> Level
incMajorLvl (Level major :: Int
major _ _) = Int -> Int -> LevelType -> Level
Level (Int
major Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) 0 LevelType
BndrLvl
incMinorLvl :: Level -> Level
incMinorLvl :: Level -> Level
incMinorLvl (Level major :: Int
major minor :: Int
minor _) = Int -> Int -> LevelType -> Level
Level Int
major (Int
minorInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) LevelType
BndrLvl
asJoinCeilLvl :: Level -> Level
asJoinCeilLvl :: Level -> Level
asJoinCeilLvl (Level major :: Int
major minor :: Int
minor _) = Int -> Int -> LevelType -> Level
Level Int
major Int
minor LevelType
JoinCeilLvl
maxLvl :: Level -> Level -> Level
maxLvl :: Level -> Level -> Level
maxLvl l1 :: Level
l1@(Level maj1 :: Int
maj1 min1 :: Int
min1 _) l2 :: Level
l2@(Level maj2 :: Int
maj2 min2 :: Int
min2 _)
| (Int
maj1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maj2) Bool -> Bool -> Bool
|| (Int
maj1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
maj2 Bool -> Bool -> Bool
&& Int
min1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
min2) = Level
l1
| Bool
otherwise = Level
l2
ltLvl :: Level -> Level -> Bool
ltLvl :: Level -> Level -> Bool
ltLvl (Level maj1 :: Int
maj1 min1 :: Int
min1 _) (Level maj2 :: Int
maj2 min2 :: Int
min2 _)
= (Int
maj1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maj2) Bool -> Bool -> Bool
|| (Int
maj1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
maj2 Bool -> Bool -> Bool
&& Int
min1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
min2)
ltMajLvl :: Level -> Level -> Bool
ltMajLvl :: Level -> Level -> Bool
ltMajLvl (Level maj1 :: Int
maj1 _ _) (Level maj2 :: Int
maj2 _ _) = Int
maj1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maj2
isTopLvl :: Level -> Bool
isTopLvl :: Level -> Bool
isTopLvl (Level 0 0 _) = Bool
True
isTopLvl _ = Bool
False
isJoinCeilLvl :: Level -> Bool
isJoinCeilLvl :: Level -> Bool
isJoinCeilLvl (Level _ _ t :: LevelType
t) = LevelType
t LevelType -> LevelType -> Bool
forall a. Eq a => a -> a -> Bool
== LevelType
JoinCeilLvl
instance Outputable Level where
ppr :: Level -> SDoc
ppr (Level maj :: Int
maj min :: Int
min typ :: LevelType
typ)
= [SDoc] -> SDoc
hcat [ Char -> SDoc
char '<', Int -> SDoc
int Int
maj, Char -> SDoc
char ',', Int -> SDoc
int Int
min, Char -> SDoc
char '>'
, Bool -> SDoc -> SDoc
ppWhen (LevelType
typ LevelType -> LevelType -> Bool
forall a. Eq a => a -> a -> Bool
== LevelType
JoinCeilLvl) (Char -> SDoc
char 'C') ]
instance Eq Level where
(Level maj1 :: Int
maj1 min1 :: Int
min1 _) == :: Level -> Level -> Bool
== (Level maj2 :: Int
maj2 min2 :: Int
min2 _) = Int
maj1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
maj2 Bool -> Bool -> Bool
&& Int
min1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
min2
setLevels :: FloatOutSwitches
-> CoreProgram
-> UniqSupply
-> [LevelledBind]
setLevels :: FloatOutSwitches -> CoreProgram -> UniqSupply -> [LevelledBind]
setLevels float_lams :: FloatOutSwitches
float_lams binds :: CoreProgram
binds us :: UniqSupply
us
= UniqSupply -> UniqSM [LevelledBind] -> [LevelledBind]
forall a. UniqSupply -> UniqSM a -> a
initLvl UniqSupply
us (LevelEnv -> CoreProgram -> UniqSM [LevelledBind]
do_them LevelEnv
init_env CoreProgram
binds)
where
init_env :: LevelEnv
init_env = FloatOutSwitches -> LevelEnv
initialEnv FloatOutSwitches
float_lams
do_them :: LevelEnv -> [CoreBind] -> LvlM [LevelledBind]
do_them :: LevelEnv -> CoreProgram -> UniqSM [LevelledBind]
do_them _ [] = [LevelledBind] -> UniqSM [LevelledBind]
forall (m :: * -> *) a. Monad m => a -> m a
return []
do_them env :: LevelEnv
env (b :: CoreBind
b:bs :: CoreProgram
bs)
= do { (lvld_bind :: LevelledBind
lvld_bind, env' :: LevelEnv
env') <- LevelEnv -> CoreBind -> LvlM (LevelledBind, LevelEnv)
lvlTopBind LevelEnv
env CoreBind
b
; [LevelledBind]
lvld_binds <- LevelEnv -> CoreProgram -> UniqSM [LevelledBind]
do_them LevelEnv
env' CoreProgram
bs
; [LevelledBind] -> UniqSM [LevelledBind]
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBind
lvld_bind LevelledBind -> [LevelledBind] -> [LevelledBind]
forall a. a -> [a] -> [a]
: [LevelledBind]
lvld_binds) }
lvlTopBind :: LevelEnv -> Bind Id -> LvlM (LevelledBind, LevelEnv)
lvlTopBind :: LevelEnv -> CoreBind -> LvlM (LevelledBind, LevelEnv)
lvlTopBind env :: LevelEnv
env (NonRec bndr :: Id
bndr rhs :: Expr Id
rhs)
= do { LevelledExpr
rhs' <- LevelEnv -> RecFlag -> Id -> Expr Id -> LvlM LevelledExpr
lvl_top LevelEnv
env RecFlag
NonRecursive Id
bndr Expr Id
rhs
; let (env' :: LevelEnv
env', [bndr' :: LevelledBndr
bndr']) = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
NonRecursive LevelEnv
env Level
tOP_LEVEL [Id
bndr]
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec LevelledBndr
bndr' LevelledExpr
rhs', LevelEnv
env') }
lvlTopBind env :: LevelEnv
env (Rec pairs :: [(Id, Expr Id)]
pairs)
= do { let (env' :: LevelEnv
env', bndrs' :: [LevelledBndr]
bndrs') = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
Recursive LevelEnv
env Level
tOP_LEVEL
(((Id, Expr Id) -> Id) -> [(Id, Expr Id)] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map (Id, Expr Id) -> Id
forall a b. (a, b) -> a
fst [(Id, Expr Id)]
pairs)
; [LevelledExpr]
rhss' <- ((Id, Expr Id) -> LvlM LevelledExpr)
-> [(Id, Expr Id)] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(b :: Id
b,r :: Expr Id
r) -> LevelEnv -> RecFlag -> Id -> Expr Id -> LvlM LevelledExpr
lvl_top LevelEnv
env' RecFlag
Recursive Id
b Expr Id
r) [(Id, Expr Id)]
pairs
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec ([LevelledBndr]
bndrs' [LevelledBndr] -> [LevelledExpr] -> [(LevelledBndr, LevelledExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [LevelledExpr]
rhss'), LevelEnv
env') }
lvl_top :: LevelEnv -> RecFlag -> Id -> CoreExpr -> LvlM LevelledExpr
lvl_top :: LevelEnv -> RecFlag -> Id -> Expr Id -> LvlM LevelledExpr
lvl_top env :: LevelEnv
env is_rec :: RecFlag
is_rec bndr :: Id
bndr rhs :: Expr Id
rhs
= LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs LevelEnv
env RecFlag
is_rec
(Id -> Bool
isBottomingId Id
bndr)
Maybe Int
forall a. Maybe a
Nothing
(Expr Id -> CoreExprWithFVs
freeVars Expr Id
rhs)
lvlExpr :: LevelEnv
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlExpr :: LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr env :: LevelEnv
env (_, AnnType ty :: Type
ty) = LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> LevelledExpr
forall b. Type -> Expr b
Type (Subst -> Type -> Type
CoreSubst.substTy (LevelEnv -> Subst
le_subst LevelEnv
env) Type
ty))
lvlExpr env :: LevelEnv
env (_, AnnCoercion co :: Coercion
co) = LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Coercion -> LevelledExpr
forall b. Coercion -> Expr b
Coercion (Subst -> Coercion -> Coercion
substCo (LevelEnv -> Subst
le_subst LevelEnv
env) Coercion
co))
lvlExpr env :: LevelEnv
env (_, AnnVar v :: Id
v) = LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelEnv -> Id -> LevelledExpr
lookupVar LevelEnv
env Id
v)
lvlExpr _ (_, AnnLit lit :: Literal
lit) = LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Literal -> LevelledExpr
forall b. Literal -> Expr b
Lit Literal
lit)
lvlExpr env :: LevelEnv
env (_, AnnCast expr :: CoreExprWithFVs
expr (_, co :: Coercion
co)) = do
LevelledExpr
expr' <- LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailExpr LevelEnv
env CoreExprWithFVs
expr
LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledExpr -> Coercion -> LevelledExpr
forall b. Expr b -> Coercion -> Expr b
Cast LevelledExpr
expr' (Subst -> Coercion -> Coercion
substCo (LevelEnv -> Subst
le_subst LevelEnv
env) Coercion
co))
lvlExpr env :: LevelEnv
env (_, AnnTick tickish :: Tickish Id
tickish expr :: CoreExprWithFVs
expr) = do
LevelledExpr
expr' <- LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailExpr LevelEnv
env CoreExprWithFVs
expr
let tickish' :: Tickish Id
tickish' = Subst -> Tickish Id -> Tickish Id
substTickish (LevelEnv -> Subst
le_subst LevelEnv
env) Tickish Id
tickish
LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Tickish Id -> LevelledExpr -> LevelledExpr
forall b. Tickish Id -> Expr b -> Expr b
Tick Tickish Id
tickish' LevelledExpr
expr')
lvlExpr env :: LevelEnv
env expr :: CoreExprWithFVs
expr@(_, AnnApp _ _) = LevelEnv
-> CoreExprWithFVs
-> (CoreExprWithFVs, [CoreExprWithFVs])
-> LvlM LevelledExpr
lvlApp LevelEnv
env CoreExprWithFVs
expr (CoreExprWithFVs -> (CoreExprWithFVs, [CoreExprWithFVs])
forall b a. AnnExpr b a -> (AnnExpr b a, [AnnExpr b a])
collectAnnArgs CoreExprWithFVs
expr)
lvlExpr env :: LevelEnv
env expr :: CoreExprWithFVs
expr@(_, AnnLam {})
= do { LevelledExpr
new_body <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE LevelEnv
new_env Bool
True CoreExprWithFVs
body
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
new_bndrs LevelledExpr
new_body) }
where
(bndrs :: [Id]
bndrs, body :: CoreExprWithFVs
body) = CoreExprWithFVs -> ([Id], CoreExprWithFVs)
forall bndr annot.
AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
collectAnnBndrs CoreExprWithFVs
expr
(env1 :: LevelEnv
env1, bndrs1 :: [Id]
bndrs1) = RecFlag -> LevelEnv -> [Id] -> (LevelEnv, [Id])
substBndrsSL RecFlag
NonRecursive LevelEnv
env [Id]
bndrs
(new_env :: LevelEnv
new_env, new_bndrs :: [LevelledBndr]
new_bndrs) = LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs LevelEnv
env1 (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env) [Id]
bndrs1
lvlExpr env :: LevelEnv
env (_, AnnLet bind :: AnnBind Id FVAnn
bind body :: CoreExprWithFVs
body)
= do { (bind' :: LevelledBind
bind', new_env :: LevelEnv
new_env) <- LevelEnv -> AnnBind Id FVAnn -> LvlM (LevelledBind, LevelEnv)
lvlBind LevelEnv
env AnnBind Id FVAnn
bind
; LevelledExpr
body' <- LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
new_env CoreExprWithFVs
body
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBind -> LevelledExpr -> LevelledExpr
forall b. Bind b -> Expr b -> Expr b
Let LevelledBind
bind' LevelledExpr
body') }
lvlExpr env :: LevelEnv
env (_, AnnCase scrut :: CoreExprWithFVs
scrut case_bndr :: Id
case_bndr ty :: Type
ty alts :: [AnnAlt Id FVAnn]
alts)
= do { LevelledExpr
scrut' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE LevelEnv
env Bool
True CoreExprWithFVs
scrut
; LevelEnv
-> FVAnn
-> LevelledExpr
-> Id
-> Type
-> [AnnAlt Id FVAnn]
-> LvlM LevelledExpr
lvlCase LevelEnv
env (CoreExprWithFVs -> FVAnn
freeVarsOf CoreExprWithFVs
scrut) LevelledExpr
scrut' Id
case_bndr Type
ty [AnnAlt Id FVAnn]
alts }
lvlNonTailExpr :: LevelEnv
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlNonTailExpr :: LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailExpr env :: LevelEnv
env expr :: CoreExprWithFVs
expr
= LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr (LevelEnv -> LevelEnv
placeJoinCeiling LevelEnv
env) CoreExprWithFVs
expr
lvlApp :: LevelEnv
-> CoreExprWithFVs
-> (CoreExprWithFVs, [CoreExprWithFVs])
-> LvlM LevelledExpr
lvlApp :: LevelEnv
-> CoreExprWithFVs
-> (CoreExprWithFVs, [CoreExprWithFVs])
-> LvlM LevelledExpr
lvlApp env :: LevelEnv
env orig_expr :: CoreExprWithFVs
orig_expr ((_,AnnVar fn :: Id
fn), args :: [CoreExprWithFVs]
args)
| LevelEnv -> Bool
floatOverSat LevelEnv
env
, Int
arity Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
, Int
arity Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n_val_args
, Maybe Class
Nothing <- Id -> Maybe Class
isClassOpId_maybe Id
fn
= do { [LevelledExpr]
rargs' <- (CoreExprWithFVs -> LvlM LevelledExpr)
-> [CoreExprWithFVs] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE LevelEnv
env Bool
False) [CoreExprWithFVs]
rargs
; LevelledExpr
lapp' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE LevelEnv
env Bool
False CoreExprWithFVs
lapp
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ((LevelledExpr -> LevelledExpr -> LevelledExpr)
-> LevelledExpr -> [LevelledExpr] -> LevelledExpr
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' LevelledExpr -> LevelledExpr -> LevelledExpr
forall b. Expr b -> Expr b -> Expr b
App LevelledExpr
lapp' [LevelledExpr]
rargs') }
| Bool
otherwise
= do { (_, args' :: [LevelledExpr]
args') <- ([Demand] -> CoreExprWithFVs -> UniqSM ([Demand], LevelledExpr))
-> [Demand]
-> [CoreExprWithFVs]
-> UniqSM ([Demand], [LevelledExpr])
forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM [Demand] -> CoreExprWithFVs -> UniqSM ([Demand], LevelledExpr)
lvl_arg [Demand]
stricts [CoreExprWithFVs]
args
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ((LevelledExpr -> LevelledExpr -> LevelledExpr)
-> LevelledExpr -> [LevelledExpr] -> LevelledExpr
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' LevelledExpr -> LevelledExpr -> LevelledExpr
forall b. Expr b -> Expr b -> Expr b
App (LevelEnv -> Id -> LevelledExpr
lookupVar LevelEnv
env Id
fn) [LevelledExpr]
args') }
where
n_val_args :: Int
n_val_args = (CoreExprWithFVs -> Bool) -> [CoreExprWithFVs] -> Int
forall a. (a -> Bool) -> [a] -> Int
count (Expr Id -> Bool
forall b. Expr b -> Bool
isValArg (Expr Id -> Bool)
-> (CoreExprWithFVs -> Expr Id) -> CoreExprWithFVs -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExprWithFVs -> Expr Id
forall bndr annot. AnnExpr bndr annot -> Expr bndr
deAnnotate) [CoreExprWithFVs]
args
arity :: Int
arity = Id -> Int
idArity Id
fn
stricts :: [Demand]
stricts :: [Demand]
stricts = case StrictSig -> ([Demand], DmdResult)
splitStrictSig (Id -> StrictSig
idStrictness Id
fn) of
(arg_ds :: [Demand]
arg_ds, _) | [Demand]
arg_ds [Demand] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` Int
n_val_args
-> []
| Bool
otherwise
-> [Demand]
arg_ds
(lapp :: CoreExprWithFVs
lapp, rargs :: [CoreExprWithFVs]
rargs) = Int
-> CoreExprWithFVs
-> [CoreExprWithFVs]
-> (CoreExprWithFVs, [CoreExprWithFVs])
forall t b annot.
(Eq t, Num t) =>
t
-> AnnExpr b annot
-> [AnnExpr b annot]
-> (AnnExpr b annot, [AnnExpr b annot])
left (Int
n_val_args Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
arity) CoreExprWithFVs
orig_expr []
left :: t
-> AnnExpr b annot
-> [AnnExpr b annot]
-> (AnnExpr b annot, [AnnExpr b annot])
left 0 e :: AnnExpr b annot
e rargs :: [AnnExpr b annot]
rargs = (AnnExpr b annot
e, [AnnExpr b annot]
rargs)
left n :: t
n (_, AnnApp f :: AnnExpr b annot
f a :: AnnExpr b annot
a) rargs :: [AnnExpr b annot]
rargs
| Expr b -> Bool
forall b. Expr b -> Bool
isValArg (AnnExpr b annot -> Expr b
forall bndr annot. AnnExpr bndr annot -> Expr bndr
deAnnotate AnnExpr b annot
a) = t
-> AnnExpr b annot
-> [AnnExpr b annot]
-> (AnnExpr b annot, [AnnExpr b annot])
left (t
nt -> t -> t
forall a. Num a => a -> a -> a
-1) AnnExpr b annot
f (AnnExpr b annot
aAnnExpr b annot -> [AnnExpr b annot] -> [AnnExpr b annot]
forall a. a -> [a] -> [a]
:[AnnExpr b annot]
rargs)
| Bool
otherwise = t
-> AnnExpr b annot
-> [AnnExpr b annot]
-> (AnnExpr b annot, [AnnExpr b annot])
left t
n AnnExpr b annot
f (AnnExpr b annot
aAnnExpr b annot -> [AnnExpr b annot] -> [AnnExpr b annot]
forall a. a -> [a] -> [a]
:[AnnExpr b annot]
rargs)
left _ _ _ = String -> (AnnExpr b annot, [AnnExpr b annot])
forall a. String -> a
panic "SetLevels.lvlExpr.left"
is_val_arg :: CoreExprWithFVs -> Bool
is_val_arg :: CoreExprWithFVs -> Bool
is_val_arg (_, AnnType {}) = Bool
False
is_val_arg _ = Bool
True
lvl_arg :: [Demand] -> CoreExprWithFVs -> LvlM ([Demand], LevelledExpr)
lvl_arg :: [Demand] -> CoreExprWithFVs -> UniqSM ([Demand], LevelledExpr)
lvl_arg strs :: [Demand]
strs arg :: CoreExprWithFVs
arg | (str1 :: Demand
str1 : strs' :: [Demand]
strs') <- [Demand]
strs
, CoreExprWithFVs -> Bool
is_val_arg CoreExprWithFVs
arg
= do { LevelledExpr
arg' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
env (Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isStrictDmd Demand
str1) CoreExprWithFVs
arg
; ([Demand], LevelledExpr) -> UniqSM ([Demand], LevelledExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Demand]
strs', LevelledExpr
arg') }
| Bool
otherwise
= do { LevelledExpr
arg' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
env Bool
False CoreExprWithFVs
arg
; ([Demand], LevelledExpr) -> UniqSM ([Demand], LevelledExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Demand]
strs, LevelledExpr
arg') }
lvlApp env :: LevelEnv
env _ (fun :: CoreExprWithFVs
fun, args :: [CoreExprWithFVs]
args)
=
do { [LevelledExpr]
args' <- (CoreExprWithFVs -> LvlM LevelledExpr)
-> [CoreExprWithFVs] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE LevelEnv
env Bool
False) [CoreExprWithFVs]
args
; LevelledExpr
fun' <- LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailExpr LevelEnv
env CoreExprWithFVs
fun
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ((LevelledExpr -> LevelledExpr -> LevelledExpr)
-> LevelledExpr -> [LevelledExpr] -> LevelledExpr
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' LevelledExpr -> LevelledExpr -> LevelledExpr
forall b. Expr b -> Expr b -> Expr b
App LevelledExpr
fun' [LevelledExpr]
args') }
lvlCase :: LevelEnv
-> DVarSet
-> LevelledExpr
-> Id -> Type
-> [CoreAltWithFVs]
-> LvlM LevelledExpr
lvlCase :: LevelEnv
-> FVAnn
-> LevelledExpr
-> Id
-> Type
-> [AnnAlt Id FVAnn]
-> LvlM LevelledExpr
lvlCase env :: LevelEnv
env scrut_fvs :: FVAnn
scrut_fvs scrut' :: LevelledExpr
scrut' case_bndr :: Id
case_bndr ty :: Type
ty alts :: [AnnAlt Id FVAnn]
alts
| [(con :: AltCon
con@(DataAlt {}), bs :: [Id]
bs, body :: CoreExprWithFVs
body)] <- [AnnAlt Id FVAnn]
alts
, Expr Id -> Bool
exprIsHNF (LevelledExpr -> Expr Id
forall t. TaggedExpr t -> Expr Id
deTagExpr LevelledExpr
scrut')
, Bool -> Bool
not (Level -> Bool
isTopLvl Level
dest_lvl)
, Bool -> Bool
not (LevelEnv -> Bool
floatTopLvlOnly LevelEnv
env)
=
do { (env1 :: LevelEnv
env1, (case_bndr' :: Id
case_bndr' : bs' :: [Id]
bs')) <- LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneCaseBndrs LevelEnv
env Level
dest_lvl (Id
case_bndr Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
: [Id]
bs)
; let rhs_env :: LevelEnv
rhs_env = LevelEnv -> Id -> LevelledExpr -> LevelEnv
extendCaseBndrEnv LevelEnv
env1 Id
case_bndr LevelledExpr
scrut'
; LevelledExpr
body' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
rhs_env Bool
True CoreExprWithFVs
body
; let alt' :: (AltCon, [LevelledBndr], LevelledExpr)
alt' = (AltCon
con, (Id -> LevelledBndr) -> [Id] -> [LevelledBndr]
forall a b. (a -> b) -> [a] -> [b]
map (Level -> Id -> LevelledBndr
stayPut Level
dest_lvl) [Id]
bs', LevelledExpr
body')
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledExpr
-> LevelledBndr
-> Type
-> [(AltCon, [LevelledBndr], LevelledExpr)]
-> LevelledExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case LevelledExpr
scrut' (Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
case_bndr' (Level -> FloatSpec
FloatMe Level
dest_lvl)) Type
ty' [(AltCon, [LevelledBndr], LevelledExpr)
alt']) }
| Bool
otherwise
= do { let (alts_env1 :: LevelEnv
alts_env1, [case_bndr' :: LevelledBndr
case_bndr']) = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
NonRecursive LevelEnv
env Level
incd_lvl [Id
case_bndr]
alts_env :: LevelEnv
alts_env = LevelEnv -> Id -> LevelledExpr -> LevelEnv
extendCaseBndrEnv LevelEnv
alts_env1 Id
case_bndr LevelledExpr
scrut'
; [(AltCon, [LevelledBndr], LevelledExpr)]
alts' <- (AnnAlt Id FVAnn -> UniqSM (AltCon, [LevelledBndr], LevelledExpr))
-> [AnnAlt Id FVAnn]
-> UniqSM [(AltCon, [LevelledBndr], LevelledExpr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LevelEnv
-> AnnAlt Id FVAnn -> UniqSM (AltCon, [LevelledBndr], LevelledExpr)
forall a.
LevelEnv
-> (a, [Id], CoreExprWithFVs)
-> UniqSM (a, [LevelledBndr], LevelledExpr)
lvl_alt LevelEnv
alts_env) [AnnAlt Id FVAnn]
alts
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledExpr
-> LevelledBndr
-> Type
-> [(AltCon, [LevelledBndr], LevelledExpr)]
-> LevelledExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case LevelledExpr
scrut' LevelledBndr
case_bndr' Type
ty' [(AltCon, [LevelledBndr], LevelledExpr)]
alts') }
where
ty' :: Type
ty' = Subst -> Type -> Type
substTy (LevelEnv -> Subst
le_subst LevelEnv
env) Type
ty
incd_lvl :: Level
incd_lvl = Level -> Level
incMinorLvl (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
dest_lvl :: Level
dest_lvl = (Id -> Bool) -> LevelEnv -> FVAnn -> Level
maxFvLevel (Bool -> Id -> Bool
forall a b. a -> b -> a
const Bool
True) LevelEnv
env FVAnn
scrut_fvs
lvl_alt :: LevelEnv
-> (a, [Id], CoreExprWithFVs)
-> UniqSM (a, [LevelledBndr], LevelledExpr)
lvl_alt alts_env :: LevelEnv
alts_env (con :: a
con, bs :: [Id]
bs, rhs :: CoreExprWithFVs
rhs)
= do { LevelledExpr
rhs' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
new_env Bool
True CoreExprWithFVs
rhs
; (a, [LevelledBndr], LevelledExpr)
-> UniqSM (a, [LevelledBndr], LevelledExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
con, [LevelledBndr]
bs', LevelledExpr
rhs') }
where
(new_env :: LevelEnv
new_env, bs' :: [LevelledBndr]
bs') = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
NonRecursive LevelEnv
alts_env Level
incd_lvl [Id]
bs
lvlNonTailMFE :: LevelEnv
-> Bool
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlNonTailMFE :: LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlNonTailMFE env :: LevelEnv
env strict_ctxt :: Bool
strict_ctxt ann_expr :: CoreExprWithFVs
ann_expr
= LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE (LevelEnv -> LevelEnv
placeJoinCeiling LevelEnv
env) Bool
strict_ctxt CoreExprWithFVs
ann_expr
lvlMFE :: LevelEnv
-> Bool
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlMFE :: LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE env :: LevelEnv
env _ (_, AnnType ty :: Type
ty)
= LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> LevelledExpr
forall b. Type -> Expr b
Type (Subst -> Type -> Type
CoreSubst.substTy (LevelEnv -> Subst
le_subst LevelEnv
env) Type
ty))
lvlMFE env :: LevelEnv
env strict_ctxt :: Bool
strict_ctxt (_, AnnTick t :: Tickish Id
t e :: CoreExprWithFVs
e)
= do { LevelledExpr
e' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
env Bool
strict_ctxt CoreExprWithFVs
e
; let t' :: Tickish Id
t' = Subst -> Tickish Id -> Tickish Id
substTickish (LevelEnv -> Subst
le_subst LevelEnv
env) Tickish Id
t
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Tickish Id -> LevelledExpr -> LevelledExpr
forall b. Tickish Id -> Expr b -> Expr b
Tick Tickish Id
t' LevelledExpr
e') }
lvlMFE env :: LevelEnv
env strict_ctxt :: Bool
strict_ctxt (_, AnnCast e :: CoreExprWithFVs
e (_, co :: Coercion
co))
= do { LevelledExpr
e' <- LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
env Bool
strict_ctxt CoreExprWithFVs
e
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledExpr -> Coercion -> LevelledExpr
forall b. Expr b -> Coercion -> Expr b
Cast LevelledExpr
e' (Subst -> Coercion -> Coercion
substCo (LevelEnv -> Subst
le_subst LevelEnv
env) Coercion
co)) }
lvlMFE env :: LevelEnv
env strict_ctxt :: Bool
strict_ctxt e :: CoreExprWithFVs
e@(_, AnnCase {})
| Bool
strict_ctxt
= LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
env CoreExprWithFVs
e
lvlMFE env :: LevelEnv
env strict_ctxt :: Bool
strict_ctxt ann_expr :: CoreExprWithFVs
ann_expr
| LevelEnv -> Bool
floatTopLvlOnly LevelEnv
env Bool -> Bool -> Bool
&& Bool -> Bool
not (Level -> Bool
isTopLvl Level
dest_lvl)
Bool -> Bool -> Bool
|| (Id -> Bool) -> FVAnn -> Bool
anyDVarSet Id -> Bool
isJoinId FVAnn
fvs
Bool -> Bool -> Bool
|| Expr Id -> Bool
isExprLevPoly Expr Id
expr
Bool -> Bool -> Bool
|| Expr Id -> [Id] -> Bool
notWorthFloating Expr Id
expr [Id]
abs_vars
Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
float_me
=
LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
env CoreExprWithFVs
ann_expr
| Bool
float_is_new_lam Bool -> Bool -> Bool
|| Expr Id -> Type -> Bool
exprIsTopLevelBindable Expr Id
expr Type
expr_ty
= do { LevelledExpr
expr1 <- [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs [Id]
abs_vars Level
dest_lvl LevelEnv
rhs_env RecFlag
NonRecursive
(Maybe (Int, StrictSig) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Int, StrictSig)
mb_bot_str)
Maybe Int
forall a. Maybe a
join_arity_maybe
CoreExprWithFVs
ann_expr
; Id
var <- LevelledExpr -> Maybe Int -> Bool -> LvlM Id
newLvlVar LevelledExpr
expr1 Maybe Int
forall a. Maybe a
join_arity_maybe Bool
is_mk_static
; let var2 :: Id
var2 = Id -> Int -> Maybe (Int, StrictSig) -> Id
annotateBotStr Id
var Int
float_n_lams Maybe (Int, StrictSig)
mb_bot_str
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBind -> LevelledExpr -> LevelledExpr
forall b. Bind b -> Expr b -> Expr b
Let (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec (Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
var2 (Level -> FloatSpec
FloatMe Level
dest_lvl)) LevelledExpr
expr1)
(LevelledExpr -> [Id] -> LevelledExpr
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
var2) [Id]
abs_vars)) }
| Bool
escapes_value_lam
, Bool -> Bool
not Bool
expr_ok_for_spec
, Just (tc :: TyCon
tc, _) <- HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
splitTyConApp_maybe Type
expr_ty
, Just dc :: DataCon
dc <- TyCon -> Maybe DataCon
boxingDataCon_maybe TyCon
tc
, let dc_res_ty :: Type
dc_res_ty = DataCon -> Type
dataConOrigResTy DataCon
dc
[bx_bndr :: Id
bx_bndr, ubx_bndr :: Id
ubx_bndr] = [Type] -> [Id]
mkTemplateLocals [Type
dc_res_ty, Type
expr_ty]
= do { LevelledExpr
expr1 <- LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
rhs_env CoreExprWithFVs
ann_expr
; let l1r :: Level
l1r = LevelEnv -> Level
incMinorLvlFrom LevelEnv
rhs_env
float_rhs :: LevelledExpr
float_rhs = [LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
abs_vars_w_lvls (LevelledExpr -> LevelledExpr) -> LevelledExpr -> LevelledExpr
forall a b. (a -> b) -> a -> b
$
LevelledExpr
-> LevelledBndr
-> Type
-> [(AltCon, [LevelledBndr], LevelledExpr)]
-> LevelledExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case LevelledExpr
expr1 (Level -> Id -> LevelledBndr
stayPut Level
l1r Id
ubx_bndr) Type
dc_res_ty
[(AltCon
DEFAULT, [], DataCon -> [LevelledExpr] -> LevelledExpr
forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
dc [Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
ubx_bndr])]
; Id
var <- LevelledExpr -> Maybe Int -> Bool -> LvlM Id
newLvlVar LevelledExpr
float_rhs Maybe Int
forall a. Maybe a
Nothing Bool
is_mk_static
; let l1u :: Level
l1u = LevelEnv -> Level
incMinorLvlFrom LevelEnv
env
use_expr :: LevelledExpr
use_expr = LevelledExpr
-> LevelledBndr
-> Type
-> [(AltCon, [LevelledBndr], LevelledExpr)]
-> LevelledExpr
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (LevelledExpr -> [Id] -> LevelledExpr
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
var) [Id]
abs_vars)
(Level -> Id -> LevelledBndr
stayPut Level
l1u Id
bx_bndr) Type
expr_ty
[(DataCon -> AltCon
DataAlt DataCon
dc, [Level -> Id -> LevelledBndr
stayPut Level
l1u Id
ubx_bndr], Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
ubx_bndr)]
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBind -> LevelledExpr -> LevelledExpr
forall b. Bind b -> Expr b -> Expr b
Let (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec (Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
var (Level -> FloatSpec
FloatMe Level
dest_lvl)) LevelledExpr
float_rhs)
LevelledExpr
use_expr) }
| Bool
otherwise
= LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
env CoreExprWithFVs
ann_expr
where
expr :: Expr Id
expr = CoreExprWithFVs -> Expr Id
forall bndr annot. AnnExpr bndr annot -> Expr bndr
deAnnotate CoreExprWithFVs
ann_expr
expr_ty :: Type
expr_ty = Expr Id -> Type
exprType Expr Id
expr
fvs :: FVAnn
fvs = CoreExprWithFVs -> FVAnn
freeVarsOf CoreExprWithFVs
ann_expr
fvs_ty :: TyCoVarSet
fvs_ty = Type -> TyCoVarSet
tyCoVarsOfType Type
expr_ty
is_bot :: Bool
is_bot = Maybe (Int, StrictSig) -> Bool
forall s. Maybe (Int, s) -> Bool
isBottomThunk Maybe (Int, StrictSig)
mb_bot_str
is_function :: Bool
is_function = CoreExprWithFVs -> Bool
isFunction CoreExprWithFVs
ann_expr
mb_bot_str :: Maybe (Int, StrictSig)
mb_bot_str = Expr Id -> Maybe (Int, StrictSig)
exprBotStrictness_maybe Expr Id
expr
expr_ok_for_spec :: Bool
expr_ok_for_spec = Expr Id -> Bool
exprOkForSpeculation Expr Id
expr
dest_lvl :: Level
dest_lvl = LevelEnv -> FVAnn -> TyCoVarSet -> Bool -> Bool -> Bool -> Level
destLevel LevelEnv
env FVAnn
fvs TyCoVarSet
fvs_ty Bool
is_function Bool
is_bot Bool
False
abs_vars :: [Id]
abs_vars = Level -> LevelEnv -> FVAnn -> [Id]
abstractVars Level
dest_lvl LevelEnv
env FVAnn
fvs
float_is_new_lam :: Bool
float_is_new_lam = Int
float_n_lams Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
float_n_lams :: Int
float_n_lams = (Id -> Bool) -> [Id] -> Int
forall a. (a -> Bool) -> [a] -> Int
count Id -> Bool
isId [Id]
abs_vars
(rhs_env :: LevelEnv
rhs_env, abs_vars_w_lvls :: [LevelledBndr]
abs_vars_w_lvls) = LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs LevelEnv
env Level
dest_lvl [Id]
abs_vars
join_arity_maybe :: Maybe a
join_arity_maybe = Maybe a
forall a. Maybe a
Nothing
is_mk_static :: Bool
is_mk_static = Maybe (Expr Id, Type, Expr Id, Expr Id) -> Bool
forall a. Maybe a -> Bool
isJust (Expr Id -> Maybe (Expr Id, Type, Expr Id, Expr Id)
collectMakeStaticArgs Expr Id
expr)
float_me :: Bool
float_me = Bool
saves_work Bool -> Bool -> Bool
|| Bool
saves_alloc Bool -> Bool -> Bool
|| Bool
is_mk_static
saves_work :: Bool
saves_work = Bool
escapes_value_lam Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
float_is_new_lam
escapes_value_lam :: Bool
escapes_value_lam = Level
dest_lvl Level -> Level -> Bool
`ltMajLvl` (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
saves_alloc :: Bool
saves_alloc = Level -> Bool
isTopLvl Level
dest_lvl
Bool -> Bool -> Bool
&& LevelEnv -> Bool
floatConsts LevelEnv
env
Bool -> Bool -> Bool
&& (Bool -> Bool
not Bool
strict_ctxt Bool -> Bool -> Bool
|| Bool
is_bot Bool -> Bool -> Bool
|| Expr Id -> Bool
exprIsHNF Expr Id
expr)
isBottomThunk :: Maybe (Arity, s) -> Bool
isBottomThunk :: Maybe (Int, s) -> Bool
isBottomThunk (Just (0, _)) = Bool
True
isBottomThunk _ = Bool
False
annotateBotStr :: Id -> Arity -> Maybe (Arity, StrictSig) -> Id
annotateBotStr :: Id -> Int -> Maybe (Int, StrictSig) -> Id
annotateBotStr id :: Id
id n_extra :: Int
n_extra mb_str :: Maybe (Int, StrictSig)
mb_str
= case Maybe (Int, StrictSig)
mb_str of
Nothing -> Id
id
Just (arity :: Int
arity, sig :: StrictSig
sig) -> Id
id Id -> Int -> Id
`setIdArity` (Int
arity Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n_extra)
Id -> StrictSig -> Id
`setIdStrictness` (Int -> StrictSig -> StrictSig
increaseStrictSigArity Int
n_extra StrictSig
sig)
notWorthFloating :: CoreExpr -> [Var] -> Bool
notWorthFloating :: Expr Id -> [Id] -> Bool
notWorthFloating e :: Expr Id
e abs_vars :: [Id]
abs_vars
= Expr Id -> Int -> Bool
forall a b. (Ord a, Num a) => Expr b -> a -> Bool
go Expr Id
e ((Id -> Bool) -> [Id] -> Int
forall a. (a -> Bool) -> [a] -> Int
count Id -> Bool
isId [Id]
abs_vars)
where
go :: Expr b -> a -> Bool
go (Var {}) n :: a
n = a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 0
go (Lit lit :: Literal
lit) n :: a
n = ASSERT( n==0 )
Literal -> Bool
litIsTrivial Literal
lit
go (Tick t :: Tickish Id
t e :: Expr b
e) n :: a
n = Bool -> Bool
not (Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishIsCode Tickish Id
t) Bool -> Bool -> Bool
&& Expr b -> a -> Bool
go Expr b
e a
n
go (Cast e :: Expr b
e _) n :: a
n = Expr b -> a -> Bool
go Expr b
e a
n
go (App e :: Expr b
e arg :: Expr b
arg) n :: a
n
| Type {} <- Expr b
arg = Expr b -> a -> Bool
go Expr b
e a
n
| Coercion {} <- Expr b
arg = Expr b -> a -> Bool
go Expr b
e a
n
| a
na -> a -> Bool
forall a. Eq a => a -> a -> Bool
==0 = Bool
False
| Expr b -> Bool
forall b. Expr b -> Bool
is_triv Expr b
arg = Expr b -> a -> Bool
go Expr b
e (a
na -> a -> a
forall a. Num a => a -> a -> a
-1)
| Bool
otherwise = Bool
False
go _ _ = Bool
False
is_triv :: Expr b -> Bool
is_triv (Lit {}) = Bool
True
is_triv (Var {}) = Bool
True
is_triv (Cast e :: Expr b
e _) = Expr b -> Bool
is_triv Expr b
e
is_triv (App e :: Expr b
e (Type {})) = Expr b -> Bool
is_triv Expr b
e
is_triv (App e :: Expr b
e (Coercion {})) = Expr b -> Bool
is_triv Expr b
e
is_triv (Tick t :: Tickish Id
t e :: Expr b
e) = Bool -> Bool
not (Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishIsCode Tickish Id
t) Bool -> Bool -> Bool
&& Expr b -> Bool
is_triv Expr b
e
is_triv _ = Bool
False
lvlBind :: LevelEnv
-> CoreBindWithFVs
-> LvlM (LevelledBind, LevelEnv)
lvlBind :: LevelEnv -> AnnBind Id FVAnn -> LvlM (LevelledBind, LevelEnv)
lvlBind env :: LevelEnv
env (AnnNonRec bndr :: Id
bndr rhs :: CoreExprWithFVs
rhs)
| Id -> Bool
isTyVar Id
bndr
Bool -> Bool -> Bool
|| Id -> Bool
isCoVar Id
bndr
Bool -> Bool -> Bool
|| Bool -> Bool
not (LevelEnv -> Level -> Bool
profitableFloat LevelEnv
env Level
dest_lvl)
Bool -> Bool -> Bool
|| (Level -> Bool
isTopLvl Level
dest_lvl Bool -> Bool -> Bool
&& Bool -> Bool
not (Expr Id -> Type -> Bool
exprIsTopLevelBindable Expr Id
deann_rhs Type
bndr_ty))
=
do { LevelledExpr
rhs' <- LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs LevelEnv
env RecFlag
NonRecursive Bool
is_bot Maybe Int
mb_join_arity CoreExprWithFVs
rhs
; let bind_lvl :: Level
bind_lvl = Level -> Level
incMinorLvl (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
(env' :: LevelEnv
env', [bndr' :: LevelledBndr
bndr']) = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
NonRecursive LevelEnv
env Level
bind_lvl [Id
bndr]
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec LevelledBndr
bndr' LevelledExpr
rhs', LevelEnv
env') }
| [Id] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
abs_vars
= do {
LevelledExpr
rhs' <- [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs [] Level
dest_lvl LevelEnv
env RecFlag
NonRecursive
Bool
is_bot Maybe Int
mb_join_arity CoreExprWithFVs
rhs
; (env' :: LevelEnv
env', [bndr' :: Id
bndr']) <- RecFlag -> LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneLetVars RecFlag
NonRecursive LevelEnv
env Level
dest_lvl [Id
bndr]
; let bndr2 :: Id
bndr2 = Id -> Int -> Maybe (Int, StrictSig) -> Id
annotateBotStr Id
bndr' 0 Maybe (Int, StrictSig)
mb_bot_str
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec (Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
bndr2 (Level -> FloatSpec
FloatMe Level
dest_lvl)) LevelledExpr
rhs', LevelEnv
env') }
| Bool
otherwise
= do {
LevelledExpr
rhs' <- [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs [Id]
abs_vars Level
dest_lvl LevelEnv
env RecFlag
NonRecursive
Bool
is_bot Maybe Int
mb_join_arity CoreExprWithFVs
rhs
; (env' :: LevelEnv
env', [bndr' :: Id
bndr']) <- Level -> LevelEnv -> [Id] -> [Id] -> LvlM (LevelEnv, [Id])
newPolyBndrs Level
dest_lvl LevelEnv
env [Id]
abs_vars [Id
bndr]
; let bndr2 :: Id
bndr2 = Id -> Int -> Maybe (Int, StrictSig) -> Id
annotateBotStr Id
bndr' Int
n_extra Maybe (Int, StrictSig)
mb_bot_str
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelledBndr -> LevelledExpr -> LevelledBind
forall b. b -> Expr b -> Bind b
NonRec (Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
bndr2 (Level -> FloatSpec
FloatMe Level
dest_lvl)) LevelledExpr
rhs', LevelEnv
env') }
where
bndr_ty :: Type
bndr_ty = Id -> Type
idType Id
bndr
ty_fvs :: TyCoVarSet
ty_fvs = Type -> TyCoVarSet
tyCoVarsOfType Type
bndr_ty
rhs_fvs :: FVAnn
rhs_fvs = CoreExprWithFVs -> FVAnn
freeVarsOf CoreExprWithFVs
rhs
bind_fvs :: FVAnn
bind_fvs = FVAnn
rhs_fvs FVAnn -> FVAnn -> FVAnn
`unionDVarSet` Id -> FVAnn
dIdFreeVars Id
bndr
abs_vars :: [Id]
abs_vars = Level -> LevelEnv -> FVAnn -> [Id]
abstractVars Level
dest_lvl LevelEnv
env FVAnn
bind_fvs
dest_lvl :: Level
dest_lvl = LevelEnv -> FVAnn -> TyCoVarSet -> Bool -> Bool -> Bool -> Level
destLevel LevelEnv
env FVAnn
bind_fvs TyCoVarSet
ty_fvs (CoreExprWithFVs -> Bool
isFunction CoreExprWithFVs
rhs) Bool
is_bot Bool
is_join
deann_rhs :: Expr Id
deann_rhs = CoreExprWithFVs -> Expr Id
forall bndr annot. AnnExpr bndr annot -> Expr bndr
deAnnotate CoreExprWithFVs
rhs
mb_bot_str :: Maybe (Int, StrictSig)
mb_bot_str = Expr Id -> Maybe (Int, StrictSig)
exprBotStrictness_maybe Expr Id
deann_rhs
is_bot :: Bool
is_bot = Maybe (Int, StrictSig) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Int, StrictSig)
mb_bot_str
n_extra :: Int
n_extra = (Id -> Bool) -> [Id] -> Int
forall a. (a -> Bool) -> [a] -> Int
count Id -> Bool
isId [Id]
abs_vars
mb_join_arity :: Maybe Int
mb_join_arity = Id -> Maybe Int
isJoinId_maybe Id
bndr
is_join :: Bool
is_join = Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust Maybe Int
mb_join_arity
lvlBind env :: LevelEnv
env (AnnRec pairs :: [(Id, CoreExprWithFVs)]
pairs)
| LevelEnv -> Bool
floatTopLvlOnly LevelEnv
env Bool -> Bool -> Bool
&& Bool -> Bool
not (Level -> Bool
isTopLvl Level
dest_lvl)
Bool -> Bool -> Bool
|| Bool -> Bool
not (LevelEnv -> Level -> Bool
profitableFloat LevelEnv
env Level
dest_lvl)
Bool -> Bool -> Bool
|| (Level -> Bool
isTopLvl Level
dest_lvl Bool -> Bool -> Bool
&& (Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Type -> Bool) -> (Id -> Type) -> Id -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
idType) [Id]
bndrs)
=
do { let bind_lvl :: Level
bind_lvl = Level -> Level
incMinorLvl (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
(env' :: LevelEnv
env', bndrs' :: [LevelledBndr]
bndrs') = RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs RecFlag
Recursive LevelEnv
env Level
bind_lvl [Id]
bndrs
lvl_rhs :: (Id, CoreExprWithFVs) -> LvlM LevelledExpr
lvl_rhs (b :: Id
b,r :: CoreExprWithFVs
r) = LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs LevelEnv
env' RecFlag
Recursive Bool
is_bot (Id -> Maybe Int
isJoinId_maybe Id
b) CoreExprWithFVs
r
; [LevelledExpr]
rhss' <- ((Id, CoreExprWithFVs) -> LvlM LevelledExpr)
-> [(Id, CoreExprWithFVs)] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Id, CoreExprWithFVs) -> LvlM LevelledExpr
lvl_rhs [(Id, CoreExprWithFVs)]
pairs
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec ([LevelledBndr]
bndrs' [LevelledBndr] -> [LevelledExpr] -> [(LevelledBndr, LevelledExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [LevelledExpr]
rhss'), LevelEnv
env') }
| [Id] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
abs_vars
= do { (new_env :: LevelEnv
new_env, new_bndrs :: [Id]
new_bndrs) <- RecFlag -> LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneLetVars RecFlag
Recursive LevelEnv
env Level
dest_lvl [Id]
bndrs
; [LevelledExpr]
new_rhss <- ((Id, CoreExprWithFVs) -> LvlM LevelledExpr)
-> [(Id, CoreExprWithFVs)] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LevelEnv -> (Id, CoreExprWithFVs) -> LvlM LevelledExpr
do_rhs LevelEnv
new_env) [(Id, CoreExprWithFVs)]
pairs
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ( [(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec ([Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
b (Level -> FloatSpec
FloatMe Level
dest_lvl) | Id
b <- [Id]
new_bndrs] [LevelledBndr] -> [LevelledExpr] -> [(LevelledBndr, LevelledExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [LevelledExpr]
new_rhss)
, LevelEnv
new_env) }
| [(bndr :: Id
bndr,rhs :: CoreExprWithFVs
rhs)] <- [(Id, CoreExprWithFVs)]
pairs
, (Id -> Bool) -> [Id] -> Int
forall a. (a -> Bool) -> [a] -> Int
count Id -> Bool
isId [Id]
abs_vars Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 1
= do
let (rhs_env :: LevelEnv
rhs_env, abs_vars_w_lvls :: [LevelledBndr]
abs_vars_w_lvls) = LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs LevelEnv
env Level
dest_lvl [Id]
abs_vars
rhs_lvl :: Level
rhs_lvl = LevelEnv -> Level
le_ctxt_lvl LevelEnv
rhs_env
(rhs_env' :: LevelEnv
rhs_env', [new_bndr :: Id
new_bndr]) <- RecFlag -> LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneLetVars RecFlag
Recursive LevelEnv
rhs_env Level
rhs_lvl [Id
bndr]
let
(lam_bndrs :: [Id]
lam_bndrs, rhs_body :: CoreExprWithFVs
rhs_body) = CoreExprWithFVs -> ([Id], CoreExprWithFVs)
forall bndr annot.
AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
collectAnnBndrs CoreExprWithFVs
rhs
(body_env1 :: LevelEnv
body_env1, lam_bndrs1 :: [Id]
lam_bndrs1) = RecFlag -> LevelEnv -> [Id] -> (LevelEnv, [Id])
substBndrsSL RecFlag
NonRecursive LevelEnv
rhs_env' [Id]
lam_bndrs
(body_env2 :: LevelEnv
body_env2, lam_bndrs2 :: [LevelledBndr]
lam_bndrs2) = LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs LevelEnv
body_env1 Level
rhs_lvl [Id]
lam_bndrs1
LevelledExpr
new_rhs_body <- LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs LevelEnv
body_env2 RecFlag
Recursive Bool
is_bot (Id -> Maybe Int
get_join Id
bndr) CoreExprWithFVs
rhs_body
(poly_env :: LevelEnv
poly_env, [poly_bndr :: Id
poly_bndr]) <- Level -> LevelEnv -> [Id] -> [Id] -> LvlM (LevelEnv, [Id])
newPolyBndrs Level
dest_lvl LevelEnv
env [Id]
abs_vars [Id
bndr]
(LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ([(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
poly_bndr (Level -> FloatSpec
FloatMe Level
dest_lvl)
, [LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
abs_vars_w_lvls (LevelledExpr -> LevelledExpr) -> LevelledExpr -> LevelledExpr
forall a b. (a -> b) -> a -> b
$
[LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
lam_bndrs2 (LevelledExpr -> LevelledExpr) -> LevelledExpr -> LevelledExpr
forall a b. (a -> b) -> a -> b
$
LevelledBind -> LevelledExpr -> LevelledExpr
forall b. Bind b -> Expr b -> Expr b
Let ([(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec [( Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
new_bndr (Level -> FloatSpec
StayPut Level
rhs_lvl)
, [LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
lam_bndrs2 LevelledExpr
new_rhs_body)])
(LevelledExpr -> [Id] -> LevelledExpr
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
new_bndr) [Id]
lam_bndrs1))]
, LevelEnv
poly_env)
| Bool
otherwise
= do { (new_env :: LevelEnv
new_env, new_bndrs :: [Id]
new_bndrs) <- Level -> LevelEnv -> [Id] -> [Id] -> LvlM (LevelEnv, [Id])
newPolyBndrs Level
dest_lvl LevelEnv
env [Id]
abs_vars [Id]
bndrs
; [LevelledExpr]
new_rhss <- ((Id, CoreExprWithFVs) -> LvlM LevelledExpr)
-> [(Id, CoreExprWithFVs)] -> UniqSM [LevelledExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LevelEnv -> (Id, CoreExprWithFVs) -> LvlM LevelledExpr
do_rhs LevelEnv
new_env) [(Id, CoreExprWithFVs)]
pairs
; (LevelledBind, LevelEnv) -> LvlM (LevelledBind, LevelEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ( [(LevelledBndr, LevelledExpr)] -> LevelledBind
forall b. [(b, Expr b)] -> Bind b
Rec ([Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
b (Level -> FloatSpec
FloatMe Level
dest_lvl) | Id
b <- [Id]
new_bndrs] [LevelledBndr] -> [LevelledExpr] -> [(LevelledBndr, LevelledExpr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [LevelledExpr]
new_rhss)
, LevelEnv
new_env) }
where
(bndrs :: [Id]
bndrs,rhss :: [CoreExprWithFVs]
rhss) = [(Id, CoreExprWithFVs)] -> ([Id], [CoreExprWithFVs])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, CoreExprWithFVs)]
pairs
is_join :: Bool
is_join = Id -> Bool
isJoinId ([Id] -> Id
forall a. [a] -> a
head [Id]
bndrs)
is_fun :: Bool
is_fun = (CoreExprWithFVs -> Bool) -> [CoreExprWithFVs] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all CoreExprWithFVs -> Bool
isFunction [CoreExprWithFVs]
rhss
is_bot :: Bool
is_bot = Bool
False
do_rhs :: LevelEnv -> (Id, CoreExprWithFVs) -> LvlM LevelledExpr
do_rhs env :: LevelEnv
env (bndr :: Id
bndr,rhs :: CoreExprWithFVs
rhs) = [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs [Id]
abs_vars Level
dest_lvl LevelEnv
env RecFlag
Recursive
Bool
is_bot (Id -> Maybe Int
get_join Id
bndr)
CoreExprWithFVs
rhs
get_join :: Id -> Maybe Int
get_join bndr :: Id
bndr | Bool
need_zap = Maybe Int
forall a. Maybe a
Nothing
| Bool
otherwise = Id -> Maybe Int
isJoinId_maybe Id
bndr
need_zap :: Bool
need_zap = Level
dest_lvl Level -> Level -> Bool
`ltLvl` LevelEnv -> Level
joinCeilingLevel LevelEnv
env
bind_fvs :: FVAnn
bind_fvs = (([FVAnn] -> FVAnn
unionDVarSets [ CoreExprWithFVs -> FVAnn
freeVarsOf CoreExprWithFVs
rhs | (_, rhs :: CoreExprWithFVs
rhs) <- [(Id, CoreExprWithFVs)]
pairs])
FVAnn -> FVAnn -> FVAnn
`unionDVarSet`
(FV -> FVAnn
fvDVarSet (FV -> FVAnn) -> FV -> FVAnn
forall a b. (a -> b) -> a -> b
$ [FV] -> FV
unionsFV [ Id -> FV
idFVs Id
bndr
| (bndr :: Id
bndr, (_,_)) <- [(Id, CoreExprWithFVs)]
pairs]))
FVAnn -> [Id] -> FVAnn
`delDVarSetList`
[Id]
bndrs
ty_fvs :: TyCoVarSet
ty_fvs = (Id -> TyCoVarSet -> TyCoVarSet)
-> TyCoVarSet -> [Id] -> TyCoVarSet
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (TyCoVarSet -> TyCoVarSet -> TyCoVarSet
unionVarSet (TyCoVarSet -> TyCoVarSet -> TyCoVarSet)
-> (Id -> TyCoVarSet) -> Id -> TyCoVarSet -> TyCoVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> TyCoVarSet
tyCoVarsOfType (Type -> TyCoVarSet) -> (Id -> Type) -> Id -> TyCoVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Type
idType) TyCoVarSet
emptyVarSet [Id]
bndrs
dest_lvl :: Level
dest_lvl = LevelEnv -> FVAnn -> TyCoVarSet -> Bool -> Bool -> Bool -> Level
destLevel LevelEnv
env FVAnn
bind_fvs TyCoVarSet
ty_fvs Bool
is_fun Bool
is_bot Bool
is_join
abs_vars :: [Id]
abs_vars = Level -> LevelEnv -> FVAnn -> [Id]
abstractVars Level
dest_lvl LevelEnv
env FVAnn
bind_fvs
profitableFloat :: LevelEnv -> Level -> Bool
profitableFloat :: LevelEnv -> Level -> Bool
profitableFloat env :: LevelEnv
env dest_lvl :: Level
dest_lvl
= (Level
dest_lvl Level -> Level -> Bool
`ltMajLvl` LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
Bool -> Bool -> Bool
|| Level -> Bool
isTopLvl Level
dest_lvl
lvlRhs :: LevelEnv
-> RecFlag
-> Bool
-> Maybe JoinArity
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs :: LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlRhs env :: LevelEnv
env rec_flag :: RecFlag
rec_flag is_bot :: Bool
is_bot mb_join_arity :: Maybe Int
mb_join_arity expr :: CoreExprWithFVs
expr
= [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs [] (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env) LevelEnv
env
RecFlag
rec_flag Bool
is_bot Maybe Int
mb_join_arity CoreExprWithFVs
expr
lvlFloatRhs :: [OutVar] -> Level -> LevelEnv -> RecFlag
-> Bool
-> Maybe JoinArity
-> CoreExprWithFVs
-> LvlM (Expr LevelledBndr)
lvlFloatRhs :: [Id]
-> Level
-> LevelEnv
-> RecFlag
-> Bool
-> Maybe Int
-> CoreExprWithFVs
-> LvlM LevelledExpr
lvlFloatRhs abs_vars :: [Id]
abs_vars dest_lvl :: Level
dest_lvl env :: LevelEnv
env rec :: RecFlag
rec is_bot :: Bool
is_bot mb_join_arity :: Maybe Int
mb_join_arity rhs :: CoreExprWithFVs
rhs
= do { LevelledExpr
body' <- if Bool -> Bool
not Bool
is_bot
Bool -> Bool -> Bool
&& (Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Id -> Bool
isId [Id]
bndrs
then LevelEnv -> Bool -> CoreExprWithFVs -> LvlM LevelledExpr
lvlMFE LevelEnv
body_env Bool
True CoreExprWithFVs
body
else LevelEnv -> CoreExprWithFVs -> LvlM LevelledExpr
lvlExpr LevelEnv
body_env CoreExprWithFVs
body
; LevelledExpr -> LvlM LevelledExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([LevelledBndr] -> LevelledExpr -> LevelledExpr
forall b. [b] -> Expr b -> Expr b
mkLams [LevelledBndr]
bndrs' LevelledExpr
body') }
where
(bndrs :: [Id]
bndrs, body :: CoreExprWithFVs
body) | Just join_arity :: Int
join_arity <- Maybe Int
mb_join_arity
= Int -> CoreExprWithFVs -> ([Id], CoreExprWithFVs)
forall bndr annot.
Int -> AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
collectNAnnBndrs Int
join_arity CoreExprWithFVs
rhs
| Bool
otherwise
= CoreExprWithFVs -> ([Id], CoreExprWithFVs)
forall bndr annot.
AnnExpr bndr annot -> ([bndr], AnnExpr bndr annot)
collectAnnBndrs CoreExprWithFVs
rhs
(env1 :: LevelEnv
env1, bndrs1 :: [Id]
bndrs1) = RecFlag -> LevelEnv -> [Id] -> (LevelEnv, [Id])
substBndrsSL RecFlag
NonRecursive LevelEnv
env [Id]
bndrs
all_bndrs :: [Id]
all_bndrs = [Id]
abs_vars [Id] -> [Id] -> [Id]
forall a. [a] -> [a] -> [a]
++ [Id]
bndrs1
(body_env :: LevelEnv
body_env, bndrs' :: [LevelledBndr]
bndrs') | Just _ <- Maybe Int
mb_join_arity
= LevelEnv -> Level -> RecFlag -> [Id] -> (LevelEnv, [LevelledBndr])
lvlJoinBndrs LevelEnv
env1 Level
dest_lvl RecFlag
rec [Id]
all_bndrs
| Bool
otherwise
= case LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs LevelEnv
env1 Level
dest_lvl [Id]
all_bndrs of
(env2 :: LevelEnv
env2, bndrs' :: [LevelledBndr]
bndrs') -> (LevelEnv -> LevelEnv
placeJoinCeiling LevelEnv
env2, [LevelledBndr]
bndrs')
substAndLvlBndrs :: RecFlag -> LevelEnv -> Level -> [InVar] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs :: RecFlag -> LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
substAndLvlBndrs is_rec :: RecFlag
is_rec env :: LevelEnv
env lvl :: Level
lvl bndrs :: [Id]
bndrs
= LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlBndrs LevelEnv
subst_env Level
lvl [Id]
subst_bndrs
where
(subst_env :: LevelEnv
subst_env, subst_bndrs :: [Id]
subst_bndrs) = RecFlag -> LevelEnv -> [Id] -> (LevelEnv, [Id])
substBndrsSL RecFlag
is_rec LevelEnv
env [Id]
bndrs
substBndrsSL :: RecFlag -> LevelEnv -> [InVar] -> (LevelEnv, [OutVar])
is_rec :: RecFlag
is_rec env :: LevelEnv
env@(LE { le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env }) bndrs :: [Id]
bndrs
= ( LevelEnv
env { le_subst :: Subst
le_subst = Subst
subst'
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = (IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr))
-> IdEnv ([Id], LevelledExpr)
-> [(Id, Id)]
-> IdEnv ([Id], LevelledExpr)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
add_id IdEnv ([Id], LevelledExpr)
id_env ([Id]
bndrs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
bndrs') }
, [Id]
bndrs')
where
(subst' :: Subst
subst', bndrs' :: [Id]
bndrs') = case RecFlag
is_rec of
NonRecursive -> Subst -> [Id] -> (Subst, [Id])
substBndrs Subst
subst [Id]
bndrs
Recursive -> Subst -> [Id] -> (Subst, [Id])
substRecBndrs Subst
subst [Id]
bndrs
lvlLamBndrs :: LevelEnv -> Level -> [OutVar] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs :: LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlLamBndrs env :: LevelEnv
env lvl :: Level
lvl bndrs :: [Id]
bndrs
= LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlBndrs LevelEnv
env Level
new_lvl [Id]
bndrs
where
new_lvl :: Level
new_lvl | (Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Id -> Bool
is_major [Id]
bndrs = Level -> Level
incMajorLvl Level
lvl
| Bool
otherwise = Level -> Level
incMinorLvl Level
lvl
is_major :: Id -> Bool
is_major bndr :: Id
bndr = Id -> Bool
isId Id
bndr Bool -> Bool -> Bool
&& Bool -> Bool
not (Id -> Bool
isProbablyOneShotLambda Id
bndr)
lvlJoinBndrs :: LevelEnv -> Level -> RecFlag -> [OutVar]
-> (LevelEnv, [LevelledBndr])
lvlJoinBndrs :: LevelEnv -> Level -> RecFlag -> [Id] -> (LevelEnv, [LevelledBndr])
lvlJoinBndrs env :: LevelEnv
env lvl :: Level
lvl rec :: RecFlag
rec bndrs :: [Id]
bndrs
= LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlBndrs LevelEnv
env Level
new_lvl [Id]
bndrs
where
new_lvl :: Level
new_lvl | RecFlag -> Bool
isRec RecFlag
rec = Level -> Level
incMajorLvl Level
lvl
| Bool
otherwise = Level -> Level
incMinorLvl Level
lvl
lvlBndrs :: LevelEnv -> Level -> [CoreBndr] -> (LevelEnv, [LevelledBndr])
lvlBndrs :: LevelEnv -> Level -> [Id] -> (LevelEnv, [LevelledBndr])
lvlBndrs env :: LevelEnv
env@(LE { le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env }) new_lvl :: Level
new_lvl bndrs :: [Id]
bndrs
= ( LevelEnv
env { le_ctxt_lvl :: Level
le_ctxt_lvl = Level
new_lvl
, le_join_ceil :: Level
le_join_ceil = Level
new_lvl
, le_lvl_env :: VarEnv Level
le_lvl_env = Level -> VarEnv Level -> [Id] -> VarEnv Level
addLvls Level
new_lvl VarEnv Level
lvl_env [Id]
bndrs }
, (Id -> LevelledBndr) -> [Id] -> [LevelledBndr]
forall a b. (a -> b) -> [a] -> [b]
map (Level -> Id -> LevelledBndr
stayPut Level
new_lvl) [Id]
bndrs)
stayPut :: Level -> OutVar -> LevelledBndr
stayPut :: Level -> Id -> LevelledBndr
stayPut new_lvl :: Level
new_lvl bndr :: Id
bndr = Id -> FloatSpec -> LevelledBndr
forall t. Id -> t -> TaggedBndr t
TB Id
bndr (Level -> FloatSpec
StayPut Level
new_lvl)
destLevel :: LevelEnv
-> DVarSet
-> TyCoVarSet
-> Bool
-> Bool
-> Bool
-> Level
destLevel :: LevelEnv -> FVAnn -> TyCoVarSet -> Bool -> Bool -> Bool -> Level
destLevel env :: LevelEnv
env fvs :: FVAnn
fvs fvs_ty :: TyCoVarSet
fvs_ty is_function :: Bool
is_function is_bot :: Bool
is_bot is_join :: Bool
is_join
| Level -> Bool
isTopLvl Level
max_fv_id_level
= Level
tOP_LEVEL
| Bool
is_join
= if Level
max_fv_id_level Level -> Level -> Bool
`ltLvl` Level
join_ceiling
then Level
join_ceiling
else Level
max_fv_id_level
| Bool
is_bot
= Level
as_far_as_poss
| Just n_args :: Int
n_args <- LevelEnv -> Maybe Int
floatLams LevelEnv
env
, Int
n_args Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0
, Bool
is_function
, FVAnn -> Int
countFreeIds FVAnn
fvs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n_args
= Level
as_far_as_poss
| Bool
otherwise = Level
max_fv_id_level
where
join_ceiling :: Level
join_ceiling = LevelEnv -> Level
joinCeilingLevel LevelEnv
env
max_fv_id_level :: Level
max_fv_id_level = (Id -> Bool) -> LevelEnv -> FVAnn -> Level
maxFvLevel Id -> Bool
isId LevelEnv
env FVAnn
fvs
as_far_as_poss :: Level
as_far_as_poss = (Id -> Bool) -> LevelEnv -> TyCoVarSet -> Level
maxFvLevel' Id -> Bool
isId LevelEnv
env TyCoVarSet
fvs_ty
isFunction :: CoreExprWithFVs -> Bool
isFunction :: CoreExprWithFVs -> Bool
isFunction (_, AnnLam b :: Id
b e :: CoreExprWithFVs
e) | Id -> Bool
isId Id
b = Bool
True
| Bool
otherwise = CoreExprWithFVs -> Bool
isFunction CoreExprWithFVs
e
isFunction _ = Bool
False
countFreeIds :: DVarSet -> Int
countFreeIds :: FVAnn -> Int
countFreeIds = (Id -> Int -> Int) -> Int -> UniqDFM Id -> Int
forall elt a. (elt -> a -> a) -> a -> UniqDFM elt -> a
nonDetFoldUDFM Id -> Int -> Int
add 0 (UniqDFM Id -> Int) -> (FVAnn -> UniqDFM Id) -> FVAnn -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FVAnn -> UniqDFM Id
forall a. UniqDSet a -> UniqDFM a
getUniqDSet
where
add :: Var -> Int -> Int
add :: Id -> Int -> Int
add v :: Id
v n :: Int
n | Id -> Bool
isId Id
v = Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1
| Bool
otherwise = Int
n
data LevelEnv
= LE { LevelEnv -> FloatOutSwitches
le_switches :: FloatOutSwitches
, LevelEnv -> Level
le_ctxt_lvl :: Level
, LevelEnv -> VarEnv Level
le_lvl_env :: VarEnv Level
, LevelEnv -> Level
le_join_ceil:: Level
, LevelEnv -> Subst
le_subst :: Subst
, LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env :: IdEnv ([OutVar], LevelledExpr)
}
initialEnv :: FloatOutSwitches -> LevelEnv
initialEnv :: FloatOutSwitches -> LevelEnv
initialEnv float_lams :: FloatOutSwitches
float_lams
= LE :: FloatOutSwitches
-> Level
-> VarEnv Level
-> Level
-> Subst
-> IdEnv ([Id], LevelledExpr)
-> LevelEnv
LE { le_switches :: FloatOutSwitches
le_switches = FloatOutSwitches
float_lams
, le_ctxt_lvl :: Level
le_ctxt_lvl = Level
tOP_LEVEL
, le_join_ceil :: Level
le_join_ceil = String -> Level
forall a. String -> a
panic "initialEnv"
, le_lvl_env :: VarEnv Level
le_lvl_env = VarEnv Level
forall a. VarEnv a
emptyVarEnv
, le_subst :: Subst
le_subst = Subst
emptySubst
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
forall a. VarEnv a
emptyVarEnv }
addLvl :: Level -> VarEnv Level -> OutVar -> VarEnv Level
addLvl :: Level -> VarEnv Level -> Id -> VarEnv Level
addLvl dest_lvl :: Level
dest_lvl env :: VarEnv Level
env v' :: Id
v' = VarEnv Level -> Id -> Level -> VarEnv Level
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv VarEnv Level
env Id
v' Level
dest_lvl
addLvls :: Level -> VarEnv Level -> [OutVar] -> VarEnv Level
addLvls :: Level -> VarEnv Level -> [Id] -> VarEnv Level
addLvls dest_lvl :: Level
dest_lvl env :: VarEnv Level
env vs :: [Id]
vs = (VarEnv Level -> Id -> VarEnv Level)
-> VarEnv Level -> [Id] -> VarEnv Level
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Level -> VarEnv Level -> Id -> VarEnv Level
addLvl Level
dest_lvl) VarEnv Level
env [Id]
vs
floatLams :: LevelEnv -> Maybe Int
floatLams :: LevelEnv -> Maybe Int
floatLams le :: LevelEnv
le = FloatOutSwitches -> Maybe Int
floatOutLambdas (LevelEnv -> FloatOutSwitches
le_switches LevelEnv
le)
floatConsts :: LevelEnv -> Bool
floatConsts :: LevelEnv -> Bool
floatConsts le :: LevelEnv
le = FloatOutSwitches -> Bool
floatOutConstants (LevelEnv -> FloatOutSwitches
le_switches LevelEnv
le)
floatOverSat :: LevelEnv -> Bool
floatOverSat :: LevelEnv -> Bool
floatOverSat le :: LevelEnv
le = FloatOutSwitches -> Bool
floatOutOverSatApps (LevelEnv -> FloatOutSwitches
le_switches LevelEnv
le)
floatTopLvlOnly :: LevelEnv -> Bool
floatTopLvlOnly :: LevelEnv -> Bool
floatTopLvlOnly le :: LevelEnv
le = FloatOutSwitches -> Bool
floatToTopLevelOnly (LevelEnv -> FloatOutSwitches
le_switches LevelEnv
le)
incMinorLvlFrom :: LevelEnv -> Level
incMinorLvlFrom :: LevelEnv -> Level
incMinorLvlFrom env :: LevelEnv
env = Level -> Level
incMinorLvl (LevelEnv -> Level
le_ctxt_lvl LevelEnv
env)
extendCaseBndrEnv :: LevelEnv
-> Id
-> Expr LevelledBndr
-> LevelEnv
extendCaseBndrEnv :: LevelEnv -> Id -> LevelledExpr -> LevelEnv
extendCaseBndrEnv le :: LevelEnv
le@(LE { le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env })
case_bndr :: Id
case_bndr (Var scrut_var :: Id
scrut_var)
= LevelEnv
le { le_subst :: Subst
le_subst = Subst -> Id -> Id -> Subst
extendSubstWithVar Subst
subst Id
case_bndr Id
scrut_var
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
add_id IdEnv ([Id], LevelledExpr)
id_env (Id
case_bndr, Id
scrut_var) }
extendCaseBndrEnv env :: LevelEnv
env _ _ = LevelEnv
env
placeJoinCeiling :: LevelEnv -> LevelEnv
placeJoinCeiling :: LevelEnv -> LevelEnv
placeJoinCeiling le :: LevelEnv
le@(LE { le_ctxt_lvl :: LevelEnv -> Level
le_ctxt_lvl = Level
lvl })
= LevelEnv
le { le_ctxt_lvl :: Level
le_ctxt_lvl = Level
lvl', le_join_ceil :: Level
le_join_ceil = Level
lvl' }
where
lvl' :: Level
lvl' = Level -> Level
asJoinCeilLvl (Level -> Level
incMinorLvl Level
lvl)
maxFvLevel :: (Var -> Bool) -> LevelEnv -> DVarSet -> Level
maxFvLevel :: (Id -> Bool) -> LevelEnv -> FVAnn -> Level
maxFvLevel max_me :: Id -> Bool
max_me env :: LevelEnv
env var_set :: FVAnn
var_set
= (Id -> Level -> Level) -> Level -> FVAnn -> Level
forall a. (Id -> a -> a) -> a -> FVAnn -> a
foldDVarSet ((Id -> Bool) -> LevelEnv -> Id -> Level -> Level
maxIn Id -> Bool
max_me LevelEnv
env) Level
tOP_LEVEL FVAnn
var_set
maxFvLevel' :: (Var -> Bool) -> LevelEnv -> TyCoVarSet -> Level
maxFvLevel' :: (Id -> Bool) -> LevelEnv -> TyCoVarSet -> Level
maxFvLevel' max_me :: Id -> Bool
max_me env :: LevelEnv
env var_set :: TyCoVarSet
var_set
= (Id -> Level -> Level) -> Level -> TyCoVarSet -> Level
forall elt a. (elt -> a -> a) -> a -> UniqSet elt -> a
nonDetFoldUniqSet ((Id -> Bool) -> LevelEnv -> Id -> Level -> Level
maxIn Id -> Bool
max_me LevelEnv
env) Level
tOP_LEVEL TyCoVarSet
var_set
maxIn :: (Var -> Bool) -> LevelEnv -> InVar -> Level -> Level
maxIn :: (Id -> Bool) -> LevelEnv -> Id -> Level -> Level
maxIn max_me :: Id -> Bool
max_me (LE { le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env }) in_var :: Id
in_var lvl :: Level
lvl
= case IdEnv ([Id], LevelledExpr) -> Id -> Maybe ([Id], LevelledExpr)
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv IdEnv ([Id], LevelledExpr)
id_env Id
in_var of
Just (abs_vars :: [Id]
abs_vars, _) -> (Id -> Level -> Level) -> Level -> [Id] -> Level
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Id -> Level -> Level
max_out Level
lvl [Id]
abs_vars
Nothing -> Id -> Level -> Level
max_out Id
in_var Level
lvl
where
max_out :: Id -> Level -> Level
max_out out_var :: Id
out_var lvl :: Level
lvl
| Id -> Bool
max_me Id
out_var = case VarEnv Level -> Id -> Maybe Level
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv VarEnv Level
lvl_env Id
out_var of
Just lvl' :: Level
lvl' -> Level -> Level -> Level
maxLvl Level
lvl' Level
lvl
Nothing -> Level
lvl
| Bool
otherwise = Level
lvl
lookupVar :: LevelEnv -> Id -> LevelledExpr
lookupVar :: LevelEnv -> Id -> LevelledExpr
lookupVar le :: LevelEnv
le v :: Id
v = case IdEnv ([Id], LevelledExpr) -> Id -> Maybe ([Id], LevelledExpr)
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv (LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env LevelEnv
le) Id
v of
Just (_, expr :: LevelledExpr
expr) -> LevelledExpr
expr
_ -> Id -> LevelledExpr
forall b. Id -> Expr b
Var Id
v
joinCeilingLevel :: LevelEnv -> Level
joinCeilingLevel :: LevelEnv -> Level
joinCeilingLevel = LevelEnv -> Level
le_join_ceil
abstractVars :: Level -> LevelEnv -> DVarSet -> [OutVar]
abstractVars :: Level -> LevelEnv -> FVAnn -> [Id]
abstractVars dest_lvl :: Level
dest_lvl (LE { le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env }) in_fvs :: FVAnn
in_fvs
=
(Id -> Id) -> [Id] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Id
zap ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$ [Id] -> [Id]
sortQuantVars ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$
(Id -> Bool) -> [Id] -> [Id]
forall a. (a -> Bool) -> [a] -> [a]
filter Id -> Bool
abstract_me ([Id] -> [Id]) -> [Id] -> [Id]
forall a b. (a -> b) -> a -> b
$
FVAnn -> [Id]
dVarSetElems (FVAnn -> [Id]) -> FVAnn -> [Id]
forall a b. (a -> b) -> a -> b
$
FVAnn -> FVAnn
closeOverKindsDSet (FVAnn -> FVAnn) -> FVAnn -> FVAnn
forall a b. (a -> b) -> a -> b
$
Subst -> FVAnn -> FVAnn
substDVarSet Subst
subst FVAnn
in_fvs
where
abstract_me :: Id -> Bool
abstract_me v :: Id
v = case VarEnv Level -> Id -> Maybe Level
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv VarEnv Level
lvl_env Id
v of
Just lvl :: Level
lvl -> Level
dest_lvl Level -> Level -> Bool
`ltLvl` Level
lvl
Nothing -> Bool
False
zap :: Id -> Id
zap v :: Id
v | Id -> Bool
isId Id
v = WARN( isStableUnfolding (idUnfolding v) ||
not (isEmptyRuleInfo (idSpecialisation v)),
text "absVarsOf: discarding info on" <+> ppr v )
Id -> IdInfo -> Id
setIdInfo Id
v IdInfo
vanillaIdInfo
| Bool
otherwise = Id
v
type LvlM result = UniqSM result
initLvl :: UniqSupply -> UniqSM a -> a
initLvl :: UniqSupply -> UniqSM a -> a
initLvl = UniqSupply -> UniqSM a -> a
forall a. UniqSupply -> UniqSM a -> a
initUs_
newPolyBndrs :: Level -> LevelEnv -> [OutVar] -> [InId]
-> LvlM (LevelEnv, [OutId])
newPolyBndrs :: Level -> LevelEnv -> [Id] -> [Id] -> LvlM (LevelEnv, [Id])
newPolyBndrs dest_lvl :: Level
dest_lvl
env :: LevelEnv
env@(LE { le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env, le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env })
abs_vars :: [Id]
abs_vars bndrs :: [Id]
bndrs
= ASSERT( all (not . isCoVar) bndrs )
do { [Unique]
uniqs <- UniqSM [Unique]
forall (m :: * -> *). MonadUnique m => m [Unique]
getUniquesM
; let new_bndrs :: [Id]
new_bndrs = (Id -> Unique -> Id) -> [Id] -> [Unique] -> [Id]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Id -> Unique -> Id
mk_poly_bndr [Id]
bndrs [Unique]
uniqs
bndr_prs :: [(Id, Id)]
bndr_prs = [Id]
bndrs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
new_bndrs
env' :: LevelEnv
env' = LevelEnv
env { le_lvl_env :: VarEnv Level
le_lvl_env = Level -> VarEnv Level -> [Id] -> VarEnv Level
addLvls Level
dest_lvl VarEnv Level
lvl_env [Id]
new_bndrs
, le_subst :: Subst
le_subst = (Subst -> (Id, Id) -> Subst) -> Subst -> [(Id, Id)] -> Subst
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Subst -> (Id, Id) -> Subst
add_subst Subst
subst [(Id, Id)]
bndr_prs
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = (IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr))
-> IdEnv ([Id], LevelledExpr)
-> [(Id, Id)]
-> IdEnv ([Id], LevelledExpr)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
forall b.
VarEnv ([Id], Expr b) -> (Id, Id) -> VarEnv ([Id], Expr b)
add_id IdEnv ([Id], LevelledExpr)
id_env [(Id, Id)]
bndr_prs }
; (LevelEnv, [Id]) -> LvlM (LevelEnv, [Id])
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelEnv
env', [Id]
new_bndrs) }
where
add_subst :: Subst -> (Id, Id) -> Subst
add_subst env :: Subst
env (v :: Id
v, v' :: Id
v') = Subst -> Id -> Expr Id -> Subst
extendIdSubst Subst
env Id
v (Expr Id -> [Id] -> Expr Id
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> Expr Id
forall b. Id -> Expr b
Var Id
v') [Id]
abs_vars)
add_id :: VarEnv ([Id], Expr b) -> (Id, Id) -> VarEnv ([Id], Expr b)
add_id env :: VarEnv ([Id], Expr b)
env (v :: Id
v, v' :: Id
v') = VarEnv ([Id], Expr b)
-> Id -> ([Id], Expr b) -> VarEnv ([Id], Expr b)
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv VarEnv ([Id], Expr b)
env Id
v ((Id
v'Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
:[Id]
abs_vars), Expr b -> [Id] -> Expr b
forall b. Expr b -> [Id] -> Expr b
mkVarApps (Id -> Expr b
forall b. Id -> Expr b
Var Id
v') [Id]
abs_vars)
mk_poly_bndr :: Id -> Unique -> Id
mk_poly_bndr bndr :: Id
bndr uniq :: Unique
uniq = Id -> [Id] -> Id -> Id
transferPolyIdInfo Id
bndr [Id]
abs_vars (Id -> Id) -> Id -> Id
forall a b. (a -> b) -> a -> b
$
Id -> Id -> Id
transfer_join_info Id
bndr (Id -> Id) -> Id -> Id
forall a b. (a -> b) -> a -> b
$
FastString -> Unique -> Type -> Id
mkSysLocalOrCoVar (String -> FastString
mkFastString String
str) Unique
uniq Type
poly_ty
where
str :: String
str = "poly_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ OccName -> String
occNameString (Id -> OccName
forall a. NamedThing a => a -> OccName
getOccName Id
bndr)
poly_ty :: Type
poly_ty = [Id] -> Type -> Type
mkLamTypes [Id]
abs_vars (Subst -> Type -> Type
CoreSubst.substTy Subst
subst (Id -> Type
idType Id
bndr))
dest_is_top :: Bool
dest_is_top = Level -> Bool
isTopLvl Level
dest_lvl
transfer_join_info :: Id -> Id -> Id
transfer_join_info bndr :: Id
bndr new_bndr :: Id
new_bndr
| Just join_arity :: Int
join_arity <- Id -> Maybe Int
isJoinId_maybe Id
bndr
, Bool -> Bool
not Bool
dest_is_top
= Id
new_bndr Id -> Int -> Id
`asJoinId` Int
join_arity Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Id] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
abs_vars
| Bool
otherwise
= Id
new_bndr
newLvlVar :: LevelledExpr
-> Maybe JoinArity
-> Bool
-> LvlM Id
newLvlVar :: LevelledExpr -> Maybe Int -> Bool -> LvlM Id
newLvlVar lvld_rhs :: LevelledExpr
lvld_rhs join_arity_maybe :: Maybe Int
join_arity_maybe is_mk_static :: Bool
is_mk_static
= do { Unique
uniq <- UniqSM Unique
forall (m :: * -> *). MonadUnique m => m Unique
getUniqueM
; Id -> LvlM Id
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Id
add_join_info (Unique -> Type -> Id
mk_id Unique
uniq Type
rhs_ty))
}
where
add_join_info :: Id -> Id
add_join_info var :: Id
var = Id
var Id -> Maybe Int -> Id
`asJoinId_maybe` Maybe Int
join_arity_maybe
de_tagged_rhs :: Expr Id
de_tagged_rhs = LevelledExpr -> Expr Id
forall t. TaggedExpr t -> Expr Id
deTagExpr LevelledExpr
lvld_rhs
rhs_ty :: Type
rhs_ty = Expr Id -> Type
exprType Expr Id
de_tagged_rhs
mk_id :: Unique -> Type -> Id
mk_id uniq :: Unique
uniq rhs_ty :: Type
rhs_ty
| Bool
is_mk_static
= Name -> Type -> Id
mkExportedVanillaId (Unique -> FastString -> Name
mkSystemVarName Unique
uniq (String -> FastString
mkFastString "static_ptr"))
Type
rhs_ty
| Bool
otherwise
= FastString -> Unique -> Type -> Id
mkSysLocalOrCoVar (String -> FastString
mkFastString "lvl") Unique
uniq Type
rhs_ty
cloneCaseBndrs :: LevelEnv -> Level -> [Var] -> LvlM (LevelEnv, [Var])
cloneCaseBndrs :: LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneCaseBndrs env :: LevelEnv
env@(LE { le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env })
new_lvl :: Level
new_lvl vs :: [Id]
vs
= do { UniqSupply
us <- UniqSM UniqSupply
forall (m :: * -> *). MonadUnique m => m UniqSupply
getUniqueSupplyM
; let (subst' :: Subst
subst', vs' :: [Id]
vs') = Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneBndrs Subst
subst UniqSupply
us [Id]
vs
env' :: LevelEnv
env' = LevelEnv
env { le_lvl_env :: VarEnv Level
le_lvl_env = Level -> VarEnv Level -> [Id] -> VarEnv Level
addLvls Level
new_lvl VarEnv Level
lvl_env [Id]
vs'
, le_subst :: Subst
le_subst = Subst
subst'
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = (IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr))
-> IdEnv ([Id], LevelledExpr)
-> [(Id, Id)]
-> IdEnv ([Id], LevelledExpr)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
add_id IdEnv ([Id], LevelledExpr)
id_env ([Id]
vs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
vs') }
; (LevelEnv, [Id]) -> LvlM (LevelEnv, [Id])
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelEnv
env', [Id]
vs') }
cloneLetVars :: RecFlag -> LevelEnv -> Level -> [InVar]
-> LvlM (LevelEnv, [OutVar])
cloneLetVars :: RecFlag -> LevelEnv -> Level -> [Id] -> LvlM (LevelEnv, [Id])
cloneLetVars is_rec :: RecFlag
is_rec
env :: LevelEnv
env@(LE { le_subst :: LevelEnv -> Subst
le_subst = Subst
subst, le_lvl_env :: LevelEnv -> VarEnv Level
le_lvl_env = VarEnv Level
lvl_env, le_env :: LevelEnv -> IdEnv ([Id], LevelledExpr)
le_env = IdEnv ([Id], LevelledExpr)
id_env })
dest_lvl :: Level
dest_lvl vs :: [Id]
vs
= do { UniqSupply
us <- UniqSM UniqSupply
forall (m :: * -> *). MonadUnique m => m UniqSupply
getUniqueSupplyM
; let vs1 :: [Id]
vs1 = (Id -> Id) -> [Id] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map Id -> Id
zap [Id]
vs
(subst' :: Subst
subst', vs2 :: [Id]
vs2) = case RecFlag
is_rec of
NonRecursive -> Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneBndrs Subst
subst UniqSupply
us [Id]
vs1
Recursive -> Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneRecIdBndrs Subst
subst UniqSupply
us [Id]
vs1
prs :: [(Id, Id)]
prs = [Id]
vs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
vs2
env' :: LevelEnv
env' = LevelEnv
env { le_lvl_env :: VarEnv Level
le_lvl_env = Level -> VarEnv Level -> [Id] -> VarEnv Level
addLvls Level
dest_lvl VarEnv Level
lvl_env [Id]
vs2
, le_subst :: Subst
le_subst = Subst
subst'
, le_env :: IdEnv ([Id], LevelledExpr)
le_env = (IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr))
-> IdEnv ([Id], LevelledExpr)
-> [(Id, Id)]
-> IdEnv ([Id], LevelledExpr)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
add_id IdEnv ([Id], LevelledExpr)
id_env [(Id, Id)]
prs }
; (LevelEnv, [Id]) -> LvlM (LevelEnv, [Id])
forall (m :: * -> *) a. Monad m => a -> m a
return (LevelEnv
env', [Id]
vs2) }
where
zap :: Var -> Var
zap :: Id -> Id
zap v :: Id
v | Id -> Bool
isId Id
v = Id -> Id
zap_join (Id -> Id
zapIdDemandInfo Id
v)
| Bool
otherwise = Id
v
zap_join :: Id -> Id
zap_join | Level -> Bool
isTopLvl Level
dest_lvl = Id -> Id
zapJoinId
| Bool
otherwise = Id -> Id
forall a. a -> a
id
add_id :: IdEnv ([Var], LevelledExpr) -> (Var, Var) -> IdEnv ([Var], LevelledExpr)
add_id :: IdEnv ([Id], LevelledExpr)
-> (Id, Id) -> IdEnv ([Id], LevelledExpr)
add_id id_env :: IdEnv ([Id], LevelledExpr)
id_env (v :: Id
v, v1 :: Id
v1)
| Id -> Bool
isTyVar Id
v = IdEnv ([Id], LevelledExpr) -> Id -> IdEnv ([Id], LevelledExpr)
forall a. VarEnv a -> Id -> VarEnv a
delVarEnv IdEnv ([Id], LevelledExpr)
id_env Id
v
| Bool
otherwise = IdEnv ([Id], LevelledExpr)
-> Id -> ([Id], LevelledExpr) -> IdEnv ([Id], LevelledExpr)
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv IdEnv ([Id], LevelledExpr)
id_env Id
v ([Id
v1], ASSERT(not (isCoVar v1)) Var v1)