DebugCounter.h revision 317017
1317017Sdim//===- llvm/Support/DebugCounter.h - Debug counter support ------*- C++ -*-===// 2317017Sdim// 3317017Sdim// The LLVM Compiler Infrastructure 4317017Sdim// 5317017Sdim// This file is distributed under the University of Illinois Open Source 6317017Sdim// License. See LICENSE.TXT for details. 7317017Sdim// 8317017Sdim//===----------------------------------------------------------------------===// 9317017Sdim/// \file 10317017Sdim/// \brief This file provides an implementation of debug counters. Debug 11317017Sdim/// counters are a tool that let you narrow down a miscompilation to a specific 12317017Sdim/// thing happening. 13317017Sdim/// 14317017Sdim/// To give a use case: Imagine you have a file, very large, and you 15317017Sdim/// are trying to understand the minimal transformation that breaks it. Bugpoint 16317017Sdim/// and bisection is often helpful here in narrowing it down to a specific pass, 17317017Sdim/// but it's still a very large file, and a very complicated pass to try to 18317017Sdim/// debug. That is where debug counting steps in. You can instrument the pass 19317017Sdim/// with a debug counter before it does a certain thing, and depending on the 20317017Sdim/// counts, it will either execute that thing or not. The debug counter itself 21317017Sdim/// consists of a skip and a count. Skip is the number of times shouldExecute 22317017Sdim/// needs to be called before it returns true. Count is the number of times to 23317017Sdim/// return true once Skip is 0. So a skip=47, count=2 ,would skip the first 47 24317017Sdim/// executions by returning false from shouldExecute, then execute twice, and 25317017Sdim/// then return false again. 26317017Sdim/// Note that a counter set to a negative number will always execute. 27317017Sdim/// For a concrete example, during predicateinfo creation, the renaming pass 28317017Sdim/// replaces each use with a renamed use. 29317017Sdim//// 30317017Sdim/// If I use DEBUG_COUNTER to create a counter called "predicateinfo", and 31317017Sdim/// variable name RenameCounter, and then instrument this renaming with a debug 32317017Sdim/// counter, like so: 33317017Sdim/// 34317017Sdim/// if (!DebugCounter::shouldExecute(RenameCounter) 35317017Sdim/// <continue or return or whatever not executing looks like> 36317017Sdim/// 37317017Sdim/// Now I can, from the command line, make it rename or not rename certain uses 38317017Sdim/// by setting the skip and count. 39317017Sdim/// So for example 40317017Sdim/// bin/opt -debug-counter=predicateinfo-skip=47,predicateinfo-count=1 41317017Sdim/// will skip renaming the first 47 uses, then rename one, then skip the rest. 42317017Sdim//===----------------------------------------------------------------------===// 43317017Sdim 44317017Sdim#ifndef LLVM_SUPPORT_DEBUGCOUNTER_H 45317017Sdim#define LLVM_SUPPORT_DEBUGCOUNTER_H 46317017Sdim 47317017Sdim#include "llvm/ADT/DenseMap.h" 48317017Sdim#include "llvm/ADT/UniqueVector.h" 49317017Sdim#include "llvm/Support/CommandLine.h" 50317017Sdim#include "llvm/Support/Debug.h" 51317017Sdim#include "llvm/Support/raw_ostream.h" 52317017Sdim#include <string> 53317017Sdim 54317017Sdimnamespace llvm { 55317017Sdim 56317017Sdimclass DebugCounter { 57317017Sdimpublic: 58317017Sdim /// \brief Returns a reference to the singleton instance. 59317017Sdim static DebugCounter &instance(); 60317017Sdim 61317017Sdim // Used by the command line option parser to push a new value it parsed. 62317017Sdim void push_back(const std::string &); 63317017Sdim 64317017Sdim // Register a counter with the specified name. 65317017Sdim // 66317017Sdim // FIXME: Currently, counter registration is required to happen before command 67317017Sdim // line option parsing. The main reason to register counters is to produce a 68317017Sdim // nice list of them on the command line, but i'm not sure this is worth it. 69317017Sdim static unsigned registerCounter(StringRef Name, StringRef Desc) { 70317017Sdim return instance().addCounter(Name, Desc); 71317017Sdim } 72317017Sdim inline static bool shouldExecute(unsigned CounterName) { 73317017Sdim// Compile to nothing when debugging is off 74317017Sdim#ifdef NDEBUG 75317017Sdim return true; 76317017Sdim#else 77317017Sdim auto &Us = instance(); 78317017Sdim auto Result = Us.Counters.find(CounterName); 79317017Sdim if (Result != Us.Counters.end()) { 80317017Sdim auto &CounterPair = Result->second; 81317017Sdim // We only execute while the skip (first) is zero and the count (second) 82317017Sdim // is non-zero. 83317017Sdim // Negative counters always execute. 84317017Sdim if (CounterPair.first < 0) 85317017Sdim return true; 86317017Sdim if (CounterPair.first != 0) { 87317017Sdim --CounterPair.first; 88317017Sdim return false; 89317017Sdim } 90317017Sdim if (CounterPair.second < 0) 91317017Sdim return true; 92317017Sdim if (CounterPair.second != 0) { 93317017Sdim --CounterPair.second; 94317017Sdim return true; 95317017Sdim } 96317017Sdim return false; 97317017Sdim } 98317017Sdim // Didn't find the counter, should we warn? 99317017Sdim return true; 100317017Sdim#endif // NDEBUG 101317017Sdim } 102317017Sdim 103317017Sdim // Return true if a given counter had values set (either programatically or on 104317017Sdim // the command line). This will return true even if those values are 105317017Sdim // currently in a state where the counter will always execute. 106317017Sdim static bool isCounterSet(unsigned ID) { 107317017Sdim return instance().Counters.count(ID); 108317017Sdim } 109317017Sdim 110317017Sdim // Return the skip and count for a counter. This only works for set counters. 111317017Sdim static std::pair<int, int> getCounterValue(unsigned ID) { 112317017Sdim auto &Us = instance(); 113317017Sdim auto Result = Us.Counters.find(ID); 114317017Sdim assert(Result != Us.Counters.end() && "Asking about a non-set counter"); 115317017Sdim return Result->second; 116317017Sdim } 117317017Sdim 118317017Sdim // Set a registered counter to a given value. 119317017Sdim static void setCounterValue(unsigned ID, const std::pair<int, int> &Val) { 120317017Sdim auto &Us = instance(); 121317017Sdim Us.Counters[ID] = Val; 122317017Sdim } 123317017Sdim 124317017Sdim // Dump or print the current counter set. 125317017Sdim LLVM_DUMP_METHOD void dump() { print(dbgs()); } 126317017Sdim 127317017Sdim void print(raw_ostream &OS); 128317017Sdim 129317017Sdim // Get the counter ID for a given named counter, or return 0 if none is found. 130317017Sdim unsigned getCounterId(const std::string &Name) const { 131317017Sdim return RegisteredCounters.idFor(Name); 132317017Sdim } 133317017Sdim 134317017Sdim // Return the number of registered counters. 135317017Sdim unsigned int getNumCounters() const { return RegisteredCounters.size(); } 136317017Sdim 137317017Sdim // Return the name and description of the counter with the given ID. 138317017Sdim std::pair<std::string, std::string> getCounterInfo(unsigned ID) const { 139317017Sdim return std::make_pair(RegisteredCounters[ID], CounterDesc.lookup(ID)); 140317017Sdim } 141317017Sdim 142317017Sdim // Iterate through the registered counters 143317017Sdim typedef UniqueVector<std::string> CounterVector; 144317017Sdim CounterVector::const_iterator begin() const { 145317017Sdim return RegisteredCounters.begin(); 146317017Sdim } 147317017Sdim CounterVector::const_iterator end() const { return RegisteredCounters.end(); } 148317017Sdim 149317017Sdimprivate: 150317017Sdim unsigned addCounter(const std::string &Name, const std::string &Desc) { 151317017Sdim unsigned Result = RegisteredCounters.insert(Name); 152317017Sdim CounterDesc[Result] = Desc; 153317017Sdim return Result; 154317017Sdim } 155317017Sdim DenseMap<unsigned, std::pair<long, long>> Counters; 156317017Sdim DenseMap<unsigned, std::string> CounterDesc; 157317017Sdim CounterVector RegisteredCounters; 158317017Sdim}; 159317017Sdim 160317017Sdim#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \ 161317017Sdim static const unsigned VARNAME = \ 162317017Sdim DebugCounter::registerCounter(COUNTERNAME, DESC); 163317017Sdim 164317017Sdim} // namespace llvm 165317017Sdim#endif 166