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 <string> |
---|
20 | #include <unordered_map> |
---|
21 | #include <unordered_set> |
---|
22 | |
---|
23 | #include "Common/VectorMap.h" |
---|
24 | #include "GenPoly/GenPoly.h" |
---|
25 | #include "Parser/LinkageSpec.h" |
---|
26 | #include "SynTree/Declaration.h" |
---|
27 | #include "SynTree/Visitor.h" |
---|
28 | |
---|
29 | namespace CodeTools { |
---|
30 | |
---|
31 | class DeclStats : public Visitor { |
---|
32 | struct Stats { |
---|
33 | unsigned n_decls; ///< Total number of declarations |
---|
34 | unsigned mono_decls; ///< Monomorphic declarations |
---|
35 | unsigned poly_decls; ///< Polymorphic declarations |
---|
36 | /// Count of declarations with each name |
---|
37 | std::unordered_map<std::string, unsigned> by_name; |
---|
38 | /// Count of declarations with each number of parameters |
---|
39 | VectorMap<unsigned> by_params; |
---|
40 | /// Count of declarations with each number of return types |
---|
41 | VectorMap<unsigned> by_returns; |
---|
42 | /// Count of declarations with each number of polymorphic parameters |
---|
43 | VectorMap<unsigned> n_poly_params; |
---|
44 | /// Count of declarations with each number of polymorphic return types |
---|
45 | VectorMap<unsigned> n_poly_returns; |
---|
46 | |
---|
47 | Stats() : n_decls(0), mono_decls(0), poly_decls(0), by_name(), by_params(), by_returns(), n_poly_params(), n_poly_returns() {} |
---|
48 | |
---|
49 | Stats& operator+= (const Stats& o) { |
---|
50 | n_decls += o.n_decls; |
---|
51 | mono_decls += o.mono_decls; |
---|
52 | poly_decls += o.poly_decls; |
---|
53 | |
---|
54 | for ( auto& entry : o.by_name ) { |
---|
55 | by_name[ entry.first ] += entry.second; |
---|
56 | } |
---|
57 | |
---|
58 | by_params.reserve( o.by_params.size() ); |
---|
59 | for ( unsigned i = 0; i < o.by_params.size(); ++i ) { |
---|
60 | by_params[i] += o.by_params[i]; |
---|
61 | } |
---|
62 | |
---|
63 | by_returns.reserve( o.by_returns.size() ); |
---|
64 | for ( unsigned i = 0; i < o.by_returns.size(); ++i ) { |
---|
65 | by_returns[i] += o.by_returns[i]; |
---|
66 | } |
---|
67 | |
---|
68 | n_poly_params.reserve( o.n_poly_params.size() ); |
---|
69 | for ( unsigned i = 0; i < o.n_poly_params.size(); ++i ) { |
---|
70 | n_poly_params[i] += o.n_poly_params[i]; |
---|
71 | } |
---|
72 | |
---|
73 | n_poly_returns.reserve( o.n_poly_returns.size() ); |
---|
74 | for ( unsigned i = 0; i < o.n_poly_returns.size(); ++i ) { |
---|
75 | n_poly_returns[i] += o.n_poly_returns[i]; |
---|
76 | } |
---|
77 | |
---|
78 | return *this; |
---|
79 | } |
---|
80 | }; |
---|
81 | |
---|
82 | Stats for_linkage[LinkageSpec::NoOfSpecs]; ///< Stores separate stats per linkage |
---|
83 | std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting |
---|
84 | Stats total; |
---|
85 | |
---|
86 | public: |
---|
87 | using Visitor::visit; |
---|
88 | |
---|
89 | virtual void visit( FunctionDecl *decl ) { |
---|
90 | // skip if already seen declaration for this function |
---|
91 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->get_name() : decl->get_mangleName(); |
---|
92 | if ( ! seen_names.insert( mangleName ).second ) return; |
---|
93 | |
---|
94 | Stats& stats = for_linkage[ decl->get_linkage() ]; |
---|
95 | |
---|
96 | ++stats.n_decls; |
---|
97 | FunctionType* fnTy = decl->get_functionType(); |
---|
98 | if ( fnTy->get_forall().empty() ) ++stats.mono_decls; else ++stats.poly_decls; |
---|
99 | |
---|
100 | ++stats.by_name[ decl->get_name() ]; |
---|
101 | |
---|
102 | unsigned n_params = 0; |
---|
103 | unsigned n_poly_params = 0; |
---|
104 | for ( auto pdecl : fnTy->get_parameters() ) { |
---|
105 | n_params += pdecl->get_type()->size(); |
---|
106 | if ( GenPoly::hasPolyBase( pdecl->get_type() ) ) ++n_poly_params; |
---|
107 | } |
---|
108 | ++stats.by_params.at( n_params ); |
---|
109 | ++stats.n_poly_params.at( n_poly_params ); |
---|
110 | |
---|
111 | unsigned n_returns = 0; |
---|
112 | unsigned n_poly_returns = 0; |
---|
113 | for ( auto rdecl : fnTy->get_returnVals() ) { |
---|
114 | n_returns += rdecl->get_type()->size(); |
---|
115 | if ( GenPoly::hasPolyBase( rdecl->get_type() ) ) ++n_poly_returns; |
---|
116 | } |
---|
117 | ++stats.by_returns.at( n_returns ); |
---|
118 | ++stats.n_poly_returns.at( n_poly_returns ); |
---|
119 | } |
---|
120 | |
---|
121 | private: |
---|
122 | template<typename F> |
---|
123 | void printAll( const char* name, F extract ) { |
---|
124 | std::cout << "\"" << name << "\","; |
---|
125 | for ( const auto& stats : for_linkage ) { |
---|
126 | std::cout << "," << extract(stats); |
---|
127 | } |
---|
128 | std::cout << "," << extract(total) << std::endl; |
---|
129 | } |
---|
130 | |
---|
131 | template<typename F> |
---|
132 | void printAllMap( const char* name, F extract ) { |
---|
133 | for ( const auto& entry : extract(total) ) { |
---|
134 | const auto& key = entry.first; |
---|
135 | std::cout << "\"" << name << "\"," << key; |
---|
136 | for ( const auto& stats : for_linkage ) { |
---|
137 | const auto& map = extract(stats); |
---|
138 | auto it = map.find( key ); |
---|
139 | if ( it == map.end() ) std::cout << ",0"; |
---|
140 | else std::cout << "," << it->second; |
---|
141 | } |
---|
142 | std::cout << "," << entry.second << std::endl; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | template<typename F> |
---|
147 | void printAllHisto( const char* name, F extract ) { |
---|
148 | VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs]; |
---|
149 | VectorMap<unsigned> thisto; |
---|
150 | |
---|
151 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } |
---|
152 | |
---|
153 | for ( unsigned i = 0; i < LinkageSpec::NoOfSpecs; ++i ) { |
---|
154 | // can't be a higher count in one of the sub-histograms than the total |
---|
155 | histos[i].reserve( thisto.size() ); |
---|
156 | |
---|
157 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
158 | } |
---|
159 | |
---|
160 | for ( unsigned i = 0; i < thisto.size(); ++i ) { |
---|
161 | std::cout << "\"" << name << "\"," << i; |
---|
162 | for ( const auto& histo : histos ) { |
---|
163 | std::cout << "," << histo[i]; |
---|
164 | } |
---|
165 | std::cout << "," << thisto[i] << std::endl; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | public: |
---|
170 | void print() { |
---|
171 | for ( auto& stats : for_linkage ) { |
---|
172 | total += stats; |
---|
173 | } |
---|
174 | |
---|
175 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"builtin\",\"TOTAL\"" << std::endl; |
---|
176 | |
---|
177 | printAll("mono_decls", [](const Stats& stats) { return stats.mono_decls; }); |
---|
178 | printAll("poly_decls", [](const Stats& stats) { return stats.poly_decls; }); |
---|
179 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); |
---|
180 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); |
---|
181 | |
---|
182 | printAllHisto("overloads", [](const Stats& stats) { return stats.by_name; }); |
---|
183 | printAllMap("n_poly_params", [](const Stats& stats) { return stats.n_poly_params; }); |
---|
184 | printAllMap("n_params", [](const Stats& stats) { return stats.by_params; }); |
---|
185 | printAllMap("n_poly_returns", [](const Stats& stats) { return stats.n_poly_returns; }); |
---|
186 | printAllMap("n_returns", [](const Stats& stats) { return stats.by_returns; }); |
---|
187 | } |
---|
188 | }; |
---|
189 | |
---|
190 | void printDeclStats( std::list< Declaration * > &translationUnit ) { |
---|
191 | DeclStats stats; |
---|
192 | acceptAll( translationUnit, stats ); |
---|
193 | stats.print(); |
---|
194 | } |
---|
195 | |
---|
196 | } // namespace CodeTools |
---|
197 | |
---|
198 | // Local Variables: // |
---|
199 | // tab-width: 4 // |
---|
200 | // mode: c++ // |
---|
201 | // compile-command: "make install" // |
---|
202 | // End: // |
---|