https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110359
Bug ID: 110359
Summary: d: Suboptimal codegen for __builtin_expect(cond,
false) since PR96435
Product: gcc
Version: 11.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: d
Assignee: ibuclaw at gdcproject dot org
Reporter: ibuclaw at gcc dot gnu.org
Target Milestone: ---
Since pr96435, both boolean objects *and* expressions have been evaluated in
the following way.
(*(ubyte*)&obj_or_expr) & 1
It has been noted that sometimes this can cause the back-end to optimize in
non-obvious ways - in particular with `__builtin_expect`.
---
double pow(in double x, in ulong p)
{
import gcc.builtins : __builtin_expect;
if (__builtin_expect(p == 0, false)) // 1,2
return 1;
if (__builtin_expect(p == 1, false)) // 3,4
return x;
double s = x;
double v = 1;
for (ulong i = p; i > 1; i >>= 1) // 5
{
v = (i & 0x1) ? s * v : v;
s = s * s;
}
return v * s;
}
---
The first 5 lines of assembly for the above function (corresponding souce line
marked in comments)
---
double example.pow(in double, in ulong):
test rdi, rdi
je .L6
cmp rdi, 1
je .L1
jbe .L1
---
The jbe condition looks like it is either redundant, or suboptimal.
This @safe feature could be restricted to just when reading the value of a bool
field that comes from a union.