Skip to content

Commit

Permalink
[RISCV] Strength reduce mul by 2^M - 3/5/9 (#88993)
Browse files Browse the repository at this point in the history
We can expand these as the three instruction sequence: (sub (shl X, C1), (shXadd X, x)).
  • Loading branch information
preames committed Apr 24, 2024
1 parent a38f201 commit b4f923e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 85 deletions.
14 changes: 14 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13490,6 +13490,20 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
}
}

// 2^N - 3/5/9 --> (sub (shl X, C1), (shXadd X, x))
for (uint64_t Offset : {3, 5, 9}) {
if (isPowerOf2_64(MulAmt + Offset)) {
SDLoc DL(N);
SDValue Shift1 =
DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
DAG.getConstant(Log2_64(MulAmt + Offset), DL, VT));
SDValue Mul359 = DAG.getNode(RISCVISD::SHL_ADD, DL, VT, N->getOperand(0),
DAG.getConstant(Log2_64(Offset - 1), DL, VT),
N->getOperand(0));
return DAG.getNode(ISD::SUB, DL, VT, Shift1, Mul359);
}
}

return SDValue();
}

Expand Down
Loading

0 comments on commit b4f923e

Please sign in to comment.