Classification of tycons for vectorisation
[ghc-hetmet.git] / compiler / vectorise / VectType.hs
1 module VectType ( vectTyCon, vectType )
2 where
3
4 #include "HsVersions.h"
5
6 import VectMonad
7 import VectUtils
8
9 import DataCon
10 import TyCon
11 import Type
12 import TypeRep
13
14 import UniqFM
15 import UniqSet
16 import Digraph           ( SCC(..), stronglyConnComp )
17
18 import Outputable
19
20 import Control.Monad  ( liftM2 )
21
22 -- ----------------------------------------------------------------------------
23 -- Types
24
25 vectTyCon :: TyCon -> VM TyCon
26 vectTyCon tc
27   | isFunTyCon tc        = builtin closureTyCon
28   | isBoxedTupleTyCon tc = return tc
29   | isUnLiftedTyCon tc   = return tc
30   | otherwise = do
31                   r <- lookupTyCon tc
32                   case r of
33                     Just tc' -> return tc'
34
35                     -- FIXME: just for now
36                     Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc
37
38 vectType :: Type -> VM Type
39 vectType ty | Just ty' <- coreView ty = vectType ty'
40 vectType (TyVarTy tv) = return $ TyVarTy tv
41 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
42 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
43 vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
44                                              (mapM vectType [ty1,ty2])
45 vectType ty@(ForAllTy _ _)
46   = do
47       mdicts   <- mapM paDictArgType tyvars
48       mono_ty' <- vectType mono_ty
49       return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
50   where
51     (tyvars, mono_ty) = splitForAllTys ty
52
53 vectType ty = pprPanic "vectType:" (ppr ty)
54
55 -- ----------------------------------------------------------------------------
56 -- Type definitions
57
58 type TyConGroup = ([TyCon], UniqSet TyCon)
59
60 -- | Split the given tycons into two sets depending on whether they have to be
61 -- converted (first list) or not (second list). The first argument contains
62 -- information about the conversion status of external tycons:
63 -- 
64 --   * tycons which have converted versions are mapped to True
65 --   * tycons which are not changed by vectorisation are mapped to False
66 --   * tycons which can't be converted are not elements of the map
67 --
68 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
69 classifyTyCons = classify [] []
70   where
71     classify conv keep cs [] = (conv, keep)
72     classify conv keep cs ((tcs, ds) : rs)
73       | can_convert && must_convert
74         = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
75       | can_convert
76         = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
77       | otherwise
78         = classify conv keep cs rs
79       where
80         refs = ds `delListFromUniqSet` tcs
81
82         can_convert  = isNullUFM (refs `minusUFM` cs) && all convertable tcs
83         must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
84
85         convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
86     
87 -- | Compute mutually recursive groups of tycons in topological order
88 --
89 tyConGroups :: [TyCon] -> [TyConGroup]
90 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
91   where
92     edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
93                                 , let ds = tyConsOfTyCon tc]
94
95     mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
96     mk_grp (CyclicSCC els)       = (tcs, unionManyUniqSets dss)
97       where
98         (tcs, dss) = unzip els
99
100 tyConsOfTyCon :: TyCon -> UniqSet TyCon
101 tyConsOfTyCon 
102   = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
103
104 tyConsOfType :: Type -> UniqSet TyCon
105 tyConsOfType ty
106   | Just ty' <- coreView ty    = tyConsOfType ty'
107 tyConsOfType (TyVarTy v)       = emptyUniqSet
108 tyConsOfType (TyConApp tc tys) = tyConsOfTypes tys `addOneToUniqSet` tc
109 tyConsOfType (AppTy a b)       = tyConsOfType a `unionUniqSets` tyConsOfType b
110 tyConsOfType (FunTy a b)       = (tyConsOfType a `unionUniqSets` tyConsOfType b)
111                                  `addOneToUniqSet` funTyCon
112 tyConsOfType (ForAllTy _ ty)   = tyConsOfType ty
113 tyConsOfType other             = pprPanic "ClosureConv.tyConsOfType" $ ppr other
114
115 tyConsOfTypes :: [Type] -> UniqSet TyCon
116 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
117