1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
18 // static Statistic<> NumInstsKilled("gcse", "Number of instructions killed");
20 // Later, in the code: ++NumInstsKilled;
22 //===----------------------------------------------------------------------===//
24 #ifndef LLVM_ADT_STATISTIC_H
25 #define LLVM_ADT_STATISTIC_H
31 // StatisticBase - Nontemplated base class for Statistic<> class...
35 static unsigned NumStats;
37 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
38 ++NumStats; // Keep track of how many stats are created...
40 // Out of line virtual dtor, to give the vtable etc a home.
41 virtual ~StatisticBase();
43 // destroy - Called by subclass dtor so that we can still invoke virtual
44 // functions on the subclass.
47 // printValue - Overridden by template class to print out the value type...
48 virtual void printValue(std::ostream &o) const = 0;
50 // hasSomeData - Return true if some data has been aquired. Avoid printing
51 // lots of zero counts.
53 virtual bool hasSomeData() const = 0;
56 // Statistic Class - templated on the data type we are monitoring...
57 template <typename DataType=unsigned>
58 class Statistic : private StatisticBase {
61 virtual void printValue(std::ostream &o) const { o << Value; }
62 virtual bool hasSomeData() const { return Value != DataType(); }
64 // Normal constructor, default initialize data item...
65 Statistic(const char *name, const char *desc)
66 : StatisticBase(name, desc), Value(DataType()) {}
68 // Constructor to provide an initial value...
69 Statistic(const DataType &Val, const char *name, const char *desc)
70 : StatisticBase(name, desc), Value(Val) {}
72 // Print information when destroyed, iff command line option is specified
73 ~Statistic() { destroy(); }
75 // Allow use of this class as the value itself...
76 operator DataType() const { return Value; }
77 const Statistic &operator=(DataType Val) { Value = Val; return *this; }
78 const Statistic &operator++() { ++Value; return *this; }
79 DataType operator++(int) { return Value++; }
80 const Statistic &operator--() { --Value; return *this; }
81 DataType operator--(int) { return Value--; }
82 const Statistic &operator+=(const DataType &V) { Value += V; return *this; }
83 const Statistic &operator-=(const DataType &V) { Value -= V; return *this; }
84 const Statistic &operator*=(const DataType &V) { Value *= V; return *this; }
85 const Statistic &operator/=(const DataType &V) { Value /= V; return *this; }
88 } // End llvm namespace