X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Fstranal%2FDmdAnal.lhs;h=9ac5e38c00859342928a4994935232d3c8fefe6b;hb=d1c1b7d0e7b94ede238845c91f58582bad3b3ef3;hp=7bb8134fcadf5317bbd656abeb0453051997d39d;hpb=e74067c6d49eafb291f60e8fdec01b256e9c7c9d;p=ghc-hetmet.git diff --git a/ghc/compiler/stranal/DmdAnal.lhs b/ghc/compiler/stranal/DmdAnal.lhs index 7bb8134..9ac5e38 100644 --- a/ghc/compiler/stranal/DmdAnal.lhs +++ b/ghc/compiler/stranal/DmdAnal.lhs @@ -13,34 +13,38 @@ module DmdAnal ( dmdAnalPgm, dmdAnalTopRhs, #include "HsVersions.h" -import CmdLineOpts ( DynFlags, DynFlag(..), opt_MaxWorkerArgs ) +import DynFlags ( DynFlags, DynFlag(..) ) +import StaticFlags ( opt_MaxWorkerArgs ) import NewDemand -- All of it import CoreSyn import PprCore -import CoreUtils ( exprIsValue, exprArity ) +import CoreUtils ( exprIsValue, exprIsTrivial, exprArity ) import DataCon ( dataConTyCon ) import TyCon ( isProductTyCon, isRecursiveTyCon ) import Id ( Id, idType, idInlinePragma, - isDataConId, isGlobalId, idArity, + isDataConWorkId, isGlobalId, idArity, #ifdef OLD_STRICTNESS - idDemandInfo, idStrictness, idCprInfo, + idDemandInfo, idStrictness, idCprInfo, idName, #endif idNewStrictness, idNewStrictness_maybe, setIdNewStrictness, idNewDemandInfo, idNewDemandInfo_maybe, - setIdNewDemandInfo, idName + setIdNewDemandInfo ) #ifdef OLD_STRICTNESS import IdInfo ( newStrictnessFromOld, newDemand ) #endif import Var ( Var ) import VarEnv +import TysWiredIn ( unboxedPairDataCon ) +import TysPrim ( realWorldStatePrimTy ) import UniqFM ( plusUFM_C, addToUFM_Directly, lookupUFM_Directly, keysUFM, minusUFM, ufmToList, filterUFM ) -import Type ( isUnLiftedType ) +import Type ( isUnLiftedType, coreEqType ) import CoreLint ( showPass, endPass ) import Util ( mapAndUnzip, mapAccumL, mapAccumR, lengthIs ) -import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel, isNeverActive ) +import BasicTypes ( Arity, TopLevelFlag(..), isTopLevel, isNeverActive, + RecFlag(..), isRec ) import Maybes ( orElse, expectJust ) import Outputable \end{code} @@ -52,10 +56,6 @@ To think about * Consider f x = x+1 `fatbar` error (show x) We'd like to unbox x, even if that means reboxing it in the error case. -\begin{code} -instance Outputable TopLevelFlag where - ppr flag = empty -\end{code} %************************************************************************ %* * @@ -89,8 +89,8 @@ dmdAnalTopBind :: SigEnv -> (SigEnv, CoreBind) dmdAnalTopBind sigs (NonRec id rhs) = let - ( _, _, (_, rhs1)) = dmdAnalRhs TopLevel sigs (id, rhs) - (sigs2, _, (id2, rhs2)) = dmdAnalRhs TopLevel sigs (id, rhs1) + ( _, _, (_, rhs1)) = dmdAnalRhs TopLevel NonRecursive sigs (id, rhs) + (sigs2, _, (id2, rhs2)) = dmdAnalRhs TopLevel NonRecursive sigs (id, rhs1) -- Do two passes to improve CPR information -- See comments with ignore_cpr_info in mk_sig_ty -- and with extendSigsWithLam @@ -205,7 +205,7 @@ dmdAnal sigs dmd (Lam var body) in (deferType lam_ty, Lam var' body') -dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)]) +dmdAnal sigs dmd (Case scrut case_bndr ty [alt@(DataAlt dc,bndrs,rhs)]) | let tycon = dataConTyCon dc, isProductTyCon tycon, not (isRecursiveTyCon tycon) @@ -251,36 +251,36 @@ dmdAnal sigs dmd (Case scrut case_bndr [alt@(DataAlt dc,bndrs,rhs)]) (scrut_ty, scrut') = dmdAnal sigs scrut_dmd scrut in - (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' [alt']) + (alt_ty1 `bothType` scrut_ty, Case scrut' case_bndr' ty [alt']) -dmdAnal sigs dmd (Case scrut case_bndr alts) +dmdAnal sigs dmd (Case scrut case_bndr ty alts) = let (alt_tys, alts') = mapAndUnzip (dmdAnalAlt sigs dmd) alts (scrut_ty, scrut') = dmdAnal sigs evalDmd scrut (alt_ty, case_bndr') = annotateBndr (foldr1 lubType alt_tys) case_bndr in -- pprTrace "dmdAnal:Case" (ppr alts $$ ppr alt_tys) - (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' alts') + (alt_ty `bothType` scrut_ty, Case scrut' case_bndr' ty alts') dmdAnal sigs dmd (Let (NonRec id rhs) body) = let - (sigs', lazy_fv, (id1, rhs')) = dmdAnalRhs NotTopLevel sigs (id, rhs) + (sigs', lazy_fv, (id1, rhs')) = dmdAnalRhs NotTopLevel NonRecursive sigs (id, rhs) (body_ty, body') = dmdAnal sigs' dmd body (body_ty1, id2) = annotateBndr body_ty id1 body_ty2 = addLazyFVs body_ty1 lazy_fv in -#ifdef DEBUG - -- If the actual demand is better than the vanilla - -- demand, we might do better to re-analyse with the - -- stronger demand. - (let vanilla_dmd = vanillaCall (idArity id) - actual_dmd = idNewDemandInfo id2 - in - if actual_dmd `betterDemand` vanilla_dmd && actual_dmd /= vanilla_dmd then - pprTrace "dmdLet: better demand" (ppr id <+> vcat [text "vanilla" <+> ppr vanilla_dmd, - text "actual" <+> ppr actual_dmd]) - else \x -> x) -#endif + -- If the actual demand is better than the vanilla call + -- demand, you might think that we might do better to re-analyse + -- the RHS with the stronger demand. + -- But (a) That seldom happens, because it means that *every* path in + -- the body of the let has to use that stronger demand + -- (b) It often happens temporarily in when fixpointing, because + -- the recursive function at first seems to place a massive demand. + -- But we don't want to go to extra work when the function will + -- probably iterate to something less demanding. + -- In practice, all the times the actual demand on id2 is more than + -- the vanilla call demand seem to be due to (b). So we don't + -- bother to re-analyse the RHS. (body_ty2, Let (NonRec id2 rhs') body') dmdAnal sigs dmd (Let (Rec pairs) body) @@ -305,8 +305,30 @@ dmdAnalAlt sigs dmd (con,bndrs,rhs) = let (rhs_ty, rhs') = dmdAnal sigs dmd rhs (alt_ty, bndrs') = annotateBndrs rhs_ty bndrs - in - (alt_ty, (con, bndrs', rhs')) + final_alt_ty | io_hack_reqd = alt_ty `lubType` topDmdType + | otherwise = alt_ty + + -- There's a hack here for I/O operations. Consider + -- case foo x s of { (# s, r #) -> y } + -- Is this strict in 'y'. Normally yes, but what if 'foo' is an I/O + -- operation that simply terminates the program (not in an erroneous way)? + -- In that case we should not evaluate y before the call to 'foo'. + -- Hackish solution: spot the IO-like situation and add a virtual branch, + -- as if we had + -- case foo x s of + -- (# s, r #) -> y + -- other -> return () + -- So the 'y' isn't necessarily going to be evaluated + -- + -- A more complete example where this shows up is: + -- do { let len = ; + -- ; when (...) (exitWith ExitSuccess) + -- ; print len } + + io_hack_reqd = con == DataAlt unboxedPairDataCon && + idType (head bndrs) `coreEqType` realWorldStatePrimTy + in + (final_alt_ty, (con, bndrs', rhs')) \end{code} %************************************************************************ @@ -333,19 +355,27 @@ dmdFix top_lvl sigs orig_pairs -> [(Id,CoreExpr)] -> (SigEnv, DmdEnv, [(Id,CoreExpr)]) loop n sigs pairs - | all (same_sig sigs sigs') bndrs + | found_fixpoint = (sigs', lazy_fv, pairs') -- Note: use pairs', not pairs. pairs' is the result of -- processing the RHSs with sigs (= sigs'), whereas pairs -- is the result of processing the RHSs with the *previous* -- iteration of sigs. - | n >= 10 = pprTrace "dmdFix loop" (ppr n <+> (vcat + + | n >= 10 = pprTrace "dmdFix loop" (ppr n <+> (vcat [ text "Sigs:" <+> ppr [(id,lookup sigs id, lookup sigs' id) | (id,_) <- pairs], text "env:" <+> ppr (ufmToList sigs), text "binds:" <+> pprCoreBinding (Rec pairs)])) - (emptySigEnv, emptyDmdEnv, orig_pairs) -- Safe output + (emptySigEnv, lazy_fv, orig_pairs) -- Safe output + -- The lazy_fv part is really important! orig_pairs has no strictness + -- info, including nothing about free vars. But if we have + -- letrec f = ....y..... in ...f... + -- where 'y' is free in f, we must record that y is mentioned, + -- otherwise y will get recorded as absent altogether + | otherwise = loop (n+1) sigs' pairs' where + found_fixpoint = all (same_sig sigs sigs') bndrs -- Use the new signature to do the next pair -- The occurrence analyser has arranged them in a good order -- so this can significantly reduce the number of iterations needed @@ -358,7 +388,7 @@ dmdFix top_lvl sigs orig_pairs ((sigs', lazy_fv'), pair') -- ) where - (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl sigs (id,rhs) + (sigs', lazy_fv1, pair') = dmdAnalRhs top_lvl Recursive sigs (id,rhs) lazy_fv' = plusUFM_C both lazy_fv lazy_fv1 -- old_sig = lookup sigs id -- new_sig = lookup sigs' id @@ -374,20 +404,22 @@ dmdFix top_lvl sigs orig_pairs -- since it is part of the strictness signature initialSig id = idNewStrictness_maybe id `orElse` botSig -dmdAnalRhs :: TopLevelFlag +dmdAnalRhs :: TopLevelFlag -> RecFlag -> SigEnv -> (Id, CoreExpr) -> (SigEnv, DmdEnv, (Id, CoreExpr)) -- Process the RHS of the binding, add the strictness signature -- to the Id, and augment the environment with the signature as well. -dmdAnalRhs top_lvl sigs (id, rhs) +dmdAnalRhs top_lvl rec_flag sigs (id, rhs) = (sigs', lazy_fv, (id', rhs')) where arity = idArity id -- The idArity should be up to date -- The simplifier was run just beforehand (rhs_dmd_ty, rhs') = dmdAnal sigs (vanillaCall arity) rhs - (lazy_fv, sig_ty) = WARN( arity /= dmdTypeDepth rhs_dmd_ty, ppr id ) - mkSigTy top_lvl id rhs rhs_dmd_ty + (lazy_fv, sig_ty) = WARN( arity /= dmdTypeDepth rhs_dmd_ty && not (exprIsTrivial rhs), ppr id ) + -- The RHS can be eta-reduced to just a variable, + -- in which case we should not complain. + mkSigTy top_lvl rec_flag id rhs rhs_dmd_ty id' = id `setIdNewStrictness` sig_ty sigs' = extendSigEnv top_lvl sigs id sig_ty \end{code} @@ -404,8 +436,8 @@ mkTopSigTy :: CoreExpr -> DmdType -> StrictSig -- NB: not used for never-inline things; hence False mkTopSigTy rhs dmd_ty = snd (mk_sig_ty False False rhs dmd_ty) -mkSigTy :: TopLevelFlag -> Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig) -mkSigTy top_lvl id rhs dmd_ty +mkSigTy :: TopLevelFlag -> RecFlag -> Id -> CoreExpr -> DmdType -> (DmdEnv, StrictSig) +mkSigTy top_lvl rec_flag id rhs dmd_ty = mk_sig_ty never_inline thunk_cpr_ok rhs dmd_ty where never_inline = isNeverActive (idInlinePragma id) @@ -415,6 +447,7 @@ mkSigTy top_lvl id rhs dmd_ty thunk_cpr_ok | isTopLevel top_lvl = False -- Top level things don't get -- their demandInfo set at all + | isRec rec_flag = False -- Ditto recursive things | Just dmd <- maybe_id_dmd = isStrictDmd dmd | otherwise = True -- Optimistic, first time round -- See notes below @@ -477,11 +510,10 @@ We can't start with 'not-demanded' because then consider In the first iteration we'd have no demand info for x, so assume not-demanded; then we'd get TopRes for f's CPR info. Next iteration -we'd see that t was demanded, and so give it the CPR property, but -by now f has TopRes, so it will stay TopRes. -ever_in -Instead, with the Nothing setting the first time round, we say -'yes t is demanded' the first time. +we'd see that t was demanded, and so give it the CPR property, but by +now f has TopRes, so it will stay TopRes. Instead, with the Nothing +setting the first time round, we say 'yes t is demanded' the first +time. However, this does mean that for non-recursive bindings we must iterate twice to be sure of not getting over-optimistic CPR info, @@ -719,10 +751,15 @@ extendSigEnvList = extendVarEnvList extendSigsWithLam :: SigEnv -> Id -> SigEnv -- Extend the SigEnv when we meet a lambda binder --- If the binder is marked demanded with a product demand, then give it a CPR +-- If the binder is marked demanded with a product demand, then give it a CPR -- signature, because in the likely event that this is a lambda on a fn defn -- [we only use this when the lambda is being consumed with a call demand], --- it'll be w/w'd and so it will be CPR-ish. +-- it'll be w/w'd and so it will be CPR-ish. E.g. +-- f = \x::(Int,Int). if ...strict in x... then +-- x +-- else +-- (a,b) +-- We want f to have the CPR property because x does, by the time f has been w/w'd -- -- NOTE: see notes [CPR-AND-STRICTNESS] -- @@ -736,9 +773,6 @@ extendSigsWithLam sigs id Just (Eval (Prod ds)) -> extendVarEnv sigs id (cprSig, NotTopLevel) other -> sigs -cprSig :: StrictSig -cprSig = StrictSig (mkDmdType emptyVarEnv [] RetCPR) - dmdTransform :: SigEnv -- The strictness environment -> Id -- The function @@ -750,7 +784,7 @@ dmdTransform :: SigEnv -- The strictness environment dmdTransform sigs var dmd ------ DATA CONSTRUCTOR - | isDataConId var -- Data constructor + | isDataConWorkId var -- Data constructor = let StrictSig dmd_ty = idNewStrictness var -- It must have a strictness sig DmdType _ _ con_res = dmd_ty @@ -846,22 +880,11 @@ argDemand (Defer d) = lazyDmd argDemand (Eval ds) = Eval (mapDmds argDemand ds) argDemand (Box Bot) = evalDmd argDemand (Box d) = box (argDemand d) -argDemand Bot = Abs -- Don't pass args that are consumed by bottom/err +argDemand Bot = Abs -- Don't pass args that are consumed (only) by bottom argDemand d = d \end{code} \begin{code} -betterStrictness :: StrictSig -> StrictSig -> Bool -betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2 - -betterDmdType t1 t2 = (t1 `lubType` t2) == t2 - -betterDemand :: Demand -> Demand -> Bool --- If d1 `better` d2, and d2 `better` d2, then d1==d2 -betterDemand d1 d2 = (d1 `lub` d2) == d2 -\end{code} - -\begin{code} ------------------------- -- Consider (if x then y else []) with demand V -- Then the first branch gives {y->V} and the second @@ -1131,7 +1154,15 @@ get_changes_dmd id old = newDemand (idDemandInfo id) new_better = new `betterDemand` old old_better = old `betterDemand` new -#endif + +betterStrictness :: StrictSig -> StrictSig -> Bool +betterStrictness (StrictSig t1) (StrictSig t2) = betterDmdType t1 t2 + +betterDmdType t1 t2 = (t1 `lubType` t2) == t2 + +betterDemand :: Demand -> Demand -> Bool +-- If d1 `better` d2, and d2 `better` d2, then d1==d2 +betterDemand d1 d2 = (d1 `lub` d2) == d2 squashSig (StrictSig (DmdType fv ds res)) = StrictSig (DmdType emptyDmdEnv (map squashDmd ds) res) @@ -1143,4 +1174,5 @@ squashDmd (Box d) = Box (squashDmd d) squashDmd (Eval ds) = Eval (mapDmds squashDmd ds) squashDmd (Defer ds) = Defer (mapDmds squashDmd ds) squashDmd d = d +#endif \end{code}