diff --git a/clang/include/clang/Basic/BuiltinsAArch64.def b/clang/include/clang/Basic/BuiltinsAArch64.def --- a/clang/include/clang/Basic/BuiltinsAArch64.def +++ b/clang/include/clang/Basic/BuiltinsAArch64.def @@ -251,6 +251,11 @@ TARGET_HEADER_BUILTIN(__break, "vi", "nh", "intrin.h", ALL_MS_LANGUAGES, "") +TARGET_HEADER_BUILTIN(__writex18byte, "vULiUc", "nh", "intrin.h", ALL_MS_LANGUAGES, "") +TARGET_HEADER_BUILTIN(__writex18word, "vULiUs", "nh", "intrin.h", ALL_MS_LANGUAGES, "") +TARGET_HEADER_BUILTIN(__writex18dword, "vULiULi", "nh", "intrin.h", ALL_MS_LANGUAGES, "") +TARGET_HEADER_BUILTIN(__writex18qword, "vULiULLi", "nh", "intrin.h", ALL_MS_LANGUAGES, "") + #undef BUILTIN #undef LANGBUILTIN #undef TARGET_HEADER_BUILTIN diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -9952,6 +9952,31 @@ return HigherBits; } + if (BuiltinID == AArch64::BI__writex18byte || + BuiltinID == AArch64::BI__writex18word || + BuiltinID == AArch64::BI__writex18dword || + BuiltinID == AArch64::BI__writex18qword) { + llvm::Type *IntTy = ConvertType(E->getArg(1)->getType()); + + // Read x18 as byte ptr, word ptr, dword ptr, or qword ptr + LLVMContext &Context = CGM.getLLVMContext(); + llvm::Metadata *Ops[] = {llvm::MDString::get(Context, "x18")}; + llvm::MDNode *RegName = llvm::MDNode::get(Context, Ops); + llvm::Value *Metadata = llvm::MetadataAsValue::get(Context, RegName); + llvm::Function *F = + CGM.getIntrinsic(llvm::Intrinsic::read_register, {Int64Ty}); + llvm::Value *X18 = Builder.CreateCall(F, Metadata); + X18 = Builder.CreateIntToPtr(X18, llvm::PointerType::get(IntTy, 256)); + + // Store val at x18 + offset + Value *Offset = Builder.CreateZExt(EmitScalarExpr(E->getArg(0)), Int64Ty); + Value *Ptr = Builder.CreateGEP(IntTy, X18, Offset); + Value *Val = EmitScalarExpr(E->getArg(1)); + StoreInst *Store = Builder.CreateAlignedStore( + Val, Ptr, getContext().getTypeAlignInChars(E->getType())); + return Store; + } + // Handle MSVC intrinsics before argument evaluation to prevent double // evaluation. if (Optional MsvcIntId = translateAarch64ToMsvcIntrin(BuiltinID)) diff --git a/clang/lib/Headers/intrin.h b/clang/lib/Headers/intrin.h --- a/clang/lib/Headers/intrin.h +++ b/clang/lib/Headers/intrin.h @@ -562,6 +562,11 @@ unsigned __int64 __umulh(unsigned __int64 __a, unsigned __int64 __b); void __break(int); + +void __writex18byte(unsigned long offset, unsigned char data); +void __writex18word(unsigned long offset, unsigned short data); +void __writex18dword(unsigned long offset, unsigned long data); +void __writex18qword(unsigned long offset, unsigned __int64 data); #endif /*----------------------------------------------------------------------------*\ diff --git a/clang/test/CodeGen/arm64-microsoft-intrinsics.c b/clang/test/CodeGen/arm64-microsoft-intrinsics.c --- a/clang/test/CodeGen/arm64-microsoft-intrinsics.c +++ b/clang/test/CodeGen/arm64-microsoft-intrinsics.c @@ -119,5 +119,70 @@ // CHECK-MSVC: call i64 @llvm.read_register.i64(metadata ![[MD2:.*]]) // CHECK-MSVC: call i64 @llvm.read_register.i64(metadata ![[MD3:.*]]) + +void check__writex18byte(unsigned long offset, unsigned char data) { + __writex18byte(offset, data); +} + +// CHECK-MSVC: %[[DATA_ADDR:.*]] = alloca i8, align 1 +// CHECK-MSVC: %[[OFFSET_ADDR:.*]] = alloca i32, align 4 +// CHECK-MSVC: store i8 %data, i8* %[[DATA_ADDR]], align 1 +// CHECK-MSVC: store i32 %offset, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[X18:.*]] = call i64 @llvm.read_register.i64(metadata ![[MD2]]) +// CHECK-MSVC: %[[X18_AS_PTR:.*]] = inttoptr i64 %[[X18]] to i8 addrspace(256)* +// CHECK-MSVC: %[[OFFSET:.*]] = load i32, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[ZEXT_OFFSET:.*]] = zext i32 %[[OFFSET]] to i64 +// CHECK-MSVC: %[[PTR:.*]] = getelementptr i8, i8 addrspace(256)* %[[X18_AS_PTR]], i64 %[[ZEXT_OFFSET]] +// CHECK-MSVC: %[[DATA:.*]] = load i8, i8* %[[DATA_ADDR]], align 1 +// CHECK-MSVC: store i8 %[[DATA]], i8 addrspace(256)* %[[PTR]], align 1 + +void check__writex18word(unsigned long offset, unsigned short data) { + __writex18word(offset, data); +} + +// CHECK-MSVC: %[[DATA_ADDR:.*]] = alloca i16, align 2 +// CHECK-MSVC: %[[OFFSET_ADDR:.*]] = alloca i32, align 4 +// CHECK-MSVC: store i16 %data, i16* %[[DATA_ADDR]], align 2 +// CHECK-MSVC: store i32 %offset, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[X18:.*]] = call i64 @llvm.read_register.i64(metadata ![[MD2]]) +// CHECK-MSVC: %[[X18_AS_PTR:.*]] = inttoptr i64 %[[X18]] to i16 addrspace(256)* +// CHECK-MSVC: %[[OFFSET:.*]] = load i32, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[ZEXT_OFFSET:.*]] = zext i32 %[[OFFSET]] to i64 +// CHECK-MSVC: %[[PTR:.*]] = getelementptr i16, i16 addrspace(256)* %[[X18_AS_PTR]], i64 %[[ZEXT_OFFSET]] +// CHECK-MSVC: %[[DATA:.*]] = load i16, i16* %[[DATA_ADDR]], align 2 +// CHECK-MSVC: store i16 %[[DATA]], i16 addrspace(256)* %[[PTR]], align 1 + +void check__writex18dword(unsigned long offset, unsigned long data) { + __writex18dword(offset, data); +} + +// CHECK-MSVC: %[[DATA_ADDR:.*]] = alloca i32, align 4 +// CHECK-MSVC: %[[OFFSET_ADDR:.*]] = alloca i32, align 4 +// CHECK-MSVC: store i32 %data, i32* %[[DATA_ADDR]], align 4 +// CHECK-MSVC: store i32 %offset, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[X18:.*]] = call i64 @llvm.read_register.i64(metadata ![[MD2]]) +// CHECK-MSVC: %[[X18_AS_PTR:.*]] = inttoptr i64 %[[X18]] to i32 addrspace(256)* +// CHECK-MSVC: %[[OFFSET:.*]] = load i32, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[ZEXT_OFFSET:.*]] = zext i32 %[[OFFSET]] to i64 +// CHECK-MSVC: %[[PTR:.*]] = getelementptr i32, i32 addrspace(256)* %[[X18_AS_PTR]], i64 %[[ZEXT_OFFSET]] +// CHECK-MSVC: %[[DATA:.*]] = load i32, i32* %[[DATA_ADDR]], align 4 +// CHECK-MSVC: store i32 %[[DATA]], i32 addrspace(256)* %[[PTR]], align 1 + +void check__writex18qword(unsigned long offset, unsigned __int64 data) { + __writex18qword(offset, data); +} + +// CHECK-MSVC: %[[DATA_ADDR:.*]] = alloca i64, align 8 +// CHECK-MSVC: %[[OFFSET_ADDR:.*]] = alloca i32, align 4 +// CHECK-MSVC: store i64 %data, i64* %[[DATA_ADDR]], align 8 +// CHECK-MSVC: store i32 %offset, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[X18:.*]] = call i64 @llvm.read_register.i64(metadata ![[MD2]]) +// CHECK-MSVC: %[[X18_AS_PTR:.*]] = inttoptr i64 %[[X18]] to i64 addrspace(256)* +// CHECK-MSVC: %[[OFFSET:.*]] = load i32, i32* %[[OFFSET_ADDR]], align 4 +// CHECK-MSVC: %[[ZEXT_OFFSET:.*]] = zext i32 %[[OFFSET]] to i64 +// CHECK-MSVC: %[[PTR:.*]] = getelementptr i64, i64 addrspace(256)* %[[X18_AS_PTR]], i64 %[[ZEXT_OFFSET]] +// CHECK-MSVC: %[[DATA:.*]] = load i64, i64* %[[DATA_ADDR]], align 8 +// CHECK-MSVC: store i64 %[[DATA]], i64 addrspace(256)* %[[PTR]], align 1 + // CHECK-MSVC: ![[MD2]] = !{!"x18"} // CHECK-MSVC: ![[MD3]] = !{!"sp"}