-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-llvm-selectiondag Author: Piotr Fusik (pfusik) ChangesFull diff: https://github.com/llvm/llvm-project/pull/127503.diff 3 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
index 6eff6bfe8d5b1..75c4fabe03dd4 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
@@ -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];
@@ -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);
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index c6fd72b6b76f4..59f5d15cfed30 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -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();
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 9d2f87497d6fa..531314c5bfd07 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -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;
}
}
@@ -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;
@@ -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;
@@ -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)
|
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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;
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/24/builds/5499 Here is the relevant piece of the build log for the reference
|
No description provided.