[llvm] [Transforms] LoopIdiomRecognize recognize strlen and wcslen (PR #108985)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 2 08:00:38 PDT 2024


================
@@ -1524,6 +1545,232 @@ static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
   return nullptr;
 }
 
+/// Recognizes a strlen idiom by checking for loops that increment
+/// a char pointer and then subtract with the base pointer.
+///
+/// If detected, transforms the relevant code to a strlen function
+/// call, and returns true; otherwise, returns false.
+///
+/// The core idiom we are trying to detect is:
+/// \code
+///     start = str;
+///     do {
+///       str++;
+///     } while(*str != '\0');
+/// \endcode
+///
+/// The transformed output is similar to below c-code:
+/// \code
+///     str = start + strlen(start)
+///     len = str - start
+/// \endcode
+///
+/// Later the pointer subtraction will be folded by InstCombine
+bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
+  if (DisableLIRPStrlen)
+    return false;
+
+  // Give up if the loop has multiple blocks or multiple backedges.
+  if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
+    return false;
+
+  // It should have a preheader containing nothing but an unconditional branch.
+  auto *Preheader = CurLoop->getLoopPreheader();
----------------
Meinersbur wrote:

```suggestion
  BasicBlock *Preheader = CurLoop->getLoopPreheader();
```
[LLVM does not use almost-always-auto style](https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable).

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


More information about the llvm-commits mailing list