llvm-project
llvm-project copied to clipboard
LLVM is too quick to run `LowerExpectIntrinsicPass` requiring redundant `expect` calls
https://godbolt.org/z/dE7eGzojn
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;
}
}
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
Actually, if you force is_multiple_of to have a branch, it preserves the branch weights after inlining into test in select.
unsigned is_multiple_of(unsigned a, unsigned b) {
if (__builtin_expect(a % b == 0, 1)) {
return 15; // cannot be 1 otherwise select isn't generated
} else {
return 0;
}
}
However, br that follows doesn't inherit the hint.