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"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/ADT/StringExtras.h"
33 // GetLibSupportInfoOutputFile - Return a file stream to print our output on.
34 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
36 /// -stats - Command line option to cause transformations to emit stats about
40 Enabled("stats", cl::desc("Enable statistics output from program"));
44 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
45 /// on demand (when the first statistic is bumped) and destroyed only when
46 /// llvm_shutdown is called. We print statistics from the destructor.
48 std::vector<const Statistic*> Stats;
52 void addStatistic(const Statistic *S) {
58 static ManagedStatic<StatisticInfo> StatInfo;
61 /// RegisterStatistic - The first time a statistic is bumped, this method is
63 void Statistic::RegisterStatistic() {
64 // If stats are enabled, inform StatInfo that this statistic should be
67 StatInfo->addStatistic(this);
68 // Remember we have been registered.
73 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
74 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
75 if (Cmp != 0) return Cmp < 0;
77 // Secondary key is the description.
78 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
82 // Print information when destroyed, iff command line option is specified.
83 StatisticInfo::~StatisticInfo() {
84 // Statistics not enabled?
85 if (Stats.empty()) return;
87 // Get the stream to write to.
88 std::ostream &OutStream = *GetLibSupportInfoOutputFile();
90 // Figure out how long the biggest Value and Name fields are.
91 unsigned MaxNameLen = 0, MaxValLen = 0;
92 for (unsigned i = 0, e = Stats.size(); i != e; ++i) {
93 MaxValLen = std::max(MaxValLen,
94 (unsigned)utostr(Stats[i]->getValue()).size());
95 MaxNameLen = std::max(MaxNameLen,
96 (unsigned)std::strlen(Stats[i]->getName()));
99 // Sort the fields by name.
100 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
102 // Print out the statistics header...
103 OutStream << "===" << std::string(73, '-') << "===\n"
104 << " ... Statistics Collected ...\n"
105 << "===" << std::string(73, '-') << "===\n\n";
107 // Print all of the statistics.
108 for (unsigned i = 0, e = Stats.size(); i != e; ++i) {
109 std::string CountStr = utostr(Stats[i]->getValue());
110 OutStream << std::string(MaxValLen-CountStr.size(), ' ')
111 << CountStr << " " << Stats[i]->getName()
112 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ')
113 << " - " << Stats[i]->getDesc() << "\n";
117 OutStream << std::endl; // Flush the output stream...
119 if (&OutStream != cerr.stream() && &OutStream != cout.stream())
120 delete &OutStream; // Close the file.