Detemplatize the Statistic class. The only type it is instantiated with
[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 metrics from passes.  These statistics are printed at the
12 // end of a run (from llvm_shutdown), when the -stats command line option is
13 // passed on the 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 // NOTE: Statistics *must* be declared as global variables.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #ifndef LLVM_ADT_STATISTIC_H
27 #define LLVM_ADT_STATISTIC_H
28
29 #include <ostream>
30 #include "llvm/Support/Compiler.h"
31
32 namespace llvm {
33
34 // StatisticBase - Nontemplated base class for Statistic class...
35 class StatisticBase {
36   const char *Name;
37   const char *Desc;
38   static unsigned NumStats;
39 protected:
40   StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
41     ++NumStats;  // Keep track of how many stats are created...
42   }
43   // Out of line virtual dtor, to give the vtable etc a home.
44   virtual ~StatisticBase();
45
46   // destroy - Called by subclass dtor so that we can still invoke virtual
47   // functions on the subclass.
48   void destroy() const;
49
50   // printValue - Overridden by template class to print out the value type...
51   virtual void printValue(std::ostream &o) const = 0;
52
53   // hasSomeData - Return true if some data has been aquired.  Avoid printing
54   // lots of zero counts.
55   //
56   virtual bool hasSomeData() const = 0;
57 };
58
59 // Statistic Class - templated on the data type we are monitoring...
60 class Statistic : private StatisticBase {
61   unsigned Value;
62
63   virtual void printValue(std::ostream &o) const { o << Value; }
64   virtual bool hasSomeData() const { return Value != 0; }
65 public:
66   // Normal constructor, default initialize data item...
67   Statistic(const char *name, const char *desc)
68     : StatisticBase(name, desc), Value(0) {}
69
70   // Constructor to provide an initial value...
71   Statistic(const unsigned &Val, const char *name, const char *desc)
72     : StatisticBase(name, desc), Value(Val) {}
73
74   // Print information when destroyed, iff command line option is specified
75   ~Statistic() { destroy(); }
76
77   // Allow use of this class as the value itself...
78   operator unsigned() const { return Value; }
79   const Statistic &operator=(unsigned Val) { Value = Val; return *this; }
80   const Statistic &operator++() { ++Value; return *this; }
81   unsigned operator++(int) { return Value++; }
82   const Statistic &operator--() { --Value; return *this; }
83   unsigned operator--(int) { return Value--; }
84   const Statistic &operator+=(const unsigned &V) { Value += V; return *this; }
85   const Statistic &operator-=(const unsigned &V) { Value -= V; return *this; }
86   const Statistic &operator*=(const unsigned &V) { Value *= V; return *this; }
87   const Statistic &operator/=(const unsigned &V) { Value /= V; return *this; }
88 };
89
90 } // End llvm namespace
91
92 #endif