blob: f6645c6ae401132d93e7e12a2b15c7c6fcec1031 [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 Lattner4435ac02002-05-22 17:06:20 +000023bool DebugFlag; // DebugFlag - Exported boolean set by the -debug option
24
Chris Lattner96ef1b92002-10-01 22:35:45 +000025unsigned StatisticBase::NumStats = 0;
26
Chris Lattner5ff62e92002-07-22 02:10:13 +000027// -stats - Command line option to cause transformations to emit stats about
28// what they did.
29//
30static cl::opt<bool>
31Enabled("stats", cl::desc("Enable statistics output from program"));
32
Chris Lattner0ad4c002003-02-09 21:13:57 +000033#ifndef NDEBUG
Chris Lattner5ff62e92002-07-22 02:10:13 +000034// -debug - Command line option to enable the DEBUG statements in the passes.
Chris Lattner0ad4c002003-02-09 21:13:57 +000035// This flag may only be enabled in debug builds.
Chris Lattner5ff62e92002-07-22 02:10:13 +000036static cl::opt<bool, true>
37Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
38 cl::location(DebugFlag));
Chris Lattner0ad4c002003-02-09 21:13:57 +000039#endif
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000040
Chris Lattner96ef1b92002-10-01 22:35:45 +000041struct StatRecord {
42 std::string Value;
43 const char *Name, *Desc;
44
45 StatRecord(const std::string V, const char *N, const char *D)
46 : Value(V), Name(N), Desc(D) {}
47
48 bool operator<(const StatRecord &SR) const {
49 return std::strcmp(Name, SR.Name) < 0;
50 }
51
52 void print(unsigned ValFieldSize, unsigned NameFieldSize) {
53 std::cerr << std::string(ValFieldSize-Value.length(), ' ')
54 << Value << " " << Name
55 << std::string(NameFieldSize-std::strlen(Name), ' ')
56 << " - " << Desc << "\n";
57 }
58};
59
60static std::vector<StatRecord> *AccumStats = 0;
61
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000062// Print information when destroyed, iff command line option is specified
63void StatisticBase::destroy() const {
64 if (Enabled && hasSomeData()) {
Chris Lattner96ef1b92002-10-01 22:35:45 +000065 if (AccumStats == 0)
66 AccumStats = new std::vector<StatRecord>();
67
68 std::ostringstream Out;
69 printValue(Out);
70 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
71 }
72
73 if (--NumStats == 0 && AccumStats) {
74 // Figure out how long the biggest Value and Name fields are...
75 unsigned MaxNameLen = 0, MaxValLen = 0;
76 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
Anand Shuklac1d4d792002-10-04 23:56:18 +000077 MaxValLen = std::max(MaxValLen,
78 (unsigned)(*AccumStats)[i].Value.length());
79 MaxNameLen = std::max(MaxNameLen,
80 (unsigned)std::strlen((*AccumStats)[i].Name));
Chris Lattner96ef1b92002-10-01 22:35:45 +000081 }
82
83 // Sort the fields...
84 std::stable_sort(AccumStats->begin(), AccumStats->end());
85
86 // Print out the statistics header...
87 std::cerr << "===" << std::string(73, '-') << "===\n"
88 << " ... Statistics Collected ...\n"
89 << "===" << std::string(73, '-') << "===\n\n";
90
91 // Print all of the statistics accumulated...
92 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
93 (*AccumStats)[i].print(MaxValLen, MaxNameLen);
94
95 std::cerr << std::endl; // Flush the output stream...
96
97 // Free all accumulated statistics...
98 delete AccumStats;
99 AccumStats = 0;
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000100 }
101}