public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "jankowski938 at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c/108740] New: two identical functions but the code generated differs. Why? Date: Thu, 09 Feb 2023 11:17:38 +0000 [thread overview] Message-ID: <bug-108740-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108740 Bug ID: 108740 Summary: two identical functions but the code generated differs. Why? Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: jankowski938 at gmail dot com Target Milestone: --- I know it is UB but I just wonder why two identical functions generate different code: ``` #define OPPOSITE(c) (*((typeof(x) *)&(x))) int foo(volatile int x) { OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x); return x; } int bar(volatile int x) { OPPOSITE(x) = OPPOSITE(x) + OPPOSITE(x); return x; } ``` x86-65 gcc 12.2 -Wall -Wextra -Os ``` foo: mov DWORD PTR [rsp-4], edi mov eax, DWORD PTR [rsp-4] mov edx, DWORD PTR [rsp-4] add eax, edx mov DWORD PTR [rsp-4], eax mov eax, DWORD PTR [rsp-4] ret bar: mov DWORD PTR [rsp-4], edi mov eax, DWORD PTR [rsp-4] add eax, eax ret ``` ARM-eabi-none 11.2.1 -Wall -Wextra -O3 (same -|Os) ``` foo: sub sp, sp, #8 str r0, [sp, #4] ldr r3, [sp, #4] ldr r2, [sp, #4] add r3, r3, r2 str r3, [sp, #4] ldr r0, [sp, #4] add sp, sp, #8 bx lr bar: sub sp, sp, #8 str r0, [sp, #4] ldr r0, [sp, #4] lsl r0, r0, #1 add sp, sp, #8 bx lr ``` https://godbolt.org/z/7eMbPcdqs I know that is UB in C11 onwards but I would expect both to be exactly the same.
next reply other threads:[~2023-02-09 11:17 UTC|newest] Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-02-09 11:17 jankowski938 at gmail dot com [this message] 2023-02-09 11:29 ` [Bug c/108740] " jankowski938 at gmail dot com 2023-02-09 14:07 ` [Bug ipa/108740] " rguenth at gcc dot gnu.org 2023-02-09 16:18 ` jankowski938 at gmail dot com 2023-02-09 18:06 ` pinskia at gcc dot gnu.org 2023-02-09 21:19 ` [Bug ipa/108740] two identical functions but the code generated differs due to volatile argument jankowski938 at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-108740-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).