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 MkId ( mkSysLocal )
40 import SrcLoc ( SrcLoc, noSrcLoc )
44 infixr 9 `thenSAT`, `thenSAT_`
47 %************************************************************************
49 \subsection{Static Argument Transformation Environment}
51 %************************************************************************
54 type SATEnv = IdEnv SATInfo
56 type SATInfo = ([Arg Type],[Arg Id])
58 data Arg a = Static a | NotStatic
61 delOneFromSAEnv v us env
62 = ((), delOneFromIdEnv env v)
64 updSAEnv :: Maybe (Id,SATInfo) -> SatM ()
67 updSAEnv (Just (b,(tyargs,args)))
68 = getSATInfo b `thenSAT` (\ r ->
70 Nothing -> returnSAT ()
71 Just (tyargs',args') -> delOneFromSAEnv b `thenSAT_`
72 insSAEnv b (checkArgs tyargs tyargs',
76 checkArgs as [] = notStatics (length as)
77 checkArgs [] as = notStatics (length as)
78 checkArgs (a:as) (a':as') | a == a' = a:checkArgs as as'
79 checkArgs (_:as) (_:as') = NotStatic:checkArgs as as'
81 notStatics :: Int -> [Arg a]
82 notStatics n = nOfThem n NotStatic
84 insSAEnv :: Id -> SATInfo -> SatM ()
85 insSAEnv b info us env
86 = ((), addOneToIdEnv env b info)
89 %************************************************************************
91 \subsection{Static Argument Transformation Monad}
93 %************************************************************************
95 Two items of state to thread around: a UniqueSupply and a SATEnv.
99 = UniqSupply -> SATEnv -> (result, SATEnv)
101 initSAT :: SatM a -> UniqSupply -> a
103 initSAT f us = fst (f us nullIdEnv)
106 = case splitUniqSupply us of { (s1, s2) ->
107 case m s1 env of { (m_result, menv) ->
108 k m_result s2 menv }}
111 = case splitUniqSupply us of { (s1, s2) ->
112 case m s1 env of { (_, menv) ->
115 emptyEnvSAT :: SatM ()
116 emptyEnvSAT us _ = ((), nullIdEnv)
118 returnSAT v us env = (v, env)
120 mapSAT f [] = returnSAT []
122 = f x `thenSAT` \ x' ->
123 mapSAT f xs `thenSAT` \ xs' ->
127 %************************************************************************
129 \subsection{Utility Functions}
131 %************************************************************************
134 getSATInfo :: Id -> SatM (Maybe SATInfo)
135 getSATInfo var us env
136 = (lookupIdEnv env var, env)
138 newSATName :: Id -> Type -> SatM Id
139 newSATName id ty us env
140 = case (getUnique us) of { unique ->
141 (mkSysLocal new_str unique ty noSrcLoc, env) }
143 new_str = getOccName id _APPEND_ SLIT("_sat")
145 getArgLists :: CoreExpr -> ([Arg Type],[Arg Id])
148 (tvs, lambda_bounds, body) = collectBinders expr
150 ([ Static (mkTyVarTy tv) | tv <- tvs ],
151 [ Static v | v <- lambda_bounds ])
153 dropArgs :: CoreExpr -> CoreExpr
154 dropArgs (Lam _ e) = dropArgs e
155 dropArgs (CoTyLam _ e) = dropArgs e
159 We implement saTransform using shadowing of binders, that is
161 map = \f as -> case as of
163 (a':as') -> let x = f a'
167 map = \f as -> let map = \f as -> map' as
168 in let rec map' = \as -> case as of
170 (a':as') -> let x = f a'
175 the inner map should get inlined and eliminated.
177 saTransform :: Id -> CoreExpr -> SatM CoreBinding
178 saTransform binder rhs
179 = getSATInfo binder `thenSAT` \ r ->
181 -- [Andre] test: do it only if we have more than one static argument.
182 --Just (tyargs,args) | any isStatic args
183 Just (tyargs,args) | length (filter isStatic args) > 1
184 -> newSATName binder (new_ty tyargs args) `thenSAT` \ binder' ->
185 mkNewRhs binder binder' tyargs args rhs `thenSAT` \ new_rhs ->
186 trace ("SAT "++ show (length (filter isStatic args))) (
187 returnSAT (NonRec binder new_rhs)
189 _ -> returnSAT (Rec [(binder, rhs)])
191 mkNewRhs binder binder' tyargs args rhs
193 non_static_args :: [Id]
195 = get_nsa args (snd (getArgLists rhs))
197 get_nsa :: [Arg a] -> [Arg a] -> [a]
200 get_nsa (NotStatic:args) (Static v:as) = v:get_nsa args as
201 get_nsa (_:args) (_:as) = get_nsa args as
203 local_body = foldl App (Var binder')
204 [VarArg a | a <- non_static_args]
206 nonrec_rhs = origLams local_body
208 -- HACK! The following is a fake SysLocal binder with
209 -- *the same* unique as binder.
210 -- the reason for this is the following:
211 -- this binder *will* get inlined but if it happen to be
212 -- a top level binder it is never removed as dead code,
213 -- therefore we have to remove that information (of it being
214 -- top-level or exported somehow.)
215 -- A better fix is to use binder directly but with the TopLevel
216 -- tag (or Exported tag) modified.
217 fake_binder = mkSysLocal
218 (getOccName binder _APPEND_ SLIT("_fsat"))
222 rec_body = mkValLam non_static_args
223 ( Let (NonRec fake_binder nonrec_rhs)
224 {-in-} (dropArgs rhs))
227 origLams (Let (Rec [(binder',rec_body)]) {-in-} local_body)
230 origLams = origLams' rhs
232 origLams' (Lam v e) e' = Lam v (origLams' e e')
233 origLams' (CoTyLam ty e) e' = CoTyLam ty (origLams' e e')
237 = instantiateTy (mk_inst_tyenv tyargs tv_tmpl)
238 (mkSigmaTy tv_tmpl' dict_tys' tau_ty')
240 -- get type info for the local function:
241 (tv_tmpl, dict_tys, tau_ty) = (splitSigmaTy . idType) binder
242 (reg_arg_tys, res_type) = splitFunTys tau_ty
244 -- now, we drop the ones that are
245 -- static, that is, the ones we will not pass to the local function
247 tv_tmpl' = dropStatics tyargs tv_tmpl
248 dict_tys' = dropStatics (take l args) dict_tys
249 reg_arg_tys' = dropStatics (drop l args) reg_arg_tys
250 tau_ty' = glueTyArgs reg_arg_tys' res_type
252 mk_inst_tyenv [] _ = emptyTyVarEnv
253 mk_inst_tyenv (Static s:args) (t:ts) = addToTyVarEnv (mk_inst_tyenv args ts) t s
254 mk_inst_tyenv (_:args) (_:ts) = mk_inst_tyenv args ts
257 dropStatics (Static _:args) (t:ts) = dropStatics args ts
258 dropStatics (_:args) (t:ts) = t:dropStatics args ts
260 isStatic :: Arg a -> Bool
261 isStatic NotStatic = False