blob: 7d5f65af28422fe2c973ba4e3c086f950561ccc3 [file] [log] [blame]
Chris Lattnerc758fe62002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner9eb00522002-05-10 15:36:46 +00009//
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//
Chris Lattner8c9969a2006-12-08 20:00:42 +000018// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattner9eb00522002-05-10 15:36:46 +000019//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/CommandLine.h"
David Greeneb28b1ed2010-01-05 01:28:47 +000026#include "llvm/Support/Debug.h"
Chris Lattner8c9969a2006-12-08 20:00:42 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattner471ba482009-08-23 08:43:55 +000028#include "llvm/Support/raw_ostream.h"
Owen Anderson975ce632009-06-22 23:08:27 +000029#include "llvm/System/Mutex.h"
Chris Lattner5bbf7702006-12-06 18:20:44 +000030#include "llvm/ADT/StringExtras.h"
Chris Lattnerafa3ec42003-08-13 21:32:37 +000031#include <algorithm>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000032#include <cstring>
Chris Lattnerdd978ce2003-12-14 21:27:33 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner6cbd8d12010-03-30 05:01:08 +000035// CreateInfoOutputFile - Return a file stream to print our output on.
36namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
Chris Lattnerb0e59582003-05-09 20:05:44 +000037
Chris Lattner8c9969a2006-12-08 20:00:42 +000038/// -stats - Command line option to cause transformations to emit stats about
39/// what they did.
40///
Chris Lattnerf5cad152002-07-22 02:10:13 +000041static cl::opt<bool>
42Enabled("stats", cl::desc("Enable statistics output from program"));
43
Chris Lattnerc758fe62002-10-01 22:35:45 +000044
Chris Lattner8c9969a2006-12-08 20:00:42 +000045namespace {
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 {
Chris Lattner00bb2162006-12-19 23:17:40 +000050 std::vector<const Statistic*> Stats;
Douglas Gregor83fd0152010-03-30 17:32:08 +000051 friend void llvm::PrintStatistics();
52 friend void llvm::PrintStatistics(raw_ostream &OS);
Chris Lattner8c9969a2006-12-08 20:00:42 +000053public:
54 ~StatisticInfo();
55
Chris Lattner00bb2162006-12-19 23:17:40 +000056 void addStatistic(const Statistic *S) {
Chris Lattner8c9969a2006-12-08 20:00:42 +000057 Stats.push_back(S);
Chris Lattnerc758fe62002-10-01 22:35:45 +000058 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000059};
60}
Chris Lattnerc758fe62002-10-01 22:35:45 +000061
Chris Lattner8c9969a2006-12-08 20:00:42 +000062static ManagedStatic<StatisticInfo> StatInfo;
Torok Edwin819d15c2009-09-27 11:08:03 +000063static ManagedStatic<sys::SmartMutex<true> > StatLock;
Chris Lattner8c9969a2006-12-08 20:00:42 +000064
65/// RegisterStatistic - The first time a statistic is bumped, this method is
66/// called.
Chris Lattner00bb2162006-12-19 23:17:40 +000067void Statistic::RegisterStatistic() {
Chris Lattner8c9969a2006-12-08 20:00:42 +000068 // If stats are enabled, inform StatInfo that this statistic should be
69 // printed.
Torok Edwin819d15c2009-09-27 11:08:03 +000070 sys::SmartScopedLock<true> Writer(*StatLock);
Owen Andersonca8f9862009-06-23 21:19:38 +000071 if (!Initialized) {
72 if (Enabled)
73 StatInfo->addStatistic(this);
74
75 sys::MemoryFence();
76 // Remember we have been registered.
77 Initialized = true;
78 }
Chris Lattner8c9969a2006-12-08 20:00:42 +000079}
80
Dan Gohmand78c4002008-05-13 00:00:25 +000081namespace {
82
Chris Lattner8c9969a2006-12-08 20:00:42 +000083struct NameCompare {
Chris Lattner00bb2162006-12-19 23:17:40 +000084 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
Chris Lattner8c9969a2006-12-08 20:00:42 +000085 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
86 if (Cmp != 0) return Cmp < 0;
87
88 // Secondary key is the description.
89 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
Chris Lattnerc758fe62002-10-01 22:35:45 +000090 }
91};
92
Dan Gohmand78c4002008-05-13 00:00:25 +000093}
94
Chris Lattner8c9969a2006-12-08 20:00:42 +000095// Print information when destroyed, iff command line option is specified.
96StatisticInfo::~StatisticInfo() {
Douglas Gregor83fd0152010-03-30 17:32:08 +000097 llvm::PrintStatistics();
98}
Chris Lattnerc758fe62002-10-01 22:35:45 +000099
Douglas Gregor83fd0152010-03-30 17:32:08 +0000100void llvm::EnableStatistics() {
101 Enabled.setValue(true);
102}
103
104void llvm::PrintStatistics(raw_ostream &OS) {
105 StatisticInfo &Stats = *StatInfo;
Chris Lattnerc758fe62002-10-01 22:35:45 +0000106
Chris Lattner8c9969a2006-12-08 20:00:42 +0000107 // Figure out how long the biggest Value and Name fields are.
108 unsigned MaxNameLen = 0, MaxValLen = 0;
Douglas Gregor83fd0152010-03-30 17:32:08 +0000109 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
Chris Lattner8c9969a2006-12-08 20:00:42 +0000110 MaxValLen = std::max(MaxValLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000111 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
Chris Lattner8c9969a2006-12-08 20:00:42 +0000112 MaxNameLen = std::max(MaxNameLen,
Douglas Gregor83fd0152010-03-30 17:32:08 +0000113 (unsigned)std::strlen(Stats.Stats[i]->getName()));
Chris Lattnerc758fe62002-10-01 22:35:45 +0000114 }
Chris Lattner8c9969a2006-12-08 20:00:42 +0000115
116 // Sort the fields by name.
Douglas Gregor83fd0152010-03-30 17:32:08 +0000117 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare());
Chris Lattnerc758fe62002-10-01 22:35:45 +0000118
Chris Lattner8c9969a2006-12-08 20:00:42 +0000119 // Print out the statistics header...
Douglas Gregor83fd0152010-03-30 17:32:08 +0000120 OS << "===" << std::string(73, '-') << "===\n"
121 << " ... Statistics Collected ...\n"
122 << "===" << std::string(73, '-') << "===\n\n";
Chris Lattner8c9969a2006-12-08 20:00:42 +0000123
124 // Print all of the statistics.
Douglas Gregor83fd0152010-03-30 17:32:08 +0000125 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
126 std::string CountStr = utostr(Stats.Stats[i]->getValue());
127 OS << std::string(MaxValLen-CountStr.size(), ' ')
128 << CountStr << " " << Stats.Stats[i]->getName()
129 << std::string(MaxNameLen-std::strlen(Stats.Stats[i]->getName()), ' ')
130 << " - " << Stats.Stats[i]->getDesc() << "\n";
Chris Lattner9eb00522002-05-10 15:36:46 +0000131 }
Chris Lattner8c9969a2006-12-08 20:00:42 +0000132
Douglas Gregor83fd0152010-03-30 17:32:08 +0000133 OS << '\n'; // Flush the output stream.
134 OS.flush();
135
136}
137
138void llvm::PrintStatistics() {
139 StatisticInfo &Stats = *StatInfo;
140
141 // Statistics not enabled?
142 if (Stats.Stats.empty()) return;
143
144 // Get the stream to write to.
145 raw_ostream &OutStream = *CreateInfoOutputFile();
146 PrintStatistics(OutStream);
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000147 delete &OutStream; // Close the file.
Chris Lattner9eb00522002-05-10 15:36:46 +0000148}