Fast Math Flag mapping into SDNode

Summary: Adding support for Fast flags in the SDNode to leverage fast math sub flag usage.

Reviewers: spatel, arsenm, jbhateja, hfinkel, escha, qcolombet, echristo, wristow, javed.absar

Reviewed By: spatel

Subscribers: llvm-commits, rampitec, nhaehnle, tstellar, FarhanaAleen, nemanjai, javed.absar, jbhateja, hfinkel, wdng

Differential Revision: https://reviews.llvm.org/D45710

llvm-svn: 331547
This commit is contained in:
Michael Berg 2018-05-04 18:48:20 +00:00
parent 0e51a125ea
commit 7acc81b744
8 changed files with 53 additions and 40 deletions

View File

@ -359,21 +359,22 @@ private:
bool NoUnsignedWrap : 1; bool NoUnsignedWrap : 1;
bool NoSignedWrap : 1; bool NoSignedWrap : 1;
bool Exact : 1; bool Exact : 1;
bool UnsafeAlgebra : 1;
bool NoNaNs : 1; bool NoNaNs : 1;
bool NoInfs : 1; bool NoInfs : 1;
bool NoSignedZeros : 1; bool NoSignedZeros : 1;
bool AllowReciprocal : 1; bool AllowReciprocal : 1;
bool VectorReduction : 1; bool VectorReduction : 1;
bool AllowContract : 1; bool AllowContract : 1;
bool ApproximateFuncs : 1;
bool AllowReassociation : 1;
public: public:
/// Default constructor turns off all optimization flags. /// Default constructor turns off all optimization flags.
SDNodeFlags() SDNodeFlags()
: AnyDefined(false), NoUnsignedWrap(false), NoSignedWrap(false), : AnyDefined(false), NoUnsignedWrap(false), NoSignedWrap(false),
Exact(false), UnsafeAlgebra(false), NoNaNs(false), NoInfs(false), Exact(false), NoNaNs(false), NoInfs(false),
NoSignedZeros(false), AllowReciprocal(false), VectorReduction(false), NoSignedZeros(false), AllowReciprocal(false), VectorReduction(false),
AllowContract(false) {} AllowContract(false), ApproximateFuncs(false), AllowReassociation(false) {}
/// Sets the state of the flags to the defined state. /// Sets the state of the flags to the defined state.
void setDefined() { AnyDefined = true; } void setDefined() { AnyDefined = true; }
@ -393,10 +394,6 @@ public:
setDefined(); setDefined();
Exact = b; Exact = b;
} }
void setUnsafeAlgebra(bool b) {
setDefined();
UnsafeAlgebra = b;
}
void setNoNaNs(bool b) { void setNoNaNs(bool b) {
setDefined(); setDefined();
NoNaNs = b; NoNaNs = b;
@ -421,18 +418,32 @@ public:
setDefined(); setDefined();
AllowContract = b; AllowContract = b;
} }
void setApproximateFuncs(bool b) {
setDefined();
ApproximateFuncs = b;
}
void setAllowReassociation(bool b) {
setDefined();
AllowReassociation = b;
}
// These are accessors for each flag. // These are accessors for each flag.
bool hasNoUnsignedWrap() const { return NoUnsignedWrap; } bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
bool hasNoSignedWrap() const { return NoSignedWrap; } bool hasNoSignedWrap() const { return NoSignedWrap; }
bool hasExact() const { return Exact; } bool hasExact() const { return Exact; }
bool hasUnsafeAlgebra() const { return UnsafeAlgebra; }
bool hasNoNaNs() const { return NoNaNs; } bool hasNoNaNs() const { return NoNaNs; }
bool hasNoInfs() const { return NoInfs; } bool hasNoInfs() const { return NoInfs; }
bool hasNoSignedZeros() const { return NoSignedZeros; } bool hasNoSignedZeros() const { return NoSignedZeros; }
bool hasAllowReciprocal() const { return AllowReciprocal; } bool hasAllowReciprocal() const { return AllowReciprocal; }
bool hasVectorReduction() const { return VectorReduction; } bool hasVectorReduction() const { return VectorReduction; }
bool hasAllowContract() const { return AllowContract; } bool hasAllowContract() const { return AllowContract; }
bool hasApproximateFuncs() const { return ApproximateFuncs; }
bool hasAllowReassociation() const { return AllowReassociation; }
bool isFast() const {
return NoSignedZeros && AllowReciprocal && NoNaNs && NoInfs &&
AllowContract && ApproximateFuncs && AllowReassociation;
}
/// Clear any flags in this flag set that aren't also set in Flags. /// Clear any flags in this flag set that aren't also set in Flags.
/// If the given Flags are undefined then don't do anything. /// If the given Flags are undefined then don't do anything.
@ -442,13 +453,14 @@ public:
NoUnsignedWrap &= Flags.NoUnsignedWrap; NoUnsignedWrap &= Flags.NoUnsignedWrap;
NoSignedWrap &= Flags.NoSignedWrap; NoSignedWrap &= Flags.NoSignedWrap;
Exact &= Flags.Exact; Exact &= Flags.Exact;
UnsafeAlgebra &= Flags.UnsafeAlgebra;
NoNaNs &= Flags.NoNaNs; NoNaNs &= Flags.NoNaNs;
NoInfs &= Flags.NoInfs; NoInfs &= Flags.NoInfs;
NoSignedZeros &= Flags.NoSignedZeros; NoSignedZeros &= Flags.NoSignedZeros;
AllowReciprocal &= Flags.AllowReciprocal; AllowReciprocal &= Flags.AllowReciprocal;
VectorReduction &= Flags.VectorReduction; VectorReduction &= Flags.VectorReduction;
AllowContract &= Flags.AllowContract; AllowContract &= Flags.AllowContract;
ApproximateFuncs &= Flags.ApproximateFuncs;
AllowReassociation &= Flags.AllowReassociation;
} }
}; };
@ -923,6 +935,7 @@ public:
const SDNodeFlags getFlags() const { return Flags; } const SDNodeFlags getFlags() const { return Flags; }
void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; } void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
bool isFast() { return Flags.isFast(); }
/// Clear any flags in this node that aren't also set in Flags. /// Clear any flags in this node that aren't also set in Flags.
/// If Flags is not in a defined state then this has no effect. /// If Flags is not in a defined state then this has no effect.

View File

@ -9542,7 +9542,7 @@ ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
static bool isContractable(SDNode *N) { static bool isContractable(SDNode *N) {
SDNodeFlags F = N->getFlags(); SDNodeFlags F = N->getFlags();
return F.hasAllowContract() || F.hasUnsafeAlgebra(); return F.hasAllowContract() || F.hasAllowReassociation();
} }
/// Try to perform FMA combining on a given FADD node. /// Try to perform FMA combining on a given FADD node.
@ -10567,9 +10567,9 @@ SDValue DAGCombiner::visitFMA(SDNode *N) {
return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2); return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
// TODO: FMA nodes should have flags that propagate to the created nodes. // TODO: FMA nodes should have flags that propagate to the created nodes.
// For now, create a Flags object for use with all unsafe math transforms. // For now, create a Flags object for use with reassociation math transforms.
SDNodeFlags Flags; SDNodeFlags Flags;
Flags.setUnsafeAlgebra(true); Flags.setAllowReassociation(true);
if (Options.UnsafeFPMath) { if (Options.UnsafeFPMath) {
// (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
@ -10841,9 +10841,9 @@ SDValue DAGCombiner::visitFSQRT(SDNode *N) {
return SDValue(); return SDValue();
// TODO: FSQRT nodes should have flags that propagate to the created nodes. // TODO: FSQRT nodes should have flags that propagate to the created nodes.
// For now, create a Flags object for use with all unsafe math transforms. // For now, create a Flags object for use with reassociation math transforms.
SDNodeFlags Flags; SDNodeFlags Flags;
Flags.setUnsafeAlgebra(true); Flags.setAllowReassociation(true);
return buildSqrtEstimate(N0, Flags); return buildSqrtEstimate(N0, Flags);
} }

View File

@ -2776,7 +2776,8 @@ void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Flags.setNoInfs(FMF.noInfs()); Flags.setNoInfs(FMF.noInfs());
Flags.setNoNaNs(FMF.noNaNs()); Flags.setNoNaNs(FMF.noNaNs());
Flags.setNoSignedZeros(FMF.noSignedZeros()); Flags.setNoSignedZeros(FMF.noSignedZeros());
Flags.setUnsafeAlgebra(FMF.isFast()); Flags.setApproximateFuncs(FMF.approxFunc());
Flags.setAllowReassociation(FMF.allowReassoc());
SDValue BinNodeValue = DAG.getNode(OpCode, getCurSDLoc(), Op1.getValueType(), SDValue BinNodeValue = DAG.getNode(OpCode, getCurSDLoc(), Op1.getValueType(),
Op1, Op2, Flags); Op1, Op2, Flags);

View File

@ -479,9 +479,6 @@ void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
if (getFlags().hasExact()) if (getFlags().hasExact())
OS << " exact"; OS << " exact";
if (getFlags().hasUnsafeAlgebra())
OS << " unsafe";
if (getFlags().hasNoNaNs()) if (getFlags().hasNoNaNs())
OS << " nnan"; OS << " nnan";
@ -497,6 +494,12 @@ void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
if (getFlags().hasAllowContract()) if (getFlags().hasAllowContract())
OS << " contract"; OS << " contract";
if (getFlags().hasApproximateFuncs())
OS << " afn";
if (getFlags().hasAllowReassociation())
OS << " reassoc";
if (getFlags().hasVectorReduction()) if (getFlags().hasVectorReduction())
OS << " vector-reduction"; OS << " vector-reduction";

View File

@ -5145,7 +5145,7 @@ SDValue AArch64TargetLowering::getSqrtEstimate(SDValue Operand,
EVT VT = Operand.getValueType(); EVT VT = Operand.getValueType();
SDNodeFlags Flags; SDNodeFlags Flags;
Flags.setUnsafeAlgebra(true); Flags.setAllowReassociation(true);
// Newton reciprocal square root iteration: E * 0.5 * (3 - X * E^2) // Newton reciprocal square root iteration: E * 0.5 * (3 - X * E^2)
// AArch64 reciprocal square root iteration instruction: 0.5 * (3 - M * N) // AArch64 reciprocal square root iteration instruction: 0.5 * (3 - M * N)
@ -5184,7 +5184,7 @@ SDValue AArch64TargetLowering::getRecipEstimate(SDValue Operand,
EVT VT = Operand.getValueType(); EVT VT = Operand.getValueType();
SDNodeFlags Flags; SDNodeFlags Flags;
Flags.setUnsafeAlgebra(true); Flags.setAllowReassociation(true);
// Newton reciprocal iteration: E * (2 - X * E) // Newton reciprocal iteration: E * (2 - X * E)
// AArch64 reciprocal iteration instruction: (2 - M * N) // AArch64 reciprocal iteration instruction: (2 - M * N)

View File

@ -5347,8 +5347,7 @@ SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
SDValue RHS = Op.getOperand(1); SDValue RHS = Op.getOperand(1);
EVT VT = Op.getValueType(); EVT VT = Op.getValueType();
const SDNodeFlags Flags = Op->getFlags(); const SDNodeFlags Flags = Op->getFlags();
bool Unsafe = DAG.getTarget().Options.UnsafeFPMath || bool Unsafe = DAG.getTarget().Options.UnsafeFPMath || Flags.hasAllowReciprocal();
Flags.hasUnsafeAlgebra() || Flags.hasAllowReciprocal();
if (!Unsafe && VT == MVT::f32 && Subtarget->hasFP32Denormals()) if (!Unsafe && VT == MVT::f32 && Subtarget->hasFP32Denormals())
return SDValue(); return SDValue();
@ -6698,8 +6697,8 @@ unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
const TargetOptions &Options = DAG.getTarget().Options; const TargetOptions &Options = DAG.getTarget().Options;
if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath ||
(N0->getFlags().hasUnsafeAlgebra() && (N0->getFlags().hasAllowContract() &&
N1->getFlags().hasUnsafeAlgebra())) && N1->getFlags().hasAllowContract())) &&
isFMAFasterThanFMulAndFAdd(VT)) { isFMAFasterThanFMulAndFAdd(VT)) {
return ISD::FMA; return ISD::FMA;
} }

View File

@ -63,7 +63,7 @@ define float @fmul_fadd_contract2(float %x, float %y, float %z) {
; FMFDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fadd_reassoc1:' ; FMFDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fadd_reassoc1:'
; FMFDEBUG: fmul {{t[0-9]+}}, {{t[0-9]+}} ; FMFDEBUG: fmul {{t[0-9]+}}, {{t[0-9]+}}
; FMFDEBUG: fadd {{t[0-9]+}}, {{t[0-9]+}} ; FMFDEBUG: fadd reassoc {{t[0-9]+}}, {{t[0-9]+}}
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fadd_reassoc1:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fadd_reassoc1:'
define float @fmul_fadd_reassoc1(float %x, float %y, float %z) { define float @fmul_fadd_reassoc1(float %x, float %y, float %z) {
@ -86,15 +86,14 @@ define float @fmul_fadd_reassoc1(float %x, float %y, float %z) {
; This shouldn't change anything - the intermediate fmul result is now also flagged. ; This shouldn't change anything - the intermediate fmul result is now also flagged.
; FMFDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fadd_reassoc2:' ; FMFDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fadd_reassoc2:'
; FMFDEBUG: fmul {{t[0-9]+}}, {{t[0-9]+}} ; FMFDEBUG: fma {{t[0-9]+}}, {{t[0-9]+}}
; FMFDEBUG: fadd {{t[0-9]+}}, {{t[0-9]+}}
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fadd_reassoc2:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fadd_reassoc2:'
define float @fmul_fadd_reassoc2(float %x, float %y, float %z) { define float @fmul_fadd_reassoc2(float %x, float %y, float %z) {
; FMF-LABEL: fmul_fadd_reassoc2: ; FMF-LABEL: fmul_fadd_reassoc2:
; FMF: # %bb.0: ; FMF: # %bb.0:
; FMF-NEXT: xsmulsp 0, 1, 2 ; FMF-NEXT: xsmaddasp 3, 1, 2
; FMF-NEXT: xsaddsp 1, 0, 3 ; FMF-NEXT: fmr 1, 3
; FMF-NEXT: blr ; FMF-NEXT: blr
; ;
; GLOBAL-LABEL: fmul_fadd_reassoc2: ; GLOBAL-LABEL: fmul_fadd_reassoc2:
@ -161,7 +160,7 @@ define float @fmul_fadd_fast2(float %x, float %y, float %z) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc1:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc1:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_reassoc1:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_reassoc1:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc1:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc1:'
define float @fmul_fma_reassoc1(float %x) { define float @fmul_fma_reassoc1(float %x) {
@ -197,7 +196,7 @@ define float @fmul_fma_reassoc1(float %x) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc2:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc2:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_reassoc2:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_reassoc2:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc2:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_reassoc2:'
define float @fmul_fma_reassoc2(float %x) { define float @fmul_fma_reassoc2(float %x) {
@ -233,7 +232,7 @@ define float @fmul_fma_reassoc2(float %x) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast1:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast1:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_fast1:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_fast1:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast1:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast1:'
define float @fmul_fma_fast1(float %x) { define float @fmul_fma_fast1(float %x) {
@ -269,7 +268,7 @@ define float @fmul_fma_fast1(float %x) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast2:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast2:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_fast2:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'fmul_fma_fast2:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast2:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'fmul_fma_fast2:'
define float @fmul_fma_fast2(float %x) { define float @fmul_fma_fast2(float %x) {
@ -305,7 +304,7 @@ define float @fmul_fma_fast2(float %x) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_afn:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_afn:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'sqrt_afn:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'sqrt_afn:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_afn:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_afn:'
define float @sqrt_afn(float %x) { define float @sqrt_afn(float %x) {
@ -345,7 +344,7 @@ define float @sqrt_afn(float %x) {
; FMFDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_fast:' ; FMFDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_fast:'
; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'sqrt_fast:' ; GLOBALDEBUG-LABEL: Optimized lowered selection DAG: %bb.0 'sqrt_fast:'
; GLOBALDEBUG: fmul unsafe {{t[0-9]+}} ; GLOBALDEBUG: fmul reassoc {{t[0-9]+}}
; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_fast:' ; GLOBALDEBUG: Type-legalized selection DAG: %bb.0 'sqrt_fast:'
define float @sqrt_fast(float %x) { define float @sqrt_fast(float %x) {

View File

@ -3,8 +3,6 @@
; This tests the propagation of fast-math-flags from IR instructions to SDNodeFlags. ; This tests the propagation of fast-math-flags from IR instructions to SDNodeFlags.
; FIXME: 'afn' and 'reassoc' were dropped. With 'fast', 'reassoc' got renamed to 'unsafe'.
; CHECK-LABEL: Initial selection DAG: %bb.0 'fmf_transfer:' ; CHECK-LABEL: Initial selection DAG: %bb.0 'fmf_transfer:'
; CHECK: t5: f32 = fadd nsz t2, t4 ; CHECK: t5: f32 = fadd nsz t2, t4
@ -12,9 +10,9 @@
; CHECK-NEXT: t7: f32 = fadd nnan t6, t4 ; CHECK-NEXT: t7: f32 = fadd nnan t6, t4
; CHECK-NEXT: t8: f32 = fadd ninf t7, t4 ; CHECK-NEXT: t8: f32 = fadd ninf t7, t4
; CHECK-NEXT: t9: f32 = fadd contract t8, t4 ; CHECK-NEXT: t9: f32 = fadd contract t8, t4
; CHECK-NEXT: t10: f32 = fadd t9, t4 ; CHECK-NEXT: t10: f32 = fadd afn t9, t4
; CHECK-NEXT: t11: f32 = fadd t10, t4 ; CHECK-NEXT: t11: f32 = fadd reassoc t10, t4
; CHECK-NEXT: t12: f32 = fadd unsafe nnan ninf nsz arcp contract t11, t4 ; CHECK-NEXT: t12: f32 = fadd nnan ninf nsz arcp contract afn reassoc t11, t4
; CHECK: Optimized lowered selection DAG: %bb.0 'fmf_transfer:' ; CHECK: Optimized lowered selection DAG: %bb.0 'fmf_transfer:'