Refactor statistic a big and introduce a horrible-but-necessary macro
[oota-llvm.git] / include / llvm / ADT / Statistic.h
1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the 'Statistic' class, which is designed to be an easy way
11 // to expose various metrics from passes.  These statistics are printed at the
12 // end of a run (from llvm_shutdown), when the -stats command line option is
13 // passed on the 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 NumInstsKilled("gcse", "Number of instructions killed");
19 //
20 // Later, in the code: ++NumInstsKilled;
21 //
22 // NOTE: Statistics *must* be declared as global variables.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #ifndef LLVM_ADT_STATISTIC_H
27 #define LLVM_ADT_STATISTIC_H
28
29 namespace llvm {
30
31 class StatisticBase {
32 public:
33   const char *Name;
34   const char *Desc;
35   unsigned Value : 31;
36   bool Initialized : 1;
37
38   unsigned getValue() const { return Value; }
39   const char *getName() const { return Name; }
40   const char *getDesc() const { return Desc; }
41   
42   // Allow use of this class as the value itself.
43   operator unsigned() const { return Value; }
44   const StatisticBase &operator=(unsigned Val) { Value = Val; return init(); }
45   const StatisticBase &operator++() { ++Value; return init(); }
46   unsigned operator++(int) { init(); return Value++; }
47   const StatisticBase &operator--() { --Value; return init(); }
48   unsigned operator--(int) { init(); return Value--; }
49   const StatisticBase &operator+=(const unsigned &V) {Value += V;return init();}
50   const StatisticBase &operator-=(const unsigned &V) {Value -= V;return init();}
51   const StatisticBase &operator*=(const unsigned &V) {Value *= V;return init();}
52   const StatisticBase &operator/=(const unsigned &V) {Value /= V;return init();}
53   
54 private:
55   StatisticBase &init() {
56     if (!Initialized) RegisterStatistic();
57     return *this;
58   }
59   void RegisterStatistic();
60 };
61   
62 struct Statistic : public StatisticBase {
63   Statistic(const char *name, const char *desc) {
64     Name = name; Desc = desc; Value = 0; Initialized = 0;
65   }
66 };
67
68   
69 // STATISTIC - A macro to make definition of statistics really simple.  This
70 // automatically passes the DEBUG_TYPE of the file into the statistic.
71 #define STATISTIC(VARNAME, DESC) \
72   static StatisticBase VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
73
74 } // End llvm namespace
75
76 #endif