[llvm] [InstCombine] Fix #163110: Fold icmp (shl X, L), (add (shl Y, L), 1<<L) to icmp X, (Y + 1) (PR #165975)

via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 1 00:27:26 PDT 2025


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/InstCombineCompares.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/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 28d3c772a..f6de20e01 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6002,25 +6002,26 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
   const CmpInst::Predicate Pred = I.getPredicate();
 
-  //icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw Y, 1)
+  // icmp (shl nsw X, Log2), (add nsw (shl nsw Y, Log2), K) -> icmp X, (add nsw
+  // Y, 1)
   Value *X, *Y;
   ConstantInt *CLog2M0, *CLog2M1, *CVal;
   auto M0 = m_NSWShl(m_Value(X), m_ConstantInt(CLog2M0));
-  auto M1 = m_NSWAdd(m_NSWShl (m_Value(Y), m_ConstantInt(CLog2M1)),
-                      m_ConstantInt(CVal));
+  auto M1 = m_NSWAdd(m_NSWShl(m_Value(Y), m_ConstantInt(CLog2M1)),
+                     m_ConstantInt(CVal));
 
-  if (match(&I, m_c_ICmp(M0, M1)) && CLog2M0->getValue() == CLog2M1->getValue()) {
+  if (match(&I, m_c_ICmp(M0, M1)) &&
+      CLog2M0->getValue() == CLog2M1->getValue()) {
     unsigned BitWidth = CLog2M0->getBitWidth();
     unsigned ShAmt = (unsigned)CLog2M0->getLimitedValue(BitWidth);
     APInt ExpectedK = APInt::getOneBitSet(BitWidth, ShAmt);
     if (CVal->getValue() == ExpectedK) {
-      Value *NewRHS = Builder.CreateAdd(
-          Y, ConstantInt::get(Y->getType(), 1),
-          "", /*HasNUW=*/false, /*HasNSW=*/true);
+      Value *NewRHS = Builder.CreateAdd(Y, ConstantInt::get(Y->getType(), 1),
+                                        "", /*HasNUW=*/false, /*HasNSW=*/true);
       return new ICmpInst(Pred, X, NewRHS);
     }
   }
-  
+
   Value *A, *B, *C, *D;
   if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
     if (A == Op1 || B == Op1) { // (A^B) == A  ->  B == 0

``````````

</details>


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


More information about the llvm-commits mailing list