Skip to content

Commit 7981c29

Browse files
authored
[X86] Allow using the lzcnt intrinsics for non-LZCNT targets (#128284)
Similar to D14748, we can relax lzcnt intrinsics too, especially with improved BSR lowering by #123623
1 parent 4e44bd0 commit 7981c29

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

clang/include/clang/Basic/BuiltinsX86.td

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,18 +832,15 @@ let Features = "rdseed", Attributes = [NoThrow] in {
832832
def rdseed32_step : X86Builtin<"unsigned int(unsigned int *)">;
833833
}
834834

835-
let Features = "lzcnt", Attributes = [NoThrow, Const, Constexpr] in {
836-
def lzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
837-
def lzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
838-
}
839-
840835
let Features = "bmi", Attributes = [NoThrow, Const, Constexpr] in {
841836
def bextr_u32 : X86Builtin<"unsigned int(unsigned int, unsigned int)">;
842837
}
843838

844839
let Attributes = [NoThrow, Const, Constexpr] in {
845840
def tzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
846841
def tzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
842+
def lzcnt_u16 : X86Builtin<"unsigned short(unsigned short)">;
843+
def lzcnt_u32 : X86Builtin<"unsigned int(unsigned int)">;
847844
}
848845

849846
let Features = "bmi2", Attributes = [NoThrow, Const, Constexpr] in {

clang/include/clang/Basic/BuiltinsX86_64.td

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,13 @@ let Features = "rdseed", Attributes = [NoThrow] in {
126126
def rdseed64_step : X86Builtin<"unsigned int(unsigned long long int *)">;
127127
}
128128

129-
let Features = "lzcnt", Attributes = [NoThrow, Const, Constexpr] in {
130-
def lzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
131-
}
132-
133129
let Features = "bmi", Attributes = [NoThrow, Const, Constexpr] in {
134130
def bextr_u64 : X86Builtin<"unsigned long long int(unsigned long long int, unsigned long long int)">;
135131
}
136132

137133
let Attributes = [NoThrow, Const, Constexpr] in {
138134
def tzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
135+
def lzcnt_u64 : X86Builtin<"unsigned long long int(unsigned long long int)">;
139136
}
140137

141138
let Features = "bmi2", Attributes = [NoThrow, Const, Constexpr] in {

clang/lib/Headers/lzcntintrin.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
#ifndef __LZCNTINTRIN_H
1515
#define __LZCNTINTRIN_H
1616

17-
/* Define the default attributes for the functions in this file. */
17+
/* Define the default attributes for the functions in this file.
18+
Allow using the lzcnt intrinsics even for non-LZCNT targets. Since the LZCNT
19+
intrinsics are mapped to llvm.ctlz.*, false, which can be lowered to BSR on
20+
non-LZCNT targets with zero-value input handled correctly. */
1821
#if defined(__cplusplus) && (__cplusplus >= 201103L)
1922
#define __DEFAULT_FN_ATTRS \
20-
__attribute__((__always_inline__, __nodebug__, __target__("lzcnt"))) constexpr
23+
__attribute__((__always_inline__, __nodebug__)) constexpr
2124
#else
22-
#define __DEFAULT_FN_ATTRS \
23-
__attribute__((__always_inline__, __nodebug__, __target__("lzcnt")))
25+
#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
2426
#endif
2527

2628
#ifndef _MSC_VER

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
2-
// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -target-feature +lzcnt -emit-llvm -o - | FileCheck %s
1+
// RUN: %clang_cc1 -x c -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 -x c++ -std=c++11 -ffreestanding %s -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
33

44

55
#include <immintrin.h>
@@ -56,4 +56,4 @@ char lzcntu32_2[_lzcnt_u32(0x00000010) == 27 ? 1 : -1];
5656
char lzcntu64_0[_lzcnt_u64(0x0000000000000000ULL) == 64 ? 1 : -1];
5757
char lzcntu64_1[_lzcnt_u64(0x8000000000000000ULL) == 0 ? 1 : -1];
5858
char lzcntu64_2[_lzcnt_u64(0x0000000100000000ULL) == 31 ? 1 : -1];
59-
#endif
59+
#endif

0 commit comments

Comments
 (0)