[llvm] [RegisterCoalescer] The COPY with the implicit-def of super register is not coalescable. (PR #169997)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Nov 29 05:47:27 PST 2025
https://github.com/dianqk created https://github.com/llvm/llvm-project/pull/169997
Fixes (after reland https://github.com/llvm/llvm-project/pull/168353 again) #169996.
#168353 will transform `%1:gr64 = SUBREG_TO_REG 0, %2:gr32, %subreg.sub_32bit` to `undef %1.sub_32bit:gr64_with_sub_8bit = COPY %0.sub_32bit, implicit-def %1` to remain the zero extended semantics, but RegisterCoalescer doesn't check `implicit-def`.
>From 03d3ebea64ca3867b6a9cc6ef04bf44e5e450f4b Mon Sep 17 00:00:00 2001
From: dianqk <dianqk at dianqk.net>
Date: Sat, 29 Nov 2025 21:14:49 +0800
Subject: [PATCH 1/2] Pre-commit test cases
---
.../CodeGen/X86/coalesce-implicit-def.mir | 20 +++++++++++++++++++
llvm/test/CodeGen/X86/pr169996.ll | 19 ++++++++++++++++++
2 files changed, 39 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/coalesce-implicit-def.mir
create mode 100644 llvm/test/CodeGen/X86/pr169996.ll
diff --git a/llvm/test/CodeGen/X86/coalesce-implicit-def.mir b/llvm/test/CodeGen/X86/coalesce-implicit-def.mir
new file mode 100644
index 0000000000000..952af13a680d0
--- /dev/null
+++ b/llvm/test/CodeGen/X86/coalesce-implicit-def.mir
@@ -0,0 +1,20 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -run-pass register-coalescer -mtriple x86_64-unknown-linux-gnu -o - %s | FileCheck %s
+
+---
+name: fn1
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $rdi
+
+ ; CHECK-LABEL: name: fn1
+ ; CHECK: liveins: $rdi
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:gr64_with_sub_8bit = COPY $rdi
+ ; CHECK-NEXT: $rax = COPY [[COPY]]
+ %0:gr64_with_sub_8bit = COPY $rdi
+ undef %1.sub_32bit:gr64_with_sub_8bit = COPY %0.sub_32bit, implicit-def %1
+ $rax = COPY %1
+...
+
diff --git a/llvm/test/CodeGen/X86/pr169996.ll b/llvm/test/CodeGen/X86/pr169996.ll
new file mode 100644
index 0000000000000..71e50489afd4d
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pr169996.ll
@@ -0,0 +1,19 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
+
+; FIXME: The first instruction should be `movl %edi, %eax`.
+
+define i64 @fn1(i64 %arg, ptr %arg1) {
+; CHECK-LABEL: fn1:
+; CHECK: # %bb.0:
+; CHECK-NEXT: movq %rdi, %rax
+; CHECK-NEXT: movb (%rsi), %al
+; CHECK-NEXT: retq
+ %i = trunc i64 %arg to i32
+ %i4 = load i8, ptr %arg1
+ %i5 = zext i8 %i4 to i32
+ %i6 = and i32 %i, -256
+ %i7 = or i32 %i6, %i5
+ %i12 = zext i32 %i7 to i64
+ ret i64 %i12
+}
>From ffea9ce47960daf042b77e86355a006ce3c3ab75 Mon Sep 17 00:00:00 2001
From: dianqk <dianqk at dianqk.net>
Date: Sat, 29 Nov 2025 21:34:09 +0800
Subject: [PATCH 2/2] [RegisterCoalescer] The COPY with the implicit-def of
super register is not coalescable.
---
llvm/lib/CodeGen/RegisterCoalescer.cpp | 9 +++++++++
llvm/test/CodeGen/X86/coalesce-implicit-def.mir | 6 +++++-
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index e624088a0964e..ebf50bc366cf1 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -507,6 +507,15 @@ bool CoalescerPair::setRegisters(const MachineInstr *MI) {
if (Src == Dst && SrcSub != DstSub)
return false;
+ // The implicit-def of the super register is zero extended.
+ for (unsigned I = MI->getDesc().getNumOperands(),
+ E = MI->getNumOperands();
+ I != E; ++I) {
+ const MachineOperand &MO = MI->getOperand(I);
+ if (MO.isReg() && MO.isDef() && MO.getReg() == Dst)
+ return false;
+ }
+
NewRC = TRI.getCommonSuperRegClass(SrcRC, SrcSub, DstRC, DstSub, SrcIdx,
DstIdx);
if (!NewRC)
diff --git a/llvm/test/CodeGen/X86/coalesce-implicit-def.mir b/llvm/test/CodeGen/X86/coalesce-implicit-def.mir
index 952af13a680d0..bd9de3b933394 100644
--- a/llvm/test/CodeGen/X86/coalesce-implicit-def.mir
+++ b/llvm/test/CodeGen/X86/coalesce-implicit-def.mir
@@ -1,6 +1,9 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
# RUN: llc -run-pass register-coalescer -mtriple x86_64-unknown-linux-gnu -o - %s | FileCheck %s
+# Checks that we do not merge %1 into %0.
+# `undef %1.sub_32bit = COPY %0.sub_32bit, implicit-def %1` is zero extended.
+
---
name: fn1
tracksRegLiveness: true
@@ -12,7 +15,8 @@ body: |
; CHECK: liveins: $rdi
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[COPY:%[0-9]+]]:gr64_with_sub_8bit = COPY $rdi
- ; CHECK-NEXT: $rax = COPY [[COPY]]
+ ; CHECK-NEXT: undef [[COPY1:%[0-9]+]].sub_32bit:gr64_with_sub_8bit = COPY [[COPY]].sub_32bit, implicit-def [[COPY1]]
+ ; CHECK-NEXT: $rax = COPY [[COPY1]]
%0:gr64_with_sub_8bit = COPY $rdi
undef %1.sub_32bit:gr64_with_sub_8bit = COPY %0.sub_32bit, implicit-def %1
$rax = COPY %1
More information about the llvm-commits
mailing list