2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[AnalFBWW]{Spoting good functions for splitting into workers/wrappers}
7 module AnalFBWW ( analFBWW ) where
9 #include "HsVersions.h"
11 import CoreSyn ( CoreBinding )
12 import Util ( panic{-ToDo:rm-} )
15 --import Id ( addIdFBTypeInfo )
17 --import PrelInfo ( foldrId, buildId,
18 -- nilDataCon, consDataCon, mkListTy, mkFunTy,
19 -- unpackCStringAppendId
22 --import SimplEnv -- everything
23 --import OccurAnal -- OLD: was NewOccurAnal
32 analFBWW = panic "analFBWW (ToDo)"
35 analFBWW top_binds = trace "ANALFBWW" (snd anno)
38 anals = newOccurAnalyseBinds top_binds (const False)
39 anno = mapAccumL annotateBindingFBWW nullIdEnv anals
46 | IsCons -- \ xy -> (:) ty xy
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)
55 maybeFBtoFB (Just ty) = ty
56 maybeFBtoFB (Nothing) = IsNotFB
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
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
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')
78 argJ FBGoodConsum FBGoodConsum = FBGoodConsum
79 argJ _ _ = FBBadConsum
80 prodJ FBGoodProd FBGoodProd = FBGoodProd
83 joinFBType _ _ = IsNotFB
86 -- Mutter :: IdEnv FBType need to be in an *inlinable* context.
89 analExprFBWW :: InExpr -> IdEnv OurFBType -> OurFBType
92 -- [ build g ] is a good context
94 analExprFBWW (App (CoTyApp (Var bld) _) _) env
95 | bld == buildId = goodProdFBType
98 -- [ foldr (:) ys xs ] ==> good
101 analExprFBWW (App (App (App
102 (CoTyApp (CoTyApp (Var foldr_id) _) _) (VarArg c)) _) _)
104 | pprTrace ("FOLDR:" ++ show (foldr_id == foldrId,isCons c))
106 (foldr_id == foldrId && isCons c) = goodProdFBType
108 isCons c = case lookupIdEnv env c of
111 analExprFBWW (Var v) env = maybeFBtoFB (lookupIdEnv env v)
112 analExprFBWW (Lit _) _ = unknownFBType
115 -- [ x : xs ] ==> good iff [ xs ] is good
118 analExprFBWW (Con con _ [_,VarArg y]) env
119 | con == consDataCon = maybeFBtoFB (lookupIdEnv env y)
123 analExprFBWW (Con con _ []) _
124 | con == nilDataCon = goodProdFBType
125 analExprFBWW (Con _ _ _) _ = unknownFBType
126 analExprFBWW (Prim _ _ _) _ = unknownFBType
128 -- \ xy -> (:) ty xy == a CONS
130 analExprFBWW (Lam (x,_) (Lam (y,_)
131 (Con con _ [VarArg x',VarArg y']))) env
132 | con == consDataCon && x == x' && y == y'
134 analExprFBWW (Lam (id,_) e) env
135 = addArgs 1 (analExprFBWW e (delOneFromIdEnv env id))
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)
145 analAltsFBWW (AlgAlts alts deflt) env
146 = case analDefFBWW deflt env of
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
156 tys = map (\(lit,e) -> analExprFBWW e env) alts
159 analDefFBWW NoDefault env = Nothing
160 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
164 Only add a type info if:
167 2. Is an inlineable object.
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
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!
186 analBind (Rec binds) env =
188 first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
189 (_,args,_) <- [collectBinders e]]
190 env' = delManyFromIdEnv env (map (fst.fst) binds)
192 growIdEnvList env' (fixpoint 0 binds env' first_set)
194 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
195 fixpoint n binds env maps =
198 else fixpoint (n+1) binds env maps'
200 env' = growIdEnvList env maps
201 maps' = [ (v,ty) | ((v,bind),e) <- binds,
202 (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
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))
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')
225 (env',bnds') = annotateBindingFBWW env bnds
227 annotateAltsFBWW (AlgAlts alts deflt) env = AlgAlts alts' deflt'
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'
236 alts' = [ (lit,annotateExprFBWW e env) | (lit,e) <- alts ]
237 deflt' = annotateDefFBWW deflt env
239 annotateDefFBWW NoDefault env = NoDefault
240 annotateDefFBWW (BindDefault v e) env
241 = BindDefault (fst v) (annotateExprFBWW e (delOneFromIdEnv env (fst v)))
243 annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,CoreBinding)
244 annotateBindingFBWW env bnds = (env',bnds')
246 env' = analBind bnds env
248 NonRec (v,_) e -> NonRec (fixId v) (annotateExprFBWW e env)
249 Rec bnds -> Rec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
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))