@@ -22746,68 +22746,49 @@ SDValue DAGCombiner::scalarizeExtractedVectorLoad(SDNode *EVE, EVT InVecVT,
2274622746
2274722747/// Transform a vector binary operation into a scalar binary operation by moving
2274822748/// the math/logic after an extract element of a vector.
22749- static bool scalarizeExtractedBinOpCommon(SDNode *ExtElt, SelectionDAG &DAG,
22750- const SDLoc &DL, bool IsSetCC,
22751- SDValue &ScalarOp1,
22752- SDValue &ScalarOp2) {
22749+ static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
22750+ const SDLoc &DL) {
22751+ const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2275322752 SDValue Vec = ExtElt->getOperand(0);
2275422753 SDValue Index = ExtElt->getOperand(1);
2275522754 auto *IndexC = dyn_cast<ConstantSDNode>(Index);
22756- if (!IndexC || !Vec.hasOneUse() || Vec->getNumValues() != 1)
22757- return false;
22755+ if (!IndexC ||
22756+ (!TLI.isBinOp(Vec.getOpcode()) && Vec.getOpcode() != ISD::SETCC) ||
22757+ !Vec.hasOneUse() || Vec->getNumValues() != 1)
22758+ return SDValue();
22759+
22760+ EVT ResVT = ExtElt->getValueType(0);
22761+ if (Vec.getOpcode() == ISD::SETCC &&
22762+ ResVT != Vec.getValueType().getVectorElementType())
22763+ return SDValue();
22764+
22765+ // Targets may want to avoid this to prevent an expensive register transfer.
22766+ if (!TLI.shouldScalarizeBinop(Vec))
22767+ return SDValue();
2275822768
2275922769 // Extracting an element of a vector constant is constant-folded, so this
2276022770 // transform is just replacing a vector op with a scalar op while moving the
2276122771 // extract.
2276222772 SDValue Op0 = Vec.getOperand(0);
2276322773 SDValue Op1 = Vec.getOperand(1);
2276422774 APInt SplatVal;
22765- if (isAnyConstantBuildVector(Op0, true) ||
22766- ISD::isConstantSplatVector(Op0.getNode(), SplatVal) ||
22767- isAnyConstantBuildVector(Op1, true) ||
22768- ISD::isConstantSplatVector(Op1.getNode(), SplatVal)) {
22769- // extractelt (binop X, C), IndexC --> binop (extractelt X, IndexC), C'
22770- // extractelt (binop C, X), IndexC --> binop C', (extractelt X, IndexC)
22771- // extractelt (setcc X, C, op), IndexC -> setcc (extractelt X, IndexC)), C
22772- // extractelt (setcc C, X, op), IndexC -> setcc (extractelt IndexC, X)), C
22773- EVT VT = Op0->getValueType(0).getVectorElementType();
22774- ScalarOp1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Op0, Index);
22775- ScalarOp2 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Op1, Index);
22776- return true;
22777- }
22778-
22779- return false;
22780- }
22781-
22782- static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
22783- const SDLoc &DL) {
22784- SDValue Op1, Op2;
22785- SDValue Vec = ExtElt->getOperand(0);
22786- const TargetLowering &TLI = DAG.getTargetLoweringInfo();
22787- if (!TLI.isBinOp(Vec.getOpcode()) || !TLI.shouldScalarizeBinop(Vec))
22788- return SDValue();
22789-
22790- if (!scalarizeExtractedBinOpCommon(ExtElt, DAG, DL, false, Op1, Op2))
22775+ if (!isAnyConstantBuildVector(Op0, true) &&
22776+ !ISD::isConstantSplatVector(Op0.getNode(), SplatVal) &&
22777+ !isAnyConstantBuildVector(Op1, true) &&
22778+ !ISD::isConstantSplatVector(Op1.getNode(), SplatVal))
2279122779 return SDValue();
2279222780
22793- EVT VT = ExtElt->getValueType(0);
22794- return DAG.getNode(Vec.getOpcode(), DL, VT, Op1, Op2);
22795- }
22781+ // extractelt (op X, C), IndexC --> op (extractelt X, IndexC), C'
22782+ // extractelt (op C, X), IndexC --> op C', (extractelt X, IndexC)
22783+ EVT OpVT = Op0->getValueType(0).getVectorElementType();
22784+ Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, Op0, Index);
22785+ Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, Op1, Index);
2279622786
22797- static SDValue scalarizeExtractedSetCC(SDNode *ExtElt, SelectionDAG &DAG,
22798- const SDLoc &DL) {
22799- SDValue Op1, Op2;
22800- SDValue Vec = ExtElt->getOperand(0);
22801- const TargetLowering &TLI = DAG.getTargetLoweringInfo();
22802- if (Vec.getOpcode() != ISD::SETCC || !TLI.shouldScalarizeSetCC(Vec))
22803- return SDValue();
22804-
22805- if (!scalarizeExtractedBinOpCommon(ExtElt, DAG, DL, true, Op1, Op2))
22806- return SDValue();
22807-
22808- EVT VT = ExtElt->getValueType(0);
22809- return DAG.getSetCC(DL, VT, Op1, Op2,
22810- cast<CondCodeSDNode>(Vec->getOperand(2))->get());
22787+ if (Vec.getOpcode() == ISD::SETCC)
22788+ return DAG.getSetCC(DL, ResVT, Op0, Op1,
22789+ cast<CondCodeSDNode>(Vec->getOperand(2))->get());
22790+ else
22791+ return DAG.getNode(Vec.getOpcode(), DL, ResVT, Op0, Op1);
2281122792}
2281222793
2281322794// Given a ISD::EXTRACT_VECTOR_ELT, which is a glorified bit sequence extract,
@@ -23043,11 +23024,6 @@ SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
2304323024 if (SDValue BO = scalarizeExtractedBinOp(N, DAG, DL))
2304423025 return BO;
2304523026
23046- // extract (setcc x, splat(y)), i -> setcc (extract x, i)), y
23047- if (ScalarVT == VecVT.getVectorElementType())
23048- if (SDValue SetCC = scalarizeExtractedSetCC(N, DAG, DL))
23049- return SetCC;
23050-
2305123027 if (VecVT.isScalableVector())
2305223028 return SDValue();
2305323029
0 commit comments