public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ubizjak at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/113106] New: Missing CSE with cast to volatile
Date: Thu, 21 Dec 2023 10:38:39 +0000	[thread overview]
Message-ID: <bug-113106-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113106
           Summary: Missing CSE with cast to volatile
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ubizjak at gmail dot com
  Target Milestone: ---

The following testcase:

--cut here--
int a;

int foo(void)
{
  return *(volatile int *) &a + a;
}
--cut here--

compiles with -O2 to:

        movl    a(%rip), %eax
        addl    a(%rip), %eax
        ret

with more detail:

#(insn:TI 6 7 8 2 (set (reg:SI 0 ax [orig:98 _1 ] [98])
#        (mem/v/c:SI (symbol_ref:DI ("a") [flags 0x2] <var_decl
0x7f59db15ac60a>) [1 MEM[(volatile int *)&a]+0 S4 A32])) "vol.c":5:10 85
{*movsi_internal}
#     (nil))
        movl    a(%rip), %eax   # 6     [c=5 l=6]  *movsi_internal/0
#(insn 8 6 14 2 (parallel [
#            (set (reg:SI 0 ax [102])
#                (plus:SI (reg:SI 0 ax [orig:98 _1 ] [98])
#                    (mem/c:SI (symbol_ref:DI ("a") [flags 0x2] <var_decl
0x7f59db15ac60 a>) [1 a+0 S4 A32])))
#            (clobber (reg:CC 17 flags))
#        ]) "vol.c":5:31 271 {*addsi_1}
#     (expr_list:REG_UNUSED (reg:CC 17 flags)
#        (nil)))
        addl    a(%rip), %eax   # 8     [c=9 l=6]  *addsi_1/1

This may be compiled to:

        movl    a(%rip), %eax
        addl    %eax, %eax
        ret

since only one read uses volatile.

             reply	other threads:[~2023-12-21 10:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 10:38 ubizjak at gmail dot com [this message]
2023-12-21 10:41 ` [Bug c/113106] " ubizjak at gmail dot com
2023-12-21 10:44 ` ubizjak at gmail dot com
2023-12-21 12:50 ` rguenth at gcc dot gnu.org
2023-12-21 12:57 ` ubizjak at gmail dot com
2023-12-21 13:02 ` ubizjak at gmail dot com
2023-12-21 13:29 ` [Bug rtl-optimization/113106] " rguenth at gcc dot gnu.org
2023-12-21 15:08 ` ubizjak at gmail dot com
2023-12-21 15:11 ` pinskia at gcc dot gnu.org

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-113106-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: link
Be 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).