Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:29

0001 //===---- llvm/Support/Discriminator.h -- Discriminator Utils ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the constants and utility functions for discriminators.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_DISCRIMINATOR_H
0014 #define LLVM_SUPPORT_DISCRIMINATOR_H
0015 
0016 #include "llvm/Support/Error.h"
0017 #include <assert.h>
0018 
0019 // Utility functions for encoding / decoding discriminators.
0020 /// With a given unsigned int \p U, use up to 13 bits to represent it.
0021 /// old_bit 1~5  --> new_bit 1~5
0022 /// old_bit 6~12 --> new_bit 7~13
0023 /// new_bit_6 is 0 if higher bits (7~13) are all 0
0024 static inline unsigned getPrefixEncodingFromUnsigned(unsigned U) {
0025   U &= 0xfff;
0026   return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
0027 }
0028 
0029 /// Reverse transformation as getPrefixEncodingFromUnsigned.
0030 static inline unsigned getUnsignedFromPrefixEncoding(unsigned U) {
0031   if (U & 1)
0032     return 0;
0033   U >>= 1;
0034   return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
0035 }
0036 
0037 /// Returns the next component stored in discriminator.
0038 static inline unsigned getNextComponentInDiscriminator(unsigned D) {
0039   if ((D & 1) == 0)
0040     return D >> ((D & 0x40) ? 14 : 7);
0041   else
0042     return D >> 1;
0043 }
0044 
0045 static inline unsigned encodeComponent(unsigned C) {
0046   return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
0047 }
0048 
0049 static inline unsigned encodingBits(unsigned C) {
0050   return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
0051 }
0052 
0053 // Some constants used in FS Discriminators.
0054 //
0055 namespace llvm {
0056 namespace sampleprof {
0057 enum FSDiscriminatorPass {
0058   Base = 0,
0059   Pass0 = 0,
0060   Pass1 = 1,
0061   Pass2 = 2,
0062   Pass3 = 3,
0063   Pass4 = 4,
0064   PassLast = 4,
0065 };
0066 } // namespace sampleprof
0067 
0068 // The number of bits reserved for the base discrimininator. The base
0069 // discriminaitor starts from bit 0.
0070 static const unsigned BaseDiscriminatorBitWidth = 8;
0071 
0072 // The number of bits reserved for each FS discriminator pass.
0073 static const unsigned FSDiscriminatorBitWidth = 6;
0074 
0075 // Return the number of FS passes, excluding the pass adding the base
0076 // discriminators.
0077 // The number of passes for FS discriminators. Note that the total
0078 // number of discriminaitor bits, i.e.
0079 // BaseDiscriminatorBitWidth
0080 //  + FSDiscriminatorBitWidth * getNumFSPasses()
0081 // needs to fit in an unsigned int type.
0082 static inline unsigned getNumFSPasses() {
0083   return static_cast<unsigned>(sampleprof::FSDiscriminatorPass::PassLast);
0084 }
0085 
0086 // Return the ending bit for FSPass P.
0087 static inline unsigned getFSPassBitEnd(sampleprof::FSDiscriminatorPass P) {
0088   unsigned I = static_cast<unsigned>(P);
0089   assert(I <= getNumFSPasses() && "Invalid FS discriminator pass number.");
0090   return BaseDiscriminatorBitWidth + I * FSDiscriminatorBitWidth - 1;
0091 }
0092 
0093 // Return the begining bit for FSPass P.
0094 static inline unsigned getFSPassBitBegin(sampleprof::FSDiscriminatorPass P) {
0095   if (P == sampleprof::FSDiscriminatorPass::Base)
0096     return 0;
0097   unsigned I = static_cast<unsigned>(P);
0098   assert(I <= getNumFSPasses() && "Invalid FS discriminator pass number.");
0099   return getFSPassBitEnd(static_cast<sampleprof::FSDiscriminatorPass>(I - 1)) +
0100          1;
0101 }
0102 
0103 // Return the beginning bit for the last FSPass.
0104 static inline int getLastFSPassBitBegin() {
0105   return getFSPassBitBegin(
0106       static_cast<sampleprof::FSDiscriminatorPass>(getNumFSPasses()));
0107 }
0108 
0109 // Return the ending bit for the last FSPass.
0110 static inline unsigned getLastFSPassBitEnd() {
0111   return getFSPassBitEnd(
0112       static_cast<sampleprof::FSDiscriminatorPass>(getNumFSPasses()));
0113 }
0114 
0115 // Return the beginning bit for the base (first) FSPass.
0116 static inline unsigned getBaseFSBitBegin() { return 0; }
0117 
0118 // Return the ending bit for the base (first) FSPass.
0119 static inline unsigned getBaseFSBitEnd() {
0120   return BaseDiscriminatorBitWidth - 1;
0121 }
0122 
0123 // Set bits in range of [0 .. n] to 1. Used in FS Discriminators.
0124 static inline unsigned getN1Bits(int N) {
0125   // Work around the g++ bug that folding "(1U << (N + 1)) - 1" to 0.
0126   if (N == 31)
0127     return 0xFFFFFFFF;
0128   assert((N < 32) && "N is invalid");
0129   return (1U << (N + 1)) - 1;
0130 }
0131 
0132 } // namespace llvm
0133 
0134 #endif /* LLVM_SUPPORT_DISCRIMINATOR_H */