Skip to content

Commit 94d3ff0

Browse files
author
Jessica Paquette
committed
[GlobalISel] Don't use G_FPTOSI in G_ISNAN legalization
As noted in the comments in D108227, using G_FPTOSI produces wrong results for G_ISNAN. Drop the G_FPTOSI and perform the operation on integer types. Elsewhere in LLVM, a bitcast would be the appropriate choice (as it is in SDAG). GlobalISel does not distinguish between integer and FP types, so a bitcast would be meaningless here.
1 parent 22c3841 commit 94d3ff0

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -7420,10 +7420,9 @@ LegalizerHelper::LegalizeResult LegalizerHelper::lowerIsNaN(MachineInstr &MI) {
74207420

74217421
// NaN has all exp bits set and a non zero significand. Therefore:
74227422
// isnan(V) == exp mask < abs(V)
7423-
auto FPToSI = MIRBuilder.buildFPTOSI(SrcTy, Src);
74247423
auto Mask = APInt::getSignedMaxValue(SrcTy.getScalarSizeInBits());
74257424
auto MaskCst = MIRBuilder.buildConstant(SrcTy, Mask);
7426-
auto AbsV = MIRBuilder.buildAnd(SrcTy, FPToSI, MaskCst);
7425+
auto AbsV = MIRBuilder.buildAnd(SrcTy, Src, MaskCst);
74277426
auto *FloatTy = getFloatTypeForLLT(MI.getMF()->getFunction().getContext(),
74287427
SrcTy.getScalarType());
74297428
if (!FloatTy)

llvm/test/CodeGen/AArch64/GlobalISel/legalize-isnan.mir

+4-8
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ body: |
6161
; CHECK-LABEL: name: scalar_no_flags
6262
; CHECK: liveins: $h0
6363
; CHECK: %val:_(s16) = COPY $h0
64-
; CHECK: [[FPEXT:%[0-9]+]]:_(s32) = G_FPEXT %val(s16)
65-
; CHECK: [[FPTOSI:%[0-9]+]]:_(s32) = G_FPTOSI [[FPEXT]](s32)
64+
; CHECK: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT %val(s16)
6665
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 32767
67-
; CHECK: [[AND:%[0-9]+]]:_(s32) = G_AND [[FPTOSI]], [[C]]
66+
; CHECK: [[AND:%[0-9]+]]:_(s32) = G_AND [[ANYEXT]], [[C]]
6867
; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 31744
6968
; CHECK: [[SEXT_INREG:%[0-9]+]]:_(s32) = G_SEXT_INREG [[AND]], 16
7069
; CHECK: [[ICMP:%[0-9]+]]:_(s32) = G_ICMP intpred(slt), [[C1]](s32), [[SEXT_INREG]]
@@ -89,16 +88,13 @@ body: |
8988
; CHECK-LABEL: name: vector_no_flags
9089
; CHECK: liveins: $d0
9190
; CHECK: %val:_(<4 x s16>) = COPY $d0
92-
; CHECK: [[FPTOSI:%[0-9]+]]:_(<4 x s16>) = G_FPTOSI %val(<4 x s16>)
9391
; CHECK: [[C:%[0-9]+]]:_(s16) = G_CONSTANT i16 32767
9492
; CHECK: [[BUILD_VECTOR:%[0-9]+]]:_(<4 x s16>) = G_BUILD_VECTOR [[C]](s16), [[C]](s16), [[C]](s16), [[C]](s16)
95-
; CHECK: [[AND:%[0-9]+]]:_(<4 x s16>) = G_AND [[FPTOSI]], [[BUILD_VECTOR]]
93+
; CHECK: [[AND:%[0-9]+]]:_(<4 x s16>) = G_AND %val, [[BUILD_VECTOR]]
9694
; CHECK: [[C1:%[0-9]+]]:_(s16) = G_CONSTANT i16 31744
9795
; CHECK: [[BUILD_VECTOR1:%[0-9]+]]:_(<4 x s16>) = G_BUILD_VECTOR [[C1]](s16), [[C1]](s16), [[C1]](s16), [[C1]](s16)
9896
; CHECK: [[ICMP:%[0-9]+]]:_(<4 x s16>) = G_ICMP intpred(slt), [[BUILD_VECTOR1]](<4 x s16>), [[AND]]
99-
; CHECK: %isnan:_(<4 x s1>) = G_TRUNC [[ICMP]](<4 x s16>)
100-
; CHECK: %ext:_(<4 x s16>) = G_ANYEXT %isnan(<4 x s1>)
101-
; CHECK: $d0 = COPY %ext(<4 x s16>)
97+
; CHECK: $d0 = COPY [[ICMP]](<4 x s16>)
10298
; CHECK: RET_ReallyLR implicit $d0
10399
%val:_(<4 x s16>) = COPY $d0
104100
%isnan:_(<4 x s1>) = G_ISNAN %val(<4 x s16>)

0 commit comments

Comments
 (0)