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   virtual ~StatisticBase() {}
00041 
00042   // destroy - Called by subclass dtor so that we can still invoke virtual
00043   // functions on the subclass.
00044   void destroy() const;
00045 
00046   // printValue - Overridden by template class to print out the value type...
00047   virtual void printValue(std::ostream &o) const = 0;
00048 
00049   // hasSomeData - Return true if some data has been aquired.  Avoid printing
00050   // lots of zero counts.
00051   //
00052   virtual bool hasSomeData() const = 0;
00053 };
00054 
00055 // Statistic Class - templated on the data type we are monitoring...
00056 template <typename DataType=unsigned>
00057 class Statistic : private StatisticBase {
00058   DataType Value;
00059 
00060   virtual void printValue(std::ostream &o) const { o << Value; }
00061   virtual bool hasSomeData() const { return Value != DataType(); }
00062 public:
00063   // Normal constructor, default initialize data item...
00064   Statistic(const char *name, const char *desc)
00065     : StatisticBase(name, desc), Value(DataType()) {}
00066 
00067   // Constructor to provide an initial value...
00068   Statistic(const DataType &Val, const char *name, const char *desc)
00069     : StatisticBase(name, desc), Value(Val) {}
00070 
00071   // Print information when destroyed, iff command line option is specified
00072   ~Statistic() { destroy(); }
00073 
00074   // Allow use of this class as the value itself...
00075   operator DataType() const { return Value; }
00076   const Statistic &operator=(DataType Val) { Value = Val; return *this; }
00077   const Statistic &operator++() { ++Value; return *this; }
00078   DataType operator++(int) { return Value++; }
00079   const Statistic &operator--() { --Value; return *this; }
00080   DataType operator--(int) { return Value--; }
00081   const Statistic &operator+=(const DataType &V) { Value += V; return *this; }
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 };
00086 
00087 } // End llvm namespace
00088 
00089 #endif