Skip to content

Commit 8b58cb8

Browse files
authored
[SelectionDAG][NFC] Refactor duplicate code into SDNode::bitcastToAPInt() (#127503)
1 parent 77a8338 commit 8b58cb8

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,8 @@ END_TWO_BYTE_PACK()
989989
/// Helper method returns the APInt value of a ConstantSDNode.
990990
inline const APInt &getAsAPIntVal() const;
991991

992+
inline std::optional<APInt> bitcastToAPInt() const;
993+
992994
const SDValue &getOperand(unsigned Num) const {
993995
assert(Num < NumOperands && "Invalid child # of SDNode!");
994996
return OperandList[Num];
@@ -1785,6 +1787,14 @@ class ConstantFPSDNode : public SDNode {
17851787
}
17861788
};
17871789

1790+
std::optional<APInt> SDNode::bitcastToAPInt() const {
1791+
if (auto *CN = dyn_cast<ConstantSDNode>(this))
1792+
return CN->getAPIntValue();
1793+
if (auto *CFPN = dyn_cast<ConstantFPSDNode>(this))
1794+
return CFPN->getValueAPF().bitcastToAPInt();
1795+
return std::nullopt;
1796+
}
1797+
17881798
/// Returns true if \p V is a constant integer zero.
17891799
bool isNullConstant(SDValue V);
17901800

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27419,23 +27419,20 @@ SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
2741927419
continue;
2742027420
}
2742127421

27422-
APInt Bits;
27423-
if (auto *Cst = dyn_cast<ConstantSDNode>(Elt))
27424-
Bits = Cst->getAPIntValue();
27425-
else if (auto *CstFP = dyn_cast<ConstantFPSDNode>(Elt))
27426-
Bits = CstFP->getValueAPF().bitcastToAPInt();
27427-
else
27422+
std::optional<APInt> Bits = Elt->bitcastToAPInt();
27423+
if (!Bits)
2742827424
return SDValue();
2742927425

2743027426
// Extract the sub element from the constant bit mask.
2743127427
if (DAG.getDataLayout().isBigEndian())
27432-
Bits = Bits.extractBits(NumSubBits, (Split - SubIdx - 1) * NumSubBits);
27428+
*Bits =
27429+
Bits->extractBits(NumSubBits, (Split - SubIdx - 1) * NumSubBits);
2743327430
else
27434-
Bits = Bits.extractBits(NumSubBits, SubIdx * NumSubBits);
27431+
*Bits = Bits->extractBits(NumSubBits, SubIdx * NumSubBits);
2743527432

27436-
if (Bits.isAllOnes())
27433+
if (Bits->isAllOnes())
2743727434
Indices.push_back(i);
27438-
else if (Bits == 0)
27435+
else if (*Bits == 0)
2743927436
Indices.push_back(i + NumSubElts);
2744027437
else
2744127438
return SDValue();

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,10 @@ bool ConstantFPSDNode::isValueValidForType(EVT VT,
152152

153153
bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
154154
if (N->getOpcode() == ISD::SPLAT_VECTOR) {
155-
unsigned EltSize =
156-
N->getValueType(0).getVectorElementType().getSizeInBits();
157-
if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
158-
SplatVal = Op0->getAPIntValue().trunc(EltSize);
159-
return true;
160-
}
161-
if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
162-
SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155+
if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
156+
unsigned EltSize =
157+
N->getValueType(0).getVectorElementType().getSizeInBits();
158+
SplatVal = OptAPInt->trunc(EltSize);
163159
return true;
164160
}
165161
}
@@ -215,12 +211,9 @@ bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
215211
// we care if the resultant vector is all ones, not whether the individual
216212
// constants are.
217213
SDValue NotZero = N->getOperand(i);
218-
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
219-
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
220-
if (CN->getAPIntValue().countr_one() < EltSize)
221-
return false;
222-
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
223-
if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
214+
if (auto OptAPInt = NotZero->bitcastToAPInt()) {
215+
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
216+
if (OptAPInt->countr_one() < EltSize)
224217
return false;
225218
} else
226219
return false;
@@ -259,12 +252,9 @@ bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
259252
// We only want to check enough bits to cover the vector elements, because
260253
// we care if the resultant vector is all zeros, not whether the individual
261254
// constants are.
262-
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
263-
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
264-
if (CN->getAPIntValue().countr_zero() < EltSize)
265-
return false;
266-
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
267-
if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
255+
if (auto OptAPInt = Op->bitcastToAPInt()) {
256+
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
257+
if (OptAPInt->countr_zero() < EltSize)
268258
return false;
269259
} else
270260
return false;
@@ -3405,13 +3395,9 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34053395

34063396
KnownBits Known(BitWidth); // Don't know anything.
34073397

3408-
if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3398+
if (auto OptAPInt = Op->bitcastToAPInt()) {
34093399
// We know all of the bits for a constant!
3410-
return KnownBits::makeConstant(C->getAPIntValue());
3411-
}
3412-
if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3413-
// We know all of the bits for a constant fp!
3414-
return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3400+
return KnownBits::makeConstant(*std::move(OptAPInt));
34153401
}
34163402

34173403
if (Depth >= MaxRecursionDepth)

0 commit comments

Comments
 (0)