-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[DAG] Fold trunc(abdu(x,y)) and trunc(abds(x,y)) if they have sufficient leading zero/sign bits #151471
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
base: main
Are you sure you want to change the base?
[DAG] Fold trunc(abdu(x,y)) and trunc(abds(x,y)) if they have sufficient leading zero/sign bits #151471
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 |
---|---|---|
|
@@ -607,6 +607,7 @@ namespace { | |
SDValue foldLogicOfSetCCs(bool IsAnd, SDValue N0, SDValue N1, | ||
const SDLoc &DL); | ||
SDValue foldSubToUSubSat(EVT DstVT, SDNode *N, const SDLoc &DL); | ||
SDValue foldAbdToNarrowType(EVT VT, SDNode *N, const SDLoc &DL); | ||
SDValue foldABSToABD(SDNode *N, const SDLoc &DL); | ||
SDValue foldSelectToABD(SDValue LHS, SDValue RHS, SDValue True, | ||
SDValue False, ISD::CondCode CC, const SDLoc &DL); | ||
|
@@ -3925,6 +3926,47 @@ SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *N, const SDLoc &DL) { | |
return SDValue(); | ||
} | ||
|
||
// trunc (ABDU/S A, B)) → ABDU/S (trunc A), (trunc B) | ||
SDValue DAGCombiner::foldAbdToNarrowType(EVT VT, SDNode *N, const SDLoc &DL) { | ||
SDValue Op = N->getOperand(0); | ||
|
||
unsigned Opcode = Op.getOpcode(); | ||
if (Opcode != ISD::ABDU && Opcode != ISD::ABDS) | ||
return SDValue(); | ||
|
||
SDValue Operand0 = Op.getOperand(0); | ||
SDValue Operand1 = Op.getOperand(1); | ||
|
||
// Early exit if either operand is zero. | ||
if (ISD::isBuildVectorAllZeros(Operand0.getNode()) || | ||
ISD::isBuildVectorAllZeros(Operand1.getNode())) | ||
return SDValue(); | ||
|
||
EVT SrcVT = Op.getValueType(); | ||
EVT TruncVT = N->getValueType(0); | ||
unsigned NumSrcBits = SrcVT.getScalarSizeInBits(); | ||
unsigned NumTruncBits = TruncVT.getScalarSizeInBits(); | ||
unsigned NeededBits = NumSrcBits - NumTruncBits; | ||
|
||
bool CanFold = false; | ||
|
||
if (Opcode == ISD::ABDU) { | ||
KnownBits Known = DAG.computeKnownBits(Op); | ||
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. Why are you testing the ABD result instead of the operands like the alive2 tests? Is the fold still correct? |
||
CanFold = Known.countMinLeadingZeros() >= NeededBits; | ||
} else { | ||
unsigned SignBits = DAG.ComputeNumSignBits(Op); | ||
CanFold = SignBits >= NeededBits; | ||
} | ||
|
||
if (CanFold) { | ||
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. Just because we can fold doesn't mean we should - look at the switch statement at the bottom of visitTRUNCATE - it has a series of legality/profitability checks for different opcodes so that we don't always fold trunc(abd(x,y)) -> abd(trunc(x),trunc(y)) if it'd be more costly. |
||
SDValue NewOp0 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Operand0); | ||
SDValue NewOp1 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Operand1); | ||
return DAG.getNode(Opcode, DL, TruncVT, NewOp0, NewOp1); | ||
} | ||
|
||
return SDValue(); | ||
} | ||
|
||
// Refinement of DAG/Type Legalisation (promotion) when CTLZ is used for | ||
// counting leading ones. Broadly, it replaces the substraction with a left | ||
// shift. | ||
|
@@ -16275,6 +16317,10 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) { | |
if (SDValue NewVSel = matchVSelectOpSizesWithSetCC(N)) | ||
return NewVSel; | ||
|
||
// fold trunc (ABDU/S A, B)) → ABDU/S (trunc A), (trunc B) | ||
if (SDValue V = foldAbdToNarrowType(VT, N, SDLoc(N))) | ||
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. better to handle these in the switch statement below - which is what its there for |
||
return V; | ||
|
||
// Narrow a suitable binary operation with a non-opaque constant operand by | ||
// moving it ahead of the truncate. This is limited to pre-legalization | ||
// because targets may prefer a wider type during later combines and invert | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? Doesn't visitABD handle this eventually anyhow?