1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the 'Statistic' class, which is designed to be an easy
11 // way to expose various success metrics from passes. These statistics are
12 // printed at the end of a run, when the -stats command line option is enabled
13 // on the command line.
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
18 // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
20 // Later, in the code: ++NumInstEliminated;
22 //===----------------------------------------------------------------------===//
24 #include "Support/Statistic.h"
25 #include "Support/CommandLine.h"
32 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
33 extern std::ostream *GetLibSupportInfoOutputFile();
35 unsigned StatisticBase::NumStats = 0;
37 // -stats - Command line option to cause transformations to emit stats about
41 Enabled("stats", cl::desc("Enable statistics output from program"));
45 const char *Name, *Desc;
47 StatRecord(const std::string V, const char *N, const char *D)
48 : Value(V), Name(N), Desc(D) {}
50 bool operator<(const StatRecord &SR) const {
51 return std::strcmp(Name, SR.Name) < 0;
54 void print(unsigned ValFieldSize, unsigned NameFieldSize,
56 OS << std::string(ValFieldSize-Value.length(), ' ')
57 << Value << " " << Name
58 << std::string(NameFieldSize-std::strlen(Name), ' ')
59 << " - " << Desc << "\n";
63 static std::vector<StatRecord> *AccumStats = 0;
65 // Print information when destroyed, iff command line option is specified
66 void StatisticBase::destroy() const {
67 if (Enabled && hasSomeData()) {
69 AccumStats = new std::vector<StatRecord>();
71 std::ostringstream Out;
73 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
76 if (--NumStats == 0 && AccumStats) {
77 std::ostream *OutStream = GetLibSupportInfoOutputFile();
79 // Figure out how long the biggest Value and Name fields are...
80 unsigned MaxNameLen = 0, MaxValLen = 0;
81 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
82 MaxValLen = std::max(MaxValLen,
83 (unsigned)(*AccumStats)[i].Value.length());
84 MaxNameLen = std::max(MaxNameLen,
85 (unsigned)std::strlen((*AccumStats)[i].Name));
89 std::stable_sort(AccumStats->begin(), AccumStats->end());
91 // Print out the statistics header...
92 *OutStream << "===" << std::string(73, '-') << "===\n"
93 << " ... Statistics Collected ...\n"
94 << "===" << std::string(73, '-') << "===\n\n";
96 // Print all of the statistics accumulated...
97 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
98 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
100 *OutStream << std::endl; // Flush the output stream...
102 // Free all accumulated statistics...
105 if (OutStream != &std::cerr && OutStream != &std::cout)
106 delete OutStream; // Close the file...
110 } // End llvm namespace