summaryrefslogtreecommitdiff
path: root/ghc/compiler/simplStg/StgStats.lhs
blob: 2b16fc06c9a462610e6236dc6ebe3d93bfb9ba44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1994
%
\section[StgStats]{Gathers statistical information about programs}


The program gather statistics about
\begin{enumerate}
\item number of boxed cases
\item number of unboxed cases
\item number of let-no-escapes
\item number of non-updatable lets
\item number of updatable lets
\item number of applications
\item number of primitive applications
\item number of closures (does not include lets bound to constructors)
\item number of free variables in closures
%\item number of top-level functions
%\item number of top-level CAFs
\item number of constructors
\end{enumerate}

\begin{code}
#include "HsVersions.h"

module StgStats ( showStgStats ) where

import StgSyn

import FiniteMap

import Util
\end{code}

\begin{code}
data CounterType
  = AlgCases
  | PrimCases
  | LetNoEscapes
  | NonUpdatableLets
  | UpdatableLets
  | Applications
  | PrimitiveApps
  | FreeVariables
  | Closures	-- does not include lets bound to constructors
--| UpdatableTopLevelDefs
--| NonUpdatableTopLevelDefs
  | Constructors
  deriving (Eq, Ord, Text)

type Count	= Int
type StatEnv	= FiniteMap CounterType Count
\end{code}

\begin{code}
emptySE	:: StatEnv
emptySE	= emptyFM

combineSE :: StatEnv -> StatEnv -> StatEnv
combineSE = plusFM_C (+)

combineSEs :: [StatEnv] -> StatEnv
combineSEs = foldr combineSE emptySE

countOne :: CounterType -> StatEnv
countOne c = singletonFM c 1

countN :: CounterType -> Int -> StatEnv
countN = singletonFM
\end{code}

%************************************************************************
%*									*
\subsection{Top-level list of bindings (a ``program'')}
%*									*
%************************************************************************

\begin{code}
showStgStats :: PlainStgProgram -> String
showStgStats prog = concat (map showc (fmToList (gatherStgStats prog)))
  where
    showc (AlgCases,n)         = "AlgCases               " ++ show n ++ "\n"
    showc (PrimCases,n)        = "PrimCases              " ++ show n ++ "\n"
    showc (LetNoEscapes,n)     = "LetNoEscapes           " ++ show n ++ "\n"
    showc (NonUpdatableLets,n) = "NonUpdatableLets       " ++ show n ++ "\n"
    showc (UpdatableLets,n)    = "UpdatableLets          " ++ show n ++ "\n"
    showc (Applications,n)     = "Applications           " ++ show n ++ "\n"
    showc (PrimitiveApps,n)    = "PrimitiveApps          " ++ show n ++ "\n"
    showc (Closures,n)         = "Closures               " ++ show n ++ "\n"
    showc (FreeVariables,n)    = "Free Vars in Closures  " ++ show n ++ "\n"
    showc (Constructors,n)     = "Constructors           " ++ show n ++ "\n"

gatherStgStats :: PlainStgProgram -> StatEnv

gatherStgStats binds 
  = combineSEs (map statBinding binds)
\end{code}

%************************************************************************
%*									*
\subsection{Bindings}
%*									*
%************************************************************************

\begin{code}
statBinding :: PlainStgBinding -> StatEnv

statBinding (StgNonRec b rhs)
  = statRhs (b, rhs)

statBinding (StgRec pairs)
  = combineSEs (map statRhs pairs)

statRhs :: (Id, PlainStgRhs) -> StatEnv

statRhs (b, StgRhsCon cc con args)
  = countOne Constructors		`combineSE` 
    countOne NonUpdatableLets

statRhs (b, StgRhsClosure cc bi fv u args body)
  = statExpr body			`combineSE` 
    countN FreeVariables (length fv)	`combineSE`
    countOne Closures			`combineSE` 
    (case u of
       Updatable -> countOne UpdatableLets
       _         -> countOne NonUpdatableLets)

\end{code}

%************************************************************************
%*									*
\subsection{Expressions}
%*									*
%************************************************************************

\begin{code}    
statExpr :: PlainStgExpr -> StatEnv

statExpr (StgApp _ [] lvs) 
  = emptySE
statExpr (StgApp _ _ lvs) 
  = countOne Applications

statExpr (StgConApp con as lvs)
  = countOne Constructors

statExpr (StgPrimApp op as lvs)
  = countOne PrimitiveApps

statExpr (StgSCC ty l e)
  = statExpr e

statExpr (StgLetNoEscape lvs_whole lvs_rhss binds body)
  = statBinding binds	`combineSE`
    statExpr body	`combineSE` 
    countOne LetNoEscapes

statExpr (StgLet binds body)
  = statBinding binds	`combineSE` 
    statExpr body

statExpr (StgCase expr lve lva uniq alts)
  = statExpr expr	`combineSE`
    stat_alts alts
    where
      stat_alts (StgAlgAlts ty alts def)
	= combineSEs (map stat_alg_alt alts)	`combineSE` 
	  stat_deflt def			`combineSE`
	  countOne AlgCases
	where
	  stat_alg_alt (id, bs, use_mask, e)
	    = statExpr e

      stat_alts (StgPrimAlts ty alts def)
	= combineSEs (map stat_prim_alt alts) 	`combineSE`
	  stat_deflt def			`combineSE`
	  countOne PrimCases
	where
	  stat_prim_alt (l, e)
	    = statExpr e

      stat_deflt StgNoDefault
	= emptySE

      stat_deflt (StgBindDefault b u expr)
	= statExpr expr	
\end{code}