2 module Vectorise.Type.Classify
16 type TyConGroup = ([TyCon], UniqSet TyCon)
18 -- | Split the given tycons into two sets depending on whether they have to be
19 -- converted (first list) or not (second list). The first argument contains
20 -- information about the conversion status of external tycons:
22 -- * tycons which have converted versions are mapped to True
23 -- * tycons which are not changed by vectorisation are mapped to False
24 -- * tycons which can't be converted are not elements of the map
31 classifyTyCons = classify [] []
33 classify conv keep _ [] = (conv, keep)
34 classify conv keep cs ((tcs, ds) : rs)
35 | can_convert && must_convert
36 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
38 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
40 = classify conv keep cs rs
42 refs = ds `delListFromUniqSet` tcs
44 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
45 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
47 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
50 -- | Compute mutually recursive groups of tycons in topological order
51 tyConGroups :: [TyCon] -> [TyConGroup]
52 tyConGroups tcs = map mk_grp (stronglyConnCompFromEdgedVertices edges)
54 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
55 , let ds = tyConsOfTyCon tc]
57 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
58 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
60 (tcs, dss) = unzip els
63 -- | Collect the set of TyCons used by the representation of some data type.
64 tyConsOfTyCon :: TyCon -> UniqSet TyCon
66 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
69 -- | Collect the set of TyCons that occur in these types.
70 tyConsOfTypes :: [Type] -> UniqSet TyCon
71 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
74 -- | Collect the set of TyCons that occur in this type.
75 tyConsOfType :: Type -> UniqSet TyCon
77 | Just ty' <- coreView ty = tyConsOfType ty'
78 tyConsOfType (TyVarTy _) = emptyUniqSet
79 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
81 extend | isUnLiftedTyCon tc
82 || isTupleTyCon tc = id
84 | otherwise = (`addOneToUniqSet` tc)
86 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
87 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
88 `addOneToUniqSet` funTyCon
89 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
90 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other