Skip to content

Commit ae36702

Browse files
authored
[RISCV] Guard against out of bound shifts in expandMul. (#150464)
Spotted while reviewing #150211. If we're multiplying by -3 in i32 MulAmt contains 4,294,967,293 since we zero extend to uint64_t. Adding 3 to this gives 0x100000000 which is a power of 2 and the log2 of that is 32, but we can't shift left by 32 in an i32. Detect this case and skip the transform. We could use 0, but we don't handle the case for i64 so this seemed more consistent. Normally we don't hit this case because decomposeMulByConstant handles it, but that's disabled by Xqciac. And after #150211 the code in expandMul is now unreachable for this case.
1 parent 9f724d0 commit ae36702

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16184,10 +16184,12 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1618416184
// 2^N - 3/5/9 --> (sub (shl X, C1), (shXadd X, x))
1618516185
for (uint64_t Offset : {3, 5, 9}) {
1618616186
if (isPowerOf2_64(MulAmt + Offset)) {
16187+
unsigned ShAmt = Log2_64(MulAmt + Offset);
16188+
if (ShAmt >= VT.getSizeInBits())
16189+
continue;
1618716190
SDLoc DL(N);
1618816191
SDValue Shift1 =
16189-
DAG.getNode(ISD::SHL, DL, VT, X,
16190-
DAG.getConstant(Log2_64(MulAmt + Offset), DL, VT));
16192+
DAG.getNode(ISD::SHL, DL, VT, X, DAG.getConstant(ShAmt, DL, VT));
1619116193
SDValue Mul359 =
1619216194
DAG.getNode(RISCVISD::SHL_ADD, DL, VT, X,
1619316195
DAG.getConstant(Log2_64(Offset - 1), DL, VT), X);

0 commit comments

Comments
 (0)