Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Statistic.cpp - Easy way to expose stats information --------------===// |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements the 'Statistic' class, which is designed to be an easy |
| 4 | // way to expose various success metrics from passes. These statistics are |
| 5 | // printed at the end of a run, when the -stats command line option is enabled |
| 6 | // on the command line. |
| 7 | // |
| 8 | // This is useful for reporting information like the number of instructions |
| 9 | // simplified, optimized or removed by various transformations, like this: |
| 10 | // |
| 11 | // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed"); |
| 12 | // |
| 13 | // Later, in the code: ++NumInstEliminated; |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 17 | #include "Support/Statistic.h" |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 18 | #include "Support/CommandLine.h" |
| 19 | #include <iostream> |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 20 | #include <sstream> |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 4435ac0 | 2002-05-22 17:06:20 +0000 | [diff] [blame] | 22 | bool DebugFlag; // DebugFlag - Exported boolean set by the -debug option |
| 23 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 24 | unsigned StatisticBase::NumStats = 0; |
| 25 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 26 | // -stats - Command line option to cause transformations to emit stats about |
| 27 | // what they did. |
| 28 | // |
| 29 | static cl::opt<bool> |
| 30 | Enabled("stats", cl::desc("Enable statistics output from program")); |
| 31 | |
| 32 | // -debug - Command line option to enable the DEBUG statements in the passes. |
| 33 | static cl::opt<bool, true> |
| 34 | Debug("debug", cl::desc("Enable debug output"), cl::Hidden, |
| 35 | cl::location(DebugFlag)); |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 37 | struct StatRecord { |
| 38 | std::string Value; |
| 39 | const char *Name, *Desc; |
| 40 | |
| 41 | StatRecord(const std::string V, const char *N, const char *D) |
| 42 | : Value(V), Name(N), Desc(D) {} |
| 43 | |
| 44 | bool operator<(const StatRecord &SR) const { |
| 45 | return std::strcmp(Name, SR.Name) < 0; |
| 46 | } |
| 47 | |
| 48 | void print(unsigned ValFieldSize, unsigned NameFieldSize) { |
| 49 | std::cerr << std::string(ValFieldSize-Value.length(), ' ') |
| 50 | << Value << " " << Name |
| 51 | << std::string(NameFieldSize-std::strlen(Name), ' ') |
| 52 | << " - " << Desc << "\n"; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | static std::vector<StatRecord> *AccumStats = 0; |
| 57 | |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 58 | // Print information when destroyed, iff command line option is specified |
| 59 | void StatisticBase::destroy() const { |
| 60 | if (Enabled && hasSomeData()) { |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 61 | if (AccumStats == 0) |
| 62 | AccumStats = new std::vector<StatRecord>(); |
| 63 | |
| 64 | std::ostringstream Out; |
| 65 | printValue(Out); |
| 66 | AccumStats->push_back(StatRecord(Out.str(), Name, Desc)); |
| 67 | } |
| 68 | |
| 69 | if (--NumStats == 0 && AccumStats) { |
| 70 | // Figure out how long the biggest Value and Name fields are... |
| 71 | unsigned MaxNameLen = 0, MaxValLen = 0; |
| 72 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) { |
Anand Shukla | c1d4d79 | 2002-10-04 23:56:18 +0000 | [diff] [blame] | 73 | MaxValLen = std::max(MaxValLen, |
| 74 | (unsigned)(*AccumStats)[i].Value.length()); |
| 75 | MaxNameLen = std::max(MaxNameLen, |
| 76 | (unsigned)std::strlen((*AccumStats)[i].Name)); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Sort the fields... |
| 80 | std::stable_sort(AccumStats->begin(), AccumStats->end()); |
| 81 | |
| 82 | // Print out the statistics header... |
| 83 | std::cerr << "===" << std::string(73, '-') << "===\n" |
| 84 | << " ... Statistics Collected ...\n" |
| 85 | << "===" << std::string(73, '-') << "===\n\n"; |
| 86 | |
| 87 | // Print all of the statistics accumulated... |
| 88 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) |
| 89 | (*AccumStats)[i].print(MaxValLen, MaxNameLen); |
| 90 | |
| 91 | std::cerr << std::endl; // Flush the output stream... |
| 92 | |
| 93 | // Free all accumulated statistics... |
| 94 | delete AccumStats; |
| 95 | AccumStats = 0; |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 96 | } |
| 97 | } |