blob: 1b28eed49ab1e16ea91edfc06dcdcfc579020fcf [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//
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000018// static Statistic NumInstEliminated("GCSE - Number of instructions killed");
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000019//
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"
Bill Wendlingbcd24982006-12-07 20:28:15 +000026#include "llvm/Support/Streams.h"
Chris Lattnercf845042006-12-06 18:20:44 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattnera4ca41e2003-08-13 21:32:37 +000028#include <algorithm>
Bill Wendling1a097e32006-12-07 23:41:45 +000029#include <ostream>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattnerf205fec2003-05-09 20:05:44 +000032// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattnerb6d465f2003-12-14 21:27:33 +000033namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Chris Lattnerf205fec2003-05-09 20:05:44 +000034
Chris Lattnercf845042006-12-06 18:20:44 +000035unsigned Statistic::NumStats = 0;
Chris Lattner96ef1b92002-10-01 22:35:45 +000036
Chris Lattner5ff62e92002-07-22 02:10:13 +000037// -stats - Command line option to cause transformations to emit stats about
38// what they did.
39//
40static cl::opt<bool>
41Enabled("stats", cl::desc("Enable statistics output from program"));
42
Chris Lattner96ef1b92002-10-01 22:35:45 +000043struct StatRecord {
44 std::string Value;
45 const char *Name, *Desc;
46
Alkis Evlogimenosd2c39e92004-01-06 09:16:02 +000047 StatRecord(const std::string &V, const char *N, const char *D)
Chris Lattner96ef1b92002-10-01 22:35:45 +000048 : Value(V), Name(N), Desc(D) {}
49
50 bool operator<(const StatRecord &SR) const {
51 return std::strcmp(Name, SR.Name) < 0;
52 }
53
Chris Lattnerf205fec2003-05-09 20:05:44 +000054 void print(unsigned ValFieldSize, unsigned NameFieldSize,
55 std::ostream &OS) {
56 OS << std::string(ValFieldSize-Value.length(), ' ')
57 << Value << " " << Name
58 << std::string(NameFieldSize-std::strlen(Name), ' ')
59 << " - " << Desc << "\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000060 }
61};
62
63static std::vector<StatRecord> *AccumStats = 0;
64
Chris Lattnerf3f4fd52002-05-10 15:36:46 +000065// Print information when destroyed, iff command line option is specified
Chris Lattnercf845042006-12-06 18:20:44 +000066Statistic::~Statistic() {
67 if (Enabled && Value != 0) {
Chris Lattner96ef1b92002-10-01 22:35:45 +000068 if (AccumStats == 0)
69 AccumStats = new std::vector<StatRecord>();
70
Chris Lattnercf845042006-12-06 18:20:44 +000071 AccumStats->push_back(StatRecord(utostr(Value), Name, Desc));
Chris Lattner96ef1b92002-10-01 22:35:45 +000072 }
73
74 if (--NumStats == 0 && AccumStats) {
Chris Lattnerf205fec2003-05-09 20:05:44 +000075 std::ostream *OutStream = GetLibSupportInfoOutputFile();
76
Chris Lattner96ef1b92002-10-01 22:35:45 +000077 // Figure out how long the biggest Value and Name fields are...
78 unsigned MaxNameLen = 0, MaxValLen = 0;
79 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
Misha Brukmanf976c852005-04-21 22:55:34 +000080 MaxValLen = std::max(MaxValLen,
Anand Shuklac1d4d792002-10-04 23:56:18 +000081 (unsigned)(*AccumStats)[i].Value.length());
Misha Brukmanf976c852005-04-21 22:55:34 +000082 MaxNameLen = std::max(MaxNameLen,
Anand Shuklac1d4d792002-10-04 23:56:18 +000083 (unsigned)std::strlen((*AccumStats)[i].Name));
Chris Lattner96ef1b92002-10-01 22:35:45 +000084 }
85
86 // Sort the fields...
87 std::stable_sort(AccumStats->begin(), AccumStats->end());
88
89 // Print out the statistics header...
Chris Lattnerf205fec2003-05-09 20:05:44 +000090 *OutStream << "===" << std::string(73, '-') << "===\n"
91 << " ... Statistics Collected ...\n"
92 << "===" << std::string(73, '-') << "===\n\n";
Chris Lattner96ef1b92002-10-01 22:35:45 +000093
94 // Print all of the statistics accumulated...
95 for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +000096 (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
Chris Lattner96ef1b92002-10-01 22:35:45 +000097
Chris Lattnerf205fec2003-05-09 20:05:44 +000098 *OutStream << std::endl; // Flush the output stream...
Chris Lattner96ef1b92002-10-01 22:35:45 +000099
100 // Free all accumulated statistics...
101 delete AccumStats;
102 AccumStats = 0;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000103 if (OutStream != cerr.stream() && OutStream != cout.stream())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000104 delete OutStream; // Close the file...
Chris Lattnerf3f4fd52002-05-10 15:36:46 +0000105 }
106}