{-# LANGUAGE BangPatterns, CPP, MultiWayIf #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.CoreToStg.Prep
( corePrepPgm
, corePrepExpr
, mkConvertNumLiteral
)
where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Platform
import GHC.Core.Opt.OccurAnal
import GHC.Builtin.PrimOps
import GHC.Builtin.Types.Prim ( realWorldStatePrimTy )
import GHC.Types.Id.Make ( realWorldPrimId, mkPrimOpId )
import GHC.Driver.Types
import GHC.Builtin.Names
import GHC.Core.Utils
import GHC.Core.Opt.Arity
import GHC.Core.FVs
import GHC.Core.Opt.Monad ( CoreToDo(..) )
import GHC.Core.Lint ( endPassIO )
import GHC.Core
import GHC.Core.Make hiding( FloatBind(..) )
import GHC.Core.Type
import GHC.Types.Literal
import GHC.Core.Coercion
import GHC.Tc.Utils.Env
import GHC.Core.TyCon
import GHC.Core.TyCo.Rep( UnivCoProvenance(..) )
import GHC.Types.Demand
import GHC.Types.Var
import GHC.Types.Var.Set
import GHC.Types.Var.Env
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Builtin.Types
import GHC.Core.DataCon
import GHC.Types.Basic
import GHC.Unit.Module
import GHC.Types.Unique.Supply
import GHC.Data.Maybe
import GHC.Data.OrdList
import GHC.Utils.Error
import GHC.Driver.Session
import GHC.Driver.Ways
import GHC.Utils.Misc
import GHC.Utils.Outputable
import GHC.Data.FastString
import GHC.Types.Name ( NamedThing(..), nameSrcSpan, isInternalName )
import GHC.Types.SrcLoc ( SrcSpan(..), realSrcLocSpan, mkRealSrcLoc )
import GHC.Data.Pair
import Data.Bits
import GHC.Utils.Monad ( mapAccumLM )
import Data.List ( unfoldr )
import Data.Functor.Identity
import Control.Monad
import GHC.Types.CostCentre ( CostCentre, ccFromThisModule )
import qualified Data.Set as S
type CpeArg = CoreExpr
type CpeApp = CoreExpr
type CpeBody = CoreExpr
type CpeRhs = CoreExpr
corePrepPgm :: HscEnv -> Module -> ModLocation -> CoreProgram -> [TyCon]
-> IO (CoreProgram, S.Set CostCentre)
corePrepPgm :: HscEnv
-> Module
-> ModLocation
-> CoreProgram
-> [TyCon]
-> IO (CoreProgram, Set CostCentre)
corePrepPgm HscEnv
hsc_env Module
this_mod ModLocation
mod_loc CoreProgram
binds [TyCon]
data_tycons =
DynFlags
-> SDoc
-> ((CoreProgram, Set CostCentre) -> ())
-> IO (CoreProgram, Set CostCentre)
-> IO (CoreProgram, Set CostCentre)
forall (m :: * -> *) a.
MonadIO m =>
DynFlags -> SDoc -> (a -> ()) -> m a -> m a
withTiming DynFlags
dflags
(String -> SDoc
text String
"CorePrep"SDoc -> SDoc -> SDoc
<+>SDoc -> SDoc
brackets (Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr Module
this_mod))
(() -> (CoreProgram, Set CostCentre) -> ()
forall a b. a -> b -> a
const ()) (IO (CoreProgram, Set CostCentre)
-> IO (CoreProgram, Set CostCentre))
-> IO (CoreProgram, Set CostCentre)
-> IO (CoreProgram, Set CostCentre)
forall a b. (a -> b) -> a -> b
$ do
UniqSupply
us <- Char -> IO UniqSupply
mkSplitUniqSupply Char
's'
CorePrepEnv
initialCorePrepEnv <- HscEnv -> IO CorePrepEnv
mkInitialCorePrepEnv HscEnv
hsc_env
let cost_centres :: Set CostCentre
cost_centres
| Way
WayProf Way -> Set Way -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` DynFlags -> Set Way
ways DynFlags
dflags
= Module -> CoreProgram -> Set CostCentre
collectCostCentres Module
this_mod CoreProgram
binds
| Bool
otherwise
= Set CostCentre
forall a. Set a
S.empty
implicit_binds :: CoreProgram
implicit_binds = DynFlags -> ModLocation -> [TyCon] -> CoreProgram
mkDataConWorkers DynFlags
dflags ModLocation
mod_loc [TyCon]
data_tycons
binds_out :: CoreProgram
binds_out = UniqSupply -> UniqSM CoreProgram -> CoreProgram
forall a. UniqSupply -> UniqSM a -> a
initUs_ UniqSupply
us (UniqSM CoreProgram -> CoreProgram)
-> UniqSM CoreProgram -> CoreProgram
forall a b. (a -> b) -> a -> b
$ do
Floats
floats1 <- CorePrepEnv -> CoreProgram -> UniqSM Floats
corePrepTopBinds CorePrepEnv
initialCorePrepEnv CoreProgram
binds
Floats
floats2 <- CorePrepEnv -> CoreProgram -> UniqSM Floats
corePrepTopBinds CorePrepEnv
initialCorePrepEnv CoreProgram
implicit_binds
CoreProgram -> UniqSM CoreProgram
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats -> CoreProgram
deFloatTop (Floats
floats1 Floats -> Floats -> Floats
`appendFloats` Floats
floats2))
HscEnv
-> PrintUnqualified
-> CoreToDo
-> CoreProgram
-> [CoreRule]
-> IO ()
endPassIO HscEnv
hsc_env PrintUnqualified
alwaysQualify CoreToDo
CorePrep CoreProgram
binds_out []
(CoreProgram, Set CostCentre) -> IO (CoreProgram, Set CostCentre)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreProgram
binds_out, Set CostCentre
cost_centres)
where
dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
corePrepExpr :: HscEnv -> CoreExpr -> IO CoreExpr
corePrepExpr :: HscEnv -> CpeRhs -> IO CpeRhs
corePrepExpr HscEnv
hsc_env CpeRhs
expr = do
let dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
DynFlags -> SDoc -> (CpeRhs -> ()) -> IO CpeRhs -> IO CpeRhs
forall (m :: * -> *) a.
MonadIO m =>
DynFlags -> SDoc -> (a -> ()) -> m a -> m a
withTiming DynFlags
dflags (String -> SDoc
text String
"CorePrep [expr]") (() -> CpeRhs -> ()
forall a b. a -> b -> a
const ()) (IO CpeRhs -> IO CpeRhs) -> IO CpeRhs -> IO CpeRhs
forall a b. (a -> b) -> a -> b
$ do
UniqSupply
us <- Char -> IO UniqSupply
mkSplitUniqSupply Char
's'
CorePrepEnv
initialCorePrepEnv <- HscEnv -> IO CorePrepEnv
mkInitialCorePrepEnv HscEnv
hsc_env
let new_expr :: CpeRhs
new_expr = UniqSupply -> UniqSM CpeRhs -> CpeRhs
forall a. UniqSupply -> UniqSM a -> a
initUs_ UniqSupply
us (CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
initialCorePrepEnv CpeRhs
expr)
DynFlags -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
dumpIfSet_dyn DynFlags
dflags DumpFlag
Opt_D_dump_prep String
"CorePrep" DumpFormat
FormatCore (CpeRhs -> SDoc
forall a. Outputable a => a -> SDoc
ppr CpeRhs
new_expr)
CpeRhs -> IO CpeRhs
forall (m :: * -> *) a. Monad m => a -> m a
return CpeRhs
new_expr
corePrepTopBinds :: CorePrepEnv -> [CoreBind] -> UniqSM Floats
corePrepTopBinds :: CorePrepEnv -> CoreProgram -> UniqSM Floats
corePrepTopBinds CorePrepEnv
initialCorePrepEnv CoreProgram
binds
= CorePrepEnv -> CoreProgram -> UniqSM Floats
go CorePrepEnv
initialCorePrepEnv CoreProgram
binds
where
go :: CorePrepEnv -> CoreProgram -> UniqSM Floats
go CorePrepEnv
_ [] = Floats -> UniqSM Floats
forall (m :: * -> *) a. Monad m => a -> m a
return Floats
emptyFloats
go CorePrepEnv
env (CoreBind
bind : CoreProgram
binds) = do (CorePrepEnv
env', Floats
floats, Maybe CoreBind
maybe_new_bind)
<- TopLevelFlag
-> CorePrepEnv
-> CoreBind
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
cpeBind TopLevelFlag
TopLevel CorePrepEnv
env CoreBind
bind
MASSERT(isNothing maybe_new_bind)
Floats
floatss <- CorePrepEnv -> CoreProgram -> UniqSM Floats
go CorePrepEnv
env' CoreProgram
binds
Floats -> UniqSM Floats
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats Floats -> Floats -> Floats
`appendFloats` Floats
floatss)
mkDataConWorkers :: DynFlags -> ModLocation -> [TyCon] -> [CoreBind]
mkDataConWorkers :: DynFlags -> ModLocation -> [TyCon] -> CoreProgram
mkDataConWorkers DynFlags
dflags ModLocation
mod_loc [TyCon]
data_tycons
= [ Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
id (Name -> CpeRhs -> CpeRhs
forall {b}. Name -> Expr b -> Expr b
tick_it (DataCon -> Name
forall a. NamedThing a => a -> Name
getName DataCon
data_con) (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
id))
| TyCon
tycon <- [TyCon]
data_tycons,
DataCon
data_con <- TyCon -> [DataCon]
tyConDataCons TyCon
tycon,
let id :: Id
id = DataCon -> Id
dataConWorkId DataCon
data_con
]
where
tick_it :: Name -> Expr b -> Expr b
tick_it Name
name
| DynFlags -> Int
debugLevel DynFlags
dflags Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Expr b -> Expr b
forall a. a -> a
id
| RealSrcSpan RealSrcSpan
span Maybe BufSpan
_ <- Name -> SrcSpan
nameSrcSpan Name
name = RealSrcSpan -> Expr b -> Expr b
forall {b}. RealSrcSpan -> Expr b -> Expr b
tick RealSrcSpan
span
| Just String
file <- ModLocation -> Maybe String
ml_hs_file ModLocation
mod_loc = RealSrcSpan -> Expr b -> Expr b
forall {b}. RealSrcSpan -> Expr b -> Expr b
tick (String -> RealSrcSpan
span1 String
file)
| Bool
otherwise = RealSrcSpan -> Expr b -> Expr b
forall {b}. RealSrcSpan -> Expr b -> Expr b
tick (String -> RealSrcSpan
span1 String
"???")
where tick :: RealSrcSpan -> Expr b -> Expr b
tick RealSrcSpan
span = Tickish Id -> Expr b -> Expr b
forall b. Tickish Id -> Expr b -> Expr b
Tick (RealSrcSpan -> String -> Tickish Id
forall id. RealSrcSpan -> String -> Tickish id
SourceNote RealSrcSpan
span (String -> Tickish Id) -> String -> Tickish Id
forall a b. (a -> b) -> a -> b
$ DynFlags -> SDoc -> String
showSDoc DynFlags
dflags (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name))
span1 :: String -> RealSrcSpan
span1 String
file = RealSrcLoc -> RealSrcSpan
realSrcLocSpan (RealSrcLoc -> RealSrcSpan) -> RealSrcLoc -> RealSrcSpan
forall a b. (a -> b) -> a -> b
$ FastString -> Int -> Int -> RealSrcLoc
mkRealSrcLoc (String -> FastString
mkFastString String
file) Int
1 Int
1
cpeBind :: TopLevelFlag -> CorePrepEnv -> CoreBind
-> UniqSM (CorePrepEnv,
Floats,
Maybe CoreBind)
cpeBind :: TopLevelFlag
-> CorePrepEnv
-> CoreBind
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
cpeBind TopLevelFlag
top_lvl CorePrepEnv
env (NonRec Id
bndr CpeRhs
rhs)
| Bool -> Bool
not (Id -> Bool
isJoinId Id
bndr)
= do { (CorePrepEnv
env1, Id
bndr1) <- CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id)
cpCloneBndr CorePrepEnv
env Id
bndr
; let dmd :: Demand
dmd = Id -> Demand
idDemandInfo Id
bndr
is_unlifted :: Bool
is_unlifted = HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType (Id -> Type
idType Id
bndr)
; (Floats
floats, CpeRhs
rhs1) <- TopLevelFlag
-> RecFlag
-> Demand
-> Bool
-> CorePrepEnv
-> Id
-> CpeRhs
-> UniqSM (Floats, CpeRhs)
cpePair TopLevelFlag
top_lvl RecFlag
NonRecursive
Demand
dmd Bool
is_unlifted
CorePrepEnv
env Id
bndr1 CpeRhs
rhs
; let triv_rhs :: Bool
triv_rhs = CpeRhs -> Bool
exprIsTrivial CpeRhs
rhs1
env2 :: CorePrepEnv
env2 | Bool
triv_rhs = CorePrepEnv -> Id -> CpeRhs -> CorePrepEnv
extendCorePrepEnvExpr CorePrepEnv
env1 Id
bndr CpeRhs
rhs1
| Bool
otherwise = CorePrepEnv
env1
floats1 :: Floats
floats1 | Bool
triv_rhs, Name -> Bool
isInternalName (Id -> Name
idName Id
bndr)
= Floats
floats
| Bool
otherwise
= Floats -> FloatingBind -> Floats
addFloat Floats
floats FloatingBind
new_float
new_float :: FloatingBind
new_float = Demand -> Bool -> Id -> CpeRhs -> FloatingBind
mkFloat Demand
dmd Bool
is_unlifted Id
bndr1 CpeRhs
rhs1
; (CorePrepEnv, Floats, Maybe CoreBind)
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv
env2, Floats
floats1, Maybe CoreBind
forall a. Maybe a
Nothing) }
| Bool
otherwise
= ASSERT(not (isTopLevel top_lvl))
do { (CorePrepEnv
_, Id
bndr1) <- CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id)
cpCloneBndr CorePrepEnv
env Id
bndr
; (Id
bndr2, CpeRhs
rhs1) <- CorePrepEnv -> Id -> CpeRhs -> UniqSM (Id, CpeRhs)
cpeJoinPair CorePrepEnv
env Id
bndr1 CpeRhs
rhs
; (CorePrepEnv, Floats, Maybe CoreBind)
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> Id -> Id -> CorePrepEnv
extendCorePrepEnv CorePrepEnv
env Id
bndr Id
bndr2,
Floats
emptyFloats,
CoreBind -> Maybe CoreBind
forall a. a -> Maybe a
Just (Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
bndr2 CpeRhs
rhs1)) }
cpeBind TopLevelFlag
top_lvl CorePrepEnv
env (Rec [(Id, CpeRhs)]
pairs)
| Bool -> Bool
not (Id -> Bool
isJoinId ([Id] -> Id
forall a. [a] -> a
head [Id]
bndrs))
= do { (CorePrepEnv
env', [Id]
bndrs1) <- CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bndrs
; [(Floats, CpeRhs)]
stuff <- (Id -> CpeRhs -> UniqSM (Floats, CpeRhs))
-> [Id] -> [CpeRhs] -> UniqSM [(Floats, CpeRhs)]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (TopLevelFlag
-> RecFlag
-> Demand
-> Bool
-> CorePrepEnv
-> Id
-> CpeRhs
-> UniqSM (Floats, CpeRhs)
cpePair TopLevelFlag
top_lvl RecFlag
Recursive Demand
topDmd Bool
False CorePrepEnv
env')
[Id]
bndrs1 [CpeRhs]
rhss
; let ([Floats]
floats_s, [CpeRhs]
rhss1) = [(Floats, CpeRhs)] -> ([Floats], [CpeRhs])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Floats, CpeRhs)]
stuff
all_pairs :: [(Id, CpeRhs)]
all_pairs = (FloatingBind -> [(Id, CpeRhs)] -> [(Id, CpeRhs)])
-> [(Id, CpeRhs)] -> OrdList FloatingBind -> [(Id, CpeRhs)]
forall a b. (a -> b -> b) -> b -> OrdList a -> b
foldrOL FloatingBind -> [(Id, CpeRhs)] -> [(Id, CpeRhs)]
add_float ([Id]
bndrs1 [Id] -> [CpeRhs] -> [(Id, CpeRhs)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [CpeRhs]
rhss1)
([Floats] -> OrdList FloatingBind
concatFloats [Floats]
floats_s)
; (CorePrepEnv, Floats, Maybe CoreBind)
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> [(Id, Id)] -> CorePrepEnv
extendCorePrepEnvList CorePrepEnv
env ([Id]
bndrs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
bndrs1),
FloatingBind -> Floats
unitFloat (CoreBind -> FloatingBind
FloatLet ([(Id, CpeRhs)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Id, CpeRhs)]
all_pairs)),
Maybe CoreBind
forall a. Maybe a
Nothing) }
| Bool
otherwise
= do { (CorePrepEnv
env', [Id]
bndrs1) <- CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bndrs
; [(Id, CpeRhs)]
pairs1 <- (Id -> CpeRhs -> UniqSM (Id, CpeRhs))
-> [Id] -> [CpeRhs] -> UniqSM [(Id, CpeRhs)]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (CorePrepEnv -> Id -> CpeRhs -> UniqSM (Id, CpeRhs)
cpeJoinPair CorePrepEnv
env') [Id]
bndrs1 [CpeRhs]
rhss
; let bndrs2 :: [Id]
bndrs2 = ((Id, CpeRhs) -> Id) -> [(Id, CpeRhs)] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map (Id, CpeRhs) -> Id
forall a b. (a, b) -> a
fst [(Id, CpeRhs)]
pairs1
; (CorePrepEnv, Floats, Maybe CoreBind)
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> [(Id, Id)] -> CorePrepEnv
extendCorePrepEnvList CorePrepEnv
env' ([Id]
bndrs [Id] -> [Id] -> [(Id, Id)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Id]
bndrs2),
Floats
emptyFloats,
CoreBind -> Maybe CoreBind
forall a. a -> Maybe a
Just ([(Id, CpeRhs)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Id, CpeRhs)]
pairs1)) }
where
([Id]
bndrs, [CpeRhs]
rhss) = [(Id, CpeRhs)] -> ([Id], [CpeRhs])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Id, CpeRhs)]
pairs
add_float :: FloatingBind -> [(Id, CpeRhs)] -> [(Id, CpeRhs)]
add_float (FloatLet (NonRec Id
b CpeRhs
r)) [(Id, CpeRhs)]
prs2 = (Id
b,CpeRhs
r) (Id, CpeRhs) -> [(Id, CpeRhs)] -> [(Id, CpeRhs)]
forall a. a -> [a] -> [a]
: [(Id, CpeRhs)]
prs2
add_float (FloatLet (Rec [(Id, CpeRhs)]
prs1)) [(Id, CpeRhs)]
prs2 = [(Id, CpeRhs)]
prs1 [(Id, CpeRhs)] -> [(Id, CpeRhs)] -> [(Id, CpeRhs)]
forall a. [a] -> [a] -> [a]
++ [(Id, CpeRhs)]
prs2
add_float FloatingBind
b [(Id, CpeRhs)]
_ = String -> SDoc -> [(Id, CpeRhs)]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"cpeBind" (FloatingBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr FloatingBind
b)
cpePair :: TopLevelFlag -> RecFlag -> Demand -> Bool
-> CorePrepEnv -> OutId -> CoreExpr
-> UniqSM (Floats, CpeRhs)
cpePair :: TopLevelFlag
-> RecFlag
-> Demand
-> Bool
-> CorePrepEnv
-> Id
-> CpeRhs
-> UniqSM (Floats, CpeRhs)
cpePair TopLevelFlag
top_lvl RecFlag
is_rec Demand
dmd Bool
is_unlifted CorePrepEnv
env Id
bndr CpeRhs
rhs
= ASSERT(not (isJoinId bndr))
do { (Floats
floats1, CpeRhs
rhs1) <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
rhs
; (Floats
floats2, CpeRhs
rhs2) <- Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_from_rhs Floats
floats1 CpeRhs
rhs1
; (Floats
floats3, CpeRhs
rhs3)
<- if CpeRhs -> Int
manifestArity CpeRhs
rhs1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
arity
then (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats2, Int -> CpeRhs -> CpeRhs
cpeEtaExpand Int
arity CpeRhs
rhs2)
else WARN(True, text "CorePrep: silly extra arguments:" <+> ppr bndr)
(do { Id
v <- Type -> UniqSM Id
newVar (Id -> Type
idType Id
bndr)
; let float :: FloatingBind
float = Demand -> Bool -> Id -> CpeRhs -> FloatingBind
mkFloat Demand
topDmd Bool
False Id
v CpeRhs
rhs2
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return ( Floats -> FloatingBind -> Floats
addFloat Floats
floats2 FloatingBind
float
, Int -> CpeRhs -> CpeRhs
cpeEtaExpand Int
arity (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
v)) })
; let (Floats
floats4, CpeRhs
rhs4) = Floats -> CpeRhs -> (Floats, CpeRhs)
wrapTicks Floats
floats3 CpeRhs
rhs3
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats4, CpeRhs
rhs4) }
where
arity :: Int
arity = Id -> Int
idArity Id
bndr
float_from_rhs :: Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_from_rhs Floats
floats CpeRhs
rhs
| Floats -> Bool
isEmptyFloats Floats
floats = (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
rhs)
| TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl = Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_top Floats
floats CpeRhs
rhs
| Bool
otherwise = Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_nested Floats
floats CpeRhs
rhs
float_nested :: Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_nested Floats
floats CpeRhs
rhs
| RecFlag -> Demand -> Bool -> Floats -> CpeRhs -> Bool
wantFloatNested RecFlag
is_rec Demand
dmd Bool
is_unlifted Floats
floats CpeRhs
rhs
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs
rhs)
| Bool
otherwise = Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
dontFloat Floats
floats CpeRhs
rhs
float_top :: Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
float_top Floats
floats CpeRhs
rhs
| Floats -> Bool
allLazyTop Floats
floats
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs
rhs)
| Just (Floats, CpeRhs)
floats <- Floats -> CpeRhs -> Maybe (Floats, CpeRhs)
canFloat Floats
floats CpeRhs
rhs
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats, CpeRhs)
floats
| Bool
otherwise
= Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
dontFloat Floats
floats CpeRhs
rhs
dontFloat :: Floats -> CpeRhs -> UniqSM (Floats, CpeBody)
dontFloat :: Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
dontFloat Floats
floats1 CpeRhs
rhs
= do { (Floats
floats2, CpeRhs
body) <- CpeRhs -> UniqSM (Floats, CpeRhs)
rhsToBody CpeRhs
rhs
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, Floats -> CpeRhs -> CpeRhs
wrapBinds Floats
floats1 (CpeRhs -> CpeRhs) -> CpeRhs -> CpeRhs
forall a b. (a -> b) -> a -> b
$
Floats -> CpeRhs -> CpeRhs
wrapBinds Floats
floats2 CpeRhs
body) }
cpeJoinPair :: CorePrepEnv -> JoinId -> CoreExpr
-> UniqSM (JoinId, CpeRhs)
cpeJoinPair :: CorePrepEnv -> Id -> CpeRhs -> UniqSM (Id, CpeRhs)
cpeJoinPair CorePrepEnv
env Id
bndr CpeRhs
rhs
= ASSERT(isJoinId bndr)
do { let Just Int
join_arity = Id -> Maybe Int
isJoinId_maybe Id
bndr
([Id]
bndrs, CpeRhs
body) = Int -> CpeRhs -> ([Id], CpeRhs)
forall b. Int -> Expr b -> ([b], Expr b)
collectNBinders Int
join_arity CpeRhs
rhs
; (CorePrepEnv
env', [Id]
bndrs') <- CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bndrs
; CpeRhs
body' <- CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
env' CpeRhs
body
; let rhs' :: CpeRhs
rhs' = [Id] -> CpeRhs -> CpeRhs
mkCoreLams [Id]
bndrs' CpeRhs
body'
bndr' :: Id
bndr' = Id
bndr Id -> Unfolding -> Id
`setIdUnfolding` Unfolding
evaldUnfolding
Id -> Int -> Id
`setIdArity` (Id -> Bool) -> [Id] -> Int
forall a. (a -> Bool) -> [a] -> Int
count Id -> Bool
isId [Id]
bndrs
; (Id, CpeRhs) -> UniqSM (Id, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Id
bndr', CpeRhs
rhs') }
cpeRhsE :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeRhs)
cpeRhsE :: CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env (Type Type
ty)
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, Type -> CpeRhs
forall b. Type -> Expr b
Type (CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
ty))
cpeRhsE CorePrepEnv
env (Coercion Coercion
co)
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, Coercion -> CpeRhs
forall b. Coercion -> Expr b
Coercion (CorePrepEnv -> Coercion -> Coercion
cpSubstCo CorePrepEnv
env Coercion
co))
cpeRhsE CorePrepEnv
env expr :: CpeRhs
expr@(Lit (LitNumber LitNumType
nt Integer
i))
= case CorePrepEnv -> LitNumType -> Integer -> Maybe CpeRhs
cpe_convertNumLit CorePrepEnv
env LitNumType
nt Integer
i of
Maybe CpeRhs
Nothing -> (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
expr)
Just CpeRhs
e -> CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
e
cpeRhsE CorePrepEnv
_env expr :: CpeRhs
expr@(Lit {}) = (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
expr)
cpeRhsE CorePrepEnv
env expr :: CpeRhs
expr@(Var {}) = CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeApp CorePrepEnv
env CpeRhs
expr
cpeRhsE CorePrepEnv
env expr :: CpeRhs
expr@(App {}) = CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeApp CorePrepEnv
env CpeRhs
expr
cpeRhsE CorePrepEnv
env (Let CoreBind
bind CpeRhs
body)
= do { (CorePrepEnv
env', Floats
bind_floats, Maybe CoreBind
maybe_bind') <- TopLevelFlag
-> CorePrepEnv
-> CoreBind
-> UniqSM (CorePrepEnv, Floats, Maybe CoreBind)
cpeBind TopLevelFlag
NotTopLevel CorePrepEnv
env CoreBind
bind
; (Floats
body_floats, CpeRhs
body') <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env' CpeRhs
body
; let expr' :: CpeRhs
expr' = case Maybe CoreBind
maybe_bind' of Just CoreBind
bind' -> CoreBind -> CpeRhs -> CpeRhs
forall b. Bind b -> Expr b -> Expr b
Let CoreBind
bind' CpeRhs
body'
Maybe CoreBind
Nothing -> CpeRhs
body'
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
bind_floats Floats -> Floats -> Floats
`appendFloats` Floats
body_floats, CpeRhs
expr') }
cpeRhsE CorePrepEnv
env (Tick Tickish Id
tickish CpeRhs
expr)
| Tickish Id -> TickishPlacement
forall id. Tickish id -> TickishPlacement
tickishPlace Tickish Id
tickish TickishPlacement -> TickishPlacement -> Bool
forall a. Eq a => a -> a -> Bool
== TickishPlacement
PlaceNonLam Bool -> Bool -> Bool
&& Tickish Id
tickish Tickish Id -> TickishScoping -> Bool
forall id. Tickish id -> TickishScoping -> Bool
`tickishScopesLike` TickishScoping
SoftScope
= do { (Floats
floats, CpeRhs
body) <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
expr
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (FloatingBind -> Floats
unitFloat (Tickish Id -> FloatingBind
FloatTick Tickish Id
tickish) Floats -> Floats -> Floats
`appendFloats` Floats
floats, CpeRhs
body) }
| Bool
otherwise
= do { CpeRhs
body <- CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
env CpeRhs
expr
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
tickish' CpeRhs
body) }
where
tickish' :: Tickish Id
tickish' | Breakpoint Int
n [Id]
fvs <- Tickish Id
tickish
= Int -> [Id] -> Tickish Id
forall id. Int -> [id] -> Tickish id
Breakpoint Int
n ((Id -> Id) -> [Id] -> [Id]
forall a b. (a -> b) -> [a] -> [b]
map (HasDebugCallStack => CpeRhs -> Id
CpeRhs -> Id
getIdFromTrivialExpr (CpeRhs -> Id) -> (Id -> CpeRhs) -> Id -> Id
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CorePrepEnv -> Id -> CpeRhs
lookupCorePrepEnv CorePrepEnv
env) [Id]
fvs)
| Bool
otherwise
= Tickish Id
tickish
cpeRhsE CorePrepEnv
env (Cast CpeRhs
expr Coercion
co)
= do { (Floats
floats, CpeRhs
expr') <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
expr
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs -> Coercion -> CpeRhs
forall b. Expr b -> Coercion -> Expr b
Cast CpeRhs
expr' (CorePrepEnv -> Coercion -> Coercion
cpSubstCo CorePrepEnv
env Coercion
co)) }
cpeRhsE CorePrepEnv
env expr :: CpeRhs
expr@(Lam {})
= do { let ([Id]
bndrs,CpeRhs
body) = CpeRhs -> ([Id], CpeRhs)
forall b. Expr b -> ([b], Expr b)
collectBinders CpeRhs
expr
; (CorePrepEnv
env', [Id]
bndrs') <- CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bndrs
; CpeRhs
body' <- CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
env' CpeRhs
body
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, [Id] -> CpeRhs -> CpeRhs
forall b. [b] -> Expr b -> Expr b
mkLams [Id]
bndrs' CpeRhs
body') }
cpeRhsE CorePrepEnv
env (Case CpeRhs
scrut Id
_ Type
ty [])
= do { (Floats
floats, CpeRhs
scrut') <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
scrut
; let ty' :: Type
ty' = CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
ty
co' :: Coercion
co' = Role -> Type -> Type -> Coercion
mkUnsafeCo Role
Representational (CpeRhs -> Type
exprType CpeRhs
scrut') Type
ty'
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs -> Coercion -> CpeRhs
forall b. Expr b -> Coercion -> Expr b
Cast CpeRhs
scrut' Coercion
co') }
cpeRhsE CorePrepEnv
env (Case CpeRhs
scrut Id
bndr Type
_ [Alt Id]
alts)
| CpeRhs -> Bool
isUnsafeEqualityProof CpeRhs
scrut
, Id -> Bool
isDeadBinder Id
bndr
, [(AltCon
_, [Id
co_var], CpeRhs
rhs)] <- [Alt Id]
alts
, let Pair Type
ty1 Type
ty2 = HasDebugCallStack => Id -> Pair Type
Id -> Pair Type
coVarTypes Id
co_var
the_co :: Coercion
the_co = Role -> Type -> Type -> Coercion
mkUnsafeCo Role
Nominal (CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
ty1) (CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
ty2)
env' :: CorePrepEnv
env' = CorePrepEnv -> Id -> Coercion -> CorePrepEnv
extendCoVarEnv CorePrepEnv
env Id
co_var Coercion
the_co
= CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env' CpeRhs
rhs
cpeRhsE CorePrepEnv
env (Case CpeRhs
scrut Id
bndr Type
ty [Alt Id]
alts)
= do { (Floats
floats, CpeRhs
scrut') <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeBody CorePrepEnv
env CpeRhs
scrut
; (CorePrepEnv
env', Id
bndr2) <- CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id)
cpCloneBndr CorePrepEnv
env Id
bndr
; let alts' :: [Alt Id]
alts'
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_CatchBottoms (CorePrepEnv -> DynFlags
cpe_dynFlags CorePrepEnv
env)
, Bool -> Bool
not ([Alt Id] -> Bool
forall b. [Alt b] -> Bool
altsAreExhaustive [Alt Id]
alts)
= [Alt Id] -> Maybe CpeRhs -> [Alt Id]
forall a b. [(AltCon, [a], b)] -> Maybe b -> [(AltCon, [a], b)]
addDefault [Alt Id]
alts (CpeRhs -> Maybe CpeRhs
forall a. a -> Maybe a
Just CpeRhs
err)
| Bool
otherwise = [Alt Id]
alts
where err :: CpeRhs
err = Id -> Type -> String -> CpeRhs
mkRuntimeErrorApp Id
rUNTIME_ERROR_ID Type
ty
String
"Bottoming expression returned"
; [Alt Id]
alts'' <- (Alt Id -> UniqSM (Alt Id)) -> [Alt Id] -> UniqSM [Alt Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (CorePrepEnv -> Alt Id -> UniqSM (Alt Id)
forall {a}.
CorePrepEnv -> (a, [Id], CpeRhs) -> UniqSM (a, [Id], CpeRhs)
sat_alt CorePrepEnv
env') [Alt Id]
alts'
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs -> Id -> Type -> [Alt Id] -> CpeRhs
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CpeRhs
scrut' Id
bndr2 Type
ty [Alt Id]
alts'') }
where
sat_alt :: CorePrepEnv -> (a, [Id], CpeRhs) -> UniqSM (a, [Id], CpeRhs)
sat_alt CorePrepEnv
env (a
con, [Id]
bs, CpeRhs
rhs)
= do { (CorePrepEnv
env2, [Id]
bs') <- CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bs
; CpeRhs
rhs' <- CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
env2 CpeRhs
rhs
; (a, [Id], CpeRhs) -> UniqSM (a, [Id], CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
con, [Id]
bs', CpeRhs
rhs') }
cpeBodyNF :: CorePrepEnv -> CoreExpr -> UniqSM CpeBody
cpeBodyNF :: CorePrepEnv -> CpeRhs -> UniqSM CpeRhs
cpeBodyNF CorePrepEnv
env CpeRhs
expr
= do { (Floats
floats, CpeRhs
body) <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeBody CorePrepEnv
env CpeRhs
expr
; CpeRhs -> UniqSM CpeRhs
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats -> CpeRhs -> CpeRhs
wrapBinds Floats
floats CpeRhs
body) }
cpeBody :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeBody)
cpeBody :: CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeBody CorePrepEnv
env CpeRhs
expr
= do { (Floats
floats1, CpeRhs
rhs) <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
expr
; (Floats
floats2, CpeRhs
body) <- CpeRhs -> UniqSM (Floats, CpeRhs)
rhsToBody CpeRhs
rhs
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats1 Floats -> Floats -> Floats
`appendFloats` Floats
floats2, CpeRhs
body) }
rhsToBody :: CpeRhs -> UniqSM (Floats, CpeBody)
rhsToBody :: CpeRhs -> UniqSM (Floats, CpeRhs)
rhsToBody (Tick Tickish Id
t CpeRhs
expr)
| Tickish Id -> TickishScoping
forall id. Tickish id -> TickishScoping
tickishScoped Tickish Id
t TickishScoping -> TickishScoping -> Bool
forall a. Eq a => a -> a -> Bool
== TickishScoping
NoScope
= do { (Floats
floats, CpeRhs
expr') <- CpeRhs -> UniqSM (Floats, CpeRhs)
rhsToBody CpeRhs
expr
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
t CpeRhs
expr') }
rhsToBody (Cast CpeRhs
e Coercion
co)
= do { (Floats
floats, CpeRhs
e') <- CpeRhs -> UniqSM (Floats, CpeRhs)
rhsToBody CpeRhs
e
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats, CpeRhs -> Coercion -> CpeRhs
forall b. Expr b -> Coercion -> Expr b
Cast CpeRhs
e' Coercion
co) }
rhsToBody expr :: CpeRhs
expr@(Lam {})
| Just CpeRhs
no_lam_result <- [Id] -> CpeRhs -> Maybe CpeRhs
tryEtaReducePrep [Id]
bndrs CpeRhs
body
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
no_lam_result)
| (Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Id -> Bool
isTyVar [Id]
bndrs
= (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
expr)
| Bool
otherwise
= do { let rhs :: CpeRhs
rhs = Int -> CpeRhs -> CpeRhs
cpeEtaExpand (CpeRhs -> Int
exprArity CpeRhs
expr) CpeRhs
expr
; Id
fn <- Type -> UniqSM Id
newVar (CpeRhs -> Type
exprType CpeRhs
rhs)
; let float :: FloatingBind
float = CoreBind -> FloatingBind
FloatLet (Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
fn CpeRhs
rhs)
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (FloatingBind -> Floats
unitFloat FloatingBind
float, Id -> CpeRhs
forall b. Id -> Expr b
Var Id
fn) }
where
([Id]
bndrs,CpeRhs
body) = CpeRhs -> ([Id], CpeRhs)
forall b. Expr b -> ([b], Expr b)
collectBinders CpeRhs
expr
rhsToBody CpeRhs
expr = (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
emptyFloats, CpeRhs
expr)
data ArgInfo = CpeApp CoreArg
| CpeCast Coercion
| CpeTick (Tickish Id)
instance Outputable ArgInfo where
ppr :: ArgInfo -> SDoc
ppr (CpeApp CpeRhs
arg) = String -> SDoc
text String
"app" SDoc -> SDoc -> SDoc
<+> CpeRhs -> SDoc
forall a. Outputable a => a -> SDoc
ppr CpeRhs
arg
ppr (CpeCast Coercion
co) = String -> SDoc
text String
"cast" SDoc -> SDoc -> SDoc
<+> Coercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr Coercion
co
ppr (CpeTick Tickish Id
tick) = String -> SDoc
text String
"tick" SDoc -> SDoc -> SDoc
<+> Tickish Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Tickish Id
tick
cpeApp :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeRhs)
cpeApp :: CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeApp CorePrepEnv
top_env CpeRhs
expr
= do { let (CpeRhs
terminal, [ArgInfo]
args, Int
depth) = CpeRhs -> (CpeRhs, [ArgInfo], Int)
collect_args CpeRhs
expr
; CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app CorePrepEnv
top_env CpeRhs
terminal [ArgInfo]
args Int
depth
}
where
collect_args :: CoreExpr -> (CoreExpr, [ArgInfo], Int)
collect_args :: CpeRhs -> (CpeRhs, [ArgInfo], Int)
collect_args CpeRhs
e = CpeRhs -> [ArgInfo] -> Int -> (CpeRhs, [ArgInfo], Int)
forall {c}.
Num c =>
CpeRhs -> [ArgInfo] -> c -> (CpeRhs, [ArgInfo], c)
go CpeRhs
e [] Int
0
where
go :: CpeRhs -> [ArgInfo] -> c -> (CpeRhs, [ArgInfo], c)
go (App CpeRhs
fun CpeRhs
arg) [ArgInfo]
as !c
depth
= CpeRhs -> [ArgInfo] -> c -> (CpeRhs, [ArgInfo], c)
go CpeRhs
fun (CpeRhs -> ArgInfo
CpeApp CpeRhs
arg ArgInfo -> [ArgInfo] -> [ArgInfo]
forall a. a -> [a] -> [a]
: [ArgInfo]
as)
(if CpeRhs -> Bool
forall b. Expr b -> Bool
isTyCoArg CpeRhs
arg then c
depth else c
depth c -> c -> c
forall a. Num a => a -> a -> a
+ c
1)
go (Cast CpeRhs
fun Coercion
co) [ArgInfo]
as c
depth
= CpeRhs -> [ArgInfo] -> c -> (CpeRhs, [ArgInfo], c)
go CpeRhs
fun (Coercion -> ArgInfo
CpeCast Coercion
co ArgInfo -> [ArgInfo] -> [ArgInfo]
forall a. a -> [a] -> [a]
: [ArgInfo]
as) c
depth
go (Tick Tickish Id
tickish CpeRhs
fun) [ArgInfo]
as c
depth
| Tickish Id -> TickishPlacement
forall id. Tickish id -> TickishPlacement
tickishPlace Tickish Id
tickish TickishPlacement -> TickishPlacement -> Bool
forall a. Eq a => a -> a -> Bool
== TickishPlacement
PlaceNonLam
Bool -> Bool -> Bool
&& Tickish Id
tickish Tickish Id -> TickishScoping -> Bool
forall id. Tickish id -> TickishScoping -> Bool
`tickishScopesLike` TickishScoping
SoftScope
= CpeRhs -> [ArgInfo] -> c -> (CpeRhs, [ArgInfo], c)
go CpeRhs
fun (Tickish Id -> ArgInfo
CpeTick Tickish Id
tickish ArgInfo -> [ArgInfo] -> [ArgInfo]
forall a. a -> [a] -> [a]
: [ArgInfo]
as) c
depth
go CpeRhs
terminal [ArgInfo]
as c
depth = (CpeRhs
terminal, [ArgInfo]
as, c
depth)
cpe_app :: CorePrepEnv
-> CoreExpr
-> [ArgInfo]
-> Int
-> UniqSM (Floats, CpeRhs)
cpe_app :: CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app CorePrepEnv
env (Var Id
f) (CpeApp Type{} : CpeApp CpeRhs
arg : [ArgInfo]
args) Int
depth
| Id
f Id -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
lazyIdKey
Bool -> Bool -> Bool
|| Id
f Id -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
noinlineIdKey
= let (CpeRhs
terminal, [ArgInfo]
args', Int
depth') = CpeRhs -> (CpeRhs, [ArgInfo], Int)
collect_args CpeRhs
arg
in CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app CorePrepEnv
env CpeRhs
terminal ([ArgInfo]
args' [ArgInfo] -> [ArgInfo] -> [ArgInfo]
forall a. [a] -> [a] -> [a]
++ [ArgInfo]
args) (Int
depth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
depth' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
cpe_app CorePrepEnv
env
(Var Id
f)
[ArgInfo]
args
Int
n
| Just PrimOp
KeepAliveOp <- Id -> Maybe PrimOp
isPrimOpId_maybe Id
f
, CpeApp (Type Type
arg_rep)
: CpeApp (Type Type
arg_ty)
: CpeApp (Type Type
_result_rep)
: CpeApp (Type Type
result_ty)
: CpeApp CpeRhs
arg
: CpeApp CpeRhs
s0
: CpeApp CpeRhs
k
: [ArgInfo]
rest <- [ArgInfo]
args
= do { Id
y <- Type -> UniqSM Id
newVar (CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
result_ty)
; Id
s2 <- Type -> UniqSM Id
newVar Type
realWorldStatePrimTy
;
; (Floats
floats, CpeRhs
k') <- case CpeRhs
k of
Lam Id
s CpeRhs
body -> CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app (CorePrepEnv -> Id -> CpeRhs -> CorePrepEnv
extendCorePrepEnvExpr CorePrepEnv
env Id
s CpeRhs
s0) CpeRhs
body [ArgInfo]
rest (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
2)
CpeRhs
_ -> CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app CorePrepEnv
env CpeRhs
k (CpeRhs -> ArgInfo
CpeApp CpeRhs
s0 ArgInfo -> [ArgInfo] -> [ArgInfo]
forall a. a -> [a] -> [a]
: [ArgInfo]
rest) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
; let touchId :: Id
touchId = PrimOp -> Id
mkPrimOpId PrimOp
TouchOp
expr :: CpeRhs
expr = CpeRhs -> Id -> Type -> [Alt Id] -> CpeRhs
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CpeRhs
k' Id
y Type
result_ty [(AltCon
DEFAULT, [], CpeRhs
rhs)]
rhs :: CpeRhs
rhs = let scrut :: CpeRhs
scrut = CpeRhs -> [CpeRhs] -> CpeRhs
forall b. Expr b -> [Expr b] -> Expr b
mkApps (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
touchId) [Type -> CpeRhs
forall b. Type -> Expr b
Type Type
arg_rep, Type -> CpeRhs
forall b. Type -> Expr b
Type Type
arg_ty, CpeRhs
arg, Id -> CpeRhs
forall b. Id -> Expr b
Var Id
realWorldPrimId]
in CpeRhs -> Id -> Type -> [Alt Id] -> CpeRhs
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CpeRhs
scrut Id
s2 Type
result_ty [(AltCon
DEFAULT, [], Id -> CpeRhs
forall b. Id -> Expr b
Var Id
y)]
; (Floats
floats', CpeRhs
expr') <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeBody CorePrepEnv
env CpeRhs
expr
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats Floats -> Floats -> Floats
`appendFloats` Floats
floats', CpeRhs
expr')
}
| Just PrimOp
KeepAliveOp <- Id -> Maybe PrimOp
isPrimOpId_maybe Id
f
= String -> UniqSM (Floats, CpeRhs)
forall a. String -> a
panic String
"invalid keepAlive# application"
cpe_app CorePrepEnv
env (Var Id
f) (CpeApp _runtimeRep :: CpeRhs
_runtimeRep@Type{} : CpeApp _type :: CpeRhs
_type@Type{} : CpeApp CpeRhs
arg : [ArgInfo]
rest) Int
n
| Id
f Id -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
runRWKey
, Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1
= case CpeRhs
arg of
Lam Id
s CpeRhs
body -> CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app (CorePrepEnv -> Id -> Id -> CorePrepEnv
extendCorePrepEnv CorePrepEnv
env Id
s Id
realWorldPrimId) CpeRhs
body [ArgInfo]
rest (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
2)
CpeRhs
_ -> CorePrepEnv
-> CpeRhs -> [ArgInfo] -> Int -> UniqSM (Floats, CpeRhs)
cpe_app CorePrepEnv
env CpeRhs
arg (CpeRhs -> ArgInfo
CpeApp (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
realWorldPrimId) ArgInfo -> [ArgInfo] -> [ArgInfo]
forall a. a -> [a] -> [a]
: [ArgInfo]
rest) (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
cpe_app CorePrepEnv
env (Var Id
v) [ArgInfo]
args Int
depth
= do { Id
v1 <- Id -> UniqSM Id
fiddleCCall Id
v
; let e2 :: CpeRhs
e2 = CorePrepEnv -> Id -> CpeRhs
lookupCorePrepEnv CorePrepEnv
env Id
v1
hd :: Maybe Id
hd = CpeRhs -> Maybe Id
getIdFromTrivialExpr_maybe CpeRhs
e2
; (CpeRhs
app, Floats
floats) <- CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
args CpeRhs
e2 Floats
emptyFloats [Demand]
stricts
; Maybe Id -> CpeRhs -> Floats -> Int -> UniqSM (Floats, CpeRhs)
forall {a}. Maybe Id -> CpeRhs -> a -> Int -> UniqSM (a, CpeRhs)
mb_saturate Maybe Id
hd CpeRhs
app Floats
floats Int
depth }
where
stricts :: [Demand]
stricts = case Id -> StrictSig
idStrictness Id
v of
StrictSig (DmdType DmdEnv
_ [Demand]
demands Divergence
_)
| [Demand] -> Int -> Ordering
forall a. [a] -> Int -> Ordering
listLengthCmp [Demand]
demands Int
depth Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering
GT -> [Demand]
demands
| Bool
otherwise -> []
cpe_app CorePrepEnv
env CpeRhs
fun [] Int
_ = CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
fun
cpe_app CorePrepEnv
env CpeRhs
fun [ArgInfo]
args Int
depth
= do { (Floats
fun_floats, CpeRhs
fun') <- CorePrepEnv -> Demand -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeArg CorePrepEnv
env Demand
evalDmd CpeRhs
fun
; (CpeRhs
app, Floats
floats) <- CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
args CpeRhs
fun' Floats
fun_floats []
; Maybe Id -> CpeRhs -> Floats -> Int -> UniqSM (Floats, CpeRhs)
forall {a}. Maybe Id -> CpeRhs -> a -> Int -> UniqSM (a, CpeRhs)
mb_saturate Maybe Id
forall a. Maybe a
Nothing CpeRhs
app Floats
floats Int
depth }
mb_saturate :: Maybe Id -> CpeRhs -> a -> Int -> UniqSM (a, CpeRhs)
mb_saturate Maybe Id
head CpeRhs
app a
floats Int
depth =
case Maybe Id
head of
Just Id
fn_id -> do { CpeRhs
sat_app <- Id -> CpeRhs -> Int -> UniqSM CpeRhs
maybeSaturate Id
fn_id CpeRhs
app Int
depth
; (a, CpeRhs) -> UniqSM (a, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
floats, CpeRhs
sat_app) }
Maybe Id
_other -> (a, CpeRhs) -> UniqSM (a, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
floats, CpeRhs
app)
rebuild_app
:: CorePrepEnv
-> [ArgInfo]
-> CpeApp
-> Floats
-> [Demand]
-> UniqSM (CpeApp, Floats)
rebuild_app :: CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
_ [] CpeRhs
app Floats
floats [Demand]
ss
= ASSERT(null ss)
(CpeRhs, Floats) -> UniqSM (CpeRhs, Floats)
forall (m :: * -> *) a. Monad m => a -> m a
return (CpeRhs
app, Floats
floats)
rebuild_app CorePrepEnv
env (ArgInfo
a : [ArgInfo]
as) CpeRhs
fun' Floats
floats [Demand]
ss = case ArgInfo
a of
CpeApp (Type Type
arg_ty)
-> CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
as (CpeRhs -> CpeRhs -> CpeRhs
forall b. Expr b -> Expr b -> Expr b
App CpeRhs
fun' (Type -> CpeRhs
forall b. Type -> Expr b
Type Type
arg_ty')) Floats
floats [Demand]
ss
where
arg_ty' :: Type
arg_ty' = CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env Type
arg_ty
CpeApp (Coercion Coercion
co)
-> CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
as (CpeRhs -> CpeRhs -> CpeRhs
forall b. Expr b -> Expr b -> Expr b
App CpeRhs
fun' (Coercion -> CpeRhs
forall b. Coercion -> Expr b
Coercion Coercion
co')) Floats
floats [Demand]
ss
where
co' :: Coercion
co' = CorePrepEnv -> Coercion -> Coercion
cpSubstCo CorePrepEnv
env Coercion
co
CpeApp CpeRhs
arg -> do
let (Demand
ss1, [Demand]
ss_rest)
= case ([Demand]
ss, CpeRhs -> Bool
isLazyExpr CpeRhs
arg) of
(Demand
_ : [Demand]
ss_rest, Bool
True) -> (Demand
topDmd, [Demand]
ss_rest)
(Demand
ss1 : [Demand]
ss_rest, Bool
False) -> (Demand
ss1, [Demand]
ss_rest)
([], Bool
_) -> (Demand
topDmd, [])
(Floats
fs, CpeRhs
arg') <- CorePrepEnv -> Demand -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeArg CorePrepEnv
top_env Demand
ss1 CpeRhs
arg
CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
as (CpeRhs -> CpeRhs -> CpeRhs
forall b. Expr b -> Expr b -> Expr b
App CpeRhs
fun' CpeRhs
arg') (Floats
fs Floats -> Floats -> Floats
`appendFloats` Floats
floats) [Demand]
ss_rest
CpeCast Coercion
co
-> CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
as (CpeRhs -> Coercion -> CpeRhs
forall b. Expr b -> Coercion -> Expr b
Cast CpeRhs
fun' Coercion
co') Floats
floats [Demand]
ss
where
co' :: Coercion
co' = CorePrepEnv -> Coercion -> Coercion
cpSubstCo CorePrepEnv
env Coercion
co
CpeTick Tickish Id
tickish
-> CorePrepEnv
-> [ArgInfo]
-> CpeRhs
-> Floats
-> [Demand]
-> UniqSM (CpeRhs, Floats)
rebuild_app CorePrepEnv
env [ArgInfo]
as CpeRhs
fun' (Floats -> FloatingBind -> Floats
addFloat Floats
floats (Tickish Id -> FloatingBind
FloatTick Tickish Id
tickish)) [Demand]
ss
isLazyExpr :: CoreExpr -> Bool
isLazyExpr :: CpeRhs -> Bool
isLazyExpr (Cast CpeRhs
e Coercion
_) = CpeRhs -> Bool
isLazyExpr CpeRhs
e
isLazyExpr (Tick Tickish Id
_ CpeRhs
e) = CpeRhs -> Bool
isLazyExpr CpeRhs
e
isLazyExpr (Var Id
f `App` CpeRhs
_ `App` CpeRhs
_) = Id
f Id -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
lazyIdKey
isLazyExpr CpeRhs
_ = Bool
False
mkUnsafeCo :: Role -> Type -> Type -> Coercion
mkUnsafeCo :: Role -> Type -> Type -> Coercion
mkUnsafeCo Role
role Type
ty1 Type
ty2 = UnivCoProvenance -> Role -> Type -> Type -> Coercion
mkUnivCo UnivCoProvenance
CorePrepProv Role
role Type
ty1 Type
ty2
okCpeArg :: CoreExpr -> Bool
okCpeArg :: CpeRhs -> Bool
okCpeArg (Lit Literal
_) = Bool
False
okCpeArg CpeRhs
expr = Bool -> Bool
not (CpeRhs -> Bool
exprIsTrivial CpeRhs
expr)
cpeArg :: CorePrepEnv -> Demand
-> CoreArg -> UniqSM (Floats, CpeArg)
cpeArg :: CorePrepEnv -> Demand -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeArg CorePrepEnv
env Demand
dmd CpeRhs
arg
= do { (Floats
floats1, CpeRhs
arg1) <- CorePrepEnv -> CpeRhs -> UniqSM (Floats, CpeRhs)
cpeRhsE CorePrepEnv
env CpeRhs
arg
; let arg_ty :: Type
arg_ty = CpeRhs -> Type
exprType CpeRhs
arg1
is_unlifted :: Bool
is_unlifted = HasDebugCallStack => Type -> Bool
Type -> Bool
isUnliftedType Type
arg_ty
want_float :: Floats -> CpeRhs -> Bool
want_float = RecFlag -> Demand -> Bool -> Floats -> CpeRhs -> Bool
wantFloatNested RecFlag
NonRecursive Demand
dmd Bool
is_unlifted
; (Floats
floats2, CpeRhs
arg2) <- if Floats -> CpeRhs -> Bool
want_float Floats
floats1 CpeRhs
arg1
then (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats1, CpeRhs
arg1)
else Floats -> CpeRhs -> UniqSM (Floats, CpeRhs)
dontFloat Floats
floats1 CpeRhs
arg1
; if CpeRhs -> Bool
okCpeArg CpeRhs
arg2
then do { Id
v <- Type -> UniqSM Id
newVar Type
arg_ty
; let arg3 :: CpeRhs
arg3 = Int -> CpeRhs -> CpeRhs
cpeEtaExpand (CpeRhs -> Int
exprArity CpeRhs
arg2) CpeRhs
arg2
arg_float :: FloatingBind
arg_float = Demand -> Bool -> Id -> CpeRhs -> FloatingBind
mkFloat Demand
dmd Bool
is_unlifted Id
v CpeRhs
arg3
; (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats -> FloatingBind -> Floats
addFloat Floats
floats2 FloatingBind
arg_float, Id -> CpeRhs
forall b. Id -> Expr b
varToCoreExpr Id
v) }
else (Floats, CpeRhs) -> UniqSM (Floats, CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return (Floats
floats2, CpeRhs
arg2)
}
maybeSaturate :: Id -> CpeApp -> Int -> UniqSM CpeRhs
maybeSaturate :: Id -> CpeRhs -> Int -> UniqSM CpeRhs
maybeSaturate Id
fn CpeRhs
expr Int
n_args
| Id -> Bool
hasNoBinding Id
fn
= CpeRhs -> UniqSM CpeRhs
forall (m :: * -> *) a. Monad m => a -> m a
return CpeRhs
sat_expr
| Bool
otherwise
= CpeRhs -> UniqSM CpeRhs
forall (m :: * -> *) a. Monad m => a -> m a
return CpeRhs
expr
where
fn_arity :: Int
fn_arity = Id -> Int
idArity Id
fn
excess_arity :: Int
excess_arity = Int
fn_arity Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n_args
sat_expr :: CpeRhs
sat_expr = Int -> CpeRhs -> CpeRhs
cpeEtaExpand Int
excess_arity CpeRhs
expr
cpeEtaExpand :: Arity -> CpeRhs -> CpeRhs
cpeEtaExpand :: Int -> CpeRhs -> CpeRhs
cpeEtaExpand Int
arity CpeRhs
expr
| Int
arity Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = CpeRhs
expr
| Bool
otherwise = Int -> CpeRhs -> CpeRhs
etaExpand Int
arity CpeRhs
expr
tryEtaReducePrep :: [CoreBndr] -> CoreExpr -> Maybe CoreExpr
tryEtaReducePrep :: [Id] -> CpeRhs -> Maybe CpeRhs
tryEtaReducePrep [Id]
bndrs expr :: CpeRhs
expr@(App CpeRhs
_ CpeRhs
_)
| CpeRhs -> Bool
forall b. Expr b -> Bool
ok_to_eta_reduce CpeRhs
f
, Int
n_remaining Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
, [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ((Id -> CpeRhs -> Bool) -> [Id] -> [CpeRhs] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Id -> CpeRhs -> Bool
forall {b}. Id -> Expr b -> Bool
ok [Id]
bndrs [CpeRhs]
last_args)
, Bool -> Bool
not ((Id -> Bool) -> [Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Id -> VarSet -> Bool
`elemVarSet` VarSet
fvs_remaining) [Id]
bndrs)
, CpeRhs -> Bool
exprIsHNF CpeRhs
remaining_expr
= CpeRhs -> Maybe CpeRhs
forall a. a -> Maybe a
Just CpeRhs
remaining_expr
where
(CpeRhs
f, [CpeRhs]
args) = CpeRhs -> (CpeRhs, [CpeRhs])
forall b. Expr b -> (Expr b, [Expr b])
collectArgs CpeRhs
expr
remaining_expr :: CpeRhs
remaining_expr = CpeRhs -> [CpeRhs] -> CpeRhs
forall b. Expr b -> [Expr b] -> Expr b
mkApps CpeRhs
f [CpeRhs]
remaining_args
fvs_remaining :: VarSet
fvs_remaining = CpeRhs -> VarSet
exprFreeVars CpeRhs
remaining_expr
([CpeRhs]
remaining_args, [CpeRhs]
last_args) = Int -> [CpeRhs] -> ([CpeRhs], [CpeRhs])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n_remaining [CpeRhs]
args
n_remaining :: Int
n_remaining = [CpeRhs] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CpeRhs]
args Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Id] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Id]
bndrs
ok :: Id -> Expr b -> Bool
ok Id
bndr (Var Id
arg) = Id
bndr Id -> Id -> Bool
forall a. Eq a => a -> a -> Bool
== Id
arg
ok Id
_ Expr b
_ = Bool
False
ok_to_eta_reduce :: Expr b -> Bool
ok_to_eta_reduce (Var Id
f) = Bool -> Bool
not (Id -> Bool
hasNoBinding Id
f) Bool -> Bool -> Bool
&& Bool -> Bool
not (Type -> Bool
isLinearType (Id -> Type
idType Id
f))
ok_to_eta_reduce Expr b
_ = Bool
False
tryEtaReducePrep [Id]
bndrs (Tick Tickish Id
tickish CpeRhs
e)
| Tickish Id -> Bool
forall id. Tickish id -> Bool
tickishFloatable Tickish Id
tickish
= (CpeRhs -> CpeRhs) -> Maybe CpeRhs -> Maybe CpeRhs
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
tickish) (Maybe CpeRhs -> Maybe CpeRhs) -> Maybe CpeRhs -> Maybe CpeRhs
forall a b. (a -> b) -> a -> b
$ [Id] -> CpeRhs -> Maybe CpeRhs
tryEtaReducePrep [Id]
bndrs CpeRhs
e
tryEtaReducePrep [Id]
_ CpeRhs
_ = Maybe CpeRhs
forall a. Maybe a
Nothing
data FloatingBind
= FloatLet CoreBind
| FloatCase
CpeBody
Id
AltCon [Var]
Bool
| FloatTick (Tickish Id)
data Floats = Floats OkToSpec (OrdList FloatingBind)
instance Outputable FloatingBind where
ppr :: FloatingBind -> SDoc
ppr (FloatLet CoreBind
b) = CoreBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoreBind
b
ppr (FloatCase CpeRhs
r Id
b AltCon
k [Id]
bs Bool
ok) = String -> SDoc
text String
"case" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
braces (Bool -> SDoc
forall a. Outputable a => a -> SDoc
ppr Bool
ok) SDoc -> SDoc -> SDoc
<+> CpeRhs -> SDoc
forall a. Outputable a => a -> SDoc
ppr CpeRhs
r
SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"of"SDoc -> SDoc -> SDoc
<+> Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
b SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"@"
SDoc -> SDoc -> SDoc
<> case [Id]
bs of
[] -> AltCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltCon
k
[Id]
_ -> SDoc -> SDoc
parens (AltCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr AltCon
k SDoc -> SDoc -> SDoc
<+> [Id] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Id]
bs)
ppr (FloatTick Tickish Id
t) = Tickish Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Tickish Id
t
instance Outputable Floats where
ppr :: Floats -> SDoc
ppr (Floats OkToSpec
flag OrdList FloatingBind
fs) = String -> SDoc
text String
"Floats" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
brackets (OkToSpec -> SDoc
forall a. Outputable a => a -> SDoc
ppr OkToSpec
flag) SDoc -> SDoc -> SDoc
<+>
SDoc -> SDoc
braces ([SDoc] -> SDoc
vcat ((FloatingBind -> SDoc) -> [FloatingBind] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map FloatingBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr (OrdList FloatingBind -> [FloatingBind]
forall a. OrdList a -> [a]
fromOL OrdList FloatingBind
fs)))
instance Outputable OkToSpec where
ppr :: OkToSpec -> SDoc
ppr OkToSpec
OkToSpec = String -> SDoc
text String
"OkToSpec"
ppr OkToSpec
IfUnboxedOk = String -> SDoc
text String
"IfUnboxedOk"
ppr OkToSpec
NotOkToSpec = String -> SDoc
text String
"NotOkToSpec"
data OkToSpec
= OkToSpec
| IfUnboxedOk
| NotOkToSpec
mkFloat :: Demand -> Bool -> Id -> CpeRhs -> FloatingBind
mkFloat :: Demand -> Bool -> Id -> CpeRhs -> FloatingBind
mkFloat Demand
dmd Bool
is_unlifted Id
bndr CpeRhs
rhs
| Bool
is_strict
, Bool -> Bool
not Bool
is_hnf = CpeRhs -> Id -> AltCon -> [Id] -> Bool -> FloatingBind
FloatCase CpeRhs
rhs Id
bndr AltCon
DEFAULT [] (CpeRhs -> Bool
exprOkForSpeculation CpeRhs
rhs)
| Bool
is_unlifted = ASSERT2( exprOkForSpeculation rhs, ppr rhs )
CpeRhs -> Id -> AltCon -> [Id] -> Bool -> FloatingBind
FloatCase CpeRhs
rhs Id
bndr AltCon
DEFAULT [] Bool
True
| Bool
is_hnf = CoreBind -> FloatingBind
FloatLet (Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
bndr CpeRhs
rhs)
| Bool
otherwise = CoreBind -> FloatingBind
FloatLet (Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec (Id -> Demand -> Id
setIdDemandInfo Id
bndr Demand
dmd) CpeRhs
rhs)
where
is_hnf :: Bool
is_hnf = CpeRhs -> Bool
exprIsHNF CpeRhs
rhs
is_strict :: Bool
is_strict = Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isStrictDmd Demand
dmd
emptyFloats :: Floats
emptyFloats :: Floats
emptyFloats = OkToSpec -> OrdList FloatingBind -> Floats
Floats OkToSpec
OkToSpec OrdList FloatingBind
forall a. OrdList a
nilOL
isEmptyFloats :: Floats -> Bool
isEmptyFloats :: Floats -> Bool
isEmptyFloats (Floats OkToSpec
_ OrdList FloatingBind
bs) = OrdList FloatingBind -> Bool
forall a. OrdList a -> Bool
isNilOL OrdList FloatingBind
bs
wrapBinds :: Floats -> CpeBody -> CpeBody
wrapBinds :: Floats -> CpeRhs -> CpeRhs
wrapBinds (Floats OkToSpec
_ OrdList FloatingBind
binds) CpeRhs
body
= (FloatingBind -> CpeRhs -> CpeRhs)
-> CpeRhs -> OrdList FloatingBind -> CpeRhs
forall a b. (a -> b -> b) -> b -> OrdList a -> b
foldrOL FloatingBind -> CpeRhs -> CpeRhs
mk_bind CpeRhs
body OrdList FloatingBind
binds
where
mk_bind :: FloatingBind -> CpeRhs -> CpeRhs
mk_bind (FloatCase CpeRhs
rhs Id
bndr AltCon
con [Id]
bs Bool
_) CpeRhs
body = CpeRhs -> Id -> Type -> [Alt Id] -> CpeRhs
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CpeRhs
rhs Id
bndr (CpeRhs -> Type
exprType CpeRhs
body) [(AltCon
con,[Id]
bs,CpeRhs
body)]
mk_bind (FloatLet CoreBind
bind) CpeRhs
body = CoreBind -> CpeRhs -> CpeRhs
forall b. Bind b -> Expr b -> Expr b
Let CoreBind
bind CpeRhs
body
mk_bind (FloatTick Tickish Id
tickish) CpeRhs
body = Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
tickish CpeRhs
body
addFloat :: Floats -> FloatingBind -> Floats
addFloat :: Floats -> FloatingBind -> Floats
addFloat (Floats OkToSpec
ok_to_spec OrdList FloatingBind
floats) FloatingBind
new_float
= OkToSpec -> OrdList FloatingBind -> Floats
Floats (OkToSpec -> OkToSpec -> OkToSpec
combine OkToSpec
ok_to_spec (FloatingBind -> OkToSpec
check FloatingBind
new_float)) (OrdList FloatingBind
floats OrdList FloatingBind -> FloatingBind -> OrdList FloatingBind
forall a. OrdList a -> a -> OrdList a
`snocOL` FloatingBind
new_float)
where
check :: FloatingBind -> OkToSpec
check (FloatLet {}) = OkToSpec
OkToSpec
check (FloatCase CpeRhs
_ Id
_ AltCon
_ [Id]
_ Bool
ok_for_spec)
| Bool
ok_for_spec = OkToSpec
IfUnboxedOk
| Bool
otherwise = OkToSpec
NotOkToSpec
check FloatTick{} = OkToSpec
OkToSpec
unitFloat :: FloatingBind -> Floats
unitFloat :: FloatingBind -> Floats
unitFloat = Floats -> FloatingBind -> Floats
addFloat Floats
emptyFloats
appendFloats :: Floats -> Floats -> Floats
appendFloats :: Floats -> Floats -> Floats
appendFloats (Floats OkToSpec
spec1 OrdList FloatingBind
floats1) (Floats OkToSpec
spec2 OrdList FloatingBind
floats2)
= OkToSpec -> OrdList FloatingBind -> Floats
Floats (OkToSpec -> OkToSpec -> OkToSpec
combine OkToSpec
spec1 OkToSpec
spec2) (OrdList FloatingBind
floats1 OrdList FloatingBind
-> OrdList FloatingBind -> OrdList FloatingBind
forall a. OrdList a -> OrdList a -> OrdList a
`appOL` OrdList FloatingBind
floats2)
concatFloats :: [Floats] -> OrdList FloatingBind
concatFloats :: [Floats] -> OrdList FloatingBind
concatFloats = (Floats -> OrdList FloatingBind -> OrdList FloatingBind)
-> OrdList FloatingBind -> [Floats] -> OrdList FloatingBind
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (Floats OkToSpec
_ OrdList FloatingBind
bs1) OrdList FloatingBind
bs2 -> OrdList FloatingBind
-> OrdList FloatingBind -> OrdList FloatingBind
forall a. OrdList a -> OrdList a -> OrdList a
appOL OrdList FloatingBind
bs1 OrdList FloatingBind
bs2) OrdList FloatingBind
forall a. OrdList a
nilOL
combine :: OkToSpec -> OkToSpec -> OkToSpec
combine :: OkToSpec -> OkToSpec -> OkToSpec
combine OkToSpec
NotOkToSpec OkToSpec
_ = OkToSpec
NotOkToSpec
combine OkToSpec
_ OkToSpec
NotOkToSpec = OkToSpec
NotOkToSpec
combine OkToSpec
IfUnboxedOk OkToSpec
_ = OkToSpec
IfUnboxedOk
combine OkToSpec
_ OkToSpec
IfUnboxedOk = OkToSpec
IfUnboxedOk
combine OkToSpec
_ OkToSpec
_ = OkToSpec
OkToSpec
deFloatTop :: Floats -> [CoreBind]
deFloatTop :: Floats -> CoreProgram
deFloatTop (Floats OkToSpec
_ OrdList FloatingBind
floats)
= (FloatingBind -> CoreProgram -> CoreProgram)
-> CoreProgram -> OrdList FloatingBind -> CoreProgram
forall a b. (a -> b -> b) -> b -> OrdList a -> b
foldrOL FloatingBind -> CoreProgram -> CoreProgram
get [] OrdList FloatingBind
floats
where
get :: FloatingBind -> CoreProgram -> CoreProgram
get (FloatLet CoreBind
b) CoreProgram
bs = CoreBind -> CoreBind
get_bind CoreBind
b CoreBind -> CoreProgram -> CoreProgram
forall a. a -> [a] -> [a]
: CoreProgram
bs
get (FloatCase CpeRhs
body Id
var AltCon
_ [Id]
_ Bool
_) CoreProgram
bs = CoreBind -> CoreBind
get_bind (Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
var CpeRhs
body) CoreBind -> CoreProgram -> CoreProgram
forall a. a -> [a] -> [a]
: CoreProgram
bs
get FloatingBind
b CoreProgram
_ = String -> SDoc -> CoreProgram
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"corePrepPgm" (FloatingBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr FloatingBind
b)
get_bind :: CoreBind -> CoreBind
get_bind (NonRec Id
x CpeRhs
e) = Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
x (CpeRhs -> CpeRhs
occurAnalyseExpr CpeRhs
e)
get_bind (Rec [(Id, CpeRhs)]
xes) = [(Id, CpeRhs)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(Id
x, CpeRhs -> CpeRhs
occurAnalyseExpr CpeRhs
e) | (Id
x, CpeRhs
e) <- [(Id, CpeRhs)]
xes]
canFloat :: Floats -> CpeRhs -> Maybe (Floats, CpeRhs)
canFloat :: Floats -> CpeRhs -> Maybe (Floats, CpeRhs)
canFloat (Floats OkToSpec
ok_to_spec OrdList FloatingBind
fs) CpeRhs
rhs
| OkToSpec
OkToSpec <- OkToSpec
ok_to_spec
, Just OrdList FloatingBind
fs' <- OrdList FloatingBind
-> [FloatingBind] -> Maybe (OrdList FloatingBind)
go OrdList FloatingBind
forall a. OrdList a
nilOL (OrdList FloatingBind -> [FloatingBind]
forall a. OrdList a -> [a]
fromOL OrdList FloatingBind
fs)
= (Floats, CpeRhs) -> Maybe (Floats, CpeRhs)
forall a. a -> Maybe a
Just (OkToSpec -> OrdList FloatingBind -> Floats
Floats OkToSpec
OkToSpec OrdList FloatingBind
fs', CpeRhs
rhs)
| Bool
otherwise
= Maybe (Floats, CpeRhs)
forall a. Maybe a
Nothing
where
go :: OrdList FloatingBind -> [FloatingBind]
-> Maybe (OrdList FloatingBind)
go :: OrdList FloatingBind
-> [FloatingBind] -> Maybe (OrdList FloatingBind)
go (OrdList FloatingBind
fbs_out) [] = OrdList FloatingBind -> Maybe (OrdList FloatingBind)
forall a. a -> Maybe a
Just OrdList FloatingBind
fbs_out
go OrdList FloatingBind
fbs_out (fb :: FloatingBind
fb@(FloatLet CoreBind
_) : [FloatingBind]
fbs_in)
= OrdList FloatingBind
-> [FloatingBind] -> Maybe (OrdList FloatingBind)
go (OrdList FloatingBind
fbs_out OrdList FloatingBind -> FloatingBind -> OrdList FloatingBind
forall a. OrdList a -> a -> OrdList a
`snocOL` FloatingBind
fb) [FloatingBind]
fbs_in
go OrdList FloatingBind
fbs_out (ft :: FloatingBind
ft@FloatTick{} : [FloatingBind]
fbs_in)
= OrdList FloatingBind
-> [FloatingBind] -> Maybe (OrdList FloatingBind)
go (OrdList FloatingBind
fbs_out OrdList FloatingBind -> FloatingBind -> OrdList FloatingBind
forall a. OrdList a -> a -> OrdList a
`snocOL` FloatingBind
ft) [FloatingBind]
fbs_in
go OrdList FloatingBind
_ (FloatCase{} : [FloatingBind]
_) = Maybe (OrdList FloatingBind)
forall a. Maybe a
Nothing
wantFloatNested :: RecFlag -> Demand -> Bool -> Floats -> CpeRhs -> Bool
wantFloatNested :: RecFlag -> Demand -> Bool -> Floats -> CpeRhs -> Bool
wantFloatNested RecFlag
is_rec Demand
dmd Bool
is_unlifted Floats
floats CpeRhs
rhs
= Floats -> Bool
isEmptyFloats Floats
floats
Bool -> Bool -> Bool
|| Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isStrictDmd Demand
dmd
Bool -> Bool -> Bool
|| Bool
is_unlifted
Bool -> Bool -> Bool
|| (RecFlag -> Floats -> Bool
allLazyNested RecFlag
is_rec Floats
floats Bool -> Bool -> Bool
&& CpeRhs -> Bool
exprIsHNF CpeRhs
rhs)
allLazyTop :: Floats -> Bool
allLazyTop :: Floats -> Bool
allLazyTop (Floats OkToSpec
OkToSpec OrdList FloatingBind
_) = Bool
True
allLazyTop Floats
_ = Bool
False
allLazyNested :: RecFlag -> Floats -> Bool
allLazyNested :: RecFlag -> Floats -> Bool
allLazyNested RecFlag
_ (Floats OkToSpec
OkToSpec OrdList FloatingBind
_) = Bool
True
allLazyNested RecFlag
_ (Floats OkToSpec
NotOkToSpec OrdList FloatingBind
_) = Bool
False
allLazyNested RecFlag
is_rec (Floats OkToSpec
IfUnboxedOk OrdList FloatingBind
_) = RecFlag -> Bool
isNonRec RecFlag
is_rec
data CorePrepEnv
= CPE { CorePrepEnv -> DynFlags
cpe_dynFlags :: DynFlags
, CorePrepEnv -> IdEnv CpeRhs
cpe_env :: IdEnv CoreExpr
, CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env :: Maybe CpeTyCoEnv
, CorePrepEnv -> LitNumType -> Integer -> Maybe CpeRhs
cpe_convertNumLit :: LitNumType -> Integer -> Maybe CoreExpr
}
mkInitialCorePrepEnv :: HscEnv -> IO CorePrepEnv
mkInitialCorePrepEnv :: HscEnv -> IO CorePrepEnv
mkInitialCorePrepEnv HscEnv
hsc_env = do
LitNumType -> Integer -> Maybe CpeRhs
convertNumLit <- HscEnv -> IO (LitNumType -> Integer -> Maybe CpeRhs)
mkConvertNumLiteral HscEnv
hsc_env
CorePrepEnv -> IO CorePrepEnv
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> IO CorePrepEnv) -> CorePrepEnv -> IO CorePrepEnv
forall a b. (a -> b) -> a -> b
$ CPE :: DynFlags
-> IdEnv CpeRhs
-> Maybe CpeTyCoEnv
-> (LitNumType -> Integer -> Maybe CpeRhs)
-> CorePrepEnv
CPE
{ cpe_dynFlags :: DynFlags
cpe_dynFlags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
, cpe_env :: IdEnv CpeRhs
cpe_env = IdEnv CpeRhs
forall a. VarEnv a
emptyVarEnv
, cpe_tyco_env :: Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
forall a. Maybe a
Nothing
, cpe_convertNumLit :: LitNumType -> Integer -> Maybe CpeRhs
cpe_convertNumLit = LitNumType -> Integer -> Maybe CpeRhs
convertNumLit
}
extendCorePrepEnv :: CorePrepEnv -> Id -> Id -> CorePrepEnv
extendCorePrepEnv :: CorePrepEnv -> Id -> Id -> CorePrepEnv
extendCorePrepEnv CorePrepEnv
cpe Id
id Id
id'
= CorePrepEnv
cpe { cpe_env :: IdEnv CpeRhs
cpe_env = IdEnv CpeRhs -> Id -> CpeRhs -> IdEnv CpeRhs
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv (CorePrepEnv -> IdEnv CpeRhs
cpe_env CorePrepEnv
cpe) Id
id (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
id') }
extendCorePrepEnvExpr :: CorePrepEnv -> Id -> CoreExpr -> CorePrepEnv
extendCorePrepEnvExpr :: CorePrepEnv -> Id -> CpeRhs -> CorePrepEnv
extendCorePrepEnvExpr CorePrepEnv
cpe Id
id CpeRhs
expr
= CorePrepEnv
cpe { cpe_env :: IdEnv CpeRhs
cpe_env = IdEnv CpeRhs -> Id -> CpeRhs -> IdEnv CpeRhs
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv (CorePrepEnv -> IdEnv CpeRhs
cpe_env CorePrepEnv
cpe) Id
id CpeRhs
expr }
extendCorePrepEnvList :: CorePrepEnv -> [(Id,Id)] -> CorePrepEnv
extendCorePrepEnvList :: CorePrepEnv -> [(Id, Id)] -> CorePrepEnv
extendCorePrepEnvList CorePrepEnv
cpe [(Id, Id)]
prs
= CorePrepEnv
cpe { cpe_env :: IdEnv CpeRhs
cpe_env = IdEnv CpeRhs -> [(Id, CpeRhs)] -> IdEnv CpeRhs
forall a. VarEnv a -> [(Id, a)] -> VarEnv a
extendVarEnvList (CorePrepEnv -> IdEnv CpeRhs
cpe_env CorePrepEnv
cpe)
(((Id, Id) -> (Id, CpeRhs)) -> [(Id, Id)] -> [(Id, CpeRhs)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Id
id, Id
id') -> (Id
id, Id -> CpeRhs
forall b. Id -> Expr b
Var Id
id')) [(Id, Id)]
prs) }
lookupCorePrepEnv :: CorePrepEnv -> Id -> CoreExpr
lookupCorePrepEnv :: CorePrepEnv -> Id -> CpeRhs
lookupCorePrepEnv CorePrepEnv
cpe Id
id
= case IdEnv CpeRhs -> Id -> Maybe CpeRhs
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv (CorePrepEnv -> IdEnv CpeRhs
cpe_env CorePrepEnv
cpe) Id
id of
Maybe CpeRhs
Nothing -> Id -> CpeRhs
forall b. Id -> Expr b
Var Id
id
Just CpeRhs
exp -> CpeRhs
exp
data CpeTyCoEnv = TCE TvSubstEnv CvSubstEnv
emptyTCE :: CpeTyCoEnv
emptyTCE :: CpeTyCoEnv
emptyTCE = TvSubstEnv -> CvSubstEnv -> CpeTyCoEnv
TCE TvSubstEnv
emptyTvSubstEnv CvSubstEnv
emptyCvSubstEnv
extend_tce_cv :: CpeTyCoEnv -> CoVar -> Coercion -> CpeTyCoEnv
extend_tce_cv :: CpeTyCoEnv -> Id -> Coercion -> CpeTyCoEnv
extend_tce_cv (TCE TvSubstEnv
tv_env CvSubstEnv
cv_env) Id
cv Coercion
co
= TvSubstEnv -> CvSubstEnv -> CpeTyCoEnv
TCE TvSubstEnv
tv_env (CvSubstEnv -> Id -> Coercion -> CvSubstEnv
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv CvSubstEnv
cv_env Id
cv Coercion
co)
extend_tce_tv :: CpeTyCoEnv -> TyVar -> Type -> CpeTyCoEnv
extend_tce_tv :: CpeTyCoEnv -> Id -> Type -> CpeTyCoEnv
extend_tce_tv (TCE TvSubstEnv
tv_env CvSubstEnv
cv_env) Id
tv Type
ty
= TvSubstEnv -> CvSubstEnv -> CpeTyCoEnv
TCE (TvSubstEnv -> Id -> Type -> TvSubstEnv
forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv TvSubstEnv
tv_env Id
tv Type
ty) CvSubstEnv
cv_env
lookup_tce_cv :: CpeTyCoEnv -> CoVar -> Coercion
lookup_tce_cv :: CpeTyCoEnv -> Id -> Coercion
lookup_tce_cv (TCE TvSubstEnv
_ CvSubstEnv
cv_env) Id
cv
= case CvSubstEnv -> Id -> Maybe Coercion
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv CvSubstEnv
cv_env Id
cv of
Just Coercion
co -> Coercion
co
Maybe Coercion
Nothing -> Id -> Coercion
mkCoVarCo Id
cv
lookup_tce_tv :: CpeTyCoEnv -> TyVar -> Type
lookup_tce_tv :: CpeTyCoEnv -> Id -> Type
lookup_tce_tv (TCE TvSubstEnv
tv_env CvSubstEnv
_) Id
tv
= case TvSubstEnv -> Id -> Maybe Type
forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv TvSubstEnv
tv_env Id
tv of
Just Type
ty -> Type
ty
Maybe Type
Nothing -> Id -> Type
mkTyVarTy Id
tv
extendCoVarEnv :: CorePrepEnv -> CoVar -> Coercion -> CorePrepEnv
extendCoVarEnv :: CorePrepEnv -> Id -> Coercion -> CorePrepEnv
extendCoVarEnv cpe :: CorePrepEnv
cpe@(CPE { cpe_tyco_env :: CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
mb_tce }) Id
cv Coercion
co
= CorePrepEnv
cpe { cpe_tyco_env :: Maybe CpeTyCoEnv
cpe_tyco_env = CpeTyCoEnv -> Maybe CpeTyCoEnv
forall a. a -> Maybe a
Just (CpeTyCoEnv -> Id -> Coercion -> CpeTyCoEnv
extend_tce_cv CpeTyCoEnv
tce Id
cv Coercion
co) }
where
tce :: CpeTyCoEnv
tce = Maybe CpeTyCoEnv
mb_tce Maybe CpeTyCoEnv -> CpeTyCoEnv -> CpeTyCoEnv
forall a. Maybe a -> a -> a
`orElse` CpeTyCoEnv
emptyTCE
cpSubstTy :: CorePrepEnv -> Type -> Type
cpSubstTy :: CorePrepEnv -> Type -> Type
cpSubstTy (CPE { cpe_tyco_env :: CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
mb_env }) Type
ty
= case Maybe CpeTyCoEnv
mb_env of
Just CpeTyCoEnv
env -> Identity Type -> Type
forall a. Identity a -> a
runIdentity (CpeTyCoEnv -> Type -> Identity Type
subst_ty CpeTyCoEnv
env Type
ty)
Maybe CpeTyCoEnv
Nothing -> Type
ty
cpSubstCo :: CorePrepEnv -> Coercion -> Coercion
cpSubstCo :: CorePrepEnv -> Coercion -> Coercion
cpSubstCo (CPE { cpe_tyco_env :: CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
mb_env }) Coercion
co
= case Maybe CpeTyCoEnv
mb_env of
Just CpeTyCoEnv
tce -> Identity Coercion -> Coercion
forall a. Identity a -> a
runIdentity (CpeTyCoEnv -> Coercion -> Identity Coercion
subst_co CpeTyCoEnv
tce Coercion
co)
Maybe CpeTyCoEnv
Nothing -> Coercion
co
subst_tyco_mapper :: TyCoMapper CpeTyCoEnv Identity
subst_tyco_mapper :: TyCoMapper CpeTyCoEnv Identity
subst_tyco_mapper = TyCoMapper :: forall env (m :: * -> *).
(env -> Id -> m Type)
-> (env -> Id -> m Coercion)
-> (env -> CoercionHole -> m Coercion)
-> (env -> Id -> ArgFlag -> m (env, Id))
-> (TyCon -> m TyCon)
-> TyCoMapper env m
TyCoMapper
{ tcm_tyvar :: CpeTyCoEnv -> Id -> Identity Type
tcm_tyvar = \CpeTyCoEnv
env Id
tv -> Type -> Identity Type
forall (m :: * -> *) a. Monad m => a -> m a
return (CpeTyCoEnv -> Id -> Type
lookup_tce_tv CpeTyCoEnv
env Id
tv)
, tcm_covar :: CpeTyCoEnv -> Id -> Identity Coercion
tcm_covar = \CpeTyCoEnv
env Id
cv -> Coercion -> Identity Coercion
forall (m :: * -> *) a. Monad m => a -> m a
return (CpeTyCoEnv -> Id -> Coercion
lookup_tce_cv CpeTyCoEnv
env Id
cv)
, tcm_hole :: CpeTyCoEnv -> CoercionHole -> Identity Coercion
tcm_hole = \CpeTyCoEnv
_ CoercionHole
hole -> String -> SDoc -> Identity Coercion
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"subst_co_mapper:hole" (CoercionHole -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoercionHole
hole)
, tcm_tycobinder :: CpeTyCoEnv -> Id -> ArgFlag -> Identity (CpeTyCoEnv, Id)
tcm_tycobinder = \CpeTyCoEnv
env Id
tcv ArgFlag
_vis -> if Id -> Bool
isTyVar Id
tcv
then (CpeTyCoEnv, Id) -> Identity (CpeTyCoEnv, Id)
forall (m :: * -> *) a. Monad m => a -> m a
return (CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_tv_bndr CpeTyCoEnv
env Id
tcv)
else (CpeTyCoEnv, Id) -> Identity (CpeTyCoEnv, Id)
forall (m :: * -> *) a. Monad m => a -> m a
return (CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_cv_bndr CpeTyCoEnv
env Id
tcv)
, tcm_tycon :: TyCon -> Identity TyCon
tcm_tycon = \TyCon
tc -> TyCon -> Identity TyCon
forall (m :: * -> *) a. Monad m => a -> m a
return TyCon
tc }
subst_ty :: CpeTyCoEnv -> Type -> Identity Type
subst_co :: CpeTyCoEnv -> Coercion -> Identity Coercion
(CpeTyCoEnv -> Type -> Identity Type
subst_ty, CpeTyCoEnv -> [Type] -> Identity [Type]
_, CpeTyCoEnv -> Coercion -> Identity Coercion
subst_co, CpeTyCoEnv -> [Coercion] -> Identity [Coercion]
_) = TyCoMapper CpeTyCoEnv Identity
-> (CpeTyCoEnv -> Type -> Identity Type,
CpeTyCoEnv -> [Type] -> Identity [Type],
CpeTyCoEnv -> Coercion -> Identity Coercion,
CpeTyCoEnv -> [Coercion] -> Identity [Coercion])
forall (m :: * -> *) env.
Monad m =>
TyCoMapper env m
-> (env -> Type -> m Type, env -> [Type] -> m [Type],
env -> Coercion -> m Coercion, env -> [Coercion] -> m [Coercion])
mapTyCoX TyCoMapper CpeTyCoEnv Identity
subst_tyco_mapper
cpSubstTyVarBndr :: CorePrepEnv -> TyVar -> (CorePrepEnv, TyVar)
cpSubstTyVarBndr :: CorePrepEnv -> Id -> (CorePrepEnv, Id)
cpSubstTyVarBndr env :: CorePrepEnv
env@(CPE { cpe_tyco_env :: CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
mb_env }) Id
tv
= case Maybe CpeTyCoEnv
mb_env of
Maybe CpeTyCoEnv
Nothing -> (CorePrepEnv
env, Id
tv)
Just CpeTyCoEnv
tce -> (CorePrepEnv
env { cpe_tyco_env :: Maybe CpeTyCoEnv
cpe_tyco_env = CpeTyCoEnv -> Maybe CpeTyCoEnv
forall a. a -> Maybe a
Just CpeTyCoEnv
tce' }, Id
tv')
where
(CpeTyCoEnv
tce', Id
tv') = CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_tv_bndr CpeTyCoEnv
tce Id
tv
subst_tv_bndr :: CpeTyCoEnv -> TyVar -> (CpeTyCoEnv, TyVar)
subst_tv_bndr :: CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_tv_bndr CpeTyCoEnv
tce Id
tv
= (CpeTyCoEnv -> Id -> Type -> CpeTyCoEnv
extend_tce_tv CpeTyCoEnv
tce Id
tv (Id -> Type
mkTyVarTy Id
tv'), Id
tv')
where
tv' :: Id
tv' = Name -> Type -> Id
mkTyVar (Id -> Name
tyVarName Id
tv) Type
kind'
kind' :: Type
kind' = Identity Type -> Type
forall a. Identity a -> a
runIdentity (Identity Type -> Type) -> Identity Type -> Type
forall a b. (a -> b) -> a -> b
$ CpeTyCoEnv -> Type -> Identity Type
subst_ty CpeTyCoEnv
tce (Type -> Identity Type) -> Type -> Identity Type
forall a b. (a -> b) -> a -> b
$ Id -> Type
tyVarKind Id
tv
cpSubstCoVarBndr :: CorePrepEnv -> CoVar -> (CorePrepEnv, CoVar)
cpSubstCoVarBndr :: CorePrepEnv -> Id -> (CorePrepEnv, Id)
cpSubstCoVarBndr env :: CorePrepEnv
env@(CPE { cpe_tyco_env :: CorePrepEnv -> Maybe CpeTyCoEnv
cpe_tyco_env = Maybe CpeTyCoEnv
mb_env }) Id
cv
= case Maybe CpeTyCoEnv
mb_env of
Maybe CpeTyCoEnv
Nothing -> (CorePrepEnv
env, Id
cv)
Just CpeTyCoEnv
tce -> (CorePrepEnv
env { cpe_tyco_env :: Maybe CpeTyCoEnv
cpe_tyco_env = CpeTyCoEnv -> Maybe CpeTyCoEnv
forall a. a -> Maybe a
Just CpeTyCoEnv
tce' }, Id
cv')
where
(CpeTyCoEnv
tce', Id
cv') = CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_cv_bndr CpeTyCoEnv
tce Id
cv
subst_cv_bndr :: CpeTyCoEnv -> CoVar -> (CpeTyCoEnv, CoVar)
subst_cv_bndr :: CpeTyCoEnv -> Id -> (CpeTyCoEnv, Id)
subst_cv_bndr CpeTyCoEnv
tce Id
cv
= (CpeTyCoEnv -> Id -> Coercion -> CpeTyCoEnv
extend_tce_cv CpeTyCoEnv
tce Id
cv (Id -> Coercion
mkCoVarCo Id
cv'), Id
cv')
where
cv' :: Id
cv' = Name -> Type -> Id
mkCoVar (Id -> Name
varName Id
cv) Type
ty'
ty' :: Type
ty' = Identity Type -> Type
forall a. Identity a -> a
runIdentity (CpeTyCoEnv -> Type -> Identity Type
subst_ty CpeTyCoEnv
tce (Type -> Identity Type) -> Type -> Identity Type
forall a b. (a -> b) -> a -> b
$ Id -> Type
varType Id
cv)
cpCloneBndrs :: CorePrepEnv -> [InVar] -> UniqSM (CorePrepEnv, [OutVar])
cpCloneBndrs :: CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
cpCloneBndrs CorePrepEnv
env [Id]
bs = (CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id))
-> CorePrepEnv -> [Id] -> UniqSM (CorePrepEnv, [Id])
forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id)
cpCloneBndr CorePrepEnv
env [Id]
bs
cpCloneBndr :: CorePrepEnv -> InVar -> UniqSM (CorePrepEnv, OutVar)
cpCloneBndr :: CorePrepEnv -> Id -> UniqSM (CorePrepEnv, Id)
cpCloneBndr CorePrepEnv
env Id
bndr
| Id -> Bool
isTyVar Id
bndr
= (CorePrepEnv, Id) -> UniqSM (CorePrepEnv, Id)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> Id -> (CorePrepEnv, Id)
cpSubstTyVarBndr CorePrepEnv
env Id
bndr)
| Id -> Bool
isCoVar Id
bndr
= (CorePrepEnv, Id) -> UniqSM (CorePrepEnv, Id)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> Id -> (CorePrepEnv, Id)
cpSubstCoVarBndr CorePrepEnv
env Id
bndr)
| Bool
otherwise
= do { Id
bndr' <- Id -> UniqSM Id
forall {m :: * -> *}. MonadUnique m => Id -> m Id
clone_it Id
bndr
; let unfolding' :: Unfolding
unfolding' = Unfolding -> Unfolding
zapUnfolding (Id -> Unfolding
realIdUnfolding Id
bndr)
bndr'' :: Id
bndr'' = Id
bndr' Id -> Unfolding -> Id
`setIdUnfolding` Unfolding
unfolding'
Id -> RuleInfo -> Id
`setIdSpecialisation` RuleInfo
emptyRuleInfo
; (CorePrepEnv, Id) -> UniqSM (CorePrepEnv, Id)
forall (m :: * -> *) a. Monad m => a -> m a
return (CorePrepEnv -> Id -> Id -> CorePrepEnv
extendCorePrepEnv CorePrepEnv
env Id
bndr Id
bndr'', Id
bndr'') }
where
clone_it :: Id -> m Id
clone_it Id
bndr
| Id -> Bool
isLocalId Id
bndr
= do { Unique
uniq <- m Unique
forall (m :: * -> *). MonadUnique m => m Unique
getUniqueM
; let ty' :: Type
ty' = CorePrepEnv -> Type -> Type
cpSubstTy CorePrepEnv
env (Id -> Type
idType Id
bndr)
; Id -> m Id
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> Unique -> Id
setVarUnique (Id -> Type -> Id
setIdType Id
bndr Type
ty') Unique
uniq) }
| Bool
otherwise
= Id -> m Id
forall (m :: * -> *) a. Monad m => a -> m a
return Id
bndr
fiddleCCall :: Id -> UniqSM Id
fiddleCCall :: Id -> UniqSM Id
fiddleCCall Id
id
| Id -> Bool
isFCallId Id
id = (Id
id Id -> Unique -> Id
`setVarUnique`) (Unique -> Id) -> UniqSM Unique -> UniqSM Id
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UniqSM Unique
forall (m :: * -> *). MonadUnique m => m Unique
getUniqueM
| Bool
otherwise = Id -> UniqSM Id
forall (m :: * -> *) a. Monad m => a -> m a
return Id
id
newVar :: Type -> UniqSM Id
newVar :: Type -> UniqSM Id
newVar Type
ty
= Type -> ()
seqType Type
ty () -> UniqSM Id -> UniqSM Id
`seq` do
Unique
uniq <- UniqSM Unique
forall (m :: * -> *). MonadUnique m => m Unique
getUniqueM
Id -> UniqSM Id
forall (m :: * -> *) a. Monad m => a -> m a
return (FastString -> Unique -> Type -> Type -> Id
mkSysLocalOrCoVar (String -> FastString
fsLit String
"sat") Unique
uniq Type
Many Type
ty)
wrapTicks :: Floats -> CoreExpr -> (Floats, CoreExpr)
wrapTicks :: Floats -> CpeRhs -> (Floats, CpeRhs)
wrapTicks (Floats OkToSpec
flag OrdList FloatingBind
floats0) CpeRhs
expr =
(OkToSpec -> OrdList FloatingBind -> Floats
Floats OkToSpec
flag ([FloatingBind] -> OrdList FloatingBind
forall a. [a] -> OrdList a
toOL ([FloatingBind] -> OrdList FloatingBind)
-> [FloatingBind] -> OrdList FloatingBind
forall a b. (a -> b) -> a -> b
$ [FloatingBind] -> [FloatingBind]
forall a. [a] -> [a]
reverse [FloatingBind]
floats1), (Tickish Id -> CpeRhs -> CpeRhs)
-> CpeRhs -> [Tickish Id] -> CpeRhs
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tickish Id -> CpeRhs -> CpeRhs
mkTick CpeRhs
expr ([Tickish Id] -> [Tickish Id]
forall a. [a] -> [a]
reverse [Tickish Id]
ticks1))
where ([FloatingBind]
floats1, [Tickish Id]
ticks1) = (([FloatingBind], [Tickish Id])
-> FloatingBind -> ([FloatingBind], [Tickish Id]))
-> ([FloatingBind], [Tickish Id])
-> OrdList FloatingBind
-> ([FloatingBind], [Tickish Id])
forall b a. (b -> a -> b) -> b -> OrdList a -> b
foldlOL ([FloatingBind], [Tickish Id])
-> FloatingBind -> ([FloatingBind], [Tickish Id])
go ([], []) (OrdList FloatingBind -> ([FloatingBind], [Tickish Id]))
-> OrdList FloatingBind -> ([FloatingBind], [Tickish Id])
forall a b. (a -> b) -> a -> b
$ OrdList FloatingBind
floats0
go :: ([FloatingBind], [Tickish Id])
-> FloatingBind -> ([FloatingBind], [Tickish Id])
go ([FloatingBind]
floats, [Tickish Id]
ticks) (FloatTick Tickish Id
t)
= ASSERT(tickishPlace t == PlaceNonLam)
([FloatingBind]
floats, if (Tickish Id -> Bool) -> [Tickish Id] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Tickish Id -> Tickish Id -> Bool)
-> Tickish Id -> Tickish Id -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Tickish Id -> Tickish Id -> Bool
forall b. Eq b => Tickish b -> Tickish b -> Bool
tickishContains Tickish Id
t) [Tickish Id]
ticks
then [Tickish Id]
ticks else Tickish Id
tTickish Id -> [Tickish Id] -> [Tickish Id]
forall a. a -> [a] -> [a]
:[Tickish Id]
ticks)
go ([FloatingBind]
floats, [Tickish Id]
ticks) FloatingBind
f
= ((Tickish Id -> FloatingBind -> FloatingBind)
-> FloatingBind -> [Tickish Id] -> FloatingBind
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tickish Id -> FloatingBind -> FloatingBind
wrap FloatingBind
f ([Tickish Id] -> [Tickish Id]
forall a. [a] -> [a]
reverse [Tickish Id]
ticks)FloatingBind -> [FloatingBind] -> [FloatingBind]
forall a. a -> [a] -> [a]
:[FloatingBind]
floats, [Tickish Id]
ticks)
wrap :: Tickish Id -> FloatingBind -> FloatingBind
wrap Tickish Id
t (FloatLet CoreBind
bind) = CoreBind -> FloatingBind
FloatLet (Tickish Id -> CoreBind -> CoreBind
wrapBind Tickish Id
t CoreBind
bind)
wrap Tickish Id
t (FloatCase CpeRhs
r Id
b AltCon
con [Id]
bs Bool
ok) = CpeRhs -> Id -> AltCon -> [Id] -> Bool -> FloatingBind
FloatCase (Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
t CpeRhs
r) Id
b AltCon
con [Id]
bs Bool
ok
wrap Tickish Id
_ FloatingBind
other = String -> SDoc -> FloatingBind
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"wrapTicks: unexpected float!"
(FloatingBind -> SDoc
forall a. Outputable a => a -> SDoc
ppr FloatingBind
other)
wrapBind :: Tickish Id -> CoreBind -> CoreBind
wrapBind Tickish Id
t (NonRec Id
binder CpeRhs
rhs) = Id -> CpeRhs -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
binder (Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
t CpeRhs
rhs)
wrapBind Tickish Id
t (Rec [(Id, CpeRhs)]
pairs) = [(Id, CpeRhs)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec ((CpeRhs -> CpeRhs) -> [(Id, CpeRhs)] -> [(Id, CpeRhs)]
forall b c a. (b -> c) -> [(a, b)] -> [(a, c)]
mapSnd (Tickish Id -> CpeRhs -> CpeRhs
mkTick Tickish Id
t) [(Id, CpeRhs)]
pairs)
collectCostCentres :: Module -> CoreProgram -> S.Set CostCentre
collectCostCentres :: Module -> CoreProgram -> Set CostCentre
collectCostCentres Module
mod_name
= (Set CostCentre -> CoreBind -> Set CostCentre)
-> Set CostCentre -> CoreProgram -> Set CostCentre
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Set CostCentre -> CoreBind -> Set CostCentre
go_bind Set CostCentre
forall a. Set a
S.empty
where
go :: Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e = case CpeRhs
e of
Var{} -> Set CostCentre
cs
Lit{} -> Set CostCentre
cs
App CpeRhs
e1 CpeRhs
e2 -> Set CostCentre -> CpeRhs -> Set CostCentre
go (Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e1) CpeRhs
e2
Lam Id
_ CpeRhs
e -> Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e
Let CoreBind
b CpeRhs
e -> Set CostCentre -> CpeRhs -> Set CostCentre
go (Set CostCentre -> CoreBind -> Set CostCentre
go_bind Set CostCentre
cs CoreBind
b) CpeRhs
e
Case CpeRhs
scrt Id
_ Type
_ [Alt Id]
alts -> Set CostCentre -> [Alt Id] -> Set CostCentre
go_alts (Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
scrt) [Alt Id]
alts
Cast CpeRhs
e Coercion
_ -> Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e
Tick (ProfNote CostCentre
cc Bool
_ Bool
_) CpeRhs
e ->
Set CostCentre -> CpeRhs -> Set CostCentre
go (if CostCentre -> Module -> Bool
ccFromThisModule CostCentre
cc Module
mod_name then CostCentre -> Set CostCentre -> Set CostCentre
forall a. Ord a => a -> Set a -> Set a
S.insert CostCentre
cc Set CostCentre
cs else Set CostCentre
cs) CpeRhs
e
Tick Tickish Id
_ CpeRhs
e -> Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e
Type{} -> Set CostCentre
cs
Coercion{} -> Set CostCentre
cs
go_alts :: Set CostCentre -> [Alt Id] -> Set CostCentre
go_alts = (Set CostCentre -> Alt Id -> Set CostCentre)
-> Set CostCentre -> [Alt Id] -> Set CostCentre
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Set CostCentre
cs (AltCon
_con, [Id]
_bndrs, CpeRhs
e) -> Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs CpeRhs
e)
go_bind :: S.Set CostCentre -> CoreBind -> S.Set CostCentre
go_bind :: Set CostCentre -> CoreBind -> Set CostCentre
go_bind Set CostCentre
cs (NonRec Id
b CpeRhs
e) =
Set CostCentre -> CpeRhs -> Set CostCentre
go (Set CostCentre
-> (CpeRhs -> Set CostCentre) -> Maybe CpeRhs -> Set CostCentre
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set CostCentre
cs (Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs) (Id -> Maybe CpeRhs
get_unf Id
b)) CpeRhs
e
go_bind Set CostCentre
cs (Rec [(Id, CpeRhs)]
bs) =
(Set CostCentre -> (Id, CpeRhs) -> Set CostCentre)
-> Set CostCentre -> [(Id, CpeRhs)] -> Set CostCentre
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Set CostCentre
cs' (Id
b, CpeRhs
e) -> Set CostCentre -> CpeRhs -> Set CostCentre
go (Set CostCentre
-> (CpeRhs -> Set CostCentre) -> Maybe CpeRhs -> Set CostCentre
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set CostCentre
cs' (Set CostCentre -> CpeRhs -> Set CostCentre
go Set CostCentre
cs') (Id -> Maybe CpeRhs
get_unf Id
b)) CpeRhs
e) Set CostCentre
cs [(Id, CpeRhs)]
bs
get_unf :: Id -> Maybe CpeRhs
get_unf = Unfolding -> Maybe CpeRhs
maybeUnfoldingTemplate (Unfolding -> Maybe CpeRhs)
-> (Id -> Unfolding) -> Id -> Maybe CpeRhs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id -> Unfolding
realIdUnfolding
mkConvertNumLiteral
:: HscEnv
-> IO (LitNumType -> Integer -> Maybe CoreExpr)
mkConvertNumLiteral :: HscEnv -> IO (LitNumType -> Integer -> Maybe CpeRhs)
mkConvertNumLiteral HscEnv
hsc_env = do
let
dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
guardBignum :: m a -> m a
guardBignum m a
act
| DynFlags -> UnitId
homeUnitId DynFlags
dflags UnitId -> UnitId -> Bool
forall a. Eq a => a -> a -> Bool
== UnitId
primUnitId
= a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ String -> a
forall a. String -> a
panic String
"Bignum literals are not supported in ghc-prim"
| DynFlags -> UnitId
homeUnitId DynFlags
dflags UnitId -> UnitId -> Bool
forall a. Eq a => a -> a -> Bool
== UnitId
bignumUnitId
= a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$ String -> a
forall a. String -> a
panic String
"Bignum literals are not supported in ghc-bignum"
| Bool
otherwise = m a
act
lookupBignumId :: Name -> IO Id
lookupBignumId Name
n = IO Id -> IO Id
forall {m :: * -> *} {a}. Monad m => m a -> m a
guardBignum (HasDebugCallStack => TyThing -> Id
TyThing -> Id
tyThingId (TyThing -> Id) -> IO TyThing -> IO Id
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HscEnv -> Name -> IO TyThing
lookupGlobal HscEnv
hsc_env Name
n)
Id
bignatFromWordListId <- Name -> IO Id
lookupBignumId Name
bignatFromWordListName
let
convertNumLit :: LitNumType -> Integer -> Maybe CpeRhs
convertNumLit LitNumType
nt Integer
i = case LitNumType
nt of
LitNumType
LitNumInteger -> CpeRhs -> Maybe CpeRhs
forall a. a -> Maybe a
Just (Integer -> CpeRhs
convertInteger Integer
i)
LitNumType
LitNumNatural -> CpeRhs -> Maybe CpeRhs
forall a. a -> Maybe a
Just (Integer -> CpeRhs
convertNatural Integer
i)
LitNumType
_ -> Maybe CpeRhs
forall a. Maybe a
Nothing
convertInteger :: Integer -> CpeRhs
convertInteger Integer
i
| Platform -> Integer -> Bool
platformInIntRange Platform
platform Integer
i
= DataCon -> [CpeRhs] -> CpeRhs
forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
integerISDataCon [Literal -> CpeRhs
forall b. Literal -> Expr b
Lit (Platform -> Integer -> Literal
mkLitInt Platform
platform Integer
i)]
| Bool
otherwise
= let con :: DataCon
con = if Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0 then DataCon
integerIPDataCon else DataCon
integerINDataCon
in DataCon -> CpeRhs -> CpeRhs
mkBigNum DataCon
con (Integer -> CpeRhs
convertBignatPrim (Integer -> Integer
forall a. Num a => a -> a
abs Integer
i))
convertNatural :: Integer -> CpeRhs
convertNatural Integer
i
| Platform -> Integer -> Bool
platformInWordRange Platform
platform Integer
i
= DataCon -> [CpeRhs] -> CpeRhs
forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
naturalNSDataCon [Literal -> CpeRhs
forall b. Literal -> Expr b
Lit (Platform -> Integer -> Literal
mkLitWord Platform
platform Integer
i)]
| Bool
otherwise
= DataCon -> CpeRhs -> CpeRhs
mkBigNum DataCon
naturalNBDataCon (Integer -> CpeRhs
convertBignatPrim Integer
i)
mkBigNum :: DataCon -> CpeRhs -> CpeRhs
mkBigNum DataCon
con CpeRhs
ba = CpeRhs -> [CpeRhs] -> CpeRhs
mkCoreApps (Id -> CpeRhs
forall b. Id -> Expr b
Var (DataCon -> Id
dataConWorkId DataCon
con)) [CpeRhs
ba]
convertBignatPrim :: Integer -> CpeRhs
convertBignatPrim Integer
i =
let
target :: Platform
target = DynFlags -> Platform
targetPlatform DynFlags
dflags
words :: CpeRhs
words = Type -> [CpeRhs] -> CpeRhs
mkListExpr Type
wordTy ([CpeRhs] -> [CpeRhs]
forall a. [a] -> [a]
reverse ((Integer -> Maybe (CpeRhs, Integer)) -> Integer -> [CpeRhs]
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr Integer -> Maybe (CpeRhs, Integer)
forall {b}. Integer -> Maybe (Expr b, Integer)
f Integer
i))
where
f :: Integer -> Maybe (Expr b, Integer)
f Integer
0 = Maybe (Expr b, Integer)
forall a. Maybe a
Nothing
f Integer
x = let low :: Integer
low = Integer
x Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
mask
high :: Integer
high = Integer
x Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` Int
bits
in (Expr b, Integer) -> Maybe (Expr b, Integer)
forall a. a -> Maybe a
Just (DataCon -> [Expr b] -> Expr b
forall b. DataCon -> [Arg b] -> Arg b
mkConApp DataCon
wordDataCon [Literal -> Expr b
forall b. Literal -> Expr b
Lit (Platform -> Integer -> Literal
mkLitWord Platform
platform Integer
low)], Integer
high)
bits :: Int
bits = Platform -> Int
platformWordSizeInBits Platform
target
mask :: Integer
mask = Integer
2 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
bits Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1
in CpeRhs -> [CpeRhs] -> CpeRhs
forall b. Expr b -> [Expr b] -> Expr b
mkApps (Id -> CpeRhs
forall b. Id -> Expr b
Var Id
bignatFromWordListId) [CpeRhs
words]
(LitNumType -> Integer -> Maybe CpeRhs)
-> IO (LitNumType -> Integer -> Maybe CpeRhs)
forall (m :: * -> *) a. Monad m => a -> m a
return LitNumType -> Integer -> Maybe CpeRhs
convertNumLit