public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/113106] New: Missing CSE with cast to volatile
@ 2023-12-21 10:38 ubizjak at gmail dot com
2023-12-21 10:41 ` [Bug c/113106] " ubizjak at gmail dot com
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 10:38 UTC (permalink / raw)
To: gcc-bugs
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.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
@ 2023-12-21 10:41 ` ubizjak at gmail dot com
2023-12-21 10:44 ` ubizjak at gmail dot com
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 10:41 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #1 from Uroš Bizjak <ubizjak at gmail dot com> ---
Perhaps related,
--cut here--
int a;
int foo(void)
{
return *(volatile int *) &a + *(volatile int *) &a;
}
--cut here--
compiles with -O2 to:
movl a(%rip), %eax
movl a(%rip), %edx
addl %edx, %eax
ret
but may be compiled to:
movl a(%rip), %eax
addl a(%rip), %eax
ret
(the memory read may propagate to the insn)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
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
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 10:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #2 from Uroš Bizjak <ubizjak at gmail dot com> ---
For reference, the same optimization should be applied with address spaces:
--cut here--
int __seg_gs b;
int bar(void)
{
return *(volatile __seg_gs int *) &b + b;
}
--cut here--
the above testcase currently compiles to:
movl %gs:b(%rip), %eax
addl %gs:b(%rip), %eax
ret
but can be compiled to:
movl %gs:b(%rip), %eax
addl %eax, %eax
ret
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
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
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-21 12:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |rguenth at gcc dot gnu.org
--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
The situation with address-spaces isn't valid as we need to preserve the second
load because it's volatile. I think we simply refuse to combine
volatile loads out of caution in the first case.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (2 preceding siblings ...)
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
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 12:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #4 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Richard Biener from comment #3)
> The situation with address-spaces isn't valid as we need to preserve the
> second load because it's volatile. I think we simply refuse to combine
> volatile loads out of caution in the first case.
int __seg_gs b;
return *(volatile __seg_gs int *) &b + b;
But the above is the same w.r.t to volatile as:
int a;
return *(volatile int *) &a + a;
?
BTW: I also checked with clang, and it creates expected code in all cases.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (3 preceding siblings ...)
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
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 13:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
The issue in comment #2 happens in a couple of places when compiling linux
kernel (with named address spaces enabled). However, the issue is not specific
to named AS, I was just more attentive to moves from %gs: prefixed locations.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (4 preceding siblings ...)
2023-12-21 13:02 ` ubizjak at gmail dot com
@ 2023-12-21 13:29 ` 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
7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-21 13:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|c |rtl-optimization
Ever confirmed|0 |1
Keywords| |missed-optimization
Last reconfirmed| |2023-12-21
Status|UNCONFIRMED |NEW
--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Uroš Bizjak from comment #4)
> (In reply to Richard Biener from comment #3)
> > The situation with address-spaces isn't valid as we need to preserve the
> > second load because it's volatile. I think we simply refuse to combine
> > volatile loads out of caution in the first case.
>
> int __seg_gs b;
> return *(volatile __seg_gs int *) &b + b;
>
> But the above is the same w.r.t to volatile as:
>
> int a;
> return *(volatile int *) &a + a;
>
> ?
>
> BTW: I also checked with clang, and it creates expected code in all cases.
But you don't get
movl %gs:b(%rip), %eax
addl %eax, %eax
or
movl b(%rip), %eax
addl %eax, %eax
which I think would be wrong. The volatile access doesn't need to yield
the same value as the non-volatile one so we can't value-number them the
same.
The combine issue remains of course. But GCCs point was always that
trying to optimize volatile is wasted time.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (5 preceding siblings ...)
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
7 siblings, 0 replies; 9+ messages in thread
From: ubizjak at gmail dot com @ 2023-12-21 15:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #7 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Richard Biener from comment #6)
> > BTW: I also checked with clang, and it creates expected code in all cases.
>
> But you don't get
>
> movl %gs:b(%rip), %eax
> addl %eax, %eax
>
> or
>
> movl b(%rip), %eax
> addl %eax, %eax
>
> which I think would be wrong. The volatile access doesn't need to yield
> the same value as the non-volatile one so we can't value-number them the
> same.
The above is the code that clang produces for the testcases in Comment #2 and
Comment #0.
clang version 15.0.7 (Fedora 15.0.7-2.fc37)
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug rtl-optimization/113106] Missing CSE with cast to volatile
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
` (6 preceding siblings ...)
2023-12-21 15:08 ` ubizjak at gmail dot com
@ 2023-12-21 15:11 ` pinskia at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-21 15:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113106
--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Uroš Bizjak from comment #1)
> Perhaps related,
>
> --cut here--
> int a;
>
> int foo(void)
> {
> return *(volatile int *) &a + *(volatile int *) &a;
> }
> --cut here--
>
> compiles with -O2 to:
>
> movl a(%rip), %eax
> movl a(%rip), %edx
> addl %edx, %eax
> ret
>
> but may be compiled to:
>
> movl a(%rip), %eax
> addl a(%rip), %eax
> ret
>
> (the memory read may propagate to the insn)
That is pr 3506
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2023-12-21 15:11 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-21 10:38 [Bug c/113106] New: Missing CSE with cast to volatile ubizjak at gmail dot com
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
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).