39341ef1795b9b04239d4925587d59a056648c4b
[ghc-hetmet.git] / compiler / vectorise / VectCore.hs
1
2 -- | Simple vectorised constructors and projections.
3 module VectCore (
4   Vect, VVar, VExpr, VBind,
5
6   vectorised, lifted,
7   mapVect,
8
9   vVarType,
10
11   vNonRec, vRec,
12
13   vVar, vType, vNote, vLet,
14   vLams, vLamsWithoutLC, vVarApps,
15   vCaseDEFAULT
16 ) where
17
18 #include "HsVersions.h"
19
20 import CoreSyn
21 import Type           ( Type )
22 import Var
23
24 -- | Contains the vectorised and lifted versions of some thing.
25 type Vect a = (a,a)
26 type VVar   = Vect Var
27 type VExpr  = Vect CoreExpr
28 type VBind  = Vect CoreBind
29
30
31 -- | Get the vectorised version of a thing.
32 vectorised :: Vect a -> a
33 vectorised = fst
34
35
36 -- | Get the lifted version of a thing.
37 lifted :: Vect a -> a
38 lifted = snd
39
40
41 -- | Apply some function to both the vectorised and lifted versions of a thing.
42 mapVect :: (a -> b) -> Vect a -> Vect b
43 mapVect f (x,y) = (f x, f y)
44
45
46 -- | Combine vectorised and lifted versions of two things componentwise.
47 zipWithVect :: (a -> b -> c) -> Vect a -> Vect b -> Vect c
48 zipWithVect f (x1,y1) (x2,y2) = (f x1 x2, f y1 y2)
49
50
51 -- | Get the type of a vectorised variable.
52 vVarType :: VVar -> Type
53 vVarType = varType . vectorised
54
55
56 -- | Wrap a vectorised variable as a vectorised expression.
57 vVar :: VVar -> VExpr
58 vVar = mapVect Var
59
60
61 -- | Wrap a vectorised type as a vectorised expression.
62 vType :: Type -> VExpr
63 vType ty = (Type ty, Type ty)
64
65
66 -- | Make a vectorised note.
67 vNote :: Note -> VExpr -> VExpr
68 vNote = mapVect . Note
69
70
71 -- | Make a vectorised non-recursive binding.
72 vNonRec :: VVar -> VExpr -> VBind
73 vNonRec = zipWithVect NonRec
74
75
76 -- | Make a vectorised recursive binding.
77 vRec :: [VVar] -> [VExpr] -> VBind
78 vRec vs es = (Rec (zip vvs ves), Rec (zip lvs les))
79   where
80     (vvs, lvs) = unzip vs
81     (ves, les) = unzip es
82
83
84 -- | Make a vectorised let expresion.
85 vLet :: VBind -> VExpr -> VExpr
86 vLet = zipWithVect Let
87
88
89 -- | Make a vectorised lambda abstraction.
90 --   The lifted version also binds the lifting context.
91 vLams   :: Var          -- ^ Var bound to the lifting context.
92         -> [VVar]       -- ^ Parameter vars for the abstraction.
93         -> VExpr        -- ^ Body of the abstraction.
94         -> VExpr
95
96 vLams lc vs (ve, le) 
97   = (mkLams vvs ve, mkLams (lc:lvs) le)
98   where
99     (vvs,lvs) = unzip vs
100
101
102 -- | Like `vLams` but the lifted version doesn't bind the lifting context.
103 vLamsWithoutLC :: [VVar] -> VExpr -> VExpr
104 vLamsWithoutLC vvs (ve,le) 
105   = (mkLams vs ve, mkLams ls le)
106   where
107     (vs,ls) = unzip vvs
108
109
110 -- | Apply some argument variables to an expression.
111 --   The lifted version is also applied to the variable of the lifting context.
112 vVarApps :: Var -> VExpr -> [VVar] -> VExpr
113 vVarApps lc (ve, le) vvs 
114   = (ve `mkVarApps` vs, le `mkVarApps` (lc : ls))
115   where
116     (vs,ls) = unzip vvs 
117
118
119 vCaseDEFAULT
120         :: VExpr        -- scrutiniy
121         -> VVar         -- bnder
122         -> Type         -- type of vectorised version
123         -> Type         -- type of lifted version
124         -> VExpr        -- body of alternative.
125         -> VExpr
126
127 vCaseDEFAULT (vscrut, lscrut) (vbndr, lbndr) vty lty (vbody, lbody)
128   = (Case vscrut vbndr vty (mkDEFAULT vbody),
129      Case lscrut lbndr lty (mkDEFAULT lbody))
130   where
131     mkDEFAULT e = [(DEFAULT, [], e)]
132