Fix vectorisation of binders in case expressions
[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/Commentary/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 vectBndrNew :: Var -> FastString -> VM VVar
137 vectBndrNew v fs
138   = do
139       vty <- vectType (idType v)
140       vv  <- newLocalVVar fs vty
141       updLEnv (upd vv)
142       return vv
143   where
144     upd vv env = env { local_vars = extendVarEnv (local_vars env) v vv }
145
146 vectBndrIn :: Var -> VM a -> VM (VVar, a)
147 vectBndrIn v p
148   = localV
149   $ do
150       vv <- vectBndr v
151       x <- p
152       return (vv, x)
153
154 vectBndrNewIn :: Var -> FastString -> VM a -> VM (VVar, a)
155 vectBndrNewIn v fs p
156   = localV
157   $ do
158       vv <- vectBndrNew v fs
159       x  <- p
160       return (vv, x)
161
162 vectBndrIn' :: Var -> (VVar -> VM a) -> VM (VVar, a)
163 vectBndrIn' v p
164   = localV
165   $ do
166       vv <- vectBndr v
167       x  <- p vv
168       return (vv, x)
169
170 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
171 vectBndrsIn vs p
172   = localV
173   $ do
174       vvs <- mapM vectBndr vs
175       x <- p
176       return (vvs, x)
177
178 -- ----------------------------------------------------------------------------
179 -- Expressions
180
181 vectVar :: Var -> VM VExpr
182 vectVar v
183   = do
184       r <- lookupVar v
185       case r of
186         Local (vv,lv) -> return (Var vv, Var lv)
187         Global vv     -> do
188                            let vexpr = Var vv
189                            lexpr <- liftPA vexpr
190                            return (vexpr, lexpr)
191
192 vectPolyVar :: Var -> [Type] -> VM VExpr
193 vectPolyVar v tys
194   = do
195       vtys <- mapM vectType tys
196       r <- lookupVar v
197       case r of
198         Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
199                                      (polyApply (Var lv) vtys)
200         Global poly    -> do
201                             vexpr <- polyApply (Var poly) vtys
202                             lexpr <- liftPA vexpr
203                             return (vexpr, lexpr)
204
205 vectLiteral :: Literal -> VM VExpr
206 vectLiteral lit
207   = do
208       lexpr <- liftPA (Lit lit)
209       return (Lit lit, lexpr)
210
211 vectPolyExpr :: CoreExprWithFVs -> VM VExpr
212 vectPolyExpr expr
213   = polyAbstract tvs $ \abstract ->
214     do
215       mono' <- vectExpr mono
216       return $ mapVect abstract mono'
217   where
218     (tvs, mono) = collectAnnTypeBinders expr  
219                 
220 vectExpr :: CoreExprWithFVs -> VM VExpr
221 vectExpr (_, AnnType ty)
222   = liftM vType (vectType ty)
223
224 vectExpr (_, AnnVar v) = vectVar v
225
226 vectExpr (_, AnnLit lit) = vectLiteral lit
227
228 vectExpr (_, AnnNote note expr)
229   = liftM (vNote note) (vectExpr expr)
230
231 vectExpr e@(_, AnnApp _ arg)
232   | isAnnTypeArg arg
233   = vectTyAppExpr fn tys
234   where
235     (fn, tys) = collectAnnTypeArgs e
236
237 vectExpr (_, AnnApp fn arg)
238   = do
239       arg_ty' <- vectType arg_ty
240       res_ty' <- vectType res_ty
241       fn'     <- vectExpr fn
242       arg'    <- vectExpr arg
243       mkClosureApp arg_ty' res_ty' fn' arg'
244   where
245     (arg_ty, res_ty) = splitFunTy . exprType $ deAnnotate fn
246
247 vectExpr (_, AnnCase scrut bndr ty alts)
248   | isAlgType scrut_ty
249   = vectAlgCase scrut bndr ty alts
250   where
251     scrut_ty = exprType (deAnnotate scrut)
252
253 vectExpr (_, AnnCase expr bndr ty alts)
254   = panic "vectExpr: case"
255
256 vectExpr (_, AnnLet (AnnNonRec bndr rhs) body)
257   = do
258       vrhs <- localV . inBind bndr $ vectPolyExpr rhs
259       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
260       return $ vLet (vNonRec vbndr vrhs) vbody
261
262 vectExpr (_, AnnLet (AnnRec bs) body)
263   = do
264       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
265                                 $ liftM2 (,)
266                                   (zipWithM vect_rhs bndrs rhss)
267                                   (vectPolyExpr body)
268       return $ vLet (vRec vbndrs vrhss) vbody
269   where
270     (bndrs, rhss) = unzip bs
271
272     vect_rhs bndr rhs = localV
273                       . inBind bndr
274                       $ vectExpr rhs
275
276 vectExpr e@(fvs, AnnLam bndr _)
277   | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
278   | otherwise = vectLam fvs bs body
279   where
280     (bs,body) = collectAnnValBinders e
281
282 vectLam :: VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
283 vectLam fvs bs body
284   = do
285       tyvars <- localTyVars
286       (vs, vvs) <- readLEnv $ \env ->
287                    unzip [(var, vv) | var <- varSetElems fvs
288                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
289
290       arg_tys <- mapM (vectType . idType) bs
291       res_ty  <- vectType (exprType $ deAnnotate body)
292
293       buildClosures tyvars vvs arg_tys res_ty
294         . hoistPolyVExpr tyvars
295         $ do
296             lc <- builtin liftingContext
297             (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
298                                            (vectExpr body)
299             return $ vLams lc vbndrs vbody
300   
301 vectTyAppExpr :: CoreExprWithFVs -> [Type] -> VM VExpr
302 vectTyAppExpr (_, AnnVar v) tys = vectPolyVar v tys
303 vectTyAppExpr e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
304
305 type CoreAltWithFVs = AnnAlt Id VarSet
306
307 -- We convert
308 --
309 --   case e :: t of v { ... }
310 --
311 -- to
312 --
313 --   V:    let v' = e in case v' of _ { ... }
314 --   L:    let v' = e in case v' `cast` ... of _ { ... }
315 --
316 -- When lifting, we have to do it this way because v must have the type
317 -- [:V(T):] but the scrutinee must be cast to the representation type. We also
318 -- have to handle the case where v is a wild var correctly.
319 --   
320
321 -- FIXME: this is too lazy
322 vectAlgCase scrut bndr ty [(DEFAULT, [], body)]
323   = do
324       vscrut <- vectExpr scrut
325       vty    <- vectType ty
326       lty    <- mkPArrayType vty
327       (vbndr, vbody) <- vectBndrIn bndr (vectExpr body)
328       return $ vCaseDEFAULT vscrut vbndr vty lty vbody
329
330 vectAlgCase scrut bndr ty [(DataAlt dc, bndrs, body)]
331   = do
332       vty <- vectType ty
333       lty <- mkPArrayType vty
334       vexpr <- vectExpr scrut
335       (vbndr, (vbndrs, vbody)) <- vect_scrut_bndr
336                                 . vectBndrsIn bndrs
337                                 $ vectExpr body
338
339       (vscrut, arr_tc, arg_tys) <- mkVScrut (vVar vbndr)
340       vect_dc <- maybeV (lookupDataCon dc)
341       let [arr_dc] = tyConDataCons arr_tc
342       let shape_tys = take (dataConRepArity arr_dc - length bndrs)
343                            (dataConRepArgTys arr_dc)
344       shape_bndrs <- mapM (newLocalVar FSLIT("s")) shape_tys
345       return . vLet (vNonRec vbndr vexpr)
346              $ vCaseProd vscrut vty lty vect_dc arr_dc shape_bndrs vbndrs vbody
347   where
348     vect_scrut_bndr | isDeadBinder bndr = vectBndrNewIn bndr FSLIT("scrut")
349                     | otherwise         = vectBndrIn bndr
350