[llvm] [SelectionDAG][NFC] Refactor duplicate code into SDNode::bitcastToAPInt() (PR #127503)
Piotr Fusik via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 17 06:59:56 PST 2025
https://github.com/pfusik created https://github.com/llvm/llvm-project/pull/127503
None
>From 0e45daae938a2e20832cb8169473da53f629d7ff Mon Sep 17 00:00:00 2001
From: Piotr Fusik <p.fusik at samsung.com>
Date: Mon, 17 Feb 2025 15:48:12 +0100
Subject: [PATCH] [SelectionDAG][NFC] Refactor duplicate code into
SDNode::bitcastToAPInt()
---
llvm/include/llvm/CodeGen/SelectionDAGNodes.h | 10 +++++
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 6 +--
.../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 38 ++++++-------------
3 files changed, 24 insertions(+), 30 deletions(-)
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)
More information about the llvm-commits
mailing list