{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

\section[Demand]{@Demand@: A decoupled implementation of a demand domain}
-}

{-# LANGUAGE CPP, FlexibleInstances, TypeSynonymInstances, RecordWildCards #-}

module Demand (
        StrDmd, UseDmd(..), Count,

        Demand, DmdShell, CleanDemand, getStrDmd, getUseDmd,
        mkProdDmd, mkOnceUsedDmd, mkManyUsedDmd, mkHeadStrict, oneifyDmd,
        toCleanDmd,
        absDmd, topDmd, botDmd, seqDmd,
        lubDmd, bothDmd,
        lazyApply1Dmd, lazyApply2Dmd, strictApply1Dmd,
        catchArgDmd,
        isTopDmd, isAbsDmd, isSeqDmd,
        peelUseCall, cleanUseDmd_maybe, strictenDmd, bothCleanDmd,
        addCaseBndrDmd,

        DmdType(..), dmdTypeDepth, lubDmdType, bothDmdType,
        nopDmdType, botDmdType, mkDmdType,
        addDemand, removeDmdTyArgs,
        BothDmdArg, mkBothDmdArg, toBothDmdArg,

        DmdEnv, emptyDmdEnv,
        peelFV, findIdDemand,

        DmdResult, CPRResult,
        isBotRes, isTopRes,
        topRes, botRes, exnRes, cprProdRes,
        vanillaCprProdRes, cprSumRes,
        appIsBottom, isBottomingSig, pprIfaceStrictSig,
        trimCPRInfo, returnsCPR_maybe,
        StrictSig(..), mkStrictSig, mkClosedStrictSig,
        nopSig, botSig, exnSig, cprProdSig,
        isTopSig, hasDemandEnvSig,
        splitStrictSig, strictSigDmdEnv,
        increaseStrictSigArity, etaExpandStrictSig,

        seqDemand, seqDemandList, seqDmdType, seqStrictSig,

        evalDmd, cleanEvalDmd, cleanEvalProdDmd, isStrictDmd,
        splitDmdTy, splitFVs,
        deferAfterIO,
        postProcessUnsat, postProcessDmdType,

        splitProdDmd_maybe, peelCallDmd, peelManyCalls, mkCallDmd,
        mkWorkerDemand, dmdTransformSig, dmdTransformDataConSig,
        dmdTransformDictSelSig, argOneShots, argsOneShots, saturatedByOneShots,
        trimToType, TypeShape(..),

        useCount, isUsedOnce, reuseEnv,
        killUsageDemand, killUsageSig, zapUsageDemand, zapUsageEnvSig,
        zapUsedOnceDemand, zapUsedOnceSig,
        strictifyDictDmd, strictifyDmd

     ) where

#include "GhclibHsVersions.h"

import GhcPrelude

import DynFlags
import Outputable
import Var ( Var )
import VarEnv
import UniqFM
import Util
import BasicTypes
import Binary
import Maybes           ( orElse )

import Type            ( Type )
import TyCon           ( isNewTyCon, isClassTyCon )
import DataCon         ( splitDataProductType_maybe )

{-
************************************************************************
*                                                                      *
        Joint domain for Strictness and Absence
*                                                                      *
************************************************************************
-}

data JointDmd s u = JD { JointDmd s u -> s
sd :: s, JointDmd s u -> u
ud :: u }
  deriving ( JointDmd s u -> JointDmd s u -> Bool
(JointDmd s u -> JointDmd s u -> Bool)
-> (JointDmd s u -> JointDmd s u -> Bool) -> Eq (JointDmd s u)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall s u. (Eq s, Eq u) => JointDmd s u -> JointDmd s u -> Bool
/= :: JointDmd s u -> JointDmd s u -> Bool
$c/= :: forall s u. (Eq s, Eq u) => JointDmd s u -> JointDmd s u -> Bool
== :: JointDmd s u -> JointDmd s u -> Bool
$c== :: forall s u. (Eq s, Eq u) => JointDmd s u -> JointDmd s u -> Bool
Eq, Int -> JointDmd s u -> ShowS
[JointDmd s u] -> ShowS
JointDmd s u -> String
(Int -> JointDmd s u -> ShowS)
-> (JointDmd s u -> String)
-> ([JointDmd s u] -> ShowS)
-> Show (JointDmd s u)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall s u. (Show s, Show u) => Int -> JointDmd s u -> ShowS
forall s u. (Show s, Show u) => [JointDmd s u] -> ShowS
forall s u. (Show s, Show u) => JointDmd s u -> String
showList :: [JointDmd s u] -> ShowS
$cshowList :: forall s u. (Show s, Show u) => [JointDmd s u] -> ShowS
show :: JointDmd s u -> String
$cshow :: forall s u. (Show s, Show u) => JointDmd s u -> String
showsPrec :: Int -> JointDmd s u -> ShowS
$cshowsPrec :: forall s u. (Show s, Show u) => Int -> JointDmd s u -> ShowS
Show )

getStrDmd :: JointDmd s u -> s
getStrDmd :: JointDmd s u -> s
getStrDmd = JointDmd s u -> s
forall s u. JointDmd s u -> s
sd

getUseDmd :: JointDmd s u -> u
getUseDmd :: JointDmd s u -> u
getUseDmd = JointDmd s u -> u
forall s u. JointDmd s u -> u
ud

-- Pretty-printing
instance (Outputable s, Outputable u) => Outputable (JointDmd s u) where
  ppr :: JointDmd s u -> SDoc
ppr (JD {sd :: forall s u. JointDmd s u -> s
sd = s
s, ud :: forall s u. JointDmd s u -> u
ud = u
u}) = SDoc -> SDoc
angleBrackets (s -> SDoc
forall a. Outputable a => a -> SDoc
ppr s
s SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
',' SDoc -> SDoc -> SDoc
<> u -> SDoc
forall a. Outputable a => a -> SDoc
ppr u
u)

-- Well-formedness preserving constructors for the joint domain
mkJointDmd :: s -> u -> JointDmd s u
mkJointDmd :: s -> u -> JointDmd s u
mkJointDmd s
s u
u = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: s
sd = s
s, ud :: u
ud = u
u }

mkJointDmds :: [s] -> [u] -> [JointDmd s u]
mkJointDmds :: [s] -> [u] -> [JointDmd s u]
mkJointDmds [s]
ss [u]
as = String -> (s -> u -> JointDmd s u) -> [s] -> [u] -> [JointDmd s u]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"mkJointDmds" s -> u -> JointDmd s u
forall s u. s -> u -> JointDmd s u
mkJointDmd [s]
ss [u]
as


{-
************************************************************************
*                                                                      *
            Strictness domain
*                                                                      *
************************************************************************

        Lazy
         |
  ExnStr x -
           |
        HeadStr
        /     \
    SCall      SProd
        \      /
        HyperStr

Note [Exceptions and strictness]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Exceptions need rather careful treatment, especially because of 'catch'
('catch#'), 'catchSTM' ('catchSTM#'), and 'orElse' ('catchRetry#').
See Trac #11555, #10712 and #13330, and for some more background, #11222.

There are three main pieces.

* The Termination type includes ThrowsExn, meaning "under the given
  demand this expression either diverges or throws an exception".

  This is relatively uncontroversial. The primops raise# and
  raiseIO# both return ThrowsExn; nothing else does.

* An ArgStr has an ExnStr flag to say how to process the Termination
  result of the argument.  If the ExnStr flag is ExnStr, we squash
  ThrowsExn to topRes.  (This is done in postProcessDmdResult.)

Here is the key example

    catchRetry# (\s -> retry# s) blah

We analyse the argument (\s -> retry# s) with demand
    Str ExnStr (SCall HeadStr)
i.e. with the ExnStr flag set.
  - First we analyse the argument with the "clean-demand" (SCall
    HeadStr), getting a DmdResult of ThrowsExn from the saturated
    application of retry#.
  - Then we apply the post-processing for the shell, squashing the
    ThrowsExn to topRes.

This also applies uniformly to free variables.  Consider

    let r = \st -> retry# st
    in catchRetry# (\s -> ...(r s')..) handler st

If we give the first argument of catch a strict signature, we'll get a demand
'C(S)' for 'r'; that is, 'r' is definitely called with one argument, which
indeed it is.  But when we post-process the free-var demands on catchRetry#'s
argument (in postProcessDmdEnv), we'll give 'r' a demand of (Str ExnStr (SCall
HeadStr)); and if we feed that into r's RHS (which would be reasonable) we'll
squash the retry just as if we'd inlined 'r'.

* We don't try to get clever about 'catch#' and 'catchSTM#' at the moment. We
previously (#11222) tried to take advantage of the fact that 'catch#' calls its
first argument eagerly. See especially commit
9915b6564403a6d17651e9969e9ea5d7d7e78e7f. We analyzed that first argument with
a strict demand, and then performed a post-processing step at the end to change
ThrowsExn to TopRes.  The trouble, I believe, is that to use this approach
correctly, we'd need somewhat different information about that argument.
Diverges, ThrowsExn (i.e., diverges or throws an exception), and Dunno are the
wrong split here.  In order to evaluate part of the argument speculatively,
we'd need to know that it *does not throw an exception*. That is, that it
either diverges or succeeds. But we don't currently have a way to talk about
that. Abstractly and approximately,

catch# m f s = case ORACLE m s of
  DivergesOrSucceeds -> m s
  Fails exc -> f exc s

where the magical ORACLE determines whether or not (m s) throws an exception
when run, and if so which one. If we want, we can safely consider (catch# m f s)
strict in anything that both branches are strict in (by performing demand
analysis for 'catch#' in the same way we do for case). We could also safely
consider it strict in anything demanded by (m s) that is guaranteed not to
throw an exception under that demand, but I don't know if we have the means
to express that.

My mind keeps turning to this model (not as an actual change to the type, but
as a way to think about what's going on in the analysis):

newtype IO a = IO {unIO :: State# s -> (# s, (# SomeException | a #) #)}
instance Monad IO where
  return a = IO $ \s -> (# s, (# | a #) #)
  IO m >>= f = IO $ \s -> case m s of
    (# s', (# e | #) #) -> (# s', e #)
    (# s', (# | a #) #) -> unIO (f a) s
raiseIO# e s = (# s, (# e | #) #)
catch# m f s = case m s of
  (# s', (# e | #) #) -> f e s'
  res -> res

Thinking about it this way seems likely to be productive for analyzing IO
exception behavior, but imprecise exceptions and asynchronous exceptions remain
quite slippery beasts. Can we incorporate them? I think we can. We can imagine
applying 'seq#' to evaluate @m s@, determining whether it throws an imprecise
or asynchronous exception or whether it succeeds or throws an IO exception.
This confines the peculiarities to 'seq#', which is indeed rather essentially
peculiar.
-}

-- | Vanilla strictness domain
data StrDmd
  = HyperStr             -- ^ Hyper-strict (bottom of the lattice).
                         -- See Note [HyperStr and Use demands]

  | SCall StrDmd         -- ^ Call demand
                         -- Used only for values of function type

  | SProd [ArgStr]       -- ^ Product
                         -- Used only for values of product type
                         -- Invariant: not all components are HyperStr (use HyperStr)
                         --            not all components are Lazy     (use HeadStr)

  | HeadStr              -- ^ Head-Strict
                         -- A polymorphic demand: used for values of all types,
                         --                       including a type variable

  deriving ( StrDmd -> StrDmd -> Bool
(StrDmd -> StrDmd -> Bool)
-> (StrDmd -> StrDmd -> Bool) -> Eq StrDmd
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StrDmd -> StrDmd -> Bool
$c/= :: StrDmd -> StrDmd -> Bool
== :: StrDmd -> StrDmd -> Bool
$c== :: StrDmd -> StrDmd -> Bool
Eq, Int -> StrDmd -> ShowS
[StrDmd] -> ShowS
StrDmd -> String
(Int -> StrDmd -> ShowS)
-> (StrDmd -> String) -> ([StrDmd] -> ShowS) -> Show StrDmd
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StrDmd] -> ShowS
$cshowList :: [StrDmd] -> ShowS
show :: StrDmd -> String
$cshow :: StrDmd -> String
showsPrec :: Int -> StrDmd -> ShowS
$cshowsPrec :: Int -> StrDmd -> ShowS
Show )

-- | Strictness of a function argument.
type ArgStr = Str StrDmd

-- | Strictness demand.
data Str s = Lazy         -- ^ Lazy (top of the lattice)
           | Str ExnStr s -- ^ Strict
  deriving ( Str s -> Str s -> Bool
(Str s -> Str s -> Bool) -> (Str s -> Str s -> Bool) -> Eq (Str s)
forall s. Eq s => Str s -> Str s -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Str s -> Str s -> Bool
$c/= :: forall s. Eq s => Str s -> Str s -> Bool
== :: Str s -> Str s -> Bool
$c== :: forall s. Eq s => Str s -> Str s -> Bool
Eq, Int -> Str s -> ShowS
[Str s] -> ShowS
Str s -> String
(Int -> Str s -> ShowS)
-> (Str s -> String) -> ([Str s] -> ShowS) -> Show (Str s)
forall s. Show s => Int -> Str s -> ShowS
forall s. Show s => [Str s] -> ShowS
forall s. Show s => Str s -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Str s] -> ShowS
$cshowList :: forall s. Show s => [Str s] -> ShowS
show :: Str s -> String
$cshow :: forall s. Show s => Str s -> String
showsPrec :: Int -> Str s -> ShowS
$cshowsPrec :: forall s. Show s => Int -> Str s -> ShowS
Show )

-- | How are exceptions handled for strict demands?
data ExnStr  -- See Note [Exceptions and strictness]
  = VanStr   -- ^ "Vanilla" case, ordinary strictness

  | ExnStr   -- ^ @Str ExnStr d@ means be strict like @d@ but then degrade
             -- the 'Termination' info 'ThrowsExn' to 'Dunno'.
             -- e.g. the first argument of @catch@ has this strictness.
  deriving( ExnStr -> ExnStr -> Bool
(ExnStr -> ExnStr -> Bool)
-> (ExnStr -> ExnStr -> Bool) -> Eq ExnStr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ExnStr -> ExnStr -> Bool
$c/= :: ExnStr -> ExnStr -> Bool
== :: ExnStr -> ExnStr -> Bool
$c== :: ExnStr -> ExnStr -> Bool
Eq, Int -> ExnStr -> ShowS
[ExnStr] -> ShowS
ExnStr -> String
(Int -> ExnStr -> ShowS)
-> (ExnStr -> String) -> ([ExnStr] -> ShowS) -> Show ExnStr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ExnStr] -> ShowS
$cshowList :: [ExnStr] -> ShowS
show :: ExnStr -> String
$cshow :: ExnStr -> String
showsPrec :: Int -> ExnStr -> ShowS
$cshowsPrec :: Int -> ExnStr -> ShowS
Show )

-- Well-formedness preserving constructors for the Strictness domain
strBot, strTop :: ArgStr
strBot :: ArgStr
strBot = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
HyperStr
strTop :: ArgStr
strTop = ArgStr
forall s. Str s
Lazy

mkSCall :: StrDmd -> StrDmd
mkSCall :: StrDmd -> StrDmd
mkSCall StrDmd
HyperStr = StrDmd
HyperStr
mkSCall StrDmd
s        = StrDmd -> StrDmd
SCall StrDmd
s

mkSProd :: [ArgStr] -> StrDmd
mkSProd :: [ArgStr] -> StrDmd
mkSProd [ArgStr]
sx
  | (ArgStr -> Bool) -> [ArgStr] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ArgStr -> Bool
isHyperStr [ArgStr]
sx = StrDmd
HyperStr
  | (ArgStr -> Bool) -> [ArgStr] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ArgStr -> Bool
isLazy     [ArgStr]
sx = StrDmd
HeadStr
  | Bool
otherwise         = [ArgStr] -> StrDmd
SProd [ArgStr]
sx

isLazy :: ArgStr -> Bool
isLazy :: ArgStr -> Bool
isLazy ArgStr
Lazy     = Bool
True
isLazy (Str {}) = Bool
False

isHyperStr :: ArgStr -> Bool
isHyperStr :: ArgStr -> Bool
isHyperStr (Str ExnStr
_ StrDmd
HyperStr) = Bool
True
isHyperStr ArgStr
_                = Bool
False

-- Pretty-printing
instance Outputable StrDmd where
  ppr :: StrDmd -> SDoc
ppr StrDmd
HyperStr      = Char -> SDoc
char Char
'B'
  ppr (SCall StrDmd
s)     = Char -> SDoc
char Char
'C' SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (StrDmd -> SDoc
forall a. Outputable a => a -> SDoc
ppr StrDmd
s)
  ppr StrDmd
HeadStr       = Char -> SDoc
char Char
'S'
  ppr (SProd [ArgStr]
sx)    = Char -> SDoc
char Char
'S' SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens ([SDoc] -> SDoc
hcat ((ArgStr -> SDoc) -> [ArgStr] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map ArgStr -> SDoc
forall a. Outputable a => a -> SDoc
ppr [ArgStr]
sx))

instance Outputable ArgStr where
  ppr :: ArgStr -> SDoc
ppr (Str ExnStr
x StrDmd
s)     = (case ExnStr
x of ExnStr
VanStr -> SDoc
empty; ExnStr
ExnStr -> Char -> SDoc
char Char
'x')
                      SDoc -> SDoc -> SDoc
<> StrDmd -> SDoc
forall a. Outputable a => a -> SDoc
ppr StrDmd
s
  ppr ArgStr
Lazy          = Char -> SDoc
char Char
'L'

lubArgStr :: ArgStr -> ArgStr -> ArgStr
lubArgStr :: ArgStr -> ArgStr -> ArgStr
lubArgStr ArgStr
Lazy        ArgStr
_           = ArgStr
forall s. Str s
Lazy
lubArgStr ArgStr
_           ArgStr
Lazy        = ArgStr
forall s. Str s
Lazy
lubArgStr (Str ExnStr
x1 StrDmd
s1) (Str ExnStr
x2 StrDmd
s2) = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str (ExnStr
x1 ExnStr -> ExnStr -> ExnStr
`lubExnStr` ExnStr
x2) (StrDmd
s1 StrDmd -> StrDmd -> StrDmd
`lubStr` StrDmd
s2)

lubExnStr :: ExnStr -> ExnStr -> ExnStr
lubExnStr :: ExnStr -> ExnStr -> ExnStr
lubExnStr ExnStr
VanStr ExnStr
VanStr = ExnStr
VanStr
lubExnStr ExnStr
_      ExnStr
_      = ExnStr
ExnStr   -- ExnStr is lazier

lubStr :: StrDmd -> StrDmd -> StrDmd
lubStr :: StrDmd -> StrDmd -> StrDmd
lubStr StrDmd
HyperStr StrDmd
s              = StrDmd
s
lubStr (SCall StrDmd
s1) StrDmd
HyperStr     = StrDmd -> StrDmd
SCall StrDmd
s1
lubStr (SCall StrDmd
_)  StrDmd
HeadStr      = StrDmd
HeadStr
lubStr (SCall StrDmd
s1) (SCall StrDmd
s2)   = StrDmd -> StrDmd
SCall (StrDmd
s1 StrDmd -> StrDmd -> StrDmd
`lubStr` StrDmd
s2)
lubStr (SCall StrDmd
_)  (SProd [ArgStr]
_)    = StrDmd
HeadStr
lubStr (SProd [ArgStr]
sx) StrDmd
HyperStr     = [ArgStr] -> StrDmd
SProd [ArgStr]
sx
lubStr (SProd [ArgStr]
_)  StrDmd
HeadStr      = StrDmd
HeadStr
lubStr (SProd [ArgStr]
s1) (SProd [ArgStr]
s2)
    | [ArgStr]
s1 [ArgStr] -> [ArgStr] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [ArgStr]
s2      = [ArgStr] -> StrDmd
mkSProd ((ArgStr -> ArgStr -> ArgStr) -> [ArgStr] -> [ArgStr] -> [ArgStr]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgStr -> ArgStr -> ArgStr
lubArgStr [ArgStr]
s1 [ArgStr]
s2)
    | Bool
otherwise                = StrDmd
HeadStr
lubStr (SProd [ArgStr]
_) (SCall StrDmd
_)     = StrDmd
HeadStr
lubStr StrDmd
HeadStr   StrDmd
_             = StrDmd
HeadStr

bothArgStr :: ArgStr -> ArgStr -> ArgStr
bothArgStr :: ArgStr -> ArgStr -> ArgStr
bothArgStr ArgStr
Lazy        ArgStr
s           = ArgStr
s
bothArgStr ArgStr
s           ArgStr
Lazy        = ArgStr
s
bothArgStr (Str ExnStr
x1 StrDmd
s1) (Str ExnStr
x2 StrDmd
s2) = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str (ExnStr
x1 ExnStr -> ExnStr -> ExnStr
`bothExnStr` ExnStr
x2) (StrDmd
s1 StrDmd -> StrDmd -> StrDmd
`bothStr` StrDmd
s2)

bothExnStr :: ExnStr -> ExnStr -> ExnStr
bothExnStr :: ExnStr -> ExnStr -> ExnStr
bothExnStr ExnStr
ExnStr ExnStr
ExnStr = ExnStr
ExnStr
bothExnStr ExnStr
_      ExnStr
_      = ExnStr
VanStr

bothStr :: StrDmd -> StrDmd -> StrDmd
bothStr :: StrDmd -> StrDmd -> StrDmd
bothStr StrDmd
HyperStr StrDmd
_             = StrDmd
HyperStr
bothStr StrDmd
HeadStr StrDmd
s              = StrDmd
s
bothStr (SCall StrDmd
_)  StrDmd
HyperStr    = StrDmd
HyperStr
bothStr (SCall StrDmd
s1) StrDmd
HeadStr     = StrDmd -> StrDmd
SCall StrDmd
s1
bothStr (SCall StrDmd
s1) (SCall StrDmd
s2)  = StrDmd -> StrDmd
SCall (StrDmd
s1 StrDmd -> StrDmd -> StrDmd
`bothStr` StrDmd
s2)
bothStr (SCall StrDmd
_)  (SProd [ArgStr]
_)   = StrDmd
HyperStr  -- Weird

bothStr (SProd [ArgStr]
_)  StrDmd
HyperStr    = StrDmd
HyperStr
bothStr (SProd [ArgStr]
s1) StrDmd
HeadStr     = [ArgStr] -> StrDmd
SProd [ArgStr]
s1
bothStr (SProd [ArgStr]
s1) (SProd [ArgStr]
s2)
    | [ArgStr]
s1 [ArgStr] -> [ArgStr] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [ArgStr]
s2      = [ArgStr] -> StrDmd
mkSProd ((ArgStr -> ArgStr -> ArgStr) -> [ArgStr] -> [ArgStr] -> [ArgStr]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgStr -> ArgStr -> ArgStr
bothArgStr [ArgStr]
s1 [ArgStr]
s2)
    | Bool
otherwise                = StrDmd
HyperStr  -- Weird
bothStr (SProd [ArgStr]
_) (SCall StrDmd
_)    = StrDmd
HyperStr

-- utility functions to deal with memory leaks
seqStrDmd :: StrDmd -> ()
seqStrDmd :: StrDmd -> ()
seqStrDmd (SProd [ArgStr]
ds)   = [ArgStr] -> ()
seqStrDmdList [ArgStr]
ds
seqStrDmd (SCall StrDmd
s)    = StrDmd -> ()
seqStrDmd StrDmd
s
seqStrDmd StrDmd
_            = ()

seqStrDmdList :: [ArgStr] -> ()
seqStrDmdList :: [ArgStr] -> ()
seqStrDmdList [] = ()
seqStrDmdList (ArgStr
d:[ArgStr]
ds) = ArgStr -> ()
seqArgStr ArgStr
d () -> () -> ()
`seq` [ArgStr] -> ()
seqStrDmdList [ArgStr]
ds

seqArgStr :: ArgStr -> ()
seqArgStr :: ArgStr -> ()
seqArgStr ArgStr
Lazy      = ()
seqArgStr (Str ExnStr
x StrDmd
s) = ExnStr
x ExnStr -> () -> ()
`seq` StrDmd -> ()
seqStrDmd StrDmd
s

-- Splitting polymorphic demands
splitArgStrProdDmd :: Int -> ArgStr -> Maybe [ArgStr]
splitArgStrProdDmd :: Int -> ArgStr -> Maybe [ArgStr]
splitArgStrProdDmd Int
n ArgStr
Lazy      = [ArgStr] -> Maybe [ArgStr]
forall a. a -> Maybe a
Just (Int -> ArgStr -> [ArgStr]
forall a. Int -> a -> [a]
replicate Int
n ArgStr
forall s. Str s
Lazy)
splitArgStrProdDmd Int
n (Str ExnStr
_ StrDmd
s) = Int -> StrDmd -> Maybe [ArgStr]
splitStrProdDmd Int
n StrDmd
s

splitStrProdDmd :: Int -> StrDmd -> Maybe [ArgStr]
splitStrProdDmd :: Int -> StrDmd -> Maybe [ArgStr]
splitStrProdDmd Int
n StrDmd
HyperStr   = [ArgStr] -> Maybe [ArgStr]
forall a. a -> Maybe a
Just (Int -> ArgStr -> [ArgStr]
forall a. Int -> a -> [a]
replicate Int
n ArgStr
strBot)
splitStrProdDmd Int
n StrDmd
HeadStr    = [ArgStr] -> Maybe [ArgStr]
forall a. a -> Maybe a
Just (Int -> ArgStr -> [ArgStr]
forall a. Int -> a -> [a]
replicate Int
n ArgStr
strTop)
splitStrProdDmd Int
n (SProd [ArgStr]
ds) = WARN( not (ds `lengthIs` n),
                                     text "splitStrProdDmd" $$ ppr n $$ ppr ds )
                               [ArgStr] -> Maybe [ArgStr]
forall a. a -> Maybe a
Just [ArgStr]
ds
splitStrProdDmd Int
_ (SCall {}) = Maybe [ArgStr]
forall a. Maybe a
Nothing
      -- This can happen when the programmer uses unsafeCoerce,
      -- and we don't then want to crash the compiler (Trac #9208)

{-
************************************************************************
*                                                                      *
            Absence domain
*                                                                      *
************************************************************************

         Used
         /   \
     UCall   UProd
         \   /
         UHead
          |
  Count x -
        |
       Abs
-}

-- | Domain for genuine usage
data UseDmd
  = UCall Count UseDmd   -- ^ Call demand for absence.
                         -- Used only for values of function type

  | UProd [ArgUse]       -- ^ Product.
                         -- Used only for values of product type
                         -- See Note [Don't optimise UProd(Used) to Used]
                         --
                         -- Invariant: Not all components are Abs
                         -- (in that case, use UHead)

  | UHead                -- ^ May be used but its sub-components are
                         -- definitely *not* used.  For product types, UHead
                         -- is equivalent to U(AAA); see mkUProd.
                         --
                         -- UHead is needed only to express the demand
                         -- of 'seq' and 'case' which are polymorphic;
                         -- i.e. the scrutinised value is of type 'a'
                         -- rather than a product type. That's why we
                         -- can't use UProd [A,A,A]
                         --
                         -- Since (UCall _ Abs) is ill-typed, UHead doesn't
                         -- make sense for lambdas

  | Used                 -- ^ May be used and its sub-components may be used.
                         -- (top of the lattice)
  deriving ( UseDmd -> UseDmd -> Bool
(UseDmd -> UseDmd -> Bool)
-> (UseDmd -> UseDmd -> Bool) -> Eq UseDmd
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UseDmd -> UseDmd -> Bool
$c/= :: UseDmd -> UseDmd -> Bool
== :: UseDmd -> UseDmd -> Bool
$c== :: UseDmd -> UseDmd -> Bool
Eq, Int -> UseDmd -> ShowS
[UseDmd] -> ShowS
UseDmd -> String
(Int -> UseDmd -> ShowS)
-> (UseDmd -> String) -> ([UseDmd] -> ShowS) -> Show UseDmd
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UseDmd] -> ShowS
$cshowList :: [UseDmd] -> ShowS
show :: UseDmd -> String
$cshow :: UseDmd -> String
showsPrec :: Int -> UseDmd -> ShowS
$cshowsPrec :: Int -> UseDmd -> ShowS
Show )

-- Extended usage demand for absence and counting
type ArgUse = Use UseDmd

data Use u
  = Abs             -- Definitely unused
                    -- Bottom of the lattice

  | Use Count u     -- May be used with some cardinality
  deriving ( Use u -> Use u -> Bool
(Use u -> Use u -> Bool) -> (Use u -> Use u -> Bool) -> Eq (Use u)
forall u. Eq u => Use u -> Use u -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Use u -> Use u -> Bool
$c/= :: forall u. Eq u => Use u -> Use u -> Bool
== :: Use u -> Use u -> Bool
$c== :: forall u. Eq u => Use u -> Use u -> Bool
Eq, Int -> Use u -> ShowS
[Use u] -> ShowS
Use u -> String
(Int -> Use u -> ShowS)
-> (Use u -> String) -> ([Use u] -> ShowS) -> Show (Use u)
forall u. Show u => Int -> Use u -> ShowS
forall u. Show u => [Use u] -> ShowS
forall u. Show u => Use u -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Use u] -> ShowS
$cshowList :: forall u. Show u => [Use u] -> ShowS
show :: Use u -> String
$cshow :: forall u. Show u => Use u -> String
showsPrec :: Int -> Use u -> ShowS
$cshowsPrec :: forall u. Show u => Int -> Use u -> ShowS
Show )

-- | Abstract counting of usages
data Count = One | Many
  deriving ( Count -> Count -> Bool
(Count -> Count -> Bool) -> (Count -> Count -> Bool) -> Eq Count
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Count -> Count -> Bool
$c/= :: Count -> Count -> Bool
== :: Count -> Count -> Bool
$c== :: Count -> Count -> Bool
Eq, Int -> Count -> ShowS
[Count] -> ShowS
Count -> String
(Int -> Count -> ShowS)
-> (Count -> String) -> ([Count] -> ShowS) -> Show Count
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Count] -> ShowS
$cshowList :: [Count] -> ShowS
show :: Count -> String
$cshow :: Count -> String
showsPrec :: Int -> Count -> ShowS
$cshowsPrec :: Int -> Count -> ShowS
Show )

-- Pretty-printing
instance Outputable ArgUse where
  ppr :: ArgUse -> SDoc
ppr ArgUse
Abs           = Char -> SDoc
char Char
'A'
  ppr (Use Count
Many UseDmd
a)   = UseDmd -> SDoc
forall a. Outputable a => a -> SDoc
ppr UseDmd
a
  ppr (Use Count
One  UseDmd
a)   = Char -> SDoc
char Char
'1' SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> UseDmd -> SDoc
forall a. Outputable a => a -> SDoc
ppr UseDmd
a

instance Outputable UseDmd where
  ppr :: UseDmd -> SDoc
ppr UseDmd
Used           = Char -> SDoc
char Char
'U'
  ppr (UCall Count
c UseDmd
a)    = Char -> SDoc
char Char
'C' SDoc -> SDoc -> SDoc
<> Count -> SDoc
forall a. Outputable a => a -> SDoc
ppr Count
c SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (UseDmd -> SDoc
forall a. Outputable a => a -> SDoc
ppr UseDmd
a)
  ppr UseDmd
UHead          = Char -> SDoc
char Char
'H'
  ppr (UProd [ArgUse]
as)     = Char -> SDoc
char Char
'U' SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens ([SDoc] -> SDoc
hcat (SDoc -> [SDoc] -> [SDoc]
punctuate (Char -> SDoc
char Char
',') ((ArgUse -> SDoc) -> [ArgUse] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map ArgUse -> SDoc
forall a. Outputable a => a -> SDoc
ppr [ArgUse]
as)))

instance Outputable Count where
  ppr :: Count -> SDoc
ppr Count
One  = Char -> SDoc
char Char
'1'
  ppr Count
Many = String -> SDoc
text String
""

useBot, useTop :: ArgUse
useBot :: ArgUse
useBot     = ArgUse
forall u. Use u
Abs
useTop :: ArgUse
useTop     = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many UseDmd
Used

mkUCall :: Count -> UseDmd -> UseDmd
--mkUCall c Used = Used c
mkUCall :: Count -> UseDmd -> UseDmd
mkUCall Count
c UseDmd
a  = Count -> UseDmd -> UseDmd
UCall Count
c UseDmd
a

mkUProd :: [ArgUse] -> UseDmd
mkUProd :: [ArgUse] -> UseDmd
mkUProd [ArgUse]
ux
  | (ArgUse -> Bool) -> [ArgUse] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (ArgUse -> ArgUse -> Bool
forall a. Eq a => a -> a -> Bool
== ArgUse
forall u. Use u
Abs) [ArgUse]
ux    = UseDmd
UHead
  | Bool
otherwise          = [ArgUse] -> UseDmd
UProd [ArgUse]
ux

lubCount :: Count -> Count -> Count
lubCount :: Count -> Count -> Count
lubCount Count
_ Count
Many = Count
Many
lubCount Count
Many Count
_ = Count
Many
lubCount Count
x Count
_    = Count
x

lubArgUse :: ArgUse -> ArgUse -> ArgUse
lubArgUse :: ArgUse -> ArgUse -> ArgUse
lubArgUse ArgUse
Abs ArgUse
x                   = ArgUse
x
lubArgUse ArgUse
x ArgUse
Abs                   = ArgUse
x
lubArgUse (Use Count
c1 UseDmd
a1) (Use Count
c2 UseDmd
a2) = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use (Count -> Count -> Count
lubCount Count
c1 Count
c2) (UseDmd -> UseDmd -> UseDmd
lubUse UseDmd
a1 UseDmd
a2)

lubUse :: UseDmd -> UseDmd -> UseDmd
lubUse :: UseDmd -> UseDmd -> UseDmd
lubUse UseDmd
UHead       UseDmd
u               = UseDmd
u
lubUse (UCall Count
c UseDmd
u) UseDmd
UHead           = Count -> UseDmd -> UseDmd
UCall Count
c UseDmd
u
lubUse (UCall Count
c1 UseDmd
u1) (UCall Count
c2 UseDmd
u2) = Count -> UseDmd -> UseDmd
UCall (Count -> Count -> Count
lubCount Count
c1 Count
c2) (UseDmd -> UseDmd -> UseDmd
lubUse UseDmd
u1 UseDmd
u2)
lubUse (UCall Count
_ UseDmd
_) UseDmd
_               = UseDmd
Used
lubUse (UProd [ArgUse]
ux) UseDmd
UHead            = [ArgUse] -> UseDmd
UProd [ArgUse]
ux
lubUse (UProd [ArgUse]
ux1) (UProd [ArgUse]
ux2)
     | [ArgUse]
ux1 [ArgUse] -> [ArgUse] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [ArgUse]
ux2       = [ArgUse] -> UseDmd
UProd ([ArgUse] -> UseDmd) -> [ArgUse] -> UseDmd
forall a b. (a -> b) -> a -> b
$ (ArgUse -> ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse] -> [ArgUse]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgUse -> ArgUse -> ArgUse
lubArgUse [ArgUse]
ux1 [ArgUse]
ux2
     | Bool
otherwise                   = UseDmd
Used
lubUse (UProd {}) (UCall {})       = UseDmd
Used
-- lubUse (UProd {}) Used             = Used
lubUse (UProd [ArgUse]
ux) UseDmd
Used             = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map (ArgUse -> ArgUse -> ArgUse
`lubArgUse` ArgUse
useTop) [ArgUse]
ux)
lubUse UseDmd
Used       (UProd [ArgUse]
ux)       = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map (ArgUse -> ArgUse -> ArgUse
`lubArgUse` ArgUse
useTop) [ArgUse]
ux)
lubUse UseDmd
Used UseDmd
_                      = UseDmd
Used  -- Note [Used should win]

-- `both` is different from `lub` in its treatment of counting; if
-- `both` is computed for two used, the result always has
--  cardinality `Many` (except for the inner demands of UCall demand -- [TODO] explain).
--  Also,  x `bothUse` x /= x (for anything but Abs).

bothArgUse :: ArgUse -> ArgUse -> ArgUse
bothArgUse :: ArgUse -> ArgUse -> ArgUse
bothArgUse ArgUse
Abs ArgUse
x                   = ArgUse
x
bothArgUse ArgUse
x ArgUse
Abs                   = ArgUse
x
bothArgUse (Use Count
_ UseDmd
a1) (Use Count
_ UseDmd
a2)   = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many (UseDmd -> UseDmd -> UseDmd
bothUse UseDmd
a1 UseDmd
a2)


bothUse :: UseDmd -> UseDmd -> UseDmd
bothUse :: UseDmd -> UseDmd -> UseDmd
bothUse UseDmd
UHead       UseDmd
u               = UseDmd
u
bothUse (UCall Count
c UseDmd
u) UseDmd
UHead           = Count -> UseDmd -> UseDmd
UCall Count
c UseDmd
u

-- Exciting special treatment of inner demand for call demands:
--    use `lubUse` instead of `bothUse`!
bothUse (UCall Count
_ UseDmd
u1) (UCall Count
_ UseDmd
u2)   = Count -> UseDmd -> UseDmd
UCall Count
Many (UseDmd
u1 UseDmd -> UseDmd -> UseDmd
`lubUse` UseDmd
u2)

bothUse (UCall {}) UseDmd
_                = UseDmd
Used
bothUse (UProd [ArgUse]
ux) UseDmd
UHead            = [ArgUse] -> UseDmd
UProd [ArgUse]
ux
bothUse (UProd [ArgUse]
ux1) (UProd [ArgUse]
ux2)
      | [ArgUse]
ux1 [ArgUse] -> [ArgUse] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [ArgUse]
ux2       = [ArgUse] -> UseDmd
UProd ([ArgUse] -> UseDmd) -> [ArgUse] -> UseDmd
forall a b. (a -> b) -> a -> b
$ (ArgUse -> ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse] -> [ArgUse]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgUse -> ArgUse -> ArgUse
bothArgUse [ArgUse]
ux1 [ArgUse]
ux2
      | Bool
otherwise                   = UseDmd
Used
bothUse (UProd {}) (UCall {})       = UseDmd
Used
-- bothUse (UProd {}) Used             = Used  -- Note [Used should win]
bothUse UseDmd
Used (UProd [ArgUse]
ux)             = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map (ArgUse -> ArgUse -> ArgUse
`bothArgUse` ArgUse
useTop) [ArgUse]
ux)
bothUse (UProd [ArgUse]
ux) UseDmd
Used             = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map (ArgUse -> ArgUse -> ArgUse
`bothArgUse` ArgUse
useTop) [ArgUse]
ux)
bothUse UseDmd
Used UseDmd
_                      = UseDmd
Used  -- Note [Used should win]

peelUseCall :: UseDmd -> Maybe (Count, UseDmd)
peelUseCall :: UseDmd -> Maybe (Count, UseDmd)
peelUseCall (UCall Count
c UseDmd
u)   = (Count, UseDmd) -> Maybe (Count, UseDmd)
forall a. a -> Maybe a
Just (Count
c,UseDmd
u)
peelUseCall UseDmd
_             = Maybe (Count, UseDmd)
forall a. Maybe a
Nothing

addCaseBndrDmd :: Demand    -- On the case binder
               -> [Demand]  -- On the components of the constructor
               -> [Demand]  -- Final demands for the components of the constructor
-- See Note [Demand on case-alternative binders]
addCaseBndrDmd :: Demand -> [Demand] -> [Demand]
addCaseBndrDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
ms, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
mu }) [Demand]
alt_dmds
  = case ArgUse
mu of
     ArgUse
Abs     -> [Demand]
alt_dmds
     Use Count
_ UseDmd
u -> (Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Demand -> Demand -> Demand
bothDmd [Demand]
alt_dmds ([ArgStr] -> [ArgUse] -> [Demand]
forall s u. [s] -> [u] -> [JointDmd s u]
mkJointDmds [ArgStr]
ss [ArgUse]
us)
             where
                Just [ArgStr]
ss = Int -> ArgStr -> Maybe [ArgStr]
splitArgStrProdDmd Int
arity ArgStr
ms  -- Guaranteed not to be a call
                Just [ArgUse]
us = Int -> UseDmd -> Maybe [ArgUse]
splitUseProdDmd      Int
arity UseDmd
u   -- Ditto
  where
    arity :: Int
arity = [Demand] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Demand]
alt_dmds

{- Note [Demand on case-alternative binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The demand on a binder in a case alternative comes
  (a) From the demand on the binder itself
  (b) From the demand on the case binder
Forgetting (b) led directly to Trac #10148.

Example. Source code:
  f x@(p,_) = if p then foo x else True

  foo (p,True) = True
  foo (p,q)    = foo (q,p)

After strictness analysis:
  f = \ (x_an1 [Dmd=<S(SL),1*U(U,1*U)>] :: (Bool, Bool)) ->
      case x_an1
      of wild_X7 [Dmd=<L,1*U(1*U,1*U)>]
      { (p_an2 [Dmd=<S,1*U>], ds_dnz [Dmd=<L,A>]) ->
      case p_an2 of _ {
        False -> GHC.Types.True;
        True -> foo wild_X7 }

It's true that ds_dnz is *itself* absent, but the use of wild_X7 means
that it is very much alive and demanded.  See Trac #10148 for how the
consequences play out.

This is needed even for non-product types, in case the case-binder
is used but the components of the case alternative are not.

Note [Don't optimise UProd(Used) to Used]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
These two UseDmds:
   UProd [Used, Used]   and    Used
are semantically equivalent, but we do not turn the former into
the latter, for a regrettable-subtle reason.  Suppose we did.
then
  f (x,y) = (y,x)
would get
  StrDmd = Str  = SProd [Lazy, Lazy]
  UseDmd = Used = UProd [Used, Used]
But with the joint demand of <Str, Used> doesn't convey any clue
that there is a product involved, and so the worthSplittingFun
will not fire.  (We'd need to use the type as well to make it fire.)
Moreover, consider
  g h p@(_,_) = h p
This too would get <Str, Used>, but this time there really isn't any
point in w/w since the components of the pair are not used at all.

So the solution is: don't aggressively collapse UProd [Used,Used] to
Used; intead leave it as-is. In effect we are using the UseDmd to do a
little bit of boxity analysis.  Not very nice.

Note [Used should win]
~~~~~~~~~~~~~~~~~~~~~~
Both in lubUse and bothUse we want (Used `both` UProd us) to be Used.
Why?  Because Used carries the implication the whole thing is used,
box and all, so we don't want to w/w it.  If we use it both boxed and
unboxed, then we are definitely using the box, and so we are quite
likely to pay a reboxing cost.  So we make Used win here.

Example is in the Buffer argument of GHC.IO.Handle.Internals.writeCharBuffer

Baseline: (A) Not making Used win (UProd wins)
Compare with: (B) making Used win for lub and both

            Min          -0.3%     -5.6%    -10.7%    -11.0%    -33.3%
            Max          +0.3%    +45.6%    +11.5%    +11.5%     +6.9%
 Geometric Mean          -0.0%     +0.5%     +0.3%     +0.2%     -0.8%

Baseline: (B) Making Used win for both lub and both
Compare with: (C) making Used win for both, but UProd win for lub

            Min          -0.1%     -0.3%     -7.9%     -8.0%     -6.5%
            Max          +0.1%     +1.0%    +21.0%    +21.0%     +0.5%
 Geometric Mean          +0.0%     +0.0%     -0.0%     -0.1%     -0.1%
-}

-- If a demand is used multiple times (i.e. reused), than any use-once
-- mentioned there, that is not protected by a UCall, can happen many times.
markReusedDmd :: ArgUse -> ArgUse
markReusedDmd :: ArgUse -> ArgUse
markReusedDmd ArgUse
Abs         = ArgUse
forall u. Use u
Abs
markReusedDmd (Use Count
_ UseDmd
a)   = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many (UseDmd -> UseDmd
markReused UseDmd
a)

markReused :: UseDmd -> UseDmd
markReused :: UseDmd -> UseDmd
markReused (UCall Count
_ UseDmd
u)      = Count -> UseDmd -> UseDmd
UCall Count
Many UseDmd
u   -- No need to recurse here
markReused (UProd [ArgUse]
ux)       = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map ArgUse -> ArgUse
markReusedDmd [ArgUse]
ux)
markReused UseDmd
u                = UseDmd
u

isUsedMU :: ArgUse -> Bool
-- True <=> markReusedDmd d = d
isUsedMU :: ArgUse -> Bool
isUsedMU ArgUse
Abs          = Bool
True
isUsedMU (Use Count
One UseDmd
_)  = Bool
False
isUsedMU (Use Count
Many UseDmd
u) = UseDmd -> Bool
isUsedU UseDmd
u

isUsedU :: UseDmd -> Bool
-- True <=> markReused d = d
isUsedU :: UseDmd -> Bool
isUsedU UseDmd
Used           = Bool
True
isUsedU UseDmd
UHead          = Bool
True
isUsedU (UProd [ArgUse]
us)     = (ArgUse -> Bool) -> [ArgUse] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ArgUse -> Bool
isUsedMU [ArgUse]
us
isUsedU (UCall Count
One UseDmd
_)  = Bool
False
isUsedU (UCall Count
Many UseDmd
_) = Bool
True  -- No need to recurse

-- Squashing usage demand demands
seqUseDmd :: UseDmd -> ()
seqUseDmd :: UseDmd -> ()
seqUseDmd (UProd [ArgUse]
ds)   = [ArgUse] -> ()
seqArgUseList [ArgUse]
ds
seqUseDmd (UCall Count
c UseDmd
d)  = Count
c Count -> () -> ()
`seq` UseDmd -> ()
seqUseDmd UseDmd
d
seqUseDmd UseDmd
_            = ()

seqArgUseList :: [ArgUse] -> ()
seqArgUseList :: [ArgUse] -> ()
seqArgUseList []     = ()
seqArgUseList (ArgUse
d:[ArgUse]
ds) = ArgUse -> ()
seqArgUse ArgUse
d () -> () -> ()
`seq` [ArgUse] -> ()
seqArgUseList [ArgUse]
ds

seqArgUse :: ArgUse -> ()
seqArgUse :: ArgUse -> ()
seqArgUse (Use Count
c UseDmd
u)  = Count
c Count -> () -> ()
`seq` UseDmd -> ()
seqUseDmd UseDmd
u
seqArgUse ArgUse
_          = ()

-- Splitting polymorphic Maybe-Used demands
splitUseProdDmd :: Int -> UseDmd -> Maybe [ArgUse]
splitUseProdDmd :: Int -> UseDmd -> Maybe [ArgUse]
splitUseProdDmd Int
n UseDmd
Used        = [ArgUse] -> Maybe [ArgUse]
forall a. a -> Maybe a
Just (Int -> ArgUse -> [ArgUse]
forall a. Int -> a -> [a]
replicate Int
n ArgUse
useTop)
splitUseProdDmd Int
n UseDmd
UHead       = [ArgUse] -> Maybe [ArgUse]
forall a. a -> Maybe a
Just (Int -> ArgUse -> [ArgUse]
forall a. Int -> a -> [a]
replicate Int
n ArgUse
forall u. Use u
Abs)
splitUseProdDmd Int
n (UProd [ArgUse]
ds)  = WARN( not (ds `lengthIs` n),
                                      text "splitUseProdDmd" $$ ppr n
                                                             $$ ppr ds )
                                [ArgUse] -> Maybe [ArgUse]
forall a. a -> Maybe a
Just [ArgUse]
ds
splitUseProdDmd Int
_ (UCall Count
_ UseDmd
_) = Maybe [ArgUse]
forall a. Maybe a
Nothing
      -- This can happen when the programmer uses unsafeCoerce,
      -- and we don't then want to crash the compiler (Trac #9208)

useCount :: Use u -> Count
useCount :: Use u -> Count
useCount Use u
Abs         = Count
One
useCount (Use Count
One u
_) = Count
One
useCount Use u
_           = Count
Many


{-
************************************************************************
*                                                                      *
         Clean demand for Strictness and Usage
*                                                                      *
************************************************************************

This domain differst from JointDemand in the sence that pure absence
is taken away, i.e., we deal *only* with non-absent demands.

Note [Strict demands]
~~~~~~~~~~~~~~~~~~~~~
isStrictDmd returns true only of demands that are
   both strict
   and  used
In particular, it is False for <HyperStr, Abs>, which can and does
arise in, say (Trac #7319)
   f x = raise# <some exception>
Then 'x' is not used, so f gets strictness <HyperStr,Abs> -> .
Now the w/w generates
   fx = let x <HyperStr,Abs> = absentError "unused"
        in raise <some exception>
At this point we really don't want to convert to
   fx = case absentError "unused" of x -> raise <some exception>
Since the program is going to diverge, this swaps one error for another,
but it's really a bad idea to *ever* evaluate an absent argument.
In Trac #7319 we get
   T7319.exe: Oops!  Entered absent arg w_s1Hd{v} [lid] [base:GHC.Base.String{tc 36u}]

Note [Dealing with call demands]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Call demands are constructed and deconstructed coherently for
strictness and absence. For instance, the strictness signature for the
following function

f :: (Int -> (Int, Int)) -> (Int, Bool)
f g = (snd (g 3), True)

should be: <L,C(U(AU))>m
-}

type CleanDemand = JointDmd StrDmd UseDmd
     -- A demand that is at least head-strict

bothCleanDmd :: CleanDemand -> CleanDemand -> CleanDemand
bothCleanDmd :: CleanDemand -> CleanDemand -> CleanDemand
bothCleanDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s1, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
a1}) (JD { sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s2, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
a2})
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd
s1 StrDmd -> StrDmd -> StrDmd
`bothStr` StrDmd
s2, ud :: UseDmd
ud = UseDmd
a1 UseDmd -> UseDmd -> UseDmd
`bothUse` UseDmd
a2 }

mkHeadStrict :: CleanDemand -> CleanDemand
mkHeadStrict :: CleanDemand -> CleanDemand
mkHeadStrict CleanDemand
cd = CleanDemand
cd { sd :: StrDmd
sd = StrDmd
HeadStr }

mkOnceUsedDmd, mkManyUsedDmd :: CleanDemand -> Demand
mkOnceUsedDmd :: CleanDemand -> Demand
mkOnceUsedDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s,ud :: forall s u. JointDmd s u -> u
ud = UseDmd
a}) = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
s, ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One UseDmd
a }
mkManyUsedDmd :: CleanDemand -> Demand
mkManyUsedDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s,ud :: forall s u. JointDmd s u -> u
ud = UseDmd
a}) = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
s, ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many UseDmd
a }

evalDmd :: Demand
-- Evaluated strictly, and used arbitrarily deeply
evalDmd :: Demand
evalDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
HeadStr, ud :: ArgUse
ud = ArgUse
useTop }

mkProdDmd :: [Demand] -> CleanDemand
mkProdDmd :: [Demand] -> CleanDemand
mkProdDmd [Demand]
dx
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = [ArgStr] -> StrDmd
mkSProd ([ArgStr] -> StrDmd) -> [ArgStr] -> StrDmd
forall a b. (a -> b) -> a -> b
$ (Demand -> ArgStr) -> [Demand] -> [ArgStr]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> ArgStr
forall s u. JointDmd s u -> s
getStrDmd [Demand]
dx
       , ud :: UseDmd
ud = [ArgUse] -> UseDmd
mkUProd ([ArgUse] -> UseDmd) -> [ArgUse] -> UseDmd
forall a b. (a -> b) -> a -> b
$ (Demand -> ArgUse) -> [Demand] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> ArgUse
forall s u. JointDmd s u -> u
getUseDmd [Demand]
dx }

mkCallDmd :: CleanDemand -> CleanDemand
mkCallDmd :: CleanDemand -> CleanDemand
mkCallDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = StrDmd
d, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
u})
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd -> StrDmd
mkSCall StrDmd
d, ud :: UseDmd
ud = Count -> UseDmd -> UseDmd
mkUCall Count
One UseDmd
u }

-- See Note [Demand on the worker] in WorkWrap
mkWorkerDemand :: Int -> Demand
mkWorkerDemand :: Int -> Demand
mkWorkerDemand Int
n = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
forall s. Str s
Lazy, ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One (Int -> UseDmd
forall t. (Eq t, Num t) => t -> UseDmd
go Int
n) }
  where go :: t -> UseDmd
go t
0 = UseDmd
Used
        go t
n = Count -> UseDmd -> UseDmd
mkUCall Count
One (UseDmd -> UseDmd) -> UseDmd -> UseDmd
forall a b. (a -> b) -> a -> b
$ t -> UseDmd
go (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1)

cleanEvalDmd :: CleanDemand
cleanEvalDmd :: CleanDemand
cleanEvalDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd
HeadStr, ud :: UseDmd
ud = UseDmd
Used }

cleanEvalProdDmd :: Arity -> CleanDemand
cleanEvalProdDmd :: Int -> CleanDemand
cleanEvalProdDmd Int
n = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd
HeadStr, ud :: UseDmd
ud = [ArgUse] -> UseDmd
UProd (Int -> ArgUse -> [ArgUse]
forall a. Int -> a -> [a]
replicate Int
n ArgUse
useTop) }


{-
************************************************************************
*                                                                      *
           Demand: combining stricness and usage
*                                                                      *
************************************************************************
-}

type Demand = JointDmd ArgStr ArgUse

lubDmd :: Demand -> Demand -> Demand
lubDmd :: Demand -> Demand -> Demand
lubDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s1, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a1}) (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s2, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a2})
 = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
s1 ArgStr -> ArgStr -> ArgStr
`lubArgStr` ArgStr
s2
      , ud :: ArgUse
ud = ArgUse
a1 ArgUse -> ArgUse -> ArgUse
`lubArgUse` ArgUse
a2 }

bothDmd :: Demand -> Demand -> Demand
bothDmd :: Demand -> Demand -> Demand
bothDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s1, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a1}) (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s2, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a2})
 = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
s1 ArgStr -> ArgStr -> ArgStr
`bothArgStr` ArgStr
s2
      , ud :: ArgUse
ud = ArgUse
a1 ArgUse -> ArgUse -> ArgUse
`bothArgUse` ArgUse
a2 }

lazyApply1Dmd, lazyApply2Dmd, strictApply1Dmd, catchArgDmd :: Demand

strictApply1Dmd :: Demand
strictApply1Dmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr (StrDmd -> StrDmd
SCall StrDmd
HeadStr)
                     , ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many (Count -> UseDmd -> UseDmd
UCall Count
One UseDmd
Used) }

-- First argument of catchRetry# and catchSTM#:
--    uses its arg once, applies it once
--    and catches exceptions (the ExnStr) part
catchArgDmd :: Demand
catchArgDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
ExnStr (StrDmd -> StrDmd
SCall StrDmd
HeadStr)
                 , ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One (Count -> UseDmd -> UseDmd
UCall Count
One UseDmd
Used) }

lazyApply1Dmd :: Demand
lazyApply1Dmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
forall s. Str s
Lazy
                   , ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One (Count -> UseDmd -> UseDmd
UCall Count
One UseDmd
Used) }

-- Second argument of catch#:
--    uses its arg at most once, applies it once
--    but is lazy (might not be called at all)
lazyApply2Dmd :: Demand
lazyApply2Dmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
forall s. Str s
Lazy
                   , ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One (Count -> UseDmd -> UseDmd
UCall Count
One (Count -> UseDmd -> UseDmd
UCall Count
One UseDmd
Used)) }

absDmd :: Demand
absDmd :: Demand
absDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
forall s. Str s
Lazy, ud :: ArgUse
ud = ArgUse
forall u. Use u
Abs }

topDmd :: Demand
topDmd :: Demand
topDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
forall s. Str s
Lazy, ud :: ArgUse
ud = ArgUse
useTop }

botDmd :: Demand
botDmd :: Demand
botDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
strBot, ud :: ArgUse
ud = ArgUse
useBot }

seqDmd :: Demand
seqDmd :: Demand
seqDmd = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
HeadStr, ud :: ArgUse
ud = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
One UseDmd
UHead }

oneifyDmd :: JointDmd s (Use u) -> JointDmd s (Use u)
oneifyDmd :: JointDmd s (Use u) -> JointDmd s (Use u)
oneifyDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = s
s, ud :: forall s u. JointDmd s u -> u
ud = Use Count
_ u
a }) = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: s
sd = s
s, ud :: Use u
ud = Count -> u -> Use u
forall u. Count -> u -> Use u
Use Count
One u
a }
oneifyDmd JointDmd s (Use u)
jd                            = JointDmd s (Use u)
jd

isTopDmd :: Demand -> Bool
-- Used to suppress pretty-printing of an uninformative demand
isTopDmd :: Demand -> Bool
isTopDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
Lazy, ud :: forall s u. JointDmd s u -> u
ud = Use Count
Many UseDmd
Used}) = Bool
True
isTopDmd Demand
_                                    = Bool
False

isAbsDmd :: JointDmd (Str s) (Use u) -> Bool
isAbsDmd :: JointDmd (Str s) (Use u) -> Bool
isAbsDmd (JD {ud :: forall s u. JointDmd s u -> u
ud = Use u
Abs}) = Bool
True   -- The strictness part can be HyperStr
isAbsDmd JointDmd (Str s) (Use u)
_               = Bool
False  -- for a bottom demand

isSeqDmd :: Demand -> Bool
isSeqDmd :: Demand -> Bool
isSeqDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = Str ExnStr
VanStr StrDmd
HeadStr, ud :: forall s u. JointDmd s u -> u
ud = Use Count
_ UseDmd
UHead}) = Bool
True
isSeqDmd Demand
_                                                = Bool
False

isUsedOnce :: JointDmd (Str s) (Use u) -> Bool
isUsedOnce :: JointDmd (Str s) (Use u) -> Bool
isUsedOnce (JD { ud :: forall s u. JointDmd s u -> u
ud = Use u
a }) = case Use u -> Count
forall u. Use u -> Count
useCount Use u
a of
                               Count
One  -> Bool
True
                               Count
Many -> Bool
False

-- More utility functions for strictness
seqDemand :: Demand -> ()
seqDemand :: Demand -> ()
seqDemand (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
u}) = ArgStr -> ()
seqArgStr ArgStr
s () -> () -> ()
`seq` ArgUse -> ()
seqArgUse ArgUse
u

seqDemandList :: [Demand] -> ()
seqDemandList :: [Demand] -> ()
seqDemandList [] = ()
seqDemandList (Demand
d:[Demand]
ds) = Demand -> ()
seqDemand Demand
d () -> () -> ()
`seq` [Demand] -> ()
seqDemandList [Demand]
ds

isStrictDmd :: JointDmd (Str s) (Use u) -> Bool
-- See Note [Strict demands]
isStrictDmd :: JointDmd (Str s) (Use u) -> Bool
isStrictDmd (JD {ud :: forall s u. JointDmd s u -> u
ud = Use u
Abs})  = Bool
False
isStrictDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = Str s
Lazy}) = Bool
False
isStrictDmd JointDmd (Str s) (Use u)
_                = Bool
True

isWeakDmd :: Demand -> Bool
isWeakDmd :: Demand -> Bool
isWeakDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a}) = ArgStr -> Bool
isLazy ArgStr
s Bool -> Bool -> Bool
&& ArgUse -> Bool
isUsedMU ArgUse
a

cleanUseDmd_maybe :: Demand -> Maybe UseDmd
cleanUseDmd_maybe :: Demand -> Maybe UseDmd
cleanUseDmd_maybe (JD { ud :: forall s u. JointDmd s u -> u
ud = Use Count
_ UseDmd
u }) = UseDmd -> Maybe UseDmd
forall a. a -> Maybe a
Just UseDmd
u
cleanUseDmd_maybe Demand
_                     = Maybe UseDmd
forall a. Maybe a
Nothing

splitFVs :: Bool   -- Thunk
         -> DmdEnv -> (DmdEnv, DmdEnv)
splitFVs :: Bool -> DmdEnv -> (DmdEnv, DmdEnv)
splitFVs Bool
is_thunk DmdEnv
rhs_fvs
  | Bool
is_thunk  = (Unique -> Demand -> (DmdEnv, DmdEnv) -> (DmdEnv, DmdEnv))
-> (DmdEnv, DmdEnv) -> DmdEnv -> (DmdEnv, DmdEnv)
forall elt a. (Unique -> elt -> a -> a) -> a -> UniqFM elt -> a
nonDetFoldUFM_Directly Unique -> Demand -> (DmdEnv, DmdEnv) -> (DmdEnv, DmdEnv)
forall s u u.
Unique
-> JointDmd (Str s) u
-> (UniqFM (JointDmd (Str s) u), UniqFM (JointDmd (Str s) (Use u)))
-> (UniqFM (JointDmd (Str s) u), UniqFM (JointDmd (Str s) (Use u)))
add (DmdEnv
forall a. VarEnv a
emptyVarEnv, DmdEnv
forall a. VarEnv a
emptyVarEnv) DmdEnv
rhs_fvs
                -- It's OK to use nonDetFoldUFM_Directly because we
                -- immediately forget the ordering by putting the elements
                -- in the envs again
  | Bool
otherwise = (Demand -> Bool) -> DmdEnv -> (DmdEnv, DmdEnv)
forall a. (a -> Bool) -> VarEnv a -> (VarEnv a, VarEnv a)
partitionVarEnv Demand -> Bool
isWeakDmd DmdEnv
rhs_fvs
  where
    add :: Unique
-> JointDmd (Str s) u
-> (UniqFM (JointDmd (Str s) u), UniqFM (JointDmd (Str s) (Use u)))
-> (UniqFM (JointDmd (Str s) u), UniqFM (JointDmd (Str s) (Use u)))
add Unique
uniq dmd :: JointDmd (Str s) u
dmd@(JD { sd :: forall s u. JointDmd s u -> s
sd = Str s
s, ud :: forall s u. JointDmd s u -> u
ud = u
u }) (UniqFM (JointDmd (Str s) u)
lazy_fv, UniqFM (JointDmd (Str s) (Use u))
sig_fv)
      | Str s
Lazy <- Str s
s = (UniqFM (JointDmd (Str s) u)
-> Unique -> JointDmd (Str s) u -> UniqFM (JointDmd (Str s) u)
forall elt. UniqFM elt -> Unique -> elt -> UniqFM elt
addToUFM_Directly UniqFM (JointDmd (Str s) u)
lazy_fv Unique
uniq JointDmd (Str s) u
dmd, UniqFM (JointDmd (Str s) (Use u))
sig_fv)
      | Bool
otherwise = ( UniqFM (JointDmd (Str s) u)
-> Unique -> JointDmd (Str s) u -> UniqFM (JointDmd (Str s) u)
forall elt. UniqFM elt -> Unique -> elt -> UniqFM elt
addToUFM_Directly UniqFM (JointDmd (Str s) u)
lazy_fv Unique
uniq (JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str s
sd = Str s
forall s. Str s
Lazy, ud :: u
ud = u
u })
                    , UniqFM (JointDmd (Str s) (Use u))
-> Unique
-> JointDmd (Str s) (Use u)
-> UniqFM (JointDmd (Str s) (Use u))
forall elt. UniqFM elt -> Unique -> elt -> UniqFM elt
addToUFM_Directly UniqFM (JointDmd (Str s) (Use u))
sig_fv  Unique
uniq (JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str s
sd = Str s
s,    ud :: Use u
ud = Use u
forall u. Use u
Abs }) )

data TypeShape = TsFun TypeShape
               | TsProd [TypeShape]
               | TsUnk

instance Outputable TypeShape where
  ppr :: TypeShape -> SDoc
ppr TypeShape
TsUnk        = String -> SDoc
text String
"TsUnk"
  ppr (TsFun TypeShape
ts)   = String -> SDoc
text String
"TsFun" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (TypeShape -> SDoc
forall a. Outputable a => a -> SDoc
ppr TypeShape
ts)
  ppr (TsProd [TypeShape]
tss) = SDoc -> SDoc
parens ([SDoc] -> SDoc
hsep ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ([SDoc] -> [SDoc]) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> a -> b
$ (TypeShape -> SDoc) -> [TypeShape] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map TypeShape -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TypeShape]
tss)

trimToType :: Demand -> TypeShape -> Demand
-- See Note [Trimming a demand to a type]
trimToType :: Demand -> TypeShape -> Demand
trimToType (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
ms, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
mu }) TypeShape
ts
  = ArgStr -> ArgUse -> Demand
forall s u. s -> u -> JointDmd s u
JD (ArgStr -> TypeShape -> ArgStr
go_ms ArgStr
ms TypeShape
ts) (ArgUse -> TypeShape -> ArgUse
go_mu ArgUse
mu TypeShape
ts)
  where
    go_ms :: ArgStr -> TypeShape -> ArgStr
    go_ms :: ArgStr -> TypeShape -> ArgStr
go_ms ArgStr
Lazy      TypeShape
_  = ArgStr
forall s. Str s
Lazy
    go_ms (Str ExnStr
x StrDmd
s) TypeShape
ts = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
x (StrDmd -> TypeShape -> StrDmd
go_s StrDmd
s TypeShape
ts)

    go_s :: StrDmd -> TypeShape -> StrDmd
    go_s :: StrDmd -> TypeShape -> StrDmd
go_s StrDmd
HyperStr    TypeShape
_            = StrDmd
HyperStr
    go_s (SCall StrDmd
s)   (TsFun TypeShape
ts)   = StrDmd -> StrDmd
SCall (StrDmd -> TypeShape -> StrDmd
go_s StrDmd
s TypeShape
ts)
    go_s (SProd [ArgStr]
mss) (TsProd [TypeShape]
tss)
      | [ArgStr] -> [TypeShape] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [ArgStr]
mss [TypeShape]
tss       = [ArgStr] -> StrDmd
SProd ((ArgStr -> TypeShape -> ArgStr)
-> [ArgStr] -> [TypeShape] -> [ArgStr]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgStr -> TypeShape -> ArgStr
go_ms [ArgStr]
mss [TypeShape]
tss)
    go_s StrDmd
_           TypeShape
_            = StrDmd
HeadStr

    go_mu :: ArgUse -> TypeShape -> ArgUse
    go_mu :: ArgUse -> TypeShape -> ArgUse
go_mu ArgUse
Abs TypeShape
_ = ArgUse
forall u. Use u
Abs
    go_mu (Use Count
c UseDmd
u) TypeShape
ts = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
c (UseDmd -> TypeShape -> UseDmd
go_u UseDmd
u TypeShape
ts)

    go_u :: UseDmd -> TypeShape -> UseDmd
    go_u :: UseDmd -> TypeShape -> UseDmd
go_u UseDmd
UHead       TypeShape
_          = UseDmd
UHead
    go_u (UCall Count
c UseDmd
u) (TsFun TypeShape
ts) = Count -> UseDmd -> UseDmd
UCall Count
c (UseDmd -> TypeShape -> UseDmd
go_u UseDmd
u TypeShape
ts)
    go_u (UProd [ArgUse]
mus) (TsProd [TypeShape]
tss)
      | [ArgUse] -> [TypeShape] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [ArgUse]
mus [TypeShape]
tss      = [ArgUse] -> UseDmd
UProd ((ArgUse -> TypeShape -> ArgUse)
-> [ArgUse] -> [TypeShape] -> [ArgUse]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ArgUse -> TypeShape -> ArgUse
go_mu [ArgUse]
mus [TypeShape]
tss)
    go_u UseDmd
_           TypeShape
_           = UseDmd
Used

{-
Note [Trimming a demand to a type]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider this:

  f :: a -> Bool
  f x = case ... of
          A g1 -> case (x |> g1) of (p,q) -> ...
          B    -> error "urk"

where A,B are the constructors of a GADT.  We'll get a U(U,U) demand
on x from the A branch, but that's a stupid demand for x itself, which
has type 'a'. Indeed we get ASSERTs going off (notably in
splitUseProdDmd, Trac #8569).

Bottom line: we really don't want to have a binder whose demand is more
deeply-nested than its type.  There are various ways to tackle this.
When processing (x |> g1), we could "trim" the incoming demand U(U,U)
to match x's type.  But I'm currently doing so just at the moment when
we pin a demand on a binder, in DmdAnal.findBndrDmd.


Note [Threshold demands]
~~~~~~~~~~~~~~~~~~~~~~~~
Threshold usage demand is generated to figure out if
cardinality-instrumented demands of a binding's free variables should
be unleashed. See also [Aggregated demand for cardinality].

Note [Replicating polymorphic demands]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some demands can be considered as polymorphic. Generally, it is
applicable to such beasts as tops, bottoms as well as Head-Used and
Head-stricts demands. For instance,

S ~ S(L, ..., L)

Also, when top or bottom is occurred as a result demand, it in fact
can be expanded to saturate a callee's arity.
-}

splitProdDmd_maybe :: Demand -> Maybe [Demand]
-- Split a product into its components, iff there is any
-- useful information to be extracted thereby
-- The demand is not necessarily strict!
splitProdDmd_maybe :: Demand -> Maybe [Demand]
splitProdDmd_maybe (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
u })
  = case (ArgStr
s,ArgUse
u) of
      (Str ExnStr
_ (SProd [ArgStr]
sx), Use Count
_ UseDmd
u) | Just [ArgUse]
ux <- Int -> UseDmd -> Maybe [ArgUse]
splitUseProdDmd ([ArgStr] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ArgStr]
sx) UseDmd
u
                                  -> [Demand] -> Maybe [Demand]
forall a. a -> Maybe a
Just ([ArgStr] -> [ArgUse] -> [Demand]
forall s u. [s] -> [u] -> [JointDmd s u]
mkJointDmds [ArgStr]
sx [ArgUse]
ux)
      (Str ExnStr
_ StrDmd
s, Use Count
_ (UProd [ArgUse]
ux)) | Just [ArgStr]
sx <- Int -> StrDmd -> Maybe [ArgStr]
splitStrProdDmd ([ArgUse] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ArgUse]
ux) StrDmd
s
                                  -> [Demand] -> Maybe [Demand]
forall a. a -> Maybe a
Just ([ArgStr] -> [ArgUse] -> [Demand]
forall s u. [s] -> [u] -> [JointDmd s u]
mkJointDmds [ArgStr]
sx [ArgUse]
ux)
      (ArgStr
Lazy,    Use Count
_ (UProd [ArgUse]
ux)) -> [Demand] -> Maybe [Demand]
forall a. a -> Maybe a
Just ([ArgStr] -> [ArgUse] -> [Demand]
forall s u. [s] -> [u] -> [JointDmd s u]
mkJointDmds (Int -> ArgStr -> [ArgStr]
forall a. Int -> a -> [a]
replicate ([ArgUse] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ArgUse]
ux) ArgStr
forall s. Str s
Lazy) [ArgUse]
ux)
      (ArgStr, ArgUse)
_ -> Maybe [Demand]
forall a. Maybe a
Nothing

{-
************************************************************************
*                                                                      *
                   Demand results
*                                                                      *
************************************************************************


DmdResult:     Dunno CPRResult
               /
           ThrowsExn
             /
        Diverges


CPRResult:         NoCPR
                   /    \
            RetProd    RetSum ConTag


Product constructors return (Dunno (RetProd rs))
In a fixpoint iteration, start from Diverges
We have lubs, but not glbs; but that is ok.
-}

------------------------------------------------------------------------
-- Constructed Product Result
------------------------------------------------------------------------

data Termination r
  = Diverges    -- Definitely diverges
  | ThrowsExn   -- Definitely throws an exception or diverges
  | Dunno r     -- Might diverge or converge
  deriving( Termination r -> Termination r -> Bool
(Termination r -> Termination r -> Bool)
-> (Termination r -> Termination r -> Bool) -> Eq (Termination r)
forall r. Eq r => Termination r -> Termination r -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Termination r -> Termination r -> Bool
$c/= :: forall r. Eq r => Termination r -> Termination r -> Bool
== :: Termination r -> Termination r -> Bool
$c== :: forall r. Eq r => Termination r -> Termination r -> Bool
Eq, Int -> Termination r -> ShowS
[Termination r] -> ShowS
Termination r -> String
(Int -> Termination r -> ShowS)
-> (Termination r -> String)
-> ([Termination r] -> ShowS)
-> Show (Termination r)
forall r. Show r => Int -> Termination r -> ShowS
forall r. Show r => [Termination r] -> ShowS
forall r. Show r => Termination r -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Termination r] -> ShowS
$cshowList :: forall r. Show r => [Termination r] -> ShowS
show :: Termination r -> String
$cshow :: forall r. Show r => Termination r -> String
showsPrec :: Int -> Termination r -> ShowS
$cshowsPrec :: forall r. Show r => Int -> Termination r -> ShowS
Show )

type DmdResult = Termination CPRResult

data CPRResult = NoCPR          -- Top of the lattice
               | RetProd        -- Returns a constructor from a product type
               | RetSum ConTag  -- Returns a constructor from a data type
               deriving( CPRResult -> CPRResult -> Bool
(CPRResult -> CPRResult -> Bool)
-> (CPRResult -> CPRResult -> Bool) -> Eq CPRResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CPRResult -> CPRResult -> Bool
$c/= :: CPRResult -> CPRResult -> Bool
== :: CPRResult -> CPRResult -> Bool
$c== :: CPRResult -> CPRResult -> Bool
Eq, Int -> CPRResult -> ShowS
[CPRResult] -> ShowS
CPRResult -> String
(Int -> CPRResult -> ShowS)
-> (CPRResult -> String)
-> ([CPRResult] -> ShowS)
-> Show CPRResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CPRResult] -> ShowS
$cshowList :: [CPRResult] -> ShowS
show :: CPRResult -> String
$cshow :: CPRResult -> String
showsPrec :: Int -> CPRResult -> ShowS
$cshowsPrec :: Int -> CPRResult -> ShowS
Show )

lubCPR :: CPRResult -> CPRResult -> CPRResult
lubCPR :: CPRResult -> CPRResult -> CPRResult
lubCPR (RetSum Int
t1) (RetSum Int
t2)
  | Int
t1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
t2                       = Int -> CPRResult
RetSum Int
t1
lubCPR CPRResult
RetProd     CPRResult
RetProd     = CPRResult
RetProd
lubCPR CPRResult
_ CPRResult
_                     = CPRResult
NoCPR

lubDmdResult :: DmdResult -> DmdResult -> DmdResult
lubDmdResult :: DmdResult -> DmdResult -> DmdResult
lubDmdResult DmdResult
Diverges       DmdResult
r              = DmdResult
r
lubDmdResult DmdResult
ThrowsExn      DmdResult
Diverges       = DmdResult
forall r. Termination r
ThrowsExn
lubDmdResult DmdResult
ThrowsExn      DmdResult
r              = DmdResult
r
lubDmdResult (Dunno CPRResult
c1)     DmdResult
Diverges       = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno CPRResult
c1
lubDmdResult (Dunno CPRResult
c1)     DmdResult
ThrowsExn      = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno CPRResult
c1
lubDmdResult (Dunno CPRResult
c1)     (Dunno CPRResult
c2)     = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno (CPRResult
c1 CPRResult -> CPRResult -> CPRResult
`lubCPR` CPRResult
c2)
-- This needs to commute with defaultDmd, i.e.
-- defaultDmd (r1 `lubDmdResult` r2) = defaultDmd r1 `lubDmd` defaultDmd r2
-- (See Note [Default demand on free variables] for why)

bothDmdResult :: DmdResult -> Termination () -> DmdResult
-- See Note [Asymmetry of 'both' for DmdType and DmdResult]
bothDmdResult :: DmdResult -> Termination () -> DmdResult
bothDmdResult DmdResult
_ Termination ()
Diverges   = DmdResult
forall r. Termination r
Diverges
bothDmdResult DmdResult
r Termination ()
ThrowsExn  = case DmdResult
r of { DmdResult
Diverges -> DmdResult
r; DmdResult
_ -> DmdResult
forall r. Termination r
ThrowsExn }
bothDmdResult DmdResult
r (Dunno {}) = DmdResult
r
-- This needs to commute with defaultDmd, i.e.
-- defaultDmd (r1 `bothDmdResult` r2) = defaultDmd r1 `bothDmd` defaultDmd r2
-- (See Note [Default demand on free variables] for why)

instance Outputable r => Outputable (Termination r) where
  ppr :: Termination r -> SDoc
ppr Termination r
Diverges      = Char -> SDoc
char Char
'b'
  ppr Termination r
ThrowsExn     = Char -> SDoc
char Char
'x'
  ppr (Dunno r
c)     = r -> SDoc
forall a. Outputable a => a -> SDoc
ppr r
c

instance Outputable CPRResult where
  ppr :: CPRResult -> SDoc
ppr CPRResult
NoCPR        = SDoc
empty
  ppr (RetSum Int
n)   = Char -> SDoc
char Char
'm' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n
  ppr CPRResult
RetProd      = Char -> SDoc
char Char
'm'

seqDmdResult :: DmdResult -> ()
seqDmdResult :: DmdResult -> ()
seqDmdResult DmdResult
Diverges  = ()
seqDmdResult DmdResult
ThrowsExn = ()
seqDmdResult (Dunno CPRResult
c) = CPRResult -> ()
seqCPRResult CPRResult
c

seqCPRResult :: CPRResult -> ()
seqCPRResult :: CPRResult -> ()
seqCPRResult CPRResult
NoCPR        = ()
seqCPRResult (RetSum Int
n)   = Int
n Int -> () -> ()
`seq` ()
seqCPRResult CPRResult
RetProd      = ()


------------------------------------------------------------------------
-- Combined demand result                                             --
------------------------------------------------------------------------

-- [cprRes] lets us switch off CPR analysis
-- by making sure that everything uses TopRes
topRes, exnRes, botRes :: DmdResult
topRes :: DmdResult
topRes = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno CPRResult
NoCPR
exnRes :: DmdResult
exnRes = DmdResult
forall r. Termination r
ThrowsExn
botRes :: DmdResult
botRes = DmdResult
forall r. Termination r
Diverges

cprSumRes :: ConTag -> DmdResult
cprSumRes :: Int -> DmdResult
cprSumRes Int
tag = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno (CPRResult -> DmdResult) -> CPRResult -> DmdResult
forall a b. (a -> b) -> a -> b
$ Int -> CPRResult
RetSum Int
tag

cprProdRes :: [DmdType] -> DmdResult
cprProdRes :: [DmdType] -> DmdResult
cprProdRes [DmdType]
_arg_tys = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno (CPRResult -> DmdResult) -> CPRResult -> DmdResult
forall a b. (a -> b) -> a -> b
$ CPRResult
RetProd

vanillaCprProdRes :: Arity -> DmdResult
vanillaCprProdRes :: Int -> DmdResult
vanillaCprProdRes Int
_arity = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno (CPRResult -> DmdResult) -> CPRResult -> DmdResult
forall a b. (a -> b) -> a -> b
$ CPRResult
RetProd

isTopRes :: DmdResult -> Bool
isTopRes :: DmdResult -> Bool
isTopRes (Dunno CPRResult
NoCPR) = Bool
True
isTopRes DmdResult
_             = Bool
False

isBotRes :: DmdResult -> Bool
-- True if the result diverges or throws an exception
isBotRes :: DmdResult -> Bool
isBotRes DmdResult
Diverges   = Bool
True
isBotRes DmdResult
ThrowsExn  = Bool
True
isBotRes (Dunno {}) = Bool
False

trimCPRInfo :: Bool -> Bool -> DmdResult -> DmdResult
trimCPRInfo :: Bool -> Bool -> DmdResult -> DmdResult
trimCPRInfo Bool
trim_all Bool
trim_sums DmdResult
res
  = DmdResult -> DmdResult
trimR DmdResult
res
  where
    trimR :: DmdResult -> DmdResult
trimR (Dunno CPRResult
c) = CPRResult -> DmdResult
forall r. r -> Termination r
Dunno (CPRResult -> CPRResult
trimC CPRResult
c)
    trimR DmdResult
res       = DmdResult
res

    trimC :: CPRResult -> CPRResult
trimC (RetSum Int
n)   | Bool
trim_all Bool -> Bool -> Bool
|| Bool
trim_sums = CPRResult
NoCPR
                       | Bool
otherwise             = Int -> CPRResult
RetSum Int
n
    trimC CPRResult
RetProd      | Bool
trim_all  = CPRResult
NoCPR
                       | Bool
otherwise = CPRResult
RetProd
    trimC CPRResult
NoCPR = CPRResult
NoCPR

returnsCPR_maybe :: DmdResult -> Maybe ConTag
returnsCPR_maybe :: DmdResult -> Maybe Int
returnsCPR_maybe (Dunno CPRResult
c) = CPRResult -> Maybe Int
retCPR_maybe CPRResult
c
returnsCPR_maybe DmdResult
_         = Maybe Int
forall a. Maybe a
Nothing

retCPR_maybe :: CPRResult -> Maybe ConTag
retCPR_maybe :: CPRResult -> Maybe Int
retCPR_maybe (RetSum Int
t)  = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
t
retCPR_maybe CPRResult
RetProd     = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
fIRST_TAG
retCPR_maybe CPRResult
NoCPR       = Maybe Int
forall a. Maybe a
Nothing

-- See Notes [Default demand on free variables]
-- and [defaultDmd vs. resTypeArgDmd]
defaultDmd :: Termination r -> Demand
defaultDmd :: Termination r -> Demand
defaultDmd (Dunno {}) = Demand
absDmd
defaultDmd Termination r
_          = Demand
botDmd  -- Diverges or ThrowsExn

resTypeArgDmd :: Termination r -> Demand
-- TopRes and BotRes are polymorphic, so that
--      BotRes === (Bot -> BotRes) === ...
--      TopRes === (Top -> TopRes) === ...
-- This function makes that concrete
-- Also see Note [defaultDmd vs. resTypeArgDmd]
resTypeArgDmd :: Termination r -> Demand
resTypeArgDmd (Dunno r
_) = Demand
topDmd
resTypeArgDmd Termination r
_         = Demand
botDmd   -- Diverges or ThrowsExn

{-
Note [defaultDmd and resTypeArgDmd]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These functions are similar: They express the demand on something not
explicitly mentioned in the environment resp. the argument list. Yet they are
different:
 * Variables not mentioned in the free variables environment are definitely
   unused, so we can use absDmd there.
 * Further arguments *can* be used, of course. Hence topDmd is used.


************************************************************************
*                                                                      *
           Demand environments and types
*                                                                      *
************************************************************************
-}

type DmdEnv = VarEnv Demand   -- See Note [Default demand on free variables]

data DmdType = DmdType
                  DmdEnv        -- Demand on explicitly-mentioned
                                --      free variables
                  [Demand]      -- Demand on arguments
                  DmdResult     -- See [Nature of result demand]

{-
Note [Nature of result demand]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A DmdResult contains information about termination (currently distinguishing
definite divergence and no information; it is possible to include definite
convergence here), and CPR information about the result.

The semantics of this depends on whether we are looking at a DmdType, i.e. the
demand put on by an expression _under a specific incoming demand_ on its
environment, or at a StrictSig describing a demand transformer.

For a
 * DmdType, the termination information is true given the demand it was
   generated with, while for
 * a StrictSig it holds after applying enough arguments.

The CPR information, though, is valid after the number of arguments mentioned
in the type is given. Therefore, when forgetting the demand on arguments, as in
dmdAnalRhs, this needs to be considere (via removeDmdTyArgs).

Consider
  b2 x y = x `seq` y `seq` error (show x)
this has a strictness signature of
  <S><S>b
meaning that "b2 `seq` ()" and "b2 1 `seq` ()" might well terminate, but
for "b2 1 2 `seq` ()" we get definite divergence.

For comparison,
  b1 x = x `seq` error (show x)
has a strictness signature of
  <S>b
and "b1 1 `seq` ()" is known to terminate.

Now consider a function h with signature "<C(S)>", and the expression
  e1 = h b1
now h puts a demand of <C(S)> onto its argument, and the demand transformer
turns it into
  <S>b
Now the DmdResult "b" does apply to us, even though "b1 `seq` ()" does not
diverge, and we do not anything being passed to b.

Note [Asymmetry of 'both' for DmdType and DmdResult]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'both' for DmdTypes is *asymmetrical*, because there is only one
result!  For example, given (e1 e2), we get a DmdType dt1 for e1, use
its arg demand to analyse e2 giving dt2, and then do (dt1 `bothType` dt2).
Similarly with
  case e of { p -> rhs }
we get dt_scrut from the scrutinee and dt_rhs from the RHS, and then
compute (dt_rhs `bothType` dt_scrut).

We
 1. combine the information on the free variables,
 2. take the demand on arguments from the first argument
 3. combine the termination results, but
 4. take CPR info from the first argument.

3 and 4 are implementd in bothDmdResult.
-}

-- Equality needed for fixpoints in DmdAnal
instance Eq DmdType where
  == :: DmdType -> DmdType -> Bool
(==) (DmdType DmdEnv
fv1 [Demand]
ds1 DmdResult
res1)
       (DmdType DmdEnv
fv2 [Demand]
ds2 DmdResult
res2) = DmdEnv -> [(Unique, Demand)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList DmdEnv
fv1 [(Unique, Demand)] -> [(Unique, Demand)] -> Bool
forall a. Eq a => a -> a -> Bool
== DmdEnv -> [(Unique, Demand)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList DmdEnv
fv2
         -- It's OK to use nonDetUFMToList here because we're testing for
         -- equality and even though the lists will be in some arbitrary
         -- Unique order, it is the same order for both
                              Bool -> Bool -> Bool
&& [Demand]
ds1 [Demand] -> [Demand] -> Bool
forall a. Eq a => a -> a -> Bool
== [Demand]
ds2 Bool -> Bool -> Bool
&& DmdResult
res1 DmdResult -> DmdResult -> Bool
forall a. Eq a => a -> a -> Bool
== DmdResult
res2

lubDmdType :: DmdType -> DmdType -> DmdType
lubDmdType :: DmdType -> DmdType -> DmdType
lubDmdType DmdType
d1 DmdType
d2
  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
lub_fv [Demand]
lub_ds DmdResult
lub_res
  where
    n :: Int
n = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (DmdType -> Int
dmdTypeDepth DmdType
d1) (DmdType -> Int
dmdTypeDepth DmdType
d2)
    (DmdType DmdEnv
fv1 [Demand]
ds1 DmdResult
r1) = Int -> DmdType -> DmdType
ensureArgs Int
n DmdType
d1
    (DmdType DmdEnv
fv2 [Demand]
ds2 DmdResult
r2) = Int -> DmdType -> DmdType
ensureArgs Int
n DmdType
d2

    lub_fv :: DmdEnv
lub_fv  = (Demand -> Demand -> Demand)
-> DmdEnv -> Demand -> DmdEnv -> Demand -> DmdEnv
forall a.
(a -> a -> a) -> VarEnv a -> a -> VarEnv a -> a -> VarEnv a
plusVarEnv_CD Demand -> Demand -> Demand
lubDmd DmdEnv
fv1 (DmdResult -> Demand
forall r. Termination r -> Demand
defaultDmd DmdResult
r1) DmdEnv
fv2 (DmdResult -> Demand
forall r. Termination r -> Demand
defaultDmd DmdResult
r2)
    lub_ds :: [Demand]
lub_ds  = String
-> (Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"lubDmdType" Demand -> Demand -> Demand
lubDmd [Demand]
ds1 [Demand]
ds2
    lub_res :: DmdResult
lub_res = DmdResult -> DmdResult -> DmdResult
lubDmdResult DmdResult
r1 DmdResult
r2

{-
Note [The need for BothDmdArg]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, the right argument to bothDmdType, as well as the return value of
dmdAnalStar via postProcessDmdType, was a DmdType. But bothDmdType only needs
to know about the free variables and termination information, but nothing about
the demand put on arguments, nor cpr information. So we make that explicit by
only passing the relevant information.
-}

type BothDmdArg = (DmdEnv, Termination ())

mkBothDmdArg :: DmdEnv -> BothDmdArg
mkBothDmdArg :: DmdEnv -> BothDmdArg
mkBothDmdArg DmdEnv
env = (DmdEnv
env, () -> Termination ()
forall r. r -> Termination r
Dunno ())

toBothDmdArg :: DmdType -> BothDmdArg
toBothDmdArg :: DmdType -> BothDmdArg
toBothDmdArg (DmdType DmdEnv
fv [Demand]
_ DmdResult
r) = (DmdEnv
fv, DmdResult -> Termination ()
forall r. Termination r -> Termination ()
go DmdResult
r)
  where
    go :: Termination r -> Termination ()
go (Dunno {}) = () -> Termination ()
forall r. r -> Termination r
Dunno ()
    go Termination r
ThrowsExn  = Termination ()
forall r. Termination r
ThrowsExn
    go Termination r
Diverges   = Termination ()
forall r. Termination r
Diverges

bothDmdType :: DmdType -> BothDmdArg -> DmdType
bothDmdType :: DmdType -> BothDmdArg -> DmdType
bothDmdType (DmdType DmdEnv
fv1 [Demand]
ds1 DmdResult
r1) (DmdEnv
fv2, Termination ()
t2)
    -- See Note [Asymmetry of 'both' for DmdType and DmdResult]
    -- 'both' takes the argument/result info from its *first* arg,
    -- using its second arg just for its free-var info.
  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType ((Demand -> Demand -> Demand)
-> DmdEnv -> Demand -> DmdEnv -> Demand -> DmdEnv
forall a.
(a -> a -> a) -> VarEnv a -> a -> VarEnv a -> a -> VarEnv a
plusVarEnv_CD Demand -> Demand -> Demand
bothDmd DmdEnv
fv1 (DmdResult -> Demand
forall r. Termination r -> Demand
defaultDmd DmdResult
r1) DmdEnv
fv2 (Termination () -> Demand
forall r. Termination r -> Demand
defaultDmd Termination ()
t2))
            [Demand]
ds1
            (DmdResult
r1 DmdResult -> Termination () -> DmdResult
`bothDmdResult` Termination ()
t2)

instance Outputable DmdType where
  ppr :: DmdType -> SDoc
ppr (DmdType DmdEnv
fv [Demand]
ds DmdResult
res)
    = [SDoc] -> SDoc
hsep [[SDoc] -> SDoc
hcat ((Demand -> SDoc) -> [Demand] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Demand]
ds) SDoc -> SDoc -> SDoc
<> DmdResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr DmdResult
res,
            if [(Unique, Demand)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Unique, Demand)]
fv_elts then SDoc
empty
            else SDoc -> SDoc
braces ([SDoc] -> SDoc
fsep (((Unique, Demand) -> SDoc) -> [(Unique, Demand)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (Unique, Demand) -> SDoc
forall a a. (Outputable a, Outputable a) => (a, a) -> SDoc
pp_elt [(Unique, Demand)]
fv_elts))]
    where
      pp_elt :: (a, a) -> SDoc
pp_elt (a
uniq, a
dmd) = a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
uniq SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"->" SDoc -> SDoc -> SDoc
<> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
dmd
      fv_elts :: [(Unique, Demand)]
fv_elts = DmdEnv -> [(Unique, Demand)]
forall elt. UniqFM elt -> [(Unique, elt)]
nonDetUFMToList DmdEnv
fv
        -- It's OK to use nonDetUFMToList here because we only do it for
        -- pretty printing

emptyDmdEnv :: VarEnv Demand
emptyDmdEnv :: DmdEnv
emptyDmdEnv = DmdEnv
forall a. VarEnv a
emptyVarEnv

-- nopDmdType is the demand of doing nothing
-- (lazy, absent, no CPR information, no termination information).
-- Note that it is ''not'' the top of the lattice (which would be "may use everything"),
-- so it is (no longer) called topDmd
nopDmdType, botDmdType, exnDmdType :: DmdType
nopDmdType :: DmdType
nopDmdType = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [] DmdResult
topRes
botDmdType :: DmdType
botDmdType = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [] DmdResult
botRes
exnDmdType :: DmdType
exnDmdType = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [] DmdResult
exnRes

cprProdDmdType :: Arity -> DmdType
cprProdDmdType :: Int -> DmdType
cprProdDmdType Int
arity
  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [] (Int -> DmdResult
vanillaCprProdRes Int
arity)

isTopDmdType :: DmdType -> Bool
isTopDmdType :: DmdType -> Bool
isTopDmdType (DmdType DmdEnv
env [] DmdResult
res)
  | DmdResult -> Bool
isTopRes DmdResult
res Bool -> Bool -> Bool
&& DmdEnv -> Bool
forall a. VarEnv a -> Bool
isEmptyVarEnv DmdEnv
env = Bool
True
isTopDmdType DmdType
_                        = Bool
False

mkDmdType :: DmdEnv -> [Demand] -> DmdResult -> DmdType
mkDmdType :: DmdEnv -> [Demand] -> DmdResult -> DmdType
mkDmdType DmdEnv
fv [Demand]
ds DmdResult
res = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv [Demand]
ds DmdResult
res

dmdTypeDepth :: DmdType -> Arity
dmdTypeDepth :: DmdType -> Int
dmdTypeDepth (DmdType DmdEnv
_ [Demand]
ds DmdResult
_) = [Demand] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Demand]
ds

-- Remove any demand on arguments. This is used in dmdAnalRhs on the body
removeDmdTyArgs :: DmdType -> DmdType
removeDmdTyArgs :: DmdType -> DmdType
removeDmdTyArgs = Int -> DmdType -> DmdType
ensureArgs Int
0

-- This makes sure we can use the demand type with n arguments,
-- It extends the argument list with the correct resTypeArgDmd
-- It also adjusts the DmdResult: Divergence survives additional arguments,
-- CPR information does not (and definite converge also would not).
ensureArgs :: Arity -> DmdType -> DmdType
ensureArgs :: Int -> DmdType -> DmdType
ensureArgs Int
n DmdType
d | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
depth = DmdType
d
               | Bool
otherwise  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv [Demand]
ds' DmdResult
r'
  where depth :: Int
depth = DmdType -> Int
dmdTypeDepth DmdType
d
        DmdType DmdEnv
fv [Demand]
ds DmdResult
r = DmdType
d

        ds' :: [Demand]
ds' = Int -> [Demand] -> [Demand]
forall a. Int -> [a] -> [a]
take Int
n ([Demand]
ds [Demand] -> [Demand] -> [Demand]
forall a. [a] -> [a] -> [a]
++ Demand -> [Demand]
forall a. a -> [a]
repeat (DmdResult -> Demand
forall r. Termination r -> Demand
resTypeArgDmd DmdResult
r))
        r' :: DmdResult
r' = case DmdResult
r of    -- See [Nature of result demand]
              Dunno CPRResult
_ -> DmdResult
topRes
              DmdResult
_       -> DmdResult
r


seqDmdType :: DmdType -> ()
seqDmdType :: DmdType -> ()
seqDmdType (DmdType DmdEnv
env [Demand]
ds DmdResult
res) =
  DmdEnv -> ()
seqDmdEnv DmdEnv
env () -> () -> ()
`seq` [Demand] -> ()
seqDemandList [Demand]
ds () -> () -> ()
`seq` DmdResult -> ()
seqDmdResult DmdResult
res () -> () -> ()
`seq` ()

seqDmdEnv :: DmdEnv -> ()
seqDmdEnv :: DmdEnv -> ()
seqDmdEnv DmdEnv
env = ([Demand] -> ()) -> DmdEnv -> ()
forall elt. ([elt] -> ()) -> UniqFM elt -> ()
seqEltsUFM [Demand] -> ()
seqDemandList DmdEnv
env

splitDmdTy :: DmdType -> (Demand, DmdType)
-- Split off one function argument
-- We already have a suitable demand on all
-- free vars, so no need to add more!
splitDmdTy :: DmdType -> (Demand, DmdType)
splitDmdTy (DmdType DmdEnv
fv (Demand
dmd:[Demand]
dmds) DmdResult
res_ty) = (Demand
dmd, DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv [Demand]
dmds DmdResult
res_ty)
splitDmdTy ty :: DmdType
ty@(DmdType DmdEnv
_ [] DmdResult
res_ty)       = (DmdResult -> Demand
forall r. Termination r -> Demand
resTypeArgDmd DmdResult
res_ty, DmdType
ty)

-- When e is evaluated after executing an IO action, and d is e's demand, then
-- what of this demand should we consider, given that the IO action can cleanly
-- exit?
-- * We have to kill all strictness demands (i.e. lub with a lazy demand)
-- * We can keep usage information (i.e. lub with an absent demand)
-- * We have to kill definite divergence
-- * We can keep CPR information.
-- See Note [IO hack in the demand analyser] in DmdAnal
deferAfterIO :: DmdType -> DmdType
deferAfterIO :: DmdType -> DmdType
deferAfterIO d :: DmdType
d@(DmdType DmdEnv
_ [Demand]
_ DmdResult
res) =
    case DmdType
d DmdType -> DmdType -> DmdType
`lubDmdType` DmdType
nopDmdType of
        DmdType DmdEnv
fv [Demand]
ds DmdResult
_ -> DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv [Demand]
ds (DmdResult -> DmdResult
defer_res DmdResult
res)
  where
  defer_res :: DmdResult -> DmdResult
defer_res r :: DmdResult
r@(Dunno {}) = DmdResult
r
  defer_res DmdResult
_            = DmdResult
topRes  -- Diverges and ThrowsExn

strictenDmd :: Demand -> CleanDemand
strictenDmd :: Demand -> CleanDemand
strictenDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
u})
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = ArgStr -> StrDmd
poke_s ArgStr
s, ud :: UseDmd
ud = ArgUse -> UseDmd
poke_u ArgUse
u }
  where
    poke_s :: ArgStr -> StrDmd
poke_s ArgStr
Lazy      = StrDmd
HeadStr
    poke_s (Str ExnStr
_ StrDmd
s) = StrDmd
s
    poke_u :: ArgUse -> UseDmd
poke_u ArgUse
Abs       = UseDmd
UHead
    poke_u (Use Count
_ UseDmd
u) = UseDmd
u

-- Deferring and peeling

type DmdShell   -- Describes the "outer shell"
                -- of a Demand
   = JointDmd (Str ()) (Use ())

toCleanDmd :: Demand -> (DmdShell, CleanDemand)
-- Splits a Demand into its "shell" and the inner "clean demand"
toCleanDmd :: Demand -> (DmdShell, CleanDemand)
toCleanDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
u })
  = (JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str ()
sd = Str ()
ss, ud :: Use ()
ud = Use ()
us }, JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd
s', ud :: UseDmd
ud = UseDmd
u' })
    -- See Note [Analyzing with lazy demand and lambdas]
    -- See Note [Analysing with absent demand]
  where
    (Str ()
ss, StrDmd
s') = case ArgStr
s of
                Str ExnStr
x StrDmd
s' -> (ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
x (), StrDmd
s')
                ArgStr
Lazy     -> (Str ()
forall s. Str s
Lazy,     StrDmd
HeadStr)

    (Use ()
us, UseDmd
u') = case ArgUse
u of
                 Use Count
c UseDmd
u' -> (Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
c (), UseDmd
u')
                 ArgUse
Abs      -> (Use ()
forall u. Use u
Abs,      UseDmd
Used)

-- This is used in dmdAnalStar when post-processing
-- a function's argument demand. So we only care about what
-- does to free variables, and whether it terminates.
-- see Note [The need for BothDmdArg]
postProcessDmdType :: DmdShell -> DmdType -> BothDmdArg
postProcessDmdType :: DmdShell -> DmdType -> BothDmdArg
postProcessDmdType du :: DmdShell
du@(JD { sd :: forall s u. JointDmd s u -> s
sd = Str ()
ss }) (DmdType DmdEnv
fv [Demand]
_ DmdResult
res_ty)
    = (DmdShell -> DmdEnv -> DmdEnv
postProcessDmdEnv DmdShell
du DmdEnv
fv, Termination ()
term_info)
    where
       term_info :: Termination ()
term_info = case Str () -> DmdResult -> DmdResult
postProcessDmdResult Str ()
ss DmdResult
res_ty of
                     Dunno CPRResult
_   -> () -> Termination ()
forall r. r -> Termination r
Dunno ()
                     DmdResult
ThrowsExn -> Termination ()
forall r. Termination r
ThrowsExn
                     DmdResult
Diverges  -> Termination ()
forall r. Termination r
Diverges

postProcessDmdResult :: Str () -> DmdResult -> DmdResult
postProcessDmdResult :: Str () -> DmdResult -> DmdResult
postProcessDmdResult Str ()
Lazy           DmdResult
_         = DmdResult
topRes
postProcessDmdResult (Str ExnStr
ExnStr ()
_) DmdResult
ThrowsExn = DmdResult
topRes  -- Key point!
-- Note that only ThrowsExn results can be caught, not Diverges
postProcessDmdResult Str ()
_              DmdResult
res       = DmdResult
res

postProcessDmdEnv :: DmdShell -> DmdEnv -> DmdEnv
postProcessDmdEnv :: DmdShell -> DmdEnv -> DmdEnv
postProcessDmdEnv ds :: DmdShell
ds@(JD { sd :: forall s u. JointDmd s u -> s
sd = Str ()
ss, ud :: forall s u. JointDmd s u -> u
ud = Use ()
us }) DmdEnv
env
  | Use ()
Abs <- Use ()
us       = DmdEnv
emptyDmdEnv
    -- In this case (postProcessDmd ds) == id; avoid a redundant rebuild
    -- of the environment. Be careful, bad things will happen if this doesn't
    -- match postProcessDmd (see #13977).
  | Str ExnStr
VanStr ()
_ <- Str ()
ss
  , Use Count
One ()
_ <- Use ()
us = DmdEnv
env
  | Bool
otherwise       = (Demand -> Demand) -> DmdEnv -> DmdEnv
forall a b. (a -> b) -> VarEnv a -> VarEnv b
mapVarEnv (DmdShell -> Demand -> Demand
postProcessDmd DmdShell
ds) DmdEnv
env
  -- For the Absent case just discard all usage information
  -- We only processed the thing at all to analyse the body
  -- See Note [Always analyse in virgin pass]

reuseEnv :: DmdEnv -> DmdEnv
reuseEnv :: DmdEnv -> DmdEnv
reuseEnv = (Demand -> Demand) -> DmdEnv -> DmdEnv
forall a b. (a -> b) -> VarEnv a -> VarEnv b
mapVarEnv (DmdShell -> Demand -> Demand
postProcessDmd
                        (JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str ()
sd = ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr (), ud :: Use ()
ud = Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
Many () }))

postProcessUnsat :: DmdShell -> DmdType -> DmdType
postProcessUnsat :: DmdShell -> DmdType -> DmdType
postProcessUnsat ds :: DmdShell
ds@(JD { sd :: forall s u. JointDmd s u -> s
sd = Str ()
ss }) (DmdType DmdEnv
fv [Demand]
args DmdResult
res_ty)
  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType (DmdShell -> DmdEnv -> DmdEnv
postProcessDmdEnv DmdShell
ds DmdEnv
fv)
            ((Demand -> Demand) -> [Demand] -> [Demand]
forall a b. (a -> b) -> [a] -> [b]
map (DmdShell -> Demand -> Demand
postProcessDmd DmdShell
ds) [Demand]
args)
            (Str () -> DmdResult -> DmdResult
postProcessDmdResult Str ()
ss DmdResult
res_ty)

postProcessDmd :: DmdShell -> Demand -> Demand
postProcessDmd :: DmdShell -> Demand -> Demand
postProcessDmd (JD { sd :: forall s u. JointDmd s u -> s
sd = Str ()
ss, ud :: forall s u. JointDmd s u -> u
ud = Use ()
us }) (JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
a})
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: ArgStr
sd = ArgStr
s', ud :: ArgUse
ud = ArgUse
a' }
  where
    s' :: ArgStr
s' = case Str ()
ss of
           Str ()
Lazy         -> ArgStr
forall s. Str s
Lazy
           Str ExnStr
ExnStr ()
_ -> ArgStr -> ArgStr
markExnStr ArgStr
s
           Str ExnStr
VanStr ()
_ -> ArgStr
s
    a' :: ArgUse
a' = case Use ()
us of
           Use ()
Abs        -> ArgUse
forall u. Use u
Abs
           Use Count
Many ()
_ -> ArgUse -> ArgUse
markReusedDmd ArgUse
a
           Use Count
One  ()
_ -> ArgUse
a

markExnStr :: ArgStr -> ArgStr
markExnStr :: ArgStr -> ArgStr
markExnStr (Str ExnStr
VanStr StrDmd
s) = ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
ExnStr StrDmd
s
markExnStr ArgStr
s              = ArgStr
s

-- Peels one call level from the demand, and also returns
-- whether it was unsaturated (separately for strictness and usage)
peelCallDmd :: CleanDemand -> (CleanDemand, DmdShell)
-- Exploiting the fact that
-- on the strictness side      C(B) = B
-- and on the usage side       C(U) = U
peelCallDmd :: CleanDemand -> (CleanDemand, DmdShell)
peelCallDmd (JD {sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
u})
  = (JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: StrDmd
sd = StrDmd
s', ud :: UseDmd
ud = UseDmd
u' }, JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str ()
sd = Str ()
ss, ud :: Use ()
ud = Use ()
us })
  where
    (StrDmd
s', Str ()
ss) = case StrDmd
s of
                 SCall StrDmd
s' -> (StrDmd
s',       ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr ())
                 StrDmd
HyperStr -> (StrDmd
HyperStr, ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr ())
                 StrDmd
_        -> (StrDmd
HeadStr,  Str ()
forall s. Str s
Lazy)
    (UseDmd
u', Use ()
us) = case UseDmd
u of
                 UCall Count
c UseDmd
u' -> (UseDmd
u',   Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
c    ())
                 UseDmd
_          -> (UseDmd
Used, Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
Many ())
       -- The _ cases for usage includes UHead which seems a bit wrong
       -- because the body isn't used at all!
       -- c.f. the Abs case in toCleanDmd

-- Peels that multiple nestings of calls clean demand and also returns
-- whether it was unsaturated (separately for strictness and usage
-- see Note [Demands from unsaturated function calls]
peelManyCalls :: Int -> CleanDemand -> DmdShell
peelManyCalls :: Int -> CleanDemand -> DmdShell
peelManyCalls Int
n (JD { sd :: forall s u. JointDmd s u -> s
sd = StrDmd
str, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
abs })
  = JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: Str ()
sd = Int -> StrDmd -> Str ()
go_str Int
n StrDmd
str, ud :: Use ()
ud = Int -> UseDmd -> Use ()
go_abs Int
n UseDmd
abs }
  where
    go_str :: Int -> StrDmd -> Str ()  -- True <=> unsaturated, defer
    go_str :: Int -> StrDmd -> Str ()
go_str Int
0 StrDmd
_          = ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr ()
    go_str Int
_ StrDmd
HyperStr   = ExnStr -> () -> Str ()
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr () -- == go_str (n-1) HyperStr, as HyperStr = Call(HyperStr)
    go_str Int
n (SCall StrDmd
d') = Int -> StrDmd -> Str ()
go_str (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) StrDmd
d'
    go_str Int
_ StrDmd
_          = Str ()
forall s. Str s
Lazy

    go_abs :: Int -> UseDmd -> Use ()      -- Many <=> unsaturated, or at least
    go_abs :: Int -> UseDmd -> Use ()
go_abs Int
0 UseDmd
_              = Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
One ()   --          one UCall Many in the demand
    go_abs Int
n (UCall Count
One UseDmd
d') = Int -> UseDmd -> Use ()
go_abs (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) UseDmd
d'
    go_abs Int
_ UseDmd
_              = Count -> () -> Use ()
forall u. Count -> u -> Use u
Use Count
Many ()

{-
Note [Demands from unsaturated function calls]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Consider a demand transformer d1 -> d2 -> r for f.
If a sufficiently detailed demand is fed into this transformer,
e.g <C(C(S)), C1(C1(S))> arising from "f x1 x2" in a strict, use-once context,
then d1 and d2 is precisely the demand unleashed onto x1 and x2 (similar for
the free variable environment) and furthermore the result information r is the
one we want to use.

An anonymous lambda is also an unsaturated function all (needs one argument,
none given), so this applies to that case as well.

But the demand fed into f might be less than <C(C(S)), C1(C1(S))>. There are a few cases:
 * Not enough demand on the strictness side:
   - In that case, we need to zap all strictness in the demand on arguments and
     free variables.
   - Furthermore, we remove CPR information. It could be left, but given the incoming
     demand is not enough to evaluate so far we just do not bother.
   - And finally termination information: If r says that f diverges for sure,
     then this holds when the demand guarantees that two arguments are going to
     be passed. If the demand is lower, we may just as well converge.
     If we were tracking definite convegence, than that would still hold under
     a weaker demand than expected by the demand transformer.
 * Not enough demand from the usage side: The missing usage can be expanded
   using UCall Many, therefore this is subsumed by the third case:
 * At least one of the uses has a cardinality of Many.
   - Even if f puts a One demand on any of its argument or free variables, if
     we call f multiple times, we may evaluate this argument or free variable
     multiple times. So forget about any occurrence of "One" in the demand.

In dmdTransformSig, we call peelManyCalls to find out if we are in any of these
cases, and then call postProcessUnsat to reduce the demand appropriately.

Similarly, dmdTransformDictSelSig and dmdAnal, when analyzing a Lambda, use
peelCallDmd, which peels only one level, but also returns the demand put on the
body of the function.
-}

peelFV :: DmdType -> Var -> (DmdType, Demand)
peelFV :: DmdType -> Var -> (DmdType, Demand)
peelFV (DmdType DmdEnv
fv [Demand]
ds DmdResult
res) Var
id = -- pprTrace "rfv" (ppr id <+> ppr dmd $$ ppr fv)
                               (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv' [Demand]
ds DmdResult
res, Demand
dmd)
  where
  fv' :: DmdEnv
fv' = DmdEnv
fv DmdEnv -> Var -> DmdEnv
forall a. VarEnv a -> Var -> VarEnv a
`delVarEnv` Var
id
  -- See Note [Default demand on free variables]
  dmd :: Demand
dmd  = DmdEnv -> Var -> Maybe Demand
forall a. VarEnv a -> Var -> Maybe a
lookupVarEnv DmdEnv
fv Var
id Maybe Demand -> Demand -> Demand
forall a. Maybe a -> a -> a
`orElse` DmdResult -> Demand
forall r. Termination r -> Demand
defaultDmd DmdResult
res

addDemand :: Demand -> DmdType -> DmdType
addDemand :: Demand -> DmdType -> DmdType
addDemand Demand
dmd (DmdType DmdEnv
fv [Demand]
ds DmdResult
res) = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
fv (Demand
dmdDemand -> [Demand] -> [Demand]
forall a. a -> [a] -> [a]
:[Demand]
ds) DmdResult
res

findIdDemand :: DmdType -> Var -> Demand
findIdDemand :: DmdType -> Var -> Demand
findIdDemand (DmdType DmdEnv
fv [Demand]
_ DmdResult
res) Var
id
  = DmdEnv -> Var -> Maybe Demand
forall a. VarEnv a -> Var -> Maybe a
lookupVarEnv DmdEnv
fv Var
id Maybe Demand -> Demand -> Demand
forall a. Maybe a -> a -> a
`orElse` DmdResult -> Demand
forall r. Termination r -> Demand
defaultDmd DmdResult
res

{-
Note [Default demand on free variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the variable is not mentioned in the environment of a demand type,
its demand is taken to be a result demand of the type.
    For the stricness component,
     if the result demand is a Diverges, then we use HyperStr
                                         else we use Lazy
    For the usage component, we use Absent.
So we use either absDmd or botDmd.

Also note the equations for lubDmdResult (resp. bothDmdResult) noted there.

Note [Always analyse in virgin pass]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tricky point: make sure that we analyse in the 'virgin' pass. Consider
   rec { f acc x True  = f (...rec { g y = ...g... }...)
         f acc x False = acc }
In the virgin pass for 'f' we'll give 'f' a very strict (bottom) type.
That might mean that we analyse the sub-expression containing the
E = "...rec g..." stuff in a bottom demand.  Suppose we *didn't analyse*
E, but just returned botType.

Then in the *next* (non-virgin) iteration for 'f', we might analyse E
in a weaker demand, and that will trigger doing a fixpoint iteration
for g.  But *because it's not the virgin pass* we won't start g's
iteration at bottom.  Disaster.  (This happened in $sfibToList' of
nofib/spectral/fibheaps.)

So in the virgin pass we make sure that we do analyse the expression
at least once, to initialise its signatures.

Note [Analyzing with lazy demand and lambdas]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The insight for analyzing lambdas follows from the fact that for
strictness S = C(L). This polymorphic expansion is critical for
cardinality analysis of the following example:

{-# NOINLINE build #-}
build g = (g (:) [], g (:) [])

h c z = build (\x ->
                let z1 = z ++ z
                 in if c
                    then \y -> x (y ++ z1)
                    else \y -> x (z1 ++ y))

One can see that `build` assigns to `g` demand <L,C(C1(U))>.
Therefore, when analyzing the lambda `(\x -> ...)`, we
expect each lambda \y -> ... to be annotated as "one-shot"
one. Therefore (\x -> \y -> x (y ++ z)) should be analyzed with a
demand <C(C(..), C(C1(U))>.

This is achieved by, first, converting the lazy demand L into the
strict S by the second clause of the analysis.

Note [Analysing with absent demand]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose we analyse an expression with demand <L,A>.  The "A" means
"absent", so this expression will never be needed.  What should happen?
There are several wrinkles:

* We *do* want to analyse the expression regardless.
  Reason: Note [Always analyse in virgin pass]

  But we can post-process the results to ignore all the usage
  demands coming back. This is done by postProcessDmdType.

* In a previous incarnation of GHC we needed to be extra careful in the
  case of an *unlifted type*, because unlifted values are evaluated
  even if they are not used.  Example (see Trac #9254):
     f :: (() -> (# Int#, () #)) -> ()
          -- Strictness signature is
          --    <C(S(LS)), 1*C1(U(A,1*U()))>
          -- I.e. calls k, but discards first component of result
     f k = case k () of (# _, r #) -> r

     g :: Int -> ()
     g y = f (\n -> (# case y of I# y2 -> y2, n #))

  Here f's strictness signature says (correctly) that it calls its
  argument function and ignores the first component of its result.
  This is correct in the sense that it'd be fine to (say) modify the
  function so that always returned 0# in the first component.

  But in function g, we *will* evaluate the 'case y of ...', because
  it has type Int#.  So 'y' will be evaluated.  So we must record this
  usage of 'y', else 'g' will say 'y' is absent, and will w/w so that
  'y' is bound to an aBSENT_ERROR thunk.

  However, the argument of toCleanDmd always satisfies the let/app
  invariant; so if it is unlifted it is also okForSpeculation, and so
  can be evaluated in a short finite time -- and that rules out nasty
  cases like the one above.  (I'm not quite sure why this was a
  problem in an earlier version of GHC, but it isn't now.)


************************************************************************
*                                                                      *
                     Demand signatures
*                                                                      *
************************************************************************

In a let-bound Id we record its strictness info.
In principle, this strictness info is a demand transformer, mapping
a demand on the Id into a DmdType, which gives
        a) the free vars of the Id's value
        b) the Id's arguments
        c) an indication of the result of applying
           the Id to its arguments

However, in fact we store in the Id an extremely emascuated demand
transfomer, namely

                a single DmdType
(Nevertheless we dignify StrictSig as a distinct type.)

This DmdType gives the demands unleashed by the Id when it is applied
to as many arguments as are given in by the arg demands in the DmdType.
Also see Note [Nature of result demand] for the meaning of a DmdResult in a
strictness signature.

If an Id is applied to less arguments than its arity, it means that
the demand on the function at a call site is weaker than the vanilla
call demand, used for signature inference. Therefore we place a top
demand on all arguments. Otherwise, the demand is specified by Id's
signature.

For example, the demand transformer described by the demand signature
        StrictSig (DmdType {x -> <S,1*U>} <L,A><L,U(U,U)>m)
says that when the function is applied to two arguments, it
unleashes demand <S,1*U> on the free var x, <L,A> on the first arg,
and <L,U(U,U)> on the second, then returning a constructor.

If this same function is applied to one arg, all we can say is that it
uses x with <L,U>, and its arg with demand <L,U>.
-}

newtype StrictSig = StrictSig DmdType
                  deriving( StrictSig -> StrictSig -> Bool
(StrictSig -> StrictSig -> Bool)
-> (StrictSig -> StrictSig -> Bool) -> Eq StrictSig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StrictSig -> StrictSig -> Bool
$c/= :: StrictSig -> StrictSig -> Bool
== :: StrictSig -> StrictSig -> Bool
$c== :: StrictSig -> StrictSig -> Bool
Eq )

instance Outputable StrictSig where
   ppr :: StrictSig -> SDoc
ppr (StrictSig DmdType
ty) = DmdType -> SDoc
forall a. Outputable a => a -> SDoc
ppr DmdType
ty

-- Used for printing top-level strictness pragmas in interface files
pprIfaceStrictSig :: StrictSig -> SDoc
pprIfaceStrictSig :: StrictSig -> SDoc
pprIfaceStrictSig (StrictSig (DmdType DmdEnv
_ [Demand]
dmds DmdResult
res))
  = [SDoc] -> SDoc
hcat ((Demand -> SDoc) -> [Demand] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Demand]
dmds) SDoc -> SDoc -> SDoc
<> DmdResult -> SDoc
forall a. Outputable a => a -> SDoc
ppr DmdResult
res

mkStrictSig :: DmdType -> StrictSig
mkStrictSig :: DmdType -> StrictSig
mkStrictSig DmdType
dmd_ty = DmdType -> StrictSig
StrictSig DmdType
dmd_ty

mkClosedStrictSig :: [Demand] -> DmdResult -> StrictSig
mkClosedStrictSig :: [Demand] -> DmdResult -> StrictSig
mkClosedStrictSig [Demand]
ds DmdResult
res = DmdType -> StrictSig
mkStrictSig (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [Demand]
ds DmdResult
res)

splitStrictSig :: StrictSig -> ([Demand], DmdResult)
splitStrictSig :: StrictSig -> ([Demand], DmdResult)
splitStrictSig (StrictSig (DmdType DmdEnv
_ [Demand]
dmds DmdResult
res)) = ([Demand]
dmds, DmdResult
res)

increaseStrictSigArity :: Int -> StrictSig -> StrictSig
-- Add extra arguments to a strictness signature
increaseStrictSigArity :: Int -> StrictSig -> StrictSig
increaseStrictSigArity Int
arity_increase sig :: StrictSig
sig@(StrictSig dmd_ty :: DmdType
dmd_ty@(DmdType DmdEnv
env [Demand]
dmds DmdResult
res))
  | DmdType -> Bool
isTopDmdType DmdType
dmd_ty = StrictSig
sig
  | Int
arity_increase Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = StrictSig
sig
  | Bool
otherwise           = DmdType -> StrictSig
StrictSig (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
env [Demand]
dmds' DmdResult
res)
  where
    dmds' :: [Demand]
dmds' = Int -> Demand -> [Demand]
forall a. Int -> a -> [a]
replicate Int
arity_increase Demand
topDmd [Demand] -> [Demand] -> [Demand]
forall a. [a] -> [a] -> [a]
++ [Demand]
dmds

etaExpandStrictSig :: Arity -> StrictSig -> StrictSig
-- We are expanding (\x y. e) to (\x y z. e z)
-- Add exta demands to the /end/ of the arg demands if necessary
etaExpandStrictSig :: Int -> StrictSig -> StrictSig
etaExpandStrictSig Int
arity sig :: StrictSig
sig@(StrictSig dmd_ty :: DmdType
dmd_ty@(DmdType DmdEnv
env [Demand]
dmds DmdResult
res))
  | DmdType -> Bool
isTopDmdType DmdType
dmd_ty = StrictSig
sig
  | Int
arity_increase Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = StrictSig
sig
  | Bool
otherwise           = DmdType -> StrictSig
StrictSig (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
env [Demand]
dmds' DmdResult
res)
  where
    arity_increase :: Int
arity_increase = Int
arity Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Demand] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Demand]
dmds
    dmds' :: [Demand]
dmds' = [Demand]
dmds [Demand] -> [Demand] -> [Demand]
forall a. [a] -> [a] -> [a]
++ Int -> Demand -> [Demand]
forall a. Int -> a -> [a]
replicate Int
arity_increase Demand
topDmd

isTopSig :: StrictSig -> Bool
isTopSig :: StrictSig -> Bool
isTopSig (StrictSig DmdType
ty) = DmdType -> Bool
isTopDmdType DmdType
ty

hasDemandEnvSig :: StrictSig -> Bool
hasDemandEnvSig :: StrictSig -> Bool
hasDemandEnvSig (StrictSig (DmdType DmdEnv
env [Demand]
_ DmdResult
_)) = Bool -> Bool
not (DmdEnv -> Bool
forall a. VarEnv a -> Bool
isEmptyVarEnv DmdEnv
env)

strictSigDmdEnv :: StrictSig -> DmdEnv
strictSigDmdEnv :: StrictSig -> DmdEnv
strictSigDmdEnv (StrictSig (DmdType DmdEnv
env [Demand]
_ DmdResult
_)) = DmdEnv
env

isBottomingSig :: StrictSig -> Bool
-- True if the signature diverges or throws an exception
isBottomingSig :: StrictSig -> Bool
isBottomingSig (StrictSig (DmdType DmdEnv
_ [Demand]
_ DmdResult
res)) = DmdResult -> Bool
isBotRes DmdResult
res

nopSig, botSig, exnSig :: StrictSig
nopSig :: StrictSig
nopSig = DmdType -> StrictSig
StrictSig DmdType
nopDmdType
botSig :: StrictSig
botSig = DmdType -> StrictSig
StrictSig DmdType
botDmdType
exnSig :: StrictSig
exnSig = DmdType -> StrictSig
StrictSig DmdType
exnDmdType

cprProdSig :: Arity -> StrictSig
cprProdSig :: Int -> StrictSig
cprProdSig Int
arity = DmdType -> StrictSig
StrictSig (Int -> DmdType
cprProdDmdType Int
arity)

seqStrictSig :: StrictSig -> ()
seqStrictSig :: StrictSig -> ()
seqStrictSig (StrictSig DmdType
ty) = DmdType -> ()
seqDmdType DmdType
ty

dmdTransformSig :: StrictSig -> CleanDemand -> DmdType
-- (dmdTransformSig fun_sig dmd) considers a call to a function whose
-- signature is fun_sig, with demand dmd.  We return the demand
-- that the function places on its context (eg its args)
dmdTransformSig :: StrictSig -> CleanDemand -> DmdType
dmdTransformSig (StrictSig dmd_ty :: DmdType
dmd_ty@(DmdType DmdEnv
_ [Demand]
arg_ds DmdResult
_)) CleanDemand
cd
  = DmdShell -> DmdType -> DmdType
postProcessUnsat (Int -> CleanDemand -> DmdShell
peelManyCalls ([Demand] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Demand]
arg_ds) CleanDemand
cd) DmdType
dmd_ty
    -- see Note [Demands from unsaturated function calls]

dmdTransformDataConSig :: Arity -> StrictSig -> CleanDemand -> DmdType
-- Same as dmdTransformSig but for a data constructor (worker),
-- which has a special kind of demand transformer.
-- If the constructor is saturated, we feed the demand on
-- the result into the constructor arguments.
dmdTransformDataConSig :: Int -> StrictSig -> CleanDemand -> DmdType
dmdTransformDataConSig Int
arity (StrictSig (DmdType DmdEnv
_ [Demand]
_ DmdResult
con_res))
                             (JD { sd :: forall s u. JointDmd s u -> s
sd = StrDmd
str, ud :: forall s u. JointDmd s u -> u
ud = UseDmd
abs })
  | Just [ArgStr]
str_dmds <- Int -> StrDmd -> Maybe [ArgStr]
forall a. (Eq a, Num a) => a -> StrDmd -> Maybe [ArgStr]
go_str Int
arity StrDmd
str
  , Just [ArgUse]
abs_dmds <- Int -> UseDmd -> Maybe [ArgUse]
forall t. (Eq t, Num t) => t -> UseDmd -> Maybe [ArgUse]
go_abs Int
arity UseDmd
abs
  = DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv ([ArgStr] -> [ArgUse] -> [Demand]
forall s u. [s] -> [u] -> [JointDmd s u]
mkJointDmds [ArgStr]
str_dmds [ArgUse]
abs_dmds) DmdResult
con_res
                -- Must remember whether it's a product, hence con_res, not TopRes

  | Bool
otherwise   -- Not saturated
  = DmdType
nopDmdType
  where
    go_str :: a -> StrDmd -> Maybe [ArgStr]
go_str a
0 StrDmd
dmd        = Int -> StrDmd -> Maybe [ArgStr]
splitStrProdDmd Int
arity StrDmd
dmd
    go_str a
n (SCall StrDmd
s') = a -> StrDmd -> Maybe [ArgStr]
go_str (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1) StrDmd
s'
    go_str a
n StrDmd
HyperStr   = a -> StrDmd -> Maybe [ArgStr]
go_str (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1) StrDmd
HyperStr
    go_str a
_ StrDmd
_          = Maybe [ArgStr]
forall a. Maybe a
Nothing

    go_abs :: t -> UseDmd -> Maybe [ArgUse]
go_abs t
0 UseDmd
dmd            = Int -> UseDmd -> Maybe [ArgUse]
splitUseProdDmd Int
arity UseDmd
dmd
    go_abs t
n (UCall Count
One UseDmd
u') = t -> UseDmd -> Maybe [ArgUse]
go_abs (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1) UseDmd
u'
    go_abs t
_ UseDmd
_              = Maybe [ArgUse]
forall a. Maybe a
Nothing

dmdTransformDictSelSig :: StrictSig -> CleanDemand -> DmdType
-- Like dmdTransformDataConSig, we have a special demand transformer
-- for dictionary selectors.  If the selector is saturated (ie has one
-- argument: the dictionary), we feed the demand on the result into
-- the indicated dictionary component.
dmdTransformDictSelSig :: StrictSig -> CleanDemand -> DmdType
dmdTransformDictSelSig (StrictSig (DmdType DmdEnv
_ [Demand
dict_dmd] DmdResult
_)) CleanDemand
cd
   | (CleanDemand
cd',DmdShell
defer_use) <- CleanDemand -> (CleanDemand, DmdShell)
peelCallDmd CleanDemand
cd
   , Just [Demand]
jds <- Demand -> Maybe [Demand]
splitProdDmd_maybe Demand
dict_dmd
   = DmdShell -> DmdType -> DmdType
postProcessUnsat DmdShell
defer_use (DmdType -> DmdType) -> DmdType -> DmdType
forall a b. (a -> b) -> a -> b
$
     DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [CleanDemand -> Demand
mkOnceUsedDmd (CleanDemand -> Demand) -> CleanDemand -> Demand
forall a b. (a -> b) -> a -> b
$ [Demand] -> CleanDemand
mkProdDmd ([Demand] -> CleanDemand) -> [Demand] -> CleanDemand
forall a b. (a -> b) -> a -> b
$ (Demand -> Demand) -> [Demand] -> [Demand]
forall a b. (a -> b) -> [a] -> [b]
map (CleanDemand -> Demand -> Demand
enhance CleanDemand
cd') [Demand]
jds] DmdResult
topRes
   | Bool
otherwise
   = DmdType
nopDmdType              -- See Note [Demand transformer for a dictionary selector]
  where
    enhance :: CleanDemand -> Demand -> Demand
enhance CleanDemand
cd Demand
old | Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isAbsDmd Demand
old = Demand
old
                   | Bool
otherwise    = CleanDemand -> Demand
mkOnceUsedDmd CleanDemand
cd  -- This is the one!

dmdTransformDictSelSig StrictSig
_ CleanDemand
_ = String -> DmdType
forall a. String -> a
panic String
"dmdTransformDictSelSig: no args"

{-
Note [Demand transformer for a dictionary selector]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If we evaluate (op dict-expr) under demand 'd', then we can push the demand 'd'
into the appropriate field of the dictionary. What *is* the appropriate field?
We just look at the strictness signature of the class op, which will be
something like: U(AAASAAAAA).  Then replace the 'S' by the demand 'd'.

For single-method classes, which are represented by newtypes the signature
of 'op' won't look like U(...), so the splitProdDmd_maybe will fail.
That's fine: if we are doing strictness analysis we are also doing inlining,
so we'll have inlined 'op' into a cast.  So we can bale out in a conservative
way, returning nopDmdType.

It is (just.. Trac #8329) possible to be running strictness analysis *without*
having inlined class ops from single-method classes.  Suppose you are using
ghc --make; and the first module has a local -O0 flag.  So you may load a class
without interface pragmas, ie (currently) without an unfolding for the class
ops.   Now if a subsequent module in the --make sweep has a local -O flag
you might do strictness analysis, but there is no inlining for the class op.
This is weird, so I'm not worried about whether this optimises brilliantly; but
it should not fall over.
-}

argsOneShots :: StrictSig -> Arity -> [[OneShotInfo]]
-- See Note [Computing one-shot info]
argsOneShots :: StrictSig -> Int -> [[OneShotInfo]]
argsOneShots (StrictSig (DmdType DmdEnv
_ [Demand]
arg_ds DmdResult
_)) Int
n_val_args
  | Bool
unsaturated_call = []
  | Bool
otherwise = [Demand] -> [[OneShotInfo]]
go [Demand]
arg_ds
  where
    unsaturated_call :: Bool
unsaturated_call = [Demand]
arg_ds [Demand] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` Int
n_val_args

    go :: [Demand] -> [[OneShotInfo]]
go []               = []
    go (Demand
arg_d : [Demand]
arg_ds) = Demand -> [OneShotInfo]
argOneShots Demand
arg_d [OneShotInfo] -> [[OneShotInfo]] -> [[OneShotInfo]]
forall a. [a] -> [[a]] -> [[a]]
`cons` [Demand] -> [[OneShotInfo]]
go [Demand]
arg_ds

    -- Avoid list tail like [ [], [], [] ]
    cons :: [a] -> [[a]] -> [[a]]
cons [] [] = []
    cons [a]
a  [[a]]
as = [a]
a[a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
:[[a]]
as

-- saturatedByOneShots n C1(C1(...)) = True,
--   <=>
-- there are at least n nested C1(..) calls
-- See Note [Demand on the worker] in WorkWrap
saturatedByOneShots :: Int -> Demand -> Bool
saturatedByOneShots :: Int -> Demand -> Bool
saturatedByOneShots Int
n (JD { ud :: forall s u. JointDmd s u -> u
ud = ArgUse
usg })
  = case ArgUse
usg of
      Use Count
_ UseDmd
arg_usg -> Int -> UseDmd -> Bool
forall t. (Eq t, Num t) => t -> UseDmd -> Bool
go Int
n UseDmd
arg_usg
      ArgUse
_             -> Bool
False
  where
    go :: t -> UseDmd -> Bool
go t
0 UseDmd
_             = Bool
True
    go t
n (UCall Count
One UseDmd
u) = t -> UseDmd -> Bool
go (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
1) UseDmd
u
    go t
_ UseDmd
_             = Bool
False

argOneShots :: Demand          -- depending on saturation
            -> [OneShotInfo]
argOneShots :: Demand -> [OneShotInfo]
argOneShots (JD { ud :: forall s u. JointDmd s u -> u
ud = ArgUse
usg })
  = case ArgUse
usg of
      Use Count
_ UseDmd
arg_usg -> UseDmd -> [OneShotInfo]
go UseDmd
arg_usg
      ArgUse
_             -> []
  where
    go :: UseDmd -> [OneShotInfo]
go (UCall Count
One  UseDmd
u) = OneShotInfo
OneShotLam OneShotInfo -> [OneShotInfo] -> [OneShotInfo]
forall a. a -> [a] -> [a]
: UseDmd -> [OneShotInfo]
go UseDmd
u
    go (UCall Count
Many UseDmd
u) = OneShotInfo
NoOneShotInfo OneShotInfo -> [OneShotInfo] -> [OneShotInfo]
forall a. a -> [a] -> [a]
: UseDmd -> [OneShotInfo]
go UseDmd
u
    go UseDmd
_              = []

{- Note [Computing one-shot info]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider a call
    f (\pqr. e1) (\xyz. e2) e3
where f has usage signature
    C1(C(C1(U))) C1(U) U
Then argsOneShots returns a [[OneShotInfo]] of
    [[OneShot,NoOneShotInfo,OneShot],  [OneShot]]
The occurrence analyser propagates this one-shot infor to the
binders \pqr and \xyz; see Note [Use one-shot information] in OccurAnal.
-}

-- appIsBottom returns true if an application to n args
-- would diverge or throw an exception
-- See Note [Unsaturated applications]
appIsBottom :: StrictSig -> Int -> Bool
appIsBottom :: StrictSig -> Int -> Bool
appIsBottom (StrictSig (DmdType DmdEnv
_ [Demand]
ds DmdResult
res)) Int
n
            | DmdResult -> Bool
isBotRes DmdResult
res                   = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Demand] -> Int -> Bool
forall a. [a] -> Int -> Bool
lengthExceeds [Demand]
ds Int
n
appIsBottom StrictSig
_                              Int
_ = Bool
False

{-
Note [Unsaturated applications]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If a function having bottom as its demand result is applied to a less
number of arguments than its syntactic arity, we cannot say for sure
that it is going to diverge. This is the reason why we use the
function appIsBottom, which, given a strictness signature and a number
of arguments, says conservatively if the function is going to diverge
or not.

Zap absence or one-shot information, under control of flags

Note [Killing usage information]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The flags -fkill-one-shot and -fkill-absence let you switch off the generation
of absence or one-shot information altogether.  This is only used for performance
tests, to see how important they are.
-}

zapUsageEnvSig :: StrictSig -> StrictSig
-- Remove the usage environment from the demand
zapUsageEnvSig :: StrictSig -> StrictSig
zapUsageEnvSig (StrictSig (DmdType DmdEnv
_ [Demand]
ds DmdResult
r)) = [Demand] -> DmdResult -> StrictSig
mkClosedStrictSig [Demand]
ds DmdResult
r

zapUsageDemand :: Demand -> Demand
-- Remove the usage info, but not the strictness info, from the demand
zapUsageDemand :: Demand -> Demand
zapUsageDemand = KillFlags -> Demand -> Demand
kill_usage (KillFlags -> Demand -> Demand) -> KillFlags -> Demand -> Demand
forall a b. (a -> b) -> a -> b
$ KillFlags :: Bool -> Bool -> Bool -> KillFlags
KillFlags
    { kf_abs :: Bool
kf_abs         = Bool
True
    , kf_used_once :: Bool
kf_used_once   = Bool
True
    , kf_called_once :: Bool
kf_called_once = Bool
True
    }

-- | Remove all 1* information (but not C1 information) from the demand
zapUsedOnceDemand :: Demand -> Demand
zapUsedOnceDemand :: Demand -> Demand
zapUsedOnceDemand = KillFlags -> Demand -> Demand
kill_usage (KillFlags -> Demand -> Demand) -> KillFlags -> Demand -> Demand
forall a b. (a -> b) -> a -> b
$ KillFlags :: Bool -> Bool -> Bool -> KillFlags
KillFlags
    { kf_abs :: Bool
kf_abs         = Bool
False
    , kf_used_once :: Bool
kf_used_once   = Bool
True
    , kf_called_once :: Bool
kf_called_once = Bool
False
    }

-- | Remove all 1* information (but not C1 information) from the strictness
--   signature
zapUsedOnceSig :: StrictSig -> StrictSig
zapUsedOnceSig :: StrictSig -> StrictSig
zapUsedOnceSig (StrictSig (DmdType DmdEnv
env [Demand]
ds DmdResult
r))
    = DmdType -> StrictSig
StrictSig (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
env ((Demand -> Demand) -> [Demand] -> [Demand]
forall a b. (a -> b) -> [a] -> [b]
map Demand -> Demand
zapUsedOnceDemand [Demand]
ds) DmdResult
r)

killUsageDemand :: DynFlags -> Demand -> Demand
-- See Note [Killing usage information]
killUsageDemand :: DynFlags -> Demand -> Demand
killUsageDemand DynFlags
dflags Demand
dmd
  | Just KillFlags
kfs <- DynFlags -> Maybe KillFlags
killFlags DynFlags
dflags = KillFlags -> Demand -> Demand
kill_usage KillFlags
kfs Demand
dmd
  | Bool
otherwise                    = Demand
dmd

killUsageSig :: DynFlags -> StrictSig -> StrictSig
-- See Note [Killing usage information]
killUsageSig :: DynFlags -> StrictSig -> StrictSig
killUsageSig DynFlags
dflags sig :: StrictSig
sig@(StrictSig (DmdType DmdEnv
env [Demand]
ds DmdResult
r))
  | Just KillFlags
kfs <- DynFlags -> Maybe KillFlags
killFlags DynFlags
dflags = DmdType -> StrictSig
StrictSig (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
env ((Demand -> Demand) -> [Demand] -> [Demand]
forall a b. (a -> b) -> [a] -> [b]
map (KillFlags -> Demand -> Demand
kill_usage KillFlags
kfs) [Demand]
ds) DmdResult
r)
  | Bool
otherwise                    = StrictSig
sig

data KillFlags = KillFlags
    { KillFlags -> Bool
kf_abs         :: Bool
    , KillFlags -> Bool
kf_used_once   :: Bool
    , KillFlags -> Bool
kf_called_once :: Bool
    }

killFlags :: DynFlags -> Maybe KillFlags
-- See Note [Killing usage information]
killFlags :: DynFlags -> Maybe KillFlags
killFlags DynFlags
dflags
  | Bool -> Bool
not Bool
kf_abs Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
kf_used_once = Maybe KillFlags
forall a. Maybe a
Nothing
  | Bool
otherwise                      = KillFlags -> Maybe KillFlags
forall a. a -> Maybe a
Just (KillFlags :: Bool -> Bool -> Bool -> KillFlags
KillFlags {Bool
kf_called_once :: Bool
kf_used_once :: Bool
kf_abs :: Bool
kf_called_once :: Bool
kf_used_once :: Bool
kf_abs :: Bool
..})
  where
    kf_abs :: Bool
kf_abs         = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_KillAbsence DynFlags
dflags
    kf_used_once :: Bool
kf_used_once   = GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_KillOneShot DynFlags
dflags
    kf_called_once :: Bool
kf_called_once = Bool
kf_used_once

kill_usage :: KillFlags -> Demand -> Demand
kill_usage :: KillFlags -> Demand -> Demand
kill_usage KillFlags
kfs (JD {sd :: forall s u. JointDmd s u -> s
sd = ArgStr
s, ud :: forall s u. JointDmd s u -> u
ud = ArgUse
u}) = JD :: forall s u. s -> u -> JointDmd s u
JD {sd :: ArgStr
sd = ArgStr
s, ud :: ArgUse
ud = KillFlags -> ArgUse -> ArgUse
zap_musg KillFlags
kfs ArgUse
u}

zap_musg :: KillFlags -> ArgUse -> ArgUse
zap_musg :: KillFlags -> ArgUse -> ArgUse
zap_musg KillFlags
kfs ArgUse
Abs
  | KillFlags -> Bool
kf_abs KillFlags
kfs = ArgUse
useTop
  | Bool
otherwise  = ArgUse
forall u. Use u
Abs
zap_musg KillFlags
kfs (Use Count
c UseDmd
u)
  | KillFlags -> Bool
kf_used_once KillFlags
kfs = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
Many (KillFlags -> UseDmd -> UseDmd
zap_usg KillFlags
kfs UseDmd
u)
  | Bool
otherwise        = Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
c    (KillFlags -> UseDmd -> UseDmd
zap_usg KillFlags
kfs UseDmd
u)

zap_usg :: KillFlags -> UseDmd -> UseDmd
zap_usg :: KillFlags -> UseDmd -> UseDmd
zap_usg KillFlags
kfs (UCall Count
c UseDmd
u)
    | KillFlags -> Bool
kf_called_once KillFlags
kfs = Count -> UseDmd -> UseDmd
UCall Count
Many (KillFlags -> UseDmd -> UseDmd
zap_usg KillFlags
kfs UseDmd
u)
    | Bool
otherwise          = Count -> UseDmd -> UseDmd
UCall Count
c    (KillFlags -> UseDmd -> UseDmd
zap_usg KillFlags
kfs UseDmd
u)
zap_usg KillFlags
kfs (UProd [ArgUse]
us)   = [ArgUse] -> UseDmd
UProd ((ArgUse -> ArgUse) -> [ArgUse] -> [ArgUse]
forall a b. (a -> b) -> [a] -> [b]
map (KillFlags -> ArgUse -> ArgUse
zap_musg KillFlags
kfs) [ArgUse]
us)
zap_usg KillFlags
_   UseDmd
u            = UseDmd
u

-- If the argument is a used non-newtype dictionary, give it strict
-- demand. Also split the product type & demand and recur in order to
-- similarly strictify the argument's contained used non-newtype
-- superclass dictionaries. We use the demand as our recursive measure
-- to guarantee termination.
strictifyDictDmd :: Type -> Demand -> Demand
strictifyDictDmd :: Type -> Demand -> Demand
strictifyDictDmd Type
ty Demand
dmd = case Demand -> ArgUse
forall s u. JointDmd s u -> u
getUseDmd Demand
dmd of
  Use Count
n UseDmd
_ |
    Just (TyCon
tycon, [Type]
_arg_tys, DataCon
_data_con, [Type]
inst_con_arg_tys)
      <- Type -> Maybe (TyCon, [Type], DataCon, [Type])
splitDataProductType_maybe Type
ty,
    Bool -> Bool
not (TyCon -> Bool
isNewTyCon TyCon
tycon), TyCon -> Bool
isClassTyCon TyCon
tycon -- is a non-newtype dictionary
    -> Demand
seqDmd Demand -> Demand -> Demand
`bothDmd` -- main idea: ensure it's strict
       case Demand -> Maybe [Demand]
splitProdDmd_maybe Demand
dmd of
         -- superclass cycles should not be a problem, since the demand we are
         -- consuming would also have to be infinite in order for us to diverge
         Maybe [Demand]
Nothing -> Demand
dmd -- no components have interesting demand, so stop
                        -- looking for superclass dicts
         Just [Demand]
dmds
           | (Demand -> Bool) -> [Demand] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool -> Bool
not (Bool -> Bool) -> (Demand -> Bool) -> Demand -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Demand -> Bool
forall s u. JointDmd (Str s) (Use u) -> Bool
isAbsDmd) [Demand]
dmds -> Demand
evalDmd
             -- abstract to strict w/ arbitrary component use, since this
             -- smells like reboxing; results in CBV boxed
             --
             -- TODO revisit this if we ever do boxity analysis
           | Bool
otherwise -> case [Demand] -> CleanDemand
mkProdDmd ([Demand] -> CleanDemand) -> [Demand] -> CleanDemand
forall a b. (a -> b) -> a -> b
$ (Type -> Demand -> Demand) -> [Type] -> [Demand] -> [Demand]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Type -> Demand -> Demand
strictifyDictDmd [Type]
inst_con_arg_tys [Demand]
dmds of
               JD {sd :: forall s u. JointDmd s u -> s
sd = StrDmd
s,ud :: forall s u. JointDmd s u -> u
ud = UseDmd
a} -> ArgStr -> ArgUse -> Demand
forall s u. s -> u -> JointDmd s u
JD (ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
s) (Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
n UseDmd
a)
             -- TODO could optimize with an aborting variant of zipWith since
             -- the superclass dicts are always a prefix
  ArgUse
_ -> Demand
dmd -- unused or not a dictionary

strictifyDmd :: Demand -> Demand
strictifyDmd :: Demand -> Demand
strictifyDmd dmd :: Demand
dmd@(JD { sd :: forall s u. JointDmd s u -> s
sd = ArgStr
str })
  = Demand
dmd { sd :: ArgStr
sd = ArgStr
str ArgStr -> ArgStr -> ArgStr
`bothArgStr` ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
VanStr StrDmd
HeadStr }

{-
Note [HyperStr and Use demands]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The information "HyperStr" needs to be in the strictness signature, and not in
the demand signature, because we still want to know about the demand on things. Consider

    f (x,y) True  = error (show x)
    f (x,y) False = x+1

The signature of f should be <S(SL),1*U(1*U(U),A)><S,1*U>m. If we were not
distinguishing the uses on x and y in the True case, we could either not figure
out how deeply we can unpack x, or that we do not have to pass y.


************************************************************************
*                                                                      *
                     Serialisation
*                                                                      *
************************************************************************
-}

instance Binary StrDmd where
  put_ :: BinHandle -> StrDmd -> IO ()
put_ BinHandle
bh StrDmd
HyperStr     = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
  put_ BinHandle
bh StrDmd
HeadStr      = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
  put_ BinHandle
bh (SCall StrDmd
s)    = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2
                            BinHandle -> StrDmd -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh StrDmd
s
  put_ BinHandle
bh (SProd [ArgStr]
sx)   = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
3
                            BinHandle -> [ArgStr] -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh [ArgStr]
sx
  get :: BinHandle -> IO StrDmd
get BinHandle
bh = do
         Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
         case Word8
h of
           Word8
0 -> do StrDmd -> IO StrDmd
forall (m :: * -> *) a. Monad m => a -> m a
return StrDmd
HyperStr
           Word8
1 -> do StrDmd -> IO StrDmd
forall (m :: * -> *) a. Monad m => a -> m a
return StrDmd
HeadStr
           Word8
2 -> do StrDmd
s  <- BinHandle -> IO StrDmd
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                   StrDmd -> IO StrDmd
forall (m :: * -> *) a. Monad m => a -> m a
return (StrDmd -> StrDmd
SCall StrDmd
s)
           Word8
_ -> do [ArgStr]
sx <- BinHandle -> IO [ArgStr]
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                   StrDmd -> IO StrDmd
forall (m :: * -> *) a. Monad m => a -> m a
return ([ArgStr] -> StrDmd
SProd [ArgStr]
sx)

instance Binary ExnStr where
  put_ :: BinHandle -> ExnStr -> IO ()
put_ BinHandle
bh ExnStr
VanStr = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
  put_ BinHandle
bh ExnStr
ExnStr = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1

  get :: BinHandle -> IO ExnStr
get BinHandle
bh = do Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
              ExnStr -> IO ExnStr
forall (m :: * -> *) a. Monad m => a -> m a
return (case Word8
h of
                        Word8
0 -> ExnStr
VanStr
                        Word8
_ -> ExnStr
ExnStr)

instance Binary ArgStr where
    put_ :: BinHandle -> ArgStr -> IO ()
put_ BinHandle
bh ArgStr
Lazy         = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh (Str ExnStr
x StrDmd
s)    = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
            BinHandle -> ExnStr -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh ExnStr
x
            BinHandle -> StrDmd -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh StrDmd
s

    get :: BinHandle -> IO ArgStr
get  BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> ArgStr -> IO ArgStr
forall (m :: * -> *) a. Monad m => a -> m a
return ArgStr
forall s. Str s
Lazy
              Word8
_ -> do ExnStr
x <- BinHandle -> IO ExnStr
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      StrDmd
s  <- BinHandle -> IO StrDmd
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      ArgStr -> IO ArgStr
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgStr -> IO ArgStr) -> ArgStr -> IO ArgStr
forall a b. (a -> b) -> a -> b
$ ExnStr -> StrDmd -> ArgStr
forall s. ExnStr -> s -> Str s
Str ExnStr
x StrDmd
s

instance Binary Count where
    put_ :: BinHandle -> Count -> IO ()
put_ BinHandle
bh Count
One  = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh Count
Many = do BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1

    get :: BinHandle -> IO Count
get  BinHandle
bh = do Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
                 case Word8
h of
                   Word8
0 -> Count -> IO Count
forall (m :: * -> *) a. Monad m => a -> m a
return Count
One
                   Word8
_ -> Count -> IO Count
forall (m :: * -> *) a. Monad m => a -> m a
return Count
Many

instance Binary ArgUse where
    put_ :: BinHandle -> ArgUse -> IO ()
put_ BinHandle
bh ArgUse
Abs          = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh (Use Count
c UseDmd
u)    = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
            BinHandle -> Count -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Count
c
            BinHandle -> UseDmd -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh UseDmd
u

    get :: BinHandle -> IO ArgUse
get  BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> ArgUse -> IO ArgUse
forall (m :: * -> *) a. Monad m => a -> m a
return ArgUse
forall u. Use u
Abs
              Word8
_ -> do Count
c  <- BinHandle -> IO Count
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      UseDmd
u  <- BinHandle -> IO UseDmd
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      ArgUse -> IO ArgUse
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgUse -> IO ArgUse) -> ArgUse -> IO ArgUse
forall a b. (a -> b) -> a -> b
$ Count -> UseDmd -> ArgUse
forall u. Count -> u -> Use u
Use Count
c UseDmd
u

instance Binary UseDmd where
    put_ :: BinHandle -> UseDmd -> IO ()
put_ BinHandle
bh UseDmd
Used         = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0
    put_ BinHandle
bh UseDmd
UHead        = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
    put_ BinHandle
bh (UCall Count
c UseDmd
u)    = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2
            BinHandle -> Count -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Count
c
            BinHandle -> UseDmd -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh UseDmd
u
    put_ BinHandle
bh (UProd [ArgUse]
ux)   = do
            BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
3
            BinHandle -> [ArgUse] -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh [ArgUse]
ux

    get :: BinHandle -> IO UseDmd
get  BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> UseDmd -> IO UseDmd
forall (m :: * -> *) a. Monad m => a -> m a
return (UseDmd -> IO UseDmd) -> UseDmd -> IO UseDmd
forall a b. (a -> b) -> a -> b
$ UseDmd
Used
              Word8
1 -> UseDmd -> IO UseDmd
forall (m :: * -> *) a. Monad m => a -> m a
return (UseDmd -> IO UseDmd) -> UseDmd -> IO UseDmd
forall a b. (a -> b) -> a -> b
$ UseDmd
UHead
              Word8
2 -> do Count
c <- BinHandle -> IO Count
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      UseDmd
u <- BinHandle -> IO UseDmd
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      UseDmd -> IO UseDmd
forall (m :: * -> *) a. Monad m => a -> m a
return (Count -> UseDmd -> UseDmd
UCall Count
c UseDmd
u)
              Word8
_ -> do [ArgUse]
ux <- BinHandle -> IO [ArgUse]
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
                      UseDmd -> IO UseDmd
forall (m :: * -> *) a. Monad m => a -> m a
return ([ArgUse] -> UseDmd
UProd [ArgUse]
ux)

instance (Binary s, Binary u) => Binary (JointDmd s u) where
    put_ :: BinHandle -> JointDmd s u -> IO ()
put_ BinHandle
bh (JD { sd :: forall s u. JointDmd s u -> s
sd = s
x, ud :: forall s u. JointDmd s u -> u
ud = u
y }) = do BinHandle -> s -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh s
x; BinHandle -> u -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh u
y
    get :: BinHandle -> IO (JointDmd s u)
get  BinHandle
bh = do
              s
x <- BinHandle -> IO s
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
              u
y <- BinHandle -> IO u
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
              JointDmd s u -> IO (JointDmd s u)
forall (m :: * -> *) a. Monad m => a -> m a
return (JointDmd s u -> IO (JointDmd s u))
-> JointDmd s u -> IO (JointDmd s u)
forall a b. (a -> b) -> a -> b
$ JD :: forall s u. s -> u -> JointDmd s u
JD { sd :: s
sd = s
x, ud :: u
ud = u
y }

instance Binary StrictSig where
    put_ :: BinHandle -> StrictSig -> IO ()
put_ BinHandle
bh (StrictSig DmdType
aa) = do
            BinHandle -> DmdType -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh DmdType
aa
    get :: BinHandle -> IO StrictSig
get BinHandle
bh = do
          DmdType
aa <- BinHandle -> IO DmdType
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
          StrictSig -> IO StrictSig
forall (m :: * -> *) a. Monad m => a -> m a
return (DmdType -> StrictSig
StrictSig DmdType
aa)

instance Binary DmdType where
  -- Ignore DmdEnv when spitting out the DmdType
  put_ :: BinHandle -> DmdType -> IO ()
put_ BinHandle
bh (DmdType DmdEnv
_ [Demand]
ds DmdResult
dr)
       = do BinHandle -> [Demand] -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh [Demand]
ds
            BinHandle -> DmdResult -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh DmdResult
dr
  get :: BinHandle -> IO DmdType
get BinHandle
bh
      = do [Demand]
ds <- BinHandle -> IO [Demand]
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
           DmdResult
dr <- BinHandle -> IO DmdResult
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh
           DmdType -> IO DmdType
forall (m :: * -> *) a. Monad m => a -> m a
return (DmdEnv -> [Demand] -> DmdResult -> DmdType
DmdType DmdEnv
emptyDmdEnv [Demand]
ds DmdResult
dr)

instance Binary DmdResult where
  put_ :: BinHandle -> DmdResult -> IO ()
put_ BinHandle
bh (Dunno CPRResult
c)     = do { BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0; BinHandle -> CPRResult -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh CPRResult
c }
  put_ BinHandle
bh DmdResult
ThrowsExn     = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
  put_ BinHandle
bh DmdResult
Diverges      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2

  get :: BinHandle -> IO DmdResult
get BinHandle
bh = do { Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
              ; case Word8
h of
                  Word8
0 -> do { CPRResult
c <- BinHandle -> IO CPRResult
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; DmdResult -> IO DmdResult
forall (m :: * -> *) a. Monad m => a -> m a
return (CPRResult -> DmdResult
forall r. r -> Termination r
Dunno CPRResult
c) }
                  Word8
1 -> DmdResult -> IO DmdResult
forall (m :: * -> *) a. Monad m => a -> m a
return DmdResult
forall r. Termination r
ThrowsExn
                  Word8
_ -> DmdResult -> IO DmdResult
forall (m :: * -> *) a. Monad m => a -> m a
return DmdResult
forall r. Termination r
Diverges }

instance Binary CPRResult where
    put_ :: BinHandle -> CPRResult -> IO ()
put_ BinHandle
bh (RetSum Int
n)   = do { BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
0; BinHandle -> Int -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
put_ BinHandle
bh Int
n }
    put_ BinHandle
bh CPRResult
RetProd      = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
1
    put_ BinHandle
bh CPRResult
NoCPR        = BinHandle -> Word8 -> IO ()
putByte BinHandle
bh Word8
2

    get :: BinHandle -> IO CPRResult
get  BinHandle
bh = do
            Word8
h <- BinHandle -> IO Word8
getByte BinHandle
bh
            case Word8
h of
              Word8
0 -> do { Int
n <- BinHandle -> IO Int
forall a. Binary a => BinHandle -> IO a
get BinHandle
bh; CPRResult -> IO CPRResult
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> CPRResult
RetSum Int
n) }
              Word8
1 -> CPRResult -> IO CPRResult
forall (m :: * -> *) a. Monad m => a -> m a
return CPRResult
RetProd
              Word8
_ -> CPRResult -> IO CPRResult
forall (m :: * -> *) a. Monad m => a -> m a
return CPRResult
NoCPR