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

Adhemerval Zanella via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 04:31:32 PDT 2026


================
@@ -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);
----------------
zatrazz wrote:

Yeah, that's my WIP approach, where reading outside of the basic block is diagnosed as an error rather than producing invalid IR. I am checking if it makes more sense to make it as an warning. 

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


More information about the llvm-commits mailing list