Statistic.h revision 2dd93edfc221fe6d1e102cca694da01ca36b5d11
1//===-- Support/Statistic.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_H 18#define SUPPORT_STATISTIC_H 19 20#include <iosfwd> 21 22// StatisticBase - Nontemplated base class for Statistic<> class... 23class StatisticBase { 24 const char *Name; 25 const char *Desc; 26 static unsigned NumStats; 27protected: 28 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) { 29 ++NumStats; // Keep track of how many stats are created... 30 } 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... 38 virtual void printValue(std::ostream &o) const = 0; 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... 47template <typename DataType=unsigned> 48class Statistic : private StatisticBase { 49 DataType Value; 50 51 virtual void printValue(std::ostream &o) const { o << Value; } 52 virtual bool hasSomeData() const { return Value != DataType(); } 53public: 54 // Normal constructor, default initialize data item... 55 Statistic(const char *name, const char *desc) 56 : StatisticBase(name, desc), Value(DataType()) {} 57 58 // Constructor to provide an initial value... 59 Statistic(const DataType &Val, const char *name, const char *desc) 60 : StatisticBase(name, desc), Value(Val) {} 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... 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; } 72}; 73 74#endif 75