[project @ 1998-01-08 18:03:08 by simonm]
[ghc-hetmet.git] / ghc / compiler / simplCore / AnalFBWW.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[AnalFBWW]{Spoting good functions for splitting into workers/wrappers}
5
6 \begin{code}
7 module AnalFBWW ( analFBWW ) where
8
9 #include "HsVersions.h"
10
11 import CoreSyn          ( CoreBinding )
12 import Util             ( panic{-ToDo:rm-} )
13
14 --import Util
15 --import Id                     ( addIdFBTypeInfo )
16 --import IdInfo
17 --import PrelInfo          ( foldrId, buildId,
18 --                        nilDataCon, consDataCon, mkListTy, mkFunTy,
19 --                        unpackCStringAppendId
20 --                      )
21 --import BinderInfo
22 --import SimplEnv               -- everything
23 --import OccurAnal      -- OLD: was NewOccurAnal
24 --import Maybes
25 \end{code}
26
27 \begin{code}
28 analFBWW
29         :: [CoreBinding]
30         -> [CoreBinding]
31
32 analFBWW = panic "analFBWW (ToDo)"
33
34 {- LATER:
35 analFBWW top_binds = trace "ANALFBWW" (snd anno)
36  where
37         anals :: [InBinding]
38         anals = newOccurAnalyseBinds top_binds (const False)
39         anno = mapAccumL annotateBindingFBWW nullIdEnv anals
40 \end{code}
41
42 \begin{code}
43 data OurFBType
44         = IsFB FBType
45         | IsNotFB               -- unknown
46         | IsCons                -- \ xy -> (:) ty xy
47         | IsBottom              -- _|_
48                 deriving (Eq)
49         -- We only handle *reasonable* types
50         -- Later might add concept of bottom
51         -- because foldr f z (<bottom>) = <bottom>
52 unknownFBType  = IsNotFB
53 goodProdFBType = IsFB (FBType [] FBGoodProd)
54
55 maybeFBtoFB (Just ty) = ty
56 maybeFBtoFB (Nothing) = IsNotFB
57
58 addArgs :: Int -> OurFBType -> OurFBType
59 addArgs n (IsFB (FBType args prod))
60         = IsFB (FBType (nOfThem n FBBadConsum ++ args) prod)
61 addArgs n IsNotFB = IsNotFB
62 addArgs n IsCons = panic "adding argument to a cons"
63 addArgs n IsBottom = IsNotFB
64
65 rmArg :: OurFBType -> OurFBType
66 rmArg (IsFB (FBType [] prod)) = IsNotFB -- panic "removing argument from producer"
67 rmArg (IsFB (FBType args prod)) = IsFB (FBType (tail args) prod)
68 rmArg IsBottom = IsBottom
69 rmArg _ = IsNotFB
70
71 joinFBType :: OurFBType -> OurFBType -> OurFBType
72 joinFBType (IsBottom) a = a
73 joinFBType a (IsBottom) = a
74 joinFBType (IsFB (FBType args prod)) (IsFB (FBType args' prod'))
75         | length args == length args' = (IsFB (FBType (zipWith{-Equal-} argJ args args')
76                                                       (prodJ prod prod')))
77    where
78         argJ FBGoodConsum FBGoodConsum = FBGoodConsum
79         argJ _            _            = FBBadConsum
80         prodJ FBGoodProd FBGoodProd    = FBGoodProd
81         prodJ _                   _    = FBBadProd
82
83 joinFBType _ _ = IsNotFB
84
85 --
86 -- Mutter :: IdEnv FBType need to be in an *inlinable* context.
87 --
88
89 analExprFBWW :: InExpr -> IdEnv OurFBType -> OurFBType
90
91 --
92 -- [ build g ]          is a good context
93 --
94 analExprFBWW (App (CoTyApp (Var bld) _) _) env
95         | bld == buildId         = goodProdFBType
96
97 --
98 -- [ foldr (:) ys xs ] ==> good
99 --                      (but better if xs)
100 --
101 analExprFBWW (App (App (App
102                 (CoTyApp (CoTyApp (Var foldr_id) _) _) (VarArg c)) _) _)
103                 env
104         | pprTrace ("FOLDR:" ++ show (foldr_id == foldrId,isCons c))
105                 (ppr foldr_id)
106                 (foldr_id == foldrId && isCons c) = goodProdFBType
107    where
108         isCons c = case lookupIdEnv env c of
109                     Just IsCons -> True
110                     _ -> False
111 analExprFBWW (Var v) env       = maybeFBtoFB (lookupIdEnv env v)
112 analExprFBWW (Lit _) _         = unknownFBType
113
114 --
115 -- [ x : xs ]  ==> good iff [ xs ] is good
116 --
117
118 analExprFBWW (Con con _ [_,VarArg y]) env
119         | con == consDataCon = maybeFBtoFB (lookupIdEnv env y)
120 --
121 -- [] is good
122 --
123 analExprFBWW (Con con _ []) _
124         | con == nilDataCon = goodProdFBType
125 analExprFBWW (Con _ _ _) _     = unknownFBType
126 analExprFBWW (Prim _ _ _) _    = unknownFBType
127
128 -- \ xy -> (:) ty xy == a CONS
129
130 analExprFBWW (Lam (x,_) (Lam (y,_)
131                 (Con con _ [VarArg x',VarArg y']))) env
132   | con == consDataCon && x == x' && y == y'
133   = IsCons
134 analExprFBWW (Lam (id,_) e) env
135   = addArgs 1 (analExprFBWW e (delOneFromIdEnv env id))
136
137 analExprFBWW (CoTyLam tyvar e) env = analExprFBWW e env
138 analExprFBWW (App f atom) env  = rmArg (analExprFBWW f env)
139 analExprFBWW (CoTyApp f ty) env  = analExprFBWW f env
140 analExprFBWW (SCC lab e) env   = analExprFBWW e env
141 analExprFBWW (Coerce _ _ _) env   = panic "AnalFBWW:analExprFBWW:Coerce"
142 analExprFBWW (Let binds e) env = analExprFBWW e (analBind binds env)
143 analExprFBWW (Case e alts) env = foldl1 joinFBType (analAltsFBWW alts env)
144
145 analAltsFBWW (AlgAlts alts deflt) env
146   = case analDefFBWW deflt env of
147         Just ty -> ty : tys
148         Nothing -> tys
149    where
150      tys = map (\(con,binders,e) -> analExprFBWW e (delManyFromIdEnv env (map fst binders))) alts
151 analAltsFBWW (PrimAlts alts deflt) env
152   = case analDefFBWW deflt env of
153         Just ty -> ty : tys
154         Nothing -> tys
155    where
156      tys = map (\(lit,e) -> analExprFBWW e env) alts
157
158
159 analDefFBWW NoDefault env = Nothing
160 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
161 \end{code}
162
163
164 Only add a type info if:
165
166 1. Is a functionn.
167 2. Is an inlineable object.
168
169 \begin{code}
170 analBindExpr :: BinderInfo -> InExpr -> IdEnv OurFBType -> OurFBType
171 analBindExpr bnd expr env
172   =    case analExprFBWW expr env of
173               IsFB ty@(FBType [] _) ->
174                    if oneSafeOcc False bnd
175                    then IsFB ty
176                    else IsNotFB
177               other -> other
178
179 analBind :: InBinding -> IdEnv OurFBType -> IdEnv OurFBType
180 analBind (NonRec (v,bnd) e) env =
181         case analBindExpr bnd e env of
182          ty@(IsFB _) -> addOneToIdEnv env v ty
183          ty@(IsCons) -> addOneToIdEnv env v ty
184          _ -> delOneFromIdEnv env v     -- remember about shadowing!
185
186 analBind (Rec binds) env =
187    let
188         first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
189                                 (_,args,_) <- [collectBinders e]]
190         env' = delManyFromIdEnv env (map (fst.fst) binds)
191    in
192         growIdEnvList env' (fixpoint 0 binds env' first_set)
193
194 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
195 fixpoint n binds env maps =
196         if maps == maps'
197         then maps
198         else fixpoint (n+1) binds env maps'
199    where
200         env' = growIdEnvList env maps
201         maps' = [ (v,ty) | ((v,bind),e) <- binds,
202                         (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
203
204 \end{code}
205
206
207 \begin{code}
208 annotateExprFBWW :: InExpr -> IdEnv OurFBType -> CoreExpr
209 annotateExprFBWW (Var v) env = Var v
210 annotateExprFBWW (Lit i) env = Lit i
211 annotateExprFBWW (Con c t a) env = Con c t a
212 annotateExprFBWW (Prim p t a) env = Prim p t a
213 annotateExprFBWW (Lam (id,_) e) env
214   = Lam id (annotateExprFBWW e (delOneFromIdEnv env id))
215
216 annotateExprFBWW (CoTyLam tyvar e) env = CoTyLam tyvar (annotateExprFBWW e env)
217 annotateExprFBWW (App f atom) env = App (annotateExprFBWW f env) atom
218 annotateExprFBWW (CoTyApp f ty) env = CoTyApp (annotateExprFBWW f env) ty
219 annotateExprFBWW (SCC lab e) env = SCC lab (annotateExprFBWW e env)
220 annotateExprFBWW (Coerce c ty e) env = Coerce c ty (annotateExprFBWW e env)
221 annotateExprFBWW (Case e alts) env = Case (annotateExprFBWW e env)
222                                             (annotateAltsFBWW alts env)
223 annotateExprFBWW (Let bnds e) env = Let bnds' (annotateExprFBWW e env')
224   where
225         (env',bnds') = annotateBindingFBWW env bnds
226
227 annotateAltsFBWW (AlgAlts alts deflt) env = AlgAlts alts' deflt'
228   where
229         alts' = [ let
230                    binders' = map fst binders
231                   in (con,binders',annotateExprFBWW e (delManyFromIdEnv env binders'))
232                                 | (con,binders,e) <- alts ]
233         deflt' = annotateDefFBWW deflt env
234 annotateAltsFBWW (PrimAlts alts deflt) env = PrimAlts alts' deflt'
235   where
236         alts' = [ (lit,annotateExprFBWW e env) | (lit,e) <- alts ]
237         deflt' = annotateDefFBWW deflt env
238
239 annotateDefFBWW NoDefault env = NoDefault
240 annotateDefFBWW (BindDefault v e) env
241         = BindDefault (fst v) (annotateExprFBWW e (delOneFromIdEnv env (fst v)))
242
243 annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,CoreBinding)
244 annotateBindingFBWW env bnds = (env',bnds')
245   where
246         env' = analBind bnds env
247         bnds' = case bnds of
248                   NonRec (v,_) e -> NonRec (fixId v) (annotateExprFBWW e env)
249                   Rec bnds -> Rec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
250         fixId v =
251                 (case lookupIdEnv env' v of
252                    Just (IsFB ty@(FBType xs p))
253                     | not (null xs) -> pprTrace "ADDED to:" (ppr v)
254                                         (addIdFBTypeInfo v (mkFBTypeInfo ty))
255                    _ -> v)
256 -}
257 \end{code}