8422c18695235137e9e2891b0ab9c10b0ef54d7d
[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 #include "HsVersions.h"
8
9 module AnalFBWW ( analFBWW ) where
10
11 import Ubiq{-uitous-}
12
13 import CoreSyn          ( CoreBinding(..) )
14 import Util             ( panic{-ToDo:rm-} )
15
16 --import Util
17 --import Id                     ( addIdFBTypeInfo )
18 --import IdInfo
19 --import PrelInfo          ( foldrId, buildId,
20 --                        nilDataCon, consDataCon, mkListTy, mkFunTy,
21 --                        unpackCStringAppendId
22 --                      )
23 --import BinderInfo
24 --import SimplEnv               -- everything
25 --import OccurAnal      -- OLD: was NewOccurAnal
26 --import Maybes
27 \end{code}
28
29 \begin{code}
30 analFBWW
31         :: [CoreBinding]
32         -> [CoreBinding]
33
34 analFBWW = panic "analFBWW (ToDo)"
35
36 {- LATER:
37 analFBWW top_binds = trace "ANALFBWW" (snd anno)
38  where
39         anals :: [InBinding]
40         anals = newOccurAnalyseBinds top_binds (const False)
41         anno = mapAccumL annotateBindingFBWW nullIdEnv anals
42 \end{code}
43
44 \begin{code}
45 data OurFBType
46         = IsFB FBType
47         | IsNotFB               -- unknown
48         | IsCons                -- \ xy -> (:) ty xy
49         | IsBottom              -- _|_
50                 deriving (Eq)
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)
56
57 maybeFBtoFB (Just ty) = ty
58 maybeFBtoFB (Nothing) = IsNotFB
59
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
66
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
71 rmArg _ = IsNotFB
72
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')
78                                                       (prodJ prod prod')))
79    where
80         argJ FBGoodConsum FBGoodConsum = FBGoodConsum
81         argJ _            _            = FBBadConsum
82         prodJ FBGoodProd FBGoodProd    = FBGoodProd
83         prodJ _                   _    = FBBadProd
84
85 joinFBType _ _ = IsNotFB
86
87 --
88 -- Mutter :: IdEnv FBType need to be in an *inlinable* context.
89 --
90
91 analExprFBWW :: InExpr -> IdEnv OurFBType -> OurFBType
92
93 --
94 -- [ build g ]          is a good context
95 --
96 analExprFBWW (App (CoTyApp (Var bld) _) _) env
97         | bld == buildId         = goodProdFBType
98
99 --
100 -- [ foldr (:) ys xs ] ==> good
101 --                      (but better if xs)
102 --
103 analExprFBWW (App (App (App
104                 (CoTyApp (CoTyApp (Var foldr_id) _) _) (VarArg c)) _) _)
105                 env
106         | pprTrace ("FOLDR:" ++ show (foldr_id == foldrId,isCons c))
107                 (ppr PprDebug foldr_id)
108                 (foldr_id == foldrId && isCons c) = goodProdFBType
109    where
110         isCons c = case lookupIdEnv env c of
111                     Just IsCons -> True
112                     _ -> False
113 analExprFBWW (Var v) env       = maybeFBtoFB (lookupIdEnv env v)
114 analExprFBWW (Lit _) _         = unknownFBType
115
116 --
117 -- [ x : xs ]  ==> good iff [ xs ] is good
118 --
119
120 analExprFBWW (Con con _ [_,VarArg y]) env
121         | con == consDataCon = maybeFBtoFB (lookupIdEnv env y)
122 --
123 -- [] is good
124 --
125 analExprFBWW (Con con _ []) _
126         | con == nilDataCon = goodProdFBType
127 analExprFBWW (Con _ _ _) _     = unknownFBType
128 analExprFBWW (Prim _ _ _) _    = unknownFBType
129
130 -- \ xy -> (:) ty xy == a CONS
131
132 analExprFBWW (Lam (x,_) (Lam (y,_)
133                 (Con con _ [VarArg x',VarArg y']))) env
134   | con == consDataCon && x == x' && y == y'
135   = IsCons
136 analExprFBWW (Lam (id,_) e) env
137   = addArgs 1 (analExprFBWW e (delOneFromIdEnv env id))
138
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)
145
146 analAltsFBWW (AlgAlts alts deflt) env
147   = case analDefFBWW deflt env of
148         Just ty -> ty : tys
149         Nothing -> tys
150    where
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
154         Just ty -> ty : tys
155         Nothing -> tys
156    where
157      tys = map (\(lit,e) -> analExprFBWW e env) alts
158
159
160 analDefFBWW NoDefault env = Nothing
161 analDefFBWW (BindDefault v e) env = Just (analExprFBWW e (delOneFromIdEnv env (fst v)))
162 \end{code}
163
164
165 Only add a type info if:
166
167 1. Is a functionn.
168 2. Is an inlineable object.
169
170 \begin{code}
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
176                    then IsFB ty
177                    else IsNotFB
178               other -> other
179
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!
186
187 analBind (Rec binds) env =
188    let
189         first_set = [ (v,IsFB (FBType [FBBadConsum | _ <- args ] FBGoodProd)) | ((v,_),e) <- binds,
190                                 (_,_,args,_) <- [collectBinders e]]
191         env' = delManyFromIdEnv env (map (fst.fst) binds)
192    in
193         growIdEnvList env' (fixpoint 0 binds env' first_set)
194
195 fixpoint :: Int -> [(InBinder,InExpr)] -> IdEnv OurFBType -> [(Id,OurFBType)] -> [(Id,OurFBType)]
196 fixpoint n binds env maps =
197         if maps == maps'
198         then maps
199         else fixpoint (n+1) binds env maps'
200    where
201         env' = growIdEnvList env maps
202         maps' = [ (v,ty) | ((v,bind),e) <- binds,
203                         (ty@(IsFB (FBType cons prod))) <- [analBindExpr bind e env']]
204
205 \end{code}
206
207
208 \begin{code}
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))
216
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')
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 PprDebug v)
254                                         (addIdFBTypeInfo v (mkFBTypeInfo ty))
255                    _ -> v)
256 -}
257 \end{code}