mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 13:11:39 +01:00
[DAG] use SDNode flags 'nsz' to enable fadd/fsub with zero folds
As discussed in D24815, let's start the process of killing off the broken fast-math global state housed in TargetOptions and eliminate the need for function-level fast-math attributes. Here we enable two similar folds that are possible when we don't care about signed-zero: fadd nsz x, 0 --> x fsub nsz 0, x --> -x Note that although the test cases include a 'sin' function call, I'm side-stepping the FMF-on-calls question (and lack of support in the DAG) for now. It's not needed for these tests - isNegatibleForFree/GetNegatedExpression just look through a ISD::FSIN node. Also, when we create an FNEG node and propagate the Flags of the FSUB to it, this doesn't actually do anything today because Flags are silently dropped for any node that is not a binary operator. Differential Revision: https://reviews.llvm.org/D25297 llvm-svn: 284824
This commit is contained in:
parent
da9223b586
commit
80150ed05d
@ -622,7 +622,8 @@ static char isNegatibleForFree(SDValue Op, bool LegalOperations,
|
|||||||
Depth + 1);
|
Depth + 1);
|
||||||
case ISD::FSUB:
|
case ISD::FSUB:
|
||||||
// We can't turn -(A-B) into B-A when we honor signed zeros.
|
// We can't turn -(A-B) into B-A when we honor signed zeros.
|
||||||
if (!Options->UnsafeFPMath) return 0;
|
if (!Options->UnsafeFPMath && !Op.getNode()->getFlags()->hasNoSignedZeros())
|
||||||
|
return 0;
|
||||||
|
|
||||||
// fold (fneg (fsub A, B)) -> (fsub B, A)
|
// fold (fneg (fsub A, B)) -> (fsub B, A)
|
||||||
return 1;
|
return 1;
|
||||||
@ -685,9 +686,6 @@ static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
|
|||||||
LegalOperations, Depth+1),
|
LegalOperations, Depth+1),
|
||||||
Op.getOperand(0), Flags);
|
Op.getOperand(0), Flags);
|
||||||
case ISD::FSUB:
|
case ISD::FSUB:
|
||||||
// We can't turn -(A-B) into B-A when we honor signed zeros.
|
|
||||||
assert(Options.UnsafeFPMath);
|
|
||||||
|
|
||||||
// fold (fneg (fsub 0, B)) -> B
|
// fold (fneg (fsub 0, B)) -> B
|
||||||
if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
|
if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
|
||||||
if (N0CFP->isZero())
|
if (N0CFP->isZero())
|
||||||
@ -8462,17 +8460,20 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
|
|||||||
return DAG.getNode(ISD::FSUB, DL, VT, N1,
|
return DAG.getNode(ISD::FSUB, DL, VT, N1,
|
||||||
GetNegatedExpression(N0, DAG, LegalOperations), Flags);
|
GetNegatedExpression(N0, DAG, LegalOperations), Flags);
|
||||||
|
|
||||||
|
// FIXME: Auto-upgrade the target/function-level option.
|
||||||
|
if (Options.UnsafeFPMath || N->getFlags()->hasNoSignedZeros()) {
|
||||||
|
// fold (fadd A, 0) -> A
|
||||||
|
if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1))
|
||||||
|
if (N1C->isZero())
|
||||||
|
return N0;
|
||||||
|
}
|
||||||
|
|
||||||
// If 'unsafe math' is enabled, fold lots of things.
|
// If 'unsafe math' is enabled, fold lots of things.
|
||||||
if (Options.UnsafeFPMath) {
|
if (Options.UnsafeFPMath) {
|
||||||
// No FP constant should be created after legalization as Instruction
|
// No FP constant should be created after legalization as Instruction
|
||||||
// Selection pass has a hard time dealing with FP constants.
|
// Selection pass has a hard time dealing with FP constants.
|
||||||
bool AllowNewConst = (Level < AfterLegalizeDAG);
|
bool AllowNewConst = (Level < AfterLegalizeDAG);
|
||||||
|
|
||||||
// fold (fadd A, 0) -> A
|
|
||||||
if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1))
|
|
||||||
if (N1C->isZero())
|
|
||||||
return N0;
|
|
||||||
|
|
||||||
// fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
|
// fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
|
||||||
if (N1CFP && N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
|
if (N1CFP && N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
|
||||||
isConstantFPBuildVectorOrConstantFP(N0.getOperand(1)))
|
isConstantFPBuildVectorOrConstantFP(N0.getOperand(1)))
|
||||||
@ -8599,19 +8600,22 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) {
|
|||||||
return DAG.getNode(ISD::FADD, DL, VT, N0,
|
return DAG.getNode(ISD::FADD, DL, VT, N0,
|
||||||
GetNegatedExpression(N1, DAG, LegalOperations), Flags);
|
GetNegatedExpression(N1, DAG, LegalOperations), Flags);
|
||||||
|
|
||||||
// If 'unsafe math' is enabled, fold lots of things.
|
// FIXME: Auto-upgrade the target/function-level option.
|
||||||
if (Options.UnsafeFPMath) {
|
if (Options.UnsafeFPMath || N->getFlags()->hasNoSignedZeros()) {
|
||||||
// (fsub A, 0) -> A
|
|
||||||
if (N1CFP && N1CFP->isZero())
|
|
||||||
return N0;
|
|
||||||
|
|
||||||
// (fsub 0, B) -> -B
|
// (fsub 0, B) -> -B
|
||||||
if (N0CFP && N0CFP->isZero()) {
|
if (N0CFP && N0CFP->isZero()) {
|
||||||
if (isNegatibleForFree(N1, LegalOperations, TLI, &Options))
|
if (isNegatibleForFree(N1, LegalOperations, TLI, &Options))
|
||||||
return GetNegatedExpression(N1, DAG, LegalOperations);
|
return GetNegatedExpression(N1, DAG, LegalOperations);
|
||||||
if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
|
if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
|
||||||
return DAG.getNode(ISD::FNEG, DL, VT, N1);
|
return DAG.getNode(ISD::FNEG, DL, VT, N1, Flags);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If 'unsafe math' is enabled, fold lots of things.
|
||||||
|
if (Options.UnsafeFPMath) {
|
||||||
|
// (fsub A, 0) -> A
|
||||||
|
if (N1CFP && N1CFP->isZero())
|
||||||
|
return N0;
|
||||||
|
|
||||||
// (fsub x, x) -> 0.0
|
// (fsub x, x) -> 0.0
|
||||||
if (N0 == N1)
|
if (N0 == N1)
|
||||||
|
@ -23,21 +23,13 @@ define double @strict(double %e) nounwind {
|
|||||||
ret double %h
|
ret double %h
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME:
|
|
||||||
; 'fast' implies no-signed-zeros, so the negates fold away.
|
; 'fast' implies no-signed-zeros, so the negates fold away.
|
||||||
; The 'sin' does not need any fast-math-flags for this transform.
|
; The 'sin' does not need any fast-math-flags for this transform.
|
||||||
|
|
||||||
define double @fast(double %e) nounwind {
|
define double @fast(double %e) nounwind {
|
||||||
; CHECK-LABEL: fast:
|
; CHECK-LABEL: fast:
|
||||||
; CHECK: # BB#0:
|
; CHECK: # BB#0:
|
||||||
; CHECK-NEXT: pushq %rax
|
; CHECK-NEXT: jmp sin
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: callq sin
|
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: popq %rax
|
|
||||||
; CHECK-NEXT: retq
|
|
||||||
;
|
;
|
||||||
%f = fsub fast double 0.0, %e
|
%f = fsub fast double 0.0, %e
|
||||||
%g = call double @sin(double %f) readonly
|
%g = call double @sin(double %f) readonly
|
||||||
@ -45,20 +37,12 @@ define double @fast(double %e) nounwind {
|
|||||||
ret double %h
|
ret double %h
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME:
|
|
||||||
; No-signed-zeros is all that we need for this transform.
|
; No-signed-zeros is all that we need for this transform.
|
||||||
|
|
||||||
define double @nsz(double %e) nounwind {
|
define double @nsz(double %e) nounwind {
|
||||||
; CHECK-LABEL: nsz:
|
; CHECK-LABEL: nsz:
|
||||||
; CHECK: # BB#0:
|
; CHECK: # BB#0:
|
||||||
; CHECK-NEXT: pushq %rax
|
; CHECK-NEXT: jmp sin
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: callq sin
|
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: popq %rax
|
|
||||||
; CHECK-NEXT: retq
|
|
||||||
;
|
;
|
||||||
%f = fsub nsz double 0.0, %e
|
%f = fsub nsz double 0.0, %e
|
||||||
%g = call double @sin(double %f) readonly
|
%g = call double @sin(double %f) readonly
|
||||||
@ -66,7 +50,6 @@ define double @nsz(double %e) nounwind {
|
|||||||
ret double %h
|
ret double %h
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME:
|
|
||||||
; The 1st negate is strict, so we can't kill that sub, but the 2nd disappears.
|
; The 1st negate is strict, so we can't kill that sub, but the 2nd disappears.
|
||||||
|
|
||||||
define double @semi_strict1(double %e) nounwind {
|
define double @semi_strict1(double %e) nounwind {
|
||||||
@ -76,8 +59,7 @@ define double @semi_strict1(double %e) nounwind {
|
|||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
||||||
; CHECK-NEXT: callq sin
|
; CHECK-NEXT: callq sin
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
; CHECK-NEXT: vxorpd {{.*}}(%rip), %xmm0, %xmm0
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: popq %rax
|
; CHECK-NEXT: popq %rax
|
||||||
; CHECK-NEXT: retq
|
; CHECK-NEXT: retq
|
||||||
;
|
;
|
||||||
@ -87,18 +69,15 @@ define double @semi_strict1(double %e) nounwind {
|
|||||||
ret double %h
|
ret double %h
|
||||||
}
|
}
|
||||||
|
|
||||||
; FIXME:
|
|
||||||
; The 2nd negate is strict, so we can't kill it. It becomes an add of zero instead.
|
; The 2nd negate is strict, so we can't kill it. It becomes an add of zero instead.
|
||||||
|
|
||||||
define double @semi_strict2(double %e) nounwind {
|
define double @semi_strict2(double %e) nounwind {
|
||||||
; CHECK-LABEL: semi_strict2:
|
; CHECK-LABEL: semi_strict2:
|
||||||
; CHECK: # BB#0:
|
; CHECK: # BB#0:
|
||||||
; CHECK-NEXT: pushq %rax
|
; CHECK-NEXT: pushq %rax
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
|
||||||
; CHECK-NEXT: callq sin
|
; CHECK-NEXT: callq sin
|
||||||
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
|
||||||
; CHECK-NEXT: vsubsd %xmm0, %xmm1, %xmm0
|
; CHECK-NEXT: vaddsd %xmm1, %xmm0, %xmm0
|
||||||
; CHECK-NEXT: popq %rax
|
; CHECK-NEXT: popq %rax
|
||||||
; CHECK-NEXT: retq
|
; CHECK-NEXT: retq
|
||||||
;
|
;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user