LLVM API Documentation
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 // Out of line virtual dtor, to give the vtable etc a home. 00065 StatisticBase::~StatisticBase() { 00066 } 00067 00068 // Print information when destroyed, iff command line option is specified 00069 void StatisticBase::destroy() const { 00070 if (Enabled && hasSomeData()) { 00071 if (AccumStats == 0) 00072 AccumStats = new std::vector<StatRecord>(); 00073 00074 std::ostringstream Out; 00075 printValue(Out); 00076 AccumStats->push_back(StatRecord(Out.str(), Name, Desc)); 00077 } 00078 00079 if (--NumStats == 0 && AccumStats) { 00080 std::ostream *OutStream = GetLibSupportInfoOutputFile(); 00081 00082 // Figure out how long the biggest Value and Name fields are... 00083 unsigned MaxNameLen = 0, MaxValLen = 0; 00084 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) { 00085 MaxValLen = std::max(MaxValLen, 00086 (unsigned)(*AccumStats)[i].Value.length()); 00087 MaxNameLen = std::max(MaxNameLen, 00088 (unsigned)std::strlen((*AccumStats)[i].Name)); 00089 } 00090 00091 // Sort the fields... 00092 std::stable_sort(AccumStats->begin(), AccumStats->end()); 00093 00094 // Print out the statistics header... 00095 *OutStream << "===" << std::string(73, '-') << "===\n" 00096 << " ... Statistics Collected ...\n" 00097 << "===" << std::string(73, '-') << "===\n\n"; 00098 00099 // Print all of the statistics accumulated... 00100 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) 00101 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream); 00102 00103 *OutStream << std::endl; // Flush the output stream... 00104 00105 // Free all accumulated statistics... 00106 delete AccumStats; 00107 AccumStats = 0; 00108 if (OutStream != &std::cerr && OutStream != &std::cout) 00109 delete OutStream; // Close the file... 00110 } 00111 }