blob: 10f13f554c0c800263a44f13b1025d694b6bd57a [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf3f4fd52002-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 Lattner975f0582006-12-08 20:00:42 +000018// static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000019//
20// Later, in the code: ++NumInstEliminated;
21//
22//===----------------------------------------------------------------------===//
23
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/Support/CommandLine.h"
Chris Lattner975f0582006-12-08 20:00:42 +000026#include "llvm/Support/ManagedStatic.h"
Bill Wendlingbcd24982006-12-07 20:28:15 +000027#include "llvm/Support/Streams.h"
Chris Lattnercf845042006-12-06 18:20:44 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattnera4ca41e2003-08-13 21:32:37 +000029#include <algorithm>
Bill Wendling1a097e32006-12-07 23:41:45 +000030#include <ostream>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner975f0582006-12-08 20:00:42 +000033// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000034namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000035
Chris Lattner975f0582006-12-08 20:00:42 +000036/// -stats - Command line option to cause transformations to emit stats about
37/// what they did.
38///
Chris Lattner5ff62e92002-07-22 02:10:13 +000039static cl::opt<bool>
40Enabled("stats", cl::desc("Enable statistics output from program"));
41
Chris Lattner96ef1b92002-10-01 22:35:45 +000042
Chris Lattner975f0582006-12-08 20:00:42 +000043namespace {
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.
47class StatisticInfo {
Chris Lattner0a361522006-12-19 23:17:40 +000048 std::vector<const Statistic*> Stats;
Chris Lattner975f0582006-12-08 20:00:42 +000049public:
50 ~StatisticInfo();
51
Chris Lattner0a361522006-12-19 23:17:40 +000052 void addStatistic(const Statistic *S) {
Chris Lattner975f0582006-12-08 20:00:42 +000053 Stats.push_back(S);
Chris Lattner96ef1b92002-10-01 22:35:45 +000054 }
Chris Lattner975f0582006-12-08 20:00:42 +000055};
56}
Chris Lattner96ef1b92002-10-01 22:35:45 +000057
Chris Lattner975f0582006-12-08 20:00:42 +000058static ManagedStatic<StatisticInfo> StatInfo;
59
60
61/// RegisterStatistic - The first time a statistic is bumped, this method is
62/// called.
Chris Lattner0a361522006-12-19 23:17:40 +000063void Statistic::RegisterStatistic() {
Chris Lattner975f0582006-12-08 20:00:42 +000064 // If stats are enabled, inform StatInfo that this statistic should be
65 // printed.
66 if (Enabled)
67 StatInfo->addStatistic(this);
68 // Remember we have been registered.
69 Initialized = true;
70}
71
72struct NameCompare {
Chris Lattner0a361522006-12-19 23:17:40 +000073 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
Chris Lattner975f0582006-12-08 20:00:42 +000074 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
75 if (Cmp != 0) return Cmp < 0;
76
77 // Secondary key is the description.
78 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
Chris Lattner96ef1b92002-10-01 22:35:45 +000079 }
80};
81
Chris Lattner975f0582006-12-08 20:00:42 +000082// Print information when destroyed, iff command line option is specified.
83StatisticInfo::~StatisticInfo() {
84 // Statistics not enabled?
85 if (Stats.empty()) return;
Chris Lattner96ef1b92002-10-01 22:35:45 +000086
Chris Lattner975f0582006-12-08 20:00:42 +000087 // Get the stream to write to.
88 std::ostream &OutStream = *GetLibSupportInfoOutputFile();
Chris Lattner96ef1b92002-10-01 22:35:45 +000089
Chris Lattner975f0582006-12-08 20:00:42 +000090 // 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()));
Chris Lattner96ef1b92002-10-01 22:35:45 +000097 }
Chris Lattner975f0582006-12-08 20:00:42 +000098
99 // Sort the fields by name.
100 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
Chris Lattner96ef1b92002-10-01 22:35:45 +0000101
Chris Lattner975f0582006-12-08 20:00:42 +0000102 // Print out the statistics header...
103 OutStream << "===" << std::string(73, '-') << "===\n"
104 << " ... Statistics Collected ...\n"
105 << "===" << std::string(73, '-') << "===\n\n";
106
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";
114
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000115 }
Chris Lattner975f0582006-12-08 20:00:42 +0000116
117 OutStream << std::endl; // Flush the output stream...
118
119 if (&OutStream != cerr.stream() && &OutStream != cout.stream())
120 delete &OutStream; // Close the file.
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000121}