2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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 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 _ [] lvs)
153 statExpr (StgApp _ _ lvs)
154 = countOne Applications
156 statExpr (StgCon con as lvs)
157 = countOne ConstructorApps
159 statExpr (StgPrim op as lvs)
160 = countOne PrimitiveApps
162 statExpr (StgSCC ty l e)
165 statExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
166 = statBinding False{-not top-level-} binds `combineSE`
167 statExpr body `combineSE`
168 countOne LetNoEscapes
170 statExpr (StgLet binds body)
171 = statBinding False{-not top-level-} binds `combineSE`
174 statExpr (StgCase expr lve lva uniq alts)
175 = statExpr expr `combineSE`
178 stat_alts (StgAlgAlts ty alts def)
179 = combineSEs (map statExpr [ e | (_,_,_,e) <- alts ])
181 stat_deflt def `combineSE`
184 stat_alts (StgPrimAlts ty alts def)
185 = combineSEs (map statExpr [ e | (_,e) <- alts ])
187 stat_deflt def `combineSE`
190 stat_deflt StgNoDefault = emptySE
192 stat_deflt (StgBindDefault b u expr) = statExpr expr