Fix vectorisation of sum type constructors
[ghc-hetmet.git] / compiler / vectorise / VectUtils.hs
index 78e386d..958c5e6 100644 (file)
@@ -3,11 +3,14 @@ module VectUtils (
   collectAnnValBinders,
   mkDataConTag,
   splitClosureTy,
-  mkPReprType, mkPReprAlts,
-  mkPADictType, mkPArrayType,
+
+  mkBuiltinCo,
+  mkPADictType, mkPArrayType, mkPReprType,
+
   parrayReprTyCon, parrayReprDataCon, mkVScrut,
+  prDFunOfTyCon,
   paDictArgType, paDictOfType, paDFunType,
-  paMethod, lengthPA, replicatePA, emptyPA, liftPA,
+  paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
   polyAbstract, polyApply, polyVApply,
   hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
   buildClosure, buildClosures,
@@ -22,21 +25,25 @@ import VectMonad
 import DsUtils
 import CoreSyn
 import CoreUtils
+import Coercion
 import Type
 import TypeRep
 import TyCon
-import DataCon            ( DataCon, dataConWrapId, dataConTag )
+import DataCon
 import Var
 import Id                 ( mkWildId )
 import MkId               ( unwrapFamInstScrut )
+import Name               ( Name )
 import PrelNames
 import TysWiredIn
+import TysPrim            ( intPrimTy )
 import BasicTypes         ( Boxity(..) )
 
 import Outputable
 import FastString
 
-import Control.Monad         ( liftM, zipWithM_ )
+import Data.List             ( zipWith4 )
+import Control.Monad         ( liftM, liftM2, zipWithM_ )
 
 collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
 collectAnnTypeArgs expr = go expr []
@@ -61,29 +68,45 @@ isAnnTypeArg (_, AnnType t) = True
 isAnnTypeArg _              = False
 
 mkDataConTag :: DataCon -> CoreExpr
-mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
+mkDataConTag = mkIntLitInt . dataConTag
 
-isClosureTyCon :: TyCon -> Bool
-isClosureTyCon tc = tyConName tc == closureTyConName
+splitUnTy :: String -> Name -> Type -> Type
+splitUnTy s name ty
+  | Just (tc, [ty']) <- splitTyConApp_maybe ty
+  , tyConName tc == name
+  = ty'
 
-splitClosureTy :: Type -> (Type, Type)
-splitClosureTy ty
-  | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
-  , isClosureTyCon tc
-  = (arg_ty, res_ty)
+  | otherwise = pprPanic s (ppr ty)
+
+splitBinTy :: String -> Name -> Type -> (Type, Type)
+splitBinTy s name ty
+  | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
+  , tyConName tc == name
+  = (ty1, ty2)
 
-  | otherwise = pprPanic "splitClosureTy" (ppr ty)
+  | otherwise = pprPanic s (ppr ty)
 
-isPArrayTyCon :: TyCon -> Bool
-isPArrayTyCon tc = tyConName tc == parrayTyConName
+splitFixedTyConApp :: TyCon -> Type -> [Type]
+splitFixedTyConApp tc ty
+  | Just (tc', tys) <- splitTyConApp_maybe ty
+  , tc == tc'
+  = tys
+
+  | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty)
+
+splitClosureTy :: Type -> (Type, Type)
+splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
 
 splitPArrayTy :: Type -> Type
-splitPArrayTy ty
-  | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
-  , isPArrayTyCon tc
-  = arg_ty
+splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
+
+splitPrimTyCon :: Type -> Maybe TyCon
+splitPrimTyCon ty
+  | Just (tycon, []) <- splitTyConApp_maybe ty
+  , isPrimTyCon tycon
+  = Just tycon
 
-  | otherwise = pprPanic "splitPArrayTy" (ppr ty)
+  | otherwise = Nothing
 
 mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
 mkBuiltinTyConApp get_tc tys
@@ -110,79 +133,33 @@ mkBuiltinTyConApps1 get_tc dft tys
   where
     mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
 
-mkPReprType :: [[Type]] -> VM Type
-mkPReprType [] = return unitTy
-mkPReprType tys
-  = do
-      embed <- builtin embedTyCon
-      cross <- builtin crossTyCon
-      plus  <- builtin plusTyCon
-
-      let mk_embed ty      = mkTyConApp embed [ty]
-          mk_cross ty1 ty2 = mkTyConApp cross [ty1, ty2]
-          mk_plus  ty1 ty2 = mkTyConApp plus  [ty1, ty2]
-
-          mk_tup   []      = unitTy
-          mk_tup   tys     = foldr1 mk_cross tys
-
-          mk_sum   []      = unitTy
-          mk_sum   tys     = foldr1 mk_plus  tys
-
-      return . mk_sum
-             . map (mk_tup . map mk_embed)
-             $ tys
-
-mkPReprAlts :: [[CoreExpr]] -> VM ([CoreExpr], Type)
-mkPReprAlts ess
-  = do
-      embed_tc <- builtin embedTyCon
-      embed_dc <- builtin embedDataCon
-      cross_tc <- builtin crossTyCon
-      cross_dc <- builtin crossDataCon
-      plus_tc  <- builtin plusTyCon
-      left_dc  <- builtin leftDataCon
-      right_dc <- builtin rightDataCon
-
-      let mk_embed (expr, ty, pa)
-            = (mkConApp   embed_dc [Type ty, pa, expr],
-               mkTyConApp embed_tc [ty])
-
-          mk_cross (expr1, ty1) (expr2, ty2)
-            = (mkConApp   cross_dc [Type ty1, Type ty2, expr1, expr2],
-               mkTyConApp cross_tc [ty1, ty2])
-
-          mk_tup [] = (Var unitDataConId, unitTy)
-          mk_tup es = foldr1 mk_cross es
-
-          mk_sum []           = ([Var unitDataConId], unitTy)
-          mk_sum [(expr, ty)] = ([expr], ty)
-          mk_sum ((expr, lty) : es)
-            = let (alts, rty) = mk_sum es
-              in
-              (mkConApp left_dc [Type lty, Type rty, expr]
-                 : [mkConApp right_dc [Type lty, Type rty, alt] | alt <- alts],
-               mkTyConApp plus_tc [lty, rty])
-      
-      liftM (mk_sum . map (mk_tup . map mk_embed))
-            (mapM (mapM init) ess)
-  where
-    init expr = let ty = exprType expr
-                in do
-                     pa <- paDictOfType ty
-                     return (expr, ty, pa)
-
 mkClosureType :: Type -> Type -> VM Type
 mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
 
 mkClosureTypes :: [Type] -> Type -> VM Type
 mkClosureTypes = mkBuiltinTyConApps closureTyCon
 
+mkPReprType :: Type -> VM Type
+mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty]
+
 mkPADictType :: Type -> VM Type
 mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
 
 mkPArrayType :: Type -> VM Type
+mkPArrayType ty
+  | Just tycon <- splitPrimTyCon ty
+  = do
+      arr <- traceMaybeV "mkPArrayType" (ppr tycon)
+           $ lookupPrimPArray tycon
+      return $ mkTyConApp arr []
 mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
 
+mkBuiltinCo :: (Builtins -> TyCon) -> VM Coercion
+mkBuiltinCo get_tc
+  = do
+      tc <- builtin get_tc
+      return $ mkTyConApp tc []
+
 parrayReprTyCon :: Type -> VM (TyCon, [Type])
 parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
 
@@ -199,6 +176,10 @@ mkVScrut (ve, le)
       (tc, arg_tys) <- parrayReprTyCon (exprType ve)
       return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
 
+prDFunOfTyCon :: TyCon -> VM CoreExpr
+prDFunOfTyCon tycon
+  = liftM Var (traceMaybeV "prDictOfTyCon" (ppr tycon) (lookupTyConPR tycon))
+
 paDictArgType :: TyVar -> VM (Maybe Type)
 paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
   where
@@ -254,24 +235,44 @@ paDFunApply dfun tys
       dicts <- mapM paDictOfType tys
       return $ mkApps (mkTyApps dfun tys) dicts
 
-paMethod :: (Builtins -> Var) -> Type -> VM CoreExpr
-paMethod method ty
+type PAMethod = (Builtins -> Var, String)
+
+pa_length    = (lengthPAVar,    "lengthPA")
+pa_replicate = (replicatePAVar, "replicatePA")
+pa_empty     = (emptyPAVar,     "emptyPA")
+
+paMethod :: PAMethod -> Type -> VM CoreExpr
+paMethod (method, name) ty
+  | Just tycon <- splitPrimTyCon ty
+  = do
+      fn <- traceMaybeV "paMethod" (ppr tycon <+> text name)
+          $ lookupPrimMethod tycon name
+      return (Var fn)
+
+paMethod (method, name) ty
   = do
       fn   <- builtin method
       dict <- paDictOfType ty
       return $ mkApps (Var fn) [Type ty, dict]
 
+mkPR :: Type -> VM CoreExpr
+mkPR ty
+  = do
+      fn   <- builtin mkPRVar
+      dict <- paDictOfType ty
+      return $ mkApps (Var fn) [Type ty, dict]
+
 lengthPA :: CoreExpr -> VM CoreExpr
-lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty)
+lengthPA x = liftM (`App` x) (paMethod pa_length ty)
   where
     ty = splitPArrayTy (exprType x)
 
 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
 replicatePA len x = liftM (`mkApps` [len,x])
-                          (paMethod replicatePAVar (exprType x))
+                          (paMethod pa_replicate (exprType x))
 
 emptyPA :: Type -> VM CoreExpr
-emptyPA = paMethod emptyPAVar
+emptyPA = paMethod pa_empty
 
 liftPA :: CoreExpr -> VM CoreExpr
 liftPA x