[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 |
---|
[07de76b] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Fri Dec 13 23:39:33 2019 |
---|
| 13 | // Update Count : 2 |
---|
[41a7137] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "DeclStats.h" |
---|
| 17 | |
---|
[bf2438c] | 18 | #include <iostream> // for operator<<, basic_ostream, cout |
---|
| 19 | #include <map> // for map |
---|
| 20 | #include <string> // for string, operator+, operator<<, cha... |
---|
| 21 | #include <unordered_map> // for unordered_map |
---|
| 22 | #include <unordered_set> // for unordered_set |
---|
| 23 | #include <utility> // for pair, make_pair |
---|
| 24 | |
---|
[e5c74d1] | 25 | #include "Common/PassVisitor.h" |
---|
[bf2438c] | 26 | #include "Common/VectorMap.h" // for VectorMap |
---|
[07de76b] | 27 | #include "SynTree/LinkageSpec.h" // for ::NoOfSpecs, Spec |
---|
[bf2438c] | 28 | #include "SynTree/Declaration.h" // for FunctionDecl, TypeDecl, Declaration |
---|
| 29 | #include "SynTree/Expression.h" // for UntypedExpr, Expression |
---|
| 30 | #include "SynTree/Statement.h" // for CompoundStmt |
---|
| 31 | #include "SynTree/Type.h" // for Type, FunctionType, PointerType |
---|
| 32 | #include "SynTree/Visitor.h" // for maybeAccept, Visitor, acceptAll |
---|
[41a7137] | 33 | |
---|
| 34 | namespace CodeTools { |
---|
[bf2438c] | 35 | |
---|
[e5c74d1] | 36 | struct DeclStats : public WithShortCircuiting { |
---|
[424931d] | 37 | template<typename T> |
---|
| 38 | static void sum(T& a, const T& b) { a += b; } |
---|
[6a5f0e7] | 39 | |
---|
[424931d] | 40 | static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) { |
---|
| 41 | a.reserve( b.size() ); |
---|
| 42 | for ( unsigned i = 0; i < b.size(); ++i ) { |
---|
| 43 | a[i] += b[i]; |
---|
| 44 | } |
---|
| 45 | } |
---|
[fa2de95] | 46 | |
---|
[424931d] | 47 | template<typename K> |
---|
| 48 | static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) { |
---|
| 49 | for ( const auto& entry : b ) { |
---|
| 50 | a[ entry.first ] += entry.second; |
---|
[6a5f0e7] | 51 | } |
---|
[424931d] | 52 | } |
---|
[6215a5c] | 53 | |
---|
[424931d] | 54 | template<typename K> |
---|
| 55 | static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) { |
---|
| 56 | for ( const auto& entry : b ) { |
---|
| 57 | a[ entry.first ] += entry.second; |
---|
[6a5f0e7] | 58 | } |
---|
[424931d] | 59 | } |
---|
[6215a5c] | 60 | |
---|
[424931d] | 61 | struct ArgPackStats { |
---|
[326cd2b] | 62 | VectorMap<unsigned> n; ///< Count of decls with each number of elements |
---|
| 63 | VectorMap<unsigned> n_basic; ///< Count of decls with each number of basic type elements |
---|
| 64 | VectorMap<unsigned> n_generic; ///< Count of decls with each number of generic type elements |
---|
| 65 | VectorMap<unsigned> n_poly; ///< Count of decls with each number of polymorphic elements |
---|
| 66 | VectorMap<unsigned> n_compound; ///< Count of decls with each number of non-generic compound types |
---|
| 67 | std::map<unsigned, unsigned> p_basic; ///< Count of decls with each percentage of basic type elements |
---|
| 68 | std::map<unsigned, unsigned> p_generic; ///< Count of decls with each percentage of generic type elements |
---|
| 69 | std::map<unsigned, unsigned> p_poly; ///< Count of decls with each percentage of polymorphic elements |
---|
| 70 | std::map<unsigned, unsigned> p_compound; ///< Count of decls with each percentage of non-generic compound type elements |
---|
| 71 | VectorMap<unsigned> n_types; ///< Count of decls with each number of distinct types in the pack |
---|
[131dbb3] | 72 | /// Count of decls with each percentage of new types in lists. |
---|
| 73 | /// Types used in the parameter list that recur in the return list are not considered to be new. |
---|
| 74 | std::map<unsigned, unsigned> p_new; |
---|
[424931d] | 75 | |
---|
| 76 | ArgPackStats& operator+= (const ArgPackStats& o) { |
---|
| 77 | sum(n, o.n); |
---|
| 78 | sum(n_basic, o.n_basic); |
---|
[326cd2b] | 79 | sum(n_generic, o.n_generic); |
---|
[424931d] | 80 | sum(n_poly, o.n_poly); |
---|
[326cd2b] | 81 | sum(n_compound, o.n_compound); |
---|
[424931d] | 82 | sum(p_basic, o.p_basic); |
---|
[326cd2b] | 83 | sum(p_generic, o.p_generic); |
---|
[424931d] | 84 | sum(p_poly, o.p_poly); |
---|
[326cd2b] | 85 | sum(p_compound, o.p_compound); |
---|
[eed5e48] | 86 | sum(n_types, o.n_types); |
---|
[131dbb3] | 87 | sum(p_new, o.p_new); |
---|
[bf2438c] | 88 | |
---|
[424931d] | 89 | return *this; |
---|
| 90 | } |
---|
| 91 | }; |
---|
[bf2438c] | 92 | |
---|
[424931d] | 93 | struct Stats { |
---|
| 94 | unsigned n_decls; ///< Total number of declarations |
---|
| 95 | /// Count of declarations with each number of assertion parameters |
---|
| 96 | VectorMap<unsigned> n_type_params; |
---|
[326cd2b] | 97 | /// Count of generic types with each number of type parameters |
---|
| 98 | VectorMap<unsigned> n_generic_params; |
---|
| 99 | /// Count of maximum nesting depth of types |
---|
| 100 | VectorMap<unsigned> n_generic_nesting; |
---|
[424931d] | 101 | /// Count of declarations with each name |
---|
| 102 | std::unordered_map<std::string, unsigned> by_name; |
---|
[f923b5f] | 103 | /// Count of uses of each basic type |
---|
| 104 | std::unordered_map<std::string, unsigned> basic_type_names; |
---|
[326cd2b] | 105 | /// Count of uses of each generic type name (includes "*", "[]", "(*)", "[,]") |
---|
| 106 | std::unordered_map<std::string, unsigned> generic_type_names; |
---|
| 107 | /// Count of uses of each non-generic aggregate type |
---|
[f923b5f] | 108 | std::unordered_map<std::string, unsigned> compound_type_names; |
---|
[131dbb3] | 109 | /// Count of decls using each basic type |
---|
| 110 | std::unordered_map<std::string, unsigned> basic_type_decls; |
---|
[326cd2b] | 111 | /// Count of decls using each generic type (includes "*", "[]", "(*)", "[,]") |
---|
| 112 | std::unordered_map<std::string, unsigned> generic_type_decls; |
---|
[131dbb3] | 113 | /// Count of decls using each compound type |
---|
| 114 | std::unordered_map<std::string, unsigned> compound_type_decls; |
---|
[424931d] | 115 | /// Stats for the parameter list |
---|
| 116 | ArgPackStats params; |
---|
| 117 | /// Stats for the return list |
---|
| 118 | ArgPackStats returns; |
---|
[bf2438c] | 119 | |
---|
[424931d] | 120 | /// Count of declarations with each number of assertions |
---|
| 121 | std::map<unsigned, unsigned> n_assns; |
---|
| 122 | /// Stats for the assertions' parameters |
---|
| 123 | ArgPackStats assn_params; |
---|
| 124 | /// Stats for the assertions' return types |
---|
| 125 | ArgPackStats assn_returns; |
---|
[bf2438c] | 126 | |
---|
[e23d20b] | 127 | Stats() : n_decls(0), n_type_params(), n_generic_params(), n_generic_nesting(), |
---|
| 128 | by_name(), basic_type_names(), generic_type_names(), compound_type_names(), |
---|
| 129 | basic_type_decls(), generic_type_decls(), compound_type_decls(), params(), |
---|
[326cd2b] | 130 | returns(), n_assns(), assn_params(), assn_returns() {} |
---|
[567903e] | 131 | |
---|
[6a5f0e7] | 132 | public: |
---|
| 133 | Stats& operator+= (const Stats& o) { |
---|
| 134 | sum( n_decls, o.n_decls ); |
---|
| 135 | sum( n_type_params, o.n_type_params ); |
---|
[326cd2b] | 136 | sum( n_generic_params, o.n_generic_params ); |
---|
| 137 | sum( n_generic_nesting, o.n_generic_nesting ); |
---|
[6a5f0e7] | 138 | sum( by_name, o.by_name ); |
---|
[f923b5f] | 139 | sum( basic_type_names, o.basic_type_names ); |
---|
[326cd2b] | 140 | sum( generic_type_names, o.generic_type_names ); |
---|
[f923b5f] | 141 | sum( compound_type_names, o.compound_type_names ); |
---|
[131dbb3] | 142 | sum( basic_type_decls, o.basic_type_decls ); |
---|
[326cd2b] | 143 | sum( generic_type_decls, o.generic_type_decls ); |
---|
[131dbb3] | 144 | sum( compound_type_decls, o.compound_type_decls ); |
---|
[424931d] | 145 | sum( params, o.params ); |
---|
| 146 | sum( returns, o.returns ); |
---|
| 147 | sum( n_assns, o.n_assns ); |
---|
| 148 | sum( assn_params, o.assn_params ); |
---|
| 149 | sum( assn_returns, o.assn_returns ); |
---|
[bf2438c] | 150 | |
---|
[fa2de95] | 151 | return *this; |
---|
| 152 | } |
---|
| 153 | }; |
---|
| 154 | |
---|
[326cd2b] | 155 | /// number of counting bins for linkages |
---|
| 156 | static const unsigned n_named_specs = 8; |
---|
[68fe946e] | 157 | /// Mapping function from linkage to bin. |
---|
| 158 | static unsigned linkage_index( LinkageSpec::Spec spec ) { |
---|
| 159 | switch ( spec ) { |
---|
| 160 | case LinkageSpec::Intrinsic: return 0; |
---|
| 161 | case LinkageSpec::C: return 1; |
---|
| 162 | case LinkageSpec::Cforall: return 2; |
---|
| 163 | case LinkageSpec::AutoGen: return 3; |
---|
| 164 | case LinkageSpec::Compiler: return 4; |
---|
| 165 | case LinkageSpec::BuiltinCFA: return 5; |
---|
| 166 | case LinkageSpec::BuiltinC: return 6; |
---|
| 167 | default: return 7; |
---|
| 168 | } |
---|
| 169 | } |
---|
[326cd2b] | 170 | |
---|
| 171 | Stats for_linkage[n_named_specs]; ///< Stores separate stats per linkage |
---|
[fa2de95] | 172 | std::unordered_set<std::string> seen_names; ///< Stores manglenames already seen to avoid double-counting |
---|
| 173 | Stats total; |
---|
[31868da] | 174 | /// Count of expressions with (depth, fanout) |
---|
| 175 | std::map<std::pair<unsigned, unsigned>, unsigned> exprs_by_fanout_at_depth; |
---|
[fa2de95] | 176 | |
---|
[e23d20b] | 177 | void countType( const std::string& name, unsigned& n, std::unordered_map<std::string, |
---|
| 178 | unsigned>& names, std::unordered_map<std::string, unsigned>& decls, |
---|
[326cd2b] | 179 | std::unordered_set<std::string>& elSeen ) { |
---|
| 180 | ++n; |
---|
| 181 | ++names[ name ]; |
---|
| 182 | if ( elSeen.insert( name ).second ) { ++decls[ name ]; } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void update_max( unsigned& max, unsigned crnt ) { |
---|
| 186 | if ( crnt > max ) max = crnt; |
---|
| 187 | } |
---|
| 188 | |
---|
[e23d20b] | 189 | void analyzeSubtype( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen, |
---|
[326cd2b] | 190 | unsigned& n_poly, bool& seen_poly, unsigned& max_depth, unsigned depth ) { |
---|
| 191 | unsigned x; |
---|
| 192 | analyzeType( ty, stats, elSeen, x, x, n_poly, x, seen_poly, max_depth, depth + 1 ); |
---|
| 193 | } |
---|
| 194 | |
---|
[e23d20b] | 195 | void analyzeSubtypes( std::list<DeclarationWithType*>& tys, Stats& stats, |
---|
| 196 | std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly, |
---|
[326cd2b] | 197 | unsigned& max_depth, unsigned depth, unsigned& n_subs ) { |
---|
| 198 | for ( DeclarationWithType* dwt : tys ) { |
---|
| 199 | Type* ty = dwt->get_type(); |
---|
| 200 | n_subs += (unsigned)( dynamic_cast<VoidType*>(ty) != nullptr ); |
---|
| 201 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
[e23d20b] | 205 | void analyzeSubtypes( std::list<Expression*>& tys, Stats& stats, |
---|
| 206 | std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly, |
---|
[326cd2b] | 207 | unsigned& max_depth, unsigned depth ) { |
---|
| 208 | for ( Expression* expr : tys ) { |
---|
| 209 | TypeExpr* texpr = dynamic_cast<TypeExpr*>(expr); |
---|
| 210 | if ( ! texpr ) continue; |
---|
| 211 | Type* ty = texpr->get_type(); |
---|
| 212 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
[e23d20b] | 216 | void analyzeSubtypes( std::list<Type*>& tys, Stats& stats, |
---|
| 217 | std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly, |
---|
[326cd2b] | 218 | unsigned& max_depth, unsigned depth ) { |
---|
| 219 | for ( Type* ty : tys ) { |
---|
| 220 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
[e23d20b] | 224 | void analyzeType( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen, |
---|
| 225 | unsigned& n_basic, unsigned& n_generic, unsigned& n_poly, unsigned& n_agg, |
---|
[326cd2b] | 226 | bool& seen_poly, unsigned& max_depth, unsigned depth = 0 ) { |
---|
| 227 | if ( BasicType* bt = dynamic_cast<BasicType*>(ty) ) { |
---|
| 228 | std::string name = BasicType::typeNames[ bt->get_kind() ]; |
---|
| 229 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); |
---|
| 230 | update_max( max_depth, depth ); |
---|
| 231 | } else if ( PointerType* pt = dynamic_cast<PointerType*>(ty) ) { |
---|
| 232 | std::string name = "*"; |
---|
[e23d20b] | 233 | countType( |
---|
[326cd2b] | 234 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); |
---|
[e23d20b] | 235 | analyzeSubtype( |
---|
[326cd2b] | 236 | pt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 237 | ++stats.n_generic_params.at( 1 ); |
---|
| 238 | } else if ( ArrayType* at = dynamic_cast<ArrayType*>(ty) ) { |
---|
| 239 | std::string name = "[]"; |
---|
[e23d20b] | 240 | countType( |
---|
[326cd2b] | 241 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); |
---|
[e23d20b] | 242 | analyzeSubtype( |
---|
[326cd2b] | 243 | at->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 244 | ++stats.n_generic_params.at( 1 ); |
---|
| 245 | } else if ( ReferenceType* rt = dynamic_cast<ReferenceType*>(ty) ) { |
---|
| 246 | std::string name = "&"; |
---|
[e23d20b] | 247 | countType( |
---|
[326cd2b] | 248 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); |
---|
[e23d20b] | 249 | analyzeSubtype( |
---|
[326cd2b] | 250 | rt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 251 | ++stats.n_generic_params.at( 1 ); |
---|
| 252 | } else if ( FunctionType* ft = dynamic_cast<FunctionType*>(ty) ) { |
---|
| 253 | std::string name = "(*)"; |
---|
[e23d20b] | 254 | countType( |
---|
[326cd2b] | 255 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); |
---|
| 256 | unsigned n_subs = 0; |
---|
[e23d20b] | 257 | analyzeSubtypes( |
---|
| 258 | ft->get_returnVals(), stats, elSeen, n_poly, seen_poly, max_depth, depth, |
---|
[326cd2b] | 259 | n_subs ); |
---|
[e23d20b] | 260 | analyzeSubtypes( |
---|
| 261 | ft->get_parameters(), stats, elSeen, n_poly, seen_poly, max_depth, depth, |
---|
[326cd2b] | 262 | n_subs ); |
---|
| 263 | ++stats.n_generic_params.at( n_subs ); |
---|
| 264 | } else if ( TypeInstType* vt = dynamic_cast<TypeInstType*>(ty) ) { |
---|
| 265 | if ( ! seen_poly ) { |
---|
| 266 | ++n_poly; |
---|
| 267 | seen_poly = true; |
---|
| 268 | } |
---|
[e23d20b] | 269 | countType( |
---|
| 270 | vt->get_name(), n_agg, stats.compound_type_names, stats.compound_type_decls, |
---|
[326cd2b] | 271 | elSeen ); |
---|
| 272 | update_max( max_depth, depth ); |
---|
| 273 | } else if ( ReferenceToType* st = dynamic_cast<ReferenceToType*>(ty) ) { |
---|
| 274 | std::list<Expression*>& params = st->get_parameters(); |
---|
| 275 | if ( params.empty() ) { |
---|
[e23d20b] | 276 | countType( |
---|
| 277 | st->get_name(), n_agg, stats.compound_type_names, |
---|
[326cd2b] | 278 | stats.compound_type_decls, elSeen ); |
---|
| 279 | update_max( max_depth, depth ); |
---|
| 280 | } else { |
---|
[e23d20b] | 281 | countType( |
---|
| 282 | st->get_name(), n_generic, stats.generic_type_names, |
---|
[326cd2b] | 283 | stats.generic_type_decls, elSeen); |
---|
| 284 | analyzeSubtypes( params, stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 285 | ++stats.n_generic_params.at( params.size() ); |
---|
| 286 | } |
---|
| 287 | } else if ( TupleType* tt = dynamic_cast<TupleType*>(ty) ) { |
---|
| 288 | std::string name = "[,]"; |
---|
[e23d20b] | 289 | countType( |
---|
[326cd2b] | 290 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); |
---|
[e23d20b] | 291 | analyzeSubtypes( |
---|
[326cd2b] | 292 | tt->get_types(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); |
---|
| 293 | ++stats.n_generic_params.at( tt->size() ); |
---|
| 294 | } else if ( dynamic_cast<VarArgsType*>(ty) ) { |
---|
| 295 | std::string name = "..."; |
---|
[e23d20b] | 296 | countType( |
---|
[326cd2b] | 297 | name, n_agg, stats.compound_type_names, stats.compound_type_decls, elSeen ); |
---|
| 298 | update_max( max_depth, depth ); |
---|
| 299 | } else if ( dynamic_cast<ZeroType*>(ty) ) { |
---|
| 300 | std::string name = "0"; |
---|
| 301 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); |
---|
| 302 | update_max( max_depth, depth ); |
---|
| 303 | } else if ( dynamic_cast<OneType*>(ty) ) { |
---|
| 304 | std::string name = "1"; |
---|
| 305 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); |
---|
| 306 | update_max( max_depth, depth ); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[f923b5f] | 310 | /// Update arg pack stats based on a declaration list |
---|
[e23d20b] | 311 | void analyze( Stats& stats, std::unordered_set<std::string>& seen, |
---|
| 312 | std::unordered_set<std::string>& elSeen, ArgPackStats& pstats, |
---|
[326cd2b] | 313 | std::list<DeclarationWithType*>& decls ) { |
---|
[eed5e48] | 314 | std::unordered_set<std::string> types; |
---|
[326cd2b] | 315 | unsigned n = 0; ///< number of args/returns |
---|
| 316 | unsigned n_basic = 0; ///< number of basic types |
---|
| 317 | unsigned n_generic = 0; ///< number of generic types (includes "*", "&", "[]", "(*)", "[,]") |
---|
| 318 | unsigned n_poly = 0; ///< number of polymorphic types |
---|
| 319 | unsigned n_agg = 0; ///< number of non-generic aggregate types |
---|
| 320 | unsigned n_new = 0; ///< number of new types |
---|
[e23d20b] | 321 | |
---|
[f923b5f] | 322 | for ( auto decl : decls ) { |
---|
[eed5e48] | 323 | Type* dt = decl->get_type(); |
---|
| 324 | |
---|
| 325 | n += dt->size(); |
---|
[bf2438c] | 326 | |
---|
[eed5e48] | 327 | std::stringstream ss; |
---|
| 328 | dt->print( ss ); |
---|
| 329 | types.insert( ss.str() ); |
---|
[326cd2b] | 330 | if ( seen.insert( ss.str() ).second ) { ++n_new; } |
---|
| 331 | |
---|
| 332 | bool seen_poly = false; |
---|
| 333 | unsigned max_depth = 0; |
---|
[e23d20b] | 334 | analyzeType( |
---|
[326cd2b] | 335 | dt, stats, elSeen, n_basic, n_generic, n_poly, n_agg, seen_poly, max_depth ); |
---|
| 336 | ++stats.n_generic_nesting.at( max_depth ); |
---|
[f923b5f] | 337 | } |
---|
[e23d20b] | 338 | |
---|
[f923b5f] | 339 | ++pstats.n.at( n ); |
---|
| 340 | ++pstats.n_basic.at( n_basic ); |
---|
[326cd2b] | 341 | ++pstats.n_generic.at( n_generic ); |
---|
[f923b5f] | 342 | ++pstats.n_poly.at( n_poly ); |
---|
[326cd2b] | 343 | ++pstats.n_compound.at( n_agg ); |
---|
[f923b5f] | 344 | if ( n > 0 ) { |
---|
| 345 | ++pstats.p_basic[ n_basic*100/n ]; |
---|
[326cd2b] | 346 | ++pstats.p_generic[ n_generic*100/n ]; |
---|
[f923b5f] | 347 | ++pstats.p_poly[ n_poly*100/n ]; |
---|
[326cd2b] | 348 | ++pstats.p_compound[ n_agg*100/n ]; |
---|
| 349 | if ( n > 1 ) ++pstats.p_new[ (n_new-1)*100/(n-1) ]; |
---|
[f923b5f] | 350 | } |
---|
[eed5e48] | 351 | ++pstats.n_types.at( types.size() ); |
---|
[f923b5f] | 352 | } |
---|
[bf2438c] | 353 | |
---|
[f923b5f] | 354 | void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) { |
---|
[131dbb3] | 355 | std::unordered_set<std::string> seen; |
---|
[326cd2b] | 356 | std::unordered_set<std::string> elSeen; |
---|
| 357 | analyze( stats, seen, elSeen, params, fnTy->get_parameters() ); |
---|
| 358 | analyze( stats, seen, elSeen, returns, fnTy->get_returnVals() ); |
---|
[6a5f0e7] | 359 | } |
---|
| 360 | |
---|
[31868da] | 361 | void analyzeExpr( UntypedExpr *expr, unsigned depth ) { |
---|
| 362 | auto& args = expr->get_args(); |
---|
| 363 | unsigned fanout = args.size(); |
---|
[bf2438c] | 364 | |
---|
[31868da] | 365 | ++exprs_by_fanout_at_depth[ std::make_pair(depth, fanout) ]; |
---|
| 366 | for ( Expression* arg : args ) { |
---|
| 367 | if ( UntypedExpr *uearg = dynamic_cast<UntypedExpr*>(arg) ) { |
---|
| 368 | analyzeExpr( uearg, depth+1 ); |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
[fa2de95] | 373 | public: |
---|
[e5c74d1] | 374 | void previsit( FunctionDecl *decl ) { |
---|
[fa2de95] | 375 | // skip if already seen declaration for this function |
---|
[e5c74d1] | 376 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->name : decl->get_mangleName(); |
---|
| 377 | if ( seen_names.insert( mangleName ).second ) { |
---|
[68fe946e] | 378 | Stats& stats = for_linkage[ linkage_index( decl->linkage ) ]; |
---|
[e5c74d1] | 379 | |
---|
| 380 | ++stats.n_decls; |
---|
| 381 | FunctionType* fnTy = decl->type; |
---|
| 382 | const Type::ForallList& forall = fnTy->forall; |
---|
| 383 | ++stats.n_type_params.at( forall.size() ); |
---|
| 384 | unsigned n_assns = 0; |
---|
| 385 | for ( TypeDecl* fdecl : forall ) { |
---|
| 386 | n_assns += fdecl->assertions.size(); |
---|
| 387 | for ( DeclarationWithType* assn : fdecl->assertions ) { |
---|
| 388 | FunctionType *assnTy = nullptr; |
---|
| 389 | if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { |
---|
| 390 | if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { |
---|
[e23d20b] | 391 | assnTy = dynamic_cast<FunctionType*>(ptrTy->base); |
---|
[e5c74d1] | 392 | } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); |
---|
| 393 | } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { |
---|
| 394 | assnTy = assnDecl->type; |
---|
| 395 | } |
---|
| 396 | if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); |
---|
[6a5f0e7] | 397 | } |
---|
| 398 | } |
---|
[e5c74d1] | 399 | ++stats.n_assns[ n_assns ]; |
---|
[e23d20b] | 400 | ++stats.by_name[ decl->name ]; |
---|
[e5c74d1] | 401 | analyzeFunc( fnTy, stats, stats.params, stats.returns ); |
---|
| 402 | } |
---|
[fa2de95] | 403 | } |
---|
| 404 | |
---|
[e5c74d1] | 405 | void previsit( UntypedExpr *expr ) { |
---|
| 406 | visit_children = false; |
---|
[31868da] | 407 | analyzeExpr( expr, 0 ); |
---|
| 408 | } |
---|
| 409 | |
---|
[fa2de95] | 410 | private: |
---|
| 411 | template<typename F> |
---|
[424931d] | 412 | void printAll( const std::string& name, F extract ) { |
---|
[fa2de95] | 413 | std::cout << "\"" << name << "\","; |
---|
| 414 | for ( const auto& stats : for_linkage ) { |
---|
| 415 | std::cout << "," << extract(stats); |
---|
| 416 | } |
---|
| 417 | std::cout << "," << extract(total) << std::endl; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | template<typename F> |
---|
[424931d] | 421 | void printAllMap( const std::string& name, F extract ) { |
---|
[fa2de95] | 422 | for ( const auto& entry : extract(total) ) { |
---|
| 423 | const auto& key = entry.first; |
---|
| 424 | std::cout << "\"" << name << "\"," << key; |
---|
| 425 | for ( const auto& stats : for_linkage ) { |
---|
| 426 | const auto& map = extract(stats); |
---|
| 427 | auto it = map.find( key ); |
---|
| 428 | if ( it == map.end() ) std::cout << ",0"; |
---|
| 429 | else std::cout << "," << it->second; |
---|
| 430 | } |
---|
| 431 | std::cout << "," << entry.second << std::endl; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | template<typename F> |
---|
[424931d] | 436 | void printAllHisto( const std::string& name, F extract ) { |
---|
[326cd2b] | 437 | VectorMap<unsigned> histos[n_named_specs]; |
---|
[fa2de95] | 438 | VectorMap<unsigned> thisto; |
---|
| 439 | |
---|
| 440 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } |
---|
| 441 | |
---|
[326cd2b] | 442 | for ( unsigned i = 0; i < n_named_specs; ++i ) { |
---|
[fa2de95] | 443 | // can't be a higher count in one of the sub-histograms than the total |
---|
| 444 | histos[i].reserve( thisto.size() ); |
---|
| 445 | |
---|
| 446 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | for ( unsigned i = 0; i < thisto.size(); ++i ) { |
---|
| 450 | std::cout << "\"" << name << "\"," << i; |
---|
| 451 | for ( const auto& histo : histos ) { |
---|
| 452 | std::cout << "," << histo[i]; |
---|
| 453 | } |
---|
| 454 | std::cout << "," << thisto[i] << std::endl; |
---|
| 455 | } |
---|
| 456 | } |
---|
[6a5f0e7] | 457 | |
---|
| 458 | template<typename F> |
---|
[424931d] | 459 | void printAllSparseHisto( const std::string& name, F extract ) { |
---|
[326cd2b] | 460 | std::map<unsigned, unsigned> histos[n_named_specs]; |
---|
[6a5f0e7] | 461 | std::map<unsigned, unsigned> thisto; |
---|
| 462 | |
---|
| 463 | for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } |
---|
| 464 | |
---|
[326cd2b] | 465 | for ( unsigned i = 0; i < n_named_specs; ++i ) { |
---|
[6a5f0e7] | 466 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | for ( const auto& entry : thisto ) { |
---|
| 470 | const auto& key = entry.first; |
---|
| 471 | std::cout << "\"" << name << "\"," << key; |
---|
[326cd2b] | 472 | for ( unsigned i = 0; i < n_named_specs; ++i ) { |
---|
[6a5f0e7] | 473 | auto it = histos[i].find( key ); |
---|
| 474 | if ( it == histos[i].end() ) std::cout << ",0"; |
---|
| 475 | else std::cout << "," << it->second; |
---|
| 476 | } |
---|
| 477 | std::cout << "," << entry.second << std::endl; |
---|
| 478 | } |
---|
| 479 | } |
---|
[424931d] | 480 | |
---|
| 481 | template<typename F> |
---|
| 482 | void printAllPack( const std::string& name, F extract ) { |
---|
| 483 | printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); |
---|
[326cd2b] | 484 | printAllMap("n_generic_" + name, [&extract](const Stats& stats) { return extract(stats).n_generic; }); |
---|
[424931d] | 485 | printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); |
---|
[326cd2b] | 486 | printAllMap("n_compound_" + name, [&extract](const Stats& stats) { return extract(stats).n_compound; }); |
---|
[424931d] | 487 | printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); |
---|
| 488 | printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); |
---|
[326cd2b] | 489 | printAllMap("%_generic_" + name, [&extract](const Stats& stats) { return extract(stats).p_generic; }); |
---|
[424931d] | 490 | printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); |
---|
[326cd2b] | 491 | printAllMap("%_compound_" + name, [&extract](const Stats& stats) { return extract(stats).p_compound; }); |
---|
[eed5e48] | 492 | printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; }); |
---|
[131dbb3] | 493 | printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; }); |
---|
[424931d] | 494 | } |
---|
[31868da] | 495 | |
---|
[bf2438c] | 496 | void printPairMap( const std::string& name, |
---|
[31868da] | 497 | const std::map<std::pair<unsigned, unsigned>, unsigned>& map ) { |
---|
| 498 | for ( const auto& entry : map ) { |
---|
| 499 | const auto& key = entry.first; |
---|
[bf2438c] | 500 | std::cout << "\"" << name << "\"," << key.first << "," << key.second << "," |
---|
[31868da] | 501 | << entry.second << std::endl; |
---|
| 502 | } |
---|
| 503 | } |
---|
[bf2438c] | 504 | |
---|
[fa2de95] | 505 | public: |
---|
| 506 | void print() { |
---|
| 507 | for ( auto& stats : for_linkage ) { |
---|
| 508 | total += stats; |
---|
| 509 | } |
---|
| 510 | |
---|
[326cd2b] | 511 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"compiler\",\"builtinCFA\",\"builtinC\",\"other\",\"TOTAL\"" << std::endl; |
---|
[fa2de95] | 512 | |
---|
[6a5f0e7] | 513 | printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); |
---|
[326cd2b] | 514 | printAllMap("n_generic_params", [](const Stats& stats) { return stats.n_generic_params; }); |
---|
| 515 | printAllMap("n_generic_nesting", [](const Stats& stats) { return stats.n_generic_nesting; }); |
---|
[fa2de95] | 516 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); |
---|
| 517 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); |
---|
[6a5f0e7] | 518 | printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; }); |
---|
[f923b5f] | 519 | printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); }); |
---|
[97d246d] | 520 | printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; }); |
---|
[131dbb3] | 521 | printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; }); |
---|
[326cd2b] | 522 | printAll("generic_type_names", [](const Stats& stats) { return stats.generic_type_names.size(); }); |
---|
| 523 | printAllSparseHisto("generic_type_uses", [](const Stats& stats) { return stats.generic_type_names; }); |
---|
| 524 | printAllSparseHisto("decls_using_generic_type", [](const Stats& stats) { return stats.generic_type_decls; }); |
---|
[f923b5f] | 525 | printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); }); |
---|
[97d246d] | 526 | printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; }); |
---|
[131dbb3] | 527 | printAllSparseHisto("decls_using_compound_type", [](const Stats& stats) { return stats.compound_type_decls; }); |
---|
[424931d] | 528 | printAllPack("params", [](const Stats& stats) { return stats.params; }); |
---|
| 529 | printAllPack("returns", [](const Stats& stats) { return stats.returns; }); |
---|
| 530 | printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; }); |
---|
| 531 | printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; }); |
---|
| 532 | printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; }); |
---|
[31868da] | 533 | std::cout << std::endl; |
---|
| 534 | |
---|
| 535 | printPairMap("exprs_by_depth+fanout", exprs_by_fanout_at_depth); |
---|
[fa2de95] | 536 | } |
---|
| 537 | }; |
---|
[41a7137] | 538 | |
---|
| 539 | void printDeclStats( std::list< Declaration * > &translationUnit ) { |
---|
[e5c74d1] | 540 | PassVisitor<DeclStats> stats; |
---|
[fa2de95] | 541 | acceptAll( translationUnit, stats ); |
---|
[e5c74d1] | 542 | stats.pass.print(); |
---|
[41a7137] | 543 | } |
---|
[bf2438c] | 544 | |
---|
[41a7137] | 545 | } // namespace CodeTools |
---|
| 546 | |
---|
| 547 | // Local Variables: // |
---|
| 548 | // tab-width: 4 // |
---|
| 549 | // mode: c++ // |
---|
| 550 | // compile-command: "make install" // |
---|
| 551 | // End: // |
---|