484ff5fbebc6ca5cc5f17cc3e62d3f13d4525bc7
[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 is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/System/Atomic.h"
30
31 namespace llvm {
32
33 class Statistic {
34 public:
35   const char *Name;
36   const char *Desc;
37   unsigned Value;
38   bool Initialized;
39
40   unsigned getValue() const { return Value; }
41   const char *getName() const { return Name; }
42   const char *getDesc() const { return Desc; }
43
44   /// construct - This should only be called for non-global statistics.
45   void construct(const char *name, const char *desc) {
46     Name = name; Desc = desc;
47     Value = 0; Initialized = 0;
48   }
49
50   // Allow use of this class as the value itself.
51   operator unsigned() const { return Value; }
52   const Statistic &operator=(unsigned Val) {
53     Value = Val;
54     return init();
55   }
56   
57   const Statistic &operator++() {
58     sys::AtomicIncrement(&Value);
59     return init();
60   }
61   
62   unsigned operator++(int) {
63     init();
64     unsigned OldValue = Value;
65     sys::AtomicIncrement(&Value);
66     return OldValue;
67   }
68   
69   const Statistic &operator--() {
70     sys::AtomicDecrement(&Value);
71     return init();
72   }
73   
74   unsigned operator--(int) {
75     init();
76     unsigned OldValue = Value;
77     sys::AtomicDecrement(&Value);
78     return OldValue;
79   }
80   
81   const Statistic &operator+=(const unsigned &V) {
82     sys::AtomicAdd(&Value, V);
83     return init();
84   }
85   
86   const Statistic &operator-=(const unsigned &V) {
87     sys::AtomicAdd(&Value, -V);
88     return init();
89   }
90   
91   const Statistic &operator*=(const unsigned &V) {
92     sys::AtomicMul(&Value, V);
93     return init();
94   }
95   
96   const Statistic &operator/=(const unsigned &V) {
97     sys::AtomicDiv(&Value, V);
98     return init();
99   }
100
101 protected:
102   Statistic &init() {
103     bool tmp = Initialized;
104     sys::MemoryFence();
105     if (!tmp) RegisterStatistic();
106     return *this;
107   }
108   void RegisterStatistic();
109 };
110
111 // STATISTIC - A macro to make definition of statistics really simple.  This
112 // automatically passes the DEBUG_TYPE of the file into the statistic.
113 #define STATISTIC(VARNAME, DESC) \
114   static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
115
116 } // End llvm namespace
117
118 #endif