[llvm] [Mips] Respect FPU hazards on MIPS-I to MIPS-III (PR #223411)

Christian Krause via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 22:11:42 PDT 2026


https://github.com/cckrause updated https://github.com/llvm/llvm-project/pull/223411

>From ba73363bbb84f75a66ccfed04f09947b87555c89 Mon Sep 17 00:00:00 2001
From: Christian <chris at christiankrause.de>
Date: Mon, 14 Sep 2026 15:31:58 +0200
Subject: [PATCH] generalize MIPS-I load-delay-slot hazard check to also cover
 FPU transfer delay slots (mfc1/cfc1 results read too early); recognize lwc1
 as a load-delay instruction so loads into FPU registers get the required nop;
 update/add tests to match

---
 llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp  | 93 +++++++++++--------
 llvm/lib/Target/Mips/MipsInstrInfo.cpp        |  4 +
 .../CodeGen/Mips/fpu-delay-in-delay-slot.ll   | 35 +++++++
 llvm/test/CodeGen/Mips/llvm-ir/fptosi.ll      | 40 +++++++-
 llvm/test/CodeGen/Mips/llvm-ir/load.ll        |  8 +-
 llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll  |  8 +-
 llvm/test/CodeGen/Mips/llvm-ir/select-flt.ll  |  8 +-
 .../test/CodeGen/Mips/mips1-fpu-load-delay.ll | 27 ++++++
 8 files changed, 175 insertions(+), 48 deletions(-)
 create mode 100644 llvm/test/CodeGen/Mips/fpu-delay-in-delay-slot.ll
 create mode 100644 llvm/test/CodeGen/Mips/mips1-fpu-load-delay.ll

diff --git a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
index 7d7dac22520ea..231f57085a36a 100644
--- a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
+++ b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
@@ -16,6 +16,7 @@
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerUnion.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
@@ -1101,6 +1102,37 @@ bool MipsDelaySlotFiller::examinePred(MachineBasicBlock &Pred,
   return true;
 }
 
+/// Moving an instruction whose result is not available to the next instruction
+/// into a delay slot moves that unprotected shadow across the branch: the
+/// instruction executed next is the branch target or the fall-through, neither
+/// of which is adjacent to the candidate in the listing. Returns true if the
+/// hazard would be exposed on any path. \p IsSafe decides whether one
+/// particular instruction may execute in the shadow of \p Candidate.
+static bool delayExposesHazard(
+    const BranchInformation &BranchInfo, const MachineInstr &Candidate,
+    function_ref<bool(const MachineInstr &, const MachineInstr &)> IsSafe) {
+  // With no branch, or a branch whose target we cannot see, we do not know
+  // which instruction executes next. Assume the worst.
+  if (!BranchInfo.hasBranchInstr() || BranchInfo.isIndirectBranch())
+    return true;
+
+  const MachineBasicBlock *TargetMBB = BranchInfo.getBranchTarget();
+  if (!TargetMBB || TargetMBB->empty())
+    return true;
+
+  bool Exposed = !IsSafe(TargetMBB->instr_front(), Candidate);
+
+  // A conditional branch also falls through, so the instruction after the
+  // delay slot can be the one that reads the result.
+  if (!BranchInfo.isUnconditionalBranch()) {
+    if (!BranchInfo.hasBranchElseInstr())
+      return true;
+    Exposed |= !IsSafe(*BranchInfo.getBranchElseInstr(), Candidate);
+  }
+
+  return Exposed;
+}
+
 bool MipsDelaySlotFiller::delayHasHazard(const MipsSubtarget &STI,
                                          const MachineInstr &Candidate,
                                          const BranchInformation &BranchInfo,
@@ -1114,49 +1146,28 @@ bool MipsDelaySlotFiller::delayHasHazard(const MipsSubtarget &STI,
   HasHazard |= IM.hasHazard(Candidate);
   HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
 
-  // This only matters for MIPS1 and only if we do not have a hazard already
-  if (STI.hasMips1() && !STI.hasMips2() && !HasHazard) {
+  if (!HasHazard) {
     const MipsInstrInfo *TII = STI.getInstrInfo();
-    const bool HasLoadDelaySlot = TII->HasLoadDelaySlot(Candidate);
-
-    // We only need to act if the candidate is having a load delay slot
-    if (HasLoadDelaySlot) {
-      // We have no branch so we can not determine a hazard
-      // Assume the worst
-      if (!BranchInfo.hasBranchInstr()) {
-        return true;
-      }
 
-      // Being an indirect branch means we can not tell if we are a hazard
-      // Assume the worst
-      if (BranchInfo.isIndirectBranch()) {
-        return true;
-      }
-
-      // If this is a direct branch we should find a MBB operand for the jump
-      // target
-      const MachineBasicBlock *TargetMBB = BranchInfo.getBranchTarget();
-      if (!TargetMBB || TargetMBB->empty()) {
-        return true;
-      }
-
-      const auto &BranchTargetInstr = TargetMBB->instr_front();
-      bool HasNewHazard =
-          !TII->SafeInLoadDelaySlot(BranchTargetInstr, Candidate);
-      // If the branch is unconditional then we do not need to bother to check
-      // the next instruction after the branch
-      if (!BranchInfo.isUnconditionalBranch()) {
-        // We are a conditional branch so we should have an `else` branch
-        if (BranchInfo.hasBranchElseInstr()) {
-          HasNewHazard |= !TII->SafeInLoadDelaySlot(
-              *BranchInfo.getBranchElseInstr(), Candidate);
-        } else {
-          // Without the `else` branch we need to assume the worst
-          HasNewHazard = true;
-        }
-      }
-      return HasNewHazard;
-    }
+    // MIPS-I has no load-use interlock: the instruction executed after a load
+    // must not read the loaded register.
+    if (STI.hasMips1() && !STI.hasMips2() && TII->HasLoadDelaySlot(Candidate))
+      return delayExposesHazard(
+          BranchInfo, Candidate,
+          [TII](const MachineInstr &InShadow, const MachineInstr &Cand) {
+            return TII->SafeInLoadDelaySlot(InShadow, Cand);
+          });
+
+    // MIPS-I to MIPS-III have the same problem for transfers out of the FPU.
+    // This mirrors the condition in
+    // MipsBranchExpansion::handleFPUDelaySlot(), which covers the
+    // straight-line case.
+    if (!STI.hasMips32() && !STI.hasMips4() && TII->HasFPUDelaySlot(Candidate))
+      return delayExposesHazard(
+          BranchInfo, Candidate,
+          [TII](const MachineInstr &InShadow, const MachineInstr &Cand) {
+            return TII->SafeInFPUDelaySlot(InShadow, Cand);
+          });
   }
 
   return HasHazard;
diff --git a/llvm/lib/Target/Mips/MipsInstrInfo.cpp b/llvm/lib/Target/Mips/MipsInstrInfo.cpp
index 20a0f1c2d77c0..b640969cbe27f 100644
--- a/llvm/lib/Target/Mips/MipsInstrInfo.cpp
+++ b/llvm/lib/Target/Mips/MipsInstrInfo.cpp
@@ -682,6 +682,10 @@ bool MipsInstrInfo::HasLoadDelaySlot(const MachineInstr &MI) const {
   case Mips::LW:
   case Mips::LWR:
   case Mips::LWL:
+  // A load into a coprocessor register has the same delay. The other FPU
+  // loads cannot appear on MIPS-I: LDC1 is MIPS-II and LWXC1 is MIPS-IV /
+  // MIPS32R2.
+  case Mips::LWC1:
     return true;
   default:
     return false;
diff --git a/llvm/test/CodeGen/Mips/fpu-delay-in-delay-slot.ll b/llvm/test/CodeGen/Mips/fpu-delay-in-delay-slot.ll
new file mode 100644
index 0000000000000..62371a5ad821f
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/fpu-delay-in-delay-slot.ll
@@ -0,0 +1,35 @@
+; RUN: llc < %s -mtriple=mipsel -mcpu=mips1 -relocation-model=static \
+; RUN:   | FileCheck %s -check-prefixes=ALL,DELAY
+; RUN: llc < %s -mtriple=mipsel -mcpu=mips2 -relocation-model=static \
+; RUN:   | FileCheck %s -check-prefixes=ALL,DELAY
+; RUN: llc < %s -mtriple=mipsel -mcpu=mips32 -relocation-model=static \
+; RUN:   | FileCheck %s -check-prefixes=ALL,INTERLOCK
+
+; On MIPS-I to MIPS-III a transfer out of the FPU is not interlocked: the
+; instruction executed after an "mfc1" must not read its destination. Filling
+; a branch delay slot with the mfc1 moves that shadow onto the branch target
+; and the fall-through, so the delay slot filler must not do it there.
+; From MIPS32 on the hardware interlocks and the slot may be filled.
+
+define i32 @xfer(float %a, float %b, i32 %sel) nounwind {
+; ALL-LABEL: xfer:
+entry:
+  %x = bitcast float %a to i32
+  %y = bitcast float %b to i32
+  %cmp = icmp ne i32 %sel, 0
+  br i1 %cmp, label %if.then, label %if.else
+
+if.then:
+  %add = add i32 %x, %y
+  ret i32 %add
+
+if.else:
+  %sub = sub i32 %x, %y
+  ret i32 %sub
+
+; The instruction in the branch delay slot must not be an mfc1.
+; DELAY:          {{b(ne|eq)z}} ${{[0-9]+}},
+; DELAY-NEXT:     nop
+; INTERLOCK:      {{b(ne|eq)z}} ${{[0-9]+}},
+; INTERLOCK-NEXT: mfc1
+}
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/fptosi.ll b/llvm/test/CodeGen/Mips/llvm-ir/fptosi.ll
index a5be8ca28bf00..e379619663078 100644
--- a/llvm/test/CodeGen/Mips/llvm-ir/fptosi.ll
+++ b/llvm/test/CodeGen/Mips/llvm-ir/fptosi.ll
@@ -14,7 +14,7 @@
 ; RUN: llc < %s -mtriple=mips-linux-gnu -mcpu=mips32r6 -asm-show-inst |\
 ; RUN:   FileCheck %s -check-prefixes=M32R6
 ; RUN: llc < %s -mtriple=mips64-linux-gnu -mcpu=mips3 -asm-show-inst |\
-; RUN:   FileCheck %s -check-prefixes=M64
+; RUN:   FileCheck %s -check-prefixes=M3
 ; RUN: llc < %s -mtriple=mips64-linux-gnu -mcpu=mips64 -asm-show-inst |\
 ; RUN:   FileCheck %s -check-prefixes=M64
 ; RUN: llc < %s -mtriple=mips64-linux-gnu -mcpu=mips64r2 -asm-show-inst |\
@@ -112,6 +112,25 @@ define i32 @test1(float %t) {
 ; M32R6-NEXT:    # <MCOperand Reg:V0>
 ; M32R6-NEXT:    # <MCOperand Reg:F0>>
 ;
+; M3-LABEL: test1:
+; M3:       # %bb.0: # %entry
+; M3-NEXT:    trunc.w.s $f0, $f12 # <MCInst #[[#MCINST1:]] TRUNC_W_S
+; M3-NEXT:    # <MCOperand Reg:F0>
+; M3-NEXT:    # <MCOperand Reg:F12>>
+; M3-NEXT:    mfc1 $2, $f0 # <MCInst #[[#MCINST3:]] MFC1
+; M3-NEXT:    # <MCOperand Reg:V0>
+; M3-NEXT:    # <MCOperand Reg:F0>>
+; M3-NEXT:    nop # <MCInst #[[#MCINST7:]] SLL
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Imm:0>>
+; M3-NEXT:    jr $ra # <MCInst #[[#MCINST2:]] JR
+; M3-NEXT:    # <MCOperand Reg:RA_64>>
+; M3-NEXT:    nop # <MCInst #[[#MCINST7]] SLL
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Imm:0>>
+;
 ; M64-LABEL: test1:
 ; M64:       # %bb.0: # %entry
 ; M64-NEXT:    trunc.w.s $f0, $f12 # <MCInst #[[#MCINST1:]] TRUNC_W_S
@@ -303,6 +322,25 @@ define i32 @test2(double %t) {
 ; M32R6-NEXT:    # <MCOperand Reg:V0>
 ; M32R6-NEXT:    # <MCOperand Reg:F0>>
 ;
+; M3-LABEL: test2:
+; M3:       # %bb.0: # %entry
+; M3-NEXT:    trunc.w.d $f0, $f12 # <MCInst #[[#MCINST23:]] TRUNC_W_D64
+; M3-NEXT:    # <MCOperand Reg:F0>
+; M3-NEXT:    # <MCOperand Reg:D12_64>>
+; M3-NEXT:    mfc1 $2, $f0 # <MCInst #[[#MCINST3]] MFC1
+; M3-NEXT:    # <MCOperand Reg:V0>
+; M3-NEXT:    # <MCOperand Reg:F0>>
+; M3-NEXT:    nop # <MCInst #[[#MCINST7]] SLL
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Imm:0>>
+; M3-NEXT:    jr $ra # <MCInst #[[#MCINST2]] JR
+; M3-NEXT:    # <MCOperand Reg:RA_64>>
+; M3-NEXT:    nop # <MCInst #[[#MCINST7]] SLL
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Reg:ZERO>
+; M3-NEXT:    # <MCOperand Imm:0>>
+;
 ; M64-LABEL: test2:
 ; M64:       # %bb.0: # %entry
 ; M64-NEXT:    trunc.w.d $f0, $f12 # <MCInst #[[#MCINST23:]] TRUNC_W_D64
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/load.ll b/llvm/test/CodeGen/Mips/llvm-ir/load.ll
index f0200fb09fcea..204decb37c638 100644
--- a/llvm/test/CodeGen/Mips/llvm-ir/load.ll
+++ b/llvm/test/CodeGen/Mips/llvm-ir/load.ll
@@ -1489,12 +1489,16 @@ define float @f8() {
 ; MIPS1-PSX-NEXT:    lui $1, %hi(e) # <MCInst #[[#MCINST1]] LUi
 ; MIPS1-PSX-NEXT:    # <MCOperand Reg:AT>
 ; MIPS1-PSX-NEXT:    # <MCOperand Expr:%hi(e)>>
-; MIPS1-PSX-NEXT:    jr $ra # <MCInst #[[#MCINST2]] JR
-; MIPS1-PSX-NEXT:    # <MCOperand Reg:RA>>
 ; MIPS1-PSX-NEXT:    lwc1 $f0, %lo(e)($1) # <MCInst #[[#MCINST28:]] LWC1
 ; MIPS1-PSX-NEXT:    # <MCOperand Reg:F0>
 ; MIPS1-PSX-NEXT:    # <MCOperand Reg:AT>
 ; MIPS1-PSX-NEXT:    # <MCOperand Expr:%lo(e)>>
+; MIPS1-PSX-NEXT:    jr $ra # <MCInst #[[#MCINST2]] JR
+; MIPS1-PSX-NEXT:    # <MCOperand Reg:RA>>
+; MIPS1-PSX-NEXT:    nop # <MCInst #[[#MCINST13]] SLL
+; MIPS1-PSX-NEXT:    # <MCOperand Reg:ZERO>
+; MIPS1-PSX-NEXT:    # <MCOperand Reg:ZERO>
+; MIPS1-PSX-NEXT:    # <MCOperand Imm:0>>
 entry:
   %0 = load float, ptr @e
   ret float %0
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll b/llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll
index ca91fa18ecd2c..4f56ed2932a0d 100644
--- a/llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll
+++ b/llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll
@@ -45,8 +45,10 @@ define double @tst_select_i1_double(i1 signext %s, double %x, double %y) {
 ; M2-NEXT:    nop
 ; M2-NEXT:  $BB0_2:
 ; M2-NEXT:    mtc1 $7, $f0
-; M2-NEXT:    jr $ra
+; M2-NEXT:    nop
 ; M2-NEXT:    mtc1 $6, $f1
+; M2-NEXT:    jr $ra
+; M2-NEXT:    nop
 ;
 ; CMOV32R1-LABEL: tst_select_i1_double:
 ; CMOV32R1:       # %bb.0: # %entry
@@ -145,8 +147,10 @@ define double @tst_select_i1_double(i1 signext %s, double %x, double %y) {
 ; MIPS1-PSX-NEXT:    nop
 ; MIPS1-PSX-NEXT:  $BB0_2:
 ; MIPS1-PSX-NEXT:    mtc1 $6, $f0
-; MIPS1-PSX-NEXT:    jr $ra
+; MIPS1-PSX-NEXT:    nop
 ; MIPS1-PSX-NEXT:    mtc1 $7, $f1
+; MIPS1-PSX-NEXT:    jr $ra
+; MIPS1-PSX-NEXT:    nop
 entry:
   %r = select i1 %s, double %x, double %y
   ret double %r
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/select-flt.ll b/llvm/test/CodeGen/Mips/llvm-ir/select-flt.ll
index cc16097a4f038..56108e453d3e0 100644
--- a/llvm/test/CodeGen/Mips/llvm-ir/select-flt.ll
+++ b/llvm/test/CodeGen/Mips/llvm-ir/select-flt.ll
@@ -37,11 +37,15 @@ define float @tst_select_i1_float(i1 signext %s, float %x, float %y) {
 ; M2-NEXT:    bnez $1, $BB0_2
 ; M2-NEXT:    nop
 ; M2-NEXT:  # %bb.1: # %entry
-; M2-NEXT:    jr $ra
 ; M2-NEXT:    mtc1 $6, $f0
-; M2-NEXT:  $BB0_2:
+; M2-NEXT:    nop
 ; M2-NEXT:    jr $ra
+; M2-NEXT:    nop
+; M2-NEXT:  $BB0_2:
 ; M2-NEXT:    mtc1 $5, $f0
+; M2-NEXT:    nop
+; M2-NEXT:    jr $ra
+; M2-NEXT:    nop
 ;
 ; CMOV32R1-LABEL: tst_select_i1_float:
 ; CMOV32R1:       # %bb.0: # %entry
diff --git a/llvm/test/CodeGen/Mips/mips1-fpu-load-delay.ll b/llvm/test/CodeGen/Mips/mips1-fpu-load-delay.ll
new file mode 100644
index 0000000000000..51ddbfe746992
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/mips1-fpu-load-delay.ll
@@ -0,0 +1,27 @@
+; RUN: llc < %s -mtriple=mipsel -mcpu=mips1 -relocation-model=static \
+; RUN:   | FileCheck %s -check-prefixes=ALL,MIPS1
+; RUN: llc < %s -mtriple=mipsel -mcpu=mips2 -relocation-model=static \
+; RUN:   | FileCheck %s -check-prefixes=ALL,MIPS2
+
+; MIPS-I has no load-use interlock, and that applies to loads into a
+; coprocessor register too: the instruction after an "lwc1" must not read the
+; loaded register. From MIPS-II on the hardware interlocks and no nop is
+; needed.
+
+ at v = external global [4 x float]
+
+define float @sum3() nounwind {
+; ALL-LABEL: sum3:
+entry:
+  %0 = load float, ptr @v
+  %1 = load float, ptr getelementptr inbounds ([4 x float], ptr @v, i32 0, i32 1)
+  %add = fadd float %0, %1
+  %2 = load float, ptr getelementptr inbounds ([4 x float], ptr @v, i32 0, i32 2)
+  %add2 = fadd float %add, %2
+  ret float %add2
+
+; ALL:        lwc1 $f{{[0-9]+}}
+; MIPS1:      nop
+; MIPS2-NOT:  nop
+; ALL:        add.s
+}



More information about the llvm-commits mailing list