X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FdeSugar%2FDsBinds.lhs;h=c43f98596d58b553ad3111084c09428643e04b6f;hb=de896403dfe48bc999e5501eb8b517624dd2e5d4;hp=19e5ff332f062a9d9562904e3f96e1105538ffee;hpb=2c8f04b5b883db74f449dfc8c224929fe28b027d;p=ghc-hetmet.git diff --git a/ghc/compiler/deSugar/DsBinds.lhs b/ghc/compiler/deSugar/DsBinds.lhs index 19e5ff3..c43f985 100644 --- a/ghc/compiler/deSugar/DsBinds.lhs +++ b/ghc/compiler/deSugar/DsBinds.lhs @@ -1,5 +1,5 @@ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996 +% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 % \section[DsBinds]{Pattern-matching bindings (HsBinds and MonoBinds)} @@ -8,75 +8,48 @@ in that the @Rec@/@NonRec@/etc structure is thrown away (whereas at lower levels it is preserved with @let@/@letrec@s). \begin{code} -module DsBinds ( dsBinds, dsMonoBinds ) where +module DsBinds ( dsMonoBinds, AutoScc(..) ) where #include "HsVersions.h" -import {-# SOURCE #-} DsExpr + +import {-# SOURCE #-} DsExpr( dsExpr ) import HsSyn -- lots of things import CoreSyn -- lots of things import CoreUtils ( coreExprType ) -import TcHsSyn ( TypecheckedHsBinds, TypecheckedHsExpr, - TypecheckedMonoBinds, - TypecheckedPat - ) +import TcHsSyn ( TypecheckedMonoBinds ) import DsMonad import DsGRHSs ( dsGuarded ) import DsUtils import Match ( matchWrapper ) -import BasicTypes ( Module, RecFlag(..) ) +import BasicTypes ( RecFlag(..) ) import CmdLineOpts ( opt_SccProfilingOn, opt_AutoSccsOnAllToplevs, - opt_AutoSccsOnExportedToplevs + opt_AutoSccsOnExportedToplevs, opt_AutoSccsOnDicts ) -import CostCentre ( mkAutoCC, IsCafCC(..), mkAllDictsCC ) -import Id ( idType, Id ) -import Name ( isExported ) -import Type ( mkTyVarTy, isDictTy, instantiateTy - ) -import TyVar ( zipTyVarEnv ) -import TysPrim ( voidTy ) -import Outputable ( assertPanic ) +import CostCentre ( CostCentre, mkAutoCC, IsCafCC(..) ) +import Id ( idType, idName, isUserExportedId, isSpecPragmaId, Id ) +import NameSet +import VarEnv +import VarSet +import Type ( mkTyVarTy, isDictTy ) +import Subst ( mkTyVarSubst, substTy ) +import TysWiredIn ( voidTy ) +import Outputable + +import Maybe +import IOExts (trace) \end{code} %************************************************************************ %* * -\subsection[toplevel-and-regular-DsBinds]{Regular and top-level @dsBinds@} -%* * -%************************************************************************ - -Like @dsBinds@, @dsBind@ returns a @[CoreBinding]@, but it may be -that some of the binders are of unboxed type. This is sorted out when -the caller wraps the bindings round an expression. - -\begin{code} - -dsBinds :: Bool -- if candidate, auto add scc's on toplevs ? - -> TypecheckedHsBinds - -> DsM [CoreBinding] - -dsBinds _ EmptyBinds = returnDs [] -dsBinds auto_scc (ThenBinds binds_1 binds_2) - = andDs (++) (dsBinds auto_scc binds_1) (dsBinds auto_scc binds_2) - -dsBinds auto_scc (MonoBind binds sigs is_rec) - = dsMonoBinds auto_scc binds [] `thenDs` \ prs -> - returnDs (case is_rec of - Recursive -> [Rec prs] - NonRecursive -> [NonRec binder rhs | (binder,rhs) <- prs] - ) -\end{code} - - -%************************************************************************ -%* * \subsection[dsMonoBinds]{Desugaring a @MonoBinds@} %* * %************************************************************************ \begin{code} -dsMonoBinds :: Bool -- False => don't (auto-)annotate scc on toplevs. +dsMonoBinds :: AutoScc -- scc annotation policy (see below) -> TypecheckedMonoBinds -> [(Id,CoreExpr)] -- Put this on the end (avoid quadratic append) -> DsM [(Id,CoreExpr)] -- Result @@ -97,73 +70,109 @@ dsMonoBinds _ (VarMonoBind var expr) rest -- we only need do this here addDictScc var core_expr `thenDs` \ core_expr' -> - returnDs ((var, core_expr') : rest) + let + -- Gross hack to prevent inlining into SpecPragmaId rhss + -- Consider fromIntegral = fromInteger . toInteger + -- spec1 = fromIntegral Int Float + -- Even though fromIntegral is small we don't want to inline + -- it inside spec1, so that we collect the specialised call + -- Solution: make spec1 an INLINE thing. + core_expr'' = mkInline (isSpecPragmaId var) core_expr' + in + + returnDs ((var, core_expr'') : rest) dsMonoBinds auto_scc (FunMonoBind fun _ matches locn) rest = putSrcLocDs locn $ matchWrapper (FunMatch fun) matches error_string `thenDs` \ (args, body) -> - addAutoScc auto_scc (fun, mkValLam args body) `thenDs` \ pair -> + addAutoScc auto_scc (fun, mkLams args body) `thenDs` \ pair -> returnDs (pair : rest) where - error_string = "function " ++ showForErr fun + error_string = "function " ++ showSDoc (ppr fun) -dsMonoBinds _ (PatMonoBind pat grhss_and_binds locn) rest +dsMonoBinds auto_scc (PatMonoBind pat grhss locn) rest = putSrcLocDs locn $ - dsGuarded grhss_and_binds `thenDs` \ body_expr -> - mkSelectorBinds pat body_expr `thenDs` \ sel_binds -> + dsGuarded grhss `thenDs` \ body_expr -> + mkSelectorBinds pat body_expr `thenDs` \ sel_binds -> + mapDs (addAutoScc auto_scc) sel_binds `thenDs` \ sel_binds -> returnDs (sel_binds ++ rest) - -- Common special case: no type or dictionary abstraction -dsMonoBinds auto_scc (AbsBinds [] [] exports binds) rest - = mapDs (addAutoScc auto_scc) [(global, Var local) | (_, global, local) <- exports] `thenDs` \ exports' -> - dsMonoBinds False binds (exports' ++ rest) - - -- Another common case: one exported variable + -- Common case: one exported variable -- All non-recursive bindings come through this way -dsMonoBinds auto_scc (AbsBinds all_tyvars dicts [(tyvars, global, local)] binds) rest +dsMonoBinds auto_scc + (AbsBinds all_tyvars dicts exps@[(tyvars, global, local)] inlines binds) rest = ASSERT( all (`elem` tyvars) all_tyvars ) - dsMonoBinds False binds [] `thenDs` \ core_prs -> + dsMonoBinds (addSccs auto_scc exps) binds [] `thenDs` \ core_prs -> let -- Always treat the binds as recursive, because the typechecker -- makes rather mixed-up dictionary bindings core_binds = [Rec core_prs] + global' = (global, mkInline (idName global `elemNameSet` inlines) $ + mkLams tyvars $ mkLams dicts $ + mkDsLets core_binds (Var local)) in - addAutoScc auto_scc (global, mkLam tyvars dicts $ - mkCoLetsAny core_binds (Var local)) `thenDs` \ global' -> returnDs (global' : rest) -dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports binds) rest - = dsMonoBinds False binds [] `thenDs` \ core_prs -> + -- Another common special case: no type or dictionary abstraction +dsMonoBinds auto_scc (AbsBinds [] [] exports inlines binds) rest + = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs -> let - core_binds = [Rec core_prs] + exports' = [(global, Var local) | (_, global, local) <- exports] + in + returnDs (addLocalInlines exports inlines core_prs ++ exports' ++ rest) - tup_expr = mkLam all_tyvars dicts $ - mkCoLetsAny core_binds $ - mkTupleExpr locals - locals = [local | (_, _, local) <- exports] - local_tys = map idType locals +dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports inlines binds) rest + = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs -> + let + core_binds = [Rec (addLocalInlines exports inlines core_prs)] + + tup_expr = mkTupleExpr locals + tup_ty = coreExprType tup_expr + poly_tup_expr = mkLams all_tyvars $ mkLams dicts $ + mkDsLets core_binds tup_expr + locals = [local | (_, _, local) <- exports] + local_tys = map idType locals in - newSysLocalDs (coreExprType tup_expr) `thenDs` \ tup_id -> + newSysLocalDs (coreExprType poly_tup_expr) `thenDs` \ poly_tup_id -> let - dict_args = map VarArg dicts + dict_args = map Var dicts mk_bind (tyvars, global, local) n -- locals !! n == local = -- Need to make fresh locals to bind in the selector, because -- some of the tyvars will be bound to voidTy - newSysLocalsDs (map (instantiateTy env) local_tys) `thenDs` \ locals' -> - addAutoScc auto_scc - (global, mkLam tyvars dicts $ - mkTupleSelector locals' (locals' !! n) $ - mkValApp (mkTyApp (Var tup_id) ty_args) dict_args) + newSysLocalsDs (map (substTy env) local_tys) `thenDs` \ locals' -> + newSysLocalDs (substTy env tup_ty) `thenDs` \ tup_id -> + returnDs (global, mkLams tyvars $ mkLams dicts $ + mkTupleSelector locals' (locals' !! n) tup_id $ + mkApps (mkTyApps (Var poly_tup_id) ty_args) dict_args) where mk_ty_arg all_tyvar | all_tyvar `elem` tyvars = mkTyVarTy all_tyvar | otherwise = voidTy ty_args = map mk_ty_arg all_tyvars - env = all_tyvars `zipTyVarEnv` ty_args + env = mkTyVarSubst all_tyvars ty_args in zipWithDs mk_bind exports [0..] `thenDs` \ export_binds -> -- don't scc (auto-)annotate the tuple itself. - returnDs ((tup_id, tup_expr) : (export_binds ++ rest)) + returnDs ((poly_tup_id, poly_tup_expr) : (export_binds ++ rest)) +\end{code} + + +%************************************************************************ +%* * +\subsection{Adding inline pragmas} +%* * +%************************************************************************ + +\begin{code} +mkInline :: Bool -> CoreExpr -> CoreExpr +mkInline True body = Note InlineMe body +mkInline False body = body + +addLocalInlines :: [(a, Id, Id)] -> NameSet -> [(Id,CoreExpr)] -> [(Id,CoreExpr)] +addLocalInlines exports inlines pairs + = [(bndr, mkInline (bndr `elemVarSet` local_inlines) rhs) | (bndr,rhs) <- pairs] + where + local_inlines = mkVarSet [l | (_,g,l) <- exports, idName g `elemNameSet` inlines] \end{code} @@ -174,16 +183,34 @@ dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports binds) rest %************************************************************************ \begin{code} -addAutoScc :: Bool -- if needs be, decorate toplevs? +data AutoScc + = TopLevel + | TopLevelAddSccs (Id -> Maybe Id) + | NoSccs + +addSccs :: AutoScc -> [(a,Id,Id)] -> AutoScc +addSccs auto_scc@(TopLevelAddSccs _) exports = auto_scc +addSccs NoSccs exports = NoSccs +addSccs TopLevel exports + = TopLevelAddSccs (\id -> case [ exp | (_,exp,loc) <- exports, loc == id ] of + (exp:_) | opt_AutoSccsOnAllToplevs || + (isUserExportedId exp && + opt_AutoSccsOnExportedToplevs) + -> Just exp + _ -> Nothing) + +addAutoScc :: AutoScc -- if needs be, decorate toplevs? -> (Id, CoreExpr) -> DsM (Id, CoreExpr) -addAutoScc auto_scc_candidate pair@(bndr, core_expr) - | auto_scc_candidate && worthSCC core_expr && - (opt_AutoSccsOnAllToplevs || (isExported bndr && opt_AutoSccsOnExportedToplevs)) - = getModuleAndGroupDs `thenDs` \ (mod,grp) -> - returnDs (bndr, Note (SCC (mkAutoCC bndr mod grp IsNotCafCC)) core_expr) - | otherwise +addAutoScc (TopLevelAddSccs auto_scc_fn) pair@(bndr, core_expr) + | do_auto_scc && worthSCC core_expr + = getModuleDs `thenDs` \ mod -> + returnDs (bndr, Note (SCC (mkAutoCC top_bndr mod NotCafCC)) core_expr) + where do_auto_scc = isJust maybe_auto_scc + maybe_auto_scc = auto_scc_fn bndr + (Just top_bndr) = maybe_auto_scc +addAutoScc _ pair = returnDs pair worthSCC (Note (SCC _) _) = False @@ -191,18 +218,20 @@ worthSCC (Con _ _) = False worthSCC core_expr = True \end{code} -If profiling and dealing with a dict binding, wrap the dict in "_scc_ DICT ": +If profiling and dealing with a dict binding, +wrap the dict in @_scc_ DICT @: \begin{code} -addDictScc var rhs - | not ( opt_SccProfilingOn || opt_AutoSccsOnAllToplevs) - -- the latter is so that -unprof-auto-scc-all adds dict sccs +addDictScc var rhs = returnDs rhs + +{- DISABLED for now (need to somehow make up a name for the scc) -- SDM + | not ( opt_SccProfilingOn && opt_AutoSccsOnDicts) || not (isDictTy (idType var)) = returnDs rhs -- That's easy: do nothing | otherwise = getModuleAndGroupDs `thenDs` \ (mod, grp) -> - -- ToDo: do -dicts-all flag (mark dict things with individual CCs) returnDs (Note (SCC (mkAllDictsCC mod grp False)) rhs) +-} \end{code}