blob: ae9f57f80ed365fb58270bf9f6b333abab772275 [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"
Chris Lattner96ef1b92002-10-01 22:35:45 +000019#include <sstream>
Chris Lattner038e05a2003-08-01 22:15:41 +000020#include <iostream>
Chris Lattnera4ca41e2003-08-13 21:32:37 +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 Lattner96ef1b92002-10-01 22:35:45 +000026unsigned StatisticBase::NumStats = 0;
27
Chris Lattner5ff62e92002-07-22 02:10:13 +000028// -stats - Command line option to cause transformations to emit stats about
29// what they did.
30//
31static cl::opt<bool>
32Enabled("stats", cl::desc("Enable statistics output from program"));
33
Chris Lattner96ef1b92002-10-01 22:35:45 +000034struct StatRecord {
35 std::string Value;
36 const char *Name, *Desc;
37
38 StatRecord(const std::string V, const char *N, const char *D)
39 : Value(V), Name(N), Desc(D) {}
40
41 bool operator<(const StatRecord &SR) const {
42 return std::strcmp(Name, SR.Name) < 0;
43 }
44
Chris Lattnerf205fec2003-05-09 20:05:44 +000045 void print(unsigned ValFieldSize, unsigned NameFieldSize,
46 std::ostream &OS) {
47 OS << std::string(ValFieldSize-Value.length(), ' ')
48 << Value << " " << Name
49 << std::string(NameFieldSize-std::strlen(Name), ' ')
50 << " - " << Desc << "\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000051 }
52};
53
54static std::vector<StatRecord> *AccumStats = 0;
55
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000056// Print information when destroyed, iff command line option is specified
57void StatisticBase::destroy() const {
58 if (Enabled && hasSomeData()) {
Chris Lattner96ef1b92002-10-01 22:35:45 +000059 if (AccumStats == 0)
60 AccumStats = new std::vector<StatRecord>();
61
62 std::ostringstream Out;
63 printValue(Out);
64 AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
65 }
66
67 if (--NumStats == 0 && AccumStats) {
Chris Lattnerf205fec2003-05-09 20:05:44 +000068 std::ostream *OutStream = GetLibSupportInfoOutputFile();
69
Chris Lattner96ef1b92002-10-01 22:35:45 +000070 // Figure out how long the biggest Value and Name fields are...
71 unsigned MaxNameLen = 0, MaxValLen = 0;
72 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
Anand Shuklac1d4d792002-10-04 23:56:18 +000073 MaxValLen = std::max(MaxValLen,
74 (unsigned)(*AccumStats)[i].Value.length());
75 MaxNameLen = std::max(MaxNameLen,
76 (unsigned)std::strlen((*AccumStats)[i].Name));
Chris Lattner96ef1b92002-10-01 22:35:45 +000077 }
78
79 // Sort the fields...
80 std::stable_sort(AccumStats->begin(), AccumStats->end());
81
82 // Print out the statistics header...
Chris Lattnerf205fec2003-05-09 20:05:44 +000083 *OutStream << "===" << std::string(73, '-') << "===\n"
84 << " ... Statistics Collected ...\n"
85 << "===" << std::string(73, '-') << "===\n\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000086
87 // Print all of the statistics accumulated...
88 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +000089 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
Chris Lattner96ef1b92002-10-01 22:35:45 +000090
Chris Lattnerf205fec2003-05-09 20:05:44 +000091 *OutStream << std::endl; // Flush the output stream...
Chris Lattner96ef1b92002-10-01 22:35:45 +000092
93 // Free all accumulated statistics...
94 delete AccumStats;
95 AccumStats = 0;
Chris Lattnerf205fec2003-05-09 20:05:44 +000096 if (OutStream != &std::cerr && OutStream != &std::cout)
97 delete OutStream; // Close the file...
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000098 }
99}