[C++11] Replace LLVM atomics with std::atomic.
[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/Support/Valgrind.h"
30 #include <atomic>
31
32 namespace llvm {
33 class raw_ostream;
34
35 class Statistic {
36 public:
37   const char *Name;
38   const char *Desc;
39   std::atomic<unsigned> Value;
40   bool Initialized;
41
42   unsigned getValue() const { return Value; }
43   const char *getName() const { return Name; }
44   const char *getDesc() const { return Desc; }
45
46   /// construct - This should only be called for non-global statistics.
47   void construct(const char *name, const char *desc) {
48     Name = name; Desc = desc;
49     Value = 0; Initialized = 0;
50   }
51
52   // Allow use of this class as the value itself.
53   operator unsigned() const { return Value; }
54
55 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
56    const Statistic &operator=(unsigned Val) {
57     Value = Val;
58     return init();
59   }
60
61   const Statistic &operator++() {
62     // FIXME: This function and all those that follow carefully use an
63     // atomic operation to update the value safely in the presence of
64     // concurrent accesses, but not to read the return value, so the
65     // return value is not thread safe.
66     ++Value;
67     return init();
68   }
69
70   unsigned operator++(int) {
71     init();
72     unsigned OldValue = Value++;
73     return OldValue;
74   }
75
76   const Statistic &operator--() {
77     --Value;
78     return init();
79   }
80
81   unsigned operator--(int) {
82     init();
83     unsigned OldValue = Value--;
84     return OldValue;
85   }
86
87   const Statistic &operator+=(const unsigned &V) {
88     if (!V) return *this;
89     Value += V;
90     return init();
91   }
92
93   const Statistic &operator-=(const unsigned &V) {
94     if (!V) return *this;
95     Value -= V;
96     return init();
97   }
98
99   const Statistic &operator*=(const unsigned &V) {
100     unsigned Original, Result;
101     do {
102       Original = Value;
103       Result = Original * V;
104     } while (!Value.compare_exchange_strong(Original, Result));
105     return init();
106   }
107
108   const Statistic &operator/=(const unsigned &V) {
109     unsigned Original, Result;
110     do {
111       Original = Value;
112       Result = Original / V;
113     } while (!Value.compare_exchange_strong(Original, Result));
114     return init();
115   }
116
117 #else  // Statistics are disabled in release builds.
118
119   const Statistic &operator=(unsigned Val) {
120     return *this;
121   }
122
123   const Statistic &operator++() {
124     return *this;
125   }
126
127   unsigned operator++(int) {
128     return 0;
129   }
130
131   const Statistic &operator--() {
132     return *this;
133   }
134
135   unsigned operator--(int) {
136     return 0;
137   }
138
139   const Statistic &operator+=(const unsigned &V) {
140     return *this;
141   }
142
143   const Statistic &operator-=(const unsigned &V) {
144     return *this;
145   }
146
147   const Statistic &operator*=(const unsigned &V) {
148     return *this;
149   }
150
151   const Statistic &operator/=(const unsigned &V) {
152     return *this;
153   }
154
155 #endif  // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
156
157 protected:
158   Statistic &init() {
159     bool tmp = Initialized;
160     std::atomic_thread_fence(std::memory_order_seq_cst);
161     if (!tmp) RegisterStatistic();
162     TsanHappensAfter(this);
163     return *this;
164   }
165   void RegisterStatistic();
166 };
167
168 // STATISTIC - A macro to make definition of statistics really simple.  This
169 // automatically passes the DEBUG_TYPE of the file into the statistic.
170 #define STATISTIC(VARNAME, DESC) \
171   static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, {}, 0 }
172
173 /// \brief Enable the collection and printing of statistics.
174 void EnableStatistics();
175
176 /// \brief Check if statistics are enabled.
177 bool AreStatisticsEnabled();
178
179 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
180 void PrintStatistics();
181
182 /// \brief Print statistics to the given output stream.
183 void PrintStatistics(raw_ostream &OS);
184
185 } // End llvm namespace
186
187 #endif