1 module VectType ( vectTyCon, vectType, vectTypeEnv,
2 PAInstance, buildPADict )
5 #include "HsVersions.h"
11 import HscTypes ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
20 import FamInstEnv ( FamInst, mkLocalFamInst )
21 import InstEnv ( Instance, mkLocalInstance, instanceDFunId )
24 import BasicTypes ( StrictnessMark(..), OverlapFlag(..), boolToRecFlag )
26 import Id ( mkWildId )
27 import Name ( Name, getOccName )
29 import TysWiredIn ( unitTy, unitTyCon, intTy, intDataCon, unitDataConId )
30 import TysPrim ( intPrimTy )
35 import Digraph ( SCC(..), stronglyConnComp )
39 import Control.Monad ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
40 import Data.List ( inits, tails, zipWith4, zipWith5 )
42 -- ----------------------------------------------------------------------------
45 vectTyCon :: TyCon -> VM TyCon
47 | isFunTyCon tc = builtin closureTyCon
48 | isBoxedTupleTyCon tc = return tc
49 | isUnLiftedTyCon tc = return tc
53 Just tc' -> return tc'
55 -- FIXME: just for now
56 Nothing -> pprTrace "ccTyCon:" (ppr tc) $ return tc
58 vectType :: Type -> VM Type
59 vectType ty | Just ty' <- coreView ty = vectType ty'
60 vectType (TyVarTy tv) = return $ TyVarTy tv
61 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
62 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
63 vectType (FunTy ty1 ty2) = liftM2 TyConApp (builtin closureTyCon)
64 (mapM vectType [ty1,ty2])
65 vectType ty@(ForAllTy _ _)
67 mdicts <- mapM paDictArgType tyvars
68 mono_ty' <- vectType mono_ty
69 return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
71 (tyvars, mono_ty) = splitForAllTys ty
73 vectType ty = pprPanic "vectType:" (ppr ty)
75 -- ----------------------------------------------------------------------------
78 type TyConGroup = ([TyCon], UniqSet TyCon)
80 data PAInstance = PAInstance {
82 , painstOrigTyCon :: TyCon
83 , painstVectTyCon :: TyCon
84 , painstArrTyCon :: TyCon
87 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
90 cs <- readGEnv $ mk_map . global_tycons
91 let (conv_tcs, keep_tcs) = classifyTyCons cs groups
92 keep_dcs = concatMap tyConDataCons keep_tcs
93 zipWithM_ defTyCon keep_tcs keep_tcs
94 zipWithM_ defDataCon keep_dcs keep_dcs
95 new_tcs <- vectTyConDecls conv_tcs
97 let orig_tcs = keep_tcs ++ conv_tcs
98 vect_tcs = keep_tcs ++ new_tcs
100 repr_tcs <- zipWithM buildPReprTyCon orig_tcs vect_tcs
101 parr_tcs <- zipWithM buildPArrayTyCon orig_tcs vect_tcs
102 dfuns <- mapM mkPADFun vect_tcs
103 defTyConPAs (zip vect_tcs dfuns)
104 binds <- sequence (zipWith5 buildTyConBindings orig_tcs
110 let all_new_tcs = new_tcs ++ repr_tcs ++ parr_tcs
112 let new_env = extendTypeEnvList env
113 (map ATyCon all_new_tcs
114 ++ [ADataCon dc | tc <- all_new_tcs
115 , dc <- tyConDataCons tc])
117 return (new_env, map mkLocalFamInst (repr_tcs ++ parr_tcs), concat binds)
119 tycons = typeEnvTyCons env
120 groups = tyConGroups tycons
122 mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
124 keep_tc tc = let dcs = tyConDataCons tc
126 defTyCon tc tc >> zipWithM_ defDataCon dcs dcs
129 vectTyConDecls :: [TyCon] -> VM [TyCon]
130 vectTyConDecls tcs = fixV $ \tcs' ->
132 mapM_ (uncurry defTyCon) (lazy_zip tcs tcs')
133 mapM vectTyConDecl tcs
136 lazy_zip (x:xs) ~(y:ys) = (x,y) : lazy_zip xs ys
138 vectTyConDecl :: TyCon -> VM TyCon
141 name' <- cloneName mkVectTyConOcc name
142 rhs' <- vectAlgTyConRhs (algTyConRhs tc)
144 liftDs $ buildAlgTyCon name'
146 [] -- no stupid theta
148 rec_flag -- FIXME: is this ok?
149 False -- FIXME: no generics
150 False -- not GADT syntax
151 Nothing -- not a family instance
154 tyvars = tyConTyVars tc
155 rec_flag = boolToRecFlag (isRecursiveTyCon tc)
157 vectAlgTyConRhs :: AlgTyConRhs -> VM AlgTyConRhs
158 vectAlgTyConRhs (DataTyCon { data_cons = data_cons
162 data_cons' <- mapM vectDataCon data_cons
163 zipWithM_ defDataCon data_cons data_cons'
164 return $ DataTyCon { data_cons = data_cons'
168 vectDataCon :: DataCon -> VM DataCon
170 | not . null $ dataConExTyVars dc = pprPanic "vectDataCon: existentials" (ppr dc)
171 | not . null $ dataConEqSpec dc = pprPanic "vectDataCon: eq spec" (ppr dc)
174 name' <- cloneName mkVectDataConOcc name
175 tycon' <- vectTyCon tycon
176 arg_tys <- mapM vectType rep_arg_tys
178 liftDs $ buildDataCon name'
180 (map (const NotMarkedStrict) arg_tys)
181 [] -- no labelled fields
183 [] -- no existential tvs for now
184 [] -- no eq spec for now
189 name = dataConName dc
190 univ_tvs = dataConUnivTyVars dc
191 rep_arg_tys = dataConRepArgTys dc
192 tycon = dataConTyCon dc
194 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
195 mk_fam_inst fam_tc arg_tc
196 = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
198 buildPReprTyCon :: TyCon -> TyCon -> VM TyCon
199 buildPReprTyCon orig_tc vect_tc
201 name <- cloneName mkPReprTyConOcc (tyConName orig_tc)
202 rhs_ty <- buildPReprType vect_tc
203 prepr_tc <- builtin preprTyCon
204 liftDs $ buildSynTyCon name
206 (SynonymTyCon rhs_ty)
207 (Just $ mk_fam_inst prepr_tc vect_tc)
209 tyvars = tyConTyVars vect_tc
212 data Repr = ProdRepr {
213 prod_components :: [Type]
214 , prod_tycon :: TyCon
215 , prod_data_con :: DataCon
216 , prod_arr_tycon :: TyCon
217 , prod_arr_data_con :: DataCon
221 sum_components :: [Repr]
223 , sum_arr_tycon :: TyCon
224 , sum_arr_data_con :: DataCon
227 mkProduct :: [Type] -> VM Repr
230 tycon <- builtin (prodTyCon arity)
231 let [data_con] = tyConDataCons tycon
233 (arr_tycon, _) <- parrayReprTyCon $ mkTyConApp tycon tys
234 let [arr_data_con] = tyConDataCons arr_tycon
237 prod_components = tys
239 , prod_data_con = data_con
240 , prod_arr_tycon = arr_tycon
241 , prod_arr_data_con = arr_data_con
246 mkSum :: [Repr] -> VM Repr
247 mkSum [repr] = return repr
250 tycon <- builtin (sumTyCon arity)
251 (arr_tycon, _) <- parrayReprTyCon
255 let [arr_data_con] = tyConDataCons arr_tycon
258 sum_components = reprs
260 , sum_arr_tycon = arr_tycon
261 , sum_arr_data_con = arr_data_con
266 reprType :: Repr -> Type
267 reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
268 = mkTyConApp tycon tys
269 reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
270 = mkTyConApp tycon (map reprType reprs)
272 arrReprType :: Repr -> VM Type
273 arrReprType = mkPArrayType . reprType
275 arrShapeTys :: Repr -> VM [Type]
276 arrShapeTys (SumRepr {})
278 int_arr <- builtin parrayIntPrimTyCon
279 return [intPrimTy, mkTyConApp int_arr [], mkTyConApp int_arr []]
280 arrShapeTys repr = return [intPrimTy]
282 arrShapeVars :: Repr -> VM [Var]
283 arrShapeVars repr = mapM (newLocalVar FSLIT("sh")) =<< arrShapeTys repr
285 replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
286 replicateShape (ProdRepr {}) len _ = return [len]
287 replicateShape (SumRepr {}) len tag
289 rep <- builtin replicatePAIntPrimVar
290 up <- builtin upToPAIntPrimVar
291 return [len, Var rep `mkApps` [len, tag], Var up `App` len]
293 arrReprElemTys :: Repr -> [[Type]]
294 arrReprElemTys (SumRepr { sum_components = prods })
295 = map arrProdElemTys prods
296 arrReprElemTys prod@(ProdRepr {})
297 = [arrProdElemTys prod]
299 arrProdElemTys (ProdRepr { prod_components = [] })
301 arrProdElemTys (ProdRepr { prod_components = tys })
304 arrReprTys :: Repr -> VM [[Type]]
305 arrReprTys = mapM (mapM mkPArrayType) . arrReprElemTys
307 arrReprVars :: Repr -> VM [[Var]]
309 = mapM (mapM (newLocalVar FSLIT("rs"))) =<< arrReprTys repr
311 mkRepr :: TyCon -> VM Repr
314 =<< mapM mkProduct (map dataConRepArgTys $ tyConDataCons vect_tc)
316 buildPReprType :: TyCon -> VM Type
317 buildPReprType = liftM reprType . mkRepr
319 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
320 buildToPRepr repr vect_tc prepr_tc _
322 arg <- newLocalVar FSLIT("x") arg_ty
323 result <- to_repr repr (Var arg)
326 . wrapFamInstBody prepr_tc var_tys
329 var_tys = mkTyVarTys $ tyConTyVars vect_tc
330 arg_ty = mkTyConApp vect_tc var_tys
331 res_ty = reprType repr
333 cons = tyConDataCons vect_tc
336 to_repr (SumRepr { sum_components = prods
337 , sum_tycon = tycon })
340 (vars, bodies) <- mapAndUnzipM prod_alt prods
341 return . Case expr (mkWildId (exprType expr)) res_ty
342 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
344 mk_alt con vars sum_con body
345 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
347 ty_args = map (Type . reprType) prods
351 (vars, body) <- prod_alt prod
352 return $ Case expr (mkWildId (exprType expr)) res_ty
353 [(DataAlt con, vars, body)]
355 prod_alt (ProdRepr { prod_components = tys
356 , prod_data_con = data_con })
358 vars <- mapM (newLocalVar FSLIT("r")) tys
359 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
361 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
362 buildFromPRepr repr vect_tc prepr_tc _
364 arg_ty <- mkPReprType res_ty
365 arg <- newLocalVar FSLIT("x") arg_ty
369 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
371 var_tys = mkTyVarTys $ tyConTyVars vect_tc
372 res_ty = mkTyConApp vect_tc var_tys
374 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
377 from_repr repr@(SumRepr { sum_components = prods
378 , sum_tycon = tycon })
381 vars <- mapM (newLocalVar FSLIT("x")) (map reprType prods)
382 bodies <- sequence . zipWith3 from_prod prods cons
384 return . Case expr (mkWildId (reprType repr)) res_ty
385 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
387 sum_alt data_con var body = (DataAlt data_con, [var], body)
389 from_repr repr expr = from_prod repr con expr
391 from_prod prod@(ProdRepr { prod_components = tys
392 , prod_data_con = data_con })
396 vars <- mapM (newLocalVar FSLIT("y")) tys
397 return $ Case expr (mkWildId (reprType prod)) res_ty
398 [(DataAlt data_con, vars, con `mkVarApps` vars)]
400 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
401 buildToArrPRepr repr vect_tc prepr_tc arr_tc
403 arg_ty <- mkPArrayType el_ty
404 arg <- newLocalVar FSLIT("xs") arg_ty
406 res_ty <- mkPArrayType (reprType repr)
408 shape_vars <- arrShapeVars repr
409 repr_vars <- arrReprVars repr
411 parray_co <- mkBuiltinCo parrayTyCon
413 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
414 co = mkAppCoercion parray_co
416 $ mkTyConApp repr_co var_tys
418 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
420 result <- to_repr shape_vars repr_vars repr
424 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
425 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
427 var_tys = mkTyVarTys $ tyConTyVars vect_tc
428 el_ty = mkTyConApp vect_tc var_tys
430 [arr_dc] = tyConDataCons arr_tc
432 to_repr shape_vars@(len_var : _)
434 (SumRepr { sum_components = prods
435 , sum_arr_tycon = tycon
436 , sum_arr_data_con = data_con })
438 exprs <- zipWithM (to_prod len_var) repr_vars prods
440 return . wrapFamInstBody tycon tys
442 $ map Type tys ++ map Var shape_vars ++ exprs
444 tys = map reprType prods
446 to_repr [len_var] [repr_vars] prod = to_prod len_var repr_vars prod
450 (ProdRepr { prod_components = tys
451 , prod_arr_tycon = tycon
452 , prod_arr_data_con = data_con })
453 = return . wrapFamInstBody tycon tys
455 $ map Type tys ++ map Var (len_var : repr_vars)
457 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
458 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
460 arg_ty <- mkPArrayType =<< mkPReprType el_ty
461 arg <- newLocalVar FSLIT("xs") arg_ty
463 res_ty <- mkPArrayType el_ty
465 shape_vars <- arrShapeVars repr
466 repr_vars <- arrReprVars repr
468 parray_co <- mkBuiltinCo parrayTyCon
470 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
471 co = mkAppCoercion parray_co
472 $ mkTyConApp repr_co var_tys
474 scrut = mkCoerce co (Var arg)
476 result = wrapFamInstBody arr_tc var_tys
478 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
481 (from_repr repr scrut shape_vars repr_vars res_ty result)
483 var_tys = mkTyVarTys $ tyConTyVars vect_tc
484 el_ty = mkTyConApp vect_tc var_tys
486 [arr_dc] = tyConDataCons arr_tc
488 from_repr (SumRepr { sum_components = prods
489 , sum_arr_tycon = tycon
490 , sum_arr_data_con = data_con })
497 vars <- mapM (newLocalVar FSLIT("xs")) =<< mapM arrReprType prods
498 result <- go prods repr_vars vars body
500 let scrut = unwrapFamInstScrut tycon ty_args expr
501 return . Case scrut (mkWildId scrut_ty) res_ty
502 $ [(DataAlt data_con, shape_vars ++ vars, result)]
504 ty_args = map reprType prods
505 scrut_ty = mkTyConApp tycon ty_args
507 go [] [] [] body = return body
508 go (prod : prods) (repr_vars : rss) (var : vars) body
510 shape_vars <- mapM (newLocalVar FSLIT("s")) =<< arrShapeTys prod
512 from_prod prod (Var var) shape_vars repr_vars res_ty
513 =<< go prods rss vars body
515 from_repr repr expr shape_vars [repr_vars] res_ty body
516 = from_prod repr expr shape_vars repr_vars res_ty body
518 from_prod prod@(ProdRepr { prod_components = tys
519 , prod_arr_tycon = tycon
520 , prod_arr_data_con = data_con })
527 let scrut = unwrapFamInstScrut tycon tys expr
528 scrut_ty = mkTyConApp tycon tys
529 ty <- arrReprType prod
531 return $ Case scrut (mkWildId scrut_ty) res_ty
532 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
534 buildPRDictRepr :: Repr -> VM CoreExpr
535 buildPRDictRepr (ProdRepr {
536 prod_components = tys
541 dfun <- prDFunOfTyCon tycon
542 return $ dfun `mkTyApps` tys `mkApps` prs
544 buildPRDictRepr (SumRepr {
545 sum_components = prods
546 , sum_tycon = tycon })
548 prs <- mapM buildPRDictRepr prods
549 dfun <- prDFunOfTyCon tycon
550 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
552 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
553 buildPRDict repr vect_tc prepr_tc _
555 dict <- buildPRDictRepr repr
557 pr_co <- mkBuiltinCo prTyCon
558 let co = mkAppCoercion pr_co
560 $ mkTyConApp arg_co var_tys
562 return $ mkCoerce co dict
564 var_tys = mkTyVarTys $ tyConTyVars vect_tc
566 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
568 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
569 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
571 name' <- cloneName mkPArrayTyConOcc orig_name
572 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
573 parray <- builtin parrayTyCon
575 liftDs $ buildAlgTyCon name'
577 [] -- no stupid theta
579 rec_flag -- FIXME: is this ok?
580 False -- FIXME: no generics
581 False -- not GADT syntax
582 (Just $ mk_fam_inst parray vect_tc)
584 orig_name = tyConName orig_tc
585 tyvars = tyConTyVars vect_tc
586 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
589 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
590 buildPArrayTyConRhs orig_name vect_tc repr_tc
592 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
593 return $ DataTyCon { data_cons = [data_con], is_enum = False }
595 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
596 buildPArrayDataCon orig_name vect_tc repr_tc
598 dc_name <- cloneName mkPArrayDataConOcc orig_name
599 repr <- mkRepr vect_tc
601 shape_tys <- arrShapeTys repr
602 repr_tys <- arrReprTys repr
604 let tys = shape_tys ++ concat repr_tys
606 liftDs $ buildDataCon dc_name
608 (map (const NotMarkedStrict) tys)
609 [] -- no field labels
610 (tyConTyVars vect_tc)
611 [] -- no existentials
617 mkPADFun :: TyCon -> VM Var
619 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
621 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
622 -> VM [(Var, CoreExpr)]
623 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
625 repr <- mkRepr vect_tc
626 vectDataConWorkers repr orig_tc vect_tc arr_tc
627 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
629 return $ (dfun, dict) : binds
631 orig_dcs = tyConDataCons orig_tc
632 vect_dcs = tyConDataCons vect_tc
633 [arr_dc] = tyConDataCons arr_tc
635 repr_tys = map dataConRepArgTys vect_dcs
637 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
639 vectDataConWorkers repr orig_tc vect_tc arr_tc
642 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
643 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
646 (tail $ tails arr_tys)
647 mapM_ (uncurry hoistBinding) bs
649 tyvars = tyConTyVars vect_tc
650 var_tys = mkTyVarTys tyvars
651 ty_args = map Type var_tys
653 res_ty = mkTyConApp vect_tc var_tys
655 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
656 arr_tys = arrReprElemTys repr
658 [arr_dc] = tyConDataCons arr_tc
660 mk_data_con con tys pre post
661 = liftM2 (,) (vect_data_con con)
662 (lift_data_con tys (concat pre)
666 vect_data_con con = return $ mkConApp con ty_args
667 lift_data_con tys pre_tys post_tys tag
669 len <- builtin liftingContext
670 args <- mapM (newLocalVar FSLIT("xs"))
671 =<< mapM mkPArrayType tys
673 shape <- replicateShape repr (Var len) tag
674 repr <- mk_arr_repr (Var len) (map Var args)
676 pre <- mapM emptyPA pre_tys
677 post <- mapM emptyPA post_tys
679 return . mkLams (len : args)
680 . wrapFamInstBody arr_tc var_tys
682 $ ty_args ++ shape ++ pre ++ repr ++ post
686 units <- replicatePA len (Var unitDataConId)
689 mk_arr_repr len arrs = return arrs
691 def_worker data_con arg_tys mk_body
695 . polyAbstract tyvars $ \abstract ->
696 liftM (abstract . vectorised)
697 $ buildClosures tyvars [] arg_tys res_ty mk_body
699 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
700 defGlobalVar orig_worker vect_worker
701 return (vect_worker, body)
703 orig_worker = dataConWorkId data_con
705 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
706 buildPADict repr vect_tc prepr_tc arr_tc dfun
707 = polyAbstract tvs $ \abstract ->
709 meth_binds <- mapM (mk_method repr) paMethods
710 let meth_exprs = map (Var . fst) meth_binds
712 pa_dc <- builtin paDataCon
713 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
714 body = Let (Rec meth_binds) dict
715 return . mkInlineMe $ abstract body
717 tvs = tyConTyVars arr_tc
718 arg_tys = mkTyVarTys tvs
720 mk_method repr (name, build)
723 body <- build repr vect_tc prepr_tc arr_tc
724 var <- newLocalVar name (exprType body)
725 return (var, mkInlineMe body)
727 paMethods = [(FSLIT("toPRepr"), buildToPRepr),
728 (FSLIT("fromPRepr"), buildFromPRepr),
729 (FSLIT("toArrPRepr"), buildToArrPRepr),
730 (FSLIT("fromArrPRepr"), buildFromArrPRepr),
731 (FSLIT("dictPRepr"), buildPRDict)]
733 -- | Split the given tycons into two sets depending on whether they have to be
734 -- converted (first list) or not (second list). The first argument contains
735 -- information about the conversion status of external tycons:
737 -- * tycons which have converted versions are mapped to True
738 -- * tycons which are not changed by vectorisation are mapped to False
739 -- * tycons which can't be converted are not elements of the map
741 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
742 classifyTyCons = classify [] []
744 classify conv keep cs [] = (conv, keep)
745 classify conv keep cs ((tcs, ds) : rs)
746 | can_convert && must_convert
747 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
749 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
751 = classify conv keep cs rs
753 refs = ds `delListFromUniqSet` tcs
755 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
756 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
758 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
760 -- | Compute mutually recursive groups of tycons in topological order
762 tyConGroups :: [TyCon] -> [TyConGroup]
763 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
765 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
766 , let ds = tyConsOfTyCon tc]
768 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
769 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
771 (tcs, dss) = unzip els
773 tyConsOfTyCon :: TyCon -> UniqSet TyCon
775 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
777 tyConsOfType :: Type -> UniqSet TyCon
779 | Just ty' <- coreView ty = tyConsOfType ty'
780 tyConsOfType (TyVarTy v) = emptyUniqSet
781 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
783 extend | isUnLiftedTyCon tc
784 || isTupleTyCon tc = id
786 | otherwise = (`addOneToUniqSet` tc)
788 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
789 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
790 `addOneToUniqSet` funTyCon
791 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
792 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
794 tyConsOfTypes :: [Type] -> UniqSet TyCon
795 tyConsOfTypes = unionManyUniqSets . map tyConsOfType