{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE BinaryLiterals #-} {-# LANGUAGE PatternSynonyms #-} {- (c) The University of Glasgow 2006 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 -} -- | A language to express the evaluation context of an expression as a -- 'Demand' and track how an expression evaluates free variables and arguments -- in turn as a 'DmdType'. -- -- Lays out the abstract domain for "GHC.Core.Opt.DmdAnal". module GHC.Types.Demand ( -- * Demands Boxity(..), Card(C_00, C_01, C_0N, C_10, C_11, C_1N), CardNonAbs, CardNonOnce, Demand(AbsDmd, BotDmd, (:*)), SubDemand(Prod, Poly), mkProd, viewProd, -- ** Algebra absDmd, topDmd, botDmd, seqDmd, topSubDmd, -- *** Least upper bound lubCard, lubDmd, lubSubDmd, -- *** Plus plusCard, plusDmd, plusSubDmd, -- *** Multiply multCard, multDmd, multSubDmd, -- ** Predicates on @Card@inalities and @Demand@s isAbs, isUsedOnce, isStrict, isAbsDmd, isUsedOnceDmd, isStrUsedDmd, isStrictDmd, isTopDmd, isWeakDmd, onlyBoxedArguments, -- ** Special demands evalDmd, -- *** Demands used in PrimOp signatures lazyApply1Dmd, lazyApply2Dmd, strictOnceApply1Dmd, strictManyApply1Dmd, -- ** Other @Demand@ operations oneifyCard, oneifyDmd, strictifyDmd, strictifyDictDmd, lazifyDmd, peelCallDmd, peelManyCalls, mkCalledOnceDmd, mkCalledOnceDmds, mkWorkerDemand, subDemandIfEvaluated, -- ** Extracting one-shot information argOneShots, argsOneShots, saturatedByOneShots, -- ** Manipulating Boxity of a Demand unboxDeeplyDmd, -- * Divergence Divergence(..), topDiv, botDiv, exnDiv, lubDivergence, isDeadEndDiv, -- * Demand environments DmdEnv(..), addVarDmdEnv, mkTermDmdEnv, nopDmdEnv, plusDmdEnv, plusDmdEnvs, reuseEnv, -- * Demand types DmdType(..), dmdTypeDepth, -- ** Algebra nopDmdType, botDmdType, lubDmdType, plusDmdType, multDmdType, discardArgDmds, -- ** Other operations peelFV, findIdDemand, addDemand, splitDmdTy, deferAfterPreciseException, -- * Demand signatures DmdSig(..), mkDmdSigForArity, mkClosedDmdSig, mkVanillaDmdSig, splitDmdSig, dmdSigDmdEnv, hasDemandEnvSig, nopSig, botSig, isNopSig, isBottomingSig, isDeadEndSig, isDeadEndAppSig, trimBoxityDmdSig, transferArgBoxityDmdSig, -- ** Handling arity adjustments prependArgsDmdSig, etaConvertDmdSig, -- * Demand transformers from demand signatures DmdTransformer, dmdTransformSig, dmdTransformDataConSig, dmdTransformDictSelSig, -- * Trim to a type shape TypeShape(..), trimToType, trimBoxity, -- * @seq@ing stuff seqDemand, seqDemandList, seqDmdType, seqDmdSig, -- * Zapping usage information zapUsageDemand, zapDmdEnvSig, zapUsedOnceDemand, zapUsedOnceSig ) where import GHC.Prelude import GHC.Types.Var import GHC.Types.Var.Env import GHC.Types.Unique.FM import GHC.Types.Basic import GHC.Data.Maybe ( orElse ) import GHC.Core.Type ( Type ) import GHC.Core.TyCon ( isNewTyCon, isClassTyCon ) import GHC.Core.DataCon ( splitDataProductType_maybe, StrictnessMark, isMarkedStrict ) import GHC.Core.Multiplicity ( scaledThing ) import GHC.Utils.Binary import GHC.Utils.Misc import GHC.Utils.Outputable import GHC.Utils.Panic import GHC.Utils.Panic.Plain import Data.Coerce (coerce) import Data.Function {- ************************************************************************ * * Boxity: Whether the box of something is used * * ************************************************************************ -} {- Note [Strictness and Unboxing] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If an argument is used strictly by the function body, we may use use call-by-value instead of call-by-need for that argument. What's more, we may unbox an argument that is used strictly, discarding the box at the call site. This can reduce allocations of the program drastically if the box really isn't needed in the function body. Here's an example: ``` even :: Int -> Bool even (I# 0) = True even (I# 1) = False even (I# n) = even (I# (n -# 2)) ``` All three code paths of 'even' are (a) strict in the argument, and (b) immediately discard the boxed 'Int'. Now if we have a call site like `even (I# 42)`, then it would be terrible to allocate the 'I#' box for the argument only to tear it apart immediately in the body of 'even'! Hence, worker/wrapper will allocate a wrapper for 'even' that not only uses call-by-value for the argument (e.g., `case I# 42 of b { $weven b }`), but also *unboxes* the argument, resulting in ``` even :: Int -> Bool even (I# n) = $weven n $weven :: Int# -> Bool $weven 0 = True $weven 1 = False $weven n = $weven (n -# 2) ``` And now the box in `even (I# 42)` will cancel away after inlining the wrapper. As far as the permission to unbox is concerned, *evaluatedness* of the argument is the important trait. Unboxing implies eager evaluation of an argument and we don't want to change the termination properties of the function. One way to ensure that is to unbox strict arguments only, but strictness is only a sufficient condition for evaluatedness. See Note [Unboxing evaluated arguments] in "GHC.Core.Opt.DmdAnal", where we manage to unbox *strict fields* of unboxed arguments that the function is not actually strict in, simply by realising that those fields have to be evaluated. Note [Boxity analysis] ~~~~~~~~~~~~~~~~~~~~~~ Alas, we don't want to unbox *every* strict argument (as Note [Strictness and Unboxing] might suggest). Here's an example (from T19871): ``` data Huge = H Bool Bool ... Bool ann :: Huge -> (Bool, Huge) ann h@(Huge True _ ... _) = (False, h) ann h = (True, h) ``` Unboxing 'h' yields ``` $wann :: Bool -> Bool -> ... -> Bool -> (Bool, Huge) $wann True b2 ... bn = (False, Huge True b2 ... bn) $wann b1 b2 ... bn = (True, Huge b1 b2 ... bn) ``` The pair constructor really needs its fields boxed. But '$wann' doesn't get passed 'h' anymore, only its components! Ergo it has to reallocate the 'Huge' box, in a process called "reboxing". After w/w, call sites like `case ... of Just h -> ann h` pay for the allocation of the additional box. In earlier versions of GHC we simply accepted that reboxing would sometimes happen, but we found some cases where it made a big difference: #19407, for example. We therefore perform a simple syntactic boxity analysis that piggy-backs on demand analysis in order to determine whether the box of a strict argument is always discarded in the function body, in which case we can pass it unboxed without risking regressions such as in 'ann' above. But as soon as one use needs the box, we want Boxed to win over any Unboxed uses. The demand signature (cf. Note [Demand notation]) will say whether it uses its arguments boxed or unboxed. Indeed it does so for every sub-component of the argument demand. Here's an example: ``` f :: (Int, Int) -> Bool f (a, b) = even (a + b) -- demand signature: <1!P(1!L,1!L)> ``` The '!' indicates places where we want to unbox, the lack thereof indicates the box is used by the function. Boxity flags are part of the 'Poly' and 'Prod' 'SubDemand's, see Note [Why Boxity in SubDemand and not in Demand?]. The given demand signature says "Unbox the pair and then nestedly unbox its two fields". By contrast, the demand signature of 'ann' above would look like <1P(1L,L,...,L)>, lacking any '!'. A demand signature like <1P(1!L)> -- Boxed outside but Unboxed in the field -- doesn't make a lot of sense, as we can never unbox the field without unboxing the containing record. See Note [Finalising boxity for demand signatures] in "GHC.Core.Opt.DmdAnal" for how we avoid to spread this and other kinds of misinformed boxities. Due to various practical reasons, Boxity Analysis is not conservative at times. Here are reasons for too much optimism: * Note [Function body boxity and call sites] is an observation about when it is beneficial to unbox a parameter that is returned from a function. Note [Unboxed demand on function bodies returning small products] derives a heuristic from the former Note, pretending that all call sites of a function need returned small products Unboxed. * Note [Boxity for bottoming functions] in DmdAnal makes all bottoming functions unbox their arguments, incurring reboxing in code paths that will diverge anyway. In turn we get more unboxing in hot code paths. Boxity analysis fixes a number of issues: #19871, #19407, #4267, #16859, #18907, #13331 Note [Function body boxity and call sites] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider (from T5949) ``` f n p = case n of 0 -> p :: (a, b) _ -> f (n-1) p -- Worker/wrapper split if we decide to unbox: $wf n x y = case n of 0 -> (# x, y #) _ -> $wf (n-1) x y f n (x,y) = case $wf n x y of (# r, s #) -> (r,s) ``` When is it better to /not/ to unbox 'p'? That depends on the callers of 'f'! If all call sites 1. Wouldn't need to allocate fresh boxes for 'p', and 2. Needed the result pair of 'f' boxed Only then we'd see an increase in allocation resulting from unboxing. But as soon as only one of (1) or (2) holds, it really doesn't matter if 'f' unboxes 'p' (and its result, it's important that CPR follows suit). For example ``` res = ... case f m (field t) of (r1,r2) -> ... -- (1) holds arg = ... [ f m (x,y) ] ... -- (2) holds ``` Because one of the boxes in the call site can cancel away: ``` res = ... case field1 t of (x1,x2) -> case field2 t of (y1,y2) -> case $wf x1 x2 y1 y2 of (#r1,r2#) -> ... arg = ... [ case $wf x1 x2 y1 y2 of (#r1,r2#) -> (r1,r2) ] ... ``` And when call sites neither have arg boxes (1) nor need the result boxed (2), then hesitating to unbox means /more/ allocation in the call site because of the need for fresh argument boxes. Summary: If call sites that satisfy both (1) and (2) occur more often than call sites that satisfy neither condition, then it's best /not/ to unbox 'p'. Note [Unboxed demand on function bodies returning small products] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Note [Boxity analysis] achieves its biggest wins when we avoid reboxing huge records. But when we return small products from a function, we often get faster programs by pretending that the caller unboxes the result. Long version: Observation: Big record arguments (e.g., DynFlags) tend to be modified much less frequently than small records (e.g., Int). Result: Big records tend to be passed around boxed (unmodified) much more frequently than small records. Consequence: The larger the record, the more likely conditions (1) and (2) from Note [Function body boxity and call sites] are met, in which case unboxing returned parameters leads to reboxing. So we put an Unboxed demand on function bodies returning small products and a Boxed demand on the others. What is regarded a small product is controlled by the -fdmd-unbox-width flag. This also manages to unbox functions like ``` sum z [] = z sum (I# n) ((I# x):xs) = sum (I# (n +# x)) xs ``` where we can unbox 'z' on the grounds that it's but a small box anyway. That in turn means that the I# allocation in the recursive call site can cancel away and we get a non-allocating loop, nice and tight. Note that this is the typical case in "Observation" above: A small box is unboxed, modified, the result reboxed for the recursive call. Originally, this came up in binary-trees' check' function and #4267 which (similarly) features a strict fold over a tree. We'd also regress in join004 and join007 if we didn't assume an optimistic Unboxed demand on the function body. T17932 features a (non-recursive) function that returns a large record, e.g., ``` flags (Options f x) = <huge> `seq` f ``` and here we won't unbox 'f' because it has 5 fields (which is larger than the default -fdmd-unbox-width threshold). Why not focus on putting Unboxed demands on *all recursive* function? Then we'd unbox ``` flags 0 (Options f x) = <huge> `seq` f flags n o = flags (n-1) o ``` and that seems hardly useful. (NB: Similar to 'f' from Note [Preserving Boxity of results is rarely a win], but there we only had 2 fields.) What about the Boxity of *fields* of a small, returned box? Consider ``` sumIO :: Int -> Int -> IO Int sumIO 0 !z = return z -- What DmdAnal sees: sumIO 0 z s = z `seq` (# s, z #) sumIO n !z = sumIO (n-1) (z+n) ``` We really want 'z' to unbox here. Yet its use in the returned unboxed pair is fundamentally a Boxed one! CPR would manage to unbox it, but DmdAnal runs before that. There is an Unboxed use in the recursive call to 'go' though. But 'IO Int' returns a small product, and 'Int' is a small product itself. So we'll put the RHS of 'sumIO' under sub-demand '!P(L,L!P(L))', indicating that *if* we evaluate 'z', we don't need the box later on. And indeed the bang will evaluate `z`, so we conclude with a total demand of `1!P(L)` on `z` and unbox it. Unlike for recursive functions, where we can often speed up the loop by unboxing at the cost of a bit of reboxing in the base case, the wins for non-recursive functions quickly turn into losses when unboxing too deeply. That happens in T11545, T18109 and T18174. Therefore, we deeply unbox recursive function bodies but only shallowly unbox non-recursive function bodies (governed by the max_depth variable). The implementation is in 'GHC.Core.Opt.DmdAnal.unboxWhenSmall'. It is quite vital, guarding for regressions in test cases like #2387, #3586, #16040, #5075 and #19871. Note that this is fundamentally working around a phase problem, namely that the results of boxity analysis depend on CPR analysis (and vice versa, of course). Note [unboxedWins] ~~~~~~~~~~~~~~~~~~ We used to use '_unboxedWins' below in 'lubBoxity', which was too optimistic. While it worked around some shortcomings of the phase separation between Boxity analysis and CPR analysis, it was a gross hack which caused regressions itself that needed all kinds of fixes and workarounds. Examples (from #21119): * As #20767 says, L and B were no longer top and bottom of our lattice * In #20746 we unboxed huge Handle types that were never needed boxed in the first place. See Note [deferAfterPreciseException]. * It also caused unboxing of huge records where we better shouldn't, for example in T19871.absent. * It became impossible to work with when implementing !7599, mostly due to the chaos that results from #20767. Conclusion: We should use 'boxedWins' in 'lubBoxity', #21119. Fortunately, we could come up with a number of better mechanisms to make up for the sometimes huge regressions that would have otherwise incured: 1. A beefed up Note [Unboxed demand on function bodies returning small products] that works recursively fixes most regressions. It's a bit unsound, but pretty well-behaved. 2. We saw bottoming functions spoil boxity in some less severe cases and countered that with Note [Boxity for bottoming functions]. -} boxedWins :: Boxity -> Boxity -> Boxity boxedWins :: Boxity -> Boxity -> Boxity boxedWins Boxity Unboxed Boxity Unboxed = Boxity Unboxed boxedWins Boxity _ !Boxity _ = Boxity Boxed _unboxedWins :: Boxity -> Boxity -> Boxity -- See Note [unboxedWins] _unboxedWins :: Boxity -> Boxity -> Boxity _unboxedWins Boxity Boxed Boxity Boxed = Boxity Boxed _unboxedWins Boxity _ !Boxity _ = Boxity Unboxed lubBoxity :: Boxity -> Boxity -> Boxity -- See Note [Boxity analysis] for the lattice. lubBoxity :: Boxity -> Boxity -> Boxity lubBoxity = Boxity -> Boxity -> Boxity boxedWins {- ************************************************************************ * * Card: Combining Strictness and Usage * * ************************************************************************ -} {- Note [Evaluation cardinalities] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The demand analyser uses an (abstraction of) /evaluation cardinality/ of type Card, to specify how many times a term is evaluated. A Card C_lu represents an /interval/ of possible cardinalities [l..u], meaning * Evaluated /at least/ 'l' times (strictness). Hence 'l' is either 0 (lazy) or 1 (strict) * Evaluated /at most/ 'u' times (usage). Hence 'u' is either 0 (not used at all), or 1 (used at most once) or n (no information) Intervals describe sets, so the underlying lattice is the powerset lattice. Usually l<=u, but we also have C_10, the interval [1,0], the empty interval, denoting the empty set. This is the bottom element of the lattice. See Note [Demand notation] for the notation we use for each of the constructors. Note [Bit vector representation for Card] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ While the 6 inhabitants of Card admit an efficient representation as an enumeration, implementing operations such as lubCard, plusCard and multCard leads to unreasonably bloated code. This was the old defn for lubCard, for example: -- Handle C_10 (bot) lubCard C_10 n = n -- bot lubCard n C_10 = n -- bot -- Handle C_0N (top) lubCard C_0N _ = C_0N -- top lubCard _ C_0N = C_0N -- top -- Handle C_11 lubCard C_00 C_11 = C_01 -- {0} ∪ {1} = {0,1} lubCard C_11 C_00 = C_01 -- {0} ∪ {1} = {0,1} lubCard C_11 n = n -- {1} is a subset of all other intervals lubCard n C_11 = n -- {1} is a subset of all other intervals -- Handle C_1N lubCard C_1N C_1N = C_1N -- reflexivity lubCard _ C_1N = C_0N -- {0} ∪ {1,n} = top lubCard C_1N _ = C_0N -- {0} ∪ {1,n} = top -- Handle C_01 lubCard C_01 _ = C_01 -- {0} ∪ {0,1} = {0,1} lubCard _ C_01 = C_01 -- {0} ∪ {0,1} = {0,1} -- Handle C_00 lubCard C_00 C_00 = C_00 -- reflexivity There's a much more compact way to encode these operations if Card is represented not as distinctly denoted intervals, but as the subset of the set of all cardinalities {0,1,n} instead. We represent such a subset as a bit vector of length 3 (which fits in an Int). That's actually pretty common for such powerset lattices. There's one bit per denoted cardinality that is set iff that cardinality is part of the denoted set, with n being the most significand bit (index 2) and 0 being represented by the least significand bit (index 0). How does that help? Well, for one, lubCard just becomes lubCard (Card a) (Card b) = Card (a .|. b) The other operations, 'plusCard' and 'multCard', become significantly more tricky, but immensely more compact. It's all straight-line code with a few bit twiddling instructions now! Note [Algebraic specification for plusCard and multCard] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The representation change in Note [Bit vector representation for Card] admits very dense definitions of 'plusCard' and 'multCard' in terms of bit twiddling, but the connection to the algebraic operations they implement is lost. It's helpful to have a written specification of what 'plusCard' and 'multCard' here that says what they should compute. * plusCard: a@[l1,u1] + b@[l2,u2] = r@[l1+l2,u1+u2]. - In terms of sets, 0 ∈ r iff 0 ∈ a and 0 ∈ b. Examples: set in C_00 + C_00, C_01 + C_0N, but not in C_10 + C_00 - In terms of sets, 1 ∈ r iff 1 ∈ a or 1 ∈ b. Examples: set in C_01 + C_00, C_0N + C_0N, but not in C_10 + C_00 - In terms of sets, n ∈ r iff n ∈ a or n ∈ b, or (1 ∈ a and 1 ∈ b), so not unlike add with carry. Examples: set in C_01 + C_01, C_01 + C_0N, but not in C_10 + C_01 - Handy special cases: o 'plusCard C_10' bumps up the strictness of its argument, just like 'lubCard C_00' lazifies it, without touching upper bounds. See also 'strictifyCard' o Similarly, 'plusCard C_0N' discards usage information (incl. absence) but leaves strictness alone. * multCard: a@[l1,u1] * b@[l2,u2] = r@[l1*l2,u1*u2]. - In terms of sets, 0 ∈ r iff 0 ∈ a or 0 ∈ b. Examples: set in C_00 * C_10, C_01 * C_1N, but not in C_10 * C_1N - In terms of sets, 1 ∈ r iff 1 ∈ a and 1 ∈ b. Examples: set in C_01 * C_01, C_01 * C_1N, but not in C_11 * C_10 - In terms of sets, n ∈ r iff 1 ∈ r and (n ∈ a or n ∈ b). Examples: set in C_1N * C_01, C_1N * C_0N, but not in C_10 * C_1N - Handy special cases: o 'multCard C_1N c' is the same as 'plusCard c c' and drops used-once info. But unlike 'plusCard C_0N', it leaves absence and strictness. o 'multCard C_01' drops strictness info, like 'lubCard C_00'. o 'multCard C_0N' does both; it discards all strictness and used-once info and retains only absence info. -} -- | Describes an interval of /evaluation cardinalities/. -- See Note [Evaluation cardinalities] -- See Note [Bit vector representation for Card] newtype Card = Card Int deriving Card -> Card -> Bool (Card -> Card -> Bool) -> (Card -> Card -> Bool) -> Eq Card forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: Card -> Card -> Bool == :: Card -> Card -> Bool $c/= :: Card -> Card -> Bool /= :: Card -> Card -> Bool Eq -- | A subtype of 'Card' for which the upper bound is never 0 (no 'C_00' or -- 'C_10'). The only four inhabitants are 'C_01', 'C_0N', 'C_11', 'C_1N'. -- Membership can be tested with 'isCardNonAbs'. -- See 'D' and 'Call' for use sites and explanation. type CardNonAbs = Card -- | A subtype of 'Card' for which the upper bound is never 1 (no 'C_01' or -- 'C_11'). The only four inhabitants are 'C_00', 'C_0N', 'C_10', 'C_1N'. -- Membership can be tested with 'isCardNonOnce'. -- See 'Poly' for use sites and explanation. type CardNonOnce = Card -- | Absent, {0}. Pretty-printed as A. pattern C_00 :: Card pattern $mC_00 :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_00 :: Card C_00 = Card 0b001 -- | Bottom, {}. Pretty-printed as A. pattern C_10 :: Card pattern $mC_10 :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_10 :: Card C_10 = Card 0b000 -- | Strict and used once, {1}. Pretty-printed as 1. pattern C_11 :: Card pattern $mC_11 :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_11 :: Card C_11 = Card 0b010 -- | Used at most once, {0,1}. Pretty-printed as M. pattern C_01 :: Card pattern $mC_01 :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_01 :: Card C_01 = Card 0b011 -- | Strict and used (possibly) many times, {1,n}. Pretty-printed as S. pattern C_1N :: Card pattern $mC_1N :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_1N :: Card C_1N = Card 0b110 -- | Every possible cardinality; the top element, {0,1,n}. Pretty-printed as L. pattern C_0N :: Card pattern $mC_0N :: forall {r}. Card -> ((# #) -> r) -> ((# #) -> r) -> r $bC_0N :: Card C_0N = Card 0b111 {-# COMPLETE C_00, C_01, C_0N, C_10, C_11, C_1N :: Card #-} _botCard, topCard :: Card _botCard :: Card _botCard = Card C_10 topCard :: Card topCard = Card C_0N -- | True <=> lower bound is 1. isStrict :: Card -> Bool -- See Note [Bit vector representation for Card] isStrict :: Card -> Bool isStrict (Card Int c) = Int c Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b001 Int -> Int -> Bool forall a. Eq a => a -> a -> Bool == Int 0 -- simply check 0 bit is not set -- | True <=> upper bound is 0. isAbs :: Card -> Bool -- See Note [Bit vector representation for Card] isAbs :: Card -> Bool isAbs (Card Int c) = Int c Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b110 Int -> Int -> Bool forall a. Eq a => a -> a -> Bool == Int 0 -- simply check 1 and n bit are not set -- | True <=> upper bound is 1. isUsedOnce :: Card -> Bool -- See Note [Bit vector representation for Card] isUsedOnce :: Card -> Bool isUsedOnce (Card Int c) = Int c Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b100 Int -> Int -> Bool forall a. Eq a => a -> a -> Bool == Int 0 -- simply check n bit is not set -- | Is this a 'CardNonAbs'? isCardNonAbs :: Card -> Bool isCardNonAbs :: Card -> Bool isCardNonAbs = Bool -> Bool not (Bool -> Bool) -> (Card -> Bool) -> Card -> Bool forall b c a. (b -> c) -> (a -> b) -> a -> c . Card -> Bool isAbs -- | Is this a 'CardNonOnce'? isCardNonOnce :: Card -> Bool isCardNonOnce :: Card -> Bool isCardNonOnce Card n = Card -> Bool isAbs Card n Bool -> Bool -> Bool || Bool -> Bool not (Card -> Bool isUsedOnce Card n) -- | Intersect with [0,1]. oneifyCard :: Card -> Card oneifyCard :: Card -> Card oneifyCard = Card -> Card -> Card glbCard Card C_01 -- | Intersect with [1,n]. The same as @'plusCard' 'C_10'@. strictifyCard :: Card -> Card strictifyCard :: Card -> Card strictifyCard = Card -> Card -> Card glbCard Card C_1N -- | Denotes '∪' on 'Card'. lubCard :: Card -> Card -> Card -- See Note [Bit vector representation for Card] lubCard :: Card -> Card -> Card lubCard (Card Int a) (Card Int b) = Int -> Card Card (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int b) -- main point of the bit-vector encoding! -- | Denotes '∩' on 'Card'. glbCard :: Card -> Card -> Card -- See Note [Bit vector representation for Card] glbCard :: Card -> Card -> Card glbCard (Card Int a) (Card Int b) = Int -> Card Card (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int b) -- | Denotes '+' on lower and upper bounds of 'Card'. plusCard :: Card -> Card -> Card -- See Note [Algebraic specification for plusCard and multCard] plusCard :: Card -> Card -> Card plusCard (Card Int a) (Card Int b) = Int -> Card Card (Int bit0 Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int bit1 Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int bitN) where bit0 :: Int bit0 = (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b001 bit1 :: Int bit1 = (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b010 bitN :: Int bitN = ((Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int -> Int -> Int forall a. Bits a => a -> Int -> a shiftL (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int b) Int 1) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b100 -- | Denotes '*' on lower and upper bounds of 'Card'. multCard :: Card -> Card -> Card -- See Note [Algebraic specification for plusCard and multCard] multCard :: Card -> Card -> Card multCard (Card Int a) (Card Int b) = Int -> Card Card (Int bit0 Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int bit1 Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int bitN) where bit0 :: Int bit0 = (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b001 bit1 :: Int bit1 = (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b010 bitN :: Int bitN = (Int a Int -> Int -> Int forall a. Bits a => a -> a -> a .|. Int b) Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int -> Int -> Int forall a. Bits a => a -> Int -> a shiftL Int bit1 Int 1 Int -> Int -> Int forall a. Bits a => a -> a -> a .&. Int 0b100 {- ************************************************************************ * * Demand: Evaluation contexts * * ************************************************************************ -} -- | A demand describes -- -- * How many times a variable is evaluated, via a 'Card'inality, and -- * How deep its value was evaluated in turn, via a 'SubDemand'. -- -- Examples (using Note [Demand notation]): -- -- * 'seq' puts demand @1A@ on its first argument: It evaluates the argument -- strictly (@1@), but not any deeper (@A@). -- * 'fst' puts demand @1P(1L,A)@ on its argument: It evaluates the argument -- pair strictly and the first component strictly, but no nested info -- beyond that (@L@). Its second argument is not used at all. -- * '$' puts demand @1C(1,L)@ on its first argument: It calls (@C@) the -- argument function with one argument, exactly once (@1@). No info -- on how the result of that call is evaluated (@L@). -- * 'maybe' puts demand @MC(M,L)@ on its second argument: It evaluates -- the argument function at most once ((M)aybe) and calls it once when -- it is evaluated. -- * @fst p + fst p@ puts demand @SP(SL,A)@ on @p@: It's @1P(1L,A)@ -- multiplied by two, so we get @S@ (used at least once, possibly multiple -- times). -- -- This data type is quite similar to @'Scaled' 'SubDemand'@, but it's scaled -- by 'Card', which is an /interval/ on 'Multiplicity', the upper bound of -- which could be used to infer uniqueness types. Also we treat 'AbsDmd' and -- 'BotDmd' specially, as the concept of a 'SubDemand' doesn't apply when there -- isn't any evaluation at all. If you don't care, simply use '(:*)'. data Demand = BotDmd -- ^ A bottoming demand, produced by a diverging function ('C_10'), hence there is no -- 'SubDemand' that describes how it was evaluated. | AbsDmd -- ^ An absent demand: Evaluated exactly 0 times ('C_00'), hence there is no -- 'SubDemand' that describes how it was evaluated. | D !CardNonAbs !SubDemand -- ^ Don't use this internal data constructor; use '(:*)' instead. -- Since BotDmd deals with 'C_10' and AbsDmd deals with 'C_00', the -- cardinality component is CardNonAbs deriving Demand -> Demand -> Bool (Demand -> Demand -> Bool) -> (Demand -> Demand -> Bool) -> Eq Demand forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: Demand -> Demand -> Bool == :: Demand -> Demand -> Bool $c/= :: Demand -> Demand -> Bool /= :: Demand -> Demand -> Bool Eq -- | Only meant to be used in the pattern synonym below! viewDmdPair :: Demand -> (Card, SubDemand) viewDmdPair :: Demand -> (Card, SubDemand) viewDmdPair Demand BotDmd = (Card C_10, SubDemand botSubDmd) viewDmdPair Demand AbsDmd = (Card C_00, SubDemand botSubDmd) viewDmdPair (D Card n SubDemand sd) = (Card n, SubDemand sd) -- | @c :* sd@ is a demand that says \"evaluated @c@ times, and any trace in -- which it is evaluated will evaluate at least as deep as @sd@\". -- -- Matching on this pattern synonym is a complete match. -- If the matched demand was 'AbsDmd', it will match as @C_00 :* seqSubDmd@. -- If the matched demand was 'BotDmd', it will match as @C_10 :* botSubDmd@. -- The builder of this pattern synonym simply /discards/ the 'SubDemand' if the -- 'Card' was absent and returns 'AbsDmd' or 'BotDmd' instead. It will assert -- that the discarded sub-demand was 'seqSubDmd' and 'botSubDmd', respectively. -- -- Call sites should consider whether they really want to look at the -- 'SubDemand' of an absent demand and match on 'AbsDmd' and/or 'BotDmd' -- otherwise. Really, any other 'SubDemand' would be allowed and -- might work better, depending on context. pattern (:*) :: HasDebugCallStack => Card -> SubDemand -> Demand pattern n $m:* :: forall {r}. HasDebugCallStack => Demand -> (Card -> SubDemand -> r) -> ((# #) -> r) -> r $b:* :: HasDebugCallStack => Card -> SubDemand -> Demand :* sd <- (viewDmdPair -> (n, sd)) where Card C_10 :* SubDemand sd = Demand BotDmd Demand -> (Demand -> Demand) -> Demand forall a b. a -> (a -> b) -> b & Bool -> SDoc -> Demand -> Demand forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (SubDemand sd SubDemand -> SubDemand -> Bool forall a. Eq a => a -> a -> Bool == SubDemand botSubDmd) (String -> SDoc forall doc. IsLine doc => String -> doc text String "B /=" SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <+> SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) Card C_00 :* SubDemand sd = Demand AbsDmd Demand -> (Demand -> Demand) -> Demand forall a b. a -> (a -> b) -> b & Bool -> SDoc -> Demand -> Demand forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (SubDemand sd SubDemand -> SubDemand -> Bool forall a. Eq a => a -> a -> Bool == SubDemand botSubDmd) (String -> SDoc forall doc. IsLine doc => String -> doc text String "A /=" SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <+> SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) Card n :* SubDemand sd = Card -> SubDemand -> Demand D Card n SubDemand sd Demand -> (Demand -> Demand) -> Demand forall a b. a -> (a -> b) -> b & Bool -> SDoc -> Demand -> Demand forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (Card -> Bool isCardNonAbs Card n) (Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n SDoc -> SDoc -> SDoc forall doc. IsDoc doc => doc -> doc -> doc $$ SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) {-# COMPLETE (:*) #-} -- | A sub-demand describes an /evaluation context/ (in the sense of an -- operational semantics), e.g. how deep the denoted thing is going to be -- evaluated. See 'Demand' for examples. -- -- See Note [SubDemand denotes at least one evaluation] for a more detailed -- description of what a sub-demand means. -- -- See Note [Demand notation] for the extensively used short-hand notation. -- See also Note [Why Boxity in SubDemand and not in Demand?]. data SubDemand = Poly !Boxity !CardNonOnce -- ^ Polymorphic demand, the denoted thing is evaluated arbitrarily deep, -- with the specified cardinality at every level. The 'Boxity' applies only -- to the outer evaluation context as well as all inner evaluation context. -- See Note [Boxity in Poly] for why we want it to carry 'Boxity'. -- Expands to 'Call' via 'viewCall' and to 'Prod' via 'viewProd'. -- -- @Poly b n@ is semantically equivalent to @Prod b [n :* Poly b n, ...] -- or @Call n (Poly Boxed n)@. 'viewCall' and 'viewProd' do these rewrites. -- -- In Note [Demand notation]: @L === P(L,L,...)@ and @L === C(L)@, -- @B === P(B,B,...)@ and @B === C(B)@, -- @!A === !P(A,A,...)@ and @!A === C(A)@, -- and so on. -- -- We'll only see 'Poly' with 'C_10' (B), 'C_00' (A), 'C_0N' (L) and sometimes -- 'C_1N' (S) through 'plusSubDmd', never 'C_01' (M) or 'C_11' (1) (grep the -- source code). Hence 'CardNonOnce', which is closed under 'lub' and 'plus'. -- -- Why doesn't this constructor simply carry a 'Demand' instead of its fields? -- See Note [Call SubDemand vs. evaluation Demand]. | Call !CardNonAbs !SubDemand -- ^ @Call n sd@ describes the evaluation context of @n@ function -- applications (with one argument), where the result of each call is -- evaluated according to @sd@. -- @sd@ describes program traces in which the denoted thing was called at all, -- see Note [SubDemand denotes at least one evaluation]. -- That Note also explains why it doesn't make sense for @n@ to be absent, -- hence we forbid it with 'CardNonAbs'. Absent call demands can still be -- expressed with 'Poly'. -- Used only for values of function type. Use the smart constructor 'mkCall' -- whenever possible! | Prod !Boxity ![Demand] -- ^ @Prod b ds@ describes the evaluation context of a case scrutinisation -- on an expression of product type, where the product components are -- evaluated according to @ds@. The 'Boxity' @b@ says whether or not the box -- of the product was used. -- | We have to respect Poly rewrites through 'viewCall' and 'viewProd'. instance Eq SubDemand where SubDemand d1 == :: SubDemand -> SubDemand -> Bool == SubDemand d2 = case SubDemand d1 of Prod Boxity b1 [Demand] ds1 | Just (Boxity b2, [Demand] ds2) <- Int -> SubDemand -> Maybe (Boxity, [Demand]) viewProd ([Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] ds1) SubDemand d2 -> Boxity b1 Boxity -> Boxity -> Bool forall a. Eq a => a -> a -> Bool == Boxity b2 Bool -> Bool -> Bool && [Demand] ds1 [Demand] -> [Demand] -> Bool forall a. Eq a => a -> a -> Bool == [Demand] ds2 Call Card n1 SubDemand sd1 | Just (Card n2, SubDemand sd2) <- SubDemand -> Maybe (Card, SubDemand) viewCall SubDemand d2 -> Card n1 Card -> Card -> Bool forall a. Eq a => a -> a -> Bool == Card n2 Bool -> Bool -> Bool && SubDemand sd1 SubDemand -> SubDemand -> Bool forall a. Eq a => a -> a -> Bool == SubDemand sd2 Poly Boxity b1 Card n1 | Poly Boxity b2 Card n2 <- SubDemand d2 -> Boxity b1 Boxity -> Boxity -> Bool forall a. Eq a => a -> a -> Bool == Boxity b2 Bool -> Bool -> Bool && Card n1 Card -> Card -> Bool forall a. Eq a => a -> a -> Bool == Card n2 SubDemand _ -> Bool False topSubDmd, botSubDmd, seqSubDmd :: SubDemand topSubDmd :: SubDemand topSubDmd = Boxity -> Card -> SubDemand Poly Boxity Boxed Card C_0N botSubDmd :: SubDemand botSubDmd = Boxity -> Card -> SubDemand Poly Boxity Unboxed Card C_10 seqSubDmd :: SubDemand seqSubDmd = Boxity -> Card -> SubDemand Poly Boxity Unboxed Card C_00 -- | The uniform field demand when viewing a 'Poly' as a 'Prod', as in -- 'viewProd'. polyFieldDmd :: Boxity -> CardNonOnce -> Demand polyFieldDmd :: Boxity -> Card -> Demand polyFieldDmd Boxity _ Card C_00 = Demand AbsDmd polyFieldDmd Boxity _ Card C_10 = Demand BotDmd polyFieldDmd Boxity Boxed Card C_0N = Demand topDmd polyFieldDmd Boxity b Card n = Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> Card -> SubDemand Poly Boxity b Card n Demand -> (Demand -> Demand) -> Demand forall a b. a -> (a -> b) -> b & Bool -> SDoc -> Demand -> Demand forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (Card -> Bool isCardNonOnce Card n) (Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n) -- | A smart constructor for 'Prod', applying rewrite rules along the semantic -- equality @Prod b [n :* Poly Boxed n, ...] === Poly b n@, simplifying to -- 'Poly' 'SubDemand's when possible. Examples: -- -- * Rewrites @P(L,L)@ (e.g., arguments @Boxed@, @[L,L]@) to @L@ -- * Rewrites @!P(L!L,L!L)@ (e.g., arguments @Unboxed@, @[L!L,L!L]@) to @!L@ -- * Does not rewrite @P(1L)@, @P(L!L)@, @!P(L)@ or @P(L,A)@ -- mkProd :: Boxity -> [Demand] -> SubDemand mkProd :: Boxity -> [Demand] -> SubDemand mkProd Boxity b [Demand] ds | (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all (Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand AbsDmd) [Demand] ds = Boxity -> Card -> SubDemand Poly Boxity b Card C_00 | (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all (Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand BotDmd) [Demand] ds = Boxity -> Card -> SubDemand Poly Boxity b Card C_10 | dmd :: Demand dmd@(Card n :* Poly Boxity b2 Card m):[Demand] _ <- [Demand] ds , Card n Card -> Card -> Bool forall a. Eq a => a -> a -> Bool == Card m -- don't rewrite P(SL) to S , Boxity b Boxity -> Boxity -> Bool forall a. Eq a => a -> a -> Bool == Boxity b2 -- don't rewrite P(S!S) to !S , (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all (Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand dmd) [Demand] ds -- don't rewrite P(L,A) to L = Boxity -> Card -> SubDemand Poly Boxity b Card n | Bool otherwise = Boxity -> [Demand] -> SubDemand Prod Boxity b [Demand] ds -- | @viewProd n sd@ interprets @sd@ as a 'Prod' of arity @n@, expanding 'Poly' -- demands as necessary. viewProd :: Arity -> SubDemand -> Maybe (Boxity, [Demand]) -- It's quite important that this function is optimised well; -- it is used by lubSubDmd and plusSubDmd. viewProd :: Int -> SubDemand -> Maybe (Boxity, [Demand]) viewProd Int n (Prod Boxity b [Demand] ds) | [Demand] ds [Demand] -> Int -> Bool forall a. [a] -> Int -> Bool `lengthIs` Int n = (Boxity, [Demand]) -> Maybe (Boxity, [Demand]) forall a. a -> Maybe a Just (Boxity b, [Demand] ds) -- Note the strict application to replicate: This makes sure we don't allocate -- a thunk for it, inlines it and lets case-of-case fire at call sites. viewProd Int n (Poly Boxity b Card card) | let !ds :: [Demand] ds = Int -> Demand -> [Demand] forall a. Int -> a -> [a] replicate Int n (Demand -> [Demand]) -> Demand -> [Demand] forall a b. (a -> b) -> a -> b $! Boxity -> Card -> Demand polyFieldDmd Boxity b Card card = (Boxity, [Demand]) -> Maybe (Boxity, [Demand]) forall a. a -> Maybe a Just (Boxity b, [Demand] ds) viewProd Int _ SubDemand _ = Maybe (Boxity, [Demand]) forall a. Maybe a Nothing {-# INLINE viewProd #-} -- we want to fuse away the replicate and the allocation -- for Arity. Otherwise, #18304 bites us. -- | A smart constructor for 'Call', applying rewrite rules along the semantic -- equality @Call C_0N (Poly C_0N) === Poly C_0N@, simplifying to 'Poly' 'SubDemand's -- when possible. mkCall :: CardNonAbs -> SubDemand -> SubDemand --mkCall C_1N sd@(Poly Boxed C_1N) = sd -- NO! #21085 strikes. See Note [mkCall and plusSubDmd] mkCall :: Card -> SubDemand -> SubDemand mkCall Card C_0N sd :: SubDemand sd@(Poly Boxity Boxed Card C_0N) = SubDemand sd mkCall Card n SubDemand sd = Bool -> SDoc -> SubDemand -> SubDemand forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (Card -> Bool isCardNonAbs Card n) (Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n SDoc -> SDoc -> SDoc forall doc. IsDoc doc => doc -> doc -> doc $$ SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) (SubDemand -> SubDemand) -> SubDemand -> SubDemand forall a b. (a -> b) -> a -> b $ Card -> SubDemand -> SubDemand Call Card n SubDemand sd -- | @viewCall sd@ interprets @sd@ as a 'Call', expanding 'Poly' subdemands as -- necessary. viewCall :: SubDemand -> Maybe (Card, SubDemand) viewCall :: SubDemand -> Maybe (Card, SubDemand) viewCall (Call Card n SubDemand sd) = (Card, SubDemand) -> Maybe (Card, SubDemand) forall a. a -> Maybe a Just (Card n :: Card, SubDemand sd) viewCall (Poly Boxity _ Card n) | Card -> Bool isAbs Card n = (Card, SubDemand) -> Maybe (Card, SubDemand) forall a. a -> Maybe a Just (Card n :: Card, SubDemand botSubDmd) | Bool otherwise = (Card, SubDemand) -> Maybe (Card, SubDemand) forall a. a -> Maybe a Just (Card n :: Card, Boxity -> Card -> SubDemand Poly Boxity Boxed Card n) viewCall SubDemand _ = Maybe (Card, SubDemand) forall a. Maybe a Nothing topDmd, absDmd, botDmd, seqDmd :: Demand topDmd :: Demand topDmd = Card C_0N HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand topSubDmd absDmd :: Demand absDmd = Demand AbsDmd botDmd :: Demand botDmd = Demand BotDmd seqDmd :: Demand seqDmd = Card C_11 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand seqSubDmd -- | Sets 'Boxity' to 'Unboxed' for non-'Call' sub-demands and recurses into 'Prod'. unboxDeeplySubDmd :: SubDemand -> SubDemand unboxDeeplySubDmd :: SubDemand -> SubDemand unboxDeeplySubDmd (Poly Boxity _ Card n) = Boxity -> Card -> SubDemand Poly Boxity Unboxed Card n unboxDeeplySubDmd (Prod Boxity _ [Demand] ds) = Boxity -> [Demand] -> SubDemand mkProd Boxity Unboxed ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] strictMap Demand -> Demand unboxDeeplyDmd [Demand] ds) unboxDeeplySubDmd call :: SubDemand call@Call{} = SubDemand call -- | Sets 'Boxity' to 'Unboxed' for the 'Demand', recursing into 'Prod's. -- Don't recurse into lazy arguments; see GHC.Core.Opt.DmdAnal -- Note [No lazy, Unboxed demands in demand signature] unboxDeeplyDmd :: Demand -> Demand unboxDeeplyDmd :: Demand -> Demand unboxDeeplyDmd Demand AbsDmd = Demand AbsDmd unboxDeeplyDmd Demand BotDmd = Demand BotDmd unboxDeeplyDmd dmd :: Demand dmd@(D Card n SubDemand sd) | Card -> Bool isStrict Card n = Card -> SubDemand -> Demand D Card n (SubDemand -> SubDemand unboxDeeplySubDmd SubDemand sd) | Bool otherwise = Demand dmd multDmd :: Card -> Demand -> Demand multDmd :: Card -> Demand -> Demand multDmd Card C_11 Demand dmd = Demand dmd -- An optimisation -- The following four lines make sure that we rewrite to AbsDmd and BotDmd -- whenever the leading cardinality is absent (C_00 or C_10). -- Otherwise it may happen that the SubDemand is not 'botSubDmd', triggering -- the assertion in `:*`. -- Example: `multDmd B 1L = BA`, so with an inner `seqSubDmd`. Our lattice -- allows us to always rewrite this to proper BotDmd and we maintain the -- invariant that this is indeed the case. multDmd Card C_00 Demand _ = Demand AbsDmd multDmd Card _ Demand AbsDmd = Demand AbsDmd multDmd Card C_10 (D Card n SubDemand _) = if Card -> Bool isStrict Card n then Demand BotDmd else Demand AbsDmd multDmd Card n Demand BotDmd = if Card -> Bool isStrict Card n then Demand BotDmd else Demand AbsDmd -- See Note [SubDemand denotes at least one evaluation] for the strictifyCard multDmd Card n (D Card m SubDemand sd) = Card -> Card -> Card multCard Card n Card m HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Card -> SubDemand -> SubDemand multSubDmd (Card -> Card strictifyCard Card n) SubDemand sd multSubDmd :: Card -> SubDemand -> SubDemand multSubDmd :: Card -> SubDemand -> SubDemand multSubDmd Card C_11 SubDemand sd = SubDemand sd -- An optimisation, for when sd is a deep Prod -- The following three equations don't have an impact on Demands, only on -- Boxity. They are needed so that we don't trigger the assertions in `:*` -- when called from `multDmd`. multSubDmd Card C_00 SubDemand _ = SubDemand seqSubDmd -- Otherwise `multSubDmd A L == A /= !A` multSubDmd Card C_10 (Poly Boxity _ Card n) = if Card -> Bool isStrict Card n then SubDemand botSubDmd else SubDemand seqSubDmd -- Otherwise `multSubDmd B L == B /= !B` multSubDmd Card C_10 (Call Card n SubDemand _) = if Card -> Bool isStrict Card n then SubDemand botSubDmd else SubDemand seqSubDmd -- Otherwise we'd call `mkCall` with absent cardinality multSubDmd Card n (Poly Boxity b Card m) = Boxity -> Card -> SubDemand Poly Boxity b (Card -> Card -> Card multCard Card n Card m) multSubDmd Card n (Call Card n' SubDemand sd) = Card -> SubDemand -> SubDemand mkCall (Card -> Card -> Card multCard Card n Card n') SubDemand sd multSubDmd Card n (Prod Boxity b [Demand] ds) = Boxity -> [Demand] -> SubDemand mkProd Boxity b ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] strictMap (Card -> Demand -> Demand multDmd Card n) [Demand] ds) lazifyIfStrict :: Card -> SubDemand -> SubDemand lazifyIfStrict :: Card -> SubDemand -> SubDemand lazifyIfStrict Card n SubDemand sd = Card -> SubDemand -> SubDemand multSubDmd (Card -> Card -> Card glbCard Card C_01 Card n) SubDemand sd -- | Denotes '∪' on 'Demand'. lubDmd :: Demand -> Demand -> Demand lubDmd :: Demand -> Demand -> Demand lubDmd Demand BotDmd Demand dmd2 = Demand dmd2 lubDmd Demand dmd1 Demand BotDmd = Demand dmd1 lubDmd (Card n1 :* SubDemand sd1) (Card n2 :* SubDemand sd2) = -- pprTraceWith "lubDmd" (\it -> ppr (n1:*sd1) $$ ppr (n2:*sd2) $$ ppr it) $ Card -> Card -> Card lubCard Card n1 Card n2 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand -> SubDemand -> SubDemand lubSubDmd SubDemand sd1 SubDemand sd2 lubSubDmd :: SubDemand -> SubDemand -> SubDemand -- Shortcuts for neutral and absorbing elements. -- Below we assume that Boxed always wins. lubSubDmd :: SubDemand -> SubDemand -> SubDemand lubSubDmd (Poly Boxity Unboxed Card C_10) SubDemand sd = SubDemand sd lubSubDmd SubDemand sd (Poly Boxity Unboxed Card C_10) = SubDemand sd lubSubDmd sd :: SubDemand sd@(Poly Boxity Boxed Card C_0N) SubDemand _ = SubDemand sd lubSubDmd SubDemand _ sd :: SubDemand sd@(Poly Boxity Boxed Card C_0N) = SubDemand sd -- Handle Prod lubSubDmd (Prod Boxity b1 [Demand] ds1) (Poly Boxity b2 Card n2) | let !d :: Demand d = Boxity -> Card -> Demand polyFieldDmd Boxity b2 Card n2 = Boxity -> [Demand] -> SubDemand mkProd (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] strictMap (Demand -> Demand -> Demand lubDmd Demand d) [Demand] ds1) lubSubDmd (Prod Boxity b1 [Demand] ds1) (Prod Boxity b2 [Demand] ds2) | [Demand] -> [Demand] -> Bool forall a b. [a] -> [b] -> Bool equalLength [Demand] ds1 [Demand] ds2 = Boxity -> [Demand] -> SubDemand mkProd (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) ((Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] strictZipWith Demand -> Demand -> Demand lubDmd [Demand] ds1 [Demand] ds2) -- Handle Call lubSubDmd (Call Card n1 SubDemand sd1) (SubDemand -> Maybe (Card, SubDemand) viewCall -> Just (Card n2, SubDemand sd2)) = Card -> SubDemand -> SubDemand mkCall (Card -> Card -> Card lubCard Card n1 Card n2) (SubDemand -> SubDemand -> SubDemand lubSubDmd SubDemand sd1 SubDemand sd2) -- Handle Poly lubSubDmd (Poly Boxity b1 Card n1) (Poly Boxity b2 Card n2) = Boxity -> Card -> SubDemand Poly (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) (Card -> Card -> Card lubCard Card n1 Card n2) -- Other Poly case by commutativity lubSubDmd sd1 :: SubDemand sd1@Poly{} SubDemand sd2 = SubDemand -> SubDemand -> SubDemand lubSubDmd SubDemand sd2 SubDemand sd1 -- Otherwise (Call `lub` Prod) return Top lubSubDmd SubDemand _ SubDemand _ = SubDemand topSubDmd -- | Denotes '+' on 'Demand'. plusDmd :: Demand -> Demand -> Demand plusDmd :: Demand -> Demand -> Demand plusDmd Demand AbsDmd Demand dmd2 = Demand dmd2 plusDmd Demand dmd1 Demand AbsDmd = Demand dmd1 plusDmd (Card n1 :* SubDemand sd1) (Card n2 :* SubDemand sd2) = -- pprTraceWith "plusDmd" (\it -> ppr (n1:*sd1) $$ ppr (n2:*sd2) $$ ppr it) $ -- Why lazify? See Note [SubDemand denotes at least one evaluation] -- and also Note [Unrealised opportunity in plusDmd] which applies when both -- n1 and n2 are lazy already Card -> Card -> Card plusCard Card n1 Card n2 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand -> SubDemand -> SubDemand plusSubDmd (Card -> SubDemand -> SubDemand lazifyIfStrict Card n1 SubDemand sd1) (Card -> SubDemand -> SubDemand lazifyIfStrict Card n2 SubDemand sd2) plusSubDmd :: SubDemand -> SubDemand -> SubDemand -- Shortcuts for neutral and absorbing elements. -- Below we assume that Boxed always wins. plusSubDmd :: SubDemand -> SubDemand -> SubDemand plusSubDmd (Poly Boxity Unboxed Card C_00) SubDemand sd = SubDemand sd plusSubDmd SubDemand sd (Poly Boxity Unboxed Card C_00) = SubDemand sd plusSubDmd sd :: SubDemand sd@(Poly Boxity Boxed Card C_1N) SubDemand _ = SubDemand sd plusSubDmd SubDemand _ sd :: SubDemand sd@(Poly Boxity Boxed Card C_1N) = SubDemand sd -- Handle Prod plusSubDmd (Prod Boxity b1 [Demand] ds1) (Poly Boxity b2 Card n2) | let !d :: Demand d = Boxity -> Card -> Demand polyFieldDmd Boxity b2 Card n2 = Boxity -> [Demand] -> SubDemand mkProd (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] strictMap (Demand -> Demand -> Demand plusDmd Demand d) [Demand] ds1) plusSubDmd (Prod Boxity b1 [Demand] ds1) (Prod Boxity b2 [Demand] ds2) | [Demand] -> [Demand] -> Bool forall a b. [a] -> [b] -> Bool equalLength [Demand] ds1 [Demand] ds2 = Boxity -> [Demand] -> SubDemand mkProd (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) ((Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] strictZipWith Demand -> Demand -> Demand plusDmd [Demand] ds1 [Demand] ds2) -- Handle Call plusSubDmd (Call Card n1 SubDemand sd1) (SubDemand -> Maybe (Card, SubDemand) viewCall -> Just (Card n2, SubDemand sd2)) = Card -> SubDemand -> SubDemand mkCall (Card -> Card -> Card plusCard Card n1 Card n2) (SubDemand -> SubDemand -> SubDemand lubSubDmd SubDemand sd1 SubDemand sd2) -- Handle Poly plusSubDmd (Poly Boxity b1 Card n1) (Poly Boxity b2 Card n2) = Boxity -> Card -> SubDemand Poly (Boxity -> Boxity -> Boxity lubBoxity Boxity b1 Boxity b2) (Card -> Card -> Card plusCard Card n1 Card n2) -- Other Poly case by commutativity plusSubDmd sd1 :: SubDemand sd1@Poly{} SubDemand sd2 = SubDemand -> SubDemand -> SubDemand plusSubDmd SubDemand sd2 SubDemand sd1 -- Otherwise (Call `plus` Prod) return Top plusSubDmd SubDemand _ SubDemand _ = SubDemand topSubDmd -- | Used to suppress pretty-printing of an uninformative demand isTopDmd :: Demand -> Bool isTopDmd :: Demand -> Bool isTopDmd Demand dmd = Demand dmd Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand topDmd isAbsDmd :: Demand -> Bool isAbsDmd :: Demand -> Bool isAbsDmd (Card n :* SubDemand _) = Card -> Bool isAbs Card n -- | Contrast with isStrictUsedDmd. See Note [Strict demands] isStrictDmd :: Demand -> Bool isStrictDmd :: Demand -> Bool isStrictDmd (Card n :* SubDemand _) = Card -> Bool isStrict Card n -- | Not absent and used strictly. See Note [Strict demands] isStrUsedDmd :: Demand -> Bool isStrUsedDmd :: Demand -> Bool isStrUsedDmd (Card n :* SubDemand _) = Card -> Bool isStrict Card n Bool -> Bool -> Bool && Bool -> Bool not (Card -> Bool isAbs Card n) -- | Is the value used at most once? isUsedOnceDmd :: Demand -> Bool isUsedOnceDmd :: Demand -> Bool isUsedOnceDmd (Card n :* SubDemand _) = Card -> Bool isUsedOnce Card n -- | We try to avoid tracking weak free variable demands in strictness -- signatures for analysis performance reasons. -- See Note [Lazy and unleashable free variables] in "GHC.Core.Opt.DmdAnal". isWeakDmd :: Demand -> Bool isWeakDmd :: Demand -> Bool isWeakDmd dmd :: Demand dmd@(Card n :* SubDemand _) = Bool -> Bool not (Card -> Bool isStrict Card n) Bool -> Bool -> Bool && Demand -> Bool is_plus_idem_dmd Demand dmd where -- @is_plus_idem_* thing@ checks whether @thing `plus` thing = thing@, -- e.g. if @thing@ is idempotent wrt. to @plus@. -- is_plus_idem_card n = plusCard n n == n is_plus_idem_card :: Card -> Bool is_plus_idem_card = Card -> Bool isCardNonOnce -- is_plus_idem_dmd dmd = plusDmd dmd dmd == dmd is_plus_idem_dmd :: Demand -> Bool is_plus_idem_dmd Demand AbsDmd = Bool True is_plus_idem_dmd Demand BotDmd = Bool True is_plus_idem_dmd (Card n :* SubDemand sd) = Card -> Bool is_plus_idem_card Card n Bool -> Bool -> Bool && SubDemand -> Bool is_plus_idem_sub_dmd SubDemand sd -- is_plus_idem_sub_dmd sd = plusSubDmd sd sd == sd is_plus_idem_sub_dmd :: SubDemand -> Bool is_plus_idem_sub_dmd (Poly Boxity _ Card n) = Bool -> Bool -> Bool forall a. HasCallStack => Bool -> a -> a assert (Card -> Bool isCardNonOnce Card n) Bool True is_plus_idem_sub_dmd (Prod Boxity _ [Demand] ds) = (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all Demand -> Bool is_plus_idem_dmd [Demand] ds is_plus_idem_sub_dmd (Call Card n SubDemand _) = Card -> Bool is_plus_idem_card Card n evalDmd :: Demand evalDmd :: Demand evalDmd = Card C_1N HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand topSubDmd -- | First argument of 'GHC.Exts.maskAsyncExceptions#': @1C(1,L)@. -- Called exactly once. strictOnceApply1Dmd :: Demand strictOnceApply1Dmd :: Demand strictOnceApply1Dmd = Card C_11 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Card -> SubDemand -> SubDemand mkCall Card C_11 SubDemand topSubDmd -- | First argument of 'GHC.Exts.atomically#': @SC(S,L)@. -- Called at least once, possibly many times. strictManyApply1Dmd :: Demand strictManyApply1Dmd :: Demand strictManyApply1Dmd = Card C_1N HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Card -> SubDemand -> SubDemand mkCall Card C_1N SubDemand topSubDmd -- | First argument of catch#: @MC(M,L)@. -- Evaluates its arg lazily, but then applies it exactly once to one argument. lazyApply1Dmd :: Demand lazyApply1Dmd :: Demand lazyApply1Dmd = Card C_01 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Card -> SubDemand -> SubDemand mkCall Card C_01 SubDemand topSubDmd -- | Second argument of catch#: @MC(M,C(1,L))@. -- Calls its arg lazily, but then applies it exactly once to an additional argument. lazyApply2Dmd :: Demand lazyApply2Dmd :: Demand lazyApply2Dmd = Card C_01 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Card -> SubDemand -> SubDemand mkCall Card C_01 (Card -> SubDemand -> SubDemand mkCall Card C_11 SubDemand topSubDmd) -- | Make a 'Demand' evaluated at-most-once. oneifyDmd :: Demand -> Demand oneifyDmd :: Demand -> Demand oneifyDmd Demand AbsDmd = Demand AbsDmd oneifyDmd Demand BotDmd = Demand BotDmd oneifyDmd (Card n :* SubDemand sd) = Card -> Card oneifyCard Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand sd -- | Make a 'Demand' evaluated at-least-once (e.g. strict). strictifyDmd :: Demand -> Demand strictifyDmd :: Demand -> Demand strictifyDmd = Demand -> Demand -> Demand plusDmd Demand seqDmd -- | 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 (Card n :* Prod Boxity b [Demand] ds) | Bool -> Bool not (Card -> Bool isAbs Card n) , Just [Type] field_tys <- Type -> Maybe [Type] as_non_newtype_dict Type ty = Card C_1N HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> [Demand] -> SubDemand mkProd Boxity b ((Type -> Demand -> Demand) -> [Type] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] zipWith Type -> Demand -> Demand strictifyDictDmd [Type] field_tys [Demand] ds) -- main idea: ensure it's strict where -- Return a TyCon and a list of field types if the given -- type is a non-newtype dictionary type as_non_newtype_dict :: Type -> Maybe [Type] as_non_newtype_dict Type ty | Just (TyCon tycon, [Type] _arg_tys, DataCon _data_con, (Scaled Type -> Type) -> [Scaled Type] -> [Type] forall a b. (a -> b) -> [a] -> [b] map Scaled Type -> Type forall a. Scaled a -> a scaledThing -> [Type] inst_con_arg_tys) <- Type -> Maybe (TyCon, [Type], DataCon, [Scaled Type]) splitDataProductType_maybe Type ty , Bool -> Bool not (TyCon -> Bool isNewTyCon TyCon tycon) , TyCon -> Bool isClassTyCon TyCon tycon = [Type] -> Maybe [Type] forall a. a -> Maybe a Just [Type] inst_con_arg_tys | Bool otherwise = Maybe [Type] forall a. Maybe a Nothing strictifyDictDmd Type _ Demand dmd = Demand dmd -- | Make a 'Demand' lazy. lazifyDmd :: Demand -> Demand lazifyDmd :: Demand -> Demand lazifyDmd = Card -> Demand -> Demand multDmd Card C_01 -- | Wraps the 'SubDemand' with a one-shot call demand: @d@ -> @C(1,d)@. mkCalledOnceDmd :: SubDemand -> SubDemand mkCalledOnceDmd :: SubDemand -> SubDemand mkCalledOnceDmd SubDemand sd = Card -> SubDemand -> SubDemand mkCall Card C_11 SubDemand sd -- | @mkCalledOnceDmds n d@ returns @C(1,C1...C(1,d))@ where there are @n@ @C1@'s. mkCalledOnceDmds :: Arity -> SubDemand -> SubDemand mkCalledOnceDmds :: Int -> SubDemand -> SubDemand mkCalledOnceDmds Int arity SubDemand sd = (SubDemand -> SubDemand) -> SubDemand -> [SubDemand] forall a. (a -> a) -> a -> [a] iterate SubDemand -> SubDemand mkCalledOnceDmd SubDemand sd [SubDemand] -> Int -> SubDemand forall a. HasCallStack => [a] -> Int -> a !! Int arity -- | Peels one call level from the sub-demand, and also returns how many -- times we entered the lambda body. peelCallDmd :: SubDemand -> (Card, SubDemand) peelCallDmd :: SubDemand -> (Card, SubDemand) peelCallDmd SubDemand sd = SubDemand -> Maybe (Card, SubDemand) viewCall SubDemand sd Maybe (Card, SubDemand) -> (Card, SubDemand) -> (Card, SubDemand) forall a. Maybe a -> a -> a `orElse` (Card topCard, SubDemand topSubDmd) -- Peels multiple nestings of 'Call' sub-demands and also returns -- whether it was unsaturated in the form of a 'Card'inality, denoting -- how many times the lambda body was entered. -- See Note [Demands from unsaturated function calls]. peelManyCalls :: Arity -> SubDemand -> (Card, SubDemand) peelManyCalls :: Int -> SubDemand -> (Card, SubDemand) peelManyCalls Int k SubDemand sd = Int -> Card -> SubDemand -> (Card, SubDemand) forall {t}. (Eq t, Num t) => t -> Card -> SubDemand -> (Card, SubDemand) go Int k Card C_11 SubDemand sd where go :: t -> Card -> SubDemand -> (Card, SubDemand) go t 0 !Card n !SubDemand sd = (Card n, SubDemand sd) go t k !Card n (SubDemand -> Maybe (Card, SubDemand) viewCall -> Just (Card m, SubDemand sd)) = t -> Card -> SubDemand -> (Card, SubDemand) go (t kt -> t -> t forall a. Num a => a -> a -> a -t 1) (Card n Card -> Card -> Card `multCard` Card m) SubDemand sd go t _ Card _ SubDemand _ = (Card topCard, SubDemand topSubDmd) {-# INLINE peelManyCalls #-} -- so that the pair cancels away in a `fst _` context -- | Extract the 'SubDemand' of a 'Demand'. -- PRECONDITION: The SubDemand must be used in a context where the expression -- denoted by the Demand is under evaluation. subDemandIfEvaluated :: Demand -> SubDemand subDemandIfEvaluated :: Demand -> SubDemand subDemandIfEvaluated (Card _ :* SubDemand sd) = SubDemand sd -- See Note [Demand on the worker] in GHC.Core.Opt.WorkWrap mkWorkerDemand :: Int -> Demand mkWorkerDemand :: Int -> Demand mkWorkerDemand Int n = Card C_01 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Int -> SubDemand forall {t}. (Eq t, Num t) => t -> SubDemand go Int n where go :: t -> SubDemand go t 0 = SubDemand topSubDmd go t n = Card -> SubDemand -> SubDemand mkCall Card C_01 (SubDemand -> SubDemand) -> SubDemand -> SubDemand forall a b. (a -> b) -> a -> b $ t -> SubDemand go (t nt -> t -> t forall a. Num a => a -> a -> a -t 1) argsOneShots :: DmdSig -> Arity -> [[OneShotInfo]] -- ^ See Note [Computing one-shot info] argsOneShots :: DmdSig -> Int -> [[OneShotInfo]] argsOneShots (DmdSig (DmdType DmdEnv _ [Demand] arg_ds)) 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 argOneShots :: Demand -- ^ depending on saturation -> [OneShotInfo] -- ^ See Note [Computing one-shot info] argOneShots :: Demand -> [OneShotInfo] argOneShots Demand AbsDmd = [] -- This defn conflicts with 'saturatedByOneShots', argOneShots Demand BotDmd = [] -- according to which we should return -- @repeat OneShotLam@ here... argOneShots (Card _ :* SubDemand sd) = SubDemand -> [OneShotInfo] go SubDemand sd where go :: SubDemand -> [OneShotInfo] go (Call Card n SubDemand sd) | Card -> Bool isUsedOnce Card n = OneShotInfo OneShotLam OneShotInfo -> [OneShotInfo] -> [OneShotInfo] forall a. a -> [a] -> [a] : SubDemand -> [OneShotInfo] go SubDemand sd | Bool otherwise = OneShotInfo NoOneShotInfo OneShotInfo -> [OneShotInfo] -> [OneShotInfo] forall a. a -> [a] -> [a] : SubDemand -> [OneShotInfo] go SubDemand sd go SubDemand _ = [] -- | -- @saturatedByOneShots n C(M,C(M,...)) = True@ -- <=> -- There are at least n nested C(M,..) calls. -- See Note [Demand on the worker] in GHC.Core.Opt.WorkWrap saturatedByOneShots :: Int -> Demand -> Bool saturatedByOneShots :: Int -> Demand -> Bool saturatedByOneShots Int _ Demand AbsDmd = Bool True saturatedByOneShots Int _ Demand BotDmd = Bool True saturatedByOneShots Int n (Card _ :* SubDemand sd) = Card -> Bool isUsedOnce (Card -> Bool) -> Card -> Bool forall a b. (a -> b) -> a -> b $ (Card, SubDemand) -> Card forall a b. (a, b) -> a fst ((Card, SubDemand) -> Card) -> (Card, SubDemand) -> Card forall a b. (a -> b) -> a -> b $ Int -> SubDemand -> (Card, SubDemand) peelManyCalls Int n SubDemand sd {- Note [Strict demands] ~~~~~~~~~~~~~~~~~~~~~~~~ 'isStrUsedDmd' returns true only of demands that are both strict and used In particular, it is False for <B> (i.e. strict and not used, cardinality C_10), which can and does arise in, say (#7319) f x = raise# <some exception> Then 'x' is not used, so f gets strictness <B> -> . Now the w/w generates fx = let x <B> = 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 #7319 we get T7319.exe: Oops! Entered absent arg w_s1Hd{v} [lid] [base:GHC.Base.String{tc 36u}] Note [SubDemand denotes at least one evaluation] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider a demand `n :* sd` on a binding `let x = e in <body>`. (Similarly, a call sub-demand `Cn(sd)` on a lambda `\_. e`). While `n` describes how *often* `x` had been evaluated in <body>, the sub-demand `sd` describes how *deep* `e` has been evaluated, under the following PREMISE: *for all program traces where `x` had been evaluated at all* That is, `sd` disregards all program traces where `x` had not been evaluated, because it can't describe the depth of an evaluation that never happened. NB: The Premise only makes a difference for lower bounds/strictness. Upper bounds/usage are unaffected by adding or leaving out evaluations that never happen. The Premise comes into play when we have lazy Demands. For example, if `x` was demanded with `LP(SL,A)`, so perhaps the full expression was let x = (e1, e2) in (x `seq` fun y `seq` case x of (a,b) -> a, True) then `x` will be evaluated lazily, but in any trace in which `x` is evaluated, the pair in its RHS will ultimately be evaluated deeply with sub-demand `P(SL,A)`. That means that `e1` is ultimately evaluated strictly, even though evaluation of the field does not directly follow the eval of `x` due to the intermittent call `fun y`. How does the additional strictness help? The long version is the list of examples at the end of this Note (as procured in #21081 and #18903). The short version is * We get to take advantage of call-by-value/let-to-case in more situations, as for e1 above. See example "More let-to-case" below. * Note [Eta reduction based on evaluation context] applies in more situations. See example "More eta reduction" below. * We get to unbox more results, see example "More CPR" below. It seems like we don't give up anything in return. Indeed that is the case: * If we dropped the Premise, then a lazy `n` in `nP(m..)` would always force `m` to be lazy, too. That is quite redundant! It seems wasteful not to use the lower bound of `m` for something more useful. So indeed we give up on nothing in return for some nice wins. * Even if `n` is absent (so the Premise does hold for no trace whatsoever), it's pretty easy to describe how `e` was evaluated. Answer: 'botSubDmd'. We use it when expanding 'Absent' and 'Bottom' demands in 'viewDmdPair' as well as when expanding absent 'Poly's to 'Call' sub-demands in 'viewCall'. Of course, we now have to maintain the Premise when we unpack and rebuild Demands. For strict demands, we know that the Premise indeed always holds for any program trace abstracted over, whereas we have to be careful for lazy demands. In particular, when doing `plusDmd` we have to *lazify* the nested SubDemand if the outer cardinality is lazy. E.g., LP(SL) + SP(L) = (L+S)P((M*SL)+L) = SP(L+L) = SP(L) Multiplying with `M`/`C_01` is the "lazify" part here and is implemented in `lazifyIfStrict`. Example proving that point: d2 :: <LP(SL)><SP(A)> d2 x y = y `seq` (case x of (a,b) -> a, True) -- What is the demand on x in (d2 x x)? NOT SP(SL)!! We used to apply the same reasoning to Call SubDemands `Cn(sd)` in `plusSubDmd`, but that led to #21717, because different calls return different heap objects. See Note [Call SubDemand vs. evaluation Demand]. There are a couple more examples that improve in T21081. Here is a selection of those examples demonstrating the usefulness of The Premise: * "More let-to-case" (from testcase T21081): ```hs f :: (Bool, Bool) -> (Bool, Bool) f pr = (case pr of (a,b) -> a /= b, True) g :: Int -> (Bool, Bool) g x = let y = let z = odd x in (z,z) in f y ``` Although `f` is lazy in `pr`, we could case-bind `z` because it is always evaluated when `y` is evaluated. So we give `pr` demand `LP(SL,SL)` (most likely with better upper bounds/usage) and demand analysis then infers a strict demand for `z`. * "More eta reduction" (from testcase T21081): ```hs myfoldl :: (a -> b -> a) -> a -> [b] -> a myfoldl f z [] = z myfoldl f !z (x:xs) = myfoldl (\a b -> f a b) (f z x) xs ``` Here, we can give `f` a demand of `LC(S,C(1,L))` (instead of the lazier `LC(L,C(1,L))`) which says "Whenever `f` is evaluated (lazily), it is also called with two arguments". And Note [Eta reduction based on evaluation context] means we can rewrite `\a b -> f a b` to `f` in the call site of `myfoldl`. Nice! * "More CPR" (from testcase T18903): ```hs h :: Int -> Int h m = let g :: Int -> (Int,Int) g 1 = (m, 0) g n = (2 * n, 2 `div` n) {-# NOINLINE g #-} in case m of 1 -> 0 2 -> snd (g m) _ -> uncurry (+) (g m) ``` We want to give `g` the demand `MC(1,P(MP(L),1P(L)))`, so we see that in each call site of `g`, we are strict in the second component of the returned pair. That in turn means that Nested CPR can unbox the result of the division even though it might throw. Note [Unrealised opportunity in plusDmd] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Recall the lazification of SubDemands happening in `plusDmd` as described in Note [SubDemand denotes at least one evaluation]. We *could* do better when both Demands are lazy already. Example (fun 1, fun 2) Both args put Demand SC(S,L) on `fun`. The lazy pair arg context lazifies this to LC(S,L), and it would be reasonable to report this Demand on `fun` for the entire pair expression; after all, `fun` is called whenever it is evaluated. But our definition of `plusDmd` will compute LC(S,L) + LC(S,L) = (L+L)(M*C(S,L) + M*C(S,L)) = L(C(L,L)) = L Which is clearly less precise. Doing better here could mean to `lub` when both demands are lazy, e.g., LC(S,L) + LC(S,L) = (L+L)(C(S,L) ⊔ C(S,L)) = L(C(S,L)) Indeed that's what we did at one point between 9.4 and 9.6 after !7599, but it means that we need a function `lubPlusSubDmd` that lubs on lower bounds but plus'es upper bounds, implying maintenance challenges and complicated explanations. Plus, NoFib says that this special case doesn't bring all that much (geom. mean +0.0% counted instructions), so we don't bother anymore. Note [Call SubDemand vs. evaluation Demand] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Although both evaluation Demands and Call SubDemands carry a (Card,SubDemand) pair, their interpretation is quite different. Example: f x = fst x * snd x -- f :: <SP(1L,1L)>, because 1P(1L,A)+1P(A,1L) = SP(1L,1L) g x = fst (x 1) * snd (x 2) -- g :: <SC(S,P(ML,ML))>, because 1C(1,P(1L,A))+1C(1,P(A,1L)) = SC(S,P(ML,ML)) The point about this example is that both demands have P(A,1L)/P(1L,A) as sub-expressions, but when these sub-demands occur 1. under an evaluation demand, we combine with `plusSubDmd` 2. whereas under a Call sub-demand, we combine with `lubSubDmd` And thus (1) yields a stricter demand on the pair components than (2). In #21717 we saw that we really need lub in (2), because otherwise we make an unsound prediction in `g (\n -> if n == 1 then (1,1) else (bot,2))`; we'd say that the `bot` expression is always evaluated, when it clearly is not. Operationally, every call to `g` gives back a potentially distinct, heap-allocated pair with potentially different contents, and we must `lubSubDmd` over all such calls to approximate how any of those pairs might be used. That is in stark contrast to f's argument `x`: Operationally, every eval of `x` must yield the same pair and `f` evaluates both components of that pair. The theorem "every eval of `x` returns the same heap object" is a very strong MUST-alias property and we capitalise on that by using `plusSubDmd` in (1). And indeed we *must* use `plusSubDmd` in (1) for sound upper bounds in an analysis that assumes call-by-need (as opposed to the weaker call-by-name) for let bindings. Consider h x = fst x * fst x -- h :: <SP(SL,A)> And the expression `let a=1; p=(a,a)} in h p`. Here, *although* the RHS of `p` is only evaluated once under call-by-need, `a` is still evaluated twice. If we had used `lubSubDmd`, we'd see SP(1L,A) and the 1L unsoundly says "exactly once". If the analysis had assumed call-by-name, it would be sound to say "a is used once in p": p is used multiple times and hence so would a, as if p was a function. So using `plusSubDmd` does not only yield better strictness, it is also "holding up the other end of the bargain" of the call-by-need assumption for upper bounds. (To SG's knowledge, the distinction between call-by-name and call-by-need does not matter for strictness analysis/lower bounds, thus it would be sound to use `lubSubDmd` all the time there.) Note [mkCall and plusSubDmd] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We never rewrite a strict, non-absent Call sub-demand like C(S,S) to a polymorphic sub-demand like S, otherwise #21085 strikes. Consider the following inequality (would also for M and 1 instead of L and S, but we forbid such Polys): L+S = S = C(S,S) < C(S,L) = C(L,L)+C(S,S) Note that L=C(L,L). If we also had S=C(S,S), we'd be in trouble: Now `plusSubDmd` would no longer maintain the equality relation on sub-demands, much less monotonicity. Bad! Clearly, `n <= Cn(n)` is unproblematic, as is `n >= Cn(n)` for any `n` except 1 and S. But `C(S,S) >= S` would mean trouble, because then we'd get the problematic `C(S,S) = S`. We have just established that `S < C(S,S)`! As such, the rewrite C(S,S) to S is anti-monotone and we forbid it, first and foremost in `mkCall` (which is the only place that rewrites Cn(n) to n). Crisis and #21085 averted! Note [Computing one-shot info] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider a call f (\pqr. e1) (\xyz. e2) e3 where f has usage signature <C(M,C(L,C(M,L)))><C(M,L)><L> 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 [Sources of one-shot information] in GHC.Core.Opt.OccurAnal. Note [Boxity in Poly] ~~~~~~~~~~~~~~~~~~~~~ To support Note [Boxity analysis], it makes sense that 'Prod' carries a 'Boxity'. But why does 'Poly' have to carry a 'Boxity', too? Shouldn't all 'Poly's be 'Boxed'? Couldn't we simply use 'Prod Unboxed' when we need to express an unboxing demand? 'botSubDmd' (B) needs to be the bottom of the lattice, so it needs to be an Unboxed demand (and deeply, at that). Similarly, 'seqSubDmd' (A) is an Unboxed demand. So why not say that Polys with absent cardinalities have Unboxed boxity? That doesn't work, because we also need the boxed equivalents. Here's an example for A (function 'absent' in T19871): ``` f _ True = 1 f a False = a `seq` 2 -- demand on a: MA, the A is short for `Poly Boxed C_00` g a = a `seq` f a True -- demand on a: SA, which is `Poly Boxed C_00` h True p = g p -- SA on p (inherited from g) h False p@(x,y) = x+y -- S!P(1!L,1!L) on p ``` If A is treated as Unboxed, we get reboxing in the call site to 'g'. So we obviously would need a Boxed variant of A. Rather than introducing a lot of special cases, we just carry the Boxity in 'Poly'. Plus, we could most likely find examples like the above for any other cardinality. Note [Why Boxity in SubDemand and not in Demand?] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In #19871, we started out by storing 'Boxity' in 'SubDemand', in the 'Prod' constructor only. But then we found that we weren't able to express the unboxing 'seqSubDmd', because that one really is a `Poly C_00` sub-demand. We then tried to store the Boxity in 'Demand' instead, for these reasons: 1. The whole boxity-of-seq business comes to a satisfying conclusion 2. Putting Boxity in the SubDemand is weird to begin with, because it describes the box and not its fields, just as the evaluation cardinality of a Demand describes how often the box is used. It makes more sense that Card and Boxity travel together. Also the alternative would have been to store Boxity with Poly, which is even weirder and more redundant. But then we regressed in T7837 (grep #19871 for boring specifics), which needed to transfer an ambient unboxed *demand* on a dictionary selector to its argument dictionary, via a 'Call' sub-demand `C(1,sd)`, as Note [Demand transformer for a dictionary selector] explains. Annoyingly, the boxity info has to be stored in the *sub-demand* `sd`! There's no demand to store the boxity in. So we bit the bullet and now we store Boxity in 'SubDemand', both in 'Prod' *and* 'Poly'. See also Note [Boxity in Poly]. Note [Demand transformer for data constructors] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider the expression (x,y) with sub-demand P(SL,A). What is the demand on x,y? Obviously `x` is used strictly, and `y` not at all. So we want to decompose a product demand, and feed its components demands into the arguments. That is the job of dmdTransformDataConSig. More precisely, * it gets the demand on the data constructor itself; in the above example that is C(1,C(1,P(SL,A))) * it returns the demands on the arguments; in the above example that is [SL, A] Nasty wrinkle. Consider this code (#22475 has more realistic examples but assume this is what the demand analyser sees) data T = MkT !Int Bool get :: T -> Bool get (MkT _ b) = b foo = let v::Int = I# 7 t::T = MkT v True in get t Now `v` is unused by `get`, /but/ we can't give `v` an Absent demand, else we'll drop the binding and replace it with an error thunk. Then the code generator (more specifically GHC.Stg.InferTags.Rewrite) will add an extra eval of MkT's argument to give foo = let v::Int = error "absent" t::T = case v of v' -> MkT v' True in get t Boo! Because of this extra eval (added in STG-land), the truth is that `MkT` may (or may not) evaluate its arguments (as established in #21497). Hence the use of `bump` in dmdTransformDataConSig, which adds in a `C_01` eval. The `C_01` says "may or may not evaluate" which is absolutely faithful to what InferTags.Rewrite does. In particular it is very important /not/ to make that a `C_11` eval, see Note [Data-con worker strictness]. -} {- ********************************************************************* * * Divergence: Whether evaluation surely diverges * * ********************************************************************* -} -- | 'Divergence' characterises whether something surely diverges. -- Models a subset lattice of the following exhaustive set of divergence -- results: -- -- [n] nontermination (e.g. loops) -- [i] throws imprecise exception -- [p] throws precise exceTtion -- [c] converges (reduces to WHNF). -- -- The different lattice elements correspond to different subsets, indicated by -- juxtaposition of indicators (e.g. __nc__ definitely doesn't throw an -- exception, and may or may not reduce to WHNF). -- -- @ -- Dunno (nipc) -- | -- ExnOrDiv (nip) -- | -- Diverges (ni) -- @ -- -- As you can see, we don't distinguish __n__ and __i__. -- See Note [Precise exceptions and strictness analysis] for why __p__ is so -- special compared to __i__. data Divergence = Diverges -- ^ Definitely throws an imprecise exception or diverges. | ExnOrDiv -- ^ Definitely throws a *precise* exception, an imprecise -- exception or diverges. Never converges, hence 'isDeadEndDiv'! -- See scenario 1 in Note [Precise exceptions and strictness analysis]. | Dunno -- ^ Might diverge, throw any kind of exception or converge. deriving Divergence -> Divergence -> Bool (Divergence -> Divergence -> Bool) -> (Divergence -> Divergence -> Bool) -> Eq Divergence forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: Divergence -> Divergence -> Bool == :: Divergence -> Divergence -> Bool $c/= :: Divergence -> Divergence -> Bool /= :: Divergence -> Divergence -> Bool Eq lubDivergence :: Divergence -> Divergence -> Divergence lubDivergence :: Divergence -> Divergence -> Divergence lubDivergence Divergence Diverges Divergence div = Divergence div lubDivergence Divergence div Divergence Diverges = Divergence div lubDivergence Divergence ExnOrDiv Divergence ExnOrDiv = Divergence ExnOrDiv lubDivergence Divergence _ Divergence _ = Divergence Dunno -- This needs to commute with defaultFvDmd, i.e. -- defaultFvDmd (r1 `lubDivergence` r2) = defaultFvDmd r1 `lubDmd` defaultFvDmd r2 -- (See Note [Default demand on free variables and arguments] for why) -- | See Note [Asymmetry of plusDmdType], which concludes that 'plusDivergence' -- needs to be symmetric. -- Strictly speaking, we should have @plusDivergence Dunno Diverges = ExnOrDiv@. -- But that regresses in too many places (every infinite loop, basically) to be -- worth it and is only relevant in higher-order scenarios -- (e.g. Divergence of @f (throwIO blah)@). -- So 'plusDivergence' currently is 'glbDivergence', really. plusDivergence :: Divergence -> Divergence -> Divergence plusDivergence :: Divergence -> Divergence -> Divergence plusDivergence Divergence Dunno Divergence Dunno = Divergence Dunno plusDivergence Divergence Diverges Divergence _ = Divergence Diverges plusDivergence Divergence _ Divergence Diverges = Divergence Diverges plusDivergence Divergence _ Divergence _ = Divergence ExnOrDiv -- | In a non-strict scenario, we might not force the Divergence, in which case -- we might converge, hence Dunno. multDivergence :: Card -> Divergence -> Divergence multDivergence :: Card -> Divergence -> Divergence multDivergence Card n Divergence _ | Bool -> Bool not (Card -> Bool isStrict Card n) = Divergence Dunno multDivergence Card _ Divergence d = Divergence d topDiv, exnDiv, botDiv :: Divergence topDiv :: Divergence topDiv = Divergence Dunno exnDiv :: Divergence exnDiv = Divergence ExnOrDiv botDiv :: Divergence botDiv = Divergence Diverges -- | True if the 'Divergence' indicates that evaluation will not return. -- See Note [Dead ends]. isDeadEndDiv :: Divergence -> Bool isDeadEndDiv :: Divergence -> Bool isDeadEndDiv Divergence Diverges = Bool True isDeadEndDiv Divergence ExnOrDiv = Bool True isDeadEndDiv Divergence Dunno = Bool False -- See Notes [Default demand on free variables and arguments] -- and Scenario 1 in [Precise exceptions and strictness analysis] defaultFvDmd :: Divergence -> Demand defaultFvDmd :: Divergence -> Demand defaultFvDmd Divergence Dunno = Demand absDmd defaultFvDmd Divergence ExnOrDiv = Demand absDmd -- This is the whole point of ExnOrDiv! defaultFvDmd Divergence Diverges = Demand botDmd -- Diverges defaultArgDmd :: Divergence -> Demand -- TopRes and BotRes are polymorphic, so that -- BotRes === (Bot -> BotRes) === ... -- TopRes === (Top -> TopRes) === ... -- This function makes that concrete -- Also see Note [Default demand on free variables and arguments] defaultArgDmd :: Divergence -> Demand defaultArgDmd Divergence Dunno = Demand topDmd -- NB: not botDmd! We don't want to mask the precise exception by forcing the -- argument. But it is still absent. defaultArgDmd Divergence ExnOrDiv = Demand absDmd defaultArgDmd Divergence Diverges = Demand botDmd {- Note [Precise vs imprecise exceptions] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ An exception is considered to be /precise/ when it is thrown by the 'raiseIO#' primop. It follows that all other primops (such as 'raise#' or division-by-zero) throw /imprecise/ exceptions. Note that the actual type of the exception thrown doesn't have any impact! GHC undertakes some effort not to apply an optimisation that would mask a /precise/ exception with some other source of nontermination, such as genuine divergence or an imprecise exception, so that the user can reliably intercept the precise exception with a catch handler before and after optimisations. See also the wiki page on precise exceptions: https://gitlab.haskell.org/ghc/ghc/wikis/exceptions/precise-exceptions Section 5 of "Tackling the awkward squad" talks about semantic concerns. Imprecise exceptions are actually more interesting than precise ones (which are fairly standard) from the perspective of semantics. See the paper "A Semantics for Imprecise Exceptions" for more details. Note [Dead ends] ~~~~~~~~~~~~~~~~ We call an expression that either diverges or throws a precise or imprecise exception a "dead end". We used to call such an expression just "bottoming", but with the measures we take to preserve precise exception semantics (see Note [Precise exceptions and strictness analysis]), that is no longer accurate: 'exnDiv' is no longer the bottom of the Divergence lattice. Yet externally to demand analysis, we mostly care about being able to drop dead code etc., which is all due to the property that such an expression never returns, hence we consider throwing a precise exception to be a dead end. See also 'isDeadEndDiv'. Note [Precise exceptions and strictness analysis] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We have to take care to preserve precise exception semantics in strictness analysis (#17676). There are two scenarios that need careful treatment. The fixes were discussed at https://gitlab.haskell.org/ghc/ghc/wikis/fixing-precise-exceptions Recall that raiseIO# raises a *precise* exception, in contrast to raise# which raises an *imprecise* exception. See Note [Precise vs imprecise exceptions]. Scenario 1: Precise exceptions in case alternatives --------------------------------------------------- Unlike raise# (which returns botDiv), we want raiseIO# to return exnDiv. Here's why. Consider this example from #13380 (similarly #17676): f x y | x>0 = raiseIO# Exc | y>0 = return 1 | otherwise = return 2 Is 'f' strict in 'y'? One might be tempted to say yes! But that plays fast and loose with the precise exception; after optimisation, (f 42 (error "boom")) turns from throwing the precise Exc to throwing the imprecise user error "boom". So, the defaultFvDmd of raiseIO# should be lazy (topDmd), which can be achieved by giving it divergence exnDiv. See Note [Default demand on free variables and arguments]. Why don't we just give it topDiv instead of introducing exnDiv? Because then the simplifier will fail to discard raiseIO#'s continuation in case raiseIO# x s of { (# s', r #) -> <BIG> } which we'd like to optimise to case raiseIO# x s of {} Hence we came up with exnDiv. The default FV demand of exnDiv is lazy (and its default arg dmd is absent), but otherwise (in terms of 'isDeadEndDiv') it behaves exactly as botDiv, so that dead code elimination works as expected. This is tracked by T13380b. Scenario 2: Precise exceptions in case scrutinees ------------------------------------------------- Consider (more complete examples in #148, #1592, testcase strun003) case foo x s of { (# s', r #) -> y } Is this strict in 'y'? Often not! If @foo x s@ might throw a precise exception (ultimately via raiseIO#), then we must not force 'y', which may fail to terminate or throw an imprecise exception, until we have performed @foo x s@. So we have to 'deferAfterPreciseException' (which 'lub's with 'exnDmdType' to model the exceptional control flow) when @foo x s@ may throw a precise exception. Motivated by T13380{d,e,f}. See Note [Which scrutinees may throw precise exceptions] in "GHC.Core.Opt.DmdAnal". We have to be careful not to discard dead-end Divergence from case alternatives, though (#18086): m = putStrLn "foo" >> error "bar" 'm' should still have 'exnDiv', which is why it is not sufficient to lub with 'nopDmdType' (which has 'topDiv') in 'deferAfterPreciseException'. Historical Note: This used to be called the "IO hack". But that term is rather a bad fit because 1. It's easily confused with the "State hack", which also affects IO. 2. Neither "IO" nor "hack" is a good description of what goes on here, which is deferring strictness results after possibly throwing a precise exception. The "hack" is probably not having to defer when we can prove that the expression may not throw a precise exception (increasing precision of the analysis), but that's just a favourable guess. Note [Exceptions and strictness] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We used to smart about catching exceptions, but we aren't anymore. See #14998 for the way it's resolved at the moment. Here's a historic breakdown: Apparently, exception handling prim-ops didn't use to have any special strictness signatures, thus defaulting to nopSig, which assumes they use their arguments lazily. Joachim was the first to realise that we could provide richer information. Thus, in 0558911f91c (Dec 13), he added signatures to primops.txt.pp indicating that functions like `catch#` and `catchRetry#` call their argument, which is useful information for usage analysis. Still with a 'Lazy' strictness demand (i.e. 'lazyApply1Dmd'), though, and the world was fine. In 7c0fff4 (July 15), Simon argued that giving `catch#` et al. a 'strictApply1Dmd' leads to substantial performance gains. That was at the cost of correctness, as #10712 proved. So, back to 'lazyApply1Dmd' in 28638dfe79e (Dec 15). Motivated to reproduce the gains of 7c0fff4 without the breakage of #10712, Ben opened #11222. Simon made the demand analyser "understand catch" in 9915b656 (Jan 16) by adding a new 'catchArgDmd', which basically said to call its argument strictly, but also swallow any thrown exceptions in 'multDivergence'. This was realized by extending the 'Str' constructor of 'ArgStr' with a 'ExnStr' field, indicating that it catches the exception, and adding a 'ThrowsExn' constructor to the 'Divergence' lattice as an element between 'Dunno' and 'Diverges'. Then along came #11555 and finally #13330, so we had to revert to 'lazyApply1Dmd' again in 701256df88c (Mar 17). This left the other variants like 'catchRetry#' having 'catchArgDmd', which is where #14998 picked up. Item 1 was concerned with measuring the impact of also making `catchRetry#` and `catchSTM#` have 'lazyApply1Dmd'. The result was that there was none. We removed the last usages of 'catchArgDmd' in 00b8ecb7 (Apr 18). There was a lot of dead code resulting from that change, that we removed in ef6b283 (Jan 19): We got rid of 'ThrowsExn' and 'ExnStr' again and removed any code that was dealing with the peculiarities. Where did the speed-ups vanish to? In #14998, item 3 established that turning 'catch#' strict in its first argument didn't bring back any of the alleged performance benefits. Item 2 of that ticket finally found out that it was entirely due to 'catchException's new (since #11555) definition, which was simply catchException !io handler = catch io handler While 'catchException' is arguably the saner semantics for 'catch', it is an internal helper function in "GHC.IO". Its use in "GHC.IO.Handle.Internals.do_operation" made for the huge allocation differences: Remove the bang and you find the regressions we originally wanted to avoid with 'catchArgDmd'. See also #exceptions_and_strictness# in "GHC.IO". So history keeps telling us that the only possibly correct strictness annotation for the first argument of 'catch#' is 'lazyApply1Dmd', because 'catch#' really is not strict in its argument: Just try this in GHCi :set -XScopedTypeVariables import Control.Exception catch undefined (\(_ :: SomeException) -> putStrLn "you'll see this") Any analysis that assumes otherwise will be broken in some way or another (beyond `-fno-pedantic-bottoms`). But then #13380 and #17676 suggest (in Mar 20) that we need to re-introduce a subtly different variant of `ThrowsExn` (which we call `ExnOrDiv` now) that is only used by `raiseIO#` in order to preserve precise exceptions by strictness analysis, while not impacting the ability to eliminate dead code. See Note [Precise exceptions and strictness analysis]. Note [Default demand on free variables and arguments] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Free variables not mentioned in the environment of a 'DmdType' are demanded according to the demand type's Divergence: * In a Diverges (botDiv) context, that demand is botDmd (strict and absent). * In all other contexts, the demand is absDmd (lazy and absent). This is recorded in 'defaultFvDmd'. Similarly, we can eta-expand demand types to get demands on excess arguments not accounted for in the type, by consulting 'defaultArgDmd': * In a Diverges (botDiv) context, that demand is again botDmd. * In a ExnOrDiv (exnDiv) context, that demand is absDmd: We surely diverge before evaluating the excess argument, but don't want to eagerly evaluate it (cf. Note [Precise exceptions and strictness analysis]). * In a Dunno context (topDiv), the demand is topDmd, because it's perfectly possible to enter the additional lambda and evaluate it in unforeseen ways (so, not absent). Note [Bottom CPR iff Dead-Ending Divergence] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Both CPR analysis and Demand analysis handle recursive functions by doing fixed-point iteration. To find the *least* (e.g., most informative) fixed-point, iteration starts with the bottom element of the semantic domain. Diverging functions generally have the bottom element as their least fixed-point. One might think that CPR analysis and Demand analysis then agree in when a function gets a bottom denotation. E.g., whenever it has 'botCpr', it should also have 'botDiv'. But that is not the case, because strictness analysis has to be careful around precise exceptions, see Note [Precise vs imprecise exceptions]. So Demand analysis gives some diverging functions 'exnDiv' (which is *not* the bottom element) when the CPR signature says 'botCpr', and that's OK. Here's an example (from #18086) where that is the case: ioTest :: IO () ioTest = do putStrLn "hi" undefined However, one can loosely say that we give a function 'botCpr' whenever its 'Divergence' is 'exnDiv' or 'botDiv', i.e., dead-ending. But that's just a consequence of fixed-point iteration, it's not important that they agree. ************************************************************************ * * Demand environments and types * * ************************************************************************ -} -- Subject to Note [Default demand on free variables and arguments] -- | Captures the result of an evaluation of an expression, by -- -- * Listing how the free variables of that expression have been evaluted -- ('de_fvs') -- * Saying whether or not evaluation would surely diverge ('de_div') -- -- See Note [Demand env Equality]. data DmdEnv = DE { DmdEnv -> VarEnv Demand de_fvs :: !(VarEnv Demand), DmdEnv -> Divergence de_div :: !Divergence } instance Eq DmdEnv where DE VarEnv Demand fv1 Divergence div1 == :: DmdEnv -> DmdEnv -> Bool == DE VarEnv Demand fv2 Divergence div2 = Divergence div1 Divergence -> Divergence -> Bool forall a. Eq a => a -> a -> Bool == Divergence div2 Bool -> Bool -> Bool && Divergence -> VarEnv Demand -> VarEnv Demand forall {key}. Divergence -> UniqFM key Demand -> UniqFM key Demand canonicalise Divergence div1 VarEnv Demand fv1 VarEnv Demand -> VarEnv Demand -> Bool forall a. Eq a => a -> a -> Bool == Divergence -> VarEnv Demand -> VarEnv Demand forall {key}. Divergence -> UniqFM key Demand -> UniqFM key Demand canonicalise Divergence div2 VarEnv Demand fv2 where canonicalise :: Divergence -> UniqFM key Demand -> UniqFM key Demand canonicalise Divergence div UniqFM key Demand fv = (Demand -> Bool) -> UniqFM key Demand -> UniqFM key Demand forall elt key. (elt -> Bool) -> UniqFM key elt -> UniqFM key elt filterUFM (Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool /= Divergence -> Demand defaultFvDmd Divergence div) UniqFM key Demand fv mkEmptyDmdEnv :: Divergence -> DmdEnv mkEmptyDmdEnv :: Divergence -> DmdEnv mkEmptyDmdEnv Divergence div = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand forall a. VarEnv a emptyVarEnv Divergence div -- | Build a potentially terminating 'DmdEnv' from a finite map that says what -- has been evaluated so far mkTermDmdEnv :: VarEnv Demand -> DmdEnv mkTermDmdEnv :: VarEnv Demand -> DmdEnv mkTermDmdEnv VarEnv Demand fvs = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand fvs Divergence topDiv nopDmdEnv :: DmdEnv nopDmdEnv :: DmdEnv nopDmdEnv = Divergence -> DmdEnv mkEmptyDmdEnv Divergence topDiv botDmdEnv :: DmdEnv botDmdEnv :: DmdEnv botDmdEnv = Divergence -> DmdEnv mkEmptyDmdEnv Divergence botDiv exnDmdEnv :: DmdEnv exnDmdEnv :: DmdEnv exnDmdEnv = Divergence -> DmdEnv mkEmptyDmdEnv Divergence exnDiv lubDmdEnv :: DmdEnv -> DmdEnv -> DmdEnv lubDmdEnv :: DmdEnv -> DmdEnv -> DmdEnv lubDmdEnv (DE VarEnv Demand fv1 Divergence d1) (DE VarEnv Demand fv2 Divergence d2) = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand lub_fv Divergence lub_div where -- See Note [Demand env Equality] lub_fv :: VarEnv Demand lub_fv = (Demand -> Demand -> Demand) -> VarEnv Demand -> Demand -> VarEnv Demand -> Demand -> VarEnv Demand forall a. (a -> a -> a) -> VarEnv a -> a -> VarEnv a -> a -> VarEnv a plusVarEnv_CD Demand -> Demand -> Demand lubDmd VarEnv Demand fv1 (Divergence -> Demand defaultFvDmd Divergence d1) VarEnv Demand fv2 (Divergence -> Demand defaultFvDmd Divergence d2) lub_div :: Divergence lub_div = Divergence -> Divergence -> Divergence lubDivergence Divergence d1 Divergence d2 addVarDmdEnv :: DmdEnv -> Id -> Demand -> DmdEnv addVarDmdEnv :: DmdEnv -> Id -> Demand -> DmdEnv addVarDmdEnv env :: DmdEnv env@(DE VarEnv Demand fvs Divergence div) Id id Demand dmd = VarEnv Demand -> Divergence -> DmdEnv DE (VarEnv Demand -> Id -> Demand -> VarEnv Demand forall a. VarEnv a -> Id -> a -> VarEnv a extendVarEnv VarEnv Demand fvs Id id (Demand dmd Demand -> Demand -> Demand `plusDmd` DmdEnv -> Id -> Demand lookupDmdEnv DmdEnv env Id id)) Divergence div plusDmdEnv :: DmdEnv -> DmdEnv -> DmdEnv plusDmdEnv :: DmdEnv -> DmdEnv -> DmdEnv plusDmdEnv (DE VarEnv Demand fv1 Divergence d1) (DE VarEnv Demand fv2 Divergence d2) -- In contrast to Note [Asymmetry of plusDmdType], this function is symmetric. | VarEnv Demand -> Bool forall a. VarEnv a -> Bool isEmptyVarEnv VarEnv Demand fv2, Divergence -> Demand defaultFvDmd Divergence d2 Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand absDmd = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand fv1 (Divergence d1 Divergence -> Divergence -> Divergence `plusDivergence` Divergence d2) -- a very common case that is much more efficient | VarEnv Demand -> Bool forall a. VarEnv a -> Bool isEmptyVarEnv VarEnv Demand fv1, Divergence -> Demand defaultFvDmd Divergence d1 Demand -> Demand -> Bool forall a. Eq a => a -> a -> Bool == Demand absDmd = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand fv2 (Divergence d1 Divergence -> Divergence -> Divergence `plusDivergence` Divergence d2) -- another very common case that is much more efficient | Bool otherwise = VarEnv Demand -> Divergence -> DmdEnv DE ((Demand -> Demand -> Demand) -> VarEnv Demand -> Demand -> VarEnv Demand -> Demand -> VarEnv Demand forall a. (a -> a -> a) -> VarEnv a -> a -> VarEnv a -> a -> VarEnv a plusVarEnv_CD Demand -> Demand -> Demand plusDmd VarEnv Demand fv1 (Divergence -> Demand defaultFvDmd Divergence d1) VarEnv Demand fv2 (Divergence -> Demand defaultFvDmd Divergence d2)) (Divergence d1 Divergence -> Divergence -> Divergence `plusDivergence` Divergence d2) -- | 'DmdEnv' is a monoid via 'plusDmdEnv' and 'nopDmdEnv'; this is its 'msum' plusDmdEnvs :: [DmdEnv] -> DmdEnv plusDmdEnvs :: [DmdEnv] -> DmdEnv plusDmdEnvs [] = DmdEnv nopDmdEnv plusDmdEnvs [DmdEnv] pdas = (DmdEnv -> DmdEnv -> DmdEnv) -> [DmdEnv] -> DmdEnv forall a. HasCallStack => (a -> a -> a) -> [a] -> a foldl1' DmdEnv -> DmdEnv -> DmdEnv plusDmdEnv [DmdEnv] pdas multDmdEnv :: Card -> DmdEnv -> DmdEnv multDmdEnv :: Card -> DmdEnv -> DmdEnv multDmdEnv Card C_11 DmdEnv env = DmdEnv env multDmdEnv Card C_00 DmdEnv _ = DmdEnv nopDmdEnv multDmdEnv Card n (DE VarEnv Demand fvs Divergence div) = VarEnv Demand -> Divergence -> DmdEnv DE ((Demand -> Demand) -> VarEnv Demand -> VarEnv Demand forall a b. (a -> b) -> VarEnv a -> VarEnv b mapVarEnv (Card -> Demand -> Demand multDmd Card n) VarEnv Demand fvs) (Card -> Divergence -> Divergence multDivergence Card n Divergence div) reuseEnv :: DmdEnv -> DmdEnv reuseEnv :: DmdEnv -> DmdEnv reuseEnv = Card -> DmdEnv -> DmdEnv multDmdEnv Card C_1N lookupDmdEnv :: DmdEnv -> Id -> Demand -- See Note [Default demand on free variables and arguments] lookupDmdEnv :: DmdEnv -> Id -> Demand lookupDmdEnv (DE VarEnv Demand fv Divergence div) Id id = VarEnv Demand -> Id -> Maybe Demand forall a. VarEnv a -> Id -> Maybe a lookupVarEnv VarEnv Demand fv Id id Maybe Demand -> Demand -> Demand forall a. Maybe a -> a -> a `orElse` Divergence -> Demand defaultFvDmd Divergence div delDmdEnv :: DmdEnv -> Id -> DmdEnv delDmdEnv :: DmdEnv -> Id -> DmdEnv delDmdEnv (DE VarEnv Demand fv Divergence div) Id id = VarEnv Demand -> Divergence -> DmdEnv DE (VarEnv Demand fv VarEnv Demand -> Id -> VarEnv Demand forall a. VarEnv a -> Id -> VarEnv a `delVarEnv` Id id) Divergence div -- | Characterises how an expression -- -- * Evaluates its free variables ('dt_env') including divergence info -- * Evaluates its arguments ('dt_args') -- data DmdType = DmdType { DmdType -> DmdEnv dt_env :: !DmdEnv -- ^ Demands on free variables. -- See Note [Demand type Divergence] , DmdType -> [Demand] dt_args :: ![Demand] -- ^ Demand on arguments } -- | See Note [Demand env Equality]. instance Eq DmdType where DmdType DmdEnv env1 [Demand] ds1 == :: DmdType -> DmdType -> Bool == DmdType DmdEnv env2 [Demand] ds2 = [Demand] ds1 [Demand] -> [Demand] -> Bool forall a. Eq a => a -> a -> Bool == [Demand] ds2 -- cheap checks first Bool -> Bool -> Bool && DmdEnv env1 DmdEnv -> DmdEnv -> Bool forall a. Eq a => a -> a -> Bool == DmdEnv env2 -- | Compute the least upper bound of two 'DmdType's elicited /by the same -- incoming demand/! lubDmdType :: DmdType -> DmdType -> DmdType lubDmdType :: DmdType -> DmdType -> DmdType lubDmdType DmdType d1 DmdType d2 = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv lub_fv [Demand] lub_ds 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) = Int -> DmdType -> DmdType etaExpandDmdType Int n DmdType d1 (DmdType DmdEnv fv2 [Demand] ds2) = Int -> DmdType -> DmdType etaExpandDmdType Int n DmdType d2 lub_ds :: [Demand] lub_ds = String -> (Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. HasDebugCallStack => String -> (a -> b -> c) -> [a] -> [b] -> [c] zipWithEqual String "lubDmdType" Demand -> Demand -> Demand lubDmd [Demand] ds1 [Demand] ds2 lub_fv :: DmdEnv lub_fv = DmdEnv -> DmdEnv -> DmdEnv lubDmdEnv DmdEnv fv1 DmdEnv fv2 discardArgDmds :: DmdType -> DmdEnv discardArgDmds :: DmdType -> DmdEnv discardArgDmds (DmdType DmdEnv fv [Demand] _) = DmdEnv fv plusDmdType :: DmdType -> DmdEnv -> DmdType plusDmdType :: DmdType -> DmdEnv -> DmdType plusDmdType (DmdType DmdEnv fv [Demand] ds) DmdEnv fv' -- See Note [Asymmetry of plusDmdType] -- 'DmdEnv' forms a (monoidal) action on 'DmdType' via this operation. = DmdEnv -> [Demand] -> DmdType DmdType (DmdEnv -> DmdEnv -> DmdEnv plusDmdEnv DmdEnv fv DmdEnv fv') [Demand] ds botDmdType :: DmdType botDmdType :: DmdType botDmdType = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv botDmdEnv [] -- | The demand type of doing nothing (lazy, absent, no Divergence -- information). Note that it is ''not'' the top of the lattice (which would be -- "may use everything"), so it is (no longer) called topDmdType. nopDmdType :: DmdType nopDmdType :: DmdType nopDmdType = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv nopDmdEnv [] -- | The demand type of an unspecified expression that is guaranteed to -- throw a (precise or imprecise) exception or diverge. exnDmdType :: DmdType exnDmdType :: DmdType exnDmdType = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv exnDmdEnv [] dmdTypeDepth :: DmdType -> Arity dmdTypeDepth :: DmdType -> Int dmdTypeDepth = [Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length ([Demand] -> Int) -> (DmdType -> [Demand]) -> DmdType -> Int forall b c a. (b -> c) -> (a -> b) -> a -> c . DmdType -> [Demand] dt_args -- | This makes sure we can use the demand type with n arguments after eta -- expansion, where n must not be lower than the demand types depth. -- It appends the argument list with the correct 'defaultArgDmd'. etaExpandDmdType :: Arity -> DmdType -> DmdType etaExpandDmdType :: Int -> DmdType -> DmdType etaExpandDmdType Int n d :: DmdType d@DmdType{dt_args :: DmdType -> [Demand] dt_args = [Demand] ds, dt_env :: DmdType -> DmdEnv dt_env = DmdEnv env} | Int n Int -> Int -> Bool forall a. Eq a => a -> a -> Bool == Int depth = DmdType d | Int n Int -> Int -> Bool forall a. Ord a => a -> a -> Bool > Int depth = DmdType d{dt_args = inc_ds} | Bool otherwise = String -> SDoc -> DmdType forall a. HasCallStack => String -> SDoc -> a pprPanic String "etaExpandDmdType: arity decrease" (Int -> SDoc forall a. Outputable a => a -> SDoc ppr Int n SDoc -> SDoc -> SDoc forall doc. IsDoc doc => doc -> doc -> doc $$ DmdType -> SDoc forall a. Outputable a => a -> SDoc ppr DmdType d) where depth :: Int depth = [Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] ds -- Arity increase: -- * Demands on FVs are still valid -- * Demands on args also valid, plus we can extend with defaultArgDmd -- as appropriate for the given Divergence -- * Divergence is still valid: -- - A dead end after 2 arguments stays a dead end after 3 arguments -- - The remaining case is Dunno, which is already topDiv inc_ds :: [Demand] inc_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 (Divergence -> Demand defaultArgDmd (DmdEnv -> Divergence de_div DmdEnv env))) -- | A conservative approximation for a given 'DmdType' in case of an arity -- decrease. Currently, it's just nopDmdType. decreaseArityDmdType :: DmdType -> DmdType decreaseArityDmdType :: DmdType -> DmdType decreaseArityDmdType DmdType _ = DmdType nopDmdType 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 ty :: DmdType ty@DmdType{dt_args :: DmdType -> [Demand] dt_args=Demand dmd:[Demand] args} = (Demand dmd, DmdType ty{dt_args=args}) splitDmdTy ty :: DmdType ty@DmdType{dt_env :: DmdType -> DmdEnv dt_env=DmdEnv env} = (Divergence -> Demand defaultArgDmd (DmdEnv -> Divergence de_div DmdEnv env), DmdType ty) multDmdType :: Card -> DmdType -> DmdType multDmdType :: Card -> DmdType -> DmdType multDmdType Card n (DmdType DmdEnv fv [Demand] args) = -- pprTrace "multDmdType" (ppr n $$ ppr fv $$ ppr (multDmdEnv n fv)) $ DmdEnv -> [Demand] -> DmdType DmdType (Card -> DmdEnv -> DmdEnv multDmdEnv Card n DmdEnv fv) ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map (Card -> Demand -> Demand multDmd Card n) [Demand] args) peelFV :: DmdType -> Var -> (DmdType, Demand) peelFV :: DmdType -> Id -> (DmdType, Demand) peelFV (DmdType DmdEnv fv [Demand] ds) Id id = -- pprTrace "rfv" (ppr id <+> ppr dmd $$ ppr fv) (DmdEnv -> [Demand] -> DmdType DmdType DmdEnv fv' [Demand] ds, Demand dmd) where -- Force these arguments so that old `Env` is not retained. !fv' :: DmdEnv fv' = DmdEnv fv DmdEnv -> Id -> DmdEnv `delDmdEnv` Id id !dmd :: Demand dmd = DmdEnv -> Id -> Demand lookupDmdEnv DmdEnv fv Id id addDemand :: Demand -> DmdType -> DmdType addDemand :: Demand -> DmdType -> DmdType addDemand Demand dmd (DmdType DmdEnv fv [Demand] ds) = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv fv (Demand dmdDemand -> [Demand] -> [Demand] forall a. a -> [a] -> [a] :[Demand] ds) findIdDemand :: DmdType -> Var -> Demand findIdDemand :: DmdType -> Id -> Demand findIdDemand (DmdType DmdEnv fv [Demand] _) Id id = DmdEnv -> Id -> Demand lookupDmdEnv DmdEnv fv Id id -- | When e is evaluated after executing an IO action that may throw a precise -- exception, we act as if there is an additional control flow path that is -- taken if e throws a precise exception. The demand type of this control flow -- path -- * is lazy and absent ('topDmd') and boxed in all free variables and arguments -- * has 'exnDiv' 'Divergence' result -- See Note [Precise exceptions and strictness analysis] -- -- So we can simply take a variant of 'nopDmdType', 'exnDmdType'. -- Why not 'nopDmdType'? Because then the result of 'e' can never be 'exnDiv'! -- That means failure to drop dead-ends, see #18086. deferAfterPreciseException :: DmdType -> DmdType deferAfterPreciseException :: DmdType -> DmdType deferAfterPreciseException = DmdType -> DmdType -> DmdType lubDmdType DmdType exnDmdType {- Note [deferAfterPreciseException] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The big picture is in Note [Precise exceptions and strictness analysis] The idea is that we want to treat case <I/O operation> of (# s', r #) -> rhs as if it was case <I/O operation> of Just (# s', r #) -> rhs Nothing -> error That is, the I/O operation might throw an exception, so that 'rhs' never gets reached. For example, we don't want to be strict in the strict free variables of 'rhs'. So we have the simple definition deferAfterPreciseException = lubDmdType (DmdType emptyDmdEnv [] exnDiv) Historically, when we had `lubBoxity = _unboxedWins` (see Note [unboxedWins]), we had a more complicated definition for deferAfterPreciseException to make sure it preserved boxity in its argument. That was needed for code like case <I/O operation> of (# s', r) -> f x which uses `x` *boxed*. If we `lub`bed it with `(DmdType emptyDmdEnv [] exnDiv)` we'd get an *unboxed* demand on `x` (because we let Unboxed win), which led to #20746. Nowadays with `lubBoxity = boxedWins` we don't need the complicated definition. Note [Demand type Divergence] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In contrast to DmdSigs, DmdTypes are elicited under a specific incoming demand. This is described in detail in Note [Understanding DmdType and DmdSig]. Here, we'll focus on what that means for a DmdType's Divergence in a higher-order scenario. Consider err x y = x `seq` y `seq` error (show x) this has a strictness signature of <1L><1L>b meaning that we don't know what happens when we call err in weaker contexts than C(1,C(1,L)), like @err `seq` ()@ (1A) and @err 1 `seq` ()@ (C(S,A)). We may not unleash the botDiv, hence assume topDiv. Of course, in @err 1 2 `seq` ()@ the incoming demand C(S,C(S,A)) is strong enough and we see that the expression diverges. Now consider a function f g = g 1 2 with signature <C(1,C(1,L))>, and the expression f err `seq` () now f puts a strictness demand of C(1,C(1,L)) onto its argument, which is unleashed on err via the App rule. In contrast to weaker head strictness, this demand is strong enough to unleash err's signature and hence we see that the whole expression diverges! Note [Demand env Equality] ~~~~~~~~~~~~~~~~~~~~~~~~~~~ What is the difference between the Demand env {x->A} and {}? Answer: There is none! They have the exact same semantics, because any var that is not mentioned in 'de_fvs' implicitly has demand 'defaultFvDmd', based on the divergence of the demand env 'de_div'. Similarly, b{x->B, y->A} is the same as b{y->A}, because the default FV demand of BotDiv is B. But neither is equal to b{}, because y has demand B in the latter, not A as before. The Eq instance of DmdEnv must reflect that, otherwise we can get into monotonicity issues during fixed-point iteration ({x->A} /= {} /= {x->A} /= ...). It does so by filtering out any default FV demands prior to comparing 'de_fvs'. Note that 'lubDmdEnv' maintains this kind of equality by using 'plusVarEnv_CD', involving 'defaultFvDmd' for any entries present in one 'de_fvs' but not the other. Note [Asymmetry of plusDmdType] ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'plus' for DmdTypes is *asymmetrical*, because there can only one be one type contributing argument demands! 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 `plusType` 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 `plusType` 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, as in plusDivergence. Since we don't use argument demands of the second argument anyway, 'plus's second argument is just a 'PlusDmdType'. But note that the argument demand types are not guaranteed to be observed in left to right order. For example, analysis of a case expression will pass the demand type for the alts as the left argument and the type for the scrutinee as the right argument. Also, it is not at all clear if there is such an order; consider the LetUp case, where the RHS might be forced at any point while evaluating the let body. Therefore, it is crucial that 'plusDivergence' is symmetric! 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(1,C(1,L))> 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(1,C(1,L)). Then we have to 'multDmdType' the announced demand type. Examples: * Not strict enough, e.g. C(1,C(1,L)): - We have to multiply all argument and free variable demands with C_01, zapping strictness. - We have to multiply divergence with C_01. 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 convergence, than that would still hold under a weaker demand than expected by the demand transformer. * Used more than once, e.g. C(S,C(1,L)): - Multiply with C_1N. Even if f puts a used-once 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. In dmdTransformSig, we call peelManyCalls to find out the 'Card'inality with which we have to multiply and then call multDmdType with that. 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. -} {- ************************************************************************ * * Demand signatures * * ************************************************************************ In a let-bound Id we record its demand signature. In principle, this demand signature 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 emasculated demand transformer, namely a single DmdType (Nevertheless we dignify DmdSig 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 [Demand type Divergence] for the meaning of a Divergence 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 DmdSig (DmdType {x -> <1L>} <A><1P(L,L)>) says that when the function is applied to two arguments, it unleashes demand 1L on the free var x, A on the first arg, and 1P(L,L) on the second. If this same function is applied to one arg, all we can say is that it uses x with 1L, and its arg with demand 1P(L,L). Note [Understanding DmdType and DmdSig] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Demand types are sound approximations of an expression's semantics relative to the incoming demand we put the expression under. Consider the following expression: \x y -> x `seq` (y, 2*x) Here is a table with demand types resulting from different incoming demands we put that expression under. Note the monotonicity; a stronger incoming demand yields a more precise demand type: incoming demand | demand type -------------------------------- 1A | <L><L>{} C(1,C(1,L)) | <1P(L)><L>{} C(1,C(1,1P(1P(L),A))) | <1P(A)><A>{} Note that in the first example, the depth of the demand type was *higher* than the arity of the incoming call demand due to the anonymous lambda. The converse is also possible and happens when we unleash demand signatures. In @f x y@, the incoming call demand on f has arity 2. But if all we have is a demand signature with depth 1 for @f@ (which we can safely unleash, see below), the demand type of @f@ under a call demand of arity 2 has a *lower* depth of 1. So: Demand types are elicited by putting an expression under an incoming (call) demand, the arity of which can be lower or higher than the depth of the resulting demand type. In contrast, a demand signature summarises a function's semantics *without* immediately specifying the incoming demand it was produced under. Despite StrSig being a newtype wrapper around DmdType, it actually encodes two things: * The threshold (i.e., minimum arity) to unleash the signature * A demand type that is sound to unleash when the minimum arity requirement is met. Here comes the subtle part: The threshold is encoded in the wrapped demand type's depth! So in mkDmdSigForArity we make sure to trim the list of argument demands to the given threshold arity. Call sites will make sure that this corresponds to the arity of the call demand that elicited the wrapped demand type. See also Note [What are demand signatures?]. -} -- | The depth of the wrapped 'DmdType' encodes the arity at which it is safe -- to unleash. Better construct this through 'mkDmdSigForArity'. -- See Note [Understanding DmdType and DmdSig] newtype DmdSig = DmdSig DmdType deriving DmdSig -> DmdSig -> Bool (DmdSig -> DmdSig -> Bool) -> (DmdSig -> DmdSig -> Bool) -> Eq DmdSig forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: DmdSig -> DmdSig -> Bool == :: DmdSig -> DmdSig -> Bool $c/= :: DmdSig -> DmdSig -> Bool /= :: DmdSig -> DmdSig -> Bool Eq -- | Turns a 'DmdType' computed for the particular 'Arity' into a 'DmdSig' -- unleashable at that arity. See Note [Understanding DmdType and DmdSig]. mkDmdSigForArity :: Arity -> DmdType -> DmdSig mkDmdSigForArity :: Int -> DmdType -> DmdSig mkDmdSigForArity Int arity dmd_ty :: DmdType dmd_ty@(DmdType DmdEnv fvs [Demand] args) | Int arity Int -> Int -> Bool forall a. Ord a => a -> a -> Bool < DmdType -> Int dmdTypeDepth DmdType dmd_ty = DmdType -> DmdSig DmdSig (DmdType -> DmdSig) -> DmdType -> DmdSig forall a b. (a -> b) -> a -> b $ DmdEnv -> [Demand] -> DmdType DmdType DmdEnv fvs (Int -> [Demand] -> [Demand] forall a. Int -> [a] -> [a] take Int arity [Demand] args) | Bool otherwise = DmdType -> DmdSig DmdSig (Int -> DmdType -> DmdType etaExpandDmdType Int arity DmdType dmd_ty) mkClosedDmdSig :: [Demand] -> Divergence -> DmdSig mkClosedDmdSig :: [Demand] -> Divergence -> DmdSig mkClosedDmdSig [Demand] ds Divergence div = Int -> DmdType -> DmdSig mkDmdSigForArity ([Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] ds) (DmdEnv -> [Demand] -> DmdType DmdType (Divergence -> DmdEnv mkEmptyDmdEnv Divergence div) [Demand] ds) mkVanillaDmdSig :: Arity -> Divergence -> DmdSig mkVanillaDmdSig :: Int -> Divergence -> DmdSig mkVanillaDmdSig Int ar Divergence div = [Demand] -> Divergence -> DmdSig mkClosedDmdSig (Int -> Demand -> [Demand] forall a. Int -> a -> [a] replicate Int ar Demand topDmd) Divergence div splitDmdSig :: DmdSig -> ([Demand], Divergence) splitDmdSig :: DmdSig -> ([Demand], Divergence) splitDmdSig (DmdSig (DmdType DmdEnv env [Demand] dmds)) = ([Demand] dmds, DmdEnv -> Divergence de_div DmdEnv env) dmdSigDmdEnv :: DmdSig -> DmdEnv dmdSigDmdEnv :: DmdSig -> DmdEnv dmdSigDmdEnv (DmdSig (DmdType DmdEnv env [Demand] _)) = DmdEnv env hasDemandEnvSig :: DmdSig -> Bool hasDemandEnvSig :: DmdSig -> Bool hasDemandEnvSig = Bool -> Bool not (Bool -> Bool) -> (DmdSig -> Bool) -> DmdSig -> Bool forall b c a. (b -> c) -> (a -> b) -> a -> c . VarEnv Demand -> Bool forall a. VarEnv a -> Bool isEmptyVarEnv (VarEnv Demand -> Bool) -> (DmdSig -> VarEnv Demand) -> DmdSig -> Bool forall b c a. (b -> c) -> (a -> b) -> a -> c . DmdEnv -> VarEnv Demand de_fvs (DmdEnv -> VarEnv Demand) -> (DmdSig -> DmdEnv) -> DmdSig -> VarEnv Demand forall b c a. (b -> c) -> (a -> b) -> a -> c . DmdSig -> DmdEnv dmdSigDmdEnv botSig :: DmdSig botSig :: DmdSig botSig = DmdType -> DmdSig DmdSig DmdType botDmdType nopSig :: DmdSig nopSig :: DmdSig nopSig = DmdType -> DmdSig DmdSig DmdType nopDmdType isNopSig :: DmdSig -> Bool isNopSig :: DmdSig -> Bool isNopSig (DmdSig DmdType ty) = DmdType ty DmdType -> DmdType -> Bool forall a. Eq a => a -> a -> Bool == DmdType nopDmdType -- | True if the signature diverges or throws an exception in a saturated call. -- See Note [Dead ends]. isDeadEndSig :: DmdSig -> Bool isDeadEndSig :: DmdSig -> Bool isDeadEndSig (DmdSig (DmdType DmdEnv env [Demand] _)) = Divergence -> Bool isDeadEndDiv (DmdEnv -> Divergence de_div DmdEnv env) -- | True if the signature diverges or throws an imprecise exception in a saturated call. -- NB: In constrast to 'isDeadEndSig' this returns False for 'exnDiv'. -- See Note [Dead ends] -- and Note [Precise vs imprecise exceptions]. isBottomingSig :: DmdSig -> Bool isBottomingSig :: DmdSig -> Bool isBottomingSig (DmdSig (DmdType DmdEnv env [Demand] _)) = DmdEnv -> Divergence de_div DmdEnv env Divergence -> Divergence -> Bool forall a. Eq a => a -> a -> Bool == Divergence botDiv -- | True when the signature indicates all arguments are boxed onlyBoxedArguments :: DmdSig -> Bool onlyBoxedArguments :: DmdSig -> Bool onlyBoxedArguments (DmdSig (DmdType DmdEnv _ [Demand] dmds)) = (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all Demand -> Bool demandIsBoxed [Demand] dmds where demandIsBoxed :: Demand -> Bool demandIsBoxed Demand BotDmd = Bool True demandIsBoxed Demand AbsDmd = Bool True demandIsBoxed (Card _ :* SubDemand sd) = SubDemand -> Bool subDemandIsboxed SubDemand sd subDemandIsboxed :: SubDemand -> Bool subDemandIsboxed (Poly Boxity Unboxed Card _) = Bool False subDemandIsboxed (Poly Boxity _ Card _) = Bool True subDemandIsboxed (Call Card _ SubDemand sd) = SubDemand -> Bool subDemandIsboxed SubDemand sd subDemandIsboxed (Prod Boxity Unboxed [Demand] _) = Bool False subDemandIsboxed (Prod Boxity _ [Demand] ds) = (Demand -> Bool) -> [Demand] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all Demand -> Bool demandIsBoxed [Demand] ds -- | Returns true if an application to n value args would diverge or throw an -- exception. -- -- If a function having 'botDiv' is applied to a less number of arguments than -- its syntactic arity, we cannot say for sure that it is going to diverge. -- Hence this function conservatively returns False in that case. -- See Note [Dead ends]. isDeadEndAppSig :: DmdSig -> Int -> Bool isDeadEndAppSig :: DmdSig -> Int -> Bool isDeadEndAppSig (DmdSig (DmdType DmdEnv env [Demand] ds)) Int n = Divergence -> Bool isDeadEndDiv (DmdEnv -> Divergence de_div DmdEnv env) Bool -> Bool -> Bool && Bool -> Bool not ([Demand] -> Int -> Bool forall a. [a] -> Int -> Bool lengthExceeds [Demand] ds Int n) trimBoxityDmdEnv :: DmdEnv -> DmdEnv trimBoxityDmdEnv :: DmdEnv -> DmdEnv trimBoxityDmdEnv (DE VarEnv Demand fvs Divergence div) = VarEnv Demand -> Divergence -> DmdEnv DE ((Demand -> Demand) -> VarEnv Demand -> VarEnv Demand forall a b. (a -> b) -> VarEnv a -> VarEnv b mapVarEnv Demand -> Demand trimBoxity VarEnv Demand fvs) Divergence div trimBoxityDmdType :: DmdType -> DmdType trimBoxityDmdType :: DmdType -> DmdType trimBoxityDmdType (DmdType DmdEnv env [Demand] ds) = DmdEnv -> [Demand] -> DmdType DmdType (DmdEnv -> DmdEnv trimBoxityDmdEnv DmdEnv env) ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map Demand -> Demand trimBoxity [Demand] ds) trimBoxityDmdSig :: DmdSig -> DmdSig trimBoxityDmdSig :: DmdSig -> DmdSig trimBoxityDmdSig = (DmdType -> DmdType) -> DmdSig -> DmdSig forall a b. Coercible a b => a -> b coerce DmdType -> DmdType trimBoxityDmdType -- | Transfers the boxity of the left arg to the demand structure of the right -- arg. This only makes sense if applied to new and old demands of the same -- value. transferBoxity :: Demand -> Demand -> Demand transferBoxity :: Demand -> Demand -> Demand transferBoxity Demand from Demand to = Demand -> Demand -> Demand go_dmd Demand from Demand to where go_dmd :: Demand -> Demand -> Demand go_dmd (Card from_n :* SubDemand from_sd) to_dmd :: Demand to_dmd@(Card to_n :* SubDemand to_sd) | Card -> Bool isAbs Card from_n Bool -> Bool -> Bool || Card -> Bool isAbs Card to_n = Demand to_dmd | Bool otherwise = case (SubDemand from_sd, SubDemand to_sd) of (Poly Boxity from_b Card _, Poly Boxity _ Card to_c) -> Card to_n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> Card -> SubDemand Poly Boxity from_b Card to_c (SubDemand _, Prod Boxity _ [Demand] to_ds) | Just (Boxity from_b, [Demand] from_ds) <- Int -> SubDemand -> Maybe (Boxity, [Demand]) viewProd ([Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] to_ds) SubDemand from_sd -> Card to_n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> [Demand] -> SubDemand mkProd Boxity from_b ((Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] strictZipWith Demand -> Demand -> Demand go_dmd [Demand] from_ds [Demand] to_ds) (Prod Boxity from_b [Demand] from_ds, SubDemand _) | Just (Boxity _, [Demand] to_ds) <- Int -> SubDemand -> Maybe (Boxity, [Demand]) viewProd ([Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] from_ds) SubDemand to_sd -> Card to_n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> [Demand] -> SubDemand mkProd Boxity from_b ((Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] strictZipWith Demand -> Demand -> Demand go_dmd [Demand] from_ds [Demand] to_ds) (SubDemand, SubDemand) _ -> Demand -> Demand trimBoxity Demand to_dmd transferArgBoxityDmdType :: DmdType -> DmdType -> DmdType transferArgBoxityDmdType :: DmdType -> DmdType -> DmdType transferArgBoxityDmdType _from :: DmdType _from@(DmdType DmdEnv _ [Demand] from_ds) to :: DmdType to@(DmdType DmdEnv to_env [Demand] to_ds) | [Demand] -> [Demand] -> Bool forall a b. [a] -> [b] -> Bool equalLength [Demand] from_ds [Demand] to_ds = -- pprTraceWith "transfer" (\r -> ppr _from $$ ppr to $$ ppr r) $ DmdEnv -> [Demand] -> DmdType DmdType DmdEnv to_env -- Only arg boxity! See Note [Don't change boxity without worker/wrapper] ((Demand -> Demand -> Demand) -> [Demand] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] zipWith Demand -> Demand -> Demand transferBoxity [Demand] from_ds [Demand] to_ds) | Bool otherwise = DmdType -> DmdType trimBoxityDmdType DmdType to transferArgBoxityDmdSig :: DmdSig -> DmdSig -> DmdSig transferArgBoxityDmdSig :: DmdSig -> DmdSig -> DmdSig transferArgBoxityDmdSig = (DmdType -> DmdType -> DmdType) -> DmdSig -> DmdSig -> DmdSig forall a b. Coercible a b => a -> b coerce DmdType -> DmdType -> DmdType transferArgBoxityDmdType prependArgsDmdSig :: Int -> DmdSig -> DmdSig -- ^ Add extra ('topDmd') arguments to a strictness signature. -- In contrast to 'etaConvertDmdSig', this /prepends/ additional argument -- demands. This is used by FloatOut. prependArgsDmdSig :: Int -> DmdSig -> DmdSig prependArgsDmdSig Int new_args sig :: DmdSig sig@(DmdSig dmd_ty :: DmdType dmd_ty@(DmdType DmdEnv env [Demand] dmds)) | Int new_args Int -> Int -> Bool forall a. Eq a => a -> a -> Bool == Int 0 = DmdSig sig | DmdType dmd_ty DmdType -> DmdType -> Bool forall a. Eq a => a -> a -> Bool == DmdType nopDmdType = DmdSig sig | Bool otherwise = DmdType -> DmdSig DmdSig (DmdEnv -> [Demand] -> DmdType DmdType DmdEnv env [Demand] dmds') where dmds' :: [Demand] dmds' = Bool -> SDoc -> [Demand] -> [Demand] forall a. HasCallStack => Bool -> SDoc -> a -> a assertPpr (Int new_args Int -> Int -> Bool forall a. Ord a => a -> a -> Bool > Int 0) (Int -> SDoc forall a. Outputable a => a -> SDoc ppr Int new_args) ([Demand] -> [Demand]) -> [Demand] -> [Demand] forall a b. (a -> b) -> a -> b $ Int -> Demand -> [Demand] forall a. Int -> a -> [a] replicate Int new_args Demand topDmd [Demand] -> [Demand] -> [Demand] forall a. [a] -> [a] -> [a] ++ [Demand] dmds etaConvertDmdSig :: Arity -> DmdSig -> DmdSig -- ^ We are expanding (\x y. e) to (\x y z. e z) or reducing from the latter to -- the former (when the Simplifier identifies a new join points, for example). -- In contrast to 'prependArgsDmdSig', this /appends/ extra arg demands if -- necessary. -- This works by looking at the 'DmdType' (which was produced under a call -- demand for the old arity) and trying to transfer as many facts as we can to -- the call demand of new arity. -- An arity increase (resulting in a stronger incoming demand) can retain much -- of the info, while an arity decrease (a weakening of the incoming demand) -- must fall back to a conservative default. etaConvertDmdSig :: Int -> DmdSig -> DmdSig etaConvertDmdSig Int arity (DmdSig DmdType dmd_ty) | Int arity Int -> Int -> Bool forall a. Ord a => a -> a -> Bool < DmdType -> Int dmdTypeDepth DmdType dmd_ty = DmdType -> DmdSig DmdSig (DmdType -> DmdSig) -> DmdType -> DmdSig forall a b. (a -> b) -> a -> b $ DmdType -> DmdType decreaseArityDmdType DmdType dmd_ty | Bool otherwise = DmdType -> DmdSig DmdSig (DmdType -> DmdSig) -> DmdType -> DmdSig forall a b. (a -> b) -> a -> b $ Int -> DmdType -> DmdType etaExpandDmdType Int arity DmdType dmd_ty {- ************************************************************************ * * Demand transformers * * ************************************************************************ -} -- | A /demand transformer/ is a monotone function from an incoming evaluation -- context ('SubDemand') to a 'DmdType', describing how the denoted thing -- (i.e. expression, function) uses its arguments and free variables, and -- whether it diverges. -- -- See Note [Understanding DmdType and DmdSig] -- and Note [What are demand signatures?]. type DmdTransformer = SubDemand -> DmdType -- | Extrapolate a demand signature ('DmdSig') into a 'DmdTransformer'. -- -- Given a function's 'DmdSig' and a 'SubDemand' for the evaluation context, -- return how the function evaluates its free variables and arguments. dmdTransformSig :: DmdSig -> DmdTransformer dmdTransformSig :: DmdSig -> DmdTransformer dmdTransformSig (DmdSig dmd_ty :: DmdType dmd_ty@(DmdType DmdEnv _ [Demand] arg_ds)) SubDemand sd = Card -> DmdType -> DmdType multDmdType ((Card, SubDemand) -> Card forall a b. (a, b) -> a fst ((Card, SubDemand) -> Card) -> (Card, SubDemand) -> Card forall a b. (a -> b) -> a -> b $ Int -> SubDemand -> (Card, SubDemand) peelManyCalls ([Demand] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [Demand] arg_ds) SubDemand sd) DmdType dmd_ty -- see Note [Demands from unsaturated function calls] -- and Note [What are demand signatures?] -- | A special 'DmdTransformer' for data constructors that feeds product -- demands into the constructor arguments. dmdTransformDataConSig :: [StrictnessMark] -> DmdTransformer -- See Note [Demand transformer for data constructors] dmdTransformDataConSig :: [StrictnessMark] -> DmdTransformer dmdTransformDataConSig [StrictnessMark] str_marks SubDemand sd = case Int -> SubDemand -> Maybe (Boxity, [Demand]) viewProd Int arity SubDemand body_sd of Just (Boxity _, [Demand] dmds) -> Card -> [Demand] -> DmdType mk_body_ty Card n [Demand] dmds Maybe (Boxity, [Demand]) Nothing -> DmdType nopDmdType where arity :: Int arity = [StrictnessMark] -> Int forall a. [a] -> Int forall (t :: * -> *) a. Foldable t => t a -> Int length [StrictnessMark] str_marks (Card n, SubDemand body_sd) = Int -> SubDemand -> (Card, SubDemand) peelManyCalls Int arity SubDemand sd mk_body_ty :: Card -> [Demand] -> DmdType mk_body_ty Card n [Demand] dmds = DmdEnv -> [Demand] -> DmdType DmdType DmdEnv nopDmdEnv ((StrictnessMark -> Demand -> Demand) -> [StrictnessMark] -> [Demand] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] zipWith (Card -> StrictnessMark -> Demand -> Demand bump Card n) [StrictnessMark] str_marks [Demand] dmds) bump :: Card -> StrictnessMark -> Demand -> Demand bump Card n StrictnessMark str Demand dmd | StrictnessMark -> Bool isMarkedStrict StrictnessMark str = Card -> Demand -> Demand multDmd Card n (Demand -> Demand -> Demand plusDmd Demand str_field_dmd Demand dmd) | Bool otherwise = Card -> Demand -> Demand multDmd Card n Demand dmd str_field_dmd :: Demand str_field_dmd = Card C_01 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand seqSubDmd -- Why not C_11? See Note [Data-con worker strictness] -- | A special 'DmdTransformer' for dictionary selectors that feeds the demand -- on the result into the indicated dictionary component (if saturated). -- See Note [Demand transformer for a dictionary selector]. dmdTransformDictSelSig :: DmdSig -> DmdTransformer -- NB: This currently doesn't handle newtype dictionaries. -- It should simply apply call_sd directly to the dictionary, I suppose. dmdTransformDictSelSig :: DmdSig -> DmdTransformer dmdTransformDictSelSig (DmdSig (DmdType DmdEnv _ [Card _ :* SubDemand prod])) SubDemand call_sd | (Card n, SubDemand sd') <- SubDemand -> (Card, SubDemand) peelCallDmd SubDemand call_sd , Prod Boxity _ [Demand] sig_ds <- SubDemand prod = Card -> DmdType -> DmdType multDmdType Card n (DmdType -> DmdType) -> DmdType -> DmdType forall a b. (a -> b) -> a -> b $ DmdEnv -> [Demand] -> DmdType DmdType DmdEnv nopDmdEnv [Card C_11 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* Boxity -> [Demand] -> SubDemand mkProd Boxity Unboxed ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map (SubDemand -> Demand -> Demand enhance SubDemand sd') [Demand] sig_ds)] | Bool otherwise = DmdType nopDmdType -- See Note [Demand transformer for a dictionary selector] where enhance :: SubDemand -> Demand -> Demand enhance SubDemand _ Demand AbsDmd = Demand AbsDmd enhance SubDemand _ Demand BotDmd = Demand BotDmd enhance SubDemand sd Demand _dmd_var = Card C_11 HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand sd -- This is the one! -- C_11, because we multiply with n above dmdTransformDictSelSig DmdSig sig SubDemand sd = String -> SDoc -> DmdType forall a. HasCallStack => String -> SDoc -> a pprPanic String "dmdTransformDictSelSig: no args" (DmdSig -> SDoc forall a. Outputable a => a -> SDoc ppr DmdSig sig SDoc -> SDoc -> SDoc forall doc. IsDoc doc => doc -> doc -> doc $$ SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) {- Note [What are demand signatures?] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Demand analysis interprets expressions in the abstract domain of demand transformers. Given a (sub-)demand that denotes the evaluation context, the abstract transformer of an expression gives us back a demand type denoting how other things (like arguments and free vars) were used when the expression was evaluated. Here's an example: f x y = if x + expensive then \z -> z + y * ... else \z -> z * ... The abstract transformer (let's call it F_e) of the if expression (let's call it e) would transform an incoming (undersaturated!) head demand 1A into a demand type like {x-><1L>,y-><L>}<L>. In pictures: Demand ---F_e---> DmdType <1A> {x-><1L>,y-><L>}<L> Let's assume that the demand transformers we compute for an expression are correct wrt. to some concrete semantics for Core. How do demand signatures fit in? They are strange beasts, given that they come with strict rules when to it's sound to unleash them. Fortunately, we can formalise the rules with Galois connections. Consider f's strictness signature, {}<1L><L>. It's a single-point approximation of the actual abstract transformer of f's RHS for arity 2. So, what happens is that we abstract *once more* from the abstract domain we already are in, replacing the incoming Demand by a simple lattice with two elements denoting incoming arity: A_2 = {<2, >=2} (where '<2' is the top element and >=2 the bottom element). Here's the diagram: A_2 -----f_f----> DmdType ^ | | α γ | | v SubDemand --F_f----> DmdType With α(C(1,C(1,_))) = >=2 α(_) = <2 γ(ty) = ty and F_f being the abstract transformer of f's RHS and f_f being the abstracted abstract transformer computable from our demand signature simply by f_f(>=2) = {}<1L><L> f_f(<2) = multDmdType C_0N {}<1L><L> where multDmdType makes a proper top element out of the given demand type. In practice, the A_n domain is not just a simple Bool, but a Card, which is exactly the Card with which we have to multDmdType. The Card for arity n is computed by calling @peelManyCalls n@, which corresponds to α above. Note [Demand transformer for a dictionary selector] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suppose we have a superclass selector 'sc_sel' and a class method selector 'op_sel', and a function that uses both, like this -- Strictness sig: 1P(1,A) sc_sel (x,y) = x -- Strictness sig: 1P(A,1) op_sel (p,q)= q f d v = op_sel (sc_sel d) v What do we learn about the demand on 'd'? Alas, we see only the demand from 'sc_sel', namely '1P(1,A)'. We /don't/ see that 'd' really has a nested demand '1P(1P(A,1C(1,1)),A)'. On the other hand, if we inlined the two selectors we'd have f d x = case d of (x,_) -> case x of (_,q) -> q v If we analyse that, we'll get a richer, nested demand on 'd'. We want to behave /as if/ we'd inlined 'op_sel' and 'sc_sel'. We can do this easily by building a richer demand transformer for dictionary selectors than is expressible by a regular demand signature. And that is what 'dmdTransformDictSelSig' does: it transforms the demand on the result to a demand on the (single) argument. How does it do that? 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: P(AAA1AAAAA). Then replace the '1' (or any other non-absent demand, really) by the demand 'd'. The '1' acts as if it was a demand variable, the whole signature really means `\d. P(AAAdAAAAA)` for any incoming demand 'd'. For single-method classes, which are represented by newtypes the signature of 'op' won't look like P(...), so matching on Prod 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. SG: Although we then probably want to apply the eval demand 'd' directly to 'op' rather than turning it into 'topSubDmd'... It is (just.. #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. -} zapDmdEnv :: DmdEnv -> DmdEnv zapDmdEnv :: DmdEnv -> DmdEnv zapDmdEnv (DE VarEnv Demand _ Divergence div) = Divergence -> DmdEnv mkEmptyDmdEnv Divergence div -- | Remove the demand environment from the signature. zapDmdEnvSig :: DmdSig -> DmdSig zapDmdEnvSig :: DmdSig -> DmdSig zapDmdEnvSig (DmdSig (DmdType DmdEnv env [Demand] ds)) = DmdType -> DmdSig DmdSig (DmdEnv -> [Demand] -> DmdType DmdType (DmdEnv -> DmdEnv zapDmdEnv DmdEnv env) [Demand] ds) 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 { 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 `C_01 :*` info (but not `CM` sub-demands) 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 { 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 `C_01 :*` info (but not `CM` sub-demands) from the strictness -- signature zapUsedOnceSig :: DmdSig -> DmdSig zapUsedOnceSig :: DmdSig -> DmdSig zapUsedOnceSig (DmdSig (DmdType DmdEnv env [Demand] ds)) = DmdType -> DmdSig DmdSig (DmdEnv -> [Demand] -> DmdType DmdType DmdEnv env ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map Demand -> Demand zapUsedOnceDemand [Demand] ds)) data KillFlags = KillFlags { KillFlags -> Bool kf_abs :: Bool , KillFlags -> Bool kf_used_once :: Bool , KillFlags -> Bool kf_called_once :: Bool } kill_usage_card :: KillFlags -> Card -> Card kill_usage_card :: KillFlags -> Card -> Card kill_usage_card KillFlags kfs Card C_00 | KillFlags -> Bool kf_abs KillFlags kfs = Card C_0N kill_usage_card KillFlags kfs Card C_10 | KillFlags -> Bool kf_abs KillFlags kfs = Card C_1N kill_usage_card KillFlags kfs Card C_01 | KillFlags -> Bool kf_used_once KillFlags kfs = Card C_0N kill_usage_card KillFlags kfs Card C_11 | KillFlags -> Bool kf_used_once KillFlags kfs = Card C_1N kill_usage_card KillFlags _ Card n = Card n kill_usage :: KillFlags -> Demand -> Demand kill_usage :: KillFlags -> Demand -> Demand kill_usage KillFlags _ Demand AbsDmd = Demand AbsDmd kill_usage KillFlags _ Demand BotDmd = Demand BotDmd kill_usage KillFlags kfs (Card n :* SubDemand sd) = KillFlags -> Card -> Card kill_usage_card KillFlags kfs Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* KillFlags -> SubDemand -> SubDemand kill_usage_sd KillFlags kfs SubDemand sd kill_usage_sd :: KillFlags -> SubDemand -> SubDemand kill_usage_sd :: KillFlags -> SubDemand -> SubDemand kill_usage_sd KillFlags kfs (Call Card n SubDemand sd) | KillFlags -> Bool kf_called_once KillFlags kfs = Card -> SubDemand -> SubDemand mkCall (Card -> Card -> Card lubCard Card C_1N Card n) (KillFlags -> SubDemand -> SubDemand kill_usage_sd KillFlags kfs SubDemand sd) | Bool otherwise = Card -> SubDemand -> SubDemand mkCall Card n (KillFlags -> SubDemand -> SubDemand kill_usage_sd KillFlags kfs SubDemand sd) kill_usage_sd KillFlags kfs (Prod Boxity b [Demand] ds) = Boxity -> [Demand] -> SubDemand mkProd Boxity b ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map (KillFlags -> Demand -> Demand kill_usage KillFlags kfs) [Demand] ds) kill_usage_sd KillFlags _ SubDemand sd = SubDemand sd {- ********************************************************************* * * TypeShape and demand trimming * * ********************************************************************* -} data TypeShape -- See Note [Trimming a demand to a type] -- in GHC.Core.Opt.DmdAnal = TsFun TypeShape | TsProd [TypeShape] | TsUnk trimToType :: Demand -> TypeShape -> Demand -- See Note [Trimming a demand to a type] in GHC.Core.Opt.DmdAnal trimToType :: Demand -> TypeShape -> Demand trimToType Demand AbsDmd TypeShape _ = Demand AbsDmd trimToType Demand BotDmd TypeShape _ = Demand BotDmd trimToType (Card n :* SubDemand sd) TypeShape ts = Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand -> TypeShape -> SubDemand go SubDemand sd TypeShape ts where go :: SubDemand -> TypeShape -> SubDemand go (Prod Boxity b [Demand] ds) (TsProd [TypeShape] tss) | [Demand] -> [TypeShape] -> Bool forall a b. [a] -> [b] -> Bool equalLength [Demand] ds [TypeShape] tss = Boxity -> [Demand] -> SubDemand mkProd Boxity b ((Demand -> TypeShape -> Demand) -> [Demand] -> [TypeShape] -> [Demand] forall a b c. (a -> b -> c) -> [a] -> [b] -> [c] zipWith Demand -> TypeShape -> Demand trimToType [Demand] ds [TypeShape] tss) go (Call Card n SubDemand sd) (TsFun TypeShape ts) = Card -> SubDemand -> SubDemand mkCall Card n (SubDemand -> TypeShape -> SubDemand go SubDemand sd TypeShape ts) go sd :: SubDemand sd@Poly{} TypeShape _ = SubDemand sd go SubDemand _ TypeShape _ = SubDemand topSubDmd -- | Drop all boxity trimBoxity :: Demand -> Demand trimBoxity :: Demand -> Demand trimBoxity Demand AbsDmd = Demand AbsDmd trimBoxity Demand BotDmd = Demand BotDmd trimBoxity (Card n :* SubDemand sd) = Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :* SubDemand -> SubDemand go SubDemand sd where go :: SubDemand -> SubDemand go (Poly Boxity _ Card n) = Boxity -> Card -> SubDemand Poly Boxity Boxed Card n go (Prod Boxity _ [Demand] ds) = Boxity -> [Demand] -> SubDemand mkProd Boxity Boxed ((Demand -> Demand) -> [Demand] -> [Demand] forall a b. (a -> b) -> [a] -> [b] map Demand -> Demand trimBoxity [Demand] ds) go (Call Card n SubDemand sd) = Card -> SubDemand -> SubDemand mkCall Card n (SubDemand -> SubDemand) -> SubDemand -> SubDemand forall a b. (a -> b) -> a -> b $ SubDemand -> SubDemand go SubDemand sd {- ************************************************************************ * * 'seq'ing demands * * ************************************************************************ -} seqDemand :: Demand -> () seqDemand :: Demand -> () seqDemand Demand AbsDmd = () seqDemand Demand BotDmd = () seqDemand (Card _ :* SubDemand sd) = SubDemand -> () seqSubDemand SubDemand sd seqSubDemand :: SubDemand -> () seqSubDemand :: SubDemand -> () seqSubDemand (Prod Boxity _ [Demand] ds) = [Demand] -> () seqDemandList [Demand] ds seqSubDemand (Call Card _ SubDemand sd) = SubDemand -> () seqSubDemand SubDemand sd seqSubDemand (Poly Boxity _ Card _) = () seqDemandList :: [Demand] -> () seqDemandList :: [Demand] -> () seqDemandList = (Demand -> () -> ()) -> () -> [Demand] -> () forall a b. (a -> b -> b) -> b -> [a] -> b forall (t :: * -> *) a b. Foldable t => (a -> b -> b) -> b -> t a -> b foldr (() -> () -> () forall a b. a -> b -> b seq (() -> () -> ()) -> (Demand -> ()) -> Demand -> () -> () forall b c a. (b -> c) -> (a -> b) -> a -> c . Demand -> () seqDemand) () seqDmdType :: DmdType -> () seqDmdType :: DmdType -> () seqDmdType (DmdType DmdEnv env [Demand] ds) = DmdEnv -> () seqDmdEnv DmdEnv env () -> () -> () forall a b. a -> b -> b `seq` [Demand] -> () seqDemandList [Demand] ds () -> () -> () forall a b. a -> b -> b `seq` () seqDmdEnv :: DmdEnv -> () seqDmdEnv :: DmdEnv -> () seqDmdEnv (DE VarEnv Demand fvs Divergence _) = (Demand -> ()) -> VarEnv Demand -> () forall elt key. (elt -> ()) -> UniqFM key elt -> () seqEltsUFM Demand -> () seqDemand VarEnv Demand fvs seqDmdSig :: DmdSig -> () seqDmdSig :: DmdSig -> () seqDmdSig (DmdSig DmdType ty) = DmdType -> () seqDmdType DmdType ty {- ************************************************************************ * * Outputable and Binary instances * * ************************************************************************ -} -- Just for debugging purposes. instance Show Card where show :: Card -> String show Card C_00 = String "C_00" show Card C_01 = String "C_01" show Card C_0N = String "C_0N" show Card C_10 = String "C_10" show Card C_11 = String "C_11" show Card C_1N = String "C_1N" {- Note [Demand notation] ~~~~~~~~~~~~~~~~~~~~~~~~~ This Note should be kept up to date with the documentation of `-fstrictness` in the user's guide. For pretty-printing demands, we use quite a compact notation with some abbreviations. Here's the BNF: card ::= B {} | A {0} | M {0,1} | L {0,1,n} | 1 {1} | S {1,n} box ::= ! Unboxed | <empty> Boxed d ::= card sd The :* constructor, just juxtaposition | card abbreviation: Same as "card card" sd ::= box card @Poly box card@ | box P(d,d,..) @Prod box [d1,d2,..]@ | Ccard(sd) @Call card sd@ So, L can denote a 'Card', polymorphic 'SubDemand' or polymorphic 'Demand', but it's always clear from context which "overload" is meant. It's like return-type inference of e.g. 'read'. Examples are in the haddock for 'Demand'. This is the syntax for demand signatures: div ::= <empty> topDiv | x exnDiv | b botDiv sig ::= {x->dx,y->dy,z->dz...}<d1><d2><d3>...<dn>div ^ ^ ^ ^ ^ ^ | | | | | | | \---+---+------/ | | | | demand on free demand on divergence variables arguments information (omitted if empty) (omitted if no information) -} -- | See Note [Demand notation] -- Current syntax was discussed in #19016. instance Outputable Card where ppr :: Card -> SDoc ppr Card C_00 = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'A' -- "Absent" ppr Card C_01 = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'M' -- "Maybe" ppr Card C_0N = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'L' -- "Lazy" ppr Card C_11 = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char '1' -- "exactly 1" ppr Card C_1N = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'S' -- "Strict" ppr Card C_10 = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'B' -- "Bottom" -- | See Note [Demand notation] instance Outputable Demand where ppr :: Demand -> SDoc ppr Demand AbsDmd = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'A' ppr Demand BotDmd = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'B' ppr (Card C_0N :* Poly Boxity Boxed Card C_0N) = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'L' -- Print LL as just L ppr (Card C_1N :* Poly Boxity Boxed Card C_1N) = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'S' -- Dito SS ppr (Card n :* SubDemand sd) = Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd -- | See Note [Demand notation] instance Outputable SubDemand where ppr :: SubDemand -> SDoc ppr (Poly Boxity b Card n) = Boxity -> SDoc pp_boxity Boxity b SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n ppr (Call Card n SubDemand sd) = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'C' SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SDoc -> SDoc forall doc. IsLine doc => doc -> doc parens (Card -> SDoc forall a. Outputable a => a -> SDoc ppr Card n SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SDoc forall doc. IsLine doc => doc comma SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SubDemand -> SDoc forall a. Outputable a => a -> SDoc ppr SubDemand sd) ppr (Prod Boxity b [Demand] ds) = Boxity -> SDoc pp_boxity Boxity b SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'P' SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SDoc -> SDoc forall doc. IsLine doc => doc -> doc parens ([Demand] -> SDoc forall {a}. Outputable a => [a] -> SDoc fields [Demand] ds) where fields :: [a] -> SDoc fields [] = SDoc forall doc. IsOutput doc => doc empty fields [a x] = a -> SDoc forall a. Outputable a => a -> SDoc ppr a x fields (a x:[a] xs) = a -> SDoc forall a. Outputable a => a -> SDoc ppr a x SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> Char -> SDoc forall doc. IsLine doc => Char -> doc char Char ',' SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> [a] -> SDoc fields [a] xs pp_boxity :: Boxity -> SDoc pp_boxity :: Boxity -> SDoc pp_boxity Boxity Unboxed = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char '!' pp_boxity Boxity _ = SDoc forall doc. IsOutput doc => doc empty instance Outputable Divergence where ppr :: Divergence -> SDoc ppr Divergence Diverges = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'b' -- for (b)ottom ppr Divergence ExnOrDiv = Char -> SDoc forall doc. IsLine doc => Char -> doc char Char 'x' -- for e(x)ception ppr Divergence Dunno = SDoc forall doc. IsOutput doc => doc empty instance Outputable DmdEnv where ppr :: DmdEnv -> SDoc ppr (DE VarEnv Demand fvs Divergence div) = Divergence -> SDoc forall a. Outputable a => a -> SDoc ppr Divergence div SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> if [(Unique, Demand)] -> Bool forall a. [a] -> Bool forall (t :: * -> *) a. Foldable t => t a -> Bool null [(Unique, Demand)] fv_elts then SDoc forall doc. IsOutput doc => doc empty else SDoc -> SDoc forall doc. IsLine doc => doc -> doc braces ([SDoc] -> SDoc forall doc. IsLine doc => [doc] -> doc 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 forall doc. IsLine doc => doc -> doc -> doc <> String -> SDoc forall doc. IsLine doc => String -> doc text String "->" SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> a -> SDoc forall a. Outputable a => a -> SDoc ppr a dmd fv_elts :: [(Unique, Demand)] fv_elts = VarEnv Demand -> [(Unique, Demand)] forall key elt. UniqFM key elt -> [(Unique, elt)] nonDetUFMToList VarEnv Demand fvs -- It's OK to use nonDetUFMToList here because we only do it for -- pretty printing instance Outputable DmdType where ppr :: DmdType -> SDoc ppr (DmdType DmdEnv fv [Demand] ds) = [SDoc] -> SDoc forall doc. IsLine doc => [doc] -> doc hcat ((Demand -> SDoc) -> [Demand] -> [SDoc] forall a b. (a -> b) -> [a] -> [b] map (SDoc -> SDoc forall doc. IsLine doc => doc -> doc angleBrackets (SDoc -> SDoc) -> (Demand -> SDoc) -> Demand -> SDoc forall b c a. (b -> c) -> (a -> b) -> a -> c . Demand -> SDoc forall a. Outputable a => a -> SDoc ppr) [Demand] ds) SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> DmdEnv -> SDoc forall a. Outputable a => a -> SDoc ppr DmdEnv fv instance Outputable DmdSig where ppr :: DmdSig -> SDoc ppr (DmdSig DmdType ty) = DmdType -> SDoc forall a. Outputable a => a -> SDoc ppr DmdType ty instance Outputable TypeShape where ppr :: TypeShape -> SDoc ppr TypeShape TsUnk = String -> SDoc forall doc. IsLine doc => String -> doc text String "TsUnk" ppr (TsFun TypeShape ts) = String -> SDoc forall doc. IsLine doc => String -> doc text String "TsFun" SDoc -> SDoc -> SDoc forall doc. IsLine doc => doc -> doc -> doc <> SDoc -> SDoc forall doc. IsLine doc => doc -> doc parens (TypeShape -> SDoc forall a. Outputable a => a -> SDoc ppr TypeShape ts) ppr (TsProd [TypeShape] tss) = SDoc -> SDoc forall doc. IsLine doc => doc -> doc parens ([SDoc] -> SDoc forall doc. IsLine doc => [doc] -> doc hsep ([SDoc] -> SDoc) -> [SDoc] -> SDoc forall a b. (a -> b) -> a -> b $ SDoc -> [SDoc] -> [SDoc] forall doc. IsLine doc => doc -> [doc] -> [doc] punctuate SDoc forall doc. IsLine doc => doc 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) instance Binary Card where put_ :: BinHandle -> Card -> IO () put_ BinHandle bh Card C_00 = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 0 put_ BinHandle bh Card C_01 = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 1 put_ BinHandle bh Card C_0N = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 2 put_ BinHandle bh Card C_11 = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 3 put_ BinHandle bh Card C_1N = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 4 put_ BinHandle bh Card C_10 = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 5 get :: BinHandle -> IO Card get BinHandle bh = do Word8 h <- BinHandle -> IO Word8 getByte BinHandle bh case Word8 h of Word8 0 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_00 Word8 1 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_01 Word8 2 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_0N Word8 3 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_11 Word8 4 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_1N Word8 5 -> Card -> IO Card forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Card C_10 Word8 _ -> String -> SDoc -> IO Card forall a. HasCallStack => String -> SDoc -> a pprPanic String "Binary:Card" (Int -> SDoc forall a. Outputable a => a -> SDoc ppr (Word8 -> Int forall a b. (Integral a, Num b) => a -> b fromIntegral Word8 h :: Int)) instance Binary Demand where put_ :: BinHandle -> Demand -> IO () put_ BinHandle bh (Card n :* SubDemand sd) = BinHandle -> Card -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Card n IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> case Card n of Card C_00 -> () -> IO () forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return () Card C_10 -> () -> IO () forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return () Card _ -> BinHandle -> SubDemand -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh SubDemand sd get :: BinHandle -> IO Demand get BinHandle bh = BinHandle -> IO Card forall a. Binary a => BinHandle -> IO a get BinHandle bh IO Card -> (Card -> IO Demand) -> IO Demand forall a b. IO a -> (a -> IO b) -> IO b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b >>= \Card n -> case Card n of Card C_00 -> Demand -> IO Demand forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Demand AbsDmd Card C_10 -> Demand -> IO Demand forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Demand BotDmd Card _ -> (Card n HasDebugCallStack => Card -> SubDemand -> Demand Card -> SubDemand -> Demand :*) (SubDemand -> Demand) -> IO SubDemand -> IO Demand forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO SubDemand forall a. Binary a => BinHandle -> IO a get BinHandle bh instance Binary SubDemand where put_ :: BinHandle -> SubDemand -> IO () put_ BinHandle bh (Poly Boxity b Card sd) = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 0 IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> Boxity -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Boxity b IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> Card -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Card sd put_ BinHandle bh (Call Card n SubDemand sd) = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 1 IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> Card -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Card n IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> SubDemand -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh SubDemand sd put_ BinHandle bh (Prod Boxity b [Demand] ds) = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 2 IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> Boxity -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Boxity b IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> [Demand] -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh [Demand] ds get :: BinHandle -> IO SubDemand get BinHandle bh = do Word8 h <- BinHandle -> IO Word8 getByte BinHandle bh case Word8 h of Word8 0 -> Boxity -> Card -> SubDemand Poly (Boxity -> Card -> SubDemand) -> IO Boxity -> IO (Card -> SubDemand) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO Boxity forall a. Binary a => BinHandle -> IO a get BinHandle bh IO (Card -> SubDemand) -> IO Card -> IO SubDemand forall a b. IO (a -> b) -> IO a -> IO b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b <*> BinHandle -> IO Card forall a. Binary a => BinHandle -> IO a get BinHandle bh Word8 1 -> Card -> SubDemand -> SubDemand mkCall (Card -> SubDemand -> SubDemand) -> IO Card -> IO (SubDemand -> SubDemand) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO Card forall a. Binary a => BinHandle -> IO a get BinHandle bh IO (SubDemand -> SubDemand) -> IO SubDemand -> IO SubDemand forall a b. IO (a -> b) -> IO a -> IO b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b <*> BinHandle -> IO SubDemand forall a. Binary a => BinHandle -> IO a get BinHandle bh Word8 2 -> Boxity -> [Demand] -> SubDemand Prod (Boxity -> [Demand] -> SubDemand) -> IO Boxity -> IO ([Demand] -> SubDemand) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO Boxity forall a. Binary a => BinHandle -> IO a get BinHandle bh IO ([Demand] -> SubDemand) -> IO [Demand] -> IO SubDemand forall a b. IO (a -> b) -> IO a -> IO b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b <*> BinHandle -> IO [Demand] forall a. Binary a => BinHandle -> IO a get BinHandle bh Word8 _ -> String -> SDoc -> IO SubDemand forall a. HasCallStack => String -> SDoc -> a pprPanic String "Binary:SubDemand" (Int -> SDoc forall a. Outputable a => a -> SDoc ppr (Word8 -> Int forall a b. (Integral a, Num b) => a -> b fromIntegral Word8 h :: Int)) instance Binary Divergence where put_ :: BinHandle -> Divergence -> IO () put_ BinHandle bh Divergence Dunno = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 0 put_ BinHandle bh Divergence ExnOrDiv = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 1 put_ BinHandle bh Divergence Diverges = BinHandle -> Word8 -> IO () putByte BinHandle bh Word8 2 get :: BinHandle -> IO Divergence get BinHandle bh = do Word8 h <- BinHandle -> IO Word8 getByte BinHandle bh case Word8 h of Word8 0 -> Divergence -> IO Divergence forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Divergence Dunno Word8 1 -> Divergence -> IO Divergence forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Divergence ExnOrDiv Word8 2 -> Divergence -> IO Divergence forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a return Divergence Diverges Word8 _ -> String -> SDoc -> IO Divergence forall a. HasCallStack => String -> SDoc -> a pprPanic String "Binary:Divergence" (Int -> SDoc forall a. Outputable a => a -> SDoc ppr (Word8 -> Int forall a b. (Integral a, Num b) => a -> b fromIntegral Word8 h :: Int)) instance Binary DmdEnv where -- Ignore VarEnv when spitting out the DmdType put_ :: BinHandle -> DmdEnv -> IO () put_ BinHandle bh (DE VarEnv Demand _ Divergence d) = BinHandle -> Divergence -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh Divergence d get :: BinHandle -> IO DmdEnv get BinHandle bh = VarEnv Demand -> Divergence -> DmdEnv DE VarEnv Demand forall a. VarEnv a emptyVarEnv (Divergence -> DmdEnv) -> IO Divergence -> IO DmdEnv forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO Divergence forall a. Binary a => BinHandle -> IO a get BinHandle bh instance Binary DmdType where put_ :: BinHandle -> DmdType -> IO () put_ BinHandle bh (DmdType DmdEnv fv [Demand] ds) = BinHandle -> DmdEnv -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh DmdEnv fv IO () -> IO () -> IO () forall a b. IO a -> IO b -> IO b forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b *> BinHandle -> [Demand] -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh [Demand] ds get :: BinHandle -> IO DmdType get BinHandle bh = DmdEnv -> [Demand] -> DmdType DmdType (DmdEnv -> [Demand] -> DmdType) -> IO DmdEnv -> IO ([Demand] -> DmdType) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO DmdEnv forall a. Binary a => BinHandle -> IO a get BinHandle bh IO ([Demand] -> DmdType) -> IO [Demand] -> IO DmdType forall a b. IO (a -> b) -> IO a -> IO b forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b <*> BinHandle -> IO [Demand] forall a. Binary a => BinHandle -> IO a get BinHandle bh instance Binary DmdSig where put_ :: BinHandle -> DmdSig -> IO () put_ BinHandle bh (DmdSig DmdType aa) = BinHandle -> DmdType -> IO () forall a. Binary a => BinHandle -> a -> IO () put_ BinHandle bh DmdType aa get :: BinHandle -> IO DmdSig get BinHandle bh = DmdType -> DmdSig DmdSig (DmdType -> DmdSig) -> IO DmdType -> IO DmdSig forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> BinHandle -> IO DmdType forall a. Binary a => BinHandle -> IO a get BinHandle bh