Statistic.h revision 7ed47a13356daed2a34cd2209a31f92552e3bdd8
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
29namespace llvm {
30
31class Statistic {
32public:
33  const char *Name;
34  const char *Desc;
35  unsigned Value : 31;
36  bool Initialized : 1;
37
38  unsigned getValue() const { return Value; }
39  const char *getName() const { return Name; }
40  const char *getDesc() const { return Desc; }
41
42  /// construct - This should only be called for non-global statistics.
43  void construct(const char *name, const char *desc) {
44    Name = name; Desc = desc;
45    Value = 0; Initialized = 0;
46  }
47
48  // Allow use of this class as the value itself.
49  operator unsigned() const { return Value; }
50  const Statistic &operator=(unsigned Val) { Value = Val; return init(); }
51  const Statistic &operator++() { ++Value; return init(); }
52  unsigned operator++(int) { init(); return Value++; }
53  const Statistic &operator--() { --Value; return init(); }
54  unsigned operator--(int) { init(); return Value--; }
55  const Statistic &operator+=(const unsigned &V) { Value += V; return init(); }
56  const Statistic &operator-=(const unsigned &V) { Value -= V; return init(); }
57  const Statistic &operator*=(const unsigned &V) { Value *= V; return init(); }
58  const Statistic &operator/=(const unsigned &V) { Value /= V; return init(); }
59
60protected:
61  Statistic &init() {
62    if (!Initialized) RegisterStatistic();
63    return *this;
64  }
65  void RegisterStatistic();
66};
67
68// STATISTIC - A macro to make definition of statistics really simple.  This
69// automatically passes the DEBUG_TYPE of the file into the statistic.
70#define STATISTIC(VARNAME, DESC) \
71  static Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
72
73} // End llvm namespace
74
75#endif
76