Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===// |
John Criswell | b2109ce | 2003-10-20 19:46:57 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the 'Statistic' class, which is designed to be an easy way |
| 11 | // to expose various success metrics from passes. These statistics are printed |
| 12 | // at the end of a run, when the -stats command line option is enabled on the |
| 13 | // command line. |
| 14 | // |
| 15 | // This is useful for reporting information like the number of instructions |
| 16 | // simplified, optimized or removed by various transformations, like this: |
| 17 | // |
| 18 | // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed"); |
| 19 | // |
| 20 | // Later, in the code: ++NumInstEliminated; |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 24 | #ifndef SUPPORT_STATISTIC_H |
| 25 | #define SUPPORT_STATISTIC_H |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 26 | |
| 27 | #include <iosfwd> |
| 28 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | namespace llvm { |
| 30 | |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 31 | // StatisticBase - Nontemplated base class for Statistic<> class... |
| 32 | class StatisticBase { |
| 33 | const char *Name; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 34 | const char *Desc; |
| 35 | static unsigned NumStats; |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 36 | protected: |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 37 | StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) { |
| 38 | ++NumStats; // Keep track of how many stats are created... |
| 39 | } |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 40 | virtual ~StatisticBase() {} |
| 41 | |
| 42 | // destroy - Called by subclass dtor so that we can still invoke virtual |
| 43 | // functions on the subclass. |
| 44 | void destroy() const; |
| 45 | |
| 46 | // printValue - Overridden by template class to print out the value type... |
Anand Shukla | 4a9f933 | 2002-06-25 20:22:25 +0000 | [diff] [blame] | 47 | virtual void printValue(std::ostream &o) const = 0; |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 48 | |
| 49 | // hasSomeData - Return true if some data has been aquired. Avoid printing |
| 50 | // lots of zero counts. |
| 51 | // |
| 52 | virtual bool hasSomeData() const = 0; |
| 53 | }; |
| 54 | |
| 55 | // Statistic Class - templated on the data type we are monitoring... |
| 56 | template <typename DataType=unsigned> |
| 57 | class Statistic : private StatisticBase { |
| 58 | DataType Value; |
| 59 | |
Anand Shukla | 4a9f933 | 2002-06-25 20:22:25 +0000 | [diff] [blame] | 60 | virtual void printValue(std::ostream &o) const { o << Value; } |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 61 | virtual bool hasSomeData() const { return Value != DataType(); } |
| 62 | public: |
| 63 | // Normal constructor, default initialize data item... |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 64 | Statistic(const char *name, const char *desc) |
| 65 | : StatisticBase(name, desc), Value(DataType()) {} |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 66 | |
| 67 | // Constructor to provide an initial value... |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 68 | Statistic(const DataType &Val, const char *name, const char *desc) |
| 69 | : StatisticBase(name, desc), Value(Val) {} |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 70 | |
| 71 | // Print information when destroyed, iff command line option is specified |
| 72 | ~Statistic() { destroy(); } |
| 73 | |
| 74 | // Allow use of this class as the value itself... |
Chris Lattner | 4932b31 | 2002-10-31 02:50:27 +0000 | [diff] [blame] | 75 | operator DataType() const { return Value; } |
| 76 | const Statistic &operator=(DataType Val) { Value = Val; return *this; } |
| 77 | const Statistic &operator++() { ++Value; return *this; } |
| 78 | DataType operator++(int) { return Value++; } |
| 79 | const Statistic &operator+=(const DataType &V) { Value += V; return *this; } |
| 80 | const Statistic &operator-=(const DataType &V) { Value -= V; return *this; } |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 83 | } // End llvm namespace |
| 84 | |
Chris Lattner | fa10fdf | 2002-05-10 15:36:56 +0000 | [diff] [blame] | 85 | #endif |