Skip to content

[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

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions llvm/lib/Target/AMDGPU/SIISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor

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

Copy link
Contributor Author

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

SDValue MulLHS, SDValue MulRHS,
SDValue AddRHS) {
if (MulRHS.getOpcode() == ISD::SRL)
std::swap(MulLHS, MulRHS);

if (MulLHS.getValueType() != MVT::i64 || MulLHS.getOpcode() != ISD::SRL)
return SDValue();

ConstantSDNode *ShiftVal = dyn_cast<ConstantSDNode>(MulLHS.getOperand(1));
if (!ShiftVal || ShiftVal->getAsZExtVal() != 32 ||
MulLHS.getOperand(0) != AddRHS)
return SDValue();

ConstantSDNode *Const = dyn_cast<ConstantSDNode>(MulRHS.getNode());
if (!Const || Hi_32(Const->getZExtValue()) != -1)
return SDValue();

SDValue ConstMul =
DAG.getConstant(Lo_32(Const->getZExtValue()), 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.
//
Expand Down Expand Up @@ -13915,6 +13946,9 @@ SDValue SITargetLowering::tryFoldToMad64_32(SDNode *N,
SDValue MulRHS = LHS.getOperand(1);
SDValue AddRHS = RHS;

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.
Expand Down
Loading
Loading