Remove NDP-related stuff from PrelNames
[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 import VectType
9 import VectCore
10
11 import DynFlags
12 import HscTypes
13
14 import CoreLint             ( showPass, endPass )
15 import CoreSyn
16 import CoreUtils
17 import CoreFVs
18 import SimplMonad           ( SimplCount, zeroSimplCount )
19 import Rules                ( RuleBase )
20 import DataCon
21 import TyCon
22 import Type
23 import FamInstEnv           ( extendFamInstEnvList )
24 import InstEnv              ( extendInstEnvList )
25 import Var
26 import VarEnv
27 import VarSet
28 import Name                 ( Name, mkSysTvName, getName )
29 import NameEnv
30 import Id
31 import MkId                 ( unwrapFamInstScrut )
32 import OccName
33 import Module               ( Module )
34
35 import DsMonad hiding (mapAndUnzipM)
36 import DsUtils              ( mkCoreTup, mkCoreTupTy )
37
38 import Literal              ( Literal )
39 import PrelNames
40 import TysWiredIn
41 import TysPrim              ( intPrimTy )
42 import BasicTypes           ( Boxity(..) )
43
44 import Outputable
45 import FastString
46 import Control.Monad        ( liftM, liftM2, zipWithM, mapAndUnzipM )
47
48 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
49           -> IO (SimplCount, ModGuts)
50 vectorise hsc_env _ _ guts
51   = do
52       showPass dflags "Vectorisation"
53       eps <- hscEPS hsc_env
54       let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
55       Just (info', guts') <- initV hsc_env guts info (vectModule guts)
56       endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
57       return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
58   where
59     dflags = hsc_dflags hsc_env
60
61 vectModule :: ModGuts -> VM ModGuts
62 vectModule guts
63   = do
64       (types', fam_insts, tc_binds) <- vectTypeEnv (mg_types guts)
65       
66       let fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
67       updGEnv (setFamInstEnv fam_inst_env')
68      
69       -- dicts   <- mapM buildPADict pa_insts
70       -- workers <- mapM vectDataConWorkers pa_insts
71       binds'  <- mapM vectTopBind (mg_binds guts)
72       return $ guts { mg_types        = types'
73                     , mg_binds        = Rec tc_binds : binds'
74                     , mg_fam_inst_env = fam_inst_env'
75                     , mg_fam_insts    = mg_fam_insts guts ++ fam_insts
76                     }
77
78 vectTopBind :: CoreBind -> VM CoreBind
79 vectTopBind b@(NonRec var expr)
80   = do
81       var'  <- vectTopBinder var
82       expr' <- vectTopRhs var expr
83       hs    <- takeHoisted
84       return . Rec $ (var, expr) : (var', expr') : hs
85   `orElseV`
86     return b
87
88 vectTopBind b@(Rec bs)
89   = do
90       vars'  <- mapM vectTopBinder vars
91       exprs' <- zipWithM vectTopRhs vars exprs
92       hs     <- takeHoisted
93       return . Rec $ bs ++ zip vars' exprs' ++ hs
94   `orElseV`
95     return b
96   where
97     (vars, exprs) = unzip bs
98
99 vectTopBinder :: Var -> VM Var
100 vectTopBinder var
101   = do
102       vty  <- vectType (idType var)
103       var' <- cloneId mkVectOcc var vty
104       defGlobalVar var var'
105       return var'
106     
107 vectTopRhs :: Var -> CoreExpr -> VM CoreExpr
108 vectTopRhs var expr
109   = do
110       closedV . liftM vectorised
111               . inBind var
112               $ vectPolyExpr (freeVars expr)
113
114 -- ----------------------------------------------------------------------------
115 -- Bindings
116
117 vectBndr :: Var -> VM VVar
118 vectBndr v
119   = do
120       vty <- vectType (idType v)
121       lty <- mkPArrayType vty
122       let vv = v `Id.setIdType` vty
123           lv = v `Id.setIdType` lty
124       updLEnv (mapTo vv lv)
125       return (vv, lv)
126   where
127     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
128
129 vectBndrIn :: Var -> VM a -> VM (VVar, a)
130 vectBndrIn v p
131   = localV
132   $ do
133       vv <- vectBndr v
134       x <- p
135       return (vv, x)
136
137 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
138 vectBndrIn' v p
139   = localV
140   $ do
141       vv <- vectBndr v
142       x  <- p vv
143       return (vv, x)
144
145 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
146 vectBndrsIn vs p
147   = localV
148   $ do
149       vvs <- mapM vectBndr vs
150       x <- p
151       return (vvs, x)
152
153 -- ----------------------------------------------------------------------------
154 -- Expressions
155
156 vectVar :: Var -> VM VExpr
157 vectVar v
158   = do
159       r <- lookupVar v
160       case r of
161         Local (vv,lv) -> return (Var vv, Var lv)
162         Global vv     -> do
163                            let vexpr = Var vv
164                            lexpr <- liftPA vexpr
165                            return (vexpr, lexpr)
166
167 vectPolyVar :: Var -> [Type] -> VM VExpr
168 vectPolyVar v tys
169   = do
170       vtys <- mapM vectType tys
171       r <- lookupVar v
172       case r of
173         Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
174                                      (polyApply (Var lv) vtys)
175         Global poly    -> do
176                             vexpr <- polyApply (Var poly) vtys
177                             lexpr <- liftPA vexpr
178                             return (vexpr, lexpr)
179
180 vectLiteral :: Literal -> VM VExpr
181 vectLiteral lit
182   = do
183       lexpr <- liftPA (Lit lit)
184       return (Lit lit, lexpr)
185
186 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
187 vectPolyExpr expr
188   = polyAbstract tvs $ \abstract ->
189     do
190       mono' <- vectExpr mono
191       return $ mapVect abstract mono'
192   where
193     (tvs, mono) = collectAnnTypeBinders expr  
194                 
195 vectExpr :: CoreExprWithFVs -> VM VExpr
196 vectExpr (_, AnnType ty)
197   = liftM vType (vectType ty)
198
199 vectExpr (_, AnnVar v) = vectVar v
200
201 vectExpr (_, AnnLit lit) = vectLiteral lit
202
203 vectExpr (_, AnnNote note expr)
204   = liftM (vNote note) (vectExpr expr)
205
206 vectExpr e@(_, AnnApp _ arg)
207   | isAnnTypeArg arg
208   = vectTyAppExpr fn tys
209   where
210     (fn, tys) = collectAnnTypeArgs e
211
212 vectExpr (_, AnnApp fn arg)
213   = do
214       arg_ty' <- vectType arg_ty
215       res_ty' <- vectType res_ty
216       fn'     <- vectExpr fn
217       arg'    <- vectExpr arg
218       mkClosureApp arg_ty' res_ty' fn' arg'
219   where
220     (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
221
222 vectExpr (_, AnnCase scrut bndr ty alts)
223   | isAlgType scrut_ty
224   = vectAlgCase scrut bndr ty alts
225   where
226     scrut_ty = exprType (deAnnotate scrut)
227
228 vectExpr (_, AnnCase expr bndr ty alts)
229   = panic "vectExpr: case"
230
231 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
232   = do
233       vrhs <- localV . inBind bndr $ vectPolyExpr rhs
234       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
235       return $ vLet (vNonRec vbndr vrhs) vbody
236
237 vectExpr (_, AnnLet (AnnRec bs) body)
238   = do
239       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
240                                 $ liftM2 (,)
241                                   (zipWithM vect_rhs bndrs rhss)
242                                   (vectPolyExpr body)
243       return $ vLet (vRec vbndrs vrhss) vbody
244   where
245     (bndrs, rhss) = unzip bs
246
247     vect_rhs bndr rhs = localV
248                       . inBind bndr
249                       $ vectExpr rhs
250
251 vectExpr e@(fvs, AnnLam bndr _)
252   | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
253   | otherwise = vectLam fvs bs body
254   where
255     (bs,body) = collectAnnValBinders e
256
257 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
258 vectLam fvs bs body
259   = do
260       tyvars <- localTyVars
261       (vs, vvs) <- readLEnv $ \env ->
262                    unzip [(var, vv) | var <- varSetElems fvs
263                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
264
265       arg_tys <- mapM (vectType . idType) bs
266       res_ty  <- vectType (exprType $ deAnnotate body)
267
268       buildClosures tyvars vvs arg_tys res_ty
269         . hoistPolyVExpr tyvars
270         $ do
271             lc <- builtin liftingContext
272             (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
273                                            (vectExpr body)
274             return $ vLams lc vbndrs vbody
275   
276 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
277 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
278 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
279
280 type CoreAltWithFVs = AnnAlt Id VarSet
281
282 -- We convert
283 --
284 --   case e :: t of v { ... }
285 --
286 -- to
287 --
288 --   V:    let v = e in case v of _ { ... }
289 --   L:    let v = e in case v `cast` ... of _ { ... }
290 --
291 -- When lifting, we have to do it this way because v must have the type
292 -- [:V(T):] but the scrutinee must be cast to the representation type.
293 --   
294
295 -- FIXME: this is too lazy
296 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
297   = do
298       vscrut <- vectExpr scrut
299       vty    <- vectType ty
300       lty    <- mkPArrayType vty
301       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
302       return $ vCaseDEFAULT vscrut vbndr vty lty vbody
303
304 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
305   = do
306       vty <- vectType ty
307       lty <- mkPArrayType vty
308       vexpr <- vectExpr scrut
309       (vbndr, (vbndrs, vbody)) <- vectBndrIn bndr
310                                 . vectBndrsIn bndrs
311                                 $ vectExpr body
312
313       (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
314       vect_dc <- maybeV (lookupDataCon dc)
315       let [arr_dc] = tyConDataCons arr_tc
316       let shape_tys = take (dataConRepArity arr_dc - length bndrs)
317                            (dataConRepArgTys arr_dc)
318       shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
319       return . vLet (vNonRec vbndr vexpr)
320              $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody