5bf768310ccc4fe7b9c97a8d87803f6b5d772adb
[ghc.git] / compiler / vectorise / Vectorise / Type / Classify.hs
1 -- Extract from a list of type constructors those (1) which need to be vectorised and (2) those
2 -- that could be, but need not be vectorised (as a scalar representation is sufficient and more
3 -- efficient). The type constructors that cannot be vectorised will be dropped.
4 --
5 -- A type constructor will only be vectorised if it is
6 --
7 -- (1) a data type constructor, with vanilla data constructors (i.e., data constructors admitted by
8 -- Haskell 98) and
9 -- (2) at least one of the type constructors that appears in its definition is also vectorised.
10 --
11 -- If (1) is met, but not (2), the type constructor may appear in vectorised code, but there is no
12 -- need to vectorise that type constructor itself. This holds, for example, for all enumeration
13 -- types. As '([::])' is being vectorised, any type constructor whose definition involves
14 -- '([::])', either directly or indirectly, will be vectorised.
15
16 module Vectorise.Type.Classify (
17 classifyTyCons
18 ) where
19
20 import UniqSet
21 import UniqFM
22 import DataCon
23 import TyCon
24 import TypeRep
25 import Type
26 import Digraph
27
28
29 -- |From a list of type constructors, extract those thatcan be vectorised, returning them in two
30 -- sets, where the first result list /must be/ vectorised and the second result list /need not be/
31 -- vectroised.
32
33 -- The first argument determines the /conversion status/ of external type constructors as follows:
34 --
35 -- * tycons which have converted versions are mapped to 'True'
36 -- * tycons which are not changed by vectorisation are mapped to 'False'
37 -- * tycons which can't be converted are not elements of the map
38 --
39 classifyTyCons :: UniqFM Bool -- ^type constructor conversion status
40 -> [TyCon] -- ^type constructors that need to be classified
41 -> ([TyCon], [TyCon]) -- ^tycons to be converted & not to be converted
42 classifyTyCons convStatus tcs = classify [] [] convStatus (tyConGroups tcs)
43 where
44 classify conv keep _ [] = (conv, keep)
45 classify conv keep cs ((tcs, ds) : rs)
46 | can_convert && must_convert
47 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc, True) | tc <- tcs]) rs
48 | can_convert
49 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc, False) | tc <- tcs]) rs
50 | otherwise
51 = classify conv keep cs rs
52 where
53 refs = ds `delListFromUniqSet` tcs
54
55 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
56 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
57
58 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
59
60 -- Used to group type constructors into mutually dependent groups.
61 --
62 type TyConGroup = ([TyCon], UniqSet TyCon)
63
64 -- Compute mutually recursive groups of tycons in topological order.
65 --
66 tyConGroups :: [TyCon] -> [TyConGroup]
67 tyConGroups tcs = map mk_grp (stronglyConnCompFromEdgedVertices edges)
68 where
69 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
70 , let ds = tyConsOfTyCon tc]
71
72 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
73 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
74 where
75 (tcs, dss) = unzip els
76
77 -- |Collect the set of TyCons used by the representation of some data type.
78 --
79 tyConsOfTyCon :: TyCon -> UniqSet TyCon
80 tyConsOfTyCon = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
81
82 -- |Collect the set of TyCons that occur in these types.
83 --
84 tyConsOfTypes :: [Type] -> UniqSet TyCon
85 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
86
87 -- |Collect the set of TyCons that occur in this type.
88 --
89 tyConsOfType :: Type -> UniqSet TyCon
90 tyConsOfType ty
91 | Just ty' <- coreView ty = tyConsOfType ty'
92 tyConsOfType (TyVarTy _) = emptyUniqSet
93 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
94 where
95 extend | isUnLiftedTyCon tc
96 || isTupleTyCon tc = id
97
98 | otherwise = (`addOneToUniqSet` tc)
99
100 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
101 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
102 `addOneToUniqSet` funTyCon
103 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty