@@ -23018,18 +23018,33 @@ SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
2301823018 return NewShuffle;
2301923019 }
2302023020
23021- // If all insertions are zero value, try to convert to AND mask.
23022- // TODO: Do this for -1 with OR mask?
23023- if (!LegalOperations && llvm::isNullConstant(InVal) &&
23024- all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
23025- count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
23026- SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
23027- SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
23028- SmallVector<SDValue, 8> Mask(NumElts);
23029- for (unsigned I = 0; I != NumElts; ++I)
23030- Mask[I] = Ops[I] ? Zero : AllOnes;
23031- return DAG.getNode(ISD::AND, DL, VT, CurVec,
23032- DAG.getBuildVector(VT, DL, Mask));
23021+ if (!LegalOperations) {
23022+ bool IsNull = llvm::isNullConstant(InVal);
23023+ // We can convert to AND/OR mask if all insertions are zero or -1
23024+ // respectively.
23025+ if ((IsNull || llvm::isAllOnesConstant(InVal)) &&
23026+ all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
23027+ count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
23028+ SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
23029+ SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
23030+ SmallVector<SDValue, 8> Mask(NumElts);
23031+
23032+ // Build the mask and return the corresponding DAG node.
23033+ auto BuildMaskAndNode = [&](SDValue TrueVal, SDValue FalseVal,
23034+ unsigned MaskOpcode) {
23035+ for (unsigned I = 0; I != NumElts; ++I)
23036+ Mask[I] = Ops[I] ? TrueVal : FalseVal;
23037+ return DAG.getNode(MaskOpcode, DL, VT, CurVec,
23038+ DAG.getBuildVector(VT, DL, Mask));
23039+ };
23040+
23041+ // If all elements are zero, we can use AND with all ones.
23042+ if (IsNull)
23043+ return BuildMaskAndNode(Zero, AllOnes, ISD::AND);
23044+
23045+ // If all elements are -1, we can use OR with zero.
23046+ return BuildMaskAndNode(AllOnes, Zero, ISD::OR);
23047+ }
2303323048 }
2303423049
2303523050 // Failed to find a match in the chain - bail.
0 commit comments