Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 1 | //===-- Statistic.cpp - Easy way to expose stats information --------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 18 | // static Statistic NumInstEliminated("GCSE", "Number of instructions killed"); |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 19 | // |
| 20 | // Later, in the code: ++NumInstEliminated; |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ManagedStatic.h" |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Streams.h" |
Chris Lattner | cf84504 | 2006-12-06 18:20:44 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | a4ca41e | 2003-08-13 21:32:37 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 30 | #include <ostream> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 31 | #include <cstring> |
Chris Lattner | b6d465f | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 34 | // GetLibSupportInfoOutputFile - Return a file stream to print our output on. |
Chris Lattner | b6d465f | 2003-12-14 21:27:33 +0000 | [diff] [blame] | 35 | namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); } |
Chris Lattner | f205fec | 2003-05-09 20:05:44 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 37 | /// -stats - Command line option to cause transformations to emit stats about |
| 38 | /// what they did. |
| 39 | /// |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | static cl::opt<bool> |
| 41 | Enabled("stats", cl::desc("Enable statistics output from program")); |
| 42 | |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | /// StatisticInfo - This class is used in a ManagedStatic so that it is created |
| 46 | /// on demand (when the first statistic is bumped) and destroyed only when |
| 47 | /// llvm_shutdown is called. We print statistics from the destructor. |
| 48 | class StatisticInfo { |
Chris Lattner | 0a36152 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 49 | std::vector<const Statistic*> Stats; |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 50 | public: |
| 51 | ~StatisticInfo(); |
| 52 | |
Chris Lattner | 0a36152 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 53 | void addStatistic(const Statistic *S) { |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 54 | Stats.push_back(S); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 56 | }; |
| 57 | } |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 59 | static ManagedStatic<StatisticInfo> StatInfo; |
| 60 | |
| 61 | |
| 62 | /// RegisterStatistic - The first time a statistic is bumped, this method is |
| 63 | /// called. |
Chris Lattner | 0a36152 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 64 | void Statistic::RegisterStatistic() { |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 65 | // If stats are enabled, inform StatInfo that this statistic should be |
| 66 | // printed. |
| 67 | if (Enabled) |
| 68 | StatInfo->addStatistic(this); |
| 69 | // Remember we have been registered. |
| 70 | Initialized = true; |
| 71 | } |
| 72 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame^] | 73 | namespace { |
| 74 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 75 | struct NameCompare { |
Chris Lattner | 0a36152 | 2006-12-19 23:17:40 +0000 | [diff] [blame] | 76 | bool operator()(const Statistic *LHS, const Statistic *RHS) const { |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 77 | int Cmp = std::strcmp(LHS->getName(), RHS->getName()); |
| 78 | if (Cmp != 0) return Cmp < 0; |
| 79 | |
| 80 | // Secondary key is the description. |
| 81 | return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame^] | 85 | } |
| 86 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 87 | // Print information when destroyed, iff command line option is specified. |
| 88 | StatisticInfo::~StatisticInfo() { |
| 89 | // Statistics not enabled? |
| 90 | if (Stats.empty()) return; |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 92 | // Get the stream to write to. |
| 93 | std::ostream &OutStream = *GetLibSupportInfoOutputFile(); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 95 | // Figure out how long the biggest Value and Name fields are. |
| 96 | unsigned MaxNameLen = 0, MaxValLen = 0; |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 97 | for (size_t i = 0, e = Stats.size(); i != e; ++i) { |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 98 | MaxValLen = std::max(MaxValLen, |
| 99 | (unsigned)utostr(Stats[i]->getValue()).size()); |
| 100 | MaxNameLen = std::max(MaxNameLen, |
| 101 | (unsigned)std::strlen(Stats[i]->getName())); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 103 | |
| 104 | // Sort the fields by name. |
| 105 | std::stable_sort(Stats.begin(), Stats.end(), NameCompare()); |
Chris Lattner | 96ef1b9 | 2002-10-01 22:35:45 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 107 | // Print out the statistics header... |
| 108 | OutStream << "===" << std::string(73, '-') << "===\n" |
| 109 | << " ... Statistics Collected ...\n" |
| 110 | << "===" << std::string(73, '-') << "===\n\n"; |
| 111 | |
| 112 | // Print all of the statistics. |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 113 | for (size_t i = 0, e = Stats.size(); i != e; ++i) { |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 114 | std::string CountStr = utostr(Stats[i]->getValue()); |
| 115 | OutStream << std::string(MaxValLen-CountStr.size(), ' ') |
| 116 | << CountStr << " " << Stats[i]->getName() |
| 117 | << std::string(MaxNameLen-std::strlen(Stats[i]->getName()), ' ') |
| 118 | << " - " << Stats[i]->getDesc() << "\n"; |
| 119 | |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | 975f058 | 2006-12-08 20:00:42 +0000 | [diff] [blame] | 121 | |
| 122 | OutStream << std::endl; // Flush the output stream... |
| 123 | |
| 124 | if (&OutStream != cerr.stream() && &OutStream != cout.stream()) |
| 125 | delete &OutStream; // Close the file. |
Chris Lattner | f3f4fd5 | 2002-05-10 15:36:46 +0000 | [diff] [blame] | 126 | } |