Index: include/llvm/IR/IntrinsicsX86.td =================================================================== --- include/llvm/IR/IntrinsicsX86.td +++ include/llvm/IR/IntrinsicsX86.td @@ -8709,3 +8709,13 @@ def int_x86_sha256msg2 : GCCBuiltin<"__builtin_ia32_sha256msg2">, Intrinsic<[llvm_v4i32_ty], [llvm_v4i32_ty, llvm_v4i32_ty], [IntrNoMem]>; } + +//===----------------------------------------------------------------------===// +// Bit Scan intrinsics +let TargetPrefix = "x86" in { + def int_x86_bit_scan_forward : GCCBuiltin<"__builtin_ia32_bit_scan_forward">, + Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem]>; + + def int_x86_bit_scan_reverse : GCCBuiltin<"__builtin_ia32_bit_scan_reverse">, + Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem]>; +} Index: lib/Target/X86/X86IntrinsicsInfo.h =================================================================== --- lib/Target/X86/X86IntrinsicsInfo.h +++ lib/Target/X86/X86IntrinsicsInfo.h @@ -2152,6 +2152,8 @@ X86ISD::SCALAR_FP_TO_UINT_RND, 0), X86_INTRINSIC_DATA(avx512_vcvtss2usi64, INTR_TYPE_2OP, X86ISD::SCALAR_FP_TO_UINT_RND, 0), + X86_INTRINSIC_DATA(bit_scan_forward, INTR_TYPE_1OP, X86ISD::BSF, 0), + X86_INTRINSIC_DATA(bit_scan_reverse, INTR_TYPE_1OP, X86ISD::BSR, 0), X86_INTRINSIC_DATA(fma_vfmadd_pd, INTR_TYPE_3OP, X86ISD::FMADD, 0), X86_INTRINSIC_DATA(fma_vfmadd_pd_256, INTR_TYPE_3OP, X86ISD::FMADD, 0), X86_INTRINSIC_DATA(fma_vfmadd_ps, INTR_TYPE_3OP, X86ISD::FMADD, 0), Index: test/CodeGen/X86/bitscan.ll =================================================================== --- test/CodeGen/X86/bitscan.ll +++ test/CodeGen/X86/bitscan.ll @@ -0,0 +1,17 @@ +; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mcpu=corei7 | FileCheck %s +declare i32 @llvm.x86.bit.scan.forward(i32 %val) +declare i32 @llvm.x86.bit.scan.reverse(i32 %val) + +define i32 @test_bsf(i32 %val) { + %call = call i32 @llvm.x86.bit.scan.forward(i32 %val) + ret i32 %call +; CHECK-LABEL: test_bsf +; CHECK: bsfl %edi, %eax +} + +define i32 @test_bsr(i32 %val) { + %call = call i32 @llvm.x86.bit.scan.reverse(i32 %val) + ret i32 %call +; CHECK-LABEL: test_bsr +; CHECK: bsrl %edi, %eax +}