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