-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SelectionDAG] Handle more opcodes in isGuaranteedNotToBeUndefOrPoison #147019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5460,6 +5460,83 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op, | |
| } | ||
| return true; | ||
|
|
||
| case ISD::EXTRACT_SUBVECTOR: { | ||
| SDValue Src = Op.getOperand(0); | ||
| if (Src.getValueType().isScalableVector()) | ||
| break; | ||
| uint64_t Idx = Op.getConstantOperandVal(1); | ||
| unsigned NumSrcElts = Src.getValueType().getVectorNumElements(); | ||
| APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx); | ||
| return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly, | ||
| Depth + 1); | ||
| } | ||
|
|
||
| case ISD::INSERT_SUBVECTOR: { | ||
| if (Op.getValueType().isScalableVector()) | ||
| break; | ||
| SDValue Src = Op.getOperand(0); | ||
| SDValue Sub = Op.getOperand(1); | ||
| uint64_t Idx = Op.getConstantOperandVal(2); | ||
| unsigned NumSubElts = Sub.getValueType().getVectorNumElements(); | ||
| APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx); | ||
| APInt DemandedSrcElts = DemandedElts; | ||
| DemandedSrcElts.clearBits(Idx, Idx + NumSubElts); | ||
|
|
||
| if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison( | ||
| Sub, DemandedSubElts, PoisonOnly, Depth + 1)) | ||
| return false; | ||
| if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison( | ||
| Src, DemandedSrcElts, PoisonOnly, Depth + 1)) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| case ISD::EXTRACT_VECTOR_ELT: { | ||
| SDValue Src = Op.getOperand(0); | ||
| auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1)); | ||
| EVT SrcVT = Src.getValueType(); | ||
| if (SrcVT.isFixedLengthVector() && IndexC && | ||
| IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) { | ||
| APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(), | ||
| IndexC->getZExtValue()); | ||
| return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly, | ||
| Depth + 1); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case ISD::INSERT_VECTOR_ELT: { | ||
| SDValue InVec = Op.getOperand(0); | ||
| SDValue InVal = Op.getOperand(1); | ||
| SDValue EltNo = Op.getOperand(2); | ||
| EVT VT = InVec.getValueType(); | ||
| auto *IndexC = dyn_cast<ConstantSDNode>(EltNo); | ||
| if (IndexC && VT.isFixedLengthVector() && | ||
| IndexC->getAPIntValue().ult(VT.getVectorNumElements())) { | ||
| if (DemandedElts[IndexC->getZExtValue()] && | ||
| !isGuaranteedNotToBeUndefOrPoison(InVal, PoisonOnly, Depth + 1)) | ||
| return false; | ||
| APInt InVecDemandedElts = DemandedElts; | ||
| InVecDemandedElts.clearBit(IndexC->getZExtValue()); | ||
| if (!!InVecDemandedElts && | ||
| !isGuaranteedNotToBeUndefOrPoison(InVec, InVecDemandedElts, | ||
| PoisonOnly, Depth + 1)) | ||
| return false; | ||
| return true; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case ISD::SCALAR_TO_VECTOR: | ||
| // Check upper (known undef) elements. | ||
| if (DemandedElts.ugt(1) && !PoisonOnly) | ||
| return false; | ||
| // Check element zero. | ||
| if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison( | ||
| Op.getOperand(0), PoisonOnly, Depth + 1)) | ||
| return false; | ||
| return true; | ||
|
|
||
| case ISD::SPLAT_VECTOR: | ||
| return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly, | ||
| Depth + 1); | ||
|
|
@@ -5482,6 +5559,52 @@ bool SelectionDAG::isGuaranteedNotToBeUndefOrPoison(SDValue Op, | |
| return true; | ||
| } | ||
|
|
||
| case ISD::SHL: | ||
| case ISD::SRL: | ||
| case ISD::SRA: | ||
| // Shift amount operand is checked by canCreateUndefOrPoison. So it is | ||
| // enough to check operand 0 if Op can't create undef/poison. | ||
| return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, | ||
| /*ConsiderFlags*/ true, Depth) && | ||
| isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts, | ||
| PoisonOnly, Depth + 1); | ||
|
|
||
| case ISD::BSWAP: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which of these have you explicitly checked and which are just by inspection?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I've been out of office some days so memory is a bit vague...) I think I added most of these just by trying to find fairly common opcodes (at least pretty common from my perspective) that have "elementwise operands". Unfortunately this patch does not include any lit tests that is impacted by this extra DemandedElts propagation. The motivating case was something I think I saw when working on the other patches with "DoNotPoisionEltMask" for SimplifyDemanded functions. |
||
| case ISD::CTPOP: | ||
| case ISD::BITREVERSE: | ||
| case ISD::AND: | ||
| case ISD::OR: | ||
| case ISD::XOR: | ||
| case ISD::ADD: | ||
| case ISD::SUB: | ||
| case ISD::MUL: | ||
| case ISD::SADDSAT: | ||
| case ISD::UADDSAT: | ||
| case ISD::SSUBSAT: | ||
| case ISD::USUBSAT: | ||
| case ISD::SSHLSAT: | ||
| case ISD::USHLSAT: | ||
| case ISD::SMIN: | ||
| case ISD::SMAX: | ||
| case ISD::UMIN: | ||
| case ISD::UMAX: | ||
| case ISD::ZERO_EXTEND: | ||
| case ISD::SIGN_EXTEND: | ||
| case ISD::ANY_EXTEND: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should ANY_EXTEND be here (canCreateUndefOrPoison will always return true)?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including it here merely makes it pass on the DemandedElts in the canCreateUndefOrPoison call (instead of ending up in the default canCreateUndefOrPoison check without DemandedElts at the end). So having it here make sense to me, as it would be correct even if we update canCreateUndefOrPoison in the future. Btw, maybe canCreateUndefOrPoison actually should be updated handle ANY_EXTEND and return false for PoisonOnly. Not quite sure for the !PoisonOnly situation. The upper bits is undefined, but should the result of ANY_EXTEND really be considered as "Undef"? |
||
| case ISD::TRUNCATE: | ||
| case ISD::VSELECT: { | ||
| // If Op can't create undef/poison and none of its operands are undef/poison | ||
| // then Op is never undef/poison. A difference from the more common check | ||
| // below, outside the switch, is that we handle elementwise operations for | ||
| // which the DemandedElts mask is valid for all operands here. | ||
| return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, | ||
| /*ConsiderFlags*/ true, Depth) && | ||
| all_of(Op->ops(), [&](SDValue V) { | ||
| return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, | ||
| PoisonOnly, Depth + 1); | ||
| }); | ||
| } | ||
|
|
||
| // TODO: Search for noundef attributes from library functions. | ||
|
|
||
| // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.