Introduce namespace-scope functions to enable LLVM statistics without
[oota-llvm.git] / include / llvm / ADT / Statistic.h
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/System/Atomic.h"
30
31 namespace llvm {
32 class raw_ostream;
33
34 class Statistic {
35 public:
36   const char *Name;
37   const char *Desc;
38   volatile llvm::sys::cas_flag Value;
39   bool Initialized;
40
41   llvm::sys::cas_flag getValue() const { return Value; }
42   const char *getName() const { return Name; }
43   const char *getDesc() const { return Desc; }
44
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;
49   }
50
51   // Allow use of this class as the value itself.
52   operator unsigned() const { return Value; }
53   const Statistic &operator=(unsigned Val) {
54     Value = Val;
55     return init();
56   }
57   
58   const Statistic &operator++() {
59     sys::AtomicIncrement(&Value);
60     return init();
61   }
62   
63   unsigned operator++(int) {
64     init();
65     unsigned OldValue = Value;
66     sys::AtomicIncrement(&Value);
67     return OldValue;
68   }
69   
70   const Statistic &operator--() {
71     sys::AtomicDecrement(&Value);
72     return init();
73   }
74   
75   unsigned operator--(int) {
76     init();
77     unsigned OldValue = Value;
78     sys::AtomicDecrement(&Value);
79     return OldValue;
80   }
81   
82   const Statistic &operator+=(const unsigned &V) {
83     sys::AtomicAdd(&Value, V);
84     return init();
85   }
86   
87   const Statistic &operator-=(const unsigned &V) {
88     sys::AtomicAdd(&Value, -V);
89     return init();
90   }
91   
92   const Statistic &operator*=(const unsigned &V) {
93     sys::AtomicMul(&Value, V);
94     return init();
95   }
96   
97   const Statistic &operator/=(const unsigned &V) {
98     sys::AtomicDiv(&Value, V);
99     return init();
100   }
101
102 protected:
103   Statistic &init() {
104     bool tmp = Initialized;
105     sys::MemoryFence();
106     if (!tmp) RegisterStatistic();
107     return *this;
108   }
109   void RegisterStatistic();
110 };
111
112 // STATISTIC - A macro to make definition of statistics really simple.  This
113 // automatically passes the DEBUG_TYPE of the file into the statistic.
114 #define STATISTIC(VARNAME, DESC) \
115   static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
116
117 /// \brief Enable the collection and printing of statistics.
118 void EnableStatistics();
119
120 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
121 void PrintStatistics();
122
123 /// \brief Print statistics to the given output stream.
124 void PrintStatistics(raw_ostream &OS);
125
126 } // End llvm namespace
127
128 #endif