Implement bswap
[llvm/msp430.git] / lib / Support / Statistic.cpp
blob13acc1b0fa1e541086c40724be001615ee5d0b4d
1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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.
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"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include <algorithm>
30 #include <ostream>
31 #include <cstring>
32 using namespace llvm;
34 // GetLibSupportInfoOutputFile - Return a file stream to print our output on.
35 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
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"));
44 namespace {
45 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
46 /// on demand (when the first statistic is bumped) and destroyed only when
47 /// llvm_shutdown is called. We print statistics from the destructor.
48 class StatisticInfo {
49 std::vector<const Statistic*> Stats;
50 public:
51 ~StatisticInfo();
53 void addStatistic(const Statistic *S) {
54 Stats.push_back(S);
59 static ManagedStatic<StatisticInfo> StatInfo;
62 /// RegisterStatistic - The first time a statistic is bumped, this method is
63 /// called.
64 void Statistic::RegisterStatistic() {
65 // If stats are enabled, inform StatInfo that this statistic should be
66 // printed.
67 if (Enabled)
68 StatInfo->addStatistic(this);
69 // Remember we have been registered.
70 Initialized = true;
73 namespace {
75 struct NameCompare {
76 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
77 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
78 if (Cmp != 0) return Cmp < 0;
80 // Secondary key is the description.
81 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
87 // Print information when destroyed, iff command line option is specified.
88 StatisticInfo::~StatisticInfo() {
89 // Statistics not enabled?
90 if (Stats.empty()) return;
92 // Get the stream to write to.
93 std::ostream &OutStream = *GetLibSupportInfoOutputFile();
95 // Figure out how long the biggest Value and Name fields are.
96 unsigned MaxNameLen = 0, MaxValLen = 0;
97 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
98 MaxValLen = std::max(MaxValLen,
99 (unsigned)utostr(Stats[i]->getValue()).size());
100 MaxNameLen = std::max(MaxNameLen,
101 (unsigned)std::strlen(Stats[i]->getName()));
104 // Sort the fields by name.
105 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
107 // Print out the statistics header...
108 OutStream << "===" << std::string(73, '-') << "===\n"
109 << " ... Statistics Collected ...\n"
110 << "===" << std::string(73, '-') << "===\n\n";
112 // Print all of the statistics.
113 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
114 std::string CountStr = utostr(Stats[i]->getValue());
115 OutStream << std::string(MaxValLen-CountStr.size(), ' ')
116 << CountStr << " " << Stats[i]->getName()
117 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ')
118 << " - " << Stats[i]->getDesc() << "\n";
122 OutStream << std::endl; // Flush the output stream...
124 if (&OutStream != cerr.stream() && &OutStream != cout.stream())
125 delete &OutStream; // Close the file.