From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B6EF73858CDA; Wed, 14 Jun 2023 11:01:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6EF73858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686740494; bh=8eKxlgqLcuAeK1irLMqVazVcyaUxI1LHQjwRkF0QOBw=; h=From:To:Subject:Date:From; b=hyfugWEK1TZQlWHOzvDhr+TuPVK7psr6oc77GacK5x+kGjR+ZZSMm8OhqhZj5BFfM fOohl2sKAsZ4MnWMULo/19XExKnPSs/P7iaHvsGrBIFw/axCdFNWtHtWUVWyIt0bwM tTdZ0EEZfROItrO0V8pNqOaJIhWKsZsxjI4nITJI= From: "david at westcontrol dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/110249] New: __builtin_unreachable helps optimisation at -O1 but not at -O2 Date: Wed, 14 Jun 2023 11:01:34 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: david at westcontrol dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110249 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 compi= ler hints that can improve optimisation. For example, it can be used here to t= ell the compiler that the parameter is always 8-byte aligned: #include #include 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;=20=20=20=20=20 } 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 t= hat 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 v= ia a builtin, or implicit via other code) should be kept - -O2 should have at le= ast 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.)=