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; }
54 const Statistic &operator=(unsigned Val) {
59 const Statistic &operator++() {
60 // FIXME: This function and all those that follow carefully use an
61 // atomic operation to update the value safely in the presence of
62 // concurrent accesses, but not to read the return value, so the
63 // return value is not thread safe.
64 sys::AtomicIncrement(&Value);
68 unsigned operator++(int) {
70 unsigned OldValue = Value;
71 sys::AtomicIncrement(&Value);
75 const Statistic &operator--() {
76 sys::AtomicDecrement(&Value);
80 unsigned operator--(int) {
82 unsigned OldValue = Value;
83 sys::AtomicDecrement(&Value);
87 const Statistic &operator+=(const unsigned &V) {
89 sys::AtomicAdd(&Value, V);
93 const Statistic &operator-=(const unsigned &V) {
95 sys::AtomicAdd(&Value, -V);
99 const Statistic &operator*=(const unsigned &V) {
100 sys::AtomicMul(&Value, V);
104 const Statistic &operator/=(const unsigned &V) {
105 sys::AtomicDiv(&Value, V);
111 bool tmp = Initialized;
113 if (!tmp) RegisterStatistic();
114 TsanHappensAfter(this);
117 void RegisterStatistic();
120 // STATISTIC - A macro to make definition of statistics really simple. This
121 // automatically passes the DEBUG_TYPE of the file into the statistic.
122 #define STATISTIC(VARNAME, DESC) \
123 static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
125 /// \brief Enable the collection and printing of statistics.
126 void EnableStatistics();
128 /// \brief Check if statistics are enabled.
129 bool AreStatisticsEnabled();
131 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
132 void PrintStatistics();
134 /// \brief Print statistics to the given output stream.
135 void PrintStatistics(raw_ostream &OS);
137 } // End llvm namespace