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 {-# OPTIONS -fno-warn-incomplete-patterns #-}
25 -- The above warning supression flag is a temporary kludge.
26 -- While working on this module you are encouraged to remove it and fix
27 -- any warnings in the module. See
28 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
31 module StgStats ( showStgStats ) where
33 #include "HsVersions.h"
37 import FiniteMap ( emptyFM, plusFM_C, unitFM, fmToList, FiniteMap )
50 | ConstructorBinds Bool{-True<=>top-level-}
51 | ReEntrantBinds Bool{-ditto-}
52 | SingleEntryBinds Bool{-ditto-}
53 | UpdatableBinds Bool{-ditto-}
57 type StatEnv = FiniteMap CounterType Count
64 combineSE :: StatEnv -> StatEnv -> StatEnv
65 combineSE = plusFM_C (+)
67 combineSEs :: [StatEnv] -> StatEnv
68 combineSEs = foldr combineSE emptySE
70 countOne :: CounterType -> StatEnv
71 countOne c = unitFM c 1
73 countN :: CounterType -> Int -> StatEnv
77 %************************************************************************
79 \subsection{Top-level list of bindings (a ``program'')}
81 %************************************************************************
84 showStgStats :: [StgBinding] -> String
87 = "STG Statistics:\n\n"
88 ++ concat (map showc (fmToList (gatherStgStats prog)))
90 showc (x,n) = (showString (s x) . shows n) "\n"
92 s Literals = "Literals "
93 s Applications = "Applications "
94 s ConstructorApps = "ConstructorApps "
95 s PrimitiveApps = "PrimitiveApps "
96 s LetNoEscapes = "LetNoEscapes "
97 s StgCases = "StgCases "
98 s FreeVariables = "FreeVariables "
99 s (ConstructorBinds True) = "ConstructorBinds_Top "
100 s (ReEntrantBinds True) = "ReEntrantBinds_Top "
101 s (SingleEntryBinds True) = "SingleEntryBinds_Top "
102 s (UpdatableBinds True) = "UpdatableBinds_Top "
103 s (ConstructorBinds _) = "ConstructorBinds_Nested "
104 s (ReEntrantBinds _) = "ReEntrantBindsBinds_Nested "
105 s (SingleEntryBinds _) = "SingleEntryBinds_Nested "
106 s (UpdatableBinds _) = "UpdatableBinds_Nested "
108 gatherStgStats :: [StgBinding] -> StatEnv
111 = combineSEs (map (statBinding True{-top-level-}) binds)
114 %************************************************************************
116 \subsection{Bindings}
118 %************************************************************************
121 statBinding :: Bool -- True <=> top-level; False <=> nested
125 statBinding top (StgNonRec b rhs)
126 = statRhs top (b, rhs)
128 statBinding top (StgRec pairs)
129 = combineSEs (map (statRhs top) pairs)
131 statRhs :: Bool -> (Id, StgRhs) -> StatEnv
133 statRhs top (_, StgRhsCon _ _ _)
134 = countOne (ConstructorBinds top)
136 statRhs top (_, StgRhsClosure _ _ fv u _ _ body)
137 = statExpr body `combineSE`
138 countN FreeVariables (length fv) `combineSE`
141 ReEntrant -> ReEntrantBinds top
142 Updatable -> UpdatableBinds top
143 SingleEntry -> SingleEntryBinds top
147 %************************************************************************
149 \subsection{Expressions}
151 %************************************************************************
154 statExpr :: StgExpr -> StatEnv
156 statExpr (StgApp _ _) = countOne Applications
157 statExpr (StgLit _) = countOne Literals
158 statExpr (StgConApp _ _) = countOne ConstructorApps
159 statExpr (StgOpApp _ _ _) = countOne PrimitiveApps
160 statExpr (StgSCC _ e) = statExpr e
161 statExpr (StgTick _ _ e) = statExpr e
163 statExpr (StgLetNoEscape _ _ binds body)
164 = statBinding False{-not top-level-} binds `combineSE`
165 statExpr body `combineSE`
166 countOne LetNoEscapes
168 statExpr (StgLet binds body)
169 = statBinding False{-not top-level-} binds `combineSE`
172 statExpr (StgCase expr _ _ _ _ _ alts)
173 = statExpr expr `combineSE`
174 stat_alts alts `combineSE`
178 = combineSEs (map statExpr [ e | (_,_,_,e) <- alts ])