1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // DeclStats.cc -- |
---|
8 | // |
---|
9 | // Author : Aaron Moss |
---|
10 | // Created On : Wed Jan 31 16:40:00 2016 |
---|
11 | // Last Modified By : Aaron Moss |
---|
12 | // Last Modified On : Wed Jan 31 16:40:00 2016 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "DeclStats.h" |
---|
17 | |
---|
18 | #include <iostream> |
---|
19 | #include <map> |
---|
20 | #include <sstream> |
---|
21 | #include <string> |
---|
22 | #include <unordered_map> |
---|
23 | #include <unordered_set> |
---|
24 | |
---|
25 | #include "Common/VectorMap.h" |
---|
26 | #include "GenPoly/GenPoly.h" |
---|
27 | #include "Parser/LinkageSpec.h" |
---|
28 | #include "SynTree/Declaration.h" |
---|
29 | #include "SynTree/Visitor.h" |
---|
30 | |
---|
31 | namespace CodeTools { |
---|
32 | |
---|
33 | class DeclStats : public Visitor { |
---|
34 | template<typename T> |
---|
35 | static void sum(T& a, const T& b) { a += b; } |
---|
36 | |
---|
37 | static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) { |
---|
38 | a.reserve( b.size() ); |
---|
39 | for ( unsigned i = 0; i < b.size(); ++i ) { |
---|
40 | a[i] += b[i]; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | template<typename K> |
---|
45 | static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) { |
---|
46 | for ( const auto& entry : b ) { |
---|
47 | a[ entry.first ] += entry.second; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | template<typename K> |
---|
52 | static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) { |
---|
53 | for ( const auto& entry : b ) { |
---|
54 | a[ entry.first ] += entry.second; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | struct ArgPackStats { |
---|
59 | VectorMap<unsigned> n; ///< Count of decls with each number of elements |
---|
60 | VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements |
---|
61 | VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements |
---|
62 | std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements |
---|
63 | std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements |
---|
64 | VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack |
---|
65 | |
---|
66 | ArgPackStats& operator+= (const ArgPackStats& o) { |
---|
67 | sum(n, o.n); |
---|
68 | sum(n_basic, o.n_basic); |
---|
69 | sum(n_poly, o.n_poly); |
---|
70 | sum(p_basic, o.p_basic); |
---|
71 | sum(p_poly, o.p_poly); |
---|
72 | sum(n_types, o.n_types); |
---|
73 | |
---|
74 | return *this; |
---|
75 | } |
---|
76 | }; |
---|
77 | |
---|
78 | struct Stats { |
---|
79 | unsigned n_decls; ///< Total number of declarations |
---|
80 | /// Count of declarations with each number of assertion parameters |
---|
81 | VectorMap<unsigned> n_type_params; |
---|
82 | /// Count of declarations with each name |
---|
83 | std::unordered_map<std::string, unsigned> by_name; |
---|
84 | /// Count of uses of each basic type |
---|
85 | std::unordered_map<std::string, unsigned> basic_type_names; |
---|
86 | /// Count of uses of each non-basic type |
---|
87 | std::unordered_map<std::string, unsigned> compound_type_names; |
---|
88 | /// Stats for the parameter list |
---|
89 | ArgPackStats params; |
---|
90 | /// Stats for the return list |
---|
91 | ArgPackStats returns; |
---|
92 | |
---|
93 | /// Count of declarations with each number of assertions |
---|
94 | std::map<unsigned, unsigned> n_assns; |
---|
95 | /// Stats for the assertions' parameters |
---|
96 | ArgPackStats assn_params; |
---|
97 | /// Stats for the assertions' return types |
---|
98 | ArgPackStats assn_returns; |
---|
99 | |
---|
100 | Stats() : n_decls(0), n_type_params(), by_name(), basic_type_names(), compound_type_names(), params(), returns(), n_assns(), assn_params(), assn_returns() {} |
---|
101 | |
---|
102 | public: |
---|
103 | Stats& operator+= (const Stats& o) { |
---|
104 | sum( n_decls, o.n_decls ); |
---|
105 | sum( n_type_params, o.n_type_params ); |
---|
106 | sum( by_name, o.by_name ); |
---|
107 | sum( basic_type_names, o.basic_type_names ); |
---|
108 | sum( compound_type_names, o.compound_type_names ); |
---|
109 | sum( params, o.params ); |
---|
110 | sum( returns, o.returns ); |
---|
111 | sum( n_assns, o.n_assns ); |
---|
112 | sum( assn_params, o.assn_params ); |
---|
113 | sum( assn_returns, o.assn_returns ); |
---|
114 | |
---|
115 | return *this; |
---|
116 | } |
---|
117 | }; |
---|
118 | |
---|
119 | Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage |
---|
120 | std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting |
---|
121 | Stats total; |
---|
122 | |
---|
123 | /// Update arg pack stats based on a declaration list |
---|
124 | void analyze( Stats& stats, ArgPackStats& pstats, std::list<DeclarationWithType*>& decls ) { |
---|
125 | std::unordered_set<std::string> types; |
---|
126 | unsigned n = 0; |
---|
127 | unsigned n_basic = 0; |
---|
128 | unsigned n_poly = 0; |
---|
129 | for ( auto decl : decls ) { |
---|
130 | Type* dt = decl->get_type(); |
---|
131 | |
---|
132 | n += dt->size(); |
---|
133 | |
---|
134 | std::stringstream ss; |
---|
135 | dt->print( ss ); |
---|
136 | types.insert( ss.str() ); |
---|
137 | |
---|
138 | if ( dynamic_cast<BasicType*>( dt ) ) { |
---|
139 | ++n_basic; |
---|
140 | ++stats.basic_type_names[ ss.str() ]; |
---|
141 | } else if ( GenPoly::hasPolyBase( dt ) ) { |
---|
142 | ++n_poly; |
---|
143 | } else { |
---|
144 | ++stats.compound_type_names[ ss.str() ]; |
---|
145 | } |
---|
146 | } |
---|
147 | ++pstats.n.at( n ); |
---|
148 | ++pstats.n_basic.at( n_basic ); |
---|
149 | ++pstats.n_poly.at( n_poly ); |
---|
150 | if ( n > 0 ) { |
---|
151 | ++pstats.p_basic[ n_basic*100/n ]; |
---|
152 | ++pstats.p_poly[ n_poly*100/n ]; |
---|
153 | } |
---|
154 | ++pstats.n_types.at( types.size() ); |
---|
155 | } |
---|
156 | |
---|
157 | void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) { |
---|
158 | analyze( stats, params, fnTy->get_parameters() ); |
---|
159 | analyze( stats, returns, fnTy->get_returnVals() ); |
---|
160 | } |
---|
161 | |
---|
162 | public: |
---|
163 | using Visitor::visit; |
---|
164 | |
---|
165 | virtual void visit( FunctionDecl *decl ) { |
---|
166 | // skip if already seen declaration for this function |
---|
167 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName(); |
---|
168 | if ( ! seen_names.insert( mangleName ).second ) return; |
---|
169 | |
---|
170 | Stats& stats = for_linkage[ decl->get_linkage() ]; |
---|
171 | |
---|
172 | ++stats.n_decls; |
---|
173 | FunctionType* fnTy = decl->get_functionType(); |
---|
174 | const Type::ForallList& forall = fnTy->get_forall(); |
---|
175 | ++stats.n_type_params.at( forall.size() ); |
---|
176 | unsigned n_assns = 0; |
---|
177 | for ( TypeDecl* fdecl : forall ) { |
---|
178 | n_assns += fdecl->get_assertions().size(); |
---|
179 | for ( DeclarationWithType* assn : fdecl->get_assertions() ) { |
---|
180 | FunctionType *assnTy = 0; |
---|
181 | if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { |
---|
182 | if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { |
---|
183 | assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base()); |
---|
184 | } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); |
---|
185 | } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { |
---|
186 | assnTy = assnDecl->get_functionType(); |
---|
187 | } |
---|
188 | if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); |
---|
189 | } |
---|
190 | } |
---|
191 | ++stats.n_assns[ n_assns ]; |
---|
192 | |
---|
193 | ++stats.by_name[ decl->get_name() ]; |
---|
194 | |
---|
195 | analyzeFunc( fnTy, stats, stats.params, stats.returns ); |
---|
196 | } |
---|
197 | |
---|
198 | private: |
---|
199 | template<typename F> |
---|
200 | void printAll( const std::string& name, F extract ) { |
---|
201 | std::cout << "\"" << name << "\","; |
---|
202 | for ( const auto& stats : for_linkage ) { |
---|
203 | std::cout << "," << extract(stats); |
---|
204 | } |
---|
205 | std::cout << "," << extract(total) << std::endl; |
---|
206 | } |
---|
207 | |
---|
208 | template<typename F> |
---|
209 | void printAllMap( const std::string& name, F extract ) { |
---|
210 | for ( const auto& entry : extract(total) ) { |
---|
211 | const auto& key = entry.first; |
---|
212 | std::cout << "\"" << name << "\"," << key; |
---|
213 | for ( const auto& stats : for_linkage ) { |
---|
214 | const auto& map = extract(stats); |
---|
215 | auto it = map.find( key ); |
---|
216 | if ( it == map.end() ) std::cout << ",0"; |
---|
217 | else std::cout << "," << it->second; |
---|
218 | } |
---|
219 | std::cout << "," << entry.second << std::endl; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | template<typename F> |
---|
224 | void printAllHisto( const std::string& name, F extract ) { |
---|
225 | VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
226 | VectorMap<unsigned> thisto; |
---|
227 | |
---|
228 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } |
---|
229 | |
---|
230 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
231 | // can't be a higher count in one of the sub-histograms than the total |
---|
232 | histos[i].reserve( thisto.size() ); |
---|
233 | |
---|
234 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
235 | } |
---|
236 | |
---|
237 | for ( unsigned i = 0; i < thisto.size(); ++i ) { |
---|
238 | std::cout << "\"" << name << "\"," << i; |
---|
239 | for ( const auto& histo : histos ) { |
---|
240 | std::cout << "," << histo[i]; |
---|
241 | } |
---|
242 | std::cout << "," << thisto[i] << std::endl; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | template<typename F> |
---|
247 | void printAllSparseHisto( const std::string& name, F extract ) { |
---|
248 | std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
249 | std::map<unsigned, unsigned> thisto; |
---|
250 | |
---|
251 | for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } |
---|
252 | |
---|
253 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
254 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
255 | } |
---|
256 | |
---|
257 | for ( const auto& entry : thisto ) { |
---|
258 | const auto& key = entry.first; |
---|
259 | std::cout << "\"" << name << "\"," << key; |
---|
260 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
261 | auto it = histos[i].find( key ); |
---|
262 | if ( it == histos[i].end() ) std::cout << ",0"; |
---|
263 | else std::cout << "," << it->second; |
---|
264 | } |
---|
265 | std::cout << "," << entry.second << std::endl; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | template<typename F> |
---|
270 | void printAllPack( const std::string& name, F extract ) { |
---|
271 | printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); |
---|
272 | printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); |
---|
273 | printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); |
---|
274 | printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); |
---|
275 | printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); |
---|
276 | printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; }); |
---|
277 | } |
---|
278 | |
---|
279 | public: |
---|
280 | void print() { |
---|
281 | for ( auto& stats : for_linkage ) { |
---|
282 | total += stats; |
---|
283 | } |
---|
284 | |
---|
285 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl; |
---|
286 | |
---|
287 | printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); |
---|
288 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); |
---|
289 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); |
---|
290 | printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; }); |
---|
291 | printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); }); |
---|
292 | printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; }); |
---|
293 | printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); }); |
---|
294 | printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; }); |
---|
295 | printAllPack("params", [](const Stats& stats) { return stats.params; }); |
---|
296 | printAllPack("returns", [](const Stats& stats) { return stats.returns; }); |
---|
297 | printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; }); |
---|
298 | printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; }); |
---|
299 | printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; }); |
---|
300 | } |
---|
301 | }; |
---|
302 | |
---|
303 | void printDeclStats( std::list< Declaration * > &translationUnit ) { |
---|
304 | DeclStats stats; |
---|
305 | acceptAll( translationUnit, stats ); |
---|
306 | stats.print(); |
---|
307 | } |
---|
308 | |
---|
309 | } // namespace CodeTools |
---|
310 | |
---|
311 | // Local Variables: // |
---|
312 | // tab-width: 4 // |
---|
313 | // mode: c++ // |
---|
314 | // compile-command: "make install" // |
---|
315 | // End: // |
---|