From: Saleem Abdulrasool Date: Wed, 29 Oct 2014 16:35:41 +0000 (+0000) Subject: CodeGen: add __readfsdword builtin X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11a3310234511a806bb17fe6b412f94368af10ad;p=clang CodeGen: add __readfsdword builtin The Windows NT SDK uses __readfsdword and declares it as a compiler provided builtin (#pragma intrinsic(__readfsword). Because intrin.h is not referenced by winnt.h, it is not possible to provide an out-of-line definition for the intrinsic. Provide a proper compiler builtin definition. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220859 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def index 72843f1fb6..dc668a161c 100644 --- a/include/clang/Basic/Builtins.def +++ b/include/clang/Basic/Builtins.def @@ -703,6 +703,7 @@ LANGBUILTIN(_InterlockedDecrement, "LiLiD*", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedExchangeAdd, "LiLiD*Li", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedExchangePointer, "v*v*D*v*", "n", ALL_MS_LANGUAGES) LANGBUILTIN(_InterlockedExchange, "LiLiD*Li", "n", ALL_MS_LANGUAGES) +LANGBUILTIN(__readfsdword, "ULiULi", "n", ALL_MS_LANGUAGES) // C99 library functions // C99 stdlib.h diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp index c4f92edc8a..5cacfa12a4 100644 --- a/lib/CodeGen/CGBuiltin.cpp +++ b/lib/CodeGen/CGBuiltin.cpp @@ -1624,6 +1624,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD, RMWI->setVolatile(true); return RValue::get(RMWI); } + case Builtin::BI__readfsdword: { + Value *IntToPtr = + Builder.CreateIntToPtr(EmitScalarExpr(E->getArg(0)), + llvm::PointerType::get(CGM.Int32Ty, 257)); + LoadInst *Load = + Builder.CreateAlignedLoad(IntToPtr, /*Align=*/4, /*isVolatile=*/true); + return RValue::get(Load); + } } // If this is an alias for a lib function (e.g. __builtin_sin), emit diff --git a/lib/Headers/Intrin.h b/lib/Headers/Intrin.h index 13e105ec17..d5c334b2da 100644 --- a/lib/Headers/Intrin.h +++ b/lib/Headers/Intrin.h @@ -861,10 +861,6 @@ static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) __readfsbyte(unsigned long __offset) { return *__ptr_to_addr_space(257, unsigned char, __offset); } -static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) -__readfsdword(unsigned long __offset) { - return *__ptr_to_addr_space(257, unsigned long, __offset); -} static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) __readfsqword(unsigned long __offset) { return *__ptr_to_addr_space(257, unsigned __int64, __offset); diff --git a/test/CodeGen/ms-intrinsics.c b/test/CodeGen/ms-intrinsics.c index fb0f62c61c..c952c00be9 100644 --- a/test/CodeGen/ms-intrinsics.c +++ b/test/CodeGen/ms-intrinsics.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple i686--windows -fms-compatibility -Oz -emit-llvm %s -o - | FileCheck %s +// RUN: %clang_cc1 -triple i686--windows -fms-compatibility -Oz -emit-llvm %s -o - | FileCheck %s -check-prefix CHECK -check-prefix CHECK-I386 // RUN: %clang_cc1 -triple thumbv7--windows -fms-compatibility -Oz -emit-llvm %s -o - | FileCheck %s void *test_InterlockedExchangePointer(void * volatile *Target, void *Value) { @@ -36,3 +36,16 @@ long test_InterlockedExchange(long *Target, long Value) { // CHECK: %[[EXCHANGE:[0-9]+]] = atomicrmw xchg i32* %Target, i32 %Value seq_cst // CHECK: ret i32 %[[EXCHANGE:[0-9]+]] // CHECK: } + +#if defined(__i386__) +long test__readfsdword(unsigned long Offset) { + return __readfsdword(Offset); +} + +// CHECK-I386: define i32 @test__readfsdword(i32 %Offset){{.*}}{ +// CHECK-I386: %0 = inttoptr i32 %Offset to i32 addrspace(257)* +// CHECK-I386: %1 = load volatile i32 addrspace(257)* %0, align 4 +// CHECK-I386: ret i32 %1 +// CHECK-I386: } +#endif +