[llvm] [Transforms] Recognize memcmp-like loops in LoopIdiomRecognize (PR #181562)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 23 20:05:35 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,h -- llvm/include/llvm/Transforms/Scalar/LoopIdiomRecognize.h llvm/lib/Transforms/Scalar/LoopIdiomRecognize.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/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index c62ade5cb..3653a3d1a 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -3625,12 +3625,14 @@ public:
     const SCEVUse LoadSCEV = SE->getSCEVAtScope(LoadPointer, CurLoop);
     const SCEVUnknown *LoadBase;
     const APInt *LoadStep;
-    if (!match(LoadSCEV, m_scev_AffineAddRec(m_SCEVUnknown(LoadBase), m_scev_APInt(LoadStep)))) {
+    if (!match(LoadSCEV, m_scev_AffineAddRec(m_SCEVUnknown(LoadBase),
+                                             m_scev_APInt(LoadStep)))) {
       return false;
     }
 
     // TODO: Possibly handle negative strides.
-    if (LoadType->getBitWidth() != *LoadStep * CHAR_BIT || LoadStep->isNegative())
+    if (LoadType->getBitWidth() != *LoadStep * CHAR_BIT ||
+        LoadStep->isNegative())
       return false;
 
     // Strangely the IR seems to allow this. Guess we'll just give up in this
@@ -3676,16 +3678,16 @@ public:
   /// there are no padding bytes in the buffers being read from, and that the
   /// loads are always dereferenceable up to the maximum number of times the
   /// loop backedge is taken.
-  bool detectMemcmpIdiom(PHINode *&PhiToReplace,
-                         Value *&Lhs, Value *&Rhs, const SCEV *&Len) {
-    // Step 1: Make sure we have single header, latch and exit block.    
+  bool detectMemcmpIdiom(PHINode *&PhiToReplace, Value *&Lhs, Value *&Rhs,
+                         const SCEV *&Len) {
+    // Step 1: Make sure we have single header, latch and exit block.
     // Furthermore, if the body or condition block have side effects,
     // this is clearly not equivalent to memcmp.
     BasicBlock *BodyBlock = CurLoop->getHeader();
     BasicBlock *CondBlock = CurLoop->getLoopLatch();
     BasicBlock *ExitBlock = CurLoop->getUniqueExitBlock();
     if (!BodyBlock || !CondBlock || !ExitBlock)
-        return false;
+      return false;
 
     for (const Instruction &I : *BodyBlock)
       if (I.mayHaveSideEffects())
@@ -3709,11 +3711,12 @@ public:
     // a compare operation, and that the branch operation looks correct.
     using namespace PatternMatch;
     CmpInst *CI = dyn_cast<CmpInst>(Val);
-    const CondBrInst *BodyTerminator = dyn_cast<CondBrInst>(BodyBlock->getTerminator());
-    if (!match(BodyTerminator,
-              m_Br(m_Specific(CI), m_SpecificBB(CondBlock), m_SpecificBB(ExitBlock)))) {
-      return false; 
-    } 
+    const CondBrInst *BodyTerminator =
+        dyn_cast<CondBrInst>(BodyBlock->getTerminator());
+    if (!match(BodyTerminator, m_Br(m_Specific(CI), m_SpecificBB(CondBlock),
+                                    m_SpecificBB(ExitBlock)))) {
+      return false;
+    }
 
     // Step 5: Verify that the compare operation is comparing two
     // pointers satisfying certain criteria. See
@@ -3747,7 +3750,8 @@ public:
     Lhs = BaseLHS->getValue();
     Rhs = BaseRHS->getValue();
     Type *IntIdxTy = DL->getIndexType(Lhs->getType());
-    Len = getNumBytes(MaxBackedgeTaken, IntIdxTy, SE->getConstant(*StepLHS), CurLoop, DL, SE); 
+    Len = getNumBytes(MaxBackedgeTaken, IntIdxTy, SE->getConstant(*StepLHS),
+                      CurLoop, DL, SE);
     return true;
   }
 
@@ -3782,7 +3786,8 @@ bool LoopIdiomRecognize::recognizeAndInsertMemcmp() {
   if (!Expander.isSafeToExpand(Len))
     return false;
 
-  Value* LenArg = Expander.expandCodeFor(Len, Len->getType(), PhiBlock->getTerminator());
+  Value *LenArg =
+      Expander.expandCodeFor(Len, Len->getType(), PhiBlock->getTerminator());
   Builder.SetInsertPoint(PhiBlock->getTerminator());
   Value *MemCmpCall = llvm::emitMemCmp(Lhs, Rhs, LenArg, Builder, *DL, TLI);
   Value *NewCmpInst = Builder.CreateCmp(

``````````

</details>


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


More information about the llvm-commits mailing list