blob: 56bbfe9d7a92f56e7a415a63d2bf51fa7f6b3ea8 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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//
18// static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
19//
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 Lattner96ef1b92002-10-01 22:35:45 +000026#include <sstream>
Chris Lattner038e05a2003-08-01 22:15:41 +000027#include <iostream>
Chris Lattnera4ca41e2003-08-13 21:32:37 +000028#include <algorithm>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattnerf205fec2003-05-09 20:05:44 +000031// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattnerb6d465f2003-12-14 21:27:33 +000032namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000033
Chris Lattner96ef1b92002-10-01 22:35:45 +000034unsigned StatisticBase::NumStats = 0;
35
Chris Lattner52c91712006-08-30 04:17:00 +000036TEMPLATE_INSTANTIATION(class Statistic<unsigned>);
37
Chris Lattner5ff62e92002-07-22 02:10:13 +000038// -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
Chris Lattner96ef1b92002-10-01 22:35:45 +000044struct StatRecord {
45 std::string Value;
46 const char *Name, *Desc;
47
Alkis Evlogimenosd2c39e92004-01-06 09:16:02 +000048 StatRecord(const std::string &V, const char *N, const char *D)
Chris Lattner96ef1b92002-10-01 22:35:45 +000049 : Value(V), Name(N), Desc(D) {}
50
51 bool operator<(const StatRecord &SR) const {
52 return std::strcmp(Name, SR.Name) < 0;
53 }
54
Chris Lattnerf205fec2003-05-09 20:05:44 +000055 void print(unsigned ValFieldSize, unsigned NameFieldSize,
56 std::ostream &OS) {
57 OS << std::string(ValFieldSize-Value.length(), ' ')
58 << Value << " " << Name
59 << std::string(NameFieldSize-std::strlen(Name), ' ')
60 << " - " << Desc << "\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000061 }
62};
63
64static std::vector<StatRecord> *AccumStats = 0;
65
Chris Lattner70aa33e2006-06-21 16:53:47 +000066// Out of line virtual dtor, to give the vtable etc a home.
67StatisticBase::~StatisticBase() {
68}
69
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000070// Print information when destroyed, iff command line option is specified
71void StatisticBase::destroy() const {
72 if (Enabled && hasSomeData()) {
Chris Lattner96ef1b92002-10-01 22:35:45 +000073 if (AccumStats == 0)
74 AccumStats = new std::vector<StatRecord>();
75
76 std::ostringstream Out;
77 printValue(Out);
78 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
79 }
80
81 if (--NumStats == 0 && AccumStats) {
Chris Lattnerf205fec2003-05-09 20:05:44 +000082 std::ostream *OutStream = GetLibSupportInfoOutputFile();
83
Chris Lattner96ef1b92002-10-01 22:35:45 +000084 // Figure out how long the biggest Value and Name fields are...
85 unsigned MaxNameLen = 0, MaxValLen = 0;
86 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
Misha Brukmanf976c852005-04-21 22:55:34 +000087 MaxValLen = std::max(MaxValLen,
Anand Shuklac1d4d792002-10-04 23:56:18 +000088 (unsigned)(*AccumStats)[i].Value.length());
Misha Brukmanf976c852005-04-21 22:55:34 +000089 MaxNameLen = std::max(MaxNameLen,
Anand Shuklac1d4d792002-10-04 23:56:18 +000090 (unsigned)std::strlen((*AccumStats)[i].Name));
Chris Lattner96ef1b92002-10-01 22:35:45 +000091 }
92
93 // Sort the fields...
94 std::stable_sort(AccumStats->begin(), AccumStats->end());
95
96 // Print out the statistics header...
Chris Lattnerf205fec2003-05-09 20:05:44 +000097 *OutStream << "===" << std::string(73, '-') << "===\n"
98 << " ... Statistics Collected ...\n"
99 << "===" << std::string(73, '-') << "===\n\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +0000100
101 // Print all of the statistics accumulated...
102 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000103 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
Chris Lattner96ef1b92002-10-01 22:35:45 +0000104
Chris Lattnerf205fec2003-05-09 20:05:44 +0000105 *OutStream << std::endl; // Flush the output stream...
Chris Lattner96ef1b92002-10-01 22:35:45 +0000106
107 // Free all accumulated statistics...
108 delete AccumStats;
109 AccumStats = 0;
Chris Lattnerf205fec2003-05-09 20:05:44 +0000110 if (OutStream != &std::cerr && OutStream != &std::cout)
111 delete OutStream; // Close the file...
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000112 }
113}