Skip to content

Commit 34fe032

Browse files
authored
[DAGCombiner] Use getShiftAmountConstant where possible. (#97683)
In #97645, I proposed removing the LegalTypes operand to TargetLowering::getShiftAmountTy. This means we don't need to use the DAGCombiner wrapper for getShiftAmountTy that manages this flag. Now we can use getShiftAmountConstant and let it call TargetLowering::getShiftAmountTy.
1 parent f4d058f commit 34fe032

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,14 +4395,13 @@ template <class MatchContextClass> SDValue DAGCombiner::visitMUL(SDNode *N) {
43954395
// fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
43964396
if (N1IsConst && !N1IsOpaqueConst && ConstValue1.isNegatedPowerOf2()) {
43974397
unsigned Log2Val = (-ConstValue1).logBase2();
4398-
EVT ShiftVT = getShiftAmountTy(N0.getValueType());
43994398

44004399
// FIXME: If the input is something that is easily negated (e.g. a
44014400
// single-use add), we should put the negate there.
44024401
return Matcher.getNode(
44034402
ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
44044403
Matcher.getNode(ISD::SHL, DL, VT, N0,
4405-
DAG.getConstant(Log2Val, DL, ShiftVT)));
4404+
DAG.getShiftAmountConstant(Log2Val, VT, DL)));
44064405
}
44074406

44084407
// Attempt to reuse an existing umul_lohi/smul_lohi node, but only if the
@@ -5101,9 +5100,9 @@ SDValue DAGCombiner::visitMULHS(SDNode *N) {
51015100

51025101
// fold (mulhs x, 1) -> (sra x, size(x)-1)
51035102
if (isOneConstant(N1))
5104-
return DAG.getNode(ISD::SRA, DL, VT, N0,
5105-
DAG.getConstant(N0.getScalarValueSizeInBits() - 1, DL,
5106-
getShiftAmountTy(VT)));
5103+
return DAG.getNode(
5104+
ISD::SRA, DL, VT, N0,
5105+
DAG.getShiftAmountConstant(N0.getScalarValueSizeInBits() - 1, VT, DL));
51075106

51085107
// fold (mulhs x, undef) -> 0
51095108
if (N0.isUndef() || N1.isUndef())
@@ -5121,8 +5120,7 @@ SDValue DAGCombiner::visitMULHS(SDNode *N) {
51215120
N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
51225121
N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
51235122
N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
5124-
DAG.getConstant(SimpleSize, DL,
5125-
getShiftAmountTy(N1.getValueType())));
5123+
DAG.getShiftAmountConstant(SimpleSize, NewVT, DL));
51265124
return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
51275125
}
51285126
}
@@ -5192,8 +5190,7 @@ SDValue DAGCombiner::visitMULHU(SDNode *N) {
51925190
N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
51935191
N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
51945192
N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
5195-
DAG.getConstant(SimpleSize, DL,
5196-
getShiftAmountTy(N1.getValueType())));
5193+
DAG.getShiftAmountConstant(SimpleSize, NewVT, DL));
51975194
return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
51985195
}
51995196
}
@@ -5404,8 +5401,7 @@ SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
54045401
Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
54055402
// Compute the high part as N1.
54065403
Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
5407-
DAG.getConstant(SimpleSize, DL,
5408-
getShiftAmountTy(Lo.getValueType())));
5404+
DAG.getShiftAmountConstant(SimpleSize, NewVT, DL));
54095405
Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
54105406
// Compute the low part as N0.
54115407
Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
@@ -5458,8 +5454,7 @@ SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
54585454
Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
54595455
// Compute the high part as N1.
54605456
Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
5461-
DAG.getConstant(SimpleSize, DL,
5462-
getShiftAmountTy(Lo.getValueType())));
5457+
DAG.getShiftAmountConstant(SimpleSize, NewVT, DL));
54635458
Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
54645459
// Compute the low part as N0.
54655460
Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
@@ -7484,8 +7479,7 @@ SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
74847479
if (OpSizeInBits > 16) {
74857480
SDLoc DL(N);
74867481
Res = DAG.getNode(ISD::SRL, DL, VT, Res,
7487-
DAG.getConstant(OpSizeInBits - 16, DL,
7488-
getShiftAmountTy(VT)));
7482+
DAG.getShiftAmountConstant(OpSizeInBits - 16, VT, DL));
74897483
}
74907484
return Res;
74917485
}
@@ -7603,7 +7597,7 @@ static bool isBSwapHWordPair(SDValue N, MutableArrayRef<SDNode *> Parts) {
76037597
// (rotr (bswap A), 16)
76047598
static SDValue matchBSwapHWordOrAndAnd(const TargetLowering &TLI,
76057599
SelectionDAG &DAG, SDNode *N, SDValue N0,
7606-
SDValue N1, EVT VT, EVT ShiftAmountTy) {
7600+
SDValue N1, EVT VT) {
76077601
assert(N->getOpcode() == ISD::OR && VT == MVT::i32 &&
76087602
"MatchBSwapHWordOrAndAnd: expecting i32");
76097603
if (!TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
@@ -7635,7 +7629,7 @@ static SDValue matchBSwapHWordOrAndAnd(const TargetLowering &TLI,
76357629

76367630
SDLoc DL(N);
76377631
SDValue BSwap = DAG.getNode(ISD::BSWAP, DL, VT, Shift0.getOperand(0));
7638-
SDValue ShAmt = DAG.getConstant(16, DL, ShiftAmountTy);
7632+
SDValue ShAmt = DAG.getShiftAmountConstant(16, VT, DL);
76397633
return DAG.getNode(ISD::ROTR, DL, VT, BSwap, ShAmt);
76407634
}
76417635

@@ -7655,13 +7649,11 @@ SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
76557649
if (!TLI.isOperationLegalOrCustom(ISD::BSWAP, VT))
76567650
return SDValue();
76577651

7658-
if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N0, N1, VT,
7659-
getShiftAmountTy(VT)))
7652+
if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N0, N1, VT))
76607653
return BSwap;
76617654

76627655
// Try again with commuted operands.
7663-
if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N1, N0, VT,
7664-
getShiftAmountTy(VT)))
7656+
if (SDValue BSwap = matchBSwapHWordOrAndAnd(TLI, DAG, N, N1, N0, VT))
76657657
return BSwap;
76667658

76677659

@@ -7698,7 +7690,7 @@ SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
76987690

76997691
// Result of the bswap should be rotated by 16. If it's not legal, then
77007692
// do (x << 16) | (x >> 16).
7701-
SDValue ShAmt = DAG.getConstant(16, DL, getShiftAmountTy(VT));
7693+
SDValue ShAmt = DAG.getShiftAmountConstant(16, VT, DL);
77027694
if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
77037695
return DAG.getNode(ISD::ROTL, DL, VT, BSwap, ShAmt);
77047696
if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
@@ -10430,8 +10422,7 @@ SDValue DAGCombiner::visitSRA(SDNode *N) {
1043010422
TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
1043110423
TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
1043210424
TLI.isTruncateFree(VT, TruncVT)) {
10433-
SDValue Amt = DAG.getConstant(ShiftAmt, DL,
10434-
getShiftAmountTy(N0.getOperand(0).getValueType()));
10425+
SDValue Amt = DAG.getShiftAmountConstant(ShiftAmt, VT, DL);
1043510426
SDValue Shift = DAG.getNode(ISD::SRL, DL, VT,
1043610427
N0.getOperand(0), Amt);
1043710428
SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, TruncVT,
@@ -10679,10 +10670,9 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1067910670
if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
1068010671
uint64_t ShiftAmt = N1C->getZExtValue();
1068110672
SDLoc DL0(N0);
10682-
SDValue SmallShift = DAG.getNode(ISD::SRL, DL0, SmallVT,
10683-
N0.getOperand(0),
10684-
DAG.getConstant(ShiftAmt, DL0,
10685-
getShiftAmountTy(SmallVT)));
10673+
SDValue SmallShift =
10674+
DAG.getNode(ISD::SRL, DL0, SmallVT, N0.getOperand(0),
10675+
DAG.getShiftAmountConstant(ShiftAmt, SmallVT, DL0));
1068610676
AddToWorklist(SmallShift.getNode());
1068710677
APInt Mask = APInt::getLowBitsSet(OpSizeInBits, OpSizeInBits - ShiftAmt);
1068810678
return DAG.getNode(ISD::AND, DL, VT,
@@ -10726,8 +10716,7 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1072610716
if (ShAmt) {
1072710717
SDLoc DL(N0);
1072810718
Op = DAG.getNode(ISD::SRL, DL, VT, Op,
10729-
DAG.getConstant(ShAmt, DL,
10730-
getShiftAmountTy(Op.getValueType())));
10719+
DAG.getShiftAmountConstant(ShAmt, VT, DL));
1073110720
AddToWorklist(Op.getNode());
1073210721
}
1073310722
return DAG.getNode(ISD::XOR, DL, VT, Op, DAG.getConstant(1, DL, VT));
@@ -11086,7 +11075,7 @@ SDValue DAGCombiner::visitBSWAP(SDNode *N) {
1108611075
SDValue Res = N0.getOperand(0);
1108711076
if (uint64_t NewShAmt = (ShAmt->getZExtValue() - (BW / 2)))
1108811077
Res = DAG.getNode(ISD::SHL, DL, VT, Res,
11089-
DAG.getConstant(NewShAmt, DL, getShiftAmountTy(VT)));
11078+
DAG.getShiftAmountConstant(NewShAmt, VT, DL));
1109011079
Res = DAG.getZExtOrTrunc(Res, DL, HalfVT);
1109111080
Res = DAG.getNode(ISD::BSWAP, DL, HalfVT, Res);
1109211081
return DAG.getZExtOrTrunc(Res, DL, VT);
@@ -12316,9 +12305,9 @@ SDValue DAGCombiner::visitVSELECT(SDNode *N) {
1231612305
if (TLI.isOperationLegalOrCustom(ISD::ABS, VT))
1231712306
return DAG.getNode(ISD::ABS, DL, VT, LHS);
1231812307

12319-
SDValue Shift = DAG.getNode(ISD::SRA, DL, VT, LHS,
12320-
DAG.getConstant(VT.getScalarSizeInBits() - 1,
12321-
DL, getShiftAmountTy(VT)));
12308+
SDValue Shift = DAG.getNode(
12309+
ISD::SRA, DL, VT, LHS,
12310+
DAG.getShiftAmountConstant(VT.getScalarSizeInBits() - 1, VT, DL));
1232212311
SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
1232312312
AddToWorklist(Shift.getNode());
1232412313
AddToWorklist(Add.getNode());
@@ -14625,18 +14614,15 @@ SDValue DAGCombiner::reduceLoadWidth(SDNode *N) {
1462514614
// Shift the result left, if we've swallowed a left shift.
1462614615
SDValue Result = Load;
1462714616
if (ShLeftAmt != 0) {
14628-
EVT ShImmTy = getShiftAmountTy(Result.getValueType());
14629-
if (!isUIntN(ShImmTy.getScalarSizeInBits(), ShLeftAmt))
14630-
ShImmTy = VT;
1463114617
// If the shift amount is as large as the result size (but, presumably,
1463214618
// no larger than the source) then the useful bits of the result are
1463314619
// zero; we can't simply return the shortened shift, because the result
1463414620
// of that operation is undefined.
1463514621
if (ShLeftAmt >= VT.getScalarSizeInBits())
1463614622
Result = DAG.getConstant(0, DL, VT);
1463714623
else
14638-
Result = DAG.getNode(ISD::SHL, DL, VT,
14639-
Result, DAG.getConstant(ShLeftAmt, DL, ShImmTy));
14624+
Result = DAG.getNode(ISD::SHL, DL, VT, Result,
14625+
DAG.getShiftAmountConstant(ShLeftAmt, VT, DL));
1464014626
}
1464114627

1464214628
if (ShiftedOffset != 0) {
@@ -16898,7 +16884,7 @@ SDValue DAGCombiner::combineFMulOrFDivWithIntPow2(SDNode *N) {
1689816884

1689916885
// Perform actual transform.
1690016886
SDValue MantissaShiftCnt =
16901-
DAG.getConstant(*Mantissa, DL, getShiftAmountTy(NewIntVT));
16887+
DAG.getShiftAmountConstant(*Mantissa, NewIntVT, DL);
1690216888
// TODO: Sometimes Log2 is of form `(X + C)`. `(X + C) << C1` should fold to
1690316889
// `(X << C1) + (C << C1)`, but that isn't always the case because of the
1690416890
// cast. We could implement that by handle here to handle the casts.
@@ -19811,9 +19797,9 @@ ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
1981119797
// shifted by ByteShift and truncated down to NumBytes.
1981219798
if (ByteShift) {
1981319799
SDLoc DL(IVal);
19814-
IVal = DAG.getNode(ISD::SRL, DL, IVal.getValueType(), IVal,
19815-
DAG.getConstant(ByteShift*8, DL,
19816-
DC->getShiftAmountTy(IVal.getValueType())));
19800+
IVal = DAG.getNode(
19801+
ISD::SRL, DL, IVal.getValueType(), IVal,
19802+
DAG.getShiftAmountConstant(ByteShift * 8, IVal.getValueType(), DL));
1981719803
}
1981819804

1981919805
// Figure out the offset for the store and the alignment of the access.
@@ -27422,12 +27408,11 @@ SDValue DAGCombiner::foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0,
2742227408

2742327409
// and (sra X, size(X)-1), A -> "and (srl X, C2), A" iff A is a single-bit
2742427410
// constant.
27425-
EVT ShiftAmtTy = getShiftAmountTy(N0.getValueType());
2742627411
auto *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
2742727412
if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue() - 1)) == 0)) {
2742827413
unsigned ShCt = XType.getSizeInBits() - N2C->getAPIntValue().logBase2() - 1;
2742927414
if (!TLI.shouldAvoidTransformToShift(XType, ShCt)) {
27430-
SDValue ShiftAmt = DAG.getConstant(ShCt, DL, ShiftAmtTy);
27415+
SDValue ShiftAmt = DAG.getShiftAmountConstant(ShCt, XType, DL);
2743127416
SDValue Shift = DAG.getNode(ISD::SRL, DL, XType, N0, ShiftAmt);
2743227417
AddToWorklist(Shift.getNode());
2743327418

@@ -27447,7 +27432,7 @@ SDValue DAGCombiner::foldSelectCCToShiftAnd(const SDLoc &DL, SDValue N0,
2744727432
if (TLI.shouldAvoidTransformToShift(XType, ShCt))
2744827433
return SDValue();
2744927434

27450-
SDValue ShiftAmt = DAG.getConstant(ShCt, DL, ShiftAmtTy);
27435+
SDValue ShiftAmt = DAG.getShiftAmountConstant(ShCt, XType, DL);
2745127436
SDValue Shift = DAG.getNode(ISD::SRA, DL, XType, N0, ShiftAmt);
2745227437
AddToWorklist(Shift.getNode());
2745327438

@@ -27661,16 +27646,13 @@ SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1,
2766127646
const APInt &AndMask = ConstAndRHS->getAPIntValue();
2766227647
if (TLI.shouldFoldSelectWithSingleBitTest(VT, AndMask)) {
2766327648
unsigned ShCt = AndMask.getBitWidth() - 1;
27664-
SDValue ShlAmt =
27665-
DAG.getConstant(AndMask.countl_zero(), SDLoc(AndLHS),
27666-
getShiftAmountTy(AndLHS.getValueType()));
27649+
SDValue ShlAmt = DAG.getShiftAmountConstant(AndMask.countl_zero(), VT,
27650+
SDLoc(AndLHS));
2766727651
SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
2766827652

2766927653
// Now arithmetic right shift it all the way over, so the result is
2767027654
// either all-ones, or zero.
27671-
SDValue ShrAmt =
27672-
DAG.getConstant(ShCt, SDLoc(Shl),
27673-
getShiftAmountTy(Shl.getValueType()));
27655+
SDValue ShrAmt = DAG.getShiftAmountConstant(ShCt, VT, SDLoc(Shl));
2767427656
SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
2767527657

2767627658
return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
@@ -27718,9 +27700,9 @@ SDValue DAGCombiner::SimplifySelectCC(const SDLoc &DL, SDValue N0, SDValue N1,
2771827700
return SDValue();
2771927701

2772027702
// shl setcc result by log2 n2c
27721-
return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
27722-
DAG.getConstant(ShCt, SDLoc(Temp),
27723-
getShiftAmountTy(Temp.getValueType())));
27703+
return DAG.getNode(
27704+
ISD::SHL, DL, N2.getValueType(), Temp,
27705+
DAG.getShiftAmountConstant(ShCt, N2.getValueType(), SDLoc(Temp)));
2772427706
}
2772527707

2772627708
// select_cc seteq X, 0, sizeof(X), ctlz(X) -> ctlz(X)

0 commit comments

Comments
 (0)