2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %************************************************************************
6 \section[SAT]{Static Argument Transformation pass}
8 %************************************************************************
10 96/03: We aren't using the static-argument transformation right now.
12 May be seen as removing invariants from loops:
13 Arguments of recursive functions that do not change in recursive
14 calls are removed from the recursion, which is done locally
15 and only passes the arguments which effectively change.
18 map = /\ ab -> \f -> \xs -> case xs of
20 (a:b) -> f a : map f b
22 as map is recursively called with the same argument f (unmodified)
25 map = /\ ab -> \f -> \xs -> let map' ys = case ys of
30 Notice that for a compiler that uses lambda lifting this is
31 useless as map' will be transformed back to what map was.
33 We could possibly do the same for big lambdas, but we don't as
34 they will eventually be removed in later stages of the compiler,
35 therefore there is no penalty in keeping them.
37 Experimental Evidence: Heap: +/- 7%
38 Instrs: Always improves for 2 or more Static Args.
41 module SAT ( doStaticArgs ) where
43 #include "HsVersions.h"
45 import Panic ( panic )
47 doStaticArgs = panic "SAT.doStaticArgs (ToDo)"
49 {- LATER: to end of file:
56 doStaticArgs :: [CoreBind] -> UniqSupply -> [CoreBind]
60 showPass "Static argument";
61 let { binds' = initSAT (mapSAT sat_bind binds) };
62 endPass "Static argument"
63 False -- No specific flag for dumping SAT
67 sat_bind (NonRec binder expr)
68 = emptyEnvSAT `thenSAT_`
69 satExpr expr `thenSAT` (\ expr' ->
70 returnSAT (NonRec binder expr') )
71 sat_bind (Rec [(binder,rhs)])
72 = emptyEnvSAT `thenSAT_`
73 insSAEnv binder (getArgLists rhs) `thenSAT_`
74 satExpr rhs `thenSAT` (\ rhs' ->
75 saTransform binder rhs')
77 = emptyEnvSAT `thenSAT_`
78 mapSAT satExpr rhss `thenSAT` \ rhss' ->
79 returnSAT (Rec (zipEqual "doStaticArgs" binders rhss'))
81 (binders, rhss) = unzip pairs
86 = updSAEnv (Just (v,([],[]))) `thenSAT_`
89 satAtom _ = returnSAT ()
93 satExpr :: CoreExpr -> SatM CoreExpr
96 = updSAEnv (Just (v,([],[]))) `thenSAT_`
99 satExpr lit@(Lit _) = returnSAT lit
101 satExpr e@(Prim prim ty args)
102 = mapSAT satAtom args `thenSAT_`
105 satExpr (Lam binders body)
106 = satExpr body `thenSAT` \ body' ->
107 returnSAT (Lam binders body')
109 satExpr (CoTyLam tyvar body)
110 = satExpr body `thenSAT` (\ body' ->
111 returnSAT (CoTyLam tyvar body') )
113 satExpr app@(App _ _)
116 satExpr app@(CoTyApp _ _)
119 satExpr (Case expr alts)
120 = satExpr expr `thenSAT` \ expr' ->
121 sat_alts alts `thenSAT` \ alts' ->
122 returnSAT (Case expr' alts')
124 sat_alts (AlgAlts alts deflt)
125 = mapSAT satAlgAlt alts `thenSAT` \ alts' ->
126 sat_default deflt `thenSAT` \ deflt' ->
127 returnSAT (AlgAlts alts' deflt')
129 satAlgAlt (con, params, rhs)
130 = satExpr rhs `thenSAT` \ rhs' ->
131 returnSAT (con, params, rhs')
133 sat_alts (PrimAlts alts deflt)
134 = mapSAT satPrimAlt alts `thenSAT` \ alts' ->
135 sat_default deflt `thenSAT` \ deflt' ->
136 returnSAT (PrimAlts alts' deflt')
138 satPrimAlt (lit, rhs)
139 = satExpr rhs `thenSAT` \ rhs' ->
140 returnSAT (lit, rhs')
142 sat_default NoDefault
143 = returnSAT NoDefault
144 sat_default (BindDefault binder rhs)
145 = satExpr rhs `thenSAT` \ rhs' ->
146 returnSAT (BindDefault binder rhs')
148 satExpr (Let (NonRec binder rhs) body)
149 = satExpr body `thenSAT` \ body' ->
150 satExpr rhs `thenSAT` \ rhs' ->
151 returnSAT (Let (NonRec binder rhs') body')
153 satExpr (Let (Rec [(binder,rhs)]) body)
154 = satExpr body `thenSAT` \ body' ->
155 insSAEnv binder (getArgLists rhs) `thenSAT_`
156 satExpr rhs `thenSAT` \ rhs' ->
157 saTransform binder rhs' `thenSAT` \ binding ->
158 returnSAT (Let binding body')
160 satExpr (Let (Rec binds) body)
162 (binders, rhss) = unzip binds
164 satExpr body `thenSAT` \ body' ->
165 mapSAT satExpr rhss `thenSAT` \ rhss' ->
166 returnSAT (Let (Rec (zipEqual "satExpr:Rec" binders rhss')) body')
168 satExpr (Note note expr)
169 = satExpr expr `thenSAT` \ expr2 ->
170 returnSAT (Note note expr2)
174 getAppArgs :: CoreExpr -> SatM CoreExpr
177 = get app `thenSAT` \ (app',result) ->
178 updSAEnv result `thenSAT_`
182 -> SatM (CoreExpr, Maybe (Id, SATInfo))
185 = get e `thenSAT` \ (e',result) ->
190 Just (v,(tv,lv)) -> Just (v,(tv++[Static ty],lv))
194 = get e `thenSAT` \ (e', result) ->
197 (VarArg v) -> Static v
203 Just (v,(tv,lv)) -> Just (v,(tv,lv++[si]))
208 = returnSAT (var, Just (v,([],[])))
211 = satExpr e `thenSAT` \ e2 ->
212 returnSAT (e2, Nothing)