LLVM API Documentation

Statistic.h

Go to the documentation of this file.
00001 //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the 'Statistic' class, which is designed to be an easy way
00011 // to expose various success metrics from passes.  These statistics are printed
00012 // at the end of a run, when the -stats command line option is enabled on the
00013 // command line.
00014 //
00015 // This is useful for reporting information like the number of instructions
00016 // simplified, optimized or removed by various transformations, like this:
00017 //
00018 // static Statistic<> NumInstsKilled("gcse", "Number of instructions killed");
00019 //
00020 // Later, in the code: ++NumInstsKilled;
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #ifndef LLVM_ADT_STATISTIC_H
00025 #define LLVM_ADT_STATISTIC_H
00026 
00027 #include <iosfwd>
00028 
00029 namespace llvm {
00030 
00031 // StatisticBase - Nontemplated base class for Statistic<> class...
00032 class StatisticBase {
00033   const char *Name;
00034   const char *Desc;
00035   static unsigned NumStats;
00036 protected:
00037   StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) {
00038     ++NumStats;  // Keep track of how many stats are created...
00039   }
00040   // Out of line virtual dtor, to give the vtable etc a home.
00041   virtual ~StatisticBase();
00042 
00043   // destroy - Called by subclass dtor so that we can still invoke virtual
00044   // functions on the subclass.
00045   void destroy() const;
00046 
00047   // printValue - Overridden by template class to print out the value type...
00048   virtual void printValue(std::ostream &o) const = 0;
00049 
00050   // hasSomeData - Return true if some data has been aquired.  Avoid printing
00051   // lots of zero counts.
00052   //
00053   virtual bool hasSomeData() const = 0;
00054 };
00055 
00056 // Statistic Class - templated on the data type we are monitoring...
00057 template <typename DataType=unsigned>
00058 class Statistic : private StatisticBase {
00059   DataType Value;
00060 
00061   virtual void printValue(std::ostream &o) const { o << Value; }
00062   virtual bool hasSomeData() const { return Value != DataType(); }
00063 public:
00064   // Normal constructor, default initialize data item...
00065   Statistic(const char *name, const char *desc)
00066     : StatisticBase(name, desc), Value(DataType()) {}
00067 
00068   // Constructor to provide an initial value...
00069   Statistic(const DataType &Val, const char *name, const char *desc)
00070     : StatisticBase(name, desc), Value(Val) {}
00071 
00072   // Print information when destroyed, iff command line option is specified
00073   ~Statistic() { destroy(); }
00074 
00075   // Allow use of this class as the value itself...
00076   operator DataType() const { return Value; }
00077   const Statistic &operator=(DataType Val) { Value = Val; return *this; }
00078   const Statistic &operator++() { ++Value; return *this; }
00079   DataType operator++(int) { return Value++; }
00080   const Statistic &operator--() { --Value; return *this; }
00081   DataType operator--(int) { return Value--; }
00082   const Statistic &operator+=(const DataType &V) { Value += V; return *this; }
00083   const Statistic &operator-=(const DataType &V) { Value -= V; return *this; }
00084   const Statistic &operator*=(const DataType &V) { Value *= V; return *this; }
00085   const Statistic &operator/=(const DataType &V) { Value /= V; return *this; }
00086 };
00087 
00088 } // End llvm namespace
00089 
00090 #endif