[llvm] CodeGen: Use getVRegDef in salvageCopySSAImpl (PR #219518)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 09:34:04 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/219518

Look up the defining instruction of a virtual register with getVRegDef instead
of the parent of its first def operand, so this no longer depends on
MachineOperand::getParent(). getVRegDef already asserts there is at most one
def, so replace the hasOneDef assertion with a null check.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>

>From 3fb5187fe39bbf6b0eded838564ccedf1df267d5 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 28 Aug 2026 18:28:54 +0200
Subject: [PATCH] CodeGen: Use getVRegDef in salvageCopySSAImpl

Look up the defining instruction of a virtual register with getVRegDef instead
of the parent of its first def operand, so this no longer depends on
MachineOperand::getParent(). getVRegDef already asserts there is at most one
def, so replace the hasOneDef assertion with a null check.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>
---
 llvm/lib/CodeGen/MachineFunction.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp
index 144f165e17973..05b47698fb7ff 100644
--- a/llvm/lib/CodeGen/MachineFunction.cpp
+++ b/llvm/lib/CodeGen/MachineFunction.cpp
@@ -1200,14 +1200,14 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
     if (State.second)
       SubregsSeen.push_back(State.second);
 
-    assert(MRI.hasOneDef(State.first));
-    MachineInstr &Inst = *MRI.def_begin(State.first)->getParent();
-    CurInst = Inst.getIterator();
+    MachineInstr *Inst = MRI.getVRegDef(State.first);
+    assert(Inst && "Virtual register has no def");
+    CurInst = Inst->getIterator();
 
     // Any non-copy instruction is the defining instruction we're seeking.
-    if (!Inst.isCopyLike() && !TII.isCopyLikeInstr(Inst))
+    if (!Inst->isCopyLike() && !TII.isCopyLikeInstr(*Inst))
       break;
-    State = GetRegAndSubreg(Inst);
+    State = GetRegAndSubreg(*Inst);
   };
 
   // Helper lambda to apply additional subregister substitutions to a known
@@ -1233,7 +1233,7 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
   // instruction / operand pair after adding subregister qualifiers.
   if (State.first.isVirtual()) {
     // Virtual register def -- we can just look up where this happens.
-    MachineInstr *Inst = MRI.def_begin(State.first)->getParent();
+    MachineInstr *Inst = MRI.getVRegDef(State.first);
     for (auto &MO : Inst->all_defs()) {
       if (MO.getReg() != State.first)
         continue;



More information about the llvm-commits mailing list