[llvm] [InstCombine] Generalize zext(add X, -C) + C folding (PR #191723)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 14 12:00:45 PDT 2026
================
@@ -1005,13 +1005,21 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
Add, Builder.CreateBinaryIntrinsic(
Intrinsic::usub_sat, X, ConstantInt::get(Add.getType(), -*C)));
- // Fold (add (zext (add X, -1)), 1) -> (zext X) if X is non-zero.
- // TODO: There's a general form for any constant on the outer add.
- if (C->isOne()) {
- if (match(Op0, m_ZExt(m_Add(m_Value(X), m_AllOnes())))) {
- const SimplifyQuery Q = SQ.getWithInstruction(&Add);
- if (llvm::isKnownNonZero(X, Q))
- return new ZExtInst(X, Ty);
+ // Fold (add (zext (add X, -C)), C) -> (zext X) if X u>= C.
----------------
Maiowaa wrote:
Thanks for the guidance! I've added a generalized correctness proof to the PR description using symbolic C.
The constraint that C fits in the narrow bitwidth is modeled using llvm.ctlz, and the precondition X u>= C is enforced via llvm.assume. Alive2 verifies the transformation under these conditions.
For convenience, here is the link to the proof: https://alive2.llvm.org/ce/z/jCVPWr
https://github.com/llvm/llvm-project/pull/191723
More information about the llvm-commits
mailing list