Don't use <sstream> in Streams.h but <iosfwd> instead.
[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 "llvm/Support/Streams.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include <algorithm>
29 #include <ostream>
30 using namespace llvm;
31
32 // GetLibSupportInfoOutputFile - Return a file stream to print our output on...
33 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
34
35 unsigned Statistic::NumStats = 0;
36
37 // -stats - Command line option to cause transformations to emit stats about
38 // what they did.
39 //
40 static cl::opt<bool>
41 Enabled("stats", cl::desc("Enable statistics output from program"));
42
43 struct StatRecord {
44   std::string Value;
45   const char *Name, *Desc;
46
47   StatRecord(const std::string &V, const char *N, const char *D)
48     : Value(V), Name(N), Desc(D) {}
49
50   bool operator<(const StatRecord &SR) const {
51     return std::strcmp(Name, SR.Name) < 0;
52   }
53
54   void print(unsigned ValFieldSize, unsigned NameFieldSize,
55              std::ostream &OS) {
56     OS << std::string(ValFieldSize-Value.length(), ' ')
57        << Value << " " << Name
58        << std::string(NameFieldSize-std::strlen(Name), ' ')
59        << " - " << Desc << "\n";
60   }
61 };
62
63 static std::vector<StatRecord> *AccumStats = 0;
64
65 // Print information when destroyed, iff command line option is specified
66 Statistic::~Statistic() {
67   if (Enabled && Value != 0) {
68     if (AccumStats == 0)
69       AccumStats = new std::vector<StatRecord>();
70
71     AccumStats->push_back(StatRecord(utostr(Value), Name, Desc));
72   }
73
74   if (--NumStats == 0 && AccumStats) {
75     std::ostream *OutStream = GetLibSupportInfoOutputFile();
76
77     // Figure out how long the biggest Value and Name fields are...
78     unsigned MaxNameLen = 0, MaxValLen = 0;
79     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
80       MaxValLen = std::max(MaxValLen,
81                            (unsigned)(*AccumStats)[i].Value.length());
82       MaxNameLen = std::max(MaxNameLen,
83                             (unsigned)std::strlen((*AccumStats)[i].Name));
84     }
85
86     // Sort the fields...
87     std::stable_sort(AccumStats->begin(), AccumStats->end());
88
89     // Print out the statistics header...
90     *OutStream << "===" << std::string(73, '-') << "===\n"
91                << "                          ... Statistics Collected ...\n"
92                << "===" << std::string(73, '-') << "===\n\n";
93
94     // Print all of the statistics accumulated...
95     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
96       (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
97
98     *OutStream << std::endl;  // Flush the output stream...
99
100     // Free all accumulated statistics...
101     delete AccumStats;
102     AccumStats = 0;
103     if (OutStream != cerr.stream() && OutStream != cout.stream())
104       delete OutStream;   // Close the file...
105   }
106 }