[llvm] [Mips] Do not emit teq if NoZeroDivCheck in FastISel (PR #204386)

Folkert de Vries via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 11:55:14 PDT 2026


https://github.com/folkertdev updated https://github.com/llvm/llvm-project/pull/204386

>From 6d41c9c838e0f3247efefd0c54f1c59e240c1855 Mon Sep 17 00:00:00 2001
From: Xi Ruoyao <xry111 at xry111.site>
Date: Thu, 18 Jun 2026 00:54:08 +0800
Subject: [PATCH] [Mips] Do not emit teq if NoZeroDivCheck in FastISel

Fix the issue: -mno-check-zero-division is not respected at -O0 for
some test cases.
---
 llvm/lib/Target/Mips/MipsFastISel.cpp | 5 +++--
 llvm/test/CodeGen/Mips/divrem.ll      | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/Mips/MipsFastISel.cpp b/llvm/lib/Target/Mips/MipsFastISel.cpp
index 9645fb5293609..1519e9d52f8a7 100644
--- a/llvm/lib/Target/Mips/MipsFastISel.cpp
+++ b/llvm/lib/Target/Mips/MipsFastISel.cpp
@@ -74,6 +74,7 @@
 using namespace llvm;
 
 extern cl::opt<bool> EmitJalrReloc;
+extern cl::opt<bool> NoZeroDivCheck;
 
 namespace {
 
@@ -1952,8 +1953,8 @@ bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
     return false;
 
   emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
-  if (!isa<ConstantInt>(I->getOperand(1)) ||
-      dyn_cast<ConstantInt>(I->getOperand(1))->isZero()) {
+  if (!NoZeroDivCheck && (!isa<ConstantInt>(I->getOperand(1)) ||
+                          dyn_cast<ConstantInt>(I->getOperand(1))->isZero())) {
     emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
   }
 
diff --git a/llvm/test/CodeGen/Mips/divrem.ll b/llvm/test/CodeGen/Mips/divrem.ll
index a29f2793c081f..e30764fef9042 100644
--- a/llvm/test/CodeGen/Mips/divrem.ll
+++ b/llvm/test/CodeGen/Mips/divrem.ll
@@ -12,6 +12,8 @@
 ; RUN: llc -mtriple=mips64 -mcpu=mips64r2 -mno-check-zero-division -relocation-model=pic < %s | FileCheck %s -check-prefixes=ALL,ACC64,NOCHECK
 ; RUN: llc -mtriple=mips64 -mcpu=mips64r6 -mno-check-zero-division -relocation-model=pic < %s | FileCheck %s -check-prefixes=ALL,GPR64,NOCHECK
 
+; RUN: llc -mtriple=mipsel -mcpu=mips32 -O0 -mno-check-zero-division -relocation-model=pic < %s | FileCheck %s -check-prefixes=NOCHECK
+
 ; FileCheck Prefixes:
 ;   ALL - All targets
 ;   ACC32 - Accumulator based multiply/divide on 32-bit targets



More information about the llvm-commits mailing list