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"
38 volatile llvm::sys::cas_flag Value;
41 llvm::sys::cas_flag getValue() const { return Value; }
42 const char *getName() const { return Name; }
43 const char *getDesc() const { return Desc; }
45 /// construct - This should only be called for non-global statistics.
46 void construct(const char *name, const char *desc) {
47 Name = name; Desc = desc;
48 Value = 0; Initialized = 0;
51 // Allow use of this class as the value itself.
52 operator unsigned() const { return Value; }
53 const Statistic &operator=(unsigned Val) {
58 const Statistic &operator++() {
59 // FIXME: This function and all those that follow carefully use an
60 // atomic operation to update the value safely in the presence of
61 // concurrent accesses, but not to read the return value, so the
62 // return value is not thread safe.
63 sys::AtomicIncrement(&Value);
67 unsigned operator++(int) {
69 unsigned OldValue = Value;
70 sys::AtomicIncrement(&Value);
74 const Statistic &operator--() {
75 sys::AtomicDecrement(&Value);
79 unsigned operator--(int) {
81 unsigned OldValue = Value;
82 sys::AtomicDecrement(&Value);
86 const Statistic &operator+=(const unsigned &V) {
88 sys::AtomicAdd(&Value, V);
92 const Statistic &operator-=(const unsigned &V) {
94 sys::AtomicAdd(&Value, -V);
98 const Statistic &operator*=(const unsigned &V) {
99 sys::AtomicMul(&Value, V);
103 const Statistic &operator/=(const unsigned &V) {
104 sys::AtomicDiv(&Value, V);
110 bool tmp = Initialized;
112 if (!tmp) RegisterStatistic();
115 void RegisterStatistic();
118 // STATISTIC - A macro to make definition of statistics really simple. This
119 // automatically passes the DEBUG_TYPE of the file into the statistic.
120 #define STATISTIC(VARNAME, DESC) \
121 static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
123 /// \brief Enable the collection and printing of statistics.
124 void EnableStatistics();
126 /// \brief Check if statistics are enabled.
127 bool AreStatisticsEnabled();
129 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
130 void PrintStatistics();
132 /// \brief Print statistics to the given output stream.
133 void PrintStatistics(raw_ostream &OS);
135 } // End llvm namespace