-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[AMDGPU][SDAG] Try folding "lshr i64 + mad" to "mad_u64_u32" #119218
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 4 commits
ebd00b3
114095c
bfe3fae
a885dfc
0f4ea1a
b07aea4
d3db537
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 |
|---|---|---|
|
|
@@ -13857,6 +13857,37 @@ static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, EVT VT, | |
| return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad); | ||
| } | ||
|
|
||
| // Fold | ||
| // y = lshr i64 x, 32 | ||
| // res = add (mul i64 y, Const), x where "Const" is a 64-bit constant | ||
| // with Const.hi == -1 | ||
| // To | ||
| // res = mad_u64_u32 y.lo ,Const.lo, x.lo | ||
| static SDValue tryFoldMADwithSRL(SelectionDAG &DAG, const SDLoc &SL, | ||
| SDValue MulLHS, SDValue MulRHS, | ||
| SDValue AddRHS) { | ||
|
|
||
| if (MulLHS.getValueType() != MVT::i64 || MulLHS.getOpcode() != ISD::SRL) | ||
| return SDValue(); | ||
|
|
||
| ConstantSDNode *ShiftVal = dyn_cast<ConstantSDNode>(MulLHS.getOperand(1)); | ||
| if (!ShiftVal || MulLHS.getOperand(0) != AddRHS) | ||
| return SDValue(); | ||
|
|
||
| if (ShiftVal->getAsZExtVal() != 32) | ||
| return SDValue(); | ||
|
|
||
| APInt Const = dyn_cast<ConstantSDNode>(MulRHS.getNode())->getAPIntValue(); | ||
|
||
| if (!Const.isNegative() || !Const.isSignedIntN(33)) | ||
vikramRH marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return SDValue(); | ||
|
|
||
| SDValue ConstMul = | ||
| DAG.getConstant(Const.getZExtValue() & 0x00000000FFFFFFFF, SL, MVT::i32); | ||
| return getMad64_32(DAG, SL, MVT::i64, | ||
| DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulLHS), ConstMul, | ||
| DAG.getZeroExtendInReg(AddRHS, SL, MVT::i32), false); | ||
| } | ||
|
|
||
| // Fold (add (mul x, y), z) --> (mad_[iu]64_[iu]32 x, y, z) plus high | ||
| // multiplies, if any. | ||
| // | ||
|
|
@@ -13915,6 +13946,14 @@ SDValue SITargetLowering::tryFoldToMad64_32(SDNode *N, | |
| SDValue MulRHS = LHS.getOperand(1); | ||
| SDValue AddRHS = RHS; | ||
|
|
||
| if (isa<ConstantSDNode>(MulLHS) || isa<ConstantSDNode>(MulRHS)) { | ||
| if (MulRHS.getOpcode() == ISD::SRL) | ||
| std::swap(MulLHS, MulRHS); | ||
|
||
|
|
||
| if (SDValue FoldedMAD = tryFoldMADwithSRL(DAG, SL, MulLHS, MulRHS, AddRHS)) | ||
| return FoldedMAD; | ||
| } | ||
|
|
||
| // Always check whether operands are small unsigned values, since that | ||
| // knowledge is useful in more cases. Check for small signed values only if | ||
| // doing so can unlock a shorter code sequence. | ||
|
|
||
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.
Can you also implement the same in globalisel? Not sure if the original combine is there, or if that needs porting as well
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.
sure, it seems original combine needs porting