File indexing completed on 2026-05-10 08:44:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
0043 #define LLVM_SUPPORT_DEBUGCOUNTER_H
0044
0045 #include "llvm/ADT/ArrayRef.h"
0046 #include "llvm/ADT/DenseMap.h"
0047 #include "llvm/ADT/StringRef.h"
0048 #include "llvm/ADT/UniqueVector.h"
0049 #include "llvm/Support/Debug.h"
0050 #include <string>
0051
0052 namespace llvm {
0053
0054 class raw_ostream;
0055
0056 class DebugCounter {
0057 public:
0058 struct Chunk {
0059 int64_t Begin;
0060 int64_t End;
0061 void print(llvm::raw_ostream &OS);
0062 bool contains(int64_t Idx) { return Idx >= Begin && Idx <= End; }
0063 };
0064
0065 static void printChunks(raw_ostream &OS, ArrayRef<Chunk>);
0066
0067
0068
0069 static bool parseChunks(StringRef Str, SmallVector<Chunk> &Res);
0070
0071
0072 static DebugCounter &instance();
0073
0074
0075 void push_back(const std::string &);
0076
0077
0078
0079
0080
0081
0082 static unsigned registerCounter(StringRef Name, StringRef Desc) {
0083 return instance().addCounter(std::string(Name), std::string(Desc));
0084 }
0085 static bool shouldExecuteImpl(unsigned CounterName);
0086
0087 inline static bool shouldExecute(unsigned CounterName) {
0088 if (!isCountingEnabled())
0089 return true;
0090 return shouldExecuteImpl(CounterName);
0091 }
0092
0093
0094
0095
0096 static bool isCounterSet(unsigned ID) {
0097 return instance().Counters[ID].IsSet;
0098 }
0099
0100 struct CounterState {
0101 int64_t Count;
0102 uint64_t ChunkIdx;
0103 };
0104
0105
0106 static CounterState getCounterState(unsigned ID) {
0107 auto &Us = instance();
0108 auto Result = Us.Counters.find(ID);
0109 assert(Result != Us.Counters.end() && "Asking about a non-set counter");
0110 return {Result->second.Count, Result->second.CurrChunkIdx};
0111 }
0112
0113
0114 static void setCounterState(unsigned ID, CounterState State) {
0115 auto &Us = instance();
0116 auto &Counter = Us.Counters[ID];
0117 Counter.Count = State.Count;
0118 Counter.CurrChunkIdx = State.ChunkIdx;
0119 }
0120
0121
0122 LLVM_DUMP_METHOD void dump() const;
0123
0124 void print(raw_ostream &OS) const;
0125
0126
0127 unsigned getCounterId(const std::string &Name) const {
0128 return RegisteredCounters.idFor(Name);
0129 }
0130
0131
0132 unsigned int getNumCounters() const { return RegisteredCounters.size(); }
0133
0134
0135 std::pair<std::string, std::string> getCounterInfo(unsigned ID) const {
0136 return std::make_pair(RegisteredCounters[ID], Counters.lookup(ID).Desc);
0137 }
0138
0139
0140 typedef UniqueVector<std::string> CounterVector;
0141 CounterVector::const_iterator begin() const {
0142 return RegisteredCounters.begin();
0143 }
0144 CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
0145
0146
0147
0148
0149
0150
0151 static void enableAllCounters() { instance().Enabled = true; }
0152
0153 static bool isCountingEnabled() {
0154
0155 #ifdef NDEBUG
0156 return false;
0157 #else
0158 return instance().Enabled || instance().ShouldPrintCounter;
0159 #endif
0160 }
0161
0162 protected:
0163 unsigned addCounter(const std::string &Name, const std::string &Desc) {
0164 unsigned Result = RegisteredCounters.insert(Name);
0165 Counters[Result] = {};
0166 Counters[Result].Desc = Desc;
0167 return Result;
0168 }
0169
0170 struct CounterInfo {
0171 int64_t Count = 0;
0172 uint64_t CurrChunkIdx = 0;
0173 bool IsSet = false;
0174 std::string Desc;
0175 SmallVector<Chunk> Chunks;
0176 };
0177
0178 DenseMap<unsigned, CounterInfo> Counters;
0179 CounterVector RegisteredCounters;
0180
0181
0182
0183 bool Enabled = false;
0184
0185 bool ShouldPrintCounter = false;
0186
0187 bool BreakOnLast = false;
0188 };
0189
0190 #define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
0191 static const unsigned VARNAME = \
0192 DebugCounter::registerCounter(COUNTERNAME, DESC)
0193
0194 }
0195 #endif