[llvm] [RISCV] Add 2^N + 2^M expanding pattern for mul (PR #137954)
Min-Yih Hsu via llvm-commits
llvm-commits at lists.llvm.org
Wed May 7 10:56:03 PDT 2025
================
@@ -15520,6 +15520,28 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget);
}
+// 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;
+ if (isPowerOf2_64(MulAmt + MulAmtLowBit))
+ Op = ISD::SUB;
+ else if (isPowerOf2_64(MulAmt - MulAmtLowBit))
+ Op = ISD::ADD;
+ else
+ return SDValue();
+ uint64_t ShiftAmt1 = MulAmt + MulAmtLowBit;
----------------
mshockwave wrote:
should this be `MulAmt - MulAmtLowBit` for ADD?
I think the reason you can get away in the tests was because, take multiplied by 36 as an example: MuAmt equals to 36 and MulAmtLowBit is 4. While the correct ShiftAmt1 should be 36 - 4 = 32 (in which case Log2_64(32) = 5), if you add them together instead and get 40, I believe Log2_64(40) will still give you 5
https://github.com/llvm/llvm-project/pull/137954
More information about the llvm-commits
mailing list