Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.5 KiB

  1. //===-- llvm/ADT/Statistic.h - Easy way to expose stats ---------*- C++ -*-===//
  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 defines the 'Statistic' class, which is designed to be an easy way
  11. // to expose various metrics from passes. These statistics are printed at the
  12. // end of a run (from llvm_shutdown), when the -stats command line option is
  13. // passed 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 NumInstsKilled("gcse", "Number of instructions killed");
  19. //
  20. // Later, in the code: ++NumInstsKilled;
  21. //
  22. // NOTE: Statistics *must* be declared as global variables.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_ADT_STATISTIC_H
  26. #define LLVM_ADT_STATISTIC_H
  27. #include "llvm/Support/Atomic.h"
  28. #include "llvm/Support/Valgrind.h"
  29. namespace llvm {
  30. class raw_ostream;
  31. class Statistic {
  32. public:
  33. const char *Name;
  34. const char *Desc;
  35. volatile llvm::sys::cas_flag Value;
  36. bool Initialized;
  37. llvm::sys::cas_flag getValue() const { return Value; }
  38. const char *getName() const { return Name; }
  39. const char *getDesc() const { return Desc; }
  40. /// construct - This should only be called for non-global statistics.
  41. void construct(const char *name, const char *desc) {
  42. Name = name; Desc = desc;
  43. Value = 0; Initialized = 0;
  44. }
  45. // Allow use of this class as the value itself.
  46. operator unsigned() const { return Value; }
  47. #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
  48. const Statistic &operator=(unsigned Val) {
  49. Value = Val;
  50. return init();
  51. }
  52. const Statistic &operator++() {
  53. // FIXME: This function and all those that follow carefully use an
  54. // atomic operation to update the value safely in the presence of
  55. // concurrent accesses, but not to read the return value, so the
  56. // return value is not thread safe.
  57. sys::AtomicIncrement(&Value);
  58. return init();
  59. }
  60. unsigned operator++(int) {
  61. init();
  62. unsigned OldValue = Value;
  63. sys::AtomicIncrement(&Value);
  64. return OldValue;
  65. }
  66. const Statistic &operator--() {
  67. sys::AtomicDecrement(&Value);
  68. return init();
  69. }
  70. unsigned operator--(int) {
  71. init();
  72. unsigned OldValue = Value;
  73. sys::AtomicDecrement(&Value);
  74. return OldValue;
  75. }
  76. const Statistic &operator+=(const unsigned &V) {
  77. if (!V) return *this;
  78. sys::AtomicAdd(&Value, V);
  79. return init();
  80. }
  81. const Statistic &operator-=(const unsigned &V) {
  82. if (!V) return *this;
  83. sys::AtomicAdd(&Value, -V);
  84. return init();
  85. }
  86. const Statistic &operator*=(const unsigned &V) {
  87. sys::AtomicMul(&Value, V);
  88. return init();
  89. }
  90. const Statistic &operator/=(const unsigned &V) {
  91. sys::AtomicDiv(&Value, V);
  92. return init();
  93. }
  94. #else // Statistics are disabled in release builds.
  95. const Statistic &operator=(unsigned Val) {
  96. return *this;
  97. }
  98. const Statistic &operator++() {
  99. return *this;
  100. }
  101. unsigned operator++(int) {
  102. return 0;
  103. }
  104. const Statistic &operator--() {
  105. return *this;
  106. }
  107. unsigned operator--(int) {
  108. return 0;
  109. }
  110. const Statistic &operator+=(const unsigned &V) {
  111. return *this;
  112. }
  113. const Statistic &operator-=(const unsigned &V) {
  114. return *this;
  115. }
  116. const Statistic &operator*=(const unsigned &V) {
  117. return *this;
  118. }
  119. const Statistic &operator/=(const unsigned &V) {
  120. return *this;
  121. }
  122. #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)
  123. protected:
  124. Statistic &init() {
  125. bool tmp = Initialized;
  126. sys::MemoryFence();
  127. if (!tmp) RegisterStatistic();
  128. TsanHappensAfter(this);
  129. return *this;
  130. }
  131. void RegisterStatistic();
  132. };
  133. // STATISTIC - A macro to make definition of statistics really simple. This
  134. // automatically passes the DEBUG_TYPE of the file into the statistic.
  135. #define STATISTIC(VARNAME, DESC) \
  136. static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }
  137. /// \brief Enable the collection and printing of statistics.
  138. void EnableStatistics();
  139. /// \brief Check if statistics are enabled.
  140. bool AreStatisticsEnabled();
  141. /// \brief Print statistics to the file returned by CreateInfoOutputFile().
  142. void PrintStatistics();
  143. /// \brief Print statistics to the given output stream.
  144. void PrintStatistics(raw_ostream &OS);
  145. } // End llvm namespace
  146. #endif