Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27420,10 +27420,8 @@ SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
}

APInt Bits;
if (auto *Cst = dyn_cast<ConstantSDNode>(Elt))
Bits = Cst->getAPIntValue();
else if (auto *CstFP = dyn_cast<ConstantFPSDNode>(Elt))
Bits = CstFP->getValueAPF().bitcastToAPInt();
if (auto OptBits = Elt->bitcastToAPInt())
Bits = *std::move(OptBits);
else
return SDValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the move and should invert this to early return on failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean:

auto OptBits = Elt->bitcastToAPInt();
if (!OptBits)
  return SDValue();
APInt Bits = *OptBits;

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'd do

  std::optional<APInt> Bits = Elt->bitcastToAPInt();
  if (!Bits)
    return SDValue();
  // Use *Bits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


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
Loading