[clang] [llvm] [Mips] Set _MIPS_SPFPSET to 16 before MIPS32 with 32-bit FPRs (PR #246579)

Brad Smith via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 27 02:37:17 PST 2026


https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/246579

>From 8bdf70496a0db80a663fbe3baebd4ace2cabac17 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Tue, 24 Nov 2026 14:13:03 -0500
Subject: [PATCH] [Mips] Set _MIPS_SPFPSET to 16 before MIPS32 with 32-bit FPRs

Before MIPS32, single-precision arithmetic can't use the odd FPRs when
the FPRs are 32 bits wide. The backend doesn't use them there, but Clang
still defined _MIPS_SPFPSET as 32 for MIPS I, and for MIPS II to MIPS IV
with -mfp32. Define it as 16 there, as GCC does, and pass nooddspreg to
the backend so that the two agree.

Also match GCC in a few more cases, in Clang and in the backend. Only
the R5900 has 32 single-precision FPRs, so other single-float FPUs no
longer use the odd FPRs either, and soft-float no longer changes the
default. An explicit -modd-spreg still allows them.

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 clang/lib/Basic/Targets/Mips.h                | 13 ++++++++
 clang/test/Preprocessor/init-mips.c           | 32 +++++++++++++++++++
 llvm/lib/Target/Mips/MipsSubtarget.cpp        | 14 +++++---
 .../CodeGen/Mips/pre-mips32-no-odd-spreg.ll   | 12 +++++--
 4 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index 16ad603dbbb985..8113e9be34ec1c 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -316,6 +316,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
     bool OddSpregGiven = false;
     bool StrictAlign = false;
     bool FpGiven = false;
+    bool IsR5900 = false;
 
     for (const auto &Feature : Features) {
       if (Feature == "+single-float")
@@ -361,6 +362,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
         IsNoABICalls = true;
       else if (Feature == "+use-indirect-jump-hazard")
         UseIndirectJumpHazard = true;
+      else if (Feature == "+r5900")
+        IsR5900 = true;
       else if (Feature == "+nooddspreg") {
         NoOddSpreg = true;
         OddSpregGiven = false;
@@ -381,6 +384,16 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
       Features.push_back("+fp64");
     }
 
+    // Before MIPS32, single-precision arithmetic can't use the odd FPRs when
+    // the FPRs are 32 bits wide. The R5900 has 32 single-precision FPRs. Like
+    // GCC, -modd-spreg still allows them. Pass nooddspreg on so that the
+    // backend agrees.
+    if (ABI == "o32" && ISALevel < 32 && FPMode != FP64 && !IsR5900 &&
+        !OddSpregGiven && !NoOddSpreg) {
+      NoOddSpreg = true;
+      Features.push_back("+nooddspreg");
+    }
+
     resetDataLayout();
 
     return true;
diff --git a/clang/test/Preprocessor/init-mips.c b/clang/test/Preprocessor/init-mips.c
index 78391a73d40c0f..749eb0d56764e6 100644
--- a/clang/test/Preprocessor/init-mips.c
+++ b/clang/test/Preprocessor/init-mips.c
@@ -1935,6 +1935,38 @@
 // MIPS-XXR6:#define __mips_fpr 64
 // MIPS-XXR6:#define __mips_nan2008 1
 
+// Before MIPS32, the odd FPRs can't be used for single-precision arithmetic
+// when the FPRs are 32 bits wide, unless -modd-spreg is given. The R5900 has
+// 32 single-precision FPRs.
+//
+// RUN: %clang_cc1 -target-cpu mips1 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature +single-float \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature +soft-float \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips2 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// RUN: %clang_cc1 -target-cpu mips4 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-16 %s
+// SPFPSET-16:#define _MIPS_SPFPSET 16
+//
+// RUN: %clang_cc1 -target-cpu mips3 \
+// RUN:   -E -dM -triple=mips64-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// RUN: %clang_cc1 -target-cpu mips1 -target-feature -nooddspreg \
+// RUN:   -E -dM -triple=mips-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// RUN: %clang_cc1 -target-cpu r5900 -target-feature -fp64 \
+// RUN:   -E -dM -triple=mipsel-none-none < /dev/null \
+// RUN:   | FileCheck -match-full-lines -check-prefix SPFPSET-32 %s
+// SPFPSET-32:#define _MIPS_SPFPSET 32
+
 // RUN: %clang_cc1 -target-cpu mips32 \
 // RUN:   -E -dM -triple=mips-unknown-netbsd -mrelocation-model pic < /dev/null \
 // RUN:   | FileCheck -match-full-lines -check-prefix MIPS-ABICALLS-NETBSD %s
diff --git a/llvm/lib/Target/Mips/MipsSubtarget.cpp b/llvm/lib/Target/Mips/MipsSubtarget.cpp
index 147da4a3f4dd72..ce5fb5524e05bf 100644
--- a/llvm/lib/Target/Mips/MipsSubtarget.cpp
+++ b/llvm/lib/Target/Mips/MipsSubtarget.cpp
@@ -253,10 +253,16 @@ MipsSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
 
   // Before MIPS32, single-precision arithmetic can only use the even FPRs
   // when the FPRs are 32 bits wide. The odd FPRs only hold the upper half of
-  // a double. A single-float FPU such as the R5900 has 32 single-precision
-  // FPRs.
-  if (isABI_O32() && !IsSoftFloat && !IsSingleFloat && hasMips1() &&
-      !hasMips32() && !isFP64bit())
+  // a double. The R5900 has 32 single-precision FPRs. Like GCC, an explicit
+  // -nooddspreg (-modd-spreg in Clang) still allows them.
+  bool OddSPRegGiven = false;
+  for (StringRef Feature : llvm::split(FS, ','))
+    if (Feature == "-nooddspreg")
+      OddSPRegGiven = true;
+    else if (Feature == "+nooddspreg")
+      OddSPRegGiven = false;
+  if (isABI_O32() && !IsR5900 && hasMips1() && !hasMips32() && !isFP64bit() &&
+      !OddSPRegGiven)
     UseOddSPReg = false;
 
   if (InMips16Mode && !IsSoftFloat)
diff --git a/llvm/test/CodeGen/Mips/pre-mips32-no-odd-spreg.ll b/llvm/test/CodeGen/Mips/pre-mips32-no-odd-spreg.ll
index 847fd709219fb2..0452c84745478e 100644
--- a/llvm/test/CodeGen/Mips/pre-mips32-no-odd-spreg.ll
+++ b/llvm/test/CodeGen/Mips/pre-mips32-no-odd-spreg.ll
@@ -1,5 +1,6 @@
 ; Before MIPS32, single-precision arithmetic can't use the odd FPRs when the
-; FPRs are 32 bits wide.
+; FPRs are 32 bits wide, unless -nooddspreg is given explicitly, as with GCC's
+; -modd-spreg.
 
 ; RUN: llc -mtriple=mipsel -mcpu=mips1 < %s | FileCheck %s --check-prefix=EVEN
 ; RUN: llc -mtriple=mipsel -mcpu=mips2 < %s | FileCheck %s --check-prefix=EVEN
@@ -7,10 +8,16 @@
 ; RUN:   | FileCheck %s --check-prefix=EVEN
 ; RUN: llc -mtriple=mipsel -mcpu=mips4 -mattr=-fp64 < %s \
 ; RUN:   | FileCheck %s --check-prefix=EVEN
+; RUN: llc -mtriple=mipsel -mcpu=mips1 -mattr=+single-float < %s \
+; RUN:   | FileCheck %s --check-prefix=EVEN
+; RUN: llc -mtriple=mipsel -mcpu=mips1 -mattr=+soft-float < %s \
+; RUN:   | FileCheck %s --check-prefix=SOFT
 
 ; RUN: llc -mtriple=mipsel -mcpu=mips32 < %s | FileCheck %s --check-prefix=ODD
 ; RUN: llc -mtriple=mipsel -mcpu=mips3 < %s | FileCheck %s --check-prefix=ODD
-; RUN: llc -mtriple=mipsel -mcpu=mips1 -mattr=+single-float < %s \
+; RUN: llc -mtriple=mipsel -mcpu=mips1 -mattr=-nooddspreg < %s \
+; RUN:   | FileCheck %s --check-prefix=ODD
+; RUN: llc -mtriple=mipsel -mcpu=mips2 -mattr=-fp64,-nooddspreg < %s \
 ; RUN:   | FileCheck %s --check-prefix=ODD
 ; RUN: llc -mtriple=mipsel -mcpu=r5900 < %s | FileCheck %s --check-prefix=R5900
 
@@ -20,6 +27,7 @@
 ; RUN:   | llvm-readobj -A - | FileCheck %s --check-prefix=FLAGS-ODD
 
 ; EVEN:     .module nooddspreg
+; SOFT:     .module nooddspreg
 ; ODD-NOT:  .module nooddspreg
 ; R5900-NOT: .module nooddspreg
 



More information about the cfe-commits mailing list