[llvm] [InstCombine] Fold sadd/ssub with overflow bitwise pattern (PR #216607)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 15:06:37 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/InstCombineAndOrXor.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/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 07dca2f15..b1b57a511 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3375,9 +3375,9 @@ static Value *foldAndOrOfICmpEqConstantAndICmp(ICmpInst *LHS, ICmpInst *RHS,
 /// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
 /// If IsLogical is true, then the and/or is in select form and the transform
 /// must be poison-safe.
-static Value *foldSignedAddSubBitwiseOverflowCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
-                                                   bool IsAnd,
-                                                   InstCombiner::BuilderTy &Builder) {
+static Value *
+foldSignedAddSubBitwiseOverflowCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
+                                     InstCombiner::BuilderTy &Builder) {
   if (!IsAnd)
     return nullptr;
 
@@ -3393,7 +3393,8 @@ static Value *foldSignedAddSubBitwiseOverflowCheck(ICmpInst *Cmp0, ICmpInst *Cmp
   };
 
   bool Cmp0Expected, Cmp1Expected;
-  if (!IsSignBitCheck(Cmp0, Cmp0Expected) || !IsSignBitCheck(Cmp1, Cmp1Expected))
+  if (!IsSignBitCheck(Cmp0, Cmp0Expected) ||
+      !IsSignBitCheck(Cmp1, Cmp1Expected))
     return nullptr;
 
   ICmpInst *SignEqCmp = nullptr;
@@ -3407,7 +3408,8 @@ static Value *foldSignedAddSubBitwiseOverflowCheck(ICmpInst *Cmp0, ICmpInst *Cmp
     SignDiffCmp = Cmp0;
   }
 
-  auto MatchPattern = [&](ICmpInst *SignEq, ICmpInst *SignDiff, bool IsSub) -> Value * {
+  auto MatchPattern = [&](ICmpInst *SignEq, ICmpInst *SignDiff,
+                          bool IsSub) -> Value * {
     Value *A, *B;
     if (!match(SignEq->getOperand(0), m_c_Xor(m_Value(A), m_Value(B))))
       return nullptr;
@@ -3434,8 +3436,10 @@ static Value *foldSignedAddSubBitwiseOverflowCheck(ICmpInst *Cmp0, ICmpInst *Cmp
         return nullptr;
     }
 
-    Intrinsic::ID IID = IsSub ? Intrinsic::ssub_with_overflow : Intrinsic::sadd_with_overflow;
-    Function *F = Intrinsic::getOrInsertDeclaration(SignEq->getModule(), IID, A->getType());
+    Intrinsic::ID IID =
+        IsSub ? Intrinsic::ssub_with_overflow : Intrinsic::sadd_with_overflow;
+    Function *F = Intrinsic::getOrInsertDeclaration(SignEq->getModule(), IID,
+                                                    A->getType());
     Value *Call = Builder.CreateCall(F, {A, B});
     return Builder.CreateExtractValue(Call, 1);
   };
@@ -3459,7 +3463,6 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
   if (Value *V = foldSignedAddSubBitwiseOverflowCheck(LHS, RHS, IsAnd, Builder))
     return V;
 
-
   ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
   Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
   Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1);

``````````

</details>


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


More information about the llvm-commits mailing list