@@ -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+ bool isAllOnes = llvm::isAllOnesConstant(InVal);
22972+ // We can convert to AND/OR mask if all insertions are zero or -1
22973+ // respectively.
22974+ if ((isNull || isAllOnes) &&
22975+ all_of(Ops, [InVal](SDValue Op) { return !Op || Op == InVal; }) &&
22976+ count_if(Ops, [InVal](SDValue Op) { return Op == InVal; }) >= 2) {
22977+ SDValue Zero = DAG.getConstant(0, DL, MaxEltVT);
22978+ SDValue AllOnes = DAG.getAllOnesConstant(DL, MaxEltVT);
22979+ SmallVector<SDValue, 8> Mask(NumElts);
22980+
22981+ // Build the mask and return the corresponding DAG node.
22982+ auto buildMaskAndNode = [&](SDValue TrueVal, SDValue FalseVal,
22983+ unsigned MaskOpcode) {
22984+ for (unsigned I = 0; I < NumElts; ++I)
22985+ Mask[I] = Ops[I] ? TrueVal : FalseVal;
22986+ return DAG.getNode(MaskOpcode, DL, VT, CurVec,
22987+ DAG.getBuildVector(VT, DL, Mask));
22988+ };
22989+
22990+ // Use the pre-evaluated boolean value to decide the operation.
22991+ if (isAllOnes)
22992+ return buildMaskAndNode(AllOnes, Zero, ISD::OR);
22993+
22994+ return buildMaskAndNode(Zero, AllOnes, ISD::AND);
22995+ }
2298122996 }
2298222997
2298322998 // Failed to find a match in the chain - bail.
0 commit comments