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"
16 import VarSet ( varSetElems )
17 import Util ( mapAccumL )
25 computeSRTs :: [StgBinding] -> [(StgBinding,[Id])]
26 -- The incoming bindingd are filled with SRTEntries in their SRT slots
27 -- the outgoing ones have NoSRT/SRT values instead
29 computeSRTs binds = map srtTopBind binds
32 -----------------------------------------------------------------------------
33 Algorithm for figuring out SRT layout.
35 Our functions have type
37 srtExpr :: SrtOffset -- Next free offset within the SRT
38 -> StgExpr -- Expression to analyse
39 -> (StgExpr, -- (e) newly annotated expression
40 SrtIds, -- (s) SRT required for this expression (reversed)
41 SrtOffset) -- (o) new offset
43 We build a single SRT for a recursive binding group, which is why the
44 SRT building is done at the binding level rather than the
47 The SRT is built up in reverse order, to avoid too many expensive
48 appends. We therefore reverse the SRT before returning it, so that
49 the offsets will be from the beginning of the SRT.
51 -----------------------------------------------------------------------------
54 A function whose CafInfo is NoCafRefs will have an empty SRT, and its
55 closure will not appear in the SRT of any other function (unless we're
56 compiling without optimisation and the CafInfos haven't been emitted
57 in the interface files).
59 Top-Level recursive groups
61 This gets a bit complicated, but the general idea is that we want a
62 single SRT for the whole group, and we'd rather not have recursive
63 references in it if at all possible.
65 We collect all the global references for the group, and filter out
66 those that are binders in the group and not CAFs themselves. Why is
69 - if all the bindings in the group just refer to each other,
70 and none of them are CAFs, we'd like to get an empty SRT.
72 - if any of the bindings in the group refer to a CAF, this will
75 Hmm, that probably makes no sense.
79 type SrtIds = [Id] -- An *reverse-ordered* list of the Ids needed in the SRT
81 srtTopBind :: StgBinding -> (StgBinding, SrtIds)
84 = srtBind 0 bind =: \ (bind', srt, off) ->
85 (bind', reverse srt) -- The 'reverse' is because the SRT is
86 -- built up reversed, for efficiency's sake
88 srtBind :: SrtOffset -> StgBinding -> (StgBinding, SrtIds, SrtOffset)
90 srtBind off (StgNonRec (SRTEntries rhs_cafs) binder rhs)
91 = (StgNonRec srt_info binder new_rhs, this_srt, body_off)
93 (new_rhs, rhs_srt, rhs_off) = srtRhs off rhs
94 (srt_info, this_srt, body_off) = constructSRT rhs_cafs rhs_srt off rhs_off
97 srtBind off (StgRec (SRTEntries rhss_cafs) pairs)
98 = (StgRec srt_info new_pairs, this_srt, body_off)
100 ((rhss_off, rhss_srt), new_pairs) = mapAccumL do_bind (off, []) pairs
102 do_bind (off,srt) (bndr,rhs)
103 = srtRhs off rhs =: \(rhs', srt', off') ->
104 ((off', srt'++srt), (bndr, rhs'))
106 (srt_info, this_srt, body_off)
107 = constructSRT rhss_cafs rhss_srt off rhss_off
110 -----------------------------------------------------------------------------
114 srtRhs :: SrtOffset -> StgRhs -> (StgRhs, SrtIds, SrtOffset)
116 srtRhs off (StgRhsClosure cc bi free_vars u args body)
117 = srtExpr off body =: \(body, srt, off) ->
118 (StgRhsClosure cc bi free_vars u args body, srt, off)
120 srtRhs off e@(StgRhsCon cc con args) = (e, [], off)
123 -----------------------------------------------------------------------------
127 srtExpr :: SrtOffset -> StgExpr -> (StgExpr, SrtIds, SrtOffset)
129 srtExpr off e@(StgApp f args) = (e, [], off)
130 srtExpr off e@(StgLit l) = (e, [], off)
131 srtExpr off e@(StgConApp con args) = (e, [], off)
132 srtExpr off e@(StgPrimApp op args ty) = (e, [], off)
134 srtExpr off (StgSCC cc expr) =
135 srtExpr off expr =: \(expr, srt, off) ->
136 (StgSCC cc expr, srt, off)
138 srtExpr off (StgCase scrut live1 live2 uniq (SRTEntries cafs_in_alts) alts)
139 = srtCaseAlts off alts =: \(alts, alts_srt, alts_off) ->
141 (srt_info, this_srt, scrut_off)
142 = constructSRT cafs_in_alts alts_srt off alts_off
144 srtExpr scrut_off scrut =: \(scrut, scrut_srt, case_off) ->
146 (StgCase scrut live1 live2 uniq srt_info alts,
147 scrut_srt ++ this_srt,
150 srtExpr off (StgLet bind body)
151 = srtBind off bind =: \ (bind', bind_srt, body_off) ->
152 srtExpr body_off body =: \ (body', expr_srt, let_off) ->
153 (StgLet bind' body', expr_srt ++ bind_srt, let_off)
155 srtExpr off (StgLetNoEscape live1 live2 bind body)
156 = srtBind off bind =: \ (bind', bind_srt, body_off) ->
157 srtExpr body_off body =: \ (body', expr_srt, let_off) ->
158 (StgLetNoEscape live1 live2 bind' body', expr_srt ++ bind_srt, let_off)
161 srtExpr off expr = pprPanic "srtExpr" (ppr expr)
165 -----------------------------------------------------------------------------
168 Construct the SRT at this point from its sub-SRTs and any new global
169 references which aren't already contained in one of the sub-SRTs (and
173 constructSRT caf_refs sub_srt initial_offset current_offset
175 extra_refs = filter (`notElem` sub_srt) (varSetElems caf_refs)
176 this_srt = extra_refs ++ sub_srt
178 -- Add the length of the new entries to the
179 -- current offset to get the next free offset in the global SRT.
180 new_offset = current_offset + length extra_refs
181 srt_length = new_offset - initial_offset
183 srt_info | srt_length == 0 = NoSRT
184 | otherwise = SRT initial_offset srt_length
186 in ASSERT( srt_length == length this_srt )
187 (srt_info, this_srt, new_offset)
190 -----------------------------------------------------------------------------
194 srtCaseAlts :: SrtOffset -> StgCaseAlts -> (StgCaseAlts, SrtIds, SrtOffset)
196 srtCaseAlts off (StgAlgAlts t alts dflt)
197 = srtDefault off dflt =: \ ((dflt_off, dflt_srt), dflt') ->
198 mapAccumL srtAlgAlt (dflt_off, dflt_srt) alts =: \ ((alts_off, alts_srt), alts') ->
199 (StgAlgAlts t alts' dflt', alts_srt, alts_off)
201 srtCaseAlts off (StgPrimAlts t alts dflt)
202 = srtDefault off dflt =: \ ((dflt_off, dflt_srt), dflt') ->
203 mapAccumL srtPrimAlt (dflt_off, dflt_srt) alts =: \ ((alts_off, alts_srt), alts') ->
204 (StgPrimAlts t alts' dflt', alts_srt, alts_off)
206 srtAlgAlt (off,srt) (con,args,used,rhs)
207 = srtExpr off rhs =: \(rhs', rhs_srt, rhs_off) ->
208 ((rhs_off, rhs_srt ++ srt), (con,args,used,rhs'))
210 srtPrimAlt (off,srt) (lit,rhs)
211 = srtExpr off rhs =: \(rhs', rhs_srt, rhs_off) ->
212 ((rhs_off, rhs_srt ++ srt), (lit, rhs'))
214 srtDefault off StgNoDefault
215 = ((off,[]), StgNoDefault)
216 srtDefault off (StgBindDefault rhs)
217 = srtExpr off rhs =: \(rhs', srt, off) ->
218 ((off,srt), StgBindDefault rhs')
221 -----------------------------------------------------------------------------