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 is distributed under the University of Illinois Open Source
00006 // 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 metrics from passes.  These statistics are printed at the
00012 // end of a run (from llvm_shutdown), when the -stats command line option is
00013 // passed on the 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 // NOTE: Statistics *must* be declared as global variables.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #ifndef LLVM_ADT_STATISTIC_H
00027 #define LLVM_ADT_STATISTIC_H
00028 
00029 #include "llvm/System/Atomic.h"
00030 
00031 namespace llvm {
00032 
00033 class Statistic {
00034 public:
00035   const char *Name;
00036   const char *Desc;
00037   volatile llvm::sys::cas_flag Value;
00038   bool Initialized;
00039 
00040   llvm::sys::cas_flag getValue() const { return Value; }
00041   const char *getName() const { return Name; }
00042   const char *getDesc() const { return Desc; }
00043 
00044   /// construct - This should only be called for non-global statistics.
00045   void construct(const char *name, const char *desc) {
00046     Name = name; Desc = desc;
00047     Value = 0; Initialized = 0;
00048   }
00049 
00050   // Allow use of this class as the value itself.
00051   operator unsigned() const { return Value; }
00052   const Statistic &operator=(unsigned Val) {
00053     Value = Val;
00054     return init();
00055   }
00056   
00057   const Statistic &operator++() {
00058     sys::AtomicIncrement(&Value);
00059     return init();
00060   }
00061   
00062   unsigned operator++(int) {
00063     init();
00064     unsigned OldValue = Value;
00065     sys::AtomicIncrement(&Value);
00066     return OldValue;
00067   }
00068   
00069   const Statistic &operator--() {
00070     sys::AtomicDecrement(&Value);
00071     return init();
00072   }
00073   
00074   unsigned operator--(int) {
00075     init();
00076     unsigned OldValue = Value;
00077     sys::AtomicDecrement(&Value);
00078     return OldValue;
00079   }
00080   
00081   const Statistic &operator+=(const unsigned &V) {
00082     sys::AtomicAdd(&Value, V);
00083     return init();
00084   }
00085   
00086   const Statistic &operator-=(const unsigned &V) {
00087     sys::AtomicAdd(&Value, -V);
00088     return init();
00089   }
00090   
00091   const Statistic &operator*=(const unsigned &V) {
00092     sys::AtomicMul(&Value, V);
00093     return init();
00094   }
00095   
00096   const Statistic &operator/=(const unsigned &V) {
00097     sys::AtomicDiv(&Value, V);
00098     return init();
00099   }
00100 
00101 protected:
00102   Statistic &init() {
00103     bool tmp = Initialized;
00104     sys::MemoryFence();
00105     if (!tmp) RegisterStatistic();
00106     return *this;
00107   }
00108   void RegisterStatistic();
00109 };
00110 
00111 // STATISTIC - A macro to make definition of statistics really simple.  This
00112 // automatically passes the DEBUG_TYPE of the file into the statistic.
00113 #define STATISTIC(VARNAME, DESC) \
00114   static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
00115 
00116 } // End llvm namespace
00117 
00118 #endif