blob: 62dec92bc4fa79c09bdebbc3e3b9012d64b4c5b0 [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===//
Chris Lattnerfa10fdf2002-05-10 15:36:56 +00002//
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
Chris Lattner96ef1b92002-10-01 22:35:45 +000017#ifndef SUPPORT_STATISTIC_H
18#define SUPPORT_STATISTIC_H
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000019
20#include <iosfwd>
21
Chris Lattner4435ac02002-05-22 17:06:20 +000022// 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//
26extern 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
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000043// StatisticBase - Nontemplated base class for Statistic<> class...
44class StatisticBase {
45 const char *Name;
Chris Lattner96ef1b92002-10-01 22:35:45 +000046 const char *Desc;
47 static unsigned NumStats;
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000048protected:
Chris Lattner96ef1b92002-10-01 22:35:45 +000049 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
50 ++NumStats; // Keep track of how many stats are created...
51 }
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000052 virtual ~StatisticBase() {}
53
54 // destroy - Called by subclass dtor so that we can still invoke virtual
55 // functions on the subclass.
56 void destroy() const;
57
58 // printValue - Overridden by template class to print out the value type...
Anand Shukla4a9f9332002-06-25 20:22:25 +000059 virtual void printValue(std::ostream &o) const = 0;
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000060
61 // hasSomeData - Return true if some data has been aquired. Avoid printing
62 // lots of zero counts.
63 //
64 virtual bool hasSomeData() const = 0;
65};
66
67// Statistic Class - templated on the data type we are monitoring...
68template <typename DataType=unsigned>
69class Statistic : private StatisticBase {
70 DataType Value;
71
Anand Shukla4a9f9332002-06-25 20:22:25 +000072 virtual void printValue(std::ostream &o) const { o << Value; }
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000073 virtual bool hasSomeData() const { return Value != DataType(); }
74public:
75 // Normal constructor, default initialize data item...
Chris Lattner96ef1b92002-10-01 22:35:45 +000076 Statistic(const char *name, const char *desc)
77 : StatisticBase(name, desc), Value(DataType()) {}
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000078
79 // Constructor to provide an initial value...
Chris Lattner96ef1b92002-10-01 22:35:45 +000080 Statistic(const DataType &Val, const char *name, const char *desc)
81 : StatisticBase(name, desc), Value(Val) {}
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000082
83 // Print information when destroyed, iff command line option is specified
84 ~Statistic() { destroy(); }
85
86 // Allow use of this class as the value itself...
87 inline operator DataType() const { return Value; }
88 inline const DataType &operator=(DataType Val) { Value = Val; return Value; }
89 inline const DataType &operator++() { return ++Value; }
90 inline DataType operator++(int) { return Value++; }
91 inline const DataType &operator+=(const DataType &V) { return Value += V; }
92 inline const DataType &operator-=(const DataType &V) { return Value -= V; }
93};
94
95#endif