3 -- Copyright (c) 2002 Manuel M T Chakravarty & Gabriele Keller
5 -- Analysis phase for an optimised flattening transformation
7 --- DESCRIPTION ---------------------------------------------------------------
9 -- This module implements an analysis phase that identifies Core expressions
10 -- that need not be transformed during flattening. The expressions when
11 -- executed in a parallel context are implemented as an iteration over the
12 -- original scalar computation, instead of vectorising the computation. This
13 -- usually improves efficiency by increasing locality and also reduces code
16 --- DOCU ----------------------------------------------------------------------
18 -- Language: Haskell 98 with C preprocessor
20 -- Analyse the expression and annotate each simple subexpression accordingly.
22 -- The result of the analysis is stored in a new field in IdInfo (has yet to
25 -- A simple expression is any expression which is not a function, not of
26 -- recursive type and does not contain a value of PArray type. Polymorphic
27 -- variables are simple expressions even though they might be instantiated to
28 -- a parray value or function.
30 --- TODO ----------------------------------------------------------------------
34 markScalarExprs -- :: [CoreBind] -> [CoreBind]
38 import Outputable (pprPanic, ppr)
39 import CoreSyn (CoreBind)
41 import TypeRep (Type(..))
42 import Var (Var(..),Id)
43 import Literal (Literal)
44 import CoreSyn (Expr(..),CoreExpr,Bind(..))
45 import PprCore ( {- instances -} )
48 data ArrayUsage = Prim | NonPrim | Array
49 | PolyExpr (Id -> Maybe (ArrayUsage -> ArrayUsage))
50 | PolyFun (ArrayUsage -> ArrayUsage)
53 arrUsage:: CoreExpr -> ArrayUsage
54 arrUsage (Var id) = varArrayUsage id
55 arrUsage (Lit lit) = litArrayUsage lit
56 arrUsage (App expr1 expr2) =
63 (PolyFun f, _) -> f arr2
66 arrUsage (Lam b expr) =
69 arrUsage (Let (NonRec b expr1) expr2) =
70 arrUsage (App (Lam b expr2) expr1)
72 arrUsage (Let (Rec bnds) expr) =
74 t1 = foldr combineArrayUsage Prim (map bindType bnds)
76 in if isArrayUsage t1 then Array else t2
78 arrUsage (Case expr b alts) =
81 t2 = scanType (map (arrUsage . (\ (_,_,x) -> x)) alts)
84 arrUsage (Note n expr) =
94 in case (bT, exprT) of
98 scanType:: [ArrayUsage] -> ArrayUsage
100 scanType (Array:ts) = Array
101 scanType (_:ts) = scanType ts
105 -- the code expression represents a built-in function which generates
107 isArrayGen:: CoreExpr -> Bool
109 panic "PArrAnal: isArrayGen: not yet implemented"
111 isArrayCon:: CoreExpr -> Bool
113 panic "PArrAnal: isArrayCon: not yet implemented"
115 markScalarExprs:: [CoreBind] -> [CoreBind]
117 panic "PArrAnal.markScalarExprs: not implemented yet"
120 varArrayUsage:: Id -> ArrayUsage
122 panic "PArrAnal.varArrayUsage: not yet implented"
124 litArrayUsage:: Literal -> ArrayUsage
126 panic "PArrAnal.litArrayUsage: not yet implented"
129 typeArrayUsage:: Type -> ArrayUsage
130 typeArrayUsage (TyVarTy tvar) =
131 PolyExpr (tIdFun tvar)
132 typeArrayUsage (AppTy _ _) =
133 panic "PArrAnal.typeArrayUsage: AppTy case not yet implemented"
134 typeArrayUsage (TyConApp tc tcargs) =
136 tcargsAU = map typeArrayUsage tcargs
137 tcCombine = foldr combineArrayUsage Prim tcargsAU
139 typeArrayUsage t@(PredTy _) =
140 pprPanic "PArrAnal.typeArrayUsage: encountered 'PredType - shouldn't be here!"
144 combineArrayUsage:: ArrayUsage -> ArrayUsage -> ArrayUsage
145 combineArrayUsage Array _ = Array
146 combineArrayUsage _ Array = Array
147 combineArrayUsage (PolyExpr f1) (PolyExpr f2) =
155 case (f1lookup, f2lookup) of
156 (Nothing, _) -> f2lookup
157 (_, Nothing) -> f1lookup
158 (Just f1', Just f2') -> Just ( \e -> (combineArrayUsage (f1' e) (f2' e)))
159 combineArrayUsage (PolyFun f) (PolyExpr g) =
160 panic ("PArrAnal.typeArrayUsage: PolyFun as argument in data" ++
161 " constructor - should not (?) happen\n")
162 combineArrayUsage (PolyExpr g) (PolyFun f) =
163 panic ("PArrAnal.typeArrayUsage: PolyFun as argument in data" ++
164 " constructor - should not (?) happen\n")
165 combineArrayUsage NonPrim _ = NonPrim
166 combineArrayUsage _ NonPrim = NonPrim
167 combineArrayUsage Prim Prim = Prim
170 isArrayUsage:: ArrayUsage -> Bool
171 isArrayUsage Array = True
172 isArrayUsage _ = False
174 -- Functions to serve as arguments for PolyExpr
175 -- ---------------------------------------------
177 tIdFun:: Var -> Var -> Maybe (ArrayUsage -> ArrayUsage)
184 -- Functions to serve as argument for PolyFun
185 -- -------------------------------------------
187 auId:: ArrayUsage -> ArrayUsage
190 auCon:: ArrayUsage -> ArrayUsage
192 auCon (PolyExpr f) = PolyExpr f'
193 where f' v = case f v of
195 Just g -> Just ( \e -> (auCon (g e)))
196 auCon (PolyFun f) = PolyFun (auCon . f)
199 -- traversal of Core expressions
200 -- -----------------------------