PA and PR from dph are now type classes
[ghc-hetmet.git] / compiler / vectorise / VectType.hs
1 module VectType ( vectTyCon, vectAndLiftType, vectType, vectTypeEnv,
2                   -- arrSumArity, pdataCompTys, pdataCompVars,
3                   buildPADict,
4                   fromVect )
5 where
6
7 import VectMonad
8 import VectUtils
9 import VectCore
10
11 import HscTypes          ( TypeEnv, extendTypeEnvList, typeEnvTyCons )
12 import CoreSyn
13 import CoreUtils
14 import MkCore            ( mkWildCase )
15 import BuildTyCl
16 import DataCon
17 import TyCon
18 import Type
19 import TypeRep
20 import Coercion
21 import FamInstEnv        ( FamInst, mkLocalFamInst )
22 import OccName
23 import MkId
24 import BasicTypes        ( StrictnessMark(..), boolToRecFlag )
25 import Var               ( Var, TyVar )
26 import Name              ( Name, getOccName )
27 import NameEnv
28
29 import Unique
30 import UniqFM
31 import UniqSet
32 import Util
33 import Digraph           ( SCC(..), stronglyConnCompFromEdgedVertices )
34
35 import Outputable
36 import FastString
37
38 import MonadUtils     ( zipWith3M, foldrM, concatMapM )
39 import Control.Monad  ( liftM, liftM2, zipWithM, zipWithM_, mapAndUnzipM )
40 import Data.List      ( inits, tails, zipWith4, zipWith5, zipWith6 )
41
42 -- ----------------------------------------------------------------------------
43 -- Types
44
45 vectTyCon :: TyCon -> VM TyCon
46 vectTyCon tc
47   | isFunTyCon tc        = builtin closureTyCon
48   | isBoxedTupleTyCon tc = return tc
49   | isUnLiftedTyCon tc   = return tc
50   | otherwise            = maybeCantVectoriseM "Tycon not vectorised:" (ppr tc)
51                          $ lookupTyCon tc
52
53 vectAndLiftType :: Type -> VM (Type, Type)
54 vectAndLiftType ty | Just ty' <- coreView ty = vectAndLiftType ty'
55 vectAndLiftType ty
56   = do
57       mdicts   <- mapM paDictArgType tyvars
58       let dicts = [dict | Just dict <- mdicts]
59       vmono_ty <- vectType mono_ty
60       lmono_ty <- mkPDataType vmono_ty
61       return (abstractType tyvars dicts vmono_ty,
62               abstractType tyvars dicts lmono_ty)
63   where
64     (tyvars, mono_ty) = splitForAllTys ty
65
66
67 vectType :: Type -> VM Type
68 vectType ty | Just ty' <- coreView ty = vectType ty'
69 vectType (TyVarTy tv) = return $ TyVarTy tv
70 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
71 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
72 vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
73                                              (mapM vectAndBoxType [ty1,ty2])
74 vectType ty@(ForAllTy _ _)
75   = do
76       mdicts   <- mapM paDictArgType tyvars
77       mono_ty' <- vectType mono_ty
78       return $ abstractType tyvars [dict | Just dict <- mdicts] mono_ty'
79   where
80     (tyvars, mono_ty) = splitForAllTys ty
81
82 vectType ty = cantVectorise "Can't vectorise type" (ppr ty)
83
84 vectAndBoxType :: Type -> VM Type
85 vectAndBoxType ty = vectType ty >>= boxType
86
87 abstractType :: [TyVar] -> [Type] -> Type -> Type
88 abstractType tyvars dicts = mkForAllTys tyvars . mkFunTys dicts
89
90 -- ----------------------------------------------------------------------------
91 -- Boxing
92
93 boxType :: Type -> VM Type
94 boxType ty
95   | Just (tycon, []) <- splitTyConApp_maybe ty
96   , isUnLiftedTyCon tycon
97   = do
98       r <- lookupBoxedTyCon tycon
99       case r of
100         Just tycon' -> return $ mkTyConApp tycon' []
101         Nothing     -> return ty
102 boxType ty = return ty
103
104 -- ----------------------------------------------------------------------------
105 -- Type definitions
106
107 type TyConGroup = ([TyCon], UniqSet TyCon)
108
109 vectTypeEnv :: TypeEnv -> VM (TypeEnv, [FamInst], [(Var, CoreExpr)])
110 vectTypeEnv env
111   = do
112       cs <- readGEnv $ mk_map . global_tycons
113       let (conv_tcs, keep_tcs) = classifyTyCons cs groups
114           keep_dcs             = concatMap tyConDataCons keep_tcs
115       zipWithM_ defTyCon   keep_tcs keep_tcs
116       zipWithM_ defDataCon keep_dcs keep_dcs
117       new_tcs <- vectTyConDecls conv_tcs
118
119       let orig_tcs = keep_tcs ++ conv_tcs
120           vect_tcs = keep_tcs ++ new_tcs
121
122       dfuns <- mapM mkPADFun vect_tcs
123       defTyConPAs (zip vect_tcs dfuns)
124       reprs <- mapM tyConRepr vect_tcs
125       repr_tcs  <- zipWith3M buildPReprTyCon orig_tcs vect_tcs reprs
126       pdata_tcs <- zipWith3M buildPDataTyCon orig_tcs vect_tcs reprs
127       binds    <- sequence (zipWith6 buildTyConBindings orig_tcs
128                                                         vect_tcs
129                                                         repr_tcs
130                                                         pdata_tcs
131                                                         dfuns
132                                                         reprs)
133
134       let all_new_tcs = new_tcs ++ repr_tcs ++ pdata_tcs
135
136       let new_env = extendTypeEnvList env
137                        (map ATyCon all_new_tcs
138                         ++ [ADataCon dc | tc <- all_new_tcs
139                                         , dc <- tyConDataCons tc])
140
141       return (new_env, map mkLocalFamInst (repr_tcs ++ pdata_tcs), concat binds)
142   where
143     tycons = typeEnvTyCons env
144     groups = tyConGroups tycons
145
146     mk_map env = listToUFM_Directly [(u, getUnique n /= u) | (u,n) <- nameEnvUniqueElts env]
147
148
149 vectTyConDecls :: [TyCon] -> VM [TyCon]
150 vectTyConDecls tcs = fixV $ \tcs' ->
151   do
152     mapM_ (uncurry defTyCon) (zipLazy tcs tcs')
153     mapM vectTyConDecl tcs
154
155 vectTyConDecl :: TyCon -> VM TyCon
156 vectTyConDecl tc
157   = do
158       name' <- cloneName mkVectTyConOcc name
159       rhs'  <- vectAlgTyConRhs tc (algTyConRhs tc)
160
161       liftDs $ buildAlgTyCon name'
162                              tyvars
163                              []           -- no stupid theta
164                              rhs'
165                              rec_flag     -- FIXME: is this ok?
166                              False        -- FIXME: no generics
167                              False        -- not GADT syntax
168                              Nothing      -- not a family instance
169   where
170     name   = tyConName tc
171     tyvars = tyConTyVars tc
172     rec_flag = boolToRecFlag (isRecursiveTyCon tc)
173
174 vectAlgTyConRhs :: TyCon -> AlgTyConRhs -> VM AlgTyConRhs
175 vectAlgTyConRhs _ (DataTyCon { data_cons = data_cons
176                              , is_enum   = is_enum
177                              })
178   = do
179       data_cons' <- mapM vectDataCon data_cons
180       zipWithM_ defDataCon data_cons data_cons'
181       return $ DataTyCon { data_cons = data_cons'
182                          , is_enum   = is_enum
183                          }
184 vectAlgTyConRhs tc _ = cantVectorise "Can't vectorise type definition:" (ppr tc)
185
186 vectDataCon :: DataCon -> VM DataCon
187 vectDataCon dc
188   | not . null $ dataConExTyVars dc
189         = cantVectorise "Can't vectorise constructor (existentials):" (ppr dc)
190   | not . null $ dataConEqSpec   dc
191         = cantVectorise "Can't vectorise constructor (eq spec):" (ppr dc)
192   | otherwise
193   = do
194       name'    <- cloneName mkVectDataConOcc name
195       tycon'   <- vectTyCon tycon
196       arg_tys  <- mapM vectType rep_arg_tys
197
198       liftDs $ buildDataCon name'
199                             False           -- not infix
200                             (map (const NotMarkedStrict) arg_tys)
201                             []              -- no labelled fields
202                             univ_tvs
203                             []              -- no existential tvs for now
204                             []              -- no eq spec for now
205                             []              -- no context
206                             arg_tys 
207                             (mkFamilyTyConApp tycon' (mkTyVarTys univ_tvs))
208                             tycon'
209   where
210     name        = dataConName dc
211     univ_tvs    = dataConUnivTyVars dc
212     rep_arg_tys = dataConRepArgTys dc
213     tycon       = dataConTyCon dc
214
215 mk_fam_inst :: TyCon -> TyCon -> (TyCon, [Type])
216 mk_fam_inst fam_tc arg_tc
217   = (fam_tc, [mkTyConApp arg_tc . mkTyVarTys $ tyConTyVars arg_tc])
218
219
220 buildPReprTyCon :: TyCon -> TyCon -> SumRepr -> VM TyCon
221 buildPReprTyCon orig_tc vect_tc repr
222   = do
223       name     <- cloneName mkPReprTyConOcc (tyConName orig_tc)
224       -- rhs_ty   <- buildPReprType vect_tc
225       rhs_ty   <- sumReprType repr
226       prepr_tc <- builtin preprTyCon
227       liftDs $ buildSynTyCon name
228                              tyvars
229                              (SynonymTyCon rhs_ty)
230                              (typeKind rhs_ty)
231                              (Just $ mk_fam_inst prepr_tc vect_tc)
232   where
233     tyvars = tyConTyVars vect_tc
234
235 data CompRepr = Keep Type
236                      CoreExpr     -- PR dictionary for the type
237               | Wrap Type
238
239 data ProdRepr = EmptyProd
240               | UnaryProd CompRepr
241               | Prod { repr_tup_tc   :: TyCon  -- representation tuple tycon
242                      , repr_ptup_tc  :: TyCon  -- PData representation tycon
243                      , repr_comp_tys :: [Type] -- representation types of
244                      , repr_comps    :: [CompRepr]          -- components
245                      }
246 data ConRepr  = ConRepr DataCon ProdRepr
247
248 data SumRepr  = EmptySum
249               | UnarySum ConRepr
250               | Sum  { repr_sum_tc   :: TyCon  -- representation sum tycon
251                      , repr_psum_tc  :: TyCon  -- PData representation tycon
252                      , repr_sel_ty   :: Type   -- type of selector
253                      , repr_con_tys :: [Type]  -- representation types of
254                      , repr_cons     :: [ConRepr]           -- components
255                      }
256
257 tyConRepr :: TyCon -> VM SumRepr
258 tyConRepr tc = sum_repr (tyConDataCons tc)
259   where
260     sum_repr []    = return EmptySum
261     sum_repr [con] = liftM UnarySum (con_repr con)
262     sum_repr cons  = do
263                        rs     <- mapM con_repr cons
264                        sum_tc <- builtin (sumTyCon arity)
265                        tys    <- mapM conReprType rs
266                        (psum_tc, _) <- pdataReprTyCon (mkTyConApp sum_tc tys)
267                        sel_ty <- builtin (selTy arity)
268                        return $ Sum { repr_sum_tc  = sum_tc
269                                     , repr_psum_tc = psum_tc
270                                     , repr_sel_ty  = sel_ty
271                                     , repr_con_tys = tys
272                                     , repr_cons    = rs
273                                     }
274       where
275         arity = length cons
276
277     con_repr con = liftM (ConRepr con) (prod_repr (dataConRepArgTys con))
278
279     prod_repr []   = return EmptyProd
280     prod_repr [ty] = liftM UnaryProd (comp_repr ty)
281     prod_repr tys  = do
282                        rs <- mapM comp_repr tys
283                        tup_tc <- builtin (prodTyCon arity)
284                        tys'    <- mapM compReprType rs
285                        (ptup_tc, _) <- pdataReprTyCon (mkTyConApp tup_tc tys')
286                        return $ Prod { repr_tup_tc   = tup_tc
287                                      , repr_ptup_tc  = ptup_tc
288                                      , repr_comp_tys = tys'
289                                      , repr_comps    = rs
290                                      }
291       where
292         arity = length tys
293     
294     comp_repr ty = liftM (Keep ty) (prDictOfType ty)
295                    `orElseV` return (Wrap ty)
296
297 sumReprType :: SumRepr -> VM Type
298 sumReprType EmptySum = voidType
299 sumReprType (UnarySum r) = conReprType r
300 sumReprType (Sum { repr_sum_tc  = sum_tc, repr_con_tys = tys })
301   = return $ mkTyConApp sum_tc tys
302
303 conReprType :: ConRepr -> VM Type
304 conReprType (ConRepr _ r) = prodReprType r
305
306 prodReprType :: ProdRepr -> VM Type
307 prodReprType EmptyProd = voidType
308 prodReprType (UnaryProd r) = compReprType r
309 prodReprType (Prod { repr_tup_tc = tup_tc, repr_comp_tys = tys })
310   = return $ mkTyConApp tup_tc tys
311
312 compReprType :: CompRepr -> VM Type
313 compReprType (Keep ty _) = return ty
314 compReprType (Wrap ty) = do
315                              wrap_tc <- builtin wrapTyCon
316                              return $ mkTyConApp wrap_tc [ty]
317
318 compOrigType :: CompRepr -> Type
319 compOrigType (Keep ty _) = ty
320 compOrigType (Wrap ty) = ty
321
322 buildToPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
323 buildToPRepr vect_tc repr_tc _ repr
324   = do
325       let arg_ty = mkTyConApp vect_tc ty_args
326       res_ty <- mkPReprType arg_ty
327       arg    <- newLocalVar (fsLit "x") arg_ty
328       result <- to_sum (Var arg) arg_ty res_ty repr
329       return $ Lam arg result
330   where
331     ty_args = mkTyVarTys (tyConTyVars vect_tc)
332
333     wrap_repr_inst = wrapFamInstBody repr_tc ty_args
334
335     to_sum arg arg_ty res_ty EmptySum
336       = do
337           void <- builtin voidVar
338           return $ wrap_repr_inst $ Var void
339
340     to_sum arg arg_ty res_ty (UnarySum r)
341       = do
342           (pat, vars, body) <- con_alt r
343           return $ mkWildCase arg arg_ty res_ty
344                    [(pat, vars, wrap_repr_inst body)]
345
346     to_sum arg arg_ty res_ty (Sum { repr_sum_tc  = sum_tc
347                                   , repr_con_tys = tys
348                                   , repr_cons    =  cons })
349       = do
350           alts <- mapM con_alt cons
351           let ty_args = map Type tys
352               alts' = [(pat, vars, wrap_repr_inst
353                                    $ mkConApp sum_con (map Type tys ++ [body]))
354                         | ((pat, vars, body), sum_con)
355                             <- zip alts (tyConDataCons sum_tc)]
356           return $ mkWildCase arg arg_ty res_ty alts'
357
358     con_alt (ConRepr con r)
359       = do
360           (vars, body) <- to_prod r
361           return (DataAlt con, vars, body)
362
363     to_prod EmptyProd
364       = do
365           void <- builtin voidVar
366           return ([], Var void)
367
368     to_prod (UnaryProd comp)
369       = do
370           var  <- newLocalVar (fsLit "x") (compOrigType comp)
371           body <- to_comp (Var var) comp
372           return ([var], body)
373
374     to_prod(Prod { repr_tup_tc   = tup_tc
375                  , repr_comp_tys = tys
376                  , repr_comps    = comps })
377       = do
378           vars  <- newLocalVars (fsLit "x") (map compOrigType comps)
379           exprs <- zipWithM to_comp (map Var vars) comps
380           return (vars, mkConApp tup_con (map Type tys ++ exprs))
381       where
382         [tup_con] = tyConDataCons tup_tc
383
384     to_comp expr (Keep _ _) = return expr
385     to_comp expr (Wrap ty)  = do
386                                 wrap_tc <- builtin wrapTyCon
387                                 return $ wrapNewTypeBody wrap_tc [ty] expr
388
389
390 buildFromPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
391 buildFromPRepr vect_tc repr_tc _ repr
392   = do
393       arg_ty <- mkPReprType res_ty
394       arg <- newLocalVar (fsLit "x") arg_ty
395
396       result <- from_sum (unwrapFamInstScrut repr_tc ty_args (Var arg))
397                          repr
398       return $ Lam arg result
399   where
400     ty_args = mkTyVarTys (tyConTyVars vect_tc)
401     res_ty  = mkTyConApp vect_tc ty_args
402
403     from_sum expr EmptySum
404       = do
405           dummy <- builtin fromVoidVar
406           return $ Var dummy `App` Type res_ty
407
408     from_sum expr (UnarySum r) = from_con expr r
409     from_sum expr (Sum { repr_sum_tc  = sum_tc
410                        , repr_con_tys = tys
411                        , repr_cons    = cons })
412       = do
413           vars  <- newLocalVars (fsLit "x") tys
414           es    <- zipWithM from_con (map Var vars) cons
415           return $ mkWildCase expr (exprType expr) res_ty
416                    [(DataAlt con, [var], e)
417                       | (con, var, e) <- zip3 (tyConDataCons sum_tc) vars es]
418
419     from_con expr (ConRepr con r)
420       = from_prod expr (mkConApp con $ map Type ty_args) r
421
422     from_prod expr con EmptyProd = return con
423     from_prod expr con (UnaryProd r)
424       = do
425           e <- from_comp expr r
426           return $ con `App` e
427      
428     from_prod expr con (Prod { repr_tup_tc   = tup_tc
429                              , repr_comp_tys = tys
430                              , repr_comps    = comps
431                              })
432       = do
433           vars <- newLocalVars (fsLit "y") tys
434           es   <- zipWithM from_comp (map Var vars) comps
435           return $ mkWildCase expr (exprType expr) res_ty
436                    [(DataAlt tup_con, vars, con `mkApps` es)]
437       where
438         [tup_con] = tyConDataCons tup_tc  
439
440     from_comp expr (Keep _ _) = return expr
441     from_comp expr (Wrap ty)
442       = do
443           wrap <- builtin wrapTyCon
444           return $ unwrapNewTypeBody wrap [ty] expr
445
446
447 buildToArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
448 buildToArrPRepr vect_tc prepr_tc pdata_tc r
449   = do
450       arg_ty <- mkPDataType el_ty
451       res_ty <- mkPDataType =<< mkPReprType el_ty
452       arg    <- newLocalVar (fsLit "xs") arg_ty
453
454       pdata_co <- mkBuiltinCo pdataTyCon
455       let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
456           co           = mkAppCoercion pdata_co
457                        . mkSymCoercion
458                        $ mkTyConApp repr_co ty_args
459
460           scrut   = unwrapFamInstScrut pdata_tc ty_args (Var arg)
461
462       (vars, result) <- to_sum r
463
464       return . Lam arg
465              $ mkWildCase scrut (mkTyConApp pdata_tc ty_args) res_ty
466                [(DataAlt pdata_dc, vars, mkCoerce co result)]
467   where
468     ty_args = mkTyVarTys $ tyConTyVars vect_tc
469     el_ty   = mkTyConApp vect_tc ty_args
470
471     [pdata_dc] = tyConDataCons pdata_tc
472
473
474     to_sum EmptySum = do
475                         pvoid <- builtin pvoidVar
476                         return ([], Var pvoid)
477     to_sum (UnarySum r) = to_con r
478     to_sum (Sum { repr_psum_tc = psum_tc
479                 , repr_sel_ty  = sel_ty
480                 , repr_con_tys = tys
481                 , repr_cons    = cons
482                 })
483       = do
484           (vars, exprs) <- mapAndUnzipM to_con cons
485           sel <- newLocalVar (fsLit "sel") sel_ty
486           return (sel : concat vars, mk_result (Var sel) exprs)
487       where
488         [psum_con] = tyConDataCons psum_tc
489         mk_result sel exprs = wrapFamInstBody psum_tc tys
490                             $ mkConApp psum_con
491                             $ map Type tys ++ (sel : exprs)
492
493     to_con (ConRepr _ r) = to_prod r
494
495     to_prod EmptyProd = do
496                           pvoid <- builtin pvoidVar
497                           return ([], Var pvoid)
498     to_prod (UnaryProd r)
499       = do
500           pty  <- mkPDataType (compOrigType r)
501           var  <- newLocalVar (fsLit "x") pty
502           expr <- to_comp (Var var) r
503           return ([var], expr)
504
505     to_prod (Prod { repr_ptup_tc  = ptup_tc
506                   , repr_comp_tys = tys
507                   , repr_comps    = comps })
508       = do
509           ptys <- mapM (mkPDataType . compOrigType) comps
510           vars <- newLocalVars (fsLit "x") ptys
511           es   <- zipWithM to_comp (map Var vars) comps
512           return (vars, mk_result es)
513       where
514         [ptup_con] = tyConDataCons ptup_tc
515         mk_result exprs = wrapFamInstBody ptup_tc tys
516                         $ mkConApp ptup_con
517                         $ map Type tys ++ exprs
518
519     to_comp expr (Keep _ _) = return expr
520
521     -- FIXME: this is bound to be wrong!
522     to_comp expr (Wrap ty)
523       = do
524           wrap_tc  <- builtin wrapTyCon
525           (pwrap_tc, _) <- pdataReprTyCon (mkTyConApp wrap_tc [ty])
526           return $ wrapNewTypeBody pwrap_tc [ty] expr
527
528
529 buildFromArrPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
530 buildFromArrPRepr vect_tc prepr_tc pdata_tc r
531   = do
532       arg_ty <- mkPDataType =<< mkPReprType el_ty
533       res_ty <- mkPDataType el_ty
534       arg    <- newLocalVar (fsLit "xs") arg_ty
535
536       pdata_co <- mkBuiltinCo pdataTyCon
537       let Just repr_co = tyConFamilyCoercion_maybe prepr_tc
538           co           = mkAppCoercion pdata_co
539                        $ mkTyConApp repr_co var_tys
540
541           scrut  = mkCoerce co (Var arg)
542
543           mk_result args = wrapFamInstBody pdata_tc var_tys
544                          $ mkConApp pdata_con
545                          $ map Type var_tys ++ args
546
547       (expr, _) <- fixV $ \ ~(_, args) ->
548                      from_sum res_ty (mk_result args) scrut r
549
550       return $ Lam arg expr
551     
552       -- (args, mk) <- from_sum res_ty scrut r
553       
554       -- let result = wrapFamInstBody pdata_tc var_tys
555       --           . mkConApp pdata_dc
556       --           $ map Type var_tys ++ args
557
558       -- return $ Lam arg (mk result)
559   where
560     var_tys = mkTyVarTys $ tyConTyVars vect_tc
561     el_ty   = mkTyConApp vect_tc var_tys
562
563     [pdata_con] = tyConDataCons pdata_tc
564
565     from_sum res_ty res expr EmptySum = return (res, [])
566     from_sum res_ty res expr (UnarySum r) = from_con res_ty res expr r
567     from_sum res_ty res expr (Sum { repr_psum_tc = psum_tc
568                                   , repr_sel_ty  = sel_ty
569                                   , repr_con_tys = tys
570                                   , repr_cons    = cons })
571       = do
572           sel  <- newLocalVar (fsLit "sel") sel_ty
573           ptys <- mapM mkPDataType tys
574           vars <- newLocalVars (fsLit "xs") ptys
575           (res', args) <- fold from_con res_ty res (map Var vars) cons
576           let scrut = unwrapFamInstScrut psum_tc tys expr
577               body  = mkWildCase scrut (exprType scrut) res_ty
578                       [(DataAlt psum_con, sel : vars, res')]
579           return (body, Var sel : args)
580       where
581         [psum_con] = tyConDataCons psum_tc
582
583
584     from_con res_ty res expr (ConRepr _ r) = from_prod res_ty res expr r
585
586     from_prod res_ty res expr EmptyProd = return (res, [])
587     from_prod res_ty res expr (UnaryProd r)
588       = from_comp res_ty res expr r
589     from_prod res_ty res expr (Prod { repr_ptup_tc  = ptup_tc
590                                     , repr_comp_tys = tys
591                                     , repr_comps    = comps })
592       = do
593           ptys <- mapM mkPDataType tys
594           vars <- newLocalVars (fsLit "ys") ptys
595           (res', args) <- fold from_comp res_ty res (map Var vars) comps
596           let scrut = unwrapFamInstScrut ptup_tc tys expr
597               body  = mkWildCase scrut (exprType scrut) res_ty
598                       [(DataAlt ptup_con, vars, res')]
599           return (body, args)
600       where
601         [ptup_con] = tyConDataCons ptup_tc
602
603     from_comp res_ty res expr (Keep _ _) = return (res, [expr])
604     from_comp res_ty res expr (Wrap ty)
605       = do
606           wrap_tc  <- builtin wrapTyCon
607           (pwrap_tc, _) <- pdataReprTyCon (mkTyConApp wrap_tc [ty])
608           return (res, [unwrapNewTypeBody pwrap_tc [ty]
609                         $ unwrapFamInstScrut pwrap_tc [ty] expr])
610
611     fold f res_ty res exprs rs = foldrM f' (res, []) (zip exprs rs)
612       where
613         f' (expr, r) (res, args) = do
614                                      (res', args') <- f res_ty res expr r
615                                      return (res', args' ++ args)
616
617 buildPRDict :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
618 buildPRDict vect_tc prepr_tc _ r
619   = do
620       dict <- sum_dict r
621       pr_co <- mkBuiltinCo prTyCon
622       let co = mkAppCoercion pr_co
623              . mkSymCoercion
624              $ mkTyConApp arg_co ty_args
625       return (mkCoerce co dict)
626   where
627     ty_args = mkTyVarTys (tyConTyVars vect_tc)
628     Just arg_co = tyConFamilyCoercion_maybe prepr_tc
629
630     sum_dict EmptySum = prDFunOfTyCon =<< builtin voidTyCon
631     sum_dict (UnarySum r) = con_dict r
632     sum_dict (Sum { repr_sum_tc  = sum_tc
633                   , repr_con_tys = tys
634                   , repr_cons    = cons
635                   })
636       = do
637           dicts <- mapM con_dict cons
638           dfun  <- prDFunOfTyCon sum_tc
639           return $ dfun `mkTyApps` tys `mkApps` dicts
640
641     con_dict (ConRepr _ r) = prod_dict r
642
643     prod_dict EmptyProd = prDFunOfTyCon =<< builtin voidTyCon
644     prod_dict (UnaryProd r) = comp_dict r
645     prod_dict (Prod { repr_tup_tc   = tup_tc
646                     , repr_comp_tys = tys
647                     , repr_comps    = comps })
648       = do
649           dicts <- mapM comp_dict comps
650           dfun <- prDFunOfTyCon tup_tc
651           return $ dfun `mkTyApps` tys `mkApps` dicts
652
653     comp_dict (Keep _ pr) = return pr
654     comp_dict (Wrap ty)   = wrapPR ty
655
656
657 buildPDataTyCon :: TyCon -> TyCon -> SumRepr -> VM TyCon
658 buildPDataTyCon orig_tc vect_tc repr = fixV $ \repr_tc ->
659   do
660     name' <- cloneName mkPDataTyConOcc orig_name
661     rhs   <- buildPDataTyConRhs orig_name vect_tc repr_tc repr
662     pdata <- builtin pdataTyCon
663
664     liftDs $ buildAlgTyCon name'
665                            tyvars
666                            []          -- no stupid theta
667                            rhs
668                            rec_flag    -- FIXME: is this ok?
669                            False       -- FIXME: no generics
670                            False       -- not GADT syntax
671                            (Just $ mk_fam_inst pdata vect_tc)
672   where
673     orig_name = tyConName orig_tc
674     tyvars = tyConTyVars vect_tc
675     rec_flag = boolToRecFlag (isRecursiveTyCon vect_tc)
676
677
678 buildPDataTyConRhs :: Name -> TyCon -> TyCon -> SumRepr -> VM AlgTyConRhs
679 buildPDataTyConRhs orig_name vect_tc repr_tc repr
680   = do
681       data_con <- buildPDataDataCon orig_name vect_tc repr_tc repr
682       return $ DataTyCon { data_cons = [data_con], is_enum = False }
683
684 buildPDataDataCon :: Name -> TyCon -> TyCon -> SumRepr -> VM DataCon
685 buildPDataDataCon orig_name vect_tc repr_tc repr
686   = do
687       dc_name  <- cloneName mkPDataDataConOcc orig_name
688       comp_tys <- sum_tys repr
689
690       liftDs $ buildDataCon dc_name
691                             False                  -- not infix
692                             (map (const NotMarkedStrict) comp_tys)
693                             []                     -- no field labels
694                             tvs
695                             []                     -- no existentials
696                             []                     -- no eq spec
697                             []                     -- no context
698                             comp_tys
699                             (mkFamilyTyConApp repr_tc (mkTyVarTys tvs))
700                             repr_tc
701   where
702     tvs   = tyConTyVars vect_tc
703
704     sum_tys EmptySum = return []
705     sum_tys (UnarySum r) = con_tys r
706     sum_tys (Sum { repr_sel_ty = sel_ty
707                  , repr_cons   = cons })
708       = liftM (sel_ty :) (concatMapM con_tys cons)
709
710     con_tys (ConRepr _ r) = prod_tys r
711
712     prod_tys EmptyProd = return []
713     prod_tys (UnaryProd r) = liftM singleton (comp_ty r)
714     prod_tys (Prod { repr_comps = comps }) = mapM comp_ty comps
715
716     comp_ty r = mkPDataType (compOrigType r)
717
718
719 mkPADFun :: TyCon -> VM Var
720 mkPADFun vect_tc
721   = newExportedVar (mkPADFunOcc $ getOccName vect_tc) =<< paDFunType vect_tc
722
723 buildTyConBindings :: TyCon -> TyCon -> TyCon -> TyCon -> Var -> SumRepr 
724                    -> VM [(Var, CoreExpr)]
725 buildTyConBindings orig_tc vect_tc prepr_tc pdata_tc dfun repr
726   = do
727       vectDataConWorkers orig_tc vect_tc pdata_tc
728       dict <- buildPADict vect_tc prepr_tc pdata_tc repr
729       binds <- takeHoisted
730       return $ (dfun, dict) : binds
731
732 vectDataConWorkers :: TyCon -> TyCon -> TyCon -> VM ()
733 vectDataConWorkers orig_tc vect_tc arr_tc
734   = do
735       bs <- sequence
736           . zipWith3 def_worker  (tyConDataCons orig_tc) rep_tys
737           $ zipWith4 mk_data_con (tyConDataCons vect_tc)
738                                  rep_tys
739                                  (inits rep_tys)
740                                  (tail $ tails rep_tys)
741       mapM_ (uncurry hoistBinding) bs
742   where
743     tyvars   = tyConTyVars vect_tc
744     var_tys  = mkTyVarTys tyvars
745     ty_args  = map Type var_tys
746     res_ty   = mkTyConApp vect_tc var_tys
747
748     cons     = tyConDataCons vect_tc
749     arity    = length cons
750     [arr_dc] = tyConDataCons arr_tc
751
752     rep_tys  = map dataConRepArgTys $ tyConDataCons vect_tc
753
754
755     mk_data_con con tys pre post
756       = liftM2 (,) (vect_data_con con)
757                    (lift_data_con tys pre post (mkDataConTag con))
758
759     sel_replicate len tag
760       | arity > 1 = do
761                       rep <- builtin (selReplicate arity)
762                       return [rep `mkApps` [len, tag]]
763
764       | otherwise = return []
765
766     vect_data_con con = return $ mkConApp con ty_args
767     lift_data_con tys pre_tys post_tys tag
768       = do
769           len  <- builtin liftingContext
770           args <- mapM (newLocalVar (fsLit "xs"))
771                   =<< mapM mkPDataType tys
772
773           sel  <- sel_replicate (Var len) tag
774
775           pre   <- mapM emptyPD (concat pre_tys)
776           post  <- mapM emptyPD (concat post_tys)
777
778           return . mkLams (len : args)
779                  . wrapFamInstBody arr_tc var_tys
780                  . mkConApp arr_dc
781                  $ ty_args ++ sel ++ pre ++ map Var args ++ post
782
783     def_worker data_con arg_tys mk_body
784       = do
785           body <- closedV
786                 . inBind orig_worker
787                 . polyAbstract tyvars $ \abstract ->
788                   liftM (abstract . vectorised)
789                 $ buildClosures tyvars [] arg_tys res_ty mk_body
790
791           vect_worker <- cloneId mkVectOcc orig_worker (exprType body)
792           defGlobalVar orig_worker vect_worker
793           return (vect_worker, body)
794       where
795         orig_worker = dataConWorkId data_con
796
797 buildPADict :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
798 buildPADict vect_tc prepr_tc arr_tc repr
799   = polyAbstract tvs $ \abstract ->
800     do
801       meth_binds <- mapM mk_method paMethods
802       let meth_exprs = map (Var . fst) meth_binds
803
804       pa_dc <- builtin paDataCon
805       let dict = mkConApp pa_dc (Type (mkTyConApp vect_tc arg_tys) : meth_exprs)
806           body = Let (Rec meth_binds) dict
807       return . mkInlineMe $ abstract body
808   where
809     tvs = tyConTyVars arr_tc
810     arg_tys = mkTyVarTys tvs
811
812     mk_method (name, build)
813       = localV
814       $ do
815           body <- build vect_tc prepr_tc arr_tc repr
816           var  <- newLocalVar name (exprType body)
817           return (var, mkInlineMe body)
818
819 paMethods :: [(FastString, TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr)]
820 paMethods = [(fsLit "dictPRepr",    buildPRDict),
821              (fsLit "toPRepr",      buildToPRepr),
822              (fsLit "fromPRepr",    buildFromPRepr),
823              (fsLit "toArrPRepr",   buildToArrPRepr),
824              (fsLit "fromArrPRepr", buildFromArrPRepr)]
825
826 -- | Split the given tycons into two sets depending on whether they have to be
827 -- converted (first list) or not (second list). The first argument contains
828 -- information about the conversion status of external tycons:
829 --
830 --   * tycons which have converted versions are mapped to True
831 --   * tycons which are not changed by vectorisation are mapped to False
832 --   * tycons which can't be converted are not elements of the map
833 --
834 classifyTyCons :: UniqFM Bool -> [TyConGroup] -> ([TyCon], [TyCon])
835 classifyTyCons = classify [] []
836   where
837     classify conv keep _  [] = (conv, keep)
838     classify conv keep cs ((tcs, ds) : rs)
839       | can_convert && must_convert
840         = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc,True) | tc <- tcs]) rs
841       | can_convert
842         = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc,False) | tc <- tcs]) rs
843       | otherwise
844         = classify conv keep cs rs
845       where
846         refs = ds `delListFromUniqSet` tcs
847
848         can_convert  = isNullUFM (refs `minusUFM` cs) && all convertable tcs
849         must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
850
851         convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
852
853 -- | Compute mutually recursive groups of tycons in topological order
854 --
855 tyConGroups :: [TyCon] -> [TyConGroup]
856 tyConGroups tcs = map mk_grp (stronglyConnCompFromEdgedVertices edges)
857   where
858     edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
859                                 , let ds = tyConsOfTyCon tc]
860
861     mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
862     mk_grp (CyclicSCC els)       = (tcs, unionManyUniqSets dss)
863       where
864         (tcs, dss) = unzip els
865
866 tyConsOfTyCon :: TyCon -> UniqSet TyCon
867 tyConsOfTyCon
868   = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
869
870 tyConsOfType :: Type -> UniqSet TyCon
871 tyConsOfType ty
872   | Just ty' <- coreView ty    = tyConsOfType ty'
873 tyConsOfType (TyVarTy _)       = emptyUniqSet
874 tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
875   where
876     extend | isUnLiftedTyCon tc
877            || isTupleTyCon   tc = id
878
879            | otherwise          = (`addOneToUniqSet` tc)
880
881 tyConsOfType (AppTy a b)       = tyConsOfType a `unionUniqSets` tyConsOfType b
882 tyConsOfType (FunTy a b)       = (tyConsOfType a `unionUniqSets` tyConsOfType b)
883                                  `addOneToUniqSet` funTyCon
884 tyConsOfType (ForAllTy _ ty)   = tyConsOfType ty
885 tyConsOfType other             = pprPanic "ClosureConv.tyConsOfType" $ ppr other
886
887 tyConsOfTypes :: [Type] -> UniqSet TyCon
888 tyConsOfTypes = unionManyUniqSets . map tyConsOfType
889
890
891 -- ----------------------------------------------------------------------------
892 -- Conversions
893
894 fromVect :: Type -> CoreExpr -> VM CoreExpr
895 fromVect ty expr | Just ty' <- coreView ty = fromVect ty' expr
896 fromVect (FunTy arg_ty res_ty) expr
897   = do
898       arg     <- newLocalVar (fsLit "x") arg_ty
899       varg    <- toVect arg_ty (Var arg)
900       varg_ty <- vectType arg_ty
901       vres_ty <- vectType res_ty
902       apply   <- builtin applyVar
903       body    <- fromVect res_ty
904                $ Var apply `mkTyApps` [varg_ty, vres_ty] `mkApps` [expr, varg]
905       return $ Lam arg body
906 fromVect ty expr
907   = identityConv ty >> return expr
908
909 toVect :: Type -> CoreExpr -> VM CoreExpr
910 toVect ty expr = identityConv ty >> return expr
911
912 identityConv :: Type -> VM ()
913 identityConv ty | Just ty' <- coreView ty = identityConv ty'
914 identityConv (TyConApp tycon tys)
915   = do
916       mapM_ identityConv tys
917       identityConvTyCon tycon
918 identityConv _ = noV
919
920 identityConvTyCon :: TyCon -> VM ()
921 identityConvTyCon tc
922   | isBoxedTupleTyCon tc = return ()
923   | isUnLiftedTyCon tc   = return ()
924   | otherwise            = do
925                              tc' <- maybeV (lookupTyCon tc)
926                              if tc == tc' then return () else noV
927