From 122fb01a0d3afdb020866c719896161a1b829e03 Mon Sep 17 00:00:00 2001 From: Mandeep Singh Grang Date: Thu, 4 Oct 2018 22:32:42 +0000 Subject: [PATCH] [COFF, ARM64] Add __getReg intrinsic Reviewers: rnk, mstorsjo, compnerd, TomTan, haripul, javed.absar, efriedma Reviewed By: efriedma Subscribers: peter.smith, efriedma, kristof.beyls, chrib, cfe-commits Differential Revision: https://reviews.llvm.org/D52838 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@343824 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/BuiltinsAArch64.def | 1 + lib/CodeGen/CGBuiltin.cpp | 17 +++++++++++++++++ lib/Headers/intrin.h | 7 +++++++ lib/Sema/SemaChecking.cpp | 3 +++ test/CodeGen/arm64-microsoft-intrinsics.c | 12 ++++++++++++ test/Sema/builtins-microsoft-arm64.c | 9 +++++++++ 6 files changed, 49 insertions(+) create mode 100644 test/Sema/builtins-microsoft-arm64.c diff --git a/include/clang/Basic/BuiltinsAArch64.def b/include/clang/Basic/BuiltinsAArch64.def index 4c46f50a8d..2b6fcdabdc 100644 --- a/include/clang/Basic/BuiltinsAArch64.def +++ b/include/clang/Basic/BuiltinsAArch64.def @@ -104,6 +104,7 @@ TARGET_HEADER_BUILTIN(_InterlockedOr64, "LLiLLiD*LLi", "nh", "intrin.h" TARGET_HEADER_BUILTIN(_InterlockedXor64, "LLiLLiD*LLi", "nh", "intrin.h", ALL_MS_LANGUAGES, "") TARGET_HEADER_BUILTIN(_ReadWriteBarrier, "v", "nh", "intrin.h", ALL_MS_LANGUAGES, "") +TARGET_HEADER_BUILTIN(__getReg, "ULLii", "nh", "intrin.h", ALL_MS_LANGUAGES, "") #undef BUILTIN #undef LANGBUILTIN diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp index 49c2b126dd..b59fa379d8 100644 --- a/lib/CodeGen/CGBuiltin.cpp +++ b/lib/CodeGen/CGBuiltin.cpp @@ -6576,6 +6576,23 @@ Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID, return Builder.CreateCall(F, {StoreVal, StoreAddr}, "stxr"); } + if (BuiltinID == AArch64::BI__getReg) { + APSInt Value; + if (!E->getArg(0)->EvaluateAsInt(Value, CGM.getContext())) + llvm_unreachable("Sema will ensure that the parameter is constant"); + + LLVMContext &Context = CGM.getLLVMContext(); + std::string Reg = Value == 31 ? "sp" : "x" + Value.toString(10); + + llvm::Metadata *Ops[] = {llvm::MDString::get(Context, Reg)}; + llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops); + llvm::Value *Metadata = llvm::MetadataAsValue::get(Context, RegName); + + llvm::Value *F = + CGM.getIntrinsic(llvm::Intrinsic::read_register, {Int64Ty}); + return Builder.CreateCall(F, Metadata); + } + if (BuiltinID == AArch64::BI__builtin_arm_clrex) { Function *F = CGM.getIntrinsic(Intrinsic::aarch64_clrex); return Builder.CreateCall(F); diff --git a/lib/Headers/intrin.h b/lib/Headers/intrin.h index edb947eef6..c5b4ff4d7d 100644 --- a/lib/Headers/intrin.h +++ b/lib/Headers/intrin.h @@ -864,6 +864,13 @@ __nop(void) { } #endif +/*----------------------------------------------------------------------------*\ +|* MS AArch64 specific +\*----------------------------------------------------------------------------*/ +#if defined(__aarch64__) +unsigned __int64 __getReg(int); +#endif + /*----------------------------------------------------------------------------*\ |* Privileged intrinsics \*----------------------------------------------------------------------------*/ diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index d6183dde95..113626374c 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -1749,6 +1749,9 @@ bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, BuiltinID == AArch64::BI__builtin_arm_wsrp) return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true); + if (BuiltinID == AArch64::BI__getReg) + return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31); + if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) return true; diff --git a/test/CodeGen/arm64-microsoft-intrinsics.c b/test/CodeGen/arm64-microsoft-intrinsics.c index d29e9305cc..89e6c369f4 100644 --- a/test/CodeGen/arm64-microsoft-intrinsics.c +++ b/test/CodeGen/arm64-microsoft-intrinsics.c @@ -66,3 +66,15 @@ void check_ReadWriteBarrier() { // CHECK-MSVC: fence syncscope("singlethread") // CHECK-LINUX: error: implicit declaration of function '_ReadWriteBarrier' + +unsigned __int64 check__getReg() { + unsigned volatile __int64 reg; + reg = __getReg(18); + reg = __getReg(31); + return reg; +} + +// CHECK-MSVC: call i64 @llvm.read_register.i64(metadata !2) +// CHECK-MSVC: call i64 @llvm.read_register.i64(metadata !3) +// CHECK-MSVC: !2 = !{!"x18"} +// CHECK-MSVC: !3 = !{!"sp"} diff --git a/test/Sema/builtins-microsoft-arm64.c b/test/Sema/builtins-microsoft-arm64.c new file mode 100644 index 0000000000..34f9834dba --- /dev/null +++ b/test/Sema/builtins-microsoft-arm64.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple arm64-windows -fsyntax-only -verify \ +// RUN: -fms-compatibility -ffreestanding -fms-compatibility-version=17.00 %s + +#include + +void check__getReg() { + __getReg(-1); // expected-error-re {{argument value {{.*}} is outside the valid range}} + __getReg(32); // expected-error-re {{argument value {{.*}} is outside the valid range}} +} -- 2.40.0