[llvm] [InstCombine] Generalize zext(add X, -C) + C folding (PR #191723)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 12 09:31:09 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 1db7cc7dd..39f1f6bc0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -909,7 +909,8 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
m_SpecificIntAllowPoison(BitWidth - 1)))) &&
match(Op1, m_One()))
return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
- if (match(Op1, m_One()) && !cast<Constant>(Op1)->containsUndefOrPoisonElement()) {
+ if (match(Op1, m_One()) &&
+ !cast<Constant>(Op1)->containsUndefOrPoisonElement()) {
if (match(Op0, m_ZExt(m_Add(m_Value(X), m_AllOnes())))) {
const SimplifyQuery Q = SQ.getWithInstruction(&Add);
if (llvm::isKnownNonZero(X, Q))
@@ -1011,20 +1012,19 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
Add, Builder.CreateBinaryIntrinsic(
Intrinsic::usub_sat, X, ConstantInt::get(Add.getType(), -*C)));
-
// Fold (add (zext (add X, -C)), C) -> (zext X) if X u>= C.
// Generalizes the C==1 case to any constant.
-/*
- {
- const APInt *InnerC;
- if (match(Op0, m_ZExt(m_Add(m_Value(X), m_APIntAllowPoison(InnerC)))) &&
- *InnerC == -(*C)) {
- const SimplifyQuery Q = SQ.getWithInstruction(&Add);
- if (C->isOne() ? llvm::isKnownNonZero(X, Q)
- : computeKnownBits(X, &Add).getMinValue().uge(*C))
- return new ZExtInst(X, Ty);
- }
- }*/
+ /*
+ {
+ const APInt *InnerC;
+ if (match(Op0, m_ZExt(m_Add(m_Value(X), m_APIntAllowPoison(InnerC)))) &&
+ *InnerC == -(*C)) {
+ const SimplifyQuery Q = SQ.getWithInstruction(&Add);
+ if (C->isOne() ? llvm::isKnownNonZero(X, Q)
+ : computeKnownBits(X, &Add).getMinValue().uge(*C))
+ return new ZExtInst(X, Ty);
+ }
+ }*/
return nullptr;
}
// match variations of a^2 + 2*a*b + b^2
``````````
</details>
https://github.com/llvm/llvm-project/pull/191723
More information about the llvm-commits
mailing list