DEBUG removal
[ghc-hetmet.git] / compiler / simplStg / SRT.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4
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.
8
9 \begin{code}
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
15 -- for details
16
17 module SRT( computeSRTs ) where
18
19 -- XXX This define is a bit of a hack, and should be done more nicely
20 #define FAST_STRING_NOT_NEEDED 1
21 #include "HsVersions.h"
22
23 import StgSyn
24 import Id               ( Id )
25 import VarSet
26 import VarEnv
27 import Maybes           ( orElse, expectJust )
28 import Bitmap
29
30 import Outputable
31
32 import List
33
34 import Util
35 \end{code}
36
37 \begin{code}
38 computeSRTs :: [StgBinding] -> [(StgBinding,[(Id,[Id])])]
39   -- The incoming bindingd are filled with SRTEntries in their SRT slots
40   -- the outgoing ones have NoSRT/SRT values instead
41
42 computeSRTs binds = srtTopBinds emptyVarEnv binds
43
44 -- --------------------------------------------------------------------------
45 -- Top-level Bindings
46
47 srtTopBinds :: IdEnv Id -> [StgBinding] -> [(StgBinding, [(Id,[Id])])]
48
49 srtTopBinds _   [] = []
50 srtTopBinds env (StgNonRec b rhs : binds) = 
51   (StgNonRec b rhs', [(b,srt')]) : srtTopBinds env' binds
52   where
53     (rhs', srt) = srtTopRhs b rhs
54     env' = maybeExtendEnv env b rhs
55     srt' = applyEnvList env srt
56 srtTopBinds env (StgRec bs : binds) = 
57   (StgRec (zip bndrs rhss), zip bndrs srts') : srtTopBinds env binds
58   where
59     (rhss, srts) = unzip [ srtTopRhs b r | (b,r) <- bs ]
60     bndrs = map fst bs
61     srts' = map (applyEnvList env) srts
62
63 -- Shorting out indirections in SRTs:  if a binding has an SRT with a single
64 -- element in it, we just inline it with that element everywhere it occurs
65 -- in other SRTs.
66 --
67 -- This is in a way a generalisation of the CafInfo.  CafInfo says
68 -- whether a top-level binding has *zero* CAF references, allowing us
69 -- to omit it from SRTs.  Here, we pick up bindings with *one* CAF
70 -- reference, and inline its SRT everywhere it occurs.  We could pass
71 -- this information across module boundaries too, but we currently
72 -- don't.
73
74 maybeExtendEnv ::IdEnv Id -> Id -> StgRhs -> IdEnv Id
75 maybeExtendEnv env bndr (StgRhsClosure _ _ _ ReEntrant (SRTEntries cafs) _ _)
76   | [one] <- varSetElems cafs
77   = extendVarEnv env bndr (applyEnv env one)
78 maybeExtendEnv env _ _ = env
79
80 applyEnvList :: IdEnv Id -> [Id] -> [Id]
81 applyEnvList env = map (applyEnv env)
82
83 applyEnv :: IdEnv Id -> Id -> Id
84 applyEnv env id = lookupVarEnv env id `orElse` id
85
86 -- ----  Top-level right hand sides:
87
88 srtTopRhs :: Id -> StgRhs -> (StgRhs, [Id])
89
90 srtTopRhs _ rhs@(StgRhsCon _ _ _) = (rhs, [])
91 srtTopRhs _ rhs@(StgRhsClosure _ _ _ _  (SRTEntries cafs) _ _)
92   = (srtRhs table rhs, elems)
93   where
94         elems = varSetElems cafs
95         table = mkVarEnv (zip elems [0..])
96
97 -- ---- Binds:
98
99 srtBind :: IdEnv Int -> StgBinding -> StgBinding
100
101 srtBind table (StgNonRec binder rhs) = StgNonRec binder (srtRhs table rhs)
102 srtBind table (StgRec pairs) = StgRec [ (b, srtRhs table r) | (b,r) <- pairs ]
103
104 -- ---- Right Hand Sides:
105
106 srtRhs :: IdEnv Int -> StgRhs -> StgRhs
107
108 srtRhs _     e@(StgRhsCon _ _ _) = e
109 srtRhs table (StgRhsClosure cc bi free_vars u srt args body)
110   = StgRhsClosure cc bi free_vars u (constructSRT table srt) args 
111         $! (srtExpr table body)
112
113 -- ---------------------------------------------------------------------------
114 -- Expressions
115
116 srtExpr :: IdEnv Int -> StgExpr -> StgExpr
117
118 srtExpr _ e@(StgApp _ _)       = e
119 srtExpr _ e@(StgLit _)         = e
120 srtExpr _ e@(StgConApp _ _)    = e
121 srtExpr _ e@(StgOpApp _ _ _)   = e
122
123 srtExpr table (StgSCC cc expr) = StgSCC cc $! srtExpr table expr
124
125 srtExpr table (StgTick m n expr) = StgTick m n $! srtExpr table expr
126
127 srtExpr table (StgCase scrut live1 live2 uniq srt alt_type alts)
128  = StgCase expr' live1 live2 uniq srt' alt_type alts'
129  where
130    expr' = srtExpr table scrut
131    srt'  = constructSRT table srt
132    alts' = map (srtAlt table) alts
133
134 srtExpr table (StgLet bind body)
135   = srtBind table bind =: \ bind' ->
136     srtExpr table body             =: \ body' ->
137     StgLet bind' body'
138      
139 srtExpr table (StgLetNoEscape live1 live2 bind body)
140   = srtBind table bind =: \ bind' ->
141     srtExpr table body             =: \ body' ->
142     StgLetNoEscape live1 live2 bind' body'
143
144 srtExpr _table expr = pprPanic "srtExpr" (ppr expr)
145
146 srtAlt :: IdEnv Int -> StgAlt -> StgAlt
147 srtAlt table (con,args,used,rhs)
148   = (,,,) con args used $! srtExpr table rhs
149
150 -----------------------------------------------------------------------------
151 -- Construct an SRT bitmap.
152
153 constructSRT :: IdEnv Int -> SRT -> SRT
154 constructSRT table (SRTEntries entries)
155  | isEmptyVarSet entries = NoSRT
156  | otherwise  = seqBitmap bitmap $ SRT offset len bitmap
157   where
158     ints = map (expectJust "constructSRT" . lookupVarEnv table) 
159                 (varSetElems entries)
160     sorted_ints = sortLe (<=) ints
161     offset = head sorted_ints
162     bitmap_entries = map (subtract offset) sorted_ints
163     len = last bitmap_entries + 1
164     bitmap = intsToBitmap len bitmap_entries
165
166 -- ---------------------------------------------------------------------------
167 -- Misc stuff
168
169 (=:) :: a -> (a -> b) -> b
170 a =: k  = k a
171
172 \end{code}