OSDN Git Service

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