[project @ 1998-12-18 17:40:31 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / AnalFBWW.lhs
index ac9414d..a1e1dab 100644 (file)
@@ -1,48 +1,47 @@
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 \section[AnalFBWW]{Spoting good functions for splitting into workers/wrappers}
 
 \begin{code}
-#include "HsVersions.h"
-
 module AnalFBWW ( analFBWW ) where
 
-IMPORT_Trace
-import Outputable
-import Pretty
-
-import PlainCore
-import TaggedCore
-import Util
-import Id                      ( addIdFBTypeInfo )
-import IdInfo           
-import IdEnv
-import AbsPrel          ( foldrId, buildId,
-                          nilDataCon, consDataCon, mkListTy, mkFunTy,
-                          unpackCStringAppendId
-                        )
-import BinderInfo
-import SimplEnv                -- everything
-import NewOccurAnal
-import Maybes
+#include "HsVersions.h"
 
+-- Just a stub for now
+import CoreSyn         ( CoreBind )
+import Panic           ( panic )
+
+--import Util
+--import Id                    ( addIdFBTypeInfo )
+--import IdInfo
+--import PrelInfo          ( foldrId, buildId,
+--                       nilDataCon, consDataCon, mkListTy, mkFunTy,
+--                       unpackCStringAppendId
+--                     )
+--import BinderInfo
+--import SimplEnv              -- everything
+--import OccurAnal     -- OLD: was NewOccurAnal
+--import Maybes
 \end{code}
 
 \begin{code}
-analFBWW 
-        :: (GlobalSwitch -> Bool)
-        -> PlainCoreProgram 
-        -> PlainCoreProgram
-analFBWW switch top_binds = trace "ANALFBWW" (snd anno)
+analFBWW
+       :: [CoreBind]
+       -> [CoreBind]
+
+analFBWW = panic "analFBWW (ToDo)"
+
+{- LATER:
+analFBWW top_binds = trace "ANALFBWW" (snd anno)
  where
        anals :: [InBinding]
-       anals = newOccurAnalyseBinds top_binds switch (const False)
-       anno = mapAccumL annotateBindingFBWW nullIdEnv anals
+       anals = newOccurAnalyseBinds top_binds (const False)
+       anno = mapAccumL annotateBindingFBWW emptyVarEnv anals
 \end{code}
 
 \begin{code}
-data OurFBType 
+data OurFBType
        = IsFB FBType
        | IsNotFB               -- unknown
        | IsCons                -- \ xy -> (:) ty xy
@@ -50,7 +49,7 @@ data OurFBType
                deriving (Eq)
        -- We only handle *reasonable* types
        -- Later might add concept of bottom
-       -- because foldr f z (<bottom>) = <bottom>              
+       -- because foldr f z (<bottom>) = <bottom>
 unknownFBType  = IsNotFB
 goodProdFBType = IsFB (FBType [] FBGoodProd)
 
@@ -58,8 +57,8 @@ maybeFBtoFB (Just ty) = ty
 maybeFBtoFB (Nothing) = IsNotFB
 
 addArgs :: Int -> OurFBType -> OurFBType
-addArgs n (IsFB (FBType args prod)) 
-       = IsFB (FBType (take n (repeat FBBadConsum) ++ args) prod)
+addArgs n (IsFB (FBType args prod))
+       = IsFB (FBType (nOfThem n FBBadConsum ++ args) prod)
 addArgs n IsNotFB = IsNotFB
 addArgs n IsCons = panic "adding argument to a cons"
 addArgs n IsBottom = IsNotFB
@@ -74,7 +73,7 @@ joinFBType :: OurFBType -> OurFBType -> OurFBType
 joinFBType (IsBottom) a = a
 joinFBType a (IsBottom) = a
 joinFBType (IsFB (FBType args prod)) (IsFB (FBType args' prod'))
-       | length args == length args' = (IsFB (FBType (zipWith argJ args args')
+       | length args == length args' = (IsFB (FBType (zipWith{-Equal-} argJ args args')
                                                      (prodJ prod prod')))
    where
        argJ FBGoodConsum FBGoodConsum = FBGoodConsum
@@ -93,70 +92,72 @@ analExprFBWW :: InExpr -> IdEnv OurFBType -> OurFBType
 --
 -- [ build g ]         is a good context
 --
-analExprFBWW (CoApp (CoTyApp (CoVar bld) _) _) env  
+analExprFBWW (App (CoTyApp (Var bld) _) _) env
        | bld == buildId         = goodProdFBType
 
 --
 -- [ foldr (:) ys xs ] ==> good
 --                     (but better if xs)
 --
-analExprFBWW (CoApp (CoApp (CoApp 
-               (CoTyApp (CoTyApp (CoVar foldr_id) _) _) (CoVarAtom c)) _) _)
-               env 
+analExprFBWW (App (App (App
+               (CoTyApp (CoTyApp (Var foldr_id) _) _) (VarArg c)) _) _)
+               env
        | pprTrace ("FOLDR:" ++ show (foldr_id == foldrId,isCons c))
-               (ppr PprDebug foldr_id)
+               (ppr foldr_id)
                (foldr_id == foldrId && isCons c) = goodProdFBType
    where
-       isCons c = case lookupIdEnv env c of
+       isCons c = case lookupVarEnv env c of
                    Just IsCons -> True
                    _ -> False
-analExprFBWW (CoVar v) env       = maybeFBtoFB (lookupIdEnv env v)
-analExprFBWW (CoLit _) _         = unknownFBType
+analExprFBWW (Var v) env       = maybeFBtoFB (lookupVarEnv env v)
+analExprFBWW (Lit _) _         = unknownFBType
 
 --
 -- [ x : xs ]  ==> good iff [ xs ] is good
 --
 
-analExprFBWW (CoCon con _ [_,CoVarAtom y]) env     
-       | con == consDataCon = maybeFBtoFB (lookupIdEnv env y)
+analExprFBWW (Con con _ [_,VarArg y]) env
+       | con == consDataCon = maybeFBtoFB (lookupVarEnv env y)
 --
 -- [] is good
 --
-analExprFBWW (CoCon con _ []) _     
+analExprFBWW (Con con _ []) _
        | con == nilDataCon = goodProdFBType
-analExprFBWW (CoCon _ _ _) _     = unknownFBType
-analExprFBWW (CoPrim _ _ _) _    = unknownFBType
+analExprFBWW (Con _ _ _) _     = unknownFBType
+analExprFBWW (Prim _ _ _) _    = unknownFBType
 
 -- \ xy -> (:) ty xy == a CONS
-analExprFBWW (CoLam [(x,_),(y,_)]
-               (CoCon con _ [CoVarAtom x',CoVarAtom y'])) env
-       | con == consDataCon && x == x' && y == y' 
-       = IsCons
-analExprFBWW (CoLam ids e) env   
-       = addArgs (length ids) (analExprFBWW e (delManyFromIdEnv env (map fst ids)))
+
+analExprFBWW (Lam (x,_) (Lam (y,_)
+               (Con con _ [VarArg x',VarArg y']))) env
+  | con == consDataCon && x == x' && y == y'
+  = IsCons
+analExprFBWW (Lam (id,_) e) env
+  = addArgs 1 (analExprFBWW e (delVarEnv env id))
+
 analExprFBWW (CoTyLam tyvar e) env = analExprFBWW e env
-analExprFBWW (CoApp f atom) env  = rmArg (analExprFBWW f env)
-analExprFBWW (CoTyApp f ty) env  = analExprFBWW f env
-analExprFBWW (CoSCC lab e) env   = analExprFBWW e env
-analExprFBWW (CoLet binds e) env = analExprFBWW e (analBind binds env) 
-analExprFBWW (CoCase e alts) env = foldl1 joinFBType (analAltsFBWW alts env)
-
-analAltsFBWW (CoAlgAlts alts deflt) env = 
-    case analDefFBWW deflt env of
+analExprFBWW (App f atom) env   = rmArg (analExprFBWW f env)
+analExprFBWW (CoTyApp f ty) env = analExprFBWW f env
+analExprFBWW (Note _ e) env     = analExprFBWW e env
+analExprFBWW (Let binds e) env  = analExprFBWW e (analBind binds env)
+analExprFBWW (Case e alts) env  = foldl1 joinFBType (analAltsFBWW alts env)
+
+analAltsFBWW (AlgAlts alts deflt) env
+  = case analDefFBWW deflt env of
        Just ty -> ty : tys
        Nothing -> tys
    where
-     tys = map (\(con,binders,e) -> analExprFBWW e (delManyFromIdEnv env (map fst binders))) alts
-analAltsFBWW (CoPrimAlts alts deflt) env = 
-    case analDefFBWW deflt env of
+     tys = map (\(con,binders,e) -> analExprFBWW e (delVarEnvList env (map fst binders))) alts
+analAltsFBWW (PrimAlts alts deflt) env
+  = case analDefFBWW deflt env of
        Just ty -> ty : tys
        Nothing -> tys
    where
      tys = map (\(lit,e) -> analExprFBWW e env) alts
 
 
-analDefFBWW CoNoDefault env = Nothing
-analDefFBWW (CoBindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
+analDefFBWW NoDefault env = Nothing
+analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delVarEnv env (fst v)))
 \end{code}
 
 
@@ -167,36 +168,36 @@ Only add a type info if:
 
 \begin{code}
 analBindExpr :: BinderInfo -> InExpr -> IdEnv OurFBType -> OurFBType
-analBindExpr bnd expr env = 
-       case analExprFBWW expr env of
-             IsFB ty@(FBType [] _) -> 
+analBindExpr bnd expr env
+  =    case analExprFBWW expr env of
+             IsFB ty@(FBType [] _) ->
                   if oneSafeOcc False bnd
                   then IsFB ty
                   else IsNotFB
              other -> other
 
 analBind :: InBinding -> IdEnv OurFBType -> IdEnv OurFBType
-analBind (CoNonRec (v,bnd) e) env = 
+analBind (NonRec (v,bnd) e) env =
        case analBindExpr bnd e env of
-        ty@(IsFB _) -> addOneToIdEnv env v ty
-        ty@(IsCons) -> addOneToIdEnv env v ty
-        _ -> delOneFromIdEnv env v     -- remember about shadowing!
+        ty@(IsFB _) -> extendVarEnv env v ty
+        ty@(IsCons) -> extendVarEnv env v ty
+        _ -> delVarEnv env v   -- remember about shadowing!
 
-analBind (CoRec binds) env = 
+analBind (Rec binds) env =
    let
        first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
-                               (_,args,_) <- [digForLambdas e]]
-       env' = delManyFromIdEnv env (map (fst.fst) binds)
+                               (_,args,_) <- [collectBinders e]]
+       env' = delVarEnvList env (map (fst.fst) binds)
    in
-       growIdEnvList env' (fixpoint 0 binds env' first_set)
+       extendVarEnvList env' (fixpoint 0 binds env' first_set)
 
 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
-fixpoint n binds env maps = 
-       if maps == maps' 
+fixpoint n binds env maps =
+       if maps == maps'
        then maps
        else fixpoint (n+1) binds env maps'
    where
-       env' = growIdEnvList env maps
+       env' = extendVarEnvList env maps
        maps' = [ (v,ty) | ((v,bind),e) <- binds,
                        (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
 
@@ -204,50 +205,52 @@ fixpoint n binds env maps =
 
 
 \begin{code}
-annotateExprFBWW :: InExpr -> IdEnv OurFBType -> PlainCoreExpr
-annotateExprFBWW (CoVar v) env = CoVar v
-annotateExprFBWW (CoLit i) env = CoLit i
-annotateExprFBWW (CoCon c t a) env = CoCon c t a
-annotateExprFBWW (CoPrim p t a) env = CoPrim p t a 
-annotateExprFBWW (CoLam ids e) env = CoLam ids' (annotateExprFBWW e (delManyFromIdEnv env ids'))
-   where ids' = map fst ids
+annotateExprFBWW :: InExpr -> IdEnv OurFBType -> CoreExpr
+annotateExprFBWW (Var v) env = Var v
+annotateExprFBWW (Lit i) env = Lit i
+annotateExprFBWW (Con c t a) env = Con c t a
+annotateExprFBWW (Prim p t a) env = Prim p t a
+annotateExprFBWW (Lam (id,_) e) env
+  = Lam id (annotateExprFBWW e (delVarEnv env id))
+
 annotateExprFBWW (CoTyLam tyvar e) env = CoTyLam tyvar (annotateExprFBWW e env)
-annotateExprFBWW (CoApp f atom) env = CoApp (annotateExprFBWW f env) atom 
+annotateExprFBWW (App f atom) env = App (annotateExprFBWW f env) atom
 annotateExprFBWW (CoTyApp f ty) env = CoTyApp (annotateExprFBWW f env) ty
-annotateExprFBWW (CoSCC lab e) env = CoSCC lab (annotateExprFBWW e env)
-annotateExprFBWW (CoCase e alts) env = CoCase (annotateExprFBWW e env)
+annotateExprFBWW (Note note e) env = Note note (annotateExprFBWW e env)
+annotateExprFBWW (Case e alts) env = Case (annotateExprFBWW e env)
                                            (annotateAltsFBWW alts env)
-annotateExprFBWW (CoLet bnds e) env = CoLet bnds' (annotateExprFBWW e env')
+annotateExprFBWW (Let bnds e) env = Let bnds' (annotateExprFBWW e env')
   where
-       (env',bnds') = annotateBindingFBWW env bnds 
+       (env',bnds') = annotateBindingFBWW env bnds
 
-annotateAltsFBWW (CoAlgAlts alts deflt) env = CoAlgAlts alts' deflt'
+annotateAltsFBWW (AlgAlts alts deflt) env = AlgAlts alts' deflt'
   where
        alts' = [ let
                   binders' = map fst binders
-                 in (con,binders',annotateExprFBWW e (delManyFromIdEnv env binders'))
+                 in (con,binders',annotateExprFBWW e (delVarEnvList env binders'))
                                | (con,binders,e) <- alts ]
        deflt' = annotateDefFBWW deflt env
-annotateAltsFBWW (CoPrimAlts alts deflt) env = CoPrimAlts alts' deflt'
+annotateAltsFBWW (PrimAlts alts deflt) env = PrimAlts alts' deflt'
   where
        alts' = [ (lit,annotateExprFBWW e env) | (lit,e) <- alts ]
        deflt' = annotateDefFBWW deflt env
 
-annotateDefFBWW CoNoDefault env = CoNoDefault
-annotateDefFBWW (CoBindDefault v e) env 
-       = CoBindDefault (fst v) (annotateExprFBWW e (delOneFromIdEnv env (fst v)))
+annotateDefFBWW NoDefault env = NoDefault
+annotateDefFBWW (BindDefault v e) env
+       = BindDefault (fst v) (annotateExprFBWW e (delVarEnv env (fst v)))
 
-annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,PlainCoreBinding)
+annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,CoreBinding)
 annotateBindingFBWW env bnds = (env',bnds')
   where
        env' = analBind bnds env
        bnds' = case bnds of
-                 CoNonRec (v,_) e -> CoNonRec (fixId v) (annotateExprFBWW e env)
-                 CoRec bnds -> CoRec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
+                 NonRec (v,_) e -> NonRec (fixId v) (annotateExprFBWW e env)
+                 Rec bnds -> Rec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
        fixId v =
-               (case lookupIdEnv env' v of
+               (case lookupVarEnv env' v of
                   Just (IsFB ty@(FBType xs p))
-                   | not (null xs) -> pprTrace "ADDED to:" (ppr PprDebug v)
-                                       (addIdFBTypeInfo v (mkFBTypeInfo ty))
+                   | not (null xs) -> pprTrace "ADDED to:" (ppr v)
+                                       (addIdFBTypeInfo v (mkFBTypeInfo ty))
                   _ -> v)
+-}
 \end{code}