[llvm] edbd582 - [Mips] Select GPR register class based on requested type size (#220012)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 00:17:17 PDT 2026


Author: Bart Van Assche
Date: 2026-09-01T03:17:12-04:00
New Revision: edbd58272b4c41329782168b15564fea14687271

URL: https://github.com/llvm/llvm-project/commit/edbd58272b4c41329782168b15564fea14687271
DIFF: https://github.com/llvm/llvm-project/commit/edbd58272b4c41329782168b15564fea14687271.diff

LOG: [Mips] Select GPR register class based on requested type size (#220012)

In MipsTargetLowering::getRegisterByName(), the register class was
previously selected solely based on Subtarget.isGP64bit(), ignoring the
requested value type size (VT).

When compiling for a 64-bit MIPS CPU with the 32-bit o32 ABI (such as
-mcpu=mips3 -target-abi=o32), Subtarget.isGP64bit() is true even though
32-bit registers are expected. Reading a 32-bit named register like $gp
(e.g., via llvm.read_register.i32) returned a 64-bit register ($gp_64).
This resulted in a cross-register-class copy from GPR64 to GPR32 that
MipsSEInstrInfo::copyPhysReg() could not lower. In builds without
assertions, this constructed an invalid TargetOpcode::PHI (opcode 0)
instruction and caused a crash in MipsMCCodeEmitter during object file
emission.

Inspect VT.getSizeInBits() in getRegisterByName() to select
GPR32RegClassID for 32-bit types and GPR64RegClassID for 64-bit types
when supported by the subtarget, reporting an error for invalid types.

Fixes: 64dfc26237d9 ("[Mips] Support "$sp" named register (#171793)")

Signed-off-by: Bart Van Assche <bvanassche at acm.org>

Added: 
    llvm/test/CodeGen/Mips/named-register-mips3-o32.ll

Modified: 
    llvm/lib/Target/Mips/MipsISelLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/Mips/MipsISelLowering.cpp b/llvm/lib/Target/Mips/MipsISelLowering.cpp
index 31e184df78690..e29f71ae5ef27 100644
--- a/llvm/lib/Target/Mips/MipsISelLowering.cpp
+++ b/llvm/lib/Target/Mips/MipsISelLowering.cpp
@@ -5002,9 +5002,23 @@ MipsTargetLowering::getRegisterByName(const char *RegName, LLT VT,
   // 3. Get register.
   if (regIdx >= 0 && regIdx < 32) {
     const MCRegisterInfo *MRI = MF.getContext().getRegisterInfo();
-    const MCRegisterClass &RC = Subtarget.isGP64bit()
-                                    ? MRI->getRegClass(Mips::GPR64RegClassID)
-                                    : MRI->getRegClass(Mips::GPR32RegClassID);
+    unsigned RegClassID = Mips::GPR32RegClassID;
+    if (VT.isValid()) {
+      if (VT.getSizeInBits() == 64) {
+        if (!Subtarget.isGP64bit())
+          report_fatal_error("64-bit registers not supported on 32-bit target");
+        RegClassID = Mips::GPR64RegClassID;
+      } else if (VT.getSizeInBits() == 32) {
+        RegClassID = Mips::GPR32RegClassID;
+      } else {
+        report_fatal_error(Twine("Invalid register \"" + StringRef(RegName) +
+                                 "\" for " + Twine(VT.getSizeInBits()) +
+                                 "-bit type."));
+      }
+    } else if (Subtarget.isGP64bit()) {
+      RegClassID = Mips::GPR64RegClassID;
+    }
+    const MCRegisterClass &RC = MRI->getRegClass(RegClassID);
     return RC.getRegister(regIdx);
   }
 

diff  --git a/llvm/test/CodeGen/Mips/named-register-mips3-o32.ll b/llvm/test/CodeGen/Mips/named-register-mips3-o32.ll
new file mode 100644
index 0000000000000..b444f5e7a0c49
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/named-register-mips3-o32.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=mips -mcpu=mips3 -target-abi=o32 -relocation-model=static \
+; RUN:   -mattr=+noabicalls -verify-machineinstrs < %s | FileCheck %s
+; RUN: llc -mtriple=mips -mcpu=mips3 -target-abi=o32 -relocation-model=static \
+; RUN:   -mattr=+noabicalls -filetype=obj < %s
+
+declare i32 @llvm.read_register.i32(metadata)
+
+define i32 @get_gp() {
+; CHECK-LABEL: get_gp:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    jr $ra
+; CHECK-NEXT:    move $2, $gp
+  %1 = call i32 @llvm.read_register.i32(metadata !0)
+  ret i32 %1
+}
+
+define i32 @get_sp() {
+; CHECK-LABEL: get_sp:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    jr $ra
+; CHECK-NEXT:    move $2, $sp
+  %1 = call i32 @llvm.read_register.i32(metadata !1)
+  ret i32 %1
+}
+
+!llvm.named.register = !{!0, !1}
+!0 = !{!"$gp"}
+!1 = !{!"$sp"}


        


More information about the llvm-commits mailing list