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