[SDAG] fix miscompile when casting int->FP->int

This is the codegen equivalent of D124692.

As shown in https://github.com/llvm/llvm-project/issues/55150 -
the existing fold may be wrong when converting to a signed value.
This is a quick fix to avoid the miscompile.
https://alive2.llvm.org/ce/z/KtaDmd

Differential Revision: https://reviews.llvm.org/D124771
This commit is contained in:
Sanjay Patel
2022-05-02 12:48:02 -04:00
parent 4070aa0156
commit 747c6a0c73
3 changed files with 17 additions and 8 deletions

View File

@@ -15527,7 +15527,7 @@ static SDValue FoldIntToFPToInt(SDNode *N, SelectionDAG &DAG) {
// This means this is also safe for a signed input and unsigned output, since
// a negative input would lead to undefined behavior.
unsigned InputSize = (int)SrcVT.getScalarSizeInBits() - IsInputSigned;
unsigned OutputSize = (int)VT.getScalarSizeInBits() - IsOutputSigned;
unsigned OutputSize = (int)VT.getScalarSizeInBits();
unsigned ActualSize = std::min(InputSize, OutputSize);
const fltSemantics &sem = DAG.EVTToAPFloatSemantics(N0.getValueType());

View File

@@ -45,10 +45,13 @@ define i32 @u32_f32_u24_u32(i32 %a) {
ret i32 %r
}
; This requires converting to FP and back.
define i32 @s32_f32_s25_s32(i32 %a) {
; CHECK-LABEL: s32_f32_s25_s32:
; CHECK: // %bb.0:
; CHECK-NEXT: sbfx w0, w0, #0, #25
; CHECK-NEXT: scvtf s0, w0
; CHECK-NEXT: fcvtzs w0, s0
; CHECK-NEXT: ret
%f = sitofp i32 %a to float
%i = fptosi float %f to i25
@@ -68,10 +71,13 @@ define i32 @s32_f32_u25_u32(i32 %a) {
ret i32 %r
}
; TODO: This could avoid converting to FP.
define i32 @u32_f32_s25_s32(i32 %a) {
; CHECK-LABEL: u32_f32_s25_s32:
; CHECK: // %bb.0:
; CHECK-NEXT: sbfx w0, w0, #0, #25
; CHECK-NEXT: ucvtf s0, w0
; CHECK-NEXT: fcvtzs w0, s0
; CHECK-NEXT: ret
%f = uitofp i32 %a to float
%i = fptosi float %f to i25

View File

@@ -82,12 +82,13 @@ define i32 @u32_f32_u24_u32(i32 %a) {
ret i32 %r
}
; This requires converting to FP and back.
define i32 @s32_f32_s25_s32(i32 %a) {
; CHECK-LABEL: s32_f32_s25_s32:
; CHECK: # %bb.0:
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: shll $7, %eax
; CHECK-NEXT: sarl $7, %eax
; CHECK-NEXT: cvtsi2ss %edi, %xmm0
; CHECK-NEXT: cvttss2si %xmm0, %eax
; CHECK-NEXT: retq
%f = sitofp i32 %a to float
%i = fptosi float %f to i25
@@ -107,12 +108,14 @@ define i32 @s32_f32_u25_u32(i32 %a) {
ret i32 %r
}
; TODO: This could avoid converting to FP.
define i32 @u32_f32_s25_s32(i32 %a) {
; CHECK-LABEL: u32_f32_s25_s32:
; CHECK: # %bb.0:
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: shll $7, %eax
; CHECK-NEXT: sarl $7, %eax
; CHECK-NEXT: cvtsi2ss %rax, %xmm0
; CHECK-NEXT: cvttss2si %xmm0, %eax
; CHECK-NEXT: retq
%f = uitofp i32 %a to float
%i = fptosi float %f to i25