blob: e500b55f5f54f2c0823dc10bed3fc58a828d8b86 [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>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000031#include <cstring>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner975f0582006-12-08 20:00:42 +000034// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000035namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000036
Chris Lattner975f0582006-12-08 20:00:42 +000037/// -stats - Command line option to cause transformations to emit stats about
38/// what they did.
39///
Chris Lattner5ff62e92002-07-22 02:10:13 +000040static cl::opt<bool>
41Enabled("stats", cl::desc("Enable statistics output from program"));
42
Chris Lattner96ef1b92002-10-01 22:35:45 +000043
Chris Lattner975f0582006-12-08 20:00:42 +000044namespace {
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 {
Chris Lattner0a361522006-12-19 23:17:40 +000049 std::vector<const Statistic*> Stats;
Chris Lattner975f0582006-12-08 20:00:42 +000050public:
51 ~StatisticInfo();
52
Chris Lattner0a361522006-12-19 23:17:40 +000053 void addStatistic(const Statistic *S) {
Chris Lattner975f0582006-12-08 20:00:42 +000054 Stats.push_back(S);
Chris Lattner96ef1b92002-10-01 22:35:45 +000055 }
Chris Lattner975f0582006-12-08 20:00:42 +000056};
57}
Chris Lattner96ef1b92002-10-01 22:35:45 +000058
Chris Lattner975f0582006-12-08 20:00:42 +000059static ManagedStatic<StatisticInfo> StatInfo;
60
61
62/// RegisterStatistic - The first time a statistic is bumped, this method is
63/// called.
Chris Lattner0a361522006-12-19 23:17:40 +000064void Statistic::RegisterStatistic() {
Chris Lattner975f0582006-12-08 20:00:42 +000065 // If stats are enabled, inform StatInfo that this statistic should be
66 // printed.
67 if (Enabled)
68 StatInfo->addStatistic(this);
69 // Remember we have been registered.
70 Initialized = true;
71}
72
73struct NameCompare {
Chris Lattner0a361522006-12-19 23:17:40 +000074 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
Chris Lattner975f0582006-12-08 20:00:42 +000075 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
76 if (Cmp != 0) return Cmp < 0;
77
78 // Secondary key is the description.
79 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
Chris Lattner96ef1b92002-10-01 22:35:45 +000080 }
81};
82
Chris Lattner975f0582006-12-08 20:00:42 +000083// Print information when destroyed, iff command line option is specified.
84StatisticInfo::~StatisticInfo() {
85 // Statistics not enabled?
86 if (Stats.empty()) return;
Chris Lattner96ef1b92002-10-01 22:35:45 +000087
Chris Lattner975f0582006-12-08 20:00:42 +000088 // Get the stream to write to.
89 std::ostream &OutStream = *GetLibSupportInfoOutputFile();
Chris Lattner96ef1b92002-10-01 22:35:45 +000090
Chris Lattner975f0582006-12-08 20:00:42 +000091 // Figure out how long the biggest Value and Name fields are.
92 unsigned MaxNameLen = 0, MaxValLen = 0;
93 for (unsigned i = 0, e = Stats.size(); i != e; ++i) {
94 MaxValLen = std::max(MaxValLen,
95 (unsigned)utostr(Stats[i]->getValue()).size());
96 MaxNameLen = std::max(MaxNameLen,
97 (unsigned)std::strlen(Stats[i]->getName()));
Chris Lattner96ef1b92002-10-01 22:35:45 +000098 }
Chris Lattner975f0582006-12-08 20:00:42 +000099
100 // Sort the fields by name.
101 std::stable_sort(Stats.begin(), Stats.end(), NameCompare());
Chris Lattner96ef1b92002-10-01 22:35:45 +0000102
Chris Lattner975f0582006-12-08 20:00:42 +0000103 // Print out the statistics header...
104 OutStream << "===" << std::string(73, '-') << "===\n"
105 << " ... Statistics Collected ...\n"
106 << "===" << std::string(73, '-') << "===\n\n";
107
108 // Print all of the statistics.
109 for (unsigned i = 0, e = Stats.size(); i != e; ++i) {
110 std::string CountStr = utostr(Stats[i]->getValue());
111 OutStream << std::string(MaxValLen-CountStr.size(), ' ')
112 << CountStr << " " << Stats[i]->getName()
113 << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ')
114 << " - " << Stats[i]->getDesc() << "\n";
115
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000116 }
Chris Lattner975f0582006-12-08 20:00:42 +0000117
118 OutStream << std::endl; // Flush the output stream...
119
120 if (&OutStream != cerr.stream() && &OutStream != cout.stream())
121 delete &OutStream; // Close the file.
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000122}