blob: 5ad569899ec895a9484417b99422ed9640ff11e9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/ManagedStatic.h"
Chris Lattner5febcae2009-08-23 08:43:55 +000027#include "llvm/Support/raw_ostream.h"
Owen Andersonb23c1be2009-06-22 23:08:27 +000028#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/ADT/StringExtras.h"
30#include <algorithm>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000031#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032using namespace llvm;
33
34// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5febcae2009-08-23 08:43:55 +000035namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036
37/// -stats - Command line option to cause transformations to emit stats about
38/// what they did.
39///
40static cl::opt<bool>
41Enabled("stats", cl::desc("Enable statistics output from program"));
42
43
44namespace {
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.
48class StatisticInfo {
49 std::vector<const Statistic*> Stats;
50public:
51 ~StatisticInfo();
52
53 void addStatistic(const Statistic *S) {
54 Stats.push_back(S);
55 }
56};
57}
58
59static ManagedStatic<StatisticInfo> StatInfo;
Owen Andersonb23c1be2009-06-22 23:08:27 +000060static ManagedStatic<sys::Mutex> StatLock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62/// RegisterStatistic - The first time a statistic is bumped, this method is
63/// called.
64void Statistic::RegisterStatistic() {
65 // If stats are enabled, inform StatInfo that this statistic should be
66 // printed.
Owen Andersonbe44bed2009-07-07 18:33:04 +000067 sys::ScopedLock Writer(*StatLock);
Owen Andersona81a9e72009-06-23 21:19:38 +000068 if (!Initialized) {
69 if (Enabled)
70 StatInfo->addStatistic(this);
71
72 sys::MemoryFence();
73 // Remember we have been registered.
74 Initialized = true;
75 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
Dan Gohman089efff2008-05-13 00:00:25 +000078namespace {
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080struct NameCompare {
81 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
82 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
83 if (Cmp != 0) return Cmp < 0;
84
85 // Secondary key is the description.
86 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
87 }
88};
89
Dan Gohman089efff2008-05-13 00:00:25 +000090}
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092// Print information when destroyed, iff command line option is specified.
93StatisticInfo::~StatisticInfo() {
94 // Statistics not enabled?
95 if (Stats.empty()) return;
96
97 // Get the stream to write to.
Chris Lattner5febcae2009-08-23 08:43:55 +000098 raw_ostream &OutStream = *GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 // Figure out how long the biggest Value and Name fields are.
101 unsigned MaxNameLen = 0, MaxValLen = 0;
Evan Cheng591bfc82008-05-05 18:30:58 +0000102 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 MaxValLen = std::max(MaxValLen,
104 (unsigned)utostr(Stats[i]->getValue()).size());
105 MaxNameLen = std::max(MaxNameLen,
106 (unsigned)std::strlen(Stats[i]->getName()));
107 }
108
109 // Sort the fields by name.
110 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
111
112 // Print out the statistics header...
113 OutStream << "===" << std::string(73, '-') << "===\n"
114 << " ... Statistics Collected ...\n"
115 << "===" << std::string(73, '-') << "===\n\n";
116
117 // Print all of the statistics.
Evan Cheng591bfc82008-05-05 18:30:58 +0000118 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 std::string CountStr = utostr(Stats[i]->getValue());
120 OutStream << std::string(MaxValLen-CountStr.size(), ' ')
121 << CountStr << " " << Stats[i]->getName()
122 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ')
123 << " - " << Stats[i]->getDesc() << "\n";
124
125 }
126
Chris Lattner5febcae2009-08-23 08:43:55 +0000127 OutStream << '\n'; // Flush the output stream...
128 OutStream.flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
Chris Lattner5febcae2009-08-23 08:43:55 +0000130 if (&OutStream != &outs() && &OutStream != &errs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 delete &OutStream; // Close the file.
132}