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 via email to