Changeset 424931d


Ignore:
Timestamp:
Feb 8, 2017, 2:34:45 PM (7 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
ea23d10
Parents:
b4d65c7
Message:

Refactored DeclStats? for extensibility and added stats on basic types

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/DeclStats.cc

    rb4d65c7 r424931d  
    3131       
    3232        class DeclStats : public Visitor {
     33                template<typename T>
     34                static void sum(T& a, const T& b) { a += b; }
     35
     36                static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) {
     37                        a.reserve( b.size() );
     38                        for ( unsigned i = 0; i < b.size(); ++i ) {
     39                                a[i] += b[i];
     40                        }
     41                }
     42
     43                template<typename K>
     44                static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) {
     45                        for ( const auto& entry : b ) {
     46                                a[ entry.first ] += entry.second;
     47                        }
     48                }
     49
     50                template<typename K>
     51                static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) {
     52                        for ( const auto& entry : b ) {
     53                                a[ entry.first ] += entry.second;
     54                        }
     55                }
     56
     57                struct ArgPackStats {
     58                        VectorMap<unsigned> n;                 ///< Count of decls with each number of elements
     59                        VectorMap<unsigned> n_basic;           ///< Count of decls with each number of basic type elements
     60                        VectorMap<unsigned> n_poly;            ///< Count of decls with each number of polymorphic elements
     61                        std::map<unsigned, unsigned> p_basic;  ///< Count of decls with each percentage of basic type elements
     62                        std::map<unsigned, unsigned> p_poly;   ///< Count of decls with each percentage of polymorphic elements
     63
     64                        ArgPackStats& operator+= (const ArgPackStats& o) {
     65                                sum(n, o.n);
     66                                sum(n_basic, o.n_basic);
     67                                sum(n_poly, o.n_poly);
     68                                sum(p_basic, o.p_basic);
     69                                sum(p_poly, o.p_poly);
     70                               
     71                                return *this;
     72                        }
     73
     74                        /// Update based on a declaration list
     75                        ArgPackStats& operator+= ( std::list<DeclarationWithType*>& decls ) {
     76                                unsigned nn = 0;
     77                                unsigned nn_basic = 0;
     78                                unsigned nn_poly = 0;
     79                                for ( auto decl : decls ) {
     80                                        nn += decl->get_type()->size();
     81                                        if ( dynamic_cast<BasicType*>( decl->get_type() ) ) ++nn_basic;
     82                                        else if ( GenPoly::hasPolyBase( decl->get_type() ) ) ++nn_poly;
     83                                }
     84                                ++n.at( nn );
     85                                ++n_basic.at( nn_basic );
     86                                ++n_poly.at( nn_poly );
     87                                if ( nn > 0 ) {
     88                                        ++p_basic[ nn_basic*100/nn ];
     89                                        ++p_poly[ nn_poly*100/nn ];
     90                                }
     91                               
     92                                return *this;
     93                        }
     94                };
     95               
    3396                struct Stats {
    3497                        unsigned n_decls;     ///< Total number of declarations
     
    37100                        /// Count of declarations with each name
    38101                        std::unordered_map<std::string, unsigned> by_name;
    39 
    40                         /// Count of declarations with each number of parameters
    41                         VectorMap<unsigned> n_params;
    42                         /// Count of declarations with each number of return types
    43                         VectorMap<unsigned> n_returns;
    44                         /// Count of declarations with each number of polymorphic parameters
    45                         VectorMap<unsigned> n_poly_params;
    46                         /// Count of declarations with each number of polymorphic return types
    47                         VectorMap<unsigned> n_poly_returns;
    48                         /// Count of declarations with each percentage of polymorphic parameters
    49                         std::map<unsigned, unsigned> p_poly_params;
    50                         /// Count of declarations with each percentage of polymorphic returns
    51                         std::map<unsigned, unsigned> p_poly_returns;
    52 
     102                        /// Stats for the parameter list
     103                        ArgPackStats params;
     104                        /// Stats for the return list
     105                        ArgPackStats returns;
     106                       
    53107                        /// Count of declarations with each number of assertions
    54                         std::map<unsigned, unsigned> n_assertions;
    55                         /// Count of assertions with each number of parameters
    56                         VectorMap<unsigned> n_assn_params;
    57                         /// Count of assertions with each number of return types
    58                         VectorMap<unsigned> n_assn_returns;
    59                         /// Count of assertions with each number of polymorphic parameters
    60                         VectorMap<unsigned> n_assn_poly_params;
    61                         /// Count of assertions with each number of polymorphic return types
    62                         VectorMap<unsigned> n_assn_poly_returns;
    63                         /// Count of assertions with each percentage of polymorphic parameters
    64                         std::map<unsigned, unsigned> p_assn_poly_params;
    65                         /// Count of assertions with each percentage of polymorphic returns
    66                         std::map<unsigned, unsigned> p_assn_poly_returns;
    67 
    68                         Stats()
    69                                 : n_decls(0), n_type_params(), by_name(),
    70                                   n_params(), n_returns(), n_poly_params(), n_poly_returns(), p_poly_params(), p_poly_returns(),
    71                                   n_assertions(), n_assn_params(), n_assn_returns(), n_assn_poly_params(), n_assn_poly_returns(), p_assn_poly_params(), p_assn_poly_returns() {}
    72 
    73                 private:
    74                         template<typename T>
    75                         static void sum(T& a, const T& b) { a += b; }
    76 
    77                         static void sum(VectorMap<unsigned>& a, const VectorMap<unsigned>& b) {
    78                                 a.reserve( b.size() );
    79                                 for ( unsigned i = 0; i < b.size(); ++i ) {
    80                                         a[i] += b[i];
    81                                 }
    82                         }
    83 
    84                         template<typename K>
    85                         static void sum(std::map<K, unsigned>& a, const std::map<K, unsigned>& b) {
    86                                 for ( const auto& entry : b ) {
    87                                         a[ entry.first ] += entry.second;
    88                                 }
    89                         }
    90 
    91                         template<typename K>
    92                         static void sum(std::unordered_map<K, unsigned>& a, const std::unordered_map<K, unsigned>& b) {
    93                                 for ( const auto& entry : b ) {
    94                                         a[ entry.first ] += entry.second;
    95                                 }
    96                         }
     108                        std::map<unsigned, unsigned> n_assns;
     109                        /// Stats for the assertions' parameters
     110                        ArgPackStats assn_params;
     111                        /// Stats for the assertions' return types
     112                        ArgPackStats assn_returns;
     113                       
     114                        Stats() : n_decls(0), n_type_params(), by_name(), params(), returns(), n_assns(), assn_params(), assn_returns() {}
    97115
    98116                public:
     
    101119                                sum( n_type_params, o.n_type_params );
    102120                                sum( by_name, o.by_name );
     121                                sum( params, o.params );
     122                                sum( returns, o.returns );
     123                                sum( n_assns, o.n_assns );
     124                                sum( assn_params, o.assn_params );
     125                                sum( assn_returns, o.assn_returns );
    103126                               
    104                                 sum( n_params, o.n_params );
    105                                 sum( n_returns, o.n_returns );
    106                                 sum( n_poly_params, o.n_poly_params );
    107                                 sum( n_poly_returns, o.n_poly_returns );
    108                                 sum( p_poly_params, o.p_poly_params );
    109                                 sum( p_poly_returns, o.p_poly_returns );
    110 
    111                                 sum( n_assertions, o.n_assertions );
    112                                 sum( n_assn_params, o.n_assn_params );
    113                                 sum( n_assn_returns, o.n_assn_returns );
    114                                 sum( n_assn_poly_params, o.n_assn_poly_params );
    115                                 sum( n_assn_poly_returns, o.n_assn_poly_returns );
    116                                 sum( p_assn_poly_params, o.p_assn_poly_params );
    117                                 sum( p_assn_poly_returns, o.p_assn_poly_returns );
    118 
    119127                                return *this;
    120128                        }
     
    125133                Stats total;
    126134
    127                 void analyzeFunc( FunctionType* fnTy, VectorMap<unsigned>& by_params, VectorMap<unsigned>& by_poly_params,
    128                                   VectorMap<unsigned>& by_returns, VectorMap<unsigned>& by_poly_returns,
    129                                   std::map<unsigned, unsigned>& p_poly_params, std::map<unsigned, unsigned>& p_poly_returns ) {
    130                         unsigned n_params = 0;
    131                         unsigned n_poly_params = 0;
    132                         for ( auto pdecl : fnTy->get_parameters() ) {
    133                                 n_params += pdecl->get_type()->size();
    134                                 if ( GenPoly::hasPolyBase( pdecl->get_type() ) ) ++n_poly_params;
    135                         }
    136                         ++by_params.at( n_params );
    137                         ++by_poly_params.at( n_poly_params );
    138                         if ( n_params > 0 ) ++p_poly_params[ n_poly_params*100/n_params ];
    139 
    140                         unsigned n_returns = 0;
    141                         unsigned n_poly_returns = 0;
    142                         for ( auto rdecl : fnTy->get_returnVals() ) {
    143                                 n_returns += rdecl->get_type()->size();
    144                                 if ( GenPoly::hasPolyBase( rdecl->get_type() ) ) ++n_poly_returns;
    145                         }
    146                         ++by_returns.at( n_returns );
    147                         ++by_poly_returns.at( n_poly_returns );
    148                         if ( n_returns > 0 ) ++p_poly_returns[ n_poly_returns*100/n_returns ];
     135                void analyzeFunc( FunctionType* fnTy, ArgPackStats& params, ArgPackStats& returns ) {
     136                        params += fnTy->get_parameters();
     137                        returns += fnTy->get_returnVals();
    149138                }
    150139
     
    163152                        const Type::ForallList& forall = fnTy->get_forall();
    164153                        ++stats.n_type_params.at( forall.size() );
    165                         unsigned n_assertions = 0;
     154                        unsigned n_assns = 0;
    166155                        for ( TypeDecl* fdecl : forall ) {
    167                                 n_assertions += fdecl->get_assertions().size();
     156                                n_assns += fdecl->get_assertions().size();
    168157                                for ( DeclarationWithType* assn : fdecl->get_assertions() ) {
    169158                                        FunctionType *assnTy = 0;
     
    175164                                                assnTy = assnDecl->get_functionType();
    176165                                        }
    177                                         if ( assnTy ) analyzeFunc( assnTy, stats.n_assn_params, stats.n_assn_poly_params, stats.n_assn_returns,
    178                                                                    stats.n_assn_poly_returns, stats.p_assn_poly_params, stats.p_assn_poly_returns );
    179                                 }
    180                         }
    181                         ++stats.n_assertions[ n_assertions ];
     166                                        if ( assnTy ) analyzeFunc( assnTy, stats.assn_params, stats.assn_returns );
     167                                }
     168                        }
     169                        ++stats.n_assns[ n_assns ];
    182170
    183171                        ++stats.by_name[ decl->get_name() ];
    184172
    185                         analyzeFunc( fnTy, stats.n_params, stats.n_poly_params, stats.n_returns,
    186                                      stats.n_poly_returns, stats.p_poly_params, stats.p_poly_returns );
     173                        analyzeFunc( fnTy, stats.params, stats.returns );
    187174                }
    188175
    189176        private:
    190177                template<typename F>
    191                 void printAll( const char* name, F extract ) {
     178                void printAll( const std::string& name, F extract ) {
    192179                        std::cout << "\"" << name << "\",";
    193180                        for ( const auto& stats : for_linkage ) {
     
    198185
    199186                template<typename F>
    200                 void printAllMap( const char* name, F extract ) {
     187                void printAllMap( const std::string& name, F extract ) {
    201188                        for ( const auto& entry : extract(total) ) {
    202189                                const auto& key = entry.first;
     
    213200
    214201                template<typename F>
    215                 void printAllHisto( const char* name, F extract ) {
     202                void printAllHisto( const std::string& name, F extract ) {
    216203                        VectorMap<unsigned> histos[LinkageSpec::NoOfSpecs];
    217204                        VectorMap<unsigned> thisto;
     
    236223
    237224                template<typename F>
    238                 void printAllSparseHisto( const char* name, F extract ) {
     225                void printAllSparseHisto( const std::string& name, F extract ) {
    239226                        std::map<unsigned, unsigned> histos[LinkageSpec::NoOfSpecs];
    240227                        std::map<unsigned, unsigned> thisto;
     
    257244                        }
    258245                }
     246
     247                template<typename F>
     248                void printAllPack( const std::string& name, F extract ) {
     249                        printAllMap("n_basic_" + name, [&extract](const Stats& stats) { return extract(stats).n_basic; });
     250                        printAllMap("n_poly_" + name, [&extract](const Stats& stats) { return extract(stats).n_poly; });
     251                        printAllMap("n_" + name, [&extract](const Stats& stats) { return extract(stats).n; });
     252                        printAllMap("%_basic_" + name, [&extract](const Stats& stats) { return extract(stats).p_basic; });
     253                        printAllMap("%_poly_" + name, [&extract](const Stats& stats) { return extract(stats).p_poly; });
     254                }
    259255               
    260256        public:
     
    270266                        printAll("unique_names", [](const Stats& stats) { return stats.by_name.size(); });
    271267                        printAllSparseHisto("overloads", [](const Stats& stats) { return stats.by_name; });
    272                        
    273                         printAllMap("n_poly_params", [](const Stats& stats) { return stats.n_poly_params; });
    274                         printAllMap("n_params", [](const Stats& stats) { return stats.n_params; });
    275                         printAllMap("%_poly_params", [](const Stats& stats) { return stats.p_poly_params; });
    276                         printAllMap("n_poly_returns", [](const Stats& stats) { return stats.n_poly_returns; });
    277                         printAllMap("n_returns", [](const Stats& stats) { return stats.n_returns; });
    278                         printAllMap("%_poly_returns", [](const Stats& stats) { return stats.p_poly_returns; });
    279 
    280                         printAllMap("n_assertions", [](const Stats& stats) { return stats.n_assertions; });
    281                         printAllMap("n_assn_poly_params", [](const Stats& stats) { return stats.n_assn_poly_params; });
    282                         printAllMap("n_assn_params", [](const Stats& stats) { return stats.n_assn_params; });
    283                         printAllMap("%_assn_poly_params", [](const Stats& stats) { return stats.p_assn_poly_params; });
    284                         printAllMap("n_assn_poly_returns", [](const Stats& stats) { return stats.n_assn_poly_returns; });
    285                         printAllMap("n_assn_returns", [](const Stats& stats) { return stats.n_assn_returns; });
    286                         printAllMap("%_assn_poly_returns", [](const Stats& stats) { return stats.p_assn_poly_returns; });
     268                        printAllPack("params", [](const Stats& stats) { return stats.params; });
     269                        printAllPack("returns", [](const Stats& stats) { return stats.returns; });
     270                        printAllMap("n_assns", [](const Stats& stats) { return stats.n_assns; });
     271                        printAllPack("assn_params", [](const Stats& stats) { return stats.assn_params; });
     272                        printAllPack("assn_returns", [](const Stats& stats) { return stats.assn_returns; });
    287273                }
    288274        };
Note: See TracChangeset for help on using the changeset viewer.