@@ -5456,6 +5456,85 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op,
54565456 }
54575457 return true;
54585458
5459+ case ISD::EXTRACT_SUBVECTOR: {
5460+ SDValue Src = Op.getOperand(0);
5461+ if (Src.getValueType().isScalableVector())
5462+ break;
5463+ uint64_t Idx = Op.getConstantOperandVal(1);
5464+ unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5465+ APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5466+ return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5467+ Depth + 1);
5468+ }
5469+
5470+ case ISD::INSERT_SUBVECTOR: {
5471+ if (Op.getValueType().isScalableVector())
5472+ break;
5473+ SDValue Src = Op.getOperand(0);
5474+ SDValue Sub = Op.getOperand(1);
5475+ uint64_t Idx = Op.getConstantOperandVal(2);
5476+ unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5477+ APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5478+ APInt DemandedSrcElts = DemandedElts;
5479+ DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5480+
5481+ if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5482+ Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5483+ return false;
5484+ if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5485+ Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5486+ return false;
5487+ return true;
5488+ }
5489+
5490+ case ISD::EXTRACT_VECTOR_ELT: {
5491+ SDValue Src = Op.getOperand(0);
5492+ auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5493+ EVT SrcVT = Src.getValueType();
5494+ if (SrcVT.isFixedLengthVector() && IndexC &&
5495+ IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5496+ APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5497+ IndexC->getZExtValue());
5498+ return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5499+ Depth + 1);
5500+ }
5501+ break;
5502+ }
5503+
5504+ case ISD::INSERT_VECTOR_ELT: {
5505+ SDValue InVec = Op.getOperand(0);
5506+ SDValue InVal = Op.getOperand(1);
5507+ SDValue EltNo = Op.getOperand(2);
5508+ EVT VT = InVec.getValueType();
5509+ auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5510+ if (IndexC && VT.isFixedLengthVector() &&
5511+ IndexC->getZExtValue() < VT.getVectorNumElements()) {
5512+ if (DemandedElts[IndexC->getZExtValue()] &&
5513+ !isGuaranteedNotToBeUndefOrPoison(InVal, PoisonOnly, Depth + 1))
5514+ return false;
5515+ APInt InVecDemandedElts = DemandedElts;
5516+ InVecDemandedElts.clearBit(IndexC->getZExtValue());
5517+ if (!!InVecDemandedElts &&
5518+ !isGuaranteedNotToBeUndefOrPoison(
5519+ peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5520+ InVecDemandedElts, PoisonOnly, Depth + 1))
5521+ return false;
5522+ return true;
5523+ }
5524+ break;
5525+ }
5526+
5527+ case ISD::SCALAR_TO_VECTOR:
5528+ // Check upper (known undef) elements.
5529+ if (DemandedElts.ugt(1) && !PoisonOnly)
5530+ return false;
5531+ // Check element zero.
5532+ if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0),
5533+ PoisonOnly,
5534+ Depth + 1))
5535+ return false;
5536+ return true;
5537+
54595538 case ISD::SPLAT_VECTOR:
54605539 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
54615540 Depth + 1);
@@ -5478,6 +5557,52 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op,
54785557 return true;
54795558 }
54805559
5560+ case ISD::SHL:
5561+ case ISD::SRL:
5562+ case ISD::SRA:
5563+ // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5564+ // enough to check operand 0 if Op can't create undef/poison.
5565+ return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5566+ /*ConsiderFlags*/ true, Depth) &&
5567+ isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5568+ PoisonOnly, Depth + 1);
5569+
5570+ case ISD::BSWAP:
5571+ case ISD::CTPOP:
5572+ case ISD::BITREVERSE:
5573+ case ISD::AND:
5574+ case ISD::OR:
5575+ case ISD::XOR:
5576+ case ISD::ADD:
5577+ case ISD::SUB:
5578+ case ISD::MUL:
5579+ case ISD::SADDSAT:
5580+ case ISD::UADDSAT:
5581+ case ISD::SSUBSAT:
5582+ case ISD::USUBSAT:
5583+ case ISD::SSHLSAT:
5584+ case ISD::USHLSAT:
5585+ case ISD::SMIN:
5586+ case ISD::SMAX:
5587+ case ISD::UMIN:
5588+ case ISD::UMAX:
5589+ case ISD::ZERO_EXTEND:
5590+ case ISD::SIGN_EXTEND:
5591+ case ISD::ANY_EXTEND:
5592+ case ISD::TRUNCATE:
5593+ case ISD::VSELECT: {
5594+ // If Op can't create undef/poison and none of its operands are undef/poison
5595+ // then Op is never undef/poison. A difference from the more common check
5596+ // below, outside the switch, is that we handle elementwise operations for
5597+ // which the DemandedElts mask is valid for all operands here.
5598+ return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5599+ /*ConsiderFlags*/ true, Depth) &&
5600+ all_of(Op->ops(), [&](SDValue V) {
5601+ return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5602+ PoisonOnly, Depth + 1);
5603+ });
5604+ }
5605+
54815606 // TODO: Search for noundef attributes from library functions.
54825607
54835608 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
@@ -12463,6 +12588,23 @@ SDValue llvm::peekThroughTruncates(SDValue V) {
1246312588 return V;
1246412589}
1246512590
12591+ SDValue llvm::peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts) {
12592+ while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12593+ SDValue InVec = V.getOperand(0);
12594+ SDValue EltNo = V.getOperand(2);
12595+ EVT VT = InVec.getValueType();
12596+ auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12597+ if (IndexC && VT.isFixedLengthVector() &&
12598+ IndexC->getZExtValue() < VT.getVectorNumElements() &&
12599+ !DemandedElts[IndexC->getZExtValue()]) {
12600+ V = InVec;
12601+ continue;
12602+ }
12603+ break;
12604+ }
12605+ return V;
12606+ }
12607+
1246612608bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
1246712609 if (V.getOpcode() != ISD::XOR)
1246812610 return false;
0 commit comments