[llvm] [InstCombine] Div ceil optimizations (PR #190175)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 5 12:21:38 PDT 2026
================
@@ -1523,6 +1523,68 @@ 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));
+}
+
+// Fold the same-type form of the div_ceil idiom:
+// add(udiv(X, Y), zext(icmp ne(urem(X, Y), 0)))
+// -> udiv(add nuw(X, Y - 1), Y)
+// where udiv and urem are the same type as the add.
----------------
dtcxzyw wrote:
Use `///` for header comments.
https://github.com/llvm/llvm-project/pull/190175
More information about the llvm-commits
mailing list