[llvm] [Transforms] LoopIdiomRecognize recognize strlen and wcslen (PR #108985)
Henry Jiang via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 2 16:13:27 PST 2025
mustartt wrote:
Finally had some time to work on this patch again. Made some major improvements to increase the range of idioms that this pass would catch.
1. Now it will catch idioms in both the pointer difference form or by direct indexing.
```
size_t strlen1(const char* str) {
const char* base = str;
while (*str)
++str;
return str - base;
}
size_t strlen2(const char* str) {
size_t i = 0;
while (str[i])
++i;
return i;
}
```
2. Made the idiom less restrictive by removing the pre-requisite that the loop preheader has to be empty. Now if there are code in the loop preheader, this will still transform the idiom.
3. Found a miscompile in the expansion logic. Replaced the old `strlen` expansion with call to `SCEVExpander` after creating the new scev expression for the closed form of the idiom. As a side-effects, this pass can handle multiple induction variables that has the AddRec `{...,+,c}` for some constant `c`.
```
const char* base = str;
size_t i = 10;
while (*str) {
++str;
i += 2;
}
/* uses of i */
```
https://github.com/llvm/llvm-project/pull/108985
More information about the llvm-commits
mailing list