blob: f25ffc6b3a8351507b4ccb9a6916ddb4f343c7e1 [file] [log] [blame]
Chris Lattnerc758fe62002-10-01 22:35:45 +00001//===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===//
Chris Lattner529ad642002-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 Lattnerc758fe62002-10-01 22:35:45 +000017#ifndef SUPPORT_STATISTIC_H
18#define SUPPORT_STATISTIC_H
Chris Lattner529ad642002-05-10 15:36:56 +000019
20#include <iosfwd>
21
Chris Lattner529ad642002-05-10 15:36:56 +000022// StatisticBase - Nontemplated base class for Statistic<> class...
23class StatisticBase {
24 const char *Name;
Chris Lattnerc758fe62002-10-01 22:35:45 +000025 const char *Desc;
26 static unsigned NumStats;
Chris Lattner529ad642002-05-10 15:36:56 +000027protected:
Chris Lattnerc758fe62002-10-01 22:35:45 +000028 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
29 ++NumStats; // Keep track of how many stats are created...
30 }
Chris Lattner529ad642002-05-10 15:36:56 +000031 virtual ~StatisticBase() {}
32
33 // destroy - Called by subclass dtor so that we can still invoke virtual
34 // functions on the subclass.
35 void destroy() const;
36
37 // printValue - Overridden by template class to print out the value type...
Anand Shuklaf1c0d922002-06-25 20:22:25 +000038 virtual void printValue(std::ostream &o) const = 0;
Chris Lattner529ad642002-05-10 15:36:56 +000039
40 // hasSomeData - Return true if some data has been aquired. Avoid printing
41 // lots of zero counts.
42 //
43 virtual bool hasSomeData() const = 0;
44};
45
46// Statistic Class - templated on the data type we are monitoring...
47template <typename DataType=unsigned>
48class Statistic : private StatisticBase {
49 DataType Value;
50
Anand Shuklaf1c0d922002-06-25 20:22:25 +000051 virtual void printValue(std::ostream &o) const { o << Value; }
Chris Lattner529ad642002-05-10 15:36:56 +000052 virtual bool hasSomeData() const { return Value != DataType(); }
53public:
54 // Normal constructor, default initialize data item...
Chris Lattnerc758fe62002-10-01 22:35:45 +000055 Statistic(const char *name, const char *desc)
56 : StatisticBase(name, desc), Value(DataType()) {}
Chris Lattner529ad642002-05-10 15:36:56 +000057
58 // Constructor to provide an initial value...
Chris Lattnerc758fe62002-10-01 22:35:45 +000059 Statistic(const DataType &Val, const char *name, const char *desc)
60 : StatisticBase(name, desc), Value(Val) {}
Chris Lattner529ad642002-05-10 15:36:56 +000061
62 // Print information when destroyed, iff command line option is specified
63 ~Statistic() { destroy(); }
64
65 // Allow use of this class as the value itself...
Chris Lattnerb017a3e2002-10-31 02:50:27 +000066 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; }
Chris Lattner529ad642002-05-10 15:36:56 +000072};
73
74#endif