1 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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
38 unsigned getValue() const { return Value; }
39 const char *getName() const { return Name; }
40 const char *getDesc() const { return Desc; }
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();}
55 StatisticBase &init() {
56 if (!Initialized) RegisterStatistic();
59 void RegisterStatistic();
62 struct Statistic : public StatisticBase {
63 Statistic(const char *name, const char *desc) {
64 Name = name; Desc = desc; Value = 0; Initialized = 0;
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;}
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 }
86 } // End llvm namespace