[project @ 1998-12-18 17:40:31 by simonpj]
[ghc-hetmet.git] / ghc / compiler / stranal / StrictAnal.lhs
index 71c6e90..3382bec 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
+% (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
 %
 \section[StrictAnal]{``Simple'' Mycroft-style strictness analyser}
 
@@ -7,38 +7,28 @@ The original version(s) of all strictness-analyser code (except the
 Semantique analyser) was written by Andy Gill.
 
 \begin{code}
-#include "HsVersions.h"
-
-module StrictAnal ( saWwTopBinds, saTopBinds ) where
+module StrictAnal ( saWwTopBinds ) where
 
-import Ubiq{-uitous-}
+#include "HsVersions.h"
 
-import CmdLineOpts     ( opt_AllStrict, opt_NumbersStrict,
-                         opt_D_dump_stranal, opt_D_simplifier_stats
-                       )
+import CmdLineOpts     ( opt_D_dump_stranal, opt_D_simplifier_stats,  opt_D_verbose_core2core )
 import CoreSyn
-import Id              ( idType, addIdStrictness,
-                         getIdDemandInfo, addIdDemandInfo,
-                         GenId{-instance Outputable-}
-                       )
-import IdInfo          ( mkStrictnessInfo, mkBottomStrictnessInfo,
-                         mkDemandInfo, willBeDemanded, DemandInfo
+import Id              ( idType, setIdStrictness,
+                         getIdDemandInfo, setIdDemandInfo,
+                         Id
                        )
-import PprCore         ( pprCoreBinding, pprBigCoreBinder )
-import PprStyle                ( PprStyle(..) )
-import PprType         ( GenType{-instance Outputable-}, GenTyVar{-ditto-} )
-import Pretty          ( ppBesides, ppStr, ppInt, ppChar, ppAboves )
+import IdInfo          ( mkStrictnessInfo )
+import CoreLint                ( beginPass, endPass )
+import ErrUtils                ( dumpIfSet )
 import SaAbsInt
 import SaLib
-import TyVar           ( GenTyVar{-instance Eq-} )
+import Demand          ( isStrict )
 import WorkWrap                -- "back-end" of strictness analyser
-import Unique          ( Unique{-instance Eq -} )
-import Util            ( zipWith4Equal, pprTrace, panic{-ToDo:rm-} )
-
-isWrapperId = panic "StrictAnal.isWrapperId (ToDo)"
+import UniqSupply       ( UniqSupply )
+import Util            ( zipWith4Equal )
+import Outputable
 \end{code}
 
-
 %************************************************************************
 %*                                                                     *
 \subsection[Thoughts]{Random thoughts}
@@ -87,50 +77,28 @@ Alas and alack.
 
 \begin{code}
 saWwTopBinds :: UniqSupply
-            -> [CoreBinding]
-            -> [CoreBinding]
+            -> [CoreBind]
+            -> IO [CoreBind]
 
 saWwTopBinds us binds
-  = let
-       strflags = (opt_AllStrict, opt_NumbersStrict)
+  = do {
+       beginPass "Strictness analysis";
 
-       -- mark each binder with its strictness
-#ifndef OMIT_STRANAL_STATS
-       (binds_w_strictness, sa_stats)
-         = sa_top_binds strflags binds nullSaStats
-#else
-       binds_w_strictness
-         = sa_top_binds strflags binds
-#endif
-    in
-    -- possibly show what we decided about strictness...
-    (if opt_D_dump_stranal
-     then pprTrace "Strictness:\n" (ppAboves (
-          map (pprCoreBinding PprDebug)  binds_w_strictness))
-     else id
-    )
-    -- possibly show how many things we marked as demanded...
-    ((if opt_D_simplifier_stats
+       -- Mark each binder with its strictness
 #ifndef OMIT_STRANAL_STATS
-     then pp_stats sa_stats
+       let { (binds_w_strictness, sa_stats) = saTopBinds binds nullSaStats };
+       dumpIfSet opt_D_simplifier_stats "Strictness analysis statistics"
+                 (pp_stats sa_stats);
 #else
-     then id
-#endif
-     else id
-    )
-       -- create worker/wrappers, and mark binders with their
-       -- "strictness info" [which encodes their
-       -- worker/wrapper-ness]
-    (workersAndWrappers binds_w_strictness us))
-#ifndef OMIT_STRANAL_STATS
-  where
-    pp_stats (SaStats tlam dlam tc dc tlet dlet)
-      = pprTrace "Binders marked demanded: "
-       (ppBesides [ppStr "Lambda vars: ", ppInt IBOX(dlam), ppChar '/', ppInt IBOX(tlam),
-                 ppStr "; Case vars: ",   ppInt IBOX(dc),   ppChar '/', ppInt IBOX(tc),
-                 ppStr "; Let vars: ",    ppInt IBOX(dlet), ppChar '/', ppInt IBOX(tlet)
-       ])
+       let { binds_w_strictness = saTopBindsBinds binds };
 #endif
+
+       -- Create worker/wrappers, and mark binders with their
+       -- "strictness info" [which encodes their worker/wrapper-ness]
+       let { binds' = workersAndWrappers us binds_w_strictness };
+
+       endPass "Strictness analysis" (opt_D_dump_stranal || opt_D_verbose_core2core) binds'
+    }
 \end{code}
 
 %************************************************************************
@@ -155,19 +123,11 @@ environment which maps @Id@s to their abstract values (i.e., an
 @AbsValEnv@ maps an @Id@ to its @AbsVal@).
 
 \begin{code}
-saTopBinds   :: StrAnalFlags -> [CoreBinding] -> [CoreBinding]     -- exported
-sa_top_binds :: StrAnalFlags -> [CoreBinding] -> SaM [CoreBinding] -- not exported
+saTopBinds :: [CoreBind] -> SaM [CoreBind] -- not exported
 
-saTopBinds strflags binds
-#ifndef OMIT_STRANAL_STATS
-  = fst (sa_top_binds strflags binds nullSaStats)
-#else
-  = sa_top_binds strflags binds
-#endif
-
-sa_top_binds strflags binds
+saTopBinds binds
   = let
-       starting_abs_env = nullAbsValEnv strflags
+       starting_abs_env = nullAbsValEnv
     in
     do_it starting_abs_env starting_abs_env binds
   where
@@ -185,14 +145,12 @@ be used; we can't turn top-level @let@s into @case@s.
 
 \begin{code}
 saTopBind :: StrictEnv -> AbsenceEnv
-         -> CoreBinding
-         -> SaM (StrictEnv, AbsenceEnv, CoreBinding)
+         -> CoreBind
+         -> SaM (StrictEnv, AbsenceEnv, CoreBind)
 
 saTopBind str_env abs_env (NonRec binder rhs)
   = saExpr str_env abs_env rhs         `thenSa` \ new_rhs ->
     let
-       strflags = getStrAnalFlags str_env
-
        str_rhs = absEval StrAnal rhs str_env
        abs_rhs = absEval AbsAnal rhs abs_env
 
@@ -203,7 +161,6 @@ saTopBind str_env abs_env (NonRec binder rhs)
 
        new_binder
          = addStrictnessInfoToId
-               strflags
                widened_str_rhs widened_abs_rhs
                binder
                rhs
@@ -217,14 +174,13 @@ saTopBind str_env abs_env (NonRec binder rhs)
 
 saTopBind str_env abs_env (Rec pairs)
   = let
-       strflags    = getStrAnalFlags str_env
        (binders,rhss) = unzip pairs
        str_rhss    = fixpoint StrAnal binders rhss str_env
        abs_rhss    = fixpoint AbsAnal binders rhss abs_env
                      -- fixpoint returns widened values
        new_str_env = growAbsValEnvList str_env (binders `zip` str_rhss)
        new_abs_env = growAbsValEnvList abs_env (binders `zip` abs_rhss)
-       new_binders = zipWith4Equal "saTopBind" (addStrictnessInfoToId strflags)
+       new_binders = zipWith4Equal "saTopBind" addStrictnessInfoToId
                                    str_rhss abs_rhss binders rhss
     in
     mapSa (saExpr new_str_env new_abs_env) rhss        `thenSa` \ new_rhss ->
@@ -247,64 +203,46 @@ environment.
 saExpr :: StrictEnv -> AbsenceEnv -> CoreExpr -> SaM CoreExpr
 
 saExpr _ _ e@(Var _)   = returnSa e
-saExpr _ _ e@(Lit _)   = returnSa e
 saExpr _ _ e@(Con  _ _)        = returnSa e
-saExpr _ _ e@(Prim _ _)        = returnSa e
+saExpr _ _ e@(Type _)  = returnSa e
 
-saExpr str_env abs_env (Lam (ValBinder arg) body)
-  = saExpr str_env abs_env body        `thenSa` \ new_body ->
-    let
-       new_arg = addDemandInfoToId str_env abs_env body arg
-    in
-    tickLambda new_arg `thenSa_` -- stats
-    returnSa (Lam (ValBinder new_arg) new_body)
-
-saExpr str_env abs_env (Lam other_binder expr)
-  = saExpr str_env abs_env expr        `thenSa` \ new_expr ->
-    returnSa (Lam other_binder new_expr)
+saExpr str_env abs_env (Lam bndr body)
+  =    -- Don't bother to set the demand-info on a lambda binder
+       -- We do that only for let(rec)-bound functions
+    saExpr str_env abs_env body        `thenSa` \ new_body ->
+    returnSa (Lam bndr new_body)
 
 saExpr str_env abs_env (App fun arg)
   = saExpr str_env abs_env fun `thenSa` \ new_fun ->
-    returnSa (App new_fun arg)
+    saExpr str_env abs_env arg `thenSa` \ new_arg ->
+    returnSa (App new_fun new_arg)
 
-saExpr str_env abs_env (SCC cc expr)
+saExpr str_env abs_env (Note note expr)
   = saExpr str_env abs_env expr        `thenSa` \ new_expr ->
-    returnSa (SCC cc new_expr)
+    returnSa (Note note new_expr)
 
-saExpr str_env abs_env (Coerce c ty expr)
-  = saExpr str_env abs_env expr        `thenSa` \ new_expr ->
-    returnSa (Coerce c ty new_expr)
-
-saExpr str_env abs_env (Case expr (AlgAlts alts deflt))
-  = saExpr    str_env abs_env expr  `thenSa` \ new_expr  ->
-    saDefault str_env abs_env deflt `thenSa` \ new_deflt ->
-    mapSa sa_alt alts              `thenSa` \ new_alts  ->
-    returnSa (Case new_expr (AlgAlts new_alts new_deflt))
+saExpr str_env abs_env (Case expr case_bndr alts)
+  = saExpr str_env abs_env expr                `thenSa` \ new_expr  ->
+    mapSa sa_alt alts                  `thenSa` \ new_alts  ->
+    let
+       new_case_bndr = addDemandInfoToCaseBndr str_env abs_env alts case_bndr
+    in
+    returnSa (Case new_expr new_case_bndr new_alts)
   where
     sa_alt (con, binders, rhs)
       = saExpr str_env abs_env rhs  `thenSa` \ new_rhs ->
        let
-           new_binders = addDemandInfoToIds str_env abs_env rhs binders
+           new_binders = map add_demand_info binders
+           add_demand_info bndr | isTyVar bndr = bndr
+                                | otherwise    = addDemandInfoToId str_env abs_env rhs bndr
        in
        tickCases new_binders       `thenSa_` -- stats
        returnSa (con, new_binders, new_rhs)
 
-saExpr str_env abs_env (Case expr (PrimAlts alts deflt))
-  = saExpr    str_env abs_env expr  `thenSa` \ new_expr  ->
-    saDefault str_env abs_env deflt `thenSa` \ new_deflt ->
-    mapSa sa_alt alts              `thenSa` \ new_alts  ->
-    returnSa (Case new_expr (PrimAlts new_alts new_deflt))
-  where
-    sa_alt (lit, rhs)
-      = saExpr str_env abs_env rhs `thenSa` \ new_rhs ->
-       returnSa (lit, new_rhs)
-
 saExpr str_env abs_env (Let (NonRec binder rhs) body)
   =    -- Analyse the RHS in the environment at hand
     saExpr str_env abs_env rhs  `thenSa` \ new_rhs  ->
     let
-       strflags = getStrAnalFlags str_env
-
        -- Bind this binder to the abstract value of the RHS; analyse
        -- the body of the `let' in the extended environment.
        str_rhs_val     = absEval StrAnal rhs str_env
@@ -320,7 +258,7 @@ saExpr str_env abs_env (Let (NonRec binder rhs) body)
 
        -- Now determine the strictness of this binder; use that info
        -- to record DemandInfo/StrictnessInfo in the binder.
-       new_binder = addStrictnessInfoToId strflags
+       new_binder = addStrictnessInfoToId
                        widened_str_rhs widened_abs_rhs
                        (addDemandInfoToId str_env abs_env body binder)
                        rhs
@@ -331,7 +269,6 @@ saExpr str_env abs_env (Let (NonRec binder rhs) body)
 
 saExpr str_env abs_env (Let (Rec pairs) body)
   = let
-       strflags       = getStrAnalFlags str_env
        (binders,rhss) = unzip pairs
        str_vals       = fixpoint StrAnal binders rhss str_env
        abs_vals       = fixpoint AbsAnal binders rhss abs_env
@@ -354,28 +291,12 @@ saExpr str_env abs_env (Let (Rec pairs) body)
 --                deciding that y is absent, which is plain wrong!
 --             It's much easier simply not to do this.
 
-       improved_binders = zipWith4Equal "saExpr" (addStrictnessInfoToId strflags)
+       improved_binders = zipWith4Equal "saExpr" addStrictnessInfoToId
                                         str_vals abs_vals binders rhss
 
-       whiter_than_white_binders = launder improved_binders
-
-       new_pairs   = whiter_than_white_binders `zip` new_rhss
+       new_pairs   = improved_binders `zip` new_rhss
     in
     returnSa (Let (Rec new_pairs) new_body)
-  where
-    launder me = {-still-} me
-\end{code}
-
-\begin{code}
-saDefault str_env abs_env NoDefault = returnSa NoDefault
-
-saDefault str_env abs_env (BindDefault bdr rhs)
-  = saExpr str_env abs_env rhs `thenSa` \ new_rhs ->
-    let
-       new_bdr = addDemandInfoToId str_env abs_env rhs bdr
-    in
-    tickCases [new_bdr]                `thenSa_` -- stats
-    returnSa (BindDefault new_bdr new_rhs)
 \end{code}
 
 
@@ -398,29 +319,19 @@ tell how many args could safely be grabbed.
 
 \begin{code}
 addStrictnessInfoToId
-       :: StrAnalFlags
-       -> AbsVal               -- Abstract strictness value
+       :: AbsVal               -- Abstract strictness value
        -> AbsVal               -- Ditto absence
        -> Id                   -- The id
        -> CoreExpr     -- Its RHS
        -> Id                   -- Augmented with strictness
 
-addStrictnessInfoToId strflags str_val abs_val binder body
-  = if isWrapperId binder then
-       binder  -- Avoid clobbering existing strictness info
-               -- (and, more importantly, worker info).
-               -- Deeply suspicious (SLPJ)
-    else
-    if (isBot str_val) then
-       binder `addIdStrictness` mkBottomStrictnessInfo
-    else
-       case (collectBinders body) of { (_, _, lambda_bounds, rhs) ->
-       let
-               tys        = map idType lambda_bounds
-               strictness = findStrictness strflags tys str_val abs_val
-       in
-       binder `addIdStrictness` mkStrictnessInfo strictness Nothing
-       }
+addStrictnessInfoToId str_val abs_val binder body
+  = case (collectTyAndValBinders body) of
+       (_, lambda_bounds, rhs) -> binder `setIdStrictness` 
+                                  mkStrictnessInfo strictness False
+               where
+                   tys        = map idType lambda_bounds
+                   strictness = findStrictness tys str_val abs_val
 \end{code}
 
 \begin{code}
@@ -430,7 +341,10 @@ addDemandInfoToId :: StrictEnv -> AbsenceEnv
                  -> Id                 -- Id augmented with Demand info
 
 addDemandInfoToId str_env abs_env expr binder
-  = binder `addIdDemandInfo` (mkDemandInfo (findDemand str_env abs_env expr binder))
+  = binder `setIdDemandInfo` (findDemand str_env abs_env expr binder)
+
+addDemandInfoToCaseBndr str_env abs_env alts binder
+  = binder `setIdDemandInfo` (findDemandAlts str_env abs_env alts binder)
 
 addDemandInfoToIds :: StrictEnv -> AbsenceEnv -> CoreExpr -> [Id] -> [Id]
 
@@ -462,7 +376,7 @@ returnSa      :: a -> SaM a
 {-# INLINE returnSa #-}
 
 tickLambda :: Id   -> SaM ()
-tickCases  :: [Id] -> SaM ()
+tickCases  :: [CoreBndr] -> SaM ()
 tickLet    :: Id   -> SaM ()
 
 #ifndef OMIT_STRANAL_STATS
@@ -491,11 +405,19 @@ tickLet var (SaStats tlam dlam tc dc tlet dlet)
     ((), SaStats tlam dlam tc dc (tlet _ADD_ tot) (dlet _ADD_ demanded)) }
 
 tick_demanded var (tot, demanded)
+  | isTyVar var = (tot, demanded)
+  | otherwise
   = (tot + 1,
-     if (willBeDemanded (getIdDemandInfo var))
+     if (isStrict (getIdDemandInfo var))
      then demanded + 1
      else demanded)
 
+pp_stats (SaStats tlam dlam tc dc tlet dlet)
+      = hcat [ptext SLIT("Lambda vars: "), int IBOX(dlam), char '/', int IBOX(tlam),
+                   ptext SLIT("; Case vars: "), int IBOX(dc),   char '/', int IBOX(tc),
+                   ptext SLIT("; Let vars: "),  int IBOX(dlet), char '/', int IBOX(tlet)
+       ]
+
 #else {-OMIT_STRANAL_STATS-}
 -- identity monad
 type SaM a = a