Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame^] | 1 | //===-- Support/StatisticReporter.h - Easy way to expose stats ---*- C++ -*-==// |
| 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 | |
| 17 | #ifndef SUPPORT_STATISTIC_REPORTER_H |
| 18 | #define SUPPORT_STATISTIC_REPORTER_H |
| 19 | |
| 20 | #include <iosfwd> |
| 21 | |
| 22 | // StatisticBase - Nontemplated base class for Statistic<> class... |
| 23 | class StatisticBase { |
| 24 | const char *Name; |
| 25 | protected: |
| 26 | StatisticBase(const char *name) : Name(name) {} |
| 27 | virtual ~StatisticBase() {} |
| 28 | |
| 29 | // destroy - Called by subclass dtor so that we can still invoke virtual |
| 30 | // functions on the subclass. |
| 31 | void destroy() const; |
| 32 | |
| 33 | // printValue - Overridden by template class to print out the value type... |
| 34 | virtual void printValue(ostream &o) const = 0; |
| 35 | |
| 36 | // hasSomeData - Return true if some data has been aquired. Avoid printing |
| 37 | // lots of zero counts. |
| 38 | // |
| 39 | virtual bool hasSomeData() const = 0; |
| 40 | }; |
| 41 | |
| 42 | // Statistic Class - templated on the data type we are monitoring... |
| 43 | template <typename DataType=unsigned> |
| 44 | class Statistic : private StatisticBase { |
| 45 | DataType Value; |
| 46 | |
| 47 | virtual void printValue(ostream &o) const { o << Value; } |
| 48 | virtual bool hasSomeData() const { return Value != DataType(); } |
| 49 | public: |
| 50 | // Normal constructor, default initialize data item... |
| 51 | Statistic(const char *name) : StatisticBase(name), Value(DataType()) {} |
| 52 | |
| 53 | // Constructor to provide an initial value... |
| 54 | Statistic(const DataType &Val, const char *name) |
| 55 | : StatisticBase(name), Value(Val) {} |
| 56 | |
| 57 | // Print information when destroyed, iff command line option is specified |
| 58 | ~Statistic() { destroy(); } |
| 59 | |
| 60 | // Allow use of this class as the value itself... |
| 61 | inline operator DataType() const { return Value; } |
| 62 | inline const DataType &operator=(DataType Val) { Value = Val; return Value; } |
| 63 | inline const DataType &operator++() { return ++Value; } |
| 64 | inline DataType operator++(int) { return Value++; } |
| 65 | inline const DataType &operator+=(const DataType &V) { return Value += V; } |
| 66 | inline const DataType &operator-=(const DataType &V) { return Value -= V; } |
| 67 | }; |
| 68 | |
| 69 | #endif |