2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 %************************************************************************
6 \section[SATMonad]{The Static Argument Transformation pass Monad}
8 %************************************************************************
10 96/03: We aren't using the static-argument transformation right now.
15 #include "HsVersions.h"
19 junk_from_SATMonad = panic "SATMonad.junk"
21 {- LATER: to end of file:
24 SATInfo(..), updSAEnv,
25 SatM(..), initSAT, emptyEnvSAT,
26 returnSAT, thenSAT, thenSAT_, mapSAT, getSATInfo, newSATName,
27 getArgLists, Arg(..), insSAEnv, saTransform,
29 SATEnv(..), isStatic, dropStatics
32 import Type ( mkTyVarTy, mkSigmaTy, TyVarTemplate,
33 splitSigmaTy, splitFunTys,
34 glueTyArgs, instantiateTy, TauType,
35 Class, ThetaType, SigmaType,
38 import Id ( mkSysLocal, idType )
39 import SrcLoc ( SrcLoc, noSrcLoc )
43 infixr 9 `thenSAT`, `thenSAT_`
46 %************************************************************************
48 \subsection{Static Argument Transformation Environment}
50 %************************************************************************
53 type SATEnv = IdEnv SATInfo
55 type SATInfo = ([Arg Type],[Arg Id])
57 data Arg a = Static a | NotStatic
60 delOneFromSAEnv v us env
61 = ((), delOneFromIdEnv env v)
63 updSAEnv :: Maybe (Id,SATInfo) -> SatM ()
66 updSAEnv (Just (b,(tyargs,args)))
67 = getSATInfo b `thenSAT` (\ r ->
69 Nothing -> returnSAT ()
70 Just (tyargs',args') -> delOneFromSAEnv b `thenSAT_`
71 insSAEnv b (checkArgs tyargs tyargs',
75 checkArgs as [] = notStatics (length as)
76 checkArgs [] as = notStatics (length as)
77 checkArgs (a:as) (a':as') | a == a' = a:checkArgs as as'
78 checkArgs (_:as) (_:as') = NotStatic:checkArgs as as'
80 notStatics :: Int -> [Arg a]
81 notStatics n = nOfThem n NotStatic
83 insSAEnv :: Id -> SATInfo -> SatM ()
84 insSAEnv b info us env
85 = ((), addOneToIdEnv env b info)
88 %************************************************************************
90 \subsection{Static Argument Transformation Monad}
92 %************************************************************************
94 Two items of state to thread around: a UniqueSupply and a SATEnv.
98 = UniqSupply -> SATEnv -> (result, SATEnv)
100 initSAT :: SatM a -> UniqSupply -> a
102 initSAT f us = fst (f us nullIdEnv)
105 = case splitUniqSupply us of { (s1, s2) ->
106 case m s1 env of { (m_result, menv) ->
107 k m_result s2 menv }}
110 = case splitUniqSupply us of { (s1, s2) ->
111 case m s1 env of { (_, menv) ->
114 emptyEnvSAT :: SatM ()
115 emptyEnvSAT us _ = ((), nullIdEnv)
117 returnSAT v us env = (v, env)
119 mapSAT f [] = returnSAT []
121 = f x `thenSAT` \ x' ->
122 mapSAT f xs `thenSAT` \ xs' ->
126 %************************************************************************
128 \subsection{Utility Functions}
130 %************************************************************************
133 getSATInfo :: Id -> SatM (Maybe SATInfo)
134 getSATInfo var us env
135 = (lookupIdEnv env var, env)
137 newSATName :: Id -> Type -> SatM Id
138 newSATName id ty us env
139 = case (getUnique us) of { unique ->
140 (mkSysLocal new_str unique ty noSrcLoc, env) }
142 new_str = getOccName id _APPEND_ SLIT("_sat")
144 getArgLists :: CoreExpr -> ([Arg Type],[Arg Id])
147 (tvs, lambda_bounds, body) = collectBinders expr
149 ([ Static (mkTyVarTy tv) | tv <- tvs ],
150 [ Static v | v <- lambda_bounds ])
152 dropArgs :: CoreExpr -> CoreExpr
153 dropArgs (Lam _ e) = dropArgs e
154 dropArgs (CoTyLam _ e) = dropArgs e
158 We implement saTransform using shadowing of binders, that is
160 map = \f as -> case as of
162 (a':as') -> let x = f a'
166 map = \f as -> let map = \f as -> map' as
167 in let rec map' = \as -> case as of
169 (a':as') -> let x = f a'
174 the inner map should get inlined and eliminated.
176 saTransform :: Id -> CoreExpr -> SatM CoreBinding
177 saTransform binder rhs
178 = getSATInfo binder `thenSAT` \ r ->
180 -- [Andre] test: do it only if we have more than one static argument.
181 --Just (tyargs,args) | any isStatic args
182 Just (tyargs,args) | length (filter isStatic args) > 1
183 -> newSATName binder (new_ty tyargs args) `thenSAT` \ binder' ->
184 mkNewRhs binder binder' tyargs args rhs `thenSAT` \ new_rhs ->
185 trace ("SAT "++ show (length (filter isStatic args))) (
186 returnSAT (NonRec binder new_rhs)
188 _ -> returnSAT (Rec [(binder, rhs)])
190 mkNewRhs binder binder' tyargs args rhs
192 non_static_args :: [Id]
194 = get_nsa args (snd (getArgLists rhs))
196 get_nsa :: [Arg a] -> [Arg a] -> [a]
199 get_nsa (NotStatic:args) (Static v:as) = v:get_nsa args as
200 get_nsa (_:args) (_:as) = get_nsa args as
202 local_body = foldl App (Var binder')
203 [VarArg a | a <- non_static_args]
205 nonrec_rhs = origLams local_body
207 -- HACK! The following is a fake SysLocal binder with
208 -- *the same* unique as binder.
209 -- the reason for this is the following:
210 -- this binder *will* get inlined but if it happen to be
211 -- a top level binder it is never removed as dead code,
212 -- therefore we have to remove that information (of it being
213 -- top-level or exported somehow.)
214 -- A better fix is to use binder directly but with the TopLevel
215 -- tag (or Exported tag) modified.
216 fake_binder = mkSysLocal
217 (getOccName binder _APPEND_ SLIT("_fsat"))
221 rec_body = mkValLam non_static_args
222 ( Let (NonRec fake_binder nonrec_rhs)
223 {-in-} (dropArgs rhs))
226 origLams (Let (Rec [(binder',rec_body)]) {-in-} local_body)
229 origLams = origLams' rhs
231 origLams' (Lam v e) e' = Lam v (origLams' e e')
232 origLams' (CoTyLam ty e) e' = CoTyLam ty (origLams' e e')
236 = instantiateTy (mk_inst_tyenv tyargs tv_tmpl)
237 (mkSigmaTy tv_tmpl' dict_tys' tau_ty')
239 -- get type info for the local function:
240 (tv_tmpl, dict_tys, tau_ty) = (splitSigmaTy . idType) binder
241 (reg_arg_tys, res_type) = splitFunTys tau_ty
243 -- now, we drop the ones that are
244 -- static, that is, the ones we will not pass to the local function
246 tv_tmpl' = dropStatics tyargs tv_tmpl
247 dict_tys' = dropStatics (take l args) dict_tys
248 reg_arg_tys' = dropStatics (drop l args) reg_arg_tys
249 tau_ty' = glueTyArgs reg_arg_tys' res_type
251 mk_inst_tyenv [] _ = emptyTyVarEnv
252 mk_inst_tyenv (Static s:args) (t:ts) = addToTyVarEnv (mk_inst_tyenv args ts) t s
253 mk_inst_tyenv (_:args) (_:ts) = mk_inst_tyenv args ts
256 dropStatics (Static _:args) (t:ts) = dropStatics args ts
257 dropStatics (_:args) (t:ts) = t:dropStatics args ts
259 isStatic :: Arg a -> Bool
260 isStatic NotStatic = False