| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===// |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file defines the 'Statistic' class, which is designed to be an easy way |
| 4 | // to expose various success metrics from passes. These statistics are printed |
| 5 | // at the end of a run, when the -stats command line option is enabled on the |
| 6 | // 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 | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 17 | #ifndef SUPPORT_STATISTIC_H |
| 18 | #define SUPPORT_STATISTIC_H |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 19 | |
| 20 | #include <iosfwd> |
| 21 | |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 22 | // StatisticBase - Nontemplated base class for Statistic<> class... |
| 23 | class StatisticBase { |
| 24 | const char *Name; |
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 25 | const char *Desc; |
| 26 | static unsigned NumStats; |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 27 | protected: |
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 28 | StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) { |
| 29 | ++NumStats; // Keep track of how many stats are created... |
| 30 | } |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 31 | virtual ~StatisticBase() {} |
| 32 | |
| 33 | // destroy - Called by subclass dtor so that we can still invoke virtual |
| 34 | // functions on the subclass. |
| 35 | void destroy() const; |
| 36 | |
| 37 | // printValue - Overridden by template class to print out the value type... |
| Anand Shukla | f1c0d92 | 2002-06-25 20:22:25 +0000 | [diff] [blame] | 38 | virtual void printValue(std::ostream &o) const = 0; |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 39 | |
| 40 | // hasSomeData - Return true if some data has been aquired. Avoid printing |
| 41 | // lots of zero counts. |
| 42 | // |
| 43 | virtual bool hasSomeData() const = 0; |
| 44 | }; |
| 45 | |
| 46 | // Statistic Class - templated on the data type we are monitoring... |
| 47 | template <typename DataType=unsigned> |
| 48 | class Statistic : private StatisticBase { |
| 49 | DataType Value; |
| 50 | |
| Anand Shukla | f1c0d92 | 2002-06-25 20:22:25 +0000 | [diff] [blame] | 51 | virtual void printValue(std::ostream &o) const { o << Value; } |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 52 | virtual bool hasSomeData() const { return Value != DataType(); } |
| 53 | public: |
| 54 | // Normal constructor, default initialize data item... |
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 55 | Statistic(const char *name, const char *desc) |
| 56 | : StatisticBase(name, desc), Value(DataType()) {} |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 57 | |
| 58 | // Constructor to provide an initial value... |
| Chris Lattner | c758fe6 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 59 | Statistic(const DataType &Val, const char *name, const char *desc) |
| 60 | : StatisticBase(name, desc), Value(Val) {} |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 61 | |
| 62 | // Print information when destroyed, iff command line option is specified |
| 63 | ~Statistic() { destroy(); } |
| 64 | |
| 65 | // Allow use of this class as the value itself... |
| Chris Lattner | b017a3e | 2002-10-31 02:50:27 +0000 | [diff] [blame] | 66 | operator DataType() const { return Value; } |
| 67 | const Statistic &operator=(DataType Val) { Value = Val; return *this; } |
| 68 | const Statistic &operator++() { ++Value; return *this; } |
| 69 | DataType operator++(int) { return Value++; } |
| 70 | const Statistic &operator+=(const DataType &V) { Value += V; return *this; } |
| 71 | const Statistic &operator-=(const DataType &V) { Value -= V; return *this; } |
| Chris Lattner | 529ad64 | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #endif |