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 ( SYN_IE(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 (nOfThem n 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{-Equal-} 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 (Coerce _ _ _) env = panic "AnalFBWW:analExprFBWW:Coerce"
144 analExprFBWW (Let binds e) env = analExprFBWW e (analBind binds env)
145 analExprFBWW (Case e alts) env = foldl1 joinFBType (analAltsFBWW alts env)
147 analAltsFBWW (AlgAlts alts deflt) env
148 = case analDefFBWW deflt env of
152 tys = map (\(con,binders,e) -> analExprFBWW e (delManyFromIdEnv env (map fst binders))) alts
153 analAltsFBWW (PrimAlts alts deflt) env
154 = case analDefFBWW deflt env of
158 tys = map (\(lit,e) -> analExprFBWW e env) alts
161 analDefFBWW NoDefault env = Nothing
162 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
166 Only add a type info if:
169 2. Is an inlineable object.
172 analBindExpr :: BinderInfo -> InExpr -> IdEnv OurFBType -> OurFBType
173 analBindExpr bnd expr env
174 = case analExprFBWW expr env of
175 IsFB ty@(FBType [] _) ->
176 if oneSafeOcc False bnd
181 analBind :: InBinding -> IdEnv OurFBType -> IdEnv OurFBType
182 analBind (NonRec (v,bnd) e) env =
183 case analBindExpr bnd e env of
184 ty@(IsFB _) -> addOneToIdEnv env v ty
185 ty@(IsCons) -> addOneToIdEnv env v ty
186 _ -> delOneFromIdEnv env v -- remember about shadowing!
188 analBind (Rec binds) env =
190 first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
191 (_,_,args,_) <- [collectBinders e]]
192 env' = delManyFromIdEnv env (map (fst.fst) binds)
194 growIdEnvList env' (fixpoint 0 binds env' first_set)
196 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
197 fixpoint n binds env maps =
200 else fixpoint (n+1) binds env maps'
202 env' = growIdEnvList env maps
203 maps' = [ (v,ty) | ((v,bind),e) <- binds,
204 (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
210 annotateExprFBWW :: InExpr -> IdEnv OurFBType -> CoreExpr
211 annotateExprFBWW (Var v) env = Var v
212 annotateExprFBWW (Lit i) env = Lit i
213 annotateExprFBWW (Con c t a) env = Con c t a
214 annotateExprFBWW (Prim p t a) env = Prim p t a
215 annotateExprFBWW (Lam (id,_) e) env
216 = Lam id (annotateExprFBWW e (delOneFromIdEnv env id))
218 annotateExprFBWW (CoTyLam tyvar e) env = CoTyLam tyvar (annotateExprFBWW e env)
219 annotateExprFBWW (App f atom) env = App (annotateExprFBWW f env) atom
220 annotateExprFBWW (CoTyApp f ty) env = CoTyApp (annotateExprFBWW f env) ty
221 annotateExprFBWW (SCC lab e) env = SCC lab (annotateExprFBWW e env)
222 annotateExprFBWW (Coerce c ty e) env = Coerce c ty (annotateExprFBWW e env)
223 annotateExprFBWW (Case e alts) env = Case (annotateExprFBWW e env)
224 (annotateAltsFBWW alts env)
225 annotateExprFBWW (Let bnds e) env = Let bnds' (annotateExprFBWW e env')
227 (env',bnds') = annotateBindingFBWW env bnds
229 annotateAltsFBWW (AlgAlts alts deflt) env = AlgAlts alts' deflt'
232 binders' = map fst binders
233 in (con,binders',annotateExprFBWW e (delManyFromIdEnv env binders'))
234 | (con,binders,e) <- alts ]
235 deflt' = annotateDefFBWW deflt env
236 annotateAltsFBWW (PrimAlts alts deflt) env = PrimAlts alts' deflt'
238 alts' = [ (lit,annotateExprFBWW e env) | (lit,e) <- alts ]
239 deflt' = annotateDefFBWW deflt env
241 annotateDefFBWW NoDefault env = NoDefault
242 annotateDefFBWW (BindDefault v e) env
243 = BindDefault (fst v) (annotateExprFBWW e (delOneFromIdEnv env (fst v)))
245 annotateBindingFBWW :: IdEnv OurFBType -> InBinding -> (IdEnv OurFBType,CoreBinding)
246 annotateBindingFBWW env bnds = (env',bnds')
248 env' = analBind bnds env
250 NonRec (v,_) e -> NonRec (fixId v) (annotateExprFBWW e env)
251 Rec bnds -> Rec [ (fixId v,annotateExprFBWW e env') | ((v,_),e) <- bnds ]
253 (case lookupIdEnv env' v of
254 Just (IsFB ty@(FBType xs p))
255 | not (null xs) -> pprTrace "ADDED to:" (ppr PprDebug v)
256 (addIdFBTypeInfo v (mkFBTypeInfo ty))