Implement a new command line option, -debug, which is meant to unify all of
[oota-llvm.git] / include / llvm / ADT / Statistic.h
1 //===-- Support/StatisticReporter.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_REPORTER_H
18 #define SUPPORT_STATISTIC_REPORTER_H
19
20 #include <iosfwd>
21
22 // DebugFlag - This boolean is set to true if the '-debug' command line option
23 // is specified.  This should probably not be referenced directly, instead, use
24 // the DEBUG macro below.
25 //
26 extern bool DebugFlag;
27
28 // DEBUG macro - This macro should be used by passes to emit debug information.
29 // In the '-debug' option is specified on the commandline, and if this is a
30 // debug build, then the code specified as the option to the macro will be
31 // executed.  Otherwise it will not be.  Example:
32 //
33 // DEBUG(cerr << "Bitset contains: " << Bitset << "\n");
34 //
35 #ifdef NDEBUG
36 #define DEBUG(X)
37 #else
38 #define DEBUG(X) \
39   do { if (DebugFlag) { X; } } while (0)
40 #endif
41
42
43 // StatisticBase - Nontemplated base class for Statistic<> class...
44 class StatisticBase {
45   const char *Name;
46 protected:
47   StatisticBase(const char *name) : Name(name) {}
48   virtual ~StatisticBase() {}
49
50   // destroy - Called by subclass dtor so that we can still invoke virtual
51   // functions on the subclass.
52   void destroy() const;
53
54   // printValue - Overridden by template class to print out the value type...
55   virtual void printValue(ostream &o) const = 0;
56
57   // hasSomeData - Return true if some data has been aquired.  Avoid printing
58   // lots of zero counts.
59   //
60   virtual bool hasSomeData() const = 0;
61 };
62
63 // Statistic Class - templated on the data type we are monitoring...
64 template <typename DataType=unsigned>
65 class Statistic : private StatisticBase {
66   DataType Value;
67
68   virtual void printValue(ostream &o) const { o << Value; }
69   virtual bool hasSomeData() const { return Value != DataType(); }
70 public:
71   // Normal constructor, default initialize data item...
72   Statistic(const char *name) : StatisticBase(name), Value(DataType()) {}
73
74   // Constructor to provide an initial value...
75   Statistic(const DataType &Val, const char *name)
76     : StatisticBase(name), Value(Val) {}
77
78   // Print information when destroyed, iff command line option is specified
79   ~Statistic() { destroy(); }
80
81   // Allow use of this class as the value itself...
82   inline operator DataType() const { return Value; }
83   inline const DataType &operator=(DataType Val) { Value = Val; return Value; }
84   inline const DataType &operator++() { return ++Value; }
85   inline DataType operator++(int) { return Value++; }
86   inline const DataType &operator+=(const DataType &V) { return Value += V; }
87   inline const DataType &operator-=(const DataType &V) { return Value -= V; }
88 };
89
90 #endif