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 "llvm/ADT/Statistic.h"
25 #include "llvm/Support/CommandLine.h"
31 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
32 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
34 unsigned StatisticBase::NumStats = 0;
36 // -stats - Command line option to cause transformations to emit stats about
40 Enabled("stats", cl::desc("Enable statistics output from program"));
44 const char *Name, *Desc;
46 StatRecord(const std::string &V, const char *N, const char *D)
47 : Value(V), Name(N), Desc(D) {}
49 bool operator<(const StatRecord &SR) const {
50 return std::strcmp(Name, SR.Name) < 0;
53 void print(unsigned ValFieldSize, unsigned NameFieldSize,
55 OS << std::string(ValFieldSize-Value.length(), ' ')
56 << Value << " " << Name
57 << std::string(NameFieldSize-std::strlen(Name), ' ')
58 << " - " << Desc << "\n";
62 static std::vector<StatRecord> *AccumStats = 0;
64 // Print information when destroyed, iff command line option is specified
65 void StatisticBase::destroy() const {
66 if (Enabled && hasSomeData()) {
68 AccumStats = new std::vector<StatRecord>();
70 std::ostringstream Out;
72 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
75 if (--NumStats == 0 && AccumStats) {
76 std::ostream *OutStream = GetLibSupportInfoOutputFile();
78 // Figure out how long the biggest Value and Name fields are...
79 unsigned MaxNameLen = 0, MaxValLen = 0;
80 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
81 MaxValLen = std::max(MaxValLen,
82 (unsigned)(*AccumStats)[i].Value.length());
83 MaxNameLen = std::max(MaxNameLen,
84 (unsigned)std::strlen((*AccumStats)[i].Name));
88 std::stable_sort(AccumStats->begin(), AccumStats->end());
90 // Print out the statistics header...
91 *OutStream << "===" << std::string(73, '-') << "===\n"
92 << " ... Statistics Collected ...\n"
93 << "===" << std::string(73, '-') << "===\n\n";
95 // Print all of the statistics accumulated...
96 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
97 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
99 *OutStream << std::endl; // Flush the output stream...
101 // Free all accumulated statistics...
104 if (OutStream != &std::cerr && OutStream != &std::cout)
105 delete OutStream; // Close the file...