1 //===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===//
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
8 // This is useful for reporting information like the number of instructions
9 // simplified, optimized or removed by various transformations, like this:
11 // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
13 // Later, in the code: ++NumInstEliminated;
15 //===----------------------------------------------------------------------===//
17 #ifndef SUPPORT_STATISTIC_H
18 #define SUPPORT_STATISTIC_H
22 // StatisticBase - Nontemplated base class for Statistic<> class...
26 static unsigned NumStats;
28 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
29 ++NumStats; // Keep track of how many stats are created...
31 virtual ~StatisticBase() {}
33 // destroy - Called by subclass dtor so that we can still invoke virtual
34 // functions on the subclass.
37 // printValue - Overridden by template class to print out the value type...
38 virtual void printValue(std::ostream &o) const = 0;
40 // hasSomeData - Return true if some data has been aquired. Avoid printing
41 // lots of zero counts.
43 virtual bool hasSomeData() const = 0;
46 // Statistic Class - templated on the data type we are monitoring...
47 template <typename DataType=unsigned>
48 class Statistic : private StatisticBase {
51 virtual void printValue(std::ostream &o) const { o << Value; }
52 virtual bool hasSomeData() const { return Value != DataType(); }
54 // Normal constructor, default initialize data item...
55 Statistic(const char *name, const char *desc)
56 : StatisticBase(name, desc), Value(DataType()) {}
58 // Constructor to provide an initial value...
59 Statistic(const DataType &Val, const char *name, const char *desc)
60 : StatisticBase(name, desc), Value(Val) {}
62 // Print information when destroyed, iff command line option is specified
63 ~Statistic() { destroy(); }
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; }