Issue 129615
Summary Optimize chains of `memcmp`
Labels new issue
Assignees
Reporter Kmeakin
    Often in C/C++, since there is no way to `switch` on strings, code will have big if-else chains of (the moral equivalent of) `memcp(s, "some string") == 0`. For example, [LLVM's own lexer](https://github.com/llvm/llvm-project/blob/main/llvm/lib/AsmParser/LLLexer.cpp#L493) compares every identifier against a few hundred different string literals to recognize keywords.

It would be nice if LLVM could optimize such chains into a more efficient method, eg a trie:

```c++
auto src(std::string_view s) {
    if (s == "aaa") return 1;
    if (s == "abb") return 2;
 if (s == "bbb") return 3;
    if (s == "baa") return 4;

    ...

 return 0;
}

auto tgt(std::string_view s) {
    if (s[0] == 'a') {
 if (s[1] == 'a' && s[2] == 'a') return 1;
        if (s[1] == 'b' && s[2] == 'b') return 2;
    }
    if (s[0] == 'b') {
        if (s[1] == 'a' && s[2] == 'a') return 3;
        if (s[1] == 'b' && s[2] == 'b') return 4;
 }
    
    ...

    return 0;
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to