[llvm-branch-commits] [llvm] [PowerPC] Use 64-bit registers for wide "r" inline asm operands (PR #239128)
Bill Wendling via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 19 21:09:07 PST 2026
https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/239128
>From a8ce570df2fd08b1a04cc018a39f43ac4220fb86 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Wed, 4 Nov 2026 23:33:17 -0800
Subject: [PATCH 1/2] [PowerPC] Use 64-bit registers for wide "r" inline asm
operands
On PPC64, an integer wider than 64 bits, such as an i128, in an "r" or
"b" operand was given a 32-bit register for each 64 bits of it, so half
of its bits were lost. Give it 64-bit registers instead. AArch64 had the
same problem (#238926).
Assisted-by: Claude Opus 5.5
---
llvm/lib/Target/PowerPC/PPCISelLowering.cpp | 10 +++-
.../CodeGen/PowerPC/inline-asm-wide-gpr.ll | 54 +++++++++++++++++++
2 files changed, 62 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index b904d355915c66..142b7b43bc0b5a 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -19019,14 +19019,20 @@ PPCTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
StringRef Constraint,
MVT VT) const {
if (Constraint.size() == 1) {
+ // On a 64-bit target, an i64 takes a 64-bit register, and an integer wider
+ // than that, such as an i128, takes one for each 64 bits of it. 32-bit
+ // registers would hold only half of it.
+ bool Use64BitGPRs =
+ Subtarget.isPPC64() && VT.isScalarInteger() && VT.getSizeInBits() >= 64;
+
// GCC RS6000 Constraint Letters
switch (Constraint[0]) {
case 'b': // R1-R31
- if (VT == MVT::i64 && Subtarget.isPPC64())
+ if (Use64BitGPRs)
return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
case 'r': // R0-R31
- if (VT == MVT::i64 && Subtarget.isPPC64())
+ if (Use64BitGPRs)
return std::make_pair(0U, &PPC::G8RCRegClass);
return std::make_pair(0U, &PPC::GPRCRegClass);
// 'd' and 'f' constraints are both defined to be "the floating point
diff --git a/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll b/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
new file mode 100644
index 00000000000000..5d54f56d1bc35e
--- /dev/null
+++ b/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
@@ -0,0 +1,54 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=powerpc64le-linux-gnu < %s | FileCheck %s
+
+; On a 64-bit target, an integer wider than 64 bits in an "r" or "b" operand
+; goes in 64-bit registers, one for each 64 bits of it. 32-bit registers would
+; hold only half of it.
+
+define void @test_i128_input(i128 %x) {
+; CHECK-LABEL: test_i128_input:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: #APP
+; CHECK-NEXT: # 3
+; CHECK-NEXT: #NO_APP
+; CHECK-NEXT: blr
+entry:
+ call void asm sideeffect "# $0", "r"(i128 %x)
+ ret void
+}
+
+define i128 @test_i128_output() {
+; CHECK-LABEL: test_i128_output:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: #APP
+; CHECK-NEXT: # 3
+; CHECK-NEXT: #NO_APP
+; CHECK-NEXT: blr
+entry:
+ %r = call i128 asm sideeffect "# $0", "=r"()
+ ret i128 %r
+}
+
+; The asm doesn't change its operand, so the function returns its argument.
+define i128 @test_i128_tied(i128 %x) {
+; CHECK-LABEL: test_i128_tied:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: #APP
+; CHECK-NEXT: #NO_APP
+; CHECK-NEXT: blr
+entry:
+ %r = call i128 asm sideeffect "", "=&r,0"(i128 %x)
+ ret i128 %r
+}
+
+define void @test_i128_input_b(i128 %x) {
+; CHECK-LABEL: test_i128_input_b:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: #APP
+; CHECK-NEXT: # 3
+; CHECK-NEXT: #NO_APP
+; CHECK-NEXT: blr
+entry:
+ call void asm sideeffect "# $0", "b"(i128 %x)
+ ret void
+}
>From 2b16b7ba89bbec0bcae1f4cea91e4877d94996af Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Wed, 11 Nov 2026 13:17:20 -0800
Subject: [PATCH 2/2] Check the register class of wide GPR operands in MIR
The assembly prints a GPR the same way whether it is used as a 32-bit or
a 64-bit register, so the old checks couldn't show the fix working. Stop
after isel and check the register class of each inline asm operand.
Also add i64 cases for "r" and "b", an i32 case, and a 32-bit PowerPC
RUN line.
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
.../CodeGen/PowerPC/inline-asm-wide-gpr.ll | 228 +++++++++++++++---
1 file changed, 199 insertions(+), 29 deletions(-)
diff --git a/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll b/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
index 5d54f56d1bc35e..94117d865a1502 100644
--- a/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
+++ b/llvm/test/CodeGen/PowerPC/inline-asm-wide-gpr.ll
@@ -1,53 +1,223 @@
-; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: llc -mtriple=powerpc64le-linux-gnu < %s | FileCheck %s
+; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=powerpc64le-linux-gnu -stop-after=finalize-isel < %s | FileCheck %s --check-prefix=PPC64
+; RUN: llc -mtriple=powerpc-linux-gnu -stop-after=finalize-isel < %s | FileCheck %s --check-prefix=PPC32
-; On a 64-bit target, an integer wider than 64 bits in an "r" or "b" operand
-; goes in 64-bit registers, one for each 64 bits of it. 32-bit registers would
-; hold only half of it.
+; Check the register class given to an integer operand of an "r" or "b"
+; constraint. The assembly prints a GPR the same way whether it is used as a
+; 32-bit or a 64-bit register, so these tests look at the MIR after isel.
+;
+; On a 64-bit target, an i64 goes in one 64-bit register (G8RC), and an integer
+; wider than 64 bits, such as an i128, goes in one 64-bit register for each 64
+; bits of it. 32-bit registers (GPRC) would hold only half of its bits. An i32
+; still goes in a 32-bit register. On a 32-bit target, every integer goes in
+; 32-bit registers, one for each 32 bits of it.
+
+define void @test_i32_input(i32 %x) {
+ ; PPC64-LABEL: name: test_i32_input
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY [[COPY]].sub_32
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC, [[COPY1]]
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm
+ ;
+ ; PPC32-LABEL: name: test_i32_input
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC, [[COPY1]]
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm
+entry:
+ call void asm sideeffect "# $0", "r"(i32 %x)
+ ret void
+}
+
+define void @test_i64_input(i64 %x) {
+ ; PPC64-LABEL: name: test_i64_input
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:g8rc = COPY [[COPY]]
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:G8RC, [[COPY1]]
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm
+ ;
+ ; PPC32-LABEL: name: test_i64_input
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3, $r4
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r4
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY2:%[0-9]+]]:gprc = COPY [[COPY1]]
+ ; PPC32-NEXT: [[COPY3:%[0-9]+]]:gprc = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC, [[COPY2]], [[COPY3]]
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm
+entry:
+ call void asm sideeffect "# $0", "r"(i64 %x)
+ ret void
+}
+
+define i64 @test_i64_output() {
+ ; PPC64-LABEL: name: test_i64_output
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, regdef:G8RC, def %0
+ ; PPC64-NEXT: $x3 = COPY %0
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm, implicit $x3
+ ;
+ ; PPC32-LABEL: name: test_i64_output
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, regdef:GPRC, def %0, def %1
+ ; PPC32-NEXT: $r3 = COPY %0
+ ; PPC32-NEXT: $r4 = COPY %1
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm, implicit $r3, implicit $r4
+entry:
+ %r = call i64 asm sideeffect "# $0", "=r"()
+ ret i64 %r
+}
+
+define void @test_i64_input_b(i64 %x) {
+ ; PPC64-LABEL: name: test_i64_input_b
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:g8rc_nox0 = COPY [[COPY]]
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:G8RC_NOX0, [[COPY1]]
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm
+ ;
+ ; PPC32-LABEL: name: test_i64_input_b
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3, $r4
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r4
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY2:%[0-9]+]]:gprc_nor0 = COPY [[COPY1]]
+ ; PPC32-NEXT: [[COPY3:%[0-9]+]]:gprc_nor0 = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC_NOR0, [[COPY2]], [[COPY3]]
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm
+entry:
+ call void asm sideeffect "# $0", "b"(i64 %x)
+ ret void
+}
define void @test_i128_input(i128 %x) {
-; CHECK-LABEL: test_i128_input:
-; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: #APP
-; CHECK-NEXT: # 3
-; CHECK-NEXT: #NO_APP
-; CHECK-NEXT: blr
+ ; PPC64-LABEL: name: test_i128_input
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3, $x4
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x4
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY2:%[0-9]+]]:g8rc = COPY [[COPY1]]
+ ; PPC64-NEXT: [[COPY3:%[0-9]+]]:g8rc = COPY [[COPY]]
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:G8RC, [[COPY2]], [[COPY3]]
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm
+ ;
+ ; PPC32-LABEL: name: test_i128_input
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3, $r4, $r5, $r6
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r6
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r5
+ ; PPC32-NEXT: [[COPY2:%[0-9]+]]:gprc = COPY $r4
+ ; PPC32-NEXT: [[COPY3:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY4:%[0-9]+]]:gprc = COPY [[COPY3]]
+ ; PPC32-NEXT: [[COPY5:%[0-9]+]]:gprc = COPY [[COPY2]]
+ ; PPC32-NEXT: [[COPY6:%[0-9]+]]:gprc = COPY [[COPY1]]
+ ; PPC32-NEXT: [[COPY7:%[0-9]+]]:gprc = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC, [[COPY4]], [[COPY5]], [[COPY6]], [[COPY7]]
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm
entry:
call void asm sideeffect "# $0", "r"(i128 %x)
ret void
}
define i128 @test_i128_output() {
-; CHECK-LABEL: test_i128_output:
-; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: #APP
-; CHECK-NEXT: # 3
-; CHECK-NEXT: #NO_APP
-; CHECK-NEXT: blr
+ ; PPC64-LABEL: name: test_i128_output
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, regdef:G8RC, def %0, def %1
+ ; PPC64-NEXT: $x3 = COPY %0
+ ; PPC64-NEXT: $x4 = COPY %1
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm, implicit $x3, implicit $x4
+ ;
+ ; PPC32-LABEL: name: test_i128_output
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, regdef:GPRC, def %0, def %1, def %2, def %3
+ ; PPC32-NEXT: $r3 = COPY %0
+ ; PPC32-NEXT: $r4 = COPY %1
+ ; PPC32-NEXT: $r5 = COPY %2
+ ; PPC32-NEXT: $r6 = COPY %3
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm, implicit $r3, implicit $r4, implicit $r5, implicit $r6
entry:
%r = call i128 asm sideeffect "# $0", "=r"()
ret i128 %r
}
-; The asm doesn't change its operand, so the function returns its argument.
define i128 @test_i128_tied(i128 %x) {
-; CHECK-LABEL: test_i128_tied:
-; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: #APP
-; CHECK-NEXT: #NO_APP
-; CHECK-NEXT: blr
+ ; PPC64-LABEL: name: test_i128_tied
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3, $x4
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x4
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY2:%[0-9]+]]:g8rc = COPY [[COPY1]]
+ ; PPC64-NEXT: [[COPY3:%[0-9]+]]:g8rc = COPY [[COPY]]
+ ; PPC64-NEXT: INLINEASM &"", sideeffect attdialect, regdef-ec:G8RC, def early-clobber %2, def early-clobber %3, reguse tiedto:$0, [[COPY2]](tied-def 3), [[COPY3]](tied-def 4)
+ ; PPC64-NEXT: $x3 = COPY %2
+ ; PPC64-NEXT: $x4 = COPY %3
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm, implicit $x3, implicit $x4
+ ;
+ ; PPC32-LABEL: name: test_i128_tied
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3, $r4, $r5, $r6
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r6
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r5
+ ; PPC32-NEXT: [[COPY2:%[0-9]+]]:gprc = COPY $r4
+ ; PPC32-NEXT: [[COPY3:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY4:%[0-9]+]]:gprc = COPY [[COPY3]]
+ ; PPC32-NEXT: [[COPY5:%[0-9]+]]:gprc = COPY [[COPY2]]
+ ; PPC32-NEXT: [[COPY6:%[0-9]+]]:gprc = COPY [[COPY1]]
+ ; PPC32-NEXT: [[COPY7:%[0-9]+]]:gprc = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"", sideeffect attdialect, regdef-ec:GPRC, def early-clobber %4, def early-clobber %5, def early-clobber %6, def early-clobber %7, reguse tiedto:$0, [[COPY4]](tied-def 3), [[COPY5]](tied-def 4), [[COPY6]](tied-def 5), [[COPY7]](tied-def 6)
+ ; PPC32-NEXT: $r3 = COPY %4
+ ; PPC32-NEXT: $r4 = COPY %5
+ ; PPC32-NEXT: $r5 = COPY %6
+ ; PPC32-NEXT: $r6 = COPY %7
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm, implicit $r3, implicit $r4, implicit $r5, implicit $r6
entry:
%r = call i128 asm sideeffect "", "=&r,0"(i128 %x)
ret i128 %r
}
define void @test_i128_input_b(i128 %x) {
-; CHECK-LABEL: test_i128_input_b:
-; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: #APP
-; CHECK-NEXT: # 3
-; CHECK-NEXT: #NO_APP
-; CHECK-NEXT: blr
+ ; PPC64-LABEL: name: test_i128_input_b
+ ; PPC64: bb.0.entry:
+ ; PPC64-NEXT: liveins: $x3, $x4
+ ; PPC64-NEXT: {{ $}}
+ ; PPC64-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY $x4
+ ; PPC64-NEXT: [[COPY1:%[0-9]+]]:g8rc = COPY $x3
+ ; PPC64-NEXT: [[COPY2:%[0-9]+]]:g8rc_nox0 = COPY [[COPY1]]
+ ; PPC64-NEXT: [[COPY3:%[0-9]+]]:g8rc_nox0 = COPY [[COPY]]
+ ; PPC64-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:G8RC_NOX0, [[COPY2]], [[COPY3]]
+ ; PPC64-NEXT: BLR8 implicit $lr8, implicit $rm
+ ;
+ ; PPC32-LABEL: name: test_i128_input_b
+ ; PPC32: bb.0.entry:
+ ; PPC32-NEXT: liveins: $r3, $r4, $r5, $r6
+ ; PPC32-NEXT: {{ $}}
+ ; PPC32-NEXT: [[COPY:%[0-9]+]]:gprc = COPY $r6
+ ; PPC32-NEXT: [[COPY1:%[0-9]+]]:gprc = COPY $r5
+ ; PPC32-NEXT: [[COPY2:%[0-9]+]]:gprc = COPY $r4
+ ; PPC32-NEXT: [[COPY3:%[0-9]+]]:gprc = COPY $r3
+ ; PPC32-NEXT: [[COPY4:%[0-9]+]]:gprc_nor0 = COPY [[COPY3]]
+ ; PPC32-NEXT: [[COPY5:%[0-9]+]]:gprc_nor0 = COPY [[COPY2]]
+ ; PPC32-NEXT: [[COPY6:%[0-9]+]]:gprc_nor0 = COPY [[COPY1]]
+ ; PPC32-NEXT: [[COPY7:%[0-9]+]]:gprc_nor0 = COPY [[COPY]]
+ ; PPC32-NEXT: INLINEASM &"# $0", sideeffect attdialect, reguse:GPRC_NOR0, [[COPY4]], [[COPY5]], [[COPY6]], [[COPY7]]
+ ; PPC32-NEXT: BLR implicit $lr, implicit $rm
entry:
call void asm sideeffect "# $0", "b"(i128 %x)
ret void
More information about the llvm-branch-commits
mailing list