[llvm] [SDAG] Constant fold frexp in signed way (PR #161015)
Hongyu Chen via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 27 10:40:55 PDT 2025
https://github.com/XChy created https://github.com/llvm/llvm-project/pull/161015
Fixes #160981
The exponential part of a floating-point number is signed. This patch prevents treating it as unsigned.
>From 9e46b00a7f2dbdbfd2483da2e976cd192673158f Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Sun, 28 Sep 2025 01:37:24 +0800
Subject: [PATCH] [SDAG] Constant fold frexp in signed way
---
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 4 ++--
llvm/test/CodeGen/X86/llvm.frexp.ll | 16 ++++++++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 7aa293af963e6..8fc7eabf90ea8 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -11161,8 +11161,8 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
APFloat FrexpMant =
frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
- SDValue Result1 =
- getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
+ SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
+ DL, VTList.VTs[1]);
return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
}
diff --git a/llvm/test/CodeGen/X86/llvm.frexp.ll b/llvm/test/CodeGen/X86/llvm.frexp.ll
index 83840dd85c533..e3a1b1b83b2e3 100644
--- a/llvm/test/CodeGen/X86/llvm.frexp.ll
+++ b/llvm/test/CodeGen/X86/llvm.frexp.ll
@@ -582,6 +582,22 @@ define i32 @test_frexp_f64_i32_only_use_exp(double %a) nounwind {
ret i32 %result.0
}
+define { float, i32 } @pr160981() {
+; X64-LABEL: pr160981:
+; X64: # %bb.0:
+; X64-NEXT: movss {{.*#+}} xmm0 = [9.9999988E-1,0.0E+0,0.0E+0,0.0E+0]
+; X64-NEXT: movl $-126, %eax
+; X64-NEXT: retq
+;
+; WIN32-LABEL: pr160981:
+; WIN32: # %bb.0:
+; WIN32-NEXT: flds __real at 3f7ffffe
+; WIN32-NEXT: movl $-126, %eax
+; WIN32-NEXT: retl
+ %ret = call { float, i32 } @llvm.frexp.f32.i32(float bitcast (i32 8388607 to float))
+ ret { float, i32 } %ret
+}
+
; FIXME: Widen vector result
; define { <2 x double>, <2 x i32> } @test_frexp_v2f64_v2i32(<2 x double> %a) nounwind {
; %result = call { <2 x double>, <2 x i32> } @llvm.frexp.v2f64.v2i32(<2 x double> %a)
More information about the llvm-commits
mailing list