1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
3 // This file implements the 'Statistic' class, which is designed to be an easy
4 // way to expose various success metrics from passes. These statistics are
5 // printed at the end of a run, when the -stats command line option is enabled
6 // on the command line.
8 // This is useful for reporting information like the number of instructions
9 // simplified, optimized or removed by various transformations, like this:
11 // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
13 // Later, in the code: ++NumInstEliminated;
15 //===----------------------------------------------------------------------===//
17 #include "Support/Statistic.h"
18 #include "Support/CommandLine.h"
23 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
24 std::ostream *GetLibSupportInfoOutputFile();
26 bool DebugFlag; // DebugFlag - Exported boolean set by the -debug option
28 unsigned StatisticBase::NumStats = 0;
30 // -stats - Command line option to cause transformations to emit stats about
34 Enabled("stats", cl::desc("Enable statistics output from program"));
37 // -debug - Command line option to enable the DEBUG statements in the passes.
38 // This flag may only be enabled in debug builds.
39 static cl::opt<bool, true>
40 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
41 cl::location(DebugFlag));
46 const char *Name, *Desc;
48 StatRecord(const std::string V, const char *N, const char *D)
49 : Value(V), Name(N), Desc(D) {}
51 bool operator<(const StatRecord &SR) const {
52 return std::strcmp(Name, SR.Name) < 0;
55 void print(unsigned ValFieldSize, unsigned NameFieldSize,
57 OS << std::string(ValFieldSize-Value.length(), ' ')
58 << Value << " " << Name
59 << std::string(NameFieldSize-std::strlen(Name), ' ')
60 << " - " << Desc << "\n";
64 static std::vector<StatRecord> *AccumStats = 0;
66 // Print information when destroyed, iff command line option is specified
67 void StatisticBase::destroy() const {
68 if (Enabled && hasSomeData()) {
70 AccumStats = new std::vector<StatRecord>();
72 std::ostringstream Out;
74 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
77 if (--NumStats == 0 && AccumStats) {
78 std::ostream *OutStream = GetLibSupportInfoOutputFile();
80 // Figure out how long the biggest Value and Name fields are...
81 unsigned MaxNameLen = 0, MaxValLen = 0;
82 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
83 MaxValLen = std::max(MaxValLen,
84 (unsigned)(*AccumStats)[i].Value.length());
85 MaxNameLen = std::max(MaxNameLen,
86 (unsigned)std::strlen((*AccumStats)[i].Name));
90 std::stable_sort(AccumStats->begin(), AccumStats->end());
92 // Print out the statistics header...
93 *OutStream << "===" << std::string(73, '-') << "===\n"
94 << " ... Statistics Collected ...\n"
95 << "===" << std::string(73, '-') << "===\n\n";
97 // Print all of the statistics accumulated...
98 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
99 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
101 *OutStream << std::endl; // Flush the output stream...
103 // Free all accumulated statistics...
106 if (OutStream != &std::cerr && OutStream != &std::cout)
107 delete OutStream; // Close the file...