| [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 | 
|---|
|  | 27 | #include "GenPoly/GenPoly.h"       // for hasPolyBase | 
|---|
| [07de76b] | 28 | #include "SynTree/LinkageSpec.h"   // for ::NoOfSpecs, Spec | 
|---|
| [bf2438c] | 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; | 
|---|
| [68fe946e] | 158 | /// Mapping function from linkage to bin. | 
|---|
|  | 159 | static unsigned linkage_index( LinkageSpec::Spec spec ) { | 
|---|
|  | 160 | switch ( spec ) { | 
|---|
|  | 161 | case LinkageSpec::Intrinsic:  return 0; | 
|---|
|  | 162 | case LinkageSpec::C:          return 1; | 
|---|
|  | 163 | case LinkageSpec::Cforall:    return 2; | 
|---|
|  | 164 | case LinkageSpec::AutoGen:    return 3; | 
|---|
|  | 165 | case LinkageSpec::Compiler:   return 4; | 
|---|
|  | 166 | case LinkageSpec::BuiltinCFA: return 5; | 
|---|
|  | 167 | case LinkageSpec::BuiltinC:   return 6; | 
|---|
|  | 168 | default:                      return 7; | 
|---|
|  | 169 | } | 
|---|
|  | 170 | } | 
|---|
| [326cd2b] | 171 |  | 
|---|
|  | 172 | Stats for_linkage[n_named_specs];            ///< Stores separate stats per linkage | 
|---|
| [fa2de95] | 173 | std::unordered_set<std::string> seen_names;  ///< Stores manglenames already seen to avoid double-counting | 
|---|
|  | 174 | Stats total; | 
|---|
| [31868da] | 175 | /// Count of expressions with (depth, fanout) | 
|---|
|  | 176 | std::map<std::pair<unsigned, unsigned>, unsigned> exprs_by_fanout_at_depth; | 
|---|
| [fa2de95] | 177 |  | 
|---|
| [e23d20b] | 178 | void countType( const std::string& name, unsigned& n, std::unordered_map<std::string, | 
|---|
|  | 179 | unsigned>& names, std::unordered_map<std::string, unsigned>& decls, | 
|---|
| [326cd2b] | 180 | std::unordered_set<std::string>& elSeen ) { | 
|---|
|  | 181 | ++n; | 
|---|
|  | 182 | ++names[ name ]; | 
|---|
|  | 183 | if ( elSeen.insert( name ).second ) { ++decls[ name ]; } | 
|---|
|  | 184 | } | 
|---|
|  | 185 |  | 
|---|
|  | 186 | void update_max( unsigned& max, unsigned crnt ) { | 
|---|
|  | 187 | if ( crnt > max ) max = crnt; | 
|---|
|  | 188 | } | 
|---|
|  | 189 |  | 
|---|
| [e23d20b] | 190 | void analyzeSubtype( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen, | 
|---|
| [326cd2b] | 191 | unsigned& n_poly, bool& seen_poly, unsigned& max_depth, unsigned depth ) { | 
|---|
|  | 192 | unsigned x; | 
|---|
|  | 193 | analyzeType( ty, stats, elSeen, x, x, n_poly, x, seen_poly, max_depth, depth + 1 ); | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
| [e23d20b] | 196 | void analyzeSubtypes( std::list<DeclarationWithType*>& tys, Stats& stats, | 
|---|
|  | 197 | std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly, | 
|---|
| [326cd2b] | 198 | unsigned& max_depth, unsigned depth, unsigned& n_subs ) { | 
|---|
|  | 199 | for ( DeclarationWithType* dwt : tys ) { | 
|---|
|  | 200 | Type* ty = dwt->get_type(); | 
|---|
|  | 201 | n_subs += (unsigned)( dynamic_cast<VoidType*>(ty) != nullptr ); | 
|---|
|  | 202 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 203 | } | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
| [e23d20b] | 206 | void analyzeSubtypes( std::list<Expression*>& 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 ( Expression* expr : tys ) { | 
|---|
|  | 210 | TypeExpr* texpr = dynamic_cast<TypeExpr*>(expr); | 
|---|
|  | 211 | if ( ! texpr ) continue; | 
|---|
|  | 212 | Type* ty = texpr->get_type(); | 
|---|
|  | 213 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 214 | } | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
| [e23d20b] | 217 | void analyzeSubtypes( std::list<Type*>& tys, Stats& stats, | 
|---|
|  | 218 | std::unordered_set<std::string>& elSeen, unsigned& n_poly, bool& seen_poly, | 
|---|
| [326cd2b] | 219 | unsigned& max_depth, unsigned depth ) { | 
|---|
|  | 220 | for ( Type* ty : tys ) { | 
|---|
|  | 221 | analyzeSubtype( ty, stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 222 | } | 
|---|
|  | 223 | } | 
|---|
|  | 224 |  | 
|---|
| [e23d20b] | 225 | void analyzeType( Type* ty, Stats& stats, std::unordered_set<std::string>& elSeen, | 
|---|
|  | 226 | unsigned& n_basic, unsigned& n_generic, unsigned& n_poly, unsigned& n_agg, | 
|---|
| [326cd2b] | 227 | bool& seen_poly, unsigned& max_depth, unsigned depth = 0 ) { | 
|---|
|  | 228 | if ( BasicType* bt = dynamic_cast<BasicType*>(ty) ) { | 
|---|
|  | 229 | std::string name = BasicType::typeNames[ bt->get_kind() ]; | 
|---|
|  | 230 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); | 
|---|
|  | 231 | update_max( max_depth, depth ); | 
|---|
|  | 232 | } else if ( PointerType* pt = dynamic_cast<PointerType*>(ty) ) { | 
|---|
|  | 233 | std::string name = "*"; | 
|---|
| [e23d20b] | 234 | countType( | 
|---|
| [326cd2b] | 235 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); | 
|---|
| [e23d20b] | 236 | analyzeSubtype( | 
|---|
| [326cd2b] | 237 | pt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 238 | ++stats.n_generic_params.at( 1 ); | 
|---|
|  | 239 | } else if ( ArrayType* at = dynamic_cast<ArrayType*>(ty) ) { | 
|---|
|  | 240 | std::string name = "[]"; | 
|---|
| [e23d20b] | 241 | countType( | 
|---|
| [326cd2b] | 242 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); | 
|---|
| [e23d20b] | 243 | analyzeSubtype( | 
|---|
| [326cd2b] | 244 | at->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 245 | ++stats.n_generic_params.at( 1 ); | 
|---|
|  | 246 | } else if ( ReferenceType* rt = dynamic_cast<ReferenceType*>(ty) ) { | 
|---|
|  | 247 | std::string name = "&"; | 
|---|
| [e23d20b] | 248 | countType( | 
|---|
| [326cd2b] | 249 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); | 
|---|
| [e23d20b] | 250 | analyzeSubtype( | 
|---|
| [326cd2b] | 251 | rt->get_base(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 252 | ++stats.n_generic_params.at( 1 ); | 
|---|
|  | 253 | } else if ( FunctionType* ft = dynamic_cast<FunctionType*>(ty) ) { | 
|---|
|  | 254 | std::string name = "(*)"; | 
|---|
| [e23d20b] | 255 | countType( | 
|---|
| [326cd2b] | 256 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); | 
|---|
|  | 257 | unsigned n_subs = 0; | 
|---|
| [e23d20b] | 258 | analyzeSubtypes( | 
|---|
|  | 259 | ft->get_returnVals(), stats, elSeen, n_poly, seen_poly, max_depth, depth, | 
|---|
| [326cd2b] | 260 | n_subs ); | 
|---|
| [e23d20b] | 261 | analyzeSubtypes( | 
|---|
|  | 262 | ft->get_parameters(), stats, elSeen, n_poly, seen_poly, max_depth, depth, | 
|---|
| [326cd2b] | 263 | n_subs ); | 
|---|
|  | 264 | ++stats.n_generic_params.at( n_subs ); | 
|---|
|  | 265 | } else if ( TypeInstType* vt = dynamic_cast<TypeInstType*>(ty) ) { | 
|---|
|  | 266 | if ( ! seen_poly ) { | 
|---|
|  | 267 | ++n_poly; | 
|---|
|  | 268 | seen_poly = true; | 
|---|
|  | 269 | } | 
|---|
| [e23d20b] | 270 | countType( | 
|---|
|  | 271 | vt->get_name(), n_agg, stats.compound_type_names, stats.compound_type_decls, | 
|---|
| [326cd2b] | 272 | elSeen ); | 
|---|
|  | 273 | update_max( max_depth, depth ); | 
|---|
|  | 274 | } else if ( ReferenceToType* st = dynamic_cast<ReferenceToType*>(ty) ) { | 
|---|
|  | 275 | std::list<Expression*>& params = st->get_parameters(); | 
|---|
|  | 276 | if ( params.empty() ) { | 
|---|
| [e23d20b] | 277 | countType( | 
|---|
|  | 278 | st->get_name(), n_agg, stats.compound_type_names, | 
|---|
| [326cd2b] | 279 | stats.compound_type_decls, elSeen ); | 
|---|
|  | 280 | update_max( max_depth, depth ); | 
|---|
|  | 281 | } else { | 
|---|
| [e23d20b] | 282 | countType( | 
|---|
|  | 283 | st->get_name(), n_generic, stats.generic_type_names, | 
|---|
| [326cd2b] | 284 | stats.generic_type_decls, elSeen); | 
|---|
|  | 285 | analyzeSubtypes( params, stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 286 | ++stats.n_generic_params.at( params.size() ); | 
|---|
|  | 287 | } | 
|---|
|  | 288 | } else if ( TupleType* tt = dynamic_cast<TupleType*>(ty) ) { | 
|---|
|  | 289 | std::string name = "[,]"; | 
|---|
| [e23d20b] | 290 | countType( | 
|---|
| [326cd2b] | 291 | name, n_generic, stats.generic_type_names, stats.generic_type_decls, elSeen); | 
|---|
| [e23d20b] | 292 | analyzeSubtypes( | 
|---|
| [326cd2b] | 293 | tt->get_types(), stats, elSeen, n_poly, seen_poly, max_depth, depth ); | 
|---|
|  | 294 | ++stats.n_generic_params.at( tt->size() ); | 
|---|
|  | 295 | } else if ( dynamic_cast<VarArgsType*>(ty) ) { | 
|---|
|  | 296 | std::string name = "..."; | 
|---|
| [e23d20b] | 297 | countType( | 
|---|
| [326cd2b] | 298 | name, n_agg, stats.compound_type_names, stats.compound_type_decls, elSeen ); | 
|---|
|  | 299 | update_max( max_depth, depth ); | 
|---|
|  | 300 | } else if ( dynamic_cast<ZeroType*>(ty) ) { | 
|---|
|  | 301 | std::string name = "0"; | 
|---|
|  | 302 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); | 
|---|
|  | 303 | update_max( max_depth, depth ); | 
|---|
|  | 304 | } else if ( dynamic_cast<OneType*>(ty) ) { | 
|---|
|  | 305 | std::string name = "1"; | 
|---|
|  | 306 | countType( name, n_basic, stats.basic_type_names, stats.basic_type_decls, elSeen ); | 
|---|
|  | 307 | update_max( max_depth, depth ); | 
|---|
|  | 308 | } | 
|---|
|  | 309 | } | 
|---|
|  | 310 |  | 
|---|
| [f923b5f] | 311 | /// Update arg pack stats based on a declaration list | 
|---|
| [e23d20b] | 312 | void analyze( Stats& stats, std::unordered_set<std::string>& seen, | 
|---|
|  | 313 | std::unordered_set<std::string>& elSeen, ArgPackStats& pstats, | 
|---|
| [326cd2b] | 314 | std::list<DeclarationWithType*>& decls ) { | 
|---|
| [eed5e48] | 315 | std::unordered_set<std::string> types; | 
|---|
| [326cd2b] | 316 | unsigned n = 0;                 ///< number of args/returns | 
|---|
|  | 317 | unsigned n_basic = 0;           ///< number of basic types | 
|---|
|  | 318 | unsigned n_generic = 0;         ///< number of generic types (includes "*", "&", "[]", "(*)", "[,]") | 
|---|
|  | 319 | unsigned n_poly = 0;            ///< number of polymorphic types | 
|---|
|  | 320 | unsigned n_agg = 0;             ///< number of non-generic aggregate types | 
|---|
|  | 321 | unsigned n_new = 0;             ///< number of new types | 
|---|
| [e23d20b] | 322 |  | 
|---|
| [f923b5f] | 323 | for ( auto decl : decls ) { | 
|---|
| [eed5e48] | 324 | Type* dt = decl->get_type(); | 
|---|
|  | 325 |  | 
|---|
|  | 326 | n += dt->size(); | 
|---|
| [bf2438c] | 327 |  | 
|---|
| [eed5e48] | 328 | std::stringstream ss; | 
|---|
|  | 329 | dt->print( ss ); | 
|---|
|  | 330 | types.insert( ss.str() ); | 
|---|
| [326cd2b] | 331 | if ( seen.insert( ss.str() ).second ) { ++n_new; } | 
|---|
|  | 332 |  | 
|---|
|  | 333 | bool seen_poly = false; | 
|---|
|  | 334 | unsigned max_depth = 0; | 
|---|
| [e23d20b] | 335 | analyzeType( | 
|---|
| [326cd2b] | 336 | dt, stats, elSeen, n_basic, n_generic, n_poly, n_agg, seen_poly, max_depth ); | 
|---|
|  | 337 | ++stats.n_generic_nesting.at( max_depth ); | 
|---|
| [f923b5f] | 338 | } | 
|---|
| [e23d20b] | 339 |  | 
|---|
| [f923b5f] | 340 | ++pstats.n.at( n ); | 
|---|
|  | 341 | ++pstats.n_basic.at( n_basic ); | 
|---|
| [326cd2b] | 342 | ++pstats.n_generic.at( n_generic ); | 
|---|
| [f923b5f] | 343 | ++pstats.n_poly.at( n_poly ); | 
|---|
| [326cd2b] | 344 | ++pstats.n_compound.at( n_agg ); | 
|---|
| [f923b5f] | 345 | if ( n > 0 ) { | 
|---|
|  | 346 | ++pstats.p_basic[ n_basic*100/n ]; | 
|---|
| [326cd2b] | 347 | ++pstats.p_generic[ n_generic*100/n ]; | 
|---|
| [f923b5f] | 348 | ++pstats.p_poly[ n_poly*100/n ]; | 
|---|
| [326cd2b] | 349 | ++pstats.p_compound[ n_agg*100/n ]; | 
|---|
|  | 350 | if ( n > 1 ) ++pstats.p_new[ (n_new-1)*100/(n-1) ]; | 
|---|
| [f923b5f] | 351 | } | 
|---|
| [eed5e48] | 352 | ++pstats.n_types.at( types.size() ); | 
|---|
| [f923b5f] | 353 | } | 
|---|
| [bf2438c] | 354 |  | 
|---|
| [f923b5f] | 355 | void analyzeFunc( FunctionType* fnTy, Stats& stats, ArgPackStats& params, ArgPackStats& returns ) { | 
|---|
| [131dbb3] | 356 | std::unordered_set<std::string> seen; | 
|---|
| [326cd2b] | 357 | std::unordered_set<std::string> elSeen; | 
|---|
|  | 358 | analyze( stats, seen, elSeen, params, fnTy->get_parameters() ); | 
|---|
|  | 359 | analyze( stats, seen, elSeen, returns, fnTy->get_returnVals() ); | 
|---|
| [6a5f0e7] | 360 | } | 
|---|
|  | 361 |  | 
|---|
| [31868da] | 362 | void analyzeExpr( UntypedExpr *expr, unsigned depth ) { | 
|---|
|  | 363 | auto& args = expr->get_args(); | 
|---|
|  | 364 | unsigned fanout = args.size(); | 
|---|
| [bf2438c] | 365 |  | 
|---|
| [31868da] | 366 | ++exprs_by_fanout_at_depth[ std::make_pair(depth, fanout) ]; | 
|---|
|  | 367 | for ( Expression* arg : args ) { | 
|---|
|  | 368 | if ( UntypedExpr *uearg = dynamic_cast<UntypedExpr*>(arg) ) { | 
|---|
|  | 369 | analyzeExpr( uearg, depth+1 ); | 
|---|
|  | 370 | } | 
|---|
|  | 371 | } | 
|---|
|  | 372 | } | 
|---|
|  | 373 |  | 
|---|
| [fa2de95] | 374 | public: | 
|---|
| [e5c74d1] | 375 | void previsit( FunctionDecl *decl ) { | 
|---|
| [fa2de95] | 376 | // skip if already seen declaration for this function | 
|---|
| [e5c74d1] | 377 | const std::string& mangleName = decl->get_mangleName().empty() ? decl->name : decl->get_mangleName(); | 
|---|
|  | 378 | if ( seen_names.insert( mangleName ).second ) { | 
|---|
| [68fe946e] | 379 | Stats& stats = for_linkage[ linkage_index( decl->linkage ) ]; | 
|---|
| [e5c74d1] | 380 |  | 
|---|
|  | 381 | ++stats.n_decls; | 
|---|
|  | 382 | FunctionType* fnTy = decl->type; | 
|---|
|  | 383 | const Type::ForallList& forall = fnTy->forall; | 
|---|
|  | 384 | ++stats.n_type_params.at( forall.size() ); | 
|---|
|  | 385 | unsigned n_assns = 0; | 
|---|
|  | 386 | for ( TypeDecl* fdecl : forall ) { | 
|---|
|  | 387 | n_assns += fdecl->assertions.size(); | 
|---|
|  | 388 | for ( DeclarationWithType* assn : fdecl->assertions ) { | 
|---|
|  | 389 | FunctionType *assnTy = nullptr; | 
|---|
|  | 390 | if ( ObjectDecl *assnObj = dynamic_cast<ObjectDecl*>(assn) ) { | 
|---|
|  | 391 | if ( PointerType *ptrTy = dynamic_cast<PointerType*>(assnObj->get_type()) ) { | 
|---|
| [e23d20b] | 392 | assnTy = dynamic_cast<FunctionType*>(ptrTy->base); | 
|---|
| [e5c74d1] | 393 | } else assnTy = dynamic_cast<FunctionType*>(assnObj->get_type()); | 
|---|
|  | 394 | } else if ( FunctionDecl *assnDecl = dynamic_cast<FunctionDecl*>(assn) ) { | 
|---|
|  | 395 | assnTy = assnDecl->type; | 
|---|
|  | 396 | } | 
|---|
|  | 397 | if ( assnTy ) analyzeFunc( assnTy, stats, stats.assn_params, stats.assn_returns ); | 
|---|
| [6a5f0e7] | 398 | } | 
|---|
|  | 399 | } | 
|---|
| [e5c74d1] | 400 | ++stats.n_assns[ n_assns ]; | 
|---|
| [e23d20b] | 401 | ++stats.by_name[ decl->name ]; | 
|---|
| [e5c74d1] | 402 | analyzeFunc( fnTy, stats, stats.params, stats.returns ); | 
|---|
|  | 403 | } | 
|---|
| [fa2de95] | 404 | } | 
|---|
|  | 405 |  | 
|---|
| [e5c74d1] | 406 | void previsit( UntypedExpr *expr ) { | 
|---|
|  | 407 | visit_children = false; | 
|---|
| [31868da] | 408 | analyzeExpr( expr, 0 ); | 
|---|
|  | 409 | } | 
|---|
|  | 410 |  | 
|---|
| [fa2de95] | 411 | private: | 
|---|
|  | 412 | template<typename F> | 
|---|
| [424931d] | 413 | void printAll( const std::string& name, F extract ) { | 
|---|
| [fa2de95] | 414 | std::cout << "\"" << name << "\","; | 
|---|
|  | 415 | for ( const auto& stats : for_linkage ) { | 
|---|
|  | 416 | std::cout << "," << extract(stats); | 
|---|
|  | 417 | } | 
|---|
|  | 418 | std::cout << "," << extract(total) << std::endl; | 
|---|
|  | 419 | } | 
|---|
|  | 420 |  | 
|---|
|  | 421 | template<typename F> | 
|---|
| [424931d] | 422 | void printAllMap( const std::string& name, F extract ) { | 
|---|
| [fa2de95] | 423 | for ( const auto& entry : extract(total) ) { | 
|---|
|  | 424 | const auto& key = entry.first; | 
|---|
|  | 425 | std::cout << "\"" << name << "\"," << key; | 
|---|
|  | 426 | for ( const auto& stats : for_linkage ) { | 
|---|
|  | 427 | const auto& map = extract(stats); | 
|---|
|  | 428 | auto it = map.find( key ); | 
|---|
|  | 429 | if ( it == map.end() ) std::cout << ",0"; | 
|---|
|  | 430 | else std::cout << "," << it->second; | 
|---|
|  | 431 | } | 
|---|
|  | 432 | std::cout  << "," << entry.second << std::endl; | 
|---|
|  | 433 | } | 
|---|
|  | 434 | } | 
|---|
|  | 435 |  | 
|---|
|  | 436 | template<typename F> | 
|---|
| [424931d] | 437 | void printAllHisto( const std::string& name, F extract ) { | 
|---|
| [326cd2b] | 438 | VectorMap<unsigned> histos[n_named_specs]; | 
|---|
| [fa2de95] | 439 | VectorMap<unsigned> thisto; | 
|---|
|  | 440 |  | 
|---|
|  | 441 | for ( const auto& entry : extract(total) ) { ++thisto.at( entry.second ); } | 
|---|
|  | 442 |  | 
|---|
| [326cd2b] | 443 | for ( unsigned i = 0; i < n_named_specs; ++i ) { | 
|---|
| [fa2de95] | 444 | // can't be a higher count in one of the sub-histograms than the total | 
|---|
|  | 445 | histos[i].reserve( thisto.size() ); | 
|---|
|  | 446 |  | 
|---|
|  | 447 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } | 
|---|
|  | 448 | } | 
|---|
|  | 449 |  | 
|---|
|  | 450 | for ( unsigned i = 0; i < thisto.size(); ++i ) { | 
|---|
|  | 451 | std::cout << "\"" << name << "\"," << i; | 
|---|
|  | 452 | for ( const auto& histo : histos ) { | 
|---|
|  | 453 | std::cout << "," << histo[i]; | 
|---|
|  | 454 | } | 
|---|
|  | 455 | std::cout << "," << thisto[i] << std::endl; | 
|---|
|  | 456 | } | 
|---|
|  | 457 | } | 
|---|
| [6a5f0e7] | 458 |  | 
|---|
|  | 459 | template<typename F> | 
|---|
| [424931d] | 460 | void printAllSparseHisto( const std::string& name, F extract ) { | 
|---|
| [326cd2b] | 461 | std::map<unsigned, unsigned> histos[n_named_specs]; | 
|---|
| [6a5f0e7] | 462 | std::map<unsigned, unsigned> thisto; | 
|---|
|  | 463 |  | 
|---|
|  | 464 | for ( const auto& entry : extract(total) ) { ++thisto[ entry.second ]; } | 
|---|
|  | 465 |  | 
|---|
| [326cd2b] | 466 | for ( unsigned i = 0; i < n_named_specs; ++i ) { | 
|---|
| [6a5f0e7] | 467 | for ( const auto& entry : extract(for_linkage[i]) ) { ++histos[i][entry.second]; } | 
|---|
|  | 468 | } | 
|---|
|  | 469 |  | 
|---|
|  | 470 | for ( const auto& entry : thisto ) { | 
|---|
|  | 471 | const auto& key = entry.first; | 
|---|
|  | 472 | std::cout << "\"" << name << "\"," << key; | 
|---|
| [326cd2b] | 473 | for ( unsigned i = 0; i < n_named_specs; ++i ) { | 
|---|
| [6a5f0e7] | 474 | auto it = histos[i].find( key ); | 
|---|
|  | 475 | if ( it == histos[i].end() ) std::cout << ",0"; | 
|---|
|  | 476 | else std::cout << "," << it->second; | 
|---|
|  | 477 | } | 
|---|
|  | 478 | std::cout << "," << entry.second << std::endl; | 
|---|
|  | 479 | } | 
|---|
|  | 480 | } | 
|---|
| [424931d] | 481 |  | 
|---|
|  | 482 | template<typename F> | 
|---|
|  | 483 | void printAllPack( const std::string& name, F extract ) { | 
|---|
|  | 484 | printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; }); | 
|---|
| [326cd2b] | 485 | printAllMap("n_generic_" + name, [&extract](const Stats& stats) { return extract(stats).n_generic; }); | 
|---|
| [424931d] | 486 | printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; }); | 
|---|
| [326cd2b] | 487 | printAllMap("n_compound_" + name, [&extract](const Stats& stats) { return extract(stats).n_compound; }); | 
|---|
| [424931d] | 488 | printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; }); | 
|---|
|  | 489 | printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; }); | 
|---|
| [326cd2b] | 490 | printAllMap("%_generic_" + name, [&extract](const Stats& stats) { return extract(stats).p_generic; }); | 
|---|
| [424931d] | 491 | printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; }); | 
|---|
| [326cd2b] | 492 | printAllMap("%_compound_" + name, [&extract](const Stats& stats) { return extract(stats).p_compound; }); | 
|---|
| [eed5e48] | 493 | printAllMap("n_distinct_types_" + name, [&extract](const Stats& stats) { return extract(stats).n_types; }); | 
|---|
| [131dbb3] | 494 | printAllMap("%_new_types_in_" + name, [&extract](const Stats& stats) { return extract(stats).p_new; }); | 
|---|
| [424931d] | 495 | } | 
|---|
| [31868da] | 496 |  | 
|---|
| [bf2438c] | 497 | void printPairMap( const std::string& name, | 
|---|
| [31868da] | 498 | const std::map<std::pair<unsigned, unsigned>, unsigned>& map ) { | 
|---|
|  | 499 | for ( const auto& entry : map ) { | 
|---|
|  | 500 | const auto& key = entry.first; | 
|---|
| [bf2438c] | 501 | std::cout << "\"" << name << "\"," << key.first << "," << key.second << "," | 
|---|
| [31868da] | 502 | << entry.second << std::endl; | 
|---|
|  | 503 | } | 
|---|
|  | 504 | } | 
|---|
| [bf2438c] | 505 |  | 
|---|
| [fa2de95] | 506 | public: | 
|---|
|  | 507 | void print() { | 
|---|
|  | 508 | for ( auto& stats : for_linkage ) { | 
|---|
|  | 509 | total += stats; | 
|---|
|  | 510 | } | 
|---|
|  | 511 |  | 
|---|
| [326cd2b] | 512 | std::cout << ",,\"intrinsic\",\"Cforall\",\"C\",\"autogen\",\"compiler\",\"builtinCFA\",\"builtinC\",\"other\",\"TOTAL\"" << std::endl; | 
|---|
| [fa2de95] | 513 |  | 
|---|
| [6a5f0e7] | 514 | printAllMap("n_type_params", [](const Stats& stats) { return stats.n_type_params; }); | 
|---|
| [326cd2b] | 515 | printAllMap("n_generic_params", [](const Stats& stats) { return stats.n_generic_params; }); | 
|---|
|  | 516 | printAllMap("n_generic_nesting", [](const Stats& stats) { return stats.n_generic_nesting; }); | 
|---|
| [fa2de95] | 517 | printAll("n_decls", [](const Stats& stats) { return stats.n_decls; }); | 
|---|
|  | 518 | printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); }); | 
|---|
| [6a5f0e7] | 519 | printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; }); | 
|---|
| [f923b5f] | 520 | printAll("basic_type_names", [](const Stats& stats) { return stats.basic_type_names.size(); }); | 
|---|
| [97d246d] | 521 | printAllSparseHisto("basic_type_uses", [](const Stats& stats) { return stats.basic_type_names; }); | 
|---|
| [131dbb3] | 522 | printAllSparseHisto("decls_using_basic_type", [](const Stats& stats) { return stats.basic_type_decls; }); | 
|---|
| [326cd2b] | 523 | printAll("generic_type_names", [](const Stats& stats) { return stats.generic_type_names.size(); }); | 
|---|
|  | 524 | printAllSparseHisto("generic_type_uses", [](const Stats& stats) { return stats.generic_type_names; }); | 
|---|
|  | 525 | printAllSparseHisto("decls_using_generic_type", [](const Stats& stats) { return stats.generic_type_decls; }); | 
|---|
| [f923b5f] | 526 | printAll("compound_type_names", [](const Stats& stats) { return stats.compound_type_names.size(); }); | 
|---|
| [97d246d] | 527 | printAllSparseHisto("compound_type_uses", [](const Stats& stats) { return stats.compound_type_names; }); | 
|---|
| [131dbb3] | 528 | printAllSparseHisto("decls_using_compound_type", [](const Stats& stats) { return stats.compound_type_decls; }); | 
|---|
| [424931d] | 529 | printAllPack("params", [](const Stats& stats) { return stats.params; }); | 
|---|
|  | 530 | printAllPack("returns", [](const Stats& stats) { return stats.returns; }); | 
|---|
|  | 531 | printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; }); | 
|---|
|  | 532 | printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; }); | 
|---|
|  | 533 | printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; }); | 
|---|
| [31868da] | 534 | std::cout << std::endl; | 
|---|
|  | 535 |  | 
|---|
|  | 536 | printPairMap("exprs_by_depth+fanout", exprs_by_fanout_at_depth); | 
|---|
| [fa2de95] | 537 | } | 
|---|
|  | 538 | }; | 
|---|
| [41a7137] | 539 |  | 
|---|
|  | 540 | void printDeclStats( std::list< Declaration * > &translationUnit ) { | 
|---|
| [e5c74d1] | 541 | PassVisitor<DeclStats> stats; | 
|---|
| [fa2de95] | 542 | acceptAll( translationUnit, stats ); | 
|---|
| [e5c74d1] | 543 | stats.pass.print(); | 
|---|
| [41a7137] | 544 | } | 
|---|
| [bf2438c] | 545 |  | 
|---|
| [41a7137] | 546 | } // namespace CodeTools | 
|---|
|  | 547 |  | 
|---|
|  | 548 | // Local Variables: // | 
|---|
|  | 549 | // tab-width: 4 // | 
|---|
|  | 550 | // mode: c++ // | 
|---|
|  | 551 | // compile-command: "make install" // | 
|---|
|  | 552 | // End: // | 
|---|