blob: 1141086c3f3c34f2b49998f0446ffe7222db0967 [file] [log] [blame]
Chris Lattner96ef1b92002-10-01 22:35:45 +00001//===-- Statistic.cpp - Easy way to expose stats information --------------===//
Chris Lattnerf3f4fd52002-05-10 15:36:46 +00002//
3// This file implements the 'Statistic' class, which is designed to be an easy
4// way to expose various success metrics from passes. These statistics are
5// printed at the end of a run, when the -stats command line option is enabled
6// on the command line.
7//
8// This is useful for reporting information like the number of instructions
9// simplified, optimized or removed by various transformations, like this:
10//
11// static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
12//
13// Later, in the code: ++NumInstEliminated;
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner96ef1b92002-10-01 22:35:45 +000017#include "Support/Statistic.h"
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000018#include "Support/CommandLine.h"
19#include <iostream>
Chris Lattner96ef1b92002-10-01 22:35:45 +000020#include <sstream>
Chris Lattner9550dc22002-10-27 19:08:03 +000021#include <algorithm>
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000022
Chris Lattnerf205fec2003-05-09 20:05:44 +000023// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
24std::ostream *GetLibSupportInfoOutputFile();
25
Chris Lattner4435ac02002-05-22 17:06:20 +000026bool DebugFlag; // DebugFlag - Exported boolean set by the -debug option
27
Chris Lattner96ef1b92002-10-01 22:35:45 +000028unsigned StatisticBase::NumStats = 0;
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030// -stats - Command line option to cause transformations to emit stats about
31// what they did.
32//
33static cl::opt<bool>
34Enabled("stats", cl::desc("Enable statistics output from program"));
35
Chris Lattner0ad4c002003-02-09 21:13:57 +000036#ifndef NDEBUG
Chris Lattner5ff62e92002-07-22 02:10:13 +000037// -debug - Command line option to enable the DEBUG statements in the passes.
Chris Lattner0ad4c002003-02-09 21:13:57 +000038// This flag may only be enabled in debug builds.
Chris Lattner5ff62e92002-07-22 02:10:13 +000039static cl::opt<bool, true>
40Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
41 cl::location(DebugFlag));
Chris Lattner0ad4c002003-02-09 21:13:57 +000042#endif
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000043
Chris Lattner96ef1b92002-10-01 22:35:45 +000044struct StatRecord {
45 std::string Value;
46 const char *Name, *Desc;
47
48 StatRecord(const std::string V, const char *N, const char *D)
49 : 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 Lattnerf3f4fd52002-05-10 15:36:46 +000066// Print information when destroyed, iff command line option is specified
67void StatisticBase::destroy() const {
68 if (Enabled && hasSomeData()) {
Chris Lattner96ef1b92002-10-01 22:35:45 +000069 if (AccumStats == 0)
70 AccumStats = new std::vector<StatRecord>();
71
72 std::ostringstream Out;
73 printValue(Out);
74 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
75 }
76
77 if (--NumStats == 0 && AccumStats) {
Chris Lattnerf205fec2003-05-09 20:05:44 +000078 std::ostream *OutStream = GetLibSupportInfoOutputFile();
79
Chris Lattner96ef1b92002-10-01 22:35:45 +000080 // Figure out how long the biggest Value and Name fields are...
81 unsigned MaxNameLen = 0, MaxValLen = 0;
82 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
Anand Shuklac1d4d792002-10-04 23:56:18 +000083 MaxValLen = std::max(MaxValLen,
84 (unsigned)(*AccumStats)[i].Value.length());
85 MaxNameLen = std::max(MaxNameLen,
86 (unsigned)std::strlen((*AccumStats)[i].Name));
Chris Lattner96ef1b92002-10-01 22:35:45 +000087 }
88
89 // Sort the fields...
90 std::stable_sort(AccumStats->begin(), AccumStats->end());
91
92 // Print out the statistics header...
Chris Lattnerf205fec2003-05-09 20:05:44 +000093 *OutStream << "===" << std::string(73, '-') << "===\n"
94 << " ... Statistics Collected ...\n"
95 << "===" << std::string(73, '-') << "===\n\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000096
97 // Print all of the statistics accumulated...
98 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +000099 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
Chris Lattner96ef1b92002-10-01 22:35:45 +0000100
Chris Lattnerf205fec2003-05-09 20:05:44 +0000101 *OutStream << std::endl; // Flush the output stream...
Chris Lattner96ef1b92002-10-01 22:35:45 +0000102
103 // Free all accumulated statistics...
104 delete AccumStats;
105 AccumStats = 0;
Chris Lattnerf205fec2003-05-09 20:05:44 +0000106 if (OutStream != &std::cerr && OutStream != &std::cout)
107 delete OutStream; // Close the file...
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000108 }
109}