LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Statistic.cpp

Go to the documentation of this file.
00001 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
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 implements the 'Statistic' class, which is designed to be an easy
00011 // way to expose various success metrics from passes.  These statistics are
00012 // printed at the end of a run, when the -stats command line option is enabled
00013 // 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<> NumInstEliminated("GCSE - Number of instructions killed");
00019 //
00020 // Later, in the code: ++NumInstEliminated;
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #include "llvm/ADT/Statistic.h"
00025 #include "llvm/Support/CommandLine.h"
00026 #include <sstream>
00027 #include <iostream>
00028 #include <algorithm>
00029 using namespace llvm;
00030 
00031 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
00032 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
00033 
00034 unsigned StatisticBase::NumStats = 0;
00035 
00036 // -stats - Command line option to cause transformations to emit stats about
00037 // what they did.
00038 //
00039 static cl::opt<bool>
00040 Enabled("stats", cl::desc("Enable statistics output from program"));
00041 
00042 struct StatRecord {
00043   std::string Value;
00044   const char *Name, *Desc;
00045 
00046   StatRecord(const std::string &V, const char *N, const char *D)
00047     : Value(V), Name(N), Desc(D) {}
00048 
00049   bool operator<(const StatRecord &SR) const {
00050     return std::strcmp(Name, SR.Name) < 0;
00051   }
00052 
00053   void print(unsigned ValFieldSize, unsigned NameFieldSize,
00054              std::ostream &OS) {
00055     OS << std::string(ValFieldSize-Value.length(), ' ')
00056        << Value << " " << Name
00057        << std::string(NameFieldSize-std::strlen(Name), ' ')
00058        << " - " << Desc << "\n";
00059   }
00060 };
00061 
00062 static std::vector<StatRecord> *AccumStats = 0;
00063 
00064 // Print information when destroyed, iff command line option is specified
00065 void StatisticBase::destroy() const {
00066   if (Enabled && hasSomeData()) {
00067     if (AccumStats == 0)
00068       AccumStats = new std::vector<StatRecord>();
00069 
00070     std::ostringstream Out;
00071     printValue(Out);
00072     AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
00073   }
00074 
00075   if (--NumStats == 0 && AccumStats) {
00076     std::ostream *OutStream = GetLibSupportInfoOutputFile();
00077 
00078     // Figure out how long the biggest Value and Name fields are...
00079     unsigned MaxNameLen = 0, MaxValLen = 0;
00080     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
00081       MaxValLen = std::max(MaxValLen, 
00082                            (unsigned)(*AccumStats)[i].Value.length());
00083       MaxNameLen = std::max(MaxNameLen, 
00084                             (unsigned)std::strlen((*AccumStats)[i].Name));
00085     }
00086 
00087     // Sort the fields...
00088     std::stable_sort(AccumStats->begin(), AccumStats->end());
00089 
00090     // Print out the statistics header...
00091     *OutStream << "===" << std::string(73, '-') << "===\n"
00092                << "                          ... Statistics Collected ...\n"
00093                << "===" << std::string(73, '-') << "===\n\n";
00094 
00095     // Print all of the statistics accumulated...
00096     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
00097       (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
00098 
00099     *OutStream << std::endl;  // Flush the output stream...
00100 
00101     // Free all accumulated statistics...
00102     delete AccumStats;
00103     AccumStats = 0;
00104     if (OutStream != &std::cerr && OutStream != &std::cout)
00105       delete OutStream;   // Close the file...
00106   }
00107 }