Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC

Commits:

27 changed files:

Changes:

  • changelog.d/T27962
    1
    +section: compiler
    
    2
    +issues: #27962
    
    3
    +mrs: !16869
    
    4
    +synopsis:
    
    5
    +  Fix a Lint error when using `runRW#` and `coerce` together
    
    6
    +description:
    
    7
    +  Just an internal compiler bug, of long standing.

  • changelog.d/T27963
    1
    +section: compiler
    
    2
    +issues: #27963
    
    3
    +mrs: !16869
    
    4
    +synopsis:
    
    5
    +  Fix a rather obscure interaction of `coerce` and RULES
    
    6
    +description:
    
    7
    +  A `coerce` could prevent a RULE firing.

  • compiler/GHC/Core.hs
    ... ... @@ -1494,6 +1494,157 @@ same way as above, otherwise the analysis would not be conservative wrt. the
    1494 1494
     lenient semantics (which includes the strict one). It is also much harder to
    
    1495 1495
     explain and maintain, as it turned out.
    
    1496 1496
     
    
    1497
    +Note [Core binder IdInfo invariants]
    
    1498
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1499
    +We allow the following IdInfo on different sorts of Core binders.
    
    1500
    +See the fields in GHC.Types.Id.Info.IdInfo:
    
    1501
    +
    
    1502
    +We classify binders into
    
    1503
    +  * let-binders                    let x=rhs in body
    
    1504
    +  * lambda binders                 (\x. e)
    
    1505
    +  * case binders                   case e of x { ... }
    
    1506
    +  * case-alternative binders       case e of _ { K x y -> ... }
    
    1507
    +
    
    1508
    +                         Let     Lambda   Case/case-alt
    
    1509
    +    ---------------------------------------------
    
    1510
    +    Occurrence           Yes     Yes      Yes
    
    1511
    +
    
    1512
    +    InlinePrag           Yes     No       No
    
    1513
    +    Strictness (DmdSig)  Yes     No       No
    
    1514
    +    CPR info (CprSig)    Yes     No       No
    
    1515
    +    CAF                  Yes     No       No
    
    1516
    +    Arity                Yes     No       No
    
    1517
    +    Call-arity           Yes     No       No
    
    1518
    +    Rules (RuleInfo)     Yes     No       No
    
    1519
    +    Unfolding            Yes     No(CBI1) evaldUnfolding only (CBI2)
    
    1520
    +    Demand info          Yes     Yes      Yes
    
    1521
    +    One-shot             No      Yes      No
    
    1522
    +    ------------------------------------------------
    
    1523
    +
    
    1524
    +The predicate `GHC.Types.Id.lamBndrInfoOK` checks the Lambda and Case columns
    
    1525
    +of this table (except one-shot info), and Core Lint applies it to every
    
    1526
    +non-let binder (`lintIdBndr`).
    
    1527
    +
    
    1528
    +Generally, lambda binders have only IdInfo that describes their /use/
    
    1529
    +(e.g. occ-info, demand info) not their /definition/.   See esp (CBI1).
    
    1530
    +
    
    1531
    +Case and case-alternative binders are similar because again they lack a definition,
    
    1532
    +and because they can /become/ a lambda binder when `mkDupableAlt` abstracts over them.
    
    1533
    +
    
    1534
    +Places that must take care to maintain these invariants (#27965):
    
    1535
    +
    
    1536
    +* Places that turn a let into a case, do so by calling `GHC.Core.Utils.mkCaseFromLet`,
    
    1537
    +  which in turn calls `GHC.Types.Id.mkCaseBndrFromLetBndr`, or by calling the latter
    
    1538
    +  directly:
    
    1539
    +
    
    1540
    +  - The desugarer, when desugaring a strict binding  let !x = rhs in body,
    
    1541
    +    generates  case x of x { DEFAULT -> body }; the case binder must not inherit
    
    1542
    +    x's IdInfo.  See `GHC.HsToCore.Utils.seqVar`.
    
    1543
    +
    
    1544
    +  - `GHC.Core.Utils.bindNonRec` (used by `mkCoreLet`, and hence by the
    
    1545
    +    Simplifier's `wrapFloats`), which turns  let x = rhs in body  into
    
    1546
    +    case rhs of x { DEFAULT -> body }  when `rhs` is unlifted and not
    
    1547
    +    ok-for-speculation.  Here `x` may well have a let-unfolding,
    
    1548
    +    e.g. one made by `makeTrivial`.
    
    1549
    +
    
    1550
    +  - `GHC.Core.Opt.SpecConstr.mkSeqs`, which builds
    
    1551
    +         case arg of arg' { DEFAULT -> rhs }
    
    1552
    +    where `arg` may be an imported Id with an unfolding.
    
    1553
    +
    
    1554
    +  - `GHC.CoreToStg.Prep.wrapBinds` turns a let-binding into a case-binding.
    
    1555
    +
    
    1556
    +* The Simplifier, when it uses a StrictBind continuation for a strict
    
    1557
    +  let-binding  let x = rhs in body. When duplicating `StrictBind`,
    
    1558
    +  `mkDupableContWithDmds` may turn `x` into the lambda binder of a join
    
    1559
    +  point, so it uses `mkLamBndrFromLetBndr` to do so.
    
    1560
    +  See (SB3) in Note [StrictBind] in GHC.Core.Opt.Simplify.Iteration.
    
    1561
    +
    
    1562
    +
    
    1563
    +(CBI1) Based on #17530 and #21496 we never attach unfoldings of any kind to lambda
    
    1564
    +       binders, not even `evaldUnfolding`.
    
    1565
    +
    
    1566
    +       It's just too easy for the call site to change and invalidate the unfolding.
    
    1567
    +       E.g. the caller of the lambda drops a seq (e.g. because the lambda is strict
    
    1568
    +       in its binder) which in turn makes the OtherCon[] unfolding a lie.  So
    
    1569
    +       unfoldings on lambda binders can never really be trusted when on lambda
    
    1570
    +       binders if there is the chance of the call site to change. So it's easiest to
    
    1571
    +       just never attach any to lambda binders to begin with, as well as stripping
    
    1572
    +       them off if we e.g. float out an expression while abstracting over some
    
    1573
    +       arguments.
    
    1574
    +
    
    1575
    +(CBI2) See Note [Evaluated-ness on case binders]
    
    1576
    +
    
    1577
    +(CBI3) Note that /occurrences/ may have IdInfo that is not present on the binder.
    
    1578
    +   Notably, consider  case e of r { Just x -> ..r...; Nothing -> ...r... }
    
    1579
    +   The occurrence of `r` in the (Just x) branch will have an unfolding `Just x`,
    
    1580
    +   and similarly in the `Nothing` branch.  But the /binder/ of `r` has only
    
    1581
    +   `evaldUnfolding` See Note [Evaluated-ness on case binders]
    
    1582
    +
    
    1583
    +Note [Evaluated-ness on case binders]
    
    1584
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1585
    +The binding site of an Id in a case expression (case-binder or case alternative) may
    
    1586
    +carry an `OtherCon []` (aka `evaldUnfolding`).  meaning "this variable is certainly
    
    1587
    +evaluated".  Specifically:
    
    1588
    +
    
    1589
    +* Case binders are always evaluated.  `GHC.Core.Opt.Simplify.Iteration.simplAlts` and
    
    1590
    +  CorePrep add `evaldUnfolding` to every case binder.
    
    1591
    +
    
    1592
    +* Case-alternative binders for strict constructor fields
    
    1593
    +      data T = MkT !Int Int
    
    1594
    +      case x of MkT y z -> ...
    
    1595
    +  `y` gets an evaldUnfolding.
    
    1596
    +
    
    1597
    +  This is done by `GHC.Core.Utils.dataConRepFSInstPat` (used by the interface-file loader,
    
    1598
    +  among others), `GHC.Core.SimpleOpt`, the Simplifier (`adjustFieldsIdInfo`), and
    
    1599
    +  worker/wrapper (for the argument and CPR result binders it makes;
    
    1600
    +  see Note [Record evaluated-ness in worker/wrapper] in GHC.Core.Opt.WorkWrap.Utils).
    
    1601
    +
    
    1602
    +Why is this information on the /binder/?  Within the Simplifier it is not
    
    1603
    +needed: in each case alternative the Simplifier adds a more precise unfolding
    
    1604
    +for the case binder (and evaluated-ness for strict-field binders) to its
    
    1605
    +in-scope set, and refreshes every occurrence from that.  But many other passes
    
    1606
    +rebuild variable occurrences /from the binder/: substitution-based passes
    
    1607
    +(SimpleOpt, Specialise, SpecConstr, CSE), SetLevels/FloatOut, the binder swap in
    
    1608
    +the occurrence analyser, CoreTidy, and CorePrep.  After such a pass, an
    
    1609
    +occurrence knows that the variable is evaluated only if the binder does.
    
    1610
    +
    
    1611
    +That knowledge is used, mainly via `exprIsHNF`/`varIsHNF` (not by
    
    1612
    +`exprOkForSpeculation`; see Note [exprOkForSpeculation and evaluated variables]
    
    1613
    +in GHC.Core.Utils), by:
    
    1614
    +
    
    1615
    +* CorePrep: deciding whether a let-binding is a value (allocate directly) or
    
    1616
    +  a thunk, e.g. `let x = Box y` where `y` is a strict field.
    
    1617
    +  See Note [Pin evaluatedness on floats] in GHC.CoreToStg.Prep.  CorePrep runs
    
    1618
    +  just after CoreTidy, which propagates binder IdInfo to occurrences.
    
    1619
    +
    
    1620
    +* SetLevels/FloatOut: floating single-alternative cases out of loops, when the
    
    1621
    +  scrutinee is evaluated; see Note [Floating single-alternative cases] in
    
    1622
    +  GHC.Core.Opt.SetLevels.  Also the call-by-value marks given to abstracted
    
    1623
    +  variables (`GHC.Types.Id.transferPolyIdInfo`).
    
    1624
    +
    
    1625
    +* Worker/wrapper: `move_transit_vars` avoids an unboxed 1-tuple for a single
    
    1626
    +  evaluated transit variable (test T18109); and the CPR result binders of the
    
    1627
    +  wrapper remain evaluated (tests T13077a, #13027).  The latter matters because
    
    1628
    +  unboxed-tuple alternatives take a fast path in the Simplifier's
    
    1629
    +  `adjustFieldsIdInfo`, and so get evaluated-ness only from the binder.
    
    1630
    +
    
    1631
    +* SimpleOpt: eliminating a case on an evaluated variable, and `mkFieldSeqFloats`.
    
    1632
    +
    
    1633
    +* Minor heuristic uses: FloatIn (the late FloatIn runs after FloatOut and CSE
    
    1634
    +  with no Simplifier in between) and occurrences created by CSE or the binder
    
    1635
    +  swap, until the next Simplifier run.
    
    1636
    +
    
    1637
    +Core Lint does /not/ depend on this information.  Nor does anything in STG or
    
    1638
    +tag inference.
    
    1639
    +
    
    1640
    +Removing the information from the binders would require these passes to track
    
    1641
    +evaluated-ness in an environment instead (as SpecConstr already does with its
    
    1642
    +ValueEnv), which they currently don't.  So we allow an OtherCon unfolding (and
    
    1643
    +only that) on case binders and case-alternative binders:
    
    1644
    +see the table in Note [Core binder IdInfo invariants]
    
    1645
    +
    
    1646
    +
    
    1647
    +
    
    1497 1648
     ************************************************************************
    
    1498 1649
     *                                                                      *
    
    1499 1650
                 In/Out type synonyms
    
    ... ... @@ -1843,16 +1994,6 @@ floatBinders (FloatTick {}) = []
    1843 1994
     
    
    1844 1995
     The @Unfolding@ type is declared here to avoid numerous loops
    
    1845 1996
     
    
    1846
    -Note [Never put `OtherCon` unfoldings on lambda binders]
    
    1847
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1848
    -Based on #21496 we never attach unfoldings of any kind to lambda binders.
    
    1849
    -It's just too easy for the call site to change and invalidate the unfolding.
    
    1850
    -E.g. the caller of the lambda drops a seq (e.g. because the lambda is strict in it's binder)
    
    1851
    -which in turn makes the OtherCon[] unfolding a lie.
    
    1852
    -So unfoldings on lambda binders can never really be trusted when on lambda binders if there
    
    1853
    -is the chance of the call site to change. So it's easiest to just never attach any
    
    1854
    -to lambda binders to begin with, as well as stripping them off if we e.g. float out
    
    1855
    -and expression while abstracting over some arguments.
    
    1856 1997
     -}
    
    1857 1998
     
    
    1858 1999
     -- | Records the /unfolding/ of an identifier, which is approximately the form the
    

  • compiler/GHC/Core/Lint.hs
    ... ... @@ -1858,6 +1858,15 @@ lintIdBndr top_lvl bind_site id thing_inside
    1858 1858
              hang (text "Lambda binder" <+> quotes (ppr id) <+> text "has an unfolding")
    
    1859 1859
                 2 (ppr unf)
    
    1860 1860
     
    
    1861
    +       -- Check that lambda, case, and case-alternative binders have
    
    1862
    +       -- only the IdInfo permitted by the table in
    
    1863
    +       -- Note [Core binder IdInfo invariants] in GHC.Core
    
    1864
    +       ; checkL (is_let_bind || lamBndrInfoOK id) $
    
    1865
    +         hang (text "Non-let binder" <+> quotes (ppr id)
    
    1866
    +               <+> text "has IdInfo that only a let-binder may have")
    
    1867
    +            2 (vcat [ text "Binding site:" <+> text (show_site bind_site)
    
    1868
    +                    , text "IdInfo:" <+> ppr (idInfo id) ])
    
    1869
    +
    
    1861 1870
            ; addLoc (IdTy id) (lintValueType id_ty)
    
    1862 1871
     
    
    1863 1872
            ; addInScopeId id thing_inside }
    
    ... ... @@ -1869,6 +1878,12 @@ lintIdBndr top_lvl bind_site id thing_inside
    1869 1878
                         LetBind -> True
    
    1870 1879
                         _       -> False
    
    1871 1880
     
    
    1881
    +    show_site :: BindingSite -> String
    
    1882
    +    show_site LambdaBind  = "lambda"
    
    1883
    +    show_site CaseBind    = "case binder"
    
    1884
    +    show_site CasePatBind = "case alternative"
    
    1885
    +    show_site LetBind     = "let"
    
    1886
    +
    
    1872 1887
     {-
    
    1873 1888
     %************************************************************************
    
    1874 1889
     %*                                                                      *
    

  • compiler/GHC/Core/Opt/CprAnal.hs
    ... ... @@ -343,7 +343,7 @@ cprTransform env id args
    343 343
       -- Any local binding, except for data structure bindings
    
    344 344
       -- See Note [Efficient Top sigs in SigEnv]
    
    345 345
       | Just sig <- lookupSigEnv env id
    
    346
    -  = applyCprTy (getCprSig sig) (length args)
    
    346
    +  = applyCprTy (cprSigType sig) (length args)
    
    347 347
       -- See Note [CPR for data structures]
    
    348 348
       | Just rhs <- cprDataStructureUnfolding_maybe id
    
    349 349
       = fst $ cprAnal env rhs
    
    ... ... @@ -362,7 +362,7 @@ cprTransform env id args
    362 362
       = cprTransformDataConWork env con args
    
    363 363
       -- Imported function
    
    364 364
       | otherwise
    
    365
    -  = applyCprTy (getCprSig (idCprSig id)) (length args)
    
    365
    +  = applyCprTy (cprSigType (idCprSig id)) (length args)
    
    366 366
     
    
    367 367
     -- | Precise, hand-written CPR transformers for select Ids
    
    368 368
     cprTransformBespoke :: Id -> [(CprType, CoreArg)] -> Maybe CprType
    
    ... ... @@ -870,27 +870,42 @@ Does f have the CPR property? Well, no.
    870 870
     However, if the strictness analyser has figured out (in a previous
    
    871 871
     iteration) that it's strict, then we DON'T need to forget the CPR info.
    
    872 872
     Instead we can retain the CPR info and do the thunk-splitting transform
    
    873
    -(see WorkWrap.splitThunk).
    
    873
    +(see Note [Thunk splitting] esp (B) CPR-led thunk splitting).
    
    874 874
     
    
    875 875
     This made a big difference to PrelBase.modInt, which had something like
    
    876 876
             modInt = \ x -> let r = ... -> I# v in
    
    877 877
                             ...body strict in r...
    
    878
    -r's RHS isn't a value yet; but modInt returns r in various branches, so
    
    879
    -if r doesn't have the CPR property then neither does modInt
    
    878
    +r's RHS isn't a value yet; but `modInt` returns `r` in various branches, so
    
    879
    +if `r` doesn't have the CPR property then neither does `modInt`
    
    880
    +
    
    880 881
     Another case I found in practice (in Complex.magnitude), looks like this:
    
    881
    -                let k = if ... then I# a else I# b
    
    882
    -                in ... body strict in k ....
    
    882
    +        let k* = if ... then I# a else I# b
    
    883
    +        in ... body strict in k ....
    
    883 884
     (For this example, it doesn't matter whether k is returned as part of
    
    884 885
     the overall result; but it does matter that k's RHS has the CPR property.)
    
    886
    +
    
    885 887
     Left to itself, the simplifier will make a join point thus:
    
    886
    -                let $j k = ...body strict in k...
    
    887
    -                if ... then $j (I# a) else $j (I# b)
    
    888
    +        let $j k = ...body strict in k...
    
    889
    +        if ... then $j (I# a) else $j (I# b)
    
    890
    +
    
    888 891
     With thunk-splitting, we get instead
    
    889
    -                let $j x = let k = I#x in ...body strict in k...
    
    890
    -                in if ... then $j a else $j b
    
    892
    +        let k* = case (if ... then I# a else I# b) of I# z -> I# z
    
    893
    +        in ... body strict in k ....
    
    894
    +Now the simplifier should pull out the `case` to give
    
    895
    +        case (if ... then I# a else I# b) of I# z ->
    
    896
    +        let k = I# z in ... body strict in k ...
    
    897
    +Now case-of-if will give
    
    898
    +        let $j z = let k = I# z in ...body strict in k... in
    
    899
    +        if ... then $j a else $j b
    
    891 900
     This is much better; there's a good chance the I# won't get allocated.
    
    901
    +TL'DR: During CPR analysis we want to give `k` the CPR property in `body`.
    
    902
    +
    
    903
    +What if the type involved has more than one constructor?  E.g.
    
    904
    +        let k* = if ... then Just a else Just b
    
    905
    +        in ... body strict in k ....
    
    906
    +That is good too: see (TS5) in Note [Thunk splitting]
    
    892 907
     
    
    893
    -But what about botCpr? Consider
    
    908
    +Finally, what about botCpr? Consider
    
    894 909
         lvl = error "boom"
    
    895 910
         fac -1 = lvl
    
    896 911
         fac 0 = 1
    

  • compiler/GHC/Core/Opt/Simplify/Env.hs
    ... ... @@ -28,7 +28,7 @@ module GHC.Core.Opt.Simplify.Env (
    28 28
             SimplEnvIS,  checkSimplEnvIS, pprBadSimplEnvIS,
    
    29 29
     
    
    30 30
             -- * Substitution results
    
    31
    -        SimplSR(..), substId, lookupRecBndr,
    
    31
    +        SimplClo(..), simplCloExpr, substId, lookupRecBndr,
    
    32 32
     
    
    33 33
             -- * Simplifying 'Id' binders
    
    34 34
             simplNonRecBndr, simplNonRecJoinBndr, simplRecBndrs, simplRecJoinBndrs,
    
    ... ... @@ -61,7 +61,7 @@ import GHC.Core.Opt.Simplify.Monad
    61 61
     import GHC.Core.Rules.Config ( RuleOpts(..) )
    
    62 62
     import GHC.Core
    
    63 63
     import GHC.Core.Utils
    
    64
    -import GHC.Core.Subst( substExprSC )
    
    64
    +import GHC.Core.Subst( substExpr )
    
    65 65
     import GHC.Core.Unfold
    
    66 66
     import GHC.Core.TyCo.Subst (emptyIdSubstEnv, mkSubst)
    
    67 67
     import GHC.Core.Multiplicity( Scaled(..), mkMultMul )
    
    ... ... @@ -470,18 +470,26 @@ pprSimplEnv env
    470 470
        ppr_one v | isId v = ppr v <+> ppr (idUnfolding v)
    
    471 471
                  | otherwise = ppr v
    
    472 472
     
    
    473
    -type SimplIdSubst = IdEnv SimplSR -- IdId |--> OutExpr
    
    473
    +type SimplIdSubst = IdEnv SimplClo -- IdId |--> OutExpr
    
    474 474
             -- See Note [Extending the IdSubstEnv] in GHC.Core.Subst
    
    475 475
     
    
    476
    --- | A substitution result.
    
    477
    -data SimplSR
    
    476
    +-- | A "closure" used in the Simplifier.  It is used
    
    477
    +--     * as the range of the substitution, 'SimplIdSubst'
    
    478
    +--     * as the argument in an 'ApplyToVal' continuation
    
    479
    +-- Roughly: either an (InExpr, StaticEnv) pair for an
    
    480
    +--                 as-yet-unsimplified expression
    
    481
    +--          or     an OutExpr, for an already-simplified one
    
    482
    +-- It's a bit like thunk: we may "evaluate" the SimplClo in a ApplyToVal
    
    483
    +--   and thereby transform it from `ContEx` to `DoneEx`
    
    484
    +--   This happens in the `ApplyToVal` case of `mkDupableContWithDmds`
    
    485
    +--   See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils
    
    486
    +data SimplClo
    
    478 487
       = DoneEx OutExpr JoinPointHood
    
    479 488
            -- If  x :-> DoneEx e ja   is in the SimplIdSubst
    
    480 489
            -- then replace occurrences of x by e
    
    481 490
            -- and  ja = Just a <=> x is a join-point of arity a
    
    482 491
            -- See Note [Join arity in SimplIdSubst]
    
    483 492
     
    
    484
    -
    
    485 493
       | DoneId OutId
    
    486 494
            -- If  x :-> DoneId v   is in the SimplIdSubst
    
    487 495
            -- then replace occurrences of x by v
    
    ... ... @@ -490,11 +498,11 @@ data SimplSR
    490 498
     
    
    491 499
       | ContEx SimplEnv
    
    492 500
                InExpr
    
    493
    -           MOutCoercion  -- See Note [The sc_cast field of ApplyToVal]
    
    501
    +           MOutCoercion  -- See Note [The MOutCoercion field of ContEx]
    
    494 502
           -- If   x :-> ContEx static_env e mco   is in the SimplISubst
    
    495 503
           -- then replace occurrences of x by (subst static_env e) |> mco
    
    496 504
     
    
    497
    -instance Outputable SimplSR where
    
    505
    +instance Outputable SimplClo where
    
    498 506
       ppr (DoneId v)    = text "DoneId" <+> ppr v
    
    499 507
       ppr (DoneEx e mj) = text "DoneEx" <> pp_mj <+> ppr e
    
    500 508
         where
    
    ... ... @@ -637,7 +645,7 @@ reSimplifying :: SimplEnv -> Bool
    637 645
     reSimplifying (SimplEnv { seInlineDepth = n }) = n>0
    
    638 646
     
    
    639 647
     ---------------------
    
    640
    -extendIdSubst :: SimplEnv -> Id -> SimplSR -> SimplEnv
    
    648
    +extendIdSubst :: SimplEnv -> Id -> SimplClo -> SimplEnv
    
    641 649
     extendIdSubst env@(SimplEnv {seIdSubst = subst}) var res
    
    642 650
       = assertPpr (isId var && not (isCoVar var)) (ppr var) $
    
    643 651
         env { seIdSubst = extendVarEnv subst var res }
    
    ... ... @@ -1032,7 +1040,7 @@ So we want to look up the inner X.g_34 in the substitution, where we'll
    1032 1040
     find that it has been substituted by b.  (Or conceivably cloned.)
    
    1033 1041
     -}
    
    1034 1042
     
    
    1035
    -substId :: SimplEnv -> InId -> SimplSR
    
    1043
    +substId :: SimplEnv -> InId -> SimplClo
    
    1036 1044
     -- Returns DoneEx only on a non-Var expression
    
    1037 1045
     substId (SimplEnv { seInScope = in_scope, seIdSubst = ids }) v
    
    1038 1046
       = case lookupVarEnv ids v of  -- Note [Global Ids in the substitution]
    
    ... ... @@ -1364,15 +1372,18 @@ getTCvSubst (SimplEnv { seInScope = in_scope, seTvSubst = tv_env, seCvSubst = cv
    1364 1372
     
    
    1365 1373
     getFullSubst :: InScopeSet -> SimplEnv -> Subst
    
    1366 1374
     getFullSubst in_scope (SimplEnv { seIdSubst = id_env, seTvSubst = tv_env, seCvSubst = cv_env })
    
    1367
    -  = mkSubst in_scope (mapVarEnv to_expr id_env) tv_env cv_env
    
    1375
    +  = mkSubst in_scope (mapVarEnv (simplCloExpr in_scope) id_env) tv_env cv_env
    
    1376
    +
    
    1377
    +simplCloExpr :: InScopeSet -> SimplClo -> OutExpr
    
    1378
    +-- Turn a SimplClo into an OutExpr, by applying the substitution
    
    1379
    +-- (if any) and the pending cast (if any)
    
    1380
    +simplCloExpr _        (DoneEx e _)       = e
    
    1381
    +simplCloExpr _        (DoneId v)         = Var v
    
    1382
    +simplCloExpr in_scope (ContEx env e mco) = mkCastMCo e' mco
    
    1368 1383
       where
    
    1369
    -    to_expr :: SimplSR -> CoreExpr
    
    1370
    -    -- A tiresome impedence-matcher
    
    1371
    -    to_expr (DoneEx e _)       = e
    
    1372
    -    to_expr (DoneId v)         = Var v
    
    1373
    -    to_expr (ContEx env e mco) = mkCastMCo e' mco
    
    1374
    -      where
    
    1375
    -        e' = GHC.Core.Subst.substExprSC (getFullSubst in_scope env) e
    
    1384
    +    e' = GHC.Core.Subst.substExpr (getFullSubst in_scope env) e
    
    1385
    +    -- NB: we use substExpr, not substExprSC: we want to get the benefit of
    
    1386
    +    --     knowing what is evaluated etc, via the in-scope set
    
    1376 1387
     
    
    1377 1388
     substTy :: HasDebugCallStack => SimplEnv -> Type -> Type
    
    1378 1389
     substTy env ty = Type.substTy (getTCvSubst env) ty
    

  • compiler/GHC/Core/Opt/Simplify/Iteration.hs
    ... ... @@ -285,8 +285,8 @@ simplRecOrTopPair :: SimplEnv
    285 285
                       -> SimplM (SimplFloats, SimplEnv)
    
    286 286
     
    
    287 287
     simplRecOrTopPair env bind_cxt old_bndr new_bndr rhs
    
    288
    -  | Just env' <- preInlineUnconditionally env (bindContextLevel bind_cxt)
    
    289
    -                                          old_bndr (UnSimplified env) rhs MRefl
    
    288
    +  | Just env' <- preInlineLetUnconditionally env (bindContextLevel bind_cxt)
    
    289
    +                                             old_bndr rhs env
    
    290 290
       = {-#SCC "simplRecOrTopPair-pre-inline-uncond" #-}
    
    291 291
         simplTrace "SimplBindr:inline-uncond1" (ppr old_bndr) $
    
    292 292
         do { tick (PreInlineUnconditionally old_bndr)
    
    ... ... @@ -1226,17 +1226,8 @@ simplExprF1 env (Tick t expr) cont = {-#SCC "simplTick" #-} simplTick env t exp
    1226 1226
     simplExprF1 env (Coercion co)  cont = {-#SCC "simplCoercionF" #-} simplCoercionF env co cont
    
    1227 1227
     
    
    1228 1228
     simplExprF1 env (Cast body co) cont
    
    1229
    -  = do  { co1 <- {-#SCC "simplCast-simplCoercion" #-}
    
    1230
    -                 simplCoercion env co
    
    1231
    -
    
    1232
    -        ; let cont1 = {-#SCC "simplCast-addCoerce" #-}
    
    1233
    -                      if isReflCo co1
    
    1234
    -                      then cont  -- See Note [Optimising reflexivity]
    
    1235
    -                      else pushCast env co1 cont
    
    1236
    -                           -- True <=> co1 is optimised
    
    1237
    -
    
    1238
    -        ; {-#SCC "simplCast-simplExprF" #-}
    
    1239
    -          simplExprF env body cont1 }
    
    1229
    +  = do  { co1 <- simplCoercion env co
    
    1230
    +        ; simplExprF env body (pushCast env co1 cont) }
    
    1240 1231
     
    
    1241 1232
     simplExprF1 env (App fun arg) cont
    
    1242 1233
       = {-#SCC "simplExprF1-App" #-} case arg of
    
    ... ... @@ -1264,9 +1255,9 @@ simplExprF1 env (App fun arg) cont
    1264 1255
               -- observed the quadratic behavior, so this extra entanglement
    
    1265 1256
               -- seems not worthwhile.
    
    1266 1257
             simplExprF env fun $
    
    1267
    -        ApplyToVal { sc_arg = arg, sc_env = UnSimplified env
    
    1258
    +        ApplyToVal { sc_arg = ContEx env arg MRefl, sc_dup = NoDup
    
    1268 1259
                        , sc_hole_ty = substTy env (exprType fun)
    
    1269
    -                   , sc_cast = MRefl, sc_cont = cont }
    
    1260
    +                   , sc_cont = cont }
    
    1270 1261
     
    
    1271 1262
     simplExprF1 env expr@(Lam {}) cont
    
    1272 1263
       = {-#SCC "simplExprF1-Lam" #-}
    
    ... ... @@ -1301,7 +1292,7 @@ simplExprF1 env (Let (NonRec bndr rhs) body) cont
    1301 1292
         do { ty' <- simplType env ty
    
    1302 1293
            ; simplExprF (extendTvSubst env bndr ty') body cont }
    
    1303 1294
     
    
    1304
    -  | Just env' <- preInlineUnconditionally env NotTopLevel bndr (UnSimplified env) rhs MRefl
    
    1295
    +  | Just env' <- preInlineLetUnconditionally env NotTopLevel bndr rhs env
    
    1305 1296
         -- Because of the let-can-float invariant, it's ok to
    
    1306 1297
         -- inline freely, or to drop the binding if it is dead.
    
    1307 1298
       = do { simplTrace "SimplBindr:inline-uncond2" (ppr bndr) $
    
    ... ... @@ -1637,9 +1628,8 @@ rebuild_go env expr cont
    1637 1628
           ApplyToTy  { sc_arg_ty = ty, sc_cont = cont}
    
    1638 1629
             -> rebuild_go env (App expr (Type ty)) cont
    
    1639 1630
     
    
    1640
    -      ApplyToVal { sc_arg = arg, sc_env = arg_se, sc_cast = arg_mco
    
    1641
    -                 , sc_cont = cont, sc_hole_ty = fun_ty }
    
    1642
    -        -> do { arg' <- simplArg env Nothing fun_ty arg_se arg arg_mco
    
    1631
    +      ApplyToVal { sc_arg = arg, sc_cont = cont, sc_hole_ty = fun_ty }
    
    1632
    +        -> do { arg' <- simplArg env Nothing fun_ty arg
    
    1643 1633
                   ; rebuild_go env (App expr arg') cont }
    
    1644 1634
     
    
    1645 1635
     completeBindX :: SimplEnv
    
    ... ... @@ -1653,6 +1643,7 @@ completeBindX env from_what bndr rhs body cont
    1653 1643
       | FromBeta arg_levity <- from_what
    
    1654 1644
       , needsCaseBindingL arg_levity rhs -- Enforcing the let-can-float-invariant
    
    1655 1645
       = do { (env1, bndr1)   <- simplNonRecBndr env bndr  -- Lambda binders don't have rules
    
    1646
    +                            -- See Note [Core binder IdInfo invariants] in GHC.Core
    
    1656 1647
            ; (floats, expr') <- simplNonRecBody env1 from_what body cont
    
    1657 1648
            -- Do not float floats past the Case binder below
    
    1658 1649
            ; let expr'' = wrapFloats floats expr'
    
    ... ... @@ -1686,69 +1677,12 @@ completeBindX env from_what bndr rhs body cont
    1686 1677
             ; let all_floats = rhs_floats `addFloats` bind_float `addFloats` body_floats
    
    1687 1678
             ; return ( all_floats, body' ) }
    
    1688 1679
     
    
    1689
    -{-
    
    1690
    -************************************************************************
    
    1680
    +
    
    1681
    +{- *********************************************************************
    
    1691 1682
     *                                                                      *
    
    1692
    -\subsection{Lambdas}
    
    1683
    +                Arguments
    
    1693 1684
     *                                                                      *
    
    1694
    -************************************************************************
    
    1695
    --}
    
    1696
    -
    
    1697
    -{- Note [Optimising reflexivity]
    
    1698
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1699
    -It's important (for compiler performance) to get rid of reflexivity as soon
    
    1700
    -as it appears.  See #11735, #14737, and #15019.
    
    1701
    -
    
    1702
    -In particular, we want to behave well on
    
    1703
    -
    
    1704
    - *  e |> co1 |> co2
    
    1705
    -    where the two happen to cancel out entirely. That is quite common;
    
    1706
    -    e.g. a newtype wrapping and unwrapping cancel.
    
    1707
    -
    
    1708
    -
    
    1709
    - * (f |> co) @t1 @t2 ... @tn x1 .. xm
    
    1710
    -   Here we will use pushCoTyArg and pushCoValArg successively, which
    
    1711
    -   build up SelCo stacks.  Silly to do that if co is reflexive.
    
    1712
    -
    
    1713
    -However, we don't want to call isReflexiveCo too much, because it uses
    
    1714
    -type equality which is expensive on big types (#14737 comment:7).
    
    1715
    -
    
    1716
    -A good compromise (determined experimentally) seems to be to call
    
    1717
    -isReflexiveCo
    
    1718
    - * when composing casts, and
    
    1719
    - * at the end
    
    1720
    -
    
    1721
    -In investigating this I saw missed opportunities for on-the-fly
    
    1722
    -coercion shrinkage. See #15090.
    
    1723
    -
    
    1724
    -Note [Avoid re-simplifying coercions]
    
    1725
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1726
    -In some benchmarks (with deeply nested cases) we successively push
    
    1727
    -casts onto the SimplCont.  We don't want to call the coercion optimiser
    
    1728
    -on each successive composition -- that's at least quadratic.  So:
    
    1729
    -
    
    1730
    -* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
    
    1731
    -  record whether the coercion optimiser has been applied to the coercion.
    
    1732
    -
    
    1733
    -* In `simplCast`, when we see (Cast e co), we simplify `co` to get
    
    1734
    -  an OutCoercion, and built a CastIt with sc_opt=True.
    
    1735
    -
    
    1736
    -  Actually not quite: if we are simplifying the result of inlining an
    
    1737
    -  unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
    
    1738
    -  just /substitute/ which is cheaper.  See `simplCoercion`.
    
    1739
    -
    
    1740
    -* In `pushCast` if we combine this new coercion with an existing one,
    
    1741
    -  we build a CastIt for (co1 ; co2) with sc_opt=False.
    
    1742
    -
    
    1743
    -* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
    
    1744
    -  composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
    
    1745
    -
    
    1746
    -* When duplicating a continuation in `mkDupableContWithDmds`, before
    
    1747
    -  duplicating a CastIt, optimise the coercion. Otherwise we'll end up
    
    1748
    -  optimising it separately in the duplicate copies.
    
    1749
    -
    
    1750
    -* See also Note [The sc_cast field of ApplyToVal] in GHC.Core.Opt.Simplify.Utils.
    
    1751
    --}
    
    1685
    +********************************************************************* -}
    
    1752 1686
     
    
    1753 1687
     simplArg :: SimplEnvIS              -- ^ Used only for its InScopeSet
    
    1754 1688
              -> Maybe ArgInfo           -- ^ Just <=> This arg `ai` occurs in an app
    
    ... ... @@ -1756,15 +1690,12 @@ simplArg :: SimplEnvIS -- ^ Used only for its InScopeSet
    1756 1690
                                         --   how `f` uses `ai`, affecting the Stop
    
    1757 1691
                                         --   continuation passed to 'simplExprC'
    
    1758 1692
              -> OutType                 -- ^ Type of the function applied to this arg
    
    1759
    -         -> StaticEnv -> CoreExpr   -- ^ Expression with its static envt
    
    1760
    -         -> MOutCoercion            -- Wrap this around the result
    
    1693
    +         -> SimplClo                -- ^ The argument
    
    1761 1694
              -> SimplM OutExpr
    
    1762
    -simplArg _ _ _ (Simplified {}) arg mco
    
    1763
    -  = -- See Note [Avoid repeated simplification]
    
    1764
    -    case mco of
    
    1765
    -       MRefl  -> return arg       -- Vastly common case
    
    1766
    -       MCo co -> return (mkCast arg co)
    
    1767
    -simplArg env mb_arg_info fun_ty (UnSimplified arg_se) arg mco
    
    1695
    +-- See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils
    
    1696
    +simplArg _ _ _ (DoneEx arg _) = return arg  -- See Note [Avoid repeated simplification]
    
    1697
    +simplArg _ _ _ (DoneId v)     = return (Var v)
    
    1698
    +simplArg env mb_arg_info fun_ty (ContEx arg_se arg mco)
    
    1768 1699
       = do { let arg_env' = arg_se `setInScopeFromE` env
    
    1769 1700
                  arg_ty   = funArgTy fun_ty
    
    1770 1701
                  stop     = case mb_arg_info of
    
    ... ... @@ -1773,13 +1704,12 @@ simplArg env mb_arg_info fun_ty (UnSimplified arg_se) arg mco
    1773 1704
                  cont     = addCastMCo mco stop
    
    1774 1705
            ; simplExprC arg_env' arg cont }
    
    1775 1706
     
    
    1776
    -{-
    
    1777
    -************************************************************************
    
    1707
    +
    
    1708
    +{- *********************************************************************
    
    1778 1709
     *                                                                      *
    
    1779
    -\subsection{Lambdas}
    
    1710
    +                Lambdas
    
    1780 1711
     *                                                                      *
    
    1781
    -************************************************************************
    
    1782
    --}
    
    1712
    +********************************************************************* -}
    
    1783 1713
     
    
    1784 1714
     simplNonRecBody :: SimplEnv -> FromWhat
    
    1785 1715
                     -> InExpr -> SimplCont
    
    ... ... @@ -1806,13 +1736,9 @@ simpl_lam env bndr body (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = cont })
    1806 1736
     
    
    1807 1737
     -- Value beta-reduction
    
    1808 1738
     -- This works for /coercion/ lambdas too
    
    1809
    -simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_env = arg_se
    
    1810
    -                                    , sc_cast = arg_mco, sc_cont = cont
    
    1739
    +simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_cont = cont
    
    1811 1740
                                         , sc_hole_ty = fun_ty})
    
    1812
    -  | Just env' <- preInlineUnconditionally env NotTopLevel bndr arg_se arg arg_mco
    
    1813
    -  , not (needsCaseBindingL arg_levity arg)
    
    1814
    -              -- Ok to test arg::InExpr in needsCaseBinding because
    
    1815
    -              -- exprOkForSpeculation is stable under simplification
    
    1741
    +  | Just env' <- preInlineBetaUnconditionally env arg_levity bndr arg
    
    1816 1742
       = simplTrace "SimplBindr:inline-uncond3"
    
    1817 1743
              (ppr bndr <+> text ":=" <+> ppr arg) $
    
    1818 1744
         do { tick (BetaReduction bndr)
    
    ... ... @@ -1823,14 +1749,14 @@ simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_env = arg_se
    1823 1749
       = do { tick (BetaReduction bndr)
    
    1824 1750
            ; let from_what = FromBeta arg_levity
    
    1825 1751
     
    
    1826
    -       ; case (arg_se, arg_mco) of
    
    1827
    -           (UnSimplified arg_env, MRefl)
    
    1752
    +       ; case arg of
    
    1753
    +           ContEx arg_env arg_expr MRefl
    
    1828 1754
                  -- Optimisation: when there is no wrapping coercion use simplNonRecE
    
    1829 1755
                  -- so that the clever floating out of let-bindings can take place
    
    1830 1756
                  -- in this round of the Simplifier
    
    1831
    -             -> simplNonRecE env from_what bndr (arg, arg_env) body cont
    
    1757
    +             -> simplNonRecE env from_what bndr (arg_expr, arg_env) body cont
    
    1832 1758
     
    
    1833
    -           _ -> do { arg' <- simplArg env Nothing fun_ty arg_se arg arg_mco
    
    1759
    +           _ -> do { arg' <- simplArg env Nothing fun_ty arg
    
    1834 1760
                        ; completeBindX env from_what bndr arg' body cont }
    
    1835 1761
            }
    
    1836 1762
       where
    
    ... ... @@ -1869,7 +1795,7 @@ simplLamBndr :: SimplEnv -> InBndr -> SimplM (SimplEnv, OutBndr)
    1869 1795
     -- could have a stable unfolding;
    
    1870 1796
     -- see Historical Note [Case binders and join points]
    
    1871 1797
     -- But now it is much simpler! We now only remove unfoldings.
    
    1872
    --- See Note [Never put `OtherCon` unfoldings on lambda binders]
    
    1798
    +-- See (CBI1) in Note [Core binder IdInfo invariants]
    
    1873 1799
     simplLamBndr env bndr = simplBinder env (zapIdUnfolding bndr)
    
    1874 1800
     
    
    1875 1801
     simplLamBndrs :: SimplEnv -> [InBndr] -> SimplM (SimplEnv, [OutBndr])
    
    ... ... @@ -1902,8 +1828,8 @@ simplNonRecE :: HasDebugCallStack
    1902 1828
     -- Otherwise it may or may not satisfy it.
    
    1903 1829
     
    
    1904 1830
     simplNonRecE env from_what bndr (rhs, rhs_se) body cont
    
    1905
    -  | assert (isId bndr && not (isJoinId bndr) ) $
    
    1906
    -    is_strict_bind
    
    1831
    +  | assertPpr (isId bndr && not (isJoinId bndr)) (ppr bndr) True
    
    1832
    +  , useStrictBind env from_what bndr rhs
    
    1907 1833
       = -- Evaluate RHS strictly
    
    1908 1834
         simplExprF (rhs_se `setInScopeFromE` env) rhs
    
    1909 1835
                    (StrictBind { sc_bndr = bndr, sc_body = body, sc_from = from_what
    
    ... ... @@ -1917,17 +1843,6 @@ simplNonRecE env from_what bndr (rhs, rhs_se) body cont
    1917 1843
            ; (floats2, expr') <- simplNonRecBody env3 from_what body cont
    
    1918 1844
            ; return (floats1 `addFloats` floats2, expr') }
    
    1919 1845
     
    
    1920
    -  where
    
    1921
    -    is_strict_bind = case from_what of
    
    1922
    -       FromBeta Unlifted -> True
    
    1923
    -       -- If we are coming from a beta-reduction (FromBeta) we must
    
    1924
    -       -- establish the let-can-float invariant, so go via StrictBind
    
    1925
    -       -- If not, the invariant holds already, and it's optional.
    
    1926
    -
    
    1927
    -       -- (FromBeta Lifted) or FromLet: look at the demand info
    
    1928
    -       _ -> seCaseCase env && isStrUsedDmd (idDemandInfo bndr)
    
    1929
    -
    
    1930
    -
    
    1931 1846
     ------------------
    
    1932 1847
     simplRecE :: SimplEnv
    
    1933 1848
               -> [(InId, InExpr)]
    
    ... ... @@ -1948,6 +1863,48 @@ simplRecE env pairs body cont
    1948 1863
             ; (floats2, expr') <- simplExprF env2 body cont
    
    1949 1864
             ; return (floats1 `addFloats` floats2, expr') }
    
    1950 1865
     
    
    1866
    +
    
    1867
    +useStrictBind :: SimplEnv -> FromWhat -> InId -> InExpr -> Bool
    
    1868
    +-- Can we use a StrictBind continuation for this let-binding?
    
    1869
    +-- See (SB3) in Note [StrictBind]
    
    1870
    +useStrictBind env from_what bndr rhs
    
    1871
    +  -- If we are coming from a beta-reduction (FromBeta) we must
    
    1872
    +  -- establish the let-can-float invariant, so we must go via StrictBind
    
    1873
    +  -- If not, the invariant holds already, and it's optional.
    
    1874
    +  | FromBeta Unlifted <- from_what
    
    1875
    +  = True
    
    1876
    +
    
    1877
    +  -- Not if the demand is non-strict
    
    1878
    +  | not (isStrUsedDmd dmd_info)
    
    1879
    +  = False
    
    1880
    +
    
    1881
    +  -- Not if the RHS is ok-for-spec; no benefit
    
    1882
    +  | exprOkForSpeculation rhs
    
    1883
    +  = False
    
    1884
    +
    
    1885
    +  -- Not if case-of-case is off
    
    1886
    +  -- See (SB1) in Note [StrictBind]
    
    1887
    +  | not (seCaseCase env)
    
    1888
    +  = False
    
    1889
    +
    
    1890
    +  | FromBeta Lifted <- from_what
    
    1891
    +  = True
    
    1892
    +
    
    1893
    +  -- Not for things with RULES or INLINE or NOINLINE
    
    1894
    +  -- or with a strictnes or CPR signature -- all things we might lose
    
    1895
    +  -- See (SB3) in Note [StrictBind]
    
    1896
    +  | isStableUnfolding (unfoldingInfo info)
    
    1897
    +    || not (isEmptyRuleInfo (ruleInfo info))
    
    1898
    +    || not (isAlwaysActive (inlinePragmaActivation (inlinePragInfo info)))
    
    1899
    +  = False
    
    1900
    +
    
    1901
    +  | otherwise
    
    1902
    +  = True
    
    1903
    +
    
    1904
    +  where
    
    1905
    +    info     = idInfo bndr
    
    1906
    +    dmd_info = demandInfo info
    
    1907
    +
    
    1951 1908
     {- Note [Dark corner with representation polymorphism]
    
    1952 1909
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1953 1910
     In `simplNonRecE`, the call to `needsCaseBinding` or to `isStrictId` will fail
    
    ... ... @@ -2262,11 +2219,14 @@ simplOutId env fun cont
    2262 2219
       | fun `hasKey` runRWKey
    
    2263 2220
       , ApplyToTy  { sc_cont = cont1 } <- cont
    
    2264 2221
       , ApplyToTy  { sc_cont = cont2, sc_arg_ty = hole_ty } <- cont1
    
    2265
    -  , ApplyToVal { sc_cont = cont3, sc_arg = arg, sc_cast = arg_mco
    
    2266
    -               , sc_env = arg_se, sc_hole_ty = fun_ty } <- cont2
    
    2222
    +  , ApplyToVal { sc_cont = cont3, sc_arg = arg_clo, sc_hole_ty = fun_ty } <- cont2
    
    2267 2223
       -- Do this even if (contIsStop cont), or if seCaseCase is off.
    
    2268 2224
       -- See Note [No eta-expansion in runRW#]
    
    2269
    -  = do { let (_saf,arg_env) = mkAltEnv env arg_se
    
    2225
    +  = do { let (arg_env, arg, arg_mco)
    
    2226
    +                = case arg_clo of
    
    2227
    +                    ContEx se e mco -> (se `setInScopeFromE` env, e, mco)
    
    2228
    +                    DoneEx e _      -> (env, e,     MRefl)
    
    2229
    +                    DoneId v        -> (env, Var v, MRefl)
    
    2270 2230
     
    
    2271 2231
                  overall_res_ty = contResultType cont3
    
    2272 2232
                  -- hole_ty is the type of the current runRW# application
    
    ... ... @@ -2281,23 +2241,32 @@ simplOutId env fun cont
    2281 2241
            --    * If we don't do this we get a beta-redex every time, so the
    
    2282 2242
            --      simplifier keeps doing more iterations.
    
    2283 2243
            --    * Even more important: see Note [No eta-expansion in runRW#]
    
    2244
    +       --
    
    2245
    +       -- But only if there is no pending cast on the argument (#27962);
    
    2246
    +       -- if there is, the general case below deals with it properly
    
    2284 2247
            ; arg' <- case arg of
    
    2285
    -           Lam s body -> do { (env', s') <- simplBinder arg_env s
    
    2286
    -                            ; body' <- simplExprC env' body inner_cont
    
    2287
    -                            ; return (Lam s' body') }
    
    2288
    -                            -- Important: do not try to eta-expand this lambda
    
    2289
    -                            -- See Note [No eta-expansion in runRW#]
    
    2248
    +           Lam s body
    
    2249
    +             | MRefl <- arg_mco
    
    2250
    +             -> do { (env', s') <- simplBinder arg_env s
    
    2251
    +                   ; body' <- simplExprC env' body inner_cont
    
    2252
    +                   ; return (Lam s' body') }
    
    2253
    +                   -- Important: do not try to eta-expand this lambda
    
    2254
    +                   -- See Note [No eta-expansion in runRW#]
    
    2290 2255
     
    
    2291 2256
                _ -> do { s' <- newId (fsLit "s") ManyTy realWorldStatePrimTy
    
    2292 2257
                        ; let (m,_,_) = splitFunTy fun_ty
    
    2293 2258
                              env'  = arg_env `addNewInScopeIds` [s']
    
    2294 2259
                              hole_ty = mkVisFunTy m realWorldStatePrimTy new_runrw_res_ty
    
    2295 2260
                              cont' = pushCastMCo env' arg_mco $
    
    2296
    -                                 ApplyToVal { sc_arg = Var s', sc_cast = MRefl
    
    2297
    -                                            , sc_env = Simplified OkDup
    
    2261
    +                                 ApplyToVal { sc_arg  = DoneId s'
    
    2262
    +                                            , sc_dup  = dup
    
    2298 2263
                                                 , sc_cont = inner_cont
    
    2299 2264
                                                 , sc_hole_ty = hole_ty }
    
    2300 2265
                                      -- cont' applies to s', then K
    
    2266
    +                         -- The argument (Var s') is small, but inner_cont may not be
    
    2267
    +                         -- dupable; see (AV2) in Note [The argument of ApplyToVal]
    
    2268
    +                         dup | contIsDupable inner_cont = OkDup
    
    2269
    +                             | otherwise                = NoDup
    
    2301 2270
                        ; body' <- simplExprC env' arg cont'
    
    2302 2271
                        ; return (Lam s' body') }
    
    2303 2272
     
    
    ... ... @@ -2364,13 +2333,12 @@ rebuildCall env info (ApplyToTy { sc_arg_ty = arg_ty, sc_hole_ty = hole_ty, sc_c
    2364 2333
     
    
    2365 2334
     ---------- Simplify value arguments --------------------
    
    2366 2335
     rebuildCall env fun_info
    
    2367
    -            (ApplyToVal { sc_arg = arg, sc_env = arg_se, sc_cast = arg_mco
    
    2368
    -                        , sc_hole_ty = fun_ty, sc_cont = cont })
    
    2369
    -  | UnSimplified arg_env <- arg_se
    
    2336
    +            (ApplyToVal { sc_arg = arg, sc_hole_ty = fun_ty, sc_cont = cont })
    
    2337
    +  | ContEx arg_env arg_expr arg_mco <- arg
    
    2370 2338
       , isStrictArgInfo fun_info  -- Strict arguments
    
    2371 2339
       , seCaseCase env            -- But only when case-of-case is on.
    
    2372 2340
                                   -- See Note [Case-of-case and full laziness]
    
    2373
    -  = simplExprF (arg_env `setInScopeFromE` env) arg $
    
    2341
    +  = simplExprF (arg_env `setInScopeFromE` env) arg_expr $
    
    2374 2342
         addCastMCo arg_mco $
    
    2375 2343
         StrictArg { sc_fun = fun_info, sc_fun_ty = fun_ty
    
    2376 2344
                   , sc_dup = NoDup
    
    ... ... @@ -2382,7 +2350,7 @@ rebuildCall env fun_info
    2382 2350
       -- There is no benefit (unlike in a let-binding), and we'd
    
    2383 2351
       -- have to be very careful about bogus strictness through
    
    2384 2352
       -- floating a demanded let.
    
    2385
    -  = do  { arg' <- simplArg env (Just fun_info) fun_ty arg_se arg arg_mco
    
    2353
    +  = do  { arg' <- simplArg env (Just fun_info) fun_ty arg
    
    2386 2354
             ; rebuildCall env (addValArgTo fun_info  arg' fun_ty) cont }
    
    2387 2355
     
    
    2388 2356
     ---------- No further useful info, revert to generic rebuild ------------
    
    ... ... @@ -2530,11 +2498,11 @@ Then
    2530 2498
      * We simplify BIG before trying the rule; but the rule does not fire
    
    2531 2499
        (forcing this simplification is why we have the RULE in this example)
    
    2532 2500
      * We inline f = \x. g x, in `simpl_lam`
    
    2533
    - * So if `simpl_lam` did preInlineUnconditionally we get (g BIG)
    
    2501
    + * So if `simpl_lam` did preInlineBetaUnconditionally we get (g BIG)
    
    2534 2502
      * Now if g has a RULE we'll simplify BIG again, and this whole thing can
    
    2535 2503
        iterate.
    
    2536 2504
      * However, if `f` did not have a RULE, so that BIG has /not/ already been
    
    2537
    -   simplified, we /want/ to do preInlineUnconditionally in simpl_lam.
    
    2505
    +   simplified, we /want/ to do preInlineBetaUnconditionally in simpl_lam.
    
    2538 2506
     
    
    2539 2507
     So we go to some effort to avoid repeatedly simplifying the same thing.
    
    2540 2508
     Suppose we are in the Plan (AFTER) case of rule application, for a rule
    
    ... ... @@ -2543,8 +2511,9 @@ and a call
    2543 2511
           f (e1,e2)
    
    2544 2512
     Then:
    
    2545 2513
     
    
    2546
    -* The (sc_env :: StaticEnv) field of ApplyToVal records if the argument
    
    2547
    -  has been evaluated.  See Note [StaticEnv] in GHC.Core.Opt.Simplify.Utils.
    
    2514
    +* The (sc_arg :: SimplClo) field of ApplyToVal records if the argument
    
    2515
    +  has been simplified: it is a DoneEx if so, and a ContEx if not.
    
    2516
    +  See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils.
    
    2548 2517
     
    
    2549 2518
     * The rule fires returning
    
    2550 2519
          RuleMatch { rm_rhs = (\x y. ...x..), rm_args = [ e1, e2 ] }
    
    ... ... @@ -2557,17 +2526,17 @@ Then:
    2557 2526
       is the reason that `RuleMatch` separates the two.
    
    2558 2527
     
    
    2559 2528
     * `fireRuleAFTER` uses `pushOutArgs` to push [ e1, e2 ] back onto the
    
    2560
    -  continuation with sc_env=Simplified NoDup.
    
    2529
    +  continuation as (DoneEx e1) and (DoneEx e2).
    
    2561 2530
     
    
    2562 2531
     * Beta-reduction fires (in `simpl_lam`) for the \x.
    
    2563 2532
     
    
    2564 2533
     * Suppose `x` occurs many times.  Then we want to behave like `let x=e1 in ..`,
    
    2565
    -  but we use `simplArg`, which in turn uses that `sc_env` flag to avoid
    
    2534
    +  but we use `simplArg`, which in turn uses the DoneEx to avoid
    
    2566 2535
       re-simplifying `e1`.
    
    2567 2536
     
    
    2568 2537
     * Suppose `x` occurs just once or not at all; then the call to
    
    2569
    -  `preInlineUnconditionally` in `simpl_lam` will fire, and extend the
    
    2570
    -  substitution with
    
    2538
    +  `preInlineBetaUnconditionally` in `simpl_lam` will fire, and extend the
    
    2539
    +  substitution with the very same SimplClo
    
    2571 2540
           x :-> DoneEx e1
    
    2572 2541
     
    
    2573 2542
     * At the /occurrence/ of `x`, the function `simplInId` calls `simplOutExpr`
    
    ... ... @@ -2774,8 +2743,7 @@ trySeqRules in_env scrut rhs cont
    2774 2743
                      , ValArg { as_arg = no_cast_scrut
    
    2775 2744
                               , as_dmd = seqDmd
    
    2776 2745
                               , as_hole_ty = res3_ty } ]
    
    2777
    -    rule_cont = ApplyToVal { sc_env = UnSimplified in_env, sc_arg = rhs
    
    2778
    -                           , sc_cast = MRefl
    
    2746
    +    rule_cont = ApplyToVal { sc_arg = ContEx in_env rhs MRefl, sc_dup = NoDup
    
    2779 2747
                                , sc_cont = cont, sc_hole_ty = res4_ty }
    
    2780 2748
     
    
    2781 2749
         out_args = [Type rhs_rep, Type scrut_ty, Type rhs_ty, no_cast_scrut]
    
    ... ... @@ -3420,6 +3388,7 @@ simplAlts (saf,env0) scrut case_bndr alts cont'
    3420 3388
             ; let case_bndr2 = case_bndr1 `setIdUnfolding` evaldUnfolding
    
    3421 3389
                   env2       = modifyInScope env1 case_bndr2
    
    3422 3390
                   -- See Note [Case binder evaluated-ness]
    
    3391
    +              -- and Note [Evaluated-ness on case binders] in GHC.Core
    
    3423 3392
                   fam_envs   = seFamEnvs env0
    
    3424 3393
     
    
    3425 3394
             ; (alt_env', scrut', case_bndr') <- improveSeq fam_envs (saf,env2) scrut
    
    ... ... @@ -3879,8 +3848,8 @@ knownCon env scrut dc dc_args case_bndr alt_bndrs rhs cont
    3879 3848
             return ( emptyFloats env
    
    3880 3849
                    , extendIdSubst env case_bndr (DoneEx scrut NotJoinPoint))
    
    3881 3850
     
    
    3882
    -      | Just env' <- preInlineUnconditionally env NotTopLevel case_bndr
    
    3883
    -                            (UnSimplified env) con_app MRefl
    
    3851
    +      | Just env' <- preInlineLetUnconditionally env NotTopLevel case_bndr
    
    3852
    +                                                 con_app env
    
    3884 3853
           = return (emptyFloats env', env')
    
    3885 3854
     
    
    3886 3855
           | otherwise
    
    ... ... @@ -4043,7 +4012,7 @@ mkDupableContWithDmds env dmds (TickIt t cont)
    4043 4012
     mkDupableContWithDmds env _
    
    4044 4013
          cont1@(StrictBind { sc_bndr = bndr, sc_body = body, sc_from = from_what
    
    4045 4014
                            , sc_env = se, sc_cont = cont})
    
    4046
    --- See Note [Duplicating StrictBind]
    
    4015
    +-- See (SB2) in Note [StrictBind]
    
    4047 4016
     -- K[ let x = <> in b ]  -->   join j x = K[ b ]
    
    4048 4017
     --                             j <>
    
    4049 4018
       = do { let sb_env = case se of
    
    ... ... @@ -4052,7 +4021,10 @@ mkDupableContWithDmds env _
    4052 4021
                                 -- The contIsDupable caught the OkDup case, and
    
    4053 4022
                                 -- we never build a StrictBind with a NoDup in it.
    
    4054 4023
     
    
    4055
    -       ; (sb_env1, bndr')      <- simplBinder sb_env bndr
    
    4024
    +       -- A lambda binder may have only occurrence and demand info;
    
    4025
    +       -- see (SB3) in Note [StrictBind]
    
    4026
    +       ; let lam_bndr = mkLamBndrFromLetBndr bndr
    
    4027
    +       ; (sb_env1, bndr')      <- simplBinder sb_env lam_bndr
    
    4056 4028
            ; (floats1, join_inner) <- simplNonRecBody sb_env1 from_what body cont
    
    4057 4029
               -- No need to use mkDupableCont before simplNonRecBody; we
    
    4058 4030
               -- use cont once here, and then share the result if necessary
    
    ... ... @@ -4060,7 +4032,10 @@ mkDupableContWithDmds env _
    4060 4032
            ; let join_body = wrapFloats floats1 join_inner
    
    4061 4033
                  res_ty    = contResultType cont
    
    4062 4034
     
    
    4063
    -       ; mkDupableStrictBind env bndr' join_body res_ty }
    
    4035
    +       -- bndr' is about to become a lambda binder, so it must have suitable
    
    4036
    +       -- IdInfo; simplNonRecE ensures this.  See Note [Core binder IdInfo invariants]
    
    4037
    +       ; assertPpr (lamBndrInfoOK bndr') (ppr bndr' $$ ppr (idInfo bndr')) $
    
    4038
    +         mkDupableStrictBind env bndr' join_body res_ty }
    
    4064 4039
     
    
    4065 4040
     mkDupableContWithDmds env _
    
    4066 4041
         (StrictArg { sc_fun = fun, sc_cont = cont
    
    ... ... @@ -4111,8 +4086,7 @@ mkDupableContWithDmds env dmds
    4111 4086
                                         , sc_arg_ty = arg_ty, sc_hole_ty = hole_ty }) }
    
    4112 4087
     
    
    4113 4088
     mkDupableContWithDmds env dmds
    
    4114
    -    (ApplyToVal { sc_arg = arg, sc_env = se, sc_cast = arg_mco
    
    4115
    -                , sc_cont = cont, sc_hole_ty = hole_ty })
    
    4089
    +    (ApplyToVal { sc_arg = arg, sc_cont = cont, sc_hole_ty = hole_ty })
    
    4116 4090
       =     -- e.g.         [...hole...] (...arg...)
    
    4117 4091
             --      ==>
    
    4118 4092
             --              let a = ...arg...
    
    ... ... @@ -4124,12 +4098,12 @@ mkDupableContWithDmds env dmds
    4124 4098
                     expectNonEmpty dmds
    
    4125 4099
             ; (floats1, cont') <- mkDupableContWithDmds env cont_dmds cont
    
    4126 4100
             ; let env' = env `setInScopeFromF` floats1
    
    4127
    -        ; arg' <- simplArg env' Nothing hole_ty se arg arg_mco
    
    4101
    +        ; arg' <- simplArg env' Nothing hole_ty arg
    
    4128 4102
             ; (let_floats2, triv_arg) <- makeTrivial env' NotTopLevel dmd (fsLit "karg") arg'
    
    4129 4103
             ; let all_floats = floats1 `addLetFloats` let_floats2
    
    4130 4104
             ; return ( all_floats
    
    4131
    -                 , ApplyToVal { sc_arg = triv_arg, sc_env = Simplified OkDup
    
    4132
    -                              , sc_cast = MRefl, sc_cont = cont', sc_hole_ty = hole_ty }) }
    
    4105
    +                 , ApplyToVal { sc_arg = DoneEx triv_arg NotJoinPoint, sc_dup = OkDup
    
    4106
    +                              , sc_cont = cont', sc_hole_ty = hole_ty }) }
    
    4133 4107
     
    
    4134 4108
     mkDupableContWithDmds env _
    
    4135 4109
         (Select { sc_bndr = case_bndr, sc_alts = alts, sc_env = se, sc_cont = cont })
    
    ... ... @@ -4253,7 +4227,9 @@ mkDupableAlt _env case_bndr jfloats (Alt con alt_bndrs alt_rhs_in)
    4253 4227
                              | otherwise = v
    
    4254 4228
     
    
    4255 4229
                   -- No lambda binder has an unfolding, but (currently) case binders can,
    
    4256
    -              -- so we must zap them here.
    
    4230
    +              -- so we must zap them here.  But case binders never have a pragma,
    
    4231
    +              -- stable unfolding or RULES, so there is no need to zap those.
    
    4232
    +              -- See Note [Core binder IdInfo invariants] in GHC.Core
    
    4257 4233
                   join_rhs   = mkLams (map zapIdUnfolding final_bndrs) rhs_with_seqs
    
    4258 4234
     
    
    4259 4235
             ; join_bndr <- newJoinId filtered_binders rhs_ty'
    
    ... ... @@ -4267,9 +4243,8 @@ mkDupableAlt _env case_bndr jfloats (Alt con alt_bndrs alt_rhs_in)
    4267 4243
                      , alt') }
    
    4268 4244
                     -- See Note [Duplicated env]
    
    4269 4245
     
    
    4270
    -{-
    
    4271
    -Note [Do not add unfoldings to join points at birth]
    
    4272
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    4246
    +{- Note [Do not add unfoldings to join points at birth]
    
    4247
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    4273 4248
     Consider this (#15360)
    
    4274 4249
     
    
    4275 4250
        case (case (case (case ...))) of
    
    ... ... @@ -4340,7 +4315,7 @@ The simplifier will find
    4340 4315
         (Var v) with continuation
    
    4341 4316
                 Select (pn -> rn) (
    
    4342 4317
                 Select [I# a -> I# a] (
    
    4343
    -            StrictBind body Stop
    
    4318
    +            StrictBind body Stop))
    
    4344 4319
     
    
    4345 4320
     So we'll call mkDupableCont on
    
    4346 4321
        Select [I# a -> I# a] (StrictBind body Stop)
    
    ... ... @@ -4479,7 +4454,7 @@ hack and bit us in multiple small and not-so-small ways
    4479 4454
     
    
    4480 4455
     Consider this
    
    4481 4456
        case (case .. ) of c {
    
    4482
    -     I# c# -> ....c....
    
    4457
    +     I# c# -> ....c.... }
    
    4483 4458
     
    
    4484 4459
     If we make a join point with c but not c# we get
    
    4485 4460
       $j = \c -> ....c....
    
    ... ... @@ -4569,7 +4544,7 @@ There are two ways to make it duplicable.
    4569 4544
                       ; False -> jump $j r2 }
    
    4570 4545
     
    
    4571 4546
       Notice that Plan B is very like the way we handle strict bindings;
    
    4572
    -  see Note [Duplicating StrictBind].  And Plan B is exactly what we'd
    
    4547
    +  see Note [StrictBind].  And Plan B is exactly what we'd
    
    4573 4548
       get if we turned use a case expression to evaluate the strict arg:
    
    4574 4549
     
    
    4575 4550
            case (case x of { True -> r1; False -> r2 }) of
    
    ... ... @@ -4642,23 +4617,80 @@ Note [Inline small things to avoid creating a thunk] in
    4642 4617
     Simplify.Utils. But if we do inline them, the entire process will
    
    4643 4618
     repeat -- back to exponential behaviour.
    
    4644 4619
     
    
    4645
    -So we are careful to keep the demand-info on a2 and a3.  Then they'll
    
    4646
    -be /strict/ let-bindings, which will be dealt with by StrictBind.
    
    4647
    -That's why contIsDupableWithDmds is careful to propagage demand
    
    4648
    -info to the auxiliary bindings it creates.  See the Demand argument
    
    4649
    -to makeTrivial.
    
    4650
    -
    
    4651
    -Note [Duplicating StrictBind]
    
    4652
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    4653
    -We make a StrictBind duplicable in a very similar way to
    
    4654
    -that for case expressions.  After all,
    
    4655
    -   let x* = e in b   is similar to    case e of x -> b
    
    4656
    -
    
    4657
    -So we potentially make a join-point for the body, thus:
    
    4658
    -   let x = <> in b   ==>   join j x = b
    
    4659
    -                           in j <>
    
    4660
    -
    
    4661
    -Just like StrictArg in fact -- and indeed they share code.
    
    4620
    +So we are careful to keep the demand-info on a2 and a3.  Then they'll be
    
    4621
    +/strict/ let-bindings, which will be dealt with by `StrictBind` (see
    
    4622
    +Note [StrictBind]).  That's why `contIsDupableWithDmds` is careful to
    
    4623
    +propagate demand info to the auxiliary bindings it creates.  See the
    
    4624
    +`Demand` argument to `makeTrivial`.
    
    4625
    +
    
    4626
    +Note [StrictBind]
    
    4627
    +~~~~~~~~~~~~~~~~~
    
    4628
    +We use StrictBind when we find
    
    4629
    +   let x! = rhs in body
    
    4630
    +where the `x` is definitely demanded in body.  When this happens we can
    
    4631
    +float out case-expressions etc from inside `rhs`; e.g.
    
    4632
    +   let { x! = case a of (p,q) -> rhs } in b
    
    4633
    +-->
    
    4634
    +   case a of (p,q) -> let x! = rhs in b
    
    4635
    +
    
    4636
    +We simplify `rhs` with a StrictBind continuation that continues with
    
    4637
    +`body`
    
    4638
    +
    
    4639
    +(SB1) We only use StrictBind when case-of-case is on:
    
    4640
    +   see Note [Case-of-case and full laziness]
    
    4641
    +
    
    4642
    +(SB2) We make a StrictBind /duplicable/ in a very similar way to
    
    4643
    +   that for case expressions.  After all,
    
    4644
    +      let x! = e in b   is similar to    case e of x -> b
    
    4645
    +
    
    4646
    +   So we potentially make a join-point for the body, thus:
    
    4647
    +     let x = <> in b   ==>   join j x = b
    
    4648
    +                             in j <>
    
    4649
    +
    
    4650
    +   Just like StrictArg in fact -- and indeed they share code.
    
    4651
    +
    
    4652
    +(SB3) When we duplicate a StrictBind (SB2), the binder `x` becomes a lambda
    
    4653
    +   binder, and so must satisfy `lamBndrInfoOK`; see Note [Core binder IdInfo
    
    4654
    +   invariants] in GHC.Core.  But a let-binder `x` usually has IdInfo that a
    
    4655
    +   lambda binder may not have (an unfolding, arity, a demand signature...).
    
    4656
    +
    
    4657
    +   So when we duplicate StrictBind, in `mkDupableContWithDmds`, we use
    
    4658
    +   `mkLamBndrFromLetBndr` to zap that info, keeping only the occurrence
    
    4659
    +   info and the demand info.
    
    4660
    +
    
    4661
    +   However, if the let-binder has
    
    4662
    +    * RULES
    
    4663
    +    * a stable unfolding
    
    4664
    +    * a non-default inlining activation
    
    4665
    +   we'd permanently lose these things if we lambda-bind it.  For example:
    
    4666
    +       let {-# NOINLINE x #-}
    
    4667
    +           x! = case y of { ... }
    
    4668
    +       in blah
    
    4669
    +   If we make a join point j x = blah, we have nowhere convenient to put that
    
    4670
    +   NOINLINE.  Ditto INLINE or RULES.  It's unusual. But it happens; one example
    
    4671
    +   is the Applicative (ST s) instance in GHC.Internal.Control.Monad.St.Lazy.Imp
    
    4672
    +         {-# NOINLINE res1 #-}
    
    4673
    +         !res1 = unST fm s
    
    4674
    +
    
    4675
    +   So in these cases we simply don't use StrictBind -- see `useStrictBind`.  We
    
    4676
    +   could imagine instead imposing these conditions only when we /duplicate/ the
    
    4677
    +   StrictBind, rather than when we /create/ it, but that would require a bigger
    
    4678
    +   refactor.
    
    4679
    +
    
    4680
    +   What if a thunk has a strictness signature, or CPR information? We
    
    4681
    +   could make `useStrictBind` say False in these cases too, but that turns
    
    4682
    +   out to be a net loss: keeping a thunk is worse than losing the strictness
    
    4683
    +   information. Example, where f1,f2 are both strict:
    
    4684
    +         let f* {- Str=<S> -} = case x of { True -> f1; False -> f2 }
    
    4685
    +         in  body
    
    4686
    +   Keeping it as-is would use CBV for calls of `f` in `body`.  But
    
    4687
    +   allowing StrictBind would give us
    
    4688
    +         join j f* = body
    
    4689
    +         in case x of { True -> j f1; False -> j f2 }
    
    4690
    +   which avoids the thunk. It's a trade-off.
    
    4691
    +
    
    4692
    +   For CPR info, see Note [Thunk splitting] in GHC.Core.Opt.WorkWrap,
    
    4693
    +   especially (B) CPR-led thunk splitting.
    
    4662 4694
     
    
    4663 4695
     Note [Join point abstraction]  Historical note
    
    4664 4696
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • compiler/GHC/Core/Opt/Simplify/Utils.hs
    ... ... @@ -12,7 +12,8 @@ module GHC.Core.Opt.Simplify.Utils (
    12 12
             tryEtaExpandRhs, wantEtaExpansion,
    
    13 13
     
    
    14 14
             -- Inlining,
    
    15
    -        preInlineUnconditionally, postInlineUnconditionally,
    
    15
    +        preInlineLetUnconditionally, preInlineBetaUnconditionally,
    
    16
    +        postInlineUnconditionally,
    
    16 17
             activeRule,
    
    17 18
             getUnfoldingInRuleMatch,
    
    18 19
             updModeForStableUnfoldings, updModeForRuleLHS, updModeForRuleRHS,
    
    ... ... @@ -174,16 +175,14 @@ data SimplCont
    174 175
                                     -- Invariant: never an identity coercion
    
    175 176
           , sc_opt  :: Bool         -- True <=> sc_co has had optCoercion applied to it
    
    176 177
                                     --      See Note [Avoid re-simplifying coercions]
    
    177
    -                                --      in GHC.Core.Opt.Simplify.Iteration
    
    178 178
           , sc_cont :: SimplCont }
    
    179 179
     
    
    180 180
       | ApplyToVal         -- (ApplyToVal arg K)[e] = K[ e arg ]
    
    181 181
           { sc_hole_ty :: OutType    -- Type of the function, presumably (forall a. blah)
    
    182 182
                                      -- See Note [The hole type in ApplyToTy]
    
    183
    -      , sc_env  :: StaticEnv     -- See Note [StaticEnv]
    
    184
    -      , sc_arg  :: CoreExpr      -- The argument
    
    185
    -      , sc_cast :: MOutCoercion  -- Wrap this OutCoercion around the (simplified) argument
    
    186
    -                                 -- See Note [The sc_cast field of ApplyToVal]
    
    183
    +      , sc_dup  :: DupFlag       -- See Note [The argument of ApplyToVal]
    
    184
    +      , sc_arg  :: SimplClo      -- The argument
    
    185
    +                                 -- Invariant: if sc_arg is a ContEx, then sc_dup = NoDup
    
    187 186
           , sc_cont :: SimplCont }
    
    188 187
     
    
    189 188
       | ApplyToTy          -- (ApplyToTy ty K)[e] = K[ e ty ]
    
    ... ... @@ -201,10 +200,11 @@ data SimplCont
    201 200
       -- The two strict forms have no DupFlag, because we never duplicate them
    
    202 201
       | StrictBind          -- (StrictBind x b K)[e] = let x = e in K[b]
    
    203 202
                             --       or, equivalently,  = K[ (\x.b) e ]
    
    203
    +                        -- See Note [StrictBind] in GHC.Core.Opt.Simplify.Iteration
    
    204 204
           { sc_from  :: FromWhat
    
    205 205
           , sc_env   :: StaticEnv  -- See Note [StaticEnv]
    
    206 206
                                    -- The sc_env in StrictBind is never (Simplified NoDup)
    
    207
    -      , sc_bndr  :: Id
    
    207
    +      , sc_bndr  :: Id         -- See (SB3) in Note [StrictBind]
    
    208 208
           , sc_body  :: CoreExpr
    
    209 209
     
    
    210 210
           , sc_cont  :: SimplCont }
    
    ... ... @@ -252,49 +252,85 @@ perhapsSubstTy (UnSimplified env) ty = substTy env ty
    252 252
     
    
    253 253
     {- Note [StaticEnv]
    
    254 254
     ~~~~~~~~~~~~~~~~~~~
    
    255
    -Consider ApplyToVal, which has
    
    256
    -    sc_env :: StaticEnv
    
    257
    -    sc_arg :: CoreExpr
    
    258
    -Initially, `sc_arg` is an un-simplified InExpr, and `sc_env` is (UnSimplified env),
    
    259
    -where `env` gives meaning to the free variables of `sc_arg`; in particular, `env`
    
    260
    -may have substitutions that must apply to the argument.
    
    261
    -
    
    262
    -But sometimes we simplify the argument, and in that case, `sc_arg` is an OutExpr,
    
    263
    -the simplified argument, and `sc_env` is (Simplified NoDup) or (Simplified OkDup).
    
    264
    -The former is the safe, conservative option, but in `mkDupableCont` we want to make
    
    265
    -the continuation duplicable, so we make the argument small and tag it with
    
    266
    -(Simplified OkDup).
    
    267
    -
    
    268
    -We later simplify the argument, e.g. in `simplArg`.  Then
    
    269
    -  * If sc_env is Simplified, it's a no-op
    
    270
    -  * If sc_env is (UnSimplified arg_env) we simplify `sc_arg` with
    
    271
    -     - Its captured envt `arg_env`
    
    255
    +Consider Select, which has
    
    256
    +    sc_env  :: StaticEnv
    
    257
    +    sc_bndr :: Id
    
    258
    +    sc_alts :: [CoreAlt]
    
    259
    +Initially, `sc_bndr` and `sc_alts` are un-simplified InThings, and `sc_env` is
    
    260
    +(UnSimplified env), where `env` gives meaning to their free variables; in
    
    261
    +particular, `env` may have substitutions that must apply to them.
    
    262
    +
    
    263
    +But sometimes (notably in `mkDupableCont`) we simplify them, and in that case
    
    264
    +`sc_bndr` and `sc_alts` are OutThings, and `sc_env` is (Simplified dup).
    
    265
    +
    
    266
    +When we finally simplify the alternatives (see `mkAltEnv`)
    
    267
    +  * If sc_env is Simplified, there is nothing to do
    
    268
    +  * If sc_env is (UnSimplified alt_env) we simplify them with
    
    269
    +     - Their captured envt `alt_env`
    
    272 270
          - but overriding its InScopeSet with the larger one at the
    
    273 271
            simplification point.
    
    274 272
         Why override the InScopeSet?  Example:
    
    275
    -          (let y = ey in f) ex
    
    276
    -    By the time we simplify ex, 'y' will be in scope.
    
    277
    -  All this is done in `simplArg`.
    
    273
    +          case (let y = ey in f) of alts
    
    274
    +    By the time we simplify alts, 'y' will be in scope.
    
    278 275
     
    
    279 276
     Note that:
    
    280 277
     
    
    281 278
     * In the Simplified case there is no environment, because the substitution has
    
    282 279
       already been applied.
    
    283 280
     
    
    284
    -* We say sc_arg :: CoreExpr, rather than sc_arg :: InExpr or sc_arg :: OutExpr,
    
    285
    -  because whether it is InExpr or OutExpr depends on `sc_env`
    
    281
    +* We say sc_alts :: [CoreAlt], rather than [InAlt] or [OutAlt],
    
    282
    +  because whether they are InAlts or OutAlts depends on `sc_env`
    
    283
    +
    
    284
    +* Same deal for StrictBind, where the StaticEnv scopes over sc_bndr and sc_body.
    
    286 285
     
    
    287
    -* Same deal for Select, and StrictBind, but the StaticEnv scopes over
    
    288
    -    * sc_bndr and sc_alts (for Select)
    
    289
    -    * sc_bndr and sc_body (for StrictBind)
    
    286
    +* ApplyToVal is similar, but simpler: it uses a SimplClo instead.
    
    287
    +  See Note [The argument of ApplyToVal].
    
    290 288
     
    
    291
    -* Even though the InScopeSet of an (UnSimplified se) is overridden in `simplArg`,
    
    292
    -  that InScopeSet is not irrelevant: it should include all the free vars of
    
    293
    -  applying the substitution to the InExpr. Reason: contHoleType uses perhapsSubstTy
    
    294
    -  to apply the substitution to the expression, and that (rightly) gives ASSERT
    
    295
    -  failures if the InScopeSet isn't big enough.
    
    289
    +* Even though the InScopeSet of an (UnSimplified se) is overridden, that
    
    290
    +  InScopeSet is not irrelevant: it should include all the free vars of
    
    291
    +  applying the substitution to the InThing. Reason: contHoleType uses
    
    292
    +  perhapsSubstTy to apply the substitution to the binder, and that
    
    293
    +  (rightly) gives ASSERT failures if the InScopeSet isn't big enough.
    
    296 294
     
    
    297 295
     (SE1) If dup = OkToDup, then continuation k is also ok-to-dup
    
    296
    +
    
    297
    +Note [The argument of ApplyToVal]
    
    298
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    299
    +ApplyToVal has
    
    300
    +    sc_dup :: DupFlag
    
    301
    +    sc_arg :: SimplClo
    
    302
    +
    
    303
    +Initially, `sc_arg` is (ContEx env arg MRefl), where `arg` is the un-simplified
    
    304
    +InExpr, and `env` gives meaning to the free variables of `arg`; in particular,
    
    305
    +`env` may have substitutions that must apply to the argument.
    
    306
    +
    
    307
    +But sometimes we simplify the argument, and in that case, `sc_arg` is
    
    308
    +(DoneEx arg' NotJoinPoint), where `arg'` is the simplified argument.
    
    309
    +
    
    310
    +The `sc_dup` flag says whether the continuation can be duplicated.  NoDup is
    
    311
    +the safe, conservative option, but in `mkDupableCont` we want to make the
    
    312
    +continuation duplicable, so we make the argument small (and hence a DoneEx)
    
    313
    +and set `sc_dup` to OkDup.  Invariants:
    
    314
    +
    
    315
    +  (AV1) If `sc_arg` is a ContEx, then `sc_dup` is NoDup.
    
    316
    +
    
    317
    +  (AV2) If `sc_dup` is OkDup then `sc_cont` is also ok-to-dup; see (SE1)
    
    318
    +        in Note [StaticEnv].
    
    319
    +
    
    320
    +We later simplify the argument, in `simplArg`.  Then
    
    321
    +  * If `sc_arg` is a DoneEx (or DoneId), it's a no-op
    
    322
    +  * If `sc_arg` is (ContEx arg_env arg mco) we simplify `arg` with
    
    323
    +     - Its captured envt `arg_env`
    
    324
    +     - but overriding its InScopeSet with the larger one at the
    
    325
    +       simplification point.
    
    326
    +    Why override the InScopeSet?  Example:
    
    327
    +          (let y = ey in f) ex
    
    328
    +    By the time we simplify ex, 'y' will be in scope.
    
    329
    +    Then we wrap the cast `mco` around the result.
    
    330
    +    See Note [The MOutCoercion field of ContEx].
    
    331
    +
    
    332
    +Using a SimplClo for the argument means that it can be substituted for a lambda
    
    333
    +binder, in `preInlineBetaUnconditionally`, without further ado.
    
    298 334
     -}
    
    299 335
     
    
    300 336
     instance Outputable DupFlag where
    
    ... ... @@ -312,9 +348,9 @@ instance Outputable SimplCont where
    312 348
         = (text "TickIt" <+> ppr t) $$ ppr cont
    
    313 349
       ppr (ApplyToTy  { sc_arg_ty = ty, sc_cont = cont })
    
    314 350
         = (text "ApplyToTy" <+> pprParendType ty) $$ ppr cont
    
    315
    -  ppr (ApplyToVal { sc_arg = arg, sc_env = env, sc_cont = cont, sc_hole_ty = hole_ty })
    
    316
    -    = (hang (text "ApplyToVal" <> braces (ppr env) <+> text "hole-ty:" <+> pprParendType hole_ty)
    
    317
    -          2 (pprParendExpr arg))
    
    351
    +  ppr (ApplyToVal { sc_arg = arg, sc_dup = dup, sc_cont = cont, sc_hole_ty = hole_ty })
    
    352
    +    = (hang (text "ApplyToVal" <> braces (ppr dup) <+> text "hole-ty:" <+> pprParendType hole_ty)
    
    353
    +          2 (ppr arg))
    
    318 354
           $$ ppr cont
    
    319 355
       ppr (StrictBind { sc_bndr = b, sc_cont = cont })
    
    320 356
         = (text "StrictBind" <+> ppr b) $$ ppr cont
    
    ... ... @@ -433,8 +469,8 @@ pushOutArgs fun_ty (arg:args) cont
    433 469
       = ApplyToTy { sc_hole_ty = fun_ty, sc_arg_ty = ty
    
    434 470
                   , sc_cont = pushOutArgs (piResultTy fun_ty ty) args cont }
    
    435 471
       | otherwise
    
    436
    -  = ApplyToVal { sc_hole_ty = fun_ty, sc_cast = MRefl
    
    437
    -               , sc_arg = arg, sc_env = Simplified NoDup
    
    472
    +  = ApplyToVal { sc_hole_ty = fun_ty
    
    473
    +               , sc_arg = DoneEx arg NotJoinPoint, sc_dup = NoDup
    
    438 474
                    , sc_cont = pushOutArgs (funResultTy fun_ty) args  cont}
    
    439 475
     
    
    440 476
     pushArgSpecs :: [ArgSpec]   -- In normal, forward order
    
    ... ... @@ -445,7 +481,7 @@ pushArgSpec :: ArgSpec -> SimplCont -> SimplCont
    445 481
     pushArgSpec (TyArg { as_arg_ty = arg_ty, as_hole_ty = hole_ty }) cont
    
    446 482
       = ApplyToTy  { sc_arg_ty = arg_ty, sc_hole_ty = hole_ty, sc_cont = cont }
    
    447 483
     pushArgSpec (ValArg { as_arg = arg, as_hole_ty = hole_ty }) cont
    
    448
    -  = ApplyToVal { sc_arg = arg, sc_env = Simplified NoDup, sc_cast = MRefl
    
    484
    +  = ApplyToVal { sc_arg = DoneEx arg NotJoinPoint, sc_dup = NoDup
    
    449 485
                    , sc_hole_ty = hole_ty, sc_cont = cont }
    
    450 486
     
    
    451 487
     argSpecArg :: ArgSpec -> OutExpr
    
    ... ... @@ -496,7 +532,12 @@ contIsStop _ = False
    496 532
     contIsDupable :: SimplCont -> Bool
    
    497 533
     contIsDupable (Stop {})                    = True
    
    498 534
     contIsDupable (ApplyToTy  { sc_cont = k }) = contIsDupable k
    
    499
    -contIsDupable (ApplyToVal { sc_env = se }) = okToDupSE se -- See (SE1) in Note [StaticEnv]
    
    535
    +contIsDupable (ApplyToVal { sc_dup = dup, sc_arg = arg })
    
    536
    +  = assertPpr (not (okToDup dup && is_cont_ex arg)) (ppr arg) $
    
    537
    +    okToDup dup -- See (AV1,AV2) in Note [The argument of ApplyToVal]
    
    538
    +  where
    
    539
    +    is_cont_ex (ContEx {}) = True
    
    540
    +    is_cont_ex _           = False
    
    500 541
     contIsDupable (Select { sc_env = se })     = okToDupSE se -- ...ditto...
    
    501 542
     contIsDupable (StrictBind { sc_env = se }) = okToDupSE se -- ...ditto...
    
    502 543
     contIsDupable (StrictArg { sc_dup = dup }) = okToDup dup  -- ...ditto...
    
    ... ... @@ -605,8 +646,8 @@ contArgs env cont
    605 646
         lone (CastIt {})     = False  --     stops it being "lone"
    
    606 647
         lone _               = True
    
    607 648
     
    
    608
    -    go args (ApplyToVal { sc_arg = arg, sc_env = se, sc_cont = k })
    
    609
    -                                        = go (interestingArg env se arg : args) k
    
    649
    +    go args (ApplyToVal { sc_arg = arg, sc_cont = k })
    
    650
    +                                        = go (interestingArg env arg : args) k
    
    610 651
         go args (ApplyToTy { sc_cont = k }) = go args k
    
    611 652
         go args (CastIt { sc_cont = k })    = go args k
    
    612 653
         go args k                           = (False, reverse args, k)
    
    ... ... @@ -621,15 +662,13 @@ contOutArgs env cont
    621 662
         go (ApplyToTy { sc_arg_ty = ty, sc_cont = cont })
    
    622 663
           = Type ty : go cont
    
    623 664
     
    
    624
    -    go (ApplyToVal { sc_arg = arg, sc_env = se, sc_cont = cont })
    
    625
    -      = case se of
    
    626
    -          Simplified {}    -> arg : go cont
    
    627
    -          UnSimplified env -> GHC.Core.Subst.substExpr (getFullSubst in_scope env) arg : go cont
    
    628
    -        -- Make sure we apply the static environment `sc_env` as a substitution
    
    665
    +    go (ApplyToVal { sc_arg = arg, sc_cont = cont })
    
    666
    +      = simplCloExpr in_scope arg : go cont
    
    667
    +        -- Make sure we apply the static environment of a ContEx as a substitution
    
    629 668
             --   to get an OutExpr.  See (BF1) in Note [tryRules: plan (BEFORE)]
    
    630 669
             --   in GHC.Core.Opt.Simplify.Iteration
    
    631
    -        -- NB: we use substExpr, not substExprSC: we want to get the benefit of
    
    632
    -        --     knowing what is evaluated etc, via the in-scope set
    
    670
    +        -- simplCloExpr also wraps the pending cast (if any) around the argument;
    
    671
    +        --   omitting it can make a rule fail to match (#27963)
    
    633 672
     
    
    634 673
         -- No more arguments
    
    635 674
         go _ = []
    
    ... ... @@ -826,23 +865,86 @@ make use of the strictness info for the function.
    826 865
     *                                                                      *
    
    827 866
     ********************************************************************* -}
    
    828 867
     
    
    829
    -{- Note [The sc_cast field of ApplyToVal]
    
    830
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    868
    +{- Note [The MOutCoercion field of ContEx]
    
    869
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    831 870
     In the `ApplyToVal` case of `pushCast` we want to push an `OutCoercion` into the
    
    832
    -argument.  But the argument may or may not have been simplified (see Note [StaticEnv])
    
    833
    -so we definitely can't just wrap the cast around the argument. Instead we store it
    
    834
    -in the `sc_cast` field -- this is the whole reason for the `sc_cast` field.
    
    871
    +argument.  But the argument may or may not have been simplified (see Note [The
    
    872
    +argument of ApplyToVal]):
    
    873
    +
    
    874
    +* If it has been simplified, it is (DoneEx arg' _), and we can simply wrap the
    
    875
    +  cast around `arg'`.
    
    876
    +
    
    877
    +* If it has not, it is (ContEx env arg mco), and we definitely can't just wrap
    
    878
    +  the (Out)cast around the InExpr `arg`. Instead we compose it with `mco` --
    
    879
    +  this is the whole reason for the MOutCoercion field of `ContEx`.
    
    880
    +
    
    881
    +All this is done by `mkCloCastMCo`.
    
    835 882
     
    
    836 883
     We deal with that pending cast mainly in `simplArg`.
    
    837 884
     
    
    838 885
     But we /also/ sometimes want to substitute that argument for a lambda-binder, in
    
    839
    -`preInlineUnconditionally`.  So we also have to put that pending cast into
    
    840
    -`ContEx`; hence the MOutCoercion field of `ContEx`.  Then we deal with it
    
    841
    -appropriately when /applying/ the substitution in `simplInId`.
    
    886
    +`preInlineBetaUnconditionally`; and since the substitution maps an Id to a SimplClo,
    
    887
    +the pending cast comes along for free.  Then we deal with it appropriately when
    
    888
    +/applying/ the substitution in `simplInId`.
    
    889
    +
    
    890
    +Note [Optimising reflexivity]
    
    891
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    892
    +It's important (for compiler performance) to get rid of reflexivity as soon
    
    893
    +as it appears.  See #11735, #14737, and #15019.
    
    894
    +
    
    895
    +In particular, we want to behave well on
    
    896
    +
    
    897
    + *  e |> co1 |> co2
    
    898
    +    where the two happen to cancel out entirely. That is quite common;
    
    899
    +    e.g. a newtype wrapping and unwrapping cancel.
    
    900
    +
    
    901
    +
    
    902
    + * (f |> co) @t1 @t2 ... @tn x1 .. xm
    
    903
    +   Here we will use pushCoTyArg and pushCoValArg successively, which
    
    904
    +   build up SelCo stacks.  Silly to do that if co is reflexive.
    
    905
    +
    
    906
    +However, we don't want to call isReflexiveCo too much, because it uses
    
    907
    +type equality which is expensive on big types (#14737 comment:7).
    
    908
    +
    
    909
    +A good compromise (determined experimentally) seems to be to call
    
    910
    +isReflexiveCo
    
    911
    + * when composing casts, and
    
    912
    + * at the end
    
    913
    +
    
    914
    +In investigating this I saw missed opportunities for on-the-fly
    
    915
    +coercion shrinkage. See #15090.
    
    916
    +
    
    917
    +Note [Avoid re-simplifying coercions]
    
    918
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    919
    +In some benchmarks (with deeply nested cases) we successively push
    
    920
    +casts onto the SimplCont.  We don't want to call the coercion optimiser
    
    921
    +on each successive composition -- that's at least quadratic.  So:
    
    922
    +
    
    923
    +* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
    
    924
    +  record whether the coercion optimiser has been applied to the coercion.
    
    925
    +
    
    926
    +* In `simplCast`, when we see (Cast e co), we simplify `co` to get
    
    927
    +  an OutCoercion, and built a CastIt with sc_opt=True.
    
    928
    +
    
    929
    +  Actually not quite: if we are simplifying the result of inlining an
    
    930
    +  unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
    
    931
    +  just /substitute/ which is cheaper.  See `simplCoercion`.
    
    932
    +
    
    933
    +* In `pushCast` if we combine this new coercion with an existing one,
    
    934
    +  we build a CastIt for (co1 ; co2) with sc_opt=False.
    
    935
    +
    
    936
    +* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
    
    937
    +  composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
    
    938
    +
    
    939
    +* When duplicating a continuation in `mkDupableContWithDmds`, before
    
    940
    +  duplicating a CastIt, optimise the coercion. Otherwise we'll end up
    
    941
    +  optimising it separately in the duplicate copies.
    
    942
    +
    
    943
    +* See also Note [The MOutCoercion field of ContEx]
    
    842 944
     -}
    
    843 945
     
    
    844 946
     optOutCoercion :: SimplEnvIS -> OutCoercion -> Bool -> OutCoercion
    
    845
    --- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
    
    947
    +-- See Note [Avoid re-simplifying coercions]
    
    846 948
     optOutCoercion env co already_optimised
    
    847 949
       | already_optimised = co  -- Ditto
    
    848 950
       | otherwise         = optCoercion opts empty_subst co
    
    ... ... @@ -862,16 +964,17 @@ pushCastMCo env (MCo co) cont = pushCast env co cont
    862 964
     
    
    863 965
     pushCast :: SimplEnv -> OutCoercion -> SimplCont -> SimplCont
    
    864 966
     pushCast env co cont
    
    865
    -  = go co True cont
    
    967
    +  | isReflCo co = cont     -- See Note [Optimising reflexivity]
    
    968
    +  | otherwise   = go co True cont
    
    866 969
       where
    
    867 970
     
    
    868 971
         -- ToDo:   pushCast Refl (ApplylToVal arg1 (ApplyToVal arg2 ...))
    
    869 972
         --         will do lots of unnecessary work.
    
    870 973
         go :: OutCoercion -> Bool -> SimplCont -> SimplCont
    
    871
    -    go co1 _ (CastIt { sc_co = co2, sc_cont = cont })  -- See Note [Optimising reflexivity] in GHC.Core.Opt.Simplify.Iteration
    
    974
    +    go co1 _ (CastIt { sc_co = co2, sc_cont = cont })  -- See Note [Optimising reflexivity]
    
    872 975
           = go (mkTransCo co1 co2) False cont
    
    873 976
                       -- False: (mkTransCo co1 co2) is not fully optimised
    
    874
    -                  -- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
    
    977
    +                  -- See Note [Avoid re-simplifying coercions]
    
    875 978
     
    
    876 979
         go co co_is_opt (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = tail })
    
    877 980
           | Just (tyL, arg_ty', m_co') <- pushCoTyArg co arg_ty
    
    ... ... @@ -886,26 +989,22 @@ pushCast env co cont
    886 989
         -- where   co :: (s1->s2) ~ (t1->t2)
    
    887 990
         --         co1 :: t1 ~ s1
    
    888 991
         --         co2 :: s2 ~ t2
    
    889
    -    go co co_is_opt cont@(ApplyToVal { sc_arg = arg, sc_env = arg_se
    
    890
    -                                     , sc_cast = arg_mco, sc_cont = tail })
    
    992
    +    go co co_is_opt cont@(ApplyToVal { sc_arg = arg, sc_cont = tail })
    
    891 993
           | not co_is_opt
    
    892 994
           = -- pushCoValArg duplicates the coercion, so optimise first
    
    893 995
             go (optOutCoercion (zapSubstEnv env) co co_is_opt) True cont
    
    894 996
     
    
    895 997
           | Just (tyL, m_co1, m_co2) <- pushCoValArg co
    
    896 998
           = {-#SCC "addCoerce-pushCoValArg" #-}
    
    897
    -        -- Dealing with m_co1 is the entire reason for the sc_cast field
    
    898
    -        -- of ApplyToVal; see Note [The sc_cast field of ApplyToVal]
    
    899
    -        ApplyToVal { sc_arg  = arg
    
    900
    -                   , sc_env  = arg_se
    
    901
    -                   , sc_cast = arg_mco `mkTransMCo` m_co1
    
    902
    -                   , sc_cont = go_mco m_co2 True tail
    
    903
    -                   , sc_hole_ty = tyL }
    
    999
    +        -- See Note [The MOutCoercion field of ContEx]
    
    1000
    +        cont { sc_arg     = mkCloCastMCo arg m_co1
    
    1001
    +             , sc_cont    = go_mco m_co2 True tail
    
    1002
    +             , sc_hole_ty = tyL }
    
    904 1003
     
    
    905 1004
         go co co_is_opt cont
    
    906 1005
           | isReflCo co = cont  -- Having this at the end makes a huge
    
    907 1006
                                 -- difference in T12227, for some reason
    
    908
    -                            -- See Note [Optimising reflexivity] in GHC.Core.Opt.Simplify.Iteration
    
    1007
    +                            -- See Note [Optimising reflexivity]
    
    909 1008
           | otherwise = CastIt { sc_co = co, sc_opt = co_is_opt, sc_cont = cont }
    
    910 1009
     
    
    911 1010
             -- If the first parameter is MRefl, then simplifying revealed a
    
    ... ... @@ -914,6 +1013,13 @@ pushCast env co cont
    914 1013
         go_mco MRefl    _   cont = cont
    
    915 1014
         go_mco (MCo co) opt cont = go co opt cont
    
    916 1015
     
    
    1016
    +mkCloCastMCo :: SimplClo -> MOutCoercion -> SimplClo
    
    1017
    +-- See Note [The MOutCoercion field of ContEx]
    
    1018
    +mkCloCastMCo clo               MRefl    = clo
    
    1019
    +mkCloCastMCo (DoneId v)        (MCo co) = DoneEx (mkCast (Var v) co) NotJoinPoint
    
    1020
    +mkCloCastMCo (DoneEx e _)      (MCo co) = DoneEx (mkCast e co)       NotJoinPoint
    
    1021
    +mkCloCastMCo (ContEx se e mco) (MCo co) = ContEx se e (mco `mkTransMCo` MCo co)
    
    1022
    +
    
    917 1023
     
    
    918 1024
     {-
    
    919 1025
     ************************************************************************
    
    ... ... @@ -1227,19 +1333,17 @@ Wrinkles:
    1227 1333
     
    
    1228 1334
     -}
    
    1229 1335
     
    
    1230
    -interestingArg :: SimplEnv -> StaticEnv -> CoreExpr -> ArgSummary
    
    1336
    +interestingArg :: SimplEnv -> SimplClo -> ArgSummary
    
    1231 1337
     -- See Note [Interesting arguments]
    
    1232
    -interestingArg env se e
    
    1233
    -  = case se of
    
    1234
    -      Simplified {}           -> go env                                0 e
    
    1235
    -      UnSimplified static_env -> go (static_env `setInScopeFromE` env) 0 e
    
    1338
    +interestingArg env clo
    
    1339
    +  = go_clo env 0 clo
    
    1236 1340
       where
    
    1341
    +    go_clo _   n (DoneId v)       = go_var n v
    
    1342
    +    go_clo env n (DoneEx e _)     = go (zapSubstEnv env)          n e
    
    1343
    +    go_clo env n (ContEx se e _)  = go (se `setInScopeFromE` env) n e
    
    1344
    +
    
    1237 1345
         -- n is # value args to which the expression is applied
    
    1238
    -    go env n (Var v)
    
    1239
    -       = case substId env v of
    
    1240
    -           DoneId v'            -> go_var n v'
    
    1241
    -           DoneEx e _           -> go (zapSubstEnv env)          n e
    
    1242
    -           ContEx se e _mco     -> go (se `setInScopeFromE` env) n e
    
    1346
    +    go env n (Var v) = go_clo env n (substId env v)
    
    1243 1347
     
    
    1244 1348
         go _   _ (Lit l)
    
    1245 1349
            | isLitRubbish l        = NonTrivArg -- See (IA3) in Note [Interesting arguments]
    
    ... ... @@ -1738,15 +1842,15 @@ is a term (not a coercion) so we can't necessarily inline the latter in
    1738 1842
     the former.
    
    1739 1843
     -}
    
    1740 1844
     
    
    1741
    -preInlineUnconditionally
    
    1845
    +preInlineLetUnconditionally
    
    1742 1846
         :: SimplEnv -> TopLevelFlag -> InId
    
    1743
    -    -> StaticEnv -> CoreExpr -> MOutCoercion  -- The argument
    
    1847
    +    -> InExpr -> SimplEnv   -- These two go together
    
    1744 1848
         -> Maybe SimplEnv       -- Returned env has extended substitution
    
    1745 1849
     -- Precondition: rhs satisfies the let-can-float invariant
    
    1746 1850
     -- See Note [Core let-can-float invariant] in GHC.Core
    
    1747 1851
     -- Reason: we don't want to inline single uses, or discard dead bindings,
    
    1748 1852
     --         for unlifted, side-effect-ful bindings
    
    1749
    -preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
    
    1853
    +preInlineLetUnconditionally env top_lvl bndr rhs rhs_env
    
    1750 1854
       | not pre_inline_unconditionally           = Nothing
    
    1751 1855
       | not active                               = Nothing
    
    1752 1856
       | isTopLevel top_lvl && isDeadEndId bndr   = Nothing -- Note [Top-level bottoming Ids]
    
    ... ... @@ -1758,26 +1862,15 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
    1758 1862
     
    
    1759 1863
       -- See Note [Stable unfoldings and preInlineUnconditionally]
    
    1760 1864
       | not (isInlinePragma inline_prag)
    
    1761
    -  , Just inl <- maybeUnfoldingTemplate unf   = assertPpr (isReflMCo rhs_mco) (ppr bndr) $
    
    1762
    -                                               Just $! (extend_subst_with inl)
    
    1865
    +  , Just inl <- maybeUnfoldingTemplate unf   = Just $! (extend_subst_with inl)
    
    1763 1866
     
    
    1764 1867
       | otherwise                                = Nothing
    
    1765 1868
       where
    
    1766 1869
         unf = idUnfolding bndr
    
    1767 1870
     
    
    1768
    -    -- If the rhs is /not/ already simplified, extend the envt with ContEx, which captures
    
    1769
    -    --    the the lexical environment for us to restore in `simplInId`.
    
    1770
    -    -- If the rhs /is/ already simplified, then extend the envt with DoneEx;
    
    1771
    -    --    That makes `simplInId` call `simplOutExpr` which avoids re-simplifying
    
    1772
    -    --    the RHS.
    
    1773
    -    -- See Note [Avoid repeated simplification] in GHC.Core.Opt.Simplify.Iteration
    
    1774
    -    extend_subst_with inl_rhs
    
    1775
    -      = extendIdSubst env bndr $!
    
    1776
    -        case rhs_se of
    
    1777
    -          Simplified _ -> case rhs_mco of
    
    1778
    -                             MRefl  -> DoneEx inl_rhs NotJoinPoint -- Common case
    
    1779
    -                             MCo co -> DoneEx (mkCast inl_rhs co) NotJoinPoint
    
    1780
    -          UnSimplified rhs_env -> ContEx rhs_env inl_rhs rhs_mco
    
    1871
    +    -- The rhs is not yet simplified, so extend the envt with ContEx, which
    
    1872
    +    -- captures the lexical environment for us to restore in `simplInId`.
    
    1873
    +    extend_subst_with inl_rhs = extendIdSubst env bndr $! ContEx rhs_env inl_rhs MRefl
    
    1781 1874
     
    
    1782 1875
         one_occ IAmDead = True -- Happens in ((\x.1) v)
    
    1783 1876
         one_occ OneOcc{ occ_n_br   = 1
    
    ... ... @@ -1793,39 +1886,6 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
    1793 1886
                  -- See Note [pre/postInlineUnconditionally in gentle mode]
    
    1794 1887
         inline_prag = idInlinePragma bndr
    
    1795 1888
     
    
    1796
    --- Be very careful before inlining inside a lambda, because (a) we must not
    
    1797
    --- invalidate occurrence information, and (b) we want to avoid pushing a
    
    1798
    --- single allocation (here) into multiple allocations (inside lambda).
    
    1799
    --- Inlining a *function* with a single *saturated* call would be ok, mind you.
    
    1800
    ---      || (if is_cheap && not (canInlineInLam rhs) then pprTrace "preinline" (ppr bndr <+> ppr rhs) ok else ok)
    
    1801
    ---      where
    
    1802
    ---              is_cheap = exprIsCheap rhs
    
    1803
    ---              ok = is_cheap && int_cxt
    
    1804
    -
    
    1805
    -        --      int_cxt         The context isn't totally boring
    
    1806
    -        -- E.g. let f = \ab.BIG in \y. map f xs
    
    1807
    -        --      Don't want to substitute for f, because then we allocate
    
    1808
    -        --      its closure every time the \y is called
    
    1809
    -        -- But: let f = \ab.BIG in \y. map (f y) xs
    
    1810
    -        --      Now we do want to substitute for f, even though it's not
    
    1811
    -        --      saturated, because we're going to allocate a closure for
    
    1812
    -        --      (f y) every time round the loop anyhow.
    
    1813
    -
    
    1814
    -        -- canInlineInLam => free vars of rhs are (Once in_lam) or Many,
    
    1815
    -        -- so substituting rhs inside a lambda doesn't change the occ info.
    
    1816
    -        -- Sadly, not quite the same as exprIsHNF.
    
    1817
    -    canInlineInLam (Lit _)    = True
    
    1818
    -    canInlineInLam (Cast e _) = canInlineInLam e
    
    1819
    -    canInlineInLam (Lam b e)  = isRuntimeVar b || canInlineInLam e
    
    1820
    -    canInlineInLam (Tick t e) = not (tickishIsCode t) && canInlineInLam e
    
    1821
    -    canInlineInLam (Var v)    = case idOccInfo v of
    
    1822
    -                                  OneOcc { occ_in_lam = IsInsideLam } -> True
    
    1823
    -                                  ManyOccs {}                         -> True
    
    1824
    -                                  _                                   -> False
    
    1825
    -    canInlineInLam _          = False
    
    1826
    -      -- not ticks.  Counting ticks cannot be duplicated, and non-counting
    
    1827
    -      -- ticks around a Lam will disappear anyway.
    
    1828
    -
    
    1829 1889
         early_phase =
    
    1830 1890
           case sePhase env of
    
    1831 1891
             SimplPhase p -> p /= FinalPhase
    
    ... ... @@ -1856,6 +1916,97 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
    1856 1916
         -- (Nor can we check for `exprIsExpandable rhs`, because that needs to look
    
    1857 1917
         -- at the non-existent unfolding for the `I# 2#` which is also floated out.)
    
    1858 1918
     
    
    1919
    +preInlineBetaUnconditionally
    
    1920
    +    :: SimplEnv -> Levity -> InId
    
    1921
    +    -> SimplClo             -- The argument
    
    1922
    +    -> Maybe SimplEnv       -- Returned env has extended substitution
    
    1923
    +-- Like preInlineLetUnconditionally, but for a beta-redex (\bndr. body) arg
    
    1924
    +-- The argument may or may not be already simplified (DoneEx or ContEx)
    
    1925
    +--
    
    1926
    +-- The binder is a lambda binder and so lacks most IdInfo;
    
    1927
    +--   see Note [Core binder IdInfo invariants]
    
    1928
    +--
    
    1929
    +-- Compared to preInlineLetUnconditionally we omit some checks:
    
    1930
    +--   * The binder is never top-level, so no need for the
    
    1931
    +--     Note [Top-level bottoming Ids] check, nor for `early_phase`
    
    1932
    +--   * A lambda binder is never a join point, so no need for the
    
    1933
    +--     Note [Do not inline exit join points] check
    
    1934
    +--   * A lambda binder never has an inline pragma, a stable unfolding, or RULES
    
    1935
    +--     (see `lamBndrInfoOK`), so no need to check activation, nor for
    
    1936
    +--     Note [Stable unfoldings and preInlineUnconditionally]
    
    1937
    +--
    
    1938
    +-- And we add one extra one
    
    1939
    +--   * Check the let-can-float invariant, and don't inline if not
    
    1940
    +
    
    1941
    +preInlineBetaUnconditionally env levity bndr arg
    
    1942
    +  | assertPpr (lamBndrInfoOK bndr) (ppr bndr) $
    
    1943
    +    not pre_inline_unconditionally           = Nothing
    
    1944
    +  | isCoVar bndr                             = Nothing -- Note [Do not inline CoVars unconditionally]
    
    1945
    +  | not (one_occ (idOccInfo bndr))           = Nothing
    
    1946
    +  | needs_case_binding                       = Nothing  -- Enforce the let-can-float invariant
    
    1947
    +  | otherwise                                = Just $! extendIdSubst env bndr arg
    
    1948
    +    -- If the arg is not yet simplified, it is a ContEx, which captures the
    
    1949
    +    --    lexical environment for us to restore in `simplInId`.
    
    1950
    +    -- If the arg is already simplified, then it is a DoneEx;
    
    1951
    +    --    That makes `simplInId` call `simplOutExpr` which avoids re-simplifying
    
    1952
    +    --    the arg.
    
    1953
    +    -- See Note [Avoid repeated simplification] in GHC.Core.Opt.Simplify.Iteration
    
    1954
    +  where
    
    1955
    +    pre_inline_unconditionally = sePreInline env
    
    1956
    +
    
    1957
    +    needs_case_binding = needsCaseBindingL levity rhs
    
    1958
    +    -- Ok to test an InExpr or OutExpr in needsCaseBindingL because
    
    1959
    +    -- exprOkForSpeculation is stable under simplification
    
    1960
    +
    
    1961
    +    rhs :: CoreExpr   -- Can be InExpr or OutExpr, but here it does not matter
    
    1962
    +    rhs = case arg of
    
    1963
    +            ContEx _ e _ -> e          -- InExpr
    
    1964
    +            DoneEx e _   -> e          -- OutExpr
    
    1965
    +            DoneId v     -> Var v      -- OutExpr
    
    1966
    +
    
    1967
    +    one_occ IAmDead = True -- Happens in ((\x.1) v)
    
    1968
    +    one_occ OneOcc{ occ_n_br   = 1
    
    1969
    +                  , occ_in_lam = NotInsideLam }   = True
    
    1970
    +    one_occ OneOcc{ occ_n_br   = 1
    
    1971
    +                  , occ_in_lam = IsInsideLam
    
    1972
    +                  , occ_int_cxt = IsInteresting } = canInlineInLam rhs
    
    1973
    +    one_occ _                                     = False
    
    1974
    +
    
    1975
    +
    
    1976
    +canInlineInLam :: CoreExpr -> Bool
    
    1977
    +-- Be very careful before inlining inside a lambda, because (a) we must not
    
    1978
    +-- invalidate occurrence information, and (b) we want to avoid pushing a
    
    1979
    +-- single allocation (here) into multiple allocations (inside lambda).
    
    1980
    +-- Inlining a *function* with a single *saturated* call would be ok, mind you.
    
    1981
    +--      || (if is_cheap && not (canInlineInLam rhs) then pprTrace "preinline" (ppr bndr <+> ppr rhs) ok else ok)
    
    1982
    +--      where
    
    1983
    +--              is_cheap = exprIsCheap rhs
    
    1984
    +--              ok = is_cheap && int_cxt
    
    1985
    +
    
    1986
    +        --      int_cxt         The context isn't totally boring
    
    1987
    +        -- E.g. let f = \ab.BIG in \y. map f xs
    
    1988
    +        --      Don't want to substitute for f, because then we allocate
    
    1989
    +        --      its closure every time the \y is called
    
    1990
    +        -- But: let f = \ab.BIG in \y. map (f y) xs
    
    1991
    +        --      Now we do want to substitute for f, even though it's not
    
    1992
    +        --      saturated, because we're going to allocate a closure for
    
    1993
    +        --      (f y) every time round the loop anyhow.
    
    1994
    +
    
    1995
    +        -- canInlineInLam => free vars of rhs are (Once in_lam) or Many,
    
    1996
    +        -- so substituting rhs inside a lambda doesn't change the occ info.
    
    1997
    +        -- Sadly, not quite the same as exprIsHNF.
    
    1998
    +canInlineInLam (Lit _)    = True
    
    1999
    +canInlineInLam (Cast e _) = canInlineInLam e
    
    2000
    +canInlineInLam (Lam b e)  = isRuntimeVar b || canInlineInLam e
    
    2001
    +canInlineInLam (Tick t e) = not (tickishIsCode t) && canInlineInLam e
    
    2002
    +canInlineInLam (Var v)    = case idOccInfo v of
    
    2003
    +                              OneOcc { occ_in_lam = IsInsideLam } -> True
    
    2004
    +                              ManyOccs {}                         -> True
    
    2005
    +                              _                                   -> False
    
    2006
    +canInlineInLam _          = False
    
    2007
    +  -- not ticks.  Counting ticks cannot be duplicated, and non-counting
    
    2008
    +  -- ticks around a Lam will disappear anyway.
    
    2009
    +
    
    1859 2010
     {-
    
    1860 2011
     ************************************************************************
    
    1861 2012
     *                                                                      *
    

  • compiler/GHC/Core/Opt/SpecConstr.hs
    ... ... @@ -2068,7 +2068,12 @@ mkSeqs seqees res_ty rhs =
    2068 2068
             -- Argument representing strict field and it's worth passing via cbv
    
    2069 2069
             | wantCbvForId arg_id
    
    2070 2070
             = Case (Var arg_id)
    
    2071
    -               (localiseId arg_id)  -- See (SCF1) in Note [SpecConstr and strict fields]
    
    2071
    +               (mkCaseBndrFromLetBndr arg_id)
    
    2072
    +                   -- mkCaseBndrFromLetBndr: arg_id may have IdInfo (e.g. an unfolding,
    
    2073
    +                   --   if it is a global) that a case binder must not have;
    
    2074
    +                   --   see Note [Core binder IdInfo invariants] in GHC.Core
    
    2075
    +                   -- Note that mkCaseBndrFromLetBndr also does localiseId:
    
    2076
    +                   --    see (SCF1) in Note [SpecConstr and strict fields]
    
    2072 2077
                    res_ty
    
    2073 2078
                    ([Alt DEFAULT [] rhs])
    
    2074 2079
     
    

  • compiler/GHC/Core/Opt/WorkWrap.hs
    ... ... @@ -598,13 +598,20 @@ tryWW ww_opts is_rec fn_id rhs
    598 598
       = splitFun ww_opts new_fn_id rhs
    
    599 599
     
    
    600 600
       -- See Note [Thunk splitting]
    
    601
    -  | isNonRec is_rec, is_thunk
    
    602
    -  = splitThunk ww_opts is_rec new_fn_id rhs
    
    601
    +  | isNonRec is_rec                    -- See (TS4)
    
    602
    +  , isStrUsedDmd (demandInfo fn_info)  -- See (TS4)
    
    603
    +  , is_thunk  -- All join points go via splitFun
    
    604
    +  = do { -- Try strictness then CPR; see (TS5)
    
    605
    +         rhs' <- splitThunkStr ww_opts new_fn_id rhs `orElseM`
    
    606
    +                 splitThunkCpr ww_opts new_fn_id rhs `orElseM`
    
    607
    +                 return rhs
    
    608
    +       ; return [ (new_fn_id, rhs') ] }
    
    603 609
     
    
    604 610
       | otherwise
    
    605 611
       = return [ (new_fn_id, rhs) ]
    
    606 612
     
    
    607 613
       where
    
    614
    +    is_join        = isJoinId fn_id
    
    608 615
         fn_info        = idInfo fn_id
    
    609 616
         (wrap_dmds, _) = splitDmdSig (dmdSigInfo fn_info)
    
    610 617
         new_fn_id      = zap_join_cpr $ zap_usage fn_id
    
    ... ... @@ -613,14 +620,13 @@ tryWW ww_opts is_rec fn_id rhs
    613 620
             -- See Note [Zapping DmdEnv after Demand Analyzer] and
    
    614 621
             -- See Note [Zapping Used Once info in WorkWrap]
    
    615 622
     
    
    616
    -    zap_join_cpr id
    
    617
    -      | isJoinId id = id `setIdCprSig` topCprSig
    
    618
    -      | otherwise   = id
    
    623
    +    zap_join_cpr id | is_join   = id `setIdCprSig` topCprSig
    
    624
    +                    | otherwise = id
    
    619 625
             -- See Note [Don't w/w join points for CPR]
    
    620 626
     
    
    621
    -    is_fun     = notNull wrap_dmds || isJoinId fn_id
    
    622
    -    is_thunk   = not is_fun && not (exprIsHNF rhs) && not (isJoinId fn_id)
    
    623
    -                            && not (isUnliftedType (idType fn_id))
    
    627
    +    is_fun     = notNull wrap_dmds || is_join
    
    628
    +    is_thunk   = not (exprIsHNF rhs)
    
    629
    +                 && not (isUnliftedType (idType fn_id))
    
    624 630
     
    
    625 631
     {-
    
    626 632
     Note [Zapping DmdEnv after Demand Analyzer]
    
    ... ... @@ -806,7 +812,7 @@ splitFun ww_opts fn_id rhs
    806 812
     
    
    807 813
         (wrap_dmds, div) = splitDmdSig (dmdSigInfo fn_info)
    
    808 814
     
    
    809
    -    cpr_ty = getCprSig (cprSigInfo fn_info)
    
    815
    +    cpr_ty = cprSigType (cprSigInfo fn_info)
    
    810 816
         -- Arity of the CPR sig should match idArity when it's not a join point.
    
    811 817
         -- See Note [Arity trimming for CPR signatures] in GHC.Core.Opt.CprAnal
    
    812 818
         cpr = assertPpr (isJoinId fn_id || cpr_ty == topCprType || ct_arty cpr_ty == arityInfo fn_info)
    
    ... ... @@ -957,114 +963,140 @@ the form [Demand=<L,1*(C(1,...(C(1,U))))>]
    957 963
     
    
    958 964
     Note [Thunk splitting]
    
    959 965
     ~~~~~~~~~~~~~~~~~~~~~~
    
    960
    -Suppose x is used strictly; never mind whether it has the CPR
    
    961
    -property.  I'll use a '*' to mean "x* is demanded strictly".
    
    962
    -
    
    963
    -      let
    
    964
    -        x* = x-rhs
    
    966
    +The worker/wrapper split mainly affects let-bound /functions/.  But we can also use
    
    967
    +demand and CPR information to improve let-bound /thunks/.  Specifically:
    
    968
    +
    
    969
    +* (A) Demand-led thunk splitting:
    
    970
    +  If a thunk is demanded strictly, perhaps nestedly, we can turn it into a
    
    971
    +  case, perhaps nestedly.  For example
    
    972
    +       let x :: (Int, Int)
    
    973
    +           {- Dmd = SP(S,L) -}
    
    974
    +           x = rhs
    
    975
    +       in body
    
    976
    +   We would ultimately like to get
    
    977
    +        case rhs of x { (a,b) ->
    
    978
    +        case a of { I# a# ->
    
    979
    +        body
    
    980
    +
    
    981
    +   This is much better: no thunk for `rhs`; and known-data-con unfoldings for `x`
    
    982
    +   and `a` inside `body`.
    
    983
    +
    
    984
    +* (B) CPR-led thunk splitting:
    
    985
    +  If a thunk has the (possibly-nested) CPR property /and/ is demanded strictly
    
    986
    +  at top level, we can again generate a case
    
    987
    +       let x :: (Int, Int)
    
    988
    +           {- Dmd = S, Cpr = (_, I# _) -}
    
    989
    +           x = rhs
    
    990
    +       in body
    
    991
    +   The CPR info says that if `x` is evaluated (which it is), it'll return a pair,
    
    992
    +   whose second component can very cheaply be evaluated to a (I# b#).  We would
    
    993
    +   like to end up with
    
    994
    +        case rhs of x { (a,b) ->
    
    995
    +        case b of { I# b# ->
    
    996
    +        body
    
    997
    +   which is good for the same reasons as before.  See (TS3) for an example.
    
    998
    +
    
    999
    +Implementation
    
    1000
    +~~~~~~~~~~~~~~
    
    1001
    +The worker/wrapper pass does not generate these nested case expressions directly.
    
    1002
    +Instead it generates a regular let-binding but, since the binder is marked as
    
    1003
    +strict, any `case` expressions in the RHS will float out.  Specifically:
    
    1004
    +
    
    1005
    +(TS1) For (A), `splitThunkStr` generates the binding
    
    1006
    +      let x* = case rhs of { (a,b) ->
    
    1007
    +               case a of { I# a# ->
    
    1008
    +               (I# a, b)
    
    1009
    +  Now when the Simplifier floats the `case` expressions out, we get
    
    1010
    +      case rhs of { (a,b) ->
    
    1011
    +      case a of { I# a# ->
    
    1012
    +      let x = (I# a, b)
    
    965 1013
           in body
    
    1014
    +  which is equivalent to the expression in (A) above.
    
    966 1015
     
    
    967
    -splitThunk transforms like this:
    
    968
    -      let
    
    969
    -        x* = let x = x-rhs in
    
    970
    -             case x of { I# a -> I# a }
    
    971
    -      in body
    
    1016
    +  Rather cleverly, `splitThunkStr` uses `mkWWstr_one`, the /existing/ function
    
    1017
    +  argument w/w splitting function, to do the unpacking.  This automatically
    
    1018
    +  deals with nested strictness.
    
    972 1019
     
    
    973
    -This is a little strange: we are re-using the same `x` in the RHS; and
    
    974
    -the RHS takes `x` apart and reboxes it. But because the outer 'let' is
    
    975
    -strict, and the inner let mentions `x` only once, the simplifier
    
    976
    -transform it to
    
    977
    -      case x-rhs of
    
    978
    -        I# a -> let x* = I# a
    
    979
    -                in body
    
    980
    -
    
    981
    -That is good: in `body` we know the form of `x`, which
    
    982
    -  * gives the CPR property, and
    
    983
    -  * allows case-of-case to happen on x
    
    984
    -
    
    985
    -Notes
    
    986
    -* I tried transforming like this:
    
    987
    -      let
    
    988
    -        x* = let x = x-rhs in
    
    989
    -             case x of { I# a -> x }
    
    990
    -      in body
    
    991
    -  where I return `x` itself, rather than reboxing it.  But this
    
    992
    -  turned out to cause some regressions, which I never fully
    
    993
    -  investigated.
    
    994
    -
    
    995
    -* Suppose x-rhs is itself a case:
    
    996
    -        x-rhs = case e of { T -> I# e1; F -> I# e2 }
    
    997
    -  Then we'll get
    
    998
    -      join j a = let x* = I# a in body
    
    999
    -      in case e of { T -> j e1; F -> j e2 }
    
    1000
    -  which is good (no boxing).  But in the original, unsplit program
    
    1001
    -  we would transform
    
    1002
    -      let x* = case e of ... in body
    
    1003
    -  ==> join j2 x = body
    
    1004
    -      in case e of { T -> j2 (I# e1); F -> j (I# e2) }
    
    1020
    +(TS2) For (B), `splitThunkCpr` behaves exactly analogously, again
    
    1021
    +  using `mkWWcpr_one` to do the unpacking.
    
    1022
    +
    
    1023
    +Wrinkles:
    
    1024
    +
    
    1025
    +(TS3) Here's an example of (B), thunk-splitting based on CPR information, in action.
    
    1026
    +  Suppose we have
    
    1027
    +        let x* = case e of { T -> Just e1; F -> Just e2 }
    
    1028
    +        in body
    
    1029
    +  If the Simplifier floats the `case` out, we get
    
    1030
    +      join j2 x = body
    
    1031
    +      in case e of { T -> j2 (Just e1); F -> j2 (Just e2) }
    
    1005 1032
       which is not good (boxing).
    
    1006 1033
     
    
    1007
    -* In fact, splitThunk uses the function argument w/w splitting
    
    1008
    -  function, mkWWstr_one, so that if x's demand is deeper (say U(U(L,L),L))
    
    1009
    -  then the splitting will go deeper too.
    
    1034
    +  If we first do the thunk-splitting transformation, we get
    
    1035
    +        let x* = case (case e of { T -> Just e1; F -> Just e2 }) of
    
    1036
    +                   Just a -> Just a
    
    1037
    +        in body
    
    1038
    +
    
    1039
    +  Now when the Simplifier floats we will get:
    
    1040
    +      join j a = let x* = Just a in body
    
    1041
    +      in case e of { T -> j e1; F -> j e2 }
    
    1042
    +  which is much better (no boxing).
    
    1043
    +
    
    1044
    +(TS4) We only do thunk-splitting for thunks that are:
    
    1045
    +        * strictly-demanded
    
    1046
    +        * non-recursive
    
    1047
    +  For lazy thunks, transforming the RHS into something more complicated is silly.
    
    1048
    +  Note top level thunks are always lazy, so the binder will have an External Name.
    
    1010 1049
     
    
    1011
    -* For recursive thunks, the Simplifier is unable to float `x-rhs` out of
    
    1050
    +  For recursive thunks, the Simplifier is unable to float `x-rhs` out of
    
    1012 1051
       `x*`'s RHS, because `x*` occurs freely in `x-rhs`, and will just change it
    
    1013
    -  back to the original definition, so we just split non-recursive thunks.
    
    1052
    +  back to the original definition.
    
    1014 1053
     
    
    1015
    -Note [Thunk splitting for top-level binders]
    
    1016
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1017
    -Top-level bindings are never strict. Yet they can be absent, as T14270 shows:
    
    1018
    -
    
    1019
    -  module T14270 (mkTrApp) where
    
    1020
    -  mkTrApp x y
    
    1021
    -    | Just ... <- ... typeRepKind x ...
    
    1022
    -    = undefined
    
    1023
    -    | otherwise
    
    1024
    -    = undefined
    
    1025
    -  typeRepKind = Tick scc undefined
    
    1026
    -
    
    1027
    -(T19180 is a profiling-free test case for this)
    
    1028
    -Note that `typeRepKind` is not exported and its only use site in
    
    1029
    -`mkTrApp` guards a bottoming expression. Thus, demand analysis
    
    1030
    -figures out that `typeRepKind` is absent and splits the thunk to
    
    1031
    -
    
    1032
    -  typeRepKind =
    
    1033
    -    let typeRepKind = Tick scc undefined in
    
    1034
    -    let typeRepKind = absentError in
    
    1035
    -    typeRepKind
    
    1036
    -
    
    1037
    -But now we have a local binding with an External Name
    
    1038
    -(See Note [About the NameSorts]). That will trigger a CoreLint error, which we
    
    1039
    -get around by localising the Id for the auxiliary bindings in 'splitThunk'.
    
    1054
    +(TS5) Notice that (B) CPR-led thunk splitting can be useful:
    
    1055
    +  * when multi-constructor data types are involved, so that the argument-unboxing
    
    1056
    +    mechanism (A) is impotent.  See the example in (TS3), which uses `Maybe`.
    
    1057
    +  * when the binder is only demanded head-strictly, but CPR has nested information.
    
    1058
    +
    
    1059
    +  So (A) and (B) have complementary benefits.  We might do even better by
    
    1060
    +  /combining/ (A) and (B), but at the moment we try (A) first, and then try (B)
    
    1061
    +  if (A) is impotent.  Combining them would be more work.
    
    1040 1062
     -}
    
    1041 1063
     
    
    1042
    --- | See Note [Thunk splitting].
    
    1043
    ---
    
    1044
    --- splitThunk converts the *non-recursive* binding
    
    1064
    +-- | splitThunkStr converts the *non-recursive* binding
    
    1045 1065
     --      x = e
    
    1046 1066
     -- into
    
    1047
    ---      x = let x' = e in
    
    1048
    ---          case x' of I# y -> let x' = I# y in x'
    
    1049
    --- See comments above. Is it not beautifully short?
    
    1050
    --- Moreover, it works just as well when there are
    
    1051
    --- several binders, and if the binders are lifted
    
    1052
    --- E.g.     x = e
    
    1053
    ---     -->  x = let x' = e in
    
    1054
    ---              case x' of (a,b) -> let x' = (a,b)  in x'
    
    1055
    --- Here, x' is a localised version of x, in case x is a
    
    1056
    --- top-level Id with an External Name, because Lint rejects local binders with
    
    1057
    --- External Names; see Note [About the NameSorts] in GHC.Types.Name.
    
    1058
    ---
    
    1059
    --- How can we do thunk-splitting on a top-level binder?  See
    
    1060
    --- Note [Thunk splitting for top-level binders].
    
    1061
    -splitThunk :: WwOpts -> RecFlag -> Var -> Expr Var -> UniqSM [(Var, Expr Var)]
    
    1062
    -splitThunk ww_opts is_rec x rhs
    
    1063
    -  = assert (not (isJoinId x)) $
    
    1064
    -    do { let x' = localiseId x -- See comment above
    
    1065
    -       ; (useful,_args, wrap_fn, fn_arg)
    
    1066
    -           <- mkWWstr_one ww_opts x' NotMarkedStrict
    
    1067
    -       ; let res = [ (x, Let (NonRec x' rhs) (wrap_fn fn_arg)) ]
    
    1068
    -       ; if useful then assertPpr (isNonRec is_rec) (ppr x) -- The thunk must be non-recursive
    
    1069
    -                   return res
    
    1070
    -                   else return [(x, rhs)] }
    1067
    +--      x = let x = e in
    
    1068
    +--          case x of I# y -> I# y
    
    1069
    +-- See Note [Thunk splitting].
    
    1070
    +splitThunkStr :: WwOpts -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr)
    
    1071
    +splitThunkStr ww_opts x rhs
    
    1072
    +  = do { (useful, _transit_vars, wrap_fn, fn_arg) <- mkWWstr_one ww_opts x NotMarkedStrict
    
    1073
    +            -- Use mkWWstr_one: see (TS1) in Note [Thunk splitting]
    
    1074
    +
    
    1075
    +       ; if useful
    
    1076
    +         then return (Just (Let (NonRec x rhs) $
    
    1077
    +                            wrap_fn fn_arg))
    
    1078
    +         else return Nothing }
    
    1079
    +
    
    1080
    +splitThunkCpr :: WwOpts -> Var -> CoreExpr -> UniqSM (Maybe CoreExpr)
    
    1081
    +-- See Note [Thunk splitting]
    
    1082
    +-- Given x* = rhs, where x* is used outermost-strictly, and x* has a (nested) CPR
    
    1083
    +-- property, say Just (Just _), return this new RHS:
    
    1084
    +--     let x' = rhs in
    
    1085
    +--     case x' of { Just p -> case p of { Just q ->   -- Unpacking code
    
    1086
    +--       Just (Just q) }}                             -- Repacking code
    
    1087
    +-- Works for multi-constructor data types, where splitThunkStr will fail
    
    1088
    +splitThunkCpr ww_opts x rhs
    
    1089
    +  = assertPpr (cprTypeArity cpr_type == 0) (ppr x) $   -- It's a thunk
    
    1090
    +    do { (useful, _transit_vars, rebuild_result, unpacker) <- mkWWcpr_one ww_opts x cpr
    
    1091
    +            -- Use mkWWcpr_one: see (TS2) in Note [Thunk splitting]
    
    1092
    +       ; if useful
    
    1093
    +         then return (Just (Let (NonRec x rhs) $
    
    1094
    +                            unpacker rebuild_result))
    
    1095
    +         else return Nothing }
    
    1096
    +
    
    1097
    +  where
    
    1098
    +    cpr_type :: CprType
    
    1099
    +    cpr_type = cprSigType (idCprSig x)
    
    1100
    +    cpr :: Cpr
    
    1101
    +    cpr = cprTypeCpr cpr_type
    
    1102
    +

  • compiler/GHC/Core/Opt/WorkWrap/Utils.hs
    ... ... @@ -8,7 +8,7 @@ A library for the ``worker\/wrapper'' back-end to the strictness analyser
    8 8
     {-# LANGUAGE ViewPatterns #-}
    
    9 9
     
    
    10 10
     module GHC.Core.Opt.WorkWrap.Utils
    
    11
    -   ( WwOpts(..), mkWwBodies, mkWWstr, mkWWstr_one
    
    11
    +   ( WwOpts(..), mkWwBodies, mkWWstr, mkWWstr_one, mkWWcpr_one
    
    12 12
        , needsVoidWorkerArg
    
    13 13
        , DataConPatContext(..)
    
    14 14
        , UnboxingDecision(..), canUnboxArg
    
    ... ... @@ -230,8 +230,7 @@ mkWwBodies opts fun_id ww_arity arg_vars res_ty demands res_cpr
    230 230
             ; let (work_args, work_marks) = unzip work_args_str
    
    231 231
     
    
    232 232
             -- Do CPR w/w.  See Note [Always do CPR w/w]
    
    233
    -        ; (useful2, wrap_fn_cpr, work_fn_cpr)
    
    234
    -              <- mkWWcpr_entry opts res_ty' res_cpr
    
    233
    +        ; (useful2, wrap_fn_cpr, work_fn_cpr) <- mkWWcpr_entry opts res_ty' res_cpr
    
    235 234
     
    
    236 235
             ; let (work_lam_args, work_call_args, work_call_str)
    
    237 236
                     | needsVoidWorkerArg fun_id arg_vars work_args
    
    ... ... @@ -985,6 +984,12 @@ mkWWstr opts args str_marks
    985 984
     --   * wrap_arg assumes work_args are in scope, and builds a ConApp that
    
    986 985
     --        reconstructs the RHS of wrap_var that we pass to the original RHS
    
    987 986
     -- See Note [Worker/wrapper for Strictness and Absence]
    
    987
    +--
    
    988
    +-- E.g. mkWWstr_one (x :: (Int,Int) with strictess saying to unbox the pair and second Int
    
    989
    +--        = ( True                                             -- useful
    
    990
    +--          , [p,q#],                                          -- work_args
    
    991
    +--          , \e -> case x of (p,q) -> case q of I# q# -> e    -- wrap_fn
    
    992
    +--          , (p, I# q) )                                      -- wrap_arg
    
    988 993
     mkWWstr_one :: WwOpts
    
    989 994
                 -> Var
    
    990 995
                 -> StrictnessMark
    
    ... ... @@ -1041,7 +1046,7 @@ unbox_one_arg opts arg_var
    1041 1046
     
    
    1042 1047
                  -- Apply str info to new args. Also remove OtherCon unfoldings so they
    
    1043 1048
                  -- don't end up in lambda binders of the worker.
    
    1044
    -             -- See Note [Never put `OtherCon` unfoldings on lambda binders]
    
    1049
    +             -- See (CBI1) in Note [Core binder IdInfo invariants]
    
    1045 1050
                  arg_ids' = map zapIdUnfolding $
    
    1046 1051
                             zipWithEqual setIdDemandInfo arg_ids ds
    
    1047 1052
     
    
    ... ... @@ -1637,7 +1642,9 @@ mk_res_bndr body_ty = do
    1637 1642
     --   1. A 'WwUse' capturing whether the split does anything useful.
    
    1638 1643
     --   2. The list of transit variables (see the Note).
    
    1639 1644
     --   3. The result builder expression for the wrapper.  The original case binder if not useful.
    
    1640
    ---   4. The result unpacking expression for the worker. 'nop_fn' if not useful.
    
    1645
    +--      Assumes that the transit vars are in scope
    
    1646
    +--   4. The result unpacking expression for the worker. 'nop_fn' if not useful
    
    1647
    +--      Brings the transit vars into scope
    
    1641 1648
     type CprWwResultOne  = (WwUse, OrdList Var,  CoreExpr , CoreExpr -> CoreExpr)
    
    1642 1649
     type CprWwResultMany = (WwUse, OrdList Var, [CoreExpr], CoreExpr -> CoreExpr)
    
    1643 1650
     
    
    ... ... @@ -1711,8 +1718,7 @@ unbox_one_result opts res_bndr
    1711 1718
     move_transit_vars :: [Id] -> (CoreExpr -> CoreExpr -> CoreExpr, CoreExpr)
    
    1712 1719
     move_transit_vars vars
    
    1713 1720
       | [var] <- vars
    
    1714
    -  , let var_ty = idType var
    
    1715
    -  , isUnliftedType var_ty || exprIsHNF (Var var)
    
    1721
    +  , varIsHNF var  -- NB: True of variables of unlifted type
    
    1716 1722
       -- See Note [No unboxed tuple for single, unlifted transit var]
    
    1717 1723
       --   * Wrapper: `unbox scrut alt = (case <scrut> of a -> <alt>)`
    
    1718 1724
       --   * Worker:  `tup = a`
    
    ... ... @@ -1769,7 +1775,7 @@ To achieve said transformation, 'mkWWcpr_entry'
    1769 1775
          what belongs in the case scrutinee of the unpacker through the communicated
    
    1770 1776
          Id `r`: The unpacking expression will be free in that variable.
    
    1771 1777
          (This is a similar contract as that of 'mkWWstr_one' for strict args.)
    
    1772
    -  3. 'mkWWstr_one' produces a bunch of *transit vars*: Those result variables
    
    1778
    +  3. 'mkWWcpr_one' produces a bunch of *transit vars*: Those result variables
    
    1773 1779
          that have to be transferred from the worker to the wrapper, where the
    
    1774 1780
          constructed result can be rebuilt, `a` and `b` above. Part (3) is
    
    1775 1781
          responsible for tupling them up in the worker and taking the tuple apart
    
    ... ... @@ -1816,7 +1822,7 @@ return unboxed instead of in an unboxed singleton tuple:
    1816 1822
         We want  `$wh :: Int# -> [Int]`.
    
    1817 1823
         We'd get `$wh :: Int# -> (# [Int] #)`.
    
    1818 1824
     
    
    1819
    -By considering vars as unlifted that satisfy 'exprIsHNF', we catch (3).
    
    1825
    +By considering vars as unlifted that satisfy 'varIsHNF', we catch (3).
    
    1820 1826
     Why not check for 'exprOkForSpeculation'? Quite perplexingly, evaluated vars
    
    1821 1827
     are not ok-for-spec, see Note [exprOkForSpeculation and evaluated variables].
    
    1822 1828
     For (1) and (2) we would have to look at the term. WW only looks at the
    

  • compiler/GHC/Core/Ppr.hs
    ... ... @@ -637,6 +637,7 @@ instance Outputable IdInfo where
    637 637
         [ (has_prag,         text "InlPrag=" <> pprInlineDebug prag_info)
    
    638 638
         , (has_occ,          text "Occ=" <> ppr occ_info)
    
    639 639
         , (has_dmd,          text "Dmd=" <> ppr dmd_info)
    
    640
    +    , (has_cpr,          text "Cpr=" <> ppr cpr_info)
    
    640 641
         , (has_lbv ,         text "OS=" <> ppr lbv_info)
    
    641 642
         , (has_arity,        text "Arity=" <> int arity)
    
    642 643
         , (has_called_arity, text "CallArity=" <> int called_arity)
    
    ... ... @@ -658,6 +659,9 @@ instance Outputable IdInfo where
    658 659
           lbv_info  = oneShotInfo info
    
    659 660
           has_lbv   = not (hasNoOneShotInfo lbv_info)
    
    660 661
     
    
    662
    +      cpr_info = cprSigInfo info
    
    663
    +      has_cpr = not $ isTopCprSig cpr_info
    
    664
    +
    
    661 665
           arity = arityInfo info
    
    662 666
           has_arity = arity /= 0
    
    663 667
     
    

  • compiler/GHC/Core/Utils.hs
    ... ... @@ -13,7 +13,7 @@ module GHC.Core.Utils (
    13 13
             mkTick, mkTicks, mkTickNoHNF, tickHNFArgs,
    
    14 14
             mkTickCpe,
    
    15 15
             bindNonRec, needsCaseBinding, needsCaseBindingL,
    
    16
    -        mkAltExpr, mkDefaultCase, mkSingleAltCase,
    
    16
    +        mkAltExpr, mkDefaultCase, mkSingleAltCase, mkCaseFromLet,
    
    17 17
     
    
    18 18
             -- * Taking expressions apart
    
    19 19
             findDefault, addDefault, findAlt, isDefaultAlt,
    
    ... ... @@ -29,7 +29,7 @@ module GHC.Core.Utils (
    29 29
             exprIsTrivial, getIdFromTrivialExpr, getIdFromTrivialExpr_maybe,
    
    30 30
             trivial_expr_fold,
    
    31 31
             exprIsDupable, exprIsCheap, exprIsExpandable, exprIsCheapX, CheapAppFun,
    
    32
    -        exprIsHNF, exprOkForSpeculation, exprOkToDiscard, exprOkForSpecEval,
    
    32
    +        exprIsHNF, varIsHNF, exprOkForSpeculation, exprOkToDiscard, exprOkForSpecEval,
    
    33 33
             exprIsWorkFree, exprIsConLike,
    
    34 34
             isCheapApp, isExpandableApp, isSaturatedConApp,
    
    35 35
             exprIsTickedString, exprIsTickedString_maybe,
    
    ... ... @@ -756,8 +756,17 @@ bindNonRec bndr rhs body
    756 756
       | needsCaseBinding (idType bndr) rhs = case_bind
    
    757 757
       | otherwise                          = let_bind
    
    758 758
       where
    
    759
    -    case_bind = mkDefaultCase rhs bndr body
    
    760 759
         let_bind  = Let (NonRec bndr rhs) body
    
    760
    +    case_bind = mkCaseFromLet bndr rhs body
    
    761
    +
    
    762
    +mkCaseFromLet :: Id -> CoreExpr -> CoreExpr -> CoreExpr
    
    763
    +-- Given x=rhs in body, return
    
    764
    +--   case rhs of x { DEFAULT -> body }
    
    765
    +mkCaseFromLet bndr rhs body
    
    766
    +  = mkDefaultCase rhs (mkCaseBndrFromLetBndr bndr) body
    
    767
    +    -- mkCaseBndrFromLetBndr: bndr may have IdInfo suitable for a
    
    768
    +    -- let-binder; see Note [Core binder IdInfo invariants]
    
    769
    +    -- in GHC.Core
    
    761 770
     
    
    762 771
     -- | `needsCaseBinding` tests whether we have to use a @case@ rather than @let@
    
    763 772
     -- binding for this expression as per the invariants of 'CoreExpr': see
    
    ... ... @@ -2630,12 +2639,37 @@ it doesn't have the trickiness of the let-can-float invariant to worry about.
    2630 2639
     exprIsHNF :: CoreExpr -> Bool           -- True => Value-lambda, constructor, PAP
    
    2631 2640
     exprIsHNF = exprIsHNFlike isDataConWorkId isEvaldUnfolding True
    
    2632 2641
     
    
    2642
    +-- | @varIsHNF v@ is the same as @exprIsHNF (Var v)@, but avoids building
    
    2643
    +-- the @Var@ only to take it apart again
    
    2644
    +varIsHNF :: Var -> Bool
    
    2645
    +varIsHNF = varIsHNFlike isDataConWorkId isEvaldUnfolding
    
    2646
    +
    
    2633 2647
     -- | Similar to 'exprIsHNF' but includes CONLIKE functions as well as
    
    2634 2648
     -- data constructors. Conlike arguments are considered interesting by the
    
    2635 2649
     -- inliner.
    
    2636 2650
     exprIsConLike :: CoreExpr -> Bool       -- True => lambda, conlike, PAP
    
    2637 2651
     exprIsConLike = exprIsHNFlike isConLikeId isConLikeUnfolding False
    
    2638 2652
     
    
    2653
    +varIsHNFlike :: (Var -> Bool) -> (Unfolding -> Bool) -> Var -> Bool
    
    2654
    +-- The (Var v) case of exprIsHNFlike
    
    2655
    +{-# INLINE varIsHNFlike #-}
    
    2656
    +varIsHNFlike is_con is_con_unf v
    
    2657
    +  =  idArity v > 0 || is_con v
    
    2658
    +        -- A PAP is a value, as is a nullary constructor, so that [] and ()
    
    2659
    +        -- are values, for example; and (e.g.) primops that don't have unfoldings.
    
    2660
    +        -- This is (id_app_is_value v []) in exprIsHNFlike
    
    2661
    +
    
    2662
    +  || is_con_unf (idUnfolding v)
    
    2663
    +        -- Check the thing's unfolding; it might be bound to a value
    
    2664
    +        --   or to a guaranteed-evaluated variable (isEvaldUnfolding)
    
    2665
    +        --   Contrast with Note [exprOkForSpeculation and evaluated variables]
    
    2666
    +        -- We don't look through loop breakers here, which is a bit conservative
    
    2667
    +        -- but otherwise I worry that if an Id's unfolding is just itself,
    
    2668
    +        -- we could get an infinite loop
    
    2669
    +
    
    2670
    +  || definitelyUnliftedType (idType v)
    
    2671
    +        -- Unlifted binders are always evaluated (#20140)
    
    2672
    +
    
    2639 2673
     -- | Returns true for values or value-like expressions. These are lambdas,
    
    2640 2674
     -- constructors / CONLIKE functions (as determined by the function argument)
    
    2641 2675
     -- or PAPs.
    
    ... ... @@ -2649,20 +2683,8 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
    2649 2683
       = -- pprTraceWith "hnf" (\r -> ppr r <+> ppr e) $
    
    2650 2684
         is_hnf_like e
    
    2651 2685
       where
    
    2652
    -    is_hnf_like (Var v) -- NB: There are no value args at this point
    
    2653
    -      =  id_app_is_value v [] -- Catches nullary constructors,
    
    2654
    -                              --      so that [] and () are values, for example
    
    2655
    -                              -- and (e.g.) primops that don't have unfoldings
    
    2656
    -      || is_con_unf (idUnfolding v)
    
    2657
    -        -- Check the thing's unfolding; it might be bound to a value
    
    2658
    -        --   or to a guaranteed-evaluated variable (isEvaldUnfolding)
    
    2659
    -        --   Contrast with Note [exprOkForSpeculation and evaluated variables]
    
    2660
    -        -- We don't look through loop breakers here, which is a bit conservative
    
    2661
    -        -- but otherwise I worry that if an Id's unfolding is just itself,
    
    2662
    -        -- we could get an infinite loop
    
    2663
    -
    
    2664
    -      || definitelyUnliftedType (idType v)
    
    2665
    -        -- Unlifted binders are always evaluated (#20140)
    
    2686
    +    is_hnf_like (Var v) = varIsHNFlike is_con is_con_unf v
    
    2687
    +                          -- NB: There are no value args at this point
    
    2666 2688
     
    
    2667 2689
         is_hnf_like (Lit lit)
    
    2668 2690
           | isLitRubbish lit = rubbish_lit_result
    
    ... ... @@ -2697,6 +2719,8 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e
    2697 2719
         app_is_value _          _  = False
    
    2698 2720
     
    
    2699 2721
         id_app_is_value id val_args
    
    2722
    +      | null val_args  -- We can get here via (f @Int), say
    
    2723
    +      = varIsHNFlike is_con is_con_unf id
    
    2700 2724
           | Just dc <- isDataConWorkId_maybe id
    
    2701 2725
           , isUnaryClassDataCon  dc
    
    2702 2726
           = all is_hnf_like val_args  -- Look through unary class data cons
    

  • compiler/GHC/CoreToStg/Prep.hs
    ... ... @@ -2366,7 +2366,7 @@ wrapBinds floats body
    2366 2366
         -- See Note [BindInfo and FloatInfo] on whether we pick Case or Let here
    
    2367 2367
         mk_bind f@(Float bind CaseBound _) body
    
    2368 2368
           | NonRec bndr rhs <- bind
    
    2369
    -      = mkDefaultCase rhs bndr body
    
    2369
    +      = mkCaseFromLet bndr rhs body
    
    2370 2370
           | otherwise
    
    2371 2371
           = pprPanic "wrapBinds" (ppr f)
    
    2372 2372
         mk_bind (Float bind _ _) body
    

  • compiler/GHC/HsToCore/Utils.hs
    ... ... @@ -131,19 +131,20 @@ selectMatchVars ps = mapM (uncurry selectMatchVar) ps
    131 131
     
    
    132 132
     selectMatchVar :: Mult -> Pat GhcTc -> DsM Id
    
    133 133
     -- Postcondition: the returned Id has an Internal Name
    
    134
    -selectMatchVar w (BangPat _ pat)    = selectMatchVar w (unLoc pat)
    
    135
    -selectMatchVar w (LazyPat _ pat)    = selectMatchVar w (unLoc pat)
    
    136
    -selectMatchVar w (ParPat _  pat)    = selectMatchVar w (unLoc pat)
    
    137
    -selectMatchVar _w (VarPat _ var)    = return (localiseId (unLoc var))
    
    138
    -                                  -- Note [Localise pattern binders]
    
    139
    -                                  --
    
    140
    -                                  -- Remark: when the pattern is a variable (or
    
    141
    -                                  -- an @-pattern), then w is the same as the
    
    142
    -                                  -- multiplicity stored within the variable
    
    143
    -                                  -- itself. It's easier to pull it from the
    
    144
    -                                  -- variable, so we ignore the multiplicity.
    
    145
    -selectMatchVar _w (AsPat _ var _) = assert (isManyTy _w ) (return (localiseId (unLoc var)))
    
    146
    -selectMatchVar w other_pat        = newSysLocalDs (Scaled w (hsPatType other_pat))
    
    134
    +selectMatchVar w (BangPat _ pat) = selectMatchVar w (unLoc pat)
    
    135
    +selectMatchVar w (LazyPat _ pat) = selectMatchVar w (unLoc pat)
    
    136
    +selectMatchVar w (ParPat _  pat) = selectMatchVar w (unLoc pat)
    
    137
    +
    
    138
    +selectMatchVar _w (VarPat _ var)  = return (mkLamBndrFromLetBndr (unLoc var))
    
    139
    +selectMatchVar _w (AsPat _ var _) = assert (isManyTy _w ) $
    
    140
    +                                    return (mkLamBndrFromLetBndr (unLoc var))
    
    141
    +  -- mkLamBndrFromLetBndr: see Note [Localise pattern binders]
    
    142
    +  --
    
    143
    +  -- Remark: when the pattern is a variable (or an @-pattern), then w is
    
    144
    +  -- the same as the multiplicity stored within the variable
    
    145
    +  -- itself. It's easier to pull it from the variable, so we ignore _w here
    
    146
    +
    
    147
    +selectMatchVar w other_pat = newSysLocalDs (Scaled w (hsPatType other_pat))
    
    147 148
     
    
    148 149
     {- Note [Localise pattern binders]
    
    149 150
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -167,9 +168,15 @@ It's quite rare for this to actually *happen* -- the only case I know
    167 168
     of is tc003 compiled with the 'hpc' way -- but that only makes it
    
    168 169
     all the more annoying.
    
    169 170
     
    
    170
    -To avoid this, we craftily call 'localiseId' in the desugarer, which
    
    171
    -simply turns the External Name for the Id into an Internal one, but
    
    172
    -doesn't change the unique.  So the desugarer produces this:
    
    171
    +Very similarly, we might have
    
    172
    +    {-# NOINLINE a #-}
    
    173
    +    [Just a] = e
    
    174
    +and we don't allow a NOINLINE on case-alternative binders.
    
    175
    +
    
    176
    +To avoid these problems, we craftily call `mkLamBndrFromLetBndr` in the
    
    177
    +desugarer, which simply turns the External Name for the Id into an Internal one,
    
    178
    +and expunges bad IdInfo, but doesn't change the unique.  So the desugarer produces
    
    179
    +this:
    
    173 180
                  M.a{r8} = case e of (v:_) ->
    
    174 181
                            case v of Just a{r8} -> M.a{r8}
    
    175 182
     The unique is still 'r8', but the binding site in the pattern
    
    ... ... @@ -265,8 +272,14 @@ bindMatchId new mid body
    265 272
     -- Used to force variables when desugaring strict binders. It's crucial that the
    
    266 273
     -- variable is shadowed by the case binder. See Wrinkle 1 in
    
    267 274
     -- Note [Desugar Strict binds] in GHC.HsToCore.Binds.
    
    275
    +--
    
    268 276
     seqVar :: Var -> CoreExpr -> CoreExpr
    
    269
    -seqVar var body = mkDefaultCase (Var var) var body
    
    277
    +seqVar var body = mkCaseFromLet var (Var var) body
    
    278
    +    -- mkCaseFromLet: the case binder has the same Name (and hence Unique), type
    
    279
    +    -- and multiplicity as `var`, but not its IdInfo: `var` may have, say, a
    
    280
    +    -- NOINLINE pragma, but a case binder must not; see Note [Core binder IdInfo
    
    281
    +    -- invariants] in GHC.Core, and #27965.  E.g. `res1` in
    
    282
    +    -- GHC.Internal.Control.Monad.ST.Lazy.Imp has a NOINLINE pragma.
    
    270 283
     
    
    271 284
     mkCoLetMatchResult :: HasDebugCallStack => CoreBind -> MatchResult CoreExpr -> MatchResult CoreExpr
    
    272 285
     mkCoLetMatchResult bind = fmap (mkCoreLet bind)
    
    ... ... @@ -684,7 +697,7 @@ mkSelectorBinds ticks pat ctx val_expr
    684 697
         binders = collectPatBinders CollNoDictBinders pat'
    
    685 698
         ticks'  = ticks ++ repeat []
    
    686 699
     
    
    687
    -    local_binders = map localiseId binders      -- See Note [Localise pattern binders]
    
    700
    +    local_binders = map mkLamBndrFromLetBndr binders  -- See Note [Localise pattern binders]
    
    688 701
     
    
    689 702
     strip_bangs :: LPat (GhcPass p) -> LPat (GhcPass p)
    
    690 703
     -- Remove outermost bangs and parens
    

  • compiler/GHC/Types/Cpr.hs
    ... ... @@ -8,6 +8,7 @@
    8 8
     module GHC.Types.Cpr (
    
    9 9
         Cpr (ConCpr), topCpr, botCpr, flatConCpr, asConCpr,
    
    10 10
         CprType (..), topCprType, botCprType, flatConCprType,
    
    11
    +    cprTypeArity, cprTypeCpr,
    
    11 12
         lubCprType, applyCprTy, abstractCprTy, trimCprTy,
    
    12 13
         UnpackConFieldsResult (..), unpackConFieldsCpr,
    
    13 14
         CprSig (..), topCprSig, isTopCprSig, mkCprSigForArity, mkCprSig,
    
    ... ... @@ -106,6 +107,12 @@ data CprType
    106 107
                           --   'ct_arty' arguments
    
    107 108
       }
    
    108 109
     
    
    110
    +cprTypeArity :: CprType -> Arity
    
    111
    +cprTypeArity = ct_arty
    
    112
    +
    
    113
    +cprTypeCpr :: CprType -> Cpr
    
    114
    +cprTypeCpr = ct_cpr
    
    115
    +
    
    109 116
     instance Eq CprType where
    
    110 117
       a == b =  ct_cpr a == ct_cpr b
    
    111 118
              && (ct_arty a == ct_arty b || ct_cpr a == topCpr)
    
    ... ... @@ -167,7 +174,7 @@ seqCprTy (CprType _ cpr) = seqCpr cpr
    167 174
     
    
    168 175
     -- | The arity of the wrapped 'CprType' is the arity at which it is safe
    
    169 176
     -- to unleash. See Note [Understanding DmdType and DmdSig] in "GHC.Types.Demand"
    
    170
    -newtype CprSig = CprSig { getCprSig :: CprType }
    
    177
    +newtype CprSig = CprSig { cprSigType :: CprType }
    
    171 178
       deriving (Eq, Binary)
    
    172 179
     
    
    173 180
     -- | Turns a 'CprType' computed for the particular 'Arity' into a 'CprSig'
    

  • compiler/GHC/Types/Id.hs
    ... ... @@ -94,6 +94,7 @@ module GHC.Types.Id (
    94 94
             idArity,
    
    95 95
             idCallArity, idFunRepArity,
    
    96 96
             idSpecialisation, idCoreRules, idHasRules,
    
    97
    +        lamBndrInfoOK,
    
    97 98
             idCafInfo, idLFInfo_maybe,
    
    98 99
             idOneShotInfo,
    
    99 100
             idOccInfo,
    
    ... ... @@ -103,6 +104,7 @@ module GHC.Types.Id (
    103 104
     
    
    104 105
             -- ** Writing 'IdInfo' fields
    
    105 106
             setIdUnfolding, zapIdUnfolding, setCaseBndrEvald,
    
    107
    +        mkCaseBndrFromLetBndr, mkLamBndrFromLetBndr,
    
    106 108
             setIdArity,
    
    107 109
             setIdCallArity,
    
    108 110
     
    
    ... ... @@ -881,10 +883,33 @@ setCaseBndrEvald :: StrictnessMark -> Id -> Id
    881 883
     -- itself, and any pattern-bound variables that are argument of a
    
    882 884
     -- strict constructor.  It just marks the variable as already-evaluated,
    
    883 885
     -- so that (for example) a subsequent 'seq' can be dropped
    
    886
    +-- See Note [Evaluated-ness on case binders] in GHC.Core
    
    884 887
     setCaseBndrEvald str id
    
    885 888
       | isMarkedStrict str = id `setIdUnfolding` evaldUnfolding
    
    886 889
       | otherwise          = id
    
    887 890
     
    
    891
    +-- | Just like `mkLamBndrFromLetBndr` but in addition
    
    892
    +--   record that it is evaluated.
    
    893
    +-- See Note [Core binder IdInfo invariants] in GHC.Core
    
    894
    +mkCaseBndrFromLetBndr :: Id -> Id
    
    895
    +mkCaseBndrFromLetBndr id
    
    896
    +  = mkLamBndrFromLetBndr id `setIdUnfolding` evaldUnfolding
    
    897
    +
    
    898
    +-- | Make a lambda binder from a binder that might have been let-bound, e.g.
    
    899
    +--      let x = rhs in body   ==>   (\x. body) rhs
    
    900
    +-- The let-binder may have IdInfo (an unfolding, an inline pragma, RULES)
    
    901
    +-- that a case binder must not have, so we keep its Name, type, multiplicity
    
    902
    +-- and IdDetails, plus OccInfo and DemandInfo, but reset its other IdInfo.
    
    903
    +-- See Note [Core binder IdInfo invariants] in GHC.Core
    
    904
    +mkLamBndrFromLetBndr :: Id -> Id
    
    905
    +mkLamBndrFromLetBndr id
    
    906
    +  = localiseId id `setIdInfo` zapped_info
    
    907
    +  where
    
    908
    +    info = idInfo id
    
    909
    +    zapped_info = vanillaIdInfo `setOccInfo`    occInfo info
    
    910
    +                                `setDemandInfo` (demandInfo info)
    
    911
    +
    
    912
    +
    
    888 913
     -- | Similar to trimUnfolding, but also removes evaldness info.
    
    889 914
     zapIdUnfolding :: Id -> Id
    
    890 915
     zapIdUnfolding v
    
    ... ... @@ -905,6 +930,32 @@ idCoreRules id = ruleInfoRules (idSpecialisation id)
    905 930
     idHasRules :: Id -> Bool
    
    906 931
     idHasRules id = not (isEmptyRuleInfo (idSpecialisation id))
    
    907 932
     
    
    933
    +-- | True <=> the Id has only the IdInfo permitted on a lambda, case, or
    
    934
    +-- case-alternative binder.  See the table in Note [Core binder IdInfo invariants]
    
    935
    +-- in GHC.Core; Core Lint checks this invariant.
    
    936
    +--
    
    937
    +-- We allow occurrence info, demand info, and one-shot info (which the table
    
    938
    +-- permits only on lambda binders, but a case binder may be about to become one).
    
    939
    +-- We also allow an `OtherCon []` unfolding; lambda binders must have no unfolding
    
    940
    +-- at all, but Lint checks that separately.
    
    941
    +lamBndrInfoOK :: Id -> Bool
    
    942
    +lamBndrInfoOK id
    
    943
    +  =  isDefaultInlinePragma (inlinePragInfo info)
    
    944
    +  && isNopSig (dmdSigInfo info)
    
    945
    +  && isTopCprSig (cprSigInfo info)
    
    946
    +  && mayHaveCafRefs (cafInfo info)
    
    947
    +  && arityInfo info == unknownArity
    
    948
    +  && callArityInfo info == unknownArity
    
    949
    +  && isEmptyRuleInfo (ruleInfo info)
    
    950
    +  && unf_ok (realUnfoldingInfo info)
    
    951
    +  where
    
    952
    +    info = idInfo id
    
    953
    +
    
    954
    +    -- See Note [Evaluated-ness on case binders] in GHC.Core
    
    955
    +    unf_ok NoUnfolding   = True
    
    956
    +    unf_ok (OtherCon []) = True
    
    957
    +    unf_ok _             = False
    
    958
    +
    
    908 959
     setIdSpecialisation :: Id -> RuleInfo -> Id
    
    909 960
     setIdSpecialisation id spec_info = modifyIdInfo (`setRuleInfo` spec_info) id
    
    910 961
     
    

  • compiler/GHC/Utils/Monad.hs
    ... ... @@ -16,7 +16,7 @@ module GHC.Utils.Monad
    16 16
             , mapAccumLM
    
    17 17
             , mapSndM
    
    18 18
             , concatMapM
    
    19
    -        , mapMaybeM
    
    19
    +        , mapMaybeM, orElseM
    
    20 20
             , anyM, allM, orM
    
    21 21
             , foldlM, foldlM_, foldrM, foldMapM
    
    22 22
             , whenM, unlessM
    
    ... ... @@ -250,6 +250,14 @@ unlessM :: Monad m => m Bool -> m () -> m ()
    250 250
     unlessM condM acc = do { cond <- condM
    
    251 251
                            ; unless cond acc }
    
    252 252
     
    
    253
    +-- | Monadic version of @orElse@
    
    254
    +infixr 4 `orElseM`
    
    255
    +orElseM :: Monad m => m (Maybe a) -> m a -> m a
    
    256
    +orElseM ma def = do { mb_r <- ma
    
    257
    +                    ; case mb_r of
    
    258
    +                        Just x  -> return x
    
    259
    +                        Nothing -> def }
    
    260
    +
    
    253 261
     -- | Like 'filterM', only it reverses the sense of the test.
    
    254 262
     filterOutM :: (Applicative m) => (a -> m Bool) -> [a] -> m [a]
    
    255 263
     filterOutM p =
    

  • testsuite/tests/simplCore/should_compile/T15445.stderr
    ... ... @@ -4,6 +4,7 @@ Rule fired: Int# -> Integer -> Int# (GHC.Internal.Bignum.Integer)
    4 4
     Rule fired: USPEC plusTwoRec @Int (T15445a)
    
    5 5
     Rule fired: USPEC $fShowList @Int (GHC.Internal.Show)
    
    6 6
     Rule fired: Class op >> (BUILTIN)
    
    7
    +Rule fired: Class op show (BUILTIN)
    
    7 8
     Rule fired: USPEC plusTwoRec @Int (T15445a)
    
    8 9
     Rule fired: Class op enumFromTo (BUILTIN)
    
    9 10
     Rule fired: eftInt (GHC.Internal.Enum)
    
    ... ... @@ -11,7 +12,6 @@ Rule fired: Class op show (BUILTIN)
    11 12
     Rule fired: USPEC plusTwoRec @Int (T15445a)
    
    12 13
     Rule fired: Class op enumFromTo (BUILTIN)
    
    13 14
     Rule fired: eftInt (GHC.Internal.Enum)
    
    14
    -Rule fired: Class op show (BUILTIN)
    
    15 15
     Rule fired: eftIntList (GHC.Internal.Enum)
    
    16 16
     Rule fired: ># (BUILTIN)
    
    17 17
     Rule fired: ==# (BUILTIN)
    

  • testsuite/tests/simplCore/should_compile/T27962.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +
    
    3
    +module T27962 where
    
    4
    +
    
    5
    +import GHC.Exts
    
    6
    +
    
    7
    +newtype N = MkN Int
    
    8
    +
    
    9
    +-- After inlining myRun, we get  (runRW# @LiftedRep @Int |> co) (\_ -> ...)
    
    10
    +-- and the simplifier pushes co into the argument of runRW#.
    
    11
    +-- #27962: the simplifier dropped that cast, yielding ill-typed Core
    
    12
    +myRun :: (State# RealWorld -> N) -> N
    
    13
    +myRun = coerce (runRW# :: (State# RealWorld -> Int) -> Int)
    
    14
    +{-# INLINE myRun #-}
    
    15
    +
    
    16
    +foo x = myRun (\_ -> MkN (x+1))

  • testsuite/tests/simplCore/should_compile/T27963.hs
    1
    +{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
    
    2
    +
    
    3
    +module T27963 where
    
    4
    +
    
    5
    +-- This test is pretty delicate because it relies on a function
    
    6
    +-- that has BOTH a rule AND an INLINE pragma
    
    7
    +--
    
    8
    +-- See #27963 for some discussion
    
    9
    +--
    
    10
    +-- Still, it should (just) work
    
    11
    +
    
    12
    +import GHC.Exts
    
    13
    +
    
    14
    +newtype N = MkN Int
    
    15
    +
    
    16
    +f x = x
    
    17
    +{-# INLINE f #-}
    
    18
    +{-# RULES "f/42" forall x. f x = 42::Int #-}
    
    19
    +
    
    20
    +-- The rule "f/42" should fire in both h1 and h2
    
    21
    +h1,h2 :: N -> Int
    
    22
    +h1 n = f (coerce n) + 0
    
    23
    +h2 n = (coerce f) n + 0   -- #27963: the rule did not fire here

  • testsuite/tests/simplCore/should_compile/T27963.stderr
    1
    +Rule fired: f/42 (T27963)
    
    2
    +Rule fired: f/42 (T27963)

  • testsuite/tests/simplCore/should_compile/T27965.hs
    1
    +-- Lint checks that we don't produce a lambda-binder
    
    2
    +-- with a NOINLINE pragma on it (#27965)
    
    3
    +
    
    4
    +module T27965 where
    
    5
    +
    
    6
    +h fm s = let {-# NOINLINE res1 #-}
    
    7
    +             !res1 = fm s
    
    8
    +             !(f, s') = res1
    
    9
    +         in (f+1::Int, s')

  • testsuite/tests/simplCore/should_compile/T27965a.hs
    1
    +{-# LANGUAGE MonoLocalBinds #-}
    
    2
    +module M where
    
    3
    +
    
    4
    +f :: (Int, Int) -> Int
    
    5
    +f p = let {-# NOINLINE a #-}
    
    6
    +          (a, b) = p
    
    7
    +      in a + b

  • testsuite/tests/simplCore/should_compile/T28158.hs
    1
    +module T28158 where
    
    2
    +
    
    3
    +-- Here we miss CPR on y
    
    4
    +-- It's a bit of an obscure example, but still; see #28158
    
    5
    +f :: a -> a -> a -> [Int] -> [Int]
    
    6
    +f a b c xs = let y = case xs of
    
    7
    +                      [0] -> Just a
    
    8
    +                      [1] -> Just b
    
    9
    +                      _   -> Just c
    
    10
    +             in h y (case y of
    
    11
    +                       Just z  -> reverse (reverse (reverse (
    
    12
    +                                  reverse (reverse (reverse (
    
    13
    +                                  reverse (reverse xs)))))))
    
    14
    +                       Nothing -> xs++xs)
    
    15
    +
    
    16
    +h :: Maybe a -> b -> b
    
    17
    +h (Just y) e = h Nothing e
    
    18
    +h Nothing  e = e

  • testsuite/tests/simplCore/should_compile/T28158.stderr
    1
    +
    
    2
    +==================== Tidy Core ====================
    
    3
    +Result size of Tidy Core
    
    4
    +  = {terms: 72, types: 76, coercions: 0, joins: 1/1}
    
    5
    +
    
    6
    +Rec {
    
    7
    +h = \ @a @b ds e ->
    
    8
    +      case ds of {
    
    9
    +        Nothing -> e;
    
    10
    +        Just y -> h Nothing e
    
    11
    +      }
    
    12
    +end Rec }
    
    13
    +
    
    14
    +f = \ @a a1 b c xs ->
    
    15
    +      join {
    
    16
    +        $j ww
    
    17
    +          = h (Just ww)
    
    18
    +              (reverse1
    
    19
    +                 (reverse1
    
    20
    +                    (reverse1
    
    21
    +                       (reverse1
    
    22
    +                          (reverse1 (reverse1 (reverse1 (reverse xs) []) []) []) [])
    
    23
    +                       [])
    
    24
    +                    [])
    
    25
    +                 []) } in
    
    26
    +      case xs of {
    
    27
    +        [] -> jump $j c;
    
    28
    +        : ds ds1 ->
    
    29
    +          case ds of { I# ds2 ->
    
    30
    +          case ds2 of {
    
    31
    +            __DEFAULT -> jump $j c;
    
    32
    +            0# ->
    
    33
    +              case ds1 of {
    
    34
    +                [] -> jump $j a1;
    
    35
    +                : ipv ipv1 -> jump $j c
    
    36
    +              };
    
    37
    +            1# ->
    
    38
    +              case ds1 of {
    
    39
    +                [] -> jump $j b;
    
    40
    +                : ipv ipv1 -> jump $j c
    
    41
    +              }
    
    42
    +          }
    
    43
    +          }
    
    44
    +      }
    
    45
    +
    
    46
    +
    
    47
    +

  • testsuite/tests/simplCore/should_compile/all.T
    ... ... @@ -631,3 +631,9 @@ test('T27936', normal, compile, [''])
    631 631
     test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques'])
    
    632 632
     test('T27892', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
    
    633 633
     test('T28095a', grep_errmsg(r'^f = g$'), compile, ['-O -ddump-cse -dsuppress-all -dsuppress-uniques'])
    
    634
    +test('T27962', normal, compile, [''])
    
    635
    +test('T27962a', [extra_files(['T27962.hs'])], multimod_compile, ['T27962', '-v0 -O'])
    
    636
    +test('T27963', [grep_errmsg(r'f/42')], compile, ['-O -ddump-rule-firings'])
    
    637
    +test('T27965', normal, compile, ['-O'])
    
    638
    +test('T27965a', normal, compile, ['-O'])
    
    639
    +test('T28158', normal, compile, ['-O -dno-typeable-binds -dsuppress-all -dsuppress-uniques -ddump-simpl'])