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/Atomic.h"
30 #include "llvm/Support/Valgrind.h"
41 volatile llvm::sys::cas_flag Value;
44 llvm::sys::cas_flag getValue() const { return Value; }
45 const char *getName() const { return Name; }
46 const char *getDesc() const { return Desc; }
48 /// construct - This should only be called for non-global statistics.
49 void construct(const char *name, const char *desc) {
50 Name = name; Desc = desc;
51 Value = 0; Initialized = false;
54 // Allow use of this class as the value itself.
55 operator unsigned() const { return Value; }
57 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
58 const Statistic &operator=(unsigned Val) {
63 const Statistic &operator++() {
64 // FIXME: This function and all those that follow carefully use an
65 // atomic operation to update the value safely in the presence of
66 // concurrent accesses, but not to read the return value, so the
67 // return value is not thread safe.
68 sys::AtomicIncrement(&Value);
72 unsigned operator++(int) {
74 unsigned OldValue = Value;
75 sys::AtomicIncrement(&Value);
79 const Statistic &operator--() {
80 sys::AtomicDecrement(&Value);
84 unsigned operator--(int) {
86 unsigned OldValue = Value;
87 sys::AtomicDecrement(&Value);
91 const Statistic &operator+=(const unsigned &V) {
93 sys::AtomicAdd(&Value, V);
97 const Statistic &operator-=(const unsigned &V) {
99 sys::AtomicAdd(&Value, -V);
103 const Statistic &operator*=(const unsigned &V) {
104 sys::AtomicMul(&Value, V);
108 const Statistic &operator/=(const unsigned &V) {
109 sys::AtomicDiv(&Value, V);
113 #else // Statistics are disabled in release builds.
115 const Statistic &operator=(unsigned Val) {
119 const Statistic &operator++() {
123 unsigned operator++(int) {
127 const Statistic &operator--() {
131 unsigned operator--(int) {
135 const Statistic &operator+=(const unsigned &V) {
139 const Statistic &operator-=(const unsigned &V) {
143 const Statistic &operator*=(const unsigned &V) {
147 const Statistic &operator/=(const unsigned &V) {
151 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
155 bool tmp = Initialized;
157 if (!tmp) RegisterStatistic();
158 TsanHappensAfter(this);
161 void RegisterStatistic();
164 // STATISTIC - A macro to make definition of statistics really simple. This
165 // automatically passes the DEBUG_TYPE of the file into the statistic.
166 #define STATISTIC(VARNAME, DESC) \
167 static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
169 /// \brief Enable the collection and printing of statistics.
170 void EnableStatistics();
172 /// \brief Check if statistics are enabled.
173 bool AreStatisticsEnabled();
175 /// \brief Return a file stream to print our output on.
176 std::unique_ptr<raw_fd_ostream> CreateInfoOutputFile();
178 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
179 void PrintStatistics();
181 /// \brief Print statistics to the given output stream.
182 void PrintStatistics(raw_ostream &OS);
184 } // End llvm namespace