Skip to content

Commit 1127662

Browse files
committed
[SelectionDAG] Make sure FMF are propagated when getSetcc canonicalizes FP constants to RHS.
getNode handling for ISD:SETCC calls FoldSETCC which can canonicalize FP constants to the RHS. When this happens we should create the node with the FMF that was requested. By using FlagInserter when can ensure any calls to getNode/getSetcc during canonicalization will also get the flags. Differential Revision: https://reviews.llvm.org/D88063
1 parent 27e1cc6 commit 1127662

File tree

9 files changed

+30
-19
lines changed

9 files changed

+30
-19
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,13 @@ class SelectionDAG {
339339
FlagInserter *LastInserter;
340340

341341
public:
342-
FlagInserter(SelectionDAG &SDAG, SDNode *N)
343-
: DAG(SDAG), Flags(N->getFlags()),
342+
FlagInserter(SelectionDAG &SDAG, SDNodeFlags Flags)
343+
: DAG(SDAG), Flags(Flags),
344344
LastInserter(SDAG.getFlagInserter()) {
345345
SDAG.setFlagInserter(this);
346346
}
347+
FlagInserter(SelectionDAG &SDAG, SDNode *N)
348+
: FlagInserter(SDAG, N->getFlags()) {}
347349

348350
FlagInserter(const FlagInserter &) = delete;
349351
FlagInserter &operator=(const FlagInserter &) = delete;
@@ -1083,8 +1085,8 @@ class SelectionDAG {
10831085
/// Helper function to make it easier to build SetCC's if you just have an
10841086
/// ISD::CondCode instead of an SDValue.
10851087
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS,
1086-
ISD::CondCode Cond, SDNodeFlags Flags = SDNodeFlags(),
1087-
SDValue Chain = SDValue(), bool IsSignaling = false) {
1088+
ISD::CondCode Cond, SDValue Chain = SDValue(),
1089+
bool IsSignaling = false) {
10881090
assert(LHS.getValueType().isVector() == RHS.getValueType().isVector() &&
10891091
"Cannot compare scalars to vectors");
10901092
assert(LHS.getValueType().isVector() == VT.isVector() &&
@@ -1094,7 +1096,7 @@ class SelectionDAG {
10941096
if (Chain)
10951097
return getNode(IsSignaling ? ISD::STRICT_FSETCCS : ISD::STRICT_FSETCC, DL,
10961098
{VT, MVT::Other}, {Chain, LHS, RHS, getCondCode(Cond)});
1097-
return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond), Flags);
1099+
return getNode(ISD::SETCC, DL, VT, LHS, RHS, getCondCode(Cond));
10981100
}
10991101

11001102
/// Helper function to make it easier to build Select's if you just have

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7437,7 +7437,7 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
74377437
// FIXME Can we handle multiple uses? Could we token factor the chain
74387438
// results from the new/old setcc?
74397439
SDValue SetCC =
7440-
DAG.getSetCC(SDLoc(N0), VT, LHS, RHS, NotCC, SDNodeFlags(),
7440+
DAG.getSetCC(SDLoc(N0), VT, LHS, RHS, NotCC,
74417441
N0.getOperand(0), N0Opcode == ISD::STRICT_FSETCCS);
74427442
CombineTo(N, SetCC);
74437443
DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), SetCC.getValue(1));

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,15 +1754,15 @@ bool SelectionDAGLegalize::LegalizeSetCCCondCode(
17541754
if (CCCode != ISD::SETO && CCCode != ISD::SETUO) {
17551755
// If we aren't the ordered or unorder operation,
17561756
// then the pattern is (LHS CC1 RHS) Opc (LHS CC2 RHS).
1757-
SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1, SDNodeFlags(), Chain,
1757+
SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1, Chain,
17581758
IsSignaling);
1759-
SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2, SDNodeFlags(), Chain,
1759+
SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2, Chain,
17601760
IsSignaling);
17611761
} else {
17621762
// Otherwise, the pattern is (LHS CC1 LHS) Opc (RHS CC2 RHS)
1763-
SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1, SDNodeFlags(), Chain,
1763+
SetCC1 = DAG.getSetCC(dl, VT, LHS, LHS, CC1, Chain,
17641764
IsSignaling);
1765-
SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2, SDNodeFlags(), Chain,
1765+
SetCC2 = DAG.getSetCC(dl, VT, RHS, RHS, CC2, Chain,
17661766
IsSignaling);
17671767
}
17681768
if (Chain)

llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,18 +1793,18 @@ void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
17931793
// The following can be improved, but not that much.
17941794
SDValue Tmp1, Tmp2, Tmp3, OutputChain;
17951795
Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()), LHSHi,
1796-
RHSHi, ISD::SETOEQ, SDNodeFlags(), Chain, IsSignaling);
1796+
RHSHi, ISD::SETOEQ, Chain, IsSignaling);
17971797
OutputChain = Tmp1->getNumValues() > 1 ? Tmp1.getValue(1) : SDValue();
17981798
Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
1799-
RHSLo, CCCode, SDNodeFlags(), OutputChain, IsSignaling);
1799+
RHSLo, CCCode, OutputChain, IsSignaling);
18001800
OutputChain = Tmp2->getNumValues() > 1 ? Tmp2.getValue(1) : SDValue();
18011801
Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
18021802
Tmp1 =
18031803
DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()), LHSHi, RHSHi,
1804-
ISD::SETUNE, SDNodeFlags(), OutputChain, IsSignaling);
1804+
ISD::SETUNE, OutputChain, IsSignaling);
18051805
OutputChain = Tmp1->getNumValues() > 1 ? Tmp1.getValue(1) : SDValue();
18061806
Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()), LHSHi,
1807-
RHSHi, CCCode, SDNodeFlags(), OutputChain, IsSignaling);
1807+
RHSHi, CCCode, OutputChain, IsSignaling);
18081808
OutputChain = Tmp2->getNumValues() > 1 ? Tmp2.getValue(1) : SDValue();
18091809
Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
18101810
NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3097,10 +3097,11 @@ void SelectionDAGBuilder::visitFCmp(const User &I) {
30973097

30983098
SDNodeFlags Flags;
30993099
Flags.copyFMF(*FPMO);
3100+
SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
31003101

31013102
EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
31023103
I.getType());
3103-
setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition, Flags));
3104+
setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
31043105
}
31053106

31063107
// Check if the condition of the select has one use or two users that are both

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6426,7 +6426,7 @@ bool TargetLowering::expandFP_TO_UINT(SDNode *Node, SDValue &Result,
64266426
SDValue Sel;
64276427

64286428
if (Node->isStrictFPOpcode()) {
6429-
Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT, SDNodeFlags(),
6429+
Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT,
64306430
Node->getOperand(0), /*IsSignaling*/ true);
64316431
Chain = Sel.getValue(1);
64326432
} else {

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8287,7 +8287,7 @@ SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
82878287
EVT DstSetCCVT =
82888288
getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), DstVT);
82898289
SDValue Sel = DAG.getSetCC(dl, SetCCVT, Src, Cst, ISD::SETLT,
8290-
SDNodeFlags(), Chain, true);
8290+
Chain, true);
82918291
Chain = Sel.getValue(1);
82928292

82938293
SDValue FltOfs = DAG.getSelect(

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20457,7 +20457,7 @@ X86TargetLowering::FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
2045720457
*DAG.getContext(), TheVT);
2045820458
SDValue Cmp;
2045920459
if (IsStrict) {
20460-
Cmp = DAG.getSetCC(DL, ResVT, Value, ThreshVal, ISD::SETLT, SDNodeFlags(),
20460+
Cmp = DAG.getSetCC(DL, ResVT, Value, ThreshVal, ISD::SETLT,
2046120461
Chain, /*IsSignaling*/ true);
2046220462
Chain = Cmp.getValue(1);
2046320463
} else {

llvm/test/CodeGen/X86/fmf-propagation.ll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ define float @fmf_transfer(float %x, float %y) {
2828
ret float %f8
2929
}
3030

31-
; CHECK: Optimized type-legalized selection DAG: %bb.0 'fmf_setcc:'
31+
; CHECK-LABEL: Optimized type-legalized selection DAG: %bb.0 'fmf_setcc:'
3232
; CHECK: t13: i8 = setcc nnan ninf nsz arcp contract afn reassoc t2, ConstantFP:f32<0.000000e+00>, setlt:ch
3333

3434
define float @fmf_setcc(float %x, float %y) {
3535
%cmp = fcmp fast ult float %x, 0.0
3636
%ret = select i1 %cmp, float %x, float %y
3737
ret float %ret
3838
}
39+
40+
; CHECK-LABEL: Initial selection DAG: %bb.0 'fmf_setcc_canon:'
41+
; CHECK: t14: i8 = setcc nnan ninf nsz arcp contract afn reassoc t2, ConstantFP:f32<0.000000e+00>, setgt:ch
42+
define float @fmf_setcc_canon(float %x, float %y) {
43+
%cmp = fcmp fast ult float 0.0, %x
44+
%ret = select i1 %cmp, float %x, float %y
45+
ret float %ret
46+
}

0 commit comments

Comments
 (0)