blob: d69809ff186e54f1c5f2701e1b44e9290664f9b7 [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===//
John Criswellb2109ce2003-10-20 19:46:57 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerfa10fdf2002-05-10 15:36:56 +00009//
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<> NumInstEliminated("GCSE - Number of instructions killed");
19//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Chris Lattner96ef1b92002-10-01 22:35:45 +000024#ifndef SUPPORT_STATISTIC_H
25#define SUPPORT_STATISTIC_H
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000026
27#include <iosfwd>
28
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000029// StatisticBase - Nontemplated base class for Statistic<> class...
30class StatisticBase {
31 const char *Name;
Chris Lattner96ef1b92002-10-01 22:35:45 +000032 const char *Desc;
33 static unsigned NumStats;
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000034protected:
Chris Lattner96ef1b92002-10-01 22:35:45 +000035 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
36 ++NumStats; // Keep track of how many stats are created...
37 }
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000038 virtual ~StatisticBase() {}
39
40 // destroy - Called by subclass dtor so that we can still invoke virtual
41 // functions on the subclass.
42 void destroy() const;
43
44 // printValue - Overridden by template class to print out the value type...
Anand Shukla4a9f9332002-06-25 20:22:25 +000045 virtual void printValue(std::ostream &o) const = 0;
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000046
47 // hasSomeData - Return true if some data has been aquired. Avoid printing
48 // lots of zero counts.
49 //
50 virtual bool hasSomeData() const = 0;
51};
52
53// Statistic Class - templated on the data type we are monitoring...
54template <typename DataType=unsigned>
55class Statistic : private StatisticBase {
56 DataType Value;
57
Anand Shukla4a9f9332002-06-25 20:22:25 +000058 virtual void printValue(std::ostream &o) const { o << Value; }
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000059 virtual bool hasSomeData() const { return Value != DataType(); }
60public:
61 // Normal constructor, default initialize data item...
Chris Lattner96ef1b92002-10-01 22:35:45 +000062 Statistic(const char *name, const char *desc)
63 : StatisticBase(name, desc), Value(DataType()) {}
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000064
65 // Constructor to provide an initial value...
Chris Lattner96ef1b92002-10-01 22:35:45 +000066 Statistic(const DataType &Val, const char *name, const char *desc)
67 : StatisticBase(name, desc), Value(Val) {}
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000068
69 // Print information when destroyed, iff command line option is specified
70 ~Statistic() { destroy(); }
71
72 // Allow use of this class as the value itself...
Chris Lattner4932b312002-10-31 02:50:27 +000073 operator DataType() const { return Value; }
74 const Statistic &operator=(DataType Val) { Value = Val; return *this; }
75 const Statistic &operator++() { ++Value; return *this; }
76 DataType operator++(int) { return Value++; }
77 const Statistic &operator+=(const DataType &V) { Value += V; return *this; }
78 const Statistic &operator-=(const DataType &V) { Value -= V; return *this; }
Chris Lattnerfa10fdf2002-05-10 15:36:56 +000079};
80
81#endif