[project @ 1998-05-22 15:23:11 by simonm]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsBinds.lhs
index 901274d..19e5ff3 100644 (file)
@@ -8,38 +8,36 @@ in that the @Rec@/@NonRec@/etc structure is thrown away (whereas at
 lower levels it is preserved with @let@/@letrec@s).
 
 \begin{code}
-#include "HsVersions.h"
-
 module DsBinds ( dsBinds, dsMonoBinds ) where
 
-IMP_Ubiq()
-IMPORT_DELOOPER(DsLoop)                -- break dsExpr-ish loop
+#include "HsVersions.h"
+
+import {-# SOURCE #-} DsExpr
 
 import HsSyn           -- lots of things
 import CoreSyn         -- lots of things
 import CoreUtils       ( coreExprType )
-import TcHsSyn         ( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr),
-                         SYN_IE(TypecheckedMonoBinds),
-                         SYN_IE(TypecheckedPat)
+import TcHsSyn         ( TypecheckedHsBinds, TypecheckedHsExpr,
+                         TypecheckedMonoBinds,
+                         TypecheckedPat
                        )
 import DsMonad
 import DsGRHSs         ( dsGuarded )
 import DsUtils
 import Match           ( matchWrapper )
 
+import BasicTypes       ( Module, RecFlag(..) )
 import CmdLineOpts     ( opt_SccProfilingOn, opt_AutoSccsOnAllToplevs, 
-                         opt_AutoSccsOnExportedToplevs, opt_CompilingGhcInternals )
-import CostCentre      ( mkAutoCC, IsCafCC(..), mkAllDictsCC, preludeDictsCostCentre )
-import Id              ( idType, SYN_IE(DictVar), GenId, SYN_IE(Id) )
-import ListSetOps      ( minusList, intersectLists )
+                         opt_AutoSccsOnExportedToplevs
+                       )
+import CostCentre      ( mkAutoCC, IsCafCC(..), mkAllDictsCC )
+import Id              ( idType, Id )
 import Name            ( isExported )
-import PprType         ( GenType )
-import Outputable      ( PprStyle(..) )
 import Type            ( mkTyVarTy, isDictTy, instantiateTy
                        )
-import TyVar           ( tyVarSetToList, GenTyVar{-instance Eq-} )
+import TyVar           ( zipTyVarEnv )
 import TysPrim         ( voidTy )
-import Util            ( isIn, panic, assertPanic  )
+import Outputable      ( assertPanic )
 \end{code}
 
 %************************************************************************
@@ -53,17 +51,20 @@ 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 :: TypecheckedHsBinds -> DsM [CoreBinding]
 
-dsBinds EmptyBinds                  = returnDs []
-dsBinds (ThenBinds  binds_1 binds_2) = andDs (++) (dsBinds binds_1) (dsBinds binds_2)
+dsBinds :: Bool   -- if candidate, auto add scc's on toplevs ?
+       -> TypecheckedHsBinds 
+       -> DsM [CoreBinding]
 
-dsBinds (MonoBind binds sigs is_rec)
-  = dsMonoBinds is_rec binds           `thenDs` \ prs ->
-    returnDs (if is_rec then
-               [Rec prs]
-             else
-               [NonRec binder rhs | (binder,rhs) <- prs]
+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}
 
@@ -75,58 +76,66 @@ dsBinds (MonoBind binds sigs is_rec)
 %************************************************************************
 
 \begin{code}
-dsMonoBinds :: RecFlag -> TypecheckedMonoBinds -> DsM [(Id,CoreExpr)]
+dsMonoBinds :: Bool            -- False => don't (auto-)annotate scc on toplevs.
+           -> TypecheckedMonoBinds
+           -> [(Id,CoreExpr)]          -- Put this on the end (avoid quadratic append)
+           -> DsM [(Id,CoreExpr)]      -- Result
 
-dsMonoBinds is_rec EmptyMonoBinds = returnDs []
+dsMonoBinds _ EmptyMonoBinds rest = returnDs rest
 
-dsMonoBinds is_rec (AndMonoBinds  binds_1 binds_2)
-  = andDs (++) (dsMonoBinds is_rec binds_1) (dsMonoBinds is_rec binds_2)
+dsMonoBinds auto_scc (AndMonoBinds  binds_1 binds_2) rest
+  = dsMonoBinds auto_scc binds_2 rest  `thenDs` \ rest' ->
+    dsMonoBinds auto_scc binds_1 rest'
 
-dsMonoBinds is_rec (CoreMonoBind var core_expr)
-  = returnDs [(var, core_expr)]
+dsMonoBinds _ (CoreMonoBind var core_expr) rest
+  = returnDs ((var, core_expr) : rest)
 
-dsMonoBinds is_rec (VarMonoBind var expr)
+dsMonoBinds _ (VarMonoBind var expr) rest
   = dsExpr expr                        `thenDs` \ core_expr ->
 
        -- Dictionary bindings are always VarMonoBinds, so
        -- we only need do this here
     addDictScc var core_expr   `thenDs` \ core_expr' ->
 
-    returnDs [(var, core_expr')]
+    returnDs ((var, core_expr') : rest)
 
-dsMonoBinds is_rec (FunMonoBind fun _ matches locn)
+dsMonoBinds auto_scc (FunMonoBind fun _ matches locn) rest
   = putSrcLocDs locn   $
     matchWrapper (FunMatch fun) matches error_string   `thenDs` \ (args, body) ->
-    returnDs [(fun, mkValLam args body)]
+    addAutoScc auto_scc (fun, mkValLam args body)       `thenDs` \ pair ->
+    returnDs (pair : rest)
   where
     error_string = "function " ++ showForErr fun
 
-dsMonoBinds is_rec (PatMonoBind pat grhss_and_binds locn)
+dsMonoBinds _ (PatMonoBind pat grhss_and_binds locn) rest
   = putSrcLocDs locn $
-    dsGuarded grhss_and_binds                  `thenDs` \ body_expr ->
-    mkSelectorBinds pat body_expr
+    dsGuarded grhss_and_binds          `thenDs` \ body_expr ->
+    mkSelectorBinds pat body_expr      `thenDs` \ sel_binds ->
+    returnDs (sel_binds ++ rest)
 
        -- Common special case: no type or dictionary abstraction
-dsMonoBinds is_rec (AbsBinds [] [] exports binds)
-  = dsMonoBinds is_rec binds                   `thenDs` \ prs ->
-    returnDs (prs ++ [(global, Var local) | (_, global, local) <- exports])
+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
        -- All non-recursive bindings come through this way
-dsMonoBinds is_rec (AbsBinds all_tyvars dicts [(tyvars, global, local)] binds)
+dsMonoBinds auto_scc (AbsBinds all_tyvars dicts [(tyvars, global, local)] binds) rest
   = ASSERT( all (`elem` tyvars) all_tyvars )
-    dsMonoBinds is_rec binds                           `thenDs` \ core_prs ->
+    dsMonoBinds False binds []                 `thenDs` \ core_prs ->
     let 
-       core_binds | is_rec    = [Rec core_prs]
-                  | otherwise = [NonRec b e | (b,e) <- core_prs]
+       -- Always treat the binds as recursive, because the typechecker
+       -- makes rather mixed-up dictionary bindings
+       core_binds = [Rec core_prs]
     in
-    returnDs [(global, mkLam tyvars dicts $ mkCoLetsAny core_binds (Var local))]
+    addAutoScc auto_scc (global, mkLam tyvars dicts $ 
+                                mkCoLetsAny core_binds (Var local)) `thenDs` \ global' ->
+    returnDs (global' : rest)
 
-dsMonoBinds is_rec (AbsBinds all_tyvars dicts exports binds)
-  = dsMonoBinds is_rec binds                           `thenDs` \ core_prs ->
+dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports binds) rest
+  = dsMonoBinds False binds []                 `thenDs` \ core_prs ->
     let 
-       core_binds | is_rec    = [Rec core_prs]
-                  | otherwise = [NonRec b e | (b,e) <- core_prs]
+       core_binds = [Rec core_prs]
 
        tup_expr = mkLam all_tyvars dicts $
                   mkCoLetsAny core_binds $
@@ -142,17 +151,44 @@ dsMonoBinds is_rec (AbsBinds all_tyvars dicts exports binds)
          =     -- 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' ->
-           returnDs (global, mkLam tyvars dicts $
-                             mkTupleSelector locals' (locals' !! n) $
-                             mkValApp (mkTyApp (Var tup_id) ty_args) dict_args)
+           addAutoScc auto_scc
+                      (global, mkLam tyvars dicts $
+                               mkTupleSelector locals' (locals' !! n) $
+                               mkValApp (mkTyApp (Var 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 `zip` ty_args
+           env     = all_tyvars `zipTyVarEnv` ty_args
     in
     zipWithDs mk_bind exports [0..]            `thenDs` \ export_binds ->
-    returnDs ((tup_id, tup_expr) : export_binds)
+     -- don't scc (auto-)annotate the tuple itself.
+    returnDs ((tup_id, tup_expr) : (export_binds ++ rest))
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection[addAutoScc]{Adding automatic sccs}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+addAutoScc :: Bool             -- 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 
+     = returnDs pair
+
+worthSCC (Note (SCC _) _) = False
+worthSCC (Con _ _)        = False
+worthSCC core_expr        = True
 \end{code}
 
 If profiling and dealing with a dict binding, wrap the dict in "_scc_ DICT <dict>":
@@ -164,14 +200,9 @@ addDictScc var rhs
     || not (isDictTy (idType var))
   = returnDs rhs                               -- That's easy: do nothing
 
-  | opt_CompilingGhcInternals
-  = returnDs (SCC prel_dicts_cc rhs)
-
   | otherwise
   = getModuleAndGroupDs        `thenDs` \ (mod, grp) ->
 
        -- ToDo: do -dicts-all flag (mark dict things with individual CCs)
-    returnDs (SCC (mkAllDictsCC mod grp False) rhs)
-
-prel_dicts_cc = preludeDictsCostCentre False{-not dupd-} -- ditto
+    returnDs (Note (SCC (mkAllDictsCC mod grp False)) rhs)
 \end{code}