blob: 6c652f8d3f162abcba48d61230b068f50df22c1d [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"
27#include "llvm/Support/Streams.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>
31#include <ostream>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000032#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
36namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
37
38/// -stats - Command line option to cause transformations to emit stats about
39/// what they did.
40///
41static cl::opt<bool>
42Enabled("stats", cl::desc("Enable statistics output from program"));
43
44
45namespace {
46/// StatisticInfo - This class is used in a ManagedStatic so that it is created
47/// on demand (when the first statistic is bumped) and destroyed only when
48/// llvm_shutdown is called. We print statistics from the destructor.
49class StatisticInfo {
50 std::vector<const Statistic*> Stats;
51public:
52 ~StatisticInfo();
53
54 void addStatistic(const Statistic *S) {
55 Stats.push_back(S);
56 }
57};
58}
59
60static ManagedStatic<StatisticInfo> StatInfo;
Owen Andersonb23c1be2009-06-22 23:08:27 +000061static ManagedStatic<sys::Mutex> StatLock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63/// RegisterStatistic - The first time a statistic is bumped, this method is
64/// called.
65void Statistic::RegisterStatistic() {
66 // If stats are enabled, inform StatInfo that this statistic should be
67 // printed.
Owen Andersonb23c1be2009-06-22 23:08:27 +000068 sys::ScopedLock Writer(&*StatLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 if (Enabled)
70 StatInfo->addStatistic(this);
71 // Remember we have been registered.
72 Initialized = true;
73}
74
Dan Gohman089efff2008-05-13 00:00:25 +000075namespace {
76
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077struct NameCompare {
78 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
79 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
80 if (Cmp != 0) return Cmp < 0;
81
82 // Secondary key is the description.
83 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
84 }
85};
86
Dan Gohman089efff2008-05-13 00:00:25 +000087}
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089// Print information when destroyed, iff command line option is specified.
90StatisticInfo::~StatisticInfo() {
91 // Statistics not enabled?
92 if (Stats.empty()) return;
93
94 // Get the stream to write to.
95 std::ostream &OutStream = *GetLibSupportInfoOutputFile();
96
97 // Figure out how long the biggest Value and Name fields are.
98 unsigned MaxNameLen = 0, MaxValLen = 0;
Evan Cheng591bfc82008-05-05 18:30:58 +000099 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 MaxValLen = std::max(MaxValLen,
101 (unsigned)utostr(Stats[i]->getValue()).size());
102 MaxNameLen = std::max(MaxNameLen,
103 (unsigned)std::strlen(Stats[i]->getName()));
104 }
105
106 // Sort the fields by name.
107 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
108
109 // Print out the statistics header...
110 OutStream << "===" << std::string(73, '-') << "===\n"
111 << " ... Statistics Collected ...\n"
112 << "===" << std::string(73, '-') << "===\n\n";
113
114 // Print all of the statistics.
Evan Cheng591bfc82008-05-05 18:30:58 +0000115 for (size_t i = 0, e = Stats.size(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 std::string CountStr = utostr(Stats[i]->getValue());
117 OutStream << std::string(MaxValLen-CountStr.size(), ' ')
118 << CountStr << " " << Stats[i]->getName()
119 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ')
120 << " - " << Stats[i]->getDesc() << "\n";
121
122 }
123
124 OutStream << std::endl; // Flush the output stream...
125
126 if (&OutStream != cerr.stream() && &OutStream != cout.stream())
127 delete &OutStream; // Close the file.
128}