1 module VectType ( vectTyCon, vectType )
4 #include "HsVersions.h"
16 import Digraph ( SCC(..), stronglyConnComp )
20 import Control.Monad ( liftM2 )
22 -- ----------------------------------------------------------------------------
25 vectTyCon :: TyCon -> VM TyCon
27 | isFunTyCon tc = builtin closureTyCon
28 | isBoxedTupleTyCon tc = return tc
29 | isUnLiftedTyCon tc = return tc
33 Just tc' -> return tc'
35 -- FIXME: just for now
36 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
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 _ _)
47 mdicts <- mapM paDictArgType tyvars
48 mono_ty' <- vectType mono_ty
49 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
51 (tyvars, mono_ty) = splitForAllTys ty
53 vectType ty = pprPanic "vectType:" (ppr ty)
55 -- ----------------------------------------------------------------------------
58 type TyConGroup = ([TyCon], UniqSet TyCon)
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:
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
68 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
69 classifyTyCons = classify [] []
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
76 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
78 = classify conv keep cs rs
80 refs = ds `delListFromUniqSet` tcs
82 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
83 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
85 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
87 -- | Compute mutually recursive groups of tycons in topological order
89 tyConGroups :: [TyCon] -> [TyConGroup]
90 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
92 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
93 , let ds = tyConsOfTyCon tc]
95 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
96 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
98 (tcs, dss) = unzip els
100 tyConsOfTyCon :: TyCon -> UniqSet TyCon
102 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
104 tyConsOfType :: Type -> UniqSet TyCon
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
115 tyConsOfTypes :: [Type] -> UniqSet TyCon
116 tyConsOfTypes = unionManyUniqSets . map tyConsOfType