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