2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[StgStats]{Gathers statistical information about programs}
7 The program gather statistics about
9 \item number of boxed cases
10 \item number of unboxed cases
11 \item number of let-no-escapes
12 \item number of non-updatable lets
13 \item number of updatable lets
14 \item number of applications
15 \item number of primitive applications
16 \item number of closures (does not include lets bound to constructors)
17 \item number of free variables in closures
18 %\item number of top-level functions
19 %\item number of top-level CAFs
20 \item number of constructors
24 module StgStats ( showStgStats ) where
26 #include "HsVersions.h"
30 import FiniteMap ( emptyFM, plusFM_C, unitFM, fmToList, FiniteMap )
44 | ConstructorBinds Bool{-True<=>top-level-}
45 | ReEntrantBinds Bool{-ditto-}
46 | SingleEntryBinds Bool{-ditto-}
47 | UpdatableBinds Bool{-ditto-}
51 type StatEnv = FiniteMap CounterType Count
58 combineSE :: StatEnv -> StatEnv -> StatEnv
59 combineSE = plusFM_C (+)
61 combineSEs :: [StatEnv] -> StatEnv
62 combineSEs = foldr combineSE emptySE
64 countOne :: CounterType -> StatEnv
65 countOne c = unitFM c 1
67 countN :: CounterType -> Int -> StatEnv
71 %************************************************************************
73 \subsection{Top-level list of bindings (a ``program'')}
75 %************************************************************************
78 showStgStats :: [StgBinding] -> String
81 = "STG Statistics:\n\n"
82 ++ concat (map showc (fmToList (gatherStgStats prog)))
84 showc (x,n) = (showString (s x) . shows n) "\n"
86 s Literals = "Literals "
87 s Applications = "Applications "
88 s ConstructorApps = "ConstructorApps "
89 s PrimitiveApps = "PrimitiveApps "
90 s LetNoEscapes = "LetNoEscapes "
91 s AlgCases = "AlgCases "
92 s PrimCases = "PrimCases "
93 s FreeVariables = "FreeVariables "
94 s (ConstructorBinds True) = "ConstructorBinds_Top "
95 s (ReEntrantBinds True) = "ReEntrantBinds_Top "
96 s (SingleEntryBinds True) = "SingleEntryBinds_Top "
97 s (UpdatableBinds True) = "UpdatableBinds_Top "
98 s (ConstructorBinds _) = "ConstructorBinds_Nested "
99 s (ReEntrantBinds _) = "ReEntrantBindsBinds_Nested "
100 s (SingleEntryBinds _) = "SingleEntryBinds_Nested "
101 s (UpdatableBinds _) = "UpdatableBinds_Nested "
103 gatherStgStats :: [StgBinding] -> StatEnv
106 = combineSEs (map (statBinding True{-top-level-}) binds)
109 %************************************************************************
111 \subsection{Bindings}
113 %************************************************************************
116 statBinding :: Bool -- True <=> top-level; False <=> nested
120 statBinding top (StgNonRec b rhs)
121 = statRhs top (b, rhs)
123 statBinding top (StgRec pairs)
124 = combineSEs (map (statRhs top) pairs)
126 statRhs :: Bool -> (Id, StgRhs) -> StatEnv
128 statRhs top (b, StgRhsCon cc con args)
129 = countOne (ConstructorBinds top)
131 statRhs top (b, StgRhsClosure cc bi fv u _srt args body)
132 = statExpr body `combineSE`
133 countN FreeVariables (length fv) `combineSE`
136 ReEntrant -> ReEntrantBinds top
137 Updatable -> UpdatableBinds top
138 SingleEntry -> SingleEntryBinds top
142 %************************************************************************
144 \subsection{Expressions}
146 %************************************************************************
149 statExpr :: StgExpr -> StatEnv
151 statExpr (StgApp _ _) = countOne Applications
152 statExpr (StgLit _) = countOne Literals
153 statExpr (StgConApp _ _) = countOne ConstructorApps
154 statExpr (StgOpApp _ _ _) = countOne PrimitiveApps
155 statExpr (StgSCC l e) = statExpr e
157 statExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
158 = statBinding False{-not top-level-} binds `combineSE`
159 statExpr body `combineSE`
160 countOne LetNoEscapes
162 statExpr (StgLet binds body)
163 = statBinding False{-not top-level-} binds `combineSE`
166 statExpr (StgCase expr lve lva bndr srt alts)
167 = statExpr expr `combineSE`
170 stat_alts (StgAlgAlts ty alts def)
171 = combineSEs (map statExpr [ e | (_,_,_,e) <- alts ])
173 stat_deflt def `combineSE`
176 stat_alts (StgPrimAlts ty alts def)
177 = combineSEs (map statExpr [ e | (_,e) <- alts ])
179 stat_deflt def `combineSE`
182 stat_deflt StgNoDefault = emptySE
184 stat_deflt (StgBindDefault expr) = statExpr expr