Nicer names for hoisted functions
[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, zipWithM, 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 var 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' <- zipWithM vectTopRhs vars 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 :: Var -> CoreExpr -> VM CoreExpr
112 vectTopRhs var expr
113   = do
114       lc <- newLocalVar FSLIT("lc") intPrimTy
115       closedV . liftM vectorised
116               . inBind var
117               $ vectPolyExpr lc (freeVars expr)
118
119 -- ----------------------------------------------------------------------------
120 -- Bindings
121
122 vectBndr :: Var -> VM VVar
123 vectBndr v
124   = do
125       vty <- vectType (idType v)
126       lty <- mkPArrayType vty
127       let vv = v `Id.setIdType` vty
128           lv = v `Id.setIdType` lty
129       updLEnv (mapTo vv lv)
130       return (vv, lv)
131   where
132     mapTo vv lv env = env { local_vars = extendVarEnv (local_vars env) v (vv, lv) }
133
134 vectBndrIn :: Var -> VM a -> VM (VVar, a)
135 vectBndrIn v p
136   = localV
137   $ do
138       vv <- vectBndr v
139       x <- p
140       return (vv, x)
141
142 vectBndrsIn :: [Var] -> VM a -> VM ([VVar], a)
143 vectBndrsIn vs p
144   = localV
145   $ do
146       vvs <- mapM vectBndr vs
147       x <- p
148       return (vvs, x)
149
150 -- ----------------------------------------------------------------------------
151 -- Expressions
152
153 capply :: VExpr -> VExpr -> VM VExpr
154 capply (vfn, lfn) (varg, larg)
155   = do
156       apply  <- builtin applyClosureVar
157       applyP <- builtin applyClosurePVar
158       return (mkApps (Var apply)  [Type arg_ty, Type res_ty, vfn, varg],
159               mkApps (Var applyP) [Type arg_ty, Type res_ty, lfn, larg])
160   where
161     fn_ty            = exprType vfn
162     (arg_ty, res_ty) = splitClosureTy fn_ty
163
164 vectVar :: Var -> Var -> VM VExpr
165 vectVar lc v
166   = do
167       r <- lookupVar v
168       case r of
169         Local (vv,lv) -> return (Var vv, Var lv)
170         Global vv     -> do
171                            let vexpr = Var vv
172                            lexpr <- replicatePA vexpr (Var lc)
173                            return (vexpr, lexpr)
174
175 vectPolyVar :: Var -> Var -> [Type] -> VM VExpr
176 vectPolyVar lc v tys
177   = do
178       vtys <- mapM vectType tys
179       r <- lookupVar v
180       case r of
181         Local (vv, lv) -> liftM2 (,) (polyApply (Var vv) vtys)
182                                      (polyApply (Var lv) vtys)
183         Global poly    -> do
184                             vexpr <- polyApply (Var poly) vtys
185                             lexpr <- replicatePA vexpr (Var lc)
186                             return (vexpr, lexpr)
187
188 vectLiteral :: Var -> Literal -> VM VExpr
189 vectLiteral lc lit
190   = do
191       lexpr <- replicatePA (Lit lit) (Var lc)
192       return (Lit lit, lexpr)
193
194 vectPolyExpr :: Var -> CoreExprWithFVs -> VM VExpr
195 vectPolyExpr lc expr
196   = polyAbstract tvs $ \abstract ->
197     -- FIXME: shadowing (tvs in lc)
198     do
199       mono' <- vectExpr lc mono
200       return $ mapVect abstract mono'
201   where
202     (tvs, mono) = collectAnnTypeBinders expr  
203                 
204 vectExpr :: Var -> CoreExprWithFVs -> VM VExpr
205 vectExpr lc (_, AnnType ty)
206   = liftM vType (vectType ty)
207
208 vectExpr lc (_, AnnVar v) = vectVar lc v
209
210 vectExpr lc (_, AnnLit lit) = vectLiteral lc lit
211
212 vectExpr lc (_, AnnNote note expr)
213   = liftM (vNote note) (vectExpr lc expr)
214
215 vectExpr lc e@(_, AnnApp _ arg)
216   | isAnnTypeArg arg
217   = vectTyAppExpr lc fn tys
218   where
219     (fn, tys) = collectAnnTypeArgs e
220
221 vectExpr lc (_, AnnApp fn arg)
222   = do
223       fn'  <- vectExpr lc fn
224       arg' <- vectExpr lc arg
225       capply fn' arg'
226
227 vectExpr lc (_, AnnCase expr bndr ty alts)
228   = panic "vectExpr: case"
229
230 vectExpr lc (_, AnnLet (AnnNonRec bndr rhs) body)
231   = do
232       vrhs <- localV . inBind bndr $ vectPolyExpr lc rhs
233       (vbndr, vbody) <- vectBndrIn bndr (vectExpr lc body)
234       return $ vLet (vNonRec vbndr vrhs) vbody
235
236 vectExpr lc (_, AnnLet (AnnRec bs) body)
237   = do
238       (vbndrs, (vrhss, vbody)) <- vectBndrsIn bndrs
239                                 $ liftM2 (,)
240                                   (zipWithM vect_rhs bndrs rhss)
241                                   (vectPolyExpr lc body)
242       return $ vLet (vRec vbndrs vrhss) vbody
243   where
244     (bndrs, rhss) = unzip bs
245
246     vect_rhs bndr rhs = localV
247                       . inBind bndr
248                       $ vectExpr lc rhs
249
250 vectExpr lc e@(fvs, AnnLam bndr _)
251   | not (isId bndr) = pprPanic "vectExpr" (ppr $ deAnnotate e)
252   | otherwise = vectLam lc fvs bs body
253   where
254     (bs,body) = collectAnnValBinders e
255
256 vectLam :: Var -> VarSet -> [Var] -> CoreExprWithFVs -> VM VExpr
257 vectLam lc fvs bs body
258   = do
259       tyvars <- localTyVars
260       (vs, vvs) <- readLEnv $ \env ->
261                    unzip [(var, vv) | var <- varSetElems fvs
262                                     , Just vv <- [lookupVarEnv (local_vars env) var]]
263
264       arg_tys <- mapM (vectType . idType) bs
265       res_ty  <- vectType (exprType $ deAnnotate body)
266
267       buildClosures tyvars lc vvs arg_tys res_ty
268         . hoistPolyVExpr tyvars
269         $ do
270             new_lc <- newLocalVar FSLIT("lc") intPrimTy
271             (vbndrs, vbody) <- vectBndrsIn (vs ++ bs)
272                                            (vectExpr new_lc body)
273             return $ vLams new_lc vbndrs vbody
274   
275 vectTyAppExpr :: Var -> CoreExprWithFVs -> [Type] -> VM (CoreExpr, CoreExpr)
276 vectTyAppExpr lc (_, AnnVar v) tys = vectPolyVar lc v tys
277 vectTyAppExpr lc e tys = pprPanic "vectTyAppExpr" (ppr $ deAnnotate e)
278