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"
39 volatile llvm::sys::cas_flag Value;
42 llvm::sys::cas_flag 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.
66 sys::AtomicIncrement(&Value);
70 unsigned operator++(int) {
72 unsigned OldValue = Value;
73 sys::AtomicIncrement(&Value);
77 const Statistic &operator--() {
78 sys::AtomicDecrement(&Value);
82 unsigned operator--(int) {
84 unsigned OldValue = Value;
85 sys::AtomicDecrement(&Value);
89 const Statistic &operator+=(const unsigned &V) {
91 sys::AtomicAdd(&Value, V);
95 const Statistic &operator-=(const unsigned &V) {
97 sys::AtomicAdd(&Value, -V);
101 const Statistic &operator*=(const unsigned &V) {
102 sys::AtomicMul(&Value, V);
106 const Statistic &operator/=(const unsigned &V) {
107 sys::AtomicDiv(&Value, V);
111 #else // Statistics are disabled in release builds.
113 const Statistic &operator=(unsigned Val) {
117 const Statistic &operator++() {
121 unsigned operator++(int) {
125 const Statistic &operator--() {
129 unsigned operator--(int) {
133 const Statistic &operator+=(const unsigned &V) {
137 const Statistic &operator-=(const unsigned &V) {
141 const Statistic &operator*=(const unsigned &V) {
145 const Statistic &operator/=(const unsigned &V) {
149 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
153 bool tmp = Initialized;
155 if (!tmp) RegisterStatistic();
156 TsanHappensAfter(this);
159 void RegisterStatistic();
162 // STATISTIC - A macro to make definition of statistics really simple. This
163 // automatically passes the DEBUG_TYPE of the file into the statistic.
164 #define STATISTIC(VARNAME, DESC) \
165 static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
167 /// \brief Enable the collection and printing of statistics.
168 void EnableStatistics();
170 /// \brief Check if statistics are enabled.
171 bool AreStatisticsEnabled();
173 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
174 void PrintStatistics();
176 /// \brief Print statistics to the given output stream.
177 void PrintStatistics(raw_ostream &OS);
179 } // End llvm namespace