@@ -22966,18 +22966,33 @@ SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2296622966 return NewShuffle;
2296722967 }
2296822968
22969- // If all insertions are zero value, try to convert to AND mask.
22970- // TODO: Do this for -1 with OR mask?
22971- if (!LegalOperations && llvm::isNullConstant(InVal) &&
22972- all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
22973- count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
22974- SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
22975- SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
22976- SmallVector<SDValue, 8> Mask(NumElts);
22977- for (unsigned I = 0; I != NumElts; ++I)
22978- Mask[I] = Ops[I] ? Zero : AllOnes;
22979- return DAG.getNode(ISD::AND, DL, VT, CurVec,
22980- DAG.getBuildVector(VT, DL, Mask));
22969+ if (!LegalOperations) {
22970+ bool IsNull = llvm::isNullConstant(InVal);
22971+ // We can convert to AND/OR mask if all insertions are zero or -1
22972+ // respectively.
22973+ if ((IsNull || llvm::isAllOnesConstant(InVal)) &&
22974+ all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
22975+ count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
22976+ SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
22977+ SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
22978+ SmallVector<SDValue, 8> Mask(NumElts);
22979+
22980+ // Build the mask and return the corresponding DAG node.
22981+ auto buildMaskAndNode = [&](SDValue TrueVal, SDValue FalseVal,
22982+ unsigned MaskOpcode) {
22983+ for (unsigned I = 0; I < NumElts; ++I)
22984+ Mask[I] = Ops[I] ? TrueVal : FalseVal;
22985+ return DAG.getNode(MaskOpcode, DL, VT, CurVec,
22986+ DAG.getBuildVector(VT, DL, Mask));
22987+ };
22988+
22989+ // If all elements are zero, we can use AND with all ones.
22990+ if (IsNull)
22991+ return buildMaskAndNode(Zero, AllOnes, ISD::AND);
22992+
22993+ // If all elements are -1, we can use OR with zero.
22994+ return buildMaskAndNode(AllOnes, Zero, ISD::OR);
22995+ }
2298122996 }
2298222997
2298322998 // Failed to find a match in the chain - bail.
0 commit comments