[llvm] add narrowExtractedVectorUnaryOp to simplify cast nodes (PR #87977)
Simon Pilgrim via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 8 03:05:41 PDT 2024
================
@@ -24083,6 +24083,55 @@ static SDValue narrowInsertExtractVectorBinOp(SDNode *Extract,
BinOp->getFlags());
}
+/// If we are extracting a subvector produced by a wide unary operator try
+/// to use a narrow unary operator and/or avoid extraction.
+static SDValue narrowExtractedVectorUnaryOp(SDNode *Extract, SelectionDAG &DAG,
+ bool LegalOperations) {
+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
+ SDValue UnaryOp = Extract->getOperand(0);
+ unsigned UnaryOpcode = UnaryOp.getOpcode();
+
+ if (UnaryOpcode != ISD::FP_TO_SINT || UnaryOp->getNumValues() != 1)
+ return SDValue();
+
+ // The extract index must be a constant, so we can map it to a concat operand.
+ auto *ExtractIndexC = dyn_cast<ConstantSDNode>(Extract->getOperand(1));
+ if (!ExtractIndexC)
+ return SDValue();
+
+ EVT WideUVT = UnaryOp.getValueType();
+ if (!WideUVT.isFixedLengthVector())
+ return SDValue();
+
+ EVT VT = Extract->getValueType(0);
+ unsigned ExtractIndex = ExtractIndexC->getZExtValue();
+ assert(ExtractIndex % VT.getVectorNumElements() == 0 &&
+ "Extract index is not a multiple of the vector length.");
+
+ // Bail out if this is not a proper multiple width extraction.
+ unsigned WideWidth = WideUVT.getSizeInBits();
+ unsigned NarrowWidth = VT.getSizeInBits();
+ if (WideWidth % NarrowWidth != 0)
+ return SDValue();
+
+ unsigned NarrowingRatio = WideWidth / NarrowWidth;
+ unsigned WideNumElts = WideUVT.getVectorNumElements();
+
+ // Bail out if the target does not support a narrower version of the unaryop.
+ EVT NarrowUVT = EVT::getVectorVT(*DAG.getContext(), WideUVT.getScalarType(),
+ WideNumElts / NarrowingRatio);
+ if (!TLI.isOperationLegalOrCustomOrPromote(UnaryOpcode, NarrowUVT,
+ LegalOperations))
+ return SDValue();
+
----------------
RKSimon wrote:
You need to add similar code to extract_subvector stage here: https://github.com/llvm/llvm-project/blob/170c525d79a4ab3659041b0655ac9697768fc915/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L24157-L24169
https://github.com/llvm/llvm-project/pull/87977
More information about the llvm-commits
mailing list