Simplify handling of variables during vectorisation
[ghc-hetmet.git] / compiler / vectorise / Vectorise.hs
index c974c20..83f0480 100644 (file)
@@ -5,6 +5,7 @@ where
 
 import VectMonad
 import VectUtils
+import VectType
 
 import DynFlags
 import HscTypes
@@ -18,7 +19,8 @@ import Rules                ( RuleBase )
 import DataCon
 import TyCon
 import Type
-import TypeRep
+import FamInstEnv           ( extendFamInstEnvList )
+import InstEnv              ( extendInstEnvList )
 import Var
 import VarEnv
 import VarSet
@@ -38,8 +40,7 @@ import BasicTypes           ( Boxity(..) )
 
 import Outputable
 import FastString
-import Control.Monad        ( liftM, liftM2, mapAndUnzipM, zipWithM_ )
-import Data.Maybe           ( maybeToList )
+import Control.Monad        ( liftM, liftM2, mapAndUnzipM )
 
 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
           -> IO (SimplCount, ModGuts)
@@ -57,8 +58,22 @@ vectorise hsc_env _ _ guts
 vectModule :: ModGuts -> VM ModGuts
 vectModule guts
   = do
+      (types', fam_insts, pa_insts) <- vectTypeEnv (mg_types guts)
+      
+      let insts         = map painstInstance pa_insts
+          fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
+          inst_env'     = extendInstEnvList (mg_inst_env guts) insts
+      updGEnv (setInstEnvs inst_env' fam_inst_env')
+     
+      dicts  <- mapM buildPADict pa_insts 
       binds' <- mapM vectTopBind (mg_binds guts)
-      return $ guts { mg_binds = binds' }
+      return $ guts { mg_types        = types'
+                    , mg_binds        = Rec (concat dicts) : binds'
+                    , mg_inst_env     = inst_env'
+                    , mg_fam_inst_env = fam_inst_env'
+                    , mg_insts        = mg_insts guts ++ insts
+                    , mg_fam_insts    = mg_fam_insts guts ++ fam_insts
+                    }
 
 vectTopBind :: CoreBind -> VM CoreBind
 vectTopBind b@(NonRec var expr)
@@ -107,7 +122,7 @@ vectBndr v
       updLEnv (mapTo vv lv)
       return (vv, lv)
   where
-    mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }
+    mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
 
 vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
 vectBndrIn v p
@@ -128,15 +143,6 @@ vectBndrsIn vs p
 -- ----------------------------------------------------------------------------
 -- Expressions
 
-replicateP :: CoreExpr -> CoreExpr -> VM CoreExpr
-replicateP expr len
-  = do
-      dict <- paDictOfType ty
-      rep  <- builtin replicatePAVar
-      return $ mkApps (Var rep) [Type ty, dict, expr, len]
-  where
-    ty = exprType expr
-
 capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
 capply (vfn, lfn) (varg, larg)
   = do
@@ -153,49 +159,28 @@ vectVar lc v
   = do
       r <- lookupVar v
       case r of
-        Local es     -> return es
-        Global vexpr -> do
-                          lexpr <- replicateP vexpr lc
-                          return (vexpr, lexpr)
+        Local (vv,lv) -> return (Var vv, Var lv)
+        Global vv     -> do
+                           let vexpr = Var vv
+                           lexpr <- replicatePA vexpr lc
+                           return (vexpr, lexpr)
 
 vectPolyVar :: CoreExpr -> Var -> [Type] -> VM (CoreExpr, CoreExpr)
 vectPolyVar lc v tys
   = do
+      vtys <- mapM vectType tys
       r <- lookupVar v
       case r of
-        Local (vexpr, lexpr) -> liftM2 (,) (mk_app vexpr) (mk_app lexpr)
-        Global poly          -> do
-                                  vexpr <- mk_app poly
-                                  lexpr <- replicateP vexpr lc
-                                  return (vexpr, lexpr)
-  where
-    mk_app e = applyToTypes e =<< mapM vectType tys
-
-abstractOverTyVars :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
-abstractOverTyVars tvs p
-  = do
-      mdicts <- mapM mk_dict_var tvs
-      zipWithM_ (\tv -> maybe (defLocalTyVar tv) (defLocalTyVarWithPA tv . Var)) tvs mdicts
-      p (mk_lams mdicts)
-  where
-    mk_dict_var tv = do
-                       r <- paDictArgType tv
-                       case r of
-                         Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
-                         Nothing -> return Nothing
-
-    mk_lams mdicts = mkLams (tvs ++ [dict | Just dict <- mdicts])
-
-applyToTypes :: CoreExpr -> [Type] -> VM CoreExpr
-applyToTypes expr tys
-  = do
-      dicts <- mapM paDictOfType tys
-      return $ expr `mkTyApps` tys `mkApps` dicts
+        Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
+                                     (polyApply (Var lv) vtys)
+        Global poly    -> do
+                            vexpr <- polyApply (Var poly) vtys
+                            lexpr <- replicatePA vexpr lc
+                            return (vexpr, lexpr)
 
 vectPolyExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
 vectPolyExpr lc expr
-  = localV
-  . abstractOverTyVars tvs $ \mk_lams ->
+  = polyAbstract tvs $ \mk_lams ->
     -- FIXME: shadowing (tvs in lc)
     do
       (vmono, lmono) <- vectExpr lc mono
@@ -214,7 +199,7 @@ vectExpr lc (_, AnnVar v)   = vectVar lc v
 vectExpr lc (_, AnnLit lit)
   = do
       let vexpr = Lit lit
-      lexpr <- replicateP vexpr lc
+      lexpr <- replicatePA vexpr lc
       return (vexpr, lexpr)
 
 vectExpr lc (_, AnnNote note expr)
@@ -279,8 +264,8 @@ vectExpr lc (fvs, AnnLam bndr body)
       res_ty <- vectType (exprType $ deAnnotate body)
 
       -- FIXME: move the functions to the top level
-      mono_vfn <- applyToTypes (Var vfn_var) (map TyVarTy tyvars)
-      mono_lfn <- applyToTypes (Var lfn_var) (map TyVarTy tyvars)
+      mono_vfn <- polyApply (Var vfn_var) (mkTyVarTys tyvars)
+      mono_lfn <- polyApply (Var lfn_var) (mkTyVarTys tyvars)
 
       mk_clo <- builtin mkClosureVar
       mk_cloP <- builtin mkClosurePVar
@@ -292,7 +277,6 @@ vectExpr lc (fvs, AnnLam bndr body)
                              `mkApps`   [pa_dict, mono_vfn, mono_lfn, lenv]
 
       return (vclo, lclo)
-       
 
 data CEnvInfo = CEnvInfo {
                cenv_vars         :: [Var]
@@ -310,8 +294,8 @@ mkCEnvInfo fvs arg body
       locals <- readLEnv local_vars
       let
           (vars, vals) = unzip
-                 [(var, val) | var      <- varSetElems fvs
-                             , Just val <- [lookupVarEnv locals var]]
+                 [(var, (Var v, Var v')) | var      <- varSetElems fvs
+                                         , Just (v,v') <- [lookupVarEnv locals var]]
       vtys <- mapM (vectType . varType) vars
 
       (vty, repr_tycon, repr_tyargs, repr_datacon) <- mk_env_ty vtys
@@ -363,7 +347,7 @@ mkClosureFns :: CEnvInfo -> [TyVar] -> Var -> CoreExprWithFVs
              -> VM (CoreExpr, CoreExpr)
 mkClosureFns info tyvars arg body
   = closedV
-  . abstractOverTyVars tyvars
+  . polyAbstract tyvars
   $ \mk_tlams ->
   do
     (vfn, lfn) <- mkClosureMonoFns info arg body
@@ -397,9 +381,9 @@ mkClosureMonoFns info arg body
 
     bind_lenv lenv lbody lc_bndr [lbndr]
       = do
-          lengthPA <- builtin lengthPAVar
+          len <- lengthPA (Var lbndr)
           return . Let (NonRec lbndr lenv)
-                 $ Case (mkApps (Var lengthPA) [Type vty, (Var lbndr)])
+                 $ Case len
                         lc_bndr
                         (exprType lbody)
                         [(DEFAULT, [], lbody)]
@@ -421,36 +405,3 @@ vectTyAppExpr :: CoreExpr -> CoreExprWithFVs -> [Type] -> VM (CoreExpr, CoreExpr
 vectTyAppExpr lc (_, AnnVar v) tys = vectPolyVar lc v tys
 vectTyAppExpr lc e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
 
--- ----------------------------------------------------------------------------
--- Types
-
-vectTyCon :: TyCon -> VM TyCon
-vectTyCon tc
-  | isFunTyCon tc        = builtin closureTyCon
-  | isBoxedTupleTyCon tc = return tc
-  | isUnLiftedTyCon tc   = return tc
-  | otherwise = do
-                  r <- lookupTyCon tc
-                  case r of
-                    Just tc' -> return tc'
-
-                    -- FIXME: just for now
-                    Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc
-
-vectType :: Type -> VM Type
-vectType ty | Just ty' <- coreView ty = vectType ty'
-vectType (TyVarTy tv) = return $ TyVarTy tv
-vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
-vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
-vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
-                                             (mapM vectType [ty1,ty2])
-vectType ty@(ForAllTy _ _)
-  = do
-      mdicts   <- mapM paDictArgType tyvars
-      mono_ty' <- vectType mono_ty
-      return $ tyvars `mkForAllTys` ([dict | Just dict <- mdicts] `mkFunTys` mono_ty')
-  where
-    (tyvars, mono_ty) = splitForAllTys ty
-
-vectType ty = pprPanic "vectType:" (ppr ty)
-