llvm / llvm/llvm-project

`memcmp` -> `bcmp` optimization not firing when fed to bitwise or

Open
#183,003 4 comments 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Testcase:
```c++
char *p;
bool b;
int main() {
b |= __builtin_memcmp(p, "1234567", 7);
}
```

This produces unnecessarily slow code, which computes not only whether the strings are equal but also whether they are greater or less: https://godbolt.org/z/Tf71qovK3

It looks like the transform from the `memcmp` to `bcmp` lib function is not firing here, though it should be able to.

Contributor guide

Open the contributing guide

Research direction

Start with the provided C++ testcase using __builtin_memcmp in a bitwise-or expression, and reproduce the generated code from the linked Godbolt example. Trace the memcmp-to-bcmp optimization and verify that the completed change makes this case use the bcmp form without computing ordering information.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.