[llvm] [ValueTracking] frem in computeKnownFPClass can not return +/-Inf (PR #186748)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 16 01:23:11 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: None (134ARG)

<details>
<summary>Changes</summary>

`frem` only produces finite numbers or NaN, never +/-Inf. Before the patch `computeKnownFPClass` failed to clear the `fcInf` mask for `Instruction::FRem`, causing potential missed optimizations.

Fix #<!-- -->186746.

---
Full diff: https://github.com/llvm/llvm-project/pull/186748.diff


2 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+4) 
- (added) llvm/test/Transforms/InstCombine/frem-inf.ll (+26) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d71fccf8e3223..f6560149d3692 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5656,6 +5656,10 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
   case Instruction::FRem: {
     const bool WantNan = (InterestedClasses & fcNan) != fcNone;
 
+    if (Op->getOpcode() == Instruction::FRem) {
+      Known.knownNot(fcInf);
+    }
+
     if (Op->getOperand(0) == Op->getOperand(1) &&
         isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
       if (Op->getOpcode() == Instruction::FDiv) {
diff --git a/llvm/test/Transforms/InstCombine/frem-inf.ll b/llvm/test/Transforms/InstCombine/frem-inf.ll
new file mode 100644
index 0000000000000..58ebc8d3e4d5f
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/frem-inf.ll
@@ -0,0 +1,26 @@
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+declare i1 @llvm.is.fpclass.f64(double, i32)
+
+define i1 @test_frem_is_fpclass_inf(double %a, double %b) {
+; CHECK-LABEL: @test_frem_is_fpclass_inf(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %rem = frem double %a, %b
+  ; 516 checks for +inf (512) or -inf (4)
+  %is.inf = call i1 @llvm.is.fpclass.f64(double %rem, i32 516)
+  ret i1 %is.inf
+}
+
+define i1 @test_frem_fcmp_inf(double %a, double %b) {
+; CHECK-LABEL: @test_frem_fcmp_inf(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %rem = frem double %a, %b
+  %is.inf = fcmp oeq double %rem, 0x7FF0000000000000
+  ret i1 %is.inf
+}

``````````

</details>


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


More information about the llvm-commits mailing list