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 reprProducts :: Repr -> [Repr]
267 reprProducts (SumRepr { sum_components = rs }) = rs
268 reprProducts repr = [repr]
270 reprType :: Repr -> Type
271 reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
272 = mkTyConApp tycon tys
273 reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
274 = mkTyConApp tycon (map reprType reprs)
276 arrReprType :: Repr -> VM Type
277 arrReprType = mkPArrayType . reprType
279 reprTys :: Repr -> [[Type]]
280 reprTys (SumRepr { sum_components = prods }) = map prodTys prods
281 reprTys prod = [prodTys prod]
283 prodTys (ProdRepr { prod_components = tys }) = tys
285 reprVars :: Repr -> VM [[Var]]
286 reprVars = mapM (mapM (newLocalVar FSLIT("r"))) . reprTys
288 arrShapeTys :: Repr -> VM [Type]
289 arrShapeTys (SumRepr {})
291 int_arr <- builtin parrayIntPrimTyCon
292 return [intPrimTy, mkTyConApp int_arr [], mkTyConApp int_arr []]
293 arrShapeTys repr = return [intPrimTy]
295 arrShapeVars :: Repr -> VM [Var]
296 arrShapeVars repr = mapM (newLocalVar FSLIT("sh")) =<< arrShapeTys repr
298 replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
299 replicateShape (ProdRepr {}) len _ = return [len]
300 replicateShape (SumRepr {}) len tag
302 rep <- builtin replicatePAIntPrimVar
303 up <- builtin upToPAIntPrimVar
304 return [len, Var rep `mkApps` [len, tag], Var up `App` len]
306 arrReprElemTys :: Repr -> [[Type]]
307 arrReprElemTys (SumRepr { sum_components = prods })
308 = map arrProdElemTys prods
309 arrReprElemTys prod@(ProdRepr {})
310 = [arrProdElemTys prod]
312 arrProdElemTys (ProdRepr { prod_components = [] })
314 arrProdElemTys (ProdRepr { prod_components = tys })
317 arrReprTys :: Repr -> VM [[Type]]
318 arrReprTys = mapM (mapM mkPArrayType) . arrReprElemTys
320 arrReprVars :: Repr -> VM [[Var]]
322 = mapM (mapM (newLocalVar FSLIT("rs"))) =<< arrReprTys repr
324 mkRepr :: TyCon -> VM Repr
327 =<< mapM mkProduct (map dataConRepArgTys $ tyConDataCons vect_tc)
329 buildPReprType :: TyCon -> VM Type
330 buildPReprType = liftM reprType . mkRepr
332 buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
333 buildToPRepr repr vect_tc prepr_tc _
335 arg <- newLocalVar FSLIT("x") arg_ty
336 result <- to_repr repr (Var arg)
339 . wrapFamInstBody prepr_tc var_tys
342 var_tys = mkTyVarTys $ tyConTyVars vect_tc
343 arg_ty = mkTyConApp vect_tc var_tys
344 res_ty = reprType repr
346 cons = tyConDataCons vect_tc
349 to_repr (SumRepr { sum_components = prods
350 , sum_tycon = tycon })
353 (vars, bodies) <- mapAndUnzipM prod_alt prods
354 return . Case expr (mkWildId (exprType expr)) res_ty
355 $ zipWith4 mk_alt cons vars (tyConDataCons tycon) bodies
357 mk_alt con vars sum_con body
358 = (DataAlt con, vars, mkConApp sum_con (ty_args ++ [body]))
360 ty_args = map (Type . reprType) prods
364 (vars, body) <- prod_alt prod
365 return $ Case expr (mkWildId (exprType expr)) res_ty
366 [(DataAlt con, vars, body)]
368 prod_alt (ProdRepr { prod_components = tys
369 , prod_data_con = data_con })
371 vars <- mapM (newLocalVar FSLIT("r")) tys
372 return (vars, mkConApp data_con (map Type tys ++ map Var vars))
374 buildFromPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
375 buildFromPRepr repr vect_tc prepr_tc _
377 arg_ty <- mkPReprType res_ty
378 arg <- newLocalVar FSLIT("x") arg_ty
382 $ unwrapFamInstScrut prepr_tc var_tys (Var arg)
384 var_tys = mkTyVarTys $ tyConTyVars vect_tc
385 res_ty = mkTyConApp vect_tc var_tys
387 cons = map (`mkConApp` map Type var_tys) (tyConDataCons vect_tc)
390 from_repr repr@(SumRepr { sum_components = prods
391 , sum_tycon = tycon })
394 vars <- mapM (newLocalVar FSLIT("x")) (map reprType prods)
395 bodies <- sequence . zipWith3 from_prod prods cons
397 return . Case expr (mkWildId (reprType repr)) res_ty
398 $ zipWith3 sum_alt (tyConDataCons tycon) vars bodies
400 sum_alt data_con var body = (DataAlt data_con, [var], body)
402 from_repr repr expr = from_prod repr con expr
404 from_prod prod@(ProdRepr { prod_components = tys
405 , prod_data_con = data_con })
409 vars <- mapM (newLocalVar FSLIT("y")) tys
410 return $ Case expr (mkWildId (reprType prod)) res_ty
411 [(DataAlt data_con, vars, con `mkVarApps` vars)]
413 buildToArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
414 buildToArrPRepr repr vect_tc prepr_tc arr_tc
416 arg_ty <- mkPArrayType el_ty
417 arg <- newLocalVar FSLIT("xs") arg_ty
419 res_ty <- mkPArrayType (reprType repr)
421 shape_vars <- arrShapeVars repr
422 repr_vars <- arrReprVars repr
424 parray_co <- mkBuiltinCo parrayTyCon
426 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
427 co = mkAppCoercion parray_co
429 $ mkTyConApp repr_co var_tys
431 scrut = unwrapFamInstScrut arr_tc var_tys (Var arg)
433 result <- to_repr shape_vars repr_vars repr
437 $ Case scrut (mkWildId (mkTyConApp arr_tc var_tys)) res_ty
438 [(DataAlt arr_dc, shape_vars ++ concat repr_vars, result)]
440 var_tys = mkTyVarTys $ tyConTyVars vect_tc
441 el_ty = mkTyConApp vect_tc var_tys
443 [arr_dc] = tyConDataCons arr_tc
445 to_repr shape_vars@(len_var : _)
447 (SumRepr { sum_components = prods
448 , sum_arr_tycon = tycon
449 , sum_arr_data_con = data_con })
451 exprs <- zipWithM (to_prod len_var) repr_vars prods
453 return . wrapFamInstBody tycon tys
455 $ map Type tys ++ map Var shape_vars ++ exprs
457 tys = map reprType prods
459 to_repr [len_var] [repr_vars] prod = to_prod len_var repr_vars prod
463 (ProdRepr { prod_components = tys
464 , prod_arr_tycon = tycon
465 , prod_arr_data_con = data_con })
466 = return . wrapFamInstBody tycon tys
468 $ map Type tys ++ map Var (len_var : repr_vars)
470 buildFromArrPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
471 buildFromArrPRepr repr vect_tc prepr_tc arr_tc
473 arg_ty <- mkPArrayType =<< mkPReprType el_ty
474 arg <- newLocalVar FSLIT("xs") arg_ty
476 res_ty <- mkPArrayType el_ty
478 shape_vars <- arrShapeVars repr
479 repr_vars <- arrReprVars repr
481 parray_co <- mkBuiltinCo parrayTyCon
483 let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
484 co = mkAppCoercion parray_co
485 $ mkTyConApp repr_co var_tys
487 scrut = mkCoerce co (Var arg)
489 result = wrapFamInstBody arr_tc var_tys
491 $ map Type var_tys ++ map Var (shape_vars ++ concat repr_vars)
494 (from_repr repr scrut shape_vars repr_vars res_ty result)
496 var_tys = mkTyVarTys $ tyConTyVars vect_tc
497 el_ty = mkTyConApp vect_tc var_tys
499 [arr_dc] = tyConDataCons arr_tc
501 from_repr (SumRepr { sum_components = prods
502 , sum_arr_tycon = tycon
503 , sum_arr_data_con = data_con })
510 vars <- mapM (newLocalVar FSLIT("xs")) =<< mapM arrReprType prods
511 result <- go prods repr_vars vars body
513 let scrut = unwrapFamInstScrut tycon ty_args expr
514 return . Case scrut (mkWildId scrut_ty) res_ty
515 $ [(DataAlt data_con, shape_vars ++ vars, result)]
517 ty_args = map reprType prods
518 scrut_ty = mkTyConApp tycon ty_args
520 go [] [] [] body = return body
521 go (prod : prods) (repr_vars : rss) (var : vars) body
523 shape_vars <- mapM (newLocalVar FSLIT("s")) =<< arrShapeTys prod
525 from_prod prod (Var var) shape_vars repr_vars res_ty
526 =<< go prods rss vars body
528 from_repr repr expr shape_vars [repr_vars] res_ty body
529 = from_prod repr expr shape_vars repr_vars res_ty body
531 from_prod prod@(ProdRepr { prod_components = tys
532 , prod_arr_tycon = tycon
533 , prod_arr_data_con = data_con })
540 let scrut = unwrapFamInstScrut tycon tys expr
541 scrut_ty = mkTyConApp tycon tys
542 ty <- arrReprType prod
544 return $ Case scrut (mkWildId scrut_ty) res_ty
545 [(DataAlt data_con, shape_vars ++ repr_vars, body)]
547 buildPRDictRepr :: Repr -> VM CoreExpr
548 buildPRDictRepr (ProdRepr {
549 prod_components = tys
554 dfun <- prDFunOfTyCon tycon
555 return $ dfun `mkTyApps` tys `mkApps` prs
557 buildPRDictRepr (SumRepr {
558 sum_components = prods
559 , sum_tycon = tycon })
561 prs <- mapM buildPRDictRepr prods
562 dfun <- prDFunOfTyCon tycon
563 return $ dfun `mkTyApps` map reprType prods `mkApps` prs
565 buildPRDict :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
566 buildPRDict repr vect_tc prepr_tc _
568 dict <- buildPRDictRepr repr
570 pr_co <- mkBuiltinCo prTyCon
571 let co = mkAppCoercion pr_co
573 $ mkTyConApp arg_co var_tys
575 return $ mkCoerce co dict
577 var_tys = mkTyVarTys $ tyConTyVars vect_tc
579 Just arg_co = tyConFamilyCoercion_maybe prepr_tc
581 buildPArrayTyCon :: TyCon -> TyCon -> VM TyCon
582 buildPArrayTyCon orig_tc vect_tc = fixV $ \repr_tc ->
584 name' <- cloneName mkPArrayTyConOcc orig_name
585 rhs <- buildPArrayTyConRhs orig_name vect_tc repr_tc
586 parray <- builtin parrayTyCon
588 liftDs $ buildAlgTyCon name'
590 [] -- no stupid theta
592 rec_flag -- FIXME: is this ok?
593 False -- FIXME: no generics
594 False -- not GADT syntax
595 (Just $ mk_fam_inst parray vect_tc)
597 orig_name = tyConName orig_tc
598 tyvars = tyConTyVars vect_tc
599 rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
602 buildPArrayTyConRhs :: Name -> TyCon -> TyCon -> VM AlgTyConRhs
603 buildPArrayTyConRhs orig_name vect_tc repr_tc
605 data_con <- buildPArrayDataCon orig_name vect_tc repr_tc
606 return $ DataTyCon { data_cons = [data_con], is_enum = False }
608 buildPArrayDataCon :: Name -> TyCon -> TyCon -> VM DataCon
609 buildPArrayDataCon orig_name vect_tc repr_tc
611 dc_name <- cloneName mkPArrayDataConOcc orig_name
612 repr <- mkRepr vect_tc
614 shape_tys <- arrShapeTys repr
615 repr_tys <- arrReprTys repr
617 let tys = shape_tys ++ concat repr_tys
619 liftDs $ buildDataCon dc_name
621 (map (const NotMarkedStrict) tys)
622 [] -- no field labels
623 (tyConTyVars vect_tc)
624 [] -- no existentials
630 mkPADFun :: TyCon -> VM Var
632 = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
634 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var
635 -> VM [(Var, CoreExpr)]
636 buildTyConBindings orig_tc vect_tc prepr_tc arr_tc dfun
638 repr <- mkRepr vect_tc
639 vectDataConWorkers repr orig_tc vect_tc arr_tc
640 dict <- buildPADict repr vect_tc prepr_tc arr_tc dfun
642 return $ (dfun, dict) : binds
644 orig_dcs = tyConDataCons orig_tc
645 vect_dcs = tyConDataCons vect_tc
646 [arr_dc] = tyConDataCons arr_tc
648 repr_tys = map dataConRepArgTys vect_dcs
650 vectDataConWorkers :: Repr -> TyCon -> TyCon -> TyCon
652 vectDataConWorkers repr orig_tc vect_tc arr_tc
655 . zipWith3 def_worker (tyConDataCons orig_tc) rep_tys
656 $ zipWith4 mk_data_con (tyConDataCons vect_tc)
659 (tail $ tails arr_tys)
660 mapM_ (uncurry hoistBinding) bs
662 tyvars = tyConTyVars vect_tc
663 var_tys = mkTyVarTys tyvars
664 ty_args = map Type var_tys
666 res_ty = mkTyConApp vect_tc var_tys
668 rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
669 arr_tys = arrReprElemTys repr
671 [arr_dc] = tyConDataCons arr_tc
673 mk_data_con con tys pre post
674 = liftM2 (,) (vect_data_con con)
675 (lift_data_con tys (concat pre)
679 vect_data_con con = return $ mkConApp con ty_args
680 lift_data_con tys pre_tys post_tys tag
682 len <- builtin liftingContext
683 args <- mapM (newLocalVar FSLIT("xs"))
684 =<< mapM mkPArrayType tys
686 shape <- replicateShape repr (Var len) tag
687 repr <- mk_arr_repr (Var len) (map Var args)
689 pre <- mapM emptyPA pre_tys
690 post <- mapM emptyPA post_tys
692 return . mkLams (len : args)
693 . wrapFamInstBody arr_tc var_tys
695 $ ty_args ++ shape ++ pre ++ repr ++ post
699 units <- replicatePA len (Var unitDataConId)
702 mk_arr_repr len arrs = return arrs
704 def_worker data_con arg_tys mk_body
708 . polyAbstract tyvars $ \abstract ->
709 liftM (abstract . vectorised)
710 $ buildClosures tyvars [] arg_tys res_ty mk_body
712 vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
713 defGlobalVar orig_worker vect_worker
714 return (vect_worker, body)
716 orig_worker = dataConWorkId data_con
718 buildPADict :: Repr -> TyCon -> TyCon -> TyCon -> Var -> VM CoreExpr
719 buildPADict repr vect_tc prepr_tc arr_tc dfun
720 = polyAbstract tvs $ \abstract ->
722 meth_binds <- mapM (mk_method repr) paMethods
723 let meth_exprs = map (Var . fst) meth_binds
725 pa_dc <- builtin paDataCon
726 let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
727 body = Let (Rec meth_binds) dict
728 return . mkInlineMe $ abstract body
730 tvs = tyConTyVars arr_tc
731 arg_tys = mkTyVarTys tvs
733 mk_method repr (name, build)
736 body <- build repr vect_tc prepr_tc arr_tc
737 var <- newLocalVar name (exprType body)
738 return (var, mkInlineMe body)
740 paMethods = [(FSLIT("toPRepr"), buildToPRepr),
741 (FSLIT("fromPRepr"), buildFromPRepr),
742 (FSLIT("toArrPRepr"), buildToArrPRepr),
743 (FSLIT("fromArrPRepr"), buildFromArrPRepr),
744 (FSLIT("dictPRepr"), buildPRDict)]
746 -- | Split the given tycons into two sets depending on whether they have to be
747 -- converted (first list) or not (second list). The first argument contains
748 -- information about the conversion status of external tycons:
750 -- * tycons which have converted versions are mapped to True
751 -- * tycons which are not changed by vectorisation are mapped to False
752 -- * tycons which can't be converted are not elements of the map
754 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
755 classifyTyCons = classify [] []
757 classify conv keep cs [] = (conv, keep)
758 classify conv keep cs ((tcs, ds) : rs)
759 | can_convert && must_convert
760 = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
762 = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
764 = classify conv keep cs rs
766 refs = ds `delListFromUniqSet` tcs
768 can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
769 must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
771 convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
773 -- | Compute mutually recursive groups of tycons in topological order
775 tyConGroups :: [TyCon] -> [TyConGroup]
776 tyConGroups tcs = map mk_grp (stronglyConnComp edges)
778 edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
779 , let ds = tyConsOfTyCon tc]
781 mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
782 mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
784 (tcs, dss) = unzip els
786 tyConsOfTyCon :: TyCon -> UniqSet TyCon
788 = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
790 tyConsOfType :: Type -> UniqSet TyCon
792 | Just ty' <- coreView ty = tyConsOfType ty'
793 tyConsOfType (TyVarTy v) = emptyUniqSet
794 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
796 extend | isUnLiftedTyCon tc
797 || isTupleTyCon tc = id
799 | otherwise = (`addOneToUniqSet` tc)
801 tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
802 tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
803 `addOneToUniqSet` funTyCon
804 tyConsOfType (ForAllTy _ ty) = tyConsOfType ty
805 tyConsOfType other = pprPanic "ClosureConv.tyConsOfType" $ ppr other
807 tyConsOfTypes :: [Type] -> UniqSet TyCon
808 tyConsOfTypes = unionManyUniqSets . map tyConsOfType