Collect hoisted vectorised functions
[ghc-hetmet.git] / compiler / vectorise / VectUtils.hs
index 2a3b3fa..5b70bf4 100644 (file)
@@ -1,7 +1,10 @@
 module VectUtils (
+  collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
   splitClosureTy,
   mkPADictType, mkPArrayType,
-  paDictArgType, paDictOfType
+  paDictArgType, paDictOfType,
+  lookupPArrayFamInst,
+  hoistExpr
 ) where
 
 #include "HsVersions.h"
@@ -9,6 +12,7 @@ module VectUtils (
 import VectMonad
 
 import CoreSyn
+import CoreUtils
 import Type
 import TypeRep
 import TyCon
@@ -16,9 +20,26 @@ import Var
 import PrelNames
 
 import Outputable
+import FastString
 
 import Control.Monad         ( liftM )
 
+collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type])
+collectAnnTypeArgs expr = go expr []
+  where
+    go (_, AnnApp f (_, AnnType ty)) tys = go f (ty : tys)
+    go e                             tys = (e, tys)
+
+collectAnnTypeBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann)
+collectAnnTypeBinders expr = go [] expr
+  where
+    go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e
+    go bs e                           = (reverse bs, e)
+
+isAnnTypeArg :: AnnExpr b ann -> Bool
+isAnnTypeArg (_, AnnType t) = True
+isAnnTypeArg _              = False
+
 isClosureTyCon :: TyCon -> Bool
 isClosureTyCon tc = tyConUnique tc == closureTyConKey
 
@@ -87,3 +108,14 @@ paDFunApply dfun tys
       dicts <- mapM paDictOfType tys
       return $ mkApps (mkTyApps dfun tys) dicts
 
+lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
+lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
+
+hoistExpr :: FastString -> CoreExpr -> VM Var
+hoistExpr fs expr
+  = do
+      var <- newLocalVar fs (exprType expr)
+      updLEnv $ \env ->
+        env { local_bindings = (var, expr) : local_bindings env }
+      return var
+