LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Statistic.h
Go to the documentation of this file.
1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
17 //
18 // static Statistic NumInstsKilled("gcse", "Number of instructions killed");
19 //
20 // Later, in the code: ++NumInstsKilled;
21 //
22 // NOTE: Statistics *must* be declared as global variables.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #ifndef LLVM_ADT_STATISTIC_H
27 #define LLVM_ADT_STATISTIC_H
28 
29 #include "llvm/Support/Atomic.h"
30 #include "llvm/Support/Valgrind.h"
31 
32 namespace llvm {
33 class raw_ostream;
34 
35 class Statistic {
36 public:
37  const char *Name;
38  const char *Desc;
41 
42  llvm::sys::cas_flag getValue() const { return Value; }
43  const char *getName() const { return Name; }
44  const char *getDesc() const { return Desc; }
45 
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;
50  }
51 
52  // Allow use of this class as the value itself.
53  operator unsigned() const { return Value; }
54 
55 #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
56  const Statistic &operator=(unsigned Val) {
57  Value = Val;
58  return init();
59  }
60 
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.
67  return init();
68  }
69 
70  unsigned operator++(int) {
71  init();
72  unsigned OldValue = Value;
74  return OldValue;
75  }
76 
77  const Statistic &operator--() {
79  return init();
80  }
81 
82  unsigned operator--(int) {
83  init();
84  unsigned OldValue = Value;
86  return OldValue;
87  }
88 
89  const Statistic &operator+=(const unsigned &V) {
90  if (!V) return *this;
91  sys::AtomicAdd(&Value, V);
92  return init();
93  }
94 
95  const Statistic &operator-=(const unsigned &V) {
96  if (!V) return *this;
97  sys::AtomicAdd(&Value, -V);
98  return init();
99  }
100 
101  const Statistic &operator*=(const unsigned &V) {
102  sys::AtomicMul(&Value, V);
103  return init();
104  }
105 
106  const Statistic &operator/=(const unsigned &V) {
107  sys::AtomicDiv(&Value, V);
108  return init();
109  }
110 
111 #else // Statistics are disabled in release builds.
112 
113  const Statistic &operator=(unsigned Val) {
114  return *this;
115  }
116 
117  const Statistic &operator++() {
118  return *this;
119  }
120 
121  unsigned operator++(int) {
122  return 0;
123  }
124 
125  const Statistic &operator--() {
126  return *this;
127  }
128 
129  unsigned operator--(int) {
130  return 0;
131  }
132 
133  const Statistic &operator+=(const unsigned &V) {
134  return *this;
135  }
136 
137  const Statistic &operator-=(const unsigned &V) {
138  return *this;
139  }
140 
141  const Statistic &operator*=(const unsigned &V) {
142  return *this;
143  }
144 
145  const Statistic &operator/=(const unsigned &V) {
146  return *this;
147  }
148 
149 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
150 
151 protected:
153  bool tmp = Initialized;
155  if (!tmp) RegisterStatistic();
156  TsanHappensAfter(this);
157  return *this;
158  }
159  void RegisterStatistic();
160 };
161 
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 }
166 
167 /// \brief Enable the collection and printing of statistics.
168 void EnableStatistics();
169 
170 /// \brief Check if statistics are enabled.
171 bool AreStatisticsEnabled();
172 
173 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
174 void PrintStatistics();
175 
176 /// \brief Print statistics to the given output stream.
177 void PrintStatistics(raw_ostream &OS);
178 
179 } // End llvm namespace
180 
181 #endif
unsigned operator++(int)
Definition: Statistic.h:70
bool Initialized
Definition: Statistic.h:40
const Statistic & operator=(unsigned Val)
Definition: Statistic.h:56
const Statistic & operator-=(const unsigned &V)
Definition: Statistic.h:95
cas_flag AtomicAdd(volatile cas_flag *ptr, cas_flag val)
const Statistic & operator+=(const unsigned &V)
Definition: Statistic.h:89
void construct(const char *name, const char *desc)
construct - This should only be called for non-global statistics.
Definition: Statistic.h:47
void RegisterStatistic()
Definition: Statistic.cpp:70
#define TsanHappensAfter(cv)
Definition: Valgrind.h:56
cas_flag AtomicDiv(volatile cas_flag *ptr, cas_flag val)
void EnableStatistics()
Enable the collection and printing of statistics.
Definition: Statistic.cpp:106
const Statistic & operator++()
Definition: Statistic.h:61
const Statistic & operator--()
Definition: Statistic.h:77
llvm::sys::cas_flag getValue() const
Definition: Statistic.h:42
void MemoryFence()
Definition: Atomic.cpp:28
const Statistic & operator*=(const unsigned &V)
Definition: Statistic.h:101
cas_flag AtomicMul(volatile cas_flag *ptr, cas_flag val)
Statistic & init()
Definition: Statistic.h:152
const Statistic & operator/=(const unsigned &V)
Definition: Statistic.h:106
const char * getDesc() const
Definition: Statistic.h:44
const char * getName() const
Definition: Statistic.h:43
const char * Name
Definition: Statistic.h:37
const char * Desc
Definition: Statistic.h:38
uint32_t cas_flag
Definition: Atomic.h:26
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
Definition: Statistic.cpp:146
LLVM Value Representation.
Definition: Value.h:66
unsigned operator--(int)
Definition: Statistic.h:82
cas_flag AtomicIncrement(volatile cas_flag *ptr)
volatile llvm::sys::cas_flag Value
Definition: Statistic.h:39
cas_flag AtomicDecrement(volatile cas_flag *ptr)
bool AreStatisticsEnabled()
Check if statistics are enabled.
Definition: Statistic.cpp:110