Instantiate Statistic<> in one place, not in every .o file that uses it.
[oota-llvm.git] / lib / Support / Statistic.cpp
1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
17 //
18 // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
19 //
20 // Later, in the code: ++NumInstEliminated;
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/CommandLine.h"
26 #include <sstream>
27 #include <iostream>
28 #include <algorithm>
29 using namespace llvm;
30
31 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
32 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
33
34 unsigned StatisticBase::NumStats = 0;
35
36 TEMPLATE_INSTANTIATION(class Statistic<unsigned>);
37
38 // -stats - Command line option to cause transformations to emit stats about
39 // what they did.
40 //
41 static cl::opt<bool>
42 Enabled("stats", cl::desc("Enable statistics output from program"));
43
44 struct StatRecord {
45   std::string Value;
46   const char *Name, *Desc;
47
48   StatRecord(const std::string &V, const char *N, const char *D)
49     : Value(V), Name(N), Desc(D) {}
50
51   bool operator<(const StatRecord &SR) const {
52     return std::strcmp(Name, SR.Name) < 0;
53   }
54
55   void print(unsigned ValFieldSize, unsigned NameFieldSize,
56              std::ostream &OS) {
57     OS << std::string(ValFieldSize-Value.length(), ' ')
58        << Value << " " << Name
59        << std::string(NameFieldSize-std::strlen(Name), ' ')
60        << " - " << Desc << "\n";
61   }
62 };
63
64 static std::vector<StatRecord> *AccumStats = 0;
65
66 // Out of line virtual dtor, to give the vtable etc a home.
67 StatisticBase::~StatisticBase() {
68 }
69
70 // Print information when destroyed, iff command line option is specified
71 void StatisticBase::destroy() const {
72   if (Enabled && hasSomeData()) {
73     if (AccumStats == 0)
74       AccumStats = new std::vector<StatRecord>();
75
76     std::ostringstream Out;
77     printValue(Out);
78     AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
79   }
80
81   if (--NumStats == 0 && AccumStats) {
82     std::ostream *OutStream = GetLibSupportInfoOutputFile();
83
84     // Figure out how long the biggest Value and Name fields are...
85     unsigned MaxNameLen = 0, MaxValLen = 0;
86     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
87       MaxValLen = std::max(MaxValLen,
88                            (unsigned)(*AccumStats)[i].Value.length());
89       MaxNameLen = std::max(MaxNameLen,
90                             (unsigned)std::strlen((*AccumStats)[i].Name));
91     }
92
93     // Sort the fields...
94     std::stable_sort(AccumStats->begin(), AccumStats->end());
95
96     // Print out the statistics header...
97     *OutStream << "===" << std::string(73, '-') << "===\n"
98                << "                          ... Statistics Collected ...\n"
99                << "===" << std::string(73, '-') << "===\n\n";
100
101     // Print all of the statistics accumulated...
102     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
103       (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
104
105     *OutStream << std::endl;  // Flush the output stream...
106
107     // Free all accumulated statistics...
108     delete AccumStats;
109     AccumStats = 0;
110     if (OutStream != &std::cerr && OutStream != &std::cout)
111       delete OutStream;   // Close the file...
112   }
113 }