Use new closure generation code in vectorisation
[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                 ( mkSysTvName, getName )
29 import NameEnv
30 import Id
31 import MkId                 ( unwrapFamInstScrut )
32 import OccName
33
34 import DsMonad hiding (mapAndUnzipM)
35 import DsUtils              ( mkCoreTup, mkCoreTupTy )
36
37 import Literal              ( Literal )
38 import PrelNames
39 import TysWiredIn
40 import TysPrim              ( intPrimTy )
41 import BasicTypes           ( Boxity(..) )
42
43 import Outputable
44 import FastString
45 import Control.Monad        ( liftM, liftM2, mapAndUnzipM )
46
47 vectorise :: HscEnv -> UniqSupply -> RuleBase -> ModGuts
48           -> IO (SimplCount, ModGuts)
49 vectorise hsc_env _ _ guts
50   = do
51       showPass dflags "Vectorisation"
52       eps <- hscEPS hsc_env
53       let info = hptVectInfo hsc_env `plusVectInfo` eps_vect_info eps
54       Just (info', guts') <- initV hsc_env guts info (vectModule guts)
55       endPass dflags "Vectorisation" Opt_D_dump_vect (mg_binds guts')
56       return (zeroSimplCount dflags, guts' { mg_vect_info = info' })
57   where
58     dflags = hsc_dflags hsc_env
59
60 vectModule :: ModGuts -> VM ModGuts
61 vectModule guts
62   = do
63       (types', fam_insts, pa_insts) <- vectTypeEnv (mg_types guts)
64       
65       let insts         = map painstInstance pa_insts
66           fam_inst_env' = extendFamInstEnvList (mg_fam_inst_env guts) fam_insts
67           inst_env'     = extendInstEnvList (mg_inst_env guts) insts
68       updGEnv (setInstEnvs inst_env' fam_inst_env')
69      
70       dicts  <- mapM buildPADict pa_insts 
71       binds' <- mapM vectTopBind (mg_binds guts)
72       return $ guts { mg_types        = types'
73                     , mg_binds        = Rec (concat dicts) : binds'
74                     , mg_inst_env     = inst_env'
75                     , mg_fam_inst_env = fam_inst_env'
76                     , mg_insts        = mg_insts guts ++ insts
77                     , mg_fam_insts    = mg_fam_insts guts ++ fam_insts
78                     }
79
80 vectTopBind :: CoreBind -> VM CoreBind
81 vectTopBind b@(NonRec var expr)
82   = do
83       var'  <- vectTopBinder var
84       expr' <- vectTopRhs expr
85       hs    <- takeHoisted
86       return . Rec $ (var, expr) : (var', expr') : hs
87   `orElseV`
88     return b
89
90 vectTopBind b@(Rec bs)
91   = do
92       vars'  <- mapM vectTopBinder vars
93       exprs' <- mapM vectTopRhs exprs
94       hs     <- takeHoisted
95       return . Rec $ bs ++ zip vars' exprs' ++ hs
96   `orElseV`
97     return b
98   where
99     (vars, exprs) = unzip bs
100
101 vectTopBinder :: Var -> VM Var
102 vectTopBinder var
103   = do
104       vty <- vectType (idType var)
105       name <- cloneName mkVectOcc (getName var)
106       let var' | isExportedId var = Id.mkExportedLocalId name vty
107                | otherwise        = Id.mkLocalId         name vty
108       defGlobalVar var var'
109       return var'
110     
111 vectTopRhs :: CoreExpr -> VM CoreExpr
112 vectTopRhs expr
113   = do
114       lc <- newLocalVar FSLIT("lc") intPrimTy
115       closedV . liftM vectorised
116               $ vectPolyExpr lc (freeVars expr)
117
118 -- ----------------------------------------------------------------------------
119 -- Bindings
120
121 vectBndr :: Var -> VM VVar
122 vectBndr v
123   = do
124       vty <- vectType (idType v)
125       lty <- mkPArrayType vty
126       let vv = v `Id.setIdType` vty
127           lv = v `Id.setIdType` lty
128       updLEnv (mapTo vv lv)
129       return (vv, lv)
130   where
131     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
132
133 vectBndrIn :: Var -> VM a -> VM (VVar, a)
134 vectBndrIn v p
135   = localV
136   $ do
137       vv <- vectBndr v
138       x <- p
139       return (vv, x)
140
141 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
142 vectBndrsIn vs p
143   = localV
144   $ do
145       vvs <- mapM vectBndr vs
146       x <- p
147       return (vvs, x)
148
149 -- ----------------------------------------------------------------------------
150 -- Expressions
151
152 capply :: VExpr -> VExpr -> VM VExpr
153 capply (vfn, lfn) (varg, larg)
154   = do
155       apply  <- builtin applyClosureVar
156       applyP <- builtin applyClosurePVar
157       return (mkApps (Var apply)  [Type arg_ty, Type res_ty, vfn, varg],
158               mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
159   where
160     fn_ty            = exprType vfn
161     (arg_ty, res_ty) = splitClosureTy fn_ty
162
163 vectVar :: Var -> Var -> VM VExpr
164 vectVar lc 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 <- replicatePA vexpr (Var lc)
172                            return (vexpr, lexpr)
173
174 vectPolyVar :: Var -> Var -> [Type] -> VM VExpr
175 vectPolyVar lc 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 <- replicatePA vexpr (Var lc)
185                             return (vexpr, lexpr)
186
187 vectLiteral :: Var -> Literal -> VM VExpr
188 vectLiteral lc lit
189   = do
190       lexpr <- replicatePA (Lit lit) (Var lc)
191       return (Lit lit, lexpr)
192
193 vectPolyExpr :: Var -> CoreExprWithFVs -> VM VExpr
194 vectPolyExpr lc expr
195   = polyAbstract tvs $ \abstract ->
196     -- FIXME: shadowing (tvs in lc)
197     do
198       mono' <- vectExpr lc mono
199       return $ mapVect abstract mono'
200   where
201     (tvs, mono) = collectAnnTypeBinders expr  
202                 
203 vectExpr :: Var -> CoreExprWithFVs -> VM VExpr
204 vectExpr lc (_, AnnType ty)
205   = liftM vType (vectType ty)
206
207 vectExpr lc (_, AnnVar v) = vectVar lc v
208
209 vectExpr lc (_, AnnLit lit) = vectLiteral lc lit
210
211 vectExpr lc (_, AnnNote note expr)
212   = liftM (vNote note) (vectExpr lc expr)
213
214 vectExpr lc e@(_, AnnApp _ arg)
215   | isAnnTypeArg arg
216   = vectTyAppExpr lc fn tys
217   where
218     (fn, tys) = collectAnnTypeArgs e
219
220 vectExpr lc (_, AnnApp fn arg)
221   = do
222       fn'  <- vectExpr lc fn
223       arg' <- vectExpr lc arg
224       capply fn' arg'
225
226 vectExpr lc (_, AnnCase expr bndr ty alts)
227   = panic "vectExpr: case"
228
229 vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
230   = do
231       vrhs <- vectPolyExpr lc rhs
232       (vbndr, vbody) <- vectBndrIn bndr (vectExpr lc body)
233       return $ vLet (vNonRec vbndr vrhs) vbody
234
235 vectExpr lc (_, AnnLet (AnnRec bs) body)
236   = do
237       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
238                                 $ liftM2 (,)
239                                   (mapM (vectExpr lc) rhss)
240                                   (vectPolyExpr lc body)
241       return $ vLet (vRec vbndrs vrhss) vbody
242   where
243     (bndrs, rhss) = unzip bs
244
245 vectExpr lc e@(_, AnnLam bndr body)
246   | isTyVar bndr = pprPanic "vectExpr" (ppr $ deAnnotate e)
247
248 vectExpr lc (fvs, AnnLam bndr body)
249   = do
250       tyvars <- localTyVars
251       (vs, vvs) <- readLEnv $ \env ->
252                    unzip [(var, vv) | var <- varSetElems fvs
253                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
254
255       arg_ty <- vectType (idType bndr)
256       res_ty <- vectType (exprType $ deAnnotate body)
257       buildClosure tyvars lc vvs arg_ty res_ty
258         . hoistPolyVExpr FSLIT("fn") tyvars
259         $ do
260             new_lc <- newLocalVar FSLIT("lc") intPrimTy
261             (vbndrs, vbody) <- vectBndrsIn (vs ++ [bndr])
262                                            (vectExpr new_lc body)
263             return $ vLams new_lc vbndrs vbody
264
265 vectTyAppExpr :: Var -> CoreExprWithFVs -> [Type] -> VM (CoreExpr, CoreExpr)
266 vectTyAppExpr lc (_, AnnVar v) tys = vectPolyVar lc v tys
267 vectTyAppExpr lc e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
268