2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
5 Run through the STG code and compute the Static Reference Table for
6 each let-binding. At the same time, we figure out which top-level
7 bindings have no CAF references, and record the fact in their IdInfo.
10 module SRT( computeSRTs ) where
12 #include "HsVersions.h"
17 import BasicTypes ( TopLevelFlag(..), isTopLevel )
18 import Util ( mapAccumL )
26 computeSRTs :: [StgBinding] -> [(StgBinding,[Id])]
27 -- The incoming bindingd are filled with SRTEntries in their SRT slots
28 -- the outgoing ones have NoSRT/SRT values instead
30 computeSRTs binds = map srtTopBind binds
33 -----------------------------------------------------------------------------
34 Algorithm for figuring out SRT layout.
36 Our functions have type
38 srtExpr :: SrtOffset -- Next free offset within the SRT
39 -> StgExpr -- Expression to analyse
40 -> (StgExpr, -- (e) newly annotated expression
41 SrtIds, -- (s) SRT required for this expression (reversed)
42 SrtOffset) -- (o) new offset
44 We build a single SRT for a recursive binding group, which is why the
45 SRT building is done at the binding level rather than the
48 The SRT is built up in reverse order, to avoid too many expensive
49 appends. We therefore reverse the SRT before returning it, so that
50 the offsets will be from the beginning of the SRT.
52 -----------------------------------------------------------------------------
55 A function whose CafInfo is NoCafRefs will have an empty SRT, and its
56 closure will not appear in the SRT of any other function (unless we're
57 compiling without optimisation and the CafInfos haven't been emitted
58 in the interface files).
60 Top-Level recursive groups
62 This gets a bit complicated, but the general idea is that we want a
63 single SRT for the whole group, and we'd rather not have recursive
64 references in it if at all possible.
66 We collect all the global references for the group, and filter out
67 those that are binders in the group and not CAFs themselves. Why is
70 - if all the bindings in the group just refer to each other,
71 and none of them are CAFs, we'd like to get an empty SRT.
73 - if any of the bindings in the group refer to a CAF, this will
76 Hmm, that probably makes no sense.
80 type SrtIds = [Id] -- An *reverse-ordered* list of the Ids needed in the SRT
82 srtTopBind :: StgBinding -> (StgBinding, SrtIds)
85 = srtBind TopLevel 0 bind =: \ (bind', srt, off) ->
87 then (bind', []) -- Don't need an SRT for a static constructor
88 else (bind', reverse srt) -- The 'reverse' is because the SRT is
89 -- built up reversed, for efficiency's sake
91 isConBind (StgNonRec _ _ r) = isConRhs r
92 isConBind (StgRec _ bs) = all isConRhs (map snd bs)
94 isConRhs (StgRhsCon _ _ _) = True
97 srtBind :: TopLevelFlag -> SrtOffset -> StgBinding
98 -> (StgBinding, SrtIds, SrtOffset)
100 srtBind top off (StgNonRec (SRTEntries rhs_cafs) binder rhs)
101 = (StgNonRec srt_info binder new_rhs, this_srt, body_off)
103 (new_rhs, rhs_srt, rhs_off) = srtRhs off rhs
104 (srt_info, this_srt, body_off) = constructSRT rhs_cafs rhs_srt off rhs_off
107 srtBind top off (StgRec (SRTEntries rhss_cafs) pairs)
108 = (StgRec srt_info new_pairs, this_srt, body_off)
110 ((rhss_off, rhss_srt), new_pairs) = mapAccumL do_bind (off, []) pairs
112 do_bind (off,srt) (bndr,rhs)
113 = srtRhs off rhs =: \(rhs', srt', off') ->
114 ((off', srt'++srt), (bndr, rhs'))
116 non_caf_binders = [ b | (b, rhs) <- pairs, not (caf_rhs rhs) ]
119 | isTopLevel top = filterVarSet (`notElem` non_caf_binders) rhss_cafs
120 | otherwise = rhss_cafs
122 (srt_info, this_srt, body_off)
123 = constructSRT filtered_rhss_cafs rhss_srt off rhss_off
125 caf_rhs (StgRhsClosure _ _ free_vars _ [] body) = True
129 -----------------------------------------------------------------------------
133 srtRhs :: SrtOffset -> StgRhs -> (StgRhs, SrtIds, SrtOffset)
135 srtRhs off (StgRhsClosure cc bi free_vars u args body)
136 = srtExpr off body =: \(body, srt, off) ->
137 (StgRhsClosure cc bi free_vars u args body, srt, off)
139 srtRhs off e@(StgRhsCon cc con args) = (e, [], off)
142 -----------------------------------------------------------------------------
146 srtExpr :: SrtOffset -> StgExpr -> (StgExpr, SrtIds, SrtOffset)
148 srtExpr off e@(StgApp f args) = (e, [], off)
149 srtExpr off e@(StgLit l) = (e, [], off)
150 srtExpr off e@(StgConApp con args) = (e, [], off)
151 srtExpr off e@(StgOpApp op args ty) = (e, [], off)
153 srtExpr off (StgSCC cc expr) =
154 srtExpr off expr =: \(expr, srt, off) ->
155 (StgSCC cc expr, srt, off)
157 srtExpr off (StgCase scrut live1 live2 uniq (SRTEntries cafs_in_alts) alts)
158 = srtCaseAlts off alts =: \(alts, alts_srt, alts_off) ->
160 (srt_info, this_srt, scrut_off)
161 = constructSRT cafs_in_alts alts_srt off alts_off
163 srtExpr scrut_off scrut =: \(scrut, scrut_srt, case_off) ->
165 (StgCase scrut live1 live2 uniq srt_info alts,
166 scrut_srt ++ this_srt,
169 srtExpr off (StgLet bind body)
170 = srtBind NotTopLevel off bind =: \ (bind', bind_srt, body_off) ->
171 srtExpr body_off body =: \ (body', expr_srt, let_off) ->
172 (StgLet bind' body', expr_srt ++ bind_srt, let_off)
174 srtExpr off (StgLetNoEscape live1 live2 bind body)
175 = srtBind NotTopLevel off bind =: \ (bind', bind_srt, body_off) ->
176 srtExpr body_off body =: \ (body', expr_srt, let_off) ->
177 (StgLetNoEscape live1 live2 bind' body', expr_srt ++ bind_srt, let_off)
180 srtExpr off expr = pprPanic "srtExpr" (ppr expr)
184 -----------------------------------------------------------------------------
187 Construct the SRT at this point from its sub-SRTs and any new global
188 references which aren't already contained in one of the sub-SRTs (and
192 constructSRT caf_refs sub_srt initial_offset current_offset
194 extra_refs = filter (`notElem` sub_srt) (varSetElems caf_refs)
195 this_srt = extra_refs ++ sub_srt
197 -- Add the length of the new entries to the
198 -- current offset to get the next free offset in the global SRT.
199 new_offset = current_offset + length extra_refs
200 srt_length = new_offset - initial_offset
202 srt_info | srt_length == 0 = NoSRT
203 | otherwise = SRT initial_offset srt_length
205 in ASSERT( srt_length == length this_srt )
206 (srt_info, this_srt, new_offset)
209 -----------------------------------------------------------------------------
213 srtCaseAlts :: SrtOffset -> StgCaseAlts -> (StgCaseAlts, SrtIds, SrtOffset)
215 srtCaseAlts off (StgAlgAlts t alts dflt)
216 = srtDefault off dflt =: \ ((dflt_off, dflt_srt), dflt') ->
217 mapAccumL srtAlgAlt (dflt_off, dflt_srt) alts =: \ ((alts_off, alts_srt), alts') ->
218 (StgAlgAlts t alts' dflt', alts_srt, alts_off)
220 srtCaseAlts off (StgPrimAlts t alts dflt)
221 = srtDefault off dflt =: \ ((dflt_off, dflt_srt), dflt') ->
222 mapAccumL srtPrimAlt (dflt_off, dflt_srt) alts =: \ ((alts_off, alts_srt), alts') ->
223 (StgPrimAlts t alts' dflt', alts_srt, alts_off)
225 srtAlgAlt (off,srt) (con,args,used,rhs)
226 = srtExpr off rhs =: \(rhs', rhs_srt, rhs_off) ->
227 ((rhs_off, rhs_srt ++ srt), (con,args,used,rhs'))
229 srtPrimAlt (off,srt) (lit,rhs)
230 = srtExpr off rhs =: \(rhs', rhs_srt, rhs_off) ->
231 ((rhs_off, rhs_srt ++ srt), (lit, rhs'))
233 srtDefault off StgNoDefault
234 = ((off,[]), StgNoDefault)
235 srtDefault off (StgBindDefault rhs)
236 = srtExpr off rhs =: \(rhs', srt, off) ->
237 ((off,srt), StgBindDefault rhs')
240 -----------------------------------------------------------------------------