From cd0174aacb734904205ed7827fb923acda08f79a Mon Sep 17 00:00:00 2001 From: Weining Lu Date: Mon, 31 Oct 2022 08:46:58 +0800 Subject: [PATCH] [Clang][LoongArch] Support inline asm constraint 'J' 'J' is defined in GCC [1] but not documented [2] while Linux [3] has already used it in LoongArch port. [1]: https://github.com/gcc-mirror/gcc/blob/master/gcc/config/loongarch/constraints.md#L61 [2]: https://gcc.gnu.org/onlinedocs/gccint/Machine-Constraints.html [3]: https://github.com/torvalds/linux/blob/master/arch/loongarch/include/asm/cmpxchg.h#L19 Differential Revision: https://reviews.llvm.org/D136835 --- clang/lib/Basic/Targets/LoongArch.cpp | 4 ++++ clang/test/CodeGen/LoongArch/inline-asm-constraints.c | 6 ++++++ llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp | 9 +++++++++ llvm/test/CodeGen/LoongArch/inline-asm-constraint-error.ll | 6 ++++++ llvm/test/CodeGen/LoongArch/inline-asm-constraint.ll | 11 +++++++++++ 5 files changed, 36 insertions(+) diff --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp index 1ad9c0f..12d53a9 100644 --- a/clang/lib/Basic/Targets/LoongArch.cpp +++ b/clang/lib/Basic/Targets/LoongArch.cpp @@ -88,6 +88,10 @@ bool LoongArchTargetInfo::validateAsmConstraint( // A signed 12-bit constant (for arithmetic instructions). Info.setRequiresImmediate(-2048, 2047); return true; + case 'J': + // Integer zero. + Info.setRequiresImmediate(0); + return true; case 'K': // An unsigned 12-bit constant (for logic instructions). Info.setRequiresImmediate(0, 4095); diff --git a/clang/test/CodeGen/LoongArch/inline-asm-constraints.c b/clang/test/CodeGen/LoongArch/inline-asm-constraints.c index d7d425e..b194942 100644 --- a/clang/test/CodeGen/LoongArch/inline-asm-constraints.c +++ b/clang/test/CodeGen/LoongArch/inline-asm-constraints.c @@ -43,6 +43,12 @@ void test_I(void) { asm volatile ("" :: "I"(-2048)); } +void test_J(void) { +// CHECK-LABEL: define{{.*}} void @test_J() +// CHECK: call void asm sideeffect "", "J"(i32 0) + asm volatile ("" :: "J"(0)); +} + void test_K(void) { // CHECK-LABEL: define{{.*}} void @test_K() // CHECK: call void asm sideeffect "", "K"(i32 4095) diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp index f99764f..1c58fa0d 100644 --- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp +++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp @@ -2184,6 +2184,7 @@ LoongArchTargetLowering::getConstraintType(StringRef Constraint) const { // offset that is suitable for use in instructions with the same // addressing mode as st.w and ld.w. // 'I': A signed 12-bit constant (for arithmetic instructions). + // 'J': Integer zero. // 'K': An unsigned 12-bit constant (for logic instructions). // "ZB": An address that is held in a general-purpose register. The offset is // zero. @@ -2198,6 +2199,7 @@ LoongArchTargetLowering::getConstraintType(StringRef Constraint) const { return C_RegisterClass; case 'l': case 'I': + case 'J': case 'K': return C_Immediate; case 'k': @@ -2301,6 +2303,13 @@ void LoongArchTargetLowering::LowerAsmOperandForConstraint( DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getGRLenVT())); } return; + case 'J': + // Validate & create an integer zero operand. + if (auto *C = dyn_cast(Op)) + if (C->getZExtValue() == 0) + Ops.push_back( + DAG.getTargetConstant(0, SDLoc(Op), Subtarget.getGRLenVT())); + return; case 'K': // Validate & create a 12-bit unsigned immediate operand. if (auto *C = dyn_cast(Op)) { diff --git a/llvm/test/CodeGen/LoongArch/inline-asm-constraint-error.ll b/llvm/test/CodeGen/LoongArch/inline-asm-constraint-error.ll index 470581d..570fd43 100644 --- a/llvm/test/CodeGen/LoongArch/inline-asm-constraint-error.ll +++ b/llvm/test/CodeGen/LoongArch/inline-asm-constraint-error.ll @@ -17,6 +17,12 @@ define void @constraint_I() { ret void } +define void @constraint_J() { +; CHECK: error: value out of range for constraint 'J' + tail call void asm sideeffect "addi.w $$a0, $$a0, $$0", "J"(i32 1) + ret void +} + define void @constraint_K() { ; CHECK: error: value out of range for constraint 'K' tail call void asm sideeffect "andi.w $$a0, $$a0, $0", "K"(i32 4096) diff --git a/llvm/test/CodeGen/LoongArch/inline-asm-constraint.ll b/llvm/test/CodeGen/LoongArch/inline-asm-constraint.ll index ed2c621..4b63d3b 100644 --- a/llvm/test/CodeGen/LoongArch/inline-asm-constraint.ll +++ b/llvm/test/CodeGen/LoongArch/inline-asm-constraint.ll @@ -58,6 +58,17 @@ define void @constraint_I() nounwind { ret void } +define void @constraint_J() nounwind { +; CHECK-LABEL: constraint_J: +; CHECK: # %bb.0: +; CHECK-NEXT: #APP +; CHECK-NEXT: addi.w $a0, $a0, 0 +; CHECK-NEXT: #NO_APP +; CHECK-NEXT: ret + tail call void asm sideeffect "addi.w $$a0, $$a0, $0", "J"(i32 0) + ret void +} + define void @constraint_K() nounwind { ; CHECK-LABEL: constraint_K: ; CHECK: # %bb.0: -- 2.7.4