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