@@ -5513,8 +5513,9 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op,
55135513 APInt InVecDemandedElts = DemandedElts;
55145514 InVecDemandedElts.clearBit(IndexC->getZExtValue());
55155515 if (!!InVecDemandedElts &&
5516- !isGuaranteedNotToBeUndefOrPoison(InVec, InVecDemandedElts,
5517- PoisonOnly, Depth + 1))
5516+ !isGuaranteedNotToBeUndefOrPoison(
5517+ peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5518+ InVecDemandedElts, PoisonOnly, Depth + 1))
55185519 return false;
55195520 return true;
55205521 }
@@ -8215,23 +8216,42 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
82158216 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
82168217 // for scalable vectors where we will generate appropriate code to
82178218 // deal with out-of-bounds cases correctly.
8218- if (N3C && N1.getValueType() .isFixedLengthVector() &&
8219- N3C->getZExtValue() >= N1.getValueType() .getVectorNumElements())
8219+ if (N3C && VT .isFixedLengthVector() &&
8220+ N3C->getZExtValue() >= VT .getVectorNumElements())
82208221 return getUNDEF(VT);
82218222
82228223 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
82238224 if (N3.isUndef())
82248225 return getUNDEF(VT);
82258226
8226- // If the inserted element is an UNDEF , just use the input vector.
8227- if (N2.isUndef() )
8227+ // If inserting poison , just use the input vector.
8228+ if (N2.getOpcode() == ISD::POISON )
82288229 return N1;
82298230
8231+ // Inserting undef into undef/poison is still undef.
8232+ if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8233+ return getUNDEF(VT);
8234+
8235+ // If the inserted element is an UNDEF, just use the input vector.
8236+ // But not if skipping the insert could make the result more poisonous.
8237+ if (N2.isUndef()) {
8238+ if (N3C && VT.isFixedLengthVector()) {
8239+ APInt EltMask =
8240+ APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8241+ if (isGuaranteedNotToBePoison(N1, EltMask))
8242+ return N1;
8243+ } else if (isGuaranteedNotToBePoison(N1))
8244+ return N1;
8245+ }
82308246 break;
82318247 }
82328248 case ISD::INSERT_SUBVECTOR: {
8233- // Inserting undef into undef is still undef.
8234- if (N1.isUndef() && N2.isUndef())
8249+ // If inserting poison, just use the input vector,
8250+ if (N2.getOpcode() == ISD::POISON)
8251+ return N1;
8252+
8253+ // Inserting undef into undef/poison is still undef.
8254+ if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
82358255 return getUNDEF(VT);
82368256
82378257 EVT N2VT = N2.getValueType();
@@ -8260,11 +8280,37 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
82608280 if (VT == N2VT)
82618281 return N2;
82628282
8263- // If this is an insert of an extracted vector into an undef vector, we
8264- // can just use the input to the extract.
8283+ // If this is an insert of an extracted vector into an undef/poison vector,
8284+ // we can just use the input to the extract. But not if skipping the
8285+ // extract+insert could make the result more poisonous.
82658286 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8266- N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
8267- return N2.getOperand(0);
8287+ N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8288+ if (N1.getOpcode() == ISD::POISON)
8289+ return N2.getOperand(0);
8290+ if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8291+ unsigned LoBit = N3->getAsZExtVal();
8292+ unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8293+ APInt EltMask =
8294+ APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8295+ if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8296+ return N2.getOperand(0);
8297+ } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8298+ return N2.getOperand(0);
8299+ }
8300+
8301+ // If the inserted subvector is UNDEF, just use the input vector.
8302+ // But not if skipping the insert could make the result more poisonous.
8303+ if (N2.isUndef()) {
8304+ if (VT.isFixedLengthVector()) {
8305+ unsigned LoBit = N3->getAsZExtVal();
8306+ unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8307+ APInt EltMask =
8308+ APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8309+ if (isGuaranteedNotToBePoison(N1, EltMask))
8310+ return N1;
8311+ } else if (isGuaranteedNotToBePoison(N1))
8312+ return N1;
8313+ }
82688314 break;
82698315 }
82708316 case ISD::BITCAST:
@@ -12729,6 +12775,23 @@ SDValue llvm::peekThroughExtractSubvectors(SDValue V) {
1272912775 return V;
1273012776}
1273112777
12778+ SDValue llvm::peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts) {
12779+ while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12780+ SDValue InVec = V.getOperand(0);
12781+ SDValue EltNo = V.getOperand(2);
12782+ EVT VT = InVec.getValueType();
12783+ auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12784+ if (IndexC && VT.isFixedLengthVector() &&
12785+ IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12786+ !DemandedElts[IndexC->getZExtValue()]) {
12787+ V = InVec;
12788+ continue;
12789+ }
12790+ break;
12791+ }
12792+ return V;
12793+ }
12794+
1273212795SDValue llvm::peekThroughTruncates(SDValue V) {
1273312796 while (V.getOpcode() == ISD::TRUNCATE)
1273412797 V = V.getOperand(0);
0 commit comments