[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
Thu May 8 09:35:52 PDT 2025


================
@@ -15520,6 +15520,30 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget);
 }
 
+// X * (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;
+  uint64_t ShiftAmt1;
+  if (isPowerOf2_64(MulAmt + MulAmtLowBit)) {
+    Op = ISD::SUB;
+    ShiftAmt1 = MulAmt + MulAmtLowBit;
+  } else if (isPowerOf2_64(MulAmt - MulAmtLowBit)) {
+    Op = ISD::ADD;
+    ShiftAmt1 = MulAmt - MulAmtLowBit;
+  } else
+    return SDValue();
+  SDLoc DL(N);
+  SDValue Shift1 =
+      DAG.getNode(ISD::SHL, DL, N->getValueType(0), N->getOperand(0),
----------------
mshockwave wrote:

nit: could we factor out `N->getValueType(0)` into its own variable? It appears 4 times in this block of code.

https://github.com/llvm/llvm-project/pull/137954


More information about the llvm-commits mailing list