Oops! didn't mean to put this in there yet.
[oota-llvm.git] / include / llvm / ADT / Statistic.h
1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
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<> NumInstsKilled("gcse", "Number of instructions killed");
19 //
20 // Later, in the code: ++NumInstsKilled;
21 //
22 //===----------------------------------------------------------------------===//
23
24 #ifndef LLVM_ADT_STATISTIC_H
25 #define LLVM_ADT_STATISTIC_H
26
27 #include <ostream>
28 #include "llvm/Support/Compiler.h"
29
30 namespace llvm {
31
32 // StatisticBase - Nontemplated base class for Statistic<> class...
33 class StatisticBase {
34   const char *Name;
35   const char *Desc;
36   static unsigned NumStats;
37 protected:
38   StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
39     ++NumStats;  // Keep track of how many stats are created...
40   }
41   // Out of line virtual dtor, to give the vtable etc a home.
42   virtual ~StatisticBase();
43
44   // destroy - Called by subclass dtor so that we can still invoke virtual
45   // functions on the subclass.
46   void destroy() const;
47
48   // printValue - Overridden by template class to print out the value type...
49   virtual void printValue(std::ostream &o) const = 0;
50
51   // hasSomeData - Return true if some data has been aquired.  Avoid printing
52   // lots of zero counts.
53   //
54   virtual bool hasSomeData() const = 0;
55 };
56
57 // Statistic Class - templated on the data type we are monitoring...
58 template <typename DataType=unsigned>
59 class Statistic : private StatisticBase {
60   DataType Value;
61
62   virtual void printValue(std::ostream &o) const { o << Value; }
63   virtual bool hasSomeData() const { return Value != DataType(); }
64 public:
65   // Normal constructor, default initialize data item...
66   Statistic(const char *name, const char *desc)
67     : StatisticBase(name, desc), Value(DataType()) {}
68
69   // Constructor to provide an initial value...
70   Statistic(const DataType &Val, const char *name, const char *desc)
71     : StatisticBase(name, desc), Value(Val) {}
72
73   // Print information when destroyed, iff command line option is specified
74   ~Statistic() { destroy(); }
75
76   // Allow use of this class as the value itself...
77   operator DataType() const { return Value; }
78   const Statistic &operator=(DataType Val) { Value = Val; return *this; }
79   const Statistic &operator++() { ++Value; return *this; }
80   DataType operator++(int) { return Value++; }
81   const Statistic &operator--() { --Value; return *this; }
82   DataType operator--(int) { return Value--; }
83   const Statistic &operator+=(const DataType &V) { Value += V; return *this; }
84   const Statistic &operator-=(const DataType &V) { Value -= V; return *this; }
85   const Statistic &operator*=(const DataType &V) { Value *= V; return *this; }
86   const Statistic &operator/=(const DataType &V) { Value /= V; return *this; }
87 };
88
89 EXTERN_TEMPLATE_INSTANTIATION(class Statistic<unsigned>);
90
91 } // End llvm namespace
92
93 #endif