[llvm] [CodeGen] Fix partial def handling in HandlePhysRegUse and FindLastPartialDef (PR #186437)
Kiroo via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 3 12:03:19 PDT 2026
https://github.com/zlfn updated https://github.com/llvm/llvm-project/pull/186437
>From 0d6484bba48907c6b66013ed8c8d3c0dc7206b08 Mon Sep 17 00:00:00 2001
From: zlfn <ung at zlfn.space>
Date: Sat, 14 Mar 2026 01:13:13 +0900
Subject: [PATCH 1/4] [LiveVariables] Fix FindLastPartialDef missing partial
def at Dist=0
`FindLastPartialDef` initializes `LastDefDist` to 0, but `DistanceMap`
also assigns 0 to the first instruction in a block. The strict
comparison `Dist > LastDefDist` never matches at Dist=0, so a partial
def at the start of a block is silently skipped.
Fix by accepting the first valid candidate unconditionally.
Fixes #156428
Assisted-by: Claude Code
---
llvm/lib/CodeGen/LiveVariables.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/LiveVariables.cpp b/llvm/lib/CodeGen/LiveVariables.cpp
index 246d538332af7..8ce9bbc5c1547 100644
--- a/llvm/lib/CodeGen/LiveVariables.cpp
+++ b/llvm/lib/CodeGen/LiveVariables.cpp
@@ -222,8 +222,8 @@ MachineInstr *LiveVariables::FindLastPartialDef(Register Reg) {
if (!Def)
continue;
unsigned Dist = DistanceMap[Def];
- if (Dist > LastDefDist) {
- LastDef = Def;
+ if (!LastDef || Dist > LastDefDist) {
+ LastDef = Def;
LastDefDist = Dist;
}
}
>From e9920df64a3e4c97b58c789404805ec7d3f7bf49 Mon Sep 17 00:00:00 2001
From: zlfn <ung at zlfn.space>
Date: Sat, 14 Mar 2026 01:13:31 +0900
Subject: [PATCH 2/4] [LiveVariables] Add missing implicit uses in
HandlePhysRegUse for partial defs
`HandlePhysRegUse` adds `implicit-def $superreg` on the last partial
def instruction but does not add implicit uses for sub-registers defined
by earlier instructions. Without these, MachineCopyPropagation treats
the super-register def as clobbering all sub-registers and incorrectly
eliminates prior definitions as dead copies.
Fix by adding implicit uses for sub-registers whose definitions differ
from `LastPartialDef`, matching the behavior already described in the
existing comment (`implicit-def EAX, implicit killed AH`).
Fixes #156428
Assisted-by: Claude Code
---
llvm/lib/CodeGen/LiveVariables.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/lib/CodeGen/LiveVariables.cpp b/llvm/lib/CodeGen/LiveVariables.cpp
index 8ce9bbc5c1547..1badab5709728 100644
--- a/llvm/lib/CodeGen/LiveVariables.cpp
+++ b/llvm/lib/CodeGen/LiveVariables.cpp
@@ -254,6 +254,15 @@ void LiveVariables::HandlePhysRegUse(Register Reg, MachineInstr &MI) {
if (LastPartialDef) {
LastPartialDef->addOperand(
MachineOperand::CreateReg(Reg, /*IsDef=*/true, /*IsImp=*/true));
+ // Add implicit uses for sub-registers that were defined by earlier
+ // instructions. Without these, later passes (e.g. MachineCopyPropagation)
+ // may think the earlier definitions are dead.
+ for (MCPhysReg SubReg : TRI->subregs(Reg)) {
+ if (PhysRegDef[SubReg] && PhysRegDef[SubReg] != LastPartialDef)
+ LastPartialDef->addOperand(MachineOperand::CreateReg(SubReg,
+ /*IsDef=*/false,
+ /*IsImp=*/true));
+ }
}
} else if (LastDef && !PhysRegUse[Reg.id()] &&
!LastDef->findRegisterDefOperand(Reg, /*TRI=*/nullptr))
>From 977802cb637e825c800616869fdfb98dc9769f14 Mon Sep 17 00:00:00 2001
From: zlfn <ung at zlfn.space>
Date: Thu, 26 Mar 2026 19:23:07 +0900
Subject: [PATCH 3/4] [AVR] Add regression test for spurious super-register
implicit-def
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Add a MIR test that demonstrates LiveVariables incorrectly adding
implicit-def of a super-register ($r25r24) when only a sub-register
($r24) is defined. On AVR, the two halves of every register pair are
independent — writing r24 does not clobber r25.
The spurious implicit-def causes DeadMachineInstrElim to remove a
live MOVRdRr to $r25, resulting in a subsequent read of an undefined
register — a silent miscompilation.
See https://github.com/llvm/llvm-project/issues/156428
---
.../AVR/livevars-subreg-implicit-def.mir | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
diff --git a/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir b/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
new file mode 100644
index 0000000000000..21a59ddc4cb25
--- /dev/null
+++ b/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
@@ -0,0 +1,34 @@
+# RUN: llc -mtriple=avr --run-pass=livevars,dead-mi-elimination -o - %s | FileCheck %s
+--- |
+ target triple = "avr--"
+ declare void @use()
+ define void @test_subreg_no_spurious_superreg_def() { ret void }
+...
+---
+# LiveVariables must not mark a sub-register def as killing the entire
+# super-register. On AVR the two halves of every register pair are
+# independent — writing r24 does not clobber r25.
+#
+# Previously, LiveVariables incorrectly added implicit-def $r25r24 to
+# LDIRdK, which made DeadMachineInstrElim think $r25 = MOVRdRr is dead
+# and remove it. The subsequent MOVRdRr of $r25 then reads an undefined
+# register — a miscompilation.
+#
+# See https://github.com/llvm/llvm-project/issues/156428
+name: test_subreg_no_spurious_superreg_def
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $r22
+
+ ; CHECK-LABEL: name: test_subreg_no_spurious_superreg_def
+ ; The MOVRdRr to $r25 must not be removed — $r20 needs its value.
+ ; CHECK: $r25 = MOVRdRr
+ ; CHECK: $r24 = LDIRdK 42
+ ; CHECK: $r20 = MOVRdRr $r25
+ $r25 = MOVRdRr $r22
+ $r24 = LDIRdK 42
+ $r20 = MOVRdRr $r25
+ RCALLk @use, implicit $r25r24, implicit $r20
+
+...
>From 34f241be5d25028964da206df89c755eb54b06c9 Mon Sep 17 00:00:00 2001
From: zlfn <ung at zlfn.space>
Date: Mon, 30 Mar 2026 12:53:39 +0900
Subject: [PATCH 4/4] [AVR] Use inline asm instead of function call in
regression test
Replace RCALLk @use with INLINEASM to avoid depending on AVR calling
convention details while still keeping the registers live.
---
llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir b/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
index 21a59ddc4cb25..f6e6d8b3409eb 100644
--- a/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
+++ b/llvm/test/CodeGen/AVR/livevars-subreg-implicit-def.mir
@@ -1,7 +1,6 @@
# RUN: llc -mtriple=avr --run-pass=livevars,dead-mi-elimination -o - %s | FileCheck %s
--- |
target triple = "avr--"
- declare void @use()
define void @test_subreg_no_spurious_superreg_def() { ret void }
...
---
@@ -29,6 +28,6 @@ body: |
$r25 = MOVRdRr $r22
$r24 = LDIRdK 42
$r20 = MOVRdRr $r25
- RCALLk @use, implicit $r25r24, implicit $r20
+ INLINEASM &"", 1 /* sideeffect attdialect */, 9 /* reguse */, $r25r24, 9 /* reguse */, $r20
...
More information about the llvm-commits
mailing list