Skip to content

Commit c0f2a88

Browse files
committed
[RISCV] Guard against out of bound shifts in expandMul.
Spotted while reviewing llvm#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. We should use 0 instead of the shl in this case. Normally we don't hit this case because decomeMulByConstant handles it, but that's disabled by Qciac. And after llvm#150211 the path in expandMul will also be unreachable. So I didn't add a test to avoid messing with that review.
1 parent 05e08cd commit c0f2a88

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16184,10 +16184,15 @@ 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);
1618716188
SDLoc DL(N);
16188-
SDValue Shift1 =
16189+
SDValue Shift1;
16190+
if (ShAmt >= VT.getSizeInBits())
16191+
Shift1 = DAG.getConstant(0, DL, VT);
16192+
else
16193+
Shift1 =
1618916194
DAG.getNode(ISD::SHL, DL, VT, X,
16190-
DAG.getConstant(Log2_64(MulAmt + Offset), DL, VT));
16195+
DAG.getConstant(ShAmt, DL, VT));
1619116196
SDValue Mul359 =
1619216197
DAG.getNode(RISCVISD::SHL_ADD, DL, VT, X,
1619316198
DAG.getConstant(Log2_64(Offset - 1), DL, VT), X);

0 commit comments

Comments
 (0)