[llvm] [InstCombine] Div ceil optimizations (PR #190175)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 13 09:07:48 PDT 2026


================
@@ -1523,6 +1523,49 @@ static Instruction *foldBoxMultiply(BinaryOperator &I) {
   return nullptr;
 }
 
+/// Return true if X + (Y-1) is provably non-wrapping in X's type
+static bool checkDivCeilNUW(Value *X, Value *Y, BinaryOperator &I,
+                            AssumptionCache &AC, DominatorTree &DT) {
+  ConstantRange CRX = computeConstantRange(X, /*ForSigned=*/false,
+                                           /*UseInstrInfo=*/true, &AC, &I, &DT);
+  ConstantRange CRY = computeConstantRange(Y, /*ForSigned=*/false,
+                                           /*UseInstrInfo=*/true, &AC, &I, &DT);
+  APInt MaxX = CRX.getUnsignedMax();
+  APInt MaxY = CRY.getUnsignedMax();
+  unsigned BitWidth = MaxX.getBitWidth();
+  // MaxX + (MaxY - 1) <= UINT_MAX  <==>  MaxX <= UINT_MAX - (MaxY - 1)
+  return !MaxX.ugt(APInt::getMaxValue(BitWidth) - (MaxY - 1));
----------------
dtcxzyw wrote:

```suggestion
  return !MaxX.ugt(-MaxY);
```


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


More information about the llvm-commits mailing list