2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
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 #include "HsVersions.h"
26 module StgStats ( showStgStats ) where
45 | Closures -- does not include lets bound to constructors
46 --| UpdatableTopLevelDefs
47 --| NonUpdatableTopLevelDefs
49 deriving (Eq, Ord, Text)
52 type StatEnv = FiniteMap CounterType Count
59 combineSE :: StatEnv -> StatEnv -> StatEnv
60 combineSE = plusFM_C (+)
62 combineSEs :: [StatEnv] -> StatEnv
63 combineSEs = foldr combineSE emptySE
65 countOne :: CounterType -> StatEnv
66 countOne c = singletonFM c 1
68 countN :: CounterType -> Int -> StatEnv
72 %************************************************************************
74 \subsection{Top-level list of bindings (a ``program'')}
76 %************************************************************************
79 showStgStats :: PlainStgProgram -> String
80 showStgStats prog = concat (map showc (fmToList (gatherStgStats prog)))
82 showc (AlgCases,n) = "AlgCases " ++ show n ++ "\n"
83 showc (PrimCases,n) = "PrimCases " ++ show n ++ "\n"
84 showc (LetNoEscapes,n) = "LetNoEscapes " ++ show n ++ "\n"
85 showc (NonUpdatableLets,n) = "NonUpdatableLets " ++ show n ++ "\n"
86 showc (UpdatableLets,n) = "UpdatableLets " ++ show n ++ "\n"
87 showc (Applications,n) = "Applications " ++ show n ++ "\n"
88 showc (PrimitiveApps,n) = "PrimitiveApps " ++ show n ++ "\n"
89 showc (Closures,n) = "Closures " ++ show n ++ "\n"
90 showc (FreeVariables,n) = "Free Vars in Closures " ++ show n ++ "\n"
91 showc (Constructors,n) = "Constructors " ++ show n ++ "\n"
93 gatherStgStats :: PlainStgProgram -> StatEnv
96 = combineSEs (map statBinding binds)
99 %************************************************************************
101 \subsection{Bindings}
103 %************************************************************************
106 statBinding :: PlainStgBinding -> StatEnv
108 statBinding (StgNonRec b rhs)
111 statBinding (StgRec pairs)
112 = combineSEs (map statRhs pairs)
114 statRhs :: (Id, PlainStgRhs) -> StatEnv
116 statRhs (b, StgRhsCon cc con args)
117 = countOne Constructors `combineSE`
118 countOne NonUpdatableLets
120 statRhs (b, StgRhsClosure cc bi fv u args body)
121 = statExpr body `combineSE`
122 countN FreeVariables (length fv) `combineSE`
123 countOne Closures `combineSE`
125 Updatable -> countOne UpdatableLets
126 _ -> countOne NonUpdatableLets)
130 %************************************************************************
132 \subsection{Expressions}
134 %************************************************************************
137 statExpr :: PlainStgExpr -> StatEnv
139 statExpr (StgApp _ [] lvs)
141 statExpr (StgApp _ _ lvs)
142 = countOne Applications
144 statExpr (StgConApp con as lvs)
145 = countOne Constructors
147 statExpr (StgPrimApp op as lvs)
148 = countOne PrimitiveApps
150 statExpr (StgSCC ty l e)
153 statExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
154 = statBinding binds `combineSE`
155 statExpr body `combineSE`
156 countOne LetNoEscapes
158 statExpr (StgLet binds body)
159 = statBinding binds `combineSE`
162 statExpr (StgCase expr lve lva uniq alts)
163 = statExpr expr `combineSE`
166 stat_alts (StgAlgAlts ty alts def)
167 = combineSEs (map stat_alg_alt alts) `combineSE`
168 stat_deflt def `combineSE`
171 stat_alg_alt (id, bs, use_mask, e)
174 stat_alts (StgPrimAlts ty alts def)
175 = combineSEs (map stat_prim_alt alts) `combineSE`
176 stat_deflt def `combineSE`
182 stat_deflt StgNoDefault
185 stat_deflt (StgBindDefault b u expr)