zpu: wip - add pass to convert registers to stack slots
[llvm/zpu.git] / lib / Support / Statistic.cpp
blobe32ab74a2d4c459eeb3ab6fcccbc3bc19e803b01
1 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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.
15 // This is useful for reporting information like the number of instructions
16 // simplified, optimized or removed by various transformations, like this:
18 // static Statistic NumInstEliminated("GCSE", "Number of instructions killed");
20 // Later, in the code: ++NumInstEliminated;
22 //===----------------------------------------------------------------------===//
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Mutex.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include <algorithm>
32 #include <cstring>
33 using namespace llvm;
35 // CreateInfoOutputFile - Return a file stream to print our output on.
36 namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
38 /// -stats - Command line option to cause transformations to emit stats about
39 /// what they did.
40 ///
41 static cl::opt<bool>
42 Enabled("stats", cl::desc("Enable statistics output from program"));
45 namespace {
46 /// StatisticInfo - This class is used in a ManagedStatic so that it is created
47 /// on demand (when the first statistic is bumped) and destroyed only when
48 /// llvm_shutdown is called. We print statistics from the destructor.
49 class StatisticInfo {
50 std::vector<const Statistic*> Stats;
51 friend void llvm::PrintStatistics();
52 friend void llvm::PrintStatistics(raw_ostream &OS);
53 public:
54 ~StatisticInfo();
56 void addStatistic(const Statistic *S) {
57 Stats.push_back(S);
62 static ManagedStatic<StatisticInfo> StatInfo;
63 static ManagedStatic<sys::SmartMutex<true> > StatLock;
65 /// RegisterStatistic - The first time a statistic is bumped, this method is
66 /// called.
67 void Statistic::RegisterStatistic() {
68 // If stats are enabled, inform StatInfo that this statistic should be
69 // printed.
70 sys::SmartScopedLock<true> Writer(*StatLock);
71 if (!Initialized) {
72 if (Enabled)
73 StatInfo->addStatistic(this);
75 sys::MemoryFence();
76 // Remember we have been registered.
77 Initialized = true;
81 namespace {
83 struct NameCompare {
84 bool operator()(const Statistic *LHS, const Statistic *RHS) const {
85 int Cmp = std::strcmp(LHS->getName(), RHS->getName());
86 if (Cmp != 0) return Cmp < 0;
88 // Secondary key is the description.
89 return std::strcmp(LHS->getDesc(), RHS->getDesc()) < 0;
95 // Print information when destroyed, iff command line option is specified.
96 StatisticInfo::~StatisticInfo() {
97 llvm::PrintStatistics();
100 void llvm::EnableStatistics() {
101 Enabled.setValue(true);
104 void llvm::PrintStatistics(raw_ostream &OS) {
105 StatisticInfo &Stats = *StatInfo;
107 // Figure out how long the biggest Value and Name fields are.
108 unsigned MaxNameLen = 0, MaxValLen = 0;
109 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
110 MaxValLen = std::max(MaxValLen,
111 (unsigned)utostr(Stats.Stats[i]->getValue()).size());
112 MaxNameLen = std::max(MaxNameLen,
113 (unsigned)std::strlen(Stats.Stats[i]->getName()));
116 // Sort the fields by name.
117 std::stable_sort(Stats.Stats.begin(), Stats.Stats.end(), NameCompare());
119 // Print out the statistics header...
120 OS << "===" << std::string(73, '-') << "===\n"
121 << " ... Statistics Collected ...\n"
122 << "===" << std::string(73, '-') << "===\n\n";
124 // Print all of the statistics.
125 for (size_t i = 0, e = Stats.Stats.size(); i != e; ++i) {
126 std::string CountStr = utostr(Stats.Stats[i]->getValue());
127 OS << std::string(MaxValLen-CountStr.size(), ' ')
128 << CountStr << " " << Stats.Stats[i]->getName()
129 << std::string(MaxNameLen-std::strlen(Stats.Stats[i]->getName()), ' ')
130 << " - " << Stats.Stats[i]->getDesc() << "\n";
133 OS << '\n'; // Flush the output stream.
134 OS.flush();
138 void llvm::PrintStatistics() {
139 StatisticInfo &Stats = *StatInfo;
141 // Statistics not enabled?
142 if (Stats.Stats.empty()) return;
144 // Get the stream to write to.
145 raw_ostream &OutStream = *CreateInfoOutputFile();
146 PrintStatistics(OutStream);
147 delete &OutStream; // Close the file.