[llvm] [LegalizeTypes] Expand UDIV/UREM by constant via chunk summation (PR #146238)
Shivam Gupta via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 14 05:01:43 PDT 2026
================
@@ -8233,6 +8231,94 @@ bool TargetLowering::expandDIVREMByConstant(SDNode *N,
DAG.getConstant(0, dl, HiLoVT));
Sum = DAG.getNode(ISD::ADD, dl, HiLoVT, Sum, Carry);
}
+ } else {
+ // If we cannot split in two halves. Let's look for a smaller chunk
+ // width where (1 << ChunkWidth) mod Divisor == 1.
+ // This ensures that the sum of all such chunks modulo Divisor
+ // is equivalent to the original value modulo Divisor.
+ const APInt &Divisor = CN->getAPIntValue();
+ unsigned BitWidth = VT.getScalarSizeInBits();
+ unsigned BestChunkWidth = 0;
+
+ // Determine the largest legal scalar integer type we can safely use
+ // for chunk operations.
+ EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
+
+ // Clamp to the original bit width.
+ unsigned MaxChunk =
+ std::min<unsigned>(LegalVT.getScalarSizeInBits(), BitWidth);
+
+ // Find the largest W in (MaxChunk/2, MaxChunk] such that
+ // 2^W ≡ 1 (mod Divisor). If this holds, the value can be
+ // reduced modulo Divisor by summing W-bit chunks.
+ //
+ // Instead of constructing 2^W for each candidate, compute
+ // 2^MaxChunk mod Divisor once and walk downward, maintaining:
+ //
+ // Mod == 2^i mod Divisor
+ //
+ // For each decrement of i, update Mod by multiplying with
+ // the modular inverse of 2 (Divisor is known to be odd here).
+ // Compute 2^MaxChunk mod Divisor
+ APInt Mod(Divisor.getBitWidth(), 1);
+ for (unsigned k = 0; k < MaxChunk; ++k)
+ Mod = (Mod.shl(1)).urem(Divisor);
+
+ // Since Divisor is odd, inverse of 2 mod D is (D+1)/2
+ APInt Inv2 = (Divisor + 1).lshr(1);
+
+ // Walk downward to find largest valid W
+ for (unsigned i = MaxChunk; i > MaxChunk / 2; --i) {
+ if (Mod.isOne()) {
+ BestChunkWidth = i;
+ break;
+ }
+
+ // Move from 2^i to 2^(i-1)
+ Mod = (Mod * Inv2).urem(Divisor);
+ }
+
+ // If we found a good chunk width, slice the number and sum the pieces.
+ if (!BestChunkWidth)
+ return false;
+
+ EVT ChunkVT = EVT::getIntegerVT(*DAG.getContext(), BestChunkWidth);
+
+ SDValue In =
+ LL ? DAG.getNode(ISD::BUILD_PAIR, dl, VT, LL, LH) : N->getOperand(0);
+
+ SmallVector<SDValue, 8> Parts;
+ // Split into fixed-size chunks
+ for (unsigned i = 0; i < BitWidth; i += BestChunkWidth) {
+ SDValue Shift = DAG.getShiftAmountConstant(i, VT, dl);
+ SDValue Chunk = DAG.getNode(ISD::SRL, dl, VT, In, Shift);
+ Chunk = DAG.getNode(ISD::TRUNCATE, dl, ChunkVT, Chunk);
+ Parts.push_back(Chunk);
+ }
+ assert(!Parts.empty() && "Failed to split divisor into chunks");
+ Sum = Parts[0];
+
+ // Use uaddo_carry if we can, otherwise use a compare to detect overflow.
+ // same logic as used in above if condition.
+ SDValue Carry = DAG.getConstant(0, dl, ChunkVT);
+ EVT SetCCType =
+ getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ChunkVT);
+ for (unsigned i = 1; i < Parts.size(); ++i) {
+ if (isOperationLegalOrCustom(ISD::UADDO_CARRY, ChunkVT)) {
+ SDVTList VTList = DAG.getVTList(ChunkVT, SetCCType);
+ SDValue UAdd = DAG.getNode(ISD::UADDO, dl, VTList, Sum, Parts[i]);
+ Sum = DAG.getNode(ISD::UADDO_CARRY, dl, VTList, UAdd, Carry,
+ UAdd.getValue(1));
+ } else {
+ SDValue Add = DAG.getNode(ISD::ADD, dl, ChunkVT, Sum, Parts[i]);
+ SDValue NewCarry = DAG.getSetCC(dl, SetCCType, Add, Sum, ISD::SETULT);
----------------
xgupta wrote:
SetCC or carry math is not needed in new logic now.
https://github.com/llvm/llvm-project/pull/146238
More information about the llvm-commits
mailing list