1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 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.
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 // NOTE: Statistics *must* be declared as global variables.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_ADT_STATISTIC_H
27 #define LLVM_ADT_STATISTIC_H
29 #include "llvm/Support/Valgrind.h"
39 std::atomic<unsigned> Value;
42 unsigned getValue() const { return Value; }
43 const char *getName() const { return Name; }
44 const char *getDesc() const { return Desc; }
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;
52 // Allow use of this class as the value itself.
53 operator unsigned() const { return Value; }
55 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
56 const Statistic &operator=(unsigned Val) {
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.
70 unsigned operator++(int) {
72 unsigned OldValue = Value++;
76 const Statistic &operator--() {
81 unsigned operator--(int) {
83 unsigned OldValue = Value--;
87 const Statistic &operator+=(const unsigned &V) {
93 const Statistic &operator-=(const unsigned &V) {
99 const Statistic &operator*=(const unsigned &V) {
100 unsigned Original, Result;
103 Result = Original * V;
104 } while (!Value.compare_exchange_strong(Original, Result));
108 const Statistic &operator/=(const unsigned &V) {
109 unsigned Original, Result;
112 Result = Original / V;
113 } while (!Value.compare_exchange_strong(Original, Result));
117 #else // Statistics are disabled in release builds.
119 const Statistic &operator=(unsigned Val) {
123 const Statistic &operator++() {
127 unsigned operator++(int) {
131 const Statistic &operator--() {
135 unsigned operator--(int) {
139 const Statistic &operator+=(const unsigned &V) {
143 const Statistic &operator-=(const unsigned &V) {
147 const Statistic &operator*=(const unsigned &V) {
151 const Statistic &operator/=(const unsigned &V) {
155 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
159 bool tmp = Initialized;
160 std::atomic_thread_fence(std::memory_order_seq_cst);
161 if (!tmp) RegisterStatistic();
162 TsanHappensAfter(this);
165 void RegisterStatistic();
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 }
173 /// \brief Enable the collection and printing of statistics.
174 void EnableStatistics();
176 /// \brief Check if statistics are enabled.
177 bool AreStatisticsEnabled();
179 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
180 void PrintStatistics();
182 /// \brief Print statistics to the given output stream.
183 void PrintStatistics(raw_ostream &OS);
185 } // End llvm namespace