Skip to content

Commit b4f923e

Browse files
authored
[RISCV] Strength reduce mul by 2^M - 3/5/9 (#88993)
We can expand these as the three instruction sequence: (sub (shl X, C1), (shXadd X, x)).
1 parent a38f201 commit b4f923e

File tree

3 files changed

+165
-85
lines changed

3 files changed

+165
-85
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13490,6 +13490,20 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1349013490
}
1349113491
}
1349213492

13493+
// 2^N - 3/5/9 --> (sub (shl X, C1), (shXadd X, x))
13494+
for (uint64_t Offset : {3, 5, 9}) {
13495+
if (isPowerOf2_64(MulAmt + Offset)) {
13496+
SDLoc DL(N);
13497+
SDValue Shift1 =
13498+
DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
13499+
DAG.getConstant(Log2_64(MulAmt + Offset), DL, VT));
13500+
SDValue Mul359 = DAG.getNode(RISCVISD::SHL_ADD, DL, VT, N->getOperand(0),
13501+
DAG.getConstant(Log2_64(Offset - 1), DL, VT),
13502+
N->getOperand(0));
13503+
return DAG.getNode(ISD::SUB, DL, VT, Shift1, Mul359);
13504+
}
13505+
}
13506+
1349313507
return SDValue();
1349413508
}
1349513509

0 commit comments

Comments
 (0)