[llvm] [SelectionDAG] Optimize 32-bit udiv with 33-bit magic constants on 64-bit targets (PR #181288)
MITSUNARI Shigeo via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 12 19:47:42 PST 2026
================
@@ -6864,6 +6894,46 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
PostShift = PostShifts[0];
}
+ // Our Approach: Use optimized 33-bit method for 32-bit division
+ if (Use33BitOptimization) {
+ // x is i32, MagicFactor is pre-shifted i64 constant
+ // Compute: (i64(x) * MagicFactor) >> 64
+ EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), 64);
+ SDValue X64 = DAG.getNode(ISD::ZERO_EXTEND, dl, WideVT, N0);
+
+ // Get the pre-shifted constant (it's already in MagicFactor as i64)
+ SDValue MagicFactor64 = isa<ConstantSDNode>(MagicFactor)
+ ? MagicFactor
+ : MagicFactors[0];
+
+ SDValue Result;
+ // Perform 64x64 -> 128 multiplication and extract high 64 bits
+ if (isOperationLegalOrCustom(ISD::MULHU, WideVT, IsAfterLegalization)) {
+ SDValue High = DAG.getNode(ISD::MULHU, dl, WideVT, X64, MagicFactor64);
+ Created.push_back(High.getNode());
+ // Truncate back to i32
+ Result = DAG.getNode(ISD::TRUNCATE, dl, VT, High);
+ } else if (isOperationLegalOrCustom(ISD::UMUL_LOHI, WideVT, IsAfterLegalization)) {
+ SDValue LoHi = DAG.getNode(ISD::UMUL_LOHI, dl,
+ DAG.getVTList(WideVT, WideVT),
+ X64, MagicFactor64);
+ SDValue High = SDValue(LoHi.getNode(), 1);
+ Created.push_back(LoHi.getNode());
+ Result = DAG.getNode(ISD::TRUNCATE, dl, VT, High);
+ } else {
+ // Fallback to standard path if 64-bit MULHU is not available
----------------
herumi wrote:
Sorry, I forgot to remove the debugging code. I'll remove it.
https://github.com/llvm/llvm-project/pull/181288
More information about the llvm-commits
mailing list