Statistic.h revision 975f05852d15c98540b50de7df704d67e5a794cd
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 Statistic { 32 const char *Name; 33 const char *Desc; 34 unsigned Value : 31; 35 bool Initialized : 1; 36public: 37 // Normal constructor, default initialize data item... 38 Statistic(const char *name, const char *desc) 39 : Name(name), Desc(desc), Value(0), Initialized(0) { 40 } 41 42 unsigned getValue() const { return Value; } 43 const char *getName() const { return Name; } 44 const char *getDesc() const { return Desc; } 45 46 // Allow use of this class as the value itself. 47 operator unsigned() const { return Value; } 48 const Statistic &operator=(unsigned Val) { Value = Val; return init(); } 49 const Statistic &operator++() { ++Value; return init(); } 50 unsigned operator++(int) { init(); return Value++; } 51 const Statistic &operator--() { --Value; return init(); } 52 unsigned operator--(int) { init(); return Value--; } 53 const Statistic &operator+=(const unsigned &V) { Value += V; return init(); } 54 const Statistic &operator-=(const unsigned &V) { Value -= V; return init(); } 55 const Statistic &operator*=(const unsigned &V) { Value *= V; return init(); } 56 const Statistic &operator/=(const unsigned &V) { Value /= V; return init(); } 57 58private: 59 Statistic &init() { 60 if (!Initialized) RegisterStatistic(); 61 return *this; 62 } 63 void RegisterStatistic(); 64}; 65 66} // End llvm namespace 67 68#endif 69