Handle unlifted tycons and tuples correctly during vectorisation
[ghc-hetmet.git] / compiler / vectorise / VectType.hs
1 module VectType ( vectTyCon, vectType, vectTypeEnv )
2 where
3
4 #include "HsVersions.h"
5
6 import VectMonad
7 import VectUtils
8
9 import HscTypes          ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
10 import DataCon
11 import TyCon
12 import Type
13 import TypeRep
14 import OccName
15 import MkId
16 import BasicTypes        ( StrictnessMark(..), boolToRecFlag )
17 import NameEnv
18
19 import Unique
20 import UniqFM
21 import UniqSet
22 import Digraph           ( SCC(..), stronglyConnComp )
23
24 import Outputable
25
26 import Control.Monad  ( liftM2, zipWithM_ )
27
28 -- ----------------------------------------------------------------------------
29 -- Types
30
31 vectTyCon :: TyCon -> VM TyCon
32 vectTyCon tc
33   | isFunTyCon tc        = builtin closureTyCon
34   | isBoxedTupleTyCon tc = return tc
35   | isUnLiftedTyCon tc   = return tc
36   | otherwise = do
37                   r <- lookupTyCon tc
38                   case r of
39                     Just tc' -> return tc'
40
41                     -- FIXME: just for now
42                     Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc
43
44 vectType :: Type -> VM Type
45 vectType ty | Just ty' <- coreView ty = vectType ty'
46 vectType (TyVarTy tv) = return $ TyVarTy tv
47 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
48 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
49 vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
50                                              (mapM vectType [ty1,ty2])
51 vectType ty@(ForAllTy _ _)
52   = do
53       mdicts   <- mapM paDictArgType tyvars
54       mono_ty' <- vectType mono_ty
55       return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
56   where
57     (tyvars, mono_ty) = splitForAllTys ty
58
59 vectType ty = pprPanic "vectType:" (ppr ty)
60
61 -- ----------------------------------------------------------------------------
62 -- Type definitions
63
64 type TyConGroup = ([TyCon], UniqSet TyCon)
65
66 vectTypeEnv :: TypeEnv -> VM TypeEnv
67 vectTypeEnv env
68   = do
69       cs <- readGEnv $ mk_map . global_tycons
70       let (conv_tcs, keep_tcs) = classifyTyCons cs groups
71           keep_dcs             = concatMap tyConDataCons keep_tcs
72       zipWithM_ defTyCon   keep_tcs keep_tcs
73       zipWithM_ defDataCon keep_dcs keep_dcs
74       new_tcs <- vectTyConDecls conv_tcs
75       return $ extendTypeEnvList env
76                  (map ATyCon new_tcs ++ [ADataCon dc | tc <- new_tcs
77                                                      , dc <- tyConDataCons tc])
78   where
79     tycons = typeEnvTyCons env
80     groups = tyConGroups tycons
81
82     mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
83
84     keep_tc tc = let dcs = tyConDataCons tc
85                  in
86                  defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
87
88
89 vectTyConDecls :: [TyCon] -> VM [TyCon]
90 vectTyConDecls tcs = fixV $ \tcs' ->
91   do
92     mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
93     mapM vectTyConDecl tcs
94   where
95     lazy_zip [] _ = []
96     lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
97
98 vectTyConDecl :: TyCon -> VM TyCon
99 vectTyConDecl tc
100   = do
101       name' <- cloneName mkVectTyConOcc name
102       rhs'  <- vectAlgTyConRhs (algTyConRhs tc)
103
104       return $ mkAlgTyCon name'
105                           kind
106                           tyvars
107                           []              -- no stupid theta
108                           rhs'
109                           []              -- no selector ids
110                           NoParentTyCon   -- FIXME
111                           rec_flag        -- FIXME: is this ok?
112                           False           -- FIXME: no generics
113                           False           -- not GADT syntax
114   where
115     name   = tyConName tc
116     kind   = tyConKind tc
117     tyvars = tyConTyVars tc
118     rec_flag = boolToRecFlag (isRecursiveTyCon tc)
119
120 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
121 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
122                            , is_enum   = is_enum
123                            })
124   = do
125       data_cons' <- mapM vectDataCon data_cons
126       zipWithM_ defDataCon data_cons data_cons'
127       return $ DataTyCon { data_cons = data_cons'
128                          , is_enum   = is_enum
129                          }
130
131 vectDataCon :: DataCon -> VM DataCon
132 vectDataCon dc
133   | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
134   | not . null $ dataConEqSpec   dc = pprPanic "vectDataCon: eq spec" (ppr dc)
135   | otherwise
136   = do
137       name'    <- cloneName mkVectDataConOcc name
138       tycon'   <- vectTyCon tycon
139       arg_tys  <- mapM vectType rep_arg_tys
140       wrk_name <- cloneName mkDataConWorkerOcc name'
141
142       let ids      = mkDataConIds (panic "vectDataCon: wrapped id")
143                                   wrk_name
144                                   data_con
145           data_con = mkDataCon name'
146                                False           -- not infix
147                                (map (const NotMarkedStrict) arg_tys)
148                                []              -- no labelled fields
149                                univ_tvs
150                                []              -- no existential tvs for now
151                                []              -- no eq spec for now
152                                []              -- no theta
153                                arg_tys
154                                tycon'
155                                []              -- no stupid theta
156                                ids
157       return data_con
158   where
159     name        = dataConName dc
160     univ_tvs    = dataConUnivTyVars dc
161     rep_arg_tys = dataConOrigArgTys dc
162     tycon       = dataConTyCon dc
163
164 -- | Split the given tycons into two sets depending on whether they have to be
165 -- converted (first list) or not (second list). The first argument contains
166 -- information about the conversion status of external tycons:
167 -- 
168 --   * tycons which have converted versions are mapped to True
169 --   * tycons which are not changed by vectorisation are mapped to False
170 --   * tycons which can't be converted are not elements of the map
171 --
172 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
173 classifyTyCons = classify [] []
174   where
175     classify conv keep cs [] = (conv, keep)
176     classify conv keep cs ((tcs, ds) : rs)
177       | can_convert && must_convert
178         = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
179       | can_convert
180         = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
181       | otherwise
182         = classify conv keep cs rs
183       where
184         refs = ds `delListFromUniqSet` tcs
185
186         can_convert  = isNullUFM (refs `minusUFM` cs) && all convertable tcs
187         must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
188
189         convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
190     
191 -- | Compute mutually recursive groups of tycons in topological order
192 --
193 tyConGroups :: [TyCon] -> [TyConGroup]
194 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
195   where
196     edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
197                                 , let ds = tyConsOfTyCon tc]
198
199     mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
200     mk_grp (CyclicSCC els)       = (tcs, unionManyUniqSets dss)
201       where
202         (tcs, dss) = unzip els
203
204 tyConsOfTyCon :: TyCon -> UniqSet TyCon
205 tyConsOfTyCon 
206   = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
207
208 tyConsOfType :: Type -> UniqSet TyCon
209 tyConsOfType ty
210   | Just ty' <- coreView ty    = tyConsOfType ty'
211 tyConsOfType (TyVarTy v)       = emptyUniqSet
212 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
213   where
214     extend | isUnLiftedTyCon tc
215            || isTupleTyCon   tc = id
216
217            | otherwise          = (`addOneToUniqSet` tc)
218
219 tyConsOfType (AppTy a b)       = tyConsOfType a `unionUniqSets` tyConsOfType b
220 tyConsOfType (FunTy a b)       = (tyConsOfType a `unionUniqSets` tyConsOfType b)
221                                  `addOneToUniqSet` funTyCon
222 tyConsOfType (ForAllTy _ ty)   = tyConsOfType ty
223 tyConsOfType other             = pprPanic "ClosureConv.tyConsOfType" $ ppr other
224
225 tyConsOfTypes :: [Type] -> UniqSet TyCon
226 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
227