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 {-# OPTIONS -fno-warn-incomplete-patterns #-}
11 -- The above warning supression flag is a temporary kludge.
12 -- While working on this module you are encouraged to remove it and fix
13 -- any warnings in the module. See
14 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 module SRT( computeSRTs ) where
19 #include "HsVersions.h"
25 import Maybes ( orElse, expectJust )
36 computeSRTs :: [StgBinding] -> [(StgBinding,[(Id,[Id])])]
37 -- The incoming bindingd are filled with SRTEntries in their SRT slots
38 -- the outgoing ones have NoSRT/SRT values instead
40 computeSRTs binds = srtTopBinds emptyVarEnv binds
42 -- --------------------------------------------------------------------------
45 srtTopBinds :: IdEnv Id -> [StgBinding] -> [(StgBinding, [(Id,[Id])])]
48 srtTopBinds env (StgNonRec b rhs : binds) =
49 (StgNonRec b rhs', [(b,srt')]) : srtTopBinds env' binds
51 (rhs', srt) = srtTopRhs b rhs
52 env' = maybeExtendEnv env b rhs
53 srt' = applyEnvList env srt
54 srtTopBinds env (StgRec bs : binds) =
55 (StgRec (zip bndrs rhss), zip bndrs srts') : srtTopBinds env binds
57 (rhss, srts) = unzip [ srtTopRhs b r | (b,r) <- bs ]
59 srts' = map (applyEnvList env) srts
61 -- Shorting out indirections in SRTs: if a binding has an SRT with a single
62 -- element in it, we just inline it with that element everywhere it occurs
65 -- This is in a way a generalisation of the CafInfo. CafInfo says
66 -- whether a top-level binding has *zero* CAF references, allowing us
67 -- to omit it from SRTs. Here, we pick up bindings with *one* CAF
68 -- reference, and inline its SRT everywhere it occurs. We could pass
69 -- this information across module boundaries too, but we currently
72 maybeExtendEnv ::IdEnv Id -> Id -> StgRhs -> IdEnv Id
73 maybeExtendEnv env bndr (StgRhsClosure _ _ _ ReEntrant (SRTEntries cafs) _ _)
74 | [one] <- varSetElems cafs
75 = extendVarEnv env bndr (applyEnv env one)
76 maybeExtendEnv env _ _ = env
78 applyEnvList :: IdEnv Id -> [Id] -> [Id]
79 applyEnvList env = map (applyEnv env)
81 applyEnv :: IdEnv Id -> Id -> Id
82 applyEnv env id = lookupVarEnv env id `orElse` id
84 -- ---- Top-level right hand sides:
86 srtTopRhs :: Id -> StgRhs -> (StgRhs, [Id])
88 srtTopRhs _ rhs@(StgRhsCon _ _ _) = (rhs, [])
89 srtTopRhs _ rhs@(StgRhsClosure _ _ _ _ (SRTEntries cafs) _ _)
90 = (srtRhs table rhs, elems)
92 elems = varSetElems cafs
93 table = mkVarEnv (zip elems [0..])
97 srtBind :: IdEnv Int -> StgBinding -> StgBinding
99 srtBind table (StgNonRec binder rhs) = StgNonRec binder (srtRhs table rhs)
100 srtBind table (StgRec pairs) = StgRec [ (b, srtRhs table r) | (b,r) <- pairs ]
102 -- ---- Right Hand Sides:
104 srtRhs :: IdEnv Int -> StgRhs -> StgRhs
106 srtRhs _ e@(StgRhsCon _ _ _) = e
107 srtRhs table (StgRhsClosure cc bi free_vars u srt args body)
108 = StgRhsClosure cc bi free_vars u (constructSRT table srt) args
109 $! (srtExpr table body)
111 -- ---------------------------------------------------------------------------
114 srtExpr :: IdEnv Int -> StgExpr -> StgExpr
116 srtExpr _ e@(StgApp _ _) = e
117 srtExpr _ e@(StgLit _) = e
118 srtExpr _ e@(StgConApp _ _) = e
119 srtExpr _ e@(StgOpApp _ _ _) = e
121 srtExpr table (StgSCC cc expr) = StgSCC cc $! srtExpr table expr
123 srtExpr table (StgTick m n expr) = StgTick m n $! srtExpr table expr
125 srtExpr table (StgCase scrut live1 live2 uniq srt alt_type alts)
126 = StgCase expr' live1 live2 uniq srt' alt_type alts'
128 expr' = srtExpr table scrut
129 srt' = constructSRT table srt
130 alts' = map (srtAlt table) alts
132 srtExpr table (StgLet bind body)
133 = srtBind table bind =: \ bind' ->
134 srtExpr table body =: \ body' ->
137 srtExpr table (StgLetNoEscape live1 live2 bind body)
138 = srtBind table bind =: \ bind' ->
139 srtExpr table body =: \ body' ->
140 StgLetNoEscape live1 live2 bind' body'
142 srtExpr _table expr = pprPanic "srtExpr" (ppr expr)
144 srtAlt :: IdEnv Int -> StgAlt -> StgAlt
145 srtAlt table (con,args,used,rhs)
146 = (,,,) con args used $! srtExpr table rhs
148 -----------------------------------------------------------------------------
149 -- Construct an SRT bitmap.
151 constructSRT :: IdEnv Int -> SRT -> SRT
152 constructSRT table (SRTEntries entries)
153 | isEmptyVarSet entries = NoSRT
154 | otherwise = seqBitmap bitmap $ SRT offset len bitmap
156 ints = map (expectJust "constructSRT" . lookupVarEnv table)
157 (varSetElems entries)
158 sorted_ints = sortLe (<=) ints
159 offset = head sorted_ints
160 bitmap_entries = map (subtract offset) sorted_ints
161 len = last bitmap_entries + 1
162 bitmap = intsToBitmap len bitmap_entries
164 -- ---------------------------------------------------------------------------
167 (=:) :: a -> (a -> b) -> b