Discriminator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- llvm/Support/Discriminator.h -- Discriminator Utils ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the constants and utility functions for discriminators.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_DISCRIMINATOR_H
  18. #define LLVM_SUPPORT_DISCRIMINATOR_H
  19. #include "llvm/Support/Error.h"
  20. #include <assert.h>
  21. // Utility functions for encoding / decoding discriminators.
  22. /// With a given unsigned int \p U, use up to 13 bits to represent it.
  23. /// old_bit 1~5 --> new_bit 1~5
  24. /// old_bit 6~12 --> new_bit 7~13
  25. /// new_bit_6 is 0 if higher bits (7~13) are all 0
  26. static inline unsigned getPrefixEncodingFromUnsigned(unsigned U) {
  27. U &= 0xfff;
  28. return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
  29. }
  30. /// Reverse transformation as getPrefixEncodingFromUnsigned.
  31. static inline unsigned getUnsignedFromPrefixEncoding(unsigned U) {
  32. if (U & 1)
  33. return 0;
  34. U >>= 1;
  35. return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
  36. }
  37. /// Returns the next component stored in discriminator.
  38. static inline unsigned getNextComponentInDiscriminator(unsigned D) {
  39. if ((D & 1) == 0)
  40. return D >> ((D & 0x40) ? 14 : 7);
  41. else
  42. return D >> 1;
  43. }
  44. static inline unsigned encodeComponent(unsigned C) {
  45. return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
  46. }
  47. static inline unsigned encodingBits(unsigned C) {
  48. return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
  49. }
  50. // Some constants used in FS Discriminators.
  51. //
  52. namespace llvm {
  53. namespace sampleprof {
  54. enum FSDiscriminatorPass {
  55. Base = 0,
  56. Pass0 = 0,
  57. Pass1 = 1,
  58. Pass2 = 2,
  59. Pass3 = 3,
  60. Pass4 = 4,
  61. PassLast = 4,
  62. };
  63. } // namespace sampleprof
  64. using namespace sampleprof;
  65. // The number of bits reserved for the base discrimininator. The base
  66. // discriminaitor starts from bit 0.
  67. static const unsigned BaseDiscriminatorBitWidth = 8;
  68. // The number of bits reserved for each FS discriminator pass.
  69. static const unsigned FSDiscriminatorBitWidth = 6;
  70. // Return the number of FS passes, excluding the pass adding the base
  71. // discriminators.
  72. // The number of passes for FS discriminators. Note that the total
  73. // number of discriminaitor bits, i.e.
  74. // BaseDiscriminatorBitWidth
  75. // + FSDiscriminatorBitWidth * getNumFSPasses()
  76. // needs to fit in an unsigned int type.
  77. static inline unsigned getNumFSPasses() {
  78. return static_cast<unsigned>(FSDiscriminatorPass::PassLast);
  79. }
  80. // Return the ending bit for FSPass P.
  81. static inline unsigned getFSPassBitEnd(FSDiscriminatorPass P) {
  82. unsigned I = static_cast<unsigned>(P);
  83. assert(I <= getNumFSPasses() && "Invalid FS discriminator pass number.");
  84. return BaseDiscriminatorBitWidth + I * FSDiscriminatorBitWidth - 1;
  85. }
  86. // Return the begining bit for FSPass P.
  87. static inline unsigned getFSPassBitBegin(FSDiscriminatorPass P) {
  88. if (P == FSDiscriminatorPass::Base)
  89. return 0;
  90. unsigned I = static_cast<unsigned>(P);
  91. assert(I <= getNumFSPasses() && "Invalid FS discriminator pass number.");
  92. return getFSPassBitEnd(static_cast<FSDiscriminatorPass>(I - 1)) + 1;
  93. }
  94. // Return the beginning bit for the last FSPass.
  95. static inline int getLastFSPassBitBegin() {
  96. return getFSPassBitBegin(static_cast<FSDiscriminatorPass>(getNumFSPasses()));
  97. }
  98. // Return the ending bit for the last FSPass.
  99. static inline unsigned getLastFSPassBitEnd() {
  100. return getFSPassBitEnd(static_cast<FSDiscriminatorPass>(getNumFSPasses()));
  101. }
  102. // Return the beginning bit for the base (first) FSPass.
  103. static inline unsigned getBaseFSBitBegin() { return 0; }
  104. // Return the ending bit for the base (first) FSPass.
  105. static inline unsigned getBaseFSBitEnd() {
  106. return BaseDiscriminatorBitWidth - 1;
  107. }
  108. // Set bits in range of [0 .. n] to 1. Used in FS Discriminators.
  109. static inline unsigned getN1Bits(int N) {
  110. // Work around the g++ bug that folding "(1U << (N + 1)) - 1" to 0.
  111. if (N == 31)
  112. return 0xFFFFFFFF;
  113. assert((N < 32) && "N is invalid");
  114. return (1U << (N + 1)) - 1;
  115. }
  116. } // namespace llvm
  117. #endif /* LLVM_SUPPORT_DISCRIMINATOR_H */
  118. #ifdef __GNUC__
  119. #pragma GCC diagnostic pop
  120. #endif