1 //===---- llvm/Support/Discriminator.h -- Discriminator Utils ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the constants and utility functions for discriminators.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_DISCRIMINATOR_H
14 #define LLVM_SUPPORT_DISCRIMINATOR_H
15 
16 // Utility functions for encoding / decoding discriminators.
17 /// With a given unsigned int \p U, use up to 13 bits to represent it.
18 /// old_bit 1~5  --> new_bit 1~5
19 /// old_bit 6~12 --> new_bit 7~13
20 /// new_bit_6 is 0 if higher bits (7~13) are all 0
getPrefixEncodingFromUnsigned(unsigned U)21 static inline unsigned getPrefixEncodingFromUnsigned(unsigned U) {
22   U &= 0xfff;
23   return U > 0x1f ? (((U & 0xfe0) << 1) | (U & 0x1f) | 0x20) : U;
24 }
25 
26 /// Reverse transformation as getPrefixEncodingFromUnsigned.
getUnsignedFromPrefixEncoding(unsigned U)27 static inline unsigned getUnsignedFromPrefixEncoding(unsigned U) {
28   if (U & 1)
29     return 0;
30   U >>= 1;
31   return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
32 }
33 
34 /// Returns the next component stored in discriminator.
getNextComponentInDiscriminator(unsigned D)35 static inline unsigned getNextComponentInDiscriminator(unsigned D) {
36   if ((D & 1) == 0)
37     return D >> ((D & 0x40) ? 14 : 7);
38   else
39     return D >> 1;
40 }
41 
encodeComponent(unsigned C)42 static inline unsigned encodeComponent(unsigned C) {
43   return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
44 }
45 
encodingBits(unsigned C)46 static inline unsigned encodingBits(unsigned C) {
47   return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
48 }
49 
50 // Some constants used in FS Discriminators.
51 #define BASE_DIS_BIT_BEG 0
52 #define BASE_DIS_BIT_END 7
53 
54 #define PASS_1_DIS_BIT_BEG 8
55 #define PASS_1_DIS_BIT_END 13
56 
57 #define PASS_2_DIS_BIT_BEG 14
58 #define PASS_2_DIS_BIT_END 19
59 
60 #define PASS_3_DIS_BIT_BEG 20
61 #define PASS_3_DIS_BIT_END 25
62 
63 #define PASS_LAST_DIS_BIT_BEG 26
64 #define PASS_LAST_DIS_BIT_END 31
65 
66 // Set bits range [0 .. n] to 1. Used in FS Discriminators.
getN1Bits(int N)67 static inline unsigned getN1Bits(int N) {
68   if (N >= 31)
69     return 0xFFFFFFFF;
70   return (1 << (N + 1)) - 1;
71 }
72 
73 #endif /* LLVM_SUPPORT_DISCRIMINATOR_H */
74