-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[RISCV] Add 2^N + 2^M expanding pattern for mul #137954
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 |
---|---|---|
|
@@ -15456,6 +15456,30 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG, | |
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget); | ||
} | ||
|
||
// X * (2^N +/- 2^M) -> (add/sub (shl X, C1), (shl X, C2)) | ||
static SDValue expandMulToAddOrSubOfShl(SDNode *N, SelectionDAG &DAG, | ||
uint64_t MulAmt) { | ||
uint64_t MulAmtLowBit = MulAmt & (-MulAmt); | ||
ISD::NodeType Op; | ||
uint64_t ShiftAmt1; | ||
if (isPowerOf2_64(MulAmt + MulAmtLowBit)) { | ||
Op = ISD::SUB; | ||
ShiftAmt1 = MulAmt + MulAmtLowBit; | ||
} else if (isPowerOf2_64(MulAmt - MulAmtLowBit)) { | ||
Op = ISD::ADD; | ||
ShiftAmt1 = MulAmt - MulAmtLowBit; | ||
} else { | ||
return SDValue(); | ||
} | ||
EVT VT = N->getValueType(0); | ||
SDLoc DL(N); | ||
SDValue Shift1 = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | ||
DAG.getConstant(Log2_64(ShiftAmt1), DL, VT)); | ||
SDValue Shift2 = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | ||
DAG.getConstant(Log2_64(MulAmtLowBit), DL, VT)); | ||
return DAG.getNode(Op, DL, VT, Shift1, Shift2); | ||
} | ||
|
||
// Try to expand a scalar multiply to a faster sequence. | ||
static SDValue expandMul(SDNode *N, SelectionDAG &DAG, | ||
TargetLowering::DAGCombinerInfo &DCI, | ||
|
@@ -15589,22 +15613,7 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG, | |
return DAG.getNode(ISD::SUB, DL, VT, Shift1, Mul359); | ||
} | ||
} | ||
} | ||
|
||
// 2^N - 2^M -> (sub (shl X, C1), (shl X, C2)) | ||
uint64_t MulAmtLowBit = MulAmt & (-MulAmt); | ||
if (isPowerOf2_64(MulAmt + MulAmtLowBit)) { | ||
uint64_t ShiftAmt1 = MulAmt + MulAmtLowBit; | ||
SDLoc DL(N); | ||
SDValue Shift1 = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | ||
DAG.getConstant(Log2_64(ShiftAmt1), DL, VT)); | ||
SDValue Shift2 = | ||
DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | ||
DAG.getConstant(Log2_64(MulAmtLowBit), DL, VT)); | ||
return DAG.getNode(ISD::SUB, DL, VT, Shift1, Shift2); | ||
} | ||
|
||
if (HasShlAdd) { | ||
for (uint64_t Divisor : {3, 5, 9}) { | ||
if (MulAmt % Divisor != 0) | ||
continue; | ||
|
@@ -15630,6 +15639,9 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG, | |
} | ||
} | ||
|
||
if (SDValue V = expandMulToAddOrSubOfShl(N, DAG, MulAmt)) | ||
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. and is there any reason you moved this below strength reduction via division? (line 15681 ~ 15704) 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 vaguely remember there being a reason I broke the conditions in this order, but it looks like there's no test diffs resulting from this, so it's probably(?) fine. |
||
return V; | ||
|
||
return SDValue(); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.