Skip to content

Commit 04bb32e

Browse files
committed
[DAG] Extract helper for (neg x) [nfc]
This is a frequently reoccurring pattern, let's factor it out. Differential Revision: https://reviews.llvm.org/D135301
1 parent 9dd0476 commit 04bb32e

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,9 @@ class SelectionDAG {
926926
/// BooleanContent for type OpVT or truncating it.
927927
SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT);
928928

929+
/// Create negative operation as (SUB 0, Val).
930+
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT);
931+
929932
/// Create a bitwise NOT operation as (XOR Val, -1).
930933
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT);
931934

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,8 +3981,7 @@ SDValue DAGCombiner::visitMUL(SDNode *N) {
39813981

39823982
// fold (mul x, -1) -> 0-x
39833983
if (N1IsConst && ConstValue1.isAllOnes())
3984-
return DAG.getNode(ISD::SUB, DL, VT,
3985-
DAG.getConstant(0, DL, VT), N0);
3984+
return DAG.getNegative(N0, DL, VT);
39863985

39873986
// fold (mul x, (1 << c)) -> x << c
39883987
if (isConstantOrConstantVector(N1, /*NoOpaques*/ true) &&
@@ -4049,7 +4048,7 @@ SDValue DAGCombiner::visitMUL(SDNode *N) {
40494048
DAG.getConstant(TZeros, DL, VT)))
40504049
: DAG.getNode(MathOp, DL, VT, Shl, N0);
40514050
if (ConstValue1.isNegative())
4052-
R = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), R);
4051+
R = DAG.getNegative(R, DL, VT);
40534052
return R;
40544053
}
40554054
}
@@ -4303,7 +4302,7 @@ SDValue DAGCombiner::visitSDIV(SDNode *N) {
43034302
// fold (sdiv X, -1) -> 0-X
43044303
ConstantSDNode *N1C = isConstOrConstSplat(N1);
43054304
if (N1C && N1C->isAllOnes())
4306-
return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), N0);
4305+
return DAG.getNegative(N0, DL, VT);
43074306

43084307
// fold (sdiv X, MIN_SIGNED) -> select(X == MIN_SIGNED, 1, 0)
43094308
if (N1C && N1C->getAPIntValue().isMinSignedValue())
@@ -8682,8 +8681,7 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
86828681
// fold (not (add X, -1)) -> (neg X)
86838682
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::ADD &&
86848683
isAllOnesOrAllOnesSplat(N0.getOperand(1))) {
8685-
return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
8686-
N0.getOperand(0));
8684+
return DAG.getNegative(N0.getOperand(0), DL, VT);
86878685
}
86888686

86898687
// fold (xor (and x, y), y) -> (and (not x), y)
@@ -11305,8 +11303,7 @@ SDValue DAGCombiner::visitVSELECT(SDNode *N) {
1130511303
if (SatCC == ISD::SETUGT && Other.getOpcode() == ISD::ADD &&
1130611304
ISD::matchBinaryPredicate(OpRHS, CondRHS, MatchUSUBSAT,
1130711305
/*AllowUndefs*/ true)) {
11308-
OpRHS = DAG.getNode(ISD::SUB, DL, VT,
11309-
DAG.getConstant(0, DL, VT), OpRHS);
11306+
OpRHS = DAG.getNegative(OpRHS, DL, VT);
1131011307
return DAG.getNode(ISD::USUBSAT, DL, VT, OpLHS, OpRHS);
1131111308
}
1131211309

@@ -12394,7 +12391,7 @@ SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
1239412391
N0.getOperand(1).getOpcode() == ISD::ZERO_EXTEND &&
1239512392
TLI.isOperationLegalOrCustom(ISD::SUB, VT)) {
1239612393
SDValue Zext = DAG.getZExtOrTrunc(N0.getOperand(1).getOperand(0), DL, VT);
12397-
return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Zext);
12394+
return DAG.getNegative(Zext, DL, VT);
1239812395
}
1239912396
// Eliminate this sign extend by doing a decrement in the destination type:
1240012397
// sext i32 ((zext i8 X to i32) + (-1)) to i64 --> (zext i8 X to i64) + (-1)

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,10 @@ SDValue SelectionDAG::getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
14611461
return getZeroExtendInReg(Op, DL, VT);
14621462
}
14631463

1464+
SDValue SelectionDAG::getNegative(SDValue Val, const SDLoc &DL, EVT VT) {
1465+
return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1466+
}
1467+
14641468
/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
14651469
SDValue SelectionDAG::getNOT(const SDLoc &DL, SDValue Val, EVT VT) {
14661470
return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,8 +3421,7 @@ void SelectionDAGBuilder::visitSelect(const User &I) {
34213421
Values[i] =
34223422
DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
34233423
if (Negate)
3424-
Values[i] = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, dl, VT),
3425-
Values[i]);
3424+
Values[i] = DAG.getNegative(Values[i], dl, VT);
34263425
}
34273426
} else {
34283427
for (unsigned i = 0; i != NumValues; ++i) {

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,8 +3254,7 @@ static SDValue lowerCTLZ_CTTZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
32543254
// For CTTZ_ZERO_UNDEF, we need to extract the lowest set bit using X & -X.
32553255
// The trailing zero count is equal to log2 of this single bit value.
32563256
if (Op.getOpcode() == ISD::CTTZ_ZERO_UNDEF) {
3257-
SDValue Neg =
3258-
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Src);
3257+
SDValue Neg = DAG.getNegative(Src, DL, VT);
32593258
Src = DAG.getNode(ISD::AND, DL, VT, Src, Neg);
32603259
}
32613260

@@ -8207,8 +8206,7 @@ performSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
82078206
DAG.ComputeNumSignBits(Src.getOperand(0)) > 32) {
82088207
SDLoc DL(N);
82098208
SDValue Freeze = DAG.getFreeze(Src.getOperand(0));
8210-
SDValue Neg =
8211-
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, MVT::i64), Freeze);
8209+
SDValue Neg = DAG.getNegative(Freeze, DL, VT);
82128210
Neg = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, Neg,
82138211
DAG.getValueType(MVT::i32));
82148212
return DAG.getNode(ISD::SMAX, DL, MVT::i64, Freeze, Neg);
@@ -9458,8 +9456,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
94589456
DAG.getConstant(1, DL, VT));
94599457
else
94609458
Neg = LHS;
9461-
SDValue Mask = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
9462-
Neg); // -(and (x, 0x1))
9459+
SDValue Mask = DAG.getNegative(Neg, DL, VT); // -x
94639460
SDValue And = DAG.getNode(ISD::AND, DL, VT, Mask, Src1); // Mask & z
94649461
return DAG.getNode(Opcode, DL, VT, And, Src2); // And Op y
94659462
}

0 commit comments

Comments
 (0)