blob: fad61d3e39a4fe696204456fdad98bc21569430c [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Tyler Veness <calcmogul@gmail.com>
3Date: Tue, 11 Jul 2023 22:56:09 -0700
4Subject: [PATCH 29/31] Use C++20 <bit> header
5
6---
7 llvm/include/llvm/ADT/DenseMap.h | 3 +-
8 llvm/include/llvm/ADT/Hashing.h | 35 +--
9 llvm/include/llvm/ADT/bit.h | 287 -------------------------
10 llvm/include/llvm/Support/MathExtras.h | 21 +-
11 4 files changed, 31 insertions(+), 315 deletions(-)
12
13diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
14index e9bd3bfa4a6fe0fa26ff20069bbadc816c8baa65..93b50c9e53af4ea3af5fd0329a8a03bdce659e9d 100644
15--- a/llvm/include/llvm/ADT/DenseMap.h
16+++ b/llvm/include/llvm/ADT/DenseMap.h
17@@ -23,6 +23,7 @@
18 #include "llvm/Support/ReverseIteration.h"
19 #include "llvm/Support/type_traits.h"
20 #include <algorithm>
21+#include <bit>
22 #include <cassert>
23 #include <cstddef>
24 #include <cstring>
25@@ -933,7 +934,7 @@ class SmallDenseMap
26 public:
27 explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
28 if (NumInitBuckets > InlineBuckets)
29- NumInitBuckets = llvm::bit_ceil(NumInitBuckets);
30+ NumInitBuckets = std::bit_ceil(NumInitBuckets);
31 init(NumInitBuckets);
32 }
33
34diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h
35index 781bdb7416392e3f60a1ac3a38fbcf5324b5395f..28934add722f518ae1e9cb9c4a23d2212a47cbdf 100644
36--- a/llvm/include/llvm/ADT/Hashing.h
37+++ b/llvm/include/llvm/ADT/Hashing.h
38@@ -49,6 +49,7 @@
39 #include "llvm/Support/SwapByteOrder.h"
40 #include "llvm/Support/type_traits.h"
41 #include <algorithm>
42+#include <bit>
43 #include <cassert>
44 #include <cstring>
45 #include <optional>
46@@ -224,30 +225,30 @@ inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
47 uint64_t b = fetch64(s + 8);
48 uint64_t c = fetch64(s + len - 8) * k2;
49 uint64_t d = fetch64(s + len - 16) * k0;
50- return hash_16_bytes(llvm::rotr<uint64_t>(a - b, 43) +
51- llvm::rotr<uint64_t>(c ^ seed, 30) + d,
52- a + llvm::rotr<uint64_t>(b ^ k3, 20) - c + len + seed);
53+ return hash_16_bytes(std::rotr<uint64_t>(a - b, 43) +
54+ std::rotr<uint64_t>(c ^ seed, 30) + d,
55+ a + std::rotr<uint64_t>(b ^ k3, 20) - c + len + seed);
56 }
57
58 inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
59 uint64_t z = fetch64(s + 24);
60 uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
61- uint64_t b = llvm::rotr<uint64_t>(a + z, 52);
62- uint64_t c = llvm::rotr<uint64_t>(a, 37);
63+ uint64_t b = std::rotr<uint64_t>(a + z, 52);
64+ uint64_t c = std::rotr<uint64_t>(a, 37);
65 a += fetch64(s + 8);
66- c += llvm::rotr<uint64_t>(a, 7);
67+ c += std::rotr<uint64_t>(a, 7);
68 a += fetch64(s + 16);
69 uint64_t vf = a + z;
70- uint64_t vs = b + llvm::rotr<uint64_t>(a, 31) + c;
71+ uint64_t vs = b + std::rotr<uint64_t>(a, 31) + c;
72 a = fetch64(s + 16) + fetch64(s + len - 32);
73 z = fetch64(s + len - 8);
74- b = llvm::rotr<uint64_t>(a + z, 52);
75- c = llvm::rotr<uint64_t>(a, 37);
76+ b = std::rotr<uint64_t>(a + z, 52);
77+ c = std::rotr<uint64_t>(a, 37);
78 a += fetch64(s + len - 24);
79- c += llvm::rotr<uint64_t>(a, 7);
80+ c += std::rotr<uint64_t>(a, 7);
81 a += fetch64(s + len - 16);
82 uint64_t wf = a + z;
83- uint64_t ws = b + llvm::rotr<uint64_t>(a, 31) + c;
84+ uint64_t ws = b + std::rotr<uint64_t>(a, 31) + c;
85 uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
86 return shift_mix((seed ^ (r * k0)) + vs) * k2;
87 }
88@@ -280,7 +281,7 @@ struct hash_state {
89 hash_state state = {0,
90 seed,
91 hash_16_bytes(seed, k1),
92- llvm::rotr<uint64_t>(seed ^ k1, 49),
93+ std::rotr<uint64_t>(seed ^ k1, 49),
94 seed * k1,
95 shift_mix(seed),
96 0};
97@@ -294,10 +295,10 @@ struct hash_state {
98 static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
99 a += fetch64(s);
100 uint64_t c = fetch64(s + 24);
101- b = llvm::rotr<uint64_t>(b + a + c, 21);
102+ b = std::rotr<uint64_t>(b + a + c, 21);
103 uint64_t d = a;
104 a += fetch64(s + 8) + fetch64(s + 16);
105- b += llvm::rotr<uint64_t>(a, 44) + d;
106+ b += std::rotr<uint64_t>(a, 44) + d;
107 a += c;
108 }
109
110@@ -305,11 +306,11 @@ struct hash_state {
111 /// We mix all 64 bytes even when the chunk length is smaller, but we
112 /// record the actual length.
113 void mix(const char *s) {
114- h0 = llvm::rotr<uint64_t>(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
115- h1 = llvm::rotr<uint64_t>(h1 + h4 + fetch64(s + 48), 42) * k1;
116+ h0 = std::rotr<uint64_t>(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
117+ h1 = std::rotr<uint64_t>(h1 + h4 + fetch64(s + 48), 42) * k1;
118 h0 ^= h6;
119 h1 += h3 + fetch64(s + 40);
120- h2 = llvm::rotr<uint64_t>(h2 + h5, 33) * k1;
121+ h2 = std::rotr<uint64_t>(h2 + h5, 33) * k1;
122 h3 = h4 * k1;
123 h4 = h0 + h5;
124 mix_32_bytes(s, h3, h4);
125diff --git a/llvm/include/llvm/ADT/bit.h b/llvm/include/llvm/ADT/bit.h
126index 2840c5f608d3ea896e1867dd4710685da9572f2d..0a4a3634820efbc0a8ca675e3ad7c98469260d0b 100644
127--- a/llvm/include/llvm/ADT/bit.h
128+++ b/llvm/include/llvm/ADT/bit.h
129@@ -27,18 +27,6 @@
130 #include <cstdlib> // for _byteswap_{ushort,ulong,uint64}
131 #endif
132
133-#ifdef _MSC_VER
134-// Declare these intrinsics manually rather including intrin.h. It's very
135-// expensive, and bit.h is popular via MathExtras.h.
136-// #include <intrin.h>
137-extern "C" {
138-unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
139-unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
140-unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
141-unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
142-}
143-#endif
144-
145 namespace llvm {
146
147 // This implementation of bit_cast is different from the C++20 one in two ways:
148@@ -106,281 +94,6 @@ template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
149 }
150 }
151
152-template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
153-[[nodiscard]] constexpr inline bool has_single_bit(T Value) noexcept {
154- return (Value != 0) && ((Value & (Value - 1)) == 0);
155-}
156-
157-namespace detail {
158-template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
159- static unsigned count(T Val) {
160- if (!Val)
161- return std::numeric_limits<T>::digits;
162- if (Val & 0x1)
163- return 0;
164-
165- // Bisection method.
166- unsigned ZeroBits = 0;
167- T Shift = std::numeric_limits<T>::digits >> 1;
168- T Mask = std::numeric_limits<T>::max() >> Shift;
169- while (Shift) {
170- if ((Val & Mask) == 0) {
171- Val >>= Shift;
172- ZeroBits |= Shift;
173- }
174- Shift >>= 1;
175- Mask >>= Shift;
176- }
177- return ZeroBits;
178- }
179-};
180-
181-#if defined(__GNUC__) || defined(_MSC_VER)
182-template <typename T> struct TrailingZerosCounter<T, 4> {
183- static unsigned count(T Val) {
184- if (Val == 0)
185- return 32;
186-
187-#if __has_builtin(__builtin_ctz) || defined(__GNUC__)
188- return __builtin_ctz(Val);
189-#elif defined(_MSC_VER)
190- unsigned long Index;
191- _BitScanForward(&Index, Val);
192- return Index;
193-#endif
194- }
195-};
196-
197-#if !defined(_MSC_VER) || defined(_M_X64)
198-template <typename T> struct TrailingZerosCounter<T, 8> {
199- static unsigned count(T Val) {
200- if (Val == 0)
201- return 64;
202-
203-#if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
204- return __builtin_ctzll(Val);
205-#elif defined(_MSC_VER)
206- unsigned long Index;
207- _BitScanForward64(&Index, Val);
208- return Index;
209-#endif
210- }
211-};
212-#endif
213-#endif
214-} // namespace detail
215-
216-/// Count number of 0's from the least significant bit to the most
217-/// stopping at the first 1.
218-///
219-/// Only unsigned integral types are allowed.
220-///
221-/// Returns std::numeric_limits<T>::digits on an input of 0.
222-template <typename T> [[nodiscard]] int countr_zero(T Val) {
223- static_assert(std::is_unsigned_v<T>,
224- "Only unsigned integral types are allowed.");
225- return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val);
226-}
227-
228-namespace detail {
229-template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
230- static unsigned count(T Val) {
231- if (!Val)
232- return std::numeric_limits<T>::digits;
233-
234- // Bisection method.
235- unsigned ZeroBits = 0;
236- for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
237- T Tmp = Val >> Shift;
238- if (Tmp)
239- Val = Tmp;
240- else
241- ZeroBits |= Shift;
242- }
243- return ZeroBits;
244- }
245-};
246-
247-#if defined(__GNUC__) || defined(_MSC_VER)
248-template <typename T> struct LeadingZerosCounter<T, 4> {
249- static unsigned count(T Val) {
250- if (Val == 0)
251- return 32;
252-
253-#if __has_builtin(__builtin_clz) || defined(__GNUC__)
254- return __builtin_clz(Val);
255-#elif defined(_MSC_VER)
256- unsigned long Index;
257- _BitScanReverse(&Index, Val);
258- return Index ^ 31;
259-#endif
260- }
261-};
262-
263-#if !defined(_MSC_VER) || defined(_M_X64)
264-template <typename T> struct LeadingZerosCounter<T, 8> {
265- static unsigned count(T Val) {
266- if (Val == 0)
267- return 64;
268-
269-#if __has_builtin(__builtin_clzll) || defined(__GNUC__)
270- return __builtin_clzll(Val);
271-#elif defined(_MSC_VER)
272- unsigned long Index;
273- _BitScanReverse64(&Index, Val);
274- return Index ^ 63;
275-#endif
276- }
277-};
278-#endif
279-#endif
280-} // namespace detail
281-
282-/// Count number of 0's from the most significant bit to the least
283-/// stopping at the first 1.
284-///
285-/// Only unsigned integral types are allowed.
286-///
287-/// Returns std::numeric_limits<T>::digits on an input of 0.
288-template <typename T> [[nodiscard]] int countl_zero(T Val) {
289- static_assert(std::is_unsigned_v<T>,
290- "Only unsigned integral types are allowed.");
291- return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val);
292-}
293-
294-/// Count the number of ones from the most significant bit to the first
295-/// zero bit.
296-///
297-/// Ex. countl_one(0xFF0FFF00) == 8.
298-/// Only unsigned integral types are allowed.
299-///
300-/// Returns std::numeric_limits<T>::digits on an input of all ones.
301-template <typename T> [[nodiscard]] int countl_one(T Value) {
302- static_assert(std::is_unsigned_v<T>,
303- "Only unsigned integral types are allowed.");
304- return llvm::countl_zero<T>(~Value);
305-}
306-
307-/// Count the number of ones from the least significant bit to the first
308-/// zero bit.
309-///
310-/// Ex. countr_one(0x00FF00FF) == 8.
311-/// Only unsigned integral types are allowed.
312-///
313-/// Returns std::numeric_limits<T>::digits on an input of all ones.
314-template <typename T> [[nodiscard]] int countr_one(T Value) {
315- static_assert(std::is_unsigned_v<T>,
316- "Only unsigned integral types are allowed.");
317- return llvm::countr_zero<T>(~Value);
318-}
319-
320-/// Returns the number of bits needed to represent Value if Value is nonzero.
321-/// Returns 0 otherwise.
322-///
323-/// Ex. bit_width(5) == 3.
324-template <typename T> [[nodiscard]] int bit_width(T Value) {
325- static_assert(std::is_unsigned_v<T>,
326- "Only unsigned integral types are allowed.");
327- return std::numeric_limits<T>::digits - llvm::countl_zero(Value);
328-}
329-
330-/// Returns the largest integral power of two no greater than Value if Value is
331-/// nonzero. Returns 0 otherwise.
332-///
333-/// Ex. bit_floor(5) == 4.
334-template <typename T> [[nodiscard]] T bit_floor(T Value) {
335- static_assert(std::is_unsigned_v<T>,
336- "Only unsigned integral types are allowed.");
337- if (!Value)
338- return 0;
339- return T(1) << (llvm::bit_width(Value) - 1);
340-}
341-
342-/// Returns the smallest integral power of two no smaller than Value if Value is
343-/// nonzero. Returns 1 otherwise.
344-///
345-/// Ex. bit_ceil(5) == 8.
346-///
347-/// The return value is undefined if the input is larger than the largest power
348-/// of two representable in T.
349-template <typename T> [[nodiscard]] T bit_ceil(T Value) {
350- static_assert(std::is_unsigned_v<T>,
351- "Only unsigned integral types are allowed.");
352- if (Value < 2)
353- return 1;
354- return T(1) << llvm::bit_width<T>(Value - 1u);
355-}
356-
357-namespace detail {
358-template <typename T, std::size_t SizeOfT> struct PopulationCounter {
359- static int count(T Value) {
360- // Generic version, forward to 32 bits.
361- static_assert(SizeOfT <= 4, "Not implemented!");
362-#if defined(__GNUC__)
363- return (int)__builtin_popcount(Value);
364-#else
365- uint32_t v = Value;
366- v = v - ((v >> 1) & 0x55555555);
367- v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
368- return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
369-#endif
370- }
371-};
372-
373-template <typename T> struct PopulationCounter<T, 8> {
374- static int count(T Value) {
375-#if defined(__GNUC__)
376- return (int)__builtin_popcountll(Value);
377-#else
378- uint64_t v = Value;
379- v = v - ((v >> 1) & 0x5555555555555555ULL);
380- v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
381- v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
382- return int((uint64_t)(v * 0x0101010101010101ULL) >> 56);
383-#endif
384- }
385-};
386-} // namespace detail
387-
388-/// Count the number of set bits in a value.
389-/// Ex. popcount(0xF000F000) = 8
390-/// Returns 0 if the word is zero.
391-template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
392-[[nodiscard]] inline int popcount(T Value) noexcept {
393- return detail::PopulationCounter<T, sizeof(T)>::count(Value);
394-}
395-
396-// Forward-declare rotr so that rotl can use it.
397-template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
398-[[nodiscard]] constexpr T rotr(T V, int R);
399-
400-template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
401-[[nodiscard]] constexpr T rotl(T V, int R) {
402- unsigned N = std::numeric_limits<T>::digits;
403-
404- R = R % N;
405- if (!R)
406- return V;
407-
408- if (R < 0)
409- return llvm::rotr(V, -R);
410-
411- return (V << R) | (V >> (N - R));
412-}
413-
414-template <typename T, typename> [[nodiscard]] constexpr T rotr(T V, int R) {
415- unsigned N = std::numeric_limits<T>::digits;
416-
417- R = R % N;
418- if (!R)
419- return V;
420-
421- if (R < 0)
422- return llvm::rotl(V, -R);
423-
424- return (V >> R) | (V << (N - R));
425-}
426-
427 } // namespace llvm
428
429 #endif
430diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
431index b82d9883c41008dcbbd933709c6e854ad74c5b58..5f034b694989d8ef24e0b249abd12a5c20146b97 100644
432--- a/llvm/include/llvm/Support/MathExtras.h
433+++ b/llvm/include/llvm/Support/MathExtras.h
434@@ -15,6 +15,7 @@
435
436 #include "llvm/ADT/bit.h"
437 #include "llvm/Support/Compiler.h"
438+#include <bit>
439 #include <cassert>
440 #include <climits>
441 #include <cstdint>
442@@ -235,12 +236,12 @@ constexpr inline bool isShiftedMask_64(uint64_t Value) {
443 /// Return true if the argument is a power of two > 0.
444 /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
445 constexpr inline bool isPowerOf2_32(uint32_t Value) {
446- return llvm::has_single_bit(Value);
447+ return std::has_single_bit(Value);
448 }
449
450 /// Return true if the argument is a power of two > 0 (64 bit edition.)
451 constexpr inline bool isPowerOf2_64(uint64_t Value) {
452- return llvm::has_single_bit(Value);
453+ return std::has_single_bit(Value);
454 }
455
456 /// Return true if the argument contains a non-empty sequence of ones with the
457@@ -252,8 +253,8 @@ inline bool isShiftedMask_32(uint32_t Value, unsigned &MaskIdx,
458 unsigned &MaskLen) {
459 if (!isShiftedMask_32(Value))
460 return false;
461- MaskIdx = llvm::countr_zero(Value);
462- MaskLen = llvm::popcount(Value);
463+ MaskIdx = std::countr_zero(Value);
464+ MaskLen = std::popcount(Value);
465 return true;
466 }
467
468@@ -265,8 +266,8 @@ inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx,
469 unsigned &MaskLen) {
470 if (!isShiftedMask_64(Value))
471 return false;
472- MaskIdx = llvm::countr_zero(Value);
473- MaskLen = llvm::popcount(Value);
474+ MaskIdx = std::countr_zero(Value);
475+ MaskLen = std::popcount(Value);
476 return true;
477 }
478
479@@ -284,26 +285,26 @@ template <> constexpr inline size_t CTLog2<1>() { return 0; }
480 /// (32 bit edition.)
481 /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
482 inline unsigned Log2_32(uint32_t Value) {
483- return static_cast<unsigned>(31 - llvm::countl_zero(Value));
484+ return static_cast<unsigned>(31 - std::countl_zero(Value));
485 }
486
487 /// Return the floor log base 2 of the specified value, -1 if the value is zero.
488 /// (64 bit edition.)
489 inline unsigned Log2_64(uint64_t Value) {
490- return static_cast<unsigned>(63 - llvm::countl_zero(Value));
491+ return static_cast<unsigned>(63 - std::countl_zero(Value));
492 }
493
494 /// Return the ceil log base 2 of the specified value, 32 if the value is zero.
495 /// (32 bit edition).
496 /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
497 inline unsigned Log2_32_Ceil(uint32_t Value) {
498- return static_cast<unsigned>(32 - llvm::countl_zero(Value - 1));
499+ return static_cast<unsigned>(32 - std::countl_zero(Value - 1));
500 }
501
502 /// Return the ceil log base 2 of the specified value, 64 if the value is zero.
503 /// (64 bit edition.)
504 inline unsigned Log2_64_Ceil(uint64_t Value) {
505- return static_cast<unsigned>(64 - llvm::countl_zero(Value - 1));
506+ return static_cast<unsigned>(64 - std::countl_zero(Value - 1));
507 }
508
509 /// A and B are either alignments or offsets. Return the minimum alignment that