public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug d/110359] New: d: Suboptimal codegen for __builtin_expect(cond, false) since PR96435
@ 2023-06-21 18:44 ibuclaw at gcc dot gnu.org
  2023-06-26  1:28 ` [Bug d/110359] " cvs-commit at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: ibuclaw at gcc dot gnu.org @ 2023-06-21 18:44 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-06-26  2:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 18:44 [Bug d/110359] New: d: Suboptimal codegen for __builtin_expect(cond, false) since PR96435 ibuclaw at gcc dot gnu.org
2023-06-26  1:28 ` [Bug d/110359] " cvs-commit at gcc dot gnu.org
2023-06-26  1:40 ` cvs-commit at gcc dot gnu.org
2023-06-26  2:07 ` cvs-commit at gcc dot gnu.org
2023-06-26  2:09 ` cvs-commit at gcc dot gnu.org
2023-06-26  2:10 ` cvs-commit at gcc dot gnu.org
2023-06-26  2:13 ` ibuclaw at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).