Statistic.h revision 3d3a429acb1de79eb15575ed4d90a0a951fda576
1//===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 StatisticBase {
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  // Allow use of this class as the value itself.
43  operator unsigned() const { return Value; }
44  const StatisticBase &operator=(unsigned Val) { Value = Val; return init(); }
45  const StatisticBase &operator++() { ++Value; return init(); }
46  unsigned operator++(int) { init(); return Value++; }
47  const StatisticBase &operator--() { --Value; return init(); }
48  unsigned operator--(int) { init(); return Value--; }
49  const StatisticBase &operator+=(const unsigned &V) {Value += V;return init();}
50  const StatisticBase &operator-=(const unsigned &V) {Value -= V;return init();}
51  const StatisticBase &operator*=(const unsigned &V) {Value *= V;return init();}
52  const StatisticBase &operator/=(const unsigned &V) {Value /= V;return init();}
53
54protected:
55  StatisticBase &init() {
56    if (!Initialized) RegisterStatistic();
57    return *this;
58  }
59  void RegisterStatistic();
60};
61
62struct Statistic : public StatisticBase {
63  Statistic(const char *name, const char *desc) {
64    Name = name; Desc = desc; Value = 0; Initialized = 0;
65  }
66
67  // Allow use of this class as the value itself.
68  operator unsigned() const { return Value; }
69  const Statistic &operator=(unsigned Val) { Value = Val; init(); return *this;}
70  const Statistic &operator++() { ++Value; init(); return *this;}
71  unsigned operator++(int) { init(); return Value++; }
72  const Statistic &operator--() { --Value; init(); return *this;}
73  unsigned operator--(int) { init(); return Value--; }
74  const Statistic &operator+=(const unsigned &V) {Value += V;init();return *this;}
75  const Statistic &operator-=(const unsigned &V) {Value -= V;init();return *this;}
76  const Statistic &operator*=(const unsigned &V) {Value *= V;init();return *this;}
77  const Statistic &operator/=(const unsigned &V) {Value /= V;init();return *this;}
78};
79
80
81// STATISTIC - A macro to make definition of statistics really simple.  This
82// automatically passes the DEBUG_TYPE of the file into the statistic.
83#define STATISTIC(VARNAME, DESC) \
84  static StatisticBase VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
85
86} // End llvm namespace
87
88#endif
89