[llvm] [RISCV][SDAG] Prefer ShortForwardBranch to lower sdiv by pow2 (PR #67364)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 09:02:01 PDT 2023


================
@@ -19304,6 +19304,26 @@ unsigned RISCVTargetLowering::getCustomCtpopCost(EVT VT,
   return isCtpopFast(VT) ? 0 : 1;
 }
 
+SDValue
+RISCVTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
+                                   SelectionDAG &DAG,
+                                   SmallVectorImpl<SDNode *> &Created) const {
+  AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes();
+  if (isIntDivCheap(N->getValueType(0), Attr))
+    return SDValue(N, 0); // Lower SDIV as SDIV
+
+  // Only perform this transform if short forward branch opt is supported.
+  if (!Subtarget.hasShortForwardBranchOpt())
+    return SDValue();
+  EVT VT = N->getValueType(0);
+  if (!(VT == MVT::i32 || (VT == MVT::i64 && Subtarget.is64Bit())))
+    return SDValue();
+  unsigned Lg2 = Divisor.countr_zero();
+  // ensure 2**k-1 < 2048
----------------
topperc wrote:

Why not compare the divisor to 2048? Why do we need to count the trailing zeros?

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


More information about the llvm-commits mailing list