[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:08:23 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
+  if (Lg2 >= 11)
----------------
topperc wrote:

Probably better to check `Divisor > 2048 || Division < -2048`? The Lg2 check made sense when Lg2 was needed later, but that's in TargetLowering::buildSDIVPow2WithCMov now.

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


More information about the llvm-commits mailing list