Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Statistic.cpp - Easy way to expose stats information --------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 9 | // |
| 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 | // |
| 18 | // static Statistic<> NumInstEliminated("GCSE - Number of instructions killed"); |
| 19 | // |
| 20 | // Later, in the code: ++NumInstEliminated; |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 24 | #include "Support/Statistic.h" |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 25 | #include "Support/CommandLine.h" |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 26 | #include <sstream> |
Chris Lattner | 038e05a | 2003-08-01 22:15:41 +0000 | [diff] [blame] | 27 | #include <iostream> |
Chris Lattner | a4ca41e | 2003-08-13 21:32:37 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | b6d465f | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 31 | // GetLibSupportInfoOutputFile - Return a file stream to print our output on... |
Chris Lattner | b6d465f | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 32 | namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); } |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 34 | unsigned StatisticBase::NumStats = 0; |
| 35 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | // -stats - Command line option to cause transformations to emit stats about |
| 37 | // what they did. |
| 38 | // |
| 39 | static cl::opt<bool> |
| 40 | Enabled("stats", cl::desc("Enable statistics output from program")); |
| 41 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 42 | struct StatRecord { |
| 43 | std::string Value; |
| 44 | const char *Name, *Desc; |
| 45 | |
Alkis Evlogimenos | d2c39e9 | 2004-01-06 09:16:02 +0000 | [diff] [blame^] | 46 | StatRecord(const std::string &V, const char *N, const char *D) |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 47 | : Value(V), Name(N), Desc(D) {} |
| 48 | |
| 49 | bool operator<(const StatRecord &SR) const { |
| 50 | return std::strcmp(Name, SR.Name) < 0; |
| 51 | } |
| 52 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 53 | void print(unsigned ValFieldSize, unsigned NameFieldSize, |
| 54 | std::ostream &OS) { |
| 55 | OS << std::string(ValFieldSize-Value.length(), ' ') |
| 56 | << Value << " " << Name |
| 57 | << std::string(NameFieldSize-std::strlen(Name), ' ') |
| 58 | << " - " << Desc << "\n"; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 59 | } |
| 60 | }; |
| 61 | |
| 62 | static std::vector<StatRecord> *AccumStats = 0; |
| 63 | |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 64 | // Print information when destroyed, iff command line option is specified |
| 65 | void StatisticBase::destroy() const { |
| 66 | if (Enabled && hasSomeData()) { |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 67 | if (AccumStats == 0) |
| 68 | AccumStats = new std::vector<StatRecord>(); |
| 69 | |
| 70 | std::ostringstream Out; |
| 71 | printValue(Out); |
| 72 | AccumStats->push_back(StatRecord(Out.str(), Name, Desc)); |
| 73 | } |
| 74 | |
| 75 | if (--NumStats == 0 && AccumStats) { |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 76 | std::ostream *OutStream = GetLibSupportInfoOutputFile(); |
| 77 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 78 | // Figure out how long the biggest Value and Name fields are... |
| 79 | unsigned MaxNameLen = 0, MaxValLen = 0; |
| 80 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) { |
Anand Shukla | c1d4d79 | 2002-10-04 23:56:18 +0000 | [diff] [blame] | 81 | MaxValLen = std::max(MaxValLen, |
| 82 | (unsigned)(*AccumStats)[i].Value.length()); |
| 83 | MaxNameLen = std::max(MaxNameLen, |
| 84 | (unsigned)std::strlen((*AccumStats)[i].Name)); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Sort the fields... |
| 88 | std::stable_sort(AccumStats->begin(), AccumStats->end()); |
| 89 | |
| 90 | // Print out the statistics header... |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 91 | *OutStream << "===" << std::string(73, '-') << "===\n" |
| 92 | << " ... Statistics Collected ...\n" |
| 93 | << "===" << std::string(73, '-') << "===\n\n"; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 94 | |
| 95 | // Print all of the statistics accumulated... |
| 96 | for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 97 | (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 98 | |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 99 | *OutStream << std::endl; // Flush the output stream... |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 100 | |
| 101 | // Free all accumulated statistics... |
| 102 | delete AccumStats; |
| 103 | AccumStats = 0; |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 104 | if (OutStream != &std::cerr && OutStream != &std::cout) |
| 105 | delete OutStream; // Close the file... |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 106 | } |
| 107 | } |