Refactor slightly
[ghc-hetmet.git] / compiler / vectorise / Vectorise.hs
1 module Vectorise( vectorise )
2 where
3
4 #include "HsVersions.h"
5
6 import VectMonad
7 import VectUtils
8
9 import DynFlags
10 import HscTypes
11
12 import CoreLint             ( showPass, endPass )
13 import CoreSyn
14 import CoreUtils
15 import CoreFVs
16 import TyCon
17 import Type
18 import TypeRep
19 import Var
20 import VarEnv
21 import Name                 ( mkSysTvName )
22 import NameEnv
23 import Id
24
25 import DsMonad hiding (mapAndUnzipM)
26
27 import PrelNames
28
29 import Outputable
30 import FastString
31 import Control.Monad        ( liftM, liftM2, mapAndUnzipM, zipWithM_ )
32 import Data.Maybe           ( maybeToList )
33
34 vectorise :: HscEnv -> ModGuts -> IO ModGuts
35 vectorise hsc_env guts
36   | not (Opt_Vectorise `dopt` dflags) = return guts
37   | otherwise
38   = do
39       showPass dflags "Vectorisation"
40       eps <- hscEPS hsc_env
41       let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
42       Just (info', guts') <- initV hsc_env guts info (vectModule guts)
43       endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
44       return $ guts' { mg_vect_info = info' }
45   where
46     dflags = hsc_dflags hsc_env
47
48 vectModule :: ModGuts -> VM ModGuts
49 vectModule guts = return guts
50
51 -- ----------------------------------------------------------------------------
52 -- Bindings
53
54 vectBndr :: Var -> VM (Var, Var)
55 vectBndr v
56   = do
57       vty <- vectType (idType v)
58       lty <- mkPArrayType vty
59       let vv = v `Id.setIdType` vty
60           lv = v `Id.setIdType` lty
61       updLEnv (mapTo vv lv)
62       return (vv, lv)
63   where
64     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (Var vv, Var lv) }
65
66 vectBndrIn :: Var -> VM a -> VM (Var, Var, a)
67 vectBndrIn v p
68   = localV
69   $ do
70       (vv, lv) <- vectBndr v
71       x <- p
72       return (vv, lv, x)
73
74 vectBndrsIn :: [Var] -> VM a -> VM ([Var], [Var], a)
75 vectBndrsIn vs p
76   = localV
77   $ do
78       (vvs, lvs) <- mapAndUnzipM vectBndr vs
79       x <- p
80       return (vvs, lvs, x)
81
82 -- ----------------------------------------------------------------------------
83 -- Expressions
84
85 replicateP :: CoreExpr -> CoreExpr -> VM CoreExpr
86 replicateP expr len
87   = do
88       dict <- paDictOfType ty
89       rep  <- builtin replicatePAVar
90       return $ mkApps (Var rep) [Type ty, dict, expr, len]
91   where
92     ty = exprType expr
93
94 capply :: (CoreExpr, CoreExpr) -> (CoreExpr, CoreExpr) -> VM (CoreExpr, CoreExpr)
95 capply (vfn, lfn) (varg, larg)
96   = do
97       apply  <- builtin applyClosureVar
98       applyP <- builtin applyClosurePVar
99       return (mkApps (Var apply)  [Type arg_ty, Type res_ty, vfn, varg],
100               mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
101   where
102     fn_ty            = exprType vfn
103     (arg_ty, res_ty) = splitClosureTy fn_ty
104
105 vectVar :: CoreExpr -> Var -> VM (CoreExpr, CoreExpr)
106 vectVar lc v = local v `orElseV` global v
107   where
108     local  v = maybeV (readLEnv $ \env -> lookupVarEnv (local_vars env) v)
109     global v = do
110                  vexpr <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
111                  lexpr <- replicateP vexpr lc
112                  return (vexpr, lexpr)
113
114 vectPolyVar :: CoreExpr -> Var -> [Type] -> VM (CoreExpr, CoreExpr)
115 vectPolyVar lc v tys
116   = do
117       r <- readLEnv $ \env -> lookupVarEnv (local_vars env) v
118       case r of
119         Just (vexpr, lexpr) -> liftM2 (,) (mk_app vexpr) (mk_app lexpr)
120         Nothing ->
121           do
122             poly  <- maybeV (readGEnv $ \env -> lookupVarEnv (global_vars env) v)
123             vexpr <- mk_app poly
124             lexpr <- replicateP vexpr lc
125             return (vexpr, lexpr)
126   where
127     mk_app e = applyToTypes e =<< mapM vectType tys
128
129 abstractOverTyVars :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a
130 abstractOverTyVars tvs p
131   = do
132       mdicts <- mapM mk_dict_var tvs
133       zipWithM_ (\tv -> maybe (deleteTyVarPA tv) (extendTyVarPA tv . Var)) tvs mdicts
134       p (mk_lams mdicts)
135   where
136     mk_dict_var tv = do
137                        r <- paDictArgType tv
138                        case r of
139                          Just ty -> liftM Just (newLocalVar FSLIT("dPA") ty)
140                          Nothing -> return Nothing
141
142     mk_lams mdicts = mkLams [arg | (tv, mdict) <- zip tvs mdicts
143                                  , arg <- tv : maybeToList mdict]
144
145 applyToTypes :: CoreExpr -> [Type] -> VM CoreExpr
146 applyToTypes expr tys
147   = do
148       dicts <- mapM paDictOfType tys
149       return $ mkApps expr [arg | (ty, dict) <- zip tys dicts
150                                 , arg <- [Type ty, dict]]
151     
152
153 vectPolyExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
154 vectPolyExpr lc expr
155   = localV
156   . abstractOverTyVars tvs $ \mk_lams ->
157     -- FIXME: shadowing (tvs in lc)
158     do
159       (vmono, lmono) <- vectExpr lc mono
160       return $ (mk_lams vmono, mk_lams lmono)
161   where
162     (tvs, mono) = collectAnnTypeBinders expr  
163                 
164 vectExpr :: CoreExpr -> CoreExprWithFVs -> VM (CoreExpr, CoreExpr)
165 vectExpr lc (_, AnnType ty)
166   = do
167       vty <- vectType ty
168       return (Type vty, Type vty)
169
170 vectExpr lc (_, AnnVar v)   = vectVar lc v
171
172 vectExpr lc (_, AnnLit lit)
173   = do
174       let vexpr = Lit lit
175       lexpr <- replicateP vexpr lc
176       return (vexpr, lexpr)
177
178 vectExpr lc (_, AnnNote note expr)
179   = do
180       (vexpr, lexpr) <- vectExpr lc expr
181       return (Note note vexpr, Note note lexpr)
182
183 vectExpr lc e@(_, AnnApp _ arg)
184   | isAnnTypeArg arg
185   = vectTyAppExpr lc fn tys
186   where
187     (fn, tys) = collectAnnTypeArgs e
188
189 vectExpr lc (_, AnnApp fn arg)
190   = do
191       fn'  <- vectExpr lc fn
192       arg' <- vectExpr lc arg
193       capply fn' arg'
194
195 vectExpr lc (_, AnnCase expr bndr ty alts)
196   = panic "vectExpr: case"
197
198 vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
199   = do
200       (vrhs, lrhs) <- vectPolyExpr lc rhs
201       (vbndr, lbndr, (vbody, lbody)) <- vectBndrIn bndr (vectExpr lc body)
202       return (Let (NonRec vbndr vrhs) vbody,
203               Let (NonRec lbndr lrhs) lbody)
204
205 vectExpr lc (_, AnnLet (AnnRec prs) body)
206   = do
207       (vbndrs, lbndrs, (vrhss, vbody, lrhss, lbody)) <- vectBndrsIn bndrs vect
208       return (Let (Rec (zip vbndrs vrhss)) vbody,
209               Let (Rec (zip lbndrs lrhss)) lbody)
210   where
211     (bndrs, rhss) = unzip prs
212     
213     vect = do
214              (vrhss, lrhss) <- mapAndUnzipM (vectExpr lc) rhss
215              (vbody, lbody) <- vectPolyExpr lc body
216              return (vrhss, vbody, lrhss, lbody)
217
218 vectExpr lc e@(_, AnnLam bndr body)
219   | isTyVar bndr = pprPanic "vectExpr" (ppr $ deAnnotate e)
220
221 vectTyAppExpr :: CoreExpr -> CoreExprWithFVs -> [Type] -> VM (CoreExpr, CoreExpr)
222 vectTyAppExpr lc (_, AnnVar v) tys = vectPolyVar lc v tys
223 vectTyAppExpr lc e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
224
225 -- ----------------------------------------------------------------------------
226 -- Types
227
228 vectTyCon :: TyCon -> VM TyCon
229 vectTyCon tc
230   | isFunTyCon tc        = builtin closureTyCon
231   | isBoxedTupleTyCon tc = return tc
232   | isUnLiftedTyCon tc   = return tc
233   | otherwise = do
234                   r <- lookupTyCon tc
235                   case r of
236                     Just tc' -> return tc'
237
238                     -- FIXME: just for now
239                     Nothing  -> pprTrace "ccTyCon:" (ppr tc) $ return tc
240
241 vectType :: Type -> VM Type
242 vectType ty | Just ty' <- coreView ty = vectType ty
243 vectType (TyVarTy tv) = return $ TyVarTy tv
244 vectType (AppTy ty1 ty2) = liftM2 AppTy (vectType ty1) (vectType ty2)
245 vectType (TyConApp tc tys) = liftM2 TyConApp (vectTyCon tc) (mapM vectType tys)
246 vectType (FunTy ty1 ty2)   = liftM2 TyConApp (builtin closureTyCon)
247                                              (mapM vectType [ty1,ty2])
248 vectType (ForAllTy tv ty)
249   = do
250       r   <- paDictArgType tv
251       ty' <- vectType ty
252       return $ ForAllTy tv (wrap r ty')
253   where
254     wrap Nothing      = id
255     wrap (Just pa_ty) = FunTy pa_ty
256
257 vectType ty = pprPanic "vectType:" (ppr ty)
258