2002-10-01 22:35:45 +00:00
|
|
|
//===-- Statistic.cpp - Easy way to expose stats information --------------===//
|
2002-05-10 15:36:46 +00:00
|
|
|
//
|
|
|
|
// This file implements the 'Statistic' class, which is designed to be an easy
|
|
|
|
// way to expose various success metrics from passes. These statistics are
|
|
|
|
// printed at the end of a run, when the -stats command line option is enabled
|
|
|
|
// on the command line.
|
|
|
|
//
|
|
|
|
// This is useful for reporting information like the number of instructions
|
|
|
|
// simplified, optimized or removed by various transformations, like this:
|
|
|
|
//
|
|
|
|
// static Statistic<> NumInstEliminated("GCSE - Number of instructions killed");
|
|
|
|
//
|
|
|
|
// Later, in the code: ++NumInstEliminated;
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-10-01 22:35:45 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-10 15:36:46 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-10-01 22:35:45 +00:00
|
|
|
#include <sstream>
|
2003-08-01 22:15:41 +00:00
|
|
|
#include <iostream>
|
2002-05-10 15:36:46 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
|
|
|
|
std::ostream *GetLibSupportInfoOutputFile();
|
|
|
|
|
2002-10-01 22:35:45 +00:00
|
|
|
unsigned StatisticBase::NumStats = 0;
|
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
// -stats - Command line option to cause transformations to emit stats about
|
|
|
|
// what they did.
|
|
|
|
//
|
|
|
|
static cl::opt<bool>
|
|
|
|
Enabled("stats", cl::desc("Enable statistics output from program"));
|
|
|
|
|
2002-10-01 22:35:45 +00:00
|
|
|
struct StatRecord {
|
|
|
|
std::string Value;
|
|
|
|
const char *Name, *Desc;
|
|
|
|
|
|
|
|
StatRecord(const std::string V, const char *N, const char *D)
|
|
|
|
: Value(V), Name(N), Desc(D) {}
|
|
|
|
|
|
|
|
bool operator<(const StatRecord &SR) const {
|
|
|
|
return std::strcmp(Name, SR.Name) < 0;
|
|
|
|
}
|
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
void print(unsigned ValFieldSize, unsigned NameFieldSize,
|
|
|
|
std::ostream &OS) {
|
|
|
|
OS << std::string(ValFieldSize-Value.length(), ' ')
|
|
|
|
<< Value << " " << Name
|
|
|
|
<< std::string(NameFieldSize-std::strlen(Name), ' ')
|
|
|
|
<< " - " << Desc << "\n";
|
2002-10-01 22:35:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<StatRecord> *AccumStats = 0;
|
|
|
|
|
2002-05-10 15:36:46 +00:00
|
|
|
// Print information when destroyed, iff command line option is specified
|
|
|
|
void StatisticBase::destroy() const {
|
|
|
|
if (Enabled && hasSomeData()) {
|
2002-10-01 22:35:45 +00:00
|
|
|
if (AccumStats == 0)
|
|
|
|
AccumStats = new std::vector<StatRecord>();
|
|
|
|
|
|
|
|
std::ostringstream Out;
|
|
|
|
printValue(Out);
|
|
|
|
AccumStats->push_back(StatRecord(Out.str(), Name, Desc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--NumStats == 0 && AccumStats) {
|
2003-05-09 20:05:44 +00:00
|
|
|
std::ostream *OutStream = GetLibSupportInfoOutputFile();
|
|
|
|
|
2002-10-01 22:35:45 +00:00
|
|
|
// Figure out how long the biggest Value and Name fields are...
|
|
|
|
unsigned MaxNameLen = 0, MaxValLen = 0;
|
|
|
|
for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
|
2002-10-04 23:56:18 +00:00
|
|
|
MaxValLen = std::max(MaxValLen,
|
|
|
|
(unsigned)(*AccumStats)[i].Value.length());
|
|
|
|
MaxNameLen = std::max(MaxNameLen,
|
|
|
|
(unsigned)std::strlen((*AccumStats)[i].Name));
|
2002-10-01 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the fields...
|
|
|
|
std::stable_sort(AccumStats->begin(), AccumStats->end());
|
|
|
|
|
|
|
|
// Print out the statistics header...
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << "===" << std::string(73, '-') << "===\n"
|
|
|
|
<< " ... Statistics Collected ...\n"
|
|
|
|
<< "===" << std::string(73, '-') << "===\n\n";
|
2002-10-01 22:35:45 +00:00
|
|
|
|
|
|
|
// Print all of the statistics accumulated...
|
|
|
|
for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
|
2003-05-09 20:05:44 +00:00
|
|
|
(*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
|
2002-10-01 22:35:45 +00:00
|
|
|
|
2003-05-09 20:05:44 +00:00
|
|
|
*OutStream << std::endl; // Flush the output stream...
|
2002-10-01 22:35:45 +00:00
|
|
|
|
|
|
|
// Free all accumulated statistics...
|
|
|
|
delete AccumStats;
|
|
|
|
AccumStats = 0;
|
2003-05-09 20:05:44 +00:00
|
|
|
if (OutStream != &std::cerr && OutStream != &std::cout)
|
|
|
|
delete OutStream; // Close the file...
|
2002-05-10 15:36:46 +00:00
|
|
|
}
|
|
|
|
}
|