Statistic.h revision b2109ce97881269a610fa4afbcbca350e975174d
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===-- Support/Statistic.h - Easy way to expose stats ----------*- C++ -*-===// 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The LLVM Compiler Infrastructure 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This file was developed by the LLVM research group and is distributed under 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the University of Illinois Open Source License. See LICENSE.TXT for details. 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file defines the 'Statistic' class, which is designed to be an easy way 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to expose various success metrics from passes. These statistics are printed 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// at the end of a run, when the -stats command line option is enabled on the 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// command line. 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This is useful for reporting information like the number of instructions 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// simplified, optimized or removed by various transformations, like this: 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// static Statistic<> NumInstEliminated("GCSE - Number of instructions killed"); 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Later, in the code: ++NumInstEliminated; 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===// 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef SUPPORT_STATISTIC_H 25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define SUPPORT_STATISTIC_H 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <iosfwd> 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 29// StatisticBase - Nontemplated base class for Statistic<> class... 30class StatisticBase { 31 const char *Name; 32 const char *Desc; 33 static unsigned NumStats; 34protected: 35 StatisticBase(const char *name, const char *desc) : Name(name), Desc(desc) { 36 ++NumStats; // Keep track of how many stats are created... 37 } 38 virtual ~StatisticBase() {} 39 40 // destroy - Called by subclass dtor so that we can still invoke virtual 41 // functions on the subclass. 42 void destroy() const; 43 44 // printValue - Overridden by template class to print out the value type... 45 virtual void printValue(std::ostream &o) const = 0; 46 47 // hasSomeData - Return true if some data has been aquired. Avoid printing 48 // lots of zero counts. 49 // 50 virtual bool hasSomeData() const = 0; 51}; 52 53// Statistic Class - templated on the data type we are monitoring... 54template <typename DataType=unsigned> 55class Statistic : private StatisticBase { 56 DataType Value; 57 58 virtual void printValue(std::ostream &o) const { o << Value; } 59 virtual bool hasSomeData() const { return Value != DataType(); } 60public: 61 // Normal constructor, default initialize data item... 62 Statistic(const char *name, const char *desc) 63 : StatisticBase(name, desc), Value(DataType()) {} 64 65 // Constructor to provide an initial value... 66 Statistic(const DataType &Val, const char *name, const char *desc) 67 : StatisticBase(name, desc), Value(Val) {} 68 69 // Print information when destroyed, iff command line option is specified 70 ~Statistic() { destroy(); } 71 72 // Allow use of this class as the value itself... 73 operator DataType() const { return Value; } 74 const Statistic &operator=(DataType Val) { Value = Val; return *this; } 75 const Statistic &operator++() { ++Value; return *this; } 76 DataType operator++(int) { return Value++; } 77 const Statistic &operator+=(const DataType &V) { Value += V; return *this; } 78 const Statistic &operator-=(const DataType &V) { Value -= V; return *this; } 79}; 80 81#endif 82