Skip to content

Commit 01e872d

Browse files
committed
[DAGCombiner] Relax nsz constraint for FP optimizations
Some floating-point optimization don't trigger because they can produce incorrect results around signed zeros, and rely on the existence of the nsz flag which commonly appears when fast-math is enabled. However, this flag is not a hard requirement when all of the users of the combined value are either guaranteed to overwrite the sign-bit or simply ignore it (comparisons, etc.). The optimizations affected: - fadd x, +0.0 -> x - fsub x, -0.0 -> x - fsub +0.0, x -> fneg x - fdiv(x, sqrt(x)) -> sqrt(x) - frem lowering with power-of-2 divisors
1 parent 853ed3b commit 01e872d

File tree

6 files changed

+132
-10
lines changed

6 files changed

+132
-10
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,12 @@ class SelectionDAG {
23262326
/// +nan are considered positive, -0.0, -inf and -nan are not.
23272327
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const;
23282328

2329+
/// Check if a use of a float value is insensitive to signed zeros.
2330+
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const;
2331+
2332+
/// Check if at most two uses of a value are insensitive to signed zeros.
2333+
LLVM_ABI bool canIgnoreSignBitOfZero(SDValue Op) const;
2334+
23292335
/// Test whether two SDValues are known to compare equal. This
23302336
/// is true if they are the same value, or if one is negative zero and the
23312337
/// other positive zero.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17869,7 +17869,8 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
1786917869
// N0 + -0.0 --> N0 (also allowed with +0.0 and fast-math)
1787017870
ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, true);
1787117871
if (N1C && N1C->isZero())
17872-
if (N1C->isNegative() || Flags.hasNoSignedZeros())
17872+
if (N1C->isNegative() || Flags.hasNoSignedZeros() ||
17873+
DAG.canIgnoreSignBitOfZero(SDValue(N, 0)))
1787317874
return N0;
1787417875

1787517876
if (SDValue NewSel = foldBinOpIntoSelect(N))
@@ -18081,7 +18082,8 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) {
1808118082

1808218083
// (fsub A, 0) -> A
1808318084
if (N1CFP && N1CFP->isZero()) {
18084-
if (!N1CFP->isNegative() || Flags.hasNoSignedZeros()) {
18085+
if (!N1CFP->isNegative() || Flags.hasNoSignedZeros() ||
18086+
DAG.canIgnoreSignBitOfZero(SDValue(N, 0))) {
1808518087
return N0;
1808618088
}
1808718089
}
@@ -18094,7 +18096,8 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) {
1809418096

1809518097
// (fsub -0.0, N1) -> -N1
1809618098
if (N0CFP && N0CFP->isZero()) {
18097-
if (N0CFP->isNegative() || Flags.hasNoSignedZeros()) {
18099+
if (N0CFP->isNegative() || Flags.hasNoSignedZeros() ||
18100+
DAG.canIgnoreSignBitOfZero(SDValue(N, 0))) {
1809818101
// We cannot replace an FSUB(+-0.0,X) with FNEG(X) when denormals are
1809918102
// flushed to zero, unless all users treat denorms as zero (DAZ).
1810018103
// FIXME: This transform will change the sign of a NaN and the behavior
@@ -18744,7 +18747,8 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) {
1874418747
}
1874518748

1874618749
// Fold X/Sqrt(X) -> Sqrt(X)
18747-
if (Flags.hasNoSignedZeros() && Flags.hasAllowReassociation())
18750+
if ((Flags.hasNoSignedZeros() || DAG.canIgnoreSignBitOfZero(SDValue(N, 0))) &&
18751+
Flags.hasAllowReassociation())
1874818752
if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0))
1874918753
return N1;
1875018754

@@ -18795,8 +18799,9 @@ SDValue DAGCombiner::visitFREM(SDNode *N) {
1879518799
TLI.isOperationLegalOrCustom(ISD::FDIV, VT) &&
1879618800
TLI.isOperationLegalOrCustom(ISD::FTRUNC, VT) &&
1879718801
DAG.isKnownToBeAPowerOfTwoFP(N1)) {
18798-
bool NeedsCopySign =
18799-
!Flags.hasNoSignedZeros() && !DAG.cannotBeOrderedNegativeFP(N0);
18802+
bool NeedsCopySign = !Flags.hasNoSignedZeros() &&
18803+
!DAG.cannotBeOrderedNegativeFP(N0) &&
18804+
!DAG.canIgnoreSignBitOfZero(SDValue(N, 0));
1880018805
SDValue Div = DAG.getNode(ISD::FDIV, DL, VT, N0, N1);
1880118806
SDValue Rnd = DAG.getNode(ISD::FTRUNC, DL, VT, Div);
1880218807
SDValue MLA;

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,6 +6118,46 @@ bool SelectionDAG::cannotBeOrderedNegativeFP(SDValue Op) const {
61186118
llvm_unreachable("covered opcode switch");
61196119
}
61206120

6121+
bool SelectionDAG::canIgnoreSignBitOfZero(const SDUse &Use) const {
6122+
assert(Use.getValueType().isFloatingPoint());
6123+
const SDNode *User = Use.getUser();
6124+
unsigned OperandNo = Use.getOperandNo();
6125+
// Check if this use is insensitive to the sign of zero
6126+
switch (User->getOpcode()) {
6127+
case ISD::SETCC:
6128+
// Comparisons: IEEE-754 specifies +0.0 == -0.0.
6129+
case ISD::FABS:
6130+
// fabs always produces +0.0.
6131+
return true;
6132+
case ISD::FCOPYSIGN:
6133+
// copysign overwrites the sign bit of the first operand.
6134+
return OperandNo == 0;
6135+
case ISD::FADD:
6136+
case ISD::FSUB: {
6137+
// Arithmetic with non-zero constants fixes the uncertainty around the
6138+
// sign bit.
6139+
SDValue Other = User->getOperand(1 - OperandNo);
6140+
return isKnownNeverZeroFloat(Other);
6141+
}
6142+
case ISD::FP_TO_SINT:
6143+
case ISD::FP_TO_UINT:
6144+
// fp-to-int conversions normalize signed zeros.
6145+
return true;
6146+
default:
6147+
return false;
6148+
}
6149+
}
6150+
6151+
bool SelectionDAG::canIgnoreSignBitOfZero(SDValue Op) const {
6152+
// FIXME: Limit the amount of checked uses to not introduce a compile-time
6153+
// regression. Ideally, this should be implemented as a demanded-bits
6154+
// optimization that stems from the users.
6155+
if (Op->use_size() > 2)
6156+
return false;
6157+
return all_of(Op->uses(),
6158+
[&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6159+
}
6160+
61216161
bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
61226162
// Check the obvious case.
61236163
if (A == B) return true;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=aarch64 | FileCheck %s
3+
4+
; Test that nsz constraint can be bypassed when all uses are sign-insensitive.
5+
6+
define i1 @test_fadd_neg_zero_fcmp(float %x) {
7+
; CHECK-LABEL: test_fadd_neg_zero_fcmp:
8+
; CHECK: // %bb.0:
9+
; CHECK-NEXT: fmov s1, #1.00000000
10+
; CHECK-NEXT: fcmp s0, s1
11+
; CHECK-NEXT: cset w0, eq
12+
; CHECK-NEXT: ret
13+
%add = fadd float %x, -0.0
14+
%cmp = fcmp oeq float %add, 1.0
15+
ret i1 %cmp
16+
}
17+
18+
define float @test_fsub_zero_fabs(float %x) {
19+
; CHECK-LABEL: test_fsub_zero_fabs:
20+
; CHECK: // %bb.0:
21+
; CHECK-NEXT: fabs s0, s0
22+
; CHECK-NEXT: ret
23+
%sub = fsub float %x, 0.0
24+
%abs = call float @llvm.fabs.f32(float %sub)
25+
ret float %abs
26+
}
27+
28+
define float @test_fsub_neg_zero_copysign(float %x, float %y) {
29+
; CHECK-LABEL: test_fsub_neg_zero_copysign:
30+
; CHECK: // %bb.0:
31+
; CHECK-NEXT: mvni v2.4s, #128, lsl #24
32+
; CHECK-NEXT: // kill: def $s0 killed $s0 def $q0
33+
; CHECK-NEXT: // kill: def $s1 killed $s1 def $q1
34+
; CHECK-NEXT: bif v0.16b, v1.16b, v2.16b
35+
; CHECK-NEXT: // kill: def $s0 killed $s0 killed $q0
36+
; CHECK-NEXT: ret
37+
%sub = fsub float -0.0, %x
38+
%copysign = call float @llvm.copysign.f32(float %sub, float %y)
39+
ret float %copysign
40+
}
41+
42+
define i1 @test_div_sqrt_fcmp(float %x) {
43+
; CHECK-LABEL: test_div_sqrt_fcmp:
44+
; CHECK: // %bb.0:
45+
; CHECK-NEXT: fsqrt s0, s0
46+
; CHECK-NEXT: fcmp s0, #0.0
47+
; CHECK-NEXT: cset w0, gt
48+
; CHECK-NEXT: ret
49+
%sqrt = call float @llvm.sqrt.f32(float %x)
50+
%div = fdiv reassoc float %x, %sqrt
51+
%cmp = fcmp ogt float %div, 0.0
52+
ret i1 %cmp
53+
}
54+
55+
define float @test_frem_fabs(float %x) {
56+
; CHECK-LABEL: test_frem_fabs:
57+
; CHECK: // %bb.0:
58+
; CHECK-NEXT: fmov s1, #0.50000000
59+
; CHECK-NEXT: fmov s2, #-2.00000000
60+
; CHECK-NEXT: fmul s1, s0, s1
61+
; CHECK-NEXT: frintz s1, s1
62+
; CHECK-NEXT: fmadd s0, s1, s2, s0
63+
; CHECK-NEXT: fabs s0, s0
64+
; CHECK-NEXT: ret
65+
%rem = frem float %x, 2.0
66+
%abs = call float @llvm.fabs.f32(float %rem)
67+
ret float %abs
68+
}
69+
70+
declare float @llvm.fabs.f32(float)
71+
declare float @llvm.copysign.f32(float, float)
72+
declare float @llvm.sqrt.f32(float)

llvm/test/CodeGen/AMDGPU/fcanonicalize-elimination.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ define amdgpu_kernel void @test_fold_canonicalize_fabs_value_f32(ptr addrspace(1
371371
%id = tail call i32 @llvm.amdgcn.workitem.id.x()
372372
%gep = getelementptr inbounds float, ptr addrspace(1) %arg, i32 %id
373373
%load = load float, ptr addrspace(1) %gep, align 4
374-
%v0 = fadd float %load, 0.0
374+
%v0 = fadd float %load, 1.0
375375
%v = tail call float @llvm.fabs.f32(float %v0)
376376
%canonicalized = tail call float @llvm.canonicalize.f32(float %v)
377377
store float %canonicalized, ptr addrspace(1) %gep, align 4

llvm/test/CodeGen/AMDGPU/swdev380865.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ define amdgpu_kernel void @_Z6kernelILi4000ELi1EEvPd(ptr addrspace(1) %x.coerce)
2828
; CHECK-NEXT: v_mov_b32_e32 v1, s7
2929
; CHECK-NEXT: .LBB0_1: ; %for.cond4.preheader
3030
; CHECK-NEXT: ; =>This Inner Loop Header: Depth=1
31-
; CHECK-NEXT: v_add_f64 v[0:1], v[0:1], 0
3231
; CHECK-NEXT: s_mov_b32 s6, 0
3332
; CHECK-NEXT: s_mov_b32 s7, 0x40140000
34-
; CHECK-NEXT: s_add_i32 s1, s1, s0
35-
; CHECK-NEXT: s_cmpk_lt_i32 s1, 0xa00
3633
; CHECK-NEXT: v_add_f64 v[0:1], v[0:1], s[6:7]
3734
; CHECK-NEXT: s_mov_b32 s6, 0
3835
; CHECK-NEXT: s_mov_b32 s7, 0x40180000
36+
; CHECK-NEXT: s_add_i32 s1, s1, s0
37+
; CHECK-NEXT: s_cmpk_lt_i32 s1, 0xa00
3938
; CHECK-NEXT: v_add_f64 v[0:1], v[0:1], s[6:7]
4039
; CHECK-NEXT: s_mov_b32 s6, 0
4140
; CHECK-NEXT: s_mov_b32 s7, 0x401c0000

0 commit comments

Comments
 (0)