[llvm] [CodeGen] Mark read_register of allocatable physreg as live-in (PR #200825)

Adhemerval Zanella via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 06:56:25 PDT 2026


https://github.com/zatrazz created https://github.com/llvm/llvm-project/pull/200825

Reading an allocatable register via llvm.read_register / llvm.read_volatile_register lowers to a COPY from a physical register that might never be defined in the function. With LLVM_ENABLE_EXPENSIVE_CHECKS the machine verifier rejects this as "Using an undefined physical register" (e.g. AArch64 read-fp-reg.ll reading d5).

Declare the register as a function live-in in Select_READ_REGISTER, the same way incoming physreg arguments are modelled, so the COPY no longer uses an undefined register. Reserved registers (e.g. the stack pointer) are exempt and need no live-in.

The same "Using an undefined physical register" verifier error was hit for Mips readcyclecounter.ll, which was first xfailed over with "XFAIL: expensive_checks" (b8054104d36d) and then properly fixed by reserving the hardware register (0c809ea33614, "[Mips] Reserve hardware register HWR2"). Reserving is not an option for allocatable registers, so marking them live-in is the analogous root-cause fix here.

>From dd1df917a05eda4b317e5f939d86f0e7c0c34aaa Mon Sep 17 00:00:00 2001
From: Adhemerval Zanella <zatrazz at gmail.com>
Date: Fri, 29 May 2026 15:23:11 -0300
Subject: [PATCH] [CodeGen] Mark read_register of allocatable physreg as
 live-in

Reading an allocatable register via llvm.read_register /
llvm.read_volatile_register lowers to a COPY from a physical register that
might never be defined in the function. With LLVM_ENABLE_EXPENSIVE_CHECKS
the machine verifier rejects this as "Using an undefined physical register"
(e.g. AArch64 read-fp-reg.ll reading d5).

Declare the register as a function live-in in Select_READ_REGISTER, the
same way incoming physreg arguments are modelled, so the COPY no longer
uses an undefined register. Reserved registers (e.g. the stack pointer)
are exempt and need no live-in.

The same "Using an undefined physical register" verifier error was hit for
Mips readcyclecounter.ll, which was first xfailed over with
"XFAIL: expensive_checks" (b8054104d36d) and then properly fixed by
reserving the hardware register (0c809ea33614, "[Mips] Reserve hardware
register HWR2"). Reserving is not an option for allocatable registers, so
marking them live-in is the analogous root-cause fix here.
---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 3dc599b556ddd..c295982f9f9de 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -2544,6 +2544,17 @@ void SelectionDAGISel::Select_READ_REGISTER(SDNode *Op) {
   } else {
     New =
         CurDAG->getCopyFromReg(Op->getOperand(0), dl, Reg, Op->getValueType(0));
+    // Reading an allocatable physical register copies a value that is not
+    // defined in this function. Declare it as a live-in so the resulting COPY
+    // does not use an undefined physical register (which the machine verifier
+    // rejects). Reserved registers (e.g. the stack pointer) are exempt and
+    // need no live-in.
+    MachineFunction &MFn = CurDAG->getMachineFunction();
+    MachineRegisterInfo &MRI = MFn.getRegInfo();
+    const TargetRegisterInfo *TRI = MFn.getSubtarget().getRegisterInfo();
+    if (Reg.isPhysical() && !TRI->getReservedRegs(MFn).test(Reg) &&
+        !MRI.isLiveIn(Reg))
+      MRI.addLiveIn(Reg);
   }
 
   New->setNodeId(-1);



More information about the llvm-commits mailing list