Skip to content

Commit d254014

Browse files
committed
[DAG] Add willNotOverflowAdd/willNotOverflowSub helper functions.
Matches similar instructions on InstCombine
1 parent 2db0acb commit d254014

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,11 @@ class SelectionDAG {
20412041
: computeOverflowForUnsignedAdd(N0, N1);
20422042
}
20432043

2044+
/// Determine if the result of the addition of 2 nodes can never overflow.
2045+
bool willNotOverflowAdd(bool IsSigned, SDValue N0, SDValue N1) const {
2046+
return computeOverflowForAdd(IsSigned, N0, N1) == OFK_Never;
2047+
}
2048+
20442049
/// Determine if the result of the signed sub of 2 nodes can overflow.
20452050
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const;
20462051

@@ -2054,6 +2059,11 @@ class SelectionDAG {
20542059
: computeOverflowForUnsignedSub(N0, N1);
20552060
}
20562061

2062+
/// Determine if the result of the sub of 2 nodes can never overflow.
2063+
bool willNotOverflowSub(bool IsSigned, SDValue N0, SDValue N1) const {
2064+
return computeOverflowForSub(IsSigned, N0, N1) == OFK_Never;
2065+
}
2066+
20572067
/// Test if the given value is known to have exactly one bit set. This differs
20582068
/// from computeKnownBits in that it doesn't necessarily determine which bit
20592069
/// is set.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,7 +3040,7 @@ SDValue DAGCombiner::visitADDSAT(SDNode *N) {
30403040
return N0;
30413041

30423042
// If it cannot overflow, transform into an add.
3043-
if (DAG.computeOverflowForAdd(IsSigned, N0, N1) == SelectionDAG::OFK_Never)
3043+
if (DAG.willNotOverflowAdd(IsSigned, N0, N1))
30443044
return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
30453045

30463046
return SDValue();
@@ -3310,7 +3310,7 @@ SDValue DAGCombiner::visitADDO(SDNode *N) {
33103310
return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT));
33113311

33123312
// If it cannot overflow, transform into an add.
3313-
if (DAG.computeOverflowForAdd(IsSigned, N0, N1) == SelectionDAG::OFK_Never)
3313+
if (DAG.willNotOverflowAdd(IsSigned, N0, N1))
33143314
return CombineTo(N, DAG.getNode(ISD::ADD, DL, VT, N0, N1),
33153315
DAG.getConstant(0, DL, CarryVT));
33163316

@@ -4170,7 +4170,7 @@ SDValue DAGCombiner::visitSUBSAT(SDNode *N) {
41704170
return N0;
41714171

41724172
// If it cannot overflow, transform into an sub.
4173-
if (DAG.computeOverflowForSub(IsSigned, N0, N1) == SelectionDAG::OFK_Never)
4173+
if (DAG.willNotOverflowSub(IsSigned, N0, N1))
41744174
return DAG.getNode(ISD::SUB, DL, VT, N0, N1);
41754175

41764176
return SDValue();
@@ -4236,7 +4236,7 @@ SDValue DAGCombiner::visitSUBO(SDNode *N) {
42364236
return CombineTo(N, N0, DAG.getConstant(0, DL, CarryVT));
42374237

42384238
// If it cannot overflow, transform into an sub.
4239-
if (DAG.computeOverflowForSub(IsSigned, N0, N1) == SelectionDAG::OFK_Never)
4239+
if (DAG.willNotOverflowSub(IsSigned, N0, N1))
42404240
return CombineTo(N, DAG.getNode(ISD::SUB, DL, VT, N0, N1),
42414241
DAG.getConstant(0, DL, CarryVT));
42424242

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,13 +1040,10 @@ static SDValue combineShiftToAVG(SDValue Op, SelectionDAG &DAG,
10401040
// larger type size to do the transform.
10411041
if (!TLI.isOperationLegalOrCustom(AVGOpc, VT))
10421042
return SDValue();
1043-
1044-
if (DAG.computeOverflowForAdd(IsSigned, Add.getOperand(0),
1045-
Add.getOperand(1)) ==
1046-
SelectionDAG::OFK_Never &&
1047-
(!Add2 || DAG.computeOverflowForAdd(IsSigned, Add2.getOperand(0),
1048-
Add2.getOperand(1)) ==
1049-
SelectionDAG::OFK_Never))
1043+
if (DAG.willNotOverflowAdd(IsSigned, Add.getOperand(0),
1044+
Add.getOperand(1)) &&
1045+
(!Add2 || DAG.willNotOverflowAdd(IsSigned, Add2.getOperand(0),
1046+
Add2.getOperand(1))))
10501047
NVT = VT;
10511048
else
10521049
return SDValue();

0 commit comments

Comments
 (0)