[llvm] InstCombine: improve optimizations for ceiling division with no overflow (PR #142869)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 16 00:38:43 PDT 2025


================
@@ -1787,6 +1787,36 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
   if (Instruction *Ashr = foldAddToAshr(I))
     return Ashr;
 
+  // Ceiling division by power-of-2:
+  // (X >> log2(N)) + zext(X & (N-1) != 0) --> (X + (N-1)) >> log2(N)
+  // This is valid when adding (N-1) to X doesn't overflow.
+  {
+    Value *X = nullptr;
+    const APInt *ShiftAmt = nullptr, *Mask = nullptr;
----------------
nikic wrote:

```suggestion
    Value *X;
    const APInt *ShiftAmt, *Mask;
```

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


More information about the llvm-commits mailing list