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 (Note _ e) env = analExprFBWW e env
141 analExprFBWW (Let binds e) env = analExprFBWW e (analBind binds env)
142 analExprFBWW (Case e alts) env = foldl1 joinFBType (analAltsFBWW alts env)
144 analAltsFBWW (AlgAlts alts deflt) env
145 = case analDefFBWW deflt env of
149 tys = map (\(con,binders,e) -> analExprFBWW e (delManyFromIdEnv env (map fst binders))) alts
150 analAltsFBWW (PrimAlts alts deflt) env
151 = case analDefFBWW deflt env of
155 tys = map (\(lit,e) -> analExprFBWW e env) alts
158 analDefFBWW NoDefault env = Nothing
159 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
163 Only add a type info if:
166 2. Is an inlineable object.
169 analBindExpr :: BinderInfo -> InExpr -> IdEnv OurFBType -> OurFBType
170 analBindExpr bnd expr env
171 = case analExprFBWW expr env of
172 IsFB ty@(FBType [] _) ->
173 if oneSafeOcc False bnd
178 analBind :: InBinding -> IdEnv OurFBType -> IdEnv OurFBType
179 analBind (NonRec (v,bnd) e) env =
180 case analBindExpr bnd e env of
181 ty@(IsFB _) -> addOneToIdEnv env v ty
182 ty@(IsCons) -> addOneToIdEnv env v ty
183 _ -> delOneFromIdEnv env v -- remember about shadowing!
185 analBind (Rec binds) env =
187 first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
188 (_,args,_) <- [collectBinders e]]
189 env' = delManyFromIdEnv env (map (fst.fst) binds)
191 growIdEnvList env' (fixpoint 0 binds env' first_set)
193 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
194 fixpoint n binds env maps =
197 else fixpoint (n+1) binds env maps'
199 env' = growIdEnvList env maps
200 maps' = [ (v,ty) | ((v,bind),e) <- binds,
201 (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
207 annotateExprFBWW :: InExpr -> IdEnv OurFBType -> CoreExpr
208 annotateExprFBWW (Var v) env = Var v
209 annotateExprFBWW (Lit i) env = Lit i
210 annotateExprFBWW (Con c t a) env = Con c t a
211 annotateExprFBWW (Prim p t a) env = Prim p t a
212 annotateExprFBWW (Lam (id,_) e) env
213 = Lam id (annotateExprFBWW e (delOneFromIdEnv env id))
215 annotateExprFBWW (CoTyLam tyvar e) env = CoTyLam tyvar (annotateExprFBWW e env)
216 annotateExprFBWW (App f atom) env = App (annotateExprFBWW f env) atom
217 annotateExprFBWW (CoTyApp f ty) env = CoTyApp (annotateExprFBWW f env) ty
218 annotateExprFBWW (Note note e) env = Note note (annotateExprFBWW e env)
219 annotateExprFBWW (Case e alts) env = Case (annotateExprFBWW e env)
220 (annotateAltsFBWW alts env)
221 annotateExprFBWW (Let bnds e) env = Let bnds' (annotateExprFBWW e env')
223 (env',bnds') = annotateBindingFBWW env bnds
225 annotateAltsFBWW (AlgAlts alts deflt) env = AlgAlts alts' deflt'
228 binders' = map fst binders
229 in (con,binders',annotateExprFBWW e (delManyFromIdEnv env binders'))
230 | (con,binders,e) <- alts ]
231 deflt' = annotateDefFBWW deflt env
232 annotateAltsFBWW (PrimAlts alts deflt) env = PrimAlts alts' deflt'
234 alts' = [ (lit,annotateExprFBWW e env) | (lit,e) <- alts ]
235 deflt' = annotateDefFBWW deflt env
237 annotateDefFBWW NoDefault env = NoDefault
238 annotateDefFBWW (BindDefault v e) env
239 = BindDefault (fst v) (annotateExprFBWW e (delOneFromIdEnv env (fst v)))
241 annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,CoreBinding)
242 annotateBindingFBWW env bnds = (env',bnds')
244 env' = analBind bnds env
246 NonRec (v,_) e -> NonRec (fixId v) (annotateExprFBWW e env)
247 Rec bnds -> Rec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
249 (case lookupIdEnv env' v of
250 Just (IsFB ty@(FBType xs p))
251 | not (null xs) -> pprTrace "ADDED to:" (ppr v)
252 (addIdFBTypeInfo v (mkFBTypeInfo ty))