[PATCH] D75723: [X86] Make intrinsics _BitScan* not limited to Windows

Kan Shengchen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 5 21:47:28 PST 2020


skan created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Currently if we want to use _BitScan*, we have to include intrin.h, which can
only be included on Windows. Intrinsics _BitScan* are implemented by LLVM
IR, so the restriction doesn't make sense.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75723

Files:
  clang/include/clang/Basic/BuiltinsX86.def
  clang/include/clang/Basic/BuiltinsX86_64.def
  clang/test/CodeGen/bitscan-builtins.c


Index: clang/test/CodeGen/bitscan-builtins.c
===================================================================
--- clang/test/CodeGen/bitscan-builtins.c
+++ clang/test/CodeGen/bitscan-builtins.c
@@ -3,6 +3,45 @@
 // PR33722
 // RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-unknown -fms-extensions -fms-compatibility-version=19.00 -emit-llvm -o - %s | FileCheck %s
 
+// Using _BitScan* intrinsics should not need any header file, so the tests for
+// them are put placed before any include directive intentionally.
+unsigned char test_BitScanForward(unsigned *index, unsigned mask) {
+  return _BitScanForward(index, mask);
+// CHECK: @test_BitScanForward
+// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
+// CHECK: %[[result:.*]] = phi i8 [ 0, %{{.*}} ], [ 1, %{{.*}} ]
+// CHECK: ret i8 %[[result]]
+// CHECK: %{{.*}} = call i32 @llvm.cttz.i32(i32 %{{.*}}, i1 true)
+}
+
+unsigned char test_BitScanForward64(unsigned *index, unsigned long long mask) {
+  return _BitScanForward64(index, mask);
+// CHECK: @test_BitScanForward64
+// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
+// CHECK: %[[result:.*]] = phi i8 [ 0, %{{.*}} ], [ 1, %{{.*}} ]
+// CHECK: ret i8 %[[result]]
+// CHECK: %{{.*}} = call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 true)
+}
+
+unsigned char test_BitScanReverse(unsigned *index, unsigned mask) {
+  return _BitScanReverse(index, mask);
+// CHECK: @test_BitScanReverse
+// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
+// CHECK: %[[result:.*]] = phi i8 [ 0, %{{.*}} ], [ 1, %{{.*}} ]
+// CHECK: ret i8 %[[result]]
+// CHECK:  %[[call:.*]] = call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 true)
+// CHECK:  %[[sub:.*]] = sub nsw i32 31, %[[call]]
+}
+
+unsigned char test_BitScanReverse64(unsigned *index, unsigned long long mask) {
+  return _BitScanReverse64(index, mask);
+// CHECK: @test_BitScanReverse64
+// CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}
+// CHECK: %[[result:.*]] = phi i8 [ 0, %{{.*}} ], [ 1, %{{.*}} ]
+// CHECK: ret i8 %[[result]]
+// CHECK:  %{{.*}} = call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 true)
+// CHECK:  %{{.*}} = sub nsw i32 63, %{{.*}}
+}
 #include <x86intrin.h>
 
 int test_bit_scan_forward(int a) {
Index: clang/include/clang/Basic/BuiltinsX86_64.def
===================================================================
--- clang/include/clang/Basic/BuiltinsX86_64.def
+++ clang/include/clang/Basic/BuiltinsX86_64.def
@@ -21,9 +21,6 @@
 #  define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANG, FEATURE) BUILTIN(ID, TYPE, ATTRS)
 #endif
 
-TARGET_HEADER_BUILTIN(_BitScanForward64, "UcUNi*ULLi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
-TARGET_HEADER_BUILTIN(_BitScanReverse64, "UcUNi*ULLi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
-
 TARGET_HEADER_BUILTIN(__mulh,  "LLiLLiLLi",    "nch", "intrin.h", ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(__umulh, "ULLiULLiULLi", "nch", "intrin.h", ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(_mul128, "LLiLLiLLiLLi*",      "nch",   "intrin.h", ALL_MS_LANGUAGES, "")
@@ -43,6 +40,10 @@
 TARGET_HEADER_BUILTIN(_InterlockedXor64,         "LLiLLiD*LLi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(_InterlockedCompareExchange128, "UcLLiD*LLiLLiLLi*", "nh", "intrin.h", ALL_MS_LANGUAGES, "cx16")
 
+// BITSCAN
+TARGET_BUILTIN(_BitScanForward64, "UcUNi*ULLi", "n", "")
+TARGET_BUILTIN(_BitScanReverse64, "UcUNi*ULLi", "n", "")
+
 TARGET_BUILTIN(__builtin_ia32_readeflags_u64, "UOi", "n", "")
 TARGET_BUILTIN(__builtin_ia32_writeeflags_u64, "vUOi", "n", "")
 TARGET_BUILTIN(__builtin_ia32_cvtss2si64, "OiV4f", "ncV:128:", "sse")
Index: clang/include/clang/Basic/BuiltinsX86.def
===================================================================
--- clang/include/clang/Basic/BuiltinsX86.def
+++ clang/include/clang/Basic/BuiltinsX86.def
@@ -1900,10 +1900,11 @@
 TARGET_BUILTIN(__builtin_ia32_enqcmd, "Ucv*vC*", "n", "enqcmd")
 TARGET_BUILTIN(__builtin_ia32_enqcmds, "Ucv*vC*", "n", "enqcmd")
 
-// MSVC
-TARGET_HEADER_BUILTIN(_BitScanForward, "UcUNi*UNi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
-TARGET_HEADER_BUILTIN(_BitScanReverse, "UcUNi*UNi", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
+// BITSCAN
+TARGET_BUILTIN(_BitScanForward, "UcUNi*UNi", "n", "")
+TARGET_BUILTIN(_BitScanReverse, "UcUNi*UNi", "n", "")
 
+// MSVC
 TARGET_HEADER_BUILTIN(_ReadWriteBarrier, "v", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(_ReadBarrier,      "v", "nh", "intrin.h", ALL_MS_LANGUAGES, "")
 TARGET_HEADER_BUILTIN(_WriteBarrier,     "v", "nh", "intrin.h", ALL_MS_LANGUAGES, "")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75723.248648.patch
Type: text/x-patch
Size: 4522 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200306/967270bd/attachment.bin>


More information about the cfe-commits mailing list