[llvm] [LegalizeTypes] Expand 128-bit UDIV/UREM by constant via Chunk Addition (PR #146238)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 00:14:44 PST 2026


================
@@ -8200,6 +8198,97 @@ 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.
+    unsigned MaxChunk;
----------------
arsenm wrote:

```suggestion
```

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


More information about the llvm-commits mailing list