Incomplete support for boxing during vectorisation
[ghc-hetmet.git] / compiler / vectorise / VectUtils.hs
index 1c72bb7..a540b4d 100644 (file)
@@ -1,8 +1,16 @@
+{-# OPTIONS -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
+-- for details
+
 module VectUtils (
   collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
   collectAnnValBinders,
-  mkDataConTag,
-  splitClosureTy,
+  dataConTagZ, mkDataConTag, mkDataConTagLit,
+
+  newLocalVVar,
 
   mkBuiltinCo,
   mkPADictType, mkPArrayType, mkPReprType,
@@ -10,7 +18,7 @@ module VectUtils (
   parrayReprTyCon, parrayReprDataCon, mkVScrut,
   prDFunOfTyCon,
   paDictArgType, paDictOfType, paDFunType,
-  paMethod, mkPR, lengthPA, replicatePA, emptyPA, liftPA,
+  paMethod, mkPR, lengthPA, replicatePA, emptyPA, packPA, combinePA, liftPA,
   polyAbstract, polyApply, polyVApply,
   hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
   buildClosure, buildClosures,
@@ -38,6 +46,7 @@ import PrelNames
 import TysWiredIn
 import TysPrim            ( intPrimTy )
 import BasicTypes         ( Boxity(..) )
+import Literal            ( Literal, mkMachInt )
 
 import Outputable
 import FastString
@@ -67,38 +76,14 @@ isAnnTypeArg :: AnnExpr b ann -> Bool
 isAnnTypeArg (_, AnnType t) = True
 isAnnTypeArg _              = False
 
-mkDataConTag :: DataCon -> CoreExpr
-mkDataConTag con = mkIntLitInt (dataConTag con - fIRST_TAG)
-
-splitUnTy :: String -> Name -> Type -> Type
-splitUnTy s name ty
-  | Just (tc, [ty']) <- splitTyConApp_maybe ty
-  , tyConName tc == name
-  = ty'
-
-  | otherwise = pprPanic s (ppr ty)
+dataConTagZ :: DataCon -> Int
+dataConTagZ con = dataConTag con - fIRST_TAG
 
-splitBinTy :: String -> Name -> Type -> (Type, Type)
-splitBinTy s name ty
-  | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
-  , tyConName tc == name
-  = (ty1, ty2)
+mkDataConTagLit :: DataCon -> Literal
+mkDataConTagLit = mkMachInt . toInteger . dataConTagZ
 
-  | otherwise = pprPanic s (ppr ty)
-
-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 = splitUnTy "splitPArrayTy" parrayTyConName
+mkDataConTag :: DataCon -> CoreExpr
+mkDataConTag = mkIntLitInt . dataConTagZ
 
 splitPrimTyCon :: Type -> Maybe TyCon
 splitPrimTyCon ty
@@ -240,6 +225,7 @@ type PAMethod = (Builtins -> Var, String)
 pa_length    = (lengthPAVar,    "lengthPA")
 pa_replicate = (replicatePAVar, "replicatePA")
 pa_empty     = (emptyPAVar,     "emptyPA")
+pa_pack      = (packPAVar,      "packPA")
 
 paMethod :: PAMethod -> Type -> VM CoreExpr
 paMethod (method, name) ty
@@ -262,10 +248,8 @@ mkPR ty
       dict <- paDictOfType ty
       return $ mkApps (Var fn) [Type ty, dict]
 
-lengthPA :: CoreExpr -> VM CoreExpr
-lengthPA x = liftM (`App` x) (paMethod pa_length ty)
-  where
-    ty = splitPArrayTy (exprType x)
+lengthPA :: Type -> CoreExpr -> VM CoreExpr
+lengthPA ty x = liftM (`App` x) (paMethod pa_length ty)
 
 replicatePA :: CoreExpr -> CoreExpr -> VM CoreExpr
 replicatePA len x = liftM (`mkApps` [len,x])
@@ -274,6 +258,18 @@ replicatePA len x = liftM (`mkApps` [len,x])
 emptyPA :: Type -> VM CoreExpr
 emptyPA = paMethod pa_empty
 
+packPA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> VM CoreExpr
+packPA ty xs len sel = liftM (`mkApps` [len, sel])
+                             (paMethod pa_pack ty)
+
+combinePA :: Type -> CoreExpr -> CoreExpr -> CoreExpr -> [CoreExpr]
+          -> VM CoreExpr
+combinePA ty len sel is xs
+  = liftM (`mkApps` (len : sel : is : xs))
+          (paMethod (combinePAVar n, "combine" ++ show n ++ "PA") ty)
+  where
+    n = length xs
+
 liftPA :: CoreExpr -> VM CoreExpr
 liftPA x
   = do
@@ -350,6 +346,19 @@ takeHoisted
       setGEnv $ env { global_bindings = [] }
       return $ global_bindings env
 
+boxExpr :: Type -> VExpr -> VM VExpr
+boxExpr ty (vexpr, lexpr)
+  | Just (tycon, []) <- splitTyConApp_maybe ty
+  , isUnLiftedTyCon tycon
+  = do
+      r <- lookupBoxedTyCon tycon
+      case r of
+        Just tycon' -> let [dc] = tyConDataCons tycon'
+                       in
+                       return (mkConApp dc [vexpr], lexpr)
+        Nothing     -> return (vexpr, lexpr)
+
+
 mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr
 mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
   = do
@@ -359,15 +368,13 @@ mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv)
       return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv],
               Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv])
 
-mkClosureApp :: VExpr -> VExpr -> VM VExpr
-mkClosureApp (vclo, lclo) (varg, larg)
+mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
+mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
   = do
       vapply <- builtin applyClosureVar
       lapply <- builtin applyClosurePVar
       return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
               Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
-  where
-    (arg_ty, res_ty) = splitClosureTy (exprType vclo)
 
 buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
 buildClosures tvs vars [] res_ty mk_body
@@ -436,7 +443,7 @@ mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM
 mkLiftEnv lc [ty] [v]
   = return (Var v, \env body ->
                    do
-                     len <- lengthPA (Var v)
+                     len <- lengthPA ty (Var v)
                      return . Let (NonRec v env)
                             $ Case len lc (exprType body) [(DEFAULT, [], body)])