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