public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110249] New: __builtin_unreachable helps optimisation at -O1 but not at -O2
@ 2023-06-14 11:01 david at westcontrol dot com
  2023-06-14 11:15 ` [Bug c/110249] " amonakov at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: david at westcontrol dot com @ 2023-06-14 11:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110249

            Bug ID: 110249
           Summary: __builtin_unreachable helps optimisation at -O1 but
                    not at -O2
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david at westcontrol dot com
  Target Milestone: ---

Sometimes it can be useful to use __builtin_unreachable() to give the compiler
hints that can improve optimisation.  For example, it can be used here to tell
the compiler that the parameter is always 8-byte aligned:

#include <stdint.h>
#include <string.h>

uint64_t read64(const uint64_t * p) {
    if ((uint64_t) p % 8 ) {
        __builtin_unreachable();
    }
     uint64_t value;
     memcpy( &value, p, sizeof(uint64_t) );
     return value;     
}

For some targets, such as 32-bit ARM and especially RISC-V, this can make a
difference to the generated code.  In testing, when given -O1 the compiler
takes advantage of the explicit undefined behaviour to see that the pointer is
aligned, and generates a single 64-bit load.  With -O2, however, it seems that
information is lost - perhaps due to earlier optimisation passes - and now slow
unaligned load code is generated.

Ideally, such optimisation information from undefined behaviour (explicit via a
builtin, or implicit via other code) should be kept - -O2 should have at least
as much information as -O1.  An alternative would be the addition of a more
directed "__builtin_assume" function that could be used.

(I know that in this particular case, __builtin_assume_aligned is available and
works exactly as intended at -O1 and -O2, but I think this is a more general
issue than just alignments.)

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

end of thread, other threads:[~2023-09-19 15:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-14 11:01 [Bug c/110249] New: __builtin_unreachable helps optimisation at -O1 but not at -O2 david at westcontrol dot com
2023-06-14 11:15 ` [Bug c/110249] " amonakov at gcc dot gnu.org
2023-06-14 11:38 ` david at westcontrol dot com
2023-06-14 12:16 ` rguenth at gcc dot gnu.org
2023-06-14 12:43 ` david at westcontrol dot com
2023-09-19 14:30 ` [Bug tree-optimization/110249] " cvs-commit at gcc dot gnu.org
2023-09-19 15:04 ` amacleod at redhat dot com

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).