llvm / llvm/llvm-project

LLVM is too quick to run `LowerExpectIntrinsicPass` requiring redundant `expect` calls

Open
#199,610 1 comment 1 reaction 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

https://godbolt.org/z/dE7eGzojn

```c++
bool is_multiple_of(unsigned a, unsigned b) {
return __builtin_expect(a % b == 0, 1);
}

unsigned test(unsigned n, unsigned* out) {
auto b = is_multiple_of(n, 2);

if (b) {
return 0;
} else {
*out = 0;

return 1;
}
}

unsigned test2(unsigned n, unsigned* out) {
auto b = is_multiple_of(n, 2);

// redundant expect call
if (__builtin_expect(b, 1)) {
return 0;
} else {
*out = 0;

return 1;
}
}
```

```assembly
test(unsigned int, unsigned int*):
xor eax, eax
test dil, 1
je .LBB1_2
mov dword ptr [rsi], 0
mov eax, 1
.LBB1_2:
ret

test2(unsigned int, unsigned int*):
xor eax, eax
test dil, 1
jne .LBB2_1
ret
.LBB2_1:
mov dword ptr [rsi], 0
mov eax, 1
ret
```

Contributor guide

Open the contributing guide

Research direction

Start with the Compiler Explorer reproducer at https://godbolt.org/z/dE7eGzojn and investigate the LowerExpectIntrinsicPass named in the issue. Compare the generated code for test and test2; done means the redundant expect call no longer changes the resulting control flow or code unnecessarily.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.