Skip to content

[SelectionDAG][NFC] Refactor duplicate code into SDNode::bitcastToAPInt() #127503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/CodeGen/SelectionDAGNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ END_TWO_BYTE_PACK()
/// Helper method returns the APInt value of a ConstantSDNode.
inline const APInt &getAsAPIntVal() const;

inline std::optional<APInt> bitcastToAPInt() const;

const SDValue &getOperand(unsigned Num) const {
assert(Num < NumOperands && "Invalid child # of SDNode!");
return OperandList[Num];
Expand Down Expand Up @@ -1785,6 +1787,14 @@ class ConstantFPSDNode : public SDNode {
}
};

std::optional<APInt> SDNode::bitcastToAPInt() const {
if (auto *CN = dyn_cast<ConstantSDNode>(this))
return CN->getAPIntValue();
if (auto *CFPN = dyn_cast<ConstantFPSDNode>(this))
return CFPN->getValueAPF().bitcastToAPInt();
return std::nullopt;
}

/// Returns true if \p V is a constant integer zero.
bool isNullConstant(SDValue V);

Expand Down
17 changes: 7 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27419,23 +27419,20 @@ SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
continue;
}

APInt Bits;
if (auto *Cst = dyn_cast<ConstantSDNode>(Elt))
Bits = Cst->getAPIntValue();
else if (auto *CstFP = dyn_cast<ConstantFPSDNode>(Elt))
Bits = CstFP->getValueAPF().bitcastToAPInt();
else
std::optional<APInt> Bits = Elt->bitcastToAPInt();
if (!Bits)
return SDValue();

// Extract the sub element from the constant bit mask.
if (DAG.getDataLayout().isBigEndian())
Bits = Bits.extractBits(NumSubBits, (Split - SubIdx - 1) * NumSubBits);
*Bits =
Bits->extractBits(NumSubBits, (Split - SubIdx - 1) * NumSubBits);
else
Bits = Bits.extractBits(NumSubBits, SubIdx * NumSubBits);
*Bits = Bits->extractBits(NumSubBits, SubIdx * NumSubBits);

if (Bits.isAllOnes())
if (Bits->isAllOnes())
Indices.push_back(i);
else if (Bits == 0)
else if (*Bits == 0)
Indices.push_back(i + NumSubElts);
else
return SDValue();
Expand Down
38 changes: 12 additions & 26 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,10 @@ bool ConstantFPSDNode::isValueValidForType(EVT VT,

bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
if (N->getOpcode() == ISD::SPLAT_VECTOR) {
unsigned EltSize =
N->getValueType(0).getVectorElementType().getSizeInBits();
if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
SplatVal = Op0->getAPIntValue().trunc(EltSize);
return true;
}
if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
unsigned EltSize =
N->getValueType(0).getVectorElementType().getSizeInBits();
SplatVal = OptAPInt->trunc(EltSize);
return true;
}
}
Expand Down Expand Up @@ -215,12 +211,9 @@ bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
// we care if the resultant vector is all ones, not whether the individual
// constants are.
SDValue NotZero = N->getOperand(i);
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
if (CN->getAPIntValue().countr_one() < EltSize)
return false;
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
if (auto OptAPInt = NotZero->bitcastToAPInt()) {
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (OptAPInt->countr_one() < EltSize)
return false;
} else
return false;
Expand Down Expand Up @@ -259,12 +252,9 @@ bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
// We only want to check enough bits to cover the vector elements, because
// we care if the resultant vector is all zeros, not whether the individual
// constants are.
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
if (CN->getAPIntValue().countr_zero() < EltSize)
return false;
} else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
if (auto OptAPInt = Op->bitcastToAPInt()) {
unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
if (OptAPInt->countr_zero() < EltSize)
return false;
} else
return false;
Expand Down Expand Up @@ -3434,13 +3424,9 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,

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

if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
if (auto OptAPInt = Op->bitcastToAPInt()) {
// We know all of the bits for a constant!
return KnownBits::makeConstant(C->getAPIntValue());
}
if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
// We know all of the bits for a constant fp!
return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
return KnownBits::makeConstant(*std::move(OptAPInt));
}

if (Depth >= MaxRecursionDepth)
Expand Down