source: src/CodeTools/DeclStats.cc@ 356c62a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 356c62a was 131dbb3, checked in by Aaron Moss <a3moss@…>, 9 years ago

More decl stats

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[41a7137]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>
[567903e]19#include <map>
[f923b5f]20#include <sstream>
[fa2de95]21#include <string>
22#include <unordered_map>
23#include <unordered_set>
24
25#include "Common/VectorMap.h"
[6215a5c]26#include "GenPoly/GenPoly.h"
[fa2de95]27#include "Parser/LinkageSpec.h"
28#include "SynTree/Declaration.h"
29#include "SynTree/Visitor.h"
[41a7137]30
31namespace CodeTools {
[fa2de95]32
33 class DeclStats : public Visitor {
[424931d]34 template<typename T>
35 static void sum(T& a, const T& b) { a += b; }
[6a5f0e7]36
[424931d]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 }
[fa2de95]43
[424931d]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;
[6a5f0e7]48 }
[424931d]49 }
[6215a5c]50
[424931d]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;
[6a5f0e7]55 }
[424931d]56 }
[6215a5c]57
[424931d]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
[eed5e48]64 VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack
[131dbb3]65 /// Count of decls with each percentage of new types in lists.
66 /// Types used in the parameter list that recur in the return list are not considered to be new.
67 std::map<unsigned, unsigned> p_new;
[424931d]68
69 ArgPackStats& operator+= (const ArgPackStats& o) {
70 sum(n, o.n);
71 sum(n_basic, o.n_basic);
72 sum(n_poly, o.n_poly);
73 sum(p_basic, o.p_basic);
74 sum(p_poly, o.p_poly);
[eed5e48]75 sum(n_types, o.n_types);
[131dbb3]76 sum(p_new, o.p_new);
[424931d]77
78 return *this;
79 }
80 };
81
82 struct Stats {
83 unsigned n_decls; ///< Total number of declarations
84 /// Count of declarations with each number of assertion parameters
85 VectorMap<unsigned> n_type_params;
86 /// Count of declarations with each name
87 std::unordered_map<std::string, unsigned> by_name;
[f923b5f]88 /// Count of uses of each basic type
89 std::unordered_map<std::string, unsigned> basic_type_names;
90 /// Count of uses of each non-basic type
91 std::unordered_map<std::string, unsigned> compound_type_names;
[131dbb3]92 /// Count of decls using each basic type
93 std::unordered_map<std::string, unsigned> basic_type_decls;
94 /// Count of decls using each compound type
95 std::unordered_map<std::string, unsigned> compound_type_decls;
[424931d]96 /// Stats for the parameter list
97 ArgPackStats params;
98 /// Stats for the return list
99 ArgPackStats returns;
100
101 /// Count of declarations with each number of assertions
102 std::map<unsigned, unsigned> n_assns;
103 /// Stats for the assertions' parameters
104 ArgPackStats assn_params;
105 /// Stats for the assertions' return types
106 ArgPackStats assn_returns;
107
[131dbb3]108 Stats() : n_decls(0), n_type_params(), by_name(), basic_type_names(), compound_type_names(), basic_type_decls(), compound_type_decls(), params(), returns(), n_assns(), assn_params(), assn_returns() {}
[567903e]109
[6a5f0e7]110 public:
111 Stats& operator+= (const Stats& o) {
112 sum( n_decls, o.n_decls );
113 sum( n_type_params, o.n_type_params );
114 sum( by_name, o.by_name );
[f923b5f]115 sum( basic_type_names, o.basic_type_names );
116 sum( compound_type_names, o.compound_type_names );
[131dbb3]117 sum( basic_type_decls, o.basic_type_decls );
118 sum( compound_type_decls, o.compound_type_decls );
[424931d]119 sum( params, o.params );
120 sum( returns, o.returns );
121 sum( n_assns, o.n_assns );
122 sum( assn_params, o.assn_params );
123 sum( assn_returns, o.assn_returns );
[6a5f0e7]124
[fa2de95]125 return *this;
126 }
127 };
128
129 Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage
130 std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting
131 Stats total;
132
[f923b5f]133 /// Update arg pack stats based on a declaration list
[131dbb3]134 void analyze( Stats& stats, std::unordered_set<std::string>& seen, ArgPackStats& pstats, std::list<DeclarationWithType*>& decls ) {
[eed5e48]135 std::unordered_set<std::string> types;
[131dbb3]136 unsigned n = 0; ///< number of args/returns
137 unsigned n_basic = 0; ///< number of basic types
138 unsigned n_poly = 0; ///< number of polymorphic types
139 unsigned n_new = 0; ///< number of new types
[f923b5f]140 for ( auto decl : decls ) {
[eed5e48]141 Type* dt = decl->get_type();
142
143 n += dt->size();
144
145 std::stringstream ss;
146 dt->print( ss );
147 types.insert( ss.str() );
[131dbb3]148 bool this_new = seen.insert( ss.str() ).second;
149 if ( this_new ) { ++n_new; }
[eed5e48]150
151 if ( dynamic_cast<BasicType*>( dt ) ) {
[f923b5f]152 ++n_basic;
153 ++stats.basic_type_names[ ss.str() ];
[131dbb3]154 if ( this_new ) {
155 ++stats.basic_type_decls[ ss.str() ];
156 }
[eed5e48]157 } else if ( GenPoly::hasPolyBase( dt ) ) {
[f923b5f]158 ++n_poly;
159 } else {
160 ++stats.compound_type_names[ ss.str() ];
[131dbb3]161 if ( this_new ) {
162 ++stats.compound_type_decls[ ss.str() ];
163 }
[f923b5f]164 }
165 }
166 ++pstats.n.at( n );
167 ++pstats.n_basic.at( n_basic );
168 ++pstats.n_poly.at( n_poly );
169 if ( n > 0 ) {
170 ++pstats.p_basic[ n_basic*100/n ];
171 ++pstats.p_poly[ n_poly*100/n ];
[131dbb3]172 ++pstats.p_new[ n_new*100/n ];
[f923b5f]173 }
[eed5e48]174 ++pstats.n_types.at( types.size() );
[f923b5f]175 }
176
177 void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) {
[131dbb3]178 std::unordered_set<std::string> seen;
179 analyze( stats, seen, params, fnTy->get_parameters() );
180 analyze( stats, seen, returns, fnTy->get_returnVals() );
[6a5f0e7]181 }
182
[fa2de95]183 public:
184 using Visitor::visit;
185
186 virtual void visit( FunctionDecl *decl ) {
187 // skip if already seen declaration for this function
188 const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName();
189 if ( ! seen_names.insert( mangleName ).second ) return;
190
191 Stats& stats = for_linkage[ decl->get_linkage() ];
192
193 ++stats.n_decls;
194 FunctionType* fnTy = decl->get_functionType();
[6a5f0e7]195 const Type::ForallList& forall = fnTy->get_forall();
196 ++stats.n_type_params.at( forall.size() );
[424931d]197 unsigned n_assns = 0;
[6a5f0e7]198 for ( TypeDecl* fdecl : forall ) {
[424931d]199 n_assns += fdecl->get_assertions().size();
[6a5f0e7]200 for ( DeclarationWithType* assn : fdecl->get_assertions() ) {
201 FunctionType *assnTy = 0;
202 if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) {
203 if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) {
204 assnTy = dynamic_cast<FunctionType*>(ptrTy->get_base());
205 } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type());
206 } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) {
207 assnTy = assnDecl->get_functionType();
208 }
[f923b5f]209 if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns );
[6a5f0e7]210 }
211 }
[424931d]212 ++stats.n_assns[ n_assns ];
[fa2de95]213
214 ++stats.by_name[ decl->get_name() ];
215
[f923b5f]216 analyzeFunc( fnTy, stats, stats.params, stats.returns );
[fa2de95]217 }
218
219 private:
220 template<typename F>
[424931d]221 void printAll( const std::string& name, F extract ) {
[fa2de95]222 std::cout << "\"" << name << "\",";
223 for ( const auto& stats : for_linkage ) {
224 std::cout << "," << extract(stats);
225 }
226 std::cout << "," << extract(total) << std::endl;
227 }
228
229 template<typename F>
[424931d]230 void printAllMap( const std::string& name, F extract ) {
[fa2de95]231 for ( const auto& entry : extract(total) ) {
232 const auto& key = entry.first;
233 std::cout << "\"" << name << "\"," << key;
234 for ( const auto& stats : for_linkage ) {
235 const auto& map = extract(stats);
236 auto it = map.find( key );
237 if ( it == map.end() ) std::cout << ",0";
238 else std::cout << "," << it->second;
239 }
240 std::cout << "," << entry.second << std::endl;
241 }
242 }
243
244 template<typename F>
[424931d]245 void printAllHisto( const std::string& name, F extract ) {
[fa2de95]246 VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs];
247 VectorMap<unsigned> thisto;
248
249 for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); }
250
251 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
252 // can't be a higher count in one of the sub-histograms than the total
253 histos[i].reserve( thisto.size() );
254
255 for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; }
256 }
257
258 for ( unsigned i = 0; i < thisto.size(); ++i ) {
259 std::cout << "\"" << name << "\"," << i;
260 for ( const auto& histo : histos ) {
261 std::cout << "," << histo[i];
262 }
263 std::cout << "," << thisto[i] << std::endl;
264 }
265 }
[6a5f0e7]266
267 template<typename F>
[424931d]268 void printAllSparseHisto( const std::string& name, F extract ) {
[6a5f0e7]269 std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs];
270 std::map<unsigned, unsigned> thisto;
271
272 for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; }
273
274 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
275 for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; }
276 }
277
278 for ( const auto& entry : thisto ) {
279 const auto& key = entry.first;
280 std::cout << "\"" << name << "\"," << key;
281 for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) {
282 auto it = histos[i].find( key );
283 if ( it == histos[i].end() ) std::cout << ",0";
284 else std::cout << "," << it->second;
285 }
286 std::cout << "," << entry.second << std::endl;
287 }
288 }
[424931d]289
290 template<typename F>
291 void printAllPack( const std::string& name, F extract ) {
292 printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; });
293 printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; });
294 printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; });
295 printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; });
296 printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; });
[eed5e48]297 printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; });
[131dbb3]298 printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; });
[424931d]299 }
[fa2de95]300
301 public:
302 void print() {
303 for ( auto& stats : for_linkage ) {
304 total += stats;
305 }
306
307 std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl;
308
[6a5f0e7]309 printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; });
[fa2de95]310 printAll("n_decls", [](const Stats& stats) { return stats.n_decls; });
311 printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
[6a5f0e7]312 printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; });
[f923b5f]313 printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); });
[97d246d]314 printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; });
[131dbb3]315 printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; });
[f923b5f]316 printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); });
[97d246d]317 printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; });
[131dbb3]318 printAllSparseHisto("decls_using_compound_type", [](const Stats& stats) { return stats.compound_type_decls; });
[424931d]319 printAllPack("params", [](const Stats& stats) { return stats.params; });
320 printAllPack("returns", [](const Stats& stats) { return stats.returns; });
321 printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; });
322 printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; });
323 printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; });
[fa2de95]324 }
325 };
[41a7137]326
327 void printDeclStats( std::list< Declaration * > &translationUnit ) {
[fa2de95]328 DeclStats stats;
329 acceptAll( translationUnit, stats );
330 stats.print();
[41a7137]331 }
332
333} // namespace CodeTools
334
335// Local Variables: //
336// tab-width: 4 //
337// mode: c++ //
338// compile-command: "make install" //
339// End: //
Note: See TracBrowser for help on using the repository browser.