2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[AnalFBWW]{Spoting good functions for splitting into workers/wrappers}
7 #include "HsVersions.h"
9 module AnalFBWW ( analFBWW ) where
13 import CoreSyn ( CoreBinding(..) )
14 import Util ( panic{-ToDo:rm-} )
17 --import Id ( addIdFBTypeInfo )
19 --import PrelInfo ( foldrId, buildId,
20 -- nilDataCon, consDataCon, mkListTy, mkFunTy,
21 -- unpackCStringAppendId
24 --import SimplEnv -- everything
25 --import OccurAnal -- OLD: was NewOccurAnal
34 analFBWW = panic "analFBWW (ToDo)"
37 analFBWW top_binds = trace "ANALFBWW" (snd anno)
40 anals = newOccurAnalyseBinds top_binds (const False)
41 anno = mapAccumL annotateBindingFBWW nullIdEnv anals
48 | IsCons -- \ xy -> (:) ty xy
51 -- We only handle *reasonable* types
52 -- Later might add concept of bottom
53 -- because foldr f z (<bottom>) = <bottom>
54 unknownFBType = IsNotFB
55 goodProdFBType = IsFB (FBType [] FBGoodProd)
57 maybeFBtoFB (Just ty) = ty
58 maybeFBtoFB (Nothing) = IsNotFB
60 addArgs :: Int -> OurFBType -> OurFBType
61 addArgs n (IsFB (FBType args prod))
62 = IsFB (FBType (take n (repeat FBBadConsum) ++ args) prod)
63 addArgs n IsNotFB = IsNotFB
64 addArgs n IsCons = panic "adding argument to a cons"
65 addArgs n IsBottom = IsNotFB
67 rmArg :: OurFBType -> OurFBType
68 rmArg (IsFB (FBType [] prod)) = IsNotFB -- panic "removing argument from producer"
69 rmArg (IsFB (FBType args prod)) = IsFB (FBType (tail args) prod)
70 rmArg IsBottom = IsBottom
73 joinFBType :: OurFBType -> OurFBType -> OurFBType
74 joinFBType (IsBottom) a = a
75 joinFBType a (IsBottom) = a
76 joinFBType (IsFB (FBType args prod)) (IsFB (FBType args' prod'))
77 | length args == length args' = (IsFB (FBType (zipWith argJ args args')
80 argJ FBGoodConsum FBGoodConsum = FBGoodConsum
81 argJ _ _ = FBBadConsum
82 prodJ FBGoodProd FBGoodProd = FBGoodProd
85 joinFBType _ _ = IsNotFB
88 -- Mutter :: IdEnv FBType need to be in an *inlinable* context.
91 analExprFBWW :: InExpr -> IdEnv OurFBType -> OurFBType
94 -- [ build g ] is a good context
96 analExprFBWW (App (CoTyApp (Var bld) _) _) env
97 | bld == buildId = goodProdFBType
100 -- [ foldr (:) ys xs ] ==> good
101 -- (but better if xs)
103 analExprFBWW (App (App (App
104 (CoTyApp (CoTyApp (Var foldr_id) _) _) (VarArg c)) _) _)
106 | pprTrace ("FOLDR:" ++ show (foldr_id == foldrId,isCons c))
107 (ppr PprDebug foldr_id)
108 (foldr_id == foldrId && isCons c) = goodProdFBType
110 isCons c = case lookupIdEnv env c of
113 analExprFBWW (Var v) env = maybeFBtoFB (lookupIdEnv env v)
114 analExprFBWW (Lit _) _ = unknownFBType
117 -- [ x : xs ] ==> good iff [ xs ] is good
120 analExprFBWW (Con con _ [_,VarArg y]) env
121 | con == consDataCon = maybeFBtoFB (lookupIdEnv env y)
125 analExprFBWW (Con con _ []) _
126 | con == nilDataCon = goodProdFBType
127 analExprFBWW (Con _ _ _) _ = unknownFBType
128 analExprFBWW (Prim _ _ _) _ = unknownFBType
130 -- \ xy -> (:) ty xy == a CONS
132 analExprFBWW (Lam (x,_) (Lam (y,_)
133 (Con con _ [VarArg x',VarArg y']))) env
134 | con == consDataCon && x == x' && y == y'
136 analExprFBWW (Lam (id,_) e) env
137 = addArgs 1 (analExprFBWW e (delOneFromIdEnv env id))
139 analExprFBWW (CoTyLam tyvar e) env = analExprFBWW e env
140 analExprFBWW (App f atom) env = rmArg (analExprFBWW f env)
141 analExprFBWW (CoTyApp f ty) env = analExprFBWW f env
142 analExprFBWW (SCC lab e) env = analExprFBWW e env
143 analExprFBWW (Let binds e) env = analExprFBWW e (analBind binds env)
144 analExprFBWW (Case e alts) env = foldl1 joinFBType (analAltsFBWW alts env)
146 analAltsFBWW (AlgAlts alts deflt) env
147 = case analDefFBWW deflt env of
151 tys = map (\(con,binders,e) -> analExprFBWW e (delManyFromIdEnv env (map fst binders))) alts
152 analAltsFBWW (PrimAlts alts deflt) env
153 = case analDefFBWW deflt env of
157 tys = map (\(lit,e) -> analExprFBWW e env) alts
160 analDefFBWW NoDefault env = Nothing
161 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
165 Only add a type info if:
168 2. Is an inlineable object.
171 analBindExpr :: BinderInfo -> InExpr -> IdEnv OurFBType -> OurFBType
172 analBindExpr bnd expr env
173 = case analExprFBWW expr env of
174 IsFB ty@(FBType [] _) ->
175 if oneSafeOcc False bnd
180 analBind :: InBinding -> IdEnv OurFBType -> IdEnv OurFBType
181 analBind (NonRec (v,bnd) e) env =
182 case analBindExpr bnd e env of
183 ty@(IsFB _) -> addOneToIdEnv env v ty
184 ty@(IsCons) -> addOneToIdEnv env v ty
185 _ -> delOneFromIdEnv env v -- remember about shadowing!
187 analBind (Rec binds) env =
189 first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
190 (_,_,args,_) <- [collectBinders e]]
191 env' = delManyFromIdEnv env (map (fst.fst) binds)
193 growIdEnvList env' (fixpoint 0 binds env' first_set)
195 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
196 fixpoint n binds env maps =
199 else fixpoint (n+1) binds env maps'
201 env' = growIdEnvList env maps
202 maps' = [ (v,ty) | ((v,bind),e) <- binds,
203 (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
209 annotateExprFBWW :: InExpr -> IdEnv OurFBType -> CoreExpr
210 annotateExprFBWW (Var v) env = Var v
211 annotateExprFBWW (Lit i) env = Lit i
212 annotateExprFBWW (Con c t a) env = Con c t a
213 annotateExprFBWW (Prim p t a) env = Prim p t a
214 annotateExprFBWW (Lam (id,_) e) env
215 = Lam id (annotateExprFBWW e (delOneFromIdEnv env id))
217 annotateExprFBWW (CoTyLam tyvar e) env = CoTyLam tyvar (annotateExprFBWW e env)
218 annotateExprFBWW (App f atom) env = App (annotateExprFBWW f env) atom
219 annotateExprFBWW (CoTyApp f ty) env = CoTyApp (annotateExprFBWW f env) ty
220 annotateExprFBWW (SCC lab e) env = SCC lab (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 PprDebug v)
254 (addIdFBTypeInfo v (mkFBTypeInfo ty))