Skip to content

Commit f9584fb

Browse files
committed
[clang][x86] Add constexpr support for all remaining BMI1 intrinsics
BEXTR/TZCNT are already handled, so we just need to tag the rest of the intrinsics, which are all expanded to generic patterns.
1 parent 271dc4a commit f9584fb

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,13 @@ X86 Support
518518
* Supported MINMAX intrinsics of ``*_(mask(z)))_minmax(ne)_p[s|d|h|bh]`` and
519519
``*_(mask(z)))_minmax_s[s|d|h]``.
520520

521-
- The following bit manipulation intrinsics can now be used in constant expressions:
522-
all lzcnt intrinsics in lzcntintrin.h
523-
all bextr intrinsics in bmiintrin.h
524-
all tzcnt intrinsics in bmiintrin.h
525-
all bzhi intrinsics in bmi2intrin.h
526-
all pdep intrinsics in bmi2intrin.h
527-
all pext intrinsics in bmi2intrin.h
528-
all intrinsics in tbmintrin.h
521+
- All intrinsics in lzcntintrin.h can now be used in constant expressions.
522+
523+
- All intrinsics in bmiintrin.h can now be used in constant expressions.
524+
525+
- All bzhi/pdep/pext intrinsics in bmi2intrin.h can now be used in constant expressions.
526+
527+
- All intrinsics in tbmintrin.h can now be used in constant expressions.
529528

530529
Arm and AArch64 Support
531530
^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Headers/bmiintrin.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,13 @@ _mm_tzcnt_64(unsigned long long __X)
169169
#if !defined(__SCE__) || __has_feature(modules) || defined(__BMI__)
170170

171171
/* Define the default attributes for the functions in this file. */
172-
#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
173-
174172
#if defined(__cplusplus) && (__cplusplus >= 201103L)
175-
#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS constexpr
173+
#define __DEFAULT_FN_ATTRS \
174+
__attribute__((__always_inline__, __nodebug__, __target__("bmi"))) constexpr
175+
constexpr
176176
#else
177-
#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS
177+
#define __DEFAULT_FN_ATTRS \
178+
__attribute__((__always_inline__, __nodebug__, __target__("bmi")))
178179
#endif
179180

180181
/// Performs a bitwise AND of the second operand with the one's
@@ -234,7 +235,7 @@ __andn_u32(unsigned int __X, unsigned int __Y)
234235
/// \returns An unsigned integer whose least significant bits contain the
235236
/// extracted bits.
236237
/// \see _bextr_u32
237-
static __inline__ unsigned int __DEFAULT_FN_ATTRS_CONSTEXPR
238+
static __inline__ unsigned int __DEFAULT_FN_ATTRS
238239
__bextr_u32(unsigned int __X, unsigned int __Y) {
239240
return __builtin_ia32_bextr_u32(__X, __Y);
240241
}
@@ -258,7 +259,7 @@ __bextr_u32(unsigned int __X, unsigned int __Y) {
258259
/// \returns An unsigned integer whose least significant bits contain the
259260
/// extracted bits.
260261
/// \see __bextr_u32
261-
static __inline__ unsigned int __DEFAULT_FN_ATTRS_CONSTEXPR
262+
static __inline__ unsigned int __DEFAULT_FN_ATTRS
262263
_bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z) {
263264
return __builtin_ia32_bextr_u32(__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
264265
}
@@ -280,7 +281,7 @@ _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z) {
280281
/// \returns An unsigned integer whose least significant bits contain the
281282
/// extracted bits.
282283
/// \see __bextr_u32
283-
static __inline__ unsigned int __DEFAULT_FN_ATTRS_CONSTEXPR
284+
static __inline__ unsigned int __DEFAULT_FN_ATTRS
284285
_bextr2_u32(unsigned int __X, unsigned int __Y) {
285286
return __builtin_ia32_bextr_u32(__X, __Y);
286287
}
@@ -453,7 +454,7 @@ __andn_u64 (unsigned long long __X, unsigned long long __Y)
453454
/// \returns An unsigned 64-bit integer whose least significant bits contain the
454455
/// extracted bits.
455456
/// \see _bextr_u64
456-
static __inline__ unsigned long long __DEFAULT_FN_ATTRS_CONSTEXPR
457+
static __inline__ unsigned long long __DEFAULT_FN_ATTRS
457458
__bextr_u64(unsigned long long __X, unsigned long long __Y) {
458459
return __builtin_ia32_bextr_u64(__X, __Y);
459460
}
@@ -477,7 +478,7 @@ __bextr_u64(unsigned long long __X, unsigned long long __Y) {
477478
/// \returns An unsigned 64-bit integer whose least significant bits contain the
478479
/// extracted bits.
479480
/// \see __bextr_u64
480-
static __inline__ unsigned long long __DEFAULT_FN_ATTRS_CONSTEXPR
481+
static __inline__ unsigned long long __DEFAULT_FN_ATTRS
481482
_bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z) {
482483
return __builtin_ia32_bextr_u64(__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
483484
}
@@ -499,7 +500,7 @@ _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z) {
499500
/// \returns An unsigned 64-bit integer whose least significant bits contain the
500501
/// extracted bits.
501502
/// \see __bextr_u64
502-
static __inline__ unsigned long long __DEFAULT_FN_ATTRS_CONSTEXPR
503+
static __inline__ unsigned long long __DEFAULT_FN_ATTRS
503504
_bextr2_u64(unsigned long long __X, unsigned long long __Y) {
504505
return __builtin_ia32_bextr_u64(__X, __Y);
505506
}
@@ -615,7 +616,6 @@ __blsr_u64(unsigned long long __X)
615616
#endif /* __x86_64__ */
616617

617618
#undef __DEFAULT_FN_ATTRS
618-
#undef __DEFAULT_FN_ATTRS_CONSTEXPR
619619

620620
#endif /* !defined(__SCE__) || __has_feature(modules) || defined(__BMI__) */
621621

clang/test/CodeGen/X86/bmi-builtins.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ unsigned long long test_blsr_u64(unsigned long long __X) {
237237

238238
// Test constexpr handling.
239239
#if defined(__cplusplus) && (__cplusplus >= 201103L)
240+
char andnu32[__andn_u32(0x01234567, 0xFECDBA98) == (~0x01234567 & 0xFECDBA98) ? 1 : -1];
241+
char andn2u32[_andn_u32(0x01234567, 0xFECDBA98) == (~0x01234567 & 0xFECDBA98) ? 1 : -1];
242+
240243
char bextr32_0[__bextr_u32(0x00000000, 0x00000000) == 0x00000000 ? 1 : -1];
241244
char bextr32_1[__bextr_u32(0x000003F0, 0xFFFF1004) == 0x0000003F ? 1 : -1];
242245
char bextr32_2[__bextr_u32(0x000003F0, 0xFFFF3008) == 0x00000003 ? 1 : -1];
@@ -249,6 +252,15 @@ char bextr32_6[_bextr_u32(0x00000000, 0x00000000, 0x00000000) == 0x00000000 ? 1
249252
char bextr32_7[_bextr_u32(0x000003F0, 0xFFFFFF04, 0xFFFFFF10) == 0x0000003F ? 1 : -1];
250253
char bextr32_8[_bextr_u32(0x000003F0, 0xFFFFFF08, 0xFFFFFF30) == 0x00000003 ? 1 : -1];
251254

255+
char blsiu32[__blsi_u32(0x89ABCDEF) == (0x89ABCDEF & -0x89ABCDEF) ? 1 : -1];
256+
char blsi2u32[_blsi_u32(0x89ABCDEF) == (0x89ABCDEF & -0x89ABCDEF) ? 1 : -1];
257+
258+
char blsmasku32[__blsmsk_u32(0x89ABCDEF) == (0x89ABCDEF ^ (0x89ABCDEF - 1)) ? 1 : -1];
259+
char blsmask2u32[_blsmsk_u32(0x89ABCDEF) == (0x89ABCDEF ^ (0x89ABCDEF - 1)) ? 1 : -1];
260+
261+
char blsru32[__blsr_u32(0x89ABCDEF) == (0x89ABCDEF & (0x89ABCDEF - 1)) ? 1 : -1];
262+
char blsr2u32[_blsr_u32(0x89ABCDEF) == (0x89ABCDEF & (0x89ABCDEF - 1)) ? 1 : -1];
263+
252264
char tzcntu16_0[__tzcnt_u16(0x0000) == 16 ? 1 : -1];
253265
char tzcntu16_1[__tzcnt_u16(0x0001) == 0 ? 1 : -1];
254266
char tzcntu16_2[__tzcnt_u16(0x0010) == 4 ? 1 : -1];
@@ -270,6 +282,9 @@ char tzcnt3u32_1[_mm_tzcnt_32(0x00000001) == 0 ? 1 : -1];
270282
char tzcnt3u32_2[_mm_tzcnt_32(0x00000080) == 7 ? 1 : -1];
271283

272284
#ifdef __x86_64__
285+
char andnu64[__andn_u64(0x0123456789ABCDEFULL, 0xFECDBA9876543210ULL) == (~0x0123456789ABCDEFULL & 0xFECDBA9876543210ULL) ? 1 : -1];
286+
char andn2u64[_andn_u64(0x0123456789ABCDEFULL, 0xFECDBA9876543210ULL) == (~0x0123456789ABCDEFULL & 0xFECDBA9876543210ULL) ? 1 : -1];
287+
273288
char bextr64_0[__bextr_u64(0x0000000000000000ULL, 0x0000000000000000ULL) == 0x0000000000000000ULL ? 1 : -1];
274289
char bextr64_1[__bextr_u64(0xF000000000000001ULL, 0x0000000000004001ULL) == 0x7800000000000000ULL ? 1 : -1];
275290
char bextr64_2[__bextr_u64(0xF000000000000001ULL, 0xFFFFFFFFFFFF1001ULL) == 0x0000000000000000ULL ? 1 : -1];
@@ -282,6 +297,15 @@ char bextr64_6[_bextr_u64(0x0000000000000000ULL, 0x0000000000000000ULL, 0x000000
282297
char bextr64_7[_bextr_u64(0xF000000000000001ULL, 0x0000000000000001ULL, 0x0000000000000040ULL) == 0x7800000000000000ULL ? 1 : -1];
283298
char bextr64_8[_bextr_u64(0xF000000000000001ULL, 0xFFFFFFFFFFFFFF01ULL, 0xFFFFFFFFFFFFFF10ULL) == 0x0000000000000000ULL ? 1 : -1];
284299

300+
char blsiu64[__blsi_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL & -0x0123456789ABCDEFULL) ? 1 : -1];
301+
char blsi2u64[_blsi_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL & -0x0123456789ABCDEFULL) ? 1 : -1];
302+
303+
char blsmasku64[__blsmsk_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL ^ (0x0123456789ABCDEFULL - 1)) ? 1 : -1];
304+
char blsmask2u64[_blsmsk_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL ^ (0x0123456789ABCDEFULL - 1)) ? 1 : -1];
305+
306+
char blsru64[__blsr_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL & (0x0123456789ABCDEFULL - 1)) ? 1 : -1];
307+
char blsr2u64[_blsr_u64(0x0123456789ABCDEFULL) == (0x0123456789ABCDEFULL & (0x0123456789ABCDEFULL - 1)) ? 1 : -1];
308+
285309
char tzcntu64_0[__tzcnt_u64(0x0000000000000000ULL) == 64 ? 1 : -1];
286310
char tzcntu64_1[__tzcnt_u64(0x0000000000000001ULL) == 0 ? 1 : -1];
287311
char tzcntu64_2[__tzcnt_u64(0x0000000800000000ULL) == 35 ? 1 : -1];

0 commit comments

Comments
 (0)